stock.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package services
  2. import (
  3. "Medical_ERP/common/global"
  4. db "Medical_ERP/common/initialize"
  5. model2 "Medical_ERP/common/model"
  6. "Medical_ERP/dto"
  7. "Medical_ERP/models"
  8. "errors"
  9. "github.com/beego/beego/v2/core/logs"
  10. "gorm.io/gorm"
  11. )
  12. type Stock struct {
  13. }
  14. // StockIn 入库
  15. func (e *Stock) StockIn(c *dto.StockInInsertReq) error {
  16. var err error
  17. // 检查药品信息是否已存在
  18. medicineInfo, err := e.GetMedicineInfo(c.ProductID, c.EnterpriseID, c.SpecID, c.ApprovalNumber, c.ExpiryDate)
  19. if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
  20. logs.Error("db error: %s", err)
  21. return global.CreateFailedErr
  22. }
  23. tx := db.DB.Begin()
  24. // 如果药品信息不存在,则创建新的药品信息
  25. if errors.Is(err, gorm.ErrRecordNotFound) {
  26. medicineInfo = models.MedicineInfo{
  27. ProductID: c.ProductID,
  28. EnterpriseID: c.EnterpriseID,
  29. SpecID: c.SpecID,
  30. UnitID: c.UnitID,
  31. DosageFormID: c.DosageFormID,
  32. ApprovalNumber: c.ApprovalNumber,
  33. QualificationNumber: c.QualificationNumber,
  34. ProductionDate: c.ProductionDate,
  35. ExpiryDate: c.ExpiryDate,
  36. BatchNumber: c.BatchNumber,
  37. }
  38. //medicineInfo.SetDeptId(c.DeptId)
  39. //medicineInfo.SetCreateBy(c.CreateBy)
  40. err = tx.Create(&medicineInfo).Error
  41. if err != nil {
  42. tx.Rollback()
  43. logs.Error("db error: %s", err)
  44. return global.CreateFailedErr
  45. }
  46. }
  47. // 进行入库操作
  48. stockInRecord := models.StockIn{
  49. MedicineID: medicineInfo.Id,
  50. Quantity: c.Quantity,
  51. UnitPrice: c.UnitPrice,
  52. Operator: c.Operator,
  53. ForwardingUnit: c.ForwardingUnit,
  54. Date: c.Date,
  55. }
  56. stockInRecord.SetDeptId(c.DeptId)
  57. stockInRecord.SetCreateBy(c.CreateBy)
  58. err = tx.Create(&stockInRecord).Error
  59. if err != nil {
  60. tx.Rollback()
  61. logs.Error("db error: %s", err)
  62. return global.CreateFailedErr
  63. }
  64. c.Id = stockInRecord.Id
  65. var inventory models.MedicineInventory
  66. err = tx.First(&inventory, "medicine_id = ? AND dept_id = ?", medicineInfo.Id, c.DeptId).Error
  67. if err != nil {
  68. tx.Rollback()
  69. logs.Error("db error: %s", err)
  70. return global.CreateFailedErr
  71. }
  72. // 添加库存记录
  73. inventory = models.MedicineInventory{
  74. MedicineID: medicineInfo.Id,
  75. TotalIn: c.Quantity,
  76. StockInID: stockInRecord.Id,
  77. TotalOut: 0,
  78. StockOutID: 0,
  79. Balance: c.Quantity,
  80. ForwardingUnit: c.ForwardingUnit,
  81. }
  82. inventory.SetDeptId(c.DeptId)
  83. inventory.SetCreateBy(c.CreateBy)
  84. inventory.Balance += c.Quantity
  85. err = tx.Create(&inventory).Error
  86. if err != nil {
  87. tx.Rollback()
  88. logs.Error("db error: %s", err)
  89. return global.CreateFailedErr
  90. }
  91. return nil
  92. }
  93. // 出库
  94. func (e *Stock) StockInStockInList(c *dto.StockInInsertReq) error {
  95. var err error
  96. // 检查药品信息是否已存在
  97. medicineInfo, err := e.GetMedicineInfo(c.ProductID, c.EnterpriseID, c.SpecID, c.ApprovalNumber, c.ExpiryDate)
  98. if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
  99. logs.Error("db error: %s", err)
  100. return global.CreateFailedErr
  101. }
  102. tx := db.DB.Begin()
  103. // 如果药品信息不存在,则创建新的药品信息
  104. if errors.Is(err, gorm.ErrRecordNotFound) {
  105. medicineInfo = models.MedicineInfo{
  106. ProductID: c.ProductID,
  107. EnterpriseID: c.EnterpriseID,
  108. SpecID: c.SpecID,
  109. UnitID: c.UnitID,
  110. DosageFormID: c.DosageFormID,
  111. ApprovalNumber: c.ApprovalNumber,
  112. QualificationNumber: c.QualificationNumber,
  113. ProductionDate: c.ProductionDate,
  114. ExpiryDate: c.ExpiryDate,
  115. BatchNumber: c.BatchNumber,
  116. }
  117. //medicineInfo.SetDeptId(c.DeptId)
  118. //medicineInfo.SetCreateBy(c.CreateBy)
  119. err = tx.Create(&medicineInfo).Error
  120. if err != nil {
  121. tx.Rollback()
  122. logs.Error("db error: %s", err)
  123. return global.CreateFailedErr
  124. }
  125. }
  126. // 进行入库操作
  127. stockInRecord := models.StockIn{
  128. MedicineID: medicineInfo.Id,
  129. Quantity: c.Quantity,
  130. UnitPrice: c.UnitPrice,
  131. Operator: c.Operator,
  132. ForwardingUnit: c.ForwardingUnit,
  133. Date: c.Date,
  134. }
  135. stockInRecord.SetDeptId(c.DeptId)
  136. stockInRecord.SetCreateBy(c.CreateBy)
  137. err = tx.Create(&stockInRecord).Error
  138. if err != nil {
  139. tx.Rollback()
  140. logs.Error("db error: %s", err)
  141. return global.CreateFailedErr
  142. }
  143. c.Id = stockInRecord.Id
  144. var inventory models.MedicineInventory
  145. err = tx.First(&inventory, "medicine_id = ? AND dept_id = ?", medicineInfo.Id, c.DeptId).Error
  146. if err != nil {
  147. tx.Rollback()
  148. logs.Error("db error: %s", err)
  149. return global.CreateFailedErr
  150. }
  151. // 添加库存记录
  152. inventory = models.MedicineInventory{
  153. MedicineID: medicineInfo.Id,
  154. TotalIn: c.Quantity,
  155. StockInID: stockInRecord.Id,
  156. TotalOut: 0,
  157. StockOutID: 0,
  158. Balance: c.Quantity,
  159. }
  160. inventory.Balance += c.Quantity
  161. inventory.SetDeptId(c.DeptId)
  162. inventory.SetCreateBy(c.CreateBy)
  163. err = tx.Create(&inventory).Error
  164. if err != nil {
  165. tx.Rollback()
  166. logs.Error("db error: %s", err)
  167. return global.CreateFailedErr
  168. }
  169. return nil
  170. }
  171. func (e *Stock) StockOut(c *dto.StockOutInsertReq) error {
  172. var err error
  173. // 检查药品信息是否已存在
  174. medicineInfo, err := e.GetMedicineInfo(c.ProductID, c.EnterpriseID, c.SpecID, c.ApprovalNumber, c.ExpiryDate)
  175. if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
  176. logs.Error("db error: %s", err)
  177. return global.CreateFailedErr
  178. }
  179. // 如果药品信息不存在,提示错误,不能出库
  180. if errors.Is(err, gorm.ErrRecordNotFound) {
  181. logs.Error("db error: %s", err)
  182. return errors.New("药品信息不存在")
  183. }
  184. tx := db.DB.Begin()
  185. // 进行入库操作
  186. stockOutRecord := models.StockOut{
  187. MedicineID: medicineInfo.Id,
  188. Quantity: c.Quantity,
  189. //UnitPrice: c.UnitPrice,
  190. Operator: c.Operator,
  191. ReceivingUnit: c.ReceivingUnit,
  192. Date: c.Date,
  193. }
  194. err = tx.Create(&stockOutRecord).Error
  195. if err != nil {
  196. tx.Rollback()
  197. logs.Error("db error: %s", err)
  198. return global.CreateFailedErr
  199. }
  200. c.Id = stockOutRecord.Id
  201. var inventory models.MedicineInventory
  202. err = tx.First(&inventory, "medicine_id = ? AND dept_id = ?", medicineInfo.Id, c.DeptId).Error
  203. if err != nil {
  204. tx.Rollback()
  205. logs.Error("db error: %s", err)
  206. return global.CreateFailedErr
  207. }
  208. // 添加库存记录
  209. inventory = models.MedicineInventory{
  210. MedicineID: medicineInfo.Id,
  211. TotalIn: 0,
  212. StockInID: 0,
  213. TotalOut: c.Quantity,
  214. StockOutID: stockOutRecord.Id,
  215. Balance: c.Quantity,
  216. ReceivingUnit: c.ReceivingUnit,
  217. Operator: c.Operator,
  218. Date: c.Date,
  219. ControlBy: model2.ControlBy{
  220. DeptId: c.DeptId,
  221. CreateBy: c.CreateBy,
  222. },
  223. }
  224. inventory.Balance -= c.Quantity
  225. err = tx.Create(&inventory).Error
  226. if err != nil {
  227. tx.Rollback()
  228. logs.Error("db error: %s", err)
  229. return global.CreateFailedErr
  230. }
  231. return nil
  232. }
  233. func (e *Stock) GetMedicineInfo(productId, enterpriseId, specId int, approvalNumber, expiryDate string) (medicineInfo models.MedicineInfo, err error) {
  234. err = db.DB.Where("product_id = ? AND enterprise_id = ? AND spec_id = ?"+
  235. "AND approval_number = ? AND expiry_date", productId, enterpriseId, specId, approvalNumber, expiryDate).
  236. First(&medicineInfo).Error
  237. return
  238. }
  239. func (e *Stock) InsertMedicineInfo(c *dto.StockInInsertReq) (medicineInfo models.MedicineInfo, err error) {
  240. data := models.MedicineInfo{
  241. ProductID: c.ProductID,
  242. EnterpriseID: c.EnterpriseID,
  243. SpecID: c.SpecID,
  244. UnitID: c.UnitID,
  245. DosageFormID: c.DosageFormID,
  246. ApprovalNumber: c.ApprovalNumber,
  247. QualificationNumber: c.QualificationNumber,
  248. ProductionDate: c.ProductionDate,
  249. ExpiryDate: c.ExpiryDate,
  250. BatchNumber: c.BatchNumber,
  251. }
  252. //data.SetDeptId(c.DeptId)
  253. //data.SetCreateBy(c.CreateBy)
  254. err = db.DB.Create(&data).Error
  255. if err != nil {
  256. logs.Error("db error: %s", err)
  257. return data, global.CreateFailedErr
  258. }
  259. return data, nil
  260. }