warhouse_service.go 194KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457
  1. package service
  2. import (
  3. "XT_New/models"
  4. "XT_New/utils"
  5. "errors"
  6. "fmt"
  7. _ "fmt"
  8. "github.com/jinzhu/gorm"
  9. "math"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // 药品出库
  15. func DrugsDelivery(orgID int64, creater int64, advice *models.DoctorAdvice) (err error) {
  16. // 1.判断药品是否来自专用字典的药品库
  17. // 2.判断当天当前机构有没有创建出库单,没有则创建
  18. // 3.创建出库流程
  19. // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  20. isHasWay := false
  21. record_time := int64(0)
  22. if advice.Way == 1 {
  23. isHasWay = true
  24. record_time = advice.RecordDate
  25. }
  26. if isHasWay {
  27. //判断当天当前机构有没有创建出库单,没有则创建
  28. out, err := FindDrugStockOutByIsSys(orgID, 1, record_time)
  29. if err == gorm.ErrRecordNotFound {
  30. timeStr := time.Now().Format("2006-01-02")
  31. timeArr := strings.Split(timeStr, "-")
  32. total, _ := FindAllDrugWarehouseOut(orgID)
  33. total = total + 1
  34. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  35. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  36. number = number + total
  37. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  38. storeConfig, _ := GetAllStoreHouseConfig(orgID)
  39. warehouseOut := models.DrugWarehouseOut{
  40. WarehouseOutOrderNumber: warehousing_out_order,
  41. OperationTime: time.Now().Unix(),
  42. OrgId: orgID,
  43. Creater: creater,
  44. Ctime: time.Now().Unix(),
  45. Status: 1,
  46. WarehouseOutTime: record_time,
  47. Dealer: 0,
  48. Manufacturer: 0,
  49. Type: 1,
  50. IsSys: 1,
  51. StorehouseId: storeConfig.DrugStorehouseOut,
  52. }
  53. err := AddSigleDrugWarehouseOut(&warehouseOut)
  54. if err != nil {
  55. utils.TraceLog("创建出库单失败 err = %v", err)
  56. return err
  57. } else {
  58. out = warehouseOut
  59. }
  60. }
  61. // 出库流程
  62. // 1.查询该药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  63. drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
  64. if drup.ID > 0 {
  65. prescribingNumber := advice.PrescribingNumber
  66. DrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice)
  67. } else {
  68. return errors.New("药品信息不存在")
  69. }
  70. }
  71. return
  72. }
  73. // 药品自动出库 递归方式
  74. func DrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DoctorAdvice) (err error) {
  75. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  76. var deliver_number int64 = 0
  77. var stock_number int64 = 0
  78. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  79. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  80. var drug_price float64
  81. if advice.PrescribingNumberUnit == drup.MaxUnit {
  82. deliver_number = count * drup.MinNumber
  83. } else {
  84. deliver_number = count
  85. }
  86. if advice.PrescribingNumberUnit == drup.MaxUnit {
  87. drug_price = drup.RetailPrice
  88. }
  89. fmt.Println(drug_price)
  90. if advice.PrescribingNumberUnit == drup.MinUnit {
  91. drug_price = drup.MinPrice
  92. }
  93. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  94. drug_price = drup.RetailPrice
  95. }
  96. // 根据先进先出原则,查询最先入库的批次,进行出库
  97. // 如果没有对应的库存,则报错
  98. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  99. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  100. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  101. var stockMax int64
  102. var stockMin int64
  103. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  104. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  105. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  106. UpdateMinNumber(lastWarehouse.ID, stockMin)
  107. }
  108. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  109. if err != nil {
  110. return err
  111. }
  112. // 将该批次的剩余库存数量转换为拆零数量
  113. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  114. //查找药品信息
  115. //baseInfo, _ := FindBaseDrugLibRecord(advice.UserOrgId, advice.DrugId)
  116. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  117. if stock_number >= deliver_number {
  118. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  119. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  120. WarehouseOutId: warehouseout.ID,
  121. Status: 1,
  122. Ctime: time.Now().Unix(),
  123. Remark: warehouse.Remark,
  124. OrgId: orgID,
  125. Type: 1,
  126. Manufacturer: warehouse.Manufacturer,
  127. Dealer: warehouse.Dealer,
  128. IsSys: 1,
  129. SysRecordTime: advice.RecordDate,
  130. DrugId: advice.DrugId,
  131. Number: warehouse.Number,
  132. BatchNumber: warehouse.BatchNumber,
  133. Price: warehouse.RetailPrice,
  134. CountUnit: drup.MinUnit,
  135. RetailPrice: warehouse.RetailPrice,
  136. ProductDate: warehouse.ProductDate,
  137. ExpiryDate: warehouse.ExpiryDate,
  138. PatientId: advice.PatientId,
  139. Count: deliver_number, //出最小单位
  140. WarehouseInfoId: warehouse.ID,
  141. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  142. StorehouseId: storeConfig.DrugStorehouseOut,
  143. }
  144. drugflow := models.DrugFlow{
  145. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  146. WarehouseOutId: warehouseout.ID,
  147. DrugId: advice.DrugId,
  148. Number: warehouse.Number,
  149. ProductDate: warehouse.ProductDate,
  150. ExpireDate: warehouse.ExpiryDate,
  151. Count: deliver_number, //按最小单位计算,
  152. Price: warehouse.RetailPrice,
  153. Status: 1,
  154. Ctime: time.Now().Unix(),
  155. UserOrgId: orgID,
  156. Manufacturer: warehouse.Manufacturer,
  157. Dealer: warehouse.Dealer,
  158. BatchNumber: warehouse.BatchNumber,
  159. MaxUnit: drup.MinUnit,
  160. ConsumableType: 3,
  161. IsEdit: 1,
  162. Creator: advice.ExecutionStaff,
  163. IsSys: 1,
  164. PatientId: advice.PatientId,
  165. SystemTime: advice.AdviceDate,
  166. WarehousingDetailId: warehouse.ID,
  167. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  168. StorehouseId: storeConfig.DrugStorehouseOut,
  169. }
  170. CreateDrugFlowOne(drugflow)
  171. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  172. fmt.Println(errOne)
  173. //查询是否存在数据
  174. //_, errcodes := GetDrugWarehouseOutInfoIsExit(advice.DrugId, advice.PatientId, advice.RecordDate)
  175. //if errcodes == gorm.ErrRecordNotFound {
  176. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  177. // if errOne != nil {
  178. // return errOne
  179. // }
  180. //} else if errcodes == nil {
  181. // //service.GetLastDrugWarehouseOutInfoByPatientId(advice.DrugId, advice.PatientId, advice.RecordDate)
  182. // UpdatedDrugWarehouseOutInfo(warehouseOutInfo, advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId)
  183. //}
  184. details := &models.DrugAutomaticReduceDetail{
  185. WarehouseOutId: warehouseout.ID,
  186. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  187. PatientId: advice.PatientId,
  188. Ctime: time.Now().Unix(),
  189. Mtime: time.Now().Unix(),
  190. Status: 1,
  191. RecordTime: advice.RecordDate,
  192. OrgId: orgID,
  193. DrugId: advice.DrugId,
  194. Count: count,
  195. CountUnit: advice.PrescribingNumberUnit,
  196. WarehouseInfoId: warehouse.ID,
  197. StorehouseId: storeConfig.DrugStorehouseOut,
  198. }
  199. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  200. if errTwo != nil {
  201. return errTwo
  202. }
  203. var maxNumber int64 = 0
  204. var minNumber int64 = 0
  205. //var stock_max_number int64 = 0
  206. //stock_max_number = warehouse.StockMaxNumber
  207. maxNumber = deliver_number / drup.MinNumber
  208. minNumber = deliver_number % drup.MinNumber
  209. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  210. minNumber = maxNumber
  211. }
  212. if drup.MaxUnit != drup.MinUnit {
  213. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  214. return errors.New("库存数量不足")
  215. }
  216. }
  217. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  218. if warehouse.StockMaxNumber < 0 {
  219. warehouse.StockMaxNumber = 0
  220. }
  221. if warehouse.StockMinNumber < 0 {
  222. warehouse.StockMinNumber = 0
  223. }
  224. warehouse.Mtime = time.Now().Unix()
  225. if warehouse.StockMinNumber < minNumber {
  226. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  227. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  228. } else {
  229. if minNumber > 0 {
  230. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  231. }
  232. if minNumber == 0 && maxNumber != 1 {
  233. if warehouse.StockMinNumber > 0 {
  234. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  235. }
  236. }
  237. }
  238. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  239. if (warehouse.StockMinNumber - deliver_number) > 0 {
  240. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  241. //if warehouse.StockMinNumber == 0 {
  242. // warehouse.StockMaxNumber = stock_max_number
  243. //}
  244. }
  245. }
  246. if drup.MaxUnit != drup.MinUnit {
  247. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  248. return errors.New("库存数量不足")
  249. }
  250. }
  251. if warehouse.StockMinNumber <= 0 {
  252. warehouse.StockMinNumber = 0
  253. }
  254. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  255. //if(warehouse.StockMaxNumber > 0){
  256. // var sum_count int64
  257. // sum_count = maxNumber * drup.MinNumber
  258. // ModifyDrugReduceInformationSix(warehouse.DrugId,sum_count,warehouse.OrgId,warehouse.RetailPrice)
  259. //}
  260. //if(warehouse.StockMinNumber > 0){
  261. // ModifyDrugReduceInformationSix(warehouse.DrugId,minNumber,warehouse.OrgId,warehouse.RetailPrice)
  262. //}
  263. if errThree != nil {
  264. return errThree
  265. }
  266. return nil
  267. } else {
  268. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  269. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  270. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  271. WarehouseOutId: warehouseout.ID,
  272. Status: 1,
  273. Ctime: time.Now().Unix(),
  274. Remark: warehouse.Remark,
  275. OrgId: orgID,
  276. Type: 1,
  277. Manufacturer: warehouse.Manufacturer,
  278. Dealer: warehouse.Dealer,
  279. IsSys: 1,
  280. SysRecordTime: advice.RecordDate,
  281. DrugId: advice.DrugId,
  282. Number: warehouse.Number,
  283. BatchNumber: warehouse.BatchNumber,
  284. Price: warehouse.RetailPrice,
  285. CountUnit: drup.MinUnit,
  286. ProductDate: warehouse.ProductDate,
  287. ExpiryDate: warehouse.ExpiryDate,
  288. PatientId: advice.PatientId,
  289. Count: deliver_number,
  290. WarehouseInfoId: warehouse.ID,
  291. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  292. StorehouseId: storeConfig.DrugStorehouseOut,
  293. }
  294. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  295. if errOne != nil {
  296. return errOne
  297. }
  298. details := &models.DrugAutomaticReduceDetail{
  299. WarehouseOutId: warehouseout.ID,
  300. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  301. PatientId: advice.PatientId,
  302. Ctime: time.Now().Unix(),
  303. Mtime: time.Now().Unix(),
  304. Status: 1,
  305. RecordTime: advice.RecordDate,
  306. OrgId: orgID,
  307. DrugId: advice.DrugId,
  308. Count: deliver_number,
  309. CountUnit: drup.MinUnit,
  310. WarehouseInfoId: warehouse.ID,
  311. StorehouseId: storeConfig.DrugStorehouseOut,
  312. }
  313. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  314. if errTwo != nil {
  315. return errTwo
  316. }
  317. info := models.XtDrugWarehouseInfo{
  318. ID: warehouse.ID,
  319. WarehousingId: warehouse.WarehousingId,
  320. DrugId: warehouse.DrugId,
  321. Number: warehouse.Number,
  322. ProductDate: warehouse.ProductDate,
  323. ExpiryDate: warehouse.ExpiryDate,
  324. WarehousingCount: warehouse.WarehousingCount,
  325. Price: warehouse.RetailPrice,
  326. TotalPrice: warehouse.TotalPrice,
  327. Dealer: warehouse.Dealer,
  328. Manufacturer: warehouse.Manufacturer,
  329. Remark: warehouse.Remark,
  330. Ctime: warehouse.Ctime,
  331. Mtime: warehouse.Mtime,
  332. Status: 1,
  333. OrgId: warehouse.OrgId,
  334. IsReturn: warehouse.IsReturn,
  335. WarehousingOrder: warehouse.WarehousingOrder,
  336. Type: warehouse.Type,
  337. RetailPrice: warehouse.RetailPrice,
  338. RetailTotalPrice: warehouse.RetailPrice,
  339. StockMaxNumber: 0,
  340. StockMinNumber: 0,
  341. BatchNumber: warehouse.BatchNumber,
  342. MaxUnit: warehouse.MaxUnit,
  343. WarehousingInfoId: warehouse.WarehousingInfoId,
  344. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  345. }
  346. //扣减库存
  347. errThree := UpDateDrugWarehouseInfoByStock(&info)
  348. //if(warehouse.StockMaxNumber > 0){
  349. // var sum_count int64
  350. // sum_count = warehouse.StockMaxNumber * drup.MinNumber
  351. // ModifyDrugReduceInformationSix(warehouse.DrugId,sum_count,warehouse.OrgId,warehouse.RetailPrice)
  352. //}
  353. //if(warehouse.StockMinNumber > 0){
  354. // ModifyDrugReduceInformationSix(warehouse.DrugId,warehouse.StockMinNumber,warehouse.OrgId,warehouse.RetailPrice)
  355. //}
  356. if errThree != nil {
  357. return errThree
  358. }
  359. drugflow := models.DrugFlow{
  360. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  361. WarehouseOutId: warehouseout.ID,
  362. DrugId: advice.DrugId,
  363. Number: warehouse.Number,
  364. ProductDate: warehouse.ProductDate,
  365. ExpireDate: warehouse.ExpiryDate,
  366. Count: stock_number,
  367. Price: warehouse.RetailPrice,
  368. Status: 1,
  369. Ctime: time.Now().Unix(),
  370. UserOrgId: orgID,
  371. Manufacturer: warehouse.Manufacturer,
  372. Dealer: warehouse.Dealer,
  373. BatchNumber: warehouse.BatchNumber,
  374. MaxUnit: drup.MinUnit,
  375. ConsumableType: 3,
  376. IsEdit: 1,
  377. Creator: advice.ExecutionStaff,
  378. IsSys: 1,
  379. PatientId: advice.PatientId,
  380. WarehousingDetailId: warehouse.ID,
  381. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  382. StorehouseId: storeConfig.DrugStorehouseOut,
  383. }
  384. CreateDrugFlowOne(drugflow)
  385. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  386. prescribingNumber_two_temp := deliver_number - stock_number
  387. overPlusNumber := float64(prescribingNumber_two_temp)
  388. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  389. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  390. NewDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  391. }
  392. return
  393. }
  394. // 药品出库
  395. func HisDrugsDelivery(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo) (err error) {
  396. // 1.判断药品是否来自专用字典的药品库
  397. // 2.判断当天当前机构有没有创建出库单,没有则创建
  398. // 3.创建出库流程
  399. // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  400. isHasWay := false
  401. record_time := int64(0)
  402. isHasWay = true
  403. record_time = advice.RecordDate
  404. if isHasWay {
  405. //判断当天当前机构有没有创建出库单,没有则创建
  406. out, err := FindDrugStockOutByIsSys(orgID, 1, record_time)
  407. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  408. if err == gorm.ErrRecordNotFound {
  409. timeStr := time.Now().Format("2006-01-02")
  410. timeArr := strings.Split(timeStr, "-")
  411. total, _ := FindAllDrugWarehouseOut(orgID)
  412. total = total + 1
  413. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  414. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  415. number = number + total
  416. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  417. warehouseOut := models.DrugWarehouseOut{
  418. WarehouseOutOrderNumber: warehousing_out_order,
  419. OperationTime: time.Now().Unix(),
  420. OrgId: orgID,
  421. Creater: creater,
  422. Ctime: time.Now().Unix(),
  423. Status: 1,
  424. WarehouseOutTime: record_time,
  425. Dealer: 0,
  426. Manufacturer: 0,
  427. Type: 1,
  428. IsSys: 1,
  429. StorehouseId: houseConfig.DrugStorehouseOut,
  430. IsCheck: 1,
  431. }
  432. err := AddSigleDrugWarehouseOut(&warehouseOut)
  433. if err != nil {
  434. utils.TraceLog("创建出库单失败 err = %v", err)
  435. return err
  436. } else {
  437. out = warehouseOut
  438. }
  439. }
  440. // 出库流程
  441. // 1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  442. drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
  443. if drup.ID > 0 {
  444. prescribingNumber := advice.PrescribingNumber
  445. HisDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice)
  446. } else {
  447. return errors.New("药品信息不存在")
  448. }
  449. }
  450. return
  451. }
  452. // 药品出库 递归方式
  453. func HisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  454. fmt.Println("123322332323332233223", prescribingNumber)
  455. fmt.Println("233o3o22o332oo32o32o23o32o", advice.PrescribingNumberUnit)
  456. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  457. var deliver_number int64 = 0
  458. var stock_number int64 = 0
  459. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  460. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  461. if advice.PrescribingNumberUnit == drup.MaxUnit {
  462. deliver_number = count * drup.MinNumber
  463. } else {
  464. deliver_number = count
  465. }
  466. fmt.Println("c出库数量", deliver_number)
  467. var min_price float64
  468. if advice.PrescribingNumberUnit == drup.MaxUnit {
  469. min_price = drup.RetailPrice
  470. }
  471. if advice.PrescribingNumberUnit == drup.MinUnit {
  472. min_price = drup.MinPrice
  473. }
  474. if advice.PrescribingNumberUnit != drup.MaxUnit && advice.PrescribingNumberUnit != drup.MinUnit {
  475. min_price = drup.RetailPrice
  476. }
  477. fmt.Println(min_price)
  478. // 根据先进先出原则,查询最先入库的批次,进行出库
  479. // 如果没有对应的库存,则报错
  480. //查询默认仓库
  481. storeConfig, _ := GetAllStoreHouseConfig(orgID)
  482. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  483. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  484. var stockMax int64
  485. var stockMin int64
  486. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  487. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  488. fmt.Println("stockMax", stockMax)
  489. fmt.Println("stockMin", stockMin)
  490. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  491. UpdateMinNumber(lastWarehouse.ID, stockMin)
  492. }
  493. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  494. if err != nil {
  495. return err
  496. }
  497. // 将该批次的剩余库存数量转换为拆零数量
  498. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  499. fmt.Println("查询剩余库存", stock_number)
  500. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  501. if stock_number >= deliver_number {
  502. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  503. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  504. WarehouseOutId: warehouseout.ID,
  505. Status: 1,
  506. Ctime: time.Now().Unix(),
  507. Remark: warehouse.Remark,
  508. OrgId: orgID,
  509. Type: 1,
  510. Manufacturer: warehouse.Manufacturer,
  511. Dealer: warehouse.Dealer,
  512. IsSys: 1,
  513. SysRecordTime: advice.RecordDate,
  514. DrugId: advice.DrugId,
  515. Number: warehouse.Number,
  516. BatchNumber: warehouse.BatchNumber,
  517. Price: warehouse.RetailPrice,
  518. CountUnit: drup.MinUnit,
  519. RetailPrice: warehouse.RetailPrice,
  520. ProductDate: warehouse.ProductDate,
  521. ExpiryDate: warehouse.ExpiryDate,
  522. PatientId: advice.PatientId,
  523. WarehouseInfoId: warehouse.ID,
  524. Count: deliver_number,
  525. AdviceId: advice.ID,
  526. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  527. StorehouseId: storeConfig.DrugStorehouseOut,
  528. IsCheck: 1,
  529. }
  530. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  531. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  532. if errOne != nil {
  533. return errOne
  534. }
  535. drugflow := models.DrugFlow{
  536. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  537. WarehouseOutId: warehouseout.ID,
  538. DrugId: advice.DrugId,
  539. Number: warehouse.Number,
  540. ProductDate: warehouse.ProductDate,
  541. ExpireDate: warehouse.ExpiryDate,
  542. Count: deliver_number,
  543. Price: warehouse.RetailPrice,
  544. Status: 1,
  545. Ctime: time.Now().Unix(),
  546. UserOrgId: orgID,
  547. Manufacturer: warehouse.Manufacturer,
  548. Dealer: warehouse.Dealer,
  549. BatchNumber: warehouse.BatchNumber,
  550. MaxUnit: drup.MinUnit,
  551. ConsumableType: 3,
  552. IsEdit: 1,
  553. Creator: advice.ExecutionStaff,
  554. IsSys: 1,
  555. PatientId: advice.PatientId,
  556. SystemTime: advice.AdviceDate,
  557. WarehousingDetailId: warehouse.ID,
  558. AdviceId: advice.ID,
  559. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  560. StorehouseId: storeConfig.DrugStorehouseOut,
  561. WarehouseOutDetailId: lastDrugOutInfo.ID,
  562. }
  563. CreateDrugFlowOne(drugflow)
  564. details := &models.DrugAutomaticReduceDetail{
  565. WarehouseOutId: warehouseout.ID,
  566. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  567. PatientId: advice.PatientId,
  568. Ctime: time.Now().Unix(),
  569. Mtime: time.Now().Unix(),
  570. Status: 1,
  571. RecordTime: advice.RecordDate,
  572. OrgId: orgID,
  573. DrugId: advice.DrugId,
  574. Count: deliver_number,
  575. CountUnit: drup.MinUnit,
  576. WarehouseInfoId: warehouse.ID,
  577. AdviceId: advice.ID,
  578. StorehouseId: storeConfig.DrugStorehouseOut,
  579. }
  580. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  581. if errTwo != nil {
  582. return errTwo
  583. }
  584. // 出库完成后,要减去对应批次的库存数量
  585. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  586. var maxNumber int64 = 0
  587. var minNumber int64 = 0
  588. maxNumber = deliver_number / drup.MinNumber
  589. minNumber = deliver_number % drup.MinNumber
  590. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  591. minNumber = maxNumber
  592. }
  593. if drup.MaxUnit != drup.MinUnit {
  594. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  595. return errors.New("库存数量不足")
  596. }
  597. }
  598. fmt.Println("查询库存", warehouse.StockMaxNumber)
  599. fmt.Println("扣减库存我的", maxNumber)
  600. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  601. fmt.Println("剩余库存我的", warehouse.StockMaxNumber)
  602. if warehouse.StockMaxNumber < 0 {
  603. warehouse.StockMaxNumber = 0
  604. }
  605. if warehouse.StockMinNumber < 0 {
  606. warehouse.StockMinNumber = 0
  607. }
  608. warehouse.Mtime = time.Now().Unix()
  609. if warehouse.StockMinNumber < minNumber {
  610. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  611. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  612. } else {
  613. if minNumber > 0 {
  614. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  615. }
  616. if minNumber == 0 && maxNumber != 1 {
  617. if warehouse.StockMinNumber > 0 {
  618. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  619. }
  620. }
  621. }
  622. fmt.Println("maxNu目标而3232323232232323", maxNumber)
  623. fmt.Println("minNumber2222222", minNumber)
  624. fmt.Println("deliver_number23233223322332332", deliver_number)
  625. fmt.Println("warehouse.StockMinNumber", warehouse.StockMinNumber)
  626. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  627. if (warehouse.StockMinNumber - deliver_number) > 0 {
  628. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  629. //if warehouse.StockMinNumber == 0 {
  630. // warehouse.StockMaxNumber = stock_max_number
  631. //}
  632. }
  633. }
  634. if drup.MaxUnit != drup.MinUnit {
  635. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  636. return errors.New("库存数量不足")
  637. }
  638. }
  639. if warehouse.StockMinNumber <= 0 {
  640. warehouse.StockMinNumber = 0
  641. }
  642. fmt.Println("stock_max_233232323223323233223323223", warehouse.StockMinNumber)
  643. fmt.Println("stock_max_233232323223323233223323223", warehouse.StockMaxNumber)
  644. //扣减库存232332332332
  645. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  646. if errThree != nil {
  647. return errThree
  648. }
  649. return nil
  650. } else {
  651. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  652. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  653. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  654. WarehouseOutId: warehouseout.ID,
  655. Status: 1,
  656. Ctime: time.Now().Unix(),
  657. Remark: warehouse.Remark,
  658. OrgId: orgID,
  659. Type: 1,
  660. Manufacturer: warehouse.Manufacturer,
  661. Dealer: warehouse.Dealer,
  662. IsSys: 1,
  663. SysRecordTime: advice.RecordDate,
  664. DrugId: advice.DrugId,
  665. Number: warehouse.Number,
  666. BatchNumber: warehouse.BatchNumber,
  667. Price: warehouse.RetailPrice,
  668. RetailPrice: warehouse.RetailPrice,
  669. ProductDate: warehouse.ProductDate,
  670. ExpiryDate: warehouse.ExpiryDate,
  671. PatientId: advice.PatientId,
  672. WarehouseInfoId: warehouse.ID,
  673. Count: stock_number,
  674. CountUnit: drup.MinUnit,
  675. AdviceId: advice.ID,
  676. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  677. StorehouseId: storeConfig.DrugStorehouseOut,
  678. IsCheck: 1,
  679. }
  680. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  681. if errOne != nil {
  682. return errOne
  683. }
  684. drugflow := models.DrugFlow{
  685. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  686. WarehouseOutId: warehouseout.ID,
  687. DrugId: advice.DrugId,
  688. Number: warehouse.Number,
  689. ProductDate: warehouse.ProductDate,
  690. ExpireDate: warehouse.ExpiryDate,
  691. Count: stock_number,
  692. Price: warehouse.RetailPrice,
  693. Status: 1,
  694. Ctime: time.Now().Unix(),
  695. UserOrgId: orgID,
  696. Manufacturer: warehouse.Manufacturer,
  697. Dealer: warehouse.Dealer,
  698. BatchNumber: warehouse.BatchNumber,
  699. MaxUnit: drup.MinUnit,
  700. ConsumableType: 3,
  701. IsEdit: 1,
  702. Creator: advice.ExecutionStaff,
  703. IsSys: 1,
  704. PatientId: advice.PatientId,
  705. SystemTime: advice.AdviceDate,
  706. WarehousingDetailId: warehouse.ID,
  707. AdviceId: advice.ID,
  708. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  709. StorehouseId: storeConfig.DrugStorehouseOut,
  710. WarehouseOutDetailId: lastWarehouse.ID,
  711. }
  712. CreateDrugFlowOne(drugflow)
  713. //查询是否存在数据
  714. details := &models.DrugAutomaticReduceDetail{
  715. WarehouseOutId: warehouseout.ID,
  716. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  717. PatientId: advice.PatientId,
  718. Ctime: time.Now().Unix(),
  719. Mtime: time.Now().Unix(),
  720. Status: 1,
  721. RecordTime: advice.RecordDate,
  722. OrgId: orgID,
  723. DrugId: advice.DrugId,
  724. Count: stock_number,
  725. CountUnit: drup.MinUnit,
  726. WarehouseInfoId: warehouse.ID,
  727. AdviceId: advice.ID,
  728. StorehouseId: storeConfig.DrugStorehouseOut,
  729. }
  730. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  731. if errTwo != nil {
  732. return errTwo
  733. }
  734. info := models.XtDrugWarehouseInfo{
  735. ID: warehouse.ID,
  736. WarehousingId: warehouse.WarehousingId,
  737. DrugId: warehouse.DrugId,
  738. Number: warehouse.Number,
  739. ProductDate: warehouse.ProductDate,
  740. ExpiryDate: warehouse.ExpiryDate,
  741. WarehousingCount: warehouse.WarehousingCount,
  742. Price: warehouse.Price,
  743. TotalPrice: warehouse.TotalPrice,
  744. Dealer: warehouse.Dealer,
  745. Manufacturer: warehouse.Manufacturer,
  746. Remark: warehouse.Remark,
  747. Ctime: warehouse.Ctime,
  748. Mtime: warehouse.Mtime,
  749. Status: 1,
  750. OrgId: warehouse.OrgId,
  751. IsReturn: warehouse.IsReturn,
  752. WarehousingOrder: warehouse.WarehousingOrder,
  753. Type: warehouse.Type,
  754. RetailPrice: warehouse.RetailPrice,
  755. RetailTotalPrice: warehouse.RetailPrice,
  756. StockMaxNumber: 0,
  757. StockMinNumber: 0,
  758. BatchNumber: warehouse.BatchNumber,
  759. MaxUnit: warehouse.MaxUnit,
  760. MinUnit: warehouse.MinUnit,
  761. WarehousingInfoId: warehouse.WarehousingInfoId,
  762. StorehouseId: storeConfig.DrugStorehouseOut,
  763. IsCheck: 1,
  764. }
  765. errThree := UpDateDrugWarehouseInfoByStock(&info)
  766. if errThree != nil {
  767. return errThree
  768. }
  769. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  770. prescribingNumber_two_temp := deliver_number - stock_number
  771. overPlusNumber := float64(prescribingNumber_two_temp)
  772. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  773. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  774. NewHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  775. }
  776. return
  777. }
  778. // 药品出库 递归方式
  779. func AutoDrugDeliverInfo(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  780. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  781. var deliver_number int64 = 0
  782. var stock_number int64 = 0
  783. if advice.CountUnit == drup.MaxUnit {
  784. deliver_number = prescribingNumber * drup.MinNumber
  785. } else {
  786. deliver_number = prescribingNumber
  787. }
  788. // 根据先进先出原则,查询最先入库的批次,进行出库
  789. // 如果没有对应的库存,则报错
  790. //开启事物
  791. storeConfig, _ := GetAllStoreHouseConfig(orgID)
  792. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  793. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  794. var stockMax int64
  795. var stockMin int64
  796. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  797. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  798. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  799. UpdateMinNumber(lastWarehouse.ID, stockMin)
  800. }
  801. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  802. if err != nil {
  803. return err
  804. }
  805. // 将该批次的剩余库存数量转换为拆零数量
  806. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  807. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  808. if stock_number >= deliver_number {
  809. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  810. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  811. WarehouseOutId: warehouseout.ID,
  812. Status: 1,
  813. Ctime: time.Now().Unix(),
  814. Remark: advice.Remark,
  815. OrgId: orgID,
  816. Type: 1,
  817. Manufacturer: advice.Manufacturer,
  818. Dealer: warehouse.Dealer,
  819. IsSys: 0,
  820. SysRecordTime: advice.Ctime,
  821. DrugId: advice.DrugId,
  822. ExpiryDate: advice.ExpiryDate,
  823. ProductDate: advice.ProductDate,
  824. Number: advice.Number,
  825. BatchNumber: warehouse.BatchNumber,
  826. Count: deliver_number,
  827. RetailPrice: advice.RetailPrice,
  828. Price: warehouse.RetailPrice,
  829. WarehouseInfoId: warehouse.ID,
  830. CountUnit: drup.MinUnit,
  831. AdviceId: advice.ID,
  832. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  833. StorehouseId: advice.StorehouseId,
  834. AdminUserId: advice.AdminUserId,
  835. }
  836. warehouseOutInfo.Count = prescribingNumber
  837. warehouseOutInfo.CountUnit = advice.CountUnit
  838. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  839. if errOne != nil {
  840. return errOne
  841. }
  842. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  843. drugflow := models.DrugFlow{
  844. WarehouseOutId: warehouseout.ID,
  845. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  846. DrugId: advice.DrugId,
  847. Number: warehouse.Number,
  848. ProductDate: advice.ProductDate,
  849. ExpireDate: advice.ExpiryDate,
  850. Count: deliver_number,
  851. Price: warehouse.RetailPrice,
  852. Status: 1,
  853. Ctime: time.Now().Unix(),
  854. UserOrgId: advice.OrgId,
  855. Manufacturer: advice.Manufacturer,
  856. Dealer: advice.Dealer,
  857. BatchNumber: warehouse.BatchNumber,
  858. MaxUnit: drup.MinUnit,
  859. ConsumableType: 2,
  860. IsEdit: 1,
  861. Creator: 0,
  862. IsSys: 0,
  863. WarehouseOutDetailId: drugWareInfo.ID,
  864. AdviceId: advice.ID,
  865. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  866. StorehouseId: advice.StorehouseId,
  867. AdminUserId: advice.AdminUserId,
  868. }
  869. CreateDrugFlowOne(drugflow)
  870. // 出库完成后,要减去对应批次的库存数量
  871. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  872. var maxNumber int64 = 0
  873. var minNumber int64 = 0
  874. if advice.CountUnit == drup.MinUnit {
  875. maxNumber = prescribingNumber / drup.MinNumber
  876. minNumber = prescribingNumber % drup.MinNumber
  877. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  878. minNumber = maxNumber
  879. }
  880. } else {
  881. maxNumber = prescribingNumber
  882. }
  883. if warehouse.StockMaxNumber < maxNumber {
  884. return errors.New("库存数量不足")
  885. }
  886. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  887. warehouse.Mtime = time.Now().Unix()
  888. if warehouse.StockMinNumber < minNumber {
  889. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  890. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  891. } else {
  892. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  893. }
  894. if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
  895. warehouse.StockMinNumber = 0
  896. }
  897. if warehouse.StockMaxNumber < 0 {
  898. return errors.New("库存数量不足")
  899. }
  900. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  901. if errThree != nil {
  902. return errThree
  903. }
  904. //查询默认仓库
  905. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  906. //查询默认仓库剩余多少库存
  907. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  908. var sum_count int64
  909. var sum_in_count int64
  910. for _, it := range list {
  911. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  912. if it.MaxUnit == baseDrug.MaxUnit {
  913. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  914. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  915. }
  916. sum_count += it.StockMaxNumber + it.StockMinNumber
  917. sum_in_count += it.WarehousingCount
  918. }
  919. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  920. return nil
  921. } else {
  922. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  923. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  924. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  925. WarehouseOutId: warehouseout.ID,
  926. Status: 1,
  927. Ctime: time.Now().Unix(),
  928. Remark: advice.Remark,
  929. OrgId: orgID,
  930. Type: 1,
  931. Manufacturer: advice.Manufacturer,
  932. Dealer: warehouse.Dealer,
  933. IsSys: 0,
  934. SysRecordTime: advice.Ctime,
  935. DrugId: advice.DrugId,
  936. ExpiryDate: warehouse.ExpiryDate,
  937. ProductDate: warehouse.ProductDate,
  938. Number: warehouse.Number,
  939. BatchNumber: warehouse.BatchNumber,
  940. Count: stock_number,
  941. RetailPrice: advice.RetailPrice,
  942. Price: warehouse.RetailPrice,
  943. WarehouseInfoId: warehouse.ID,
  944. CountUnit: drup.MinUnit,
  945. AdviceId: advice.ID,
  946. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  947. StorehouseId: advice.StorehouseId,
  948. AdminUserId: advice.AdminUserId,
  949. }
  950. warehouseOutInfo.Count = stock_number
  951. //warehouseOutInfo.CountUnit = advice.CountUnit
  952. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  953. if errOne != nil {
  954. return errOne
  955. }
  956. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  957. drugflow := models.DrugFlow{
  958. WarehouseOutId: warehouseout.ID,
  959. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  960. DrugId: advice.DrugId,
  961. Number: warehouse.Number,
  962. ProductDate: advice.ProductDate,
  963. ExpireDate: advice.ExpiryDate,
  964. Count: stock_number,
  965. Price: warehouse.RetailPrice,
  966. Status: 1,
  967. Ctime: time.Now().Unix(),
  968. UserOrgId: advice.OrgId,
  969. Manufacturer: advice.Manufacturer,
  970. Dealer: advice.Dealer,
  971. BatchNumber: warehouse.BatchNumber,
  972. MaxUnit: drup.MinUnit,
  973. ConsumableType: 2,
  974. IsEdit: 1,
  975. Creator: 0,
  976. IsSys: 0,
  977. WarehouseOutDetailId: drugWareInfo.ID,
  978. AdviceId: advice.ID,
  979. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  980. StorehouseId: advice.StorehouseId,
  981. AdminUserId: advice.AdminUserId,
  982. }
  983. CreateDrugFlowOne(drugflow)
  984. // 出库完成后,要将该批次库存清零
  985. //扣减库存
  986. warehouse.StockMaxNumber = 0
  987. warehouse.StockMinNumber = 0
  988. warehouse.Mtime = time.Now().Unix()
  989. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  990. //查询默认仓库
  991. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  992. //查询默认仓库剩余多少库存
  993. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  994. var sum_count int64
  995. var sum_in_count int64
  996. for _, it := range list {
  997. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  998. if it.MaxUnit == baseDrug.MaxUnit {
  999. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  1000. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  1001. sum_count += it.StockMaxNumber + it.StockMinNumber
  1002. sum_in_count += it.WarehousingCount
  1003. }
  1004. }
  1005. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  1006. if errThree != nil {
  1007. return errThree
  1008. }
  1009. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1010. prescribingNumber_two_temp := deliver_number - stock_number
  1011. advice.CountUnit = drup.MinUnit
  1012. AutoDrugDeliverInfo(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  1013. }
  1014. return
  1015. }
  1016. // 耗材出库
  1017. func ConsumablesDeliveryTotal(orgID int64, patient_id int64, record_time int64, goods []*models.DialysisBeforePrepareGoods, goodOne []*models.NewDialysisBeforePrepareGoods, creator int64) (err error) {
  1018. //查询该患者当天已经出库的耗材信息
  1019. goods_yc, _ := FindConsumablesByDateTwo(orgID, patient_id, record_time)
  1020. // 和新请求的出库数据进行对比,分出那些是继续出库的,那些是需要删除出库的
  1021. for i := len(goods_yc) - 1; i >= 0; i-- {
  1022. goods_yc_temp := goods_yc[i]
  1023. for j := len(goods) - 1; j >= 0; j-- {
  1024. goods_temp := goods[j]
  1025. // 已经出库和新请求出库都存在该耗材,则判断出库数量,分成是继续出库,还是删除出库
  1026. if goods_yc_temp.GoodTypeId == goods_temp.GoodTypeId && goods_yc_temp.GoodId == goods_temp.GoodId {
  1027. // 已经出库和新请求出库的出库数量一致,则清除两个结构体里的数据(既不出库,也不删除出库)
  1028. if goods_yc_temp.Count == goods_temp.Count {
  1029. goods_yc = append(goods_yc[:i], goods_yc[i+1:]...)
  1030. goods = append(goods[:j], goods[j+1:]...)
  1031. break
  1032. }
  1033. // 如果已经出库的数量 大于 新请求出库的数量,则代表需要删除出库
  1034. if goods_yc_temp.Count > goods_temp.Count {
  1035. temp_count := goods_yc_temp.Count - goods_temp.Count
  1036. goods_yc[i].Count = temp_count
  1037. goods = append(goods[:j], goods[j+1:]...)
  1038. break
  1039. }
  1040. // 如果已经出库的数量 小于 新请求出库的梳理,则代表需要增加出库
  1041. if goods_yc_temp.Count < goods_temp.Count {
  1042. temp_count := goods_temp.Count - goods_yc_temp.Count
  1043. goods[j].Count = temp_count
  1044. goods_yc = append(goods_yc[:i], goods_yc[i+1:]...)
  1045. break
  1046. }
  1047. }
  1048. }
  1049. }
  1050. // goods_yc 这个数据就是需要已经出库了,但是现在需要删除出库的耗材数据
  1051. // goods 这个数据就是需要出库的耗材的数据(新增的数据)
  1052. if len(goods) > 0 {
  1053. out, err := FindStockOutByIsSys(orgID, 1, record_time)
  1054. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  1055. if err == gorm.ErrRecordNotFound {
  1056. //没有记录,则创建出库单
  1057. timeStr := time.Now().Format("2006-01-02")
  1058. timeArr := strings.Split(timeStr, "-")
  1059. total, _ := FindAllWarehouseOut(orgID)
  1060. total = total + 1
  1061. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  1062. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  1063. number = number + total
  1064. warehousing_out_order = "CKD" + strconv.FormatInt(number, 10)
  1065. warehouseOut := models.WarehouseOut{
  1066. WarehouseOutOrderNumber: warehousing_out_order,
  1067. OperationTime: time.Now().Unix(),
  1068. OrgId: orgID,
  1069. Creater: creator,
  1070. Ctime: time.Now().Unix(),
  1071. Status: 1,
  1072. WarehouseOutTime: record_time,
  1073. Dealer: 0,
  1074. Manufacturer: 0,
  1075. Type: 1,
  1076. IsSys: 1,
  1077. StorehouseId: houseConfig.StorehouseOutInfo,
  1078. IsCheck: 1,
  1079. }
  1080. err := AddSigleWarehouseOut(&warehouseOut)
  1081. if err != nil {
  1082. utils.TraceLog("创建出库单失败 err = %v", err)
  1083. return err
  1084. } else {
  1085. out = warehouseOut
  1086. }
  1087. }
  1088. for _, item := range goods {
  1089. var newCount int64 = 0
  1090. for _, it := range goodOne {
  1091. if item.GoodTypeId == it.GoodTypeId && item.GoodId == it.GoodId {
  1092. newCount = it.Count
  1093. }
  1094. }
  1095. prepare := models.DialysisBeforePrepare{
  1096. GoodTypeId: item.GoodTypeId,
  1097. GoodId: item.GoodId,
  1098. Count: item.Count,
  1099. ProjectId: item.ProjectId,
  1100. StorehouseId: houseConfig.StorehouseOutInfo,
  1101. }
  1102. ConsumablesDelivery(orgID, patient_id, record_time, &prepare, &out, newCount)
  1103. }
  1104. }
  1105. if len(goods_yc) > 0 {
  1106. for _, good_yc := range goods_yc {
  1107. out, _ := FindStockOutByIsSys(orgID, 1, record_time)
  1108. ConsumablesDeliveryDelete(orgID, patient_id, record_time, good_yc, &out)
  1109. }
  1110. }
  1111. return nil
  1112. }
  1113. //耗材出库
  1114. func ConsumablesDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, count int64) (err error) {
  1115. //开事务
  1116. //tx := XTWriteDB().Begin()
  1117. var deliver_number int64 = 0
  1118. var stock_number int64 = 0
  1119. var maxNumber int64 = 0
  1120. deliver_number = goods.Count
  1121. // 根据先进先出原则,查询最先入库的批次,进行出库
  1122. // 如果没有对应的库存,则报错
  1123. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  1124. if err != nil {
  1125. return errors.New("库存数量不足")
  1126. }
  1127. stock_number = warehouse.StockCount
  1128. //获取耗材信息
  1129. info, _ := GetGoodInformationByGoodId(goods.GoodId)
  1130. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1131. if stock_number >= deliver_number {
  1132. warehouseOutInfo := &models.WarehouseOutInfo{
  1133. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1134. WarehouseOutId: warehouseOut.ID,
  1135. WarehouseInfotId: warehouse.ID,
  1136. Status: 1,
  1137. Ctime: time.Now().Unix(),
  1138. Remark: warehouse.Remark,
  1139. OrgId: orgID,
  1140. Type: 1,
  1141. Manufacturer: warehouse.Manufacturer,
  1142. Dealer: warehouse.Dealer,
  1143. IsSys: 1,
  1144. SysRecordTime: record_time,
  1145. GoodTypeId: goods.GoodTypeId,
  1146. GoodId: goods.GoodId,
  1147. PatientId: patient_id,
  1148. Number: warehouse.Number,
  1149. LicenseNumber: warehouse.LicenseNumber,
  1150. Price: warehouse.PackingPrice,
  1151. ExpiryDate: warehouse.ExpiryDate,
  1152. ProductDate: warehouse.ProductDate,
  1153. ProjectId: goods.ProjectId,
  1154. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1155. StorehouseId: goods.StorehouseId,
  1156. IsCheck: 1,
  1157. }
  1158. warehouseOutInfo.Count = count
  1159. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time)
  1160. if errcodes == gorm.ErrRecordNotFound {
  1161. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  1162. if errOne != nil {
  1163. return errOne
  1164. }
  1165. } else if errcodes == nil {
  1166. UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, patient_id, record_time)
  1167. }
  1168. details := models.BloodAutomaticReduceDetail{
  1169. WarehouseOutId: warehouseOutInfo.ID,
  1170. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  1171. PatientId: patient_id,
  1172. Ctime: time.Now().Unix(),
  1173. Mtime: time.Now().Unix(),
  1174. Status: 1,
  1175. RecordTime: record_time,
  1176. OrgId: orgID,
  1177. GoodId: goods.GoodId,
  1178. GoodTypeId: goods.GoodTypeId,
  1179. Count: count,
  1180. ProjectId: goods.ProjectId,
  1181. StorehouseId: goods.StorehouseId,
  1182. }
  1183. //查询当天耗材是否已经存在数据
  1184. _, errcode := GetAutoMaticReduceDetail(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  1185. if errcode == gorm.ErrRecordNotFound {
  1186. errTwo := CreateAutoReduceRecord(&details)
  1187. if errTwo != nil {
  1188. return errTwo
  1189. }
  1190. //插入库存流水表
  1191. flow := models.VmStockFlow{
  1192. WarehouseOutId: warehouseOut.ID,
  1193. WarehousingId: warehouse.ID,
  1194. GoodId: goods.GoodId,
  1195. Number: warehouse.Number,
  1196. ProductDate: warehouse.ProductDate,
  1197. ExpireDate: warehouse.ExpiryDate,
  1198. Count: deliver_number,
  1199. Price: warehouse.PackingPrice,
  1200. Status: 1,
  1201. Ctime: time.Now().Unix(),
  1202. UserOrgId: orgID,
  1203. Manufacturer: info.Manufacturer,
  1204. Dealer: info.Dealer,
  1205. LicenseNumber: warehouse.LicenseNumber,
  1206. IsEdit: 2,
  1207. Creator: warehouseOut.Creater,
  1208. SystemTime: record_time,
  1209. ConsumableType: 3,
  1210. WarehouseOutDetailId: warehouseOutInfo.ID,
  1211. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1212. IsSys: 1,
  1213. PatientId: patient_id,
  1214. ProjectId: goods.ProjectId,
  1215. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1216. StorehouseId: goods.StorehouseId,
  1217. }
  1218. //查询流水是否存在
  1219. exsit, errflow := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  1220. if errflow == gorm.ErrRecordNotFound {
  1221. errThre := CreateStockFlowOne(flow)
  1222. if errThre != nil {
  1223. return errThre
  1224. }
  1225. } else if errflow == nil {
  1226. flow := models.VmStockFlow{
  1227. ID: exsit.ID,
  1228. WarehouseOutId: warehouseOut.ID,
  1229. WarehousingId: warehouse.ID,
  1230. GoodId: goods.GoodId,
  1231. Number: warehouse.Number,
  1232. ProductDate: warehouse.ProductDate,
  1233. ExpireDate: warehouse.ExpiryDate,
  1234. Count: exsit.Count + goods.Count,
  1235. Price: warehouse.PackingPrice,
  1236. Status: 1,
  1237. Ctime: time.Now().Unix(),
  1238. UserOrgId: orgID,
  1239. Manufacturer: info.Manufacturer,
  1240. Dealer: info.Dealer,
  1241. LicenseNumber: warehouse.LicenseNumber,
  1242. IsEdit: 2,
  1243. Creator: warehouseOut.Creater,
  1244. SystemTime: record_time,
  1245. ConsumableType: 3,
  1246. WarehouseOutDetailId: warehouseOutInfo.ID,
  1247. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1248. IsSys: 1,
  1249. PatientId: patient_id,
  1250. ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  1251. ProjectId: goods.ProjectId,
  1252. StorehouseId: goods.StorehouseId,
  1253. }
  1254. errFour := UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  1255. if errFour != nil {
  1256. return errFour
  1257. }
  1258. }
  1259. } else if errcode == nil {
  1260. flow := models.VmStockFlow{
  1261. WarehouseOutId: warehouseOut.ID,
  1262. WarehousingId: warehouse.ID,
  1263. GoodId: goods.GoodId,
  1264. Number: warehouse.Number,
  1265. ProductDate: warehouse.ProductDate,
  1266. ExpireDate: warehouse.ExpiryDate,
  1267. Count: deliver_number,
  1268. Price: warehouse.PackingPrice,
  1269. Status: 1,
  1270. Ctime: time.Now().Unix(),
  1271. UserOrgId: orgID,
  1272. Manufacturer: info.Manufacturer,
  1273. Dealer: info.Dealer,
  1274. LicenseNumber: warehouse.LicenseNumber,
  1275. IsEdit: 2,
  1276. Creator: warehouseOut.Creater,
  1277. SystemTime: record_time,
  1278. ConsumableType: 3,
  1279. WarehouseOutDetailId: warehouseOutInfo.ID,
  1280. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1281. IsSys: 1,
  1282. PatientId: patient_id,
  1283. ProjectId: goods.ProjectId,
  1284. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1285. StorehouseId: goods.StorehouseId,
  1286. }
  1287. //查询流水是否存在
  1288. exsit, errflow := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  1289. if errflow == gorm.ErrRecordNotFound {
  1290. CreateStockFlowOne(flow)
  1291. DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  1292. CreateAutoReduceRecord(&details)
  1293. } else if errflow == nil {
  1294. flow := models.VmStockFlow{
  1295. ID: exsit.ID,
  1296. WarehouseOutId: warehouseOut.ID,
  1297. WarehousingId: warehouse.ID,
  1298. GoodId: goods.GoodId,
  1299. Number: warehouse.Number,
  1300. ProductDate: warehouse.ProductDate,
  1301. ExpireDate: warehouse.ExpiryDate,
  1302. Count: exsit.Count + goods.Count,
  1303. Price: warehouse.PackingPrice,
  1304. Status: 1,
  1305. Ctime: time.Now().Unix(),
  1306. UserOrgId: orgID,
  1307. Manufacturer: info.Manufacturer,
  1308. Dealer: info.Dealer,
  1309. LicenseNumber: warehouse.LicenseNumber,
  1310. IsEdit: 2,
  1311. Creator: warehouseOut.Creater,
  1312. SystemTime: record_time,
  1313. ConsumableType: 3,
  1314. WarehouseOutDetailId: warehouseOutInfo.ID,
  1315. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1316. IsSys: 1,
  1317. PatientId: patient_id,
  1318. ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  1319. ProjectId: goods.ProjectId,
  1320. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1321. StorehouseId: goods.StorehouseId,
  1322. }
  1323. //UpdatedStockFlow(flow)
  1324. UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  1325. DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  1326. CreateAutoReduceRecord(&details)
  1327. }
  1328. }
  1329. maxNumber = goods.Count
  1330. if warehouse.StockCount < maxNumber {
  1331. return errors.New("库存数量不足")
  1332. }
  1333. warehouse.StockCount = warehouse.StockCount - maxNumber
  1334. warehouse.Mtime = time.Now().Unix()
  1335. if warehouse.StockCount < 0 {
  1336. return errors.New("库存数量不足")
  1337. }
  1338. ModifyGoodReduceInformationSix(goods.GoodId, goods.Count, orgID, warehouse.PackingPrice)
  1339. errThree := UpDateWarehouseInfoByStock(&warehouse)
  1340. if errThree != nil {
  1341. return errThree
  1342. }
  1343. //if errThree != nil {
  1344. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  1345. // tx.Rollback()
  1346. //} else {
  1347. // tx.Commit()
  1348. //}
  1349. return nil
  1350. } else {
  1351. // 当刚批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  1352. warehouseOutInfo := &models.WarehouseOutInfo{
  1353. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1354. WarehouseOutId: warehouseOut.ID,
  1355. WarehouseInfotId: warehouse.ID,
  1356. Status: 1,
  1357. Ctime: time.Now().Unix(),
  1358. Remark: warehouse.Remark,
  1359. OrgId: orgID,
  1360. Type: 1,
  1361. Manufacturer: warehouse.Manufacturer,
  1362. Dealer: warehouse.Dealer,
  1363. IsSys: 1,
  1364. SysRecordTime: record_time,
  1365. GoodTypeId: goods.GoodTypeId,
  1366. GoodId: goods.GoodId,
  1367. PatientId: patient_id,
  1368. Number: warehouse.Number,
  1369. LicenseNumber: warehouse.LicenseNumber,
  1370. Price: warehouse.PackingPrice,
  1371. ExpiryDate: warehouse.ExpiryDate,
  1372. ProductDate: warehouse.ProductDate,
  1373. ProjectId: goods.ProjectId,
  1374. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1375. StorehouseId: goods.StorehouseId,
  1376. IsCheck: 1,
  1377. }
  1378. warehouseOutInfo.Count = stock_number
  1379. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, goods.PatientId, record_time)
  1380. if errcodes == gorm.ErrRecordNotFound {
  1381. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  1382. if errOne != nil {
  1383. return errOne
  1384. }
  1385. //插入库存流水表
  1386. flow := models.VmStockFlow{
  1387. WarehouseOutId: warehouseOut.ID,
  1388. WarehousingId: warehouse.ID,
  1389. GoodId: goods.GoodId,
  1390. Number: warehouse.Number,
  1391. ProductDate: warehouse.ProductDate,
  1392. ExpireDate: warehouse.ExpiryDate,
  1393. Count: stock_number,
  1394. Price: warehouse.PackingPrice,
  1395. Status: 1,
  1396. Ctime: time.Now().Unix(),
  1397. UserOrgId: orgID,
  1398. Manufacturer: info.Manufacturer,
  1399. Dealer: info.Dealer,
  1400. LicenseNumber: warehouse.LicenseNumber,
  1401. IsEdit: 2,
  1402. Creator: warehouseOut.Creater,
  1403. SystemTime: record_time,
  1404. ConsumableType: 3,
  1405. WarehouseOutDetailId: warehouseOutInfo.ID,
  1406. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1407. IsSys: 1,
  1408. PatientId: patient_id,
  1409. ProjectId: goods.ProjectId,
  1410. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1411. StorehouseId: goods.StorehouseId,
  1412. }
  1413. //查询流水是否存在
  1414. exsit, errflows := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  1415. if errflows == gorm.ErrRecordNotFound {
  1416. CreateStockFlowOne(flow)
  1417. } else if errflows == nil {
  1418. flow := models.VmStockFlow{
  1419. ID: exsit.ID,
  1420. WarehouseOutId: warehouseOut.ID,
  1421. WarehousingId: warehouse.ID,
  1422. GoodId: goods.GoodId,
  1423. Number: warehouse.Number,
  1424. ProductDate: warehouse.ProductDate,
  1425. ExpireDate: warehouse.ExpiryDate,
  1426. Count: exsit.Count + goods.Count,
  1427. Price: warehouse.PackingPrice,
  1428. Status: 1,
  1429. Ctime: time.Now().Unix(),
  1430. UserOrgId: orgID,
  1431. Manufacturer: info.Manufacturer,
  1432. Dealer: info.Dealer,
  1433. LicenseNumber: warehouse.LicenseNumber,
  1434. IsEdit: 2,
  1435. Creator: warehouseOut.Creater,
  1436. SystemTime: record_time,
  1437. ConsumableType: 3,
  1438. WarehouseOutDetailId: warehouseOutInfo.ID,
  1439. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1440. IsSys: 1,
  1441. PatientId: patient_id,
  1442. ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  1443. ProjectId: goods.ProjectId,
  1444. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1445. StorehouseId: goods.StorehouseId,
  1446. }
  1447. //UpdatedStockFlow(flow)
  1448. UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  1449. }
  1450. } else if errcodes == nil {
  1451. goods.Count = deliver_number - stock_number
  1452. //更新数量为 该批次剩余数量 + 还有未出的数量
  1453. warehouseOutInfo.Count = stock_number
  1454. UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, goods.PatientId, record_time)
  1455. //插入库存流水表
  1456. flow := models.VmStockFlow{
  1457. WarehouseOutId: warehouseOut.ID,
  1458. WarehousingId: warehouse.ID,
  1459. GoodId: goods.GoodId,
  1460. Number: warehouse.Number,
  1461. ProductDate: warehouse.ProductDate,
  1462. ExpireDate: warehouse.ExpiryDate,
  1463. Count: stock_number,
  1464. Price: warehouse.PackingPrice,
  1465. Status: 1,
  1466. Ctime: time.Now().Unix(),
  1467. UserOrgId: orgID,
  1468. Manufacturer: info.Manufacturer,
  1469. Dealer: info.Dealer,
  1470. LicenseNumber: warehouse.LicenseNumber,
  1471. IsEdit: 2,
  1472. Creator: warehouseOut.Creater,
  1473. SystemTime: record_time,
  1474. ConsumableType: 3,
  1475. WarehouseOutDetailId: warehouseOutInfo.ID,
  1476. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1477. IsSys: 1,
  1478. PatientId: patient_id,
  1479. ProjectId: goods.ProjectId,
  1480. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1481. StorehouseId: goods.StorehouseId,
  1482. }
  1483. //查询流水是否存在
  1484. exsit, errflows := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  1485. if errflows == gorm.ErrRecordNotFound {
  1486. CreateStockFlowOne(flow)
  1487. } else if errflows == nil {
  1488. flow := models.VmStockFlow{
  1489. ID: exsit.ID,
  1490. WarehouseOutId: warehouseOut.ID,
  1491. WarehousingId: warehouse.ID,
  1492. GoodId: goods.GoodId,
  1493. Number: warehouse.Number,
  1494. ProductDate: warehouse.ProductDate,
  1495. ExpireDate: warehouse.ExpiryDate,
  1496. Count: exsit.Count + goods.Count,
  1497. Price: warehouse.PackingPrice,
  1498. Status: 1,
  1499. Ctime: time.Now().Unix(),
  1500. UserOrgId: orgID,
  1501. Manufacturer: info.Manufacturer,
  1502. Dealer: info.Dealer,
  1503. LicenseNumber: warehouse.LicenseNumber,
  1504. IsEdit: 2,
  1505. Creator: warehouseOut.Creater,
  1506. SystemTime: record_time,
  1507. ConsumableType: 3,
  1508. WarehouseOutDetailId: warehouseOutInfo.ID,
  1509. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1510. IsSys: 1,
  1511. PatientId: patient_id,
  1512. ProjectId: goods.ProjectId,
  1513. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1514. StorehouseId: goods.StorehouseId,
  1515. }
  1516. //UpdatedStockFlow(flow)
  1517. UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  1518. }
  1519. }
  1520. // 出库完成后,要将该批次库存清零
  1521. ModifyGoodReduceInformationSix(warehouse.GoodId, warehouse.StockCount, orgID, warehouse.PackingPrice)
  1522. warehouse.StockCount = 0
  1523. warehouse.Mtime = time.Now().Unix()
  1524. errThree := UpDateWarehouseInfoByStock(&warehouse)
  1525. if errThree != nil {
  1526. return errThree
  1527. }
  1528. //if errThree != nil {
  1529. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  1530. // tx.Rollback()
  1531. //} else {
  1532. // tx.Commit()
  1533. //}
  1534. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1535. goods.Count = deliver_number - stock_number
  1536. ConsumablesDelivery(orgID, patient_id, record_time, goods, warehouseOut, count)
  1537. }
  1538. return nil
  1539. }
  1540. //耗材出库删除
  1541. func ConsumablesDeliveryDelete(orgID int64, patient_id int64, record_time int64, good_yc *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut) (err error) {
  1542. // 先根据相关信息查询当天该耗材的出库信息
  1543. warehouseOutInfos, err := FindStockOutInfoByStock(orgID, good_yc.GoodTypeId, good_yc.GoodId, record_time, patient_id)
  1544. if err != nil {
  1545. return err
  1546. }
  1547. var delete_count int64 = 0
  1548. for _, ware := range warehouseOutInfos {
  1549. // 判断当前出库的数据和删除出库数量
  1550. if good_yc.Count <= ware.Count {
  1551. delete_count = good_yc.Count
  1552. } else {
  1553. delete_count = ware.Count
  1554. }
  1555. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  1556. // 在出库记录表里记录退库详情
  1557. warehouseOutInfo := &models.WarehouseOutInfo{
  1558. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1559. WarehouseOutId: warehouseOut.ID,
  1560. //WarehouseInfoId: warehouse.ID,
  1561. Status: 1,
  1562. Ctime: time.Now().Unix(),
  1563. Remark: "",
  1564. OrgId: orgID,
  1565. Type: 1,
  1566. Manufacturer: 0,
  1567. Dealer: 0,
  1568. IsSys: 2,
  1569. SysRecordTime: record_time,
  1570. GoodTypeId: good_yc.GoodTypeId,
  1571. GoodId: good_yc.GoodId,
  1572. PatientId: patient_id,
  1573. StorehouseId: houseConfig.StorehouseOutInfo,
  1574. }
  1575. warehouseOutInfo.Count = ware.Count - delete_count
  1576. stockInInfo, _ := FindLastStockInInfoRecord(good_yc.GoodId, orgID)
  1577. warehouseOutInfo.Price = stockInInfo.Price
  1578. errOne := UpdateAutoMaticReduceDetail(good_yc.GoodId, good_yc.GoodTypeId, record_time, patient_id, orgID, warehouseOutInfo)
  1579. if errOne != nil {
  1580. return errOne
  1581. } else {
  1582. details := &models.AutomaticReduceDetail{
  1583. WarehouseOutId: warehouseOutInfo.ID,
  1584. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  1585. PatientId: patient_id,
  1586. Ctime: time.Now().Unix(),
  1587. Mtime: time.Now().Unix(),
  1588. Status: 1,
  1589. RecordTime: record_time,
  1590. OrgId: orgID,
  1591. GoodId: good_yc.GoodId,
  1592. GoodTypeId: good_yc.GoodTypeId,
  1593. Count: warehouseOutInfo.Count,
  1594. Type: 2,
  1595. StorehouseId: houseConfig.StorehouseOutInfo,
  1596. }
  1597. //查询是否当天已经存在数据
  1598. _, errcode := GetAutoMaticReduceDetail(orgID, patient_id, record_time, good_yc.GoodId, good_yc.GoodTypeId)
  1599. if errcode == gorm.ErrRecordNotFound {
  1600. errTwo := AddSigleAutoReduceRecordInfo(details)
  1601. if errTwo != nil {
  1602. return errTwo
  1603. }
  1604. } else if errcode == nil {
  1605. DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, good_yc.GoodId, good_yc.GoodTypeId)
  1606. AddSigleAutoReduceRecordInfo(details)
  1607. }
  1608. }
  1609. // 删除出库完成后,要增加对应批次的库存数量
  1610. errThree := UpDateWarehouseInfoByStockDelete(ware.WarehouseInfotId, delete_count)
  1611. if errThree != nil {
  1612. return errThree
  1613. }
  1614. // 增加了对应的库存后,看看还有多少需要退库的
  1615. good_yc.Count = good_yc.Count - delete_count
  1616. if good_yc.Count == 0 {
  1617. return nil
  1618. }
  1619. }
  1620. if good_yc.Count == 0 {
  1621. return nil
  1622. } else {
  1623. return errors.New("退库和出库数据不匹配")
  1624. }
  1625. }
  1626. func GetAutoReduceRecordInfoByPatientId(orgid int64, patient_id int64, recordTime int64) (autoReduce []*models.AutomaticReduceDetail, err error) {
  1627. err = XTReadDB().Model(&autoReduce).Where("org_id = ? and patient_id = ? and record_time = ? and status = 1", orgid, patient_id, recordTime).Find(&autoReduce).Error
  1628. return autoReduce, err
  1629. }
  1630. func DeleteDialysisBefor(orgid int64, patient_id int64, record_date int64, goodid int64, goodtypeid int64) error {
  1631. prepare := models.DialysisBeforePrepare{}
  1632. err := XTWriteDB().Model(&prepare).Where("user_org_id = ? and patient_id = ? and record_date = ? and status = 1 and good_id = ? and good_type_id = ?", orgid, patient_id, record_date, goodid, goodtypeid).Updates(map[string]interface{}{"status": 0, "count": 0, "mtime": time.Now().Unix()}).Error
  1633. return err
  1634. }
  1635. func DeleteDialysisBeforThree(orgid int64, patient_id int64, record_date int64) error {
  1636. prepare := models.DialysisBeforePrepare{}
  1637. err := XTWriteDB().Model(&prepare).Where("user_org_id = ? and patient_id = ? and record_date = ? and status = 1", orgid, patient_id, record_date).Updates(map[string]interface{}{"status": 0, "count": 0, "mtime": time.Now().Unix()}).Error
  1638. return err
  1639. }
  1640. func GetWarehouseById(id int64) (models.WarehouseOut, error) {
  1641. out := models.WarehouseOut{}
  1642. err := XTReadDB().Where("id=? and status = 1", id).Find(&out).Error
  1643. return out, err
  1644. }
  1645. //耗材出库
  1646. func ConsumablesDeliveryOne(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64) (err error) {
  1647. ////开事务
  1648. //tx := XTWriteDB().Begin()
  1649. var deliver_number int64 = 0
  1650. var stock_number int64 = 0
  1651. var maxNumber int64 = 0
  1652. deliver_number = goods.Count
  1653. // 根据先进先出原则,查询最先入库的批次,进行出库
  1654. // 如果没有对应的库存,则报错
  1655. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  1656. if err != nil {
  1657. return err
  1658. }
  1659. stock_number = warehouse.StockCount
  1660. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1661. if stock_number >= deliver_number {
  1662. warehouseOutInfo := &models.WarehouseOutInfo{
  1663. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1664. WarehouseOutId: warehouseOut.ID,
  1665. WarehouseInfotId: warehouse.ID,
  1666. Status: 1,
  1667. Ctime: time.Now().Unix(),
  1668. Remark: goods.Remark,
  1669. OrgId: orgID,
  1670. Type: 1,
  1671. Manufacturer: goods.Manufacturer,
  1672. Dealer: goods.Dealer,
  1673. IsSys: 0,
  1674. SysRecordTime: record_time,
  1675. GoodTypeId: goods.GoodTypeId,
  1676. GoodId: goods.GoodId,
  1677. ExpiryDate: goods.ExpiryDate,
  1678. ProductDate: goods.ProductDate,
  1679. Number: warehouse.Number,
  1680. Price: goods.Price,
  1681. LicenseNumber: goods.LicenseNumber,
  1682. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1683. StorehouseId: goods.StorehouseId,
  1684. AdminUserId: goods.AdminUserId,
  1685. BuyPrice: warehouse.Price,
  1686. StockCount: goods.StockCount,
  1687. }
  1688. warehouseOutInfo.Count = goods.Count
  1689. //添加出库单详情
  1690. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  1691. if errOne != nil {
  1692. return errOne
  1693. }
  1694. stockFlow := models.VmStockFlow{
  1695. WarehousingId: warehouse.ID,
  1696. GoodId: goods.GoodId,
  1697. Number: warehouse.Number,
  1698. LicenseNumber: goods.LicenseNumber,
  1699. Count: goods.Count,
  1700. UserOrgId: goods.OrgId,
  1701. PatientId: goods.PatientId,
  1702. SystemTime: record_time,
  1703. ConsumableType: 2,
  1704. IsSys: goods.IsSys,
  1705. WarehousingOrder: "",
  1706. WarehouseOutId: warehouseOut.ID,
  1707. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1708. IsEdit: 1,
  1709. CancelStockId: 0,
  1710. CancelOrderNumber: "",
  1711. Manufacturer: goods.Manufacturer,
  1712. Dealer: goods.Dealer,
  1713. Creator: creator,
  1714. UpdateCreator: 0,
  1715. Status: 1,
  1716. Ctime: time.Now().Unix(),
  1717. Mtime: 0,
  1718. Price: warehouse.PackingPrice,
  1719. WarehousingDetailId: 0,
  1720. WarehouseOutDetailId: warehouseOutInfo.ID,
  1721. CancelOutDetailId: 0,
  1722. ProductDate: goods.ProductDate,
  1723. ExpireDate: goods.ExpiryDate,
  1724. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1725. StorehouseId: warehouse.StorehouseId,
  1726. AdminUserId: goods.AdminUserId,
  1727. BuyPrice: warehouse.Price,
  1728. StockCount: goods.StockCount,
  1729. }
  1730. //创建出库流水
  1731. CreateStockFlowOne(stockFlow)
  1732. maxNumber = goods.Count
  1733. if warehouse.StockCount < maxNumber {
  1734. return errors.New("库存数量不足")
  1735. }
  1736. warehouse.StockCount = warehouse.StockCount - maxNumber
  1737. warehouse.Mtime = time.Now().Unix()
  1738. //扣减库存
  1739. errThree := UpDateWarehouseInfoByStock(&warehouse)
  1740. fmt.Println(errThree)
  1741. //if errThree != nil {
  1742. // utils.ErrorLog("事务失败,原因为: %v", errThree.Error())
  1743. // tx.Rollback()
  1744. //} else {
  1745. // tx.Commit()
  1746. //}
  1747. //查询该机构默认仓库
  1748. storeConfig, _ := GetAllStoreHouseConfig(goods.OrgId)
  1749. //查询剩余库存
  1750. goodList, _ := GetGoodSumCountByStoreId(storeConfig.StorehouseOutInfo, goods.GoodId, goods.OrgId)
  1751. var sum_count int64
  1752. var sum_in_count int64
  1753. for _, item := range goodList {
  1754. sum_count += item.StockCount
  1755. sum_in_count += item.WarehousingCount
  1756. }
  1757. UpdateGoodByGoodId(goods.GoodId, sum_count, sum_in_count, goods.OrgId)
  1758. if errThree != nil {
  1759. return errThree
  1760. }
  1761. } else {
  1762. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  1763. warehouseOutInfo := &models.WarehouseOutInfo{
  1764. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1765. WarehouseOutId: warehouseOut.ID,
  1766. WarehouseInfotId: warehouse.ID,
  1767. Status: 1,
  1768. Ctime: time.Now().Unix(),
  1769. Remark: goods.Remark,
  1770. OrgId: orgID,
  1771. Type: 1,
  1772. Manufacturer: goods.Manufacturer,
  1773. Dealer: goods.Dealer,
  1774. IsSys: 0,
  1775. SysRecordTime: record_time,
  1776. GoodTypeId: goods.GoodTypeId,
  1777. GoodId: goods.GoodId,
  1778. ExpiryDate: goods.ExpiryDate,
  1779. ProductDate: goods.ProductDate,
  1780. Number: warehouse.Number,
  1781. Price: warehouse.PackingPrice,
  1782. LicenseNumber: goods.LicenseNumber,
  1783. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1784. StorehouseId: warehouse.StorehouseId,
  1785. AdminUserId: goods.AdminUserId,
  1786. BuyPrice: warehouse.Price,
  1787. StockCount: goods.StockCount,
  1788. }
  1789. warehouseOutInfo.Count = stock_number
  1790. //查询是否已经生成出库单
  1791. info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  1792. if errcodes == gorm.ErrRecordNotFound {
  1793. //创建出库单
  1794. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  1795. if errOne != nil {
  1796. return errOne
  1797. }
  1798. } else {
  1799. warehouseOutInfo.Count = info.Count + stock_number
  1800. UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  1801. }
  1802. stockFlow := models.VmStockFlow{
  1803. WarehousingId: warehouse.ID,
  1804. GoodId: goods.GoodId,
  1805. Number: warehouse.Number,
  1806. LicenseNumber: goods.LicenseNumber,
  1807. Count: stock_number,
  1808. UserOrgId: goods.OrgId,
  1809. PatientId: 0,
  1810. SystemTime: record_time,
  1811. ConsumableType: 2,
  1812. IsSys: 0,
  1813. WarehousingOrder: "",
  1814. WarehouseOutId: warehouseOut.ID,
  1815. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1816. IsEdit: 1,
  1817. CancelStockId: 0,
  1818. CancelOrderNumber: "",
  1819. Manufacturer: goods.Manufacturer,
  1820. Dealer: goods.Dealer,
  1821. Creator: creator,
  1822. UpdateCreator: 0,
  1823. Status: 1,
  1824. Ctime: time.Now().Unix(),
  1825. Mtime: 0,
  1826. Price: warehouse.PackingPrice,
  1827. WarehousingDetailId: 0,
  1828. WarehouseOutDetailId: warehouseOutInfo.ID,
  1829. CancelOutDetailId: 0,
  1830. ProductDate: goods.ProductDate,
  1831. ExpireDate: goods.ExpiryDate,
  1832. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1833. StorehouseId: goods.StorehouseId,
  1834. AdminUserId: goods.AdminUserId,
  1835. BuyPrice: warehouse.Price,
  1836. StockCount: goods.StockCount,
  1837. }
  1838. //创建出库明细
  1839. CreateStockFlowOne(stockFlow)
  1840. //查询该机构默认仓库
  1841. storeConfig, _ := GetAllStoreHouseConfig(goods.OrgId)
  1842. //查询剩余库存
  1843. goodList, _ := GetGoodSumCountByStoreId(storeConfig.StorehouseOutInfo, goods.GoodId, goods.OrgId)
  1844. var sum_count int64
  1845. var sum_in_count int64
  1846. for _, item := range goodList {
  1847. sum_count += item.StockCount
  1848. sum_in_count += item.WarehousingCount
  1849. }
  1850. UpdateGoodByGoodId(goods.GoodId, sum_count, sum_in_count, goods.OrgId)
  1851. // 出库完成后,要将该批次库存清零
  1852. warehouse.StockCount = 0
  1853. warehouse.Mtime = time.Now().Unix()
  1854. errThree := UpDateWarehouseInfoByStock(&warehouse)
  1855. fmt.Println(errThree)
  1856. //if errThree != nil {
  1857. // utils.ErrorLog("事务失败,原因为: %v", errThree.Error())
  1858. // tx.Rollback()
  1859. //} else {
  1860. // tx.Commit()
  1861. //}
  1862. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1863. goods.Count = deliver_number - stock_number
  1864. ConsumablesDeliveryOne(orgID, record_time, goods, warehouseOut, count, creator)
  1865. }
  1866. return nil
  1867. }
  1868. //退库逻辑
  1869. func ConsumablesDeliveryTwo(orgID int64, goods *models.WarehouseOutInfo, count int64) (err error) {
  1870. ////开事务
  1871. //tx := XTWriteDB().Begin()
  1872. var deliver_number int64 = 0
  1873. var stock_number int64 = 0
  1874. var maxNumber int64 = 0
  1875. deliver_number = goods.Count
  1876. // 根据先进先出原则,查询最先入库的批次,进行出库
  1877. warehouse, err := FindFirstWarehousingInfoByGoodId(goods.GoodId, goods.ID, goods.StorehouseId)
  1878. if err != nil {
  1879. return err
  1880. }
  1881. stock_number = warehouse.StockCount
  1882. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1883. if stock_number >= deliver_number {
  1884. warehouseOutInfo := &models.WarehouseOutInfo{
  1885. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  1886. WarehouseOutId: goods.ID,
  1887. WarehouseInfotId: warehouse.ID,
  1888. Status: 1,
  1889. Ctime: time.Now().Unix(),
  1890. Remark: goods.Remark,
  1891. OrgId: orgID,
  1892. Type: 1,
  1893. Manufacturer: goods.Manufacturer,
  1894. Dealer: goods.Dealer,
  1895. IsSys: 0,
  1896. GoodTypeId: goods.GoodTypeId,
  1897. GoodId: goods.GoodId,
  1898. ExpiryDate: goods.ExpiryDate,
  1899. ProductDate: goods.ProductDate,
  1900. Number: goods.Number,
  1901. Price: warehouse.PackingPrice,
  1902. LicenseNumber: goods.LicenseNumber,
  1903. ConsumableType: 2,
  1904. }
  1905. warehouseOutInfo.Count = goods.Count
  1906. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  1907. if errOne != nil {
  1908. return errOne
  1909. }
  1910. maxNumber = goods.Count
  1911. if warehouse.StockCount < maxNumber {
  1912. return errors.New("库存数量不足")
  1913. }
  1914. warehouse.StockCount = warehouse.StockCount - maxNumber
  1915. warehouse.Mtime = time.Now().Unix()
  1916. if warehouse.StockCount <= 0 {
  1917. return errors.New("库存数量不足")
  1918. }
  1919. errThree := UpDateWarehouseInfoByStock(&warehouse)
  1920. fmt.Println(errThree)
  1921. //if errThree != nil {
  1922. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  1923. // tx.Rollback()
  1924. //} else {
  1925. // tx.Commit()
  1926. //}
  1927. return
  1928. } else {
  1929. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  1930. warehouseOutInfo := &models.WarehouseOutInfo{
  1931. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  1932. WarehouseOutId: goods.ID,
  1933. WarehouseInfotId: warehouse.ID,
  1934. Status: 1,
  1935. Ctime: time.Now().Unix(),
  1936. Remark: goods.Remark,
  1937. OrgId: orgID,
  1938. Type: 1,
  1939. Manufacturer: goods.Manufacturer,
  1940. Dealer: goods.Dealer,
  1941. IsSys: 0,
  1942. GoodTypeId: goods.GoodTypeId,
  1943. GoodId: goods.GoodId,
  1944. ExpiryDate: goods.ExpiryDate,
  1945. ProductDate: goods.ProductDate,
  1946. Number: goods.Number,
  1947. Price: warehouse.PackingPrice,
  1948. LicenseNumber: goods.LicenseNumber,
  1949. ConsumableType: 2,
  1950. }
  1951. warehouseOutInfo.Count = stock_number
  1952. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  1953. if errOne != nil {
  1954. return errOne
  1955. }
  1956. // 出库完成后,要将该批次库存清零
  1957. warehouse.StockCount = 0
  1958. warehouse.Mtime = time.Now().Unix()
  1959. errThree := UpDateWarehouseInfoByStock(&warehouse)
  1960. //if errThree != nil {
  1961. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  1962. // tx.Rollback()
  1963. //} else {
  1964. // tx.Commit()
  1965. //}
  1966. fmt.Println(errThree)
  1967. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1968. goods.Count = deliver_number - stock_number
  1969. ConsumablesDeliveryTwo(orgID, goods, count)
  1970. }
  1971. return nil
  1972. }
  1973. func DeleteWarehouseInfo(id int64) error {
  1974. err := XTWriteDB().Model(models.WarehousingInfo{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": 0}).Error
  1975. return err
  1976. }
  1977. // 药品出库 递归方式
  1978. func NewDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DoctorAdvice) (err error) {
  1979. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  1980. var deliver_number int64 = 0
  1981. var stock_number int64 = 0
  1982. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  1983. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1984. var drug_price float64
  1985. //按最小单位数据计算
  1986. deliver_number = count
  1987. if advice.PrescribingNumberUnit == drup.MaxUnit {
  1988. drug_price = drup.RetailPrice
  1989. }
  1990. if advice.PrescribingNumberUnit == drup.MinUnit {
  1991. drug_price = drup.MinPrice
  1992. }
  1993. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  1994. drug_price = drup.RetailPrice
  1995. }
  1996. // 根据先进先出原则,查询最先入库的批次,进行出库
  1997. // 如果没有对应的库存,则报错
  1998. //查询默认仓库
  1999. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  2000. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2001. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  2002. var stockMax int64
  2003. var stockMin int64
  2004. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  2005. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  2006. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  2007. UpdateMinNumber(lastWarehouse.ID, stockMin)
  2008. }
  2009. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2010. if err != nil {
  2011. return err
  2012. }
  2013. // 将该批次的剩余库存数量转换为拆零数量
  2014. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  2015. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  2016. if stock_number >= deliver_number {
  2017. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2018. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2019. WarehouseOutId: warehouseout.ID,
  2020. Status: 1,
  2021. Ctime: time.Now().Unix(),
  2022. Remark: warehouse.Remark,
  2023. OrgId: orgID,
  2024. Type: 1,
  2025. Manufacturer: warehouse.Manufacturer,
  2026. Dealer: warehouse.Dealer,
  2027. IsSys: 1,
  2028. SysRecordTime: advice.RecordDate,
  2029. DrugId: advice.DrugId,
  2030. Number: warehouse.Number,
  2031. BatchNumber: warehouse.BatchNumber,
  2032. Price: warehouse.RetailPrice,
  2033. CountUnit: drup.MinUnit,
  2034. RetailPrice: drug_price,
  2035. ProductDate: warehouse.ProductDate,
  2036. ExpiryDate: warehouse.ExpiryDate,
  2037. PatientId: advice.PatientId,
  2038. Count: deliver_number, //出最小单位
  2039. WarehouseInfoId: warehouse.ID,
  2040. AdviceId: advice.ID,
  2041. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2042. StorehouseId: storeConfig.DrugStorehouseOut,
  2043. }
  2044. drugflow := models.DrugFlow{
  2045. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2046. WarehouseOutId: warehouseout.ID,
  2047. DrugId: advice.DrugId,
  2048. Number: warehouse.Number,
  2049. ProductDate: warehouse.ProductDate,
  2050. ExpireDate: warehouse.ExpiryDate,
  2051. Count: deliver_number, //按最小单位计算,
  2052. Price: warehouse.RetailPrice,
  2053. Status: 1,
  2054. Ctime: time.Now().Unix(),
  2055. UserOrgId: orgID,
  2056. Manufacturer: warehouse.Manufacturer,
  2057. Dealer: warehouse.Dealer,
  2058. BatchNumber: warehouse.BatchNumber,
  2059. MaxUnit: drup.MinUnit,
  2060. ConsumableType: 3,
  2061. IsEdit: 1,
  2062. Creator: advice.ExecutionStaff,
  2063. IsSys: 1,
  2064. PatientId: advice.PatientId,
  2065. WarehousingDetailId: warehouse.ID,
  2066. AdviceId: advice.ID,
  2067. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2068. StorehouseId: storeConfig.DrugStorehouseOut,
  2069. }
  2070. CreateDrugFlowOne(drugflow)
  2071. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2072. if errOne != nil {
  2073. return errOne
  2074. }
  2075. //查询是否存在数据
  2076. //_, errcodes := GetDrugWarehouseOutInfoIsExit(advice.DrugId, advice.PatientId, advice.RecordDate)
  2077. //if errcodes == gorm.ErrRecordNotFound {
  2078. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2079. // if errOne != nil {
  2080. // return errOne
  2081. // }
  2082. //} else if errcodes == nil {
  2083. // UpdatedDrugWarehouseOutInfo(warehouseOutInfo, advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId)
  2084. //}
  2085. details := &models.DrugAutomaticReduceDetail{
  2086. WarehouseOutId: warehouseout.ID,
  2087. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2088. PatientId: advice.PatientId,
  2089. Ctime: time.Now().Unix(),
  2090. Mtime: time.Now().Unix(),
  2091. Status: 1,
  2092. RecordTime: advice.RecordDate,
  2093. OrgId: orgID,
  2094. DrugId: advice.DrugId,
  2095. Count: count,
  2096. CountUnit: advice.PrescribingNumberUnit,
  2097. WarehouseInfoId: warehouse.ID,
  2098. StorehouseId: storeConfig.DrugStorehouseOut,
  2099. }
  2100. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2101. if errTwo != nil {
  2102. return errTwo
  2103. }
  2104. // 出库完成后,要减去对应批次的库存数量
  2105. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  2106. var maxNumber int64 = 0
  2107. var minNumber int64 = 0
  2108. //var stock_max_number int64 = 0
  2109. //stock_max_number = warehouse.StockMaxNumber
  2110. maxNumber = deliver_number / drup.MinNumber
  2111. minNumber = deliver_number % drup.MinNumber
  2112. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  2113. minNumber = maxNumber
  2114. }
  2115. if drup.MaxUnit != drup.MinUnit {
  2116. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  2117. return errors.New("库存数量不足")
  2118. }
  2119. }
  2120. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  2121. if warehouse.StockMaxNumber < 0 {
  2122. warehouse.StockMaxNumber = 0
  2123. }
  2124. if warehouse.StockMinNumber < 0 {
  2125. warehouse.StockMinNumber = 0
  2126. }
  2127. warehouse.Mtime = time.Now().Unix()
  2128. if warehouse.StockMinNumber < minNumber {
  2129. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  2130. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  2131. } else {
  2132. if minNumber > 0 {
  2133. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2134. }
  2135. if minNumber == 0 && maxNumber != 1 {
  2136. if warehouse.StockMinNumber > 0 {
  2137. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2138. }
  2139. }
  2140. }
  2141. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  2142. if (warehouse.StockMinNumber - deliver_number) > 0 {
  2143. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2144. //if warehouse.StockMinNumber == 0 {
  2145. // warehouse.StockMaxNumber = stock_max_number
  2146. //}
  2147. }
  2148. }
  2149. if drup.MaxUnit != drup.MinUnit {
  2150. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  2151. return errors.New("库存数量不足")
  2152. }
  2153. }
  2154. if warehouse.StockMinNumber <= 0 {
  2155. warehouse.StockMinNumber = 0
  2156. }
  2157. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  2158. //if(warehouse.StockMaxNumber > 0){
  2159. // var sum_count int64
  2160. // sum_count = warehouse.StockMaxNumber * drup.MinNumber
  2161. // ModifyDrugReduceInformationSix(warehouse.DrugId,sum_count,warehouse.OrgId,warehouse.RetailPrice)
  2162. //}
  2163. //if(warehouse.StockMinNumber > 0){
  2164. // ModifyDrugReduceInformationSix(warehouse.DrugId,warehouse.StockMinNumber,warehouse.OrgId,warehouse.RetailPrice)
  2165. //}
  2166. if errThree != nil {
  2167. return errThree
  2168. }
  2169. return nil
  2170. } else {
  2171. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2172. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2173. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2174. WarehouseOutId: warehouseout.ID,
  2175. Status: 1,
  2176. Ctime: time.Now().Unix(),
  2177. Remark: warehouse.Remark,
  2178. OrgId: orgID,
  2179. Type: 1,
  2180. Manufacturer: warehouse.Manufacturer,
  2181. Dealer: warehouse.Dealer,
  2182. IsSys: 1,
  2183. SysRecordTime: advice.RecordDate,
  2184. DrugId: advice.DrugId,
  2185. Number: warehouse.Number,
  2186. BatchNumber: warehouse.BatchNumber,
  2187. Price: warehouse.RetailPrice,
  2188. CountUnit: drup.MinUnit,
  2189. ProductDate: warehouse.ProductDate,
  2190. ExpiryDate: warehouse.ExpiryDate,
  2191. PatientId: advice.PatientId,
  2192. Count: deliver_number,
  2193. WarehouseInfoId: warehouse.ID,
  2194. AdviceId: advice.ID,
  2195. StorehouseId: storeConfig.DrugStorehouseOut,
  2196. }
  2197. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2198. if errOne != nil {
  2199. return errOne
  2200. }
  2201. //查询是否存在数据
  2202. //_, errcodes := GetDrugWarehouseOutInfoIsExit(advice.DrugId, advice.PatientId, advice.RecordDate)
  2203. //if errcodes == gorm.ErrRecordNotFound {
  2204. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2205. // if errOne != nil {
  2206. // return errOne
  2207. // }
  2208. //} else if errcodes == nil {
  2209. // UpdatedDrugWarehouseOutInfo(warehouseOutInfo, advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId)
  2210. //}
  2211. details := &models.DrugAutomaticReduceDetail{
  2212. WarehouseOutId: warehouseout.ID,
  2213. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  2214. PatientId: advice.PatientId,
  2215. Ctime: time.Now().Unix(),
  2216. Mtime: time.Now().Unix(),
  2217. Status: 1,
  2218. RecordTime: advice.RecordDate,
  2219. OrgId: orgID,
  2220. DrugId: advice.DrugId,
  2221. Count: deliver_number,
  2222. CountUnit: drup.MinUnit,
  2223. WarehouseInfoId: warehouse.ID,
  2224. StorehouseId: storeConfig.DrugStorehouseOut,
  2225. }
  2226. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2227. if errTwo != nil {
  2228. return errTwo
  2229. }
  2230. // 出库完成后,要将该批次库存清零
  2231. //warehouse.StockMaxNumber = 0
  2232. //warehouse.StockMinNumber = 0
  2233. //warehouse.Mtime = time.Now().Unix()
  2234. info := models.XtDrugWarehouseInfo{
  2235. ID: warehouse.ID,
  2236. WarehousingId: warehouse.WarehousingId,
  2237. DrugId: warehouse.DrugId,
  2238. Number: warehouse.Number,
  2239. ProductDate: warehouse.ProductDate,
  2240. ExpiryDate: warehouse.ExpiryDate,
  2241. WarehousingCount: warehouse.WarehousingCount,
  2242. Price: warehouse.Price,
  2243. TotalPrice: warehouse.TotalPrice,
  2244. Dealer: warehouse.Dealer,
  2245. Manufacturer: warehouse.Manufacturer,
  2246. Remark: warehouse.Remark,
  2247. Ctime: warehouse.Ctime,
  2248. Mtime: warehouse.Mtime,
  2249. Status: 1,
  2250. OrgId: warehouse.OrgId,
  2251. IsReturn: warehouse.IsReturn,
  2252. WarehousingOrder: warehouse.WarehousingOrder,
  2253. Type: warehouse.Type,
  2254. RetailPrice: warehouse.RetailPrice,
  2255. RetailTotalPrice: warehouse.RetailPrice,
  2256. StockMaxNumber: 0,
  2257. StockMinNumber: 0,
  2258. BatchNumber: warehouse.BatchNumber,
  2259. MaxUnit: warehouse.MaxUnit,
  2260. WarehousingInfoId: warehouse.WarehousingInfoId,
  2261. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2262. StorehouseId: storeConfig.DrugStorehouseOut,
  2263. }
  2264. //扣减库存
  2265. errThree := UpDateDrugWarehouseInfoByStock(&info)
  2266. if errThree != nil {
  2267. return errThree
  2268. }
  2269. drugflow := models.DrugFlow{
  2270. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2271. WarehouseOutId: warehouseout.ID,
  2272. DrugId: advice.DrugId,
  2273. Number: warehouse.Number,
  2274. ProductDate: warehouse.ProductDate,
  2275. ExpireDate: warehouse.ExpiryDate,
  2276. Count: deliver_number,
  2277. Price: warehouse.RetailPrice,
  2278. Status: 1,
  2279. Ctime: time.Now().Unix(),
  2280. UserOrgId: orgID,
  2281. Manufacturer: warehouse.Manufacturer,
  2282. Dealer: warehouse.Dealer,
  2283. BatchNumber: warehouse.BatchNumber,
  2284. MaxUnit: drup.MinUnit,
  2285. ConsumableType: 3,
  2286. IsEdit: 1,
  2287. Creator: advice.ExecutionStaff,
  2288. IsSys: 1,
  2289. PatientId: advice.PatientId,
  2290. WarehousingDetailId: warehouse.ID,
  2291. AdviceId: advice.ID,
  2292. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2293. StorehouseId: storeConfig.DrugStorehouseOut,
  2294. }
  2295. CreateDrugFlowOne(drugflow)
  2296. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2297. prescribingNumber_two_temp := deliver_number - stock_number
  2298. overPlusNumber := float64(prescribingNumber_two_temp)
  2299. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  2300. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  2301. NewDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  2302. }
  2303. return
  2304. }
  2305. // 药品出库 递归方式
  2306. func NewHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  2307. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  2308. var deliver_number int64 = 0
  2309. var stock_number int64 = 0
  2310. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  2311. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  2312. deliver_number = count
  2313. var min_price float64
  2314. if advice.PrescribingNumberUnit == drup.MaxUnit {
  2315. min_price = drup.RetailPrice
  2316. }
  2317. if advice.PrescribingNumberUnit == drup.MinUnit {
  2318. min_price = drup.MinPrice
  2319. }
  2320. if advice.PrescribingNumberUnit != drup.MaxUnit && advice.PrescribingNumberUnit != drup.MinUnit {
  2321. min_price = drup.RetailPrice
  2322. }
  2323. fmt.Println(min_price)
  2324. // 根据先进先出原则,查询最先入库的批次,进行出库
  2325. // 如果没有对应的库存,则报错
  2326. //开启事物
  2327. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  2328. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2329. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  2330. var stockMax int64
  2331. var stockMin int64
  2332. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  2333. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  2334. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  2335. UpdateMinNumber(lastWarehouse.ID, stockMin)
  2336. }
  2337. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2338. //查找药品信息
  2339. if err != nil {
  2340. return err
  2341. }
  2342. // 将该批次的剩余库存数量转换为拆零数量
  2343. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  2344. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  2345. if stock_number >= deliver_number {
  2346. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2347. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2348. WarehouseOutId: warehouseout.ID,
  2349. Status: 1,
  2350. Ctime: time.Now().Unix(),
  2351. Remark: warehouse.Remark,
  2352. OrgId: orgID,
  2353. Type: 1,
  2354. Manufacturer: warehouse.Manufacturer,
  2355. Dealer: warehouse.Dealer,
  2356. IsSys: 1,
  2357. SysRecordTime: advice.RecordDate,
  2358. DrugId: advice.DrugId,
  2359. Number: warehouse.Number,
  2360. BatchNumber: warehouse.BatchNumber,
  2361. Price: warehouse.RetailPrice,
  2362. CountUnit: drup.MinUnit,
  2363. RetailPrice: warehouse.RetailPrice,
  2364. ProductDate: warehouse.ProductDate,
  2365. ExpiryDate: warehouse.ExpiryDate,
  2366. PatientId: advice.PatientId,
  2367. WarehouseInfoId: warehouse.ID,
  2368. Count: deliver_number,
  2369. AdviceId: advice.ID,
  2370. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2371. StorehouseId: storeConfig.DrugStorehouseOut,
  2372. IsCheck: 1,
  2373. }
  2374. drugflow := models.DrugFlow{
  2375. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2376. WarehouseOutId: warehouseout.ID,
  2377. DrugId: advice.DrugId,
  2378. Number: warehouse.Number,
  2379. ProductDate: warehouse.ProductDate,
  2380. ExpireDate: warehouse.ExpiryDate,
  2381. Count: deliver_number,
  2382. Price: warehouse.RetailPrice,
  2383. Status: 1,
  2384. Ctime: time.Now().Unix(),
  2385. UserOrgId: orgID,
  2386. Manufacturer: warehouse.Manufacturer,
  2387. Dealer: warehouse.Dealer,
  2388. BatchNumber: warehouse.BatchNumber,
  2389. MaxUnit: drup.MinUnit,
  2390. ConsumableType: 3,
  2391. IsEdit: 1,
  2392. Creator: advice.ExecutionStaff,
  2393. IsSys: 1,
  2394. PatientId: advice.PatientId,
  2395. SystemTime: advice.RecordDate,
  2396. WarehousingDetailId: warehouse.ID,
  2397. AdviceId: advice.ID,
  2398. StorehouseId: storeConfig.DrugStorehouseOut,
  2399. }
  2400. CreateDrugFlowOne(drugflow)
  2401. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2402. if errOne != nil {
  2403. return errOne
  2404. }
  2405. details := &models.DrugAutomaticReduceDetail{
  2406. WarehouseOutId: warehouseout.ID,
  2407. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2408. PatientId: advice.PatientId,
  2409. Ctime: time.Now().Unix(),
  2410. Mtime: time.Now().Unix(),
  2411. Status: 1,
  2412. RecordTime: advice.RecordDate,
  2413. OrgId: orgID,
  2414. DrugId: advice.DrugId,
  2415. Count: deliver_number,
  2416. CountUnit: drup.MinUnit,
  2417. WarehouseInfoId: warehouse.ID,
  2418. AdviceId: advice.ID,
  2419. StorehouseId: storeConfig.DrugStorehouseOut,
  2420. }
  2421. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2422. if errTwo != nil {
  2423. return errTwo
  2424. }
  2425. // 出库完成后,要减去对应批次的库存数量
  2426. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  2427. var maxNumber int64 = 0
  2428. var minNumber int64 = 0
  2429. //var stock_max_number int64 = 0
  2430. //stock_max_number = warehouse.StockMaxNumber
  2431. maxNumber = deliver_number / drup.MinNumber
  2432. minNumber = deliver_number % drup.MinNumber
  2433. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  2434. minNumber = maxNumber
  2435. }
  2436. if drup.MaxUnit != drup.MinUnit {
  2437. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  2438. return errors.New("库存数量不足")
  2439. }
  2440. }
  2441. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  2442. if warehouse.StockMaxNumber < 0 {
  2443. warehouse.StockMaxNumber = 0
  2444. }
  2445. if warehouse.StockMinNumber < 0 {
  2446. warehouse.StockMinNumber = 0
  2447. }
  2448. warehouse.Mtime = time.Now().Unix()
  2449. if warehouse.StockMinNumber < minNumber {
  2450. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  2451. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  2452. } else {
  2453. if minNumber > 0 {
  2454. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2455. }
  2456. if minNumber == 0 && maxNumber != 1 {
  2457. if warehouse.StockMinNumber > 0 {
  2458. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2459. }
  2460. }
  2461. }
  2462. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  2463. if (warehouse.StockMinNumber - deliver_number) > 0 {
  2464. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2465. //if warehouse.StockMinNumber == 0 {
  2466. // warehouse.StockMaxNumber = stock_max_number
  2467. //}
  2468. }
  2469. }
  2470. if drup.MaxUnit != drup.MinUnit {
  2471. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  2472. return errors.New("库存数量不足")
  2473. }
  2474. }
  2475. if warehouse.StockMinNumber <= 0 {
  2476. warehouse.StockMinNumber = 0
  2477. }
  2478. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  2479. if errThree != nil {
  2480. return errThree
  2481. }
  2482. return nil
  2483. } else {
  2484. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2485. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2486. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2487. WarehouseOutId: warehouseout.ID,
  2488. Status: 1,
  2489. Ctime: time.Now().Unix(),
  2490. Remark: warehouse.Remark,
  2491. OrgId: orgID,
  2492. Type: 1,
  2493. Manufacturer: warehouse.Manufacturer,
  2494. Dealer: warehouse.Dealer,
  2495. IsSys: 1,
  2496. SysRecordTime: advice.RecordDate,
  2497. DrugId: advice.DrugId,
  2498. Number: warehouse.Number,
  2499. BatchNumber: warehouse.BatchNumber,
  2500. Price: warehouse.RetailPrice,
  2501. RetailPrice: warehouse.RetailPrice,
  2502. ProductDate: warehouse.ProductDate,
  2503. ExpiryDate: warehouse.ExpiryDate,
  2504. PatientId: advice.PatientId,
  2505. WarehouseInfoId: warehouse.ID,
  2506. Count: stock_number,
  2507. CountUnit: drup.MinUnit,
  2508. AdviceId: advice.ID,
  2509. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2510. StorehouseId: storeConfig.DrugStorehouseOut,
  2511. IsCheck: 1,
  2512. }
  2513. drugflow := models.DrugFlow{
  2514. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2515. WarehouseOutId: warehouseout.ID,
  2516. DrugId: advice.DrugId,
  2517. Number: warehouse.Number,
  2518. ProductDate: warehouse.ProductDate,
  2519. ExpireDate: warehouse.ExpiryDate,
  2520. Count: stock_number,
  2521. Price: warehouse.RetailPrice,
  2522. Status: 1,
  2523. Ctime: time.Now().Unix(),
  2524. UserOrgId: orgID,
  2525. Manufacturer: warehouse.Manufacturer,
  2526. Dealer: warehouse.Dealer,
  2527. BatchNumber: warehouse.BatchNumber,
  2528. MaxUnit: drup.MinUnit,
  2529. ConsumableType: 3,
  2530. IsEdit: 1,
  2531. Creator: advice.ExecutionStaff,
  2532. IsSys: 1,
  2533. PatientId: advice.PatientId,
  2534. SystemTime: advice.RecordDate,
  2535. WarehousingDetailId: warehouse.ID,
  2536. AdviceId: advice.ID,
  2537. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2538. StorehouseId: storeConfig.DrugStorehouseOut,
  2539. }
  2540. CreateDrugFlowOne(drugflow)
  2541. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2542. if errOne != nil {
  2543. return errOne
  2544. }
  2545. details := &models.DrugAutomaticReduceDetail{
  2546. WarehouseOutId: warehouseout.ID,
  2547. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2548. PatientId: advice.PatientId,
  2549. Ctime: time.Now().Unix(),
  2550. Mtime: time.Now().Unix(),
  2551. Status: 1,
  2552. RecordTime: advice.RecordDate,
  2553. OrgId: orgID,
  2554. DrugId: advice.DrugId,
  2555. Count: stock_number,
  2556. CountUnit: drup.MinUnit,
  2557. WarehouseInfoId: warehouse.ID,
  2558. AdviceId: advice.ID,
  2559. StorehouseId: storeConfig.DrugStorehouseOut,
  2560. }
  2561. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2562. if errTwo != nil {
  2563. return errTwo
  2564. }
  2565. info := models.XtDrugWarehouseInfo{
  2566. ID: warehouse.ID,
  2567. WarehousingId: warehouse.WarehousingId,
  2568. DrugId: warehouse.DrugId,
  2569. Number: warehouse.Number,
  2570. ProductDate: warehouse.ProductDate,
  2571. ExpiryDate: warehouse.ExpiryDate,
  2572. WarehousingCount: warehouse.WarehousingCount,
  2573. Price: warehouse.RetailPrice,
  2574. TotalPrice: warehouse.TotalPrice,
  2575. Dealer: warehouse.Dealer,
  2576. Manufacturer: warehouse.Manufacturer,
  2577. Remark: warehouse.Remark,
  2578. Ctime: warehouse.Ctime,
  2579. Mtime: warehouse.Mtime,
  2580. Status: 1,
  2581. OrgId: warehouse.OrgId,
  2582. IsReturn: warehouse.IsReturn,
  2583. WarehousingOrder: warehouse.WarehousingOrder,
  2584. Type: warehouse.Type,
  2585. RetailPrice: warehouse.RetailPrice,
  2586. RetailTotalPrice: warehouse.RetailPrice,
  2587. StockMaxNumber: 0,
  2588. StockMinNumber: 0,
  2589. BatchNumber: warehouse.BatchNumber,
  2590. MaxUnit: warehouse.MaxUnit,
  2591. WarehousingInfoId: warehouse.WarehousingInfoId,
  2592. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2593. StorehouseId: storeConfig.DrugStorehouseOut,
  2594. IsCheck: 1,
  2595. }
  2596. errThree := UpDateDrugWarehouseInfoByStock(&info)
  2597. if errThree != nil {
  2598. return errThree
  2599. }
  2600. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2601. prescribingNumber_two_temp := deliver_number - stock_number
  2602. overPlusNumber := float64(prescribingNumber_two_temp)
  2603. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  2604. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  2605. NewHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  2606. }
  2607. return
  2608. }
  2609. // 药品出库 递归方式
  2610. func HisAuToReduce(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  2611. //查询最后一次出库数据
  2612. drugInfo, _ := GetDrugWarehouseOutInfoIsExit(advice.DrugId, advice.PatientId, advice.RecordDate)
  2613. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  2614. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  2615. var count_number int64
  2616. var device_number int64
  2617. if advice.PrescribingNumberUnit == drup.MaxUnit {
  2618. device_number = count * drup.MinNumber
  2619. }
  2620. if advice.PrescribingNumberUnit == drup.MinUnit {
  2621. device_number = count
  2622. }
  2623. if drugInfo.CountUnit == drup.MaxUnit {
  2624. count_number = drugInfo.Count * drup.MinNumber
  2625. }
  2626. if drugInfo.CountUnit == drup.MinUnit {
  2627. count_number = drugInfo.Count
  2628. }
  2629. // 比较数据大小
  2630. //如果当期医嘱数量小于最后一次出库则退库
  2631. if device_number < count_number {
  2632. var cha_number int64
  2633. var maxNumber int64
  2634. var minNumber int64
  2635. cha_number = count_number - device_number
  2636. maxNumber = cha_number / drup.MinNumber
  2637. minNumber = cha_number % drup.MinNumber
  2638. //更改库存
  2639. ModifyDrugWarehouse(drugInfo.WarehouseInfoId, maxNumber, minNumber)
  2640. //修改出库表
  2641. info := models.DrugWarehouseOutInfo{
  2642. Count: device_number,
  2643. CountUnit: advice.PrescribingNumberUnit,
  2644. }
  2645. ModifyDrugWarehouseOutInfo(info, advice.DrugId, advice.PatientId, advice.RecordDate)
  2646. //修改自动出库表
  2647. detail := models.DrugAutomaticReduceDetail{
  2648. Count: device_number,
  2649. CountUnit: advice.PrescribingNumberUnit,
  2650. }
  2651. ModifyAutoMaticReduce(detail, advice.PatientId, advice.RecordDate, advice.DrugId)
  2652. //修改流水
  2653. flow := models.DrugFlow{
  2654. Count: device_number,
  2655. MaxUnit: drup.MinUnit,
  2656. }
  2657. ModifyDrugFlow(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId)
  2658. }
  2659. //如果当期医嘱数量大于最后一次出库则需要出库
  2660. if device_number > count_number {
  2661. var cha_number int64
  2662. cha_number = device_number - count_number
  2663. // 根据先进先出原则,查询最先入库的批次,进行出库
  2664. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  2665. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2666. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  2667. var stockMax int64
  2668. var stockMin int64
  2669. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  2670. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  2671. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  2672. UpdateMinNumber(lastWarehouse.ID, stockMin)
  2673. }
  2674. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2675. //查找药品信息
  2676. //baseInfo, _ := FindBaseDrugLibRecord(advice.UserOrgId, advice.DrugId)
  2677. if err != nil {
  2678. return err
  2679. }
  2680. // 将该批次的剩余库存数量转换为拆零数量
  2681. var stock_number int64
  2682. var maxNumber int64
  2683. var minNumber int64
  2684. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  2685. //如果库存数量大于出库数据,正常出库
  2686. if stock_number >= cha_number {
  2687. //扣减库存
  2688. maxNumber = cha_number / drup.MinNumber
  2689. minNumber = cha_number % drup.MinNumber
  2690. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  2691. minNumber = maxNumber
  2692. }
  2693. if drup.MaxUnit != drup.MinUnit {
  2694. if warehouse.StockMaxNumber < maxNumber {
  2695. return errors.New("库存数量不足")
  2696. }
  2697. }
  2698. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  2699. if warehouse.StockMaxNumber < 0 {
  2700. warehouse.StockMaxNumber = 0
  2701. }
  2702. warehouse.Mtime = time.Now().Unix()
  2703. if warehouse.StockMinNumber < minNumber {
  2704. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  2705. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  2706. } else {
  2707. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2708. }
  2709. if drup.MaxUnit != drup.MinUnit {
  2710. if warehouse.StockMaxNumber < 0 {
  2711. return errors.New("库存数量不足")
  2712. }
  2713. }
  2714. //扣减库存232332332332
  2715. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  2716. if errThree != nil {
  2717. return errThree
  2718. }
  2719. //修改出库表
  2720. info := models.DrugWarehouseOutInfo{
  2721. Count: device_number,
  2722. CountUnit: advice.PrescribingNumberUnit,
  2723. }
  2724. ModifyDrugWarehouseOutInfoOne(info, advice.DrugId, advice.PatientId, advice.RecordDate, warehouse.ID)
  2725. //修改自动出库表
  2726. detail := models.DrugAutomaticReduceDetail{
  2727. Count: device_number,
  2728. CountUnit: advice.PrescribingNumberUnit,
  2729. }
  2730. ModifyAutoMaticReduceOne(detail, advice.PatientId, advice.RecordDate, advice.DrugId, warehouse.ID)
  2731. //修改流水
  2732. flow := models.DrugFlow{
  2733. Count: device_number,
  2734. MaxUnit: advice.PrescribingNumberUnit,
  2735. }
  2736. ModifyDrugFlowOne(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId, warehouse.ID)
  2737. }
  2738. //如果库存数量小于出库数据,则需要出库到下一个批次
  2739. if stock_number < cha_number {
  2740. //修改出库表
  2741. info := models.DrugWarehouseOutInfo{
  2742. Count: cha_number,
  2743. CountUnit: advice.PrescribingNumberUnit,
  2744. }
  2745. ModifyDrugWarehouseOutInfoOne(info, advice.DrugId, advice.PatientId, advice.RecordDate, warehouse.ID)
  2746. //修改自动出库表
  2747. detail := models.DrugAutomaticReduceDetail{
  2748. Count: cha_number,
  2749. CountUnit: advice.PrescribingNumberUnit,
  2750. }
  2751. ModifyAutoMaticReduceOne(detail, advice.PatientId, advice.RecordDate, advice.DrugId, warehouse.ID)
  2752. //修改流水
  2753. flow := models.DrugFlow{
  2754. Count: cha_number,
  2755. MaxUnit: advice.PrescribingNumberUnit,
  2756. }
  2757. ModifyDrugFlowOne(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId, warehouse.ID)
  2758. druginfo := &models.XtDrugWarehouseInfo{
  2759. ID: warehouse.ID,
  2760. WarehousingId: warehouse.WarehousingId,
  2761. DrugId: warehouse.DrugId,
  2762. Number: warehouse.Number,
  2763. ProductDate: warehouse.ProductDate,
  2764. ExpiryDate: warehouse.ExpiryDate,
  2765. WarehousingCount: warehouse.WarehousingCount,
  2766. Price: warehouse.Price,
  2767. TotalPrice: warehouse.TotalPrice,
  2768. Dealer: warehouse.Dealer,
  2769. Manufacturer: warehouse.Manufacturer,
  2770. Remark: warehouse.Remark,
  2771. Ctime: warehouse.Ctime,
  2772. Mtime: warehouse.Mtime,
  2773. Status: 1,
  2774. OrgId: warehouse.OrgId,
  2775. IsReturn: warehouse.IsReturn,
  2776. WarehousingOrder: warehouse.WarehousingOrder,
  2777. Type: warehouse.Type,
  2778. RetailPrice: warehouse.RetailPrice,
  2779. RetailTotalPrice: warehouse.RetailPrice,
  2780. StockMaxNumber: 0,
  2781. StockMinNumber: 0,
  2782. BatchNumber: warehouse.BatchNumber,
  2783. MaxUnit: warehouse.MaxUnit,
  2784. WarehousingInfoId: warehouse.WarehousingInfoId,
  2785. }
  2786. errThree := UpDateDrugWarehouseInfoByStock(druginfo)
  2787. if errThree != nil {
  2788. return errThree
  2789. }
  2790. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2791. prescribingNumber_two_temp := cha_number - stock_number
  2792. overPlusNumber := float64(prescribingNumber_two_temp)
  2793. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  2794. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  2795. NewHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  2796. }
  2797. }
  2798. return
  2799. }
  2800. //采购管理耗材出库逻辑
  2801. func ConsumablesDeliveryNight(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64, warehouse_out_id int64, cancel_out_id int64) (err error) {
  2802. var deliver_number int64 = 0
  2803. var stock_number int64 = 0
  2804. var maxNumber int64 = 0
  2805. deliver_number = goods.Count
  2806. // 根据先进先出原则,查询最先入库的批次,进行出库
  2807. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  2808. // 如果没有对应的库存,则报错
  2809. warehouse, err := FindFirstWarehousingInfoByStockNight(goods.GoodId, goods.GoodTypeId, warehouse_out_id, goods.IsSource, houseConfig.StorehouseOutInfo)
  2810. if err != nil {
  2811. return err
  2812. }
  2813. stock_number = warehouse.StockCount
  2814. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  2815. if stock_number >= deliver_number {
  2816. warehouseOutInfo := &models.WarehouseOutInfo{
  2817. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2818. WarehouseOutId: goods.WarehouseOutId,
  2819. WarehouseInfotId: warehouse.ID,
  2820. Status: 1,
  2821. Ctime: time.Now().Unix(),
  2822. Remark: goods.Remark,
  2823. OrgId: orgID,
  2824. Type: 1,
  2825. Manufacturer: goods.Manufacturer,
  2826. Dealer: goods.Dealer,
  2827. IsSys: 0,
  2828. SysRecordTime: record_time,
  2829. GoodTypeId: goods.GoodTypeId,
  2830. GoodId: goods.GoodId,
  2831. ExpiryDate: goods.ExpiryDate,
  2832. ProductDate: goods.ProductDate,
  2833. Number: warehouse.Number,
  2834. Price: goods.Price,
  2835. LicenseNumber: goods.LicenseNumber,
  2836. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  2837. StorehouseId: warehouse.StorehouseId,
  2838. }
  2839. warehouseOutInfo.Count = goods.Count
  2840. //添加出库单详情
  2841. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  2842. if errOne != nil {
  2843. return errOne
  2844. }
  2845. stockFlow := models.VmStockFlow{
  2846. WarehousingId: warehouse.ID,
  2847. GoodId: goods.GoodId,
  2848. Number: warehouse.Number,
  2849. LicenseNumber: goods.LicenseNumber,
  2850. Count: goods.Count,
  2851. UserOrgId: goods.OrgId,
  2852. PatientId: goods.PatientId,
  2853. SystemTime: record_time,
  2854. ConsumableType: 2,
  2855. IsSys: goods.IsSys,
  2856. WarehousingOrder: "",
  2857. WarehouseOutId: goods.WarehouseOutId,
  2858. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2859. IsEdit: 1,
  2860. CancelStockId: 0,
  2861. CancelOrderNumber: "",
  2862. Manufacturer: goods.Manufacturer,
  2863. Dealer: goods.Dealer,
  2864. Creator: creator,
  2865. UpdateCreator: 0,
  2866. Status: 1,
  2867. Ctime: time.Now().Unix(),
  2868. Mtime: 0,
  2869. Price: warehouse.PackingPrice,
  2870. WarehousingDetailId: 0,
  2871. WarehouseOutDetailId: warehouseOutInfo.ID,
  2872. CancelOutDetailId: 0,
  2873. ProductDate: goods.ProductDate,
  2874. ExpireDate: goods.ExpiryDate,
  2875. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  2876. SupplyWarehouseId: warehouseOut.SupplyWarehouseId,
  2877. StorehouseId: warehouse.StorehouseId,
  2878. }
  2879. //创建出库流水
  2880. CreateStockFlowOne(stockFlow)
  2881. maxNumber = goods.Count
  2882. if warehouse.StockCount < maxNumber {
  2883. return errors.New("库存数量不足")
  2884. }
  2885. warehouse.StockCount = warehouse.StockCount - maxNumber
  2886. warehouse.Mtime = time.Now().Unix()
  2887. //扣减库存
  2888. errThree := UpDateWarehouseInfoByStock(&warehouse)
  2889. ModifyGoodReduceInformationSix(warehouse.GoodId, maxNumber, warehouse.OrgId, warehouse.PackingPrice)
  2890. if errThree != nil {
  2891. return errThree
  2892. }
  2893. } else {
  2894. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2895. warehouseOutInfo := &models.WarehouseOutInfo{
  2896. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2897. WarehouseOutId: goods.WarehouseOutId,
  2898. WarehouseInfotId: warehouse.ID,
  2899. Status: 1,
  2900. Ctime: time.Now().Unix(),
  2901. Remark: goods.Remark,
  2902. OrgId: orgID,
  2903. Type: 1,
  2904. Manufacturer: goods.Manufacturer,
  2905. Dealer: goods.Dealer,
  2906. IsSys: 0,
  2907. SysRecordTime: record_time,
  2908. GoodTypeId: goods.GoodTypeId,
  2909. GoodId: goods.GoodId,
  2910. ExpiryDate: goods.ExpiryDate,
  2911. ProductDate: goods.ProductDate,
  2912. Number: warehouse.Number,
  2913. Price: warehouse.PackingPrice,
  2914. LicenseNumber: goods.LicenseNumber,
  2915. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  2916. StorehouseId: warehouse.StorehouseId,
  2917. }
  2918. warehouseOutInfo.Count = stock_number
  2919. //查询是否已经生成出库单
  2920. info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  2921. if errcodes == gorm.ErrRecordNotFound {
  2922. //创建出库单
  2923. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  2924. if errOne != nil {
  2925. return errOne
  2926. }
  2927. } else {
  2928. warehouseOutInfo.Count = info.Count + stock_number
  2929. UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  2930. }
  2931. stockFlow := models.VmStockFlow{
  2932. WarehousingId: warehouse.ID,
  2933. GoodId: goods.GoodId,
  2934. Number: warehouse.Number,
  2935. LicenseNumber: goods.LicenseNumber,
  2936. Count: stock_number,
  2937. UserOrgId: goods.OrgId,
  2938. PatientId: 0,
  2939. SystemTime: record_time,
  2940. ConsumableType: 2,
  2941. IsSys: 0,
  2942. WarehousingOrder: "",
  2943. WarehouseOutId: goods.WarehouseOutId,
  2944. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2945. IsEdit: 1,
  2946. CancelStockId: 0,
  2947. CancelOrderNumber: "",
  2948. Manufacturer: goods.Manufacturer,
  2949. Dealer: goods.Dealer,
  2950. Creator: creator,
  2951. UpdateCreator: 0,
  2952. Status: 1,
  2953. Ctime: time.Now().Unix(),
  2954. Mtime: 0,
  2955. Price: warehouse.PackingPrice,
  2956. WarehousingDetailId: 0,
  2957. WarehouseOutDetailId: warehouseOutInfo.ID,
  2958. CancelOutDetailId: 0,
  2959. ProductDate: goods.ProductDate,
  2960. ExpireDate: goods.ExpiryDate,
  2961. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  2962. SupplyWarehouseId: warehouseOut.SupplyWarehouseId,
  2963. StorehouseId: warehouse.StorehouseId,
  2964. }
  2965. //创建出库明细
  2966. CreateStockFlowOne(stockFlow)
  2967. // 出库完成后,要将该批次库存清零
  2968. ModifyGoodReduceInformationSix(warehouse.GoodId, warehouse.StockCount, warehouse.OrgId, warehouse.PackingPrice)
  2969. warehouse.StockCount = 0
  2970. warehouse.Mtime = time.Now().Unix()
  2971. errThree := UpDateWarehouseInfoByStock(&warehouse)
  2972. if errThree != nil {
  2973. return errThree
  2974. }
  2975. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2976. goods.Count = deliver_number - stock_number
  2977. ConsumablesDeliveryNight(orgID, record_time, goods, warehouseOut, count, creator, warehouse_out_id, cancel_out_id)
  2978. }
  2979. return nil
  2980. }
  2981. func FindFirstWarehousingInfoByStockNight(good_id int64, good_type_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  2982. if is_source == 1 {
  2983. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND good_type_id = ? AND status = 1 and stock_count > 0 and supply_warehouse_id = ? and storehouse_id = ?", good_id, good_type_id, warehouse_out_id, storehouse_id).Order("ctime").First(&info).Error
  2984. }
  2985. if is_source == 2 || is_source == 0 {
  2986. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND good_type_id = ? AND status = 1 and stock_count > 0 and storehouse_id = ?", good_id, good_type_id, storehouse_id).Order("ctime").First(&info).Error
  2987. }
  2988. return info, err
  2989. }
  2990. // 药品出库 递归方式
  2991. func AutoDrugDeliverInfoNight(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo, warehouse_out_id int64, cancel_out_id int64) (err error) {
  2992. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  2993. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  2994. var deliver_number int64 = 0
  2995. var stock_number int64 = 0
  2996. if advice.CountUnit == drup.MaxUnit {
  2997. deliver_number = prescribingNumber * drup.MinNumber
  2998. } else {
  2999. deliver_number = prescribingNumber
  3000. }
  3001. // 根据先进先出原则,查询最先入库的批次,进行出库
  3002. // 如果没有对应的库存,则报错
  3003. //开启事物
  3004. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, houseConfig.DrugStorehouseOut)
  3005. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  3006. var stockMax int64
  3007. var stockMin int64
  3008. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  3009. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  3010. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  3011. UpdateMinNumber(lastWarehouse.ID, stockMin)
  3012. }
  3013. warehouse, err := FindLastDrugWarehousingInfoByIDSix(advice.DrugId, warehouse_out_id, advice.IsSource, houseConfig.DrugStorehouseOut)
  3014. if err != nil {
  3015. return err
  3016. }
  3017. // 将该批次的剩余库存数量转换为拆零数量
  3018. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  3019. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3020. if stock_number >= deliver_number {
  3021. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  3022. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3023. WarehouseOutId: advice.WarehouseOutId,
  3024. Status: 1,
  3025. Ctime: time.Now().Unix(),
  3026. Remark: advice.Remark,
  3027. OrgId: orgID,
  3028. Type: 1,
  3029. Manufacturer: advice.Manufacturer,
  3030. Dealer: warehouse.Dealer,
  3031. IsSys: 0,
  3032. SysRecordTime: advice.SysRecordTime,
  3033. DrugId: advice.DrugId,
  3034. ExpiryDate: advice.ExpiryDate,
  3035. ProductDate: advice.ProductDate,
  3036. Number: advice.Number,
  3037. BatchNumber: warehouse.BatchNumber,
  3038. Count: deliver_number,
  3039. RetailPrice: advice.RetailPrice,
  3040. Price: advice.Price,
  3041. WarehouseInfoId: warehouse.ID,
  3042. CountUnit: drup.MinUnit,
  3043. AdviceId: advice.ID,
  3044. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  3045. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  3046. StorehouseId: warehouse.StorehouseId,
  3047. IsCheck: 1,
  3048. }
  3049. warehouseOutInfo.Count = prescribingNumber
  3050. warehouseOutInfo.CountUnit = advice.CountUnit
  3051. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  3052. if errOne != nil {
  3053. return errOne
  3054. }
  3055. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  3056. drugflow := models.DrugFlow{
  3057. WarehouseOutId: advice.WarehouseOutId,
  3058. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3059. DrugId: advice.DrugId,
  3060. Number: warehouse.Number,
  3061. ProductDate: advice.ProductDate,
  3062. ExpireDate: advice.ExpiryDate,
  3063. Count: deliver_number,
  3064. Price: advice.Price,
  3065. Status: 1,
  3066. Ctime: time.Now().Unix(),
  3067. UserOrgId: advice.OrgId,
  3068. Manufacturer: advice.Manufacturer,
  3069. Dealer: advice.Dealer,
  3070. BatchNumber: warehouse.BatchNumber,
  3071. MaxUnit: drup.MinUnit,
  3072. ConsumableType: 2,
  3073. IsEdit: 1,
  3074. Creator: 0,
  3075. IsSys: 0,
  3076. WarehouseOutDetailId: drugWareInfo.ID,
  3077. AdviceId: advice.ID,
  3078. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  3079. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  3080. SystemTime: advice.SysRecordTime,
  3081. StorehouseId: warehouse.StorehouseId,
  3082. }
  3083. CreateDrugFlowOne(drugflow)
  3084. // 出库完成后,要减去对应批次的库存数量
  3085. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  3086. var maxNumber int64 = 0
  3087. var minNumber int64 = 0
  3088. if advice.CountUnit == drup.MinUnit {
  3089. maxNumber = prescribingNumber / drup.MinNumber
  3090. minNumber = prescribingNumber % drup.MinNumber
  3091. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  3092. minNumber = maxNumber
  3093. }
  3094. } else {
  3095. maxNumber = prescribingNumber
  3096. }
  3097. if warehouse.StockMaxNumber < maxNumber {
  3098. return errors.New("库存数量不足")
  3099. }
  3100. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  3101. if warehouse.StockMaxNumber < 0 {
  3102. warehouse.StockMaxNumber = 0
  3103. }
  3104. warehouse.Mtime = time.Now().Unix()
  3105. if warehouse.StockMinNumber < minNumber {
  3106. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  3107. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  3108. } else {
  3109. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  3110. }
  3111. if warehouse.StockMaxNumber < 0 {
  3112. return errors.New("库存数量不足")
  3113. }
  3114. if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
  3115. warehouse.StockMinNumber = 0
  3116. }
  3117. if warehouse.StockMaxNumber > 0 {
  3118. var sum_count int64
  3119. sum_count = maxNumber * drup.MinNumber
  3120. ModifyDrugReduceInformationSix(warehouse.DrugId, sum_count, warehouse.OrgId, warehouse.RetailPrice)
  3121. }
  3122. if warehouse.StockMinNumber > 0 {
  3123. ModifyDrugReduceInformationSix(warehouse.DrugId, minNumber, warehouse.OrgId, warehouse.RetailPrice)
  3124. }
  3125. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  3126. if errThree != nil {
  3127. return errThree
  3128. }
  3129. return nil
  3130. } else {
  3131. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  3132. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  3133. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3134. WarehouseOutId: advice.WarehouseOutId,
  3135. Status: 1,
  3136. Ctime: time.Now().Unix(),
  3137. Remark: advice.Remark,
  3138. OrgId: orgID,
  3139. Type: 1,
  3140. Manufacturer: advice.Manufacturer,
  3141. Dealer: warehouse.Dealer,
  3142. IsSys: 0,
  3143. SysRecordTime: advice.SysRecordTime,
  3144. DrugId: advice.DrugId,
  3145. ExpiryDate: warehouse.ExpiryDate,
  3146. ProductDate: warehouse.ProductDate,
  3147. Number: warehouse.Number,
  3148. BatchNumber: warehouse.BatchNumber,
  3149. Count: stock_number,
  3150. RetailPrice: advice.RetailPrice,
  3151. Price: advice.Price,
  3152. WarehouseInfoId: warehouse.ID,
  3153. CountUnit: drup.MinUnit,
  3154. AdviceId: advice.ID,
  3155. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  3156. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  3157. StorehouseId: warehouse.StorehouseId,
  3158. IsCheck: 1,
  3159. }
  3160. warehouseOutInfo.Count = stock_number
  3161. //warehouseOutInfo.CountUnit = advice.CountUnit
  3162. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  3163. if errOne != nil {
  3164. return errOne
  3165. }
  3166. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  3167. drugflow := models.DrugFlow{
  3168. WarehouseOutId: advice.WarehouseOutId,
  3169. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3170. DrugId: advice.DrugId,
  3171. Number: warehouse.Number,
  3172. ProductDate: advice.ProductDate,
  3173. ExpireDate: advice.ExpiryDate,
  3174. Count: stock_number,
  3175. Price: advice.Price,
  3176. Status: 1,
  3177. Ctime: time.Now().Unix(),
  3178. UserOrgId: advice.OrgId,
  3179. Manufacturer: advice.Manufacturer,
  3180. Dealer: advice.Dealer,
  3181. BatchNumber: warehouse.BatchNumber,
  3182. MaxUnit: drup.MinUnit,
  3183. ConsumableType: 2,
  3184. IsEdit: 1,
  3185. Creator: 0,
  3186. IsSys: 0,
  3187. WarehouseOutDetailId: drugWareInfo.ID,
  3188. AdviceId: advice.ID,
  3189. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  3190. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  3191. SystemTime: advice.SysRecordTime,
  3192. StorehouseId: warehouse.StorehouseId,
  3193. }
  3194. CreateDrugFlowOne(drugflow)
  3195. if warehouse.StockMaxNumber > 0 {
  3196. var sum_count int64
  3197. sum_count = warehouse.StockMaxNumber * drup.MinNumber
  3198. ModifyDrugReduceInformationSix(warehouse.DrugId, sum_count, warehouse.OrgId, warehouse.RetailPrice)
  3199. }
  3200. if warehouse.StockMinNumber > 0 {
  3201. ModifyDrugReduceInformationSix(warehouse.DrugId, warehouse.StockMinNumber, warehouse.OrgId, warehouse.RetailPrice)
  3202. }
  3203. // 出库完成后,要将该批次库存清零
  3204. warehouse.StockMaxNumber = 0
  3205. warehouse.StockMinNumber = 0
  3206. warehouse.Mtime = time.Now().Unix()
  3207. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  3208. if errThree != nil {
  3209. return errThree
  3210. }
  3211. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3212. prescribingNumber_two_temp := deliver_number - stock_number
  3213. advice.CountUnit = drup.MinUnit
  3214. AutoDrugDeliverInfoNight(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, warehouse_out_id, cancel_out_id)
  3215. }
  3216. return
  3217. }
  3218. //调拨出库逻辑
  3219. func ConsumablesDeliveryTwelve(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64, warehouseInfo *models.WarehousingInfo) (err error) {
  3220. var deliver_number int64 = 0
  3221. var stock_number int64 = 0
  3222. var maxNumber int64 = 0
  3223. deliver_number = goods.Count
  3224. // 根据先进先出原则,查询最先入库的批次,进行出库
  3225. // 如果没有对应的库存,则报错
  3226. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  3227. if err != nil {
  3228. return err
  3229. }
  3230. stock_number = warehouse.StockCount
  3231. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3232. if stock_number >= deliver_number {
  3233. warehouseOutInfo := &models.WarehouseOutInfo{
  3234. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  3235. WarehouseOutId: goods.WarehouseOutId,
  3236. WarehouseInfotId: warehouse.ID,
  3237. Status: 1,
  3238. Ctime: time.Now().Unix(),
  3239. Remark: goods.Remark,
  3240. OrgId: orgID,
  3241. Type: 1,
  3242. Manufacturer: warehouse.Manufacturer,
  3243. Dealer: warehouse.Dealer,
  3244. IsSys: 0,
  3245. SysRecordTime: record_time,
  3246. GoodTypeId: warehouse.GoodTypeId,
  3247. GoodId: warehouse.GoodId,
  3248. ExpiryDate: warehouse.ExpiryDate,
  3249. ProductDate: warehouse.ProductDate,
  3250. Number: warehouse.Number,
  3251. Price: warehouse.PackingPrice,
  3252. LicenseNumber: warehouse.LicenseNumber,
  3253. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3254. StorehouseId: goods.StorehouseId,
  3255. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  3256. IsCheck: 1,
  3257. }
  3258. warehouseOutInfo.Count = goods.Count
  3259. //添加出库单详情
  3260. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  3261. if errOne != nil {
  3262. return errOne
  3263. }
  3264. stockFlow := models.VmStockFlow{
  3265. WarehousingId: warehouse.ID,
  3266. GoodId: goods.GoodId,
  3267. Number: warehouse.Number,
  3268. LicenseNumber: goods.LicenseNumber,
  3269. Count: goods.Count,
  3270. UserOrgId: goods.OrgId,
  3271. PatientId: goods.PatientId,
  3272. SystemTime: record_time,
  3273. ConsumableType: 12,
  3274. IsSys: goods.IsSys,
  3275. WarehousingOrder: "",
  3276. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  3277. WarehouseOutId: goods.WarehouseOutId,
  3278. IsEdit: 1,
  3279. CancelStockId: 0,
  3280. CancelOrderNumber: "",
  3281. Manufacturer: warehouse.Manufacturer,
  3282. Dealer: warehouse.Dealer,
  3283. Creator: creator,
  3284. UpdateCreator: 0,
  3285. Status: 1,
  3286. Ctime: time.Now().Unix(),
  3287. Mtime: 0,
  3288. Price: warehouse.PackingPrice,
  3289. WarehousingDetailId: 0,
  3290. WarehouseOutDetailId: warehouseOutInfo.ID,
  3291. CancelOutDetailId: 0,
  3292. ProductDate: warehouse.ProductDate,
  3293. ExpireDate: warehouse.ExpiryDate,
  3294. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3295. StorehouseId: warehouse.StorehouseId,
  3296. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  3297. }
  3298. //创建出库流水
  3299. CreateStockFlowOne(stockFlow)
  3300. maxNumber = goods.Count
  3301. if warehouse.StockCount < maxNumber {
  3302. return errors.New("库存数量不足")
  3303. }
  3304. warehouse.StockCount = warehouse.StockCount - maxNumber
  3305. warehouse.Mtime = time.Now().Unix()
  3306. //扣减库存
  3307. errThree := UpDateWarehouseInfoByStock(&warehouse)
  3308. //入库
  3309. warehouseInfo := &models.WarehousingInfo{
  3310. WarehousingOrder: warehouseInfo.WarehousingOrder,
  3311. WarehousingId: warehouseInfo.WarehousingId,
  3312. GoodId: warehouse.GoodId,
  3313. Number: warehouse.Number,
  3314. GoodTypeId: warehouse.GoodTypeId,
  3315. ProductDate: warehouse.ProductDate,
  3316. ExpiryDate: warehouse.ExpiryDate,
  3317. WarehousingCount: goods.Count,
  3318. Price: warehouse.Price,
  3319. TotalPrice: 0,
  3320. Status: 1,
  3321. Ctime: warehouse.Ctime,
  3322. Remark: goods.Remark,
  3323. OrgId: warehouse.OrgId,
  3324. Type: 1,
  3325. Manufacturer: warehouse.Manufacturer,
  3326. StockCount: goods.Count,
  3327. Dealer: warehouse.Dealer,
  3328. LicenseNumber: "",
  3329. PackingPrice: warehouse.PackingPrice,
  3330. StorehouseId: warehouseInfo.StorehouseId,
  3331. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  3332. IsCheck: 1,
  3333. }
  3334. AddWarehosingInfo(warehouseInfo)
  3335. stockInfoFlow := models.VmStockFlow{
  3336. WarehousingId: warehouseInfo.WarehousingId,
  3337. GoodId: goods.GoodId,
  3338. Number: warehouse.Number,
  3339. LicenseNumber: goods.LicenseNumber,
  3340. Count: goods.Count,
  3341. UserOrgId: goods.OrgId,
  3342. PatientId: goods.PatientId,
  3343. SystemTime: record_time,
  3344. ConsumableType: 13,
  3345. IsSys: goods.IsSys,
  3346. WarehousingOrder: warehouseInfo.WarehousingOrder,
  3347. WarehouseOutId: 0,
  3348. WarehouseOutOrderNumber: "",
  3349. IsEdit: 1,
  3350. CancelStockId: 0,
  3351. CancelOrderNumber: "",
  3352. Manufacturer: warehouse.Manufacturer,
  3353. Dealer: warehouse.Dealer,
  3354. Creator: creator,
  3355. UpdateCreator: 0,
  3356. Status: 1,
  3357. Ctime: time.Now().Unix(),
  3358. Mtime: 0,
  3359. Price: warehouse.Price,
  3360. WarehousingDetailId: 0,
  3361. WarehouseOutDetailId: warehouseOutInfo.ID,
  3362. CancelOutDetailId: 0,
  3363. ProductDate: warehouse.ProductDate,
  3364. ExpireDate: warehouse.ExpiryDate,
  3365. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3366. StorehouseId: warehouseInfo.StorehouseId,
  3367. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  3368. }
  3369. //创建入库流水
  3370. CreateStockFlowOne(stockInfoFlow)
  3371. if errThree != nil {
  3372. return errThree
  3373. }
  3374. } else {
  3375. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  3376. warehouseOutInfo := &models.WarehouseOutInfo{
  3377. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  3378. WarehouseOutId: goods.WarehouseOutId,
  3379. WarehouseInfotId: warehouse.ID,
  3380. Status: 1,
  3381. Ctime: time.Now().Unix(),
  3382. Remark: goods.Remark,
  3383. OrgId: orgID,
  3384. Type: 1,
  3385. Manufacturer: warehouse.Manufacturer,
  3386. Dealer: warehouse.Dealer,
  3387. IsSys: 0,
  3388. SysRecordTime: record_time,
  3389. GoodTypeId: warehouse.GoodTypeId,
  3390. GoodId: warehouse.GoodId,
  3391. ExpiryDate: warehouse.ExpiryDate,
  3392. ProductDate: warehouse.ProductDate,
  3393. Number: warehouse.Number,
  3394. Price: warehouse.PackingPrice,
  3395. LicenseNumber: warehouse.LicenseNumber,
  3396. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3397. StorehouseId: warehouse.StorehouseId,
  3398. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  3399. }
  3400. warehouseOutInfo.Count = warehouse.StockCount
  3401. //查询是否已经生成出库单
  3402. info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  3403. if errcodes == gorm.ErrRecordNotFound {
  3404. //创建出库单
  3405. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  3406. if errOne != nil {
  3407. return errOne
  3408. }
  3409. } else {
  3410. warehouseOutInfo.Count = info.Count + stock_number
  3411. UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  3412. }
  3413. stockFlow := models.VmStockFlow{
  3414. WarehousingId: warehouse.ID,
  3415. GoodId: goods.GoodId,
  3416. Number: warehouse.Number,
  3417. LicenseNumber: goods.LicenseNumber,
  3418. Count: warehouse.StockCount,
  3419. UserOrgId: goods.OrgId,
  3420. PatientId: 0,
  3421. SystemTime: record_time,
  3422. ConsumableType: 12,
  3423. IsSys: 0,
  3424. WarehousingOrder: "",
  3425. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  3426. WarehouseOutId: goods.WarehouseOutId,
  3427. IsEdit: 1,
  3428. CancelStockId: 0,
  3429. CancelOrderNumber: "",
  3430. Manufacturer: warehouse.Manufacturer,
  3431. Dealer: warehouse.Dealer,
  3432. Creator: creator,
  3433. UpdateCreator: 0,
  3434. Status: 1,
  3435. Ctime: time.Now().Unix(),
  3436. Mtime: 0,
  3437. Price: warehouse.PackingPrice,
  3438. WarehousingDetailId: 0,
  3439. WarehouseOutDetailId: warehouseOutInfo.ID,
  3440. CancelOutDetailId: 0,
  3441. ProductDate: warehouse.ProductDate,
  3442. ExpireDate: warehouse.ExpiryDate,
  3443. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3444. StorehouseId: goods.StorehouseId,
  3445. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  3446. }
  3447. //创建出库明细
  3448. CreateStockFlowOne(stockFlow)
  3449. //入库
  3450. warehouseInfo := &models.WarehousingInfo{
  3451. WarehousingOrder: warehouseInfo.WarehousingOrder,
  3452. WarehousingId: warehouseInfo.WarehousingId,
  3453. GoodId: warehouse.GoodId,
  3454. Number: warehouse.Number,
  3455. GoodTypeId: warehouse.GoodTypeId,
  3456. ProductDate: warehouse.ProductDate,
  3457. ExpiryDate: warehouse.ExpiryDate,
  3458. WarehousingCount: warehouse.StockCount,
  3459. Price: warehouse.Price,
  3460. TotalPrice: 0,
  3461. Status: 1,
  3462. Ctime: warehouse.Ctime,
  3463. Remark: goods.Remark,
  3464. OrgId: warehouse.OrgId,
  3465. Type: 1,
  3466. Manufacturer: warehouse.Manufacturer,
  3467. StockCount: warehouse.StockCount,
  3468. Dealer: warehouse.Dealer,
  3469. LicenseNumber: "",
  3470. PackingPrice: warehouse.PackingPrice,
  3471. StorehouseId: warehouseInfo.StorehouseId,
  3472. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  3473. IsCheck: 1,
  3474. }
  3475. AddWarehosingInfo(warehouseInfo)
  3476. stockInfoFlow := models.VmStockFlow{
  3477. WarehousingId: warehouseInfo.WarehousingId,
  3478. GoodId: goods.GoodId,
  3479. Number: warehouse.Number,
  3480. LicenseNumber: goods.LicenseNumber,
  3481. Count: warehouse.StockCount,
  3482. UserOrgId: goods.OrgId,
  3483. PatientId: goods.PatientId,
  3484. SystemTime: record_time,
  3485. ConsumableType: 13,
  3486. IsSys: goods.IsSys,
  3487. WarehousingOrder: warehouseInfo.WarehousingOrder,
  3488. WarehouseOutId: warehouseOut.ID,
  3489. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3490. IsEdit: 1,
  3491. CancelStockId: 0,
  3492. CancelOrderNumber: "",
  3493. Manufacturer: warehouse.Manufacturer,
  3494. Dealer: warehouse.Dealer,
  3495. Creator: creator,
  3496. UpdateCreator: 0,
  3497. Status: 1,
  3498. Ctime: time.Now().Unix(),
  3499. Mtime: 0,
  3500. Price: warehouse.Price,
  3501. WarehousingDetailId: 0,
  3502. WarehouseOutDetailId: warehouseOutInfo.ID,
  3503. CancelOutDetailId: 0,
  3504. ProductDate: warehouse.ProductDate,
  3505. ExpireDate: warehouse.ExpiryDate,
  3506. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3507. StorehouseId: warehouseInfo.StorehouseId,
  3508. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  3509. }
  3510. //创建出库流水
  3511. CreateStockFlowOne(stockInfoFlow)
  3512. // 出库完成后,要将该批次库存清零
  3513. warehouse.StockCount = 0
  3514. warehouse.Mtime = time.Now().Unix()
  3515. errThree := UpDateWarehouseInfoByStock(&warehouse)
  3516. if errThree != nil {
  3517. return errThree
  3518. }
  3519. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3520. goods.Count = deliver_number - stock_number
  3521. ConsumablesDeliveryTwelve(orgID, record_time, goods, warehouseOut, count, creator, warehouseInfo)
  3522. }
  3523. return nil
  3524. }
  3525. func AddWarehosingInfo(info *models.WarehousingInfo) error {
  3526. err = XTWriteDB().Create(&info).Error
  3527. return err
  3528. }
  3529. // 药品出库 递归方式
  3530. func AutoDrugDeliverInfoTwelve(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo, drugWarehouse *models.DrugWarehouseInfo) (err error) {
  3531. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  3532. var deliver_number int64 = 0
  3533. var stock_number int64 = 0
  3534. if advice.CountUnit == drup.MaxUnit {
  3535. deliver_number = prescribingNumber * drup.MinNumber
  3536. } else {
  3537. deliver_number = prescribingNumber
  3538. }
  3539. // 根据先进先出原则,查询最先入库的批次,进行出库
  3540. // 如果没有对应的库存,则报错
  3541. //开启事物
  3542. storeConfig, _ := GetAllStoreHouseConfig(orgID)
  3543. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  3544. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  3545. var stockMax int64
  3546. var stockMin int64
  3547. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  3548. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  3549. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  3550. UpdateMinNumber(lastWarehouse.ID, stockMin)
  3551. }
  3552. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  3553. if err != nil {
  3554. return err
  3555. }
  3556. // 将该批次的剩余库存数量转换为拆零数量
  3557. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  3558. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3559. if stock_number >= deliver_number {
  3560. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  3561. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  3562. WarehouseOutId: advice.WarehouseOutId,
  3563. Status: 1,
  3564. Ctime: time.Now().Unix(),
  3565. Remark: advice.Remark,
  3566. OrgId: orgID,
  3567. Type: 1,
  3568. Manufacturer: warehouse.Manufacturer,
  3569. Dealer: warehouse.Dealer,
  3570. IsSys: 12,
  3571. SysRecordTime: advice.Ctime,
  3572. DrugId: advice.DrugId,
  3573. ExpiryDate: warehouse.ExpiryDate,
  3574. ProductDate: warehouse.ProductDate,
  3575. Number: warehouse.Number,
  3576. BatchNumber: warehouse.BatchNumber,
  3577. Count: deliver_number,
  3578. RetailPrice: warehouse.RetailPrice,
  3579. Price: warehouse.RetailPrice,
  3580. WarehouseInfoId: warehouse.ID,
  3581. CountUnit: drup.MinUnit,
  3582. AdviceId: advice.ID,
  3583. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3584. StorehouseId: advice.StorehouseId,
  3585. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  3586. IsCheck: 1,
  3587. }
  3588. warehouseOutInfo.Count = prescribingNumber
  3589. warehouseOutInfo.CountUnit = advice.CountUnit
  3590. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  3591. if errOne != nil {
  3592. return errOne
  3593. }
  3594. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  3595. drugflow := models.DrugFlow{
  3596. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  3597. WarehouseOutId: advice.WarehouseOutId,
  3598. DrugId: advice.DrugId,
  3599. Number: warehouse.Number,
  3600. ProductDate: warehouse.ProductDate,
  3601. ExpireDate: warehouse.ExpiryDate,
  3602. Count: deliver_number,
  3603. Price: warehouse.RetailPrice,
  3604. Status: 1,
  3605. Ctime: time.Now().Unix(),
  3606. UserOrgId: advice.OrgId,
  3607. Manufacturer: warehouse.Manufacturer,
  3608. Dealer: warehouse.Dealer,
  3609. BatchNumber: warehouse.BatchNumber,
  3610. MaxUnit: drup.MinUnit,
  3611. ConsumableType: 12,
  3612. IsEdit: 1,
  3613. Creator: 0,
  3614. IsSys: 12,
  3615. WarehouseOutDetailId: drugWareInfo.ID,
  3616. AdviceId: advice.ID,
  3617. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3618. StorehouseId: advice.StorehouseId,
  3619. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  3620. }
  3621. CreateDrugFlowOne(drugflow)
  3622. // 出库完成后,要减去对应批次的库存数量
  3623. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  3624. var maxNumber int64 = 0
  3625. var minNumber int64 = 0
  3626. if advice.CountUnit == drup.MinUnit {
  3627. maxNumber = prescribingNumber / drup.MinNumber
  3628. minNumber = prescribingNumber % drup.MinNumber
  3629. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  3630. minNumber = maxNumber
  3631. }
  3632. } else {
  3633. maxNumber = prescribingNumber
  3634. }
  3635. if warehouse.StockMaxNumber < maxNumber {
  3636. return errors.New("库存数量不足")
  3637. }
  3638. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  3639. warehouse.Mtime = time.Now().Unix()
  3640. if warehouse.StockMinNumber < minNumber {
  3641. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  3642. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  3643. } else {
  3644. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  3645. }
  3646. if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
  3647. warehouse.StockMinNumber = 0
  3648. }
  3649. if warehouse.StockMaxNumber < 0 {
  3650. return errors.New("库存数量不足")
  3651. }
  3652. ////扣减库存
  3653. //if(warehouse.StockMaxNumber > 0){
  3654. // var sum_count int64
  3655. // sum_count = maxNumber * drup.MinNumber
  3656. // ModifyDrugReduceInformationSix(warehouse.DrugId,sum_count,warehouse.OrgId,warehouse.RetailPrice)
  3657. //}
  3658. //if(warehouse.StockMinNumber > 0){
  3659. // var sum_count int64
  3660. // sum_count = minNumber
  3661. // ModifyDrugReduceInformationSix(warehouse.DrugId,sum_count,warehouse.OrgId,warehouse.RetailPrice)
  3662. //}
  3663. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  3664. if errThree != nil {
  3665. return errThree
  3666. }
  3667. //入库
  3668. drugInfo := models.DrugWarehouseInfo{
  3669. WarehousingOrder: drugWarehouse.WarehousingOrder,
  3670. WarehousingId: drugWarehouse.WarehousingId,
  3671. DrugId: warehouse.DrugId,
  3672. Number: warehouse.Number,
  3673. ProductDate: warehouse.ProductDate,
  3674. ExpiryDate: warehouse.ExpiryDate,
  3675. WarehousingCount: deliver_number,
  3676. Price: warehouse.RetailPrice,
  3677. TotalPrice: warehouse.TotalPrice,
  3678. Status: 1,
  3679. Ctime: warehouse.Ctime,
  3680. Remark: "",
  3681. OrgId: warehouse.OrgId,
  3682. Type: 13,
  3683. Manufacturer: warehouse.Manufacturer,
  3684. Dealer: warehouse.Dealer,
  3685. StockMaxNumber: 0,
  3686. StockMinNumber: deliver_number,
  3687. BatchNumber: warehouse.BatchNumber,
  3688. MaxUnit: drup.MinUnit,
  3689. MinUnit: drup.MinUnit,
  3690. RetailPrice: warehouse.RetailPrice,
  3691. StorehouseId: drugWarehouse.StorehouseId,
  3692. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  3693. IsCheck: 1,
  3694. }
  3695. CreateDrugWarehouseInfo(drugInfo)
  3696. flow := models.DrugFlow{
  3697. WarehousingId: drugWarehouse.WarehousingId,
  3698. DrugId: warehouse.DrugId,
  3699. Number: warehouse.Number,
  3700. BatchNumber: warehouse.BatchNumber,
  3701. Count: deliver_number,
  3702. UserOrgId: warehouse.OrgId,
  3703. PatientId: 0,
  3704. SystemTime: 0,
  3705. ConsumableType: 13,
  3706. IsSys: 13,
  3707. WarehousingOrder: drugWarehouse.WarehousingOrder,
  3708. WarehouseOutId: 0,
  3709. WarehouseOutOrderNumber: "",
  3710. IsEdit: 0,
  3711. CancelStockId: 0,
  3712. CancelOrderNumber: "",
  3713. Manufacturer: warehouse.Manufacturer,
  3714. Dealer: warehouse.Dealer,
  3715. Creator: 0,
  3716. UpdateCreator: 0,
  3717. Status: 1,
  3718. Ctime: time.Now().Unix(),
  3719. Mtime: 0,
  3720. Price: warehouse.RetailPrice,
  3721. WarehousingDetailId: warehouse.ID,
  3722. WarehouseOutDetailId: 0,
  3723. CancelOutDetailId: 0,
  3724. ExpireDate: warehouse.ExpiryDate,
  3725. ProductDate: warehouse.ProductDate,
  3726. MaxUnit: drup.MinUnit,
  3727. MinUnit: drup.MinUnit,
  3728. StockMaxNumber: 0,
  3729. StockMinNumber: deliver_number,
  3730. StorehouseId: drugWarehouse.StorehouseId,
  3731. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  3732. }
  3733. CreateDrugFlowOne(flow)
  3734. return nil
  3735. } else {
  3736. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  3737. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  3738. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  3739. WarehouseOutId: advice.WarehouseOutId,
  3740. Status: 1,
  3741. Ctime: time.Now().Unix(),
  3742. Remark: advice.Remark,
  3743. OrgId: orgID,
  3744. Type: 1,
  3745. Manufacturer: advice.Manufacturer,
  3746. Dealer: warehouse.Dealer,
  3747. IsSys: 12,
  3748. SysRecordTime: advice.Ctime,
  3749. DrugId: advice.DrugId,
  3750. ExpiryDate: warehouse.ExpiryDate,
  3751. ProductDate: warehouse.ProductDate,
  3752. Number: warehouse.Number,
  3753. BatchNumber: warehouse.BatchNumber,
  3754. Count: stock_number,
  3755. RetailPrice: advice.RetailPrice,
  3756. Price: warehouse.RetailPrice,
  3757. WarehouseInfoId: warehouse.ID,
  3758. CountUnit: drup.MinUnit,
  3759. AdviceId: advice.ID,
  3760. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3761. StorehouseId: advice.StorehouseId,
  3762. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  3763. IsCheck: 1,
  3764. }
  3765. warehouseOutInfo.Count = stock_number
  3766. //warehouseOutInfo.CountUnit = advice.CountUnit
  3767. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  3768. if errOne != nil {
  3769. return errOne
  3770. }
  3771. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  3772. drugflow := models.DrugFlow{
  3773. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  3774. WarehouseOutId: advice.WarehouseOutId,
  3775. DrugId: advice.DrugId,
  3776. Number: warehouse.Number,
  3777. ProductDate: warehouse.ProductDate,
  3778. ExpireDate: warehouse.ExpiryDate,
  3779. Count: stock_number,
  3780. Price: warehouse.RetailPrice,
  3781. Status: 1,
  3782. Ctime: time.Now().Unix(),
  3783. UserOrgId: advice.OrgId,
  3784. Manufacturer: warehouse.Manufacturer,
  3785. Dealer: warehouse.Dealer,
  3786. BatchNumber: warehouse.BatchNumber,
  3787. MaxUnit: drup.MinUnit,
  3788. ConsumableType: 12,
  3789. IsEdit: 1,
  3790. Creator: 0,
  3791. IsSys: 12,
  3792. WarehouseOutDetailId: drugWareInfo.ID,
  3793. AdviceId: advice.ID,
  3794. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3795. StorehouseId: advice.StorehouseId,
  3796. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  3797. }
  3798. CreateDrugFlowOne(drugflow)
  3799. // 出库完成后,要将该批次库存清零
  3800. //入库
  3801. drugInfo := models.DrugWarehouseInfo{
  3802. WarehousingOrder: drugWarehouse.WarehousingOrder,
  3803. WarehousingId: drugWarehouse.WarehousingId,
  3804. DrugId: warehouse.DrugId,
  3805. Number: warehouse.Number,
  3806. ProductDate: warehouse.ProductDate,
  3807. ExpiryDate: warehouse.ExpiryDate,
  3808. WarehousingCount: stock_number,
  3809. Price: warehouse.RetailPrice,
  3810. TotalPrice: warehouse.TotalPrice,
  3811. Status: 1,
  3812. Ctime: warehouse.Ctime,
  3813. Remark: "",
  3814. OrgId: warehouse.OrgId,
  3815. Type: 13,
  3816. Manufacturer: warehouse.Manufacturer,
  3817. Dealer: warehouse.Dealer,
  3818. StockMaxNumber: 0,
  3819. StockMinNumber: stock_number,
  3820. RetailTotalPrice: 0,
  3821. BatchNumber: warehouse.BatchNumber,
  3822. MaxUnit: drup.MinUnit,
  3823. MinUnit: drup.MinUnit,
  3824. RetailPrice: warehouse.RetailPrice,
  3825. StorehouseId: drugWarehouse.StorehouseId,
  3826. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  3827. }
  3828. CreateDrugWarehouseInfo(drugInfo)
  3829. flow := models.DrugFlow{
  3830. WarehousingId: drugWarehouse.WarehousingId,
  3831. DrugId: warehouse.DrugId,
  3832. Number: warehouse.Number,
  3833. BatchNumber: warehouse.BatchNumber,
  3834. Count: stock_number,
  3835. UserOrgId: warehouse.OrgId,
  3836. PatientId: 0,
  3837. SystemTime: 0,
  3838. ConsumableType: 13,
  3839. IsSys: 13,
  3840. WarehousingOrder: drugWarehouse.WarehousingOrder,
  3841. WarehouseOutId: 0,
  3842. WarehouseOutOrderNumber: "",
  3843. IsEdit: 0,
  3844. CancelStockId: 0,
  3845. CancelOrderNumber: "",
  3846. Manufacturer: warehouse.Manufacturer,
  3847. Dealer: warehouse.Dealer,
  3848. Creator: 0,
  3849. UpdateCreator: 0,
  3850. Status: 1,
  3851. Ctime: time.Now().Unix(),
  3852. Mtime: 0,
  3853. Price: warehouse.RetailPrice,
  3854. WarehousingDetailId: warehouse.ID,
  3855. WarehouseOutDetailId: 0,
  3856. CancelOutDetailId: 0,
  3857. ExpireDate: warehouse.ExpiryDate,
  3858. ProductDate: warehouse.ProductDate,
  3859. MaxUnit: drup.MinUnit,
  3860. MinUnit: drup.MinUnit,
  3861. StockMaxNumber: 0,
  3862. StockMinNumber: stock_number,
  3863. StorehouseId: drugWarehouse.StorehouseId,
  3864. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  3865. }
  3866. CreateDrugFlowOne(flow)
  3867. warehouse.StockMaxNumber = 0
  3868. warehouse.StockMinNumber = 0
  3869. warehouse.Mtime = time.Now().Unix()
  3870. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  3871. if errThree != nil {
  3872. return errThree
  3873. }
  3874. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3875. prescribingNumber_two_temp := deliver_number - stock_number
  3876. advice.CountUnit = drup.MinUnit
  3877. AutoDrugDeliverInfoTwelve(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, drugWarehouse)
  3878. }
  3879. return
  3880. }
  3881. // 药品手动出库 递归方式
  3882. func AutoDrugDeliverInfoTwenty(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  3883. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  3884. var deliver_number int64 = 0
  3885. var stock_number int64 = 0
  3886. if advice.CountUnit == drup.MaxUnit {
  3887. deliver_number = prescribingNumber * drup.MinNumber
  3888. } else {
  3889. deliver_number = prescribingNumber
  3890. }
  3891. // 根据先进先出原则,查询最先入库的批次,进行出库
  3892. // 如果没有对应的库存,则报错
  3893. //开启事物
  3894. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  3895. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  3896. var stockMax int64
  3897. var stockMin int64
  3898. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  3899. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  3900. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  3901. UpdateMinNumber(lastWarehouse.ID, stockMin)
  3902. }
  3903. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  3904. if err != nil {
  3905. return err
  3906. }
  3907. // 将该批次的剩余库存数量转换为拆零数量
  3908. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  3909. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3910. if stock_number >= deliver_number {
  3911. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  3912. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3913. WarehouseOutId: warehouseout.ID,
  3914. Status: 1,
  3915. Ctime: time.Now().Unix(),
  3916. Remark: advice.Remark,
  3917. OrgId: orgID,
  3918. Type: 1,
  3919. Manufacturer: advice.Manufacturer,
  3920. Dealer: warehouse.Dealer,
  3921. IsSys: 0,
  3922. SysRecordTime: advice.Ctime,
  3923. DrugId: advice.DrugId,
  3924. ExpiryDate: advice.ExpiryDate,
  3925. ProductDate: advice.ProductDate,
  3926. Number: advice.Number,
  3927. BatchNumber: warehouse.BatchNumber,
  3928. Count: deliver_number,
  3929. RetailPrice: advice.RetailPrice,
  3930. Price: warehouse.RetailPrice,
  3931. WarehouseInfoId: warehouse.ID,
  3932. CountUnit: drup.MinUnit,
  3933. AdviceId: advice.ID,
  3934. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3935. StorehouseId: advice.StorehouseId,
  3936. AdminUserId: advice.AdminUserId,
  3937. }
  3938. if warehouse.RetailPrice == 0 {
  3939. warehouseOutInfo.Price = advice.Price
  3940. }
  3941. warehouseOutInfo.Count = prescribingNumber
  3942. warehouseOutInfo.CountUnit = advice.CountUnit
  3943. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  3944. if errOne != nil {
  3945. return errOne
  3946. }
  3947. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  3948. drugflow := models.DrugFlow{
  3949. WarehouseOutId: warehouseout.ID,
  3950. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3951. DrugId: advice.DrugId,
  3952. Number: warehouse.Number,
  3953. ProductDate: advice.ProductDate,
  3954. ExpireDate: advice.ExpiryDate,
  3955. Count: deliver_number,
  3956. Price: warehouse.RetailPrice,
  3957. Status: 1,
  3958. Ctime: time.Now().Unix(),
  3959. UserOrgId: advice.OrgId,
  3960. Manufacturer: advice.Manufacturer,
  3961. Dealer: advice.Dealer,
  3962. BatchNumber: warehouse.BatchNumber,
  3963. MaxUnit: drup.MinUnit,
  3964. ConsumableType: 2,
  3965. IsEdit: 1,
  3966. Creator: 0,
  3967. IsSys: 0,
  3968. WarehouseOutDetailId: drugWareInfo.ID,
  3969. AdviceId: advice.ID,
  3970. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3971. StorehouseId: advice.StorehouseId,
  3972. AdminUserId: advice.AdminUserId,
  3973. }
  3974. if warehouse.RetailPrice == 0 {
  3975. drugflow.Price = advice.Price
  3976. }
  3977. CreateDrugFlowOne(drugflow)
  3978. // 出库完成后,要减去对应批次的库存数量
  3979. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  3980. var maxNumber int64 = 0
  3981. var minNumber int64 = 0
  3982. //var stock_max_number int64 = 0
  3983. //stock_max_number = warehouse.StockMaxNumber
  3984. maxNumber = deliver_number / drup.MinNumber
  3985. minNumber = deliver_number % drup.MinNumber
  3986. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  3987. minNumber = maxNumber
  3988. }
  3989. if drup.MaxUnit != drup.MinUnit {
  3990. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  3991. return errors.New("库存数量不足")
  3992. }
  3993. }
  3994. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  3995. if warehouse.StockMaxNumber < 0 {
  3996. warehouse.StockMaxNumber = 0
  3997. }
  3998. if warehouse.StockMinNumber < 0 {
  3999. warehouse.StockMinNumber = 0
  4000. }
  4001. warehouse.Mtime = time.Now().Unix()
  4002. if warehouse.StockMinNumber < minNumber {
  4003. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  4004. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  4005. } else {
  4006. if minNumber > 0 {
  4007. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  4008. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4009. } else {
  4010. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  4011. }
  4012. }
  4013. if minNumber == 0 && maxNumber != 1 {
  4014. if warehouse.StockMinNumber > 0 {
  4015. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4016. }
  4017. }
  4018. }
  4019. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  4020. if (warehouse.StockMinNumber - deliver_number) > 0 {
  4021. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4022. //if warehouse.StockMinNumber == 0 {
  4023. // warehouse.StockMaxNumber = stock_max_number
  4024. //}
  4025. }
  4026. }
  4027. if drup.MaxUnit != drup.MinUnit {
  4028. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  4029. return errors.New("库存数量不足")
  4030. }
  4031. }
  4032. if warehouse.StockMinNumber <= 0 {
  4033. warehouse.StockMinNumber = 0
  4034. }
  4035. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4036. if errThree != nil {
  4037. return errThree
  4038. }
  4039. //查询默认仓库
  4040. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  4041. //查询默认仓库剩余多少库存
  4042. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  4043. var sum_count int64
  4044. var sum_in_count int64
  4045. for _, it := range list {
  4046. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  4047. if it.MaxUnit == baseDrug.MaxUnit {
  4048. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  4049. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  4050. sum_count += it.StockMaxNumber + it.StockMinNumber
  4051. sum_in_count += it.WarehousingCount
  4052. }
  4053. }
  4054. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  4055. return nil
  4056. } else {
  4057. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  4058. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4059. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4060. WarehouseOutId: warehouseout.ID,
  4061. Status: 1,
  4062. Ctime: time.Now().Unix(),
  4063. Remark: advice.Remark,
  4064. OrgId: orgID,
  4065. Type: 1,
  4066. Manufacturer: advice.Manufacturer,
  4067. Dealer: warehouse.Dealer,
  4068. IsSys: 0,
  4069. SysRecordTime: advice.Ctime,
  4070. DrugId: advice.DrugId,
  4071. ExpiryDate: warehouse.ExpiryDate,
  4072. ProductDate: warehouse.ProductDate,
  4073. Number: warehouse.Number,
  4074. BatchNumber: warehouse.BatchNumber,
  4075. Count: stock_number,
  4076. RetailPrice: advice.RetailPrice,
  4077. Price: warehouse.RetailPrice,
  4078. WarehouseInfoId: warehouse.ID,
  4079. CountUnit: drup.MinUnit,
  4080. AdviceId: advice.ID,
  4081. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4082. StorehouseId: advice.StorehouseId,
  4083. AdminUserId: advice.AdminUserId,
  4084. }
  4085. if warehouse.RetailPrice == 0 {
  4086. warehouseOutInfo.Price = advice.Price
  4087. }
  4088. warehouseOutInfo.Count = stock_number
  4089. //warehouseOutInfo.CountUnit = advice.CountUnit
  4090. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  4091. if errOne != nil {
  4092. return errOne
  4093. }
  4094. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4095. drugflow := models.DrugFlow{
  4096. WarehouseOutId: warehouseout.ID,
  4097. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4098. DrugId: advice.DrugId,
  4099. Number: warehouse.Number,
  4100. ProductDate: advice.ProductDate,
  4101. ExpireDate: advice.ExpiryDate,
  4102. Count: stock_number,
  4103. Price: warehouse.RetailPrice,
  4104. Status: 1,
  4105. Ctime: time.Now().Unix(),
  4106. UserOrgId: advice.OrgId,
  4107. Manufacturer: advice.Manufacturer,
  4108. Dealer: advice.Dealer,
  4109. BatchNumber: warehouse.BatchNumber,
  4110. MaxUnit: drup.MinUnit,
  4111. ConsumableType: 2,
  4112. IsEdit: 1,
  4113. Creator: 0,
  4114. IsSys: 0,
  4115. WarehouseOutDetailId: drugWareInfo.ID,
  4116. AdviceId: advice.ID,
  4117. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4118. StorehouseId: advice.StorehouseId,
  4119. AdminUserId: advice.AdminUserId,
  4120. }
  4121. if warehouse.RetailPrice == 0 {
  4122. drugflow.Price = advice.Price
  4123. }
  4124. CreateDrugFlowOne(drugflow)
  4125. // 出库完成后,要将该批次库存清零
  4126. //扣减库存
  4127. warehouse.StockMaxNumber = 0
  4128. warehouse.StockMinNumber = 0
  4129. warehouse.Mtime = time.Now().Unix()
  4130. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4131. //查询默认仓库
  4132. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  4133. //查询默认仓库剩余多少库存
  4134. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  4135. var sum_count int64
  4136. var sum_in_count int64
  4137. for _, it := range list {
  4138. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  4139. if it.MaxUnit == baseDrug.MaxUnit {
  4140. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  4141. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  4142. sum_count += it.StockMaxNumber + it.StockMinNumber
  4143. sum_in_count += it.WarehousingCount
  4144. }
  4145. }
  4146. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  4147. if errThree != nil {
  4148. return errThree
  4149. }
  4150. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  4151. prescribingNumber_two_temp := deliver_number - stock_number
  4152. advice.CountUnit = drup.MinUnit
  4153. AutoDrugDeliverInfoTwenty(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  4154. }
  4155. return
  4156. }
  4157. // 药品手动出库 递归方式
  4158. func AutoDrugDeliverInfoTwentyOne(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  4159. //开事务
  4160. //tx := XTWriteDB().Begin()
  4161. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  4162. var deliver_number int64 = 0
  4163. var stock_number int64 = 0
  4164. if advice.CountUnit == drup.MaxUnit {
  4165. deliver_number = prescribingNumber * drup.MinNumber
  4166. } else {
  4167. deliver_number = prescribingNumber
  4168. }
  4169. // 根据先进先出原则,查询最先入库的批次,进行出库
  4170. // 如果没有对应的库存,则报错
  4171. //开启事物
  4172. storeConfig, _ := GetAllStoreHouseConfig(orgID)
  4173. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  4174. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  4175. var stockMax int64
  4176. var stockMin int64
  4177. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  4178. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  4179. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  4180. UpdateMinNumber(lastWarehouse.ID, stockMin)
  4181. }
  4182. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  4183. if err != nil {
  4184. return err
  4185. }
  4186. // 将该批次的剩余库存数量转换为拆零数量
  4187. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  4188. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  4189. if stock_number >= deliver_number {
  4190. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4191. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4192. WarehouseOutId: warehouseout.ID,
  4193. Status: 1,
  4194. Ctime: time.Now().Unix(),
  4195. Remark: advice.Remark,
  4196. OrgId: orgID,
  4197. Type: 1,
  4198. Manufacturer: advice.Manufacturer,
  4199. Dealer: warehouse.Dealer,
  4200. IsSys: 0,
  4201. SysRecordTime: advice.Ctime,
  4202. DrugId: advice.DrugId,
  4203. ExpiryDate: advice.ExpiryDate,
  4204. ProductDate: advice.ProductDate,
  4205. Number: advice.Number,
  4206. BatchNumber: warehouse.BatchNumber,
  4207. Count: deliver_number,
  4208. RetailPrice: advice.RetailPrice,
  4209. Price: advice.Price,
  4210. WarehouseInfoId: warehouse.ID,
  4211. CountUnit: drup.MinUnit,
  4212. AdviceId: advice.ID,
  4213. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4214. StorehouseId: advice.StorehouseId,
  4215. AdminUserId: advice.AdminUserId,
  4216. LastPrice: warehouse.Price,
  4217. StockCount: advice.StockCount,
  4218. }
  4219. warehouseOutInfo.Count = prescribingNumber
  4220. warehouseOutInfo.CountUnit = advice.CountUnit
  4221. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4222. drugflow := models.DrugFlow{
  4223. WarehouseOutId: warehouseout.ID,
  4224. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4225. DrugId: advice.DrugId,
  4226. Number: warehouse.Number,
  4227. ProductDate: advice.ProductDate,
  4228. ExpireDate: advice.ExpiryDate,
  4229. Count: deliver_number,
  4230. Price: advice.Price,
  4231. Status: 1,
  4232. Ctime: time.Now().Unix(),
  4233. UserOrgId: advice.OrgId,
  4234. Manufacturer: advice.Manufacturer,
  4235. Dealer: advice.Dealer,
  4236. BatchNumber: warehouse.BatchNumber,
  4237. MaxUnit: drup.MinUnit,
  4238. ConsumableType: 2,
  4239. IsEdit: 1,
  4240. Creator: 0,
  4241. IsSys: 0,
  4242. WarehouseOutDetailId: drugWareInfo.ID,
  4243. AdviceId: advice.ID,
  4244. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4245. StorehouseId: advice.StorehouseId,
  4246. AdminUserId: advice.AdminUserId,
  4247. LastPrice: warehouse.Price,
  4248. StockCount: advice.StockCount,
  4249. }
  4250. if warehouse.RetailPrice == 0 {
  4251. drugflow.Price = advice.Price
  4252. }
  4253. CreateDrugFlowOne(drugflow)
  4254. // 出库完成后,要减去对应批次的库存数量
  4255. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  4256. var maxNumber int64 = 0
  4257. var minNumber int64 = 0
  4258. //var stock_max_number int64 = 0
  4259. //stock_max_number = warehouse.StockMaxNumber
  4260. maxNumber = deliver_number / drup.MinNumber
  4261. minNumber = deliver_number % drup.MinNumber
  4262. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  4263. minNumber = maxNumber
  4264. }
  4265. if drup.MaxUnit != drup.MinUnit {
  4266. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  4267. return errors.New("库存数量不足")
  4268. }
  4269. }
  4270. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  4271. if warehouse.StockMaxNumber < 0 {
  4272. warehouse.StockMaxNumber = 0
  4273. }
  4274. if warehouse.StockMinNumber < 0 {
  4275. warehouse.StockMinNumber = 0
  4276. }
  4277. warehouse.Mtime = time.Now().Unix()
  4278. if warehouse.StockMinNumber < minNumber {
  4279. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  4280. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  4281. } else {
  4282. if minNumber > 0 {
  4283. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  4284. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4285. } else {
  4286. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  4287. }
  4288. }
  4289. if minNumber == 0 && maxNumber != 1 {
  4290. if warehouse.StockMinNumber > 0 {
  4291. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4292. }
  4293. }
  4294. }
  4295. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  4296. if (warehouse.StockMinNumber - deliver_number) > 0 {
  4297. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4298. //if warehouse.StockMinNumber == 0 {
  4299. // warehouse.StockMaxNumber = stock_max_number
  4300. //}
  4301. }
  4302. }
  4303. if drup.MaxUnit != drup.MinUnit {
  4304. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  4305. return errors.New("库存数量不足")
  4306. }
  4307. }
  4308. if warehouse.StockMinNumber <= 0 {
  4309. warehouse.StockMinNumber = 0
  4310. }
  4311. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4312. fmt.Println(errThree)
  4313. //if errThree != nil {
  4314. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  4315. // tx.Rollback()
  4316. //} else {
  4317. // tx.Commit()
  4318. //}
  4319. //查询默认仓库
  4320. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  4321. //查询默认仓库剩余多少库存
  4322. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  4323. var sum_count int64
  4324. var sum_in_count int64
  4325. for _, it := range list {
  4326. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  4327. if it.MaxUnit == baseDrug.MaxUnit {
  4328. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  4329. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  4330. sum_count += it.StockMaxNumber + it.StockMinNumber
  4331. sum_in_count += it.WarehousingCount
  4332. }
  4333. }
  4334. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  4335. return nil
  4336. } else {
  4337. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  4338. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4339. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4340. WarehouseOutId: warehouseout.ID,
  4341. Status: 1,
  4342. Ctime: time.Now().Unix(),
  4343. Remark: advice.Remark,
  4344. OrgId: orgID,
  4345. Type: 1,
  4346. Manufacturer: advice.Manufacturer,
  4347. Dealer: warehouse.Dealer,
  4348. IsSys: 0,
  4349. SysRecordTime: advice.Ctime,
  4350. DrugId: advice.DrugId,
  4351. ExpiryDate: warehouse.ExpiryDate,
  4352. ProductDate: warehouse.ProductDate,
  4353. Number: warehouse.Number,
  4354. BatchNumber: warehouse.BatchNumber,
  4355. Count: stock_number,
  4356. RetailPrice: advice.RetailPrice,
  4357. Price: advice.Price,
  4358. WarehouseInfoId: warehouse.ID,
  4359. CountUnit: drup.MinUnit,
  4360. AdviceId: advice.ID,
  4361. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4362. StorehouseId: advice.StorehouseId,
  4363. AdminUserId: advice.AdminUserId,
  4364. LastPrice: warehouse.Price,
  4365. StockCount: advice.StockCount,
  4366. }
  4367. warehouseOutInfo.Count = stock_number
  4368. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4369. drugflow := models.DrugFlow{
  4370. WarehouseOutId: warehouseout.ID,
  4371. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4372. DrugId: advice.DrugId,
  4373. Number: warehouse.Number,
  4374. ProductDate: advice.ProductDate,
  4375. ExpireDate: advice.ExpiryDate,
  4376. Count: stock_number,
  4377. Price: advice.Price,
  4378. Status: 1,
  4379. Ctime: time.Now().Unix(),
  4380. UserOrgId: advice.OrgId,
  4381. Manufacturer: advice.Manufacturer,
  4382. Dealer: advice.Dealer,
  4383. BatchNumber: warehouse.BatchNumber,
  4384. MaxUnit: drup.MinUnit,
  4385. ConsumableType: 2,
  4386. IsEdit: 1,
  4387. Creator: 0,
  4388. IsSys: 0,
  4389. WarehouseOutDetailId: drugWareInfo.ID,
  4390. AdviceId: advice.ID,
  4391. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4392. StorehouseId: advice.StorehouseId,
  4393. AdminUserId: advice.AdminUserId,
  4394. LastPrice: warehouse.Price,
  4395. StockCount: advice.StockCount,
  4396. }
  4397. CreateDrugFlowOne(drugflow)
  4398. // 出库完成后,要将该批次库存清零
  4399. //扣减库存
  4400. warehouse.StockMaxNumber = 0
  4401. warehouse.StockMinNumber = 0
  4402. warehouse.Mtime = time.Now().Unix()
  4403. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4404. fmt.Println(errThree)
  4405. //if errThree != nil {
  4406. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  4407. // tx.Rollback()
  4408. //} else {
  4409. // tx.Commit()
  4410. //}
  4411. //查询默认仓库
  4412. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  4413. //查询默认仓库剩余多少库存
  4414. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  4415. var sum_count int64
  4416. var sum_in_count int64
  4417. for _, it := range list {
  4418. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  4419. if it.MaxUnit == baseDrug.MaxUnit {
  4420. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  4421. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  4422. sum_count += it.StockMaxNumber + it.StockMinNumber
  4423. sum_in_count += it.WarehousingCount
  4424. }
  4425. }
  4426. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  4427. if errThree != nil {
  4428. return errThree
  4429. }
  4430. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  4431. prescribingNumber_two_temp := deliver_number - stock_number
  4432. fmt.Println("深谷数据", prescribingNumber_two_temp)
  4433. advice.CountUnit = drup.MinUnit
  4434. AutoDrugDeliverInfoTwentyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  4435. }
  4436. return
  4437. }
  4438. //耗材出库
  4439. func ConsumablesDeliveryThirty(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64) (err error) {
  4440. var deliver_number int64 = 0
  4441. var stock_number int64 = 0
  4442. var maxNumber int64 = 0
  4443. deliver_number = goods.Count
  4444. // 根据先进先出原则,查询最先入库的批次,进行出库
  4445. // 如果没有对应的库存,则报错
  4446. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  4447. if err != nil {
  4448. return err
  4449. }
  4450. stock_number = warehouse.StockCount
  4451. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  4452. if stock_number >= deliver_number {
  4453. warehouseOutInfo := &models.WarehouseOutInfo{
  4454. WarehouseInfotId: warehouse.ID,
  4455. }
  4456. //添加出库单详情
  4457. errOne := UpdatedSigleWarehouseOutInfo(goods.ID, warehouseOutInfo)
  4458. if errOne != nil {
  4459. return errOne
  4460. }
  4461. stockFlow := models.VmStockFlow{
  4462. WarehousingId: warehouse.ID,
  4463. GoodId: goods.GoodId,
  4464. Number: warehouse.Number,
  4465. LicenseNumber: goods.LicenseNumber,
  4466. Count: goods.Count,
  4467. UserOrgId: goods.OrgId,
  4468. PatientId: goods.PatientId,
  4469. SystemTime: record_time,
  4470. ConsumableType: 2,
  4471. IsSys: goods.IsSys,
  4472. WarehousingOrder: "",
  4473. WarehouseOutId: warehouseOut.ID,
  4474. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  4475. IsEdit: 1,
  4476. CancelStockId: 0,
  4477. CancelOrderNumber: "",
  4478. Manufacturer: goods.Manufacturer,
  4479. Dealer: goods.Dealer,
  4480. Creator: creator,
  4481. UpdateCreator: 0,
  4482. Status: 1,
  4483. Ctime: time.Now().Unix(),
  4484. Mtime: 0,
  4485. Price: warehouse.PackingPrice,
  4486. WarehousingDetailId: 0,
  4487. WarehouseOutDetailId: warehouseOutInfo.ID,
  4488. CancelOutDetailId: 0,
  4489. ProductDate: goods.ProductDate,
  4490. ExpireDate: goods.ExpiryDate,
  4491. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4492. StorehouseId: warehouse.StorehouseId,
  4493. AdminUserId: goods.AdminUserId,
  4494. BuyPrice: warehouse.Price,
  4495. StockCount: goods.StockCount,
  4496. }
  4497. //创建出库流水
  4498. CreateStockFlowOne(stockFlow)
  4499. UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, goods.OrgId)
  4500. maxNumber = goods.Count
  4501. if warehouse.StockCount < maxNumber {
  4502. return errors.New("库存数量不足")
  4503. }
  4504. warehouse.StockCount = warehouse.StockCount - maxNumber
  4505. warehouse.Mtime = time.Now().Unix()
  4506. //扣减库存
  4507. errThree := UpDateWarehouseInfoByStock(&warehouse)
  4508. fmt.Println(errThree)
  4509. //查询该机构默认仓库
  4510. storeConfig, _ := GetAllStoreHouseConfig(goods.OrgId)
  4511. //查询剩余库存
  4512. goodList, _ := GetGoodSumCountByStoreId(storeConfig.StorehouseOutInfo, goods.GoodId, goods.OrgId)
  4513. var sum_count int64
  4514. var sum_in_count int64
  4515. for _, item := range goodList {
  4516. sum_count += item.StockCount
  4517. sum_in_count += item.WarehousingCount
  4518. }
  4519. UpdateGoodByGoodId(goods.GoodId, sum_count, sum_in_count, goods.OrgId)
  4520. if errThree != nil {
  4521. return errThree
  4522. }
  4523. return nil
  4524. } else {
  4525. warehouseOutInfo := &models.WarehouseOutInfo{
  4526. WarehouseInfotId: warehouse.ID,
  4527. }
  4528. //添加出库单详情
  4529. errOne := UpdatedSigleWarehouseOutInfo(goods.ID, warehouseOutInfo)
  4530. if errOne != nil {
  4531. return errOne
  4532. }
  4533. stockFlow := models.VmStockFlow{
  4534. WarehousingId: warehouse.ID,
  4535. GoodId: goods.GoodId,
  4536. Number: warehouse.Number,
  4537. LicenseNumber: goods.LicenseNumber,
  4538. Count: stock_number,
  4539. UserOrgId: goods.OrgId,
  4540. PatientId: goods.PatientId,
  4541. SystemTime: record_time,
  4542. ConsumableType: 2,
  4543. IsSys: goods.IsSys,
  4544. WarehousingOrder: "",
  4545. WarehouseOutId: warehouseOut.ID,
  4546. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  4547. IsEdit: 1,
  4548. CancelStockId: 0,
  4549. CancelOrderNumber: "",
  4550. Manufacturer: goods.Manufacturer,
  4551. Dealer: goods.Dealer,
  4552. Creator: creator,
  4553. UpdateCreator: 0,
  4554. Status: 1,
  4555. Ctime: time.Now().Unix(),
  4556. Mtime: 0,
  4557. Price: warehouse.PackingPrice,
  4558. WarehousingDetailId: 0,
  4559. WarehouseOutDetailId: warehouseOutInfo.ID,
  4560. CancelOutDetailId: 0,
  4561. ProductDate: goods.ProductDate,
  4562. ExpireDate: goods.ExpiryDate,
  4563. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4564. StorehouseId: warehouse.StorehouseId,
  4565. AdminUserId: goods.AdminUserId,
  4566. BuyPrice: warehouse.Price,
  4567. StockCount: goods.StockCount,
  4568. }
  4569. //创建出库流水
  4570. CreateStockFlowOne(stockFlow)
  4571. UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, goods.OrgId)
  4572. }
  4573. //查询该机构默认仓库
  4574. storeConfig, _ := GetAllStoreHouseConfig(goods.OrgId)
  4575. //查询剩余库存
  4576. goodList, _ := GetGoodSumCountByStoreId(storeConfig.StorehouseOutInfo, goods.GoodId, goods.OrgId)
  4577. var sum_count int64
  4578. var sum_in_count int64
  4579. for _, item := range goodList {
  4580. sum_count += item.StockCount
  4581. sum_in_count += item.WarehousingCount
  4582. }
  4583. UpdateGoodByGoodId(goods.GoodId, sum_count, sum_in_count, goods.OrgId)
  4584. // 出库完成后,要将该批次库存清零
  4585. warehouse.StockCount = 0
  4586. warehouse.Mtime = time.Now().Unix()
  4587. errThree := UpDateWarehouseInfoByStock(&warehouse)
  4588. fmt.Println(errThree)
  4589. goods.Count = deliver_number - stock_number
  4590. ConsumablesDeliveryThirty(orgID, record_time, goods, warehouseOut, count, creator)
  4591. return nil
  4592. }
  4593. // 药品手动出库 递归方式
  4594. func AutoDrugDeliverInfoFourtyOne(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  4595. //开事务
  4596. //tx := XTWriteDB().Begin()
  4597. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  4598. var deliver_number int64 = 0
  4599. var stock_number int64 = 0
  4600. //fmt.Println("出库数量",prescribingNumber)
  4601. //fmt.Println("出库数量",drup.MinNumber)
  4602. //fmt.Println("dnaeiw",advice.CountUnit)
  4603. //if advice.CountUnit == drup.MaxUnit {
  4604. // deliver_number = prescribingNumber * drup.MinNumber
  4605. //} else {
  4606. deliver_number = prescribingNumber
  4607. //}
  4608. //fmt.Println("出库数量2323223323232322323232323",deliver_number)
  4609. // 根据先进先出原则,查询最先入库的批次,进行出库
  4610. // 如果没有对应的库存,则报错
  4611. //开启事物
  4612. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  4613. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  4614. var stockMax int64
  4615. var stockMin int64
  4616. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  4617. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  4618. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  4619. UpdateMinNumber(lastWarehouse.ID, stockMin)
  4620. }
  4621. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  4622. if err != nil {
  4623. return err
  4624. }
  4625. // 将该批次的剩余库存数量转换为拆零数量
  4626. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  4627. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  4628. if stock_number >= deliver_number {
  4629. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4630. drugflow := models.DrugFlow{
  4631. WarehouseOutId: warehouseout.ID,
  4632. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4633. DrugId: advice.DrugId,
  4634. Number: warehouse.Number,
  4635. ProductDate: advice.ProductDate,
  4636. ExpireDate: advice.ExpiryDate,
  4637. Count: deliver_number,
  4638. Price: advice.Price,
  4639. Status: 1,
  4640. Ctime: time.Now().Unix(),
  4641. UserOrgId: advice.OrgId,
  4642. Manufacturer: advice.Manufacturer,
  4643. Dealer: advice.Dealer,
  4644. BatchNumber: warehouse.BatchNumber,
  4645. MaxUnit: drup.MinUnit,
  4646. ConsumableType: 2,
  4647. IsEdit: 1,
  4648. Creator: 0,
  4649. IsSys: 0,
  4650. WarehouseOutDetailId: drugWareInfo.ID,
  4651. AdviceId: advice.ID,
  4652. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4653. StorehouseId: advice.StorehouseId,
  4654. AdminUserId: advice.AdminUserId,
  4655. LastPrice: warehouse.Price,
  4656. StockCount: advice.StockCount,
  4657. }
  4658. if warehouse.RetailPrice == 0 {
  4659. drugflow.Price = advice.Price
  4660. }
  4661. CreateDrugFlowOne(drugflow)
  4662. //更新出库id
  4663. UpdateDrugWarehouseOutInfo(advice.ID, warehouse.ID, advice.OrgId)
  4664. // 出库完成后,要减去对应批次的库存数量
  4665. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  4666. var maxNumber int64 = 0
  4667. var minNumber int64 = 0
  4668. //var stock_max_number int64 = 0
  4669. //stock_max_number = warehouse.StockMaxNumber
  4670. maxNumber = deliver_number / drup.MinNumber
  4671. minNumber = deliver_number % drup.MinNumber
  4672. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  4673. minNumber = maxNumber
  4674. }
  4675. if drup.MaxUnit != drup.MinUnit {
  4676. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  4677. return errors.New("库存数量不足")
  4678. }
  4679. }
  4680. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  4681. if warehouse.StockMaxNumber < 0 {
  4682. warehouse.StockMaxNumber = 0
  4683. }
  4684. if warehouse.StockMinNumber < 0 {
  4685. warehouse.StockMinNumber = 0
  4686. }
  4687. warehouse.Mtime = time.Now().Unix()
  4688. if warehouse.StockMinNumber < minNumber {
  4689. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  4690. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  4691. } else {
  4692. if minNumber > 0 {
  4693. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  4694. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4695. } else {
  4696. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  4697. }
  4698. }
  4699. if minNumber == 0 && maxNumber != 1 {
  4700. if warehouse.StockMinNumber > 0 {
  4701. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4702. }
  4703. }
  4704. }
  4705. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  4706. if (warehouse.StockMinNumber - deliver_number) > 0 {
  4707. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4708. //if warehouse.StockMinNumber == 0 {
  4709. // warehouse.StockMaxNumber = stock_max_number
  4710. //}
  4711. }
  4712. }
  4713. if drup.MaxUnit != drup.MinUnit {
  4714. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  4715. return errors.New("库存数量不足")
  4716. }
  4717. }
  4718. if warehouse.StockMinNumber <= 0 {
  4719. warehouse.StockMinNumber = 0
  4720. }
  4721. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4722. fmt.Println(errThree)
  4723. return nil
  4724. } else {
  4725. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4726. drugflow := models.DrugFlow{
  4727. WarehouseOutId: warehouseout.ID,
  4728. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4729. DrugId: advice.DrugId,
  4730. Number: warehouse.Number,
  4731. ProductDate: advice.ProductDate,
  4732. ExpireDate: advice.ExpiryDate,
  4733. Count: stock_number,
  4734. Price: advice.Price,
  4735. Status: 1,
  4736. Ctime: time.Now().Unix(),
  4737. UserOrgId: advice.OrgId,
  4738. Manufacturer: advice.Manufacturer,
  4739. Dealer: advice.Dealer,
  4740. BatchNumber: warehouse.BatchNumber,
  4741. MaxUnit: drup.MinUnit,
  4742. ConsumableType: 2,
  4743. IsEdit: 1,
  4744. Creator: 0,
  4745. IsSys: 0,
  4746. WarehouseOutDetailId: drugWareInfo.ID,
  4747. AdviceId: advice.ID,
  4748. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4749. StorehouseId: advice.StorehouseId,
  4750. AdminUserId: advice.AdminUserId,
  4751. LastPrice: warehouse.Price,
  4752. StockCount: advice.StockCount,
  4753. }
  4754. CreateDrugFlowOne(drugflow)
  4755. //更新出库id
  4756. UpdateDrugWarehouseOutInfo(advice.ID, warehouse.ID, advice.OrgId)
  4757. // 出库完成后,要将该批次库存清零
  4758. //扣减库存
  4759. warehouse.StockMaxNumber = 0
  4760. warehouse.StockMinNumber = 0
  4761. warehouse.Mtime = time.Now().Unix()
  4762. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4763. fmt.Println(errThree)
  4764. if errThree != nil {
  4765. return errThree
  4766. }
  4767. prescribingNumber_two_temp := deliver_number - stock_number
  4768. advice.CountUnit = drup.MinUnit
  4769. AutoDrugDeliverInfoFourtyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  4770. }
  4771. return
  4772. }
  4773. func UpdateDrugWarehouseOutInfo(id int64, warehouse_info_id int64, org_id int64) error {
  4774. err = XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and org_id = ? and status = 1", id, org_id).Update(map[string]interface{}{"warehouse_info_id": warehouse_info_id}).Error
  4775. return err
  4776. }
  4777. func UpdateGoodWarehouseOutById(id int64, warehouse_info_id int64, org_id int64) error {
  4778. err := XTWriteDB().Model(models.WarehouseOutInfo{}).Where("id = ? and org_id = ? and status = 1", id, org_id).Update(map[string]interface{}{"warehouse_info_id": warehouse_info_id}).Error
  4779. return err
  4780. }
  4781. func ChangeMaxNumber(id int64, count int64) error {
  4782. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1 and is_check = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  4783. return err
  4784. }
  4785. func UpdateMinNumber(id int64, count int64) error {
  4786. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1 and is_check = 1", id).Update(map[string]interface{}{"stock_min_number": count}).Error
  4787. return err
  4788. }
  4789. func FindLastDrugWarehouseOutInfo(orgid int64) (models.DrugWarehouseOutInfo, error) {
  4790. info := models.DrugWarehouseOutInfo{}
  4791. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&info).Error
  4792. return info, err
  4793. }