Added invoices endpoint

This commit is contained in:
Nedim
2023-09-27 19:20:44 +02:00
parent 937c61c635
commit 60042ebd30
9 changed files with 342 additions and 88 deletions

View File

@@ -7,54 +7,97 @@ import (
"time"
"github.com/jinzhu/gorm"
"github.com/lib/pq"
"gitlab.com/pactual1/backend/models"
"gitlab.com/pactual1/backend/shared"
)
func GetContracts(status string, companyName string, startTime time.Time, deviceIDs []int64,limit, offset int) ([]models.Contract, int64, int, error) {
// Create a slice to hold the contracts
var contracts []models.Contract
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()
// Initialize the query
db := shared.GetDb().Where("status = ?", status)
countDb := db
countDb := db
if companyName != "" {
db = db.Joins("left join companies on companies.id = contracts.buyer_id").Where("companies.name LIKE ?", "%"+companyName+"%")
countDb = countDb.Joins("left join companies on companies.id = contracts.buyer_id").Where("companies.name LIKE ?", "%"+companyName+"%")
}
if !startTime.IsZero() {
db = db.Where("start_time >= ?", startTime)
countDb = countDb.Where("start_time >= ?", startTime)
}
if len(deviceIDs) > 0 {
db = db.Where("device_ids && ARRAY[?]::int8[]", deviceIDs)
countDb = countDb.Where("device_ids && ARRAY[?]::int8[]", deviceIDs)
// Search by Status
if status != "" {
db = shared.GetDb().Where("contracts.status = ?", status)
countDb = countDb.Where("contracts.status = ?", status)
}
// 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")
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+"%")
}
}
// Search by Contract Name
if contractName != "" {
db = db.Where("contracts.name LIKE ?", "%"+contractName+"%")
countDb = countDb.Where("contracts.name LIKE ?", "%"+contractName+"%")
}
// Count the total number of contracts
var total int64
if err := countDb.Model(&models.Contract{}).Count(&total).Error; err != nil {
log.Printf("GetLatestContracts Error: Database error: %v", err)
return contracts, total, http.StatusInternalServerError, err
}
// Search by Start Time
if !startTime.IsZero() {
db = db.Where("start_time >= ?", startTime)
countDb = countDb.Where("start_time >= ?", startTime)
}
// Fetch the latest contracts from the database with LIMIT and OFFSET
if err := db.Order("created_at desc").Limit(limit).Offset(offset).Find(&contracts).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
log.Printf("GetLatestContracts Error: No contracts found: %v", err)
return contracts, total, http.StatusNotFound, err
} else {
log.Printf("GetLatestContracts Error: Database error: %v", err)
return contracts, total, http.StatusInternalServerError, err
}
}
return contracts, total, http.StatusOK, nil
// Search by End Time
if !endTime.IsZero() {
db = db.Where("end_time <= ?", endTime)
countDb = countDb.Where("end_time <= ?", endTime)
}
// 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
}
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
}
}
return contracts, total, http.StatusOK, nil
}
func GetContractByID(contractID uint64) (models.Contract, int ,error) {
// Fetch the contract creation date based on contractID

View File

@@ -0,0 +1,61 @@
package invoice
import (
"fmt"
"strings"
"gitlab.com/pactual1/backend/models"
"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
// Default sort by InvoiceDate DESC
sortField := "invoice_date"
sortOrder := "desc"
if sortBy == "InvoiceDate" || sortBy == "InvoiceDueDate" {
sortField = strings.ToLower(sortBy)
}
dbOrder := fmt.Sprintf("%s %s", sortField, sortOrder)
db := shared.GetDb()
countDb := db
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)
}
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
for _, item := range invoices[i].InvoiceItem {
sum += item.PriceCents * item.Quantity
}
invoices[i].PriceCents = sum
}
return invoices, total, nil
}