supply_service.go 51KB

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