supply_service.go 41KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. package service
  2. import (
  3. "XT_New/models"
  4. "github.com/jinzhu/gorm"
  5. "strings"
  6. "time"
  7. )
  8. //根据供应商编号获取首要联系人
  9. func FindName(code string, orgid int64) (fistname models.SpSupplierContacts, err error) {
  10. err = XTReadDB().Model(&models.SpSupplierContacts{}).Where("supplier_code = ? and status = 1 and is_first = 1 and user_org_id = ?", code, orgid).First(&fistname).Error
  11. return fistname, err
  12. }
  13. //供应商分页
  14. func GetSupplyList(ctype int64, page int64, limit int64, keyword string, orgid int64) (supplylist []*models.SpSupplierName, total int64, err error) {
  15. db := XTReadDB().Model(&supplylist).Where("xt_supplier_name.status = 1 and xt_supplier_name.user_org_id = ?", orgid)
  16. offset := (page - 1) * limit
  17. if len(keyword) > 0 {
  18. keyword = "%" + keyword + "%" //联系人
  19. db = db.Joins("join xt_supplier_contacts on xt_supplier_contacts.supplier_code = xt_supplier_name.supplier_code")
  20. db = db.Where("xt_supplier_contacts.name like ? or xt_supplier_name.supplier_code like ? or xt_supplier_name.supplier_name like ? ", keyword, keyword, keyword).Group("xt_supplier_name.id")
  21. }
  22. if ctype > 0 {
  23. db = db.Where("xt_supplier_name.supplier_type = ?", ctype)
  24. }
  25. err = db.Count(&total).Offset(offset).Order("xt_supplier_name.id desc").Limit(limit).Find(&supplylist).Error
  26. return supplylist, total, err
  27. }
  28. //修改供应商和联系人
  29. func UpdateSupplyAndContact(thisStockIn []interface{}, suid, orgId, supplierType, tcreater int64, supplierCode, supplierName, number, bank, bankAccount string, vatRate float64) error {
  30. tx := XTWriteDB().Begin()
  31. defer func() {
  32. if err != nil {
  33. tx.Rollback()
  34. } else {
  35. tx.Commit()
  36. }
  37. }()
  38. for _, item := range thisStockIn {
  39. items := item.(map[string]interface{})
  40. //判断联系人是否为新加的
  41. if items["id"] == "" || items["id"] == 0 {
  42. var tmp float64 = 0
  43. items["id"] = tmp
  44. }
  45. //id, _ := strconv.ParseInt(items["id"].(string), 10, 64)
  46. id := int64(items["id"].(float64))
  47. name := items["name"].(string)
  48. phone := items["phone"].(string)
  49. address := items["address"].(string)
  50. var tmpfirst int64
  51. //tmptype := reflect.TypeOf(items["is_first"])
  52. switch items["is_first"].(type) {
  53. case int:
  54. var aint int = items["is_first"].(int)
  55. tmpfirst = int64(aint)
  56. case float64:
  57. tmpfirst = int64(items["is_first"].(float64))
  58. }
  59. isfirst := tmpfirst
  60. updatecontacts := models.SpSupplierContacts{
  61. ID: id,
  62. Name: name,
  63. Phone: phone,
  64. Address: address,
  65. IsFirst: isfirst,
  66. SupplierCode: supplierCode,
  67. UserOrgId: orgId,
  68. Status: 1,
  69. Ctime: 0,
  70. Mtime: time.Now().Unix(),
  71. }
  72. if id == 0 { //表示这是新加的
  73. spcontacts := models.SpSupplierContacts{
  74. Name: name,
  75. Phone: phone,
  76. Address: address,
  77. IsFirst: isfirst,
  78. SupplierCode: supplierCode,
  79. UserOrgId: orgId,
  80. Status: 1,
  81. Ctime: time.Now().Unix(),
  82. Mtime: 0,
  83. } //保存一条联系人的数据
  84. err = SaveContacts(spcontacts, tx)
  85. if err != nil {
  86. return err
  87. }
  88. } else {
  89. err = UpdateContact(updatecontacts, tx)
  90. }
  91. var tmpid int64
  92. if isfirst == 1 {
  93. if id == 0 { //新加的首要联系人
  94. var spconid []*models.SpSupplierContacts
  95. spconid, err = SaveContactsId(tx, orgId)
  96. if err != nil {
  97. return err
  98. }
  99. tmpid = spconid[0].ID
  100. } else {
  101. tmpid = id
  102. }
  103. //更新供应商
  104. upsupply := models.SpSupplierName{
  105. ID: suid,
  106. SupplierCode: supplierCode,
  107. SupplierName: supplierName,
  108. SupplierType: supplierType,
  109. VatRate: vatRate,
  110. Number: number,
  111. Bank: bank,
  112. BankAccount: bankAccount,
  113. UserOrgId: orgId,
  114. Status: 1,
  115. ContactsId: tmpid,
  116. Mtime: time.Now().Unix(),
  117. Modify: tcreater,
  118. }
  119. err = UpdateSupplyNameTX(upsupply, tx)
  120. if err != nil {
  121. return err
  122. }
  123. }
  124. }
  125. return err
  126. }
  127. //更新一条供应商的信息,添加了事务的
  128. func UpdateSupplyNameTX(upsupply models.SpSupplierName, tx *gorm.DB) error {
  129. err := tx.Model(&models.SpSupplierName{}).Where("id = ? and status = 1", upsupply.ID).Updates(map[string]interface{}{"supplier_code": upsupply.SupplierCode, "supplier_name": upsupply.SupplierName, "supplier_type": upsupply.SupplierType, "vat_rate": upsupply.VatRate, "number": upsupply.Number, "bank": upsupply.Bank, "bank_account": upsupply.BankAccount, "user_org_id": upsupply.UserOrgId, "status": upsupply.Status, "contacts_id": upsupply.ContactsId, "mtime": upsupply.Mtime, "modify": upsupply.Modify}).Error
  130. return err
  131. }
  132. //更新一条供应商的信息,没有添加事务
  133. func UpdateSupplyName(upsupply models.SpSupplierName) error {
  134. err := XTWriteDB().Model(&models.SpSupplierName{}).Where("id = ? and status = 1", upsupply.ID).Updates(map[string]interface{}{"supplier_code": upsupply.SupplierCode, "supplier_name": upsupply.SupplierName, "supplier_type": upsupply.SupplierType, "vat_rate": upsupply.VatRate, "number": upsupply.Number, "bank": upsupply.Bank, "bank_account": upsupply.BankAccount, "user_org_id": upsupply.UserOrgId, "status": upsupply.Status, "contacts_id": upsupply.ContactsId, "mtime": upsupply.Mtime, "modify": upsupply.Modify}).Error
  135. return err
  136. }
  137. //更新一条联系人的信息
  138. func UpdateContact(updatecontacts models.SpSupplierContacts, tx *gorm.DB) error {
  139. err := tx.Model(&models.SpSupplierContacts{}).Where("id = ? and status = 1", updatecontacts.ID).Updates(map[string]interface{}{"name": updatecontacts.Name, "phone": updatecontacts.Phone, "address": updatecontacts.Address, "is_first": updatecontacts.IsFirst, "supplier_code": updatecontacts.SupplierCode, "user_org_id": updatecontacts.UserOrgId, "status": updatecontacts.Status, "mtime": updatecontacts.Mtime}).Error
  140. return err
  141. }
  142. //查询供应商单条记录
  143. func GetSupplyOne(id int64) (supply models.SpSupplierName, err error) {
  144. err = XTReadDB().Model(&models.SpSupplierName{}).Where("id = ? and status = 1", id).First(&supply).Error
  145. return supply, err
  146. }
  147. //删除单条联系人记录
  148. func DelContactOne(id int64) error {
  149. err := XTWriteDB().Model(&models.SpSupplierContacts{}).Where("id = ?", id).Update("status", 0).Error
  150. return err
  151. }
  152. //获取单条供应商和涉及到的联系人记录
  153. func GetSupplyAndContactOne(id, orgId int64) (supply models.SpSupplierName, contact []*models.SpSupplierContacts, err error) {
  154. err = XTReadDB().Model(&models.SpSupplierName{}).Where("id = ? and status = 1 and user_org_id = ?", id, orgId).First(&supply).Error
  155. code := supply.SupplierCode
  156. err = XTReadDB().Model(&models.SpSupplierContacts{}).Where("supplier_code = ? and status = 1 and user_org_id = ?", code, orgId).Find(&contact).Error
  157. return supply, contact, err
  158. }
  159. //删除供应商及联系人
  160. func DelSupply(supply models.SpSupplierName, orgId int64) error {
  161. tx := XTWriteDB().Begin()
  162. defer func() {
  163. if err != nil {
  164. tx.Rollback()
  165. } else {
  166. tx.Commit()
  167. }
  168. }()
  169. //删除供应商
  170. err := tx.Model(&supply).Update("status", 0).Error
  171. if err != nil {
  172. return err
  173. }
  174. var spcode models.SpSupplierName
  175. //获取供应商编号
  176. err = tx.Model(&models.SpSupplierName{}).Select("supplier_code").Where("user_org_id = ? and id = ?", orgId, supply.ID).First(&spcode).Error
  177. if err != nil {
  178. return err
  179. }
  180. //删除联系人
  181. err = tx.Model(&models.SpSupplierContacts{}).Where("supplier_code = ? and user_org_id = ?", spcode.SupplierCode, orgId).Update("status", 0).Error
  182. return err
  183. }
  184. //保存供应商和联系人
  185. func SaveSupplyAndContact(thisStockIn []interface{}, orgId, supplierType, tcreater int64, supplierCode, supplierName, number, bank, bankAccount string, vatRate float64) error {
  186. tx := XTWriteDB().Begin()
  187. defer func() {
  188. if err != nil {
  189. tx.Rollback()
  190. } else {
  191. tx.Commit()
  192. }
  193. }()
  194. for _, item := range thisStockIn {
  195. items := item.(map[string]interface{})
  196. name := items["name"].(string)
  197. phone := items["phone"].(string)
  198. address := items["address"].(string)
  199. //isfirst := int64(items["is_first"].(float64))
  200. var tmpfirst int64
  201. //tmptype := reflect.TypeOf(items["is_first"])
  202. switch items["is_first"].(type) {
  203. case int:
  204. var aint int = items["is_first"].(int)
  205. tmpfirst = int64(aint)
  206. case float64:
  207. tmpfirst = int64(items["is_first"].(float64))
  208. }
  209. isfirst := tmpfirst
  210. spcontacts := models.SpSupplierContacts{
  211. Name: name,
  212. Phone: phone,
  213. Address: address,
  214. IsFirst: isfirst,
  215. SupplierCode: supplierCode,
  216. UserOrgId: orgId,
  217. Status: 1,
  218. Ctime: time.Now().Unix(),
  219. Mtime: 0,
  220. }
  221. err = SaveContacts(spcontacts, tx)
  222. if isfirst == 1 {
  223. var spconid []*models.SpSupplierContacts
  224. spconid, err = SaveContactsId(tx, orgId)
  225. if err != nil {
  226. return err
  227. }
  228. tmpid := spconid[0].ID
  229. //保存供应商
  230. supply := models.SpSupplierName{
  231. SupplierCode: supplierCode,
  232. SupplierName: supplierName,
  233. SupplierType: supplierType,
  234. VatRate: vatRate,
  235. Number: number,
  236. Bank: bank,
  237. BankAccount: bankAccount,
  238. UserOrgId: orgId,
  239. Status: 1,
  240. ContactsId: tmpid,
  241. Ctime: time.Now().Unix(),
  242. Mtime: 0,
  243. Creater: tcreater,
  244. Modify: tcreater,
  245. }
  246. err = SaveSupplyTx(supply, tx)
  247. if err != nil {
  248. return err
  249. }
  250. }
  251. if err != nil {
  252. return err
  253. }
  254. }
  255. return err
  256. }
  257. //获取供应商编码
  258. func GetSuppliyCode(orgId int64) (spcode []*models.SpSupplierName, err error) {
  259. err = XTReadDB().Model(&models.SpSupplierName{}).Select("supplier_code").Where("supplier_code like 'gys%' and status = 1 and user_org_id = ? ", orgId).Order("supplier_code desc").First(&spcode).Error
  260. return spcode, err
  261. }
  262. //查询供应商的名字是否有重复
  263. func FindSupplierName(supplierName string, orgId int64) (sbool bool, err error) {
  264. var total int
  265. err = XTReadDB().Model(&models.SpSupplierName{}).Where("supplier_name = ? and status = 1", supplierName).Count(&total).Error
  266. if total != 0 {
  267. sbool = true
  268. } else {
  269. sbool = false
  270. }
  271. return sbool, err
  272. }
  273. //查询供应商的编号是否有重复(用于修改)
  274. func FindSupplierCode(supplierCode string, supplierid, orgid int64) (codebool bool, err error) {
  275. var total int
  276. err = XTReadDB().Model(&models.SpSupplierName{}).Where("supplier_code = ? and id != ? and user_org_id = ? and status = 1", supplierCode, supplierid, orgid).Count(&total).Error
  277. if total > 0 {
  278. codebool = true
  279. } else {
  280. codebool = false
  281. }
  282. return codebool, err
  283. }
  284. //查询供应商的编号是否有重复(用于新增)
  285. func FindSupplierCodes(supplierCode string, orgid int64) (codebool bool, err error) {
  286. var total int
  287. err = XTReadDB().Model(&models.SpSupplierName{}).Where("supplier_code = ? and status = 1 and user_org_id = ?", supplierCode, orgid).Count(&total).Error
  288. if total > 0 {
  289. codebool = true
  290. } else {
  291. codebool = false
  292. }
  293. return codebool, err
  294. }
  295. //保存一条供应商数据加事务的
  296. func SaveSupplyTx(supply models.SpSupplierName, tx *gorm.DB) error {
  297. err := tx.Create(&supply).Error
  298. return err
  299. }
  300. //保存一条供应商数据没有事务的
  301. func SaveSupply(supply models.SpSupplierName) error {
  302. err := XTWriteDB().Create(&supply).Error
  303. return err
  304. }
  305. //获取联系人的id
  306. func SaveContactsId(tx *gorm.DB, orgid int64) (spconid []*models.SpSupplierContacts, err error) {
  307. err = tx.Model(&models.SpSupplierContacts{}).Select("id").Where("status = 1 and user_org_id = ?", orgid).Order("id desc").First(&spconid).Error
  308. return
  309. }
  310. //保存一条联系人数据
  311. func SaveContacts(spcontacts models.SpSupplierContacts, tx *gorm.DB) error {
  312. err := tx.Create(&spcontacts).Error
  313. return err
  314. }
  315. func GetSupplyDrugList(orgid int64) (drug []*models.SpBaseDrug, err error) {
  316. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1 AND find_in_set('停用',drug_status) = 0")
  317. if orgid > 0 {
  318. db = db.Where("x.org_id = ?", orgid)
  319. }
  320. err = db.Preload("DrugWarehouseInfo", "status = 1 and org_id = ? and (stock_max_number >0 or stock_min_number>0)", orgid).Find(&drug).Error
  321. return drug, err
  322. }
  323. func GetSupplyGoodList(orgid int64) (good []*models.SpGoodInformation, err error) {
  324. db := XTReadDB().Table("xt_good_information as x").Where("x.status = 1 AND find_in_set('停用',good_status) = 0")
  325. if orgid > 0 {
  326. db = db.Where("x.org_id = ?", orgid)
  327. }
  328. err = db.Preload("GoodWarehouseInfo", "status = 1 and org_id = ?", orgid).Find(&good).Error
  329. return good, err
  330. }
  331. func GetSupplierList(orgid int64) (suppler []*models.SpSupplierName, err error) {
  332. err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&suppler).Error
  333. return suppler, err
  334. }
  335. func FindAllSupplyOrder(orgid int64) (total int64, err error) {
  336. err = XTReadDB().Model(&models.SupplierWarehouseInfo{}).Where("user_org_id = ?", orgid).Count(&total).Error
  337. return total, err
  338. }
  339. func CreateSupplyWarehouse(info models.SupplierWarehouseInfo) error {
  340. err := XTWriteDB().Create(&info).Error
  341. return err
  342. }
  343. func FindLastSupplyWarehouseInfo(orgid int64) (models.SupplierWarehouseInfo, error) {
  344. info := models.SupplierWarehouseInfo{}
  345. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Last(&info).Error
  346. return info, err
  347. }
  348. func CreateSupplyWarehousingOrder(order *models.SupplierWarehousingInfoOrder) error {
  349. err := XTWriteDB().Create(&order).Error
  350. return err
  351. }
  352. func GetAllPurchaseOrderList(check_id int64, startime int64, endtime int64, keyword string, page int64, limit int64, orgid int64) (info []*models.VmSupplierWarehouseInfo, total int64, err error) {
  353. db := XTReadDB().Model(&info).Where("sgj_xt.xt_supplier_warehouse_info.status = 1")
  354. likeKey := "%" + keyword + "%"
  355. offset := (page - 1) * limit
  356. if check_id > 0 {
  357. db = db.Where("sgj_xt.xt_supplier_warehouse_info.is_check = ?", check_id)
  358. }
  359. if startime > 0 {
  360. db = db.Where("sgj_xt.xt_supplier_warehouse_info.record_date >= ?", startime)
  361. }
  362. if endtime > 0 {
  363. db = db.Where("sgj_xt.xt_supplier_warehouse_info.record_date<=?", endtime)
  364. }
  365. if len(keyword) > 0 {
  366. db = db.Joins("join sgj_xt.xt_supplier_name on sgj_xt.xt_supplier_name.id = sgj_xt.xt_supplier_warehouse_info.supplier_id")
  367. db = db.Where("sgj_xt.xt_supplier_warehouse_info.number like ? or sgj_xt.xt_supplier_name.supplier_name like ? ", likeKey, likeKey).Group("sgj_xt.xt_supplier_warehouse_info.id")
  368. }
  369. if orgid > 0 {
  370. db = db.Where("sgj_xt.xt_supplier_warehouse_info.user_org_id = ?", orgid)
  371. }
  372. err = db.Count(&total).Offset(offset).Limit(limit).Preload("SupplierWarehousingInfoOrder", "status= 1 and user_org_id = ?", orgid).Preload("SpSupplierWarehouseOut", "status = 1 and user_org_id =?", orgid).Order("ctime desc").Find(&info).Error
  373. return info, total, err
  374. }
  375. func GetSupplyWarehousingOrderInfo(id int64) (order []*models.SupplierWarehousingInfoOrder, err error) {
  376. err = XTReadDB().Where("warehousing_id = ? and status = 1", id).Find(&order).Error
  377. return order, err
  378. }
  379. func GetSupplyWarehousingOrderInfoTwo(id int64, ids []string) (order []*models.SupplierWarehousingInfoOrder, err error) {
  380. err = XTReadDB().Where("warehousing_id = ? and status = 1 and project_id in(?)", id, ids).Find(&order).Error
  381. return order, err
  382. }
  383. func ModefySupplyWarehouseInfo(id int64, info models.SupplierWarehouseInfo) error {
  384. err := XTWriteDB().Model(&models.SupplierWarehouseInfo{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"rate_of_concession": info.RateOfConcession, "discount_amount": info.DiscountAmount, "document_date": info.DocumentDate, "delivery_date": info.DeliveryDate, "supplier_id": info.SupplierId, "return_remake": info.ReturnRemake}).Error
  385. return err
  386. }
  387. func ModifySupplyWarehouseOrder(order *models.SupplierWarehousingInfoOrder) error {
  388. err := XTWriteDB().Model(&models.SupplierWarehousingInfoOrder{}).Where("id = ? and status = 1", order.ID).Updates(map[string]interface{}{}).Updates(map[string]interface{}{"is_source": order.IsSource, "count": order.Count, "price": order.Price, "amount": order.Amount, "remark": order.Remark, "project_id": order.ProjectId, "supply_license_number": order.SupplyLicenseNumber, "supply_type": order.SupplyType, "supply_specification_name": order.SupplySpecificationName, "supply_total": order.SupplyTotal, "supply_manufacturer": order.SupplyManufacturer, "name": order.Name, "supply_unit": order.SupplyUnit, "manufacturer_id": order.ManufacturerId}).Error
  389. return err
  390. }
  391. func UpdateSupplyWaresing(id int64, info models.SupplierWarehouseInfo) error {
  392. err := XTWriteDB().Model(&models.SupplierWarehouseInfo{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_check": info.IsCheck, "checker": info.Checker, "check_time": info.CheckTime, "mtime": time.Now().Unix()}).Error
  393. return err
  394. }
  395. func GetPurchaseOrderDetail(id int64) (models.SupplierWarehouseInfo, error) {
  396. info := models.SupplierWarehouseInfo{}
  397. err := XTReadDB().Model(&info).Where("id =? and status =1", id).Find(&info).Error
  398. return info, err
  399. }
  400. func FindAllSupplyWarehouseOutOrder(orgid int64) (total int64, err error) {
  401. err = XTReadDB().Model(&models.SpSupplierWarehouseOut{}).Where("user_org_id = ?", orgid).Count(&total).Error
  402. return total, err
  403. }
  404. func FindSupplyWarehouseOutById(orgid int64) (models.SpSupplierWarehouseOut, error) {
  405. out := models.SpSupplierWarehouseOut{}
  406. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Last(&out).Error
  407. return out, err
  408. }
  409. func CreateSupplyWarehouseOut(out models.SpSupplierWarehouseOut) error {
  410. err := XTWriteDB().Create(&out).Error
  411. return err
  412. }
  413. func CreateSupplyWarehousOutOrder(order *models.SpSupplierWarehousingOutOrder) error {
  414. err := XTWriteDB().Create(&order).Error
  415. return err
  416. }
  417. func GetSupplyWarehouseOutById(id int64, user_org_id int64) (order []*models.SpSupplierWarehousingOutOrder, err error) {
  418. err = XTReadDB().Where("warehouse_out_id = ? and status = 1 and user_org_id = ?", id, user_org_id).Find(&order).Error
  419. return order, err
  420. }
  421. func GetSupplyWarehouseOutByIdOne(id int64, user_org_id int64, ids []string) (order []*models.SpSupplierWarehousingOutOrder, err error) {
  422. err = XTReadDB().Where("warehouse_out_id = ? and status = 1 and user_org_id = ? and project_id in(?)", id, user_org_id, ids).Find(&order).Error
  423. return order, err
  424. }
  425. func GetAllGoodOderList(check_id int64, keyword string, page int64, limit int64, startime int64, endtime int64, orgid int64) (out []*models.VmSupplierWarehouseOut, total int64, err error) {
  426. db := XTReadDB().Model(&out).Where("sgj_xt.xt_supplier_warehouse_out.status = 1")
  427. likeKey := "%" + keyword + "%"
  428. offset := (page - 1) * limit
  429. if check_id > 0 {
  430. db = db.Where("sgj_xt.xt_supplier_warehouse_out.is_check = ?", check_id)
  431. }
  432. if startime > 0 {
  433. db = db.Where("sgj_xt.xt_supplier_warehouse_out.record_date >= ?", startime)
  434. }
  435. if endtime > 0 {
  436. db = db.Where("sgj_xt.xt_supplier_warehouse_out.record_date<=?", endtime)
  437. }
  438. if len(keyword) > 0 {
  439. db = db.Joins("join sgj_xt.xt_supplier_name on sgj_xt.xt_supplier_name.id = sgj_xt.xt_supplier_warehouse_out.supplier_id")
  440. db = db.Where("sgj_xt.xt_supplier_warehouse_out.good_number like ? or sgj_xt.xt_supplier_name.supplier_name like ? ", likeKey, likeKey).Group("xt_supplier_warehouse_out.id")
  441. }
  442. if orgid > 0 {
  443. db = db.Where("sgj_xt.xt_supplier_warehouse_out.user_org_id = ?", orgid)
  444. }
  445. err = db.Count(&total).Offset(offset).Limit(limit).Preload("SpSupplierWarehousingOutOrder", "status= 1 and user_org_id = ?", orgid).Preload("SpSupplierWarehousingCancelOrder", "status= 1 and user_org_id = ?", orgid).Order("ctime desc").Find(&out).Error
  446. return out, total, err
  447. }
  448. func GetGoodOrderDetail(id int64, orgid int64) (models.SpSupplierWarehouseOut, error) {
  449. out := models.SpSupplierWarehouseOut{}
  450. err := XTReadDB().Where("id = ? and user_org_id = ? and status = 1", id, orgid).Find(&out).Error
  451. return out, err
  452. }
  453. func UpdateGoodWarehouseOut(id int64, out models.SpSupplierWarehouseOut) error {
  454. err := XTWriteDB().Model(&out).Where("id=? and status = 1", id).Updates(map[string]interface{}{"arrearage": out.Arrearage, "payment": out.Payment, "rate_of_concession": out.RateOfConcession, "discount_amount": out.DiscountAmount, "document_date": out.DocumentDate, "return_remake": out.ReturnRemake}).Error
  455. return err
  456. }
  457. func UpdateGoodWarehouseOutOrder(order *models.SpSupplierWarehousingOutOrder) error {
  458. err := XTWriteDB().Model(&order).Where("id = ? and status = 1", order.ID).Updates(map[string]interface{}{"project_id": order.ProjectId, "is_source": order.IsSource, "count": order.Count, "price": order.Count, "remark": order.Remark, "supply_batch_number": order.SupplyBatchNumber, "supply_product_date": order.SupplyProductDate, "supply_expiry_date": order.SupplyExpiryDate, "supply_type": order.SupplyType, "supply_specification_name": order.SupplySpecificationName, "supply_total": order.SupplySpecificationName, "supply_manufacturer": order.SupplyManufacturer, "name": order.Name, "supply_unit": order.SupplyUnit, "manufacturer_id": order.ManufacturerId, "supply_license_number": order.SupplyLicenseNumber, "warehousing_id": order.WarehousingId, "warehouse_info_id": order.WarehouseInfoId}).Error
  459. return err
  460. }
  461. func DeletePurchOrder(id int64, orgid int64) error {
  462. err := XTWriteDB().Model(&models.SupplierWarehouseInfo{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  463. err = XTWriteDB().Model(&models.SupplierWarehousingInfoOrder{}).Where("warehousing_id =? and user_org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  464. return err
  465. }
  466. func GetAllPurcaseOrderById(id int64, orgid int64) (order []*models.VSupplierWarehousingInfoOrder, err error) {
  467. err = XTReadDB().Model(&order).Where("warehousing_id = ? and user_org_id = ? and status =1", id, orgid).Find(&order).Error
  468. return order, err
  469. }
  470. func GetAllGoodOrderById(id int64, orgid int64) (order []*models.VSpSupplierWarehousingOutOrder, err error) {
  471. db := XTReadDB().Table("xt_supplier_warehousing_out_order as o").Where("o.status = 1")
  472. if id > 0 {
  473. db = db.Where("o.warehousing_id = ?", id)
  474. }
  475. if orgid > 0 {
  476. db = db.Where("o.user_org_id =?", orgid)
  477. }
  478. err = db.Select("o.id,o.order_number,o.project_id,o.count as count,o.supply_unit,o.is_source").Find(&order).Error
  479. return order, err
  480. }
  481. func GetAllGoodOrderByIdTwo(id int64, orgid int64) (order []*models.SpSupplierWarehousingOutOrder, err error) {
  482. err = XTReadDB().Where("warehousing_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&order).Error
  483. return order, err
  484. }
  485. func GetGoodOrderList(id int64, orgid int64) (info []*models.SpSupplierWarehouseOut, err error) {
  486. err = XTReadDB().Where("warehousing_id = ? and status = 1 and user_org_id = ?", id, orgid).Find(&info).Error
  487. return info, err
  488. }
  489. func GetReturnOrder(id int64, orgid int64) error {
  490. err := XTWriteDB().Model(&models.SupplierWarehouseInfo{}).Where("id = ? and status = 1 and user_org_id = ?", id, orgid).Updates(map[string]interface{}{"is_check": 2, "mtime": time.Now().Unix(), "check_time": 0, "checker": 0}).Error
  491. return err
  492. }
  493. func CheckGoodOrder(id int64, orgid int64, out models.SpSupplierWarehouseOut) error {
  494. err := XTWriteDB().Model(&models.SpSupplierWarehouseOut{}).Where("id = ? and user_org_id =? and status = 1", id, orgid).Updates(map[string]interface{}{"is_check": out.IsCheck, "checker": out.Checker, "check_time": out.CheckTime, "mtime": time.Now().Unix()}).Error
  495. return err
  496. }
  497. func ModefySupplyWarehousing(is_warehose int64, warehousing_id int64, orgid int64) error {
  498. err := XTWriteDB().Model(&models.SupplierWarehouseInfo{}).Where("id = ? and status = 1 and user_org_id =?", warehousing_id, orgid).Updates(map[string]interface{}{"is_warehouse": is_warehose, "mtime": time.Now().Unix()}).Error
  499. return err
  500. }
  501. func GetAllDoctorSix(orgid int64, appid int64) (appRole []*models.App_Role, err error) {
  502. err = UserReadDB().Where("org_id = ? AND app_id = ? AND status = 1", orgid, appid).Find(&appRole).Error
  503. return appRole, err
  504. }
  505. func GetSingleDrugWarehouseOrder(record_date int64, orgid int64) (*models.DrugWarehouse, error) {
  506. warehouse := models.DrugWarehouse{}
  507. var err error
  508. err = XTReadDB().Model(&warehouse).Where("warehousing_time = ? and org_id = ? and status = 1", record_date, orgid).Find(&warehouse).Error
  509. if err == gorm.ErrRecordNotFound {
  510. return nil, err
  511. }
  512. if err != nil {
  513. return nil, err
  514. }
  515. return &warehouse, nil
  516. }
  517. func GetSindleWarehouse(record_date int64, orgid int64) (*models.Warehousing, error) {
  518. warehousing := models.Warehousing{}
  519. var err error
  520. err = XTReadDB().Model(&warehousing).Where("warehousing_time = ? and org_id = ? and status = 1", record_date, orgid).Find(&warehousing).Error
  521. if err == gorm.ErrRecordNotFound {
  522. return nil, err
  523. }
  524. if err != nil {
  525. return nil, err
  526. }
  527. return &warehousing, nil
  528. }
  529. func GetLastWarehouseInfoByInfo(orgid int64) (models.Warehousing, error) {
  530. warehousing := models.Warehousing{}
  531. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&warehousing).Error
  532. return warehousing, err
  533. }
  534. func GetSupplyCancelOrder(orgid int64) (total int64, err error) {
  535. err = XTReadDB().Model(&models.SpSupplierWarehouseCancel{}).Where("user_org_id = ?", orgid).Count(&total).Error
  536. return total, err
  537. }
  538. func CreateReturnCacelOrder(cancel models.SpSupplierWarehouseCancel) error {
  539. err := XTWriteDB().Create(&cancel).Error
  540. return err
  541. }
  542. func GetLastReturnCancelOrder(orgid int64) (models.SpSupplierWarehouseCancel, error) {
  543. cancel := models.SpSupplierWarehouseCancel{}
  544. err := XTReadDB().Where("user_org_id =? and status = 1", orgid).Find(&cancel).Error
  545. return cancel, err
  546. }
  547. func CreateCancelReturnOrder(order *models.SpSupplierWarehousingCancelOrder) error {
  548. err := XTWriteDB().Create(&order).Error
  549. return err
  550. }
  551. func GetReturnCancelOrder(id int64, orgid int64) (models.SpSupplierWarehouseCancel, error) {
  552. cancel := models.SpSupplierWarehouseCancel{}
  553. err := XTReadDB().Where("id = ? and status= 1 and user_org_id =?", id, orgid).Find(&cancel).Error
  554. return cancel, err
  555. }
  556. func GetReturnCancelOrderList(id int64, orgid int64) (order models.SpSupplierWarehousingCancelOrder, err error) {
  557. err = XTReadDB().Where("warehouse_cancel_id = ? and user_org_id =? and status = 1", id, orgid).Find(&order).Error
  558. return order, err
  559. }
  560. func GetAllGoodReturnOrderList(checkid int64, keyword string, page int64, limit int64, startime int64, endtime int64, orgid int64) (order []*models.VmSpSupplierWarehouseCancel, total int64, err error) {
  561. db := XTReadDB().Table("sgj_xt.xt_supplier_warehouse_cancel").Where("sgj_xt.xt_supplier_warehouse_cancel.status = 1")
  562. likeKey := "%" + keyword + "%"
  563. offset := (page - 1) * limit
  564. if checkid > 0 {
  565. db = db.Where("sgj_xt.xt_supplier_warehouse_cancel.is_check = ?", checkid)
  566. }
  567. if len(keyword) > 0 {
  568. db = db.Joins("join sgj_xt.xt_supplier_name on sgj_xt.xt_supplier_name.id = sgj_xt.xt_supplier_warehouse_cancel.supplier_id")
  569. db = db.Where("sgj_xt.xt_supplier_warehouse_cancel.number like ? or sgj_xt.xt_supplier_name.supplier_name like ? ", likeKey, likeKey).Group("xt_supplier_warehouse_cancel.id")
  570. }
  571. if startime > 0 {
  572. db = db.Where("sgj_xt.xt_supplier_warehouse_cancel.record_date >= ?", startime)
  573. }
  574. if endtime > 0 {
  575. db = db.Where("sgj_xt.xt_supplier_warehouse_cancel.record_date <= ?", endtime)
  576. }
  577. if orgid > 0 {
  578. db = db.Where("sgj_xt.xt_supplier_warehouse_cancel.user_org_id = ?", orgid)
  579. }
  580. err = db.Count(&total).Offset(offset).Limit(limit).Preload("SpSupplierWarehousingCancelOrder", "status= 1 and user_org_id = ?", orgid).Order("ctime desc").Find(&order).Error
  581. return order, total, err
  582. }
  583. func GetGoodReturnDetail(id int64, orgid int64) (models.VmSpSupplierWarehouseCancel, error) {
  584. cancel := models.VmSpSupplierWarehouseCancel{}
  585. db := XTReadDB().Model(&cancel).Where("status = 1")
  586. if id > 0 {
  587. db = db.Where("id = ?", id)
  588. }
  589. if orgid > 0 {
  590. db = db.Where("user_org_id = ?", orgid)
  591. }
  592. err := db.Find(&cancel).Error
  593. return cancel, err
  594. }
  595. func GetGoodReturnOrderDetail(id int64, orgid int64) (order []*models.SpSupplierWarehousingCancelOrder, err error) {
  596. db := XTReadDB().Model(&order).Where("status =1")
  597. if id > 0 {
  598. db = db.Where("warehouse_cancel_id = ?", id)
  599. }
  600. if orgid > 0 {
  601. db = db.Where("user_org_id = ?", orgid)
  602. }
  603. err = db.Find(&order).Error
  604. return order, err
  605. }
  606. func UpdateWarehouseCancelOrder(order *models.SpSupplierWarehousingCancelOrder) error {
  607. err = XTWriteDB().Model(&order).Where("id = ? and status = 1", order.ID).Updates(map[string]interface{}{"manufacturer_id": order.ManufacturerId, "order_number": order.OrderNumber, "project_id": order.ProjectId, "is_source": order.IsSource, "count": order.Count, "price": order.Price, "remark": order.Remark, "rate_of_concession": order.RateOfConcession, "discount_amount": order.DiscountAmount, "type": order.Type, "supply_specification_name": order.SupplySpecificationName, "supply_type": order.SupplyType, "supply_total": order.SupplyTotal, "supply_manufacturer": order.SupplyManufacturer, "name": order.Name, "supply_unit": order.SupplyUnit, "supply_license_number": order.SupplyLicenseNumber}).Error
  608. return err
  609. }
  610. func UpdateWarehouseCancel(id int64, cancel models.SpSupplierWarehouseCancel) error {
  611. err := XTWriteDB().Model(&cancel).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"rate_of_concession": cancel.RateOfConcession, "discount_amount": cancel.DiscountAmount, "document_date": cancel.DocumentDate, "supplier_id": cancel.SupplierId, "arrearage": cancel.Arrearage, "payment": cancel.Payment, "return_remark": cancel.ReturnRemark}).Error
  612. return err
  613. }
  614. func GetSupplyCancelOrderById(warehouse_out_id int64, orgid int64) (*models.SpSupplierWarehouseCancel, error) {
  615. cancel := models.SpSupplierWarehouseCancel{}
  616. var err error
  617. err = XTReadDB().Where("warehouse_out_id = ? and user_org_id = ? and status = 1", warehouse_out_id, orgid).Find(&cancel).Error
  618. if err == gorm.ErrRecordNotFound {
  619. return nil, err
  620. }
  621. if err != nil {
  622. return nil, err
  623. }
  624. return &cancel, nil
  625. }
  626. func UpdateSupplyGoodOrder(id int64, out models.SpSupplierWarehouseOut) error {
  627. err := XTWriteDB().Model(&models.SpSupplierWarehouseOut{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_check": out.IsCheck, "checker": out.Checker, "check_time": out.CheckTime}).Error
  628. return err
  629. }
  630. func UpdateDrugSupplyWarehousingInfo(id int64, orgid int64) error {
  631. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("supply_warehouse_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  632. return err
  633. }
  634. func UpdateDrugSupplyFlow(id int64, orgid int64) error {
  635. err := XTWriteDB().Model(&models.DrugFlow{}).Where("supply_warehouse_id = ? and user_org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  636. return err
  637. }
  638. func UpdateGoodSupplyWarehousingInfo(id int64, orgid int64) error {
  639. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("supply_warehouse_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  640. return err
  641. }
  642. func UpdateGoodSupplyFlow(id int64, orgid int64) error {
  643. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("supply_warehouse_id = ? and user_org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  644. return err
  645. }
  646. func GetDrugSupplyWarehousingById(id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  647. err = XTReadDB().Where("supply_warehouse_id <> ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  648. return info, err
  649. }
  650. func UpdateSupplyWarehousing(id int64, orgid int64) error {
  651. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("supply_warehouse_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  652. return err
  653. }
  654. func GetGoodSupplyWarehousingById(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  655. err = XTReadDB().Where("supply_warehouse_id <> ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  656. return info, err
  657. }
  658. func UpdateGoodWarehousing(id int64, orgid int64) error {
  659. err := XTWriteDB().Model(&models.Warehousing{}).Where("supply_warehouse_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  660. return err
  661. }
  662. func UpdateSupplyWarehousingById(id int64, orgid int64) error {
  663. err := XTWriteDB().Model(&models.SupplierWarehouseInfo{}).Where("id = ? and user_org_id =? and status = 1", id, orgid).Updates(map[string]interface{}{"is_warehouse": 1}).Error
  664. return err
  665. }
  666. func DeleteGoodOrder(id int64, orgid int64) error {
  667. err := XTWriteDB().Model(&models.SpSupplierWarehouseOut{}).Where("id = ? and status = 1 and user_org_id = ?", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  668. err = XTWriteDB().Model(&models.SpSupplierWarehousingOutOrder{}).Where("warehouse_out_id = ? and user_org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  669. return err
  670. }
  671. func GetSupplyCancelWarehouse(id int64, orgid int64) (cancel []*models.SpSupplierWarehouseCancel, err error) {
  672. err = XTReadDB().Where("warehouse_out_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&cancel).Error
  673. return cancel, err
  674. }
  675. func GetGoodOrderListById(id int64, orgid int64) (out []*models.VmSpSupplierWarehousingOutOrder, err error) {
  676. err = XTReadDB().Where("warehouse_out_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&out).Error
  677. return out, err
  678. }
  679. func GetGoodCanceListById(id int64, orgid int64) (cancel []*models.VmSpSupplierWarehousingCancelOrder, err error) {
  680. err = XTReadDB().Where("warehouse_out_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&cancel).Error
  681. return cancel, err
  682. }
  683. func CreateDrugFlowSix(drugflow []*models.DrugFlow) (err error) {
  684. if len(drugflow) > 0 {
  685. utx := writeDb.Begin()
  686. if len(drugflow) > 0 {
  687. thisSQL := "INSERT INTO xt_drug_flow (warehousing_id, drug_id, number,batch_number,count,user_org_id,patient_id,system_time,consumable_type,is_sys,warehousing_order,warehouse_out_id,warehouse_out_order_number,is_edit,cancel_stock_id,cancel_order_number,manufacturer,dealer,creator,update_creator,status,ctime,mtime,price,warehousing_detail_id,warehouse_out_detail_id,cancel_out_detail_id,expire_date,product_date,max_unit,min_unit,supply_warehouse_id,supply_warehouse_detail_info) VALUES "
  688. insertParams := make([]string, 0)
  689. insertData := make([]interface{}, 0)
  690. for _, info := range drugflow {
  691. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  692. insertData = append(insertData, info.WarehousingId)
  693. insertData = append(insertData, info.DrugId)
  694. insertData = append(insertData, info.Number)
  695. insertData = append(insertData, info.BatchNumber)
  696. insertData = append(insertData, info.Count)
  697. insertData = append(insertData, info.UserOrgId)
  698. insertData = append(insertData, info.PatientId)
  699. insertData = append(insertData, info.SystemTime)
  700. insertData = append(insertData, info.ConsumableType)
  701. insertData = append(insertData, info.IsSys)
  702. insertData = append(insertData, info.WarehousingOrder)
  703. insertData = append(insertData, info.WarehouseOutId)
  704. insertData = append(insertData, info.WarehouseOutOrderNumber)
  705. insertData = append(insertData, info.IsEdit)
  706. insertData = append(insertData, info.CancelStockId)
  707. insertData = append(insertData, info.CancelOrderNumber)
  708. insertData = append(insertData, info.Manufacturer)
  709. insertData = append(insertData, info.Dealer)
  710. insertData = append(insertData, info.Creator)
  711. insertData = append(insertData, info.UpdateCreator)
  712. insertData = append(insertData, info.Status)
  713. insertData = append(insertData, info.Ctime)
  714. insertData = append(insertData, info.Mtime)
  715. insertData = append(insertData, info.Price)
  716. insertData = append(insertData, info.WarehousingDetailId)
  717. insertData = append(insertData, info.WarehouseOutDetailId)
  718. insertData = append(insertData, info.CancelOutDetailId)
  719. insertData = append(insertData, info.ExpireDate)
  720. insertData = append(insertData, info.ProductDate)
  721. insertData = append(insertData, info.MaxUnit)
  722. insertData = append(insertData, info.MinUnit)
  723. insertData = append(insertData, info.SupplyWarehouseId)
  724. insertData = append(insertData, info.SupplyWarehouseDetailInfo)
  725. }
  726. thisSQL += strings.Join(insertParams, ", ")
  727. err = utx.Exec(thisSQL, insertData...).Error
  728. if err != nil {
  729. utx.Rollback()
  730. return
  731. }
  732. }
  733. utx.Commit()
  734. }
  735. return
  736. }
  737. func CheckReturnOrder(id int64, orgid int64, cancel models.SpSupplierWarehouseCancel) error {
  738. err := XTWriteDB().Model(&cancel).Where("id = ? and user_org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"is_check": cancel.IsCheck, "checker": cancel.Checker, "check_time": cancel.CheckTime}).Error
  739. return err
  740. }
  741. func GetSupplyCancelOrderDetail(id int64, orgid int64) (cancel []*models.SpSupplierWarehousingCancelOrder, err error) {
  742. err = XTReadDB().Where("warehouse_cancel_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&cancel).Error
  743. return cancel, err
  744. }
  745. func DeletePurchaseOrder(id int64) error {
  746. err := XTWriteDB().Model(&models.SupplierWarehousingInfoOrder{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  747. return err
  748. }
  749. func DeleteGoodOrderById(id int64) error {
  750. err := XTWriteDB().Model(&models.SpSupplierWarehousingOutOrder{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  751. return err
  752. }
  753. func GetlastWarehouseOutById(user_org_id int64, record_date int64) (models.WarehouseOut, error) {
  754. out := models.WarehouseOut{}
  755. err := XTReadDB().Where("org_id = ? and warehouse_out_time = ? and status = 1", user_org_id, record_date).Find(&out).Error
  756. return out, err
  757. }
  758. func GetDrugWarehouseOutById(orgid int64, record_date int64) (*models.DrugWarehouseOut, error) {
  759. out := models.DrugWarehouseOut{}
  760. var err error
  761. err = XTReadDB().Where("org_id = ? and warehouse_out_time = ?", orgid, record_date).Find(&out).Error
  762. if err == gorm.ErrRecordNotFound {
  763. return nil, err
  764. }
  765. if err != nil {
  766. return nil, err
  767. }
  768. return &out, nil
  769. }
  770. func GetLastDrugWarehouseById(orgid int64, record_date int64) (models.DrugWarehouseOut, error) {
  771. out := models.DrugWarehouseOut{}
  772. err := XTReadDB().Where("org_id = ? and warehouse_out_time =? and status = 1", orgid, record_date).Find(&out).Error
  773. return out, err
  774. }
  775. func UpdateSupplyCancelById(id int64, order models.SpSupplierWarehousingCancelOrder) error {
  776. err := XTReadDB().Model(&order).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"source_count": order.SourceCount}).Error
  777. return err
  778. }
  779. func DeleteReturnOrder(id int64) error {
  780. err := XTWriteDB().Model(&models.SpSupplierWarehouseCancel{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  781. err = XTWriteDB().Model(&models.SpSupplierWarehousingCancelOrder{}).Where("warehouse_cancel_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  782. return err
  783. }
  784. func DeleteReturnOrderById(id int64) error {
  785. err = XTWriteDB().Model(&models.SpSupplierWarehousingCancelOrder{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  786. return err
  787. }
  788. func ModefyReturnOrder(id int64) error {
  789. cancel := models.SpSupplierWarehouseCancel{}
  790. err := XTWriteDB().Model(&cancel).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_check": 2, "checker": 0, "check_time": 0}).Error
  791. return err
  792. }
  793. func UpdateWarehousingInfoById(goodid int64, supply_warehouse_id int64, info models.WarehousingInfo) error {
  794. err := XTWriteDB().Model(&info).Where("good_id = ? and supply_warehouse_detail_info = ? and status = 1", goodid, supply_warehouse_id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", info.StockCount)).Error
  795. return err
  796. }
  797. func DeleteGoodWarehouseOut(goodid int64, supply_warehouse_id int64) error {
  798. err := XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("good_id = ? and supply_cancel_out_id = ? and status = 1", goodid, supply_warehouse_id).Updates(map[string]interface{}{"status": 0}).Error
  799. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("good_id = ? and supply_cancel_out_id = ? and status = 1", goodid, supply_warehouse_id).Updates(map[string]interface{}{"status": 0}).Error
  800. return err
  801. }
  802. func UpdateDrugWasehousring(goodid int64, supply_warehouse_id int64, info models.DrugWarehouseInfo) error {
  803. err := XTWriteDB().Model(&info).Where("good_id = ? and supply_cancel_out_id = ? and status = 1", goodid, supply_warehouse_id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", info.StockMaxNumber)).Error
  804. return err
  805. }
  806. func UpdateDrugWasehousringOne(goodid int64, supply_warehouse_id int64, info models.DrugWarehouseInfo) error {
  807. err := XTWriteDB().Model(&info).Where("good_id = ? and supply_cancel_out_id = ? and status = 1", goodid, supply_warehouse_id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", info.StockMinNumber)).Error
  808. return err
  809. }
  810. func DeleteDrugWarehouseOutNight(goodid int64, supply_warehouse_id int64) error {
  811. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("good_id = ? and supply_cancel_out_id = ? and status = 1", goodid, supply_warehouse_id).Updates(map[string]interface{}{"status": 0}).Error
  812. err = XTWriteDB().Model(&models.DrugFlow{}).Where("good_id = ? and supply_cancel_out_id = ? and status = 1", goodid, supply_warehouse_id).Updates(map[string]interface{}{"status": 0}).Error
  813. return err
  814. }