sales.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 state = 1 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]["unit_price"] = utils.ToFloat64(list[i]["unit_price"])
  51. // 金额
  52. list[i]["money"] = utils.ToFloat64(list[i]["unit_price"]) * utils.ToFloat64(list[i]["quantity"])
  53. if id, ok := list[i][models.FieldProductID]; ok {
  54. list[i][models.FieldProductName] = models.Read_Product_Get(utils.ToInt(id))
  55. }
  56. if id, ok := list[i][models.FieldEnterpriseID]; ok {
  57. list[i][models.FieldEnterpriseName] = models.Read_Enterprise_Get(utils.ToInt(id))
  58. }
  59. if id, ok := list[i][models.FieldSpecID]; ok {
  60. list[i][models.FieldSpecName] = models.Read_Spec_Get(utils.ToInt(id))
  61. }
  62. if id, ok := list[i][models.FieldUnitID]; ok {
  63. list[i][models.FieldUnitName] = models.Read_Unit_Get(utils.ToInt(id))
  64. }
  65. if id, ok := list[i][models.FieldDosageFormID]; ok {
  66. list[i][models.FieldDosageFormName] = models.Read_DosageForm_Get(utils.ToInt(id))
  67. }
  68. }
  69. return list, count, nil
  70. }
  71. func (e *Sales) SalesListExcel(c *dto.SalesPageReq, deptId int) (list []map[string]interface{}, count int64, err error) {
  72. mtable := models.GetMedicineInfoTableName(deptId)
  73. whereSql := "stock_out.dept_id = " + strconv.Itoa(deptId) + " AND state = 1 AND stock_out.deleted_at is null"
  74. if c.ProductID > 0 {
  75. whereSql += " AND " + mtable + ".product_id = " + strconv.Itoa(c.ProductID)
  76. }
  77. if c.EnterpriseID > 0 {
  78. whereSql += " AND " + mtable + ".enterprise_id = " + strconv.Itoa(c.EnterpriseID)
  79. }
  80. if len(c.BatchNumber) > 0 {
  81. whereSql += " AND " + mtable + ".batch_number = " + c.BatchNumber
  82. }
  83. if len(c.StartDate) > 0 {
  84. whereSql += " AND stock_out.date >= '" + c.StartDate + "'"
  85. }
  86. if len(c.EndDate) > 0 {
  87. whereSql += " AND stock_out.date <= '" + c.EndDate + "'"
  88. }
  89. if len(c.ReceivingUnit) > 0 {
  90. whereSql += " AND stock_out.receiving_unit like '%" + c.ReceivingUnit + "%'"
  91. }
  92. err = db.DB.Table("stock_out").
  93. Joins("left join " + mtable + " on stock_out.medicine_id = " + mtable + ".id").
  94. Where(whereSql).
  95. Scan(&list).Error
  96. if err != nil {
  97. logs.Error("db error: %s ", err)
  98. return list, count, global.GetFailedErr
  99. }
  100. models.InitBasicData(deptId)
  101. for i := 0; i < len(list); i++ {
  102. list[i]["unit_price"] = utils.ToFloat64(list[i]["unit_price"])
  103. // 金额
  104. list[i]["money"] = utils.ToFloat64(list[i]["unit_price"]) * utils.ToFloat64(list[i]["quantity"])
  105. if id, ok := list[i][models.FieldProductID]; ok {
  106. list[i][models.FieldProductName] = models.Read_Product_Get(utils.ToInt(id))
  107. }
  108. if id, ok := list[i][models.FieldEnterpriseID]; ok {
  109. list[i][models.FieldEnterpriseName] = models.Read_Enterprise_Get(utils.ToInt(id))
  110. }
  111. if id, ok := list[i][models.FieldSpecID]; ok {
  112. list[i][models.FieldSpecName] = models.Read_Spec_Get(utils.ToInt(id))
  113. }
  114. if id, ok := list[i][models.FieldUnitID]; ok {
  115. list[i][models.FieldUnitName] = models.Read_Unit_Get(utils.ToInt(id))
  116. }
  117. if id, ok := list[i][models.FieldDosageFormID]; ok {
  118. list[i][models.FieldDosageFormName] = models.Read_DosageForm_Get(utils.ToInt(id))
  119. }
  120. }
  121. return list, count, nil
  122. }
  123. // 销售报表
  124. func (e *Sales) SalesReportList(c *dto.SalesPageReq, deptId int) (list []map[string]interface{}, err error) {
  125. mtable := models.GetMedicineInfoTableName(deptId)
  126. whereSql := "stock_out.dept_id = " + strconv.Itoa(deptId) + " AND state = 1 AND stock_out.deleted_at is null"
  127. if c.ProductID > 0 {
  128. whereSql += " AND " + mtable + ".product_id = " + strconv.Itoa(c.ProductID)
  129. }
  130. if len(c.StartDate) > 0 {
  131. whereSql += " AND stock_out.date >= '" + c.StartDate + "'"
  132. }
  133. if len(c.EndDate) > 0 {
  134. whereSql += " AND stock_out.date <= '" + c.EndDate + "'"
  135. }
  136. if len(c.ReceivingUnit) > 0 {
  137. whereSql += " AND stock_out.receiving_unit like '%" + c.ReceivingUnit + "%'"
  138. }
  139. err = db.DB.Table("stock_out").
  140. Select(mtable + ".*,SUM(quantity) AS total").
  141. Scopes(
  142. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  143. ).
  144. Joins("left join " + mtable + " on stock_out.medicine_id = " + mtable + ".id").
  145. Where(whereSql).
  146. Group("stock_out.medicine_id").
  147. Scan(&list).Error
  148. if err != nil {
  149. logs.Error("db error: %s ", err)
  150. return list, global.GetFailedErr
  151. }
  152. models.InitBasicData(deptId)
  153. for i := 0; i < len(list); i++ {
  154. list[i]["unit_price"] = utils.ToFloat64(list[i]["unit_price"])
  155. // 金额
  156. list[i]["money"] = utils.ToFloat64(list[i]["unit_price"]) * utils.ToFloat64(list[i]["total"])
  157. if id, ok := list[i][models.FieldProductID]; ok {
  158. list[i][models.FieldProductName] = models.Read_Product_Get(utils.ToInt(id))
  159. }
  160. if id, ok := list[i][models.FieldEnterpriseID]; ok {
  161. list[i][models.FieldEnterpriseName] = models.Read_Enterprise_Get(utils.ToInt(id))
  162. }
  163. if id, ok := list[i][models.FieldSpecID]; ok {
  164. list[i][models.FieldSpecName] = models.Read_Spec_Get(utils.ToInt(id))
  165. }
  166. if id, ok := list[i][models.FieldUnitID]; ok {
  167. list[i][models.FieldUnitName] = models.Read_Unit_Get(utils.ToInt(id))
  168. }
  169. if id, ok := list[i][models.FieldDosageFormID]; ok {
  170. list[i][models.FieldDosageFormName] = models.Read_DosageForm_Get(utils.ToInt(id))
  171. }
  172. }
  173. return list, nil
  174. }
  175. // SalesOrderList 销售订单列表 包含销售数量、销售单价、销售金额
  176. func (e *Sales) SalesOrderList(c *dto.SalesOrderPageReq, deptId int) (list []map[string]interface{}, count int64, err error) {
  177. mtable := models.GetMedicineInfoTableName(deptId)
  178. whereSql := "stock_out.dept_id = " + strconv.Itoa(deptId) + " AND state = 1 AND stock_out.deleted_at is null"
  179. if c.ProductID > 0 {
  180. whereSql += " AND " + mtable + ".product_id = " + strconv.Itoa(c.ProductID)
  181. }
  182. if c.EnterpriseID > 0 {
  183. whereSql += " AND " + mtable + ".enterprise_id = " + strconv.Itoa(c.EnterpriseID)
  184. }
  185. if len(c.BatchNumber) > 0 {
  186. whereSql += " AND " + mtable + ".batch_number = " + c.BatchNumber
  187. }
  188. if len(c.Date) > 0 {
  189. whereSql += " AND stock_out.date = '" + c.Date + "'"
  190. }
  191. if len(c.ReceivingUnit) > 0 {
  192. whereSql += " AND stock_out.receiving_unit like '%" + c.ReceivingUnit + "%'"
  193. }
  194. err = db.DB.Table("stock_out").
  195. //Select(mtable + ".*,stock_out.*,stock_out.unit_price as sales_unit_price").
  196. Scopes(
  197. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  198. ).
  199. Joins("left join " + mtable + " on stock_out.medicine_id = " + mtable + ".id").
  200. Where(whereSql).
  201. Scan(&list).Limit(-1).Offset(-1).
  202. Count(&count).Error
  203. if err != nil {
  204. logs.Error("db error: %s ", err)
  205. return list, count, global.GetFailedErr
  206. }
  207. models.InitBasicData(deptId)
  208. for i := 0; i < len(list); i++ {
  209. list[i]["sales_unit_price"] = utils.ToFloat64(list[i]["unit_price"])
  210. list[i]["sales_money"] = utils.ToFloat64(list[i]["unit_price"]) * utils.ToFloat64(list[i]["quantity"])
  211. if id, ok := list[i][models.FieldProductID]; ok {
  212. list[i][models.FieldProductName] = models.Read_Product_Get(utils.ToInt(id))
  213. }
  214. if id, ok := list[i][models.FieldEnterpriseID]; ok {
  215. list[i][models.FieldEnterpriseName] = models.Read_Enterprise_Get(utils.ToInt(id))
  216. }
  217. if id, ok := list[i][models.FieldSpecID]; ok {
  218. list[i][models.FieldSpecName] = models.Read_Spec_Get(utils.ToInt(id))
  219. }
  220. if id, ok := list[i][models.FieldUnitID]; ok {
  221. list[i][models.FieldUnitName] = models.Read_Unit_Get(utils.ToInt(id))
  222. }
  223. if id, ok := list[i][models.FieldDosageFormID]; ok {
  224. list[i][models.FieldDosageFormName] = models.Read_DosageForm_Get(utils.ToInt(id))
  225. }
  226. list[i][models.FieldExpiryDate] = utils.ToDate(list[i][models.FieldExpiryDate])
  227. }
  228. return list, count, nil
  229. }
  230. // SalesStockOutExcel 销售订单列表 包含销售数量、销售单价、销售金额
  231. func (e *Sales) SalesStockOutExcel(c *dto.SalesStockOutExcelReq, deptId int) (list []map[string]interface{}, err error) {
  232. mtable := models.GetMedicineInfoTableName(deptId)
  233. whereSql := "stock_out.dept_id = " + strconv.Itoa(deptId) + " AND state = 1 AND stock_out.deleted_at is null"
  234. if len(c.Date) > 0 {
  235. whereSql += " AND stock_out.date = '" + c.Date + "'"
  236. }
  237. if len(c.ReceivingUnit) > 0 {
  238. whereSql += " AND stock_out.receiving_unit like '%" + c.ReceivingUnit + "%'"
  239. }
  240. err = db.DB.Table("stock_out").
  241. //Select(mtable + ".*,stock_out.*,stock_out.unit_price as sales_unit_price").
  242. Joins("left join " + mtable + " on stock_out.medicine_id = " + mtable + ".id").
  243. Where(whereSql).
  244. Scan(&list).Error
  245. if err != nil {
  246. logs.Error("db error: %s ", err)
  247. return list, global.GetFailedErr
  248. }
  249. models.InitBasicData(deptId)
  250. for i := 0; i < len(list); i++ {
  251. list[i]["sales_unit_price"] = utils.ToFloat64(list[i]["unit_price"])
  252. list[i]["sales_money"] = utils.ToFloat64(list[i]["unit_price"]) * utils.ToFloat64(list[i]["quantity"])
  253. if id, ok := list[i][models.FieldProductID]; ok {
  254. list[i][models.FieldProductName] = models.Read_Product_Get(utils.ToInt(id))
  255. }
  256. if id, ok := list[i][models.FieldEnterpriseID]; ok {
  257. list[i][models.FieldEnterpriseName] = models.Read_Enterprise_Get(utils.ToInt(id))
  258. }
  259. if id, ok := list[i][models.FieldSpecID]; ok {
  260. list[i][models.FieldSpecName] = models.Read_Spec_Get(utils.ToInt(id))
  261. }
  262. if id, ok := list[i][models.FieldUnitID]; ok {
  263. list[i][models.FieldUnitName] = models.Read_Unit_Get(utils.ToInt(id))
  264. }
  265. if id, ok := list[i][models.FieldDosageFormID]; ok {
  266. list[i][models.FieldDosageFormName] = models.Read_DosageForm_Get(utils.ToInt(id))
  267. }
  268. }
  269. return list, nil
  270. }