supply_service.go 50KB

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