supply_service.go 43KB

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