Upstream sync
This commit is contained in:
48
database/company/company.go
Normal file
48
database/company/company.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package company
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
"gitlab.com/pactual1/backend/models"
|
||||
"gitlab.com/pactual1/backend/shared"
|
||||
)
|
||||
|
||||
func GetCompanies(companyIDs []int64, limit, offset int) ([]models.Company, int64, int, error) {
|
||||
|
||||
companies := []models.Company{}
|
||||
db := shared.GetDb()
|
||||
countDb := db
|
||||
|
||||
// Search by IDs
|
||||
if len(companyIDs) > 0 {
|
||||
db = db.Where("companies.id IN (?)", companyIDs)
|
||||
countDb = countDb.Where("companies.id IN (?)", companyIDs)
|
||||
}
|
||||
|
||||
// Fetch total count of filtered records
|
||||
var total int64
|
||||
if err := countDb.Model(&models.Company{}).Count(&total).Error; err != nil {
|
||||
log.Printf("GetCompanies Error: Database error: %v", err)
|
||||
return companies, total, http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
// Fetch companies with custom fields
|
||||
if err := db.Select("*").
|
||||
Order("created_at desc").
|
||||
Limit(limit).
|
||||
Offset(offset).
|
||||
Find(&companies).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
log.Printf("GetCompanies Error: No companies found: %v", err)
|
||||
return companies, total, http.StatusNotFound, err
|
||||
} else {
|
||||
log.Printf("GetCompanies Error: Database error: %v", err)
|
||||
return companies, total, http.StatusInternalServerError, err
|
||||
}
|
||||
}
|
||||
|
||||
return companies, total, http.StatusOK, nil
|
||||
}
|
||||
@@ -12,17 +12,16 @@ 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, contractID int, dateCreated *time.Time, limit, offset int) ([]models.Contract, int64, int, error) {
|
||||
contractName string, deviceIDs []int64, contractIDs []int64, dateCreated *time.Time, limit, offset int) ([]models.Contract, int64, int, error) {
|
||||
|
||||
var contracts []models.Contract
|
||||
db := shared.GetDb()
|
||||
countDb := db
|
||||
|
||||
// Define custom fields to be selected, varies based on joined tables
|
||||
customFields := "contracts.*, array_length(contracts.device_ids, 1) as number_of_devices"
|
||||
customFields := "distinct contracts.*, array_length(contracts.device_ids, 1) as number_of_devices"
|
||||
|
||||
// Search by Statuses
|
||||
if len(status) > 0 {
|
||||
@@ -30,6 +29,12 @@ func GetContracts(status []string, companyName string, companyAddress string,
|
||||
countDb = countDb.Where("contracts.status IN (?)", status)
|
||||
}
|
||||
|
||||
// Search by IDs
|
||||
if len(contractIDs) > 0 {
|
||||
db = db.Where("contracts.id IN (?)", contractIDs)
|
||||
countDb = countDb.Where("contracts.id IN (?)", contractIDs)
|
||||
}
|
||||
|
||||
// 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")
|
||||
@@ -57,14 +62,8 @@ func GetContracts(status []string, companyName string, companyAddress string,
|
||||
countDb = countDb.Where("contracts.name LIKE ?", "%"+contractName+"%")
|
||||
}
|
||||
|
||||
// Search by Contract Name
|
||||
if contractID != 0{
|
||||
db = db.Where("contracts.id = ?", contractID)
|
||||
countDb = countDb.Where("contracts.id = ?", contractID)
|
||||
}
|
||||
|
||||
// Search by Start Time and End Time
|
||||
if startTime != nil &&!startTime.IsZero() {
|
||||
if startTime != nil && !startTime.IsZero() {
|
||||
db = db.Where("start_time >= ?", startTime)
|
||||
countDb = countDb.Where("start_time >= ?", startTime)
|
||||
}
|
||||
@@ -76,10 +75,9 @@ func GetContracts(status []string, companyName string, companyAddress string,
|
||||
|
||||
if dateCreated != nil && !dateCreated.IsZero() {
|
||||
db = db.Where("contracts.created_at = ?", dateCreated)
|
||||
countDb = countDb.Where("created_at = ?", dateCreated)
|
||||
countDb = countDb.Where("contracts.created_at = ?", dateCreated)
|
||||
}
|
||||
|
||||
|
||||
// Search by Device IDs
|
||||
if len(deviceIDs) > 0 {
|
||||
db = db.Where("device_ids && ?", pq.Array(deviceIDs))
|
||||
@@ -114,7 +112,7 @@ func GetContracts(status []string, companyName string, companyAddress string,
|
||||
func UpdateContract(contract models.Contract) (models.Contract, int, error) {
|
||||
|
||||
// Update contract
|
||||
if err := shared.GetDb().Update(contract).Error; err != nil {
|
||||
if err := shared.GetDb().Model(contract).Updates(contract).Error; err != nil {
|
||||
log.Printf("UpdateContractByID Error: Could not update contract: %v", err)
|
||||
return contract, http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
@@ -8,55 +8,55 @@ import (
|
||||
"gitlab.com/pactual1/backend/shared"
|
||||
)
|
||||
|
||||
func GetInvoices(buyerName string, sortBy string, limit int, offset int, id uint) ([]models.Invoice, int64, error) {
|
||||
var invoices []models.Invoice
|
||||
func GetInvoices(buyerName string, sortBy string, limit int, offset int, ids []int64) ([]models.Invoice, int64, error) {
|
||||
var invoices []models.Invoice
|
||||
|
||||
// Default sort by InvoiceDate DESC
|
||||
sortField := "invoice_date"
|
||||
sortOrder := "desc"
|
||||
// Default sort by InvoiceDate DESC
|
||||
sortField := "invoice_date"
|
||||
sortOrder := "desc"
|
||||
|
||||
if sortBy == "InvoiceDate" || sortBy == "InvoiceDueDate" {
|
||||
sortField = strings.ToLower(sortBy)
|
||||
}
|
||||
if sortBy == "InvoiceDate" || sortBy == "InvoiceDueDate" {
|
||||
sortField = strings.ToLower(sortBy)
|
||||
}
|
||||
|
||||
dbOrder := fmt.Sprintf("%s %s", sortField, sortOrder)
|
||||
dbOrder := fmt.Sprintf("%s %s", sortField, sortOrder)
|
||||
|
||||
db := shared.GetDb()
|
||||
countDb := db
|
||||
db := shared.GetDb()
|
||||
countDb := db
|
||||
|
||||
if buyerName != "" {
|
||||
db = db.Where("buyer_name LIKE ?", "%"+buyerName+"%")
|
||||
countDb = countDb.Where("buyer_name LIKE ?", "%"+buyerName+"%")
|
||||
}
|
||||
if buyerName != "" {
|
||||
db = db.Where("buyer_name LIKE ?", "%"+buyerName+"%")
|
||||
countDb = countDb.Where("buyer_name LIKE ?", "%"+buyerName+"%")
|
||||
}
|
||||
|
||||
// Added conditional for ID search
|
||||
if id != 0 {
|
||||
db = db.Where("id = ?", id)
|
||||
countDb = countDb.Where("id = ?", id)
|
||||
}
|
||||
// Added conditional for ID search
|
||||
if len(ids) > 0 {
|
||||
db = db.Where("id in (?)", ids)
|
||||
countDb = countDb.Where("id in (?)", ids)
|
||||
}
|
||||
|
||||
var total int64
|
||||
if err := countDb.Model(&models.Invoice{}).Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
var total int64
|
||||
if err := countDb.Model(&models.Invoice{}).Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if err := db.Preload("InvoiceItem").
|
||||
Order(dbOrder).
|
||||
Limit(limit).
|
||||
Offset(offset).
|
||||
Find(&invoices).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
for i := range invoices {
|
||||
var sum int64
|
||||
if invoices[i].InvoiceItem != nil {
|
||||
for _, item := range *invoices[i].InvoiceItem {
|
||||
sum += item.PriceCents * item.Quantity
|
||||
}
|
||||
invoices[i].PriceCents = sum
|
||||
}
|
||||
}
|
||||
if err := db.Preload("InvoiceItem").
|
||||
Order(dbOrder).
|
||||
Limit(limit).
|
||||
Offset(offset).
|
||||
Find(&invoices).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return invoices, total, nil
|
||||
for i := range invoices {
|
||||
var sum int64
|
||||
if invoices[i].InvoiceItem != nil {
|
||||
for _, item := range *invoices[i].InvoiceItem {
|
||||
sum += item.PriceCents * item.Quantity
|
||||
}
|
||||
invoices[i].PriceCents = sum
|
||||
}
|
||||
}
|
||||
|
||||
return invoices, total, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user