supply_service.go 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. package service
  2. import (
  3. "XT_New/models"
  4. "github.com/jinzhu/gorm"
  5. "strconv"
  6. "time"
  7. )
  8. //根据供应商编号获取首要联系人
  9. func FindName(code string) (fistname models.SpSupplierContacts, err error) {
  10. err = XTReadDB().Model(&models.SpSupplierContacts{}).Where("supplier_code = ? and status = 1", code).First(&fistname).Error
  11. return fistname, err
  12. }
  13. //供应商分页
  14. func GetSupplyList(ctype int64, page int64, limit int64, code string, sname string, cname string) (supplylist []*models.SpSupplierName, total int64, err error) {
  15. db := XTReadDB().Model(&supplylist).Where("sgj_xt.xt_supplier_name.status = 1 ")
  16. offset := (page - 1) * limit
  17. if cname != "" {
  18. cname = "%" + cname + "%" //联系人
  19. db = db.Joins("join sgj_xt.xt_supplier_contacts on sgj_xt.xt_supplier_contacts.supplier_code = sgj_xt.xt_supplier_name.supplier_code")
  20. db = db.Where("sgj_xt.xt_supplier_contacts.name like ? and sgj_xt.xt_supplier_contacts.status = 1 ", cname).Group("sgj_xt.xt_supplier_name.id")
  21. }
  22. if ctype > 0 {
  23. db = db.Where("sgj_xt.xt_supplier_name.supplier_type = ?", ctype)
  24. }
  25. if code != "" {
  26. code = "%" + code + "%" //供应商编码
  27. db = db.Where("sgj_xt.xt_supplier_name.supplier_code = ?", code)
  28. }
  29. if sname != "" {
  30. sname = "%" + sname + "%" //供应商名称
  31. db = db.Where("sgj_xt.xt_supplier_name.supplier_name = ?", sname)
  32. }
  33. err = db.Count(&total).Offset(offset).Limit(limit).Find(&supplylist).Error
  34. return supplylist, total, err
  35. }
  36. //修改供应商和联系人
  37. func UpdateSupplyAndContact(thisStockIn []interface{}, suid, orgId, supplierType, tcreater int64, supplierCode, supplierName, number, bank, bankAccount string, vatRate float64) error {
  38. tx := XTWriteDB().Begin()
  39. defer func() {
  40. if err != nil {
  41. tx.Rollback()
  42. } else {
  43. tx.Commit()
  44. }
  45. }()
  46. for _, item := range thisStockIn {
  47. items := item.(map[string]interface{})
  48. //查询是否
  49. id, _ := strconv.ParseInt(items["id"].(string), 10, 64)
  50. name := items["name"].(string)
  51. phone := items["phone"].(string)
  52. address := items["address"].(string)
  53. isfirst, _ := strconv.ParseInt(items["isfirst"].(string), 10, 64)
  54. updatecontacts := models.SpSupplierContacts{
  55. ID: id,
  56. Name: name,
  57. Phone: phone,
  58. Address: address,
  59. IsFirst: isfirst,
  60. SupplierCode: supplierCode,
  61. UserOrgId: orgId,
  62. Status: 1,
  63. Ctime: 0,
  64. Mtime: time.Now().Unix(),
  65. }
  66. if id == 0 {
  67. spcontacts := models.SpSupplierContacts{
  68. Name: name,
  69. Phone: phone,
  70. Address: address,
  71. IsFirst: isfirst,
  72. SupplierCode: supplierCode,
  73. UserOrgId: orgId,
  74. Status: 1,
  75. Ctime: time.Now().Unix(),
  76. Mtime: 0,
  77. }
  78. err = SaveContacts(spcontacts, tx)
  79. if err != nil {
  80. return err
  81. }
  82. } else {
  83. err = UpdateContact(updatecontacts, tx)
  84. }
  85. var tmpid int64
  86. if isfirst == 1 {
  87. if id == 0 {
  88. var spconid []*models.SpSupplierContacts
  89. spconid, err = SaveContactsId(tx)
  90. if err != nil {
  91. return err
  92. }
  93. tmpid = spconid[0].ID
  94. } else {
  95. tmpid = id
  96. }
  97. //更新供应商
  98. upsupply := models.SpSupplierName{
  99. ID: suid,
  100. SupplierCode: supplierCode,
  101. SupplierName: supplierName,
  102. SupplierType: supplierType,
  103. VatRate: vatRate,
  104. Number: number,
  105. Bank: bank,
  106. BankAccount: bankAccount,
  107. UserOrgId: orgId,
  108. Status: 1,
  109. ContactsId: tmpid,
  110. Mtime: time.Now().Unix(),
  111. Modify: tcreater,
  112. }
  113. err = UpdateSupplyName(upsupply, tx)
  114. if err != nil {
  115. return err
  116. }
  117. }
  118. }
  119. return err
  120. }
  121. //更新一条供应商的信息
  122. func UpdateSupplyName(upsupply models.SpSupplierName, tx *gorm.DB) error {
  123. 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
  124. return err
  125. }
  126. //更新一条联系人的信息
  127. func UpdateContact(updatecontacts models.SpSupplierContacts, tx *gorm.DB) error {
  128. 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
  129. return err
  130. }
  131. //查询供应商单条记录
  132. func GetSupplyOne(id int64) (supply models.SpSupplierName, err error) {
  133. err = XTReadDB().Model(&models.SpSupplierName{}).Where("id = ? and status = 1", id).First(&supply).Error
  134. return supply, err
  135. }
  136. //删除单条联系人记录
  137. func DelContactOne(id int64) error {
  138. err := XTWriteDB().Model(&models.SpSupplierContacts{}).Where("id = ?", id).Update("status", 0).Error
  139. return err
  140. }
  141. //获取单条供应商和涉及到的联系人记录
  142. func GetSupplyAndContactOne(id int64) (supply models.SpSupplierName, contact []*models.SpSupplierContacts, err error) {
  143. err = XTReadDB().Model(&models.SpSupplierName{}).Where("id = ? and status = 1", id).First(&supply).Error
  144. code := supply.SupplierCode
  145. err = XTReadDB().Model(&models.SpSupplierContacts{}).Where("supplier_code = ? and status = 1", code).Find(&contact).Error
  146. return supply, contact, err
  147. }
  148. //删除供应商及联系人
  149. func DelSupply(supply models.SpSupplierName) error {
  150. err := XTWriteDB().Model(&supply).Update("status", 0).Error
  151. return err
  152. }
  153. //保存供应商和联系人
  154. func SaveSupplyAndContact(thisStockIn []interface{}, orgId, supplierType, tcreater int64, supplierCode, supplierName, number, bank, bankAccount string, vatRate float64) error {
  155. tx := XTWriteDB().Begin()
  156. defer func() {
  157. if err != nil {
  158. tx.Rollback()
  159. } else {
  160. tx.Commit()
  161. }
  162. }()
  163. for _, item := range thisStockIn {
  164. items := item.(map[string]interface{})
  165. name := items["name"].(string)
  166. phone := items["phone"].(string)
  167. address := items["address"].(string)
  168. isfirst, _ := strconv.ParseInt(items["isfirst"].(string), 10, 64)
  169. spcontacts := models.SpSupplierContacts{
  170. Name: name,
  171. Phone: phone,
  172. Address: address,
  173. IsFirst: isfirst,
  174. SupplierCode: supplierCode,
  175. UserOrgId: orgId,
  176. Status: 1,
  177. Ctime: time.Now().Unix(),
  178. Mtime: 0,
  179. }
  180. err = SaveContacts(spcontacts, tx)
  181. if isfirst == 1 {
  182. var spconid []*models.SpSupplierContacts
  183. spconid, err = SaveContactsId(tx)
  184. if err != nil {
  185. return err
  186. }
  187. tmpid := spconid[0].ID
  188. //保存供应商
  189. supply := models.SpSupplierName{
  190. SupplierCode: supplierCode,
  191. SupplierName: supplierName,
  192. SupplierType: supplierType,
  193. VatRate: vatRate,
  194. Number: number,
  195. Bank: bank,
  196. BankAccount: bankAccount,
  197. UserOrgId: orgId,
  198. Status: 1,
  199. ContactsId: tmpid,
  200. Ctime: time.Now().Unix(),
  201. Mtime: 0,
  202. Creater: tcreater,
  203. Modify: tcreater,
  204. }
  205. err = SaveSupply(supply, tx)
  206. if err != nil {
  207. return err
  208. }
  209. }
  210. if err != nil {
  211. return err
  212. }
  213. }
  214. return err
  215. }
  216. //获取供应商编码
  217. func GetSuppliyCode() (spcode []*models.SpSupplierName, err error) {
  218. err = XTReadDB().Model(&models.SpSupplierName{}).Select("supplier_code").Where("supplier_code like 'gys%' and status = 1 ").Order("supplier_code desc").First(&spcode).Error
  219. return spcode, err
  220. }
  221. //查询供应商的名字是否有重复
  222. func FindSupplierName(supplierName string) (sbool bool, err error) {
  223. var total int
  224. err = XTReadDB().Model(&models.SpSupplierName{}).Where("supplier_name = ? and status = 1", supplierName).Count(&total).Error
  225. if total != 0 {
  226. sbool = true
  227. } else {
  228. sbool = false
  229. }
  230. return sbool, err
  231. }
  232. //查询供应商的编号是否有重复
  233. func FindSupplierCode(supplierCode string) (codebool bool, err error) {
  234. var total int
  235. err = XTReadDB().Model(&models.SpSupplierName{}).Where("supplier_name = ? and status = 1", supplierCode).Count(&total).Error
  236. if total != 0 {
  237. codebool = true
  238. } else {
  239. codebool = false
  240. }
  241. return codebool, err
  242. }
  243. //保存一条供应商数据
  244. func SaveSupply(supply models.SpSupplierName, tx *gorm.DB) error {
  245. err := tx.Create(&supply).Error
  246. return err
  247. }
  248. //获取联系人的id
  249. func SaveContactsId(tx *gorm.DB) (spconid []*models.SpSupplierContacts, err error) {
  250. err = tx.Model(&models.SpSupplierContacts{}).Select("id").Where("and status = 1").Order("id desc").First(&spconid).Error
  251. return
  252. }
  253. //保存一条联系人数据
  254. func SaveContacts(spcontacts models.SpSupplierContacts, tx *gorm.DB) error {
  255. err := tx.Create(&spcontacts).Error
  256. return err
  257. }
  258. func GetSupplyDrugList(orgid int64) (drug []*models.SpBaseDrug, err error) {
  259. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  260. if orgid > 0 {
  261. db = db.Where("x.org_id = ?", orgid)
  262. }
  263. err = db.Preload("DrugWarehouseInfo", "status = 1 and org_id = ? and (stock_max_number >0 or stock_min_number>0)", orgid).Find(&drug).Error
  264. return drug, err
  265. }
  266. func GetSupplyGoodList(orgid int64) (good []*models.SpGoodInformation, err error) {
  267. db := XTReadDB().Table("xt_good_information as x").Where("x.status = 1")
  268. if orgid > 0 {
  269. db = db.Where("x.org_id = ?", orgid)
  270. }
  271. err = db.Preload("GoodWarehouseInfo", "status = 1 and org_id = ?", orgid).Find(&good).Error
  272. return good, err
  273. }
  274. func GetSupplierList(orgid int64) (suppler []*models.SpSupplierName, err error) {
  275. err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&suppler).Error
  276. return suppler, err
  277. }
  278. func FindAllSupplyOrder(orgid int64) (total int64, err error) {
  279. err = XTReadDB().Model(&models.SupplierWarehouseInfo{}).Where("user_org_id = ?", orgid).Count(&total).Error
  280. return total, err
  281. }
  282. func CreateSupplyWarehouse(info models.SupplierWarehouseInfo) error {
  283. err := XTWriteDB().Create(&info).Error
  284. return err
  285. }
  286. func FindLastSupplyWarehouseInfo(orgid int64) (models.SupplierWarehouseInfo, error) {
  287. info := models.SupplierWarehouseInfo{}
  288. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Last(&info).Error
  289. return info, err
  290. }
  291. func CreateSupplyWarehousingOrder(order *models.SupplierWarehousingInfoOrder) error {
  292. err := XTWriteDB().Create(&order).Error
  293. return err
  294. }
  295. func GetAllPurchaseOrderList(check_id int64, startime int64, endtime int64, keyword string, page int64, limit int64, orgid int64) (info []*models.VmSupplierWarehouseInfo, total int64, err error) {
  296. db := XTReadDB().Model(&info).Where("sgj_xt.xt_supplier_warehouse_info.status = 1")
  297. likeKey := "%" + keyword + "%"
  298. offset := (page - 1) * limit
  299. if check_id > 0 {
  300. db = db.Where("sgj_xt.xt_supplier_warehouse_info.is_check = ?", check_id)
  301. }
  302. if startime > 0 {
  303. db = db.Where("sgj_xt.xt_supplier_warehouse_info.record_date >= ?", startime)
  304. }
  305. if endtime > 0 {
  306. db = db.Where("sgj_xt.xt_supplier_warehouse_info.record_date<=?", endtime)
  307. }
  308. if len(keyword) > 0 {
  309. db = db.Joins("join sgj_xt.xt_supplier_name on sgj_xt.xt_supplier_name.id = sgj_xt.xt_supplier_warehouse_info.supplier_id")
  310. 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")
  311. }
  312. if orgid > 0 {
  313. db = db.Where("sgj_xt.xt_supplier_warehouse_info.user_org_id = ?", orgid)
  314. }
  315. err = db.Count(&total).Offset(offset).Limit(limit).Preload("SupplierWarehousingInfoOrder", "status= 1 and user_org_id = ?", orgid).Preload("SpSupplierWarehouseOut", "status = 1 and user_org_id =?", orgid).Find(&info).Error
  316. return info, total, err
  317. }
  318. func GetSupplyWarehousingOrderInfo(id int64) (order []*models.SupplierWarehousingInfoOrder, err error) {
  319. err = XTReadDB().Where("warehousing_id = ? and status = 1", id).Find(&order).Error
  320. return order, err
  321. }
  322. func GetSupplyWarehousingOrderInfoTwo(id int64, ids []string) (order []*models.SupplierWarehousingInfoOrder, err error) {
  323. err = XTReadDB().Where("warehousing_id = ? and status = 1 and project_id in(?)", id, ids).Find(&order).Error
  324. return order, err
  325. }
  326. func ModefySupplyWarehouseInfo(id int64, info models.SupplierWarehouseInfo) error {
  327. 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
  328. return err
  329. }
  330. func ModifySupplyWarehouseOrder(order *models.SupplierWarehousingInfoOrder) error {
  331. 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
  332. return err
  333. }
  334. func UpdateSupplyWaresing(id int64, info models.SupplierWarehouseInfo) error {
  335. 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
  336. return err
  337. }
  338. func GetPurchaseOrderDetail(id int64) (models.SupplierWarehouseInfo, error) {
  339. info := models.SupplierWarehouseInfo{}
  340. err := XTReadDB().Model(&info).Where("id =? and status =1", id).Find(&info).Error
  341. return info, err
  342. }
  343. func FindAllSupplyWarehouseOutOrder(orgid int64) (total int64, err error) {
  344. err = XTReadDB().Model(&models.SpSupplierWarehouseOut{}).Where("user_org_id = ?", orgid).Count(&total).Error
  345. return total, err
  346. }
  347. func FindSupplyWarehouseOutById(orgid int64) (models.SpSupplierWarehouseOut, error) {
  348. out := models.SpSupplierWarehouseOut{}
  349. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Last(&out).Error
  350. return out, err
  351. }
  352. func CreateSupplyWarehouseOut(out models.SpSupplierWarehouseOut) error {
  353. err := XTWriteDB().Create(&out).Error
  354. return err
  355. }
  356. func CreateSupplyWarehousOutOrder(order *models.SpSupplierWarehousingOutOrder) error {
  357. err := XTWriteDB().Create(&order).Error
  358. return err
  359. }
  360. func GetSupplyWarehouseOutById(id int64, user_org_id int64) (order []*models.SpSupplierWarehousingOutOrder, err error) {
  361. err = XTReadDB().Where("warehouse_out_id = ? and status = 1 and user_org_id = ?", id, user_org_id).Find(&order).Error
  362. return order, err
  363. }
  364. func GetSupplyWarehouseOutByIdOne(id int64, user_org_id int64, ids []string) (order []*models.SpSupplierWarehousingOutOrder, err error) {
  365. 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
  366. return order, err
  367. }
  368. func GetAllGoodOderList(check_id int64, keyword string, page int64, limit int64, startime int64, endtime int64, orgid int64) (out []*models.VmSupplierWarehouseOut, total int64, err error) {
  369. db := XTReadDB().Model(&out).Where("sgj_xt.xt_supplier_warehouse_out.status = 1")
  370. likeKey := "%" + keyword + "%"
  371. offset := (page - 1) * limit
  372. if check_id > 0 {
  373. db = db.Where("sgj_xt.xt_supplier_warehouse_out.is_check = ?", check_id)
  374. }
  375. if startime > 0 {
  376. db = db.Where("sgj_xt.xt_supplier_warehouse_out.record_date >= ?", startime)
  377. }
  378. if endtime > 0 {
  379. db = db.Where("sgj_xt.xt_supplier_warehouse_out.record_date<=?", endtime)
  380. }
  381. if len(keyword) > 0 {
  382. db = db.Joins("join sgj_xt.xt_supplier_name on sgj_xt.xt_supplier_name.id = sgj_xt.xt_supplier_warehouse_out.supplier_id")
  383. 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")
  384. }
  385. if orgid > 0 {
  386. db = db.Where("sgj_xt.xt_supplier_warehouse_out.user_org_id = ?", orgid)
  387. }
  388. 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).Find(&out).Error
  389. return out, total, err
  390. }
  391. func GetGoodOrderDetail(id int64, orgid int64) (models.SpSupplierWarehouseOut, error) {
  392. out := models.SpSupplierWarehouseOut{}
  393. err := XTReadDB().Where("id = ? and user_org_id = ? and status = 1", id, orgid).Find(&out).Error
  394. return out, err
  395. }
  396. func UpdateGoodWarehouseOut(id int64, out models.SpSupplierWarehouseOut) error {
  397. err := XTWriteDB().Model(&out).Where("id=? and status = 1", id).Updates(map[string]interface{}{"arrearage": out.Arrearage, "payment": out.Payment, "rate_of_concession": out.RateOfConcession, "discount_amount": out.DiscountAmount, "document_date": out.DocumentDate, "return_remake": out.ReturnRemake}).Error
  398. return err
  399. }
  400. func UpdateGoodWarehouseOutOrder(order *models.SpSupplierWarehousingOutOrder) error {
  401. err := XTWriteDB().Model(&order).Where("id = ? and status = 1", order.ID).Updates(map[string]interface{}{"project_id": order.ProjectId, "is_source": order.IsSource, "count": order.Count, "price": order.Count, "remark": order.Remark, "supply_batch_number": order.SupplyBatchNumber, "supply_product_date": order.SupplyProductDate, "supply_expiry_date": order.SupplyExpiryDate, "supply_type": order.SupplyType, "supply_specification_name": order.SupplySpecificationName, "supply_total": order.SupplySpecificationName, "supply_manufacturer": order.SupplyManufacturer, "name": order.Name, "supply_unit": order.SupplyUnit, "manufacturer_id": order.ManufacturerId, "supply_license_number": order.SupplyLicenseNumber, "warehousing_id": order.WarehousingId, "warehouse_info_id": order.WarehouseInfoId}).Error
  402. return err
  403. }
  404. func DeletePurchOrder(id int64, orgid int64) error {
  405. err := XTWriteDB().Model(&models.SupplierWarehouseInfo{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  406. 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
  407. return err
  408. }
  409. func GetAllPurcaseOrderById(id int64, orgid int64) (order []*models.VSupplierWarehousingInfoOrder, err error) {
  410. err = XTReadDB().Model(&order).Where("warehousing_id = ? and user_org_id = ? and status =1", id, orgid).Find(&order).Error
  411. return order, err
  412. }
  413. func GetAllGoodOrderById(id int64, orgid int64) (order []*models.VSpSupplierWarehousingOutOrder, err error) {
  414. db := XTReadDB().Table("xt_supplier_warehousing_out_order as o").Where("o.status = 1")
  415. if id > 0 {
  416. db = db.Where("o.warehousing_id = ?", id)
  417. }
  418. if orgid > 0 {
  419. db = db.Where("o.user_org_id =?", orgid)
  420. }
  421. err = db.Select("o.id,o.order_number,o.project_id,o.count as count,o.supply_unit,o.is_source").Find(&order).Error
  422. return order, err
  423. }
  424. func GetAllGoodOrderByIdTwo(id int64, orgid int64) (order []*models.SpSupplierWarehousingOutOrder, err error) {
  425. err = XTReadDB().Where("warehousing_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&order).Error
  426. return order, err
  427. }
  428. func GetGoodOrderList(id int64, orgid int64) (info []*models.SpSupplierWarehouseOut, err error) {
  429. err = XTReadDB().Where("warehousing_id = ? and status = 1 and user_org_id = ?", id, orgid).Find(&info).Error
  430. return info, err
  431. }
  432. func GetReturnOrder(id int64, orgid int64) error {
  433. 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
  434. return err
  435. }
  436. func CheckGoodOrder(id int64, orgid int64, out models.SpSupplierWarehouseOut) error {
  437. 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
  438. return err
  439. }
  440. func ModefySupplyWarehousing(is_warehose int64, warehousing_id int64, orgid int64) error {
  441. 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
  442. return err
  443. }
  444. func GetAllDoctorSix(orgid int64, appid int64) (appRole []*models.App_Role, err error) {
  445. err = UserReadDB().Where("org_id = ? AND app_id = ? AND status = 1", orgid, appid).Find(&appRole).Error
  446. return appRole, err
  447. }
  448. func GetSingleDrugWarehouseOrder(record_date int64, orgid int64) (*models.DrugWarehouse, error) {
  449. warehouse := models.DrugWarehouse{}
  450. var err error
  451. err = XTReadDB().Model(&warehouse).Where("warehousing_time = ? and org_id = ? and status = 1", record_date, orgid).Find(&warehouse).Error
  452. if err == gorm.ErrRecordNotFound {
  453. return nil, err
  454. }
  455. if err != nil {
  456. return nil, err
  457. }
  458. return &warehouse, nil
  459. }
  460. func GetSindleWarehouse(record_date int64, orgid int64) (*models.Warehousing, error) {
  461. warehousing := models.Warehousing{}
  462. var err error
  463. err = XTReadDB().Model(&warehousing).Where("warehousing_time = ? and org_id = ? and status = 1", record_date, orgid).Find(&warehousing).Error
  464. if err == gorm.ErrRecordNotFound {
  465. return nil, err
  466. }
  467. if err != nil {
  468. return nil, err
  469. }
  470. return &warehousing, nil
  471. }
  472. func GetLastWarehouseInfoByInfo(orgid int64) (models.Warehousing, error) {
  473. warehousing := models.Warehousing{}
  474. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&warehousing).Error
  475. return warehousing, err
  476. }
  477. func GetSupplyCancelOrder(orgid int64) (total int64, err error) {
  478. err = XTReadDB().Model(&models.SpSupplierWarehouseCancel{}).Where("user_org_id = ?", orgid).Count(&total).Error
  479. return total, err
  480. }
  481. func CreateReturnCacelOrder(cancel models.SpSupplierWarehouseCancel) error {
  482. err := XTWriteDB().Create(&cancel).Error
  483. return err
  484. }
  485. func GetLastReturnCancelOrder(orgid int64) (models.SpSupplierWarehouseCancel, error) {
  486. cancel := models.SpSupplierWarehouseCancel{}
  487. err := XTReadDB().Where("user_org_id =? and status = 1", orgid).Find(&cancel).Error
  488. return cancel, err
  489. }
  490. func CreateCancelReturnOrder(order *models.SpSupplierWarehousingCancelOrder) error {
  491. err := XTWriteDB().Create(&order).Error
  492. return err
  493. }
  494. func GetReturnCancelOrder(id int64, orgid int64) (models.SpSupplierWarehouseCancel, error) {
  495. cancel := models.SpSupplierWarehouseCancel{}
  496. err := XTReadDB().Where("id = ? and status= 1 and user_org_id =?", id, orgid).Find(&cancel).Error
  497. return cancel, err
  498. }
  499. func GetReturnCancelOrderList(id int64, orgid int64) (order models.SpSupplierWarehousingCancelOrder, err error) {
  500. err = XTReadDB().Where("warehouse_cancel_id = ? and user_org_id =? and status = 1", id, orgid).Find(&order).Error
  501. return order, err
  502. }
  503. func GetAllGoodReturnOrderList(checkid int64, keyword string, page int64, limit int64, startime int64, endtime int64, orgid int64) (order []*models.VmSpSupplierWarehouseCancel, total int64, err error) {
  504. db := XTReadDB().Table("sgj_xt.xt_supplier_warehouse_cancel").Where("sgj_xt.xt_supplier_warehouse_cancel.status = 1")
  505. likeKey := "%" + keyword + "%"
  506. offset := (page - 1) * limit
  507. if checkid > 0 {
  508. db = db.Where("sgj_xt.xt_supplier_warehouse_cancel.is_check = ?", checkid)
  509. }
  510. if len(keyword) > 0 {
  511. db = db.Joins("join sgj_xt.xt_supplier_name on sgj_xt.xt_supplier_name.id = sgj_xt.xt_supplier_warehouse_out.supplier_id")
  512. 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")
  513. }
  514. if startime > 0 {
  515. db = db.Where("sgj_xt.xt_supplier_warehouse_cancel.record_date >= ?", startime)
  516. }
  517. if endtime > 0 {
  518. db = db.Where("sgj_xt.xt_supplier_warehouse_cancel.record_date <= ?", endtime)
  519. }
  520. if orgid > 0 {
  521. db = db.Where("sgj_xt.xt_supplier_warehouse_cancel.user_org_id = ?", orgid)
  522. }
  523. err = db.Count(&total).Offset(offset).Limit(limit).Preload("SpSupplierWarehousingCancelOrder", "status= 1 and user_org_id = ?", orgid).Find(&order).Error
  524. return order, total, err
  525. }
  526. func GetGoodReturnDetail(id int64, orgid int64) (models.VmSpSupplierWarehouseCancel, error) {
  527. cancel := models.VmSpSupplierWarehouseCancel{}
  528. db := XTReadDB().Model(&cancel).Where("status = 1")
  529. if id > 0 {
  530. db = db.Where("id = ?", id)
  531. }
  532. if orgid > 0 {
  533. db = db.Where("user_org_id = ?", orgid)
  534. }
  535. err := db.Find(&cancel).Error
  536. return cancel, err
  537. }
  538. func GetGoodReturnOrderDetail(id int64, orgid int64) (order []*models.SpSupplierWarehousingCancelOrder, err error) {
  539. db := XTReadDB().Model(&order).Where("status =1")
  540. if id > 0 {
  541. db = db.Where("warehouse_cancel_id = ?", id)
  542. }
  543. if orgid > 0 {
  544. db = db.Where("user_org_id = ?", orgid)
  545. }
  546. err = db.Find(&order).Error
  547. return order, err
  548. }
  549. func UpdateWarehouseCancelOrder(order *models.SpSupplierWarehousingCancelOrder) error {
  550. err = XTWriteDB().Where("id = ? and status = 1", order.ID).Updates(map[string]interface{}{"manufacturer_id": order.ManufacturerId, "order_number": order.OrderNumber, "project_id": order.ProjectId, "is_source": order.IsSource, "count": order.Count, "price": order.Price, "remark": order.Remark, "rate_of_concession": order.RateOfConcession, "discount_amount": order.DiscountAmount, "type": order.Type, "supply_specification_name": order.SupplySpecificationName, "supply_type": order.SupplyType, "supply_total": order.SupplyTotal, "supply_manufacturer": order.SupplyManufacturer, "name": order.Name, "supply_unit": order.SupplyUnit, "supply_license_number": order.SupplyLicenseNumber}).Error
  551. return err
  552. }
  553. func UpdateWarehouseCancel(id int64, cancel models.SpSupplierWarehouseCancel) error {
  554. 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
  555. return err
  556. }
  557. func GetSupplyCancelOrderById(warehouse_out_id int64, orgid int64) (*models.SpSupplierWarehouseCancel, error) {
  558. cancel := models.SpSupplierWarehouseCancel{}
  559. var err error
  560. err = XTReadDB().Where("warehouse_out_id = ? and user_org_id = ? and status = 1", warehouse_out_id, orgid).Find(&cancel).Error
  561. if err == gorm.ErrRecordNotFound {
  562. return nil, err
  563. }
  564. if err != nil {
  565. return nil, err
  566. }
  567. return &cancel, nil
  568. }
  569. func UpdateSupplyGoodOrder(id int64, out models.SpSupplierWarehouseOut) error {
  570. 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
  571. return err
  572. }
  573. func UpdateDrugSupplyWarehousingInfo(id int64, orgid int64) error {
  574. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("supply_warehouse_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  575. return err
  576. }
  577. func UpdateDrugSupplyFlow(id int64, orgid int64) error {
  578. 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
  579. return err
  580. }
  581. func UpdateGoodSupplyWarehousingInfo(id int64, orgid int64) error {
  582. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("supply_warehouse_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  583. return err
  584. }
  585. func UpdateGoodSupplyFlow(id int64, orgid int64) error {
  586. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("supply_warehouse_id = ? and user_org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  587. return err
  588. }
  589. func GetDrugSupplyWarehousingById(id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  590. err = XTReadDB().Where("supply_warehouse_id <> ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  591. return info, err
  592. }
  593. func UpdateSupplyWarehousing(id int64, orgid int64) error {
  594. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("supply_warehouse_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  595. return err
  596. }
  597. func GetGoodSupplyWarehousingById(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  598. err = XTReadDB().Where("supply_warehouse_id <> ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  599. return info, err
  600. }
  601. func UpdateGoodWarehousing(id int64, orgid int64) error {
  602. err := XTWriteDB().Model(&models.Warehousing{}).Where("supply_warehouse_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  603. return err
  604. }
  605. func UpdateSupplyWarehousingById(id int64, orgid int64) error {
  606. err := XTWriteDB().Model(&models.SupplierWarehouseInfo{}).Where("id = ? and user_org_id =? and status = 1", id, orgid).Updates(map[string]interface{}{"is_warehouse": 1}).Error
  607. return err
  608. }
  609. func DeleteGoodOrder(id int64, orgid int64) error {
  610. err := XTWriteDB().Model(&models.SpSupplierWarehouseOut{}).Where("id = ? and status = 1 and user_org_id = ?", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  611. 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
  612. return err
  613. }
  614. func GetSupplyCancelWarehouse(id int64, orgid int64) (cancel []*models.SpSupplierWarehouseCancel, err error) {
  615. err = XTReadDB().Where("warehouse_out_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&cancel).Error
  616. return cancel, err
  617. }
  618. func GetGoodOrderListById(id int64, orgid int64) (out []*models.VmSpSupplierWarehousingOutOrder, err error) {
  619. err = XTReadDB().Where("warehouse_out_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&out).Error
  620. return out, err
  621. }
  622. func GetGoodCanceListById(id int64, orgid int64) (cancel []*models.VmSpSupplierWarehousingCancelOrder, err error) {
  623. err = XTReadDB().Where("warehouse_out_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&cancel).Error
  624. return cancel, err
  625. }