sales.go 11 KB

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