123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- 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
- }
|