Contracts for buyer
This commit is contained in:
@@ -12,87 +12,103 @@ import (
|
||||
"gitlab.com/pactual1/backend/shared"
|
||||
)
|
||||
|
||||
func GetContracts(status string, companyName string, companyAddress string,
|
||||
companyEmail string, companyPhone string, startTime time.Time, endTime time.Time,
|
||||
contractName string, deviceIDs []int64, limit, offset int) ([]models.Contract, int64, int, error) {
|
||||
var contracts []models.Contract
|
||||
db := shared.GetDb()
|
||||
|
||||
countDb := db
|
||||
func GetContracts(status string, companyName string, companyAddress string,
|
||||
companyEmail string, companyPhone string, startTime *time.Time, endTime *time.Time,
|
||||
contractName string, deviceIDs []int64, contractID int, dateCreated *time.Time, limit, offset int) ([]models.Contract, int64, int, error) {
|
||||
|
||||
// Search by Status
|
||||
if status != "" {
|
||||
db = shared.GetDb().Where("contracts.status = ?", status)
|
||||
countDb = countDb.Where("contracts.status = ?", status)
|
||||
}
|
||||
var contracts []models.Contract
|
||||
db := shared.GetDb()
|
||||
countDb := db
|
||||
|
||||
// Search by Company Fields
|
||||
if companyName != "" || companyAddress != "" || companyEmail != "" || companyPhone != "" {
|
||||
db = db.Joins("left join companies on companies.id = contracts.buyer_id")
|
||||
countDb = countDb.Joins("left join companies on companies.id = contracts.buyer_id")
|
||||
// Define custom fields to be selected, varies based on joined tables
|
||||
customFields := "contracts.*, array_length(contracts.device_ids, 1) as number_of_devices"
|
||||
|
||||
if companyName != "" {
|
||||
db = db.Where("companies.name LIKE ?", "%"+companyName+"%")
|
||||
countDb = countDb.Where("companies.name LIKE ?", "%"+companyName+"%")
|
||||
}
|
||||
// Search by Status
|
||||
if status != "" {
|
||||
db = db.Where("contracts.status = ?", status)
|
||||
countDb = countDb.Where("contracts.status = ?", status)
|
||||
}
|
||||
|
||||
if companyAddress != "" {
|
||||
db = db.Where("companies.address LIKE ?", "%"+companyAddress+"%")
|
||||
countDb = countDb.Where("companies.address LIKE ?", "%"+companyAddress+"%")
|
||||
}
|
||||
// Search by Company Fields
|
||||
db = db.Joins("left join companies on companies.id = contracts.buyer_id")
|
||||
countDb = countDb.Joins("left join companies on companies.id = contracts.buyer_id")
|
||||
customFields += ", companies.name as buyer_name"
|
||||
if companyName != "" {
|
||||
db = db.Where("companies.name LIKE ?", "%"+companyName+"%")
|
||||
countDb = countDb.Where("companies.name LIKE ?", "%"+companyName+"%")
|
||||
}
|
||||
if companyAddress != "" {
|
||||
db = db.Where("companies.address LIKE ?", "%"+companyAddress+"%")
|
||||
countDb = countDb.Where("companies.address LIKE ?", "%"+companyAddress+"%")
|
||||
}
|
||||
if companyEmail != "" {
|
||||
db = db.Where("companies.email LIKE ?", "%"+companyEmail+"%")
|
||||
countDb = countDb.Where("companies.email LIKE ?", "%"+companyEmail+"%")
|
||||
}
|
||||
if companyPhone != "" {
|
||||
db = db.Where("companies.phone LIKE ?", "%"+companyPhone+"%")
|
||||
countDb = countDb.Where("companies.phone LIKE ?", "%"+companyPhone+"%")
|
||||
}
|
||||
|
||||
if companyEmail != "" {
|
||||
db = db.Where("companies.email LIKE ?", "%"+companyEmail+"%")
|
||||
countDb = countDb.Where("companies.email LIKE ?", "%"+companyEmail+"%")
|
||||
}
|
||||
|
||||
if companyPhone != "" {
|
||||
db = db.Where("companies.phone LIKE ?", "%"+companyPhone+"%")
|
||||
countDb = countDb.Where("companies.phone LIKE ?", "%"+companyPhone+"%")
|
||||
}
|
||||
}
|
||||
|
||||
// Search by Contract Name
|
||||
// Search by Contract Name
|
||||
if contractName != "" {
|
||||
db = db.Where("contracts.name LIKE ?", "%"+contractName+"%")
|
||||
countDb = countDb.Where("contracts.name LIKE ?", "%"+contractName+"%")
|
||||
}
|
||||
|
||||
// Search by Start Time
|
||||
if !startTime.IsZero() {
|
||||
db = db.Where("start_time >= ?", startTime)
|
||||
countDb = countDb.Where("start_time >= ?", startTime)
|
||||
}
|
||||
// Search by Contract Name
|
||||
if contractID != 0{
|
||||
db = db.Where("contracts.id = ?", contractID)
|
||||
countDb = countDb.Where("contracts.id = ?", contractID)
|
||||
}
|
||||
|
||||
// Search by End Time
|
||||
if !endTime.IsZero() {
|
||||
db = db.Where("end_time <= ?", endTime)
|
||||
countDb = countDb.Where("end_time <= ?", endTime)
|
||||
}
|
||||
// Search by Start Time and End Time
|
||||
if startTime != nil &&!startTime.IsZero() {
|
||||
db = db.Where("start_time >= ?", startTime)
|
||||
countDb = countDb.Where("start_time >= ?", startTime)
|
||||
}
|
||||
|
||||
// Search by Device IDs
|
||||
if len(deviceIDs) > 0 {
|
||||
if endTime != nil && !startTime.IsZero() {
|
||||
db = db.Where("end_time <= ?", endTime)
|
||||
countDb = countDb.Where("end_time <= ?", endTime)
|
||||
}
|
||||
|
||||
if dateCreated != nil && !dateCreated.IsZero() {
|
||||
db = db.Where("contracts.created_at = ?", dateCreated)
|
||||
countDb = countDb.Where("created_at = ?", dateCreated)
|
||||
}
|
||||
|
||||
|
||||
// Search by Device IDs
|
||||
if len(deviceIDs) > 0 {
|
||||
db = db.Where("device_ids && ?", pq.Array(deviceIDs))
|
||||
countDb = countDb.Where("device_ids && ?", pq.Array(deviceIDs))
|
||||
}
|
||||
}
|
||||
|
||||
var total int64
|
||||
if err := countDb.Model(&models.Contract{}).Count(&total).Error; err != nil {
|
||||
log.Printf("GetContracts Error: Database error: %v", err)
|
||||
return contracts, total, http.StatusInternalServerError, err
|
||||
}
|
||||
// Fetch total count of filtered records
|
||||
var total int64
|
||||
if err := countDb.Model(&models.Contract{}).Count(&total).Error; err != nil {
|
||||
log.Printf("GetContracts Error: Database error: %v", err)
|
||||
return contracts, total, http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
if err := db.Order("created_at desc").Limit(limit).Offset(offset).Find(&contracts).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
log.Printf("GetContracts Error: No contracts found: %v", err)
|
||||
return contracts, total, http.StatusNotFound, err
|
||||
} else {
|
||||
log.Printf("GetContracts Error: Database error: %v", err)
|
||||
return contracts, total, http.StatusInternalServerError, err
|
||||
}
|
||||
}
|
||||
// Fetch contracts with custom fields
|
||||
if err := db.Select(customFields).
|
||||
Order("created_at desc").
|
||||
Limit(limit).
|
||||
Offset(offset).
|
||||
Find(&contracts).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
log.Printf("GetContracts Error: No contracts found: %v", err)
|
||||
return contracts, total, http.StatusNotFound, err
|
||||
} else {
|
||||
log.Printf("GetContracts Error: Database error: %v", err)
|
||||
return contracts, total, http.StatusInternalServerError, err
|
||||
}
|
||||
}
|
||||
|
||||
return contracts, total, http.StatusOK, nil
|
||||
return contracts, total, http.StatusOK, nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -58,4 +58,3 @@ func GetInvoices(buyerName string, sortBy string, limit int, offset int, id uint
|
||||
|
||||
return invoices, total, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user