supply_service.go 51KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  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("left join xt_supplier_contacts on xt_supplier_contacts.supplier_code = xt_supplier_name.supplier_code")
  21. db = db.Where("xt_supplier_contacts.status = 1 and xt_supplier_contacts.user_org_id = ? and xt_supplier_contacts.name like ? or xt_supplier_name.supplier_code like ? or xt_supplier_name.supplier_name like ? ", orgid, 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) (shiwu string, err error) {
  160. tx := XTWriteDB().Begin()
  161. defer func() {
  162. if err != nil {
  163. shiwu = "失败"
  164. tx.Rollback()
  165. } else {
  166. shiwu = "成功"
  167. tx.Commit()
  168. }
  169. }()
  170. //删除供应商
  171. err = tx.Model(&supply).Update("status", 0).Error
  172. if err != nil {
  173. return shiwu, err
  174. }
  175. var spcode models.SpSupplierName
  176. //获取供应商编号
  177. err = tx.Model(&models.SpSupplierName{}).Select("supplier_code").Where("user_org_id = ? and id = ?", orgId, supply.ID).First(&spcode).Error
  178. if err != nil {
  179. return shiwu, err
  180. }
  181. //删除联系人
  182. err = tx.Model(&models.SpSupplierContacts{}).Where("supplier_code = ? and user_org_id = ?", spcode.SupplierCode, orgId).Update("status", 0).Error
  183. return shiwu, err
  184. }
  185. //保存供应商和联系人
  186. func SaveSupplyAndContact(thisStockIn []interface{}, orgId, supplierType, tcreater int64, supplierCode, supplierName, number, bank, bankAccount string, vatRate float64) error {
  187. tx := XTWriteDB().Begin()
  188. defer func() {
  189. if err != nil {
  190. tx.Rollback()
  191. } else {
  192. tx.Commit()
  193. }
  194. }()
  195. for _, item := range thisStockIn {
  196. items := item.(map[string]interface{})
  197. name := items["name"].(string)
  198. phone := items["phone"].(string)
  199. address := items["address"].(string)
  200. //isfirst := int64(items["is_first"].(float64))
  201. var tmpfirst int64
  202. //tmptype := reflect.TypeOf(items["is_first"])
  203. switch items["is_first"].(type) {
  204. case int:
  205. var aint int = items["is_first"].(int)
  206. tmpfirst = int64(aint)
  207. case float64:
  208. tmpfirst = int64(items["is_first"].(float64))
  209. }
  210. isfirst := tmpfirst
  211. spcontacts := models.SpSupplierContacts{
  212. Name: name,
  213. Phone: phone,
  214. Address: address,
  215. IsFirst: isfirst,
  216. SupplierCode: supplierCode,
  217. UserOrgId: orgId,
  218. Status: 1,
  219. Ctime: time.Now().Unix(),
  220. Mtime: 0,
  221. }
  222. err = SaveContacts(spcontacts, tx)
  223. if isfirst == 1 {
  224. var spconid []*models.SpSupplierContacts
  225. spconid, err = SaveContactsId(tx, orgId)
  226. if err != nil {
  227. return err
  228. }
  229. tmpid := spconid[0].ID
  230. //保存供应商
  231. supply := models.SpSupplierName{
  232. SupplierCode: supplierCode,
  233. SupplierName: supplierName,
  234. SupplierType: supplierType,
  235. VatRate: vatRate,
  236. Number: number,
  237. Bank: bank,
  238. BankAccount: bankAccount,
  239. UserOrgId: orgId,
  240. Status: 1,
  241. ContactsId: tmpid,
  242. Ctime: time.Now().Unix(),
  243. Mtime: 0,
  244. Creater: tcreater,
  245. Modify: tcreater,
  246. }
  247. err = SaveSupplyTx(supply, tx)
  248. if err != nil {
  249. return err
  250. }
  251. }
  252. if err != nil {
  253. return err
  254. }
  255. }
  256. return err
  257. }
  258. //获取供应商编码
  259. func GetSuppliyCode(orgId int64) (spcode []*models.SpSupplierName, err error) {
  260. 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
  261. return spcode, err
  262. }
  263. //查询供应商的名字是否有重复
  264. func FindSupplierName(supplierName string, orgId int64) (sbool bool, err error) {
  265. var total int
  266. err = XTReadDB().Model(&models.SpSupplierName{}).Where("supplier_name = ? and user_org_id = ? and status = 1", supplierName, orgId).Count(&total).Error
  267. if total != 0 {
  268. sbool = true
  269. } else {
  270. sbool = false
  271. }
  272. return sbool, err
  273. }
  274. //查询供应商的编号是否有重复(用于修改)
  275. func FindSupplierCode(supplierCode string, supplierid, orgid int64) (codebool bool, err error) {
  276. var total int
  277. err = XTReadDB().Model(&models.SpSupplierName{}).Where("supplier_code = ? and id != ? and user_org_id = ? and status = 1", supplierCode, supplierid, orgid).Count(&total).Error
  278. if total > 0 {
  279. codebool = true
  280. } else {
  281. codebool = false
  282. }
  283. return codebool, err
  284. }
  285. //查询供应商的编号是否有重复(用于新增)
  286. func FindSupplierCodes(supplierCode string, orgid int64) (codebool bool, err error) {
  287. var total int
  288. err = XTReadDB().Model(&models.SpSupplierName{}).Where("supplier_code = ? and status = 1 and user_org_id = ?", supplierCode, orgid).Count(&total).Error
  289. if total > 0 {
  290. codebool = true
  291. } else {
  292. codebool = false
  293. }
  294. return codebool, err
  295. }
  296. //保存一条供应商数据加事务的
  297. func SaveSupplyTx(supply models.SpSupplierName, tx *gorm.DB) error {
  298. err := tx.Create(&supply).Error
  299. return err
  300. }
  301. //保存一条供应商数据没有事务的
  302. func SaveSupply(supply models.SpSupplierName) error {
  303. err := XTWriteDB().Create(&supply).Error
  304. return err
  305. }
  306. //获取联系人的id
  307. func SaveContactsId(tx *gorm.DB, orgid int64) (spconid []*models.SpSupplierContacts, err error) {
  308. err = tx.Model(&models.SpSupplierContacts{}).Select("id").Where("status = 1 and user_org_id = ?", orgid).Order("id desc").First(&spconid).Error
  309. return
  310. }
  311. //保存一条联系人数据
  312. func SaveContacts(spcontacts models.SpSupplierContacts, tx *gorm.DB) error {
  313. err := tx.Create(&spcontacts).Error
  314. return err
  315. }
  316. func GetSupplyDrugList(orgid int64) (drug []*models.SpBaseDrug, err error) {
  317. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1 AND find_in_set('停用',drug_status) = 0")
  318. if orgid > 0 {
  319. db = db.Where("x.org_id = ?", orgid)
  320. }
  321. err = db.Preload("DrugWarehouseInfo", "status = 1 and org_id = ? and (stock_max_number >0 or stock_min_number>0)", orgid).Find(&drug).Error
  322. return drug, err
  323. }
  324. func GetSupplyGoodList(orgid int64) (good []*models.SpGoodInformation, err error) {
  325. db := XTReadDB().Table("xt_good_information as x").Where("x.status = 1 AND find_in_set('停用',good_status) = 0")
  326. if orgid > 0 {
  327. db = db.Where("x.org_id = ?", orgid)
  328. }
  329. err = db.Preload("GoodWarehouseInfo", "status = 1 and org_id = ?", orgid).Find(&good).Error
  330. return good, err
  331. }
  332. func GetSupplierList(orgid int64) (suppler []*models.SpSupplierName, err error) {
  333. err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&suppler).Error
  334. return suppler, err
  335. }
  336. func FindAllSupplyOrder(orgid int64) (total int64, err error) {
  337. err = XTReadDB().Model(&models.SupplierWarehouseInfo{}).Where("user_org_id = ?", orgid).Count(&total).Error
  338. return total, err
  339. }
  340. func CreateSupplyWarehouse(info models.SupplierWarehouseInfo) error {
  341. err := XTWriteDB().Create(&info).Error
  342. return err
  343. }
  344. func FindLastSupplyWarehouseInfo(orgid int64) (models.SupplierWarehouseInfo, error) {
  345. info := models.SupplierWarehouseInfo{}
  346. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Last(&info).Error
  347. return info, err
  348. }
  349. func CreateSupplyWarehousingOrder(order *models.SupplierWarehousingInfoOrder) error {
  350. err := XTWriteDB().Create(&order).Error
  351. return err
  352. }
  353. func GetAllPurchaseOrderList(check_id int64, startime int64, endtime int64, keyword string, page int64, limit int64, orgid int64) (info []*models.VmSupplierWarehouseInfo, total int64, err error) {
  354. db := XTReadDB().Model(&info).Where("sgj_xt.xt_supplier_warehouse_info.status = 1")
  355. likeKey := "%" + keyword + "%"
  356. offset := (page - 1) * limit
  357. if check_id > 0 {
  358. db = db.Where("sgj_xt.xt_supplier_warehouse_info.is_check = ?", check_id)
  359. }
  360. if startime > 0 {
  361. db = db.Where("sgj_xt.xt_supplier_warehouse_info.record_date >= ?", startime)
  362. }
  363. if endtime > 0 {
  364. db = db.Where("sgj_xt.xt_supplier_warehouse_info.record_date<=?", endtime)
  365. }
  366. if len(keyword) > 0 {
  367. db = db.Joins("join sgj_xt.xt_supplier_name on sgj_xt.xt_supplier_name.id = sgj_xt.xt_supplier_warehouse_info.supplier_id")
  368. 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")
  369. }
  370. if orgid > 0 {
  371. db = db.Where("sgj_xt.xt_supplier_warehouse_info.user_org_id = ?", orgid)
  372. }
  373. err = db.Count(&total).Offset(offset).Limit(limit).Preload("SupplierWarehousingInfoOrder", "status= 1 and user_org_id = ?", orgid).Preload("SpSupplierWarehouseOut", func(db *gorm.DB) *gorm.DB {
  374. return db.Where("status = 1").Preload("SpSupplierWarehousingOutOrder", "status= 1 and user_org_id = ?", orgid)
  375. }).Order("ctime desc").Find(&info).Error
  376. return info, total, err
  377. }
  378. func GetSupplyWarehousingOrderInfo(id int64) (order []*models.SupplierWarehousingInfoOrder, err error) {
  379. err = XTReadDB().Where("warehousing_id = ? and status = 1", id).Find(&order).Error
  380. return order, err
  381. }
  382. func GetSupplyWarehousingOrderInfoTwo(id int64, ids []string) (order []*models.SupplierWarehousingInfoOrder, err error) {
  383. err = XTReadDB().Where("warehousing_id = ? and status = 1 and project_id in(?)", id, ids).Find(&order).Error
  384. return order, err
  385. }
  386. func ModefySupplyWarehouseInfo(id int64, info models.SupplierWarehouseInfo) error {
  387. 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
  388. return err
  389. }
  390. func ModifySupplyWarehouseOrder(order *models.SupplierWarehousingInfoOrder) error {
  391. 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
  392. return err
  393. }
  394. func UpdateSupplyWaresing(id int64, info models.SupplierWarehouseInfo) error {
  395. 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
  396. return err
  397. }
  398. func GetPurchaseOrderDetail(id int64) (models.SupplierWarehouseInfo, error) {
  399. info := models.SupplierWarehouseInfo{}
  400. err := XTReadDB().Model(&info).Where("id =? and status =1", id).Find(&info).Error
  401. return info, err
  402. }
  403. func FindAllSupplyWarehouseOutOrder(orgid int64) (total int64, err error) {
  404. err = XTReadDB().Model(&models.SpSupplierWarehouseOut{}).Where("user_org_id = ?", orgid).Count(&total).Error
  405. return total, err
  406. }
  407. func FindSupplyWarehouseOutById(orgid int64) (models.SpSupplierWarehouseOut, error) {
  408. out := models.SpSupplierWarehouseOut{}
  409. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Last(&out).Error
  410. return out, err
  411. }
  412. func CreateSupplyWarehouseOut(out models.SpSupplierWarehouseOut) error {
  413. err := XTWriteDB().Create(&out).Error
  414. return err
  415. }
  416. func CreateSupplyWarehousOutOrder(order *models.SpSupplierWarehousingOutOrder) error {
  417. err := XTWriteDB().Create(&order).Error
  418. return err
  419. }
  420. func GetSupplyWarehouseOutById(id int64, user_org_id int64) (order []*models.SpSupplierWarehousingOutOrder, err error) {
  421. err = XTReadDB().Where("warehouse_out_id = ? and status = 1 and user_org_id = ?", id, user_org_id).Find(&order).Error
  422. return order, err
  423. }
  424. func GetSupplyWarehouseOutByIdOne(id int64, user_org_id int64, ids []string) (order []*models.SpSupplierWarehousingOutOrder, err error) {
  425. 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
  426. return order, err
  427. }
  428. func GetAllGoodOderList(check_id int64, keyword string, page int64, limit int64, startime int64, endtime int64, orgid int64) (out []*models.VmSupplierWarehouseOut, total int64, err error) {
  429. db := XTReadDB().Model(&out).Where("sgj_xt.xt_supplier_warehouse_out.status = 1")
  430. likeKey := "%" + keyword + "%"
  431. offset := (page - 1) * limit
  432. if check_id > 0 {
  433. db = db.Where("sgj_xt.xt_supplier_warehouse_out.is_check = ?", check_id)
  434. }
  435. if startime > 0 {
  436. db = db.Where("sgj_xt.xt_supplier_warehouse_out.record_date >= ?", startime)
  437. }
  438. if endtime > 0 {
  439. db = db.Where("sgj_xt.xt_supplier_warehouse_out.record_date<=?", endtime)
  440. }
  441. if len(keyword) > 0 {
  442. db = db.Joins("join sgj_xt.xt_supplier_name on sgj_xt.xt_supplier_name.id = sgj_xt.xt_supplier_warehouse_out.supplier_id")
  443. 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")
  444. }
  445. if orgid > 0 {
  446. db = db.Where("sgj_xt.xt_supplier_warehouse_out.user_org_id = ?", orgid)
  447. }
  448. 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
  449. return out, total, err
  450. }
  451. func GetGoodOrderDetail(id int64, orgid int64) (models.SpSupplierWarehouseOut, error) {
  452. out := models.SpSupplierWarehouseOut{}
  453. err := XTReadDB().Where("id = ? and user_org_id = ? and status = 1", id, orgid).Find(&out).Error
  454. return out, err
  455. }
  456. func UpdateGoodWarehouseOut(id int64, out models.SpSupplierWarehouseOut) error {
  457. 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
  458. return err
  459. }
  460. func UpdateGoodWarehouseOutOrder(order *models.SpSupplierWarehousingOutOrder) error {
  461. 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
  462. return err
  463. }
  464. func DeletePurchOrder(id int64, orgid int64) error {
  465. err := XTWriteDB().Model(&models.SupplierWarehouseInfo{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  466. 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
  467. return err
  468. }
  469. func GetAllPurcaseOrderById(id int64, orgid int64) (order []*models.VSupplierWarehousingInfoOrder, err error) {
  470. err = XTReadDB().Model(&order).Where("warehousing_id = ? and user_org_id = ? and status =1", id, orgid).Find(&order).Error
  471. return order, err
  472. }
  473. func GetAllGoodOrderById(id int64, orgid int64) (order []*models.VSpSupplierWarehousingOutOrder, err error) {
  474. db := XTReadDB().Table("xt_supplier_warehousing_out_order as o").Where("o.status = 1")
  475. if id > 0 {
  476. db = db.Where("o.warehouse_out_id = ?", id)
  477. }
  478. if orgid > 0 {
  479. db = db.Where("o.user_org_id =? and o.is_warehouse= 1", orgid)
  480. }
  481. err = db.Select("o.id,o.order_number,o.project_id,o.count as count,o.supply_unit,o.is_source,o.warehousing_id,o.warehouse_out_id").Find(&order).Error
  482. return order, err
  483. }
  484. func GetAllGoodOrderByIdSix(id int64, orgid int64) (order []*models.VSpSupplierWarehousingOutOrder, err error) {
  485. db := XTReadDB().Table("xt_supplier_warehousing_out_order as o").Where("o.status = 1")
  486. if id > 0 {
  487. db = db.Where("o.warehousing_id = ?", id)
  488. }
  489. if orgid > 0 {
  490. db = db.Where("o.user_org_id =? and o.is_warehouse= 1", orgid)
  491. }
  492. err = db.Select("o.id,o.order_number,o.project_id,o.count as count,o.supply_unit,o.is_source").Find(&order).Error
  493. return order, err
  494. }
  495. func GetAllGoodOrderByIdTwo(id int64, orgid int64) (order []*models.SpSupplierWarehousingOutOrder, err error) {
  496. err = XTReadDB().Where("warehouse_out_id = ? and user_org_id = ? and status = 1 and is_warehouse = 2", id, orgid).Find(&order).Error
  497. return order, err
  498. }
  499. func GetGoodOrderList(id int64, orgid int64) (info []*models.SpSupplierWarehouseOut, err error) {
  500. err = XTReadDB().Where("warehousing_id = ? and status = 1 and user_org_id = ?", id, orgid).Find(&info).Error
  501. return info, err
  502. }
  503. func GetReturnOrder(id int64, orgid int64) error {
  504. 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
  505. return err
  506. }
  507. func CheckGoodOrder(id int64, orgid int64, out models.SpSupplierWarehouseOut) error {
  508. 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
  509. return err
  510. }
  511. func ModefySupplyWarehousing(is_warehose int64, warehousing_id int64, orgid int64) error {
  512. 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
  513. return err
  514. }
  515. func GetAllDoctorSix(orgid int64, appid int64) (appRole []*models.App_Role, err error) {
  516. err = UserReadDB().Where("org_id = ? AND app_id = ? AND status = 1", orgid, appid).Find(&appRole).Error
  517. return appRole, err
  518. }
  519. func GetSingleDrugWarehouseOrder(record_date int64, orgid int64) (*models.DrugWarehouse, error) {
  520. warehouse := models.DrugWarehouse{}
  521. var err error
  522. err = XTReadDB().Model(&warehouse).Where("warehousing_time = ? and org_id = ? and status = 1", record_date, orgid).Find(&warehouse).Error
  523. if err == gorm.ErrRecordNotFound {
  524. return nil, err
  525. }
  526. if err != nil {
  527. return nil, err
  528. }
  529. return &warehouse, nil
  530. }
  531. func GetSindleWarehouse(record_date int64, orgid int64) (*models.Warehousing, error) {
  532. warehousing := models.Warehousing{}
  533. var err error
  534. err = XTReadDB().Model(&warehousing).Where("warehousing_time = ? and org_id = ? and status = 1", record_date, orgid).Find(&warehousing).Error
  535. if err == gorm.ErrRecordNotFound {
  536. return nil, err
  537. }
  538. if err != nil {
  539. return nil, err
  540. }
  541. return &warehousing, nil
  542. }
  543. func GetLastWarehouseInfoByInfo(orgid int64) (models.Warehousing, error) {
  544. warehousing := models.Warehousing{}
  545. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&warehousing).Error
  546. return warehousing, err
  547. }
  548. func GetSupplyCancelOrder(orgid int64) (total int64, err error) {
  549. err = XTReadDB().Model(&models.SpSupplierWarehouseCancel{}).Where("user_org_id = ?", orgid).Count(&total).Error
  550. return total, err
  551. }
  552. func CreateReturnCacelOrder(cancel models.SpSupplierWarehouseCancel) error {
  553. err := XTWriteDB().Create(&cancel).Error
  554. return err
  555. }
  556. func GetLastReturnCancelOrder(orgid int64) (models.SpSupplierWarehouseCancel, error) {
  557. cancel := models.SpSupplierWarehouseCancel{}
  558. err := XTReadDB().Where("user_org_id =? and status = 1", orgid).Find(&cancel).Error
  559. return cancel, err
  560. }
  561. func CreateCancelReturnOrder(order *models.SpSupplierWarehousingCancelOrder) error {
  562. err := XTWriteDB().Create(&order).Error
  563. return err
  564. }
  565. func GetReturnCancelOrder(id int64, orgid int64) (models.SpSupplierWarehouseCancel, error) {
  566. cancel := models.SpSupplierWarehouseCancel{}
  567. err := XTReadDB().Where("id = ? and status= 1 and user_org_id =?", id, orgid).Find(&cancel).Error
  568. return cancel, err
  569. }
  570. func GetReturnCancelOrderList(id int64, orgid int64) (order []*models.SpSupplierWarehousingCancelOrder, err error) {
  571. err = XTReadDB().Where("warehouse_cancel_id = ? and user_org_id =? and status = 1", id, orgid).Find(&order).Error
  572. return order, err
  573. }
  574. func GetAllGoodReturnOrderList(checkid int64, keyword string, page int64, limit int64, startime int64, endtime int64, orgid int64) (order []*models.VmSpSupplierWarehouseCancel, total int64, err error) {
  575. db := XTReadDB().Table("sgj_xt.xt_supplier_warehouse_cancel").Where("sgj_xt.xt_supplier_warehouse_cancel.status = 1")
  576. likeKey := "%" + keyword + "%"
  577. offset := (page - 1) * limit
  578. if checkid > 0 {
  579. db = db.Where("sgj_xt.xt_supplier_warehouse_cancel.is_check = ?", checkid)
  580. }
  581. if len(keyword) > 0 {
  582. db = db.Joins("join sgj_xt.xt_supplier_name on sgj_xt.xt_supplier_name.id = sgj_xt.xt_supplier_warehouse_cancel.supplier_id")
  583. 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")
  584. }
  585. if startime > 0 {
  586. db = db.Where("sgj_xt.xt_supplier_warehouse_cancel.record_date >= ?", startime)
  587. }
  588. if endtime > 0 {
  589. db = db.Where("sgj_xt.xt_supplier_warehouse_cancel.record_date <= ?", endtime)
  590. }
  591. if orgid > 0 {
  592. db = db.Where("sgj_xt.xt_supplier_warehouse_cancel.user_org_id = ?", orgid)
  593. }
  594. err = db.Count(&total).Offset(offset).Limit(limit).Preload("SpSupplierWarehousingCancelOrder", "status= 1 and user_org_id = ?", orgid).Order("ctime desc").Find(&order).Error
  595. return order, total, err
  596. }
  597. func GetGoodReturnDetail(id int64, orgid int64) (models.VmSpSupplierWarehouseCancel, error) {
  598. cancel := models.VmSpSupplierWarehouseCancel{}
  599. db := XTReadDB().Model(&cancel).Where("status = 1")
  600. if id > 0 {
  601. db = db.Where("id = ?", id)
  602. }
  603. if orgid > 0 {
  604. db = db.Where("user_org_id = ?", orgid)
  605. }
  606. err := db.Find(&cancel).Error
  607. return cancel, err
  608. }
  609. func GetGoodReturnOrderDetail(id int64, orgid int64) (order []*models.SpSupplierWarehousingCancelOrder, err error) {
  610. db := XTReadDB().Model(&order).Where("status =1")
  611. if id > 0 {
  612. db = db.Where("warehouse_cancel_id = ?", id)
  613. }
  614. if orgid > 0 {
  615. db = db.Where("user_org_id = ?", orgid)
  616. }
  617. err = db.Find(&order).Error
  618. return order, err
  619. }
  620. func UpdateWarehouseCancelOrder(order *models.SpSupplierWarehousingCancelOrder) error {
  621. 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, "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, "supply_count": order.SupplyCount, "deposit_rate": order.DepositRate}).Error
  622. return err
  623. }
  624. func UpdateWarehouseCancel(id int64, cancel models.SpSupplierWarehouseCancel) error {
  625. 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
  626. return err
  627. }
  628. func GetSupplyCancelOrderById(warehouse_out_id int64, orgid int64) (*models.SpSupplierWarehouseCancel, error) {
  629. cancel := models.SpSupplierWarehouseCancel{}
  630. var err error
  631. err = XTReadDB().Where("warehouse_out_id = ? and user_org_id = ? and status = 1", warehouse_out_id, orgid).Find(&cancel).Error
  632. if err == gorm.ErrRecordNotFound {
  633. return nil, err
  634. }
  635. if err != nil {
  636. return nil, err
  637. }
  638. return &cancel, nil
  639. }
  640. func UpdateSupplyGoodOrder(id int64, out models.SpSupplierWarehouseOut) error {
  641. 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
  642. return err
  643. }
  644. func GetSupplySupplyWarehouseId(id int64, orgid int64) (models.WarehousingInfo, error) {
  645. info := models.WarehousingInfo{}
  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 GetSupplySupplyWarehouseIdSeven(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  650. err = XTReadDB().Where("supply_warehouse_id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  651. return info, err
  652. }
  653. func GetDrugSupplyWarehouseId(id int64, orgid int64) (models.DrugWarehouseInfo, error) {
  654. info := models.DrugWarehouseInfo{}
  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 GetDrugSupplyWarehouseIdSeven(id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  659. err = XTReadDB().Where("supply_warehouse_id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  660. return info, err
  661. }
  662. func UpdateDrugSupplyWarehousingInfo(id int64, orgid int64) error {
  663. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("supply_warehouse_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  664. return err
  665. }
  666. func UpdateDrugSupplyFlow(id int64, orgid int64) error {
  667. 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
  668. return err
  669. }
  670. func UpdateGoodSupplyWarehousingInfo(id int64, orgid int64) error {
  671. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("supply_warehouse_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  672. return err
  673. }
  674. func UpdateGoodSupplyFlow(id int64, orgid int64) error {
  675. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("supply_warehouse_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  676. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("supply_warehouse_id =? and user_org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  677. return err
  678. }
  679. func GetDrugSupplyWarehousingById(id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  680. err = XTReadDB().Where("supply_warehouse_id <> ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  681. return info, err
  682. }
  683. func UpdateSupplyWarehousing(id int64, orgid int64) error {
  684. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("supply_warehouse_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  685. return err
  686. }
  687. func GetGoodSupplyWarehousingById(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  688. err = XTReadDB().Where("supply_warehouse_id <> ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  689. return info, err
  690. }
  691. func UpdateGoodWarehousing(id int64, orgid int64) error {
  692. err := XTWriteDB().Model(&models.Warehousing{}).Where("supply_warehouse_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  693. return err
  694. }
  695. func UpdateSupplyWarehousingById(id int64, orgid int64) error {
  696. err := XTWriteDB().Model(&models.SupplierWarehouseInfo{}).Where("id = ? and user_org_id =? and status = 1", id, orgid).Updates(map[string]interface{}{"is_warehouse": 1}).Error
  697. return err
  698. }
  699. func DeleteGoodOrder(id int64, orgid int64) error {
  700. err := XTWriteDB().Model(&models.SpSupplierWarehouseOut{}).Where("id = ? and status = 1 and user_org_id = ?", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  701. 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
  702. return err
  703. }
  704. func GetSupplyCancelWarehouse(id int64, orgid int64) (cancel []*models.SpSupplierWarehouseCancel, err error) {
  705. err = XTReadDB().Where("warehouse_out_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&cancel).Error
  706. return cancel, err
  707. }
  708. func GetGoodOrderListById(id int64, orgid int64) (out []*models.VmSpSupplierWarehousingOutOrder, err error) {
  709. err = XTReadDB().Where("warehouse_out_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&out).Error
  710. return out, err
  711. }
  712. func GetGoodCanceListById(id int64, orgid int64) (cancel []*models.VmSpSupplierWarehousingCancelOrder, err error) {
  713. err = XTReadDB().Where("warehouse_out_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&cancel).Error
  714. return cancel, err
  715. }
  716. func CreateDrugFlowSix(drugflow []*models.DrugFlow) (err error) {
  717. if len(drugflow) > 0 {
  718. utx := writeDb.Begin()
  719. if len(drugflow) > 0 {
  720. 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,storehouse_id) VALUES "
  721. insertParams := make([]string, 0)
  722. insertData := make([]interface{}, 0)
  723. for _, info := range drugflow {
  724. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  725. insertData = append(insertData, info.WarehousingId)
  726. insertData = append(insertData, info.DrugId)
  727. insertData = append(insertData, info.Number)
  728. insertData = append(insertData, info.BatchNumber)
  729. insertData = append(insertData, info.Count)
  730. insertData = append(insertData, info.UserOrgId)
  731. insertData = append(insertData, info.PatientId)
  732. insertData = append(insertData, info.SystemTime)
  733. insertData = append(insertData, info.ConsumableType)
  734. insertData = append(insertData, info.IsSys)
  735. insertData = append(insertData, info.WarehousingOrder)
  736. insertData = append(insertData, info.WarehouseOutId)
  737. insertData = append(insertData, info.WarehouseOutOrderNumber)
  738. insertData = append(insertData, info.IsEdit)
  739. insertData = append(insertData, info.CancelStockId)
  740. insertData = append(insertData, info.CancelOrderNumber)
  741. insertData = append(insertData, info.Manufacturer)
  742. insertData = append(insertData, info.Dealer)
  743. insertData = append(insertData, info.Creator)
  744. insertData = append(insertData, info.UpdateCreator)
  745. insertData = append(insertData, info.Status)
  746. insertData = append(insertData, info.Ctime)
  747. insertData = append(insertData, info.Mtime)
  748. insertData = append(insertData, info.Price)
  749. insertData = append(insertData, info.WarehousingDetailId)
  750. insertData = append(insertData, info.WarehouseOutDetailId)
  751. insertData = append(insertData, info.CancelOutDetailId)
  752. insertData = append(insertData, info.ExpireDate)
  753. insertData = append(insertData, info.ProductDate)
  754. insertData = append(insertData, info.MaxUnit)
  755. insertData = append(insertData, info.MinUnit)
  756. insertData = append(insertData, info.SupplyWarehouseId)
  757. insertData = append(insertData, info.SupplyWarehouseDetailInfo)
  758. insertData = append(insertData, info.StorehouseId)
  759. }
  760. thisSQL += strings.Join(insertParams, ", ")
  761. err = utx.Exec(thisSQL, insertData...).Error
  762. if err != nil {
  763. utx.Rollback()
  764. return
  765. }
  766. }
  767. utx.Commit()
  768. }
  769. return
  770. }
  771. func CheckReturnOrder(id int64, orgid int64, cancel models.SpSupplierWarehouseCancel) error {
  772. 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
  773. return err
  774. }
  775. func GetSupplyCancelOrderDetail(id int64, orgid int64) (cancel []*models.SpSupplierWarehousingCancelOrder, err error) {
  776. err = XTReadDB().Where("warehouse_cancel_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&cancel).Error
  777. return cancel, err
  778. }
  779. func DeletePurchaseOrder(id int64) error {
  780. err := XTWriteDB().Model(&models.SupplierWarehousingInfoOrder{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  781. return err
  782. }
  783. func DeleteGoodOrderById(id int64) error {
  784. err := XTWriteDB().Model(&models.SpSupplierWarehousingOutOrder{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  785. return err
  786. }
  787. func GetlastWarehouseOutById(user_org_id int64, record_date int64) (models.WarehouseOut, error) {
  788. out := models.WarehouseOut{}
  789. err := XTReadDB().Where("org_id = ? and warehouse_out_time = ? and is_sys = 0 and status = 1", user_org_id, record_date).Find(&out).Error
  790. return out, err
  791. }
  792. func GetDrugWarehouseOutById(orgid int64, record_date int64) (*models.DrugWarehouseOut, error) {
  793. out := models.DrugWarehouseOut{}
  794. var err error
  795. err = XTReadDB().Where("org_id = ? and warehouse_out_time = ? and status =1 and is_sys = 0", orgid, record_date).Find(&out).Error
  796. if err == gorm.ErrRecordNotFound {
  797. return nil, err
  798. }
  799. if err != nil {
  800. return nil, err
  801. }
  802. return &out, nil
  803. }
  804. func GetLastDrugWarehouseById(orgid int64, record_date int64) (models.DrugWarehouseOut, error) {
  805. out := models.DrugWarehouseOut{}
  806. err := XTReadDB().Where("org_id = ? and warehouse_out_time =? and status = 1 and is_sys =0 ", orgid, record_date).Last(&out).Error
  807. return out, err
  808. }
  809. func UpdateSupplyCancelById(id int64, order models.SpSupplierWarehousingCancelOrder) error {
  810. err := XTReadDB().Model(&order).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"source_count": order.SourceCount}).Error
  811. return err
  812. }
  813. func DeleteReturnOrder(id int64) error {
  814. err := XTWriteDB().Model(&models.SpSupplierWarehouseCancel{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  815. err = XTWriteDB().Model(&models.SpSupplierWarehousingCancelOrder{}).Where("warehouse_cancel_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  816. return err
  817. }
  818. func DeleteReturnOrderById(id int64) error {
  819. err = XTWriteDB().Model(&models.SpSupplierWarehousingCancelOrder{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  820. return err
  821. }
  822. func ModefyReturnOrder(id int64) error {
  823. cancel := models.SpSupplierWarehouseCancel{}
  824. err := XTWriteDB().Model(&cancel).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_check": 2, "checker": 0, "check_time": 0}).Error
  825. return err
  826. }
  827. func UpdateWarehousingInfoById(goodid int64, supply_warehouse_id int64, info models.WarehousingInfo) error {
  828. 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
  829. return err
  830. }
  831. func DeleteGoodWarehouseOut(goodid int64, supply_warehouse_id int64, orgid int64) error {
  832. err := XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("good_id = ? and supply_cancel_out_id = ? and status = 1 and org_id = ?", goodid, supply_warehouse_id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  833. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("good_id = ? and supply_cancel_out_id = ? and status = 1 and user_org_id =?", goodid, supply_warehouse_id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  834. return err
  835. }
  836. func UpdateDrugWasehousring(goodid int64, supply_warehouse_id int64, info models.DrugWarehouseInfo) error {
  837. err := XTWriteDB().Model(&info).Where("drug_id = ? and supply_warehouse_detail_info = ? and status = 1", goodid, supply_warehouse_id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  838. return err
  839. }
  840. func UpdateDrugWasehousringOne(goodid int64, supply_warehouse_id int64, info models.DrugWarehouseInfo) error {
  841. err := XTWriteDB().Model(&info).Where("drug_id = ? and supply_warehouse_detail_info = ? and status = 1", goodid, supply_warehouse_id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  842. return err
  843. }
  844. func DeleteDrugWarehouseOutNight(goodid int64, supply_warehouse_id int64) error {
  845. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("drug_id = ? and supply_cancel_out_id = ? and status = 1", goodid, supply_warehouse_id).Updates(map[string]interface{}{"status": 0}).Error
  846. err = XTWriteDB().Model(&models.DrugFlow{}).Where("drug_id = ? and supply_cancel_out_id = ? and status = 1", goodid, supply_warehouse_id).Updates(map[string]interface{}{"status": 0}).Error
  847. return err
  848. }
  849. func GetGoodIsSource(warehousing_id int64, project_id int64, orgid int64) (*models.SupplierWarehousingInfoOrder, error) {
  850. info := models.SupplierWarehousingInfoOrder{}
  851. var err error
  852. 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
  853. if err == gorm.ErrRecordNotFound {
  854. return nil, err
  855. }
  856. if err != nil {
  857. return nil, err
  858. }
  859. return &info, nil
  860. }
  861. func ModfySupplyWarehouseOut(warehousing_id int64, orgid int64) error {
  862. out := models.SpSupplierWarehouseOut{}
  863. 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
  864. return err
  865. }
  866. func FindWarehousingInfoTwenTy(goodId int64, supply_warehouse_detail_info int64, source int64, storehouse_id int64) (models.WarehousingInfo, error) {
  867. info := models.WarehousingInfo{}
  868. var err error
  869. if source == 1 {
  870. err = XTReadDB().Select(" good_id,sum(stock_count) as stock_count").Where("good_id = ? and status = 1 and supply_warehouse_detail_info =? and storehouse_id = ?", goodId, supply_warehouse_detail_info, storehouse_id).Find(&info).Error
  871. }
  872. if source == 2 {
  873. err = XTReadDB().Select(" good_id,sum(stock_count) as stock_count").Where("good_id = ? and status = 1 and storehouse_id = ?", goodId, storehouse_id).Find(&info).Error
  874. }
  875. return info, err
  876. }
  877. func GetDrugTotalCountTwenTy(drugid int64, supply_warehouse_detail_info int64, source int64, storehouse_id int64) (list []*models.VmDrugWarehouseInfo, err error) {
  878. if source == 1 {
  879. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  880. table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
  881. fmt.Println(table)
  882. if drugid > 0 {
  883. db = db.Where("x.drug_id = ?", drugid)
  884. }
  885. if supply_warehouse_detail_info > 0 {
  886. db = db.Where("x.supply_warehouse_detail_info = ?", supply_warehouse_detail_info)
  887. }
  888. if storehouse_id > 0 {
  889. db = db.Where("x.storehouse_id= ?", storehouse_id)
  890. }
  891. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,d.min_number,d.max_unit,d.min_unit,x.max_unit as count_unit,x.supply_warehouse_detail_info").Joins("left join xt_base_drug as d on d.id = x.drug_id").Scan(&list).Error
  892. }
  893. if source == 2 {
  894. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  895. table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
  896. fmt.Println(table)
  897. if drugid > 0 {
  898. db = db.Where("x.drug_id = ?", drugid)
  899. }
  900. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,d.min_number,d.max_unit,d.min_unit,x.max_unit as count_unit,x.supply_warehouse_detail_info").Joins("left join xt_base_drug as d on d.id = x.drug_id").Scan(&list).Error
  901. }
  902. return list, err
  903. }
  904. func GetSupplyWarehouseOutIsExsit(warehouse_out_id int64, project_id int64, orgid int64) (*models.SpSupplierWarehousingOutOrder, error) {
  905. out := models.SpSupplierWarehousingOutOrder{}
  906. var err error
  907. 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
  908. if err == gorm.ErrRecordNotFound {
  909. return nil, err
  910. }
  911. if err != nil {
  912. return nil, err
  913. }
  914. return &out, nil
  915. }
  916. func ModfySupplyCancel(id int64, orgid int64, project_id int64) error {
  917. 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
  918. err = XTWriteDB().Model(models.SpSupplierWarehousingCancelOrder{}).Where("warehouse_cancel_id = ? and user_org_id = ? and status = 1 and project_id = ?", id, orgid, project_id).Updates(map[string]interface{}{"warehouse_info_id": 0, "warehouse_out_id": 0, "warehousing_id": 0, "good_number": "", "order_number": ""}).Error
  919. return err
  920. }
  921. func UpdateSupplyWarehouseInfo(id int64) error {
  922. err = XTWriteDB().Model(&models.SupplierWarehouseInfo{}).Where("id = ? and status= 1").Updates(map[string]interface{}{"is_warehouse": 2}).Error
  923. return err
  924. }
  925. func GetDrugWarehosueInfoByWarehousingId(drug_id int64, supply_warehouse_id int64, orgid int64) (models.DrugWarehouseOutInfo, error) {
  926. info := models.DrugWarehouseOutInfo{}
  927. err := XTReadDB().Where("drug_id = ? and supply_warehouse_id = ? and org_id = ? and status = 1", drug_id, supply_warehouse_id, orgid).Last(&info).Error
  928. return info, err
  929. }
  930. func GetGoodWarehouseInfoByWarehousingId(good_id int64, supply_warehouse_id int64, orgid int64) (models.WarehouseOutInfo, error) {
  931. info := models.WarehouseOutInfo{}
  932. err := XTReadDB().Where("good_id = ? and supply_warehouse_id = ? and org_id = ? and status = 1", good_id, supply_warehouse_id, orgid).Last(&info).Error
  933. return info, err
  934. }
  935. func GetAllDrugWaresingList(drug_id int64, org_id int64) (info []*models.SpDrugWarehouseInfo, err error) {
  936. err = XTReadDB().Where("drug_id = ? and org_id = ? and status = 1 and (stock_max_number>0 or stock_min_number > 0)", drug_id, org_id).Find(&info).Error
  937. return info, err
  938. }
  939. func GetAllGoodWaresingList(good_id int64, org_id int64) (info []*models.SpWarehouseInfo, err error) {
  940. err = XTReadDB().Where("good_id = ? and org_id =? and status = 1 and stock_count>0", good_id, org_id).Find(&info).Error
  941. return info, err
  942. }
  943. func GetReturnOrderById(id int64) (models.SpSupplierWarehouseCancel, error) {
  944. order := models.SpSupplierWarehouseCancel{}
  945. err := XTReadDB().Where("id = ? and status = 1", id).Find(&order).Error
  946. return order, err
  947. }
  948. func GetWarehouseOutByDate(operation_time int64, orgid int64) (models.WarehouseOut, error) {
  949. out := models.WarehouseOut{}
  950. err := XTReadDB().Where("warehouse_out_time = ? and org_id = ? and status = 1 and is_sys= 0", operation_time, orgid).Find(&out).Error
  951. return out, err
  952. }
  953. func GetWarehouseOutById(id int64, orgid int64) (out []*models.WarehouseOutInfo, err error) {
  954. err = XTReadDB().Where("warehouse_out_id = ? and status= 1 and org_id = ?", id, orgid).Find(&out).Error
  955. return out, err
  956. }
  957. func DeleteWarehouseOutById(id int64) error {
  958. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  959. return err
  960. }
  961. func GetDrugWarehouseByDate(operation_time int64, orgid int64) (models.DrugWarehouseOut, error) {
  962. out := models.DrugWarehouseOut{}
  963. err := XTReadDB().Where("warehouse_out_time = ? and org_id = ? and status =1 and is_sys =0 ", operation_time, orgid).Find(&out).Error
  964. return out, err
  965. }
  966. func GetDrugWarehouseByIdList(id int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  967. err = XTReadDB().Where("warehouse_out_id = ? and status = 1 and org_id = ?", id, orgid).Find(&info).Error
  968. return info, err
  969. }
  970. func UpdateDrugWarehouseById(id int64) error {
  971. err := XTWriteDB().Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  972. return err
  973. }
  974. func UpdateWarehouseingById(id int64) error {
  975. err := XTWriteDB().Model(&models.SpSupplierWarehousingOutOrder{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_warehouse": 1}).Error
  976. return err
  977. }
  978. func UpdateSupplyOrderListById(id int64) error {
  979. err := XTWriteDB().Model(models.SpSupplierWarehousingOutOrder{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_warehouse": 2}).Error
  980. return err
  981. }
  982. func GetAllDrugWarehouseInfo(orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  983. err = XTReadDB().Where("org_id = ? and status = 1 and supply_warehouse_id>0", orgid).Find(&info).Error
  984. return info, err
  985. }
  986. func GetAllGoodWarehouseInfo(orgid int64) (info []*models.WarehousingInfo, err error) {
  987. err = XTReadDB().Where("org_id = ? and status = 1 and supply_warehouse_id>0", orgid).Find(&info).Error
  988. return info, err
  989. }
  990. func GetLastDrugWarehosueInfoByWarehouseOutId(orgid int64, warehouse_out_id int64) (models.XtDrugWarehouseInfo, error) {
  991. info := models.XtDrugWarehouseInfo{}
  992. err := XTReadDB().Model(&info).Where("user_org_id = ? and status = 1 and supply_warehouse_id = ?", orgid, warehouse_out_id).Find(&info).Error
  993. return info, err
  994. }