sales.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package services
  2. import (
  3. cDto "Medical_ERP/common/dto"
  4. "Medical_ERP/common/global"
  5. db "Medical_ERP/common/initialize"
  6. "Medical_ERP/dto"
  7. "Medical_ERP/models"
  8. "Medical_ERP/utils"
  9. "github.com/beego/beego/v2/core/logs"
  10. "strconv"
  11. )
  12. type Sales struct {
  13. }
  14. // SalesList 销售管理列表 包含销售数量、购进单价、销售单价、销售单价、销售金额
  15. func (e *Sales) SalesList(c *dto.SalesPageReq, deptId int) (list []map[string]interface{}, count int64, err error) {
  16. mtable := models.GetMedicineInfoTableName(deptId)
  17. whereSql := "stock_out.dept_id = " + strconv.Itoa(deptId) + " AND unit_price > 0 AND stock_out.deleted_at is null"
  18. if c.ProductID > 0 {
  19. whereSql += " AND " + mtable + ".product_id = " + strconv.Itoa(c.ProductID)
  20. }
  21. if c.EnterpriseID > 0 {
  22. whereSql += " AND " + mtable + ".enterprise_id = " + strconv.Itoa(c.EnterpriseID)
  23. }
  24. if len(c.BatchNumber) > 0 {
  25. whereSql += " AND " + mtable + ".batch_number = " + c.BatchNumber
  26. }
  27. if len(c.StartDate) > 0 {
  28. whereSql += " AND stock_out.date >= '" + c.StartDate + "'"
  29. }
  30. if len(c.EndDate) > 0 {
  31. whereSql += " AND stock_out.date <= '" + c.EndDate + "'"
  32. }
  33. if len(c.ReceivingUnit) > 0 {
  34. whereSql += " AND stock_out.receiving_unit like '%" + c.ReceivingUnit + "%'"
  35. }
  36. err = db.DB.Table("stock_out").
  37. Scopes(
  38. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  39. ).
  40. Joins("left join " + mtable + " on stock_out.medicine_id = " + mtable + ".id").
  41. Where(whereSql).
  42. Scan(&list).Limit(-1).Offset(-1).
  43. Count(&count).Error
  44. if err != nil {
  45. logs.Error("db error: %s ", err)
  46. return list, count, global.GetFailedErr
  47. }
  48. models.InitBasicData(deptId)
  49. for i := 0; i < len(list); i++ {
  50. list[i]["purchase_unit_price"] = utils.ToFloat64(list[i]["purchase_unit_price"])
  51. list[i]["sales_unit_price"] = utils.ToFloat64(list[i]["sales_unit_price"])
  52. // 购进金额
  53. list[i]["purchase_money"] = utils.ToFloat64(list[i]["purchase_unit_price"]) * utils.ToFloat64(list[i]["quantity"])
  54. // 销售金额
  55. list[i]["sales_money"] = utils.ToFloat64(list[i]["sales_unit_price"]) * utils.ToFloat64(list[i]["quantity"])
  56. if id, ok := list[i][models.FieldProductID]; ok {
  57. list[i][models.FieldProductName] = models.Read_Product_Get(utils.ToInt(id))
  58. }
  59. if id, ok := list[i][models.FieldEnterpriseID]; ok {
  60. list[i][models.FieldEnterpriseName] = models.Read_Enterprise_Get(utils.ToInt(id))
  61. }
  62. if id, ok := list[i][models.FieldSpecID]; ok {
  63. list[i][models.FieldSpecName] = models.Read_Spec_Get(utils.ToInt(id))
  64. }
  65. if id, ok := list[i][models.FieldUnitID]; ok {
  66. list[i][models.FieldUnitName] = models.Read_Unit_Get(utils.ToInt(id))
  67. }
  68. if id, ok := list[i][models.FieldDosageFormID]; ok {
  69. list[i][models.FieldDosageFormName] = models.Read_DosageForm_Get(utils.ToInt(id))
  70. }
  71. }
  72. return list, count, nil
  73. }
  74. // 销售报表
  75. func (e *Sales) SalesReportList(c *dto.SalesPageReq, deptId int) (list []map[string]interface{}, err error) {
  76. mtable := models.GetMedicineInfoTableName(deptId)
  77. whereSql := "stock_out.dept_id = " + strconv.Itoa(deptId) + " AND unit_price > 0 AND stock_out.deleted_at is null"
  78. if c.ProductID > 0 {
  79. whereSql += " AND " + mtable + ".product_id = " + strconv.Itoa(c.ProductID)
  80. }
  81. if len(c.StartDate) > 0 {
  82. whereSql += " AND stock_out.date >= '" + c.StartDate + "'"
  83. }
  84. if len(c.EndDate) > 0 {
  85. whereSql += " AND stock_out.date <= '" + c.EndDate + "'"
  86. }
  87. if len(c.ReceivingUnit) > 0 {
  88. whereSql += " AND stock_out.receiving_unit like '%" + c.ReceivingUnit + "%'"
  89. }
  90. err = db.DB.Table("stock_out").
  91. Select(mtable + ".*,SUM(quantity) AS total").
  92. Scopes(
  93. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  94. ).
  95. Joins("left join " + mtable + " on stock_out.medicine_id = " + mtable + ".id").
  96. Where(whereSql).
  97. Group("stock_out.medicine_id").
  98. Scan(&list).Error
  99. if err != nil {
  100. logs.Error("db error: %s ", err)
  101. return list, global.GetFailedErr
  102. }
  103. models.InitBasicData(deptId)
  104. for i := 0; i < len(list); i++ {
  105. list[i]["purchase_unit_price"] = utils.ToFloat64(list[i]["purchase_unit_price"])
  106. list[i]["sales_unit_price"] = utils.ToFloat64(list[i]["sales_unit_price"])
  107. // 购进金额
  108. list[i]["purchase_money"] = utils.ToFloat64(list[i]["purchase_unit_price"]) * utils.ToFloat64(list[i]["total"])
  109. // 销售金额
  110. list[i]["sales_money"] = utils.ToFloat64(list[i]["sales_unit_price"]) * utils.ToFloat64(list[i]["total"])
  111. list[i]["profit_money"] = utils.ToFloat64(list[i]["sales_money"]) - utils.ToFloat64(list[i]["purchase_money"])
  112. if id, ok := list[i][models.FieldProductID]; ok {
  113. list[i][models.FieldProductName] = models.Read_Product_Get(utils.ToInt(id))
  114. }
  115. if id, ok := list[i][models.FieldEnterpriseID]; ok {
  116. list[i][models.FieldEnterpriseName] = models.Read_Enterprise_Get(utils.ToInt(id))
  117. }
  118. if id, ok := list[i][models.FieldSpecID]; ok {
  119. list[i][models.FieldSpecName] = models.Read_Spec_Get(utils.ToInt(id))
  120. }
  121. if id, ok := list[i][models.FieldUnitID]; ok {
  122. list[i][models.FieldUnitName] = models.Read_Unit_Get(utils.ToInt(id))
  123. }
  124. if id, ok := list[i][models.FieldDosageFormID]; ok {
  125. list[i][models.FieldDosageFormName] = models.Read_DosageForm_Get(utils.ToInt(id))
  126. }
  127. }
  128. return list, nil
  129. }
  130. // SalesOrderList 销售订单列表 包含销售数量、销售单价、销售金额
  131. func (e *Sales) SalesOrderList(c *dto.SalesOrderPageReq, deptId int) (list []map[string]interface{}, count int64, err error) {
  132. mtable := models.GetMedicineInfoTableName(deptId)
  133. whereSql := "stock_out.dept_id = " + strconv.Itoa(deptId) + " AND unit_price > 0 AND stock_out.deleted_at is null"
  134. if c.ProductID > 0 {
  135. whereSql += " AND " + mtable + ".product_id = " + strconv.Itoa(c.ProductID)
  136. }
  137. if c.EnterpriseID > 0 {
  138. whereSql += " AND " + mtable + ".enterprise_id = " + strconv.Itoa(c.EnterpriseID)
  139. }
  140. if len(c.BatchNumber) > 0 {
  141. whereSql += " AND " + mtable + ".batch_number = " + c.BatchNumber
  142. }
  143. if len(c.Date) > 0 {
  144. whereSql += " AND stock_out.date = '" + c.Date + "'"
  145. }
  146. if len(c.ReceivingUnit) > 0 {
  147. whereSql += " AND stock_out.receiving_unit like '%" + c.ReceivingUnit + "%'"
  148. }
  149. err = db.DB.Table("stock_out").
  150. Scopes(
  151. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  152. ).
  153. Joins("left join " + mtable + " on stock_out.medicine_id = " + mtable + ".id").
  154. Where(whereSql).
  155. Scan(&list).Limit(-1).Offset(-1).
  156. Count(&count).Error
  157. if err != nil {
  158. logs.Error("db error: %s ", err)
  159. return list, count, global.GetFailedErr
  160. }
  161. models.InitBasicData(deptId)
  162. for i := 0; i < len(list); i++ {
  163. list[i]["sales_money"] = utils.ToFloat64(list[i]["unit_price"]) * utils.ToFloat64(list[i]["quantity"])
  164. if id, ok := list[i][models.FieldProductID]; ok {
  165. list[i][models.FieldProductName] = models.Read_Product_Get(utils.ToInt(id))
  166. }
  167. if id, ok := list[i][models.FieldEnterpriseID]; ok {
  168. list[i][models.FieldEnterpriseName] = models.Read_Enterprise_Get(utils.ToInt(id))
  169. }
  170. if id, ok := list[i][models.FieldSpecID]; ok {
  171. list[i][models.FieldSpecName] = models.Read_Spec_Get(utils.ToInt(id))
  172. }
  173. if id, ok := list[i][models.FieldUnitID]; ok {
  174. list[i][models.FieldUnitName] = models.Read_Unit_Get(utils.ToInt(id))
  175. }
  176. if id, ok := list[i][models.FieldDosageFormID]; ok {
  177. list[i][models.FieldDosageFormName] = models.Read_DosageForm_Get(utils.ToInt(id))
  178. }
  179. }
  180. return list, count, nil
  181. }
  182. // SalesStockOutExcel 销售订单列表 包含销售数量、销售单价、销售金额
  183. func (e *Sales) SalesStockOutExcel(c *dto.SalesStockOutExcelReq, deptId int) (list []map[string]interface{}, err error) {
  184. mtable := models.GetMedicineInfoTableName(deptId)
  185. whereSql := "stock_out.dept_id = " + strconv.Itoa(deptId) + " AND unit_price > 0 AND stock_out.deleted_at is null"
  186. if len(c.Date) > 0 {
  187. whereSql += " AND stock_out.date = '" + c.Date + "'"
  188. }
  189. if len(c.ReceivingUnit) > 0 {
  190. whereSql += " AND stock_out.receiving_unit like '%" + c.ReceivingUnit + "%'"
  191. }
  192. err = db.DB.Table("stock_out").
  193. Joins("left join " + mtable + " on stock_out.medicine_id = " + mtable + ".id").
  194. Where(whereSql).
  195. Scan(&list).Error
  196. if err != nil {
  197. logs.Error("db error: %s ", err)
  198. return list, global.GetFailedErr
  199. }
  200. models.InitBasicData(deptId)
  201. for i := 0; i < len(list); i++ {
  202. list[i]["sales_money"] = utils.ToFloat64(list[i]["unit_price"]) * utils.ToFloat64(list[i]["quantity"])
  203. if id, ok := list[i][models.FieldProductID]; ok {
  204. list[i][models.FieldProductName] = models.Read_Product_Get(utils.ToInt(id))
  205. }
  206. if id, ok := list[i][models.FieldEnterpriseID]; ok {
  207. list[i][models.FieldEnterpriseName] = models.Read_Enterprise_Get(utils.ToInt(id))
  208. }
  209. if id, ok := list[i][models.FieldSpecID]; ok {
  210. list[i][models.FieldSpecName] = models.Read_Spec_Get(utils.ToInt(id))
  211. }
  212. if id, ok := list[i][models.FieldUnitID]; ok {
  213. list[i][models.FieldUnitName] = models.Read_Unit_Get(utils.ToInt(id))
  214. }
  215. if id, ok := list[i][models.FieldDosageFormID]; ok {
  216. list[i][models.FieldDosageFormName] = models.Read_DosageForm_Get(utils.ToInt(id))
  217. }
  218. }
  219. return list, nil
  220. }