package services import ( cDto "Medical_ERP/common/dto" "Medical_ERP/common/global" db "Medical_ERP/common/initialize" "Medical_ERP/dto" "Medical_ERP/models" "Medical_ERP/utils" "github.com/beego/beego/v2/core/logs" "strconv" ) type Sales struct { } // SalesList 销售管理列表 包含销售数量、购进单价、销售单价、销售单价、销售金额 func (e *Sales) SalesList(c *dto.SalesPageReq, deptId int) (list []map[string]interface{}, count int64, err error) { mtable := models.GetMedicineInfoTableName(deptId) whereSql := "stock_out.dept_id = " + strconv.Itoa(deptId) + " AND state = 1 AND stock_out.deleted_at is null" if c.ProductID > 0 { whereSql += " AND " + mtable + ".product_id = " + strconv.Itoa(c.ProductID) } if c.EnterpriseID > 0 { whereSql += " AND " + mtable + ".enterprise_id = " + strconv.Itoa(c.EnterpriseID) } if len(c.BatchNumber) > 0 { whereSql += " AND " + mtable + ".batch_number = " + c.BatchNumber } if len(c.StartDate) > 0 { whereSql += " AND stock_out.date >= '" + c.StartDate + "'" } if len(c.EndDate) > 0 { whereSql += " AND stock_out.date <= '" + c.EndDate + "'" } if len(c.ReceivingUnit) > 0 { whereSql += " AND stock_out.receiving_unit like '%" + c.ReceivingUnit + "%'" } err = db.DB.Table("stock_out"). Scopes( cDto.Paginate(c.GetPageSize(), c.GetPageIndex()), ). Joins("left join " + mtable + " on stock_out.medicine_id = " + mtable + ".id"). Where(whereSql). Scan(&list).Limit(-1).Offset(-1). Count(&count).Error if err != nil { logs.Error("db error: %s ", err) return list, count, global.GetFailedErr } models.InitBasicData(deptId) for i := 0; i < len(list); i++ { list[i]["unit_price"] = utils.ToFloat64(list[i]["unit_price"]) // 金额 list[i]["money"] = utils.ToFloat64(list[i]["unit_price"]) * utils.ToFloat64(list[i]["quantity"]) if id, ok := list[i][models.FieldProductID]; ok { list[i][models.FieldProductName] = models.Read_Product_Get(utils.ToInt(id)) } if id, ok := list[i][models.FieldEnterpriseID]; ok { list[i][models.FieldEnterpriseName] = models.Read_Enterprise_Get(utils.ToInt(id)) } if id, ok := list[i][models.FieldSpecID]; ok { list[i][models.FieldSpecName] = models.Read_Spec_Get(utils.ToInt(id)) } if id, ok := list[i][models.FieldUnitID]; ok { list[i][models.FieldUnitName] = models.Read_Unit_Get(utils.ToInt(id)) } if id, ok := list[i][models.FieldDosageFormID]; ok { list[i][models.FieldDosageFormName] = models.Read_DosageForm_Get(utils.ToInt(id)) } } return list, count, nil } func (e *Sales) SalesListExcel(c *dto.SalesPageReq, deptId int) (list []map[string]interface{}, count int64, err error) { mtable := models.GetMedicineInfoTableName(deptId) whereSql := "stock_out.dept_id = " + strconv.Itoa(deptId) + " AND state = 1 AND stock_out.deleted_at is null" if c.ProductID > 0 { whereSql += " AND " + mtable + ".product_id = " + strconv.Itoa(c.ProductID) } if c.EnterpriseID > 0 { whereSql += " AND " + mtable + ".enterprise_id = " + strconv.Itoa(c.EnterpriseID) } if len(c.BatchNumber) > 0 { whereSql += " AND " + mtable + ".batch_number = " + c.BatchNumber } if len(c.StartDate) > 0 { whereSql += " AND stock_out.date >= '" + c.StartDate + "'" } if len(c.EndDate) > 0 { whereSql += " AND stock_out.date <= '" + c.EndDate + "'" } if len(c.ReceivingUnit) > 0 { whereSql += " AND stock_out.receiving_unit like '%" + c.ReceivingUnit + "%'" } err = db.DB.Table("stock_out"). Joins("left join " + mtable + " on stock_out.medicine_id = " + mtable + ".id"). Where(whereSql). Scan(&list).Error if err != nil { logs.Error("db error: %s ", err) return list, count, global.GetFailedErr } models.InitBasicData(deptId) for i := 0; i < len(list); i++ { list[i]["unit_price"] = utils.ToFloat64(list[i]["unit_price"]) // 金额 list[i]["money"] = utils.ToFloat64(list[i]["unit_price"]) * utils.ToFloat64(list[i]["quantity"]) if id, ok := list[i][models.FieldProductID]; ok { list[i][models.FieldProductName] = models.Read_Product_Get(utils.ToInt(id)) } if id, ok := list[i][models.FieldEnterpriseID]; ok { list[i][models.FieldEnterpriseName] = models.Read_Enterprise_Get(utils.ToInt(id)) } if id, ok := list[i][models.FieldSpecID]; ok { list[i][models.FieldSpecName] = models.Read_Spec_Get(utils.ToInt(id)) } if id, ok := list[i][models.FieldUnitID]; ok { list[i][models.FieldUnitName] = models.Read_Unit_Get(utils.ToInt(id)) } if id, ok := list[i][models.FieldDosageFormID]; ok { list[i][models.FieldDosageFormName] = models.Read_DosageForm_Get(utils.ToInt(id)) } } return list, count, nil } // 销售报表 func (e *Sales) SalesReportList(c *dto.SalesPageReq, deptId int) (list []map[string]interface{}, err error) { mtable := models.GetMedicineInfoTableName(deptId) whereSql := "stock_out.dept_id = " + strconv.Itoa(deptId) + " AND state = 1 AND stock_out.deleted_at is null" if c.ProductID > 0 { whereSql += " AND " + mtable + ".product_id = " + strconv.Itoa(c.ProductID) } if len(c.StartDate) > 0 { whereSql += " AND stock_out.date >= '" + c.StartDate + "'" } if len(c.EndDate) > 0 { whereSql += " AND stock_out.date <= '" + c.EndDate + "'" } if len(c.ReceivingUnit) > 0 { whereSql += " AND stock_out.receiving_unit like '%" + c.ReceivingUnit + "%'" } err = db.DB.Table("stock_out"). Select(mtable + ".*,SUM(quantity) AS total"). Scopes( cDto.Paginate(c.GetPageSize(), c.GetPageIndex()), ). Joins("left join " + mtable + " on stock_out.medicine_id = " + mtable + ".id"). Where(whereSql). Group("stock_out.medicine_id"). Scan(&list).Error if err != nil { logs.Error("db error: %s ", err) return list, global.GetFailedErr } models.InitBasicData(deptId) for i := 0; i < len(list); i++ { list[i]["unit_price"] = utils.ToFloat64(list[i]["unit_price"]) // 金额 list[i]["money"] = utils.ToFloat64(list[i]["unit_price"]) * utils.ToFloat64(list[i]["total"]) if id, ok := list[i][models.FieldProductID]; ok { list[i][models.FieldProductName] = models.Read_Product_Get(utils.ToInt(id)) } if id, ok := list[i][models.FieldEnterpriseID]; ok { list[i][models.FieldEnterpriseName] = models.Read_Enterprise_Get(utils.ToInt(id)) } if id, ok := list[i][models.FieldSpecID]; ok { list[i][models.FieldSpecName] = models.Read_Spec_Get(utils.ToInt(id)) } if id, ok := list[i][models.FieldUnitID]; ok { list[i][models.FieldUnitName] = models.Read_Unit_Get(utils.ToInt(id)) } if id, ok := list[i][models.FieldDosageFormID]; ok { list[i][models.FieldDosageFormName] = models.Read_DosageForm_Get(utils.ToInt(id)) } } return list, nil } // SalesOrderList 销售订单列表 包含销售数量、销售单价、销售金额 func (e *Sales) SalesOrderList(c *dto.SalesOrderPageReq, deptId int) (list []map[string]interface{}, count int64, err error) { mtable := models.GetMedicineInfoTableName(deptId) whereSql := "stock_out.dept_id = " + strconv.Itoa(deptId) + " AND state = 1 AND stock_out.deleted_at is null" if c.ProductID > 0 { whereSql += " AND " + mtable + ".product_id = " + strconv.Itoa(c.ProductID) } if c.EnterpriseID > 0 { whereSql += " AND " + mtable + ".enterprise_id = " + strconv.Itoa(c.EnterpriseID) } if len(c.BatchNumber) > 0 { whereSql += " AND " + mtable + ".batch_number = " + c.BatchNumber } if len(c.Date) > 0 { whereSql += " AND stock_out.date = '" + c.Date + "'" } if len(c.ReceivingUnit) > 0 { whereSql += " AND stock_out.receiving_unit like '%" + c.ReceivingUnit + "%'" } err = db.DB.Table("stock_out"). //Select(mtable + ".*,stock_out.*,stock_out.unit_price as sales_unit_price"). Scopes( cDto.Paginate(c.GetPageSize(), c.GetPageIndex()), ). Joins("left join " + mtable + " on stock_out.medicine_id = " + mtable + ".id"). Where(whereSql). Scan(&list).Limit(-1).Offset(-1). Count(&count).Error if err != nil { logs.Error("db error: %s ", err) return list, count, global.GetFailedErr } models.InitBasicData(deptId) for i := 0; i < len(list); i++ { list[i]["sales_unit_price"] = utils.ToFloat64(list[i]["unit_price"]) list[i]["sales_money"] = utils.ToFloat64(list[i]["unit_price"]) * utils.ToFloat64(list[i]["quantity"]) if id, ok := list[i][models.FieldProductID]; ok { list[i][models.FieldProductName] = models.Read_Product_Get(utils.ToInt(id)) } if id, ok := list[i][models.FieldEnterpriseID]; ok { list[i][models.FieldEnterpriseName] = models.Read_Enterprise_Get(utils.ToInt(id)) } if id, ok := list[i][models.FieldSpecID]; ok { list[i][models.FieldSpecName] = models.Read_Spec_Get(utils.ToInt(id)) } if id, ok := list[i][models.FieldUnitID]; ok { list[i][models.FieldUnitName] = models.Read_Unit_Get(utils.ToInt(id)) } if id, ok := list[i][models.FieldDosageFormID]; ok { list[i][models.FieldDosageFormName] = models.Read_DosageForm_Get(utils.ToInt(id)) } list[i][models.FieldExpiryDate] = utils.ToDate(list[i][models.FieldExpiryDate]) } return list, count, nil } // SalesStockOutExcel 销售订单列表 包含销售数量、销售单价、销售金额 func (e *Sales) SalesStockOutExcel(c *dto.SalesStockOutExcelReq, deptId int) (list []map[string]interface{}, err error) { mtable := models.GetMedicineInfoTableName(deptId) whereSql := "stock_out.dept_id = " + strconv.Itoa(deptId) + " AND state = 1 AND stock_out.deleted_at is null" if len(c.Date) > 0 { whereSql += " AND stock_out.date = '" + c.Date + "'" } if len(c.ReceivingUnit) > 0 { whereSql += " AND stock_out.receiving_unit like '%" + c.ReceivingUnit + "%'" } err = db.DB.Table("stock_out"). //Select(mtable + ".*,stock_out.*,stock_out.unit_price as sales_unit_price"). Joins("left join " + mtable + " on stock_out.medicine_id = " + mtable + ".id"). Where(whereSql). Scan(&list).Error if err != nil { logs.Error("db error: %s ", err) return list, global.GetFailedErr } models.InitBasicData(deptId) for i := 0; i < len(list); i++ { list[i]["sales_unit_price"] = utils.ToFloat64(list[i]["unit_price"]) list[i]["sales_money"] = utils.ToFloat64(list[i]["unit_price"]) * utils.ToFloat64(list[i]["quantity"]) if id, ok := list[i][models.FieldProductID]; ok { list[i][models.FieldProductName] = models.Read_Product_Get(utils.ToInt(id)) } if id, ok := list[i][models.FieldEnterpriseID]; ok { list[i][models.FieldEnterpriseName] = models.Read_Enterprise_Get(utils.ToInt(id)) } if id, ok := list[i][models.FieldSpecID]; ok { list[i][models.FieldSpecName] = models.Read_Spec_Get(utils.ToInt(id)) } if id, ok := list[i][models.FieldUnitID]; ok { list[i][models.FieldUnitName] = models.Read_Unit_Get(utils.ToInt(id)) } if id, ok := list[i][models.FieldDosageFormID]; ok { list[i][models.FieldDosageFormName] = models.Read_DosageForm_Get(utils.ToInt(id)) } } return list, nil }