supply_service.go 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. package service
  2. import (
  3. "XT_New/models"
  4. "github.com/jinzhu/gorm"
  5. "time"
  6. )
  7. //根据供应商编号获取首要联系人
  8. func FindName(code string) (fistname models.SpSupplierContacts, err error) {
  9. err = XTReadDB().Model(&models.SpSupplierContacts{}).Where("supplier_code = ? and status = 1", code).First(&fistname).Error
  10. return fistname, err
  11. }
  12. //供应商分页
  13. func GetSupplyList(ctype int64, page int64, limit int64, keyword string) (supplylist []*models.SpSupplierName, total int64, err error) {
  14. db := XTReadDB().Model(&supplylist).Where("xt_supplier_name.status = 1 ")
  15. offset := (page - 1) * limit
  16. if len(keyword) > 0 {
  17. keyword = "%" + keyword + "%" //联系人
  18. db = db.Joins("join xt_supplier_contacts on xt_supplier_contacts.supplier_code = xt_supplier_name.supplier_code")
  19. 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")
  20. }
  21. if ctype > 0 {
  22. db = db.Where("xt_supplier_name.supplier_type = ?", ctype)
  23. }
  24. err = db.Count(&total).Offset(offset).Limit(limit).Find(&supplylist).Error
  25. return supplylist, total, err
  26. }
  27. //修改供应商和联系人
  28. func UpdateSupplyAndContact(thisStockIn []interface{}, suid, orgId, supplierType, tcreater int64, supplierCode, supplierName, number, bank, bankAccount string, vatRate float64) error {
  29. tx := XTWriteDB().Begin()
  30. defer func() {
  31. if err != nil {
  32. tx.Rollback()
  33. } else {
  34. tx.Commit()
  35. }
  36. }()
  37. for _, item := range thisStockIn {
  38. items := item.(map[string]interface{})
  39. //查询是否
  40. if items["id"] == "" {
  41. var tmp float64 = 0
  42. items["id"] = tmp
  43. }
  44. //id, _ := strconv.ParseInt(items["id"].(string), 10, 64)
  45. id := int64(items["id"].(float64))
  46. name := items["name"].(string)
  47. phone := items["phone"].(string)
  48. address := items["address"].(string)
  49. isfirst := int64(items["is_first"].(int))
  50. updatecontacts := models.SpSupplierContacts{
  51. ID: id,
  52. Name: name,
  53. Phone: phone,
  54. Address: address,
  55. IsFirst: isfirst,
  56. SupplierCode: supplierCode,
  57. UserOrgId: orgId,
  58. Status: 1,
  59. Ctime: 0,
  60. Mtime: time.Now().Unix(),
  61. }
  62. if id == 0 {
  63. spcontacts := models.SpSupplierContacts{
  64. Name: name,
  65. Phone: phone,
  66. Address: address,
  67. IsFirst: isfirst,
  68. SupplierCode: supplierCode,
  69. UserOrgId: orgId,
  70. Status: 1,
  71. Ctime: time.Now().Unix(),
  72. Mtime: 0,
  73. }
  74. err = SaveContacts(spcontacts, tx)
  75. if err != nil {
  76. return err
  77. }
  78. } else {
  79. err = UpdateContact(updatecontacts, tx)
  80. }
  81. var tmpid int64
  82. if isfirst == 1 {
  83. if id == 0 {
  84. var spconid []*models.SpSupplierContacts
  85. spconid, err = SaveContactsId(tx)
  86. if err != nil {
  87. return err
  88. }
  89. tmpid = spconid[0].ID
  90. } else {
  91. tmpid = id
  92. }
  93. //更新供应商
  94. upsupply := models.SpSupplierName{
  95. ID: suid,
  96. SupplierCode: supplierCode,
  97. SupplierName: supplierName,
  98. SupplierType: supplierType,
  99. VatRate: vatRate,
  100. Number: number,
  101. Bank: bank,
  102. BankAccount: bankAccount,
  103. UserOrgId: orgId,
  104. Status: 1,
  105. ContactsId: tmpid,
  106. Mtime: time.Now().Unix(),
  107. Modify: tcreater,
  108. }
  109. err = UpdateSupplyName(upsupply, tx)
  110. if err != nil {
  111. return err
  112. }
  113. }
  114. }
  115. return err
  116. }
  117. //更新一条供应商的信息
  118. func UpdateSupplyName(upsupply models.SpSupplierName, tx *gorm.DB) error {
  119. 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
  120. return err
  121. }
  122. //更新一条联系人的信息
  123. func UpdateContact(updatecontacts models.SpSupplierContacts, tx *gorm.DB) error {
  124. 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
  125. return err
  126. }
  127. //查询供应商单条记录
  128. func GetSupplyOne(id int64) (supply models.SpSupplierName, err error) {
  129. err = XTReadDB().Model(&models.SpSupplierName{}).Where("id = ? and status = 1", id).First(&supply).Error
  130. return supply, err
  131. }
  132. //删除单条联系人记录
  133. func DelContactOne(id int64) error {
  134. err := XTWriteDB().Model(&models.SpSupplierContacts{}).Where("id = ?", id).Update("status", 0).Error
  135. return err
  136. }
  137. //获取单条供应商和涉及到的联系人记录
  138. func GetSupplyAndContactOne(id int64) (supply models.SpSupplierName, contact []*models.SpSupplierContacts, err error) {
  139. err = XTReadDB().Model(&models.SpSupplierName{}).Where("id = ? and status = 1", id).First(&supply).Error
  140. code := supply.SupplierCode
  141. err = XTReadDB().Model(&models.SpSupplierContacts{}).Where("supplier_code = ? and status = 1", code).Find(&contact).Error
  142. return supply, contact, err
  143. }
  144. //删除供应商及联系人
  145. func DelSupply(supply models.SpSupplierName) error {
  146. err := XTWriteDB().Model(&supply).Update("status", 0).Error
  147. return err
  148. }
  149. //保存供应商和联系人
  150. func SaveSupplyAndContact(thisStockIn []interface{}, orgId, supplierType, tcreater int64, supplierCode, supplierName, number, bank, bankAccount string, vatRate float64) error {
  151. tx := XTWriteDB().Begin()
  152. defer func() {
  153. if err != nil {
  154. tx.Rollback()
  155. } else {
  156. tx.Commit()
  157. }
  158. }()
  159. for _, item := range thisStockIn {
  160. items := item.(map[string]interface{})
  161. name := items["name"].(string)
  162. phone := items["phone"].(string)
  163. address := items["address"].(string)
  164. isfirst := int64(items["is_first"].(int))
  165. spcontacts := models.SpSupplierContacts{
  166. Name: name,
  167. Phone: phone,
  168. Address: address,
  169. IsFirst: isfirst,
  170. SupplierCode: supplierCode,
  171. UserOrgId: orgId,
  172. Status: 1,
  173. Ctime: time.Now().Unix(),
  174. Mtime: 0,
  175. }
  176. err = SaveContacts(spcontacts, tx)
  177. if isfirst == 1 {
  178. var spconid []*models.SpSupplierContacts
  179. spconid, err = SaveContactsId(tx)
  180. if err != nil {
  181. return err
  182. }
  183. tmpid := spconid[0].ID
  184. //保存供应商
  185. supply := models.SpSupplierName{
  186. SupplierCode: supplierCode,
  187. SupplierName: supplierName,
  188. SupplierType: supplierType,
  189. VatRate: vatRate,
  190. Number: number,
  191. Bank: bank,
  192. BankAccount: bankAccount,
  193. UserOrgId: orgId,
  194. Status: 1,
  195. ContactsId: tmpid,
  196. Ctime: time.Now().Unix(),
  197. Mtime: 0,
  198. Creater: tcreater,
  199. Modify: tcreater,
  200. }
  201. err = SaveSupply(supply, tx)
  202. if err != nil {
  203. return err
  204. }
  205. }
  206. if err != nil {
  207. return err
  208. }
  209. }
  210. return err
  211. }
  212. //获取供应商编码
  213. func GetSuppliyCode() (spcode []*models.SpSupplierName, err error) {
  214. err = XTReadDB().Model(&models.SpSupplierName{}).Select("supplier_code").Where("supplier_code like 'gys%' and status = 1 ").Order("supplier_code desc").First(&spcode).Error
  215. return spcode, err
  216. }
  217. //查询供应商的名字是否有重复
  218. func FindSupplierName(supplierName string) (sbool bool, err error) {
  219. var total int
  220. err = XTReadDB().Model(&models.SpSupplierName{}).Where("supplier_name = ? and status = 1", supplierName).Count(&total).Error
  221. if total != 0 {
  222. sbool = true
  223. } else {
  224. sbool = false
  225. }
  226. return sbool, err
  227. }
  228. //查询供应商的编号是否有重复
  229. func FindSupplierCode(supplierCode string) (codebool bool, err error) {
  230. var total int
  231. err = XTReadDB().Model(&models.SpSupplierName{}).Where("supplier_name = ? and status = 1", supplierCode).Count(&total).Error
  232. if total != 0 {
  233. codebool = true
  234. } else {
  235. codebool = false
  236. }
  237. return codebool, err
  238. }
  239. //保存一条供应商数据
  240. func SaveSupply(supply models.SpSupplierName, tx *gorm.DB) error {
  241. err := tx.Create(&supply).Error
  242. return err
  243. }
  244. //获取联系人的id
  245. func SaveContactsId(tx *gorm.DB) (spconid []*models.SpSupplierContacts, err error) {
  246. err = tx.Model(&models.SpSupplierContacts{}).Select("id").Where("and status = 1").Order("id desc").First(&spconid).Error
  247. return
  248. }
  249. //保存一条联系人数据
  250. func SaveContacts(spcontacts models.SpSupplierContacts, tx *gorm.DB) error {
  251. err := tx.Create(&spcontacts).Error
  252. return err
  253. }
  254. func GetSupplyDrugList(orgid int64) (drug []*models.SpBaseDrug, err error) {
  255. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  256. if orgid > 0 {
  257. db = db.Where("x.org_id = ?", orgid)
  258. }
  259. err = db.Preload("DrugWarehouseInfo", "status = 1 and org_id = ? and (stock_max_number >0 or stock_min_number>0)", orgid).Find(&drug).Error
  260. return drug, err
  261. }
  262. func GetSupplyGoodList(orgid int64) (good []*models.SpGoodInformation, err error) {
  263. db := XTReadDB().Table("xt_good_information as x").Where("x.status = 1")
  264. if orgid > 0 {
  265. db = db.Where("x.org_id = ?", orgid)
  266. }
  267. err = db.Preload("GoodWarehouseInfo", "status = 1 and org_id = ?", orgid).Find(&good).Error
  268. return good, err
  269. }
  270. func GetSupplierList(orgid int64) (suppler []*models.SpSupplierName, err error) {
  271. err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&suppler).Error
  272. return suppler, err
  273. }
  274. func FindAllSupplyOrder(orgid int64) (total int64, err error) {
  275. err = XTReadDB().Model(&models.SupplierWarehouseInfo{}).Where("user_org_id = ?", orgid).Count(&total).Error
  276. return total, err
  277. }
  278. func CreateSupplyWarehouse(info models.SupplierWarehouseInfo) error {
  279. err := XTWriteDB().Create(&info).Error
  280. return err
  281. }
  282. func FindLastSupplyWarehouseInfo(orgid int64) (models.SupplierWarehouseInfo, error) {
  283. info := models.SupplierWarehouseInfo{}
  284. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Last(&info).Error
  285. return info, err
  286. }
  287. func CreateSupplyWarehousingOrder(order *models.SupplierWarehousingInfoOrder) error {
  288. err := XTWriteDB().Create(&order).Error
  289. return err
  290. }
  291. func GetAllPurchaseOrderList(check_id int64, startime int64, endtime int64, keyword string, page int64, limit int64, orgid int64) (info []*models.VmSupplierWarehouseInfo, total int64, err error) {
  292. db := XTReadDB().Model(&info).Where("sgj_xt.xt_supplier_warehouse_info.status = 1")
  293. likeKey := "%" + keyword + "%"
  294. offset := (page - 1) * limit
  295. if check_id > 0 {
  296. db = db.Where("sgj_xt.xt_supplier_warehouse_info.is_check = ?", check_id)
  297. }
  298. if startime > 0 {
  299. db = db.Where("sgj_xt.xt_supplier_warehouse_info.record_date >= ?", startime)
  300. }
  301. if endtime > 0 {
  302. db = db.Where("sgj_xt.xt_supplier_warehouse_info.record_date<=?", endtime)
  303. }
  304. if len(keyword) > 0 {
  305. db = db.Joins("join sgj_xt.xt_supplier_name on sgj_xt.xt_supplier_name.id = sgj_xt.xt_supplier_warehouse_info.supplier_id")
  306. 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")
  307. }
  308. if orgid > 0 {
  309. db = db.Where("sgj_xt.xt_supplier_warehouse_info.user_org_id = ?", orgid)
  310. }
  311. 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
  312. return info, total, err
  313. }
  314. func GetSupplyWarehousingOrderInfo(id int64) (order []*models.SupplierWarehousingInfoOrder, err error) {
  315. err = XTReadDB().Where("warehousing_id = ? and status = 1", id).Find(&order).Error
  316. return order, err
  317. }
  318. func GetSupplyWarehousingOrderInfoTwo(id int64, ids []string) (order []*models.SupplierWarehousingInfoOrder, err error) {
  319. err = XTReadDB().Where("warehousing_id = ? and status = 1 and project_id in(?)", id, ids).Find(&order).Error
  320. return order, err
  321. }
  322. func ModefySupplyWarehouseInfo(id int64, info models.SupplierWarehouseInfo) error {
  323. 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
  324. return err
  325. }
  326. func ModifySupplyWarehouseOrder(order *models.SupplierWarehousingInfoOrder) error {
  327. 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
  328. return err
  329. }
  330. func UpdateSupplyWaresing(id int64, info models.SupplierWarehouseInfo) error {
  331. 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
  332. return err
  333. }
  334. func GetPurchaseOrderDetail(id int64) (models.SupplierWarehouseInfo, error) {
  335. info := models.SupplierWarehouseInfo{}
  336. err := XTReadDB().Model(&info).Where("id =? and status =1", id).Find(&info).Error
  337. return info, err
  338. }
  339. func FindAllSupplyWarehouseOutOrder(orgid int64) (total int64, err error) {
  340. err = XTReadDB().Model(&models.SpSupplierWarehouseOut{}).Where("user_org_id = ?", orgid).Count(&total).Error
  341. return total, err
  342. }
  343. func FindSupplyWarehouseOutById(orgid int64) (models.SpSupplierWarehouseOut, error) {
  344. out := models.SpSupplierWarehouseOut{}
  345. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Last(&out).Error
  346. return out, err
  347. }
  348. func CreateSupplyWarehouseOut(out models.SpSupplierWarehouseOut) error {
  349. err := XTWriteDB().Create(&out).Error
  350. return err
  351. }
  352. func CreateSupplyWarehousOutOrder(order *models.SpSupplierWarehousingOutOrder) error {
  353. err := XTWriteDB().Create(&order).Error
  354. return err
  355. }
  356. func GetSupplyWarehouseOutById(id int64, user_org_id int64) (order []*models.SpSupplierWarehousingOutOrder, err error) {
  357. err = XTReadDB().Where("warehouse_out_id = ? and status = 1 and user_org_id = ?", id, user_org_id).Find(&order).Error
  358. return order, err
  359. }
  360. func GetSupplyWarehouseOutByIdOne(id int64, user_org_id int64, ids []string) (order []*models.SpSupplierWarehousingOutOrder, err error) {
  361. 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
  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. }