supply_service.go 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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 GetAllGoodOderList(check_id int64, keyword string, page int64, limit int64, startime int64, endtime int64, orgid int64) (out []*models.VmSupplierWarehouseOut, total int64, err error) {
  365. db := XTReadDB().Model(&out).Where("sgj_xt.xt_supplier_warehouse_out.status = 1")
  366. likeKey := "%" + keyword + "%"
  367. offset := (page - 1) * limit
  368. if check_id > 0 {
  369. db = db.Where("sgj_xt.xt_supplier_warehouse_out.is_check = ?", check_id)
  370. }
  371. if startime > 0 {
  372. db = db.Where("sgj_xt.xt_supplier_warehouse_out.record_date >= ?", startime)
  373. }
  374. if endtime > 0 {
  375. db = db.Where("sgj_xt.xt_supplier_warehouse_out.record_date<=?", endtime)
  376. }
  377. if len(keyword) > 0 {
  378. db = db.Joins("join sgj_xt.xt_supplier_name on sgj_xt.xt_supplier_name.id = sgj_xt.xt_supplier_warehouse_out.supplier_id")
  379. 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")
  380. }
  381. if orgid > 0 {
  382. db = db.Where("sgj_xt.xt_supplier_warehouse_out.user_org_id = ?", orgid)
  383. }
  384. 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
  385. return out, total, err
  386. }
  387. func GetGoodOrderDetail(id int64, orgid int64) (models.SpSupplierWarehouseOut, error) {
  388. out := models.SpSupplierWarehouseOut{}
  389. err := XTReadDB().Where("id = ? and user_org_id = ? and status = 1", id, orgid).Find(&out).Error
  390. return out, err
  391. }
  392. func UpdateGoodWarehouseOut(id int64, out models.SpSupplierWarehouseOut) error {
  393. 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
  394. return err
  395. }
  396. func UpdateGoodWarehouseOutOrder(order *models.SpSupplierWarehousingOutOrder) error {
  397. 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
  398. return err
  399. }
  400. func DeletePurchOrder(id int64, orgid int64) error {
  401. err := XTWriteDB().Model(&models.SupplierWarehouseInfo{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  402. 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
  403. return err
  404. }
  405. func GetAllPurcaseOrderById(id int64, orgid int64) (order []*models.VSupplierWarehousingInfoOrder, err error) {
  406. err = XTReadDB().Model(&order).Where("warehousing_id = ? and user_org_id = ? and status =1", id, orgid).Find(&order).Error
  407. return order, err
  408. }
  409. func GetAllGoodOrderById(id int64, orgid int64) (order []*models.VSpSupplierWarehousingOutOrder, err error) {
  410. db := XTReadDB().Table("xt_supplier_warehousing_out_order as o").Where("o.status = 1")
  411. if id > 0 {
  412. db = db.Where("o.warehousing_id = ?", id)
  413. }
  414. if orgid > 0 {
  415. db = db.Where("o.user_org_id =?", orgid)
  416. }
  417. err = db.Select("o.id,o.order_number,o.project_id,o.count as count,o.supply_unit,o.is_source").Find(&order).Error
  418. return order, err
  419. }
  420. func GetAllGoodOrderByIdTwo(id int64, orgid int64) (order []*models.SpSupplierWarehousingOutOrder, err error) {
  421. err = XTReadDB().Where("warehousing_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&order).Error
  422. return order, err
  423. }
  424. func GetGoodOrderList(id int64, orgid int64) (info []*models.SpSupplierWarehouseOut, err error) {
  425. err = XTReadDB().Where("warehousing_id = ? and status = 1 and user_org_id = ?", id, orgid).Find(&info).Error
  426. return info, err
  427. }
  428. func GetReturnOrder(id int64, orgid int64) error {
  429. 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
  430. return err
  431. }
  432. func CheckGoodOrder(id int64, orgid int64, out models.SpSupplierWarehouseOut) error {
  433. 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
  434. return err
  435. }
  436. func ModefySupplyWarehousing(is_warehose int64, warehousing_id int64, orgid int64) error {
  437. 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
  438. return err
  439. }
  440. func GetAllDoctorSix(orgid int64, appid int64) (appRole []*models.App_Role, err error) {
  441. err = UserReadDB().Where("org_id = ? AND app_id = ? AND status = 1", orgid, appid).Find(&appRole).Error
  442. return appRole, err
  443. }
  444. func GetSingleDrugWarehouseOrder(record_date int64, orgid int64) (*models.DrugWarehouse, error) {
  445. warehouse := models.DrugWarehouse{}
  446. var err error
  447. err = XTReadDB().Model(&warehouse).Where("warehousing_time = ? and org_id = ? and status = 1", record_date, orgid).Find(&warehouse).Error
  448. if err == gorm.ErrRecordNotFound {
  449. return nil, err
  450. }
  451. if err != nil {
  452. return nil, err
  453. }
  454. return &warehouse, nil
  455. }
  456. func GetSindleWarehouse(record_date int64, orgid int64) (*models.Warehousing, error) {
  457. warehousing := models.Warehousing{}
  458. var err error
  459. err = XTReadDB().Model(&warehousing).Where("warehousing_time = ? and org_id = ? and status = 1", record_date, orgid).Find(&warehousing).Error
  460. if err == gorm.ErrRecordNotFound {
  461. return nil, err
  462. }
  463. if err != nil {
  464. return nil, err
  465. }
  466. return &warehousing, nil
  467. }
  468. func GetLastWarehouseInfoByInfo(orgid int64) (models.Warehousing, error) {
  469. warehousing := models.Warehousing{}
  470. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&warehousing).Error
  471. return warehousing, err
  472. }
  473. func GetSupplyCancelOrder(orgid int64) (total int64, err error) {
  474. err = XTReadDB().Model(&models.SpSupplierWarehouseCancel{}).Where("user_org_id = ?", orgid).Count(&total).Error
  475. return total, err
  476. }
  477. func CreateReturnCacelOrder(cancel models.SpSupplierWarehouseCancel) error {
  478. err := XTWriteDB().Create(&cancel).Error
  479. return err
  480. }
  481. func GetLastReturnCancelOrder(orgid int64) (models.SpSupplierWarehouseCancel, error) {
  482. cancel := models.SpSupplierWarehouseCancel{}
  483. err := XTReadDB().Where("user_org_id =? and status = 1", orgid).Find(&cancel).Error
  484. return cancel, err
  485. }
  486. func CreateCancelReturnOrder(order *models.SpSupplierWarehousingCancelOrder) error {
  487. err := XTWriteDB().Create(&order).Error
  488. return err
  489. }
  490. func GetReturnCancelOrder(id int64, orgid int64) (models.SpSupplierWarehouseCancel, error) {
  491. cancel := models.SpSupplierWarehouseCancel{}
  492. err := XTReadDB().Where("id = ? and status= 1 and user_org_id =?", id, orgid).Find(&cancel).Error
  493. return cancel, err
  494. }
  495. func GetReturnCancelOrderList(id int64, orgid int64) (order models.SpSupplierWarehousingCancelOrder, err error) {
  496. err = XTReadDB().Where("warehouse_cancel_id = ? and user_org_id =? and status = 1", id, orgid).Find(&order).Error
  497. return order, err
  498. }
  499. func GetAllGoodReturnOrderList(checkid int64, keyword string, page int64, limit int64, startime int64, endtime int64, orgid int64) (order []*models.VmSpSupplierWarehouseCancel, total int64, err error) {
  500. db := XTReadDB().Table("sgj_xt.xt_supplier_warehouse_cancel").Where("sgj_xt.xt_supplier_warehouse_cancel.status = 1")
  501. likeKey := "%" + keyword + "%"
  502. offset := (page - 1) * limit
  503. if checkid > 0 {
  504. db = db.Where("sgj_xt.xt_supplier_warehouse_cancel.is_check = ?", checkid)
  505. }
  506. if len(keyword) > 0 {
  507. db = db.Joins("join sgj_xt.xt_supplier_name on sgj_xt.xt_supplier_name.id = sgj_xt.xt_supplier_warehouse_out.supplier_id")
  508. 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")
  509. }
  510. if startime > 0 {
  511. db = db.Where("sgj_xt.xt_supplier_warehouse_cancel.record_date >= ?", startime)
  512. }
  513. if endtime > 0 {
  514. db = db.Where("sgj_xt.xt_supplier_warehouse_cancel.record_date <= ?", endtime)
  515. }
  516. if orgid > 0 {
  517. db = db.Where("sgj_xt.xt_supplier_warehouse_cancel.user_org_id = ?", orgid)
  518. }
  519. err = db.Count(&total).Offset(offset).Limit(limit).Preload("SpSupplierWarehousingCancelOrder", "status= 1 and user_org_id = ?", orgid).Find(&order).Error
  520. return order, total, err
  521. }
  522. func GetGoodReturnDetail(id int64, orgid int64) (models.VmSpSupplierWarehouseCancel, error) {
  523. cancel := models.VmSpSupplierWarehouseCancel{}
  524. db := XTReadDB().Model(&cancel).Where("status = 1")
  525. if id > 0 {
  526. db = db.Where("id = ?", id)
  527. }
  528. if orgid > 0 {
  529. db = db.Where("user_org_id = ?", orgid)
  530. }
  531. err := db.Find(&cancel).Error
  532. return cancel, err
  533. }
  534. func GetGoodReturnOrderDetail(id int64, orgid int64) (order []*models.SpSupplierWarehousingCancelOrder, err error) {
  535. db := XTReadDB().Model(&order).Where("status =1")
  536. if id > 0 {
  537. db = db.Where("warehouse_cancel_id = ?", id)
  538. }
  539. if orgid > 0 {
  540. db = db.Where("user_org_id = ?", orgid)
  541. }
  542. err = db.Find(&order).Error
  543. return order, err
  544. }
  545. func UpdateWarehouseCancelOrder(order *models.SpSupplierWarehousingCancelOrder) error {
  546. 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
  547. return err
  548. }
  549. func UpdateWarehouseCancel(id int64, cancel models.SpSupplierWarehouseCancel) error {
  550. 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
  551. return err
  552. }
  553. func GetSupplyCancelOrderById(warehouse_out_id int64, orgid int64) (*models.SpSupplierWarehouseCancel, error) {
  554. cancel := models.SpSupplierWarehouseCancel{}
  555. var err error
  556. err = XTReadDB().Where("warehouse_out_id = ? and user_org_id = ? and status = 1", warehouse_out_id, orgid).Find(&cancel).Error
  557. if err == gorm.ErrRecordNotFound {
  558. return nil, err
  559. }
  560. if err != nil {
  561. return nil, err
  562. }
  563. return &cancel, nil
  564. }
  565. func UpdateSupplyGoodOrder(id int64, out models.SpSupplierWarehouseOut) error {
  566. 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
  567. return err
  568. }
  569. func UpdateDrugSupplyWarehousingInfo(id int64, orgid int64) error {
  570. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("supply_warehouse_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  571. return err
  572. }
  573. func UpdateDrugSupplyFlow(id int64, orgid int64) error {
  574. 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
  575. return err
  576. }
  577. func UpdateGoodSupplyWarehousingInfo(id int64, orgid int64) error {
  578. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("supply_warehouse_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  579. return err
  580. }
  581. func UpdateGoodSupplyFlow(id int64, orgid int64) error {
  582. 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
  583. return err
  584. }
  585. func GetDrugSupplyWarehousingById(id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  586. err = XTReadDB().Where("supply_warehouse_id <> ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  587. return info, err
  588. }
  589. func UpdateSupplyWarehousing(id int64, orgid int64) error {
  590. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("supply_warehouse_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  591. return err
  592. }
  593. func GetGoodSupplyWarehousingById(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  594. err = XTReadDB().Where("supply_warehouse_id <> ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  595. return info, err
  596. }
  597. func UpdateGoodWarehousing(id int64, orgid int64) error {
  598. err := XTWriteDB().Model(&models.Warehousing{}).Where("supply_warehouse_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  599. return err
  600. }
  601. func UpdateSupplyWarehousingById(id int64, orgid int64) error {
  602. err := XTWriteDB().Model(&models.SupplierWarehouseInfo{}).Where("id = ? and user_org_id =? and status = 1", id, orgid).Updates(map[string]interface{}{"is_warehouse": 1}).Error
  603. return err
  604. }
  605. func DeleteGoodOrder(id int64, orgid int64) error {
  606. err := XTWriteDB().Model(&models.SpSupplierWarehouseOut{}).Where("id = ? and status = 1 and user_org_id = ?", id, orgid).Updates(map[string]interface{}{"status": 0}).Error
  607. 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
  608. return err
  609. }
  610. func GetSupplyCancelWarehouse(id int64, orgid int64) (cancel []*models.SpSupplierWarehouseCancel, err error) {
  611. err = XTReadDB().Where("warehouse_out_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&cancel).Error
  612. return cancel, err
  613. }
  614. func GetGoodOrderListById(id int64, orgid int64) (out []*models.VmSpSupplierWarehousingOutOrder, err error) {
  615. err = XTReadDB().Where("warehouse_out_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&out).Error
  616. return out, err
  617. }
  618. func GetGoodCanceListById(id int64, orgid int64) (cancel []*models.VmSpSupplierWarehousingCancelOrder, err error) {
  619. err = XTReadDB().Where("warehouse_out_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&cancel).Error
  620. return cancel, err
  621. }