supply_service.go 48KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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", func(db *gorm.DB) *gorm.DB {
  372. return db.Where("status = 1").Preload("SpSupplierWarehousingOutOrder", "status= 1 and user_org_id = ?", orgid)
  373. }).Order("ctime desc").Find(&info).Error
  374. return info, total, err
  375. }
  376. func GetSupplyWarehousingOrderInfo(id int64) (order []*models.SupplierWarehousingInfoOrder, err error) {
  377. err = XTReadDB().Where("warehousing_id = ? and status = 1", id).Find(&order).Error
  378. return order, err
  379. }
  380. func GetSupplyWarehousingOrderInfoTwo(id int64, ids []string) (order []*models.SupplierWarehousingInfoOrder, err error) {
  381. err = XTReadDB().Where("warehousing_id = ? and status = 1 and project_id in(?)", id, ids).Find(&order).Error
  382. return order, err
  383. }
  384. func ModefySupplyWarehouseInfo(id int64, info models.SupplierWarehouseInfo) error {
  385. 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
  386. return err
  387. }
  388. func ModifySupplyWarehouseOrder(order *models.SupplierWarehousingInfoOrder) error {
  389. 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
  390. return err
  391. }
  392. func UpdateSupplyWaresing(id int64, info models.SupplierWarehouseInfo) error {
  393. 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
  394. return err
  395. }
  396. func GetPurchaseOrderDetail(id int64) (models.SupplierWarehouseInfo, error) {
  397. info := models.SupplierWarehouseInfo{}
  398. err := XTReadDB().Model(&info).Where("id =? and status =1", id).Find(&info).Error
  399. return info, err
  400. }
  401. func FindAllSupplyWarehouseOutOrder(orgid int64) (total int64, err error) {
  402. err = XTReadDB().Model(&models.SpSupplierWarehouseOut{}).Where("user_org_id = ?", orgid).Count(&total).Error
  403. return total, err
  404. }
  405. func FindSupplyWarehouseOutById(orgid int64) (models.SpSupplierWarehouseOut, error) {
  406. out := models.SpSupplierWarehouseOut{}
  407. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Last(&out).Error
  408. return out, err
  409. }
  410. func CreateSupplyWarehouseOut(out models.SpSupplierWarehouseOut) error {
  411. err := XTWriteDB().Create(&out).Error
  412. return err
  413. }
  414. func CreateSupplyWarehousOutOrder(order *models.SpSupplierWarehousingOutOrder) error {
  415. err := XTWriteDB().Create(&order).Error
  416. return err
  417. }
  418. func GetSupplyWarehouseOutById(id int64, user_org_id int64) (order []*models.SpSupplierWarehousingOutOrder, err error) {
  419. err = XTReadDB().Where("warehouse_out_id = ? and status = 1 and user_org_id = ?", id, user_org_id).Find(&order).Error
  420. return order, err
  421. }
  422. func GetSupplyWarehouseOutByIdOne(id int64, user_org_id int64, ids []string) (order []*models.SpSupplierWarehousingOutOrder, err error) {
  423. 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
  424. return order, err
  425. }
  426. func GetAllGoodOderList(check_id int64, keyword string, page int64, limit int64, startime int64, endtime int64, orgid int64) (out []*models.VmSupplierWarehouseOut, total int64, err error) {
  427. db := XTReadDB().Model(&out).Where("sgj_xt.xt_supplier_warehouse_out.status = 1")
  428. likeKey := "%" + keyword + "%"
  429. offset := (page - 1) * limit
  430. if check_id > 0 {
  431. db = db.Where("sgj_xt.xt_supplier_warehouse_out.is_check = ?", check_id)
  432. }
  433. if startime > 0 {
  434. db = db.Where("sgj_xt.xt_supplier_warehouse_out.record_date >= ?", startime)
  435. }
  436. if endtime > 0 {
  437. db = db.Where("sgj_xt.xt_supplier_warehouse_out.record_date<=?", endtime)
  438. }
  439. if len(keyword) > 0 {
  440. db = db.Joins("join sgj_xt.xt_supplier_name on sgj_xt.xt_supplier_name.id = sgj_xt.xt_supplier_warehouse_out.supplier_id")
  441. 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")
  442. }
  443. if orgid > 0 {
  444. db = db.Where("sgj_xt.xt_supplier_warehouse_out.user_org_id = ?", orgid)
  445. }
  446. 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
  447. return out, total, err
  448. }
  449. func GetGoodOrderDetail(id int64, orgid int64) (models.SpSupplierWarehouseOut, error) {
  450. out := models.SpSupplierWarehouseOut{}
  451. err := XTReadDB().Where("id = ? and user_org_id = ? and status = 1", id, orgid).Find(&out).Error
  452. return out, err
  453. }
  454. func UpdateGoodWarehouseOut(id int64, out models.SpSupplierWarehouseOut) error {
  455. 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
  456. return err
  457. }
  458. func UpdateGoodWarehouseOutOrder(order *models.SpSupplierWarehousingOutOrder) error {
  459. 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
  460. return err
  461. }
  462. func DeletePurchOrder(id int64, orgid int64) error {
  463. err := XTWriteDB().Model(&models.SupplierWarehouseInfo{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  464. 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
  465. return err
  466. }
  467. func GetAllPurcaseOrderById(id int64, orgid int64) (order []*models.VSupplierWarehousingInfoOrder, err error) {
  468. err = XTReadDB().Model(&order).Where("warehousing_id = ? and user_org_id = ? and status =1", id, orgid).Find(&order).Error
  469. return order, err
  470. }
  471. func GetAllGoodOrderById(id int64, orgid int64) (order []*models.VSpSupplierWarehousingOutOrder, err error) {
  472. db := XTReadDB().Table("xt_supplier_warehousing_out_order as o").Where("o.status = 1")
  473. if id > 0 {
  474. db = db.Where("o.warehouse_out_id = ?", id)
  475. }
  476. if orgid > 0 {
  477. db = db.Where("o.user_org_id =? and o.is_warehouse= 1", orgid)
  478. }
  479. 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
  480. return order, err
  481. }
  482. func GetAllGoodOrderByIdSix(id int64, orgid int64) (order []*models.VSpSupplierWarehousingOutOrder, err error) {
  483. db := XTReadDB().Table("xt_supplier_warehousing_out_order as o").Where("o.status = 1")
  484. if id > 0 {
  485. db = db.Where("o.warehousing_id = ?", id)
  486. }
  487. if orgid > 0 {
  488. db = db.Where("o.user_org_id =? and o.is_warehouse= 1", orgid)
  489. }
  490. err = db.Select("o.id,o.order_number,o.project_id,o.count as count,o.supply_unit,o.is_source").Find(&order).Error
  491. return order, err
  492. }
  493. func GetAllGoodOrderByIdTwo(id int64, orgid int64) (order []*models.SpSupplierWarehousingOutOrder, err error) {
  494. err = XTReadDB().Where("warehouse_out_id = ? and user_org_id = ? and status = 1 and is_warehouse = 2", id, orgid).Find(&order).Error
  495. return order, err
  496. }
  497. func GetGoodOrderList(id int64, orgid int64) (info []*models.SpSupplierWarehouseOut, err error) {
  498. err = XTReadDB().Where("warehousing_id = ? and status = 1 and user_org_id = ?", id, orgid).Find(&info).Error
  499. return info, err
  500. }
  501. func GetReturnOrder(id int64, orgid int64) error {
  502. 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
  503. return err
  504. }
  505. func CheckGoodOrder(id int64, orgid int64, out models.SpSupplierWarehouseOut) error {
  506. 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
  507. return err
  508. }
  509. func ModefySupplyWarehousing(is_warehose int64, warehousing_id int64, orgid int64) error {
  510. 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
  511. return err
  512. }
  513. func GetAllDoctorSix(orgid int64, appid int64) (appRole []*models.App_Role, err error) {
  514. err = UserReadDB().Where("org_id = ? AND app_id = ? AND status = 1", orgid, appid).Find(&appRole).Error
  515. return appRole, err
  516. }
  517. func GetSingleDrugWarehouseOrder(record_date int64, orgid int64) (*models.DrugWarehouse, error) {
  518. warehouse := models.DrugWarehouse{}
  519. var err error
  520. err = XTReadDB().Model(&warehouse).Where("warehousing_time = ? and org_id = ? and status = 1", record_date, orgid).Find(&warehouse).Error
  521. if err == gorm.ErrRecordNotFound {
  522. return nil, err
  523. }
  524. if err != nil {
  525. return nil, err
  526. }
  527. return &warehouse, nil
  528. }
  529. func GetSindleWarehouse(record_date int64, orgid int64) (*models.Warehousing, error) {
  530. warehousing := models.Warehousing{}
  531. var err error
  532. err = XTReadDB().Model(&warehousing).Where("warehousing_time = ? and org_id = ? and status = 1", record_date, orgid).Find(&warehousing).Error
  533. if err == gorm.ErrRecordNotFound {
  534. return nil, err
  535. }
  536. if err != nil {
  537. return nil, err
  538. }
  539. return &warehousing, nil
  540. }
  541. func GetLastWarehouseInfoByInfo(orgid int64) (models.Warehousing, error) {
  542. warehousing := models.Warehousing{}
  543. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&warehousing).Error
  544. return warehousing, err
  545. }
  546. func GetSupplyCancelOrder(orgid int64) (total int64, err error) {
  547. err = XTReadDB().Model(&models.SpSupplierWarehouseCancel{}).Where("user_org_id = ?", orgid).Count(&total).Error
  548. return total, err
  549. }
  550. func CreateReturnCacelOrder(cancel models.SpSupplierWarehouseCancel) error {
  551. err := XTWriteDB().Create(&cancel).Error
  552. return err
  553. }
  554. func GetLastReturnCancelOrder(orgid int64) (models.SpSupplierWarehouseCancel, error) {
  555. cancel := models.SpSupplierWarehouseCancel{}
  556. err := XTReadDB().Where("user_org_id =? and status = 1", orgid).Find(&cancel).Error
  557. return cancel, err
  558. }
  559. func CreateCancelReturnOrder(order *models.SpSupplierWarehousingCancelOrder) error {
  560. err := XTWriteDB().Create(&order).Error
  561. return err
  562. }
  563. func GetReturnCancelOrder(id int64, orgid int64) (models.SpSupplierWarehouseCancel, error) {
  564. cancel := models.SpSupplierWarehouseCancel{}
  565. err := XTReadDB().Where("id = ? and status= 1 and user_org_id =?", id, orgid).Find(&cancel).Error
  566. return cancel, err
  567. }
  568. func GetReturnCancelOrderList(id int64, orgid int64) (order []*models.SpSupplierWarehousingCancelOrder, err error) {
  569. err = XTReadDB().Where("warehouse_cancel_id = ? and user_org_id =? and status = 1", id, orgid).Find(&order).Error
  570. return order, err
  571. }
  572. func GetAllGoodReturnOrderList(checkid int64, keyword string, page int64, limit int64, startime int64, endtime int64, orgid int64) (order []*models.VmSpSupplierWarehouseCancel, total int64, err error) {
  573. db := XTReadDB().Table("sgj_xt.xt_supplier_warehouse_cancel").Where("sgj_xt.xt_supplier_warehouse_cancel.status = 1")
  574. likeKey := "%" + keyword + "%"
  575. offset := (page - 1) * limit
  576. if checkid > 0 {
  577. db = db.Where("sgj_xt.xt_supplier_warehouse_cancel.is_check = ?", checkid)
  578. }
  579. if len(keyword) > 0 {
  580. db = db.Joins("join sgj_xt.xt_supplier_name on sgj_xt.xt_supplier_name.id = sgj_xt.xt_supplier_warehouse_cancel.supplier_id")
  581. 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")
  582. }
  583. if startime > 0 {
  584. db = db.Where("sgj_xt.xt_supplier_warehouse_cancel.record_date >= ?", startime)
  585. }
  586. if endtime > 0 {
  587. db = db.Where("sgj_xt.xt_supplier_warehouse_cancel.record_date <= ?", endtime)
  588. }
  589. if orgid > 0 {
  590. db = db.Where("sgj_xt.xt_supplier_warehouse_cancel.user_org_id = ?", orgid)
  591. }
  592. err = db.Count(&total).Offset(offset).Limit(limit).Preload("SpSupplierWarehousingCancelOrder", "status= 1 and user_org_id = ?", orgid).Order("ctime desc").Find(&order).Error
  593. return order, total, err
  594. }
  595. func GetGoodReturnDetail(id int64, orgid int64) (models.VmSpSupplierWarehouseCancel, error) {
  596. cancel := models.VmSpSupplierWarehouseCancel{}
  597. db := XTReadDB().Model(&cancel).Where("status = 1")
  598. if id > 0 {
  599. db = db.Where("id = ?", id)
  600. }
  601. if orgid > 0 {
  602. db = db.Where("user_org_id = ?", orgid)
  603. }
  604. err := db.Find(&cancel).Error
  605. return cancel, err
  606. }
  607. func GetGoodReturnOrderDetail(id int64, orgid int64) (order []*models.SpSupplierWarehousingCancelOrder, err error) {
  608. db := XTReadDB().Model(&order).Where("status =1")
  609. if id > 0 {
  610. db = db.Where("warehouse_cancel_id = ?", id)
  611. }
  612. if orgid > 0 {
  613. db = db.Where("user_org_id = ?", orgid)
  614. }
  615. err = db.Find(&order).Error
  616. return order, err
  617. }
  618. func UpdateWarehouseCancelOrder(order *models.SpSupplierWarehousingCancelOrder) error {
  619. 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, "supply_count": order.SupplyCount, "deposit_rate": order.DepositRate}).Error
  620. return err
  621. }
  622. func UpdateWarehouseCancel(id int64, cancel models.SpSupplierWarehouseCancel) error {
  623. 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
  624. return err
  625. }
  626. func GetSupplyCancelOrderById(warehouse_out_id int64, orgid int64) (*models.SpSupplierWarehouseCancel, error) {
  627. cancel := models.SpSupplierWarehouseCancel{}
  628. var err error
  629. err = XTReadDB().Where("warehouse_out_id = ? and user_org_id = ? and status = 1", warehouse_out_id, orgid).Find(&cancel).Error
  630. if err == gorm.ErrRecordNotFound {
  631. return nil, err
  632. }
  633. if err != nil {
  634. return nil, err
  635. }
  636. return &cancel, nil
  637. }
  638. func UpdateSupplyGoodOrder(id int64, out models.SpSupplierWarehouseOut) error {
  639. 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
  640. return err
  641. }
  642. func UpdateDrugSupplyWarehousingInfo(id int64, orgid int64) error {
  643. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("supply_warehouse_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  644. return err
  645. }
  646. func UpdateDrugSupplyFlow(id int64, orgid int64) error {
  647. 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
  648. return err
  649. }
  650. func UpdateGoodSupplyWarehousingInfo(id int64, orgid int64) error {
  651. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("supply_warehouse_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  652. return err
  653. }
  654. func UpdateGoodSupplyFlow(id int64, orgid int64) error {
  655. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("supply_warehouse_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  656. 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
  657. return err
  658. }
  659. func GetDrugSupplyWarehousingById(id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  660. err = XTReadDB().Where("supply_warehouse_id <> ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  661. return info, err
  662. }
  663. func UpdateSupplyWarehousing(id int64, orgid int64) error {
  664. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("supply_warehouse_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  665. return err
  666. }
  667. func GetGoodSupplyWarehousingById(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  668. err = XTReadDB().Where("supply_warehouse_id <> ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  669. return info, err
  670. }
  671. func UpdateGoodWarehousing(id int64, orgid int64) error {
  672. err := XTWriteDB().Model(&models.Warehousing{}).Where("supply_warehouse_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  673. return err
  674. }
  675. func UpdateSupplyWarehousingById(id int64, orgid int64) error {
  676. err := XTWriteDB().Model(&models.SupplierWarehouseInfo{}).Where("id = ? and user_org_id =? and status = 1", id, orgid).Updates(map[string]interface{}{"is_warehouse": 1}).Error
  677. return err
  678. }
  679. func DeleteGoodOrder(id int64, orgid int64) error {
  680. err := XTWriteDB().Model(&models.SpSupplierWarehouseOut{}).Where("id = ? and status = 1 and user_org_id = ?", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  681. 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
  682. return err
  683. }
  684. func GetSupplyCancelWarehouse(id int64, orgid int64) (cancel []*models.SpSupplierWarehouseCancel, err error) {
  685. err = XTReadDB().Where("warehouse_out_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&cancel).Error
  686. return cancel, err
  687. }
  688. func GetGoodOrderListById(id int64, orgid int64) (out []*models.VmSpSupplierWarehousingOutOrder, err error) {
  689. err = XTReadDB().Where("warehouse_out_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&out).Error
  690. return out, err
  691. }
  692. func GetGoodCanceListById(id int64, orgid int64) (cancel []*models.VmSpSupplierWarehousingCancelOrder, err error) {
  693. err = XTReadDB().Where("warehouse_out_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&cancel).Error
  694. return cancel, err
  695. }
  696. func CreateDrugFlowSix(drugflow []*models.DrugFlow) (err error) {
  697. if len(drugflow) > 0 {
  698. utx := writeDb.Begin()
  699. if len(drugflow) > 0 {
  700. 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 "
  701. insertParams := make([]string, 0)
  702. insertData := make([]interface{}, 0)
  703. for _, info := range drugflow {
  704. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  705. insertData = append(insertData, info.WarehousingId)
  706. insertData = append(insertData, info.DrugId)
  707. insertData = append(insertData, info.Number)
  708. insertData = append(insertData, info.BatchNumber)
  709. insertData = append(insertData, info.Count)
  710. insertData = append(insertData, info.UserOrgId)
  711. insertData = append(insertData, info.PatientId)
  712. insertData = append(insertData, info.SystemTime)
  713. insertData = append(insertData, info.ConsumableType)
  714. insertData = append(insertData, info.IsSys)
  715. insertData = append(insertData, info.WarehousingOrder)
  716. insertData = append(insertData, info.WarehouseOutId)
  717. insertData = append(insertData, info.WarehouseOutOrderNumber)
  718. insertData = append(insertData, info.IsEdit)
  719. insertData = append(insertData, info.CancelStockId)
  720. insertData = append(insertData, info.CancelOrderNumber)
  721. insertData = append(insertData, info.Manufacturer)
  722. insertData = append(insertData, info.Dealer)
  723. insertData = append(insertData, info.Creator)
  724. insertData = append(insertData, info.UpdateCreator)
  725. insertData = append(insertData, info.Status)
  726. insertData = append(insertData, info.Ctime)
  727. insertData = append(insertData, info.Mtime)
  728. insertData = append(insertData, info.Price)
  729. insertData = append(insertData, info.WarehousingDetailId)
  730. insertData = append(insertData, info.WarehouseOutDetailId)
  731. insertData = append(insertData, info.CancelOutDetailId)
  732. insertData = append(insertData, info.ExpireDate)
  733. insertData = append(insertData, info.ProductDate)
  734. insertData = append(insertData, info.MaxUnit)
  735. insertData = append(insertData, info.MinUnit)
  736. insertData = append(insertData, info.SupplyWarehouseId)
  737. insertData = append(insertData, info.SupplyWarehouseDetailInfo)
  738. }
  739. thisSQL += strings.Join(insertParams, ", ")
  740. err = utx.Exec(thisSQL, insertData...).Error
  741. if err != nil {
  742. utx.Rollback()
  743. return
  744. }
  745. }
  746. utx.Commit()
  747. }
  748. return
  749. }
  750. func CheckReturnOrder(id int64, orgid int64, cancel models.SpSupplierWarehouseCancel) error {
  751. 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
  752. return err
  753. }
  754. func GetSupplyCancelOrderDetail(id int64, orgid int64) (cancel []*models.SpSupplierWarehousingCancelOrder, err error) {
  755. err = XTReadDB().Where("warehouse_cancel_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&cancel).Error
  756. return cancel, err
  757. }
  758. func DeletePurchaseOrder(id int64) error {
  759. err := XTWriteDB().Model(&models.SupplierWarehousingInfoOrder{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  760. return err
  761. }
  762. func DeleteGoodOrderById(id int64) error {
  763. err := XTWriteDB().Model(&models.SpSupplierWarehousingOutOrder{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  764. return err
  765. }
  766. func GetlastWarehouseOutById(user_org_id int64, record_date int64) (models.WarehouseOut, error) {
  767. out := models.WarehouseOut{}
  768. err := XTReadDB().Where("org_id = ? and warehouse_out_time = ? and status = 1", user_org_id, record_date).Find(&out).Error
  769. return out, err
  770. }
  771. func GetDrugWarehouseOutById(orgid int64, record_date int64) (*models.DrugWarehouseOut, error) {
  772. out := models.DrugWarehouseOut{}
  773. var err error
  774. err = XTReadDB().Where("org_id = ? and warehouse_out_time = ?", orgid, record_date).Find(&out).Error
  775. if err == gorm.ErrRecordNotFound {
  776. return nil, err
  777. }
  778. if err != nil {
  779. return nil, err
  780. }
  781. return &out, nil
  782. }
  783. func GetLastDrugWarehouseById(orgid int64, record_date int64) (models.DrugWarehouseOut, error) {
  784. out := models.DrugWarehouseOut{}
  785. err := XTReadDB().Where("org_id = ? and warehouse_out_time =? and status = 1", orgid, record_date).Find(&out).Error
  786. return out, err
  787. }
  788. func UpdateSupplyCancelById(id int64, order models.SpSupplierWarehousingCancelOrder) error {
  789. err := XTReadDB().Model(&order).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"source_count": order.SourceCount}).Error
  790. return err
  791. }
  792. func DeleteReturnOrder(id int64) error {
  793. err := XTWriteDB().Model(&models.SpSupplierWarehouseCancel{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  794. err = XTWriteDB().Model(&models.SpSupplierWarehousingCancelOrder{}).Where("warehouse_cancel_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  795. return err
  796. }
  797. func DeleteReturnOrderById(id int64) error {
  798. err = XTWriteDB().Model(&models.SpSupplierWarehousingCancelOrder{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  799. return err
  800. }
  801. func ModefyReturnOrder(id int64) error {
  802. cancel := models.SpSupplierWarehouseCancel{}
  803. err := XTWriteDB().Model(&cancel).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_check": 2, "checker": 0, "check_time": 0}).Error
  804. return err
  805. }
  806. func UpdateWarehousingInfoById(goodid int64, supply_warehouse_id int64, info models.WarehousingInfo) error {
  807. 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
  808. return err
  809. }
  810. func DeleteGoodWarehouseOut(goodid int64, supply_warehouse_id int64, orgid int64) error {
  811. 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
  812. 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
  813. return err
  814. }
  815. func UpdateDrugWasehousring(goodid int64, supply_warehouse_id int64, info models.DrugWarehouseInfo) error {
  816. 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
  817. return err
  818. }
  819. func UpdateDrugWasehousringOne(goodid int64, supply_warehouse_id int64, info models.DrugWarehouseInfo) error {
  820. 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
  821. return err
  822. }
  823. func DeleteDrugWarehouseOutNight(goodid int64, supply_warehouse_id int64) error {
  824. 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
  825. 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
  826. return err
  827. }
  828. func GetGoodIsSource(warehousing_id int64, project_id int64, orgid int64) (*models.SupplierWarehousingInfoOrder, error) {
  829. info := models.SupplierWarehousingInfoOrder{}
  830. var err error
  831. 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
  832. if err == gorm.ErrRecordNotFound {
  833. return nil, err
  834. }
  835. if err != nil {
  836. return nil, err
  837. }
  838. return &info, nil
  839. }
  840. func ModfySupplyWarehouseOut(warehousing_id int64, orgid int64) error {
  841. out := models.SpSupplierWarehouseOut{}
  842. 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
  843. return err
  844. }
  845. func FindWarehousingInfoTwenTy(goodId int64, supply_warehouse_detail_info int64) (models.WarehousingInfo, error) {
  846. info := models.WarehousingInfo{}
  847. 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
  848. return info, err
  849. }
  850. func GetDrugTotalCountTwenTy(drugid int64, supply_warehouse_detail_info int64) (list []*models.VmDrugWarehouseInfo, err error) {
  851. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  852. table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
  853. fmt.Println(table)
  854. if drugid > 0 {
  855. db = db.Where("x.drug_id = ?", drugid)
  856. }
  857. if supply_warehouse_detail_info > 0 {
  858. db = db.Where("x.supply_warehouse_detail_info = ?", supply_warehouse_detail_info)
  859. }
  860. 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
  861. return list, err
  862. }
  863. func GetSupplyWarehouseOutIsExsit(warehouse_out_id int64, project_id int64, orgid int64) (*models.SpSupplierWarehousingOutOrder, error) {
  864. out := models.SpSupplierWarehousingOutOrder{}
  865. var err error
  866. 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
  867. if err == gorm.ErrRecordNotFound {
  868. return nil, err
  869. }
  870. if err != nil {
  871. return nil, err
  872. }
  873. return &out, nil
  874. }
  875. func ModfySupplyCancel(id int64, orgid int64) error {
  876. 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
  877. 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
  878. return err
  879. }
  880. func UpdateSupplyWarehouseInfo(id int64) error {
  881. err = XTWriteDB().Model(&models.SupplierWarehouseInfo{}).Where("id = ? and status= 1").Updates(map[string]interface{}{"is_warehouse": 2}).Error
  882. return err
  883. }
  884. func GetDrugWarehosueInfoByWarehousingId(drug_id int64, supply_warehouse_id int64, orgid int64) (models.DrugWarehouseOutInfo, error) {
  885. info := models.DrugWarehouseOutInfo{}
  886. err := XTReadDB().Where("drug_id = ? and supply_warehouse_id = ? and org_id = ? and status = 1", drug_id, supply_warehouse_id, orgid).Last(&info).Error
  887. return info, err
  888. }
  889. func GetGoodWarehouseInfoByWarehousingId(good_id int64, supply_warehouse_id int64, orgid int64) (models.WarehouseOutInfo, error) {
  890. info := models.WarehouseOutInfo{}
  891. err := XTReadDB().Where("good_id = ? and supply_warehouse_id = ? and org_id = ? and status = 1", good_id, supply_warehouse_id, orgid).Last(&info).Error
  892. return info, err
  893. }
  894. func GetAllDrugWaresingList(drug_id int64, org_id int64) (info []*models.SpDrugWarehouseInfo, err error) {
  895. 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
  896. return info, err
  897. }
  898. func GetAllGoodWaresingList(good_id int64, org_id int64) (info []*models.SpWarehouseInfo, err error) {
  899. err = XTReadDB().Where("good_id = ? and org_id =? and status = 1 and stock_count>0", good_id, org_id).Find(&info).Error
  900. return info, err
  901. }
  902. func GetReturnOrderById(id int64) (models.SpSupplierWarehouseCancel, error) {
  903. order := models.SpSupplierWarehouseCancel{}
  904. err := XTReadDB().Where("id = ? and status = 1", id).Find(&order).Error
  905. return order, err
  906. }
  907. func GetWarehouseOutByDate(operation_time int64, orgid int64) (models.WarehouseOut, error) {
  908. out := models.WarehouseOut{}
  909. err := XTReadDB().Where("warehouse_out_time = ? and org_id = ? and status = 1", operation_time, orgid).Find(&out).Error
  910. return out, err
  911. }
  912. func GetWarehouseOutById(id int64, orgid int64) (out []*models.WarehouseOutInfo, err error) {
  913. err = XTReadDB().Where("warehouse_out_id = ? and status= 1 and org_id = ?", id, orgid).Find(&out).Error
  914. return out, err
  915. }
  916. func DeleteWarehouseOutById(id int64) error {
  917. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  918. return err
  919. }
  920. func GetDrugWarehouseByDate(operation_time int64, orgid int64) (models.DrugWarehouseOut, error) {
  921. out := models.DrugWarehouseOut{}
  922. err := XTReadDB().Where("warehouse_out_time = ? and org_id = ? and status =1 ", operation_time, orgid).Find(&out).Error
  923. return out, err
  924. }
  925. func GetDrugWarehouseByIdList(id int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  926. err = XTReadDB().Where("warehouse_out_id = ? and status = 1 and org_id = ?", id, orgid).Find(&info).Error
  927. return info, err
  928. }
  929. func UpdateDrugWarehouseById(id int64) error {
  930. err := XTWriteDB().Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  931. return err
  932. }
  933. func UpdateWarehouseingById(id int64) error {
  934. err := XTWriteDB().Model(&models.SpSupplierWarehousingOutOrder{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_warehouse": 1}).Error
  935. return err
  936. }
  937. func UpdateSupplyOrderListById(id int64) error {
  938. err := XTWriteDB().Model(models.SpSupplierWarehousingOutOrder{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_warehouse": 2}).Error
  939. return err
  940. }
  941. func GetAllDrugWarehouseInfo(orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  942. err = XTReadDB().Where("org_id = ? and status = 1 and supply_warehouse_id>0", orgid).Find(&info).Error
  943. return info, err
  944. }
  945. func GetAllGoodWarehouseInfo(orgid int64) (info []*models.WarehousingInfo, err error) {
  946. err = XTReadDB().Where("org_id = ? and status = 1 and supply_warehouse_id>0", orgid).Find(&info).Error
  947. return info, err
  948. }