corrections in stockrequest table
This commit is contained in:
@@ -8,7 +8,7 @@ import (
|
||||
|
||||
type StockRequestRepository interface {
|
||||
CreateStockRequest(req *models.StockRequest) error
|
||||
GetStockRequests(tenantID int, locationID int, status string, pageNo int, pageSize int) ([]models.StockRequest, error)
|
||||
GetStockRequests(tenantID int, locationID int, status string, date string, pageNo int, pageSize int) ([]models.StockRequest, error)
|
||||
GetStockRequestByID(requestID int) (*models.StockRequest, error)
|
||||
UpdateStockRequest(requestID int, status string) error
|
||||
}
|
||||
@@ -22,10 +22,30 @@ func NewStockRequestRepository(db *gorm.DB) StockRequestRepository {
|
||||
}
|
||||
|
||||
func (r *stockRequestRepository) CreateStockRequest(req *models.StockRequest) error {
|
||||
// Fetch product details
|
||||
var prod struct {
|
||||
Productname string
|
||||
Productimage string
|
||||
}
|
||||
r.db.Table("products").Select("productname, productimage").Where("productid = ?", req.Productid).Scan(&prod)
|
||||
if req.Productname == "" {
|
||||
req.Productname = prod.Productname
|
||||
}
|
||||
if req.Productimage == "" {
|
||||
req.Productimage = prod.Productimage
|
||||
}
|
||||
|
||||
// Fetch tenant name
|
||||
var tenantName string
|
||||
r.db.Table("tenants").Select("tenantname").Where("tenantid = ?", req.Tenantid).Scan(&tenantName)
|
||||
if req.Tenantname == "" {
|
||||
req.Tenantname = tenantName
|
||||
}
|
||||
|
||||
return r.db.Create(req).Error
|
||||
}
|
||||
|
||||
func (r *stockRequestRepository) GetStockRequests(tenantID int, locationID int, status string, pageNo int, pageSize int) ([]models.StockRequest, error) {
|
||||
func (r *stockRequestRepository) GetStockRequests(tenantID int, locationID int, status string, date string, pageNo int, pageSize int) ([]models.StockRequest, error) {
|
||||
var requests []models.StockRequest
|
||||
query := r.db.Table("stockrequests").
|
||||
Select("stockrequests.*, products.productname, products.productimage").
|
||||
@@ -40,6 +60,10 @@ func (r *stockRequestRepository) GetStockRequests(tenantID int, locationID int,
|
||||
query = query.Where("stockrequests.status = ?", status)
|
||||
}
|
||||
|
||||
if date != "" {
|
||||
query = query.Where("DATE(stockrequests.created) = ?", date)
|
||||
}
|
||||
|
||||
offset := (pageNo - 1) * pageSize
|
||||
err := query.Order("stockrequests.created DESC").Offset(offset).Limit(pageSize).Find(&requests).Error
|
||||
return requests, err
|
||||
@@ -47,7 +71,12 @@ func (r *stockRequestRepository) GetStockRequests(tenantID int, locationID int,
|
||||
|
||||
func (r *stockRequestRepository) GetStockRequestByID(requestID int) (*models.StockRequest, error) {
|
||||
var req models.StockRequest
|
||||
err := r.db.Table("stockrequests").Where("requestid = ?", requestID).First(&req).Error
|
||||
err := r.db.Table("stockrequests").
|
||||
Select("stockrequests.*, products.productname, products.productimage, tenants.tenantname, tenantlocations.locationname").
|
||||
Joins("left join products on products.productid = stockrequests.productid").
|
||||
Joins("left join tenants on tenants.tenantid = stockrequests.tenantid").
|
||||
Joins("left join tenantlocations on tenantlocations.locationid = stockrequests.locationid").
|
||||
Where("requestid = ?", requestID).First(&req).Error
|
||||
return &req, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user