new_warehouse_service.go 107KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843
  1. package service
  2. import (
  3. "XT_New/models"
  4. "errors"
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. "math"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. //耗材出库
  13. func ConsumablesGoodDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, count int64) (err error) {
  14. //开事务
  15. var deliver_number int64 = 0
  16. var stock_number int64 = 0
  17. var maxNumber int64 = 0
  18. deliver_number = goods.Count
  19. // 根据先进先出原则,查询最先入库的批次,进行出库
  20. // 如果没有对应的库存,则报错
  21. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  22. if err != nil {
  23. return errors.New("库存数量不足")
  24. }
  25. stock_number = warehouse.StockCount
  26. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  27. if stock_number >= deliver_number {
  28. maxNumber = goods.Count
  29. //出库
  30. warehouse.StockCount = warehouse.StockCount - maxNumber
  31. warehouse.Mtime = time.Now().Unix()
  32. if warehouse.StockCount < 0 {
  33. return errors.New("库存数量不足")
  34. }
  35. errThree := UpDateWarehouseInfoByStock(&warehouse)
  36. if errThree != nil {
  37. return errThree
  38. }
  39. //更新his_prescripton_project出库状态值
  40. UpdateHisPrescriptionProjectStatus(goods.GoodId, record_time, orgID, patient_id)
  41. //查询剩余库存
  42. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID)
  43. var sum_count int64
  44. for _, item := range goodList {
  45. sum_count += item.StockCount
  46. }
  47. warehouseOutInfo := &models.WarehouseOutInfo{
  48. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  49. WarehouseOutId: warehouseOut.ID,
  50. WarehouseInfotId: warehouse.ID,
  51. Status: 1,
  52. Ctime: time.Now().Unix(),
  53. Remark: warehouse.Remark,
  54. OrgId: orgID,
  55. Type: 1,
  56. Manufacturer: warehouse.Manufacturer,
  57. Dealer: warehouse.Dealer,
  58. IsSys: 1,
  59. SysRecordTime: record_time,
  60. GoodTypeId: goods.GoodTypeId,
  61. GoodId: goods.GoodId,
  62. PatientId: patient_id,
  63. Number: warehouse.Number,
  64. LicenseNumber: warehouse.LicenseNumber,
  65. Price: warehouse.PackingPrice,
  66. ExpiryDate: warehouse.ExpiryDate,
  67. ProductDate: warehouse.ProductDate,
  68. ProjectId: goods.ProjectId,
  69. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  70. StorehouseId: goods.StorehouseId,
  71. IsCheck: 1,
  72. OverCount: sum_count,
  73. RegisterNumber: warehouse.RegisterNumber,
  74. }
  75. warehouseOutInfo.Count = count
  76. if orgID == 9671 || orgID == 10265 {
  77. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  78. warehouseOutInfo.Price = goodsInfo.PackingPrice
  79. }
  80. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
  81. if errcodes == gorm.ErrRecordNotFound {
  82. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  83. if errOne != nil {
  84. return errOne
  85. }
  86. } else if errcodes == nil {
  87. outInfoOne, _ := GetWarehouseOutInfoIsExistTwo(goods.GoodId, patient_id, record_time, goods.ProjectId)
  88. if count != outInfoOne.Count {
  89. UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId)
  90. }
  91. }
  92. lastOut, _ := FindWarehouseOutInfoByPatientId(patient_id, record_time, goods.GoodId, orgID)
  93. //查询已经出库的数据
  94. flowGood, _ := GetStockFlowIsBatchNumber(warehouse.ID, patient_id, record_time, goods.GoodId)
  95. var out_count int64
  96. var out_count_one int64
  97. for _, item := range flowGood {
  98. out_count += item.Count
  99. }
  100. //查询退库数据
  101. flowGoodTwo, _ := GetStockFlowIsBatchNumberThree(patient_id, record_time, goods.GoodId)
  102. for _, item := range flowGoodTwo {
  103. out_count_one += item.Count
  104. }
  105. //如果本次出库数据大于历史出库数据 新增1条流水
  106. if count > (out_count - out_count_one) {
  107. flow := models.VmStockFlow{
  108. WarehouseOutId: warehouseOut.ID,
  109. WarehousingId: warehouse.ID,
  110. GoodId: goods.GoodId,
  111. Number: warehouse.Number,
  112. ProductDate: warehouse.ProductDate,
  113. ExpireDate: warehouse.ExpiryDate,
  114. Count: count - out_count + out_count_one,
  115. Price: warehouse.PackingPrice,
  116. Status: 1,
  117. Ctime: time.Now().Unix(),
  118. UserOrgId: orgID,
  119. Manufacturer: warehouse.Manufacturer,
  120. Dealer: warehouse.Dealer,
  121. LicenseNumber: warehouse.LicenseNumber,
  122. IsEdit: 2,
  123. Creator: warehouseOut.Creater,
  124. SystemTime: record_time,
  125. ConsumableType: 3,
  126. WarehouseOutDetailId: lastOut.ID,
  127. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  128. IsSys: 1,
  129. PatientId: patient_id,
  130. ProjectId: goods.ProjectId,
  131. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  132. StorehouseId: goods.StorehouseId,
  133. OverCount: sum_count,
  134. BuyPrice: warehouse.Price,
  135. RegisterNumber: warehouse.RegisterNumber,
  136. }
  137. if orgID == 9671 || orgID == 10265 {
  138. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  139. flow.Price = goodsInfo.PackingPrice
  140. flow.BuyPrice = goodsInfo.BuyPrice
  141. }
  142. errThre := CreateStockFlowOne(flow)
  143. if errThre != nil {
  144. return errThre
  145. }
  146. }
  147. //如果本次出库数据小于历史出库数据 新增1条退库流水
  148. if count < (out_count - out_count_one) {
  149. operation_time := time.Now().Unix()
  150. //创建退库单
  151. timeStr := time.Now().Format("2006-01-02")
  152. timeArr := strings.Split(timeStr, "-")
  153. total, _ := FindAllCancelStockTotal(orgID)
  154. total = total + 1
  155. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  156. cancelStock := models.CancelStock{
  157. OrderNumber: orderNumber,
  158. OperaTime: operation_time,
  159. OrgId: orgID,
  160. Creater: warehouseOut.Creater,
  161. Ctime: time.Now().Unix(),
  162. Status: 1,
  163. ReturnTime: record_time,
  164. Type: 1,
  165. StorehouseId: goods.StorehouseId,
  166. IsCheck: 1,
  167. }
  168. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, orgID)
  169. if msgerrkonde == gorm.ErrRecordNotFound {
  170. AddSigleCancelStock(&cancelStock)
  171. }
  172. cancel, _ := GetLastCancelStockById(orgID)
  173. manufacturer, _ := GetManufactureById(warehouse.Manufacturer)
  174. deaerler, _ := GetDealerById(warehouse.Dealer)
  175. cancelStockInfo := models.CancelStockInfo{
  176. GoodId: goods.GoodId,
  177. CancelStockId: cancel.ID,
  178. GoodTypeId: goods.GoodTypeId,
  179. Count: out_count - out_count_one - count,
  180. Price: warehouse.PackingPrice,
  181. Total: 0,
  182. ProductDate: warehouse.ProductDate,
  183. ExpiryDate: warehouse.ExpiryDate,
  184. Ctime: time.Now().Unix(),
  185. Status: 1,
  186. OrgId: orgID,
  187. OrderNumber: cancel.OrderNumber,
  188. Type: 0,
  189. Dealer: deaerler.DealerName,
  190. Manufacturer: manufacturer.ManufacturerName,
  191. Number: warehouse.Number,
  192. RegisterAccount: "",
  193. Remark: "",
  194. WarehouseInfoId: warehouse.ID,
  195. PatientId: patient_id,
  196. RecordDate: record_time,
  197. StorehouseId: goods.StorehouseId,
  198. IsCheck: 1,
  199. }
  200. if orgID == 9671 || orgID == 10265 {
  201. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  202. cancelStockInfo.Price = goodsInfo.PackingPrice
  203. }
  204. CreateCancelStockInfoOne(&cancelStockInfo)
  205. cancelInfo, _ := GetLastCancelStockInfoByGoodId(goods.GoodId)
  206. flow := models.VmStockFlow{
  207. WarehousingId: warehouse.ID,
  208. GoodId: goods.GoodId,
  209. Number: warehouse.Number,
  210. LicenseNumber: warehouse.LicenseNumber,
  211. Count: out_count - out_count_one - count,
  212. UserOrgId: orgID,
  213. PatientId: patient_id,
  214. SystemTime: record_time,
  215. ConsumableType: 7,
  216. IsSys: 1,
  217. WarehousingOrder: "",
  218. WarehouseOutId: lastOut.WarehouseOutId,
  219. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  220. IsEdit: 0,
  221. CancelStockId: cancel.ID,
  222. CancelOrderNumber: cancel.OrderNumber,
  223. Manufacturer: manufacturer.ID,
  224. Dealer: 0,
  225. Creator: warehouseOut.Creater,
  226. UpdateCreator: 0,
  227. Status: 1,
  228. Ctime: time.Now().Unix(),
  229. Mtime: 0,
  230. Price: warehouse.PackingPrice,
  231. WarehousingDetailId: warehouse.ID,
  232. WarehouseOutDetailId: lastOut.ID,
  233. CancelOutDetailId: cancelInfo.ID,
  234. ProductDate: warehouse.ProductDate,
  235. ExpireDate: warehouse.ExpiryDate,
  236. StorehouseId: goods.StorehouseId,
  237. BuyPrice: warehouse.Price,
  238. ProjectId: goods.ProjectId,
  239. OverCount: sum_count,
  240. RegisterNumber: warehouse.RegisterNumber,
  241. }
  242. if orgID == 9671 || orgID == 10265 {
  243. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  244. flow.Price = goodsInfo.PackingPrice
  245. flow.BuyPrice = goodsInfo.BuyPrice
  246. }
  247. CreateStockFlowOne(flow)
  248. }
  249. details := models.BloodAutomaticReduceDetail{
  250. WarehouseOutId: lastOut.ID,
  251. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  252. PatientId: patient_id,
  253. Ctime: time.Now().Unix(),
  254. Mtime: time.Now().Unix(),
  255. Status: 1,
  256. RecordTime: record_time,
  257. OrgId: orgID,
  258. GoodId: goods.GoodId,
  259. GoodTypeId: goods.GoodTypeId,
  260. Count: count,
  261. ProjectId: goods.ProjectId,
  262. StorehouseId: goods.StorehouseId,
  263. }
  264. ////查询该耗材已经出库的数量
  265. _, errcode := GetAutoMaticReduceDetailTwenty(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  266. if errcode == gorm.ErrRecordNotFound {
  267. errTwo := CreateAutoReduceRecord(&details)
  268. if errTwo != nil {
  269. return errTwo
  270. }
  271. } else if errcode == nil {
  272. DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  273. CreateAutoReduceRecord(&details)
  274. }
  275. return nil
  276. } else {
  277. // 当刚批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  278. warehouse.StockCount = 0
  279. warehouse.Mtime = time.Now().Unix()
  280. errThree := UpDateWarehouseInfoByStock(&warehouse)
  281. if errThree != nil {
  282. return errThree
  283. }
  284. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID)
  285. var sum_count int64
  286. for _, item := range goodList {
  287. sum_count += item.StockCount
  288. }
  289. warehouseOutInfo := &models.WarehouseOutInfo{
  290. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  291. WarehouseOutId: warehouseOut.ID,
  292. WarehouseInfotId: warehouse.ID,
  293. Status: 1,
  294. Ctime: time.Now().Unix(),
  295. Remark: warehouse.Remark,
  296. OrgId: orgID,
  297. Type: 1,
  298. Manufacturer: warehouse.Manufacturer,
  299. Dealer: warehouse.Dealer,
  300. IsSys: 1,
  301. SysRecordTime: record_time,
  302. GoodTypeId: goods.GoodTypeId,
  303. GoodId: goods.GoodId,
  304. PatientId: patient_id,
  305. Number: warehouse.Number,
  306. LicenseNumber: warehouse.LicenseNumber,
  307. Price: warehouse.PackingPrice,
  308. ExpiryDate: warehouse.ExpiryDate,
  309. ProductDate: warehouse.ProductDate,
  310. ProjectId: goods.ProjectId,
  311. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  312. StorehouseId: goods.StorehouseId,
  313. IsCheck: 1,
  314. OverCount: sum_count,
  315. RegisterNumber: warehouse.RegisterNumber,
  316. }
  317. warehouseOutInfo.Count = stock_number
  318. if orgID == 9671 || orgID == 10265 {
  319. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  320. warehouseOutInfo.Price = goodsInfo.PackingPrice
  321. }
  322. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
  323. //fmt.Println("errcodes23332322332323223", errcodes, goods.ProjectId)
  324. if errcodes == gorm.ErrRecordNotFound {
  325. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  326. if errOne != nil {
  327. return errOne
  328. }
  329. } else if errcodes == nil {
  330. goods.Count = deliver_number - stock_number
  331. //更新数量为 该批次剩余数量 + 还有未出的数量
  332. warehouseOutInfo.Count = stock_number
  333. outInfoOne, _ := GetWarehouseOutInfoIsExistTwo(goods.GoodId, patient_id, record_time, goods.ProjectId)
  334. if count != outInfoOne.Count {
  335. UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId)
  336. }
  337. }
  338. lastOut, _ := FindWarehouseOutInfoByPatientId(patient_id, record_time, goods.GoodId, orgID)
  339. //查询该该批次已经出库的数据
  340. flowGood, _ := GetStockFlowIsBatchNumber(warehouse.ID, patient_id, record_time, goods.GoodId)
  341. var out_count int64
  342. var out_count_one int64
  343. for _, item := range flowGood {
  344. out_count += item.Count
  345. }
  346. flowGoodTwo, _ := GetStockFlowIsBatchNumberThree(patient_id, record_time, goods.GoodId)
  347. for _, item := range flowGoodTwo {
  348. out_count_one += item.Count
  349. }
  350. //如果出库数量 大于 历史出库数据 新增1条流水
  351. if count > out_count-out_count_one {
  352. flow := models.VmStockFlow{
  353. WarehouseOutId: warehouseOut.ID,
  354. WarehousingId: warehouse.ID,
  355. GoodId: goods.GoodId,
  356. Number: warehouse.Number,
  357. ProductDate: warehouse.ProductDate,
  358. ExpireDate: warehouse.ExpiryDate,
  359. Count: stock_number,
  360. Price: warehouse.PackingPrice,
  361. Status: 1,
  362. Ctime: time.Now().Unix(),
  363. UserOrgId: orgID,
  364. Manufacturer: warehouse.Manufacturer,
  365. Dealer: warehouse.Dealer,
  366. LicenseNumber: warehouse.LicenseNumber,
  367. IsEdit: 2,
  368. Creator: warehouseOut.Creater,
  369. SystemTime: record_time,
  370. ConsumableType: 3,
  371. WarehouseOutDetailId: lastOut.ID,
  372. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  373. IsSys: 1,
  374. PatientId: patient_id,
  375. ProjectId: goods.ProjectId,
  376. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  377. StorehouseId: goods.StorehouseId,
  378. OverCount: sum_count,
  379. BuyPrice: warehouse.Price,
  380. RegisterNumber: warehouse.RegisterNumber,
  381. }
  382. if orgID == 9671 || orgID == 10265 {
  383. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  384. flow.Price = goodsInfo.PackingPrice
  385. flow.BuyPrice = goodsInfo.BuyPrice
  386. }
  387. errThre := CreateStockFlowOne(flow)
  388. if errThre != nil {
  389. return errThre
  390. }
  391. }
  392. //退库
  393. if count < out_count-out_count_one {
  394. operation_time := time.Now().Unix()
  395. //创建退库单
  396. timeStr := time.Now().Format("2006-01-02")
  397. timeArr := strings.Split(timeStr, "-")
  398. total, _ := FindAllCancelStockTotal(orgID)
  399. total = total + 1
  400. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  401. cancelStock := models.CancelStock{
  402. OrderNumber: orderNumber,
  403. OperaTime: operation_time,
  404. OrgId: orgID,
  405. Creater: warehouseOut.Creater,
  406. Ctime: time.Now().Unix(),
  407. Status: 1,
  408. ReturnTime: record_time,
  409. Type: 1,
  410. StorehouseId: goods.StorehouseId,
  411. IsCheck: 1,
  412. }
  413. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, orgID)
  414. if msgerrkonde == gorm.ErrRecordNotFound {
  415. AddSigleCancelStock(&cancelStock)
  416. }
  417. cancel, _ := GetLastCancelStockById(orgID)
  418. manufacturer, _ := GetManufactureById(warehouse.Manufacturer)
  419. deaerler, _ := GetDealerById(warehouse.Dealer)
  420. cancelStockInfo := models.CancelStockInfo{
  421. GoodId: goods.GoodId,
  422. CancelStockId: cancel.ID,
  423. GoodTypeId: goods.GoodTypeId,
  424. Count: out_count - out_count_one - count,
  425. Price: warehouse.PackingPrice,
  426. Total: 0,
  427. ProductDate: warehouse.ProductDate,
  428. ExpiryDate: warehouse.ExpiryDate,
  429. Ctime: time.Now().Unix(),
  430. Status: 1,
  431. OrgId: orgID,
  432. OrderNumber: cancel.OrderNumber,
  433. Type: 0,
  434. Dealer: deaerler.DealerName,
  435. Manufacturer: manufacturer.ManufacturerName,
  436. Number: warehouse.Number,
  437. RegisterAccount: "",
  438. Remark: "",
  439. WarehouseInfoId: warehouse.ID,
  440. PatientId: patient_id,
  441. RecordDate: record_time,
  442. StorehouseId: goods.StorehouseId,
  443. IsCheck: 1,
  444. }
  445. if orgID == 9671 || orgID == 10265 {
  446. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  447. cancelStockInfo.Price = goodsInfo.PackingPrice
  448. }
  449. CreateCancelStockInfoOne(&cancelStockInfo)
  450. cancelInfo, _ := GetLastCancelStockInfoByGoodId(goods.GoodId)
  451. flow := models.VmStockFlow{
  452. WarehousingId: warehouse.ID,
  453. GoodId: goods.GoodId,
  454. Number: warehouse.Number,
  455. LicenseNumber: warehouse.LicenseNumber,
  456. Count: out_count - count,
  457. UserOrgId: orgID,
  458. PatientId: patient_id,
  459. SystemTime: record_time,
  460. ConsumableType: 7,
  461. IsSys: 1,
  462. WarehousingOrder: "",
  463. WarehouseOutId: lastOut.WarehouseOutId,
  464. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  465. IsEdit: 0,
  466. CancelStockId: cancel.ID,
  467. CancelOrderNumber: cancel.OrderNumber,
  468. Manufacturer: manufacturer.ID,
  469. Dealer: 0,
  470. Creator: warehouseOut.Creater,
  471. UpdateCreator: 0,
  472. Status: 1,
  473. Ctime: time.Now().Unix(),
  474. Mtime: 0,
  475. Price: warehouse.PackingPrice,
  476. WarehousingDetailId: warehouse.ID,
  477. WarehouseOutDetailId: lastOut.ID,
  478. CancelOutDetailId: cancelInfo.ID,
  479. ProductDate: warehouse.ProductDate,
  480. ExpireDate: warehouse.ExpiryDate,
  481. StorehouseId: goods.StorehouseId,
  482. BuyPrice: warehouse.Price,
  483. ProjectId: goods.ProjectId,
  484. OverCount: sum_count,
  485. RegisterNumber: warehouse.RegisterNumber,
  486. }
  487. if orgID == 9671 || orgID == 10265 {
  488. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  489. flow.Price = goodsInfo.PackingPrice
  490. flow.BuyPrice = goodsInfo.BuyPrice
  491. }
  492. CreateStockFlowOne(flow)
  493. }
  494. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  495. goods.Count = deliver_number - stock_number
  496. ConsumablesGoodDelivery(orgID, patient_id, record_time, goods, warehouseOut, count)
  497. }
  498. return nil
  499. }
  500. func FindWarehouseOutInfoByPatientId(patient_id int64, record_time int64, good_id int64, org_id int64) (models.WarehouseOutInfo, error) {
  501. outInfo := models.WarehouseOutInfo{}
  502. err := XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and org_id = ? and status = 1", patient_id, record_time, good_id, org_id).Last(&outInfo).Error
  503. return outInfo, err
  504. }
  505. func FindWarehouseOutInfoByPatientIdTwo(patient_id int64, record_time int64, good_id int64, org_id int64, project_id int64) (models.WarehouseOutInfo, error) {
  506. outInfo := models.WarehouseOutInfo{}
  507. err := XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and org_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, org_id, project_id).Last(&outInfo).Error
  508. return outInfo, err
  509. }
  510. func UpdateDrugStockCount(drug_id int64, org_id int64, storehouse_id int64, flush_count int64) error {
  511. drug := models.XtDrugStockCount{}
  512. ut := XTWriteDB().Begin()
  513. err := ut.Model(&drug).Where("drug_id = ? and user_org_id = ? and storehouse_id = ? and status = 1", drug_id, org_id, storehouse_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  514. if err != nil {
  515. ut.Rollback()
  516. return err
  517. }
  518. ut.Commit()
  519. return err
  520. }
  521. func AddDrugCount(drug_id int64, org_id int64, storehouse_id int64, out_count int64) error {
  522. ut := XTWriteDB().Begin()
  523. err := ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id = ?", storehouse_id, org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", out_count)).Error
  524. if err != nil {
  525. ut.Rollback()
  526. return err
  527. }
  528. err = ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id = ?", storehouse_id, org_id, drug_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count + ?", out_count)).Error
  529. if err != nil {
  530. ut.Rollback()
  531. return err
  532. }
  533. ut.Commit()
  534. return err
  535. }
  536. func ReduceDrugCount(drug_id int64, org_id int64, storehouse_id int64, out_count int64) error {
  537. ut := XTWriteDB().Begin()
  538. err := ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id = ?", storehouse_id, org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", out_count)).Error
  539. if err != nil {
  540. ut.Rollback()
  541. return err
  542. }
  543. err = ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id = ?", storehouse_id, org_id, drug_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count - ?", out_count)).Error
  544. if err != nil {
  545. ut.Rollback()
  546. return err
  547. }
  548. ut.Commit()
  549. return err
  550. }
  551. func ReduceDrugCountTwo(drug_id int64, org_id int64, storehouse_id int64, out_count int64) error {
  552. ut := XTWriteDB().Begin()
  553. err := ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id = ?", storehouse_id, org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", out_count)).Error
  554. if err != nil {
  555. ut.Rollback()
  556. return err
  557. }
  558. ut.Commit()
  559. return err
  560. }
  561. // 药品自动出库 递归方式
  562. func BloodDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DoctorAdvice) (err error) {
  563. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  564. var deliver_number int64 = 0
  565. var stock_number int64 = 0
  566. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  567. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  568. var drug_price float64
  569. if advice.PrescribingNumberUnit == drup.MaxUnit {
  570. deliver_number = count * drup.MinNumber
  571. } else {
  572. deliver_number = count
  573. }
  574. if advice.PrescribingNumberUnit == drup.MaxUnit {
  575. drug_price = drup.RetailPrice
  576. }
  577. fmt.Println(drug_price)
  578. if advice.PrescribingNumberUnit == drup.MinUnit {
  579. drug_price = drup.MinPrice
  580. }
  581. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  582. drug_price = drup.RetailPrice
  583. }
  584. // 根据先进先出原则,查询最先入库的批次,进行出库
  585. // 如果没有对应的库存,则报错
  586. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  587. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  588. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  589. var stockMax int64
  590. var stockMin int64
  591. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  592. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  593. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  594. UpdateMinNumber(lastWarehouse.ID, stockMin)
  595. }
  596. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  597. if err != nil {
  598. return err
  599. }
  600. // 将该批次的剩余库存数量转换为拆零数量
  601. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  602. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  603. if stock_number >= deliver_number {
  604. var maxNumber int64 = 0
  605. var minNumber int64 = 0
  606. maxNumber = deliver_number / drup.MinNumber
  607. minNumber = deliver_number % drup.MinNumber
  608. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  609. minNumber = maxNumber
  610. }
  611. if drup.MaxUnit != drup.MinUnit {
  612. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  613. return errors.New("库存数量不足")
  614. }
  615. }
  616. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  617. if warehouse.StockMaxNumber < 0 {
  618. warehouse.StockMaxNumber = 0
  619. }
  620. if warehouse.StockMinNumber < 0 {
  621. warehouse.StockMinNumber = 0
  622. }
  623. warehouse.Mtime = time.Now().Unix()
  624. if warehouse.StockMinNumber < minNumber {
  625. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  626. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  627. } else {
  628. if minNumber > 0 {
  629. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  630. }
  631. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  632. if warehouse.StockMinNumber > 0 {
  633. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  634. }
  635. }
  636. }
  637. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  638. if (warehouse.StockMinNumber - deliver_number) > 0 {
  639. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  640. }
  641. }
  642. if drup.MaxUnit != drup.MinUnit {
  643. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  644. return errors.New("库存数量不足")
  645. }
  646. }
  647. if warehouse.StockMinNumber <= 0 {
  648. warehouse.StockMinNumber = 0
  649. }
  650. //扣减库存数据
  651. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  652. if errThree != nil {
  653. return errThree
  654. }
  655. //查询剩余库存
  656. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  657. var sum_count int64
  658. for _, its := range stockInfo {
  659. if its.MaxUnit == drup.MaxUnit {
  660. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  661. }
  662. sum_count += its.StockMaxNumber + its.StockMinNumber
  663. }
  664. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  665. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  666. WarehouseOutId: warehouseout.ID,
  667. Status: 1,
  668. Ctime: time.Now().Unix(),
  669. Remark: warehouse.Remark,
  670. OrgId: orgID,
  671. Type: 1,
  672. Manufacturer: warehouse.Manufacturer,
  673. Dealer: warehouse.Dealer,
  674. IsSys: 1,
  675. SysRecordTime: advice.RecordDate,
  676. DrugId: advice.DrugId,
  677. Number: warehouse.Number,
  678. BatchNumber: warehouse.BatchNumber,
  679. Price: warehouse.RetailPrice,
  680. CountUnit: drup.MinUnit,
  681. RetailPrice: warehouse.RetailPrice,
  682. ProductDate: warehouse.ProductDate,
  683. ExpiryDate: warehouse.ExpiryDate,
  684. PatientId: advice.PatientId,
  685. Count: deliver_number, //出最小单位
  686. WarehouseInfoId: warehouse.ID,
  687. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  688. StorehouseId: storeConfig.DrugStorehouseOut,
  689. IsCheck: 1,
  690. OverCount: sum_count,
  691. AdviceId: advice.ID,
  692. }
  693. //查询今日该药品该患者是否有出库数据
  694. lastDrugOutInfo, errThreeCode := GetNewDrugWarehouseOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId)
  695. if errThreeCode == gorm.ErrRecordNotFound {
  696. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  697. if errOne != nil {
  698. return errOne
  699. }
  700. }
  701. if errThreeCode == nil {
  702. errThree := UpdateSigleDrugWarehouseOutInfo(lastDrugOutInfo.ID, warehouseOutInfo)
  703. if errThree != nil {
  704. return errThree
  705. }
  706. }
  707. var out_count int64
  708. var cancel_count int64
  709. //查询出库数据
  710. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  711. //查询退库数据
  712. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  713. for _, item := range infoCountList {
  714. out_count += item.Count
  715. }
  716. for _, item := range cancelInfoList {
  717. cancel_count += item.Count
  718. }
  719. //如果本次出库数据大于历史出库数据 新增1条流水
  720. if deliver_number > (out_count - cancel_count) {
  721. drugflow := models.DrugFlow{
  722. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  723. WarehouseOutId: warehouseout.ID,
  724. DrugId: advice.DrugId,
  725. Number: warehouse.Number,
  726. ProductDate: warehouse.ProductDate,
  727. ExpireDate: warehouse.ExpiryDate,
  728. Count: deliver_number - (out_count - cancel_count), //按最小单位计算,
  729. Price: warehouse.RetailPrice,
  730. Status: 1,
  731. Ctime: time.Now().Unix(),
  732. UserOrgId: orgID,
  733. Manufacturer: warehouse.Manufacturer,
  734. Dealer: warehouse.Dealer,
  735. BatchNumber: warehouse.BatchNumber,
  736. MaxUnit: drup.MinUnit,
  737. ConsumableType: 3,
  738. IsEdit: 1,
  739. Creator: advice.ExecutionStaff,
  740. IsSys: 1,
  741. PatientId: advice.PatientId,
  742. SystemTime: advice.AdviceDate,
  743. WarehousingDetailId: warehouse.ID,
  744. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  745. StorehouseId: storeConfig.DrugStorehouseOut,
  746. WarehouseOutDetailId: lastDrugOutInfo.ID,
  747. OverCount: sum_count,
  748. AdviceId: advice.ID,
  749. LastPrice: warehouse.Price,
  750. }
  751. CreateDrugFlowOne(drugflow)
  752. //出库数量相加
  753. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  754. }
  755. //如果本次出库数据小于历史出库数据 新增1条退库流水
  756. if deliver_number < (out_count - cancel_count) {
  757. operation_time := time.Now().Unix()
  758. ctime := time.Now().Unix()
  759. timeStr := time.Now().Format("2006-01-02")
  760. timeArr := strings.Split(timeStr, "-")
  761. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  762. total = total + 1
  763. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  764. cancelStock := models.DrugCancelStock{
  765. OrderNumber: orderNumber,
  766. OperaTime: operation_time,
  767. OrgId: advice.UserOrgId,
  768. Creater: advice.ExecutionStaff,
  769. Ctime: ctime,
  770. Status: 1,
  771. ReturnTime: advice.AdviceDate,
  772. Dealer: 0,
  773. Manufacturer: 0,
  774. Type: 1,
  775. StorehouseId: storeConfig.DrugStorehouseOut,
  776. IsCheck: 1,
  777. }
  778. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  779. if errCode == gorm.ErrRecordNotFound {
  780. AddSigleDrugCancelStock(&cancelStock)
  781. }
  782. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  783. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  784. dealer, _ := GetDealerById(warehouse.Dealer)
  785. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  786. cancelStockInfo := models.DrugCancelStockInfo{
  787. OrderNumber: cancelInfo.OrderNumber,
  788. CancelStockId: lastDrugCancelStock.ID,
  789. DrugId: advice.DrugId,
  790. Count: (out_count - cancel_count) - deliver_number,
  791. Status: 1,
  792. Ctime: ctime,
  793. OrgId: advice.UserOrgId,
  794. Type: 1,
  795. Manufacturer: manufactureName.ManufacturerName,
  796. Dealer: dealer.DealerName,
  797. Total: 0,
  798. RetailPrice: warehouse.RetailPrice,
  799. RetailTotalPrice: warehouse.RetailTotalPrice,
  800. Price: warehouse.Price,
  801. RegisterAccount: "",
  802. Remark: warehouse.Remark,
  803. BatchNumber: warehouse.BatchNumber,
  804. MaxUnit: drup.MinUnit,
  805. ProductDate: warehouse.ProductDate,
  806. ExpiryDate: warehouse.ExpiryDate,
  807. BatchNumberId: warehouse.ID,
  808. StorehouseId: storeConfig.DrugStorehouseOut,
  809. IsCheck: 1,
  810. }
  811. CreatedCancelStock(cancelStockInfo)
  812. flow := models.DrugFlow{
  813. WarehousingId: warehouse.ID,
  814. DrugId: warehouse.DrugId,
  815. Number: "",
  816. BatchNumber: warehouse.BatchNumber,
  817. Count: (out_count - cancel_count) - deliver_number,
  818. UserOrgId: advice.UserOrgId,
  819. PatientId: advice.PatientId,
  820. SystemTime: advice.AdviceDate,
  821. ConsumableType: 7,
  822. IsSys: 1,
  823. WarehousingOrder: "",
  824. WarehouseOutId: 0,
  825. WarehouseOutOrderNumber: "",
  826. IsEdit: 0,
  827. CancelStockId: cancelStock.ID,
  828. CancelOrderNumber: cancelStock.OrderNumber,
  829. Manufacturer: warehouse.Manufacturer,
  830. Dealer: warehouse.Dealer,
  831. Creator: advice.ExecutionStaff,
  832. UpdateCreator: 0,
  833. Status: 1,
  834. Ctime: time.Now().Unix(),
  835. Mtime: 0,
  836. Price: warehouse.RetailPrice,
  837. WarehousingDetailId: warehouse.ID,
  838. WarehouseOutDetailId: 0,
  839. CancelOutDetailId: lastDrugCancelStock.ID,
  840. ExpireDate: warehouse.ExpiryDate,
  841. ProductDate: warehouse.ProductDate,
  842. MaxUnit: drup.MinUnit,
  843. MinUnit: "",
  844. AdviceId: advice.ID,
  845. StorehouseId: storeConfig.DrugStorehouseOut,
  846. LastPrice: warehouse.Price,
  847. }
  848. CreateDrugFlowOne(flow)
  849. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  850. //出库数量减
  851. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  852. }
  853. //查询是否存在数据
  854. details := &models.DrugAutomaticReduceDetail{
  855. WarehouseOutId: warehouseout.ID,
  856. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  857. PatientId: advice.PatientId,
  858. Ctime: time.Now().Unix(),
  859. Mtime: time.Now().Unix(),
  860. Status: 1,
  861. RecordTime: advice.RecordDate,
  862. OrgId: orgID,
  863. DrugId: advice.DrugId,
  864. Count: deliver_number,
  865. CountUnit: drup.MinUnit,
  866. WarehouseInfoId: warehouse.ID,
  867. StorehouseId: storeConfig.DrugStorehouseOut,
  868. AdviceId: advice.ID,
  869. }
  870. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  871. if errTwo != nil {
  872. return errTwo
  873. }
  874. return nil
  875. } else {
  876. info := models.XtDrugWarehouseInfo{
  877. ID: warehouse.ID,
  878. WarehousingId: warehouse.WarehousingId,
  879. DrugId: warehouse.DrugId,
  880. Number: warehouse.Number,
  881. ProductDate: warehouse.ProductDate,
  882. ExpiryDate: warehouse.ExpiryDate,
  883. WarehousingCount: warehouse.WarehousingCount,
  884. Price: warehouse.RetailPrice,
  885. TotalPrice: warehouse.TotalPrice,
  886. Dealer: warehouse.Dealer,
  887. Manufacturer: warehouse.Manufacturer,
  888. Remark: warehouse.Remark,
  889. Ctime: warehouse.Ctime,
  890. Mtime: warehouse.Mtime,
  891. Status: 1,
  892. OrgId: warehouse.OrgId,
  893. IsReturn: warehouse.IsReturn,
  894. WarehousingOrder: warehouse.WarehousingOrder,
  895. Type: warehouse.Type,
  896. RetailPrice: warehouse.RetailPrice,
  897. RetailTotalPrice: warehouse.RetailPrice,
  898. StockMaxNumber: 0,
  899. StockMinNumber: 0,
  900. BatchNumber: warehouse.BatchNumber,
  901. MaxUnit: warehouse.MaxUnit,
  902. WarehousingInfoId: warehouse.WarehousingInfoId,
  903. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  904. StorehouseId: storeConfig.DrugStorehouseOut,
  905. IsCheck: 1,
  906. }
  907. //扣减库存
  908. errThree := UpDateDrugWarehouseInfoByStock(&info)
  909. if errThree != nil {
  910. return errThree
  911. }
  912. //查询剩余库存
  913. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  914. var sum_count int64
  915. for _, its := range stockInfo {
  916. if its.MaxUnit == drup.MaxUnit {
  917. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  918. }
  919. sum_count += its.StockMaxNumber + its.StockMinNumber
  920. }
  921. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  922. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  923. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  924. WarehouseOutId: warehouseout.ID,
  925. Status: 1,
  926. Ctime: time.Now().Unix(),
  927. Remark: warehouse.Remark,
  928. OrgId: orgID,
  929. Type: 1,
  930. Manufacturer: warehouse.Manufacturer,
  931. Dealer: warehouse.Dealer,
  932. IsSys: 1,
  933. SysRecordTime: advice.RecordDate,
  934. DrugId: advice.DrugId,
  935. Number: warehouse.Number,
  936. BatchNumber: warehouse.BatchNumber,
  937. Price: warehouse.RetailPrice,
  938. CountUnit: drup.MinUnit,
  939. ProductDate: warehouse.ProductDate,
  940. ExpiryDate: warehouse.ExpiryDate,
  941. PatientId: advice.PatientId,
  942. Count: stock_number,
  943. WarehouseInfoId: warehouse.ID,
  944. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  945. StorehouseId: storeConfig.DrugStorehouseOut,
  946. IsCheck: 1,
  947. OverCount: sum_count,
  948. AdviceId: advice.ID,
  949. }
  950. //查询是否存在出库数据
  951. _, errCode := GetSigleDrugWarehouseOutInfoSix(advice.PatientId, advice.AdviceDate, orgID, advice.ID)
  952. if errCode == gorm.ErrRecordNotFound {
  953. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  954. if errOne != nil {
  955. return errOne
  956. }
  957. }
  958. if errCode == nil {
  959. UpdateSingleDrugWarehouseOutInfo(advice.PatientId, advice.AdviceDate, orgID, warehouseOutInfo)
  960. }
  961. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  962. details := &models.DrugAutomaticReduceDetail{
  963. WarehouseOutId: warehouseout.ID,
  964. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  965. PatientId: advice.PatientId,
  966. Ctime: time.Now().Unix(),
  967. Mtime: time.Now().Unix(),
  968. Status: 1,
  969. RecordTime: advice.RecordDate,
  970. OrgId: orgID,
  971. DrugId: advice.DrugId,
  972. Count: stock_number,
  973. CountUnit: drup.MinUnit,
  974. WarehouseInfoId: warehouse.ID,
  975. StorehouseId: storeConfig.DrugStorehouseOut,
  976. AdviceId: advice.ID,
  977. }
  978. _, errcodeThree := FindDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId)
  979. if errcodeThree == gorm.ErrRecordNotFound {
  980. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  981. if errTwo != nil {
  982. return errTwo
  983. }
  984. }
  985. if errcodeThree == nil {
  986. UpdateDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId, details)
  987. }
  988. var out_count int64
  989. var cancel_count int64
  990. //查询出库数据
  991. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  992. //查询退库数据
  993. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  994. for _, item := range infoCountList {
  995. out_count += item.Count
  996. }
  997. for _, item := range cancelInfoList {
  998. cancel_count += item.Count
  999. }
  1000. //如果本次出库数据大于历史出库数据 新增1条流水
  1001. if deliver_number > (out_count - cancel_count) {
  1002. drugflow := models.DrugFlow{
  1003. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1004. WarehouseOutId: warehouseout.ID,
  1005. DrugId: advice.DrugId,
  1006. Number: warehouse.Number,
  1007. ProductDate: warehouse.ProductDate,
  1008. ExpireDate: warehouse.ExpiryDate,
  1009. Count: stock_number,
  1010. Price: warehouse.RetailPrice,
  1011. Status: 1,
  1012. Ctime: time.Now().Unix(),
  1013. UserOrgId: orgID,
  1014. Manufacturer: warehouse.Manufacturer,
  1015. Dealer: warehouse.Dealer,
  1016. BatchNumber: warehouse.BatchNumber,
  1017. MaxUnit: drup.MinUnit,
  1018. ConsumableType: 3,
  1019. IsEdit: 1,
  1020. Creator: advice.ExecutionStaff,
  1021. IsSys: 1,
  1022. PatientId: advice.PatientId,
  1023. WarehousingDetailId: warehouse.ID,
  1024. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1025. StorehouseId: storeConfig.DrugStorehouseOut,
  1026. WarehouseOutDetailId: lastDrugOutInfo.ID,
  1027. OverCount: sum_count,
  1028. AdviceId: advice.ID,
  1029. }
  1030. CreateDrugFlowOne(drugflow)
  1031. //出库数量相加
  1032. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  1033. }
  1034. if deliver_number < (out_count - cancel_count) {
  1035. operation_time := time.Now().Unix()
  1036. ctime := time.Now().Unix()
  1037. timeStr := time.Now().Format("2006-01-02")
  1038. timeArr := strings.Split(timeStr, "-")
  1039. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  1040. total = total + 1
  1041. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  1042. cancelStock := models.DrugCancelStock{
  1043. OrderNumber: orderNumber,
  1044. OperaTime: operation_time,
  1045. OrgId: advice.UserOrgId,
  1046. Creater: advice.ExecutionStaff,
  1047. Ctime: ctime,
  1048. Status: 1,
  1049. ReturnTime: advice.AdviceDate,
  1050. Dealer: 0,
  1051. Manufacturer: 0,
  1052. Type: 1,
  1053. StorehouseId: storeConfig.DrugStorehouseOut,
  1054. IsCheck: 1,
  1055. }
  1056. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  1057. if errCode == gorm.ErrRecordNotFound {
  1058. AddSigleDrugCancelStock(&cancelStock)
  1059. }
  1060. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  1061. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  1062. dealer, _ := GetDealerById(warehouse.Dealer)
  1063. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  1064. cancelStockInfo := models.DrugCancelStockInfo{
  1065. OrderNumber: cancelInfo.OrderNumber,
  1066. CancelStockId: lastDrugCancelStock.ID,
  1067. DrugId: advice.DrugId,
  1068. Count: (out_count - cancel_count) - deliver_number,
  1069. Status: 1,
  1070. Ctime: ctime,
  1071. OrgId: advice.UserOrgId,
  1072. Type: 1,
  1073. Manufacturer: manufactureName.ManufacturerName,
  1074. Dealer: dealer.DealerName,
  1075. Total: 0,
  1076. RetailPrice: warehouse.RetailPrice,
  1077. RetailTotalPrice: warehouse.RetailTotalPrice,
  1078. Price: warehouse.Price,
  1079. RegisterAccount: "",
  1080. Remark: warehouse.Remark,
  1081. BatchNumber: warehouse.BatchNumber,
  1082. MaxUnit: drup.MinUnit,
  1083. ProductDate: warehouse.ProductDate,
  1084. ExpiryDate: warehouse.ExpiryDate,
  1085. BatchNumberId: warehouse.ID,
  1086. StorehouseId: storeConfig.DrugStorehouseOut,
  1087. IsCheck: 1,
  1088. }
  1089. CreatedCancelStock(cancelStockInfo)
  1090. flow := models.DrugFlow{
  1091. WarehousingId: warehouse.ID,
  1092. DrugId: warehouse.DrugId,
  1093. Number: "",
  1094. BatchNumber: warehouse.BatchNumber,
  1095. Count: (out_count - cancel_count) - deliver_number,
  1096. UserOrgId: advice.UserOrgId,
  1097. PatientId: advice.PatientId,
  1098. SystemTime: advice.AdviceDate,
  1099. ConsumableType: 7,
  1100. IsSys: 1,
  1101. WarehousingOrder: "",
  1102. WarehouseOutId: 0,
  1103. WarehouseOutOrderNumber: "",
  1104. IsEdit: 0,
  1105. CancelStockId: cancelStock.ID,
  1106. CancelOrderNumber: cancelStock.OrderNumber,
  1107. Manufacturer: warehouse.Manufacturer,
  1108. Dealer: warehouse.Dealer,
  1109. Creator: advice.ExecutionStaff,
  1110. UpdateCreator: 0,
  1111. Status: 1,
  1112. Ctime: time.Now().Unix(),
  1113. Mtime: 0,
  1114. Price: warehouse.Price,
  1115. WarehousingDetailId: warehouse.ID,
  1116. WarehouseOutDetailId: 0,
  1117. CancelOutDetailId: lastDrugCancelStock.ID,
  1118. ExpireDate: warehouse.ExpiryDate,
  1119. ProductDate: warehouse.ProductDate,
  1120. MaxUnit: drup.MinUnit,
  1121. MinUnit: "",
  1122. AdviceId: advice.ID,
  1123. StorehouseId: storeConfig.DrugStorehouseOut,
  1124. }
  1125. CreateDrugFlowOne(flow)
  1126. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  1127. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  1128. }
  1129. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1130. prescribingNumber_two_temp := deliver_number - stock_number
  1131. overPlusNumber := float64(prescribingNumber_two_temp)
  1132. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  1133. advice.PrescribingNumberUnit = drup.MinUnit
  1134. BloodDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  1135. }
  1136. return
  1137. }
  1138. // 药品自动出库 递归方式
  1139. func BloodHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  1140. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  1141. var deliver_number int64 = 0
  1142. var stock_number int64 = 0
  1143. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  1144. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1145. var drug_price float64
  1146. if advice.PrescribingNumberUnit == drup.MaxUnit {
  1147. deliver_number = count * drup.MinNumber
  1148. } else {
  1149. deliver_number = count
  1150. }
  1151. if advice.PrescribingNumberUnit == drup.MaxUnit {
  1152. drug_price = drup.RetailPrice
  1153. }
  1154. fmt.Println(drug_price)
  1155. if advice.PrescribingNumberUnit == drup.MinUnit {
  1156. drug_price = drup.MinPrice
  1157. }
  1158. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  1159. drug_price = drup.RetailPrice
  1160. }
  1161. // 根据先进先出原则,查询最先入库的批次,进行出库
  1162. // 如果没有对应的库存,则报错
  1163. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  1164. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1165. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  1166. var stockMax int64
  1167. var stockMin int64
  1168. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  1169. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  1170. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  1171. UpdateMinNumber(lastWarehouse.ID, stockMin)
  1172. }
  1173. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1174. if err != nil {
  1175. return err
  1176. }
  1177. // 将该批次的剩余库存数量转换为拆零数量
  1178. // 将该批次的剩余库存数量转换为拆零数量
  1179. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1180. //fmt.Println("进来1")
  1181. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  1182. //fmt.Println("剩余库存1", stock_number)
  1183. }
  1184. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  1185. //fmt.Println("进来2")
  1186. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  1187. //fmt.Println("剩余库存2", stock_number)
  1188. }
  1189. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  1190. //fmt.Println("进来3")
  1191. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  1192. //fmt.Println("剩余库存3", stock_number)
  1193. }
  1194. //fmt.Println("剩余库存23333333333333333333333333333wo", stock_number)
  1195. //fmt.Println("出库数量2333333333333333333333333333333333333wo", deliver_number)
  1196. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1197. if stock_number >= deliver_number {
  1198. var maxNumber int64 = 0
  1199. var minNumber int64 = 0
  1200. maxNumber = deliver_number / drup.MinNumber
  1201. minNumber = deliver_number % drup.MinNumber
  1202. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  1203. minNumber = maxNumber
  1204. }
  1205. if drup.MaxUnit != drup.MinUnit {
  1206. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  1207. return errors.New("库存数量不足")
  1208. }
  1209. }
  1210. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1211. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  1212. }
  1213. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1214. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  1215. }
  1216. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  1217. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  1218. }
  1219. if warehouse.StockMaxNumber < 0 {
  1220. warehouse.StockMaxNumber = 0
  1221. }
  1222. if warehouse.StockMinNumber < 0 {
  1223. warehouse.StockMinNumber = 0
  1224. }
  1225. warehouse.Mtime = time.Now().Unix()
  1226. fmt.Println("minnU目标而332322323323233223323223我", minNumber)
  1227. fmt.Println("max_number232232332232322wo", maxNumber)
  1228. fmt.Println(" warehouse.StockMinNumber", warehouse.StockMinNumber)
  1229. if warehouse.StockMinNumber < minNumber {
  1230. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  1231. if warehouse.MaxUnit != warehouse.MinUnit {
  1232. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  1233. }
  1234. } else {
  1235. if minNumber > 0 {
  1236. if (warehouse.StockMinNumber - minNumber) >= 0 {
  1237. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  1238. }
  1239. }
  1240. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  1241. if warehouse.StockMinNumber > 0 {
  1242. if warehouse.StockMinNumber-deliver_number >= 0 {
  1243. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  1244. }
  1245. }
  1246. }
  1247. }
  1248. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  1249. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  1250. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  1251. }
  1252. }
  1253. if drup.MaxUnit != drup.MinUnit {
  1254. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  1255. return errors.New("库存数量不足")
  1256. }
  1257. }
  1258. if warehouse.StockMinNumber <= 0 {
  1259. warehouse.StockMinNumber = 0
  1260. }
  1261. //扣减库存数据
  1262. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  1263. if errThree != nil {
  1264. return errThree
  1265. }
  1266. //查询剩余库存
  1267. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  1268. var sum_count int64
  1269. for _, its := range stockInfo {
  1270. if its.MaxUnit == drup.MaxUnit {
  1271. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  1272. }
  1273. sum_count += its.StockMaxNumber + its.StockMinNumber
  1274. }
  1275. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1276. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1277. WarehouseOutId: warehouseout.ID,
  1278. Status: 1,
  1279. Ctime: time.Now().Unix(),
  1280. Remark: warehouse.Remark,
  1281. OrgId: orgID,
  1282. Type: 1,
  1283. Manufacturer: warehouse.Manufacturer,
  1284. Dealer: warehouse.Dealer,
  1285. IsSys: 1,
  1286. SysRecordTime: advice.RecordDate,
  1287. DrugId: advice.DrugId,
  1288. Number: warehouse.Number,
  1289. BatchNumber: warehouse.BatchNumber,
  1290. Price: warehouse.RetailPrice,
  1291. CountUnit: drup.MinUnit,
  1292. RetailPrice: warehouse.RetailPrice,
  1293. ProductDate: warehouse.ProductDate,
  1294. ExpiryDate: warehouse.ExpiryDate,
  1295. PatientId: advice.PatientId,
  1296. Count: deliver_number, //出最小单位
  1297. WarehouseInfoId: warehouse.ID,
  1298. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1299. StorehouseId: storeConfig.DrugStorehouseOut,
  1300. IsCheck: 1,
  1301. OverCount: sum_count,
  1302. AdviceId: advice.ID,
  1303. }
  1304. //查询今日该药品该患者是否有出库数据
  1305. lastDrugOutInfo, _ := GetNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID)
  1306. fmt.Println("lastDrugOutInfowwowowowoowowowowowowowowo", lastDrugOutInfo.ID)
  1307. if lastDrugOutInfo.ID == 0 {
  1308. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1309. if errOne != nil {
  1310. return errOne
  1311. }
  1312. }
  1313. if lastDrugOutInfo.ID > 0 {
  1314. var his_count int64
  1315. if lastDrugOutInfo.CountUnit == drup.MaxUnit {
  1316. his_count = lastDrugOutInfo.Count * drup.MinNumber
  1317. }
  1318. if lastDrugOutInfo.CountUnit == drup.MinUnit {
  1319. his_count = lastDrugOutInfo.Count
  1320. }
  1321. if deliver_number != his_count {
  1322. errThree := UpdateSigleDrugWarehouseOutInfo(lastDrugOutInfo.ID, warehouseOutInfo)
  1323. if errThree != nil {
  1324. return errThree
  1325. }
  1326. }
  1327. }
  1328. var out_count int64
  1329. var cancel_count int64
  1330. //查询出库数据
  1331. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  1332. //查询退库数据
  1333. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  1334. for _, item := range infoCountList {
  1335. out_count += item.Count
  1336. }
  1337. for _, item := range cancelInfoList {
  1338. cancel_count += item.Count
  1339. }
  1340. fmt.Println("deliver_number", deliver_number)
  1341. fmt.Println("out_count", out_count)
  1342. fmt.Println("cancel_count", cancel_count)
  1343. //如果本次出库数据大于历史出库数据 新增1条流水
  1344. if deliver_number > (out_count - cancel_count) {
  1345. drugflow := models.DrugFlow{
  1346. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1347. WarehouseOutId: warehouseout.ID,
  1348. DrugId: advice.DrugId,
  1349. Number: warehouse.Number,
  1350. ProductDate: warehouse.ProductDate,
  1351. ExpireDate: warehouse.ExpiryDate,
  1352. Count: deliver_number - (out_count - cancel_count), //按最小单位计算,
  1353. Price: warehouse.RetailPrice,
  1354. Status: 1,
  1355. Ctime: time.Now().Unix(),
  1356. UserOrgId: orgID,
  1357. Manufacturer: warehouse.Manufacturer,
  1358. Dealer: warehouse.Dealer,
  1359. BatchNumber: warehouse.BatchNumber,
  1360. MaxUnit: drup.MinUnit,
  1361. ConsumableType: 3,
  1362. IsEdit: 1,
  1363. Creator: advice.ExecutionStaff,
  1364. IsSys: 1,
  1365. PatientId: advice.PatientId,
  1366. SystemTime: advice.AdviceDate,
  1367. WarehousingDetailId: warehouse.ID,
  1368. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1369. StorehouseId: storeConfig.DrugStorehouseOut,
  1370. WarehouseOutDetailId: lastDrugOutInfo.ID,
  1371. OverCount: sum_count,
  1372. AdviceId: advice.ID,
  1373. LastPrice: warehouse.Price,
  1374. }
  1375. CreateDrugFlowOne(drugflow)
  1376. //出库数量相加
  1377. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  1378. }
  1379. //如果本次出库数据小于历史出库数据 新增1条退库流水
  1380. if deliver_number < (out_count - cancel_count) {
  1381. operation_time := time.Now().Unix()
  1382. ctime := time.Now().Unix()
  1383. timeStr := time.Now().Format("2006-01-02")
  1384. timeArr := strings.Split(timeStr, "-")
  1385. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  1386. total = total + 1
  1387. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  1388. cancelStock := models.DrugCancelStock{
  1389. OrderNumber: orderNumber,
  1390. OperaTime: operation_time,
  1391. OrgId: advice.UserOrgId,
  1392. Creater: advice.ExecutionStaff,
  1393. Ctime: ctime,
  1394. Status: 1,
  1395. ReturnTime: advice.AdviceDate,
  1396. Dealer: 0,
  1397. Manufacturer: 0,
  1398. Type: 1,
  1399. StorehouseId: storeConfig.DrugStorehouseOut,
  1400. IsCheck: 1,
  1401. }
  1402. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  1403. if errCode == gorm.ErrRecordNotFound {
  1404. AddSigleDrugCancelStock(&cancelStock)
  1405. }
  1406. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  1407. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  1408. dealer, _ := GetDealerById(warehouse.Dealer)
  1409. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  1410. cancelStockInfo := models.DrugCancelStockInfo{
  1411. OrderNumber: cancelInfo.OrderNumber,
  1412. CancelStockId: lastDrugCancelStock.ID,
  1413. DrugId: advice.DrugId,
  1414. Count: (out_count - cancel_count) - deliver_number,
  1415. Status: 1,
  1416. Ctime: ctime,
  1417. OrgId: advice.UserOrgId,
  1418. Type: 1,
  1419. Manufacturer: manufactureName.ManufacturerName,
  1420. Dealer: dealer.DealerName,
  1421. Total: 0,
  1422. RetailPrice: warehouse.RetailPrice,
  1423. RetailTotalPrice: warehouse.RetailTotalPrice,
  1424. Price: warehouse.Price,
  1425. RegisterAccount: "",
  1426. Remark: warehouse.Remark,
  1427. BatchNumber: warehouse.BatchNumber,
  1428. MaxUnit: drup.MinUnit,
  1429. ProductDate: warehouse.ProductDate,
  1430. ExpiryDate: warehouse.ExpiryDate,
  1431. BatchNumberId: warehouse.ID,
  1432. StorehouseId: storeConfig.DrugStorehouseOut,
  1433. IsCheck: 1,
  1434. }
  1435. CreatedCancelStock(cancelStockInfo)
  1436. flow := models.DrugFlow{
  1437. WarehousingId: warehouse.ID,
  1438. DrugId: warehouse.DrugId,
  1439. Number: "",
  1440. BatchNumber: warehouse.BatchNumber,
  1441. Count: out_count - cancel_count - deliver_number,
  1442. UserOrgId: advice.UserOrgId,
  1443. PatientId: advice.PatientId,
  1444. SystemTime: advice.AdviceDate,
  1445. ConsumableType: 7,
  1446. IsSys: 0,
  1447. WarehousingOrder: "",
  1448. WarehouseOutId: 0,
  1449. WarehouseOutOrderNumber: "",
  1450. IsEdit: 0,
  1451. CancelStockId: cancelStock.ID,
  1452. CancelOrderNumber: cancelStock.OrderNumber,
  1453. Manufacturer: warehouse.Manufacturer,
  1454. Dealer: warehouse.Dealer,
  1455. Creator: advice.ExecutionStaff,
  1456. UpdateCreator: 0,
  1457. Status: 1,
  1458. Ctime: time.Now().Unix(),
  1459. Mtime: 0,
  1460. Price: warehouse.RetailPrice,
  1461. WarehousingDetailId: warehouse.ID,
  1462. WarehouseOutDetailId: 0,
  1463. CancelOutDetailId: lastDrugCancelStock.ID,
  1464. ExpireDate: warehouse.ExpiryDate,
  1465. ProductDate: warehouse.ProductDate,
  1466. MaxUnit: drup.MinUnit,
  1467. MinUnit: "",
  1468. AdviceId: advice.ID,
  1469. StorehouseId: storeConfig.DrugStorehouseOut,
  1470. LastPrice: warehouse.Price,
  1471. OverCount: sum_count,
  1472. }
  1473. CreateDrugFlowOne(flow)
  1474. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  1475. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  1476. }
  1477. //查询是否存在数据
  1478. details := &models.DrugAutomaticReduceDetail{
  1479. WarehouseOutId: warehouseout.ID,
  1480. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1481. PatientId: advice.PatientId,
  1482. Ctime: time.Now().Unix(),
  1483. Mtime: time.Now().Unix(),
  1484. Status: 1,
  1485. RecordTime: advice.RecordDate,
  1486. OrgId: orgID,
  1487. DrugId: advice.DrugId,
  1488. Count: deliver_number,
  1489. CountUnit: drup.MinUnit,
  1490. WarehouseInfoId: warehouse.ID,
  1491. StorehouseId: storeConfig.DrugStorehouseOut,
  1492. AdviceId: advice.ID,
  1493. }
  1494. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  1495. if errTwo != nil {
  1496. return errTwo
  1497. }
  1498. return nil
  1499. } else {
  1500. info := models.XtDrugWarehouseInfo{
  1501. ID: warehouse.ID,
  1502. WarehousingId: warehouse.WarehousingId,
  1503. DrugId: warehouse.DrugId,
  1504. Number: warehouse.Number,
  1505. ProductDate: warehouse.ProductDate,
  1506. ExpiryDate: warehouse.ExpiryDate,
  1507. WarehousingCount: warehouse.WarehousingCount,
  1508. Price: warehouse.RetailPrice,
  1509. TotalPrice: warehouse.TotalPrice,
  1510. Dealer: warehouse.Dealer,
  1511. Manufacturer: warehouse.Manufacturer,
  1512. Remark: warehouse.Remark,
  1513. Ctime: warehouse.Ctime,
  1514. Mtime: warehouse.Mtime,
  1515. Status: 1,
  1516. OrgId: warehouse.OrgId,
  1517. IsReturn: warehouse.IsReturn,
  1518. WarehousingOrder: warehouse.WarehousingOrder,
  1519. Type: warehouse.Type,
  1520. RetailPrice: warehouse.RetailPrice,
  1521. RetailTotalPrice: warehouse.RetailPrice,
  1522. StockMaxNumber: 0,
  1523. StockMinNumber: 0,
  1524. BatchNumber: warehouse.BatchNumber,
  1525. MaxUnit: warehouse.MaxUnit,
  1526. WarehousingInfoId: warehouse.WarehousingInfoId,
  1527. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1528. StorehouseId: storeConfig.DrugStorehouseOut,
  1529. IsCheck: 1,
  1530. }
  1531. //扣减库存
  1532. errThree := UpDateDrugWarehouseInfoByStock(&info)
  1533. if errThree != nil {
  1534. return errThree
  1535. }
  1536. //查询剩余库存
  1537. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  1538. var sum_count int64
  1539. for _, its := range stockInfo {
  1540. if its.MaxUnit == drup.MaxUnit {
  1541. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  1542. }
  1543. sum_count += its.StockMaxNumber + its.StockMinNumber
  1544. }
  1545. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  1546. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1547. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1548. WarehouseOutId: warehouseout.ID,
  1549. Status: 1,
  1550. Ctime: time.Now().Unix(),
  1551. Remark: warehouse.Remark,
  1552. OrgId: orgID,
  1553. Type: 1,
  1554. Manufacturer: warehouse.Manufacturer,
  1555. Dealer: warehouse.Dealer,
  1556. IsSys: 1,
  1557. SysRecordTime: advice.RecordDate,
  1558. DrugId: advice.DrugId,
  1559. Number: warehouse.Number,
  1560. BatchNumber: warehouse.BatchNumber,
  1561. Price: warehouse.RetailPrice,
  1562. CountUnit: drup.MinUnit,
  1563. ProductDate: warehouse.ProductDate,
  1564. ExpiryDate: warehouse.ExpiryDate,
  1565. PatientId: advice.PatientId,
  1566. Count: stock_number,
  1567. WarehouseInfoId: warehouse.ID,
  1568. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1569. StorehouseId: storeConfig.DrugStorehouseOut,
  1570. IsCheck: 1,
  1571. OverCount: sum_count,
  1572. AdviceId: advice.ID,
  1573. }
  1574. //查询是否存在出库数据
  1575. _, errCode := GetSigleDrugWarehouseOutInfo(advice.PatientId, advice.AdviceDate, orgID)
  1576. if errCode == gorm.ErrRecordNotFound {
  1577. fmt.Println("j你俩3233223323232323233wo")
  1578. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1579. if errOne != nil {
  1580. return errOne
  1581. }
  1582. }
  1583. if errCode == nil {
  1584. lastDrugOutInfo, _ := GetSigleDrugWarehouseOutInfoOne(advice.PatientId, advice.AdviceDate, orgID)
  1585. var his_count int64
  1586. if lastDrugOutInfo.CountUnit == drup.MaxUnit {
  1587. his_count = lastDrugOutInfo.Count * drup.MinNumber
  1588. }
  1589. if lastDrugOutInfo.CountUnit == drup.MinUnit {
  1590. his_count = lastDrugOutInfo.Count
  1591. }
  1592. if deliver_number != his_count {
  1593. errThree := UpdateSingleDrugWarehouseOutInfoSix(advice.PatientId, advice.AdviceDate, orgID, warehouseOutInfo, advice.ID)
  1594. if errThree != nil {
  1595. return errThree
  1596. }
  1597. }
  1598. }
  1599. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  1600. details := &models.DrugAutomaticReduceDetail{
  1601. WarehouseOutId: warehouseout.ID,
  1602. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  1603. PatientId: advice.PatientId,
  1604. Ctime: time.Now().Unix(),
  1605. Mtime: time.Now().Unix(),
  1606. Status: 1,
  1607. RecordTime: advice.RecordDate,
  1608. OrgId: orgID,
  1609. DrugId: advice.DrugId,
  1610. Count: stock_number,
  1611. CountUnit: drup.MinUnit,
  1612. WarehouseInfoId: warehouse.ID,
  1613. StorehouseId: storeConfig.DrugStorehouseOut,
  1614. AdviceId: advice.ID,
  1615. }
  1616. _, errcodeThree := FindDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId)
  1617. if errcodeThree == gorm.ErrRecordNotFound {
  1618. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  1619. if errTwo != nil {
  1620. return errTwo
  1621. }
  1622. }
  1623. if errcodeThree == nil {
  1624. UpdateDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId, details)
  1625. }
  1626. var out_count int64
  1627. var cancel_count int64
  1628. //查询出库数据
  1629. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  1630. //查询退库数据
  1631. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  1632. for _, item := range infoCountList {
  1633. out_count += item.Count
  1634. }
  1635. for _, item := range cancelInfoList {
  1636. cancel_count += item.Count
  1637. }
  1638. //如果本次出库数据大于历史出库数据 新增1条流水
  1639. if deliver_number > (out_count - cancel_count) {
  1640. drugflow := models.DrugFlow{
  1641. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1642. WarehouseOutId: warehouseout.ID,
  1643. DrugId: advice.DrugId,
  1644. Number: warehouse.Number,
  1645. ProductDate: warehouse.ProductDate,
  1646. ExpireDate: warehouse.ExpiryDate,
  1647. Count: stock_number,
  1648. Price: warehouse.RetailPrice,
  1649. Status: 1,
  1650. Ctime: time.Now().Unix(),
  1651. UserOrgId: orgID,
  1652. Manufacturer: warehouse.Manufacturer,
  1653. Dealer: warehouse.Dealer,
  1654. BatchNumber: warehouse.BatchNumber,
  1655. MaxUnit: drup.MinUnit,
  1656. ConsumableType: 3,
  1657. IsEdit: 1,
  1658. Creator: advice.ExecutionStaff,
  1659. IsSys: 1,
  1660. PatientId: advice.PatientId,
  1661. WarehousingDetailId: warehouse.ID,
  1662. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1663. StorehouseId: storeConfig.DrugStorehouseOut,
  1664. WarehouseOutDetailId: lastDrugOutInfo.ID,
  1665. OverCount: sum_count,
  1666. AdviceId: advice.ID,
  1667. LastPrice: warehouse.Price,
  1668. SystemTime: advice.AdviceDate,
  1669. }
  1670. CreateDrugFlowOne(drugflow)
  1671. //出库数量相加
  1672. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  1673. }
  1674. if deliver_number < (out_count - cancel_count) {
  1675. operation_time := time.Now().Unix()
  1676. ctime := time.Now().Unix()
  1677. timeStr := time.Now().Format("2006-01-02")
  1678. timeArr := strings.Split(timeStr, "-")
  1679. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  1680. total = total + 1
  1681. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  1682. cancelStock := models.DrugCancelStock{
  1683. OrderNumber: orderNumber,
  1684. OperaTime: operation_time,
  1685. OrgId: advice.UserOrgId,
  1686. Creater: advice.ExecutionStaff,
  1687. Ctime: ctime,
  1688. Status: 1,
  1689. ReturnTime: advice.AdviceDate,
  1690. Dealer: 0,
  1691. Manufacturer: 0,
  1692. Type: 1,
  1693. StorehouseId: storeConfig.DrugStorehouseOut,
  1694. IsCheck: 1,
  1695. }
  1696. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  1697. if errCode == gorm.ErrRecordNotFound {
  1698. AddSigleDrugCancelStock(&cancelStock)
  1699. }
  1700. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  1701. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  1702. dealer, _ := GetDealerById(warehouse.Dealer)
  1703. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  1704. cancelStockInfo := models.DrugCancelStockInfo{
  1705. OrderNumber: cancelInfo.OrderNumber,
  1706. CancelStockId: lastDrugCancelStock.ID,
  1707. DrugId: advice.DrugId,
  1708. Count: (out_count - cancel_count) - deliver_number,
  1709. Status: 1,
  1710. Ctime: ctime,
  1711. OrgId: advice.UserOrgId,
  1712. Type: 1,
  1713. Manufacturer: manufactureName.ManufacturerName,
  1714. Dealer: dealer.DealerName,
  1715. Total: 0,
  1716. RetailPrice: warehouse.RetailPrice,
  1717. RetailTotalPrice: warehouse.RetailTotalPrice,
  1718. Price: warehouse.Price,
  1719. RegisterAccount: "",
  1720. Remark: warehouse.Remark,
  1721. BatchNumber: warehouse.BatchNumber,
  1722. MaxUnit: drup.MinUnit,
  1723. ProductDate: warehouse.ProductDate,
  1724. ExpiryDate: warehouse.ExpiryDate,
  1725. BatchNumberId: warehouse.ID,
  1726. StorehouseId: storeConfig.DrugStorehouseOut,
  1727. IsCheck: 1,
  1728. }
  1729. CreatedCancelStock(cancelStockInfo)
  1730. flow := models.DrugFlow{
  1731. WarehousingId: warehouse.ID,
  1732. DrugId: warehouse.DrugId,
  1733. Number: "",
  1734. BatchNumber: warehouse.BatchNumber,
  1735. Count: out_count - cancel_count - deliver_number,
  1736. UserOrgId: advice.UserOrgId,
  1737. PatientId: advice.PatientId,
  1738. SystemTime: advice.AdviceDate,
  1739. ConsumableType: 7,
  1740. IsSys: 0,
  1741. WarehousingOrder: "",
  1742. WarehouseOutId: 0,
  1743. WarehouseOutOrderNumber: "",
  1744. IsEdit: 0,
  1745. CancelStockId: cancelStock.ID,
  1746. CancelOrderNumber: cancelStock.OrderNumber,
  1747. Manufacturer: warehouse.Manufacturer,
  1748. Dealer: warehouse.Dealer,
  1749. Creator: advice.ExecutionStaff,
  1750. UpdateCreator: 0,
  1751. Status: 1,
  1752. Ctime: time.Now().Unix(),
  1753. Mtime: 0,
  1754. Price: warehouse.RetailPrice,
  1755. WarehousingDetailId: warehouse.ID,
  1756. WarehouseOutDetailId: 0,
  1757. CancelOutDetailId: lastDrugCancelStock.ID,
  1758. ExpireDate: warehouse.ExpiryDate,
  1759. ProductDate: warehouse.ProductDate,
  1760. MaxUnit: drup.MinUnit,
  1761. MinUnit: "",
  1762. AdviceId: advice.ID,
  1763. StorehouseId: storeConfig.DrugStorehouseOut,
  1764. LastPrice: warehouse.Price,
  1765. OverCount: sum_count,
  1766. }
  1767. CreateDrugFlowOne(flow)
  1768. //退库数量增加
  1769. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  1770. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  1771. }
  1772. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1773. prescribingNumber_two_temp := deliver_number - stock_number
  1774. overPlusNumber := float64(prescribingNumber_two_temp)
  1775. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  1776. advice.PrescribingNumberUnit = drup.MinUnit
  1777. BloodHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  1778. }
  1779. return
  1780. }
  1781. func GetSigleDrugWarehouseOutInfoSix(patient_id int64, advice_date int64, orgid int64, advice_id int64) (*models.XtDrugWarehouseOutInfo, error) {
  1782. info := models.XtDrugWarehouseOutInfo{}
  1783. var err error
  1784. err = XTReadDB().Where("patient_id = ? and advice_date = ? and org_id = ? and status = 1 and advice_id =?", patient_id, advice_date, orgid, advice_id).Find(&info).Error
  1785. if err == gorm.ErrRecordNotFound {
  1786. return nil, err
  1787. }
  1788. if err != nil {
  1789. return nil, err
  1790. }
  1791. return &info, nil
  1792. }
  1793. func GetSigleDrugWarehouseOutInfo(patient_id int64, advice_date int64, orgid int64) (*models.XtDrugWarehouseOutInfo, error) {
  1794. info := models.XtDrugWarehouseOutInfo{}
  1795. var err error
  1796. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and org_id = ? and status = 1", patient_id, advice_date, orgid).Find(&info).Error
  1797. if err == gorm.ErrRecordNotFound {
  1798. return nil, err
  1799. }
  1800. if err != nil {
  1801. return nil, err
  1802. }
  1803. return &info, nil
  1804. }
  1805. func GetSigleDrugWarehouseOutInfoOne(patient_id int64, advice_date int64, orgid int64) (models.XtDrugWarehouseOutInfo, error) {
  1806. info := models.XtDrugWarehouseOutInfo{}
  1807. err = XTReadDB().Where("patient_id = ? and advice_date = ? and org_id = ? and status = 1", patient_id, advice_date, orgid).Find(&info).Error
  1808. return info, err
  1809. }
  1810. func UpdateSingleDrugWarehouseOutInfoSix(patient_id int64, advice_date int64, orgid int64, info *models.DrugWarehouseOutInfo, advice_id int64) error {
  1811. err := XTWriteDB().Model(&models.XtDrugWarehouseOutInfo{}).Where("patient_id = ? and sys_record_time = ? and org_id = ? and advice_id = ?", patient_id, advice_date, orgid, advice_id).Update(map[string]interface{}{"count": info.Count, "price": info.Price, "total_price": info.TotalPrice, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "number": info.Number, "batch_number": info.BatchNumber, "count_unit": info.CountUnit, "warehouse_info_id": info.WarehouseInfoId, "storehouse_id": info.StorehouseId}).Error
  1812. return err
  1813. }
  1814. func UpdateSingleDrugWarehouseOutInfo(patient_id int64, advice_date int64, orgid int64, info *models.DrugWarehouseOutInfo) error {
  1815. err := XTWriteDB().Model(&models.XtDrugWarehouseOutInfo{}).Where("patient_id = ? and advice_date = ? and org_id = ?", patient_id, advice_date, orgid).Update(map[string]interface{}{"count": info.Count, "price": info.Price, "total_price": info.TotalPrice, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "number": info.Number, "batch_number": info.BatchNumber, "count_unit": info.CountUnit, "warehouse_info_id": info.WarehouseInfoId, "storehouse_id": info.StorehouseId}).Error
  1816. return err
  1817. }
  1818. func FindDrugAutoReduceRecordInfo(patient_id int64, advice_date int64, orgid int64) (*models.DrugAutomaticReduceDetail, error) {
  1819. detail := models.DrugAutomaticReduceDetail{}
  1820. var err error
  1821. err = XTReadDB().Where("patient_id = ? and record_time = ? and org_id = ?", patient_id, advice_date, orgid).Find(&detail).Error
  1822. if err == gorm.ErrRecordNotFound {
  1823. return nil, err
  1824. }
  1825. if err != nil {
  1826. return nil, err
  1827. }
  1828. return &detail, nil
  1829. }
  1830. func UpdateDrugAutoReduceRecordInfo(patient_id int64, record_date int64, drug_id int64, detail *models.DrugAutomaticReduceDetail) error {
  1831. err := XTWriteDB().Model(&models.DrugAutomaticReduceDetail{}).Where("patient_id = ? and record_date = ? and drug_id = ?", patient_id, record_date, drug_id).Update(map[string]interface{}{"patient_id": detail.PatientId, "record_time": detail.RecordTime, "drug_id": detail.DrugId, "count": detail.Count, "count_unit": detail.CountUnit, "warehouse_info_id": detail.WarehouseInfoId, "advice_id": detail.AdviceId, "storehouse_id": detail.StorehouseId}).Error
  1832. return err
  1833. }
  1834. func GetNewDrugWarehouseOutInfoSix(drugid int64, patient_id int64, record_date int64, org_id int64, advice_id int64, warehouse_info_id int64) (models.DrugWarehouseOutInfo, error) {
  1835. info := models.DrugWarehouseOutInfo{}
  1836. var err error
  1837. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time = ? and org_id = ? and status = 1 and advice_id = ? and warehouse_info_id = ?", drugid, patient_id, record_date, org_id, advice_id, warehouse_info_id).Find(&info).Error
  1838. return info, err
  1839. }
  1840. func GetNewDrugWarehouseOutInfo(drugid int64, patient_id int64, record_date int64, org_id int64) (models.DrugWarehouseOutInfo, error) {
  1841. info := models.DrugWarehouseOutInfo{}
  1842. var err error
  1843. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time = ? and org_id = ? and status = 1 ", drugid, patient_id, record_date, org_id).Find(&info).Error
  1844. return info, err
  1845. }
  1846. func UpdateSigleDrugWarehouseOutInfo(id int64, info *models.DrugWarehouseOutInfo) error {
  1847. err := XTWriteDB().Model(models.DrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "total_price": info.TotalPrice, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "mtime": time.Now().Unix(), "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "warehouse_info_id": info.WarehouseInfoId, "number": info.Number, "batch_number": info.BatchNumber, "advice_id": info.AdviceId, "storehouse_id": info.StorehouseId, "last_price": info.LastPrice, "over_count": info.OverCount}).Error
  1848. return err
  1849. }
  1850. func UpdateSigleDrugWarehouseOutInfoTwo(id int64, info *models.DrugWarehouseOutInfo) error {
  1851. err := XTWriteDB().Model(models.DrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "total_price": info.TotalPrice, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "mtime": time.Now().Unix(), "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "warehouse_info_id": info.WarehouseInfoId, "number": info.Number, "batch_number": info.BatchNumber, "advice_id": info.AdviceId, "storehouse_id": info.StorehouseId, "last_price": info.LastPrice}).Error
  1852. return err
  1853. }
  1854. func FindDrugWarehouseOutInfoCount(drugid int64, patient_id int64, record_date int64, org_id int64, advice_id int64) (flow []*models.DrugFlow, err error) {
  1855. err = XTReadDB().Where("drug_id = ? and patient_id = ? and system_time = ? and user_org_id = ? and consumable_type = 3 and status = 1 and advice_id = ?", drugid, patient_id, record_date, org_id, advice_id).Find(&flow).Error
  1856. return flow, err
  1857. }
  1858. func FindDrugCancelOutInfo(drugid int64, patient_id int64, record_date int64, org_id int64, advice_id int64) (flow []*models.DrugFlow, err error) {
  1859. err = XTReadDB().Where("drug_id = ? and patient_id = ? and system_time = ? and user_org_id = ? and consumable_type = 7 and status = 1 and advice_id = ?", drugid, patient_id, record_date, org_id, advice_id).Find(&flow).Error
  1860. return flow, err
  1861. }
  1862. func FindDrugCancelStock(return_date int64, org_id int64) (*models.DrugCancelStock, error) {
  1863. stock := models.DrugCancelStock{}
  1864. var err error
  1865. err = XTReadDB().Where("return_date = ? and org_id = ? and status = 1", return_date, org_id).Find(&stock).Error
  1866. if err == gorm.ErrRecordNotFound {
  1867. return nil, err
  1868. }
  1869. if err != nil {
  1870. return nil, err
  1871. }
  1872. return &stock, nil
  1873. }
  1874. func FindLastDrugCancelStock(advice_date int64, org_id int64) (models.DrugCancelStock, error) {
  1875. cancelStock := models.DrugCancelStock{}
  1876. err := XTReadDB().Where("return_date = ? and org_id = ? and status = 1", advice_date, org_id).Find(&cancelStock).Error
  1877. return cancelStock, err
  1878. }
  1879. func GetAllSumDrugWarehouseOut(patient_id int64, user_org_id int64, advice_date int64) (advice []*models.HisDoctorAdvice, err error) {
  1880. err = XTReadDB().Where("patient_id = ? and user_org_id = ? and advice_date = ? and status = 1", patient_id, user_org_id, advice_date).Find(&advice).Error
  1881. return advice, err
  1882. }
  1883. func UpdateDrugWarehouseOutByPatientId(patient_id int64, advice_date int64, orgid int64, info models.DrugWarehouseOutInfo) error {
  1884. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("patient_id = ? and sys_record_time = ? and org_id = ? and status = 1", patient_id, advice_date, orgid).Update(map[string]interface{}{"count": info.Count, "count_unit": info.CountUnit}).Error
  1885. return err
  1886. }
  1887. func FindeGoodInfo(orgid int64, id int64) (models.GoodInformation, error) {
  1888. information := models.GoodInformation{}
  1889. err := XTReadDB().Where("org_id = ? and id = ? and status = 1", orgid, id).Find(&information).Error
  1890. return information, err
  1891. }
  1892. func UpdateHisPrescriptionProjectStatus(project_id int64, record_time int64, user_org_id int64, patient_id int64) (models.HisPrescriptionProject, error) {
  1893. project := models.HisPrescriptionProject{}
  1894. err := XTWriteDB().Model(&project).Where("project_id = ? and record_date = ? and user_org_id = ? and patient_id = ? and status = 1", project_id, record_time, user_org_id, patient_id).Update(map[string]interface{}{"is_out": 1}).Error
  1895. return project, err
  1896. }
  1897. // 药品自动出库 递归方式
  1898. func HisSettleDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  1899. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  1900. var deliver_number int64 = 0
  1901. var stock_number int64 = 0
  1902. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  1903. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1904. var drug_price float64
  1905. if advice.PrescribingNumberUnit == drup.MaxUnit {
  1906. deliver_number = count * drup.MinNumber
  1907. } else {
  1908. deliver_number = count
  1909. }
  1910. if advice.PrescribingNumberUnit == drup.MaxUnit {
  1911. drug_price = drup.RetailPrice
  1912. }
  1913. fmt.Println(drug_price)
  1914. if advice.PrescribingNumberUnit == drup.MinUnit {
  1915. drug_price = drup.MinPrice
  1916. }
  1917. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  1918. drug_price = drup.RetailPrice
  1919. }
  1920. // 根据先进先出原则,查询最先入库的批次,进行出库
  1921. // 如果没有对应的库存,则报错
  1922. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  1923. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1924. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  1925. var stockMax int64
  1926. var stockMin int64
  1927. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  1928. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  1929. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  1930. UpdateMinNumber(lastWarehouse.ID, stockMin)
  1931. }
  1932. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1933. if err != nil {
  1934. return err
  1935. }
  1936. // 将该批次的剩余库存数量转换为拆零数量
  1937. // 将该批次的剩余库存数量转换为拆零数量
  1938. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1939. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  1940. }
  1941. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  1942. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  1943. }
  1944. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  1945. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  1946. }
  1947. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1948. if stock_number >= deliver_number {
  1949. var maxNumber int64 = 0
  1950. var minNumber int64 = 0
  1951. maxNumber = deliver_number / drup.MinNumber
  1952. minNumber = deliver_number % drup.MinNumber
  1953. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  1954. minNumber = maxNumber
  1955. }
  1956. if drup.MaxUnit != drup.MinUnit {
  1957. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  1958. return errors.New("库存数量不足")
  1959. }
  1960. }
  1961. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1962. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  1963. }
  1964. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1965. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  1966. }
  1967. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  1968. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  1969. }
  1970. if warehouse.StockMaxNumber < 0 {
  1971. warehouse.StockMaxNumber = 0
  1972. }
  1973. if warehouse.StockMinNumber < 0 {
  1974. warehouse.StockMinNumber = 0
  1975. }
  1976. warehouse.Mtime = time.Now().Unix()
  1977. fmt.Println("minnU目标而332322323323233223323223我", minNumber)
  1978. fmt.Println("max_number232232332232322wo", maxNumber)
  1979. fmt.Println(" warehouse.StockMinNumber", warehouse.StockMinNumber)
  1980. if warehouse.StockMinNumber < minNumber {
  1981. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  1982. if warehouse.MaxUnit != warehouse.MinUnit {
  1983. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  1984. }
  1985. } else {
  1986. if minNumber > 0 {
  1987. if (warehouse.StockMinNumber - minNumber) >= 0 {
  1988. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  1989. }
  1990. }
  1991. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  1992. if warehouse.StockMinNumber > 0 {
  1993. if warehouse.StockMinNumber-deliver_number >= 0 {
  1994. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  1995. }
  1996. }
  1997. }
  1998. }
  1999. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  2000. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  2001. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2002. }
  2003. }
  2004. if drup.MaxUnit != drup.MinUnit {
  2005. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  2006. return errors.New("库存数量不足")
  2007. }
  2008. }
  2009. if warehouse.StockMinNumber <= 0 {
  2010. warehouse.StockMinNumber = 0
  2011. }
  2012. //扣减库存数据
  2013. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  2014. if errThree != nil {
  2015. return errThree
  2016. }
  2017. //查询剩余库存
  2018. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  2019. var sum_count int64
  2020. for _, its := range stockInfo {
  2021. if its.MaxUnit == drup.MaxUnit {
  2022. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  2023. }
  2024. sum_count += its.StockMaxNumber + its.StockMinNumber
  2025. }
  2026. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2027. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2028. WarehouseOutId: warehouseout.ID,
  2029. Status: 1,
  2030. Ctime: time.Now().Unix(),
  2031. Remark: warehouse.Remark,
  2032. OrgId: orgID,
  2033. Type: 5,
  2034. Manufacturer: warehouse.Manufacturer,
  2035. Dealer: warehouse.Dealer,
  2036. IsSys: 1,
  2037. SysRecordTime: advice.RecordDate,
  2038. DrugId: advice.DrugId,
  2039. Number: warehouse.Number,
  2040. BatchNumber: warehouse.BatchNumber,
  2041. Price: warehouse.RetailPrice,
  2042. CountUnit: drup.MinUnit,
  2043. RetailPrice: warehouse.RetailPrice,
  2044. ProductDate: warehouse.ProductDate,
  2045. ExpiryDate: warehouse.ExpiryDate,
  2046. PatientId: advice.PatientId,
  2047. Count: deliver_number, //出最小单位
  2048. WarehouseInfoId: warehouse.ID,
  2049. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2050. StorehouseId: storeConfig.DrugStorehouseOut,
  2051. IsCheck: 1,
  2052. OverCount: sum_count,
  2053. AdviceId: advice.ID,
  2054. OrderId: warehouseout.OrderId,
  2055. }
  2056. //查询今日该药品该患者是否有出库数据
  2057. lastDrugOutInfo, _ := GetNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID)
  2058. if lastDrugOutInfo.ID == 0 {
  2059. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2060. if errOne != nil {
  2061. return errOne
  2062. }
  2063. }
  2064. if lastDrugOutInfo.ID > 0 {
  2065. var his_count int64
  2066. if lastDrugOutInfo.CountUnit == drup.MaxUnit {
  2067. his_count = lastDrugOutInfo.Count * drup.MinNumber
  2068. }
  2069. if lastDrugOutInfo.CountUnit == drup.MinUnit {
  2070. his_count = lastDrugOutInfo.Count
  2071. }
  2072. if deliver_number != his_count {
  2073. errThree := UpdateSigleDrugWarehouseOutInfo(lastDrugOutInfo.ID, warehouseOutInfo)
  2074. if errThree != nil {
  2075. return errThree
  2076. }
  2077. }
  2078. }
  2079. var out_count int64
  2080. var cancel_count int64
  2081. //查询出库数据
  2082. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2083. //查询退库数据
  2084. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2085. for _, item := range infoCountList {
  2086. out_count += item.Count
  2087. }
  2088. for _, item := range cancelInfoList {
  2089. cancel_count += item.Count
  2090. }
  2091. fmt.Println("deliver_number", deliver_number)
  2092. fmt.Println("out_count", out_count)
  2093. fmt.Println("cancel_count", cancel_count)
  2094. //如果本次出库数据大于历史出库数据 新增1条流水
  2095. if deliver_number > (out_count - cancel_count) {
  2096. drugflow := models.DrugFlow{
  2097. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2098. WarehouseOutId: warehouseout.ID,
  2099. DrugId: advice.DrugId,
  2100. Number: warehouse.Number,
  2101. ProductDate: warehouse.ProductDate,
  2102. ExpireDate: warehouse.ExpiryDate,
  2103. Count: deliver_number - (out_count - cancel_count), //按最小单位计算,
  2104. Price: warehouse.RetailPrice,
  2105. Status: 1,
  2106. Ctime: time.Now().Unix(),
  2107. UserOrgId: orgID,
  2108. Manufacturer: warehouse.Manufacturer,
  2109. Dealer: warehouse.Dealer,
  2110. BatchNumber: warehouse.BatchNumber,
  2111. MaxUnit: drup.MinUnit,
  2112. ConsumableType: 15,
  2113. IsEdit: 1,
  2114. Creator: advice.ExecutionStaff,
  2115. IsSys: 1,
  2116. PatientId: advice.PatientId,
  2117. SystemTime: advice.AdviceDate,
  2118. WarehousingDetailId: warehouse.ID,
  2119. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2120. StorehouseId: storeConfig.DrugStorehouseOut,
  2121. WarehouseOutDetailId: lastDrugOutInfo.ID,
  2122. OverCount: sum_count,
  2123. AdviceId: advice.ID,
  2124. LastPrice: warehouse.Price,
  2125. OrderId: warehouseout.OrderId,
  2126. }
  2127. CreateDrugFlowOne(drugflow)
  2128. //出库数量相加
  2129. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  2130. }
  2131. //如果本次出库数据小于历史出库数据 新增1条退库流水
  2132. if deliver_number < (out_count - cancel_count) {
  2133. operation_time := time.Now().Unix()
  2134. ctime := time.Now().Unix()
  2135. timeStr := time.Now().Format("2006-01-02")
  2136. timeArr := strings.Split(timeStr, "-")
  2137. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  2138. total = total + 1
  2139. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  2140. cancelStock := models.DrugCancelStock{
  2141. OrderNumber: orderNumber,
  2142. OperaTime: operation_time,
  2143. OrgId: advice.UserOrgId,
  2144. Creater: advice.ExecutionStaff,
  2145. Ctime: ctime,
  2146. Status: 1,
  2147. ReturnTime: advice.AdviceDate,
  2148. Dealer: 0,
  2149. Manufacturer: 0,
  2150. Type: 5,
  2151. StorehouseId: storeConfig.DrugStorehouseOut,
  2152. IsCheck: 1,
  2153. OrderId: warehouseout.OrderId,
  2154. }
  2155. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2156. if errCode == gorm.ErrRecordNotFound {
  2157. AddSigleDrugCancelStock(&cancelStock)
  2158. }
  2159. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2160. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  2161. dealer, _ := GetDealerById(warehouse.Dealer)
  2162. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  2163. cancelStockInfo := models.DrugCancelStockInfo{
  2164. OrderNumber: cancelInfo.OrderNumber,
  2165. CancelStockId: lastDrugCancelStock.ID,
  2166. DrugId: advice.DrugId,
  2167. Count: (out_count - cancel_count) - deliver_number,
  2168. Status: 1,
  2169. Ctime: ctime,
  2170. OrgId: advice.UserOrgId,
  2171. Type: 5,
  2172. Manufacturer: manufactureName.ManufacturerName,
  2173. Dealer: dealer.DealerName,
  2174. Total: 0,
  2175. RetailPrice: warehouse.RetailPrice,
  2176. RetailTotalPrice: warehouse.RetailTotalPrice,
  2177. Price: warehouse.Price,
  2178. RegisterAccount: "",
  2179. Remark: warehouse.Remark,
  2180. BatchNumber: warehouse.BatchNumber,
  2181. MaxUnit: drup.MinUnit,
  2182. ProductDate: warehouse.ProductDate,
  2183. ExpiryDate: warehouse.ExpiryDate,
  2184. BatchNumberId: warehouse.ID,
  2185. StorehouseId: storeConfig.DrugStorehouseOut,
  2186. IsCheck: 1,
  2187. OrderId: warehouseout.OrderId,
  2188. }
  2189. CreatedCancelStock(cancelStockInfo)
  2190. flow := models.DrugFlow{
  2191. WarehousingId: warehouse.ID,
  2192. DrugId: warehouse.DrugId,
  2193. Number: "",
  2194. BatchNumber: warehouse.BatchNumber,
  2195. Count: out_count - cancel_count - deliver_number,
  2196. UserOrgId: advice.UserOrgId,
  2197. PatientId: advice.PatientId,
  2198. SystemTime: advice.AdviceDate,
  2199. ConsumableType: 16,
  2200. IsSys: 0,
  2201. WarehousingOrder: "",
  2202. WarehouseOutId: 0,
  2203. WarehouseOutOrderNumber: "",
  2204. IsEdit: 0,
  2205. CancelStockId: cancelStock.ID,
  2206. CancelOrderNumber: cancelStock.OrderNumber,
  2207. Manufacturer: warehouse.Manufacturer,
  2208. Dealer: warehouse.Dealer,
  2209. Creator: advice.ExecutionStaff,
  2210. UpdateCreator: 0,
  2211. Status: 1,
  2212. Ctime: time.Now().Unix(),
  2213. Mtime: 0,
  2214. Price: warehouse.RetailPrice,
  2215. WarehousingDetailId: warehouse.ID,
  2216. WarehouseOutDetailId: 0,
  2217. CancelOutDetailId: lastDrugCancelStock.ID,
  2218. ExpireDate: warehouse.ExpiryDate,
  2219. ProductDate: warehouse.ProductDate,
  2220. MaxUnit: drup.MinUnit,
  2221. MinUnit: "",
  2222. AdviceId: advice.ID,
  2223. StorehouseId: storeConfig.DrugStorehouseOut,
  2224. LastPrice: warehouse.Price,
  2225. OverCount: sum_count,
  2226. OrderId: warehouseout.OrderId,
  2227. }
  2228. CreateDrugFlowOne(flow)
  2229. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  2230. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  2231. }
  2232. //查询是否存在数据
  2233. details := &models.DrugAutomaticReduceDetail{
  2234. WarehouseOutId: warehouseout.ID,
  2235. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2236. PatientId: advice.PatientId,
  2237. Ctime: time.Now().Unix(),
  2238. Mtime: time.Now().Unix(),
  2239. Status: 1,
  2240. RecordTime: advice.RecordDate,
  2241. OrgId: orgID,
  2242. DrugId: advice.DrugId,
  2243. Count: deliver_number,
  2244. CountUnit: drup.MinUnit,
  2245. WarehouseInfoId: warehouse.ID,
  2246. StorehouseId: storeConfig.DrugStorehouseOut,
  2247. AdviceId: advice.ID,
  2248. }
  2249. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2250. if errTwo != nil {
  2251. return errTwo
  2252. }
  2253. return nil
  2254. } else {
  2255. info := models.XtDrugWarehouseInfo{
  2256. ID: warehouse.ID,
  2257. WarehousingId: warehouse.WarehousingId,
  2258. DrugId: warehouse.DrugId,
  2259. Number: warehouse.Number,
  2260. ProductDate: warehouse.ProductDate,
  2261. ExpiryDate: warehouse.ExpiryDate,
  2262. WarehousingCount: warehouse.WarehousingCount,
  2263. Price: warehouse.RetailPrice,
  2264. TotalPrice: warehouse.TotalPrice,
  2265. Dealer: warehouse.Dealer,
  2266. Manufacturer: warehouse.Manufacturer,
  2267. Remark: warehouse.Remark,
  2268. Ctime: warehouse.Ctime,
  2269. Mtime: warehouse.Mtime,
  2270. Status: 1,
  2271. OrgId: warehouse.OrgId,
  2272. IsReturn: warehouse.IsReturn,
  2273. WarehousingOrder: warehouse.WarehousingOrder,
  2274. Type: warehouse.Type,
  2275. RetailPrice: warehouse.RetailPrice,
  2276. RetailTotalPrice: warehouse.RetailPrice,
  2277. StockMaxNumber: 0,
  2278. StockMinNumber: 0,
  2279. BatchNumber: warehouse.BatchNumber,
  2280. MaxUnit: warehouse.MaxUnit,
  2281. WarehousingInfoId: warehouse.WarehousingInfoId,
  2282. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2283. StorehouseId: storeConfig.DrugStorehouseOut,
  2284. IsCheck: 1,
  2285. }
  2286. //扣减库存
  2287. errThree := UpDateDrugWarehouseInfoByStock(&info)
  2288. if errThree != nil {
  2289. return errThree
  2290. }
  2291. //查询剩余库存
  2292. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  2293. var sum_count int64
  2294. for _, its := range stockInfo {
  2295. if its.MaxUnit == drup.MaxUnit {
  2296. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  2297. }
  2298. sum_count += its.StockMaxNumber + its.StockMinNumber
  2299. }
  2300. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2301. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2302. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2303. WarehouseOutId: warehouseout.ID,
  2304. Status: 1,
  2305. Ctime: time.Now().Unix(),
  2306. Remark: warehouse.Remark,
  2307. OrgId: orgID,
  2308. Type: 5,
  2309. Manufacturer: warehouse.Manufacturer,
  2310. Dealer: warehouse.Dealer,
  2311. IsSys: 1,
  2312. SysRecordTime: advice.RecordDate,
  2313. DrugId: advice.DrugId,
  2314. Number: warehouse.Number,
  2315. BatchNumber: warehouse.BatchNumber,
  2316. Price: warehouse.RetailPrice,
  2317. CountUnit: drup.MinUnit,
  2318. ProductDate: warehouse.ProductDate,
  2319. ExpiryDate: warehouse.ExpiryDate,
  2320. PatientId: advice.PatientId,
  2321. Count: stock_number,
  2322. WarehouseInfoId: warehouse.ID,
  2323. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2324. StorehouseId: storeConfig.DrugStorehouseOut,
  2325. IsCheck: 1,
  2326. OverCount: sum_count,
  2327. AdviceId: advice.ID,
  2328. OrderId: warehouseout.OrderId,
  2329. }
  2330. //查询是否存在出库数据
  2331. _, errCode := GetSigleDrugWarehouseOutInfo(advice.PatientId, advice.AdviceDate, orgID)
  2332. if errCode == gorm.ErrRecordNotFound {
  2333. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2334. if errOne != nil {
  2335. return errOne
  2336. }
  2337. }
  2338. if errCode == nil {
  2339. lastDrugOutInfo, _ := GetSigleDrugWarehouseOutInfoOne(advice.PatientId, advice.AdviceDate, orgID)
  2340. var his_count int64
  2341. if lastDrugOutInfo.CountUnit == drup.MaxUnit {
  2342. his_count = lastDrugOutInfo.Count * drup.MinNumber
  2343. }
  2344. if lastDrugOutInfo.CountUnit == drup.MinUnit {
  2345. his_count = lastDrugOutInfo.Count
  2346. }
  2347. if deliver_number != his_count {
  2348. errThree := UpdateSingleDrugWarehouseOutInfoSix(advice.PatientId, advice.AdviceDate, orgID, warehouseOutInfo, advice.ID)
  2349. if errThree != nil {
  2350. return errThree
  2351. }
  2352. }
  2353. }
  2354. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  2355. details := &models.DrugAutomaticReduceDetail{
  2356. WarehouseOutId: warehouseout.ID,
  2357. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  2358. PatientId: advice.PatientId,
  2359. Ctime: time.Now().Unix(),
  2360. Mtime: time.Now().Unix(),
  2361. Status: 1,
  2362. RecordTime: advice.RecordDate,
  2363. OrgId: orgID,
  2364. DrugId: advice.DrugId,
  2365. Count: stock_number,
  2366. CountUnit: drup.MinUnit,
  2367. WarehouseInfoId: warehouse.ID,
  2368. StorehouseId: storeConfig.DrugStorehouseOut,
  2369. AdviceId: advice.ID,
  2370. }
  2371. _, errcodeThree := FindDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId)
  2372. if errcodeThree == gorm.ErrRecordNotFound {
  2373. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2374. if errTwo != nil {
  2375. return errTwo
  2376. }
  2377. }
  2378. if errcodeThree == nil {
  2379. UpdateDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId, details)
  2380. }
  2381. var out_count int64
  2382. var cancel_count int64
  2383. //查询出库数据
  2384. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2385. //查询退库数据
  2386. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2387. for _, item := range infoCountList {
  2388. out_count += item.Count
  2389. }
  2390. for _, item := range cancelInfoList {
  2391. cancel_count += item.Count
  2392. }
  2393. //如果本次出库数据大于历史出库数据 新增1条流水
  2394. if deliver_number > (out_count - cancel_count) {
  2395. drugflow := models.DrugFlow{
  2396. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2397. WarehouseOutId: warehouseout.ID,
  2398. DrugId: advice.DrugId,
  2399. Number: warehouse.Number,
  2400. ProductDate: warehouse.ProductDate,
  2401. ExpireDate: warehouse.ExpiryDate,
  2402. Count: stock_number,
  2403. Price: warehouse.RetailPrice,
  2404. Status: 1,
  2405. Ctime: time.Now().Unix(),
  2406. UserOrgId: orgID,
  2407. Manufacturer: warehouse.Manufacturer,
  2408. Dealer: warehouse.Dealer,
  2409. BatchNumber: warehouse.BatchNumber,
  2410. MaxUnit: drup.MinUnit,
  2411. ConsumableType: 15,
  2412. IsEdit: 1,
  2413. Creator: advice.ExecutionStaff,
  2414. IsSys: 1,
  2415. PatientId: advice.PatientId,
  2416. WarehousingDetailId: warehouse.ID,
  2417. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2418. StorehouseId: storeConfig.DrugStorehouseOut,
  2419. WarehouseOutDetailId: lastDrugOutInfo.ID,
  2420. OverCount: sum_count,
  2421. AdviceId: advice.ID,
  2422. LastPrice: warehouse.Price,
  2423. SystemTime: advice.AdviceDate,
  2424. OrderId: warehouseout.OrderId,
  2425. }
  2426. CreateDrugFlowOne(drugflow)
  2427. //出库数量相加
  2428. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  2429. }
  2430. if deliver_number < (out_count - cancel_count) {
  2431. operation_time := time.Now().Unix()
  2432. ctime := time.Now().Unix()
  2433. timeStr := time.Now().Format("2006-01-02")
  2434. timeArr := strings.Split(timeStr, "-")
  2435. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  2436. total = total + 1
  2437. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  2438. cancelStock := models.DrugCancelStock{
  2439. OrderNumber: orderNumber,
  2440. OperaTime: operation_time,
  2441. OrgId: advice.UserOrgId,
  2442. Creater: advice.ExecutionStaff,
  2443. Ctime: ctime,
  2444. Status: 1,
  2445. ReturnTime: advice.AdviceDate,
  2446. Dealer: 0,
  2447. Manufacturer: 0,
  2448. Type: 5,
  2449. StorehouseId: storeConfig.DrugStorehouseOut,
  2450. IsCheck: 1,
  2451. OrderId: warehouseout.OrderId,
  2452. }
  2453. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2454. if errCode == gorm.ErrRecordNotFound {
  2455. AddSigleDrugCancelStock(&cancelStock)
  2456. }
  2457. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2458. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  2459. dealer, _ := GetDealerById(warehouse.Dealer)
  2460. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  2461. cancelStockInfo := models.DrugCancelStockInfo{
  2462. OrderNumber: cancelInfo.OrderNumber,
  2463. CancelStockId: lastDrugCancelStock.ID,
  2464. DrugId: advice.DrugId,
  2465. Count: (out_count - cancel_count) - deliver_number,
  2466. Status: 1,
  2467. Ctime: ctime,
  2468. OrgId: advice.UserOrgId,
  2469. Type: 5,
  2470. Manufacturer: manufactureName.ManufacturerName,
  2471. Dealer: dealer.DealerName,
  2472. Total: 0,
  2473. RetailPrice: warehouse.RetailPrice,
  2474. RetailTotalPrice: warehouse.RetailTotalPrice,
  2475. Price: warehouse.Price,
  2476. RegisterAccount: "",
  2477. Remark: warehouse.Remark,
  2478. BatchNumber: warehouse.BatchNumber,
  2479. MaxUnit: drup.MinUnit,
  2480. ProductDate: warehouse.ProductDate,
  2481. ExpiryDate: warehouse.ExpiryDate,
  2482. BatchNumberId: warehouse.ID,
  2483. StorehouseId: storeConfig.DrugStorehouseOut,
  2484. IsCheck: 1,
  2485. OrderId: warehouseout.OrderId,
  2486. }
  2487. CreatedCancelStock(cancelStockInfo)
  2488. flow := models.DrugFlow{
  2489. WarehousingId: warehouse.ID,
  2490. DrugId: warehouse.DrugId,
  2491. Number: "",
  2492. BatchNumber: warehouse.BatchNumber,
  2493. Count: out_count - cancel_count - deliver_number,
  2494. UserOrgId: advice.UserOrgId,
  2495. PatientId: advice.PatientId,
  2496. SystemTime: advice.AdviceDate,
  2497. ConsumableType: 16,
  2498. IsSys: 0,
  2499. WarehousingOrder: "",
  2500. WarehouseOutId: 0,
  2501. WarehouseOutOrderNumber: "",
  2502. IsEdit: 0,
  2503. CancelStockId: cancelStock.ID,
  2504. CancelOrderNumber: cancelStock.OrderNumber,
  2505. Manufacturer: warehouse.Manufacturer,
  2506. Dealer: warehouse.Dealer,
  2507. Creator: advice.ExecutionStaff,
  2508. UpdateCreator: 0,
  2509. Status: 1,
  2510. Ctime: time.Now().Unix(),
  2511. Mtime: 0,
  2512. Price: warehouse.RetailPrice,
  2513. WarehousingDetailId: warehouse.ID,
  2514. WarehouseOutDetailId: 0,
  2515. CancelOutDetailId: lastDrugCancelStock.ID,
  2516. ExpireDate: warehouse.ExpiryDate,
  2517. ProductDate: warehouse.ProductDate,
  2518. MaxUnit: drup.MinUnit,
  2519. MinUnit: "",
  2520. AdviceId: advice.ID,
  2521. StorehouseId: storeConfig.DrugStorehouseOut,
  2522. LastPrice: warehouse.Price,
  2523. OverCount: sum_count,
  2524. OrderId: warehouseout.OrderId,
  2525. }
  2526. CreateDrugFlowOne(flow)
  2527. //退库数量增加
  2528. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  2529. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  2530. }
  2531. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2532. prescribingNumber_two_temp := deliver_number - stock_number
  2533. overPlusNumber := float64(prescribingNumber_two_temp)
  2534. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  2535. advice.PrescribingNumberUnit = drup.MinUnit
  2536. HisSettleDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  2537. }
  2538. return
  2539. }