warhouse_service.go 374KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. _ "fmt"
  6. "math"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "XT_New/models"
  11. "XT_New/utils"
  12. "github.com/jinzhu/gorm"
  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. IsCheck: 1,
  53. }
  54. err := AddSigleDrugWarehouseOut(&warehouseOut)
  55. if err != nil {
  56. utils.TraceLog("创建出库单失败 err = %v", err)
  57. return err
  58. } else {
  59. out = warehouseOut
  60. }
  61. }
  62. // 出库流程
  63. // 1.查询该药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  64. drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
  65. if drup.ID > 0 {
  66. prescribingNumber := advice.PrescribingNumber
  67. DrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice)
  68. } else {
  69. return errors.New("药品信息不存在")
  70. }
  71. }
  72. return
  73. }
  74. // 药品自动出库 递归方式
  75. func DrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DoctorAdvice) (err error) {
  76. drugError := models.XtDrugError{
  77. ID: 0,
  78. UserOrgId: advice.UserOrgId,
  79. DrugId: advice.DrugId,
  80. RecordDate: advice.AdviceDate,
  81. PatientId: advice.PatientId,
  82. Remark: "恒泰中医院出库进来",
  83. Status: 1,
  84. Ctime: time.Now().Unix(),
  85. Mtime: 0,
  86. Prescribingnumber: advice.PrescribingNumber,
  87. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  88. SumCount: 0,
  89. }
  90. CreateDrugError(drugError)
  91. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  92. var deliver_number int64 = 0
  93. var stock_number int64 = 0
  94. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  95. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  96. var drug_price float64
  97. if advice.PrescribingNumberUnit == drup.MaxUnit {
  98. deliver_number = count * drup.MinNumber
  99. } else {
  100. deliver_number = count
  101. }
  102. if advice.PrescribingNumberUnit == drup.MaxUnit {
  103. drug_price = drup.RetailPrice
  104. }
  105. fmt.Println(drug_price)
  106. if advice.PrescribingNumberUnit == drup.MinUnit {
  107. drug_price = drup.MinPrice
  108. }
  109. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  110. drug_price = drup.RetailPrice
  111. }
  112. // 根据先进先出原则,查询最先入库的批次,进行出库
  113. // 如果没有对应的库存,则报错
  114. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  115. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  116. if lastWarehouse.MaxUnit != lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  117. //fmt.Println("lastWarehouse", lastWarehouse.StockMinNumber)
  118. //fmt.Println("drugp.min", drup.MinNumber)
  119. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  120. var stockMax int64
  121. var stockMin int64
  122. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  123. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  124. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  125. UpdateMinNumber(lastWarehouse.ID, stockMin)
  126. }
  127. }
  128. if lastWarehouse.MaxUnit == lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  129. //fmt.Println("owowowowowoowowowowowo")
  130. lastWarehouse.StockMaxNumber = lastWarehouse.StockMaxNumber + lastWarehouse.StockMinNumber
  131. ChangeMaxNumberOne(lastWarehouse.ID, lastWarehouse.StockMaxNumber)
  132. }
  133. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  134. if err != nil {
  135. return err
  136. }
  137. // 将该批次的剩余库存数量转换为拆零数量
  138. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  139. // 将该批次的剩余库存数量转换为拆零数量
  140. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  141. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  142. }
  143. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  144. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  145. }
  146. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  147. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  148. }
  149. //查找药品信息
  150. //baseInfo, _ := FindBaseDrugLibRecord(advice.UserOrgId, advice.DrugId)
  151. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  152. //fmt.Println("stock_number", stock_number)
  153. //fmt.Println("deliver_numberwowowwo", deliver_number)
  154. if stock_number >= deliver_number {
  155. var maxNumber int64 = 0
  156. var minNumber int64 = 0
  157. //var stock_max_number int64 = 0
  158. //stock_max_number = warehouse.StockMaxNumber
  159. maxNumber = deliver_number / drup.MinNumber
  160. minNumber = deliver_number % drup.MinNumber
  161. //fmt.Println("maxNumberowowowowo", warehouse.StockMaxNumber)
  162. //fmt.Println("min_nu2o2o2332o23", warehouse.StockMinNumber)
  163. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  164. minNumber = maxNumber
  165. }
  166. if drup.MaxUnit != drup.MinUnit {
  167. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  168. return errors.New("库存数量不足")
  169. }
  170. }
  171. //fmt.Println("min2o2o23o32", minNumber)
  172. //fmt.Println("maxNumberowowoow", maxNumber)
  173. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  174. //fmt.Println("金1")
  175. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  176. //fmt.Println("金1", warehouse.StockMaxNumber)
  177. }
  178. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  179. //fmt.Println("金2")
  180. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  181. }
  182. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  183. //fmt.Println("金3")
  184. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  185. }
  186. if warehouse.StockMaxNumber < 0 {
  187. warehouse.StockMaxNumber = 0
  188. }
  189. if warehouse.StockMinNumber < 0 {
  190. warehouse.StockMinNumber = 0
  191. }
  192. warehouse.Mtime = time.Now().Unix()
  193. if warehouse.StockMinNumber < minNumber {
  194. //fmt.Println("金4")
  195. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  196. if minNumber == 2 && warehouse.MaxUnit == warehouse.MinUnit && warehouse.StockMinNumber == 0 {
  197. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  198. }
  199. if warehouse.MaxUnit != warehouse.MinUnit {
  200. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  201. }
  202. } else {
  203. //fmt.Println("金5")
  204. if minNumber > 0 {
  205. if (warehouse.StockMinNumber - minNumber) >= 0 {
  206. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  207. }
  208. }
  209. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  210. //fmt.Println("6")
  211. if warehouse.StockMinNumber > 0 {
  212. if warehouse.StockMinNumber-deliver_number >= 0 {
  213. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  214. }
  215. }
  216. }
  217. }
  218. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit && warehouse.MaxUnit != warehouse.MinUnit {
  219. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  220. //fmt.Println("金7")
  221. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  222. }
  223. }
  224. if drup.MaxUnit != drup.MinUnit {
  225. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  226. return errors.New("库存数量不足")
  227. }
  228. }
  229. if warehouse.StockMinNumber <= 0 {
  230. warehouse.StockMinNumber = 0
  231. }
  232. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  233. //fmt.Println("errThreeo23o2o32o32o23o32", errThree)
  234. if errThree != nil {
  235. return errThree
  236. }
  237. //查询剩余库存
  238. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  239. var sum_count int64
  240. for _, its := range stockInfo {
  241. if its.MaxUnit == drup.MaxUnit {
  242. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  243. }
  244. sum_count += its.StockMaxNumber + its.StockMinNumber
  245. }
  246. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  247. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  248. WarehouseOutId: warehouseout.ID,
  249. Status: 1,
  250. Ctime: time.Now().Unix(),
  251. Remark: warehouse.Remark,
  252. OrgId: orgID,
  253. Type: 1,
  254. Manufacturer: warehouse.Manufacturer,
  255. Dealer: warehouse.Dealer,
  256. IsSys: 1,
  257. SysRecordTime: advice.RecordDate,
  258. DrugId: advice.DrugId,
  259. Number: warehouse.Number,
  260. BatchNumber: warehouse.BatchNumber,
  261. Price: warehouse.RetailPrice,
  262. CountUnit: drup.MinUnit,
  263. RetailPrice: warehouse.RetailPrice,
  264. ProductDate: warehouse.ProductDate,
  265. ExpiryDate: warehouse.ExpiryDate,
  266. PatientId: advice.PatientId,
  267. Count: deliver_number, //出最小单位
  268. WarehouseInfoId: warehouse.ID,
  269. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  270. StorehouseId: storeConfig.DrugStorehouseOut,
  271. IsCheck: 1,
  272. OverCount: sum_count,
  273. AdviceId: advice.ID,
  274. }
  275. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  276. fmt.Println(errOne)
  277. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  278. drugflow := models.DrugFlow{
  279. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  280. WarehouseOutId: warehouseout.ID,
  281. DrugId: advice.DrugId,
  282. Number: warehouse.Number,
  283. ProductDate: warehouse.ProductDate,
  284. ExpireDate: warehouse.ExpiryDate,
  285. Count: deliver_number, //按最小单位计算,
  286. Price: warehouse.RetailPrice,
  287. Status: 1,
  288. Ctime: time.Now().Unix(),
  289. UserOrgId: orgID,
  290. Manufacturer: warehouse.Manufacturer,
  291. Dealer: warehouse.Dealer,
  292. BatchNumber: warehouse.BatchNumber,
  293. MaxUnit: drup.MinUnit,
  294. ConsumableType: 3,
  295. IsEdit: 1,
  296. Creator: advice.ExecutionStaff,
  297. IsSys: 1,
  298. PatientId: advice.PatientId,
  299. SystemTime: advice.AdviceDate,
  300. WarehousingDetailId: warehouse.ID,
  301. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  302. StorehouseId: storeConfig.DrugStorehouseOut,
  303. WarehouseOutDetailId: lastDrugOutInfo.ID,
  304. LastPrice: warehouse.Price,
  305. OverCount: sum_count,
  306. AdviceId: advice.ID,
  307. OperateTime: time.Now().Unix(),
  308. DrugCode: advice.DrugCode,
  309. }
  310. CreateDrugFlowOne(drugflow)
  311. //查询是否存在数据
  312. details := &models.DrugAutomaticReduceDetail{
  313. WarehouseOutId: warehouseout.ID,
  314. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  315. PatientId: advice.PatientId,
  316. Ctime: time.Now().Unix(),
  317. Mtime: time.Now().Unix(),
  318. Status: 1,
  319. RecordTime: advice.RecordDate,
  320. OrgId: orgID,
  321. DrugId: advice.DrugId,
  322. Count: deliver_number,
  323. CountUnit: drup.MinUnit,
  324. WarehouseInfoId: warehouse.ID,
  325. StorehouseId: storeConfig.DrugStorehouseOut,
  326. AdviceId: advice.ID,
  327. }
  328. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  329. if errTwo != nil {
  330. return errTwo
  331. }
  332. var out_count int64
  333. //查询出库数据
  334. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  335. for _, item := range infoCountList {
  336. out_count += item.Count
  337. }
  338. //出库数量相加
  339. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  340. return nil
  341. } else {
  342. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  343. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  344. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  345. WarehouseOutId: warehouseout.ID,
  346. Status: 1,
  347. Ctime: time.Now().Unix(),
  348. Remark: warehouse.Remark,
  349. OrgId: orgID,
  350. Type: 1,
  351. Manufacturer: warehouse.Manufacturer,
  352. Dealer: warehouse.Dealer,
  353. IsSys: 1,
  354. SysRecordTime: advice.RecordDate,
  355. DrugId: advice.DrugId,
  356. Number: warehouse.Number,
  357. BatchNumber: warehouse.BatchNumber,
  358. Price: warehouse.RetailPrice,
  359. CountUnit: drup.MinUnit,
  360. ProductDate: warehouse.ProductDate,
  361. ExpiryDate: warehouse.ExpiryDate,
  362. PatientId: advice.PatientId,
  363. Count: stock_number,
  364. WarehouseInfoId: warehouse.ID,
  365. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  366. StorehouseId: storeConfig.DrugStorehouseOut,
  367. IsCheck: 1,
  368. AdviceId: advice.ID,
  369. }
  370. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  371. if errOne != nil {
  372. return errOne
  373. }
  374. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  375. details := &models.DrugAutomaticReduceDetail{
  376. WarehouseOutId: warehouseout.ID,
  377. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  378. PatientId: advice.PatientId,
  379. Ctime: time.Now().Unix(),
  380. Mtime: time.Now().Unix(),
  381. Status: 1,
  382. RecordTime: advice.RecordDate,
  383. OrgId: orgID,
  384. DrugId: advice.DrugId,
  385. Count: stock_number,
  386. CountUnit: drup.MinUnit,
  387. WarehouseInfoId: warehouse.ID,
  388. StorehouseId: storeConfig.DrugStorehouseOut,
  389. AdviceId: advice.ID,
  390. }
  391. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  392. if errTwo != nil {
  393. return errTwo
  394. }
  395. info := models.XtDrugWarehouseInfo{
  396. ID: warehouse.ID,
  397. WarehousingId: warehouse.WarehousingId,
  398. DrugId: warehouse.DrugId,
  399. Number: warehouse.Number,
  400. ProductDate: warehouse.ProductDate,
  401. ExpiryDate: warehouse.ExpiryDate,
  402. WarehousingCount: warehouse.WarehousingCount,
  403. Price: warehouse.RetailPrice,
  404. TotalPrice: warehouse.TotalPrice,
  405. Dealer: warehouse.Dealer,
  406. Manufacturer: warehouse.Manufacturer,
  407. Remark: warehouse.Remark,
  408. Ctime: warehouse.Ctime,
  409. Mtime: warehouse.Mtime,
  410. Status: 1,
  411. OrgId: warehouse.OrgId,
  412. IsReturn: warehouse.IsReturn,
  413. WarehousingOrder: warehouse.WarehousingOrder,
  414. Type: warehouse.Type,
  415. RetailPrice: warehouse.RetailPrice,
  416. RetailTotalPrice: warehouse.RetailPrice,
  417. StockMaxNumber: 0,
  418. StockMinNumber: 0,
  419. BatchNumber: warehouse.BatchNumber,
  420. MaxUnit: warehouse.MaxUnit,
  421. WarehousingInfoId: warehouse.WarehousingInfoId,
  422. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  423. StorehouseId: storeConfig.DrugStorehouseOut,
  424. IsCheck: 1,
  425. }
  426. //扣减库存
  427. errThree := UpDateDrugWarehouseInfoByStock(&info)
  428. if errThree != nil {
  429. return errThree
  430. }
  431. //查询剩余库存
  432. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  433. var sum_count int64
  434. for _, its := range stockInfo {
  435. if its.MaxUnit == drup.MaxUnit {
  436. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  437. }
  438. sum_count += its.StockMaxNumber + its.StockMinNumber
  439. }
  440. var out_count int64
  441. //查询出库数据
  442. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  443. for _, item := range infoCountList {
  444. out_count += item.Count
  445. }
  446. drugflow := models.DrugFlow{
  447. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  448. WarehouseOutId: warehouseout.ID,
  449. DrugId: advice.DrugId,
  450. Number: warehouse.Number,
  451. ProductDate: warehouse.ProductDate,
  452. ExpireDate: warehouse.ExpiryDate,
  453. Count: stock_number,
  454. Price: warehouse.RetailPrice,
  455. Status: 1,
  456. Ctime: time.Now().Unix(),
  457. UserOrgId: orgID,
  458. Manufacturer: warehouse.Manufacturer,
  459. Dealer: warehouse.Dealer,
  460. BatchNumber: warehouse.BatchNumber,
  461. MaxUnit: drup.MinUnit,
  462. ConsumableType: 3,
  463. IsEdit: 1,
  464. Creator: advice.ExecutionStaff,
  465. IsSys: 1,
  466. PatientId: advice.PatientId,
  467. WarehousingDetailId: warehouse.ID,
  468. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  469. StorehouseId: storeConfig.DrugStorehouseOut,
  470. WarehouseOutDetailId: lastDrugOutInfo.ID,
  471. LastPrice: warehouse.Price,
  472. OverCount: sum_count,
  473. AdviceId: advice.ID,
  474. OperateTime: time.Now().Unix(),
  475. DrugCode: advice.DrugCode,
  476. }
  477. CreateDrugFlowOne(drugflow)
  478. //出库数量相加
  479. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  480. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  481. prescribingNumber_two_temp := deliver_number - stock_number
  482. overPlusNumber := float64(prescribingNumber_two_temp)
  483. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  484. advice.PrescribingNumberUnit = drup.MinUnit
  485. DrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  486. }
  487. return
  488. }
  489. // 药品出库
  490. func HisDrugsDelivery(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo) (err error) {
  491. // 1.判断药品是否来自专用字典的药品库
  492. // 2.判断当天当前机构有没有创建出库单,没有则创建
  493. // 3.创建出库流程
  494. // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  495. isHasWay := false
  496. record_time := int64(0)
  497. isHasWay = true
  498. record_time = advice.RecordDate
  499. if isHasWay {
  500. //判断当天当前机构有没有创建出库单,没有则创建
  501. out, err := FindDrugStockOutByIsSys(orgID, 1, record_time)
  502. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  503. if err == gorm.ErrRecordNotFound {
  504. timeStr := time.Now().Format("2006-01-02")
  505. timeArr := strings.Split(timeStr, "-")
  506. total, _ := FindAllDrugWarehouseOut(orgID)
  507. total = total + 1
  508. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  509. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  510. number = number + total
  511. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  512. warehouseOut := models.DrugWarehouseOut{
  513. WarehouseOutOrderNumber: warehousing_out_order,
  514. OperationTime: time.Now().Unix(),
  515. OrgId: orgID,
  516. Creater: creater,
  517. Ctime: time.Now().Unix(),
  518. Status: 1,
  519. WarehouseOutTime: record_time,
  520. Dealer: 0,
  521. Manufacturer: 0,
  522. Type: 1,
  523. IsSys: 1,
  524. StorehouseId: houseConfig.DrugStorehouseOut,
  525. IsCheck: 1,
  526. }
  527. err := AddSigleDrugWarehouseOut(&warehouseOut)
  528. if err != nil {
  529. utils.TraceLog("创建出库单失败 err = %v", err)
  530. return err
  531. } else {
  532. out = warehouseOut
  533. }
  534. }
  535. // 出库流程
  536. // 1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  537. drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
  538. if drup.ID > 0 {
  539. prescribingNumber := advice.PrescribingNumber
  540. BloodHisDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice, prescribingNumber, advice.PrescribingNumberUnit)
  541. } else {
  542. return errors.New("药品信息不存在")
  543. }
  544. }
  545. return
  546. }
  547. func GetDrugFlowByWatch(patient_id int64, drug_id int64, advice_date int64, user_org_id int64) (list []*models.DrugFlow, err error) {
  548. err = XTReadDB().Where("patient_id = ? and drug_id = ? and system_time = ? and user_org_id = ? and status =1 and consumable_type = 3", patient_id, drug_id, advice_date, user_org_id).Find(&list).Error
  549. return list, err
  550. }
  551. func GetDrugWarehouseOutInfor(id int64, user_org_id int64) (models.DrugWarehouseOutInfo, error) {
  552. outInfo := models.DrugWarehouseOutInfo{}
  553. err := XTReadDB().Where("id = ? and org_id = ?", id, user_org_id).Find(&outInfo).Error
  554. return outInfo, err
  555. }
  556. func UpdateDrugWarehouseOutInfoById(id int64, count int64) error {
  557. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"count": count}).Error
  558. return err
  559. }
  560. // 药品出库 递归方式
  561. func HisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  562. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  563. var deliver_number int64 = 0
  564. var stock_number int64 = 0
  565. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  566. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  567. if advice.PrescribingNumberUnit == drup.MaxUnit {
  568. deliver_number = count * drup.MinNumber
  569. } else {
  570. deliver_number = count
  571. }
  572. var min_price float64
  573. if advice.PrescribingNumberUnit == drup.MaxUnit {
  574. min_price = drup.RetailPrice
  575. }
  576. if advice.PrescribingNumberUnit == drup.MinUnit {
  577. min_price = drup.MinPrice
  578. }
  579. if advice.PrescribingNumberUnit != drup.MaxUnit && advice.PrescribingNumberUnit != drup.MinUnit {
  580. min_price = drup.RetailPrice
  581. }
  582. fmt.Println(min_price)
  583. // 根据先进先出原则,查询最先入库的批次,进行出库
  584. // 如果没有对应的库存,则报错
  585. //查询默认仓库
  586. storeConfig, _ := GetAllStoreHouseConfig(orgID)
  587. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  588. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  589. var stockMax int64
  590. var stockMin int64
  591. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  592. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  593. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  594. UpdateMinNumber(lastWarehouse.ID, stockMin)
  595. }
  596. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  597. if err != nil {
  598. return err
  599. }
  600. // 将该批次的剩余库存数量转换为拆零数量
  601. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  602. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  603. if stock_number >= deliver_number {
  604. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  605. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  606. WarehouseOutId: warehouseout.ID,
  607. Status: 1,
  608. Ctime: time.Now().Unix(),
  609. Remark: warehouse.Remark,
  610. OrgId: orgID,
  611. Type: 1,
  612. Manufacturer: warehouse.Manufacturer,
  613. Dealer: warehouse.Dealer,
  614. IsSys: 1,
  615. SysRecordTime: advice.RecordDate,
  616. DrugId: advice.DrugId,
  617. Number: warehouse.Number,
  618. BatchNumber: warehouse.BatchNumber,
  619. Price: warehouse.RetailPrice,
  620. CountUnit: drup.MinUnit,
  621. RetailPrice: warehouse.RetailPrice,
  622. ProductDate: warehouse.ProductDate,
  623. ExpiryDate: warehouse.ExpiryDate,
  624. PatientId: advice.PatientId,
  625. WarehouseInfoId: warehouse.ID,
  626. Count: deliver_number,
  627. AdviceId: advice.ID,
  628. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  629. StorehouseId: storeConfig.DrugStorehouseOut,
  630. IsCheck: 1,
  631. LastPrice: warehouse.Price,
  632. }
  633. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  634. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  635. if errOne != nil {
  636. return errOne
  637. }
  638. drugflow := models.DrugFlow{
  639. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  640. WarehouseOutId: warehouseout.ID,
  641. DrugId: advice.DrugId,
  642. Number: warehouse.Number,
  643. ProductDate: warehouse.ProductDate,
  644. ExpireDate: warehouse.ExpiryDate,
  645. Count: deliver_number,
  646. Price: warehouse.RetailPrice,
  647. Status: 1,
  648. Ctime: time.Now().Unix(),
  649. UserOrgId: orgID,
  650. Manufacturer: warehouse.Manufacturer,
  651. Dealer: warehouse.Dealer,
  652. BatchNumber: warehouse.BatchNumber,
  653. MaxUnit: drup.MinUnit,
  654. ConsumableType: 3,
  655. IsEdit: 1,
  656. Creator: advice.ExecutionStaff,
  657. IsSys: 1,
  658. PatientId: advice.PatientId,
  659. SystemTime: advice.AdviceDate,
  660. WarehousingDetailId: warehouse.ID,
  661. AdviceId: advice.ID,
  662. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  663. StorehouseId: storeConfig.DrugStorehouseOut,
  664. WarehouseOutDetailId: lastDrugOutInfo.ID,
  665. LastPrice: warehouse.Price,
  666. }
  667. CreateDrugFlowOne(drugflow)
  668. details := &models.DrugAutomaticReduceDetail{
  669. WarehouseOutId: warehouseout.ID,
  670. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  671. PatientId: advice.PatientId,
  672. Ctime: time.Now().Unix(),
  673. Mtime: time.Now().Unix(),
  674. Status: 1,
  675. RecordTime: advice.RecordDate,
  676. OrgId: orgID,
  677. DrugId: advice.DrugId,
  678. Count: deliver_number,
  679. CountUnit: drup.MinUnit,
  680. WarehouseInfoId: warehouse.ID,
  681. AdviceId: advice.ID,
  682. StorehouseId: storeConfig.DrugStorehouseOut,
  683. }
  684. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  685. if errTwo != nil {
  686. return errTwo
  687. }
  688. // 出库完成后,要减去对应批次的库存数量
  689. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  690. var maxNumber int64 = 0
  691. var minNumber int64 = 0
  692. maxNumber = deliver_number / drup.MinNumber
  693. minNumber = deliver_number % drup.MinNumber
  694. fmt.Println("maxNumber2323233232323232323232", maxNumber)
  695. fmt.Println("minNumber2323233232323232323232", minNumber)
  696. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  697. minNumber = maxNumber
  698. }
  699. if drup.MaxUnit != drup.MinUnit {
  700. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  701. return errors.New("库存数量不足")
  702. }
  703. }
  704. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  705. if warehouse.StockMaxNumber < 0 {
  706. warehouse.StockMaxNumber = 0
  707. }
  708. if warehouse.StockMinNumber < 0 {
  709. warehouse.StockMinNumber = 0
  710. }
  711. warehouse.Mtime = time.Now().Unix()
  712. fmt.Println("warehouse.StockMinNumber", warehouse.StockMinNumber)
  713. fmt.Println("minNumber", minNumber)
  714. fmt.Println("maxNumber", maxNumber)
  715. if warehouse.StockMinNumber < minNumber {
  716. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  717. if warehouse.MaxUnit != warehouse.MinUnit {
  718. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  719. }
  720. } else {
  721. if minNumber > 0 {
  722. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  723. }
  724. }
  725. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  726. if (warehouse.StockMinNumber - deliver_number) > 0 {
  727. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  728. }
  729. }
  730. if drup.MaxUnit != drup.MinUnit {
  731. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  732. return errors.New("库存数量不足")
  733. }
  734. }
  735. if warehouse.StockMinNumber <= 0 {
  736. warehouse.StockMinNumber = 0
  737. }
  738. //扣减库存
  739. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  740. if errThree != nil {
  741. return errThree
  742. }
  743. drugInfoList, _ := FindDrugWarehouseInfoList(advice.DrugId, orgID)
  744. var drug_max_number int64
  745. var drug_min_number int64
  746. for _, item := range drugInfoList {
  747. drug_max_number += item.StockMaxNumber
  748. drug_min_number += item.StockMinNumber
  749. }
  750. drugFlushInfo := models.XtDrugWarehouseFlushInfo{
  751. DrugMaxNumber: drug_max_number,
  752. UserOrgId: orgID,
  753. WarehouseOutId: warehouseout.ID,
  754. Type: 1,
  755. SystemTime: advice.RecordDate,
  756. PatientId: advice.PatientId,
  757. Ctime: time.Now().Unix(),
  758. Mtime: 0,
  759. BatchNumberId: warehouse.ID,
  760. DrugId: advice.DrugId,
  761. Count: deliver_number,
  762. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  763. Creater: warehouseout.Creater,
  764. DrugMinNumber: drug_min_number,
  765. Unit: drup.MinUnit,
  766. AdviceId: advice.ID,
  767. Status: 1,
  768. WarehouseOutDetailId: lastDrugOutInfo.ID,
  769. }
  770. CreatedDrugFlushInfo(drugFlushInfo)
  771. return nil
  772. } else {
  773. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  774. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  775. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  776. WarehouseOutId: warehouseout.ID,
  777. Status: 1,
  778. Ctime: time.Now().Unix(),
  779. Remark: warehouse.Remark,
  780. OrgId: orgID,
  781. Type: 1,
  782. Manufacturer: warehouse.Manufacturer,
  783. Dealer: warehouse.Dealer,
  784. IsSys: 1,
  785. SysRecordTime: advice.RecordDate,
  786. DrugId: advice.DrugId,
  787. Number: warehouse.Number,
  788. BatchNumber: warehouse.BatchNumber,
  789. Price: warehouse.RetailPrice,
  790. RetailPrice: warehouse.RetailPrice,
  791. ProductDate: warehouse.ProductDate,
  792. ExpiryDate: warehouse.ExpiryDate,
  793. PatientId: advice.PatientId,
  794. WarehouseInfoId: warehouse.ID,
  795. Count: stock_number,
  796. CountUnit: drup.MinUnit,
  797. AdviceId: advice.ID,
  798. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  799. StorehouseId: storeConfig.DrugStorehouseOut,
  800. IsCheck: 1,
  801. }
  802. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  803. if errOne != nil {
  804. return errOne
  805. }
  806. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  807. drugflow := models.DrugFlow{
  808. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  809. WarehouseOutId: warehouseout.ID,
  810. DrugId: advice.DrugId,
  811. Number: warehouse.Number,
  812. ProductDate: warehouse.ProductDate,
  813. ExpireDate: warehouse.ExpiryDate,
  814. Count: stock_number,
  815. Price: warehouse.RetailPrice,
  816. Status: 1,
  817. Ctime: time.Now().Unix(),
  818. UserOrgId: orgID,
  819. Manufacturer: warehouse.Manufacturer,
  820. Dealer: warehouse.Dealer,
  821. BatchNumber: warehouse.BatchNumber,
  822. MaxUnit: drup.MinUnit,
  823. ConsumableType: 3,
  824. IsEdit: 1,
  825. Creator: advice.ExecutionStaff,
  826. IsSys: 1,
  827. PatientId: advice.PatientId,
  828. SystemTime: advice.AdviceDate,
  829. WarehousingDetailId: warehouse.ID,
  830. AdviceId: advice.ID,
  831. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  832. StorehouseId: storeConfig.DrugStorehouseOut,
  833. WarehouseOutDetailId: lastDrugOutInfo.ID,
  834. LastPrice: warehouse.Price,
  835. }
  836. CreateDrugFlowOne(drugflow)
  837. //查询是否存在数据
  838. details := &models.DrugAutomaticReduceDetail{
  839. WarehouseOutId: warehouseout.ID,
  840. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  841. PatientId: advice.PatientId,
  842. Ctime: time.Now().Unix(),
  843. Mtime: time.Now().Unix(),
  844. Status: 1,
  845. RecordTime: advice.RecordDate,
  846. OrgId: orgID,
  847. DrugId: advice.DrugId,
  848. Count: stock_number,
  849. CountUnit: drup.MinUnit,
  850. WarehouseInfoId: warehouse.ID,
  851. AdviceId: advice.ID,
  852. StorehouseId: storeConfig.DrugStorehouseOut,
  853. }
  854. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  855. if errTwo != nil {
  856. return errTwo
  857. }
  858. info := models.XtDrugWarehouseInfo{
  859. ID: warehouse.ID,
  860. WarehousingId: warehouse.WarehousingId,
  861. DrugId: warehouse.DrugId,
  862. Number: warehouse.Number,
  863. ProductDate: warehouse.ProductDate,
  864. ExpiryDate: warehouse.ExpiryDate,
  865. WarehousingCount: warehouse.WarehousingCount,
  866. Price: warehouse.Price,
  867. TotalPrice: warehouse.TotalPrice,
  868. Dealer: warehouse.Dealer,
  869. Manufacturer: warehouse.Manufacturer,
  870. Remark: warehouse.Remark,
  871. Ctime: warehouse.Ctime,
  872. Mtime: warehouse.Mtime,
  873. Status: 1,
  874. OrgId: warehouse.OrgId,
  875. IsReturn: warehouse.IsReturn,
  876. WarehousingOrder: warehouse.WarehousingOrder,
  877. Type: warehouse.Type,
  878. RetailPrice: warehouse.RetailPrice,
  879. RetailTotalPrice: warehouse.RetailPrice,
  880. StockMaxNumber: 0,
  881. StockMinNumber: 0,
  882. BatchNumber: warehouse.BatchNumber,
  883. MaxUnit: warehouse.MaxUnit,
  884. MinUnit: warehouse.MinUnit,
  885. WarehousingInfoId: warehouse.WarehousingInfoId,
  886. StorehouseId: storeConfig.DrugStorehouseOut,
  887. IsCheck: 1,
  888. }
  889. errThree := UpDateDrugWarehouseInfoByStock(&info)
  890. if errThree != nil {
  891. return errThree
  892. }
  893. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  894. prescribingNumber_two_temp := deliver_number - stock_number
  895. overPlusNumber := float64(prescribingNumber_two_temp)
  896. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  897. advice.PrescribingNumberUnit = drup.MinUnit
  898. HisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  899. }
  900. return
  901. }
  902. // 药品出库 递归方式
  903. func AutoDrugDeliverInfo(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  904. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  905. var deliver_number int64 = 0
  906. var stock_number int64 = 0
  907. if advice.CountUnit == drup.MaxUnit {
  908. deliver_number = prescribingNumber * drup.MinNumber
  909. } else {
  910. deliver_number = prescribingNumber
  911. }
  912. // 根据先进先出原则,查询最先入库的批次,进行出库
  913. // 如果没有对应的库存,则报错
  914. //开启事物
  915. storeConfig, _ := GetAllStoreHouseConfig(orgID)
  916. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  917. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  918. var stockMax int64
  919. var stockMin int64
  920. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  921. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  922. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  923. UpdateMinNumber(lastWarehouse.ID, stockMin)
  924. }
  925. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  926. if err != nil {
  927. return err
  928. }
  929. // 将该批次的剩余库存数量转换为拆零数量
  930. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  931. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  932. if stock_number >= deliver_number {
  933. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  934. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  935. WarehouseOutId: warehouseout.ID,
  936. Status: 1,
  937. Ctime: time.Now().Unix(),
  938. Remark: advice.Remark,
  939. OrgId: orgID,
  940. Type: 1,
  941. Manufacturer: advice.Manufacturer,
  942. Dealer: warehouse.Dealer,
  943. IsSys: 0,
  944. SysRecordTime: advice.Ctime,
  945. DrugId: advice.DrugId,
  946. ExpiryDate: advice.ExpiryDate,
  947. ProductDate: advice.ProductDate,
  948. Number: advice.Number,
  949. BatchNumber: warehouse.BatchNumber,
  950. Count: deliver_number,
  951. RetailPrice: advice.RetailPrice,
  952. Price: warehouse.RetailPrice,
  953. WarehouseInfoId: warehouse.ID,
  954. CountUnit: drup.MinUnit,
  955. AdviceId: advice.ID,
  956. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  957. StorehouseId: advice.StorehouseId,
  958. AdminUserId: advice.AdminUserId,
  959. IsCheck: 1,
  960. }
  961. warehouseOutInfo.Count = prescribingNumber
  962. warehouseOutInfo.CountUnit = advice.CountUnit
  963. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  964. if errOne != nil {
  965. return errOne
  966. }
  967. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  968. drugflow := models.DrugFlow{
  969. WarehouseOutId: warehouseout.ID,
  970. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  971. DrugId: advice.DrugId,
  972. Number: warehouse.Number,
  973. ProductDate: advice.ProductDate,
  974. ExpireDate: advice.ExpiryDate,
  975. Count: deliver_number,
  976. Price: warehouse.RetailPrice,
  977. Status: 1,
  978. Ctime: time.Now().Unix(),
  979. UserOrgId: advice.OrgId,
  980. Manufacturer: advice.Manufacturer,
  981. Dealer: advice.Dealer,
  982. BatchNumber: warehouse.BatchNumber,
  983. MaxUnit: drup.MinUnit,
  984. ConsumableType: 2,
  985. IsEdit: 1,
  986. Creator: 0,
  987. IsSys: 0,
  988. WarehouseOutDetailId: drugWareInfo.ID,
  989. AdviceId: advice.ID,
  990. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  991. StorehouseId: advice.StorehouseId,
  992. AdminUserId: advice.AdminUserId,
  993. }
  994. CreateDrugFlowOne(drugflow)
  995. // 出库完成后,要减去对应批次的库存数量
  996. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  997. var maxNumber int64 = 0
  998. var minNumber int64 = 0
  999. if advice.CountUnit == drup.MinUnit {
  1000. maxNumber = prescribingNumber / drup.MinNumber
  1001. minNumber = prescribingNumber % drup.MinNumber
  1002. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  1003. minNumber = maxNumber
  1004. }
  1005. } else {
  1006. maxNumber = prescribingNumber
  1007. }
  1008. if warehouse.StockMaxNumber < maxNumber {
  1009. return errors.New("库存数量不足")
  1010. }
  1011. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  1012. warehouse.Mtime = time.Now().Unix()
  1013. if warehouse.StockMinNumber < minNumber {
  1014. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  1015. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  1016. } else {
  1017. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  1018. }
  1019. if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
  1020. warehouse.StockMinNumber = 0
  1021. }
  1022. if warehouse.StockMaxNumber < 0 {
  1023. return errors.New("库存数量不足")
  1024. }
  1025. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  1026. if errThree != nil {
  1027. return errThree
  1028. }
  1029. //查询默认仓库
  1030. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  1031. //查询默认仓库剩余多少库存
  1032. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  1033. var sum_count int64
  1034. var sum_in_count int64
  1035. for _, it := range list {
  1036. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  1037. if it.MaxUnit == baseDrug.MaxUnit {
  1038. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  1039. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  1040. }
  1041. sum_count += it.StockMaxNumber + it.StockMinNumber
  1042. sum_in_count += it.WarehousingCount
  1043. }
  1044. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  1045. return nil
  1046. } else {
  1047. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  1048. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1049. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1050. WarehouseOutId: warehouseout.ID,
  1051. Status: 1,
  1052. Ctime: time.Now().Unix(),
  1053. Remark: advice.Remark,
  1054. OrgId: orgID,
  1055. Type: 1,
  1056. Manufacturer: advice.Manufacturer,
  1057. Dealer: warehouse.Dealer,
  1058. IsSys: 0,
  1059. SysRecordTime: advice.Ctime,
  1060. DrugId: advice.DrugId,
  1061. ExpiryDate: warehouse.ExpiryDate,
  1062. ProductDate: warehouse.ProductDate,
  1063. Number: warehouse.Number,
  1064. BatchNumber: warehouse.BatchNumber,
  1065. Count: stock_number,
  1066. RetailPrice: advice.RetailPrice,
  1067. Price: warehouse.RetailPrice,
  1068. WarehouseInfoId: warehouse.ID,
  1069. CountUnit: drup.MinUnit,
  1070. AdviceId: advice.ID,
  1071. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1072. StorehouseId: advice.StorehouseId,
  1073. AdminUserId: advice.AdminUserId,
  1074. IsCheck: 1,
  1075. }
  1076. warehouseOutInfo.Count = stock_number
  1077. //warehouseOutInfo.CountUnit = advice.CountUnit
  1078. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1079. if errOne != nil {
  1080. return errOne
  1081. }
  1082. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  1083. drugflow := models.DrugFlow{
  1084. WarehouseOutId: warehouseout.ID,
  1085. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1086. DrugId: advice.DrugId,
  1087. Number: warehouse.Number,
  1088. ProductDate: advice.ProductDate,
  1089. ExpireDate: advice.ExpiryDate,
  1090. Count: stock_number,
  1091. Price: warehouse.RetailPrice,
  1092. Status: 1,
  1093. Ctime: time.Now().Unix(),
  1094. UserOrgId: advice.OrgId,
  1095. Manufacturer: advice.Manufacturer,
  1096. Dealer: advice.Dealer,
  1097. BatchNumber: warehouse.BatchNumber,
  1098. MaxUnit: drup.MinUnit,
  1099. ConsumableType: 2,
  1100. IsEdit: 1,
  1101. Creator: 0,
  1102. IsSys: 0,
  1103. WarehouseOutDetailId: drugWareInfo.ID,
  1104. AdviceId: advice.ID,
  1105. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1106. StorehouseId: advice.StorehouseId,
  1107. AdminUserId: advice.AdminUserId,
  1108. }
  1109. CreateDrugFlowOne(drugflow)
  1110. // 出库完成后,要将该批次库存清零
  1111. //扣减库存
  1112. warehouse.StockMaxNumber = 0
  1113. warehouse.StockMinNumber = 0
  1114. warehouse.Mtime = time.Now().Unix()
  1115. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  1116. //查询默认仓库
  1117. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  1118. //查询默认仓库剩余多少库存
  1119. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  1120. var sum_count int64
  1121. var sum_in_count int64
  1122. for _, it := range list {
  1123. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  1124. if it.MaxUnit == baseDrug.MaxUnit {
  1125. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  1126. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  1127. sum_count += it.StockMaxNumber + it.StockMinNumber
  1128. sum_in_count += it.WarehousingCount
  1129. }
  1130. }
  1131. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  1132. if errThree != nil {
  1133. return errThree
  1134. }
  1135. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1136. prescribingNumber_two_temp := deliver_number - stock_number
  1137. advice.CountUnit = drup.MinUnit
  1138. AutoDrugDeliverInfo(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  1139. }
  1140. return
  1141. }
  1142. // 耗材出库
  1143. func ConsumablesDeliveryTotal(orgID int64, patient_id int64, record_time int64, goods []*models.DialysisBeforePrepareGoods, goodOne []*models.NewDialysisBeforePrepareGoods, creator int64) (err error) {
  1144. //查询该患者当天已经出库的耗材信息
  1145. goods_yc, _ := FindConsumablesByDateTwo(orgID, patient_id, record_time)
  1146. // 和新请求的出库数据进行对比,分出那些是继续出库的,那些是需要删除出库的
  1147. for i := len(goods_yc) - 1; i >= 0; i-- {
  1148. goods_yc_temp := goods_yc[i]
  1149. for j := len(goods) - 1; j >= 0; j-- {
  1150. goods_temp := goods[j]
  1151. // 已经出库和新请求出库都存在该耗材,则判断出库数量,分成是继续出库,还是删除出库
  1152. if goods_yc_temp.GoodTypeId == goods_temp.GoodTypeId && goods_yc_temp.GoodId == goods_temp.GoodId {
  1153. // 已经出库和新请求出库的出库数量一致,则清除两个结构体里的数据(既不出库,也不删除出库)
  1154. if goods_yc_temp.Count == goods_temp.Count {
  1155. goods_yc = append(goods_yc[:i], goods_yc[i+1:]...)
  1156. goods = append(goods[:j], goods[j+1:]...)
  1157. break
  1158. }
  1159. // 如果已经出库的数量 大于 新请求出库的数量,则代表需要删除出库
  1160. if goods_yc_temp.Count > goods_temp.Count {
  1161. temp_count := goods_yc_temp.Count - goods_temp.Count
  1162. goods_yc[i].Count = temp_count
  1163. goods = append(goods[:j], goods[j+1:]...)
  1164. break
  1165. }
  1166. // 如果已经出库的数量 小于 新请求出库的梳理,则代表需要增加出库
  1167. if goods_yc_temp.Count < goods_temp.Count {
  1168. temp_count := goods_temp.Count - goods_yc_temp.Count
  1169. goods[j].Count = temp_count
  1170. goods_yc = append(goods_yc[:i], goods_yc[i+1:]...)
  1171. break
  1172. }
  1173. }
  1174. }
  1175. }
  1176. // goods_yc 这个数据就是需要已经出库了,但是现在需要删除出库的耗材数据
  1177. // goods 这个数据就是需要出库的耗材的数据(新增的数据)
  1178. if len(goods) > 0 {
  1179. out, err := FindStockOutByIsSys(orgID, 1, record_time)
  1180. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  1181. if err == gorm.ErrRecordNotFound {
  1182. //没有记录,则创建出库单
  1183. timeStr := time.Now().Format("2006-01-02")
  1184. timeArr := strings.Split(timeStr, "-")
  1185. total, _ := FindAllWarehouseOut(orgID)
  1186. total = total + 1
  1187. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  1188. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  1189. number = number + total
  1190. warehousing_out_order = "CKD" + strconv.FormatInt(number, 10)
  1191. warehouseOut := models.WarehouseOut{
  1192. WarehouseOutOrderNumber: warehousing_out_order,
  1193. OperationTime: time.Now().Unix(),
  1194. OrgId: orgID,
  1195. Creater: creator,
  1196. Ctime: time.Now().Unix(),
  1197. Status: 1,
  1198. WarehouseOutTime: record_time,
  1199. Dealer: 0,
  1200. Manufacturer: 0,
  1201. Type: 1,
  1202. IsSys: 1,
  1203. StorehouseId: houseConfig.StorehouseOutInfo,
  1204. IsCheck: 1,
  1205. }
  1206. err := AddSigleWarehouseOut(&warehouseOut)
  1207. if err != nil {
  1208. utils.TraceLog("创建出库单失败 err = %v", err)
  1209. return err
  1210. } else {
  1211. out = warehouseOut
  1212. }
  1213. }
  1214. for _, item := range goods {
  1215. var newCount int64 = 0
  1216. for _, it := range goodOne {
  1217. if item.GoodTypeId == it.GoodTypeId && item.GoodId == it.GoodId {
  1218. newCount = it.Count
  1219. }
  1220. }
  1221. var cha_count int64
  1222. var cha_count_two int64
  1223. //查询已经出库的数量
  1224. flowGood, _ := GetStockFlowIsBatchNumberTwo(patient_id, record_time, item.GoodId)
  1225. var out_count int64
  1226. for _, item := range flowGood {
  1227. out_count += item.Count
  1228. }
  1229. //已经出库的数量 减掉目前需要出库的数据
  1230. cha_count = item.Count - out_count
  1231. cha_count_two = out_count - item.Count
  1232. prepare := models.DialysisBeforePrepare{
  1233. GoodTypeId: item.GoodTypeId,
  1234. GoodId: item.GoodId,
  1235. Count: cha_count,
  1236. ProjectId: item.ProjectId,
  1237. StorehouseId: houseConfig.StorehouseOutInfo,
  1238. }
  1239. ConsumablesGoodDelivery(orgID, patient_id, record_time, &prepare, &out, newCount)
  1240. //数量增加
  1241. if cha_count > 0 {
  1242. //出库数量累加
  1243. ModifyGoodSumCount(houseConfig.StorehouseOutInfo, cha_count, orgID, item.GoodId)
  1244. }
  1245. //已经出库的数据 大于 目前需要出库的数据
  1246. if cha_count_two > 0 {
  1247. ModifyAddGoodSumCount(houseConfig.StorehouseOutInfo, cha_count_two, orgID, item.GoodId)
  1248. //退库数量增加
  1249. UpdateSumAddCancelCount(orgID, item.GoodId, houseConfig.StorehouseOutInfo, cha_count_two)
  1250. //生成退库单
  1251. }
  1252. //更新剩余库存
  1253. goodListSix, _ := GetSumGoodList(orgID, houseConfig.StorehouseOutInfo, item.GoodId)
  1254. var flush_count int64
  1255. for _, it := range goodListSix {
  1256. flush_count += it.StockCount
  1257. }
  1258. UpdateSumGood(orgID, houseConfig.StorehouseOutInfo, item.GoodId, flush_count)
  1259. }
  1260. }
  1261. if len(goods_yc) > 0 {
  1262. for _, good_yc := range goods_yc {
  1263. out, _ := FindStockOutByIsSys(orgID, 1, record_time)
  1264. ConsumablesDeliveryDelete(orgID, patient_id, record_time, good_yc, &out)
  1265. }
  1266. }
  1267. return nil
  1268. }
  1269. // 耗材出库
  1270. func ConsumablesDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, count int64) (err error) {
  1271. //开事务
  1272. var deliver_number int64 = 0
  1273. var stock_number int64 = 0
  1274. var maxNumber int64 = 0
  1275. deliver_number = goods.Count
  1276. // 根据先进先出原则,查询最先入库的批次,进行出库
  1277. // 如果没有对应的库存,则报错
  1278. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  1279. if err != nil {
  1280. return errors.New("库存数量不足")
  1281. }
  1282. stock_number = warehouse.StockCount
  1283. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1284. if stock_number >= deliver_number {
  1285. maxNumber = goods.Count
  1286. //出库
  1287. warehouse.StockCount = warehouse.StockCount - maxNumber
  1288. warehouse.Mtime = time.Now().Unix()
  1289. if warehouse.StockCount < 0 {
  1290. return errors.New("库存数量不足")
  1291. }
  1292. errThree := UpDateWarehouseInfoByStock(&warehouse)
  1293. if errThree != nil {
  1294. return errThree
  1295. }
  1296. //查询剩余库存
  1297. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  1298. var sum_count int64
  1299. for _, item := range goodList {
  1300. sum_count += item.StockCount
  1301. }
  1302. warehouseOutInfo := &models.WarehouseOutInfo{
  1303. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1304. WarehouseOutId: warehouseOut.ID,
  1305. WarehouseInfotId: warehouse.ID,
  1306. Status: 1,
  1307. Ctime: record_time,
  1308. Mtime: time.Now().Unix(),
  1309. Remark: warehouse.Remark,
  1310. OrgId: orgID,
  1311. Type: 1,
  1312. Manufacturer: warehouse.Manufacturer,
  1313. Dealer: warehouse.Dealer,
  1314. IsSys: 1,
  1315. SysRecordTime: record_time,
  1316. GoodTypeId: goods.GoodTypeId,
  1317. GoodId: goods.GoodId,
  1318. PatientId: patient_id,
  1319. Number: warehouse.Number,
  1320. LicenseNumber: warehouse.LicenseNumber,
  1321. Price: warehouse.PackingPrice,
  1322. ExpiryDate: warehouse.ExpiryDate,
  1323. ProductDate: warehouse.ProductDate,
  1324. ProjectId: goods.ProjectId,
  1325. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1326. StorehouseId: goods.StorehouseId,
  1327. IsCheck: 1,
  1328. }
  1329. warehouseOutInfo.Count = count
  1330. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
  1331. if errcodes == gorm.ErrRecordNotFound {
  1332. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  1333. if errOne != nil {
  1334. return errOne
  1335. }
  1336. } else if errcodes == nil {
  1337. UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId)
  1338. }
  1339. details := models.BloodAutomaticReduceDetail{
  1340. WarehouseOutId: warehouseOutInfo.ID,
  1341. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  1342. PatientId: patient_id,
  1343. Ctime: record_time,
  1344. Mtime: time.Now().Unix(),
  1345. Status: 1,
  1346. RecordTime: record_time,
  1347. OrgId: orgID,
  1348. GoodId: goods.GoodId,
  1349. GoodTypeId: goods.GoodTypeId,
  1350. Count: count,
  1351. ProjectId: goods.ProjectId,
  1352. StorehouseId: goods.StorehouseId,
  1353. }
  1354. //查询当天耗材是否已经存在数据
  1355. _, errcode := GetAutoMaticReduceDetail(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  1356. if errcode == gorm.ErrRecordNotFound {
  1357. errTwo := CreateAutoReduceRecord(&details)
  1358. if errTwo != nil {
  1359. return errTwo
  1360. }
  1361. //插入库存流水表
  1362. flow := models.VmStockFlow{
  1363. WarehouseOutId: warehouseOut.ID,
  1364. WarehousingId: warehouse.ID,
  1365. GoodId: goods.GoodId,
  1366. Number: warehouse.Number,
  1367. ProductDate: warehouse.ProductDate,
  1368. ExpireDate: warehouse.ExpiryDate,
  1369. Count: deliver_number,
  1370. Price: warehouse.PackingPrice,
  1371. Status: 1,
  1372. Ctime: record_time,
  1373. Mtime: time.Now().Unix(),
  1374. UserOrgId: orgID,
  1375. Manufacturer: warehouse.Manufacturer,
  1376. Dealer: warehouse.Dealer,
  1377. LicenseNumber: warehouse.LicenseNumber,
  1378. IsEdit: 2,
  1379. Creator: warehouseOut.Creater,
  1380. SystemTime: record_time,
  1381. ConsumableType: 3,
  1382. WarehouseOutDetailId: warehouseOutInfo.ID,
  1383. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1384. IsSys: 1,
  1385. PatientId: patient_id,
  1386. ProjectId: goods.ProjectId,
  1387. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1388. StorehouseId: goods.StorehouseId,
  1389. OverCount: sum_count,
  1390. }
  1391. //查询流水是否存在
  1392. exsit, errflow := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  1393. if errflow == gorm.ErrRecordNotFound {
  1394. //创建数据
  1395. errThre := CreateStockFlowOne(flow)
  1396. if errThre != nil {
  1397. return errThre
  1398. }
  1399. } else if errflow == nil {
  1400. flow := models.VmStockFlow{
  1401. ID: exsit.ID,
  1402. WarehouseOutId: warehouseOut.ID,
  1403. WarehousingId: warehouse.ID,
  1404. GoodId: goods.GoodId,
  1405. Number: warehouse.Number,
  1406. ProductDate: warehouse.ProductDate,
  1407. ExpireDate: warehouse.ExpiryDate,
  1408. Count: exsit.Count + goods.Count,
  1409. Price: warehouse.PackingPrice,
  1410. Status: 1,
  1411. Ctime: record_time,
  1412. Mtime: time.Now().Unix(),
  1413. UserOrgId: orgID,
  1414. Manufacturer: warehouse.Manufacturer,
  1415. Dealer: warehouse.Dealer,
  1416. LicenseNumber: warehouse.LicenseNumber,
  1417. IsEdit: 2,
  1418. Creator: warehouseOut.Creater,
  1419. SystemTime: record_time,
  1420. ConsumableType: 3,
  1421. WarehouseOutDetailId: warehouseOutInfo.ID,
  1422. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1423. IsSys: 1,
  1424. PatientId: patient_id,
  1425. ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  1426. ProjectId: goods.ProjectId,
  1427. StorehouseId: goods.StorehouseId,
  1428. OverCount: sum_count,
  1429. }
  1430. errFour := UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  1431. if errFour != nil {
  1432. return errFour
  1433. }
  1434. }
  1435. } else if errcode == nil {
  1436. flow := models.VmStockFlow{
  1437. WarehouseOutId: warehouseOut.ID,
  1438. WarehousingId: warehouse.ID,
  1439. GoodId: goods.GoodId,
  1440. Number: warehouse.Number,
  1441. ProductDate: warehouse.ProductDate,
  1442. ExpireDate: warehouse.ExpiryDate,
  1443. Count: deliver_number,
  1444. Price: warehouse.PackingPrice,
  1445. Status: 1,
  1446. Ctime: record_time,
  1447. Mtime: time.Now().Unix(),
  1448. UserOrgId: orgID,
  1449. Manufacturer: warehouse.Manufacturer,
  1450. Dealer: warehouse.Dealer,
  1451. LicenseNumber: warehouse.LicenseNumber,
  1452. IsEdit: 2,
  1453. Creator: warehouseOut.Creater,
  1454. SystemTime: record_time,
  1455. ConsumableType: 3,
  1456. WarehouseOutDetailId: warehouseOutInfo.ID,
  1457. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1458. IsSys: 1,
  1459. PatientId: patient_id,
  1460. ProjectId: goods.ProjectId,
  1461. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1462. StorehouseId: goods.StorehouseId,
  1463. OverCount: sum_count,
  1464. }
  1465. //查询流水是否存在
  1466. exsit, errflow := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  1467. if errflow == gorm.ErrRecordNotFound {
  1468. CreateStockFlowOne(flow)
  1469. DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  1470. CreateAutoReduceRecord(&details)
  1471. } else if errflow == nil {
  1472. flow := models.VmStockFlow{
  1473. ID: exsit.ID,
  1474. WarehouseOutId: warehouseOut.ID,
  1475. WarehousingId: warehouse.ID,
  1476. GoodId: goods.GoodId,
  1477. Number: warehouse.Number,
  1478. ProductDate: warehouse.ProductDate,
  1479. ExpireDate: warehouse.ExpiryDate,
  1480. Count: exsit.Count + goods.Count,
  1481. Price: warehouse.PackingPrice,
  1482. Status: 1,
  1483. Ctime: record_time,
  1484. Mtime: time.Now().Unix(),
  1485. UserOrgId: orgID,
  1486. Manufacturer: warehouse.Manufacturer,
  1487. Dealer: warehouse.Dealer,
  1488. LicenseNumber: warehouse.LicenseNumber,
  1489. IsEdit: 2,
  1490. Creator: warehouseOut.Creater,
  1491. SystemTime: record_time,
  1492. ConsumableType: 3,
  1493. WarehouseOutDetailId: warehouseOutInfo.ID,
  1494. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1495. IsSys: 1,
  1496. PatientId: patient_id,
  1497. ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  1498. ProjectId: goods.ProjectId,
  1499. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1500. StorehouseId: goods.StorehouseId,
  1501. OverCount: sum_count,
  1502. }
  1503. UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  1504. DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  1505. CreateAutoReduceRecord(&details)
  1506. }
  1507. }
  1508. return nil
  1509. } else {
  1510. // 当刚批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  1511. warehouse.StockCount = 0
  1512. warehouse.Mtime = time.Now().Unix()
  1513. errThree := UpDateWarehouseInfoByStock(&warehouse)
  1514. if errThree != nil {
  1515. return errThree
  1516. }
  1517. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  1518. var sum_count int64
  1519. for _, item := range goodList {
  1520. sum_count += item.StockCount
  1521. }
  1522. warehouseOutInfo := &models.WarehouseOutInfo{
  1523. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1524. WarehouseOutId: warehouseOut.ID,
  1525. WarehouseInfotId: warehouse.ID,
  1526. Status: 1,
  1527. Ctime: record_time,
  1528. Mtime: time.Now().Unix(),
  1529. Remark: warehouse.Remark,
  1530. OrgId: orgID,
  1531. Type: 1,
  1532. Manufacturer: warehouse.Manufacturer,
  1533. Dealer: warehouse.Dealer,
  1534. IsSys: 1,
  1535. SysRecordTime: record_time,
  1536. GoodTypeId: goods.GoodTypeId,
  1537. GoodId: goods.GoodId,
  1538. PatientId: patient_id,
  1539. Number: warehouse.Number,
  1540. LicenseNumber: warehouse.LicenseNumber,
  1541. Price: warehouse.PackingPrice,
  1542. ExpiryDate: warehouse.ExpiryDate,
  1543. ProductDate: warehouse.ProductDate,
  1544. ProjectId: goods.ProjectId,
  1545. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1546. StorehouseId: goods.StorehouseId,
  1547. IsCheck: 1,
  1548. }
  1549. warehouseOutInfo.Count = stock_number
  1550. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, goods.PatientId, record_time, goods.ProjectId)
  1551. if errcodes == gorm.ErrRecordNotFound {
  1552. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  1553. if errOne != nil {
  1554. return errOne
  1555. }
  1556. //插入库存流水表
  1557. flow := models.VmStockFlow{
  1558. WarehouseOutId: warehouseOut.ID,
  1559. WarehousingId: warehouse.ID,
  1560. GoodId: goods.GoodId,
  1561. Number: warehouse.Number,
  1562. ProductDate: warehouse.ProductDate,
  1563. ExpireDate: warehouse.ExpiryDate,
  1564. Count: stock_number,
  1565. Price: warehouse.PackingPrice,
  1566. Status: 1,
  1567. Ctime: record_time,
  1568. Mtime: time.Now().Unix(),
  1569. UserOrgId: orgID,
  1570. Manufacturer: warehouse.Manufacturer,
  1571. Dealer: warehouse.Dealer,
  1572. LicenseNumber: warehouse.LicenseNumber,
  1573. IsEdit: 2,
  1574. Creator: warehouseOut.Creater,
  1575. SystemTime: record_time,
  1576. ConsumableType: 3,
  1577. WarehouseOutDetailId: warehouseOutInfo.ID,
  1578. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1579. IsSys: 1,
  1580. PatientId: patient_id,
  1581. ProjectId: goods.ProjectId,
  1582. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1583. StorehouseId: goods.StorehouseId,
  1584. OverCount: sum_count,
  1585. }
  1586. //查询流水是否存在
  1587. exsit, errflows := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  1588. if errflows == gorm.ErrRecordNotFound {
  1589. CreateStockFlowOne(flow)
  1590. } else if errflows == nil {
  1591. flow := models.VmStockFlow{
  1592. ID: exsit.ID,
  1593. WarehouseOutId: warehouseOut.ID,
  1594. WarehousingId: warehouse.ID,
  1595. GoodId: goods.GoodId,
  1596. Number: warehouse.Number,
  1597. ProductDate: warehouse.ProductDate,
  1598. ExpireDate: warehouse.ExpiryDate,
  1599. Count: exsit.Count + goods.Count,
  1600. Price: warehouse.PackingPrice,
  1601. Status: 1,
  1602. Ctime: record_time,
  1603. Mtime: time.Now().Unix(),
  1604. UserOrgId: orgID,
  1605. Manufacturer: warehouse.Manufacturer,
  1606. Dealer: warehouse.Dealer,
  1607. LicenseNumber: warehouse.LicenseNumber,
  1608. IsEdit: 2,
  1609. Creator: warehouseOut.Creater,
  1610. SystemTime: record_time,
  1611. ConsumableType: 3,
  1612. WarehouseOutDetailId: warehouseOutInfo.ID,
  1613. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1614. IsSys: 1,
  1615. PatientId: patient_id,
  1616. ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  1617. ProjectId: goods.ProjectId,
  1618. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1619. StorehouseId: goods.StorehouseId,
  1620. OverCount: sum_count,
  1621. }
  1622. errOne := UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  1623. if errOne == nil {
  1624. }
  1625. }
  1626. } else if errcodes == nil {
  1627. goods.Count = deliver_number - stock_number
  1628. //更新数量为 该批次剩余数量 + 还有未出的数量
  1629. warehouseOutInfo.Count = stock_number
  1630. UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, goods.PatientId, record_time, goods.ProjectId)
  1631. //插入库存流水表
  1632. flow := models.VmStockFlow{
  1633. WarehouseOutId: warehouseOut.ID,
  1634. WarehousingId: warehouse.ID,
  1635. GoodId: goods.GoodId,
  1636. Number: warehouse.Number,
  1637. ProductDate: warehouse.ProductDate,
  1638. ExpireDate: warehouse.ExpiryDate,
  1639. Count: stock_number,
  1640. Price: warehouse.PackingPrice,
  1641. Status: 1,
  1642. Ctime: record_time,
  1643. Mtime: time.Now().Unix(),
  1644. UserOrgId: orgID,
  1645. Manufacturer: warehouse.Manufacturer,
  1646. Dealer: warehouse.Dealer,
  1647. LicenseNumber: warehouse.LicenseNumber,
  1648. IsEdit: 2,
  1649. Creator: warehouseOut.Creater,
  1650. SystemTime: record_time,
  1651. ConsumableType: 3,
  1652. WarehouseOutDetailId: warehouseOutInfo.ID,
  1653. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1654. IsSys: 1,
  1655. PatientId: patient_id,
  1656. ProjectId: goods.ProjectId,
  1657. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1658. StorehouseId: goods.StorehouseId,
  1659. OverCount: sum_count,
  1660. }
  1661. //查询流水是否存在
  1662. exsit, errflows := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  1663. if errflows == gorm.ErrRecordNotFound {
  1664. CreateStockFlowOne(flow)
  1665. } else if errflows == nil {
  1666. flow := models.VmStockFlow{
  1667. ID: exsit.ID,
  1668. WarehouseOutId: warehouseOut.ID,
  1669. WarehousingId: warehouse.ID,
  1670. GoodId: goods.GoodId,
  1671. Number: warehouse.Number,
  1672. ProductDate: warehouse.ProductDate,
  1673. ExpireDate: warehouse.ExpiryDate,
  1674. Count: exsit.Count + goods.Count,
  1675. Price: warehouse.PackingPrice,
  1676. Status: 1,
  1677. Ctime: record_time,
  1678. Mtime: time.Now().Unix(),
  1679. UserOrgId: orgID,
  1680. Manufacturer: warehouse.Manufacturer,
  1681. Dealer: warehouse.Dealer,
  1682. LicenseNumber: warehouse.LicenseNumber,
  1683. IsEdit: 2,
  1684. Creator: warehouseOut.Creater,
  1685. SystemTime: record_time,
  1686. ConsumableType: 3,
  1687. WarehouseOutDetailId: warehouseOutInfo.ID,
  1688. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1689. IsSys: 1,
  1690. PatientId: patient_id,
  1691. ProjectId: goods.ProjectId,
  1692. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1693. StorehouseId: goods.StorehouseId,
  1694. OverCount: sum_count,
  1695. }
  1696. UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  1697. }
  1698. }
  1699. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1700. goods.Count = deliver_number - stock_number
  1701. ConsumablesDelivery(orgID, patient_id, record_time, goods, warehouseOut, count)
  1702. }
  1703. return nil
  1704. }
  1705. // 耗材出库删除
  1706. func ConsumablesDeliveryDelete(orgID int64, patient_id int64, record_time int64, good_yc *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut) (err error) {
  1707. // 先根据相关信息查询当天该耗材的出库信息
  1708. warehouseOutInfos, err := FindStockOutInfoByStock(orgID, good_yc.GoodTypeId, good_yc.GoodId, record_time, patient_id)
  1709. if err != nil {
  1710. return err
  1711. }
  1712. var delete_count int64 = 0
  1713. for _, ware := range warehouseOutInfos {
  1714. // 删除出库完成后,要增加对应批次的库存数量
  1715. errThree := UpDateWarehouseInfoByStockDelete(ware.WarehouseInfotId, delete_count, patient_id, record_time, good_yc.GoodId)
  1716. if errThree != nil {
  1717. return errThree
  1718. }
  1719. //查询剩余库存
  1720. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  1721. goodList, _ := GetAllGoodSumCount(good_yc.GoodId, orgID, houseConfig.StorehouseOutInfo)
  1722. var sum_count int64
  1723. for _, item := range goodList {
  1724. sum_count += item.StockCount
  1725. }
  1726. // 判断当前出库的数据和删除出库数量
  1727. if good_yc.Count <= ware.Count {
  1728. delete_count = good_yc.Count
  1729. } else {
  1730. delete_count = ware.Count
  1731. }
  1732. // 在出库记录表里记录退库详情
  1733. warehouseOutInfo := &models.WarehouseOutInfo{
  1734. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1735. WarehouseOutId: warehouseOut.ID,
  1736. //WarehouseInfoId: warehouse.ID,
  1737. Status: 1,
  1738. Ctime: record_time,
  1739. Mtime: time.Now().Unix(),
  1740. Remark: "",
  1741. OrgId: orgID,
  1742. Type: 1,
  1743. Manufacturer: 0,
  1744. Dealer: 0,
  1745. IsSys: 2,
  1746. SysRecordTime: record_time,
  1747. GoodTypeId: good_yc.GoodTypeId,
  1748. GoodId: good_yc.GoodId,
  1749. PatientId: patient_id,
  1750. StorehouseId: houseConfig.StorehouseOutInfo,
  1751. OverCount: sum_count,
  1752. }
  1753. warehouseOutInfo.Count = ware.Count - delete_count
  1754. stockInInfo, _ := FindLastStockInInfoRecord(good_yc.GoodId, orgID)
  1755. warehouseOutInfo.Price = stockInInfo.Price
  1756. errOne := UpdateAutoMaticReduceDetail(good_yc.GoodId, good_yc.GoodTypeId, record_time, patient_id, orgID, warehouseOutInfo)
  1757. if errOne != nil {
  1758. return errOne
  1759. } else {
  1760. details := &models.AutomaticReduceDetail{
  1761. WarehouseOutId: warehouseOutInfo.ID,
  1762. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  1763. PatientId: patient_id,
  1764. Ctime: record_time,
  1765. Mtime: time.Now().Unix(),
  1766. Status: 1,
  1767. RecordTime: record_time,
  1768. OrgId: orgID,
  1769. GoodId: good_yc.GoodId,
  1770. GoodTypeId: good_yc.GoodTypeId,
  1771. Count: warehouseOutInfo.Count,
  1772. Type: 2,
  1773. StorehouseId: houseConfig.StorehouseOutInfo,
  1774. }
  1775. //查询是否当天已经存在数据
  1776. _, errcode := GetAutoMaticReduceDetail(orgID, patient_id, record_time, good_yc.GoodId, good_yc.GoodTypeId)
  1777. if errcode == gorm.ErrRecordNotFound {
  1778. errTwo := AddSigleAutoReduceRecordInfo(details)
  1779. if errTwo != nil {
  1780. return errTwo
  1781. }
  1782. } else if errcode == nil {
  1783. DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, good_yc.GoodId, good_yc.GoodTypeId)
  1784. AddSigleAutoReduceRecordInfo(details)
  1785. }
  1786. }
  1787. //扣减数量相加
  1788. ModifyAddGoodSumCount(houseConfig.StorehouseOutInfo, delete_count, good_yc.UserOrgId, good_yc.GoodId)
  1789. //退库数量相加
  1790. UpdateSumAddCancelCount(good_yc.UserOrgId, good_yc.GoodId, houseConfig.StorehouseOutInfo, delete_count)
  1791. operation_time := time.Now().Unix()
  1792. //创建退库单
  1793. timeStr := time.Now().Format("2006-01-02")
  1794. timeArr := strings.Split(timeStr, "-")
  1795. total, _ := FindAllCancelStockTotal(good_yc.UserOrgId)
  1796. total = total + 1
  1797. orderNumber := "CKTKD" + strconv.FormatInt(good_yc.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  1798. cancelStock := models.CancelStock{
  1799. OrderNumber: orderNumber,
  1800. OperaTime: operation_time,
  1801. OrgId: good_yc.UserOrgId,
  1802. Creater: warehouseOut.Creater,
  1803. Ctime: time.Now().Unix(),
  1804. Status: 1,
  1805. ReturnTime: record_time,
  1806. Type: 1,
  1807. StorehouseId: houseConfig.StorehouseOutInfo,
  1808. IsCheck: 1,
  1809. }
  1810. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, good_yc.UserOrgId)
  1811. if msgerrkonde == gorm.ErrRecordNotFound {
  1812. AddSigleCancelStock(&cancelStock)
  1813. }
  1814. cancel, _ := GetLastCancelStockById(good_yc.UserOrgId)
  1815. manufacturer, _ := GetManufactureById(ware.Manufacturer)
  1816. deaerler, _ := GetDealerById(ware.Dealer)
  1817. cancelStockInfo := models.CancelStockInfo{
  1818. GoodId: ware.GoodId,
  1819. CancelStockId: cancel.ID,
  1820. GoodTypeId: ware.GoodTypeId,
  1821. Count: delete_count,
  1822. Price: ware.Price,
  1823. Total: 0,
  1824. ProductDate: ware.ProductDate,
  1825. ExpiryDate: ware.ExpiryDate,
  1826. Ctime: record_time,
  1827. Mtime: time.Now().Unix(),
  1828. Status: 1,
  1829. OrgId: good_yc.UserOrgId,
  1830. OrderNumber: cancel.OrderNumber,
  1831. Type: 0,
  1832. Dealer: deaerler.DealerName,
  1833. Manufacturer: manufacturer.ManufacturerName,
  1834. Number: ware.Number,
  1835. RegisterAccount: "",
  1836. Remark: "",
  1837. WarehouseInfoId: ware.WarehouseInfotId,
  1838. PatientId: ware.PatientId,
  1839. RecordDate: record_time,
  1840. StorehouseId: houseConfig.StorehouseOutInfo,
  1841. IsCheck: 1,
  1842. }
  1843. CreateCancelStockInfoOne(&cancelStockInfo)
  1844. cancelInfo, _ := GetLastCancelStockInfoByGoodId(ware.GoodId)
  1845. goodListOne, _ := GetSumGoodList(good_yc.UserOrgId, houseConfig.StorehouseOutInfo, good_yc.GoodId)
  1846. var over_count int64
  1847. for _, it := range goodListOne {
  1848. over_count += it.StockCount
  1849. }
  1850. flow := models.VmStockFlow{
  1851. WarehousingId: ware.WarehouseInfotId,
  1852. GoodId: ware.GoodId,
  1853. Number: ware.Number,
  1854. LicenseNumber: ware.LicenseNumber,
  1855. Count: ware.Count,
  1856. UserOrgId: ware.OrgId,
  1857. PatientId: ware.PatientId,
  1858. SystemTime: record_time,
  1859. ConsumableType: 7,
  1860. IsSys: 0,
  1861. WarehousingOrder: "",
  1862. WarehouseOutId: ware.WarehouseOutId,
  1863. WarehouseOutOrderNumber: ware.WarehouseOutOrderNumber,
  1864. IsEdit: 0,
  1865. CancelStockId: cancel.ID,
  1866. CancelOrderNumber: cancel.OrderNumber,
  1867. Manufacturer: manufacturer.ID,
  1868. Dealer: 0,
  1869. Creator: warehouseOut.Creater,
  1870. UpdateCreator: 0,
  1871. Status: 1,
  1872. Ctime: record_time,
  1873. Mtime: time.Now().Unix(),
  1874. Price: ware.Price,
  1875. WarehousingDetailId: ware.WarehouseInfotId,
  1876. WarehouseOutDetailId: ware.ID,
  1877. CancelOutDetailId: cancelInfo.ID,
  1878. ProductDate: ware.ProductDate,
  1879. ExpireDate: ware.ExpiryDate,
  1880. StorehouseId: houseConfig.StorehouseOutInfo,
  1881. OverCount: over_count,
  1882. }
  1883. CreateStockFlowOne(flow)
  1884. // 增加了对应的库存后,看看还有多少需要退库的
  1885. good_yc.Count = good_yc.Count - delete_count
  1886. if good_yc.Count == 0 {
  1887. return nil
  1888. }
  1889. }
  1890. if good_yc.Count == 0 {
  1891. return nil
  1892. } else {
  1893. return errors.New("退库和出库数据不匹配")
  1894. }
  1895. }
  1896. func GetAutoReduceRecordInfoByPatientId(orgid int64, patient_id int64, recordTime int64) (autoReduce []*models.AutomaticReduceDetail, err error) {
  1897. err = XTReadDB().Model(&autoReduce).Where("org_id = ? and patient_id = ? and record_time = ? and status = 1", orgid, patient_id, recordTime).Find(&autoReduce).Error
  1898. return autoReduce, err
  1899. }
  1900. func DeleteDialysisBefor(orgid int64, patient_id int64, record_date int64, goodid int64, goodtypeid int64) error {
  1901. prepare := models.DialysisBeforePrepare{}
  1902. 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
  1903. return err
  1904. }
  1905. func DeleteDialysisBeforThree(orgid int64, patient_id int64, record_date int64) error {
  1906. prepare := models.DialysisBeforePrepare{}
  1907. 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
  1908. return err
  1909. }
  1910. func GetWarehouseById(id int64) (models.WarehouseOut, error) {
  1911. out := models.WarehouseOut{}
  1912. err := XTReadDB().Where("id=? and status = 1", id).Find(&out).Error
  1913. return out, err
  1914. }
  1915. // 耗材出库
  1916. func ConsumablesDeliveryOne(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64) (err error) {
  1917. ////开事务
  1918. //tx := XTWriteDB().Begin()
  1919. var deliver_number int64 = 0
  1920. var stock_number int64 = 0
  1921. var maxNumber int64 = 0
  1922. deliver_number = goods.Count
  1923. // 根据先进先出原则,查询最先入库的批次,进行出库
  1924. // 如果没有对应的库存,则报错
  1925. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  1926. if err != nil {
  1927. return err
  1928. }
  1929. stock_number = warehouse.StockCount
  1930. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1931. if stock_number >= deliver_number {
  1932. warehouseOutInfo := &models.WarehouseOutInfo{
  1933. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1934. WarehouseOutId: warehouseOut.ID,
  1935. WarehouseInfotId: warehouse.ID,
  1936. Status: 1,
  1937. Ctime: time.Now().Unix(),
  1938. Remark: goods.Remark,
  1939. OrgId: orgID,
  1940. Type: 1,
  1941. Manufacturer: goods.Manufacturer,
  1942. Dealer: goods.Dealer,
  1943. IsSys: 0,
  1944. SysRecordTime: record_time,
  1945. GoodTypeId: goods.GoodTypeId,
  1946. GoodId: goods.GoodId,
  1947. ExpiryDate: goods.ExpiryDate,
  1948. ProductDate: goods.ProductDate,
  1949. Number: warehouse.Number,
  1950. Price: goods.Price,
  1951. LicenseNumber: goods.LicenseNumber,
  1952. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1953. StorehouseId: goods.StorehouseId,
  1954. AdminUserId: goods.AdminUserId,
  1955. BuyPrice: warehouse.Price,
  1956. StockCount: goods.StockCount,
  1957. }
  1958. warehouseOutInfo.Count = goods.Count
  1959. //添加出库单详情
  1960. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  1961. if errOne != nil {
  1962. return errOne
  1963. }
  1964. stockFlow := models.VmStockFlow{
  1965. WarehousingId: warehouse.ID,
  1966. GoodId: goods.GoodId,
  1967. Number: warehouse.Number,
  1968. LicenseNumber: goods.LicenseNumber,
  1969. Count: goods.Count,
  1970. UserOrgId: goods.OrgId,
  1971. PatientId: goods.PatientId,
  1972. SystemTime: record_time,
  1973. ConsumableType: 2,
  1974. IsSys: goods.IsSys,
  1975. WarehousingOrder: "",
  1976. WarehouseOutId: warehouseOut.ID,
  1977. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1978. IsEdit: 1,
  1979. CancelStockId: 0,
  1980. CancelOrderNumber: "",
  1981. Manufacturer: goods.Manufacturer,
  1982. Dealer: goods.Dealer,
  1983. Creator: creator,
  1984. UpdateCreator: 0,
  1985. Status: 1,
  1986. Ctime: time.Now().Unix(),
  1987. Mtime: 0,
  1988. Price: warehouse.PackingPrice,
  1989. WarehousingDetailId: 0,
  1990. WarehouseOutDetailId: warehouseOutInfo.ID,
  1991. CancelOutDetailId: 0,
  1992. ProductDate: goods.ProductDate,
  1993. ExpireDate: goods.ExpiryDate,
  1994. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1995. StorehouseId: warehouse.StorehouseId,
  1996. AdminUserId: goods.AdminUserId,
  1997. BuyPrice: warehouse.Price,
  1998. StockCount: goods.StockCount,
  1999. }
  2000. //创建出库流水
  2001. CreateStockFlowOne(stockFlow)
  2002. maxNumber = goods.Count
  2003. if warehouse.StockCount < maxNumber {
  2004. return errors.New("库存数量不足")
  2005. }
  2006. warehouse.StockCount = warehouse.StockCount - maxNumber
  2007. warehouse.Mtime = time.Now().Unix()
  2008. //扣减库存
  2009. errThree := UpDateWarehouseInfoByStock(&warehouse)
  2010. fmt.Println(errThree)
  2011. //if errThree != nil {
  2012. // utils.ErrorLog("事务失败,原因为: %v", errThree.Error())
  2013. // tx.Rollback()
  2014. //} else {
  2015. // tx.Commit()
  2016. //}
  2017. //查询该机构默认仓库
  2018. storeConfig, _ := GetAllStoreHouseConfig(goods.OrgId)
  2019. //查询剩余库存
  2020. goodList, _ := GetGoodSumCountByStoreId(storeConfig.StorehouseOutInfo, goods.GoodId, goods.OrgId)
  2021. var sum_count int64
  2022. var sum_in_count int64
  2023. for _, item := range goodList {
  2024. sum_count += item.StockCount
  2025. sum_in_count += item.WarehousingCount
  2026. }
  2027. UpdateGoodByGoodId(goods.GoodId, sum_count, sum_in_count, goods.OrgId)
  2028. if errThree != nil {
  2029. return errThree
  2030. }
  2031. } else {
  2032. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2033. warehouseOutInfo := &models.WarehouseOutInfo{
  2034. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2035. WarehouseOutId: warehouseOut.ID,
  2036. WarehouseInfotId: warehouse.ID,
  2037. Status: 1,
  2038. Ctime: time.Now().Unix(),
  2039. Remark: goods.Remark,
  2040. OrgId: orgID,
  2041. Type: 1,
  2042. Manufacturer: goods.Manufacturer,
  2043. Dealer: goods.Dealer,
  2044. IsSys: 0,
  2045. SysRecordTime: record_time,
  2046. GoodTypeId: goods.GoodTypeId,
  2047. GoodId: goods.GoodId,
  2048. ExpiryDate: goods.ExpiryDate,
  2049. ProductDate: goods.ProductDate,
  2050. Number: warehouse.Number,
  2051. Price: warehouse.PackingPrice,
  2052. LicenseNumber: goods.LicenseNumber,
  2053. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2054. StorehouseId: warehouse.StorehouseId,
  2055. AdminUserId: goods.AdminUserId,
  2056. BuyPrice: warehouse.Price,
  2057. StockCount: goods.StockCount,
  2058. IsCheck: 1,
  2059. }
  2060. warehouseOutInfo.Count = stock_number
  2061. //查询是否已经生成出库单
  2062. info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  2063. if errcodes == gorm.ErrRecordNotFound {
  2064. //创建出库单
  2065. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  2066. if errOne != nil {
  2067. return errOne
  2068. }
  2069. } else {
  2070. warehouseOutInfo.Count = info.Count + stock_number
  2071. UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  2072. }
  2073. stockFlow := models.VmStockFlow{
  2074. WarehousingId: warehouse.ID,
  2075. GoodId: goods.GoodId,
  2076. Number: warehouse.Number,
  2077. LicenseNumber: goods.LicenseNumber,
  2078. Count: stock_number,
  2079. UserOrgId: goods.OrgId,
  2080. PatientId: 0,
  2081. SystemTime: record_time,
  2082. ConsumableType: 2,
  2083. IsSys: 0,
  2084. WarehousingOrder: "",
  2085. WarehouseOutId: warehouseOut.ID,
  2086. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2087. IsEdit: 1,
  2088. CancelStockId: 0,
  2089. CancelOrderNumber: "",
  2090. Manufacturer: goods.Manufacturer,
  2091. Dealer: goods.Dealer,
  2092. Creator: creator,
  2093. UpdateCreator: 0,
  2094. Status: 1,
  2095. Ctime: time.Now().Unix(),
  2096. Mtime: 0,
  2097. Price: warehouse.PackingPrice,
  2098. WarehousingDetailId: 0,
  2099. WarehouseOutDetailId: warehouseOutInfo.ID,
  2100. CancelOutDetailId: 0,
  2101. ProductDate: goods.ProductDate,
  2102. ExpireDate: goods.ExpiryDate,
  2103. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2104. StorehouseId: goods.StorehouseId,
  2105. AdminUserId: goods.AdminUserId,
  2106. BuyPrice: warehouse.Price,
  2107. StockCount: goods.StockCount,
  2108. }
  2109. //创建出库明细
  2110. CreateStockFlowOne(stockFlow)
  2111. //查询该机构默认仓库
  2112. storeConfig, _ := GetAllStoreHouseConfig(goods.OrgId)
  2113. //查询剩余库存
  2114. goodList, _ := GetGoodSumCountByStoreId(storeConfig.StorehouseOutInfo, goods.GoodId, goods.OrgId)
  2115. var sum_count int64
  2116. var sum_in_count int64
  2117. for _, item := range goodList {
  2118. sum_count += item.StockCount
  2119. sum_in_count += item.WarehousingCount
  2120. }
  2121. UpdateGoodByGoodId(goods.GoodId, sum_count, sum_in_count, goods.OrgId)
  2122. // 出库完成后,要将该批次库存清零
  2123. warehouse.StockCount = 0
  2124. warehouse.Mtime = time.Now().Unix()
  2125. errThree := UpDateWarehouseInfoByStock(&warehouse)
  2126. fmt.Println(errThree)
  2127. //if errThree != nil {
  2128. // utils.ErrorLog("事务失败,原因为: %v", errThree.Error())
  2129. // tx.Rollback()
  2130. //} else {
  2131. // tx.Commit()
  2132. //}
  2133. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2134. goods.Count = deliver_number - stock_number
  2135. ConsumablesDeliveryOne(orgID, record_time, goods, warehouseOut, count, creator)
  2136. }
  2137. return nil
  2138. }
  2139. // 退库逻辑
  2140. func ConsumablesDeliveryTwo(orgID int64, goods *models.WarehouseOutInfo, count int64) (err error) {
  2141. ////开事务
  2142. //tx := XTWriteDB().Begin()
  2143. var deliver_number int64 = 0
  2144. var stock_number int64 = 0
  2145. var maxNumber int64 = 0
  2146. deliver_number = goods.Count
  2147. // 根据先进先出原则,查询最先入库的批次,进行出库
  2148. warehouse, err := FindFirstWarehousingInfoByGoodId(goods.GoodId, goods.ID, goods.StorehouseId)
  2149. if err != nil {
  2150. return err
  2151. }
  2152. stock_number = warehouse.StockCount
  2153. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  2154. if stock_number >= deliver_number {
  2155. warehouseOutInfo := &models.WarehouseOutInfo{
  2156. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  2157. WarehouseOutId: goods.ID,
  2158. WarehouseInfotId: warehouse.ID,
  2159. Status: 1,
  2160. Ctime: time.Now().Unix(),
  2161. Remark: goods.Remark,
  2162. OrgId: orgID,
  2163. Type: 1,
  2164. Manufacturer: goods.Manufacturer,
  2165. Dealer: goods.Dealer,
  2166. IsSys: 0,
  2167. GoodTypeId: goods.GoodTypeId,
  2168. GoodId: goods.GoodId,
  2169. ExpiryDate: goods.ExpiryDate,
  2170. ProductDate: goods.ProductDate,
  2171. Number: goods.Number,
  2172. Price: warehouse.PackingPrice,
  2173. LicenseNumber: goods.LicenseNumber,
  2174. ConsumableType: 2,
  2175. }
  2176. warehouseOutInfo.Count = goods.Count
  2177. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  2178. if errOne != nil {
  2179. return errOne
  2180. }
  2181. maxNumber = goods.Count
  2182. if warehouse.StockCount < maxNumber { //这段逻辑根本不会执行,因为当库存数量大于或等于出库数量才会进入这个分支
  2183. return errors.New("库存数量不足")
  2184. }
  2185. warehouse.StockCount = warehouse.StockCount - maxNumber
  2186. warehouse.Mtime = time.Now().Unix()
  2187. if warehouse.StockCount <= 0 {
  2188. return errors.New("库存数量不足")
  2189. }
  2190. errThree := UpDateWarehouseInfoByStock(&warehouse)
  2191. fmt.Println(errThree)
  2192. //if errThree != nil {
  2193. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  2194. // tx.Rollback()
  2195. //} else {
  2196. // tx.Commit()
  2197. //}
  2198. return
  2199. } else {
  2200. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2201. warehouseOutInfo := &models.WarehouseOutInfo{
  2202. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  2203. WarehouseOutId: goods.ID,
  2204. WarehouseInfotId: warehouse.ID,
  2205. Status: 1,
  2206. Ctime: time.Now().Unix(),
  2207. Remark: goods.Remark,
  2208. OrgId: orgID,
  2209. Type: 1,
  2210. Manufacturer: goods.Manufacturer,
  2211. Dealer: goods.Dealer,
  2212. IsSys: 0,
  2213. GoodTypeId: goods.GoodTypeId,
  2214. GoodId: goods.GoodId,
  2215. ExpiryDate: goods.ExpiryDate,
  2216. ProductDate: goods.ProductDate,
  2217. Number: goods.Number,
  2218. Price: warehouse.PackingPrice,
  2219. LicenseNumber: goods.LicenseNumber,
  2220. ConsumableType: 2,
  2221. IsCheck: 1,
  2222. StorehouseId: goods.StorehouseId,
  2223. }
  2224. warehouseOutInfo.Count = stock_number
  2225. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  2226. if errOne != nil {
  2227. return errOne
  2228. }
  2229. // 出库完成后,要将该批次库存清零
  2230. warehouse.StockCount = 0
  2231. warehouse.Mtime = time.Now().Unix()
  2232. errThree := UpDateWarehouseInfoByStock(&warehouse)
  2233. //if errThree != nil {
  2234. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  2235. // tx.Rollback()
  2236. //} else {
  2237. // tx.Commit()
  2238. //}
  2239. fmt.Println(errThree)
  2240. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2241. goods.Count = deliver_number - stock_number
  2242. ConsumablesDeliveryTwo(orgID, goods, count)
  2243. }
  2244. return nil
  2245. }
  2246. func DeleteWarehouseInfo(id int64) error {
  2247. err := XTWriteDB().Model(models.WarehousingInfo{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": 0}).Error
  2248. return err
  2249. }
  2250. // 药品出库 递归方式
  2251. func NewDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DoctorAdvice) (err error) {
  2252. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  2253. var deliver_number int64 = 0
  2254. var stock_number int64 = 0
  2255. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  2256. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  2257. var drug_price float64
  2258. //按最小单位数据计算
  2259. deliver_number = count
  2260. if advice.PrescribingNumberUnit == drup.MaxUnit {
  2261. drug_price = drup.RetailPrice
  2262. }
  2263. if advice.PrescribingNumberUnit == drup.MinUnit {
  2264. drug_price = drup.MinPrice
  2265. }
  2266. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  2267. drug_price = drup.RetailPrice
  2268. }
  2269. // 根据先进先出原则,查询最先入库的批次,进行出库
  2270. // 如果没有对应的库存,则报错
  2271. //查询默认仓库
  2272. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  2273. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2274. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  2275. var stockMax int64
  2276. var stockMin int64
  2277. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  2278. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  2279. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  2280. UpdateMinNumber(lastWarehouse.ID, stockMin)
  2281. }
  2282. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2283. if err != nil {
  2284. return err
  2285. }
  2286. // 将该批次的剩余库存数量转换为拆零数量
  2287. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  2288. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  2289. if stock_number >= deliver_number {
  2290. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2291. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2292. WarehouseOutId: warehouseout.ID,
  2293. Status: 1,
  2294. Ctime: time.Now().Unix(),
  2295. Remark: warehouse.Remark,
  2296. OrgId: orgID,
  2297. Type: 1,
  2298. Manufacturer: warehouse.Manufacturer,
  2299. Dealer: warehouse.Dealer,
  2300. IsSys: 1,
  2301. SysRecordTime: advice.RecordDate,
  2302. DrugId: advice.DrugId,
  2303. Number: warehouse.Number,
  2304. BatchNumber: warehouse.BatchNumber,
  2305. Price: warehouse.RetailPrice,
  2306. CountUnit: drup.MinUnit,
  2307. RetailPrice: drug_price,
  2308. ProductDate: warehouse.ProductDate,
  2309. ExpiryDate: warehouse.ExpiryDate,
  2310. PatientId: advice.PatientId,
  2311. Count: deliver_number, //出最小单位
  2312. WarehouseInfoId: warehouse.ID,
  2313. AdviceId: advice.ID,
  2314. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2315. StorehouseId: storeConfig.DrugStorehouseOut,
  2316. IsCheck: 1,
  2317. }
  2318. drugflow := models.DrugFlow{
  2319. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2320. WarehouseOutId: warehouseout.ID,
  2321. DrugId: advice.DrugId,
  2322. Number: warehouse.Number,
  2323. ProductDate: warehouse.ProductDate,
  2324. ExpireDate: warehouse.ExpiryDate,
  2325. Count: deliver_number, //按最小单位计算,
  2326. Price: warehouse.RetailPrice,
  2327. Status: 1,
  2328. Ctime: time.Now().Unix(),
  2329. UserOrgId: orgID,
  2330. Manufacturer: warehouse.Manufacturer,
  2331. Dealer: warehouse.Dealer,
  2332. BatchNumber: warehouse.BatchNumber,
  2333. MaxUnit: drup.MinUnit,
  2334. ConsumableType: 3,
  2335. IsEdit: 1,
  2336. Creator: advice.ExecutionStaff,
  2337. IsSys: 1,
  2338. PatientId: advice.PatientId,
  2339. WarehousingDetailId: warehouse.ID,
  2340. AdviceId: advice.ID,
  2341. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2342. StorehouseId: storeConfig.DrugStorehouseOut,
  2343. }
  2344. CreateDrugFlowOne(drugflow)
  2345. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2346. if errOne != nil {
  2347. return errOne
  2348. }
  2349. //查询是否存在数据
  2350. details := &models.DrugAutomaticReduceDetail{
  2351. WarehouseOutId: warehouseout.ID,
  2352. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2353. PatientId: advice.PatientId,
  2354. Ctime: time.Now().Unix(),
  2355. Mtime: time.Now().Unix(),
  2356. Status: 1,
  2357. RecordTime: advice.RecordDate,
  2358. OrgId: orgID,
  2359. DrugId: advice.DrugId,
  2360. Count: deliver_number,
  2361. CountUnit: drup.MinUnit,
  2362. WarehouseInfoId: warehouse.ID,
  2363. StorehouseId: storeConfig.DrugStorehouseOut,
  2364. }
  2365. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2366. if errTwo != nil {
  2367. return errTwo
  2368. }
  2369. // 出库完成后,要减去对应批次的库存数量
  2370. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  2371. var maxNumber int64 = 0
  2372. var minNumber int64 = 0
  2373. //var stock_max_number int64 = 0
  2374. //stock_max_number = warehouse.StockMaxNumber
  2375. maxNumber = deliver_number / drup.MinNumber
  2376. minNumber = deliver_number % drup.MinNumber
  2377. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  2378. minNumber = maxNumber
  2379. }
  2380. if drup.MaxUnit != drup.MinUnit {
  2381. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  2382. return errors.New("库存数量不足")
  2383. }
  2384. }
  2385. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  2386. if warehouse.StockMaxNumber < 0 {
  2387. warehouse.StockMaxNumber = 0
  2388. }
  2389. if warehouse.StockMinNumber < 0 {
  2390. warehouse.StockMinNumber = 0
  2391. }
  2392. warehouse.Mtime = time.Now().Unix()
  2393. if warehouse.StockMinNumber < minNumber {
  2394. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  2395. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  2396. } else {
  2397. if minNumber > 0 {
  2398. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2399. }
  2400. if minNumber == 0 && maxNumber != 1 {
  2401. if warehouse.StockMinNumber > 0 {
  2402. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2403. }
  2404. }
  2405. }
  2406. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  2407. if (warehouse.StockMinNumber - deliver_number) > 0 {
  2408. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2409. //if warehouse.StockMinNumber == 0 {
  2410. // warehouse.StockMaxNumber = stock_max_number
  2411. //}
  2412. }
  2413. }
  2414. if drup.MaxUnit != drup.MinUnit {
  2415. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  2416. return errors.New("库存数量不足")
  2417. }
  2418. }
  2419. if warehouse.StockMinNumber <= 0 {
  2420. warehouse.StockMinNumber = 0
  2421. }
  2422. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  2423. if errThree != nil {
  2424. return errThree
  2425. }
  2426. return nil
  2427. } else {
  2428. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2429. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2430. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2431. WarehouseOutId: warehouseout.ID,
  2432. Status: 1,
  2433. Ctime: time.Now().Unix(),
  2434. Remark: warehouse.Remark,
  2435. OrgId: orgID,
  2436. Type: 1,
  2437. Manufacturer: warehouse.Manufacturer,
  2438. Dealer: warehouse.Dealer,
  2439. IsSys: 1,
  2440. SysRecordTime: advice.RecordDate,
  2441. DrugId: advice.DrugId,
  2442. Number: warehouse.Number,
  2443. BatchNumber: warehouse.BatchNumber,
  2444. Price: warehouse.RetailPrice,
  2445. CountUnit: drup.MinUnit,
  2446. ProductDate: warehouse.ProductDate,
  2447. ExpiryDate: warehouse.ExpiryDate,
  2448. PatientId: advice.PatientId,
  2449. Count: deliver_number,
  2450. WarehouseInfoId: warehouse.ID,
  2451. AdviceId: advice.ID,
  2452. StorehouseId: storeConfig.DrugStorehouseOut,
  2453. IsCheck: 1,
  2454. }
  2455. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2456. if errOne != nil {
  2457. return errOne
  2458. }
  2459. details := &models.DrugAutomaticReduceDetail{
  2460. WarehouseOutId: warehouseout.ID,
  2461. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  2462. PatientId: advice.PatientId,
  2463. Ctime: time.Now().Unix(),
  2464. Mtime: time.Now().Unix(),
  2465. Status: 1,
  2466. RecordTime: advice.RecordDate,
  2467. OrgId: orgID,
  2468. DrugId: advice.DrugId,
  2469. Count: deliver_number,
  2470. CountUnit: drup.MinUnit,
  2471. WarehouseInfoId: warehouse.ID,
  2472. StorehouseId: storeConfig.DrugStorehouseOut,
  2473. }
  2474. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2475. if errTwo != nil {
  2476. return errTwo
  2477. }
  2478. // 出库完成后,要将该批次库存清零
  2479. warehouse.StockMaxNumber = 0
  2480. warehouse.StockMinNumber = 0
  2481. warehouse.Mtime = time.Now().Unix()
  2482. info := models.XtDrugWarehouseInfo{
  2483. ID: warehouse.ID,
  2484. WarehousingId: warehouse.WarehousingId,
  2485. DrugId: warehouse.DrugId,
  2486. Number: warehouse.Number,
  2487. ProductDate: warehouse.ProductDate,
  2488. ExpiryDate: warehouse.ExpiryDate,
  2489. WarehousingCount: warehouse.WarehousingCount,
  2490. Price: warehouse.Price,
  2491. TotalPrice: warehouse.TotalPrice,
  2492. Dealer: warehouse.Dealer,
  2493. Manufacturer: warehouse.Manufacturer,
  2494. Remark: warehouse.Remark,
  2495. Ctime: warehouse.Ctime,
  2496. Mtime: warehouse.Mtime,
  2497. Status: 1,
  2498. OrgId: warehouse.OrgId,
  2499. IsReturn: warehouse.IsReturn,
  2500. WarehousingOrder: warehouse.WarehousingOrder,
  2501. Type: warehouse.Type,
  2502. RetailPrice: warehouse.RetailPrice,
  2503. RetailTotalPrice: warehouse.RetailPrice,
  2504. StockMaxNumber: 0,
  2505. StockMinNumber: 0,
  2506. BatchNumber: warehouse.BatchNumber,
  2507. MaxUnit: warehouse.MaxUnit,
  2508. WarehousingInfoId: warehouse.WarehousingInfoId,
  2509. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2510. StorehouseId: storeConfig.DrugStorehouseOut,
  2511. IsCheck: 1,
  2512. }
  2513. //扣减库存
  2514. errThree := UpDateDrugWarehouseInfoByStock(&info)
  2515. if errThree != nil {
  2516. return errThree
  2517. }
  2518. drugflow := models.DrugFlow{
  2519. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2520. WarehouseOutId: warehouseout.ID,
  2521. DrugId: advice.DrugId,
  2522. Number: warehouse.Number,
  2523. ProductDate: warehouse.ProductDate,
  2524. ExpireDate: warehouse.ExpiryDate,
  2525. Count: deliver_number,
  2526. Price: warehouse.RetailPrice,
  2527. Status: 1,
  2528. Ctime: time.Now().Unix(),
  2529. UserOrgId: orgID,
  2530. Manufacturer: warehouse.Manufacturer,
  2531. Dealer: warehouse.Dealer,
  2532. BatchNumber: warehouse.BatchNumber,
  2533. MaxUnit: drup.MinUnit,
  2534. ConsumableType: 3,
  2535. IsEdit: 1,
  2536. Creator: advice.ExecutionStaff,
  2537. IsSys: 1,
  2538. PatientId: advice.PatientId,
  2539. WarehousingDetailId: warehouse.ID,
  2540. AdviceId: advice.ID,
  2541. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2542. StorehouseId: storeConfig.DrugStorehouseOut,
  2543. }
  2544. CreateDrugFlowOne(drugflow)
  2545. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2546. prescribingNumber_two_temp := deliver_number - stock_number
  2547. overPlusNumber := float64(prescribingNumber_two_temp)
  2548. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  2549. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  2550. NewDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  2551. }
  2552. return
  2553. }
  2554. // 药品出库 递归方式
  2555. func NewHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  2556. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  2557. var deliver_number int64 = 0
  2558. var stock_number int64 = 0
  2559. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  2560. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  2561. deliver_number = count
  2562. var min_price float64
  2563. if advice.PrescribingNumberUnit == drup.MaxUnit {
  2564. min_price = drup.RetailPrice
  2565. }
  2566. if advice.PrescribingNumberUnit == drup.MinUnit {
  2567. min_price = drup.MinPrice
  2568. }
  2569. if advice.PrescribingNumberUnit != drup.MaxUnit && advice.PrescribingNumberUnit != drup.MinUnit {
  2570. min_price = drup.RetailPrice
  2571. }
  2572. fmt.Println(min_price)
  2573. // 根据先进先出原则,查询最先入库的批次,进行出库
  2574. // 如果没有对应的库存,则报错
  2575. //开启事物
  2576. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  2577. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2578. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  2579. var stockMax int64
  2580. var stockMin int64
  2581. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  2582. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  2583. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  2584. UpdateMinNumber(lastWarehouse.ID, stockMin)
  2585. }
  2586. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2587. //查找药品信息
  2588. if err != nil {
  2589. return err
  2590. }
  2591. // 将该批次的剩余库存数量转换为拆零数量
  2592. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  2593. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  2594. if stock_number >= deliver_number {
  2595. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2596. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2597. WarehouseOutId: warehouseout.ID,
  2598. Status: 1,
  2599. Ctime: time.Now().Unix(),
  2600. Remark: warehouse.Remark,
  2601. OrgId: orgID,
  2602. Type: 1,
  2603. Manufacturer: warehouse.Manufacturer,
  2604. Dealer: warehouse.Dealer,
  2605. IsSys: 1,
  2606. SysRecordTime: advice.RecordDate,
  2607. DrugId: advice.DrugId,
  2608. Number: warehouse.Number,
  2609. BatchNumber: warehouse.BatchNumber,
  2610. Price: warehouse.RetailPrice,
  2611. CountUnit: drup.MinUnit,
  2612. RetailPrice: warehouse.RetailPrice,
  2613. ProductDate: warehouse.ProductDate,
  2614. ExpiryDate: warehouse.ExpiryDate,
  2615. PatientId: advice.PatientId,
  2616. WarehouseInfoId: warehouse.ID,
  2617. Count: deliver_number,
  2618. AdviceId: advice.ID,
  2619. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2620. StorehouseId: storeConfig.DrugStorehouseOut,
  2621. IsCheck: 1,
  2622. }
  2623. drugflow := models.DrugFlow{
  2624. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2625. WarehouseOutId: warehouseout.ID,
  2626. DrugId: advice.DrugId,
  2627. Number: warehouse.Number,
  2628. ProductDate: warehouse.ProductDate,
  2629. ExpireDate: warehouse.ExpiryDate,
  2630. Count: deliver_number,
  2631. Price: warehouse.RetailPrice,
  2632. Status: 1,
  2633. Ctime: time.Now().Unix(),
  2634. UserOrgId: orgID,
  2635. Manufacturer: warehouse.Manufacturer,
  2636. Dealer: warehouse.Dealer,
  2637. BatchNumber: warehouse.BatchNumber,
  2638. MaxUnit: drup.MinUnit,
  2639. ConsumableType: 3,
  2640. IsEdit: 1,
  2641. Creator: advice.ExecutionStaff,
  2642. IsSys: 1,
  2643. PatientId: advice.PatientId,
  2644. SystemTime: advice.RecordDate,
  2645. WarehousingDetailId: warehouse.ID,
  2646. AdviceId: advice.ID,
  2647. StorehouseId: storeConfig.DrugStorehouseOut,
  2648. }
  2649. CreateDrugFlowOne(drugflow)
  2650. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2651. if errOne != nil {
  2652. return errOne
  2653. }
  2654. details := &models.DrugAutomaticReduceDetail{
  2655. WarehouseOutId: warehouseout.ID,
  2656. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2657. PatientId: advice.PatientId,
  2658. Ctime: time.Now().Unix(),
  2659. Mtime: time.Now().Unix(),
  2660. Status: 1,
  2661. RecordTime: advice.RecordDate,
  2662. OrgId: orgID,
  2663. DrugId: advice.DrugId,
  2664. Count: deliver_number,
  2665. CountUnit: drup.MinUnit,
  2666. WarehouseInfoId: warehouse.ID,
  2667. AdviceId: advice.ID,
  2668. StorehouseId: storeConfig.DrugStorehouseOut,
  2669. }
  2670. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2671. if errTwo != nil {
  2672. return errTwo
  2673. }
  2674. // 出库完成后,要减去对应批次的库存数量
  2675. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  2676. var maxNumber int64 = 0
  2677. var minNumber int64 = 0
  2678. //var stock_max_number int64 = 0
  2679. //stock_max_number = warehouse.StockMaxNumber
  2680. maxNumber = deliver_number / drup.MinNumber
  2681. minNumber = deliver_number % drup.MinNumber
  2682. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  2683. minNumber = maxNumber
  2684. }
  2685. if drup.MaxUnit != drup.MinUnit {
  2686. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  2687. return errors.New("库存数量不足")
  2688. }
  2689. }
  2690. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  2691. if warehouse.StockMaxNumber < 0 {
  2692. warehouse.StockMaxNumber = 0
  2693. }
  2694. if warehouse.StockMinNumber < 0 {
  2695. warehouse.StockMinNumber = 0
  2696. }
  2697. warehouse.Mtime = time.Now().Unix()
  2698. if warehouse.StockMinNumber < minNumber {
  2699. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  2700. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  2701. } else {
  2702. if minNumber > 0 {
  2703. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2704. }
  2705. if minNumber == 0 && maxNumber != 1 {
  2706. if warehouse.StockMinNumber > 0 {
  2707. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2708. }
  2709. }
  2710. }
  2711. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  2712. if (warehouse.StockMinNumber - deliver_number) > 0 {
  2713. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2714. //if warehouse.StockMinNumber == 0 {
  2715. // warehouse.StockMaxNumber = stock_max_number
  2716. //}
  2717. }
  2718. }
  2719. if drup.MaxUnit != drup.MinUnit {
  2720. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  2721. return errors.New("库存数量不足")
  2722. }
  2723. }
  2724. if warehouse.StockMinNumber <= 0 {
  2725. warehouse.StockMinNumber = 0
  2726. }
  2727. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  2728. if errThree != nil {
  2729. return errThree
  2730. }
  2731. return nil
  2732. } else {
  2733. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2734. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2735. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2736. WarehouseOutId: warehouseout.ID,
  2737. Status: 1,
  2738. Ctime: time.Now().Unix(),
  2739. Remark: warehouse.Remark,
  2740. OrgId: orgID,
  2741. Type: 1,
  2742. Manufacturer: warehouse.Manufacturer,
  2743. Dealer: warehouse.Dealer,
  2744. IsSys: 1,
  2745. SysRecordTime: advice.RecordDate,
  2746. DrugId: advice.DrugId,
  2747. Number: warehouse.Number,
  2748. BatchNumber: warehouse.BatchNumber,
  2749. Price: warehouse.RetailPrice,
  2750. RetailPrice: warehouse.RetailPrice,
  2751. ProductDate: warehouse.ProductDate,
  2752. ExpiryDate: warehouse.ExpiryDate,
  2753. PatientId: advice.PatientId,
  2754. WarehouseInfoId: warehouse.ID,
  2755. Count: stock_number,
  2756. CountUnit: drup.MinUnit,
  2757. AdviceId: advice.ID,
  2758. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2759. StorehouseId: storeConfig.DrugStorehouseOut,
  2760. IsCheck: 1,
  2761. }
  2762. drugflow := models.DrugFlow{
  2763. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2764. WarehouseOutId: warehouseout.ID,
  2765. DrugId: advice.DrugId,
  2766. Number: warehouse.Number,
  2767. ProductDate: warehouse.ProductDate,
  2768. ExpireDate: warehouse.ExpiryDate,
  2769. Count: stock_number,
  2770. Price: warehouse.RetailPrice,
  2771. Status: 1,
  2772. Ctime: time.Now().Unix(),
  2773. UserOrgId: orgID,
  2774. Manufacturer: warehouse.Manufacturer,
  2775. Dealer: warehouse.Dealer,
  2776. BatchNumber: warehouse.BatchNumber,
  2777. MaxUnit: drup.MinUnit,
  2778. ConsumableType: 3,
  2779. IsEdit: 1,
  2780. Creator: advice.ExecutionStaff,
  2781. IsSys: 1,
  2782. PatientId: advice.PatientId,
  2783. SystemTime: advice.RecordDate,
  2784. WarehousingDetailId: warehouse.ID,
  2785. AdviceId: advice.ID,
  2786. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2787. StorehouseId: storeConfig.DrugStorehouseOut,
  2788. }
  2789. CreateDrugFlowOne(drugflow)
  2790. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2791. if errOne != nil {
  2792. return errOne
  2793. }
  2794. details := &models.DrugAutomaticReduceDetail{
  2795. WarehouseOutId: warehouseout.ID,
  2796. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2797. PatientId: advice.PatientId,
  2798. Ctime: time.Now().Unix(),
  2799. Mtime: time.Now().Unix(),
  2800. Status: 1,
  2801. RecordTime: advice.RecordDate,
  2802. OrgId: orgID,
  2803. DrugId: advice.DrugId,
  2804. Count: stock_number,
  2805. CountUnit: drup.MinUnit,
  2806. WarehouseInfoId: warehouse.ID,
  2807. AdviceId: advice.ID,
  2808. StorehouseId: storeConfig.DrugStorehouseOut,
  2809. }
  2810. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2811. if errTwo != nil {
  2812. return errTwo
  2813. }
  2814. info := models.XtDrugWarehouseInfo{
  2815. ID: warehouse.ID,
  2816. WarehousingId: warehouse.WarehousingId,
  2817. DrugId: warehouse.DrugId,
  2818. Number: warehouse.Number,
  2819. ProductDate: warehouse.ProductDate,
  2820. ExpiryDate: warehouse.ExpiryDate,
  2821. WarehousingCount: warehouse.WarehousingCount,
  2822. Price: warehouse.RetailPrice,
  2823. TotalPrice: warehouse.TotalPrice,
  2824. Dealer: warehouse.Dealer,
  2825. Manufacturer: warehouse.Manufacturer,
  2826. Remark: warehouse.Remark,
  2827. Ctime: warehouse.Ctime,
  2828. Mtime: warehouse.Mtime,
  2829. Status: 1,
  2830. OrgId: warehouse.OrgId,
  2831. IsReturn: warehouse.IsReturn,
  2832. WarehousingOrder: warehouse.WarehousingOrder,
  2833. Type: warehouse.Type,
  2834. RetailPrice: warehouse.RetailPrice,
  2835. RetailTotalPrice: warehouse.RetailPrice,
  2836. StockMaxNumber: 0,
  2837. StockMinNumber: 0,
  2838. BatchNumber: warehouse.BatchNumber,
  2839. MaxUnit: warehouse.MaxUnit,
  2840. WarehousingInfoId: warehouse.WarehousingInfoId,
  2841. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2842. StorehouseId: storeConfig.DrugStorehouseOut,
  2843. IsCheck: 1,
  2844. }
  2845. errThree := UpDateDrugWarehouseInfoByStock(&info)
  2846. if errThree != nil {
  2847. return errThree
  2848. }
  2849. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2850. prescribingNumber_two_temp := deliver_number - stock_number
  2851. overPlusNumber := float64(prescribingNumber_two_temp)
  2852. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  2853. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  2854. NewHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  2855. }
  2856. return
  2857. }
  2858. // 药品出库 递归方式
  2859. func HisAuToReduce(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  2860. //查询最后一次出库数据
  2861. drugInfo, _ := GetDrugWarehouseOutInfoIsExit(advice.DrugId, advice.PatientId, advice.RecordDate)
  2862. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  2863. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  2864. var count_number int64
  2865. var device_number int64
  2866. if advice.PrescribingNumberUnit == drup.MaxUnit {
  2867. device_number = count * drup.MinNumber
  2868. }
  2869. if advice.PrescribingNumberUnit == drup.MinUnit {
  2870. device_number = count
  2871. }
  2872. if drugInfo.CountUnit == drup.MaxUnit {
  2873. count_number = drugInfo.Count * drup.MinNumber
  2874. }
  2875. if drugInfo.CountUnit == drup.MinUnit {
  2876. count_number = drugInfo.Count
  2877. }
  2878. // 比较数据大小
  2879. //如果当期医嘱数量小于最后一次出库则退库
  2880. if device_number < count_number {
  2881. var cha_number int64
  2882. var maxNumber int64
  2883. var minNumber int64
  2884. cha_number = count_number - device_number
  2885. maxNumber = cha_number / drup.MinNumber
  2886. minNumber = cha_number % drup.MinNumber
  2887. //更改库存
  2888. ModifyDrugWarehouse(drugInfo.WarehouseInfoId, maxNumber, minNumber)
  2889. //修改出库表
  2890. info := models.DrugWarehouseOutInfo{
  2891. Count: device_number,
  2892. CountUnit: advice.PrescribingNumberUnit,
  2893. }
  2894. ModifyDrugWarehouseOutInfo(info, advice.DrugId, advice.PatientId, advice.RecordDate)
  2895. //修改自动出库表
  2896. detail := models.DrugAutomaticReduceDetail{
  2897. Count: device_number,
  2898. CountUnit: advice.PrescribingNumberUnit,
  2899. }
  2900. ModifyAutoMaticReduce(detail, advice.PatientId, advice.RecordDate, advice.DrugId)
  2901. //修改流水
  2902. flow := models.DrugFlow{
  2903. Count: device_number,
  2904. MaxUnit: drup.MinUnit,
  2905. }
  2906. ModifyDrugFlow(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId)
  2907. }
  2908. //如果当期医嘱数量大于最后一次出库则需要出库
  2909. if device_number > count_number {
  2910. var cha_number int64
  2911. cha_number = device_number - count_number
  2912. // 根据先进先出原则,查询最先入库的批次,进行出库
  2913. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  2914. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2915. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  2916. var stockMax int64
  2917. var stockMin int64
  2918. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  2919. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  2920. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  2921. UpdateMinNumber(lastWarehouse.ID, stockMin)
  2922. }
  2923. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2924. //查找药品信息
  2925. //baseInfo, _ := FindBaseDrugLibRecord(advice.UserOrgId, advice.DrugId)
  2926. if err != nil {
  2927. return err
  2928. }
  2929. // 将该批次的剩余库存数量转换为拆零数量
  2930. var stock_number int64
  2931. var maxNumber int64
  2932. var minNumber int64
  2933. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  2934. //如果库存数量大于出库数据,正常出库
  2935. if stock_number >= cha_number {
  2936. //扣减库存
  2937. maxNumber = cha_number / drup.MinNumber
  2938. minNumber = cha_number % drup.MinNumber
  2939. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  2940. minNumber = maxNumber
  2941. }
  2942. if drup.MaxUnit != drup.MinUnit {
  2943. if warehouse.StockMaxNumber < maxNumber {
  2944. return errors.New("库存数量不足")
  2945. }
  2946. }
  2947. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  2948. if warehouse.StockMaxNumber < 0 {
  2949. warehouse.StockMaxNumber = 0
  2950. }
  2951. warehouse.Mtime = time.Now().Unix()
  2952. if warehouse.StockMinNumber < minNumber {
  2953. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  2954. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  2955. } else {
  2956. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2957. }
  2958. if drup.MaxUnit != drup.MinUnit {
  2959. if warehouse.StockMaxNumber < 0 {
  2960. return errors.New("库存数量不足")
  2961. }
  2962. }
  2963. //扣减库存232332332332
  2964. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  2965. if errThree != nil {
  2966. return errThree
  2967. }
  2968. //修改出库表
  2969. info := models.DrugWarehouseOutInfo{
  2970. Count: device_number,
  2971. CountUnit: advice.PrescribingNumberUnit,
  2972. }
  2973. ModifyDrugWarehouseOutInfoOne(info, advice.DrugId, advice.PatientId, advice.RecordDate, warehouse.ID)
  2974. //修改自动出库表
  2975. detail := models.DrugAutomaticReduceDetail{
  2976. Count: device_number,
  2977. CountUnit: advice.PrescribingNumberUnit,
  2978. }
  2979. ModifyAutoMaticReduceOne(detail, advice.PatientId, advice.RecordDate, advice.DrugId, warehouse.ID)
  2980. //修改流水
  2981. flow := models.DrugFlow{
  2982. Count: device_number,
  2983. MaxUnit: advice.PrescribingNumberUnit,
  2984. }
  2985. ModifyDrugFlowOne(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId, warehouse.ID)
  2986. }
  2987. //如果库存数量小于出库数据,则需要出库到下一个批次
  2988. if stock_number < cha_number {
  2989. //修改出库表
  2990. info := models.DrugWarehouseOutInfo{
  2991. Count: cha_number,
  2992. CountUnit: advice.PrescribingNumberUnit,
  2993. }
  2994. ModifyDrugWarehouseOutInfoOne(info, advice.DrugId, advice.PatientId, advice.RecordDate, warehouse.ID)
  2995. //修改自动出库表
  2996. detail := models.DrugAutomaticReduceDetail{
  2997. Count: cha_number,
  2998. CountUnit: advice.PrescribingNumberUnit,
  2999. }
  3000. ModifyAutoMaticReduceOne(detail, advice.PatientId, advice.RecordDate, advice.DrugId, warehouse.ID)
  3001. //修改流水
  3002. flow := models.DrugFlow{
  3003. Count: cha_number,
  3004. MaxUnit: advice.PrescribingNumberUnit,
  3005. }
  3006. ModifyDrugFlowOne(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId, warehouse.ID)
  3007. druginfo := &models.XtDrugWarehouseInfo{
  3008. ID: warehouse.ID,
  3009. WarehousingId: warehouse.WarehousingId,
  3010. DrugId: warehouse.DrugId,
  3011. Number: warehouse.Number,
  3012. ProductDate: warehouse.ProductDate,
  3013. ExpiryDate: warehouse.ExpiryDate,
  3014. WarehousingCount: warehouse.WarehousingCount,
  3015. Price: warehouse.Price,
  3016. TotalPrice: warehouse.TotalPrice,
  3017. Dealer: warehouse.Dealer,
  3018. Manufacturer: warehouse.Manufacturer,
  3019. Remark: warehouse.Remark,
  3020. Ctime: warehouse.Ctime,
  3021. Mtime: warehouse.Mtime,
  3022. Status: 1,
  3023. OrgId: warehouse.OrgId,
  3024. IsReturn: warehouse.IsReturn,
  3025. WarehousingOrder: warehouse.WarehousingOrder,
  3026. Type: warehouse.Type,
  3027. RetailPrice: warehouse.RetailPrice,
  3028. RetailTotalPrice: warehouse.RetailPrice,
  3029. StockMaxNumber: 0,
  3030. StockMinNumber: 0,
  3031. BatchNumber: warehouse.BatchNumber,
  3032. MaxUnit: warehouse.MaxUnit,
  3033. WarehousingInfoId: warehouse.WarehousingInfoId,
  3034. StorehouseId: storeConfig.DrugStorehouseOut,
  3035. IsCheck: 1,
  3036. }
  3037. errThree := UpDateDrugWarehouseInfoByStock(druginfo)
  3038. if errThree != nil {
  3039. return errThree
  3040. }
  3041. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3042. prescribingNumber_two_temp := cha_number - stock_number
  3043. overPlusNumber := float64(prescribingNumber_two_temp)
  3044. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  3045. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  3046. NewHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  3047. }
  3048. }
  3049. return
  3050. }
  3051. // 采购管理耗材出库逻辑
  3052. 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) {
  3053. var deliver_number int64 = 0
  3054. var stock_number int64 = 0
  3055. var maxNumber int64 = 0
  3056. deliver_number = goods.Count
  3057. // 根据先进先出原则,查询最先入库的批次,进行出库
  3058. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  3059. // 如果没有对应的库存,则报错
  3060. warehouse, err := FindFirstWarehousingInfoByStockNight(goods.GoodId, goods.GoodTypeId, warehouse_out_id, goods.IsSource, houseConfig.StorehouseOutInfo)
  3061. if err != nil {
  3062. return err
  3063. }
  3064. stock_number = warehouse.StockCount
  3065. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3066. if stock_number >= deliver_number {
  3067. warehouseOutInfo := &models.WarehouseOutInfo{
  3068. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3069. WarehouseOutId: goods.WarehouseOutId,
  3070. WarehouseInfotId: warehouse.ID,
  3071. Status: 1,
  3072. Ctime: time.Now().Unix(),
  3073. Remark: goods.Remark,
  3074. OrgId: orgID,
  3075. Type: 1,
  3076. Manufacturer: goods.Manufacturer,
  3077. Dealer: goods.Dealer,
  3078. IsSys: 0,
  3079. SysRecordTime: record_time,
  3080. GoodTypeId: goods.GoodTypeId,
  3081. GoodId: goods.GoodId,
  3082. ExpiryDate: goods.ExpiryDate,
  3083. ProductDate: goods.ProductDate,
  3084. Number: warehouse.Number,
  3085. Price: goods.Price,
  3086. LicenseNumber: goods.LicenseNumber,
  3087. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  3088. StorehouseId: warehouse.StorehouseId,
  3089. IsCheck: 1,
  3090. }
  3091. warehouseOutInfo.Count = goods.Count
  3092. //添加出库单详情
  3093. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  3094. if errOne != nil {
  3095. return errOne
  3096. }
  3097. stockFlow := models.VmStockFlow{
  3098. WarehousingId: warehouse.ID,
  3099. GoodId: goods.GoodId,
  3100. Number: warehouse.Number,
  3101. LicenseNumber: goods.LicenseNumber,
  3102. Count: goods.Count,
  3103. UserOrgId: goods.OrgId,
  3104. PatientId: goods.PatientId,
  3105. SystemTime: record_time,
  3106. ConsumableType: 2,
  3107. IsSys: goods.IsSys,
  3108. WarehousingOrder: "",
  3109. WarehouseOutId: goods.WarehouseOutId,
  3110. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3111. IsEdit: 1,
  3112. CancelStockId: 0,
  3113. CancelOrderNumber: "",
  3114. Manufacturer: goods.Manufacturer,
  3115. Dealer: goods.Dealer,
  3116. Creator: creator,
  3117. UpdateCreator: 0,
  3118. Status: 1,
  3119. Ctime: time.Now().Unix(),
  3120. Mtime: 0,
  3121. Price: warehouse.PackingPrice,
  3122. WarehousingDetailId: 0,
  3123. WarehouseOutDetailId: warehouseOutInfo.ID,
  3124. CancelOutDetailId: 0,
  3125. ProductDate: goods.ProductDate,
  3126. ExpireDate: goods.ExpiryDate,
  3127. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  3128. SupplyWarehouseId: warehouseOut.SupplyWarehouseId,
  3129. StorehouseId: warehouse.StorehouseId,
  3130. }
  3131. //创建出库流水
  3132. CreateStockFlowOne(stockFlow)
  3133. maxNumber = goods.Count
  3134. if warehouse.StockCount < maxNumber {
  3135. return errors.New("库存数量不足")
  3136. }
  3137. warehouse.StockCount = warehouse.StockCount - maxNumber
  3138. warehouse.Mtime = time.Now().Unix()
  3139. //扣减库存
  3140. errThree := UpDateWarehouseInfoByStock(&warehouse)
  3141. //出库数量增加
  3142. AddGoodSumOutCount(houseConfig.StorehouseOutInfo, warehouse.GoodId, warehouse.OrgId, goods.Count)
  3143. ModifyGoodReduceInformationSix(warehouse.GoodId, maxNumber, warehouse.OrgId, warehouse.PackingPrice)
  3144. if errThree != nil {
  3145. return errThree
  3146. }
  3147. } else {
  3148. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  3149. warehouseOutInfo := &models.WarehouseOutInfo{
  3150. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3151. WarehouseOutId: goods.WarehouseOutId,
  3152. WarehouseInfotId: warehouse.ID,
  3153. Status: 1,
  3154. Ctime: time.Now().Unix(),
  3155. Remark: goods.Remark,
  3156. OrgId: orgID,
  3157. Type: 1,
  3158. Manufacturer: goods.Manufacturer,
  3159. Dealer: goods.Dealer,
  3160. IsSys: 0,
  3161. SysRecordTime: record_time,
  3162. GoodTypeId: goods.GoodTypeId,
  3163. GoodId: goods.GoodId,
  3164. ExpiryDate: goods.ExpiryDate,
  3165. ProductDate: goods.ProductDate,
  3166. Number: warehouse.Number,
  3167. Price: warehouse.PackingPrice,
  3168. LicenseNumber: goods.LicenseNumber,
  3169. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  3170. StorehouseId: warehouse.StorehouseId,
  3171. IsCheck: 1,
  3172. }
  3173. warehouseOutInfo.Count = stock_number
  3174. //查询是否已经生成出库单
  3175. info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  3176. if errcodes == gorm.ErrRecordNotFound {
  3177. //创建出库单
  3178. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  3179. if errOne != nil {
  3180. return errOne
  3181. }
  3182. } else {
  3183. warehouseOutInfo.Count = info.Count + stock_number
  3184. UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  3185. }
  3186. stockFlow := models.VmStockFlow{
  3187. WarehousingId: warehouse.ID,
  3188. GoodId: goods.GoodId,
  3189. Number: warehouse.Number,
  3190. LicenseNumber: goods.LicenseNumber,
  3191. Count: stock_number,
  3192. UserOrgId: goods.OrgId,
  3193. PatientId: 0,
  3194. SystemTime: record_time,
  3195. ConsumableType: 2,
  3196. IsSys: 0,
  3197. WarehousingOrder: "",
  3198. WarehouseOutId: goods.WarehouseOutId,
  3199. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3200. IsEdit: 1,
  3201. CancelStockId: 0,
  3202. CancelOrderNumber: "",
  3203. Manufacturer: goods.Manufacturer,
  3204. Dealer: goods.Dealer,
  3205. Creator: creator,
  3206. UpdateCreator: 0,
  3207. Status: 1,
  3208. Ctime: time.Now().Unix(),
  3209. Mtime: 0,
  3210. Price: warehouse.PackingPrice,
  3211. WarehousingDetailId: 0,
  3212. WarehouseOutDetailId: warehouseOutInfo.ID,
  3213. CancelOutDetailId: 0,
  3214. ProductDate: goods.ProductDate,
  3215. ExpireDate: goods.ExpiryDate,
  3216. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  3217. SupplyWarehouseId: warehouseOut.SupplyWarehouseId,
  3218. StorehouseId: warehouse.StorehouseId,
  3219. }
  3220. //创建出库明细
  3221. CreateStockFlowOne(stockFlow)
  3222. // 出库完成后,要将该批次库存清零
  3223. ModifyGoodReduceInformationSix(warehouse.GoodId, warehouse.StockCount, warehouse.OrgId, warehouse.PackingPrice)
  3224. warehouse.StockCount = 0
  3225. warehouse.Mtime = time.Now().Unix()
  3226. errThree := UpDateWarehouseInfoByStock(&warehouse)
  3227. if errThree != nil {
  3228. return errThree
  3229. }
  3230. //出库数量增加
  3231. AddGoodSumOutCount(houseConfig.StorehouseOutInfo, warehouse.GoodId, warehouse.OrgId, warehouse.StockCount)
  3232. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3233. goods.Count = deliver_number - stock_number
  3234. ConsumablesDeliveryNight(orgID, record_time, goods, warehouseOut, count, creator, warehouse_out_id, cancel_out_id)
  3235. }
  3236. return nil
  3237. }
  3238. func FindFirstWarehousingInfoByStockNight(good_id int64, good_type_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  3239. if is_source == 1 {
  3240. 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
  3241. }
  3242. if is_source == 2 || is_source == 0 {
  3243. 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
  3244. }
  3245. return info, err
  3246. }
  3247. // 药品出库 递归方式
  3248. 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) {
  3249. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  3250. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  3251. var deliver_number int64 = 0
  3252. var stock_number int64 = 0
  3253. var retail_price float64
  3254. if advice.CountUnit == drup.MaxUnit {
  3255. deliver_number = prescribingNumber * drup.MinNumber
  3256. formatInt := strconv.FormatInt(prescribingNumber, 10)
  3257. floatcount, _ := strconv.ParseFloat(formatInt, 64)
  3258. retail_price = floatcount * advice.Price
  3259. } else {
  3260. deliver_number = prescribingNumber
  3261. formatInt := strconv.FormatInt(prescribingNumber, 10)
  3262. floatcount, _ := strconv.ParseFloat(formatInt, 64)
  3263. retail_price = floatcount * advice.Price
  3264. }
  3265. // 根据先进先出原则,查询最先入库的批次,进行出库
  3266. // 如果没有对应的库存,则报错
  3267. //开启事物
  3268. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, houseConfig.DrugStorehouseOut)
  3269. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  3270. var stockMax int64
  3271. var stockMin int64
  3272. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  3273. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  3274. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  3275. UpdateMinNumber(lastWarehouse.ID, stockMin)
  3276. }
  3277. warehouse, err := FindLastDrugWarehousingInfoByIDSix(advice.DrugId, warehouse_out_id, advice.IsSource, houseConfig.DrugStorehouseOut)
  3278. if err != nil {
  3279. return err
  3280. }
  3281. // 将该批次的剩余库存数量转换为拆零数量
  3282. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  3283. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3284. if stock_number >= deliver_number {
  3285. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  3286. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3287. WarehouseOutId: advice.WarehouseOutId,
  3288. Status: 1,
  3289. Ctime: time.Now().Unix(),
  3290. Remark: advice.Remark,
  3291. OrgId: orgID,
  3292. Type: 1,
  3293. Manufacturer: advice.Manufacturer,
  3294. Dealer: warehouse.Dealer,
  3295. IsSys: 0,
  3296. SysRecordTime: advice.SysRecordTime,
  3297. DrugId: advice.DrugId,
  3298. ExpiryDate: advice.ExpiryDate,
  3299. ProductDate: advice.ProductDate,
  3300. Number: advice.Number,
  3301. BatchNumber: warehouse.BatchNumber,
  3302. Count: deliver_number,
  3303. RetailPrice: advice.RetailPrice,
  3304. Price: advice.Price,
  3305. WarehouseInfoId: warehouse.ID,
  3306. CountUnit: drup.MinUnit,
  3307. AdviceId: advice.ID,
  3308. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  3309. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  3310. StorehouseId: warehouse.StorehouseId,
  3311. IsCheck: 1,
  3312. }
  3313. warehouseOutInfo.Count = prescribingNumber
  3314. warehouseOutInfo.CountUnit = advice.CountUnit
  3315. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  3316. if errOne != nil {
  3317. return errOne
  3318. }
  3319. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  3320. drugflow := models.DrugFlow{
  3321. WarehouseOutId: advice.WarehouseOutId,
  3322. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3323. DrugId: advice.DrugId,
  3324. Number: warehouse.Number,
  3325. ProductDate: advice.ProductDate,
  3326. ExpireDate: advice.ExpiryDate,
  3327. Count: deliver_number,
  3328. Price: warehouse.RetailPrice,
  3329. Status: 1,
  3330. Ctime: time.Now().Unix(),
  3331. UserOrgId: advice.OrgId,
  3332. Manufacturer: advice.Manufacturer,
  3333. Dealer: advice.Dealer,
  3334. BatchNumber: warehouse.BatchNumber,
  3335. MaxUnit: drup.MinUnit,
  3336. ConsumableType: 2,
  3337. IsEdit: 1,
  3338. Creator: 0,
  3339. IsSys: 0,
  3340. WarehouseOutDetailId: drugWareInfo.ID,
  3341. AdviceId: advice.ID,
  3342. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  3343. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  3344. SystemTime: advice.SysRecordTime,
  3345. StorehouseId: warehouse.StorehouseId,
  3346. WarehousingDetailId: warehouse.ID,
  3347. WarehousingId: warehouse.ID,
  3348. RetailPrice: retail_price,
  3349. LastPrice: warehouse.Price,
  3350. }
  3351. CreateDrugFlowOne(drugflow)
  3352. // 出库完成后,要减去对应批次的库存数量
  3353. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  3354. var maxNumber int64 = 0
  3355. var minNumber int64 = 0
  3356. if advice.CountUnit == drup.MinUnit {
  3357. maxNumber = prescribingNumber / drup.MinNumber
  3358. minNumber = prescribingNumber % drup.MinNumber
  3359. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  3360. minNumber = maxNumber
  3361. }
  3362. } else {
  3363. maxNumber = prescribingNumber
  3364. }
  3365. if warehouse.StockMaxNumber < maxNumber {
  3366. return errors.New("库存数量不足")
  3367. }
  3368. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  3369. if warehouse.StockMaxNumber < 0 {
  3370. warehouse.StockMaxNumber = 0
  3371. }
  3372. warehouse.Mtime = time.Now().Unix()
  3373. if warehouse.StockMinNumber < minNumber {
  3374. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  3375. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  3376. } else {
  3377. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  3378. }
  3379. if warehouse.StockMaxNumber < 0 {
  3380. return errors.New("库存数量不足")
  3381. }
  3382. if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
  3383. warehouse.StockMinNumber = 0
  3384. }
  3385. if warehouse.StockMaxNumber > 0 {
  3386. var sum_count int64
  3387. sum_count = maxNumber * drup.MinNumber
  3388. ModifyDrugReduceInformationSix(warehouse.DrugId, sum_count, warehouse.OrgId, warehouse.RetailPrice)
  3389. //出库数量增加
  3390. AddDrugSumOutCount(houseConfig.DrugStorehouseOut, sum_count, warehouse.OrgId, warehouse.DrugId)
  3391. }
  3392. if warehouse.StockMinNumber > 0 {
  3393. ModifyDrugReduceInformationSix(warehouse.DrugId, minNumber, warehouse.OrgId, warehouse.RetailPrice)
  3394. //出库数量增加
  3395. AddDrugSumOutCount(houseConfig.DrugStorehouseOut, minNumber, warehouse.OrgId, warehouse.DrugId)
  3396. }
  3397. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  3398. if errThree != nil {
  3399. return errThree
  3400. }
  3401. return nil
  3402. } else {
  3403. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  3404. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  3405. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3406. WarehouseOutId: advice.WarehouseOutId,
  3407. Status: 1,
  3408. Ctime: time.Now().Unix(),
  3409. Remark: advice.Remark,
  3410. OrgId: orgID,
  3411. Type: 1,
  3412. Manufacturer: advice.Manufacturer,
  3413. Dealer: warehouse.Dealer,
  3414. IsSys: 0,
  3415. SysRecordTime: advice.SysRecordTime,
  3416. DrugId: advice.DrugId,
  3417. ExpiryDate: warehouse.ExpiryDate,
  3418. ProductDate: warehouse.ProductDate,
  3419. Number: warehouse.Number,
  3420. BatchNumber: warehouse.BatchNumber,
  3421. Count: stock_number,
  3422. RetailPrice: advice.RetailPrice,
  3423. Price: advice.Price,
  3424. WarehouseInfoId: warehouse.ID,
  3425. CountUnit: drup.MinUnit,
  3426. AdviceId: advice.ID,
  3427. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  3428. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  3429. StorehouseId: warehouse.StorehouseId,
  3430. IsCheck: 1,
  3431. }
  3432. warehouseOutInfo.Count = stock_number
  3433. //warehouseOutInfo.CountUnit = advice.CountUnit
  3434. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  3435. if errOne != nil {
  3436. return errOne
  3437. }
  3438. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  3439. drugflow := models.DrugFlow{
  3440. WarehouseOutId: advice.WarehouseOutId,
  3441. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3442. DrugId: advice.DrugId,
  3443. Number: warehouse.Number,
  3444. ProductDate: advice.ProductDate,
  3445. ExpireDate: advice.ExpiryDate,
  3446. Count: stock_number,
  3447. Price: warehouse.RetailPrice,
  3448. Status: 1,
  3449. Ctime: time.Now().Unix(),
  3450. UserOrgId: advice.OrgId,
  3451. Manufacturer: advice.Manufacturer,
  3452. Dealer: advice.Dealer,
  3453. BatchNumber: warehouse.BatchNumber,
  3454. MaxUnit: drup.MinUnit,
  3455. ConsumableType: 2,
  3456. IsEdit: 1,
  3457. Creator: 0,
  3458. IsSys: 0,
  3459. WarehouseOutDetailId: drugWareInfo.ID,
  3460. AdviceId: advice.ID,
  3461. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  3462. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  3463. SystemTime: advice.SysRecordTime,
  3464. StorehouseId: warehouse.StorehouseId,
  3465. WarehousingDetailId: warehouse.ID,
  3466. WarehousingId: warehouse.ID,
  3467. RetailPrice: retail_price,
  3468. LastPrice: warehouse.Price,
  3469. }
  3470. CreateDrugFlowOne(drugflow)
  3471. if warehouse.StockMaxNumber > 0 {
  3472. var sum_count int64
  3473. sum_count = warehouse.StockMaxNumber * drup.MinNumber
  3474. ModifyDrugReduceInformationSix(warehouse.DrugId, sum_count, warehouse.OrgId, warehouse.RetailPrice)
  3475. AddDrugSumOutCount(houseConfig.DrugStorehouseOut, sum_count, warehouse.OrgId, warehouse.DrugId)
  3476. }
  3477. if warehouse.StockMinNumber > 0 {
  3478. ModifyDrugReduceInformationSix(warehouse.DrugId, warehouse.StockMinNumber, warehouse.OrgId, warehouse.RetailPrice)
  3479. AddDrugSumOutCount(houseConfig.DrugStorehouseOut, warehouse.StockMinNumber, warehouse.OrgId, warehouse.DrugId)
  3480. }
  3481. // 出库完成后,要将该批次库存清零
  3482. warehouse.StockMaxNumber = 0
  3483. warehouse.StockMinNumber = 0
  3484. warehouse.Mtime = time.Now().Unix()
  3485. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  3486. if errThree != nil {
  3487. return errThree
  3488. }
  3489. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3490. prescribingNumber_two_temp := deliver_number - stock_number
  3491. advice.CountUnit = drup.MinUnit
  3492. AutoDrugDeliverInfoNight(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, warehouse_out_id, cancel_out_id)
  3493. }
  3494. return
  3495. }
  3496. // 调拨出库逻辑
  3497. func ConsumablesDeliveryTwelve(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64, warehouseInfo *models.WarehousingInfo) (err error) {
  3498. var deliver_number int64 = 0
  3499. var stock_number int64 = 0
  3500. var maxNumber int64 = 0
  3501. deliver_number = goods.Count
  3502. // 根据先进先出原则,查询最先入库的批次,进行出库
  3503. // 如果没有对应的库存,则报错
  3504. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  3505. if err != nil {
  3506. return err
  3507. }
  3508. stock_number = warehouse.StockCount
  3509. maxNumber = goods.Count
  3510. //if warehouse.StockCount < maxNumber {
  3511. // return errors.New("库存数量不足")
  3512. //}
  3513. if warehouse.StockCount-maxNumber >= 0 {
  3514. warehouse.StockCount = warehouse.StockCount - maxNumber
  3515. } else {
  3516. warehouse.StockCount = warehouse.StockCount
  3517. }
  3518. warehouse.Mtime = time.Now().Unix()
  3519. //扣减库存
  3520. errThree := UpDateWarehouseInfoByStock(&warehouse)
  3521. //查询剩余库存
  3522. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  3523. var sum_count int64
  3524. for _, item := range goodList {
  3525. sum_count += item.StockCount
  3526. }
  3527. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3528. if stock_number >= deliver_number {
  3529. warehouseOutInfo := &models.WarehouseOutInfo{
  3530. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  3531. WarehouseOutId: goods.WarehouseOutId,
  3532. WarehouseInfotId: warehouse.ID,
  3533. Status: 1,
  3534. Ctime: time.Now().Unix(),
  3535. Remark: goods.Remark,
  3536. OrgId: orgID,
  3537. Type: 1,
  3538. Manufacturer: warehouse.Manufacturer,
  3539. Dealer: warehouse.Dealer,
  3540. IsSys: 0,
  3541. SysRecordTime: record_time,
  3542. GoodTypeId: warehouse.GoodTypeId,
  3543. GoodId: warehouse.GoodId,
  3544. ExpiryDate: warehouse.ExpiryDate,
  3545. ProductDate: warehouse.ProductDate,
  3546. Number: warehouse.Number,
  3547. Price: warehouse.PackingPrice,
  3548. LicenseNumber: warehouse.LicenseNumber,
  3549. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3550. StorehouseId: goods.StorehouseId,
  3551. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  3552. IsCheck: 1,
  3553. OverCount: sum_count,
  3554. }
  3555. warehouseOutInfo.Count = goods.Count
  3556. //添加出库单详情
  3557. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  3558. if errOne != nil {
  3559. return errOne
  3560. }
  3561. stockFlow := models.VmStockFlow{
  3562. WarehousingId: warehouse.ID,
  3563. GoodId: goods.GoodId,
  3564. Number: warehouse.Number,
  3565. LicenseNumber: goods.LicenseNumber,
  3566. Count: goods.Count,
  3567. UserOrgId: goods.OrgId,
  3568. PatientId: goods.PatientId,
  3569. SystemTime: record_time,
  3570. ConsumableType: 12,
  3571. IsSys: goods.IsSys,
  3572. WarehousingOrder: "",
  3573. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  3574. WarehouseOutId: goods.WarehouseOutId,
  3575. IsEdit: 1,
  3576. CancelStockId: 0,
  3577. CancelOrderNumber: "",
  3578. Manufacturer: warehouse.Manufacturer,
  3579. Dealer: warehouse.Dealer,
  3580. Creator: creator,
  3581. UpdateCreator: 0,
  3582. Status: 1,
  3583. Ctime: time.Now().Unix(),
  3584. Mtime: 0,
  3585. Price: warehouse.PackingPrice,
  3586. WarehousingDetailId: 0,
  3587. WarehouseOutDetailId: warehouseOutInfo.ID,
  3588. CancelOutDetailId: 0,
  3589. ProductDate: warehouse.ProductDate,
  3590. ExpireDate: warehouse.ExpiryDate,
  3591. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3592. StorehouseId: warehouse.StorehouseId,
  3593. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  3594. OverCount: sum_count,
  3595. }
  3596. //创建出库流水
  3597. CreateStockFlowOne(stockFlow)
  3598. //入库
  3599. warehouseInfo := &models.WarehousingInfo{
  3600. WarehousingOrder: warehouseInfo.WarehousingOrder,
  3601. WarehousingId: warehouseInfo.WarehousingId,
  3602. GoodId: warehouse.GoodId,
  3603. Number: warehouse.Number,
  3604. GoodTypeId: warehouse.GoodTypeId,
  3605. ProductDate: warehouse.ProductDate,
  3606. ExpiryDate: warehouse.ExpiryDate,
  3607. WarehousingCount: goods.Count,
  3608. Price: warehouse.Price,
  3609. TotalPrice: 0,
  3610. Status: 1,
  3611. Ctime: warehouse.Ctime,
  3612. Remark: goods.Remark,
  3613. OrgId: warehouse.OrgId,
  3614. Type: 1,
  3615. Manufacturer: warehouse.Manufacturer,
  3616. StockCount: goods.Count,
  3617. Dealer: warehouse.Dealer,
  3618. LicenseNumber: "",
  3619. PackingPrice: warehouse.PackingPrice,
  3620. StorehouseId: warehouseInfo.StorehouseId,
  3621. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  3622. IsCheck: 1,
  3623. }
  3624. AddWarehosingInfo(warehouseInfo)
  3625. //查询剩余库存
  3626. goodListOne, _ := GetAllGoodSumCount(goods.GoodId, orgID, warehouseInfo.StorehouseId)
  3627. var sum_count_one int64
  3628. for _, item := range goodListOne {
  3629. sum_count_one += item.StockCount
  3630. }
  3631. stockInfoFlow := models.VmStockFlow{
  3632. WarehousingId: warehouseInfo.WarehousingId,
  3633. GoodId: goods.GoodId,
  3634. Number: warehouse.Number,
  3635. LicenseNumber: goods.LicenseNumber,
  3636. Count: goods.Count,
  3637. UserOrgId: goods.OrgId,
  3638. PatientId: goods.PatientId,
  3639. SystemTime: record_time,
  3640. ConsumableType: 13,
  3641. IsSys: goods.IsSys,
  3642. WarehousingOrder: warehouseInfo.WarehousingOrder,
  3643. WarehouseOutId: 0,
  3644. WarehouseOutOrderNumber: "",
  3645. IsEdit: 1,
  3646. CancelStockId: 0,
  3647. CancelOrderNumber: "",
  3648. Manufacturer: warehouse.Manufacturer,
  3649. Dealer: warehouse.Dealer,
  3650. Creator: creator,
  3651. UpdateCreator: 0,
  3652. Status: 1,
  3653. Ctime: time.Now().Unix(),
  3654. Mtime: 0,
  3655. Price: warehouse.Price,
  3656. WarehousingDetailId: 0,
  3657. WarehouseOutDetailId: warehouseOutInfo.ID,
  3658. CancelOutDetailId: 0,
  3659. ProductDate: warehouse.ProductDate,
  3660. ExpireDate: warehouse.ExpiryDate,
  3661. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3662. StorehouseId: warehouseInfo.StorehouseId,
  3663. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  3664. OverCount: sum_count_one,
  3665. }
  3666. //创建入库流水
  3667. CreateStockFlowOne(stockInfoFlow)
  3668. if errThree != nil {
  3669. return errThree
  3670. }
  3671. } else {
  3672. // 出库完成后,要将该批次库存清零
  3673. warehouse.StockCount = 0
  3674. warehouse.Mtime = time.Now().Unix()
  3675. errThree := UpDateWarehouseInfoByStock(&warehouse)
  3676. if errThree != nil {
  3677. return errThree
  3678. }
  3679. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3680. goods.Count = deliver_number - stock_number
  3681. //查询剩余库存
  3682. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  3683. var sum_count int64
  3684. for _, item := range goodList {
  3685. sum_count += item.StockCount
  3686. }
  3687. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  3688. warehouseOutInfo := &models.WarehouseOutInfo{
  3689. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  3690. WarehouseOutId: goods.WarehouseOutId,
  3691. WarehouseInfotId: warehouse.ID,
  3692. Status: 1,
  3693. Ctime: time.Now().Unix(),
  3694. Remark: goods.Remark,
  3695. OrgId: orgID,
  3696. Type: 1,
  3697. Manufacturer: warehouse.Manufacturer,
  3698. Dealer: warehouse.Dealer,
  3699. IsSys: 0,
  3700. SysRecordTime: record_time,
  3701. GoodTypeId: warehouse.GoodTypeId,
  3702. GoodId: warehouse.GoodId,
  3703. ExpiryDate: warehouse.ExpiryDate,
  3704. ProductDate: warehouse.ProductDate,
  3705. Number: warehouse.Number,
  3706. Price: warehouse.PackingPrice,
  3707. LicenseNumber: warehouse.LicenseNumber,
  3708. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3709. StorehouseId: warehouse.StorehouseId,
  3710. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  3711. IsCheck: 1,
  3712. OverCount: sum_count,
  3713. }
  3714. warehouseOutInfo.Count = stock_number
  3715. //查询是否已经生成出库单
  3716. info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  3717. if errcodes == gorm.ErrRecordNotFound {
  3718. //创建出库单
  3719. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  3720. if errOne != nil {
  3721. return errOne
  3722. }
  3723. } else {
  3724. warehouseOutInfo.Count = info.Count + stock_number
  3725. UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  3726. }
  3727. stockFlow := models.VmStockFlow{
  3728. WarehousingId: warehouse.ID,
  3729. GoodId: goods.GoodId,
  3730. Number: warehouse.Number,
  3731. LicenseNumber: goods.LicenseNumber,
  3732. Count: stock_number,
  3733. UserOrgId: goods.OrgId,
  3734. PatientId: 0,
  3735. SystemTime: record_time,
  3736. ConsumableType: 12,
  3737. IsSys: 0,
  3738. WarehousingOrder: "",
  3739. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  3740. WarehouseOutId: goods.WarehouseOutId,
  3741. IsEdit: 1,
  3742. CancelStockId: 0,
  3743. CancelOrderNumber: "",
  3744. Manufacturer: warehouse.Manufacturer,
  3745. Dealer: warehouse.Dealer,
  3746. Creator: creator,
  3747. UpdateCreator: 0,
  3748. Status: 1,
  3749. Ctime: time.Now().Unix(),
  3750. Mtime: 0,
  3751. Price: warehouse.PackingPrice,
  3752. WarehousingDetailId: 0,
  3753. WarehouseOutDetailId: warehouseOutInfo.ID,
  3754. CancelOutDetailId: 0,
  3755. ProductDate: warehouse.ProductDate,
  3756. ExpireDate: warehouse.ExpiryDate,
  3757. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3758. StorehouseId: goods.StorehouseId,
  3759. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  3760. OverCount: sum_count,
  3761. }
  3762. //创建出库明细
  3763. CreateStockFlowOne(stockFlow)
  3764. //入库
  3765. warehouseInfo := &models.WarehousingInfo{
  3766. WarehousingOrder: warehouseInfo.WarehousingOrder,
  3767. WarehousingId: warehouseInfo.WarehousingId,
  3768. GoodId: warehouse.GoodId,
  3769. Number: warehouse.Number,
  3770. GoodTypeId: warehouse.GoodTypeId,
  3771. ProductDate: warehouse.ProductDate,
  3772. ExpiryDate: warehouse.ExpiryDate,
  3773. WarehousingCount: stock_number,
  3774. Price: warehouse.Price,
  3775. TotalPrice: 0,
  3776. Status: 1,
  3777. Ctime: warehouse.Ctime,
  3778. Remark: goods.Remark,
  3779. OrgId: warehouse.OrgId,
  3780. Type: 1,
  3781. Manufacturer: warehouse.Manufacturer,
  3782. StockCount: stock_number,
  3783. Dealer: warehouse.Dealer,
  3784. LicenseNumber: "",
  3785. PackingPrice: warehouse.PackingPrice,
  3786. StorehouseId: warehouseInfo.StorehouseId,
  3787. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  3788. IsCheck: 1,
  3789. }
  3790. AddWarehosingInfo(warehouseInfo)
  3791. //查询剩余库存
  3792. goodListOne, _ := GetAllGoodSumCount(goods.GoodId, orgID, warehouseInfo.StorehouseId)
  3793. var sum_count_one int64
  3794. for _, item := range goodListOne {
  3795. sum_count_one += item.StockCount
  3796. }
  3797. stockInfoFlow := models.VmStockFlow{
  3798. WarehousingId: warehouseInfo.WarehousingId,
  3799. GoodId: goods.GoodId,
  3800. Number: warehouse.Number,
  3801. LicenseNumber: goods.LicenseNumber,
  3802. Count: stock_number,
  3803. UserOrgId: goods.OrgId,
  3804. PatientId: goods.PatientId,
  3805. SystemTime: record_time,
  3806. ConsumableType: 13,
  3807. IsSys: goods.IsSys,
  3808. WarehousingOrder: warehouseInfo.WarehousingOrder,
  3809. WarehouseOutId: warehouseOut.ID,
  3810. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3811. IsEdit: 1,
  3812. CancelStockId: 0,
  3813. CancelOrderNumber: "",
  3814. Manufacturer: warehouse.Manufacturer,
  3815. Dealer: warehouse.Dealer,
  3816. Creator: creator,
  3817. UpdateCreator: 0,
  3818. Status: 1,
  3819. Ctime: time.Now().Unix(),
  3820. Mtime: 0,
  3821. Price: warehouse.Price,
  3822. WarehousingDetailId: 0,
  3823. WarehouseOutDetailId: warehouseOutInfo.ID,
  3824. CancelOutDetailId: 0,
  3825. ProductDate: warehouse.ProductDate,
  3826. ExpireDate: warehouse.ExpiryDate,
  3827. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3828. StorehouseId: warehouseInfo.StorehouseId,
  3829. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  3830. OverCount: sum_count_one,
  3831. }
  3832. //创建出库流水
  3833. CreateStockFlowOne(stockInfoFlow)
  3834. ConsumablesDeliveryTwelve(orgID, record_time, goods, warehouseOut, count, creator, warehouseInfo)
  3835. }
  3836. return nil
  3837. }
  3838. func AddWarehosingInfo(info *models.WarehousingInfo) error {
  3839. err = XTWriteDB().Create(&info).Error
  3840. return err
  3841. }
  3842. // 药品出库 递归方式
  3843. func AutoDrugDeliverInfoTwelve(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo, drugWarehouse *models.DrugWarehouseInfo) (err error) {
  3844. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  3845. var deliver_number int64 = 0
  3846. var stock_number int64 = 0
  3847. if advice.CountUnit == drup.MaxUnit {
  3848. deliver_number = prescribingNumber * drup.MinNumber
  3849. } else {
  3850. deliver_number = prescribingNumber
  3851. }
  3852. // 根据先进先出原则,查询最先入库的批次,进行出库
  3853. // 如果没有对应的库存,则报错
  3854. //开启事物
  3855. //storeConfig, _ := GetAllStoreHouseConfig(orgID)
  3856. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  3857. //fmt.Println("lastWarehouse.StockMinNumber", lastWarehouse.StockMinNumber)
  3858. //fmt.Println("drup.MinNumber", drup.MinNumber)
  3859. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  3860. var stockMax int64
  3861. var stockMin int64
  3862. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  3863. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  3864. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  3865. UpdateMinNumber(lastWarehouse.ID, stockMin)
  3866. }
  3867. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  3868. if err != nil {
  3869. return err
  3870. }
  3871. // 将该批次的剩余库存数量转换为拆零数量
  3872. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  3873. //fmt.Println("stock23233233232", stock_number)
  3874. //fmt.Println("deliver_number", deliver_number)
  3875. // 出库完成后,要减去对应批次的库存数量
  3876. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  3877. var maxNumber int64 = 0
  3878. var minNumber int64 = 0
  3879. if advice.CountUnit == drup.MinUnit {
  3880. maxNumber = prescribingNumber / drup.MinNumber
  3881. minNumber = prescribingNumber % drup.MinNumber
  3882. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  3883. minNumber = maxNumber
  3884. }
  3885. } else {
  3886. maxNumber = prescribingNumber
  3887. }
  3888. //fmt.Println("warehouse.StockMaxNumber",warehouse.StockMaxNumber)
  3889. //fmt.Println("maxNumber9899923323232wo",maxNumber)
  3890. //if warehouse.StockMaxNumber < maxNumber {
  3891. //
  3892. // return errors.New("库存数量不足")
  3893. //}
  3894. if warehouse.StockMaxNumber-maxNumber > 0 {
  3895. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  3896. }
  3897. warehouse.Mtime = time.Now().Unix()
  3898. if warehouse.StockMinNumber < minNumber {
  3899. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  3900. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  3901. } else {
  3902. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  3903. }
  3904. if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
  3905. warehouse.StockMinNumber = 0
  3906. }
  3907. if warehouse.StockMaxNumber < 0 {
  3908. return errors.New("库存数量不足")
  3909. }
  3910. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  3911. if errThree != nil {
  3912. return errThree
  3913. }
  3914. //查询剩余库存
  3915. stockInfo, _ := GetDrugAllStockInfo(advice.StorehouseId, orgID, advice.DrugId)
  3916. var sum_count int64
  3917. for _, its := range stockInfo {
  3918. if its.MaxUnit == drup.MaxUnit {
  3919. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  3920. }
  3921. sum_count += its.StockMaxNumber + its.StockMinNumber
  3922. }
  3923. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3924. if stock_number >= deliver_number {
  3925. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  3926. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  3927. WarehouseOutId: advice.WarehouseOutId,
  3928. Status: 1,
  3929. Ctime: time.Now().Unix(),
  3930. Remark: advice.Remark,
  3931. OrgId: orgID,
  3932. Type: 1,
  3933. Manufacturer: warehouse.Manufacturer,
  3934. Dealer: warehouse.Dealer,
  3935. IsSys: 12,
  3936. SysRecordTime: advice.Ctime,
  3937. DrugId: advice.DrugId,
  3938. ExpiryDate: warehouse.ExpiryDate,
  3939. ProductDate: warehouse.ProductDate,
  3940. Number: warehouse.Number,
  3941. BatchNumber: warehouse.BatchNumber,
  3942. Count: deliver_number,
  3943. RetailPrice: warehouse.RetailPrice,
  3944. Price: warehouse.RetailPrice,
  3945. WarehouseInfoId: warehouse.ID,
  3946. CountUnit: drup.MinUnit,
  3947. AdviceId: advice.ID,
  3948. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3949. StorehouseId: advice.StorehouseId,
  3950. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  3951. IsCheck: 1,
  3952. OverCount: sum_count,
  3953. }
  3954. warehouseOutInfo.Count = prescribingNumber
  3955. warehouseOutInfo.CountUnit = advice.CountUnit
  3956. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  3957. if errOne != nil {
  3958. return errOne
  3959. }
  3960. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  3961. drugflow := models.DrugFlow{
  3962. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  3963. WarehouseOutId: advice.WarehouseOutId,
  3964. DrugId: advice.DrugId,
  3965. Number: warehouse.Number,
  3966. ProductDate: warehouse.ProductDate,
  3967. ExpireDate: warehouse.ExpiryDate,
  3968. Count: deliver_number,
  3969. Price: warehouse.RetailPrice,
  3970. Status: 1,
  3971. Ctime: time.Now().Unix(),
  3972. UserOrgId: advice.OrgId,
  3973. Manufacturer: warehouse.Manufacturer,
  3974. Dealer: warehouse.Dealer,
  3975. BatchNumber: warehouse.BatchNumber,
  3976. MaxUnit: drup.MinUnit,
  3977. ConsumableType: 12,
  3978. IsEdit: 1,
  3979. Creator: 0,
  3980. IsSys: 12,
  3981. WarehouseOutDetailId: drugWareInfo.ID,
  3982. AdviceId: advice.ID,
  3983. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3984. StorehouseId: advice.StorehouseId,
  3985. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  3986. WarehousingDetailId: warehouse.ID,
  3987. OverCount: sum_count,
  3988. }
  3989. CreateDrugFlowOne(drugflow)
  3990. //入库
  3991. drugInfo := models.DrugWarehouseInfo{
  3992. WarehousingOrder: drugWarehouse.WarehousingOrder,
  3993. WarehousingId: drugWarehouse.WarehousingId,
  3994. DrugId: warehouse.DrugId,
  3995. Number: warehouse.Number,
  3996. ProductDate: warehouse.ProductDate,
  3997. ExpiryDate: warehouse.ExpiryDate,
  3998. WarehousingCount: deliver_number,
  3999. Price: warehouse.Price,
  4000. TotalPrice: warehouse.TotalPrice,
  4001. Status: 1,
  4002. Ctime: warehouse.Ctime,
  4003. Remark: "",
  4004. OrgId: warehouse.OrgId,
  4005. Type: 13,
  4006. Manufacturer: warehouse.Manufacturer,
  4007. Dealer: warehouse.Dealer,
  4008. StockMaxNumber: 0,
  4009. StockMinNumber: deliver_number,
  4010. BatchNumber: warehouse.BatchNumber,
  4011. MaxUnit: drup.MinUnit,
  4012. MinUnit: drup.MinUnit,
  4013. RetailPrice: warehouse.RetailPrice,
  4014. StorehouseId: drugWarehouse.StorehouseId,
  4015. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  4016. IsCheck: 1,
  4017. }
  4018. CreateDrugWarehouseInfo(drugInfo)
  4019. //查询剩余库存
  4020. stockInfoOne, _ := GetDrugAllStockInfo(drugWarehouse.StorehouseId, orgID, advice.DrugId)
  4021. var sum_count_one int64
  4022. for _, its := range stockInfoOne {
  4023. if its.MaxUnit == drup.MaxUnit {
  4024. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  4025. }
  4026. sum_count_one += its.StockMaxNumber + its.StockMinNumber
  4027. }
  4028. flow := models.DrugFlow{
  4029. WarehousingId: drugWarehouse.WarehousingId,
  4030. DrugId: warehouse.DrugId,
  4031. Number: warehouse.Number,
  4032. BatchNumber: warehouse.BatchNumber,
  4033. Count: deliver_number,
  4034. UserOrgId: warehouse.OrgId,
  4035. PatientId: 0,
  4036. SystemTime: 0,
  4037. ConsumableType: 13,
  4038. IsSys: 13,
  4039. WarehousingOrder: drugWarehouse.WarehousingOrder,
  4040. WarehouseOutId: 0,
  4041. WarehouseOutOrderNumber: "",
  4042. IsEdit: 0,
  4043. CancelStockId: 0,
  4044. CancelOrderNumber: "",
  4045. Manufacturer: warehouse.Manufacturer,
  4046. Dealer: warehouse.Dealer,
  4047. Creator: 0,
  4048. UpdateCreator: 0,
  4049. Status: 1,
  4050. Ctime: time.Now().Unix(),
  4051. Mtime: 0,
  4052. Price: warehouse.RetailPrice,
  4053. WarehousingDetailId: warehouse.ID,
  4054. WarehouseOutDetailId: 0,
  4055. CancelOutDetailId: 0,
  4056. ExpireDate: warehouse.ExpiryDate,
  4057. ProductDate: warehouse.ProductDate,
  4058. MaxUnit: drup.MinUnit,
  4059. MinUnit: drup.MinUnit,
  4060. StockMaxNumber: 0,
  4061. StockMinNumber: deliver_number,
  4062. StorehouseId: drugWarehouse.StorehouseId,
  4063. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  4064. OverCount: sum_count_one,
  4065. }
  4066. CreateDrugFlowOne(flow)
  4067. return nil
  4068. } else {
  4069. warehouse.StockMaxNumber = 0
  4070. warehouse.StockMinNumber = 0
  4071. warehouse.Mtime = time.Now().Unix()
  4072. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4073. if errThree != nil {
  4074. return errThree
  4075. }
  4076. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  4077. prescribingNumber_two_temp := deliver_number - stock_number
  4078. advice.CountUnit = drup.MinUnit
  4079. //查询剩余库存
  4080. stockInfo, _ := GetDrugAllStockInfo(advice.StorehouseId, orgID, advice.DrugId)
  4081. var sum_count int64
  4082. for _, its := range stockInfo {
  4083. if its.MaxUnit == drup.MaxUnit {
  4084. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  4085. }
  4086. sum_count += its.StockMaxNumber + its.StockMinNumber
  4087. }
  4088. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  4089. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4090. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  4091. WarehouseOutId: advice.WarehouseOutId,
  4092. Status: 1,
  4093. Ctime: time.Now().Unix(),
  4094. Remark: advice.Remark,
  4095. OrgId: orgID,
  4096. Type: 1,
  4097. Manufacturer: advice.Manufacturer,
  4098. Dealer: warehouse.Dealer,
  4099. IsSys: 12,
  4100. SysRecordTime: advice.Ctime,
  4101. DrugId: advice.DrugId,
  4102. ExpiryDate: warehouse.ExpiryDate,
  4103. ProductDate: warehouse.ProductDate,
  4104. Number: warehouse.Number,
  4105. BatchNumber: warehouse.BatchNumber,
  4106. Count: stock_number,
  4107. RetailPrice: advice.RetailPrice,
  4108. Price: warehouse.RetailPrice,
  4109. WarehouseInfoId: warehouse.ID,
  4110. CountUnit: drup.MinUnit,
  4111. AdviceId: advice.ID,
  4112. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4113. StorehouseId: advice.StorehouseId,
  4114. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  4115. IsCheck: 1,
  4116. OverCount: sum_count,
  4117. }
  4118. warehouseOutInfo.Count = stock_number
  4119. //warehouseOutInfo.CountUnit = advice.CountUnit
  4120. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  4121. if errOne != nil {
  4122. return errOne
  4123. }
  4124. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4125. drugflow := models.DrugFlow{
  4126. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  4127. WarehouseOutId: advice.WarehouseOutId,
  4128. DrugId: advice.DrugId,
  4129. Number: warehouse.Number,
  4130. ProductDate: warehouse.ProductDate,
  4131. ExpireDate: warehouse.ExpiryDate,
  4132. Count: stock_number,
  4133. Price: warehouse.RetailPrice,
  4134. Status: 1,
  4135. Ctime: time.Now().Unix(),
  4136. UserOrgId: advice.OrgId,
  4137. Manufacturer: warehouse.Manufacturer,
  4138. Dealer: warehouse.Dealer,
  4139. BatchNumber: warehouse.BatchNumber,
  4140. MaxUnit: drup.MinUnit,
  4141. ConsumableType: 12,
  4142. IsEdit: 1,
  4143. Creator: 0,
  4144. IsSys: 12,
  4145. WarehouseOutDetailId: drugWareInfo.ID,
  4146. AdviceId: advice.ID,
  4147. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4148. StorehouseId: advice.StorehouseId,
  4149. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  4150. WarehousingDetailId: warehouse.ID,
  4151. OverCount: sum_count,
  4152. }
  4153. CreateDrugFlowOne(drugflow)
  4154. // 出库完成后,要将该批次库存清零
  4155. //入库
  4156. drugInfo := models.DrugWarehouseInfo{
  4157. WarehousingOrder: drugWarehouse.WarehousingOrder,
  4158. WarehousingId: drugWarehouse.WarehousingId,
  4159. DrugId: warehouse.DrugId,
  4160. Number: warehouse.Number,
  4161. ProductDate: warehouse.ProductDate,
  4162. ExpiryDate: warehouse.ExpiryDate,
  4163. WarehousingCount: stock_number,
  4164. Price: warehouse.Price,
  4165. TotalPrice: warehouse.TotalPrice,
  4166. Status: 1,
  4167. Ctime: warehouse.Ctime,
  4168. Remark: "",
  4169. OrgId: warehouse.OrgId,
  4170. Type: 13,
  4171. Manufacturer: warehouse.Manufacturer,
  4172. Dealer: warehouse.Dealer,
  4173. StockMaxNumber: 0,
  4174. StockMinNumber: stock_number,
  4175. RetailTotalPrice: 0,
  4176. BatchNumber: warehouse.BatchNumber,
  4177. MaxUnit: drup.MinUnit,
  4178. MinUnit: drup.MinUnit,
  4179. RetailPrice: warehouse.RetailPrice,
  4180. StorehouseId: drugWarehouse.StorehouseId,
  4181. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  4182. }
  4183. CreateDrugWarehouseInfo(drugInfo)
  4184. //查询剩余库存
  4185. stockInfoTwo, _ := GetDrugAllStockInfo(drugWarehouse.StorehouseId, orgID, advice.DrugId)
  4186. var sum_count_one int64
  4187. for _, its := range stockInfoTwo {
  4188. if its.MaxUnit == drup.MaxUnit {
  4189. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  4190. }
  4191. sum_count_one += its.StockMaxNumber + its.StockMinNumber
  4192. }
  4193. flow := models.DrugFlow{
  4194. WarehousingId: drugWarehouse.WarehousingId,
  4195. DrugId: warehouse.DrugId,
  4196. Number: warehouse.Number,
  4197. BatchNumber: warehouse.BatchNumber,
  4198. Count: stock_number,
  4199. UserOrgId: warehouse.OrgId,
  4200. PatientId: 0,
  4201. SystemTime: 0,
  4202. ConsumableType: 13,
  4203. IsSys: 13,
  4204. WarehousingOrder: drugWarehouse.WarehousingOrder,
  4205. WarehouseOutId: 0,
  4206. WarehouseOutOrderNumber: "",
  4207. IsEdit: 0,
  4208. CancelStockId: 0,
  4209. CancelOrderNumber: "",
  4210. Manufacturer: warehouse.Manufacturer,
  4211. Dealer: warehouse.Dealer,
  4212. Creator: 0,
  4213. UpdateCreator: 0,
  4214. Status: 1,
  4215. Ctime: time.Now().Unix(),
  4216. Mtime: 0,
  4217. Price: warehouse.RetailPrice,
  4218. WarehousingDetailId: warehouse.ID,
  4219. WarehouseOutDetailId: 0,
  4220. CancelOutDetailId: 0,
  4221. ExpireDate: warehouse.ExpiryDate,
  4222. ProductDate: warehouse.ProductDate,
  4223. MaxUnit: drup.MinUnit,
  4224. MinUnit: drup.MinUnit,
  4225. StockMaxNumber: 0,
  4226. StockMinNumber: stock_number,
  4227. StorehouseId: drugWarehouse.StorehouseId,
  4228. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  4229. OverCount: sum_count,
  4230. }
  4231. CreateDrugFlowOne(flow)
  4232. AutoDrugDeliverInfoTwelve(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, drugWarehouse)
  4233. }
  4234. return
  4235. }
  4236. // 药品手动出库 递归方式
  4237. func AutoDrugDeliverInfoTwenty(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  4238. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  4239. var deliver_number int64 = 0
  4240. var stock_number int64 = 0
  4241. if advice.CountUnit == drup.MaxUnit {
  4242. deliver_number = prescribingNumber * drup.MinNumber
  4243. } else {
  4244. deliver_number = prescribingNumber
  4245. }
  4246. // 根据先进先出原则,查询最先入库的批次,进行出库
  4247. // 如果没有对应的库存,则报错
  4248. //开启事物
  4249. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  4250. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  4251. var stockMax int64
  4252. var stockMin int64
  4253. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  4254. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  4255. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  4256. UpdateMinNumber(lastWarehouse.ID, stockMin)
  4257. }
  4258. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  4259. if err != nil {
  4260. return err
  4261. }
  4262. // 将该批次的剩余库存数量转换为拆零数量
  4263. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  4264. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  4265. if stock_number >= deliver_number {
  4266. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4267. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4268. WarehouseOutId: warehouseout.ID,
  4269. Status: 1,
  4270. Ctime: time.Now().Unix(),
  4271. Remark: advice.Remark,
  4272. OrgId: orgID,
  4273. Type: 1,
  4274. Manufacturer: advice.Manufacturer,
  4275. Dealer: warehouse.Dealer,
  4276. IsSys: 0,
  4277. SysRecordTime: advice.Ctime,
  4278. DrugId: advice.DrugId,
  4279. ExpiryDate: advice.ExpiryDate,
  4280. ProductDate: advice.ProductDate,
  4281. Number: advice.Number,
  4282. BatchNumber: warehouse.BatchNumber,
  4283. Count: deliver_number,
  4284. RetailPrice: advice.RetailPrice,
  4285. Price: warehouse.RetailPrice,
  4286. WarehouseInfoId: warehouse.ID,
  4287. CountUnit: drup.MinUnit,
  4288. AdviceId: advice.ID,
  4289. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4290. StorehouseId: advice.StorehouseId,
  4291. AdminUserId: advice.AdminUserId,
  4292. IsCheck: 1,
  4293. }
  4294. if warehouse.RetailPrice == 0 {
  4295. warehouseOutInfo.Price = advice.Price
  4296. }
  4297. warehouseOutInfo.Count = prescribingNumber
  4298. warehouseOutInfo.CountUnit = advice.CountUnit
  4299. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  4300. if errOne != nil {
  4301. return errOne
  4302. }
  4303. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4304. drugflow := models.DrugFlow{
  4305. WarehouseOutId: warehouseout.ID,
  4306. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4307. DrugId: advice.DrugId,
  4308. Number: warehouse.Number,
  4309. ProductDate: advice.ProductDate,
  4310. ExpireDate: advice.ExpiryDate,
  4311. Count: deliver_number,
  4312. Price: warehouse.RetailPrice,
  4313. Status: 1,
  4314. Ctime: time.Now().Unix(),
  4315. UserOrgId: advice.OrgId,
  4316. Manufacturer: advice.Manufacturer,
  4317. Dealer: advice.Dealer,
  4318. BatchNumber: warehouse.BatchNumber,
  4319. MaxUnit: drup.MinUnit,
  4320. ConsumableType: 2,
  4321. IsEdit: 1,
  4322. Creator: 0,
  4323. IsSys: 0,
  4324. WarehouseOutDetailId: drugWareInfo.ID,
  4325. AdviceId: advice.ID,
  4326. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4327. StorehouseId: advice.StorehouseId,
  4328. AdminUserId: advice.AdminUserId,
  4329. }
  4330. if warehouse.RetailPrice == 0 {
  4331. drugflow.Price = advice.Price
  4332. }
  4333. CreateDrugFlowOne(drugflow)
  4334. // 出库完成后,要减去对应批次的库存数量
  4335. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  4336. var maxNumber int64 = 0
  4337. var minNumber int64 = 0
  4338. //var stock_max_number int64 = 0
  4339. //stock_max_number = warehouse.StockMaxNumber
  4340. maxNumber = deliver_number / drup.MinNumber
  4341. minNumber = deliver_number % drup.MinNumber
  4342. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  4343. minNumber = maxNumber
  4344. }
  4345. if drup.MaxUnit != drup.MinUnit {
  4346. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  4347. return errors.New("库存数量不足")
  4348. }
  4349. }
  4350. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  4351. if warehouse.StockMaxNumber < 0 {
  4352. warehouse.StockMaxNumber = 0
  4353. }
  4354. if warehouse.StockMinNumber < 0 {
  4355. warehouse.StockMinNumber = 0
  4356. }
  4357. warehouse.Mtime = time.Now().Unix()
  4358. if warehouse.StockMinNumber < minNumber {
  4359. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  4360. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  4361. } else {
  4362. if minNumber > 0 {
  4363. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  4364. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4365. } else {
  4366. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  4367. }
  4368. }
  4369. if minNumber == 0 && maxNumber != 1 {
  4370. if warehouse.StockMinNumber > 0 {
  4371. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4372. }
  4373. }
  4374. }
  4375. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  4376. if (warehouse.StockMinNumber - deliver_number) > 0 {
  4377. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4378. //if warehouse.StockMinNumber == 0 {
  4379. // warehouse.StockMaxNumber = stock_max_number
  4380. //}
  4381. }
  4382. }
  4383. if drup.MaxUnit != drup.MinUnit {
  4384. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  4385. return errors.New("库存数量不足")
  4386. }
  4387. }
  4388. if warehouse.StockMinNumber <= 0 {
  4389. warehouse.StockMinNumber = 0
  4390. }
  4391. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4392. if errThree != nil {
  4393. return errThree
  4394. }
  4395. //查询默认仓库
  4396. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  4397. //查询默认仓库剩余多少库存
  4398. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  4399. var sum_count int64
  4400. var sum_in_count int64
  4401. for _, it := range list {
  4402. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  4403. if it.MaxUnit == baseDrug.MaxUnit {
  4404. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  4405. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  4406. sum_count += it.StockMaxNumber + it.StockMinNumber
  4407. sum_in_count += it.WarehousingCount
  4408. }
  4409. }
  4410. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  4411. return nil
  4412. } else {
  4413. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  4414. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4415. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4416. WarehouseOutId: warehouseout.ID,
  4417. Status: 1,
  4418. Ctime: time.Now().Unix(),
  4419. Remark: advice.Remark,
  4420. OrgId: orgID,
  4421. Type: 1,
  4422. Manufacturer: advice.Manufacturer,
  4423. Dealer: warehouse.Dealer,
  4424. IsSys: 0,
  4425. SysRecordTime: advice.Ctime,
  4426. DrugId: advice.DrugId,
  4427. ExpiryDate: warehouse.ExpiryDate,
  4428. ProductDate: warehouse.ProductDate,
  4429. Number: warehouse.Number,
  4430. BatchNumber: warehouse.BatchNumber,
  4431. Count: stock_number,
  4432. RetailPrice: advice.RetailPrice,
  4433. Price: warehouse.RetailPrice,
  4434. WarehouseInfoId: warehouse.ID,
  4435. CountUnit: drup.MinUnit,
  4436. AdviceId: advice.ID,
  4437. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4438. StorehouseId: advice.StorehouseId,
  4439. AdminUserId: advice.AdminUserId,
  4440. IsCheck: 1,
  4441. }
  4442. if warehouse.RetailPrice == 0 {
  4443. warehouseOutInfo.Price = advice.Price
  4444. }
  4445. warehouseOutInfo.Count = stock_number
  4446. //warehouseOutInfo.CountUnit = advice.CountUnit
  4447. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  4448. if errOne != nil {
  4449. return errOne
  4450. }
  4451. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4452. drugflow := models.DrugFlow{
  4453. WarehouseOutId: warehouseout.ID,
  4454. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4455. DrugId: advice.DrugId,
  4456. Number: warehouse.Number,
  4457. ProductDate: advice.ProductDate,
  4458. ExpireDate: advice.ExpiryDate,
  4459. Count: stock_number,
  4460. Price: warehouse.RetailPrice,
  4461. Status: 1,
  4462. Ctime: time.Now().Unix(),
  4463. UserOrgId: advice.OrgId,
  4464. Manufacturer: advice.Manufacturer,
  4465. Dealer: advice.Dealer,
  4466. BatchNumber: warehouse.BatchNumber,
  4467. MaxUnit: drup.MinUnit,
  4468. ConsumableType: 2,
  4469. IsEdit: 1,
  4470. Creator: 0,
  4471. IsSys: 0,
  4472. WarehouseOutDetailId: drugWareInfo.ID,
  4473. AdviceId: advice.ID,
  4474. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4475. StorehouseId: advice.StorehouseId,
  4476. AdminUserId: advice.AdminUserId,
  4477. }
  4478. if warehouse.RetailPrice == 0 {
  4479. drugflow.Price = advice.Price
  4480. }
  4481. CreateDrugFlowOne(drugflow)
  4482. // 出库完成后,要将该批次库存清零
  4483. //扣减库存
  4484. warehouse.StockMaxNumber = 0
  4485. warehouse.StockMinNumber = 0
  4486. warehouse.Mtime = time.Now().Unix()
  4487. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4488. //查询默认仓库
  4489. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  4490. //查询默认仓库剩余多少库存
  4491. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  4492. var sum_count int64
  4493. var sum_in_count int64
  4494. for _, it := range list {
  4495. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  4496. if it.MaxUnit == baseDrug.MaxUnit {
  4497. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  4498. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  4499. sum_count += it.StockMaxNumber + it.StockMinNumber
  4500. sum_in_count += it.WarehousingCount
  4501. }
  4502. }
  4503. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  4504. if errThree != nil {
  4505. return errThree
  4506. }
  4507. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  4508. prescribingNumber_two_temp := deliver_number - stock_number
  4509. advice.CountUnit = drup.MinUnit
  4510. AutoDrugDeliverInfoTwenty(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  4511. }
  4512. return
  4513. }
  4514. // 药品手动出库 递归方式
  4515. func AutoDrugDeliverInfoTwentyOne(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  4516. //开事务
  4517. //tx := XTWriteDB().Begin()
  4518. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  4519. var deliver_number int64 = 0
  4520. var stock_number int64 = 0
  4521. if advice.CountUnit == drup.MaxUnit {
  4522. deliver_number = prescribingNumber * drup.MinNumber
  4523. } else {
  4524. deliver_number = prescribingNumber
  4525. }
  4526. // 根据先进先出原则,查询最先入库的批次,进行出库
  4527. // 如果没有对应的库存,则报错
  4528. //开启事物
  4529. storeConfig, _ := GetAllStoreHouseConfig(orgID)
  4530. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  4531. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  4532. var stockMax int64
  4533. var stockMin int64
  4534. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  4535. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  4536. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  4537. UpdateMinNumber(lastWarehouse.ID, stockMin)
  4538. }
  4539. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  4540. if err != nil {
  4541. return err
  4542. }
  4543. // 将该批次的剩余库存数量转换为拆零数量
  4544. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  4545. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  4546. if stock_number >= deliver_number {
  4547. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4548. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4549. WarehouseOutId: warehouseout.ID,
  4550. Status: 1,
  4551. Ctime: time.Now().Unix(),
  4552. Remark: advice.Remark,
  4553. OrgId: orgID,
  4554. Type: 1,
  4555. Manufacturer: advice.Manufacturer,
  4556. Dealer: warehouse.Dealer,
  4557. IsSys: 0,
  4558. SysRecordTime: advice.Ctime,
  4559. DrugId: advice.DrugId,
  4560. ExpiryDate: advice.ExpiryDate,
  4561. ProductDate: advice.ProductDate,
  4562. Number: advice.Number,
  4563. BatchNumber: warehouse.BatchNumber,
  4564. Count: deliver_number,
  4565. RetailPrice: advice.RetailPrice,
  4566. Price: advice.Price,
  4567. WarehouseInfoId: warehouse.ID,
  4568. CountUnit: drup.MinUnit,
  4569. AdviceId: advice.ID,
  4570. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4571. StorehouseId: advice.StorehouseId,
  4572. AdminUserId: advice.AdminUserId,
  4573. LastPrice: warehouse.Price,
  4574. StockCount: advice.StockCount,
  4575. IsCheck: 1,
  4576. }
  4577. warehouseOutInfo.Count = prescribingNumber
  4578. warehouseOutInfo.CountUnit = advice.CountUnit
  4579. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4580. drugflow := models.DrugFlow{
  4581. WarehouseOutId: warehouseout.ID,
  4582. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4583. DrugId: advice.DrugId,
  4584. Number: warehouse.Number,
  4585. ProductDate: advice.ProductDate,
  4586. ExpireDate: advice.ExpiryDate,
  4587. Count: deliver_number,
  4588. Price: advice.Price,
  4589. Status: 1,
  4590. Ctime: time.Now().Unix(),
  4591. UserOrgId: advice.OrgId,
  4592. Manufacturer: advice.Manufacturer,
  4593. Dealer: advice.Dealer,
  4594. BatchNumber: warehouse.BatchNumber,
  4595. MaxUnit: drup.MinUnit,
  4596. ConsumableType: 2,
  4597. IsEdit: 1,
  4598. Creator: 0,
  4599. IsSys: 0,
  4600. WarehouseOutDetailId: drugWareInfo.ID,
  4601. AdviceId: advice.ID,
  4602. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4603. StorehouseId: advice.StorehouseId,
  4604. AdminUserId: advice.AdminUserId,
  4605. LastPrice: warehouse.Price,
  4606. StockCount: advice.StockCount,
  4607. }
  4608. if warehouse.RetailPrice == 0 {
  4609. drugflow.Price = advice.Price
  4610. }
  4611. CreateDrugFlowOne(drugflow)
  4612. // 出库完成后,要减去对应批次的库存数量
  4613. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  4614. var maxNumber int64 = 0
  4615. var minNumber int64 = 0
  4616. //var stock_max_number int64 = 0
  4617. //stock_max_number = warehouse.StockMaxNumber
  4618. maxNumber = deliver_number / drup.MinNumber
  4619. minNumber = deliver_number % drup.MinNumber
  4620. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  4621. minNumber = maxNumber
  4622. }
  4623. if drup.MaxUnit != drup.MinUnit {
  4624. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  4625. return errors.New("库存数量不足")
  4626. }
  4627. }
  4628. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  4629. if warehouse.StockMaxNumber < 0 {
  4630. warehouse.StockMaxNumber = 0
  4631. }
  4632. if warehouse.StockMinNumber < 0 {
  4633. warehouse.StockMinNumber = 0
  4634. }
  4635. warehouse.Mtime = time.Now().Unix()
  4636. if warehouse.StockMinNumber < minNumber {
  4637. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  4638. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  4639. } else {
  4640. if minNumber > 0 {
  4641. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  4642. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4643. } else {
  4644. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  4645. }
  4646. }
  4647. if minNumber == 0 && maxNumber != 1 {
  4648. if warehouse.StockMinNumber > 0 {
  4649. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4650. }
  4651. }
  4652. }
  4653. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  4654. if (warehouse.StockMinNumber - deliver_number) > 0 {
  4655. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4656. //if warehouse.StockMinNumber == 0 {
  4657. // warehouse.StockMaxNumber = stock_max_number
  4658. //}
  4659. }
  4660. }
  4661. if drup.MaxUnit != drup.MinUnit {
  4662. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  4663. return errors.New("库存数量不足")
  4664. }
  4665. }
  4666. if warehouse.StockMinNumber <= 0 {
  4667. warehouse.StockMinNumber = 0
  4668. }
  4669. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4670. fmt.Println(errThree)
  4671. //if errThree != nil {
  4672. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  4673. // tx.Rollback()
  4674. //} else {
  4675. // tx.Commit()
  4676. //}
  4677. //查询默认仓库
  4678. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  4679. //查询默认仓库剩余多少库存
  4680. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  4681. var sum_count int64
  4682. var sum_in_count int64
  4683. for _, it := range list {
  4684. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  4685. if it.MaxUnit == baseDrug.MaxUnit {
  4686. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  4687. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  4688. sum_count += it.StockMaxNumber + it.StockMinNumber
  4689. sum_in_count += it.WarehousingCount
  4690. }
  4691. }
  4692. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  4693. return nil
  4694. } else {
  4695. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  4696. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4697. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4698. WarehouseOutId: warehouseout.ID,
  4699. Status: 1,
  4700. Ctime: time.Now().Unix(),
  4701. Remark: advice.Remark,
  4702. OrgId: orgID,
  4703. Type: 1,
  4704. Manufacturer: advice.Manufacturer,
  4705. Dealer: warehouse.Dealer,
  4706. IsSys: 0,
  4707. SysRecordTime: advice.Ctime,
  4708. DrugId: advice.DrugId,
  4709. ExpiryDate: warehouse.ExpiryDate,
  4710. ProductDate: warehouse.ProductDate,
  4711. Number: warehouse.Number,
  4712. BatchNumber: warehouse.BatchNumber,
  4713. Count: stock_number,
  4714. RetailPrice: advice.RetailPrice,
  4715. Price: advice.Price,
  4716. WarehouseInfoId: warehouse.ID,
  4717. CountUnit: drup.MinUnit,
  4718. AdviceId: advice.ID,
  4719. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4720. StorehouseId: advice.StorehouseId,
  4721. AdminUserId: advice.AdminUserId,
  4722. LastPrice: warehouse.Price,
  4723. StockCount: advice.StockCount,
  4724. IsCheck: 1,
  4725. }
  4726. warehouseOutInfo.Count = stock_number
  4727. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4728. drugflow := models.DrugFlow{
  4729. WarehouseOutId: warehouseout.ID,
  4730. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4731. DrugId: advice.DrugId,
  4732. Number: warehouse.Number,
  4733. ProductDate: advice.ProductDate,
  4734. ExpireDate: advice.ExpiryDate,
  4735. Count: stock_number,
  4736. Price: advice.Price,
  4737. Status: 1,
  4738. Ctime: time.Now().Unix(),
  4739. UserOrgId: advice.OrgId,
  4740. Manufacturer: advice.Manufacturer,
  4741. Dealer: advice.Dealer,
  4742. BatchNumber: warehouse.BatchNumber,
  4743. MaxUnit: drup.MinUnit,
  4744. ConsumableType: 2,
  4745. IsEdit: 1,
  4746. Creator: 0,
  4747. IsSys: 0,
  4748. WarehouseOutDetailId: drugWareInfo.ID,
  4749. AdviceId: advice.ID,
  4750. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4751. StorehouseId: advice.StorehouseId,
  4752. AdminUserId: advice.AdminUserId,
  4753. LastPrice: warehouse.Price,
  4754. StockCount: advice.StockCount,
  4755. }
  4756. CreateDrugFlowOne(drugflow)
  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. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  4766. // tx.Rollback()
  4767. //} else {
  4768. // tx.Commit()
  4769. //}
  4770. //查询默认仓库
  4771. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  4772. //查询默认仓库剩余多少库存
  4773. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  4774. var sum_count int64
  4775. var sum_in_count int64
  4776. for _, it := range list {
  4777. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  4778. if it.MaxUnit == baseDrug.MaxUnit {
  4779. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  4780. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  4781. sum_count += it.StockMaxNumber + it.StockMinNumber
  4782. sum_in_count += it.WarehousingCount
  4783. }
  4784. }
  4785. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  4786. if errThree != nil {
  4787. return errThree
  4788. }
  4789. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  4790. prescribingNumber_two_temp := deliver_number - stock_number
  4791. advice.CountUnit = drup.MinUnit
  4792. AutoDrugDeliverInfoTwentyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  4793. }
  4794. return
  4795. }
  4796. // 耗材手动出库
  4797. func ConsumablesDeliveryThirty(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64) (err error) {
  4798. var deliver_number int64 = 0
  4799. var stock_number int64 = 0
  4800. var maxNumber int64 = 0
  4801. deliver_number = goods.Count
  4802. // 根据先进先出原则,查询最先入库的批次,进行出库
  4803. // 如果没有对应的库存,则报错
  4804. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  4805. if err != nil {
  4806. goodErrcode := models.XtGoodErrcode{
  4807. UserOrgId: goods.OrgId,
  4808. Errcode: "查询库存信息报错",
  4809. GoodId: goods.GoodId,
  4810. Status: 1,
  4811. Ctime: time.Now().Unix(),
  4812. Mtime: 0,
  4813. Count: 0,
  4814. StockCount: 0,
  4815. Creater: creator,
  4816. BatchNumberId: warehouse.ID,
  4817. WarehouseOutId: 0,
  4818. }
  4819. CreateGoodErrcode(goodErrcode)
  4820. return err
  4821. }
  4822. stock_number = warehouse.StockCount
  4823. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  4824. if stock_number >= deliver_number {
  4825. maxNumber = goods.Count
  4826. warehouse.StockCount = warehouse.StockCount - maxNumber
  4827. warehouse.Mtime = time.Now().Unix()
  4828. //扣减库存
  4829. errThree := UpDateWarehouseInfoByStock(&warehouse)
  4830. if errThree != nil {
  4831. goodErrcode := models.XtGoodErrcode{
  4832. UserOrgId: goods.OrgId,
  4833. Errcode: "扣减库存失败",
  4834. GoodId: goods.GoodId,
  4835. Status: 1,
  4836. Ctime: time.Now().Unix(),
  4837. Mtime: 0,
  4838. Count: 0,
  4839. StockCount: 0,
  4840. Creater: creator,
  4841. BatchNumberId: warehouse.ID,
  4842. WarehouseOutId: 0,
  4843. }
  4844. CreateGoodErrcode(goodErrcode)
  4845. return errThree
  4846. }
  4847. //查询剩余库存
  4848. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  4849. var sum_count int64
  4850. for _, item := range goodList {
  4851. sum_count += item.StockCount
  4852. }
  4853. warehouseOutInfo := &models.WarehouseOutInfo{
  4854. WarehouseInfotId: warehouse.ID,
  4855. }
  4856. //添加入库单详情ID
  4857. errOne := UpdatedSigleWarehouseOutInfo(goods.ID, warehouseOutInfo)
  4858. if errOne != nil {
  4859. goodErrcode := models.XtGoodErrcode{
  4860. UserOrgId: goods.OrgId,
  4861. Errcode: "添加入库单详情ID",
  4862. GoodId: goods.GoodId,
  4863. Status: 1,
  4864. Ctime: time.Now().Unix(),
  4865. Mtime: 0,
  4866. Count: 0,
  4867. StockCount: 0,
  4868. Creater: creator,
  4869. BatchNumberId: warehouse.ID,
  4870. WarehouseOutId: 0,
  4871. }
  4872. CreateGoodErrcode(goodErrcode)
  4873. return errOne
  4874. }
  4875. stockFlow := models.VmStockFlow{
  4876. WarehousingId: warehouse.ID,
  4877. GoodId: goods.GoodId,
  4878. Number: warehouse.Number,
  4879. LicenseNumber: goods.LicenseNumber,
  4880. Count: goods.Count,
  4881. UserOrgId: goods.OrgId,
  4882. PatientId: goods.PatientId,
  4883. SystemTime: record_time,
  4884. ConsumableType: 2,
  4885. IsSys: goods.IsSys,
  4886. WarehousingOrder: warehouse.WarehousingOrder,
  4887. WarehouseOutId: warehouseOut.ID,
  4888. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  4889. IsEdit: 1,
  4890. CancelStockId: 0,
  4891. CancelOrderNumber: "",
  4892. Manufacturer: goods.Manufacturer,
  4893. Dealer: goods.Dealer,
  4894. Creator: creator,
  4895. UpdateCreator: 0,
  4896. Status: 1,
  4897. Ctime: record_time,
  4898. Mtime: time.Now().Unix(),
  4899. Price: warehouse.PackingPrice, //零售价
  4900. WarehousingDetailId: warehouse.ID,
  4901. WarehouseOutDetailId: goods.ID,
  4902. CancelOutDetailId: 0,
  4903. ProductDate: goods.ProductDate,
  4904. ExpireDate: goods.ExpiryDate,
  4905. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4906. StorehouseId: warehouse.StorehouseId,
  4907. AdminUserId: goods.AdminUserId,
  4908. BuyPrice: warehouse.Price, //进货价
  4909. StockCount: goods.StockCount,
  4910. BatchNumberCount: warehouse.StockCount - goods.Count,
  4911. IsCheck: 1,
  4912. OverCount: sum_count,
  4913. RegisterNumber: goods.RegisterNumber,
  4914. Remark: goods.Remark,
  4915. }
  4916. //创建出库流水
  4917. errflow := CreateStockFlowOne(stockFlow)
  4918. if errflow != nil {
  4919. goodErrcode := models.XtGoodErrcode{
  4920. UserOrgId: goods.OrgId,
  4921. Errcode: "创建流水报错",
  4922. GoodId: goods.GoodId,
  4923. Status: 1,
  4924. Ctime: time.Now().Unix(),
  4925. Mtime: 0,
  4926. Count: 0,
  4927. StockCount: 0,
  4928. Creater: creator,
  4929. BatchNumberId: warehouse.ID,
  4930. WarehouseOutId: 0,
  4931. }
  4932. CreateGoodErrcode(goodErrcode)
  4933. }
  4934. //添加入库ID
  4935. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, goods.OrgId, sum_count)
  4936. if errWarehouse != nil {
  4937. goodErrcode := models.XtGoodErrcode{
  4938. UserOrgId: goods.OrgId,
  4939. Errcode: "添加入库ID报错",
  4940. GoodId: goods.GoodId,
  4941. Status: 1,
  4942. Ctime: time.Now().Unix(),
  4943. Mtime: 0,
  4944. Count: 0,
  4945. StockCount: 0,
  4946. Creater: creator,
  4947. BatchNumberId: warehouse.ID,
  4948. WarehouseOutId: 0,
  4949. }
  4950. CreateGoodErrcode(goodErrcode)
  4951. }
  4952. //更新出库数量
  4953. errsumcode := ModifyGoodSumCount(goods.StorehouseId, goods.Count, goods.OrgId, goods.GoodId)
  4954. if errsumcode != nil {
  4955. goodErrcode := models.XtGoodErrcode{
  4956. UserOrgId: goods.OrgId,
  4957. Errcode: "更新出库数量报错",
  4958. GoodId: goods.GoodId,
  4959. Status: 1,
  4960. Ctime: time.Now().Unix(),
  4961. Mtime: 0,
  4962. Count: 0,
  4963. StockCount: 0,
  4964. Creater: creator,
  4965. BatchNumberId: warehouse.ID,
  4966. WarehouseOutId: 0,
  4967. }
  4968. CreateGoodErrcode(goodErrcode)
  4969. }
  4970. if warehouse.StockCount < maxNumber {
  4971. goodErrcode := models.XtGoodErrcode{
  4972. UserOrgId: goods.OrgId,
  4973. Errcode: "出库接口库存数量不足",
  4974. GoodId: goods.GoodId,
  4975. Status: 1,
  4976. Ctime: time.Now().Unix(),
  4977. Mtime: 0,
  4978. Count: 0,
  4979. StockCount: 0,
  4980. Creater: creator,
  4981. BatchNumberId: warehouse.ID,
  4982. WarehouseOutId: 0,
  4983. }
  4984. CreateGoodErrcode(goodErrcode)
  4985. return errors.New("库存数量不足")
  4986. }
  4987. return nil
  4988. } else {
  4989. // 出库完成后,要将该批次库存清零
  4990. warehouse.StockCount = 0
  4991. warehouse.Mtime = time.Now().Unix()
  4992. errThree := UpDateWarehouseInfoByStock(&warehouse)
  4993. if errThree != nil {
  4994. goodErrcode := models.XtGoodErrcode{
  4995. UserOrgId: goods.OrgId,
  4996. Errcode: "扣减库存报错",
  4997. GoodId: goods.GoodId,
  4998. Status: 1,
  4999. Ctime: time.Now().Unix(),
  5000. Mtime: 0,
  5001. Count: 0,
  5002. StockCount: 0,
  5003. Creater: creator,
  5004. BatchNumberId: warehouse.ID,
  5005. WarehouseOutId: 0,
  5006. }
  5007. CreateGoodErrcode(goodErrcode)
  5008. return errThree
  5009. }
  5010. //查询剩余库存
  5011. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  5012. var sum_count int64
  5013. for _, item := range goodList {
  5014. sum_count += item.StockCount
  5015. }
  5016. stockFlow := models.VmStockFlow{
  5017. WarehousingId: warehouse.ID,
  5018. GoodId: goods.GoodId,
  5019. Number: warehouse.Number,
  5020. LicenseNumber: goods.LicenseNumber,
  5021. Count: stock_number,
  5022. UserOrgId: goods.OrgId,
  5023. PatientId: goods.PatientId,
  5024. SystemTime: record_time,
  5025. ConsumableType: 2,
  5026. IsSys: goods.IsSys,
  5027. WarehousingOrder: warehouse.WarehousingOrder,
  5028. WarehouseOutId: warehouseOut.ID,
  5029. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  5030. IsEdit: 1,
  5031. CancelStockId: 0,
  5032. CancelOrderNumber: "",
  5033. Manufacturer: goods.Manufacturer,
  5034. Dealer: goods.Dealer,
  5035. Creator: creator,
  5036. UpdateCreator: 0,
  5037. Status: 1,
  5038. Ctime: record_time,
  5039. Mtime: time.Now().Unix(),
  5040. Price: warehouse.PackingPrice, //零售价
  5041. WarehousingDetailId: warehouse.ID,
  5042. WarehouseOutDetailId: goods.ID,
  5043. CancelOutDetailId: 0,
  5044. ProductDate: goods.ProductDate,
  5045. ExpireDate: goods.ExpiryDate,
  5046. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5047. StorehouseId: warehouse.StorehouseId,
  5048. AdminUserId: goods.AdminUserId,
  5049. BuyPrice: warehouse.Price, //进货价
  5050. StockCount: goods.StockCount,
  5051. BatchNumberCount: 0,
  5052. IsCheck: 1,
  5053. OverCount: sum_count,
  5054. RegisterNumber: goods.RegisterNumber,
  5055. Remark: goods.Remark,
  5056. }
  5057. //创建出库流水
  5058. errflow := CreateStockFlowOne(stockFlow)
  5059. if errflow != nil {
  5060. goodErrcode := models.XtGoodErrcode{
  5061. UserOrgId: goods.OrgId,
  5062. Errcode: "创建出库流水报错",
  5063. GoodId: goods.GoodId,
  5064. Status: 1,
  5065. Ctime: time.Now().Unix(),
  5066. Mtime: 0,
  5067. Count: 0,
  5068. StockCount: 0,
  5069. Creater: creator,
  5070. BatchNumberId: warehouse.ID,
  5071. WarehouseOutId: 0,
  5072. }
  5073. CreateGoodErrcode(goodErrcode)
  5074. }
  5075. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, goods.OrgId, sum_count)
  5076. if errWarehouse != nil {
  5077. goodErrcode := models.XtGoodErrcode{
  5078. UserOrgId: goods.OrgId,
  5079. Errcode: "添加入库ID报错",
  5080. GoodId: goods.GoodId,
  5081. Status: 1,
  5082. Ctime: time.Now().Unix(),
  5083. Mtime: 0,
  5084. Count: 0,
  5085. StockCount: 0,
  5086. Creater: creator,
  5087. BatchNumberId: warehouse.ID,
  5088. WarehouseOutId: 0,
  5089. }
  5090. CreateGoodErrcode(goodErrcode)
  5091. }
  5092. //更新出库数量
  5093. errcodecout := ModifyGoodSumCount(goods.StorehouseId, stock_number, goods.OrgId, goods.GoodId)
  5094. if errcodecout != nil {
  5095. goodErrcode := models.XtGoodErrcode{
  5096. UserOrgId: goods.OrgId,
  5097. Errcode: "更新出库数量报错",
  5098. GoodId: goods.GoodId,
  5099. Status: 1,
  5100. Ctime: time.Now().Unix(),
  5101. Mtime: 0,
  5102. Count: 0,
  5103. StockCount: 0,
  5104. Creater: creator,
  5105. BatchNumberId: warehouse.ID,
  5106. WarehouseOutId: 0,
  5107. }
  5108. CreateGoodErrcode(goodErrcode)
  5109. }
  5110. goods.Count = deliver_number - stock_number
  5111. ConsumablesDeliveryThirty(orgID, record_time, goods, warehouseOut, count, creator)
  5112. }
  5113. return nil
  5114. }
  5115. // 药品手动出库 递归方式
  5116. func AutoDrugDeliverInfoFourtyOne(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo, unit string) (err error) {
  5117. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  5118. var deliver_number int64 = 0
  5119. var stock_number int64 = 0
  5120. var drug_price float64
  5121. if advice.CountUnit == drup.MaxUnit {
  5122. deliver_number = prescribingNumber * drup.MinNumber
  5123. } else {
  5124. deliver_number = prescribingNumber
  5125. }
  5126. if advice.CountUnit == drup.MaxUnit {
  5127. drug_price = drup.RetailPrice
  5128. }
  5129. fmt.Println(drug_price)
  5130. if advice.CountUnit == drup.MinUnit {
  5131. drug_price = drup.MinPrice
  5132. }
  5133. if advice.CountUnit != drup.MaxUnit || advice.CountUnit != drup.MinUnit {
  5134. drug_price = drup.RetailPrice
  5135. }
  5136. // 根据先进先出原则,查询最先入库的批次,进行出库
  5137. // 如果没有对应的库存,则报错
  5138. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, advice.StorehouseId)
  5139. if lastWarehouse.MaxUnit != lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  5140. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  5141. var stockMax int64
  5142. var stockMin int64
  5143. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  5144. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  5145. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  5146. UpdateMinNumber(lastWarehouse.ID, stockMin)
  5147. }
  5148. }
  5149. if lastWarehouse.MaxUnit == lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  5150. lastWarehouse.StockMaxNumber = lastWarehouse.StockMaxNumber + lastWarehouse.StockMinNumber
  5151. ChangeMaxNumberOne(lastWarehouse.ID, lastWarehouse.StockMaxNumber)
  5152. }
  5153. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, advice.StorehouseId)
  5154. // 将该批次的剩余库存数量转换为拆零数量
  5155. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  5156. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  5157. }
  5158. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  5159. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  5160. }
  5161. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  5162. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  5163. }
  5164. if stock_number >= deliver_number {
  5165. var maxNumber int64 = 0
  5166. var minNumber int64 = 0
  5167. maxNumber = deliver_number / drup.MinNumber
  5168. minNumber = deliver_number % drup.MinNumber
  5169. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  5170. minNumber = maxNumber
  5171. }
  5172. if drup.MaxUnit != drup.MinUnit {
  5173. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  5174. return errors.New("库存数量不足")
  5175. }
  5176. }
  5177. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  5178. //fmt.Println("金1")
  5179. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  5180. }
  5181. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  5182. //fmt.Println("金2")
  5183. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  5184. }
  5185. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  5186. //fmt.Println("金3")
  5187. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  5188. }
  5189. if warehouse.StockMaxNumber < 0 {
  5190. warehouse.StockMaxNumber = 0
  5191. }
  5192. if warehouse.StockMinNumber < 0 {
  5193. warehouse.StockMinNumber = 0
  5194. }
  5195. warehouse.Mtime = time.Now().Unix()
  5196. //fmt.Println("stockmin", warehouse.StockMinNumber)
  5197. //fmt.Println("minnubmerr", minNumber)
  5198. if warehouse.StockMinNumber < minNumber {
  5199. //fmt.Println("金4")
  5200. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  5201. if warehouse.MaxUnit != warehouse.MinUnit {
  5202. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  5203. }
  5204. if minNumber == 2 && warehouse.MaxUnit == warehouse.MinUnit && warehouse.StockMinNumber == 0 {
  5205. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  5206. }
  5207. } else {
  5208. //fmt.Println("金5")
  5209. if minNumber > 0 {
  5210. if (warehouse.StockMinNumber - minNumber) >= 0 {
  5211. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  5212. }
  5213. }
  5214. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  5215. //fmt.Println("6")
  5216. if warehouse.StockMinNumber > 0 {
  5217. if warehouse.StockMinNumber-deliver_number >= 0 {
  5218. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  5219. }
  5220. }
  5221. }
  5222. }
  5223. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit && warehouse.MaxUnit != warehouse.MinUnit {
  5224. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  5225. //fmt.Println("金7")
  5226. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  5227. }
  5228. }
  5229. if drup.MaxUnit != drup.MinUnit {
  5230. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  5231. return errors.New("库存数量不足")
  5232. }
  5233. }
  5234. if warehouse.StockMinNumber <= 0 {
  5235. warehouse.StockMinNumber = 0
  5236. }
  5237. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  5238. if errThree != nil {
  5239. return errors.New("扣减库存失败")
  5240. }
  5241. //查询该药品剩余库存
  5242. infolist, _ := FindDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId)
  5243. var over_count int64
  5244. for _, its := range infolist {
  5245. if its.MaxUnit == drup.MaxUnit {
  5246. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  5247. }
  5248. over_count += its.StockMaxNumber + its.StockMinNumber
  5249. }
  5250. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  5251. drugflow := models.DrugFlow{
  5252. WarehouseOutId: warehouseout.ID,
  5253. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  5254. DrugId: advice.DrugId,
  5255. Number: warehouse.Number,
  5256. ProductDate: advice.ProductDate,
  5257. ExpireDate: advice.ExpiryDate,
  5258. Count: deliver_number,
  5259. Price: warehouse.RetailPrice,
  5260. Status: 1,
  5261. Ctime: advice.SysRecordTime,
  5262. Mtime: time.Now().Unix(),
  5263. UserOrgId: advice.OrgId,
  5264. Manufacturer: advice.Manufacturer,
  5265. Dealer: advice.Dealer,
  5266. BatchNumber: warehouse.BatchNumber,
  5267. MaxUnit: drup.MinUnit,
  5268. ConsumableType: 2,
  5269. IsEdit: 1,
  5270. Creator: 0,
  5271. IsSys: 0,
  5272. WarehouseOutDetailId: drugWareInfo.ID,
  5273. AdviceId: advice.ID,
  5274. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5275. StorehouseId: advice.StorehouseId,
  5276. AdminUserId: advice.AdminUserId,
  5277. LastPrice: warehouse.Price,
  5278. StockCount: advice.StockCount,
  5279. WarehousingDetailId: warehouse.ID,
  5280. OverCount: over_count,
  5281. RetailPrice: drug_price, //手动出库出库价格
  5282. WarehousingId: warehouse.ID,
  5283. SystemTime: advice.SysRecordTime,
  5284. PatientId: advice.PatientId,
  5285. Remark: advice.Remark,
  5286. }
  5287. if warehouse.RetailPrice == 0 {
  5288. drugflow.Price = advice.Price
  5289. }
  5290. CreateDrugFlowOne(drugflow)
  5291. //更新出库id
  5292. UpdateDrugWarehouseOutInfo(advice.ID, warehouse.ID, advice.OrgId, over_count, drug_price)
  5293. return nil
  5294. } else {
  5295. stock_number_int := strconv.FormatInt(stock_number, 10)
  5296. stock_number_int_count, _ := strconv.ParseFloat(stock_number_int, 64)
  5297. drug_price = stock_number_int_count * advice.Price
  5298. //扣减库存
  5299. warehouse.StockMaxNumber = 0
  5300. warehouse.StockMinNumber = 0
  5301. warehouse.Mtime = time.Now().Unix()
  5302. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  5303. if errThree != nil {
  5304. return errThree
  5305. }
  5306. //查询该药品剩余库存
  5307. infolist, _ := FindDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId)
  5308. var over_count int64
  5309. for _, its := range infolist {
  5310. if its.MaxUnit == drup.MaxUnit {
  5311. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  5312. }
  5313. over_count += its.StockMaxNumber + its.StockMinNumber
  5314. }
  5315. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  5316. drugflow := models.DrugFlow{
  5317. WarehouseOutId: warehouseout.ID,
  5318. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  5319. DrugId: advice.DrugId,
  5320. Number: warehouse.Number,
  5321. ProductDate: advice.ProductDate,
  5322. ExpireDate: advice.ExpiryDate,
  5323. Count: stock_number,
  5324. Price: warehouse.RetailPrice,
  5325. Status: 1,
  5326. Ctime: advice.SysRecordTime,
  5327. Mtime: time.Now().Unix(),
  5328. UserOrgId: advice.OrgId,
  5329. Manufacturer: advice.Manufacturer,
  5330. Dealer: advice.Dealer,
  5331. BatchNumber: warehouse.BatchNumber,
  5332. MaxUnit: drup.MinUnit,
  5333. ConsumableType: 2,
  5334. IsEdit: 1,
  5335. Creator: 0,
  5336. IsSys: 0,
  5337. WarehouseOutDetailId: drugWareInfo.ID,
  5338. AdviceId: advice.ID,
  5339. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5340. StorehouseId: advice.StorehouseId,
  5341. AdminUserId: advice.AdminUserId,
  5342. LastPrice: warehouse.Price,
  5343. StockCount: advice.StockCount,
  5344. OverCount: over_count,
  5345. RetailPrice: drug_price, //手动出库出库价格
  5346. WarehousingId: warehouse.ID,
  5347. WarehousingDetailId: warehouse.ID,
  5348. SystemTime: advice.SysRecordTime,
  5349. PatientId: advice.PatientId,
  5350. Remark: advice.Remark,
  5351. }
  5352. CreateDrugFlowOne(drugflow)
  5353. //更新出库id
  5354. UpdateDrugWarehouseOutInfo(advice.ID, warehouse.ID, advice.OrgId, over_count, drug_price)
  5355. // 出库完成后,要将该批次库存清零
  5356. prescribingNumber_two_temp := deliver_number - stock_number
  5357. advice.CountUnit = drup.MinUnit
  5358. AutoDrugDeliverInfoFourtyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, unit)
  5359. }
  5360. return
  5361. }
  5362. func UpdateDrugWarehouseOutInfo(id int64, warehouse_info_id int64, org_id int64, over_count int64, drug_price float64) error {
  5363. ut := XTWriteDB().Begin()
  5364. err = ut.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and org_id = ? and status = 1", id, org_id).Update(map[string]interface{}{"warehouse_info_id": warehouse_info_id, "over_count": over_count, "price": drug_price, "retail_price": drug_price}).Error
  5365. if err != nil {
  5366. ut.Rollback()
  5367. return err
  5368. }
  5369. ut.Commit()
  5370. return err
  5371. }
  5372. func UpdateGoodWarehouseOutById(id int64, warehouse_info_id int64, org_id int64, over_count int64) error {
  5373. utx := XTWriteDB().Begin()
  5374. err := utx.Model(models.WarehouseOutInfo{}).Where("id = ? and org_id = ? and status = 1", id, org_id).Update(map[string]interface{}{"warehouse_info_id": warehouse_info_id, "over_count": over_count}).Error
  5375. if err != nil {
  5376. utx.Rollback()
  5377. return err
  5378. }
  5379. utx.Commit()
  5380. return err
  5381. }
  5382. func UpdateNewGoodWarehouseOutById(id int64, warehouse_info_id int64, org_id int64, over_count int64, tx *gorm.DB) error {
  5383. err := tx.Model(models.WarehouseOutInfo{}).Where("id = ? and org_id = ? and status = 1", id, org_id).Update(map[string]interface{}{"warehouse_info_id": warehouse_info_id, "over_count": over_count}).Error
  5384. if err != nil {
  5385. tx.Rollback()
  5386. return err
  5387. }
  5388. return err
  5389. }
  5390. func ChangeMaxNumber(id int64, count int64) error {
  5391. 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
  5392. return err
  5393. }
  5394. func ChangeNewMaxNumber(id int64, count int64, tx *gorm.DB) error {
  5395. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1 and is_check = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  5396. if err != nil {
  5397. tx.Rollback()
  5398. return err
  5399. }
  5400. return err
  5401. }
  5402. func ChangeMaxNumberOne(id int64, count int64) error {
  5403. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1 and is_check = 1", id).Updates(map[string]interface{}{"stock_max_number": count, "stock_min_number": 0}).Error
  5404. return err
  5405. }
  5406. func ChangeNewMaxNumberOne(id int64, count int64, tx *gorm.DB) error {
  5407. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1 and is_check = 1", id).Updates(map[string]interface{}{"stock_max_number": count, "stock_min_number": 0}).Error
  5408. if err != nil {
  5409. tx.Rollback()
  5410. return err
  5411. }
  5412. return err
  5413. }
  5414. func UpdateMinNumber(id int64, count int64) error {
  5415. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1 and is_check = 1", id).Update(map[string]interface{}{"stock_min_number": count}).Error
  5416. return err
  5417. }
  5418. func UpdateNewMinNumber(id int64, count int64, tx *gorm.DB) error {
  5419. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1 and is_check = 1", id).Update(map[string]interface{}{"stock_min_number": count}).Error
  5420. if err != nil {
  5421. tx.Rollback()
  5422. return err
  5423. }
  5424. return err
  5425. }
  5426. func FindLastDrugWarehouseOutInfo(orgid int64) (models.DrugWarehouseOutInfo, error) {
  5427. info := models.DrugWarehouseOutInfo{}
  5428. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&info).Error
  5429. return info, err
  5430. }
  5431. func FindNewLastDrugWarehouseOutInfo(orgid int64, tx *gorm.DB) (models.DrugWarehouseOutInfo, error) {
  5432. info := models.DrugWarehouseOutInfo{}
  5433. err := tx.Where("org_id = ? and status = 1", orgid).Last(&info).Error
  5434. if err != gorm.ErrRecordNotFound {
  5435. if err != nil {
  5436. tx.Rollback()
  5437. return info, err
  5438. }
  5439. }
  5440. return info, err
  5441. }
  5442. func GetAllGoodSumCount(goodid int64, orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  5443. err = XTReadDB().Where("good_id = ? and org_id = ? and is_check =1 and status= 1 and stock_count > 0 and storehouse_id = ?", goodid, orgid, storehouse_id).Find(&info).Error
  5444. return info, err
  5445. }
  5446. func GetNewAllGoodSumCount(goodid int64, orgid int64, storehouse_id int64, tx *gorm.DB) (info []*models.WarehousingInfo, err error) {
  5447. err = tx.Where("good_id = ? and org_id = ? and is_check =1 and status= 1 and stock_count > 0 and storehouse_id = ?", goodid, orgid, storehouse_id).Find(&info).Error
  5448. if err != gorm.ErrRecordNotFound {
  5449. if err != nil {
  5450. tx.Rollback()
  5451. return info, err
  5452. }
  5453. }
  5454. return info, err
  5455. }
  5456. func CreatedFlushInfo(info models.XtWarehouseFlushInfo) error {
  5457. ut := writeDb.Begin()
  5458. err := ut.Create(&info).Error
  5459. if err != nil {
  5460. ut.Rollback()
  5461. return err
  5462. }
  5463. ut.Commit()
  5464. return err
  5465. }
  5466. func FindDrugWarehouseInfoList(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5467. err = XTReadDB().Where("drug_id = ? and org_id = ? and status =1 and is_check =1 and (stock_max_number>0 or stock_min_number>0)", drugid, orgid).Find(&info).Error
  5468. return info, err
  5469. }
  5470. func CreatedDrugFlushInfo(info models.XtDrugWarehouseFlushInfo) error {
  5471. ut := writeDb.Begin()
  5472. err := ut.Create(&info).Error
  5473. if err != nil {
  5474. ut.Rollback()
  5475. return err
  5476. }
  5477. ut.Commit()
  5478. return err
  5479. }
  5480. func UpdateWarehouseFlushInfo(warehouse_out_id int64, record_time int64, patient_id int64, batch_nubmer_id int64, good_id int64, sum_count int64, orgid int64) error {
  5481. err := XTWriteDB().Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id = ? and system_time = ? and patient_id = ? and batch_number_id = ? and good_id = ? and user_org_id = ?", warehouse_out_id, record_time, patient_id, batch_nubmer_id, good_id, orgid).Update(map[string]interface{}{"flush_count": sum_count}).Error
  5482. return err
  5483. }
  5484. func FindLastWarehouseOutInfo(orgId int64) (models.WarehouseOutInfo, error) {
  5485. info := models.WarehouseOutInfo{}
  5486. err := XTReadDB().Model(&info).Where("org_id = ? and status = 1", orgId).Find(&info).Error
  5487. return info, err
  5488. }
  5489. func ModifyAddGoodInformation(count int64, id int64) error {
  5490. ut := XTWriteDB().Begin()
  5491. err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", count)).Error
  5492. if err != nil {
  5493. ut.Rollback()
  5494. return err
  5495. }
  5496. ut.Commit()
  5497. return err
  5498. }
  5499. func ModifyReduceGoodInformation(count int64, id int64) error {
  5500. ut := XTWriteDB().Begin()
  5501. err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", count)).Error
  5502. if err != nil {
  5503. ut.Rollback()
  5504. return err
  5505. }
  5506. ut.Commit()
  5507. return err
  5508. }
  5509. func ModifyAddCancelCount(count int64, id int64) error {
  5510. ut := XTWriteDB().Begin()
  5511. err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", count)).Error
  5512. if err != nil {
  5513. ut.Rollback()
  5514. return err
  5515. }
  5516. ut.Commit()
  5517. return err
  5518. }
  5519. func ModifyReduceCancelCount(count int64, id int64) error {
  5520. ut := XTWriteDB().Begin()
  5521. err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", count)).Error
  5522. if err != nil {
  5523. ut.Rollback()
  5524. return err
  5525. }
  5526. ut.Commit()
  5527. return err
  5528. }
  5529. func ModifyAddDrugInformation(id int64, count int64, orgid int64) error {
  5530. ut := XTWriteDB().Begin()
  5531. err = ut.Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", count)).Error
  5532. if err != nil {
  5533. ut.Rollback()
  5534. return err
  5535. }
  5536. ut.Commit()
  5537. return err
  5538. }
  5539. func ModifyReduceDrugInformation(id int64, count int64, orgid int64) error {
  5540. ut := XTWriteDB().Begin()
  5541. err = ut.Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", count)).Error
  5542. if err != nil {
  5543. ut.Rollback()
  5544. return err
  5545. }
  5546. ut.Commit()
  5547. return err
  5548. }
  5549. func ModifyAddCancelAddInfomation(id int64, count int64, orgid int64, storehouse_id int64) error {
  5550. ut := XTWriteDB().Begin()
  5551. err = ut.Model(&models.XtDrugStockCount{}).Where("drug_id = ? and status = 1 and user_org_id = ? and storehouse_id = ?", id, orgid, storehouse_id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", count)).Error
  5552. if err != nil {
  5553. ut.Rollback()
  5554. return err
  5555. }
  5556. ut.Commit()
  5557. return err
  5558. }
  5559. func ModifyAddDrugOutReduceInfomation(id int64, count int64, orgid int64, storehouse_id int64) error {
  5560. ut := XTWriteDB().Begin()
  5561. err = ut.Model(&models.XtDrugStockCount{}).Where("drug_id = ? and status = 1 and user_org_id = ? and storehouse_id =?", id, orgid, storehouse_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", count)).Error
  5562. if err != nil {
  5563. ut.Rollback()
  5564. return err
  5565. }
  5566. ut.Commit()
  5567. return err
  5568. }
  5569. func ModifyAddDrugOutAddInfomation(id int64, count int64, orgid int64, storehouse_id int64) error {
  5570. ut := XTWriteDB().Begin()
  5571. err = ut.Model(&models.XtDrugStockCount{}).Where("drug_id = ? and status = 1 and user_org_id = ? and storehouse_id =?", id, orgid, storehouse_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", count)).Error
  5572. if err != nil {
  5573. ut.Rollback()
  5574. return err
  5575. }
  5576. ut.Commit()
  5577. return err
  5578. }
  5579. func ModifyDrugCancelWarehouseInfo(id int64, count int64, orgid int64, storehouse_id int64) error {
  5580. ut := XTWriteDB().Begin()
  5581. err = ut.Model(&models.XtDrugStockCount{}).Where("drug_id = ? and status = 1 and user_org_id = ? and storehouse_id = ?", id, orgid, storehouse_id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count - ?", count)).Error
  5582. if err != nil {
  5583. ut.Rollback()
  5584. return err
  5585. }
  5586. ut.Commit()
  5587. return err
  5588. }
  5589. func ModifyGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
  5590. ut := XTWriteDB().Begin()
  5591. err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count + ?", count)).Error
  5592. if err != nil {
  5593. ut.Rollback()
  5594. return err
  5595. }
  5596. err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_act_out_count", gorm.Expr("stock_act_out_count + ?", count)).Error
  5597. if err != nil {
  5598. ut.Rollback()
  5599. return err
  5600. }
  5601. ut.Commit()
  5602. return err
  5603. }
  5604. func ModifyNewGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64, tx *gorm.DB) error {
  5605. err = tx.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count + ?", count)).Error
  5606. if err != nil {
  5607. tx.Rollback()
  5608. return err
  5609. }
  5610. err = tx.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_act_out_count", gorm.Expr("stock_act_out_count + ?", count)).Error
  5611. if err != nil {
  5612. tx.Rollback()
  5613. return err
  5614. }
  5615. return err
  5616. }
  5617. func ModifyReduceGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
  5618. ut := XTWriteDB().Begin()
  5619. err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count - ?", count)).Error
  5620. if err != nil {
  5621. ut.Rollback()
  5622. return err
  5623. }
  5624. err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_act_out_count", gorm.Expr("stock_act_out_count - ?", count)).Error
  5625. if err != nil {
  5626. ut.Rollback()
  5627. return err
  5628. }
  5629. ut.Commit()
  5630. return err
  5631. }
  5632. func ModifyReduceGoodSumCountTwenty(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
  5633. ut := XTWriteDB().Begin()
  5634. err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count - ?", count)).Error
  5635. if err != nil {
  5636. ut.Rollback()
  5637. return err
  5638. }
  5639. ut.Commit()
  5640. return err
  5641. }
  5642. func ModifyAddGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
  5643. ut := XTWriteDB().Begin()
  5644. err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count - ?", count)).Error
  5645. if err != nil {
  5646. ut.Rollback()
  5647. return err
  5648. }
  5649. ut.Commit()
  5650. return err
  5651. }
  5652. func ModifyNewAddGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64, tx *gorm.DB) error {
  5653. err = tx.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count - ?", count)).Error
  5654. if err != nil {
  5655. tx.Rollback()
  5656. return err
  5657. }
  5658. return err
  5659. }
  5660. func UpdateSumAddCancelCount(user_org_id int64, good_id int64, storehouse_id int64, count int64) error {
  5661. ut := XTWriteDB().Begin()
  5662. err := ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id = ?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_cancel_count", gorm.Expr("stock_cancel_count + ?", count)).Error
  5663. if err != nil {
  5664. ut.Rollback()
  5665. return err
  5666. }
  5667. ut.Commit()
  5668. return err
  5669. }
  5670. func UpdateNewSumAddCancelCount(user_org_id int64, good_id int64, storehouse_id int64, count int64, tx *gorm.DB) error {
  5671. err := tx.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id = ?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_cancel_count", gorm.Expr("stock_cancel_count + ?", count)).Error
  5672. if err != nil {
  5673. tx.Rollback()
  5674. return err
  5675. }
  5676. return err
  5677. }
  5678. func UpdateSumReduceCancelCount(user_org_id int64, good_id int64, storehouse_id int64, count int64) error {
  5679. ut := XTWriteDB().Begin()
  5680. err := ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id = ?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_cancel_count", gorm.Expr("stock_cancel_count - ?", count)).Error
  5681. if err != nil {
  5682. ut.Rollback()
  5683. return err
  5684. }
  5685. ut.Commit()
  5686. return err
  5687. }
  5688. func UpdateSumAddOutCount(user_org_id int64, good_id int64, storehouse_id int64, count int64) error {
  5689. ut := XTWriteDB().Begin()
  5690. err := ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id = ?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count + ?", count)).Error
  5691. if err != nil {
  5692. ut.Rollback()
  5693. return err
  5694. }
  5695. ut.Commit()
  5696. return err
  5697. }
  5698. func FindDrugWarehouseInfoFlushCount(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5699. err = XTReadDB().Where("drug_id = ? and org_id = ? and status = 1 and is_check = 1", drug_id, orgid).Find(&info).Error
  5700. return info, err
  5701. }
  5702. func FindDrugWarehouseInfoFlushCountByStorehouse(drug_id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
  5703. err = XTReadDB().Where("drug_id = ? and org_id = ? and status = 1 and is_check = 1 and storehouse_id =?", drug_id, orgid, storehouse_id).Find(&info).Error
  5704. return info, err
  5705. }
  5706. // 结算出库
  5707. func HisSettleDrugsDelivery(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo, order_id int64) (err error) {
  5708. // 1.判断药品是否来自专用字典的药品库
  5709. // 2.判断当天当前机构有没有创建出库单,没有则创建
  5710. // 3.创建出库流程
  5711. // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  5712. isHasWay := false
  5713. record_time := int64(0)
  5714. isHasWay = true
  5715. record_time = advice.RecordDate
  5716. if isHasWay {
  5717. //判断当天当前机构有没有创建结算出库单,没有则创建
  5718. out, err := FindDrugSettleStockOutByIsSys(orgID, 5, record_time)
  5719. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  5720. if err == gorm.ErrRecordNotFound {
  5721. timeStr := time.Now().Format("2006-01-02")
  5722. timeArr := strings.Split(timeStr, "-")
  5723. total, _ := FindAllDrugWarehouseOut(orgID)
  5724. total = total + 1
  5725. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  5726. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  5727. number = number + total
  5728. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  5729. warehouseOut := models.DrugWarehouseOut{
  5730. WarehouseOutOrderNumber: warehousing_out_order,
  5731. OperationTime: time.Now().Unix(),
  5732. OrgId: orgID,
  5733. Creater: creater,
  5734. Ctime: time.Now().Unix(),
  5735. Status: 1,
  5736. WarehouseOutTime: record_time,
  5737. Dealer: 0,
  5738. Manufacturer: 0,
  5739. Type: 1, //结算出库
  5740. IsSys: 5,
  5741. StorehouseId: houseConfig.DrugStorehouseOut,
  5742. IsCheck: 1,
  5743. OrderId: order_id,
  5744. }
  5745. err := AddSigleDrugWarehouseOut(&warehouseOut)
  5746. if err != nil {
  5747. utils.TraceLog("创建出库单失败 err = %v", err)
  5748. return err
  5749. } else {
  5750. out = warehouseOut
  5751. }
  5752. }
  5753. // 出库流程
  5754. // 1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  5755. drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
  5756. if drup.ID > 0 {
  5757. prescribingNumber := advice.PrescribingNumber
  5758. var prescribingNumberOne int64
  5759. prescribingCount_temp := strconv.FormatFloat(math.Abs(advice.PrescribingNumber), 'f', 0, 64)
  5760. prescribe_count, _ := strconv.ParseInt(prescribingCount_temp, 10, 64)
  5761. if advice.PrescribingNumberUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  5762. prescribingNumberOne = prescribe_count * drup.MinNumber
  5763. }
  5764. if advice.PrescribingNumberUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  5765. prescribingNumberOne = prescribe_count
  5766. }
  5767. if advice.PrescribingNumberUnit == drup.MinUnit && drup.MaxUnit == drup.MinUnit {
  5768. prescribingNumberOne = prescribe_count
  5769. }
  5770. HisSettleDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice, prescribingNumberOne)
  5771. } else {
  5772. return errors.New("药品信息不存在")
  5773. }
  5774. }
  5775. return
  5776. }
  5777. // 耗材结算出库
  5778. func ConsumableSettleDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, creator int64, order_id int64) (err error) {
  5779. var deliver_number int64 = 0
  5780. var stock_number int64 = 0
  5781. var maxNumber int64 = 0
  5782. fmt.Println("223323233233223233223233223woo", goods.Count)
  5783. deliver_number = goods.Count
  5784. // 根据先进先出原则,查询最先入库的批次,进行出库
  5785. // 如果没有对应的库存,则报错
  5786. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  5787. if err != nil {
  5788. goodErrcode := models.XtGoodErrcode{
  5789. UserOrgId: goods.UserOrgId,
  5790. Errcode: "查询库存信息报错",
  5791. GoodId: goods.GoodId,
  5792. Status: 1,
  5793. Ctime: time.Now().Unix(),
  5794. Mtime: 0,
  5795. Count: 0,
  5796. StockCount: 0,
  5797. Creater: creator,
  5798. BatchNumberId: warehouse.ID,
  5799. WarehouseOutId: 0,
  5800. }
  5801. CreateGoodErrcode(goodErrcode)
  5802. return err
  5803. }
  5804. stock_number = warehouse.StockCount
  5805. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  5806. fmt.Println("stock_numnber233232232323", stock_number)
  5807. fmt.Println("deliver_nmuberwooeooo2oo23", deliver_number)
  5808. if stock_number >= deliver_number {
  5809. maxNumber = goods.Count
  5810. warehouse.StockCount = warehouse.StockCount - maxNumber
  5811. warehouse.Mtime = time.Now().Unix()
  5812. //扣减库存
  5813. errThree := UpDateWarehouseInfoByStock(&warehouse)
  5814. if errThree != nil {
  5815. goodErrcode := models.XtGoodErrcode{
  5816. UserOrgId: goods.UserOrgId,
  5817. Errcode: "扣减库存失败",
  5818. GoodId: goods.GoodId,
  5819. Status: 1,
  5820. Ctime: time.Now().Unix(),
  5821. Mtime: 0,
  5822. Count: 0,
  5823. StockCount: 0,
  5824. Creater: creator,
  5825. BatchNumberId: warehouse.ID,
  5826. WarehouseOutId: 0,
  5827. }
  5828. CreateGoodErrcode(goodErrcode)
  5829. return errThree
  5830. }
  5831. //查询剩余库存
  5832. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  5833. var sum_count int64
  5834. for _, item := range goodList {
  5835. sum_count += item.StockCount
  5836. }
  5837. warehouseOutInfo := &models.WarehouseOutInfo{
  5838. WarehouseInfotId: warehouse.ID,
  5839. }
  5840. //添加入库单详情ID
  5841. errOne := UpdatedSigleWarehouseOutInfo(goods.ID, warehouseOutInfo)
  5842. if errOne != nil {
  5843. goodErrcode := models.XtGoodErrcode{
  5844. UserOrgId: goods.UserOrgId,
  5845. Errcode: "添加入库单详情ID",
  5846. GoodId: goods.GoodId,
  5847. Status: 1,
  5848. Ctime: time.Now().Unix(),
  5849. Mtime: 0,
  5850. Count: 0,
  5851. StockCount: 0,
  5852. Creater: creator,
  5853. BatchNumberId: warehouse.ID,
  5854. WarehouseOutId: 0,
  5855. }
  5856. CreateGoodErrcode(goodErrcode)
  5857. return errOne
  5858. }
  5859. OutInfo := &models.WarehouseOutInfo{
  5860. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  5861. WarehouseOutId: warehouseOut.ID,
  5862. WarehouseInfotId: warehouse.ID,
  5863. Status: 1,
  5864. Ctime: time.Now().Unix(),
  5865. Remark: warehouse.Remark,
  5866. OrgId: orgID,
  5867. Type: 1,
  5868. Manufacturer: warehouse.Manufacturer,
  5869. Dealer: warehouse.Dealer,
  5870. IsSys: 5,
  5871. SysRecordTime: record_time,
  5872. GoodTypeId: goods.GoodTypeId,
  5873. GoodId: goods.GoodId,
  5874. PatientId: patient_id,
  5875. Number: warehouse.Number,
  5876. LicenseNumber: warehouse.LicenseNumber,
  5877. Price: warehouse.PackingPrice,
  5878. ExpiryDate: warehouse.ExpiryDate,
  5879. ProductDate: warehouse.ProductDate,
  5880. ProjectId: goods.ProjectId,
  5881. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5882. StorehouseId: goods.StorehouseId,
  5883. IsCheck: 1,
  5884. OverCount: sum_count,
  5885. RegisterNumber: warehouse.RegisterNumber,
  5886. OrderId: order_id,
  5887. }
  5888. fmt.Println("goods.count2323232233232233232322332322332", goods.Count)
  5889. OutInfo.Count = goods.Count
  5890. AddSigleWarehouseOutInfo(OutInfo)
  5891. stockFlow := models.VmStockFlow{
  5892. WarehousingId: warehouse.ID,
  5893. GoodId: goods.GoodId,
  5894. Number: warehouse.Number,
  5895. LicenseNumber: warehouse.LicenseNumber,
  5896. Count: goods.Count,
  5897. UserOrgId: orgID,
  5898. PatientId: patient_id,
  5899. SystemTime: record_time,
  5900. ConsumableType: 15,
  5901. IsSys: 5,
  5902. WarehousingOrder: warehouse.WarehousingOrder,
  5903. WarehouseOutId: warehouseOut.ID,
  5904. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  5905. IsEdit: 1,
  5906. CancelStockId: 0,
  5907. CancelOrderNumber: "",
  5908. Manufacturer: warehouse.Manufacturer,
  5909. Dealer: warehouse.Dealer,
  5910. Creator: creator,
  5911. UpdateCreator: 0,
  5912. Status: 1,
  5913. Ctime: time.Now().Unix(),
  5914. Mtime: 0,
  5915. Price: warehouse.PackingPrice, //零售价
  5916. WarehousingDetailId: warehouse.ID,
  5917. WarehouseOutDetailId: goods.ID,
  5918. CancelOutDetailId: 0,
  5919. ProductDate: warehouse.ProductDate,
  5920. ExpireDate: warehouse.ExpiryDate,
  5921. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5922. StorehouseId: warehouse.StorehouseId,
  5923. AdminUserId: creator,
  5924. BuyPrice: warehouse.Price, //进货价
  5925. StockCount: "",
  5926. BatchNumberCount: warehouse.StockCount - goods.Count,
  5927. IsCheck: 1,
  5928. OverCount: sum_count,
  5929. RegisterNumber: warehouse.RegisterNumber,
  5930. OrderId: order_id,
  5931. ProjectId: goods.ProjectId,
  5932. }
  5933. //创建出库流水
  5934. errflow := CreateStockFlowOne(stockFlow)
  5935. if errflow != nil {
  5936. goodErrcode := models.XtGoodErrcode{
  5937. UserOrgId: orgID,
  5938. Errcode: "创建流水报错",
  5939. GoodId: goods.GoodId,
  5940. Status: 1,
  5941. Ctime: time.Now().Unix(),
  5942. Mtime: 0,
  5943. Count: 0,
  5944. StockCount: 0,
  5945. Creater: creator,
  5946. BatchNumberId: warehouse.ID,
  5947. WarehouseOutId: 0,
  5948. }
  5949. CreateGoodErrcode(goodErrcode)
  5950. }
  5951. //添加入库ID
  5952. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count)
  5953. if errWarehouse != nil {
  5954. goodErrcode := models.XtGoodErrcode{
  5955. UserOrgId: orgID,
  5956. Errcode: "添加入库ID报错",
  5957. GoodId: goods.GoodId,
  5958. Status: 1,
  5959. Ctime: time.Now().Unix(),
  5960. Mtime: 0,
  5961. Count: 0,
  5962. StockCount: 0,
  5963. Creater: creator,
  5964. BatchNumberId: warehouse.ID,
  5965. WarehouseOutId: 0,
  5966. }
  5967. CreateGoodErrcode(goodErrcode)
  5968. }
  5969. //更新出库数量
  5970. errsumcode := ModifyGoodSumCount(goods.StorehouseId, goods.Count, orgID, goods.GoodId)
  5971. if errsumcode != nil {
  5972. goodErrcode := models.XtGoodErrcode{
  5973. UserOrgId: orgID,
  5974. Errcode: "更新出库数量报错",
  5975. GoodId: goods.GoodId,
  5976. Status: 1,
  5977. Ctime: time.Now().Unix(),
  5978. Mtime: 0,
  5979. Count: 0,
  5980. StockCount: 0,
  5981. Creater: creator,
  5982. BatchNumberId: warehouse.ID,
  5983. WarehouseOutId: 0,
  5984. }
  5985. CreateGoodErrcode(goodErrcode)
  5986. }
  5987. if warehouse.StockCount < maxNumber {
  5988. goodErrcode := models.XtGoodErrcode{
  5989. UserOrgId: orgID,
  5990. Errcode: "出库接口库存数量不足",
  5991. GoodId: goods.GoodId,
  5992. Status: 1,
  5993. Ctime: time.Now().Unix(),
  5994. Mtime: 0,
  5995. Count: 0,
  5996. StockCount: 0,
  5997. Creater: creator,
  5998. BatchNumberId: warehouse.ID,
  5999. WarehouseOutId: 0,
  6000. }
  6001. CreateGoodErrcode(goodErrcode)
  6002. return errors.New("库存数量不足")
  6003. }
  6004. return nil
  6005. } else {
  6006. // 出库完成后,要将该批次库存清零
  6007. warehouse.StockCount = 0
  6008. warehouse.Mtime = time.Now().Unix()
  6009. errThree := UpDateWarehouseInfoByStock(&warehouse)
  6010. if errThree != nil {
  6011. goodErrcode := models.XtGoodErrcode{
  6012. UserOrgId: orgID,
  6013. Errcode: "扣减库存报错",
  6014. GoodId: goods.GoodId,
  6015. Status: 1,
  6016. Ctime: time.Now().Unix(),
  6017. Mtime: 0,
  6018. Count: 0,
  6019. StockCount: 0,
  6020. Creater: creator,
  6021. BatchNumberId: warehouse.ID,
  6022. WarehouseOutId: 0,
  6023. }
  6024. CreateGoodErrcode(goodErrcode)
  6025. return errThree
  6026. }
  6027. //查询剩余库存
  6028. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  6029. var sum_count int64
  6030. for _, item := range goodList {
  6031. sum_count += item.StockCount
  6032. }
  6033. OutInfo := &models.WarehouseOutInfo{
  6034. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  6035. WarehouseOutId: warehouseOut.ID,
  6036. WarehouseInfotId: warehouse.ID,
  6037. Status: 1,
  6038. Ctime: time.Now().Unix(),
  6039. Remark: warehouse.Remark,
  6040. OrgId: orgID,
  6041. Type: 1,
  6042. Manufacturer: warehouse.Manufacturer,
  6043. Dealer: warehouse.Dealer,
  6044. IsSys: 5,
  6045. SysRecordTime: record_time,
  6046. GoodTypeId: goods.GoodTypeId,
  6047. GoodId: goods.GoodId,
  6048. PatientId: patient_id,
  6049. Number: warehouse.Number,
  6050. LicenseNumber: warehouse.LicenseNumber,
  6051. Price: warehouse.PackingPrice,
  6052. ExpiryDate: warehouse.ExpiryDate,
  6053. ProductDate: warehouse.ProductDate,
  6054. ProjectId: goods.ProjectId,
  6055. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6056. StorehouseId: goods.StorehouseId,
  6057. IsCheck: 1,
  6058. OverCount: sum_count,
  6059. RegisterNumber: warehouse.RegisterNumber,
  6060. OrderId: order_id,
  6061. }
  6062. OutInfo.Count = stock_number
  6063. AddSigleWarehouseOutInfo(OutInfo)
  6064. stockFlow := models.VmStockFlow{
  6065. WarehousingId: warehouse.ID,
  6066. GoodId: goods.GoodId,
  6067. Number: warehouse.Number,
  6068. LicenseNumber: warehouse.LicenseNumber,
  6069. Count: stock_number,
  6070. UserOrgId: orgID,
  6071. PatientId: patient_id,
  6072. SystemTime: record_time,
  6073. ConsumableType: 15,
  6074. IsSys: 5,
  6075. WarehousingOrder: warehouse.WarehousingOrder,
  6076. WarehouseOutId: warehouseOut.ID,
  6077. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  6078. IsEdit: 1,
  6079. CancelStockId: 0,
  6080. CancelOrderNumber: "",
  6081. Manufacturer: warehouse.Manufacturer,
  6082. Dealer: warehouse.Dealer,
  6083. Creator: creator,
  6084. UpdateCreator: 0,
  6085. Status: 1,
  6086. Ctime: time.Now().Unix(),
  6087. Mtime: 0,
  6088. Price: warehouse.PackingPrice, //零售价
  6089. WarehousingDetailId: warehouse.ID,
  6090. WarehouseOutDetailId: goods.ID,
  6091. CancelOutDetailId: 0,
  6092. ProductDate: warehouse.ProductDate,
  6093. ExpireDate: warehouse.ExpiryDate,
  6094. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6095. StorehouseId: warehouse.StorehouseId,
  6096. AdminUserId: creator,
  6097. BuyPrice: warehouse.Price, //进货价
  6098. StockCount: "",
  6099. BatchNumberCount: 0,
  6100. IsCheck: 1,
  6101. OverCount: sum_count,
  6102. RegisterNumber: warehouse.RegisterNumber,
  6103. OrderId: order_id,
  6104. ProjectId: goods.ProjectId,
  6105. }
  6106. //创建出库流水
  6107. errflow := CreateStockFlowOne(stockFlow)
  6108. if errflow != nil {
  6109. goodErrcode := models.XtGoodErrcode{
  6110. UserOrgId: orgID,
  6111. Errcode: "创建出库流水报错",
  6112. GoodId: goods.GoodId,
  6113. Status: 1,
  6114. Ctime: time.Now().Unix(),
  6115. Mtime: 0,
  6116. Count: 0,
  6117. StockCount: 0,
  6118. Creater: creator,
  6119. BatchNumberId: warehouse.ID,
  6120. WarehouseOutId: 0,
  6121. }
  6122. CreateGoodErrcode(goodErrcode)
  6123. }
  6124. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count)
  6125. if errWarehouse != nil {
  6126. goodErrcode := models.XtGoodErrcode{
  6127. UserOrgId: orgID,
  6128. Errcode: "添加入库ID报错",
  6129. GoodId: goods.GoodId,
  6130. Status: 1,
  6131. Ctime: time.Now().Unix(),
  6132. Mtime: 0,
  6133. Count: 0,
  6134. StockCount: 0,
  6135. Creater: creator,
  6136. BatchNumberId: warehouse.ID,
  6137. WarehouseOutId: 0,
  6138. }
  6139. CreateGoodErrcode(goodErrcode)
  6140. }
  6141. //更新出库数量
  6142. errcodecout := ModifyGoodSumCount(goods.StorehouseId, stock_number, orgID, goods.GoodId)
  6143. if errcodecout != nil {
  6144. goodErrcode := models.XtGoodErrcode{
  6145. UserOrgId: orgID,
  6146. Errcode: "更新出库数量报错",
  6147. GoodId: goods.GoodId,
  6148. Status: 1,
  6149. Ctime: time.Now().Unix(),
  6150. Mtime: 0,
  6151. Count: 0,
  6152. StockCount: 0,
  6153. Creater: creator,
  6154. BatchNumberId: warehouse.ID,
  6155. WarehouseOutId: 0,
  6156. }
  6157. CreateGoodErrcode(goodErrcode)
  6158. }
  6159. }
  6160. goods.Count = deliver_number - stock_number
  6161. prepare := &models.DialysisBeforePrepare{
  6162. Count: deliver_number - stock_number,
  6163. GoodTypeId: goods.GoodTypeId,
  6164. GoodId: goods.GoodId,
  6165. PatientId: goods.PatientId,
  6166. RecordDate: goods.RecordDate,
  6167. UserOrgId: orgID,
  6168. Ctime: time.Now().Unix(),
  6169. Creater: goods.Creater,
  6170. CommdityCode: goods.CommdityCode,
  6171. Status: 1,
  6172. StorehouseId: goods.StorehouseId,
  6173. }
  6174. ConsumableSettleDelivery(orgID, patient_id, record_time, prepare, warehouseOut, creator, order_id)
  6175. return nil
  6176. }
  6177. // 保存处方出库
  6178. func ConsumablePrescriptionDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, creator int64, count int64) (err error) {
  6179. var deliver_number int64 = 0
  6180. var stock_number int64 = 0
  6181. var maxNumber int64 = 0
  6182. deliver_number = goods.Count
  6183. // 根据先进先出原则,查询最先入库的批次,进行出库
  6184. // 如果没有对应的库存,则报错
  6185. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  6186. if err != nil {
  6187. goodErrcode := models.XtGoodErrcode{
  6188. UserOrgId: goods.UserOrgId,
  6189. Errcode: "查询库存信息报错",
  6190. GoodId: goods.GoodId,
  6191. Status: 1,
  6192. Ctime: time.Now().Unix(),
  6193. Mtime: 0,
  6194. Count: 0,
  6195. StockCount: 0,
  6196. Creater: creator,
  6197. BatchNumberId: warehouse.ID,
  6198. WarehouseOutId: 0,
  6199. }
  6200. CreateGoodErrcode(goodErrcode)
  6201. return err
  6202. }
  6203. stock_number = warehouse.StockCount
  6204. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6205. if stock_number >= deliver_number {
  6206. maxNumber = goods.Count
  6207. warehouse.StockCount = warehouse.StockCount - maxNumber
  6208. warehouse.Mtime = time.Now().Unix()
  6209. //扣减库存
  6210. errThree := UpDateWarehouseInfoByStock(&warehouse)
  6211. if errThree != nil {
  6212. goodErrcode := models.XtGoodErrcode{
  6213. UserOrgId: goods.UserOrgId,
  6214. Errcode: "扣减库存失败",
  6215. GoodId: goods.GoodId,
  6216. Status: 1,
  6217. Ctime: time.Now().Unix(),
  6218. Mtime: 0,
  6219. Count: 0,
  6220. StockCount: 0,
  6221. Creater: creator,
  6222. BatchNumberId: warehouse.ID,
  6223. WarehouseOutId: 0,
  6224. }
  6225. CreateGoodErrcode(goodErrcode)
  6226. return errThree
  6227. }
  6228. //查询剩余库存
  6229. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  6230. var sum_count int64
  6231. for _, item := range goodList {
  6232. sum_count += item.StockCount
  6233. }
  6234. //查询已经出库的数据
  6235. flowGood, _ := GetStockFlowIsBatchNumberSix(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6236. var out_count int64
  6237. var out_count_one int64
  6238. for _, item := range flowGood {
  6239. out_count += item.Count
  6240. }
  6241. //查询退库数据
  6242. flowGoodTwo, _ := GetStockFlowIsBatchNumberFour(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6243. for _, item := range flowGoodTwo {
  6244. out_count_one += item.Count
  6245. }
  6246. //flowGood, _ := GetStockFlowIsBatchNumberSeventy(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6247. //
  6248. //var out_count int64
  6249. //var out_count_one int64
  6250. var out_count_two int64
  6251. //
  6252. //for _, item := range flowGood {
  6253. // out_count += item.Count
  6254. //}
  6255. //查询退库数据
  6256. //flowGoodTwo, _ := GetStockFlowIsBatchNumberFourty(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6257. //for _, item := range flowGoodTwo {
  6258. // out_count_one += item.Count
  6259. //}
  6260. wareOutList, _ := GetStockFlowIsBatchNumberSixty(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6261. for _, item := range wareOutList {
  6262. out_count_two += item.Count
  6263. }
  6264. OutInfo := &models.WarehouseOutInfo{
  6265. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  6266. WarehouseOutId: warehouseOut.ID,
  6267. WarehouseInfotId: warehouse.ID,
  6268. Status: 1,
  6269. Ctime: time.Now().Unix(),
  6270. Remark: warehouse.Remark,
  6271. OrgId: orgID,
  6272. Type: 1,
  6273. Manufacturer: warehouse.Manufacturer,
  6274. Dealer: warehouse.Dealer,
  6275. IsSys: 1,
  6276. SysRecordTime: record_time,
  6277. GoodTypeId: goods.GoodTypeId,
  6278. GoodId: goods.GoodId,
  6279. PatientId: patient_id,
  6280. Number: warehouse.Number,
  6281. LicenseNumber: warehouse.LicenseNumber,
  6282. Price: warehouse.PackingPrice,
  6283. ExpiryDate: warehouse.ExpiryDate,
  6284. ProductDate: warehouse.ProductDate,
  6285. ProjectId: goods.ProjectId,
  6286. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6287. StorehouseId: goods.StorehouseId,
  6288. IsCheck: 1,
  6289. OverCount: sum_count,
  6290. RegisterNumber: warehouse.RegisterNumber,
  6291. }
  6292. OutInfo.Count = count - out_count_two + out_count_one
  6293. _, errcodes := GetNewWarehouseOutInfoIsExistSix(goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID)
  6294. if errcodes == gorm.ErrRecordNotFound {
  6295. errOne := AddSigleWarehouseOutInfo(OutInfo)
  6296. if errOne != nil {
  6297. return errOne
  6298. }
  6299. } else if errcodes == nil {
  6300. outInfoOne, _ := GetWarehouseOutInfoIsExistThree(goods.GoodId, patient_id, record_time, goods.ProjectId)
  6301. if count != outInfoOne.Count {
  6302. UpdatedWarehouseOutInfo(OutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId)
  6303. }
  6304. }
  6305. lastOut, _ := FindWarehouseOutInfoByPatientIdTwo(patient_id, record_time, goods.GoodId, orgID, goods.ProjectId)
  6306. //如果本次出库数据大于历史出库数据 新增1条流水
  6307. if count > (out_count - out_count_one) {
  6308. stockFlow := models.VmStockFlow{
  6309. WarehousingId: warehouse.ID,
  6310. GoodId: goods.GoodId,
  6311. Number: warehouse.Number,
  6312. LicenseNumber: warehouse.LicenseNumber,
  6313. Count: count - out_count + out_count_one,
  6314. UserOrgId: orgID,
  6315. PatientId: goods.PatientId,
  6316. SystemTime: record_time,
  6317. ConsumableType: 3,
  6318. IsSys: 1,
  6319. WarehousingOrder: warehouse.WarehousingOrder,
  6320. WarehouseOutId: warehouseOut.ID,
  6321. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  6322. IsEdit: 1,
  6323. CancelStockId: 0,
  6324. CancelOrderNumber: "",
  6325. Manufacturer: warehouse.Manufacturer,
  6326. Dealer: warehouse.Dealer,
  6327. Creator: creator,
  6328. UpdateCreator: 0,
  6329. Status: 1,
  6330. Ctime: time.Now().Unix(),
  6331. Mtime: 0,
  6332. Price: warehouse.PackingPrice, //零售价
  6333. WarehousingDetailId: warehouse.ID,
  6334. WarehouseOutDetailId: goods.ID,
  6335. CancelOutDetailId: 0,
  6336. ProductDate: warehouse.ProductDate,
  6337. ExpireDate: warehouse.ExpiryDate,
  6338. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6339. StorehouseId: warehouse.StorehouseId,
  6340. AdminUserId: creator,
  6341. BuyPrice: warehouse.Price, //进货价
  6342. StockCount: "",
  6343. BatchNumberCount: warehouse.StockCount - goods.Count,
  6344. IsCheck: 1,
  6345. OverCount: sum_count,
  6346. RegisterNumber: warehouse.RegisterNumber,
  6347. ProjectId: goods.ProjectId,
  6348. OperateTime: time.Now().Unix(),
  6349. }
  6350. //创建出库流水
  6351. errflow := CreateStockFlowOne(stockFlow)
  6352. if errflow != nil {
  6353. goodErrcode := models.XtGoodErrcode{
  6354. UserOrgId: orgID,
  6355. Errcode: "创建流水报错",
  6356. GoodId: goods.GoodId,
  6357. Status: 1,
  6358. Ctime: time.Now().Unix(),
  6359. Mtime: 0,
  6360. Count: 0,
  6361. StockCount: 0,
  6362. Creater: creator,
  6363. BatchNumberId: warehouse.ID,
  6364. WarehouseOutId: 0,
  6365. }
  6366. CreateGoodErrcode(goodErrcode)
  6367. }
  6368. fmt.Println("stockFlow.Count233232323232322323232332wo0-----------------------", stockFlow.Count)
  6369. //更新出库数量
  6370. errsumcode := ModifyGoodSumCount(goods.StorehouseId, stockFlow.Count, orgID, goods.GoodId)
  6371. if errsumcode != nil {
  6372. goodErrcode := models.XtGoodErrcode{
  6373. UserOrgId: orgID,
  6374. Errcode: "更新出库数量报错",
  6375. GoodId: goods.GoodId,
  6376. Status: 1,
  6377. Ctime: time.Now().Unix(),
  6378. Mtime: 0,
  6379. Count: 0,
  6380. StockCount: 0,
  6381. Creater: creator,
  6382. BatchNumberId: warehouse.ID,
  6383. WarehouseOutId: 0,
  6384. }
  6385. CreateGoodErrcode(goodErrcode)
  6386. }
  6387. }
  6388. //如果本次出库数据小于历史出库数据 新增1条退库流水
  6389. if count < (out_count - out_count_one) {
  6390. operation_time := time.Now().Unix()
  6391. //创建退库单
  6392. timeStr := time.Now().Format("2006-01-02")
  6393. timeArr := strings.Split(timeStr, "-")
  6394. total, _ := FindAllCancelStockTotal(orgID)
  6395. total = total + 1
  6396. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  6397. cancelStock := models.CancelStock{
  6398. OrderNumber: orderNumber,
  6399. OperaTime: operation_time,
  6400. OrgId: orgID,
  6401. Creater: warehouseOut.Creater,
  6402. Ctime: time.Now().Unix(),
  6403. Status: 1,
  6404. ReturnTime: record_time,
  6405. Type: 1,
  6406. StorehouseId: goods.StorehouseId,
  6407. IsCheck: 1,
  6408. }
  6409. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, orgID)
  6410. if msgerrkonde == gorm.ErrRecordNotFound {
  6411. AddSigleCancelStock(&cancelStock)
  6412. }
  6413. cancel, _ := GetLastCancelStockById(orgID)
  6414. manufacturer, _ := GetManufactureById(warehouse.Manufacturer)
  6415. deaerler, _ := GetDealerById(warehouse.Dealer)
  6416. cancelStockInfo := models.CancelStockInfo{
  6417. GoodId: goods.GoodId,
  6418. CancelStockId: cancel.ID,
  6419. GoodTypeId: goods.GoodTypeId,
  6420. Count: out_count - out_count_one - count,
  6421. Price: warehouse.PackingPrice,
  6422. Total: 0,
  6423. ProductDate: warehouse.ProductDate,
  6424. ExpiryDate: warehouse.ExpiryDate,
  6425. Ctime: time.Now().Unix(),
  6426. Status: 1,
  6427. OrgId: orgID,
  6428. OrderNumber: cancel.OrderNumber,
  6429. Type: 0,
  6430. Dealer: deaerler.DealerName,
  6431. Manufacturer: manufacturer.ManufacturerName,
  6432. Number: warehouse.Number,
  6433. RegisterAccount: "",
  6434. Remark: "",
  6435. WarehouseInfoId: warehouse.ID,
  6436. PatientId: patient_id,
  6437. RecordDate: record_time,
  6438. StorehouseId: goods.StorehouseId,
  6439. IsCheck: 1,
  6440. }
  6441. if orgID == 9671 || orgID == 10265 {
  6442. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  6443. cancelStockInfo.Price = goodsInfo.PackingPrice
  6444. }
  6445. CreateCancelStockInfoOne(&cancelStockInfo)
  6446. cancelInfo, _ := GetLastCancelStockInfoByGoodId(goods.GoodId)
  6447. flow := models.VmStockFlow{
  6448. WarehousingId: warehouse.ID,
  6449. GoodId: goods.GoodId,
  6450. Number: warehouse.Number,
  6451. LicenseNumber: warehouse.LicenseNumber,
  6452. Count: out_count - out_count_one - count,
  6453. UserOrgId: orgID,
  6454. PatientId: patient_id,
  6455. SystemTime: record_time,
  6456. ConsumableType: 7,
  6457. IsSys: 1,
  6458. WarehousingOrder: "",
  6459. WarehouseOutId: lastOut.WarehouseOutId,
  6460. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  6461. IsEdit: 0,
  6462. CancelStockId: cancel.ID,
  6463. CancelOrderNumber: cancel.OrderNumber,
  6464. Manufacturer: manufacturer.ID,
  6465. Dealer: 0,
  6466. Creator: warehouseOut.Creater,
  6467. UpdateCreator: 0,
  6468. Status: 1,
  6469. Ctime: time.Now().Unix(),
  6470. Mtime: 0,
  6471. Price: warehouse.PackingPrice,
  6472. WarehousingDetailId: warehouse.ID,
  6473. WarehouseOutDetailId: lastOut.ID,
  6474. CancelOutDetailId: cancelInfo.ID,
  6475. ProductDate: warehouse.ProductDate,
  6476. ExpireDate: warehouse.ExpiryDate,
  6477. StorehouseId: goods.StorehouseId,
  6478. BuyPrice: warehouse.Price,
  6479. ProjectId: goods.ProjectId,
  6480. OverCount: sum_count,
  6481. RegisterNumber: warehouse.RegisterNumber,
  6482. OperateTime: time.Now().Unix(),
  6483. }
  6484. if orgID == 9671 || orgID == 10265 {
  6485. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  6486. flow.Price = goodsInfo.PackingPrice
  6487. flow.BuyPrice = goodsInfo.BuyPrice
  6488. }
  6489. CreateStockFlowOne(flow)
  6490. //退库数量增加
  6491. UpdateSumAddCancelCount(orgID, goods.GoodId, goods.StorehouseId, flow.Count)
  6492. ModifyAddGoodSumCount(goods.StorehouseId, flow.Count, orgID, goods.GoodId)
  6493. }
  6494. //添加入库ID
  6495. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count)
  6496. if errWarehouse != nil {
  6497. goodErrcode := models.XtGoodErrcode{
  6498. UserOrgId: orgID,
  6499. Errcode: "添加入库ID报错",
  6500. GoodId: goods.GoodId,
  6501. Status: 1,
  6502. Ctime: time.Now().Unix(),
  6503. Mtime: 0,
  6504. Count: 0,
  6505. StockCount: 0,
  6506. Creater: creator,
  6507. BatchNumberId: warehouse.ID,
  6508. WarehouseOutId: 0,
  6509. }
  6510. CreateGoodErrcode(goodErrcode)
  6511. }
  6512. if warehouse.StockCount < maxNumber {
  6513. goodErrcode := models.XtGoodErrcode{
  6514. UserOrgId: orgID,
  6515. Errcode: "出库接口库存数量不足",
  6516. GoodId: goods.GoodId,
  6517. Status: 1,
  6518. Ctime: time.Now().Unix(),
  6519. Mtime: 0,
  6520. Count: 0,
  6521. StockCount: 0,
  6522. Creater: creator,
  6523. BatchNumberId: warehouse.ID,
  6524. WarehouseOutId: 0,
  6525. }
  6526. CreateGoodErrcode(goodErrcode)
  6527. return errors.New("库存数量不足")
  6528. }
  6529. return nil
  6530. } else {
  6531. // 出库完成后,要将该批次库存清零
  6532. warehouse.StockCount = 0
  6533. warehouse.Mtime = time.Now().Unix()
  6534. errThree := UpDateWarehouseInfoByStock(&warehouse)
  6535. if errThree != nil {
  6536. goodErrcode := models.XtGoodErrcode{
  6537. UserOrgId: orgID,
  6538. Errcode: "扣减库存报错",
  6539. GoodId: goods.GoodId,
  6540. Status: 1,
  6541. Ctime: time.Now().Unix(),
  6542. Mtime: 0,
  6543. Count: 0,
  6544. StockCount: 0,
  6545. Creater: creator,
  6546. BatchNumberId: warehouse.ID,
  6547. WarehouseOutId: 0,
  6548. }
  6549. CreateGoodErrcode(goodErrcode)
  6550. return errThree
  6551. }
  6552. //查询剩余库存
  6553. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  6554. var sum_count int64
  6555. for _, item := range goodList {
  6556. sum_count += item.StockCount
  6557. }
  6558. fmt.Println("剩余库存", sum_count)
  6559. OutInfo := &models.WarehouseOutInfo{
  6560. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  6561. WarehouseOutId: warehouseOut.ID,
  6562. WarehouseInfotId: warehouse.ID,
  6563. Status: 1,
  6564. Ctime: time.Now().Unix(),
  6565. Remark: warehouse.Remark,
  6566. OrgId: orgID,
  6567. Type: 1,
  6568. Manufacturer: warehouse.Manufacturer,
  6569. Dealer: warehouse.Dealer,
  6570. IsSys: 1,
  6571. SysRecordTime: record_time,
  6572. GoodTypeId: goods.GoodTypeId,
  6573. GoodId: goods.GoodId,
  6574. PatientId: patient_id,
  6575. Number: warehouse.Number,
  6576. LicenseNumber: warehouse.LicenseNumber,
  6577. Price: warehouse.PackingPrice,
  6578. ExpiryDate: warehouse.ExpiryDate,
  6579. ProductDate: warehouse.ProductDate,
  6580. ProjectId: goods.ProjectId,
  6581. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6582. StorehouseId: goods.StorehouseId,
  6583. IsCheck: 1,
  6584. OverCount: sum_count,
  6585. RegisterNumber: warehouse.RegisterNumber,
  6586. }
  6587. OutInfo.Count = stock_number
  6588. _, errcodes := GetNewWarehouseOutInfoIsExistSix(goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID)
  6589. if errcodes == gorm.ErrRecordNotFound {
  6590. errOne := AddSigleWarehouseOutInfo(OutInfo)
  6591. if errOne != nil {
  6592. return errOne
  6593. }
  6594. } else if errcodes == nil {
  6595. outInfoOne, _ := GetWarehouseOutInfoIsExistTen(goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID)
  6596. if count != outInfoOne.Count {
  6597. UpdatedWarehouseOutInfoSix(OutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID)
  6598. }
  6599. }
  6600. lastOut, _ := FindWarehouseOutInfoByPatientIdTwo(patient_id, record_time, goods.GoodId, orgID, goods.ProjectId)
  6601. //查询该该批次已经出库的数据
  6602. //flowGood, _ := GetStockFlowIsBatchNumber(warehouse.ID, patient_id, record_time, goods.GoodId)
  6603. //查询已经出库的数据
  6604. //flowGood, _ := GetStockFlowIsBatchNumberSeventy(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6605. //var out_count int64
  6606. //var out_count_one int64
  6607. //for _, item := range flowGood {
  6608. // out_count += item.Count
  6609. //}
  6610. //flowGoodTwo, _ := GetStockFlowIsBatchNumberSix(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6611. //for _, item := range flowGoodTwo {
  6612. // out_count_one += item.Count
  6613. //}
  6614. //查询该该批次已经出库的数据
  6615. flowGood, _ := GetStockFlowIsBatchNumberSix(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6616. var out_count int64
  6617. var out_count_one int64
  6618. for _, item := range flowGood {
  6619. out_count += item.Count
  6620. }
  6621. flowGoodTwo, _ := GetStockFlowIsBatchNumberFour(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6622. for _, item := range flowGoodTwo {
  6623. out_count_one += item.Count
  6624. }
  6625. //如果出库数量 大于 历史出库数据 新增1条流水
  6626. if count > out_count-out_count_one {
  6627. stockFlow := models.VmStockFlow{
  6628. WarehousingId: warehouse.ID,
  6629. GoodId: goods.GoodId,
  6630. Number: warehouse.Number,
  6631. LicenseNumber: warehouse.LicenseNumber,
  6632. Count: stock_number,
  6633. UserOrgId: orgID,
  6634. PatientId: goods.PatientId,
  6635. SystemTime: record_time,
  6636. ConsumableType: 3,
  6637. IsSys: 1,
  6638. WarehousingOrder: warehouse.WarehousingOrder,
  6639. WarehouseOutId: warehouseOut.ID,
  6640. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  6641. IsEdit: 1,
  6642. CancelStockId: 0,
  6643. CancelOrderNumber: "",
  6644. Manufacturer: warehouse.Manufacturer,
  6645. Dealer: warehouse.Dealer,
  6646. Creator: creator,
  6647. UpdateCreator: 0,
  6648. Status: 1,
  6649. Ctime: time.Now().Unix(),
  6650. Mtime: 0,
  6651. Price: warehouse.PackingPrice, //零售价
  6652. WarehousingDetailId: warehouse.ID,
  6653. WarehouseOutDetailId: goods.ID,
  6654. CancelOutDetailId: 0,
  6655. ProductDate: warehouse.ProductDate,
  6656. ExpireDate: warehouse.ExpiryDate,
  6657. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6658. StorehouseId: warehouse.StorehouseId,
  6659. AdminUserId: creator,
  6660. BuyPrice: warehouse.Price, //进货价
  6661. StockCount: "",
  6662. BatchNumberCount: 0,
  6663. IsCheck: 1,
  6664. OverCount: sum_count,
  6665. RegisterNumber: warehouse.RegisterNumber,
  6666. ProjectId: goods.ProjectId,
  6667. OperateTime: time.Now().Unix(),
  6668. }
  6669. //创建出库流水
  6670. errflow := CreateStockFlowOne(stockFlow)
  6671. if errflow != nil {
  6672. goodErrcode := models.XtGoodErrcode{
  6673. UserOrgId: orgID,
  6674. Errcode: "创建出库流水报错",
  6675. GoodId: goods.GoodId,
  6676. Status: 1,
  6677. Ctime: time.Now().Unix(),
  6678. Mtime: 0,
  6679. Count: 0,
  6680. StockCount: 0,
  6681. Creater: creator,
  6682. BatchNumberId: warehouse.ID,
  6683. WarehouseOutId: 0,
  6684. }
  6685. CreateGoodErrcode(goodErrcode)
  6686. }
  6687. //更新出库数量
  6688. errcodecout := ModifyGoodSumCount(goods.StorehouseId, stock_number, orgID, goods.GoodId)
  6689. if errcodecout != nil {
  6690. goodErrcode := models.XtGoodErrcode{
  6691. UserOrgId: orgID,
  6692. Errcode: "更新出库数量报错",
  6693. GoodId: goods.GoodId,
  6694. Status: 1,
  6695. Ctime: time.Now().Unix(),
  6696. Mtime: 0,
  6697. Count: 0,
  6698. StockCount: 0,
  6699. Creater: creator,
  6700. BatchNumberId: warehouse.ID,
  6701. WarehouseOutId: 0,
  6702. }
  6703. CreateGoodErrcode(goodErrcode)
  6704. }
  6705. }
  6706. //退库
  6707. if count < out_count-out_count_one {
  6708. operation_time := time.Now().Unix()
  6709. //创建退库单
  6710. timeStr := time.Now().Format("2006-01-02")
  6711. timeArr := strings.Split(timeStr, "-")
  6712. total, _ := FindAllCancelStockTotal(orgID)
  6713. total = total + 1
  6714. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  6715. cancelStock := models.CancelStock{
  6716. OrderNumber: orderNumber,
  6717. OperaTime: operation_time,
  6718. OrgId: orgID,
  6719. Creater: warehouseOut.Creater,
  6720. Ctime: time.Now().Unix(),
  6721. Status: 1,
  6722. ReturnTime: record_time,
  6723. Type: 1,
  6724. StorehouseId: goods.StorehouseId,
  6725. IsCheck: 1,
  6726. }
  6727. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, orgID)
  6728. if msgerrkonde == gorm.ErrRecordNotFound {
  6729. AddSigleCancelStock(&cancelStock)
  6730. }
  6731. cancel, _ := GetLastCancelStockById(orgID)
  6732. manufacturer, _ := GetManufactureById(warehouse.Manufacturer)
  6733. deaerler, _ := GetDealerById(warehouse.Dealer)
  6734. cancelStockInfo := models.CancelStockInfo{
  6735. GoodId: goods.GoodId,
  6736. CancelStockId: cancel.ID,
  6737. GoodTypeId: goods.GoodTypeId,
  6738. Count: out_count - out_count_one - count,
  6739. Price: warehouse.PackingPrice,
  6740. Total: 0,
  6741. ProductDate: warehouse.ProductDate,
  6742. ExpiryDate: warehouse.ExpiryDate,
  6743. Ctime: time.Now().Unix(),
  6744. Status: 1,
  6745. OrgId: orgID,
  6746. OrderNumber: cancel.OrderNumber,
  6747. Type: 0,
  6748. Dealer: deaerler.DealerName,
  6749. Manufacturer: manufacturer.ManufacturerName,
  6750. Number: warehouse.Number,
  6751. RegisterAccount: "",
  6752. Remark: "",
  6753. WarehouseInfoId: warehouse.ID,
  6754. PatientId: patient_id,
  6755. RecordDate: record_time,
  6756. StorehouseId: goods.StorehouseId,
  6757. IsCheck: 1,
  6758. }
  6759. if orgID == 9671 || orgID == 10265 {
  6760. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  6761. cancelStockInfo.Price = goodsInfo.PackingPrice
  6762. }
  6763. CreateCancelStockInfoOne(&cancelStockInfo)
  6764. cancelInfo, _ := GetLastCancelStockInfoByGoodId(goods.GoodId)
  6765. flow := models.VmStockFlow{
  6766. WarehousingId: warehouse.ID,
  6767. GoodId: goods.GoodId,
  6768. Number: warehouse.Number,
  6769. LicenseNumber: warehouse.LicenseNumber,
  6770. Count: out_count - count,
  6771. UserOrgId: orgID,
  6772. PatientId: patient_id,
  6773. SystemTime: record_time,
  6774. ConsumableType: 7,
  6775. IsSys: 1,
  6776. WarehousingOrder: "",
  6777. WarehouseOutId: lastOut.WarehouseOutId,
  6778. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  6779. IsEdit: 0,
  6780. CancelStockId: cancel.ID,
  6781. CancelOrderNumber: cancel.OrderNumber,
  6782. Manufacturer: manufacturer.ID,
  6783. Dealer: 0,
  6784. Creator: warehouseOut.Creater,
  6785. UpdateCreator: 0,
  6786. Status: 1,
  6787. Ctime: time.Now().Unix(),
  6788. Mtime: 0,
  6789. Price: warehouse.Price,
  6790. WarehousingDetailId: warehouse.ID,
  6791. WarehouseOutDetailId: lastOut.ID,
  6792. CancelOutDetailId: cancelInfo.ID,
  6793. ProductDate: warehouse.ProductDate,
  6794. ExpireDate: warehouse.ExpiryDate,
  6795. StorehouseId: goods.StorehouseId,
  6796. BuyPrice: warehouse.Price,
  6797. ProjectId: goods.ProjectId,
  6798. OverCount: sum_count,
  6799. RegisterNumber: warehouse.RegisterNumber,
  6800. OperateTime: time.Now().Unix(),
  6801. }
  6802. if orgID == 9671 || orgID == 10265 {
  6803. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  6804. flow.Price = goodsInfo.PackingPrice
  6805. flow.BuyPrice = goodsInfo.BuyPrice
  6806. }
  6807. CreateStockFlowOne(flow)
  6808. //退库数量增加
  6809. UpdateSumAddCancelCount(orgID, goods.GoodId, goods.StorehouseId, flow.Count)
  6810. //实际出库减少
  6811. ModifyAddGoodSumCount(goods.StorehouseId, flow.Count, orgID, goods.GoodId)
  6812. }
  6813. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count)
  6814. if errWarehouse != nil {
  6815. goodErrcode := models.XtGoodErrcode{
  6816. UserOrgId: orgID,
  6817. Errcode: "添加入库ID报错",
  6818. GoodId: goods.GoodId,
  6819. Status: 1,
  6820. Ctime: time.Now().Unix(),
  6821. Mtime: 0,
  6822. Count: 0,
  6823. StockCount: 0,
  6824. Creater: creator,
  6825. BatchNumberId: warehouse.ID,
  6826. WarehouseOutId: 0,
  6827. }
  6828. CreateGoodErrcode(goodErrcode)
  6829. }
  6830. }
  6831. goods.Count = deliver_number - stock_number
  6832. prepare := &models.DialysisBeforePrepare{
  6833. Count: deliver_number - stock_number,
  6834. GoodTypeId: goods.GoodTypeId,
  6835. GoodId: goods.GoodId,
  6836. PatientId: goods.PatientId,
  6837. RecordDate: goods.RecordDate,
  6838. UserOrgId: orgID,
  6839. Ctime: time.Now().Unix(),
  6840. Creater: creator,
  6841. Status: 1,
  6842. StorehouseId: goods.StorehouseId,
  6843. ProjectId: goods.ProjectId,
  6844. }
  6845. fmt.Println("剩余出库数量", prepare.Count)
  6846. ConsumablePrescriptionDelivery(orgID, patient_id, record_time, prepare, warehouseOut, creator, count)
  6847. return nil
  6848. }
  6849. func CreateAutoDetail(detail models.AutomaticReduceDetail) error {
  6850. err := XTWriteDB().Create(&detail).Error
  6851. return err
  6852. }
  6853. func CreateNewAutoDetail(detail models.AutomaticReduceDetail, tx *gorm.DB) error {
  6854. err := tx.Create(&detail).Error
  6855. if err != nil {
  6856. tx.Rollback()
  6857. return err
  6858. }
  6859. return err
  6860. }
  6861. func UpdateAutomaticReduce(patient_id int64, record_date int64, good_id int64, project_id int64) error {
  6862. err := XTWriteDB().Model(&models.AutomaticReduceDetail{}).Where("patient_id = ? and record_time =? and good_id = ? and project_id = ? and status = 1", patient_id, record_date, good_id, project_id).Update(map[string]interface{}{"status": 0}).Error
  6863. return err
  6864. }
  6865. func UpdateNewAutomaticReduce(patient_id int64, record_date int64, good_id int64, project_id int64, tx *gorm.DB) error {
  6866. err := tx.Model(&models.AutomaticReduceDetail{}).Where("patient_id = ? and record_time =? and good_id = ? and project_id = ? and status = 1", patient_id, record_date, good_id, project_id).Update(map[string]interface{}{"status": 0}).Error
  6867. if err != nil {
  6868. tx.Rollback()
  6869. return err
  6870. }
  6871. return err
  6872. }
  6873. func GetDrugFLowByAdviceById(drug_id int64, patient_id int64, user_org_id int64, advice_date int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  6874. err = XTReadDB().Where("drug_id = ? and patient_id = ? and org_id = ? and sys_record_time = ? and status= 1", drug_id, patient_id, user_org_id, advice_date).Find(&outInfo).Error
  6875. return outInfo, err
  6876. }
  6877. func GetDrugFLowByAdviceByIdOne(drug_id int64, patient_id int64, user_org_id int64, advice_date int64, tx *gorm.DB) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  6878. err = tx.Where("drug_id = ? and patient_id = ? and org_id = ? and sys_record_time = ? and status= 1", drug_id, patient_id, user_org_id, advice_date).Find(&outInfo).Error
  6879. if err != gorm.ErrRecordNotFound {
  6880. if err != nil {
  6881. tx.Rollback()
  6882. return outInfo, err
  6883. }
  6884. }
  6885. return outInfo, err
  6886. }
  6887. func UpdateHisAdviceById(id int64) error {
  6888. err := XTWriteDB().Model(&models.HisDoctorAdviceInfo{}).Where("id = ?", id).Updates(map[string]interface{}{"execution_time": "", "execution_staff": "", "execution_state": 2}).Error
  6889. return err
  6890. }
  6891. func UpdateHisAdviceByIdOne(id int64, tx *gorm.DB) error {
  6892. err := tx.Model(&models.HisDoctorAdviceInfo{}).Where("id = ?", id).Updates(map[string]interface{}{"execution_time": "", "execution_staff": "", "execution_state": 2}).Error
  6893. if err != nil {
  6894. tx.Rollback()
  6895. return err
  6896. }
  6897. return err
  6898. }
  6899. func GetAdviceIsOut(advice_id int64, user_org_id int64, patient_id int64, advice_date int64, drug_id int64) (models.DrugWarehouseOutInfo, error) {
  6900. advice := models.DrugWarehouseOutInfo{}
  6901. err := XTReadDB().Where("advice_id = ? and org_id = ? and patient_id = ? and sys_record_time = ? and status=1 and drug_id = ?", advice_id, user_org_id, patient_id, advice_date, drug_id).Find(&advice).Error
  6902. return advice, err
  6903. }
  6904. // 药品出库
  6905. func NewHisDrugsDelivery(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo, tx *gorm.DB) (err error) {
  6906. // 1.判断药品是否来自专用字典的药品库
  6907. // 2.判断当天当前机构有没有创建出库单,没有则创建
  6908. // 3.创建出库流程
  6909. // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  6910. isHasWay := false
  6911. record_time := int64(0)
  6912. isHasWay = true
  6913. record_time = advice.RecordDate
  6914. if isHasWay {
  6915. //判断当天当前机构有没有创建出库单,没有则创建
  6916. out, err := FindNewDrugStockOutByIsSys(orgID, 1, record_time, tx)
  6917. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6918. if err == gorm.ErrRecordNotFound {
  6919. timeStr := time.Now().Format("2006-01-02")
  6920. timeArr := strings.Split(timeStr, "-")
  6921. total, _ := FindAllDrugWarehouseOut(orgID)
  6922. total = total + 1
  6923. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  6924. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  6925. number = number + total
  6926. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  6927. warehouseOut := models.DrugWarehouseOut{
  6928. WarehouseOutOrderNumber: warehousing_out_order,
  6929. OperationTime: time.Now().Unix(),
  6930. OrgId: orgID,
  6931. Creater: creater,
  6932. Ctime: time.Now().Unix(),
  6933. Status: 1,
  6934. WarehouseOutTime: record_time,
  6935. Dealer: 0,
  6936. Manufacturer: 0,
  6937. Type: 1,
  6938. IsSys: 1,
  6939. StorehouseId: houseConfig.DrugStorehouseOut,
  6940. IsCheck: 1,
  6941. }
  6942. err := AddNewSigleDrugWarehouseOut(&warehouseOut, tx)
  6943. if err != nil {
  6944. utils.TraceLog("创建出库单失败 err = %v", err)
  6945. return err
  6946. } else {
  6947. out = warehouseOut
  6948. }
  6949. }
  6950. //出库流程
  6951. //1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  6952. drup, _ := FindNewBaseDrugLibRecord(orgID, advice.DrugId, tx)
  6953. if drup.ID > 0 {
  6954. prescribingNumber := advice.PrescribingNumber
  6955. HisStoryBloodHisDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice, prescribingNumber, advice.PrescribingNumberUnit, tx)
  6956. } else {
  6957. return errors.New("药品信息不存在")
  6958. }
  6959. }
  6960. return
  6961. }
  6962. // 保存处方出库
  6963. func ConsumableNewPrescriptionDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, creator int64, count int64, tx *gorm.DB) (err error) {
  6964. var deliver_number int64 = 0
  6965. var stock_number int64 = 0
  6966. var maxNumber int64 = 0
  6967. deliver_number = goods.Count
  6968. // 根据先进先出原则,查询最先入库的批次,进行出库
  6969. // 如果没有对应的库存,则报错
  6970. warehouse, err := FindNewFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId, tx)
  6971. if err != nil {
  6972. goodErrcode := models.XtGoodErrcode{
  6973. UserOrgId: goods.UserOrgId,
  6974. Errcode: "查询库存信息报错",
  6975. GoodId: goods.GoodId,
  6976. Status: 1,
  6977. Ctime: time.Now().Unix(),
  6978. Mtime: 0,
  6979. Count: 0,
  6980. StockCount: 0,
  6981. Creater: creator,
  6982. BatchNumberId: warehouse.ID,
  6983. WarehouseOutId: 0,
  6984. }
  6985. CreateGoodErrcode(goodErrcode)
  6986. return err
  6987. }
  6988. stock_number = warehouse.StockCount
  6989. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6990. if stock_number >= deliver_number {
  6991. maxNumber = goods.Count
  6992. warehouse.StockCount = warehouse.StockCount - maxNumber
  6993. warehouse.Mtime = time.Now().Unix()
  6994. //扣减库存
  6995. errThree := UpDateNewWarehouseInfoByStock(&warehouse, tx)
  6996. if errThree != nil {
  6997. goodErrcode := models.XtGoodErrcode{
  6998. UserOrgId: goods.UserOrgId,
  6999. Errcode: "扣减库存失败",
  7000. GoodId: goods.GoodId,
  7001. Status: 1,
  7002. Ctime: time.Now().Unix(),
  7003. Mtime: 0,
  7004. Count: 0,
  7005. StockCount: 0,
  7006. Creater: creator,
  7007. BatchNumberId: warehouse.ID,
  7008. WarehouseOutId: 0,
  7009. }
  7010. CreateGoodErrcode(goodErrcode)
  7011. return errThree
  7012. }
  7013. //查询剩余库存
  7014. goodList, _ := GetNewAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId, tx)
  7015. var sum_count int64
  7016. for _, item := range goodList {
  7017. sum_count += item.StockCount
  7018. }
  7019. //查询已经出库的数据
  7020. flowGood, _ := GetNewStockFlowIsBatchNumberSix(patient_id, record_time, goods.GoodId, goods.ProjectId, tx)
  7021. var out_count int64
  7022. var out_count_one int64
  7023. for _, item := range flowGood {
  7024. out_count += item.Count
  7025. }
  7026. //查询退库数据
  7027. flowGoodTwo, _ := GetNewStockFlowIsBatchNumberFour(patient_id, record_time, goods.GoodId, goods.ProjectId, tx)
  7028. for _, item := range flowGoodTwo {
  7029. out_count_one += item.Count
  7030. }
  7031. var out_count_two int64
  7032. wareOutList, _ := GetNewStockFlowIsBatchNumberSixty(patient_id, record_time, goods.GoodId, goods.ProjectId, tx)
  7033. for _, item := range wareOutList {
  7034. out_count_two += item.Count
  7035. }
  7036. OutInfo := &models.WarehouseOutInfo{
  7037. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7038. WarehouseOutId: warehouseOut.ID,
  7039. WarehouseInfotId: warehouse.ID,
  7040. Status: 1,
  7041. Ctime: time.Now().Unix(),
  7042. Remark: warehouse.Remark,
  7043. OrgId: orgID,
  7044. Type: 1,
  7045. Manufacturer: warehouse.Manufacturer,
  7046. Dealer: warehouse.Dealer,
  7047. IsSys: 1,
  7048. SysRecordTime: record_time,
  7049. GoodTypeId: goods.GoodTypeId,
  7050. GoodId: goods.GoodId,
  7051. PatientId: patient_id,
  7052. Number: warehouse.Number,
  7053. LicenseNumber: warehouse.LicenseNumber,
  7054. Price: warehouse.PackingPrice,
  7055. ExpiryDate: warehouse.ExpiryDate,
  7056. ProductDate: warehouse.ProductDate,
  7057. ProjectId: goods.ProjectId,
  7058. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7059. StorehouseId: goods.StorehouseId,
  7060. IsCheck: 1,
  7061. OverCount: sum_count,
  7062. RegisterNumber: warehouse.RegisterNumber,
  7063. }
  7064. OutInfo.Count = count - out_count_two + out_count_one
  7065. _, errcodes := GetNewNewWarehouseOutInfoIsExistSix(goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID, tx)
  7066. if errcodes == gorm.ErrRecordNotFound {
  7067. errOne := AddNewSigleWarehouseOutInfo(OutInfo, tx)
  7068. if errOne != nil {
  7069. return errOne
  7070. }
  7071. } else if errcodes == nil {
  7072. outInfoOne, _ := GetNewWarehouseOutInfoIsExistThree(goods.GoodId, patient_id, record_time, goods.ProjectId, tx)
  7073. if count != outInfoOne.Count {
  7074. UpdatedNewWarehouseOutInfo(OutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId, tx)
  7075. }
  7076. }
  7077. lastOut, _ := FindNewWarehouseOutInfoByPatientIdTwo(patient_id, record_time, goods.GoodId, orgID, goods.ProjectId, tx)
  7078. //如果本次出库数据大于历史出库数据 新增1条流水
  7079. fmt.Println("本次出库数据", count)
  7080. fmt.Println("历史出库数据", out_count)
  7081. fmt.Println("退库数据", out_count_one)
  7082. if count > (out_count - out_count_one) {
  7083. stockFlow := models.VmStockFlow{
  7084. WarehousingId: warehouse.ID,
  7085. GoodId: goods.GoodId,
  7086. Number: warehouse.Number,
  7087. LicenseNumber: warehouse.LicenseNumber,
  7088. Count: count - out_count + out_count_one,
  7089. UserOrgId: orgID,
  7090. PatientId: goods.PatientId,
  7091. SystemTime: record_time,
  7092. ConsumableType: 3,
  7093. IsSys: 1,
  7094. WarehousingOrder: warehouse.WarehousingOrder,
  7095. WarehouseOutId: warehouseOut.ID,
  7096. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7097. IsEdit: 1,
  7098. CancelStockId: 0,
  7099. CancelOrderNumber: "",
  7100. Manufacturer: warehouse.Manufacturer,
  7101. Dealer: warehouse.Dealer,
  7102. Creator: creator,
  7103. UpdateCreator: 0,
  7104. Status: 1,
  7105. Ctime: time.Now().Unix(),
  7106. Mtime: 0,
  7107. Price: warehouse.PackingPrice, //零售价
  7108. WarehousingDetailId: warehouse.ID,
  7109. WarehouseOutDetailId: goods.ID,
  7110. CancelOutDetailId: 0,
  7111. ProductDate: warehouse.ProductDate,
  7112. ExpireDate: warehouse.ExpiryDate,
  7113. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7114. StorehouseId: warehouse.StorehouseId,
  7115. AdminUserId: creator,
  7116. BuyPrice: warehouse.Price, //进货价
  7117. StockCount: "",
  7118. BatchNumberCount: warehouse.StockCount - goods.Count,
  7119. IsCheck: 1,
  7120. OverCount: sum_count,
  7121. RegisterNumber: warehouse.RegisterNumber,
  7122. ProjectId: goods.ProjectId,
  7123. OperateTime: time.Now().Unix(),
  7124. }
  7125. //创建出库流水
  7126. errflow := CreateNewStockFlowOne(stockFlow, tx)
  7127. if errflow != nil {
  7128. goodErrcode := models.XtGoodErrcode{
  7129. UserOrgId: orgID,
  7130. Errcode: "创建流水报错",
  7131. GoodId: goods.GoodId,
  7132. Status: 1,
  7133. Ctime: time.Now().Unix(),
  7134. Mtime: 0,
  7135. Count: 0,
  7136. StockCount: 0,
  7137. Creater: creator,
  7138. BatchNumberId: warehouse.ID,
  7139. WarehouseOutId: 0,
  7140. }
  7141. CreateGoodErrcode(goodErrcode)
  7142. }
  7143. //更新出库数量
  7144. errsumcode := ModifyNewGoodSumCount(goods.StorehouseId, stockFlow.Count, orgID, goods.GoodId, tx)
  7145. if errsumcode != nil {
  7146. goodErrcode := models.XtGoodErrcode{
  7147. UserOrgId: orgID,
  7148. Errcode: "更新出库数量报错",
  7149. GoodId: goods.GoodId,
  7150. Status: 1,
  7151. Ctime: time.Now().Unix(),
  7152. Mtime: 0,
  7153. Count: 0,
  7154. StockCount: 0,
  7155. Creater: creator,
  7156. BatchNumberId: warehouse.ID,
  7157. WarehouseOutId: 0,
  7158. }
  7159. CreateGoodErrcode(goodErrcode)
  7160. }
  7161. }
  7162. //如果本次出库数据小于历史出库数据 新增1条退库流水
  7163. if count < (out_count - out_count_one) {
  7164. operation_time := time.Now().Unix()
  7165. //创建退库单
  7166. timeStr := time.Now().Format("2006-01-02")
  7167. timeArr := strings.Split(timeStr, "-")
  7168. total, _ := FindNewAllCancelStockTotal(orgID, tx)
  7169. total = total + 1
  7170. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  7171. cancelStock := models.CancelStock{
  7172. OrderNumber: orderNumber,
  7173. OperaTime: operation_time,
  7174. OrgId: orgID,
  7175. Creater: warehouseOut.Creater,
  7176. Ctime: time.Now().Unix(),
  7177. Status: 1,
  7178. ReturnTime: record_time,
  7179. Type: 1,
  7180. StorehouseId: goods.StorehouseId,
  7181. IsCheck: 1,
  7182. }
  7183. _, msgerrkonde := GetNewCancelStockDetailByOrderNumberOne(record_time, orgID, tx)
  7184. if msgerrkonde == gorm.ErrRecordNotFound {
  7185. AddNewSigleCancelStock(&cancelStock, tx)
  7186. }
  7187. cancel, _ := GetNewLastCancelStockById(orgID, tx)
  7188. manufacturer, _ := GetNewManufactureById(warehouse.Manufacturer, tx)
  7189. deaerler, _ := GetNewDealerById(warehouse.Dealer, tx)
  7190. cancelStockInfo := models.CancelStockInfo{
  7191. GoodId: goods.GoodId,
  7192. CancelStockId: cancel.ID,
  7193. GoodTypeId: goods.GoodTypeId,
  7194. Count: out_count - out_count_one - count,
  7195. Price: warehouse.PackingPrice,
  7196. Total: 0,
  7197. ProductDate: warehouse.ProductDate,
  7198. ExpiryDate: warehouse.ExpiryDate,
  7199. Ctime: time.Now().Unix(),
  7200. Status: 1,
  7201. OrgId: orgID,
  7202. OrderNumber: cancel.OrderNumber,
  7203. Type: 0,
  7204. Dealer: deaerler.DealerName,
  7205. Manufacturer: manufacturer.ManufacturerName,
  7206. Number: warehouse.Number,
  7207. RegisterAccount: "",
  7208. Remark: "",
  7209. WarehouseInfoId: warehouse.ID,
  7210. PatientId: patient_id,
  7211. RecordDate: record_time,
  7212. StorehouseId: goods.StorehouseId,
  7213. IsCheck: 1,
  7214. }
  7215. if orgID == 9671 || orgID == 10265 {
  7216. goodsInfo, _ := FindeNewGoodInfo(orgID, goods.ProjectId, tx)
  7217. cancelStockInfo.Price = goodsInfo.PackingPrice
  7218. }
  7219. CreateNewCancelStockInfoOne(&cancelStockInfo, tx)
  7220. cancelInfo, _ := GetLastNewCancelStockInfoByGoodId(goods.GoodId, tx)
  7221. flow := models.VmStockFlow{
  7222. WarehousingId: warehouse.ID,
  7223. GoodId: goods.GoodId,
  7224. Number: warehouse.Number,
  7225. LicenseNumber: warehouse.LicenseNumber,
  7226. Count: out_count - out_count_one - count,
  7227. UserOrgId: orgID,
  7228. PatientId: patient_id,
  7229. SystemTime: record_time,
  7230. ConsumableType: 7,
  7231. IsSys: 1,
  7232. WarehousingOrder: "",
  7233. WarehouseOutId: lastOut.WarehouseOutId,
  7234. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  7235. IsEdit: 0,
  7236. CancelStockId: cancel.ID,
  7237. CancelOrderNumber: cancel.OrderNumber,
  7238. Manufacturer: manufacturer.ID,
  7239. Dealer: 0,
  7240. Creator: warehouseOut.Creater,
  7241. UpdateCreator: 0,
  7242. Status: 1,
  7243. Ctime: time.Now().Unix(),
  7244. Mtime: 0,
  7245. Price: warehouse.PackingPrice,
  7246. WarehousingDetailId: warehouse.ID,
  7247. WarehouseOutDetailId: lastOut.ID,
  7248. CancelOutDetailId: cancelInfo.ID,
  7249. ProductDate: warehouse.ProductDate,
  7250. ExpireDate: warehouse.ExpiryDate,
  7251. StorehouseId: goods.StorehouseId,
  7252. BuyPrice: warehouse.Price,
  7253. ProjectId: goods.ProjectId,
  7254. OverCount: sum_count,
  7255. RegisterNumber: warehouse.RegisterNumber,
  7256. OperateTime: time.Now().Unix(),
  7257. }
  7258. if orgID == 9671 || orgID == 10265 {
  7259. goodsInfo, _ := FindeNewGoodInfo(orgID, goods.ProjectId, tx)
  7260. flow.Price = goodsInfo.PackingPrice
  7261. flow.BuyPrice = goodsInfo.BuyPrice
  7262. }
  7263. CreateNewStockFlowOne(flow, tx)
  7264. //退库数量增加
  7265. UpdateNewSumAddCancelCount(orgID, goods.GoodId, goods.StorehouseId, flow.Count, tx)
  7266. ModifyNewAddGoodSumCount(goods.StorehouseId, flow.Count, orgID, goods.GoodId, tx)
  7267. }
  7268. //添加入库ID
  7269. errWarehouse := UpdateNewGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count, tx)
  7270. if errWarehouse != nil {
  7271. goodErrcode := models.XtGoodErrcode{
  7272. UserOrgId: orgID,
  7273. Errcode: "添加入库ID报错",
  7274. GoodId: goods.GoodId,
  7275. Status: 1,
  7276. Ctime: time.Now().Unix(),
  7277. Mtime: 0,
  7278. Count: 0,
  7279. StockCount: 0,
  7280. Creater: creator,
  7281. BatchNumberId: warehouse.ID,
  7282. WarehouseOutId: 0,
  7283. }
  7284. CreateGoodErrcode(goodErrcode)
  7285. }
  7286. if warehouse.StockCount < maxNumber {
  7287. goodErrcode := models.XtGoodErrcode{
  7288. UserOrgId: orgID,
  7289. Errcode: "出库接口库存数量不足",
  7290. GoodId: goods.GoodId,
  7291. Status: 1,
  7292. Ctime: time.Now().Unix(),
  7293. Mtime: 0,
  7294. Count: 0,
  7295. StockCount: 0,
  7296. Creater: creator,
  7297. BatchNumberId: warehouse.ID,
  7298. WarehouseOutId: 0,
  7299. }
  7300. CreateGoodErrcode(goodErrcode)
  7301. return errors.New("库存数量不足")
  7302. }
  7303. return nil
  7304. } else {
  7305. // 出库完成后,要将该批次库存清零
  7306. warehouse.StockCount = 0
  7307. warehouse.Mtime = time.Now().Unix()
  7308. errThree := UpDateNewWarehouseInfoByStock(&warehouse, tx)
  7309. if errThree != nil {
  7310. goodErrcode := models.XtGoodErrcode{
  7311. UserOrgId: orgID,
  7312. Errcode: "扣减库存报错",
  7313. GoodId: goods.GoodId,
  7314. Status: 1,
  7315. Ctime: time.Now().Unix(),
  7316. Mtime: 0,
  7317. Count: 0,
  7318. StockCount: 0,
  7319. Creater: creator,
  7320. BatchNumberId: warehouse.ID,
  7321. WarehouseOutId: 0,
  7322. }
  7323. CreateGoodErrcode(goodErrcode)
  7324. return errThree
  7325. }
  7326. //查询剩余库存
  7327. goodList, _ := GetNewAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId, tx)
  7328. var sum_count int64
  7329. for _, item := range goodList {
  7330. sum_count += item.StockCount
  7331. }
  7332. fmt.Println("剩余库存", sum_count)
  7333. OutInfo := &models.WarehouseOutInfo{
  7334. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7335. WarehouseOutId: warehouseOut.ID,
  7336. WarehouseInfotId: warehouse.ID,
  7337. Status: 1,
  7338. Ctime: time.Now().Unix(),
  7339. Remark: warehouse.Remark,
  7340. OrgId: orgID,
  7341. Type: 1,
  7342. Manufacturer: warehouse.Manufacturer,
  7343. Dealer: warehouse.Dealer,
  7344. IsSys: 1,
  7345. SysRecordTime: record_time,
  7346. GoodTypeId: goods.GoodTypeId,
  7347. GoodId: goods.GoodId,
  7348. PatientId: patient_id,
  7349. Number: warehouse.Number,
  7350. LicenseNumber: warehouse.LicenseNumber,
  7351. Price: warehouse.PackingPrice,
  7352. ExpiryDate: warehouse.ExpiryDate,
  7353. ProductDate: warehouse.ProductDate,
  7354. ProjectId: goods.ProjectId,
  7355. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7356. StorehouseId: goods.StorehouseId,
  7357. IsCheck: 1,
  7358. OverCount: sum_count,
  7359. RegisterNumber: warehouse.RegisterNumber,
  7360. }
  7361. OutInfo.Count = stock_number
  7362. _, errcodes := GetNewNewWarehouseOutInfoIsExistSix(goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID, tx)
  7363. if errcodes == gorm.ErrRecordNotFound {
  7364. errOne := AddNewSigleWarehouseOutInfo(OutInfo, tx)
  7365. if errOne != nil {
  7366. return errOne
  7367. }
  7368. } else if errcodes == nil {
  7369. outInfoOne, _ := GetNewWarehouseOutInfoIsExistTen(goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID, tx)
  7370. if count != outInfoOne.Count {
  7371. UpdatedNewWarehouseOutInfoSix(OutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID, tx)
  7372. }
  7373. }
  7374. lastOut, _ := FindNewWarehouseOutInfoByPatientIdTwo(patient_id, record_time, goods.GoodId, orgID, goods.ProjectId, tx)
  7375. //查询该该批次已经出库的数据
  7376. flowGood, _ := GetNewStockFlowIsBatchNumberSix(patient_id, record_time, goods.GoodId, goods.ProjectId, tx)
  7377. var out_count int64
  7378. var out_count_one int64
  7379. for _, item := range flowGood {
  7380. out_count += item.Count
  7381. }
  7382. flowGoodTwo, _ := GetNewStockFlowIsBatchNumberFour(patient_id, record_time, goods.GoodId, goods.ProjectId, tx)
  7383. for _, item := range flowGoodTwo {
  7384. out_count_one += item.Count
  7385. }
  7386. //如果出库数量 大于 历史出库数据 新增1条流水
  7387. if count > out_count-out_count_one {
  7388. stockFlow := models.VmStockFlow{
  7389. WarehousingId: warehouse.ID,
  7390. GoodId: goods.GoodId,
  7391. Number: warehouse.Number,
  7392. LicenseNumber: warehouse.LicenseNumber,
  7393. Count: stock_number,
  7394. UserOrgId: orgID,
  7395. PatientId: goods.PatientId,
  7396. SystemTime: record_time,
  7397. ConsumableType: 3,
  7398. IsSys: 1,
  7399. WarehousingOrder: warehouse.WarehousingOrder,
  7400. WarehouseOutId: warehouseOut.ID,
  7401. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7402. IsEdit: 1,
  7403. CancelStockId: 0,
  7404. CancelOrderNumber: "",
  7405. Manufacturer: warehouse.Manufacturer,
  7406. Dealer: warehouse.Dealer,
  7407. Creator: creator,
  7408. UpdateCreator: 0,
  7409. Status: 1,
  7410. Ctime: time.Now().Unix(),
  7411. Mtime: 0,
  7412. Price: warehouse.PackingPrice, //零售价
  7413. WarehousingDetailId: warehouse.ID,
  7414. WarehouseOutDetailId: goods.ID,
  7415. CancelOutDetailId: 0,
  7416. ProductDate: warehouse.ProductDate,
  7417. ExpireDate: warehouse.ExpiryDate,
  7418. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7419. StorehouseId: warehouse.StorehouseId,
  7420. AdminUserId: creator,
  7421. BuyPrice: warehouse.Price, //进货价
  7422. StockCount: "",
  7423. BatchNumberCount: 0,
  7424. IsCheck: 1,
  7425. OverCount: sum_count,
  7426. RegisterNumber: warehouse.RegisterNumber,
  7427. ProjectId: goods.ProjectId,
  7428. OperateTime: time.Now().Unix(),
  7429. }
  7430. //创建出库流水
  7431. errflow := CreateNewStockFlowOne(stockFlow, tx)
  7432. if errflow != nil {
  7433. goodErrcode := models.XtGoodErrcode{
  7434. UserOrgId: orgID,
  7435. Errcode: "创建出库流水报错",
  7436. GoodId: goods.GoodId,
  7437. Status: 1,
  7438. Ctime: time.Now().Unix(),
  7439. Mtime: 0,
  7440. Count: 0,
  7441. StockCount: 0,
  7442. Creater: creator,
  7443. BatchNumberId: warehouse.ID,
  7444. WarehouseOutId: 0,
  7445. }
  7446. CreateGoodErrcode(goodErrcode)
  7447. }
  7448. //更新出库数量
  7449. errcodecout := ModifyNewGoodSumCount(goods.StorehouseId, stock_number, orgID, goods.GoodId, tx)
  7450. if errcodecout != nil {
  7451. goodErrcode := models.XtGoodErrcode{
  7452. UserOrgId: orgID,
  7453. Errcode: "更新出库数量报错",
  7454. GoodId: goods.GoodId,
  7455. Status: 1,
  7456. Ctime: time.Now().Unix(),
  7457. Mtime: 0,
  7458. Count: 0,
  7459. StockCount: 0,
  7460. Creater: creator,
  7461. BatchNumberId: warehouse.ID,
  7462. WarehouseOutId: 0,
  7463. }
  7464. CreateGoodErrcode(goodErrcode)
  7465. }
  7466. }
  7467. //退库
  7468. if count < out_count-out_count_one {
  7469. operation_time := time.Now().Unix()
  7470. //创建退库单
  7471. timeStr := time.Now().Format("2006-01-02")
  7472. timeArr := strings.Split(timeStr, "-")
  7473. total, _ := FindNewAllCancelStockTotal(orgID, tx)
  7474. total = total + 1
  7475. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  7476. cancelStock := models.CancelStock{
  7477. OrderNumber: orderNumber,
  7478. OperaTime: operation_time,
  7479. OrgId: orgID,
  7480. Creater: warehouseOut.Creater,
  7481. Ctime: time.Now().Unix(),
  7482. Status: 1,
  7483. ReturnTime: record_time,
  7484. Type: 1,
  7485. StorehouseId: goods.StorehouseId,
  7486. IsCheck: 1,
  7487. }
  7488. _, msgerrkonde := GetNewCancelStockDetailByOrderNumberOne(record_time, orgID, tx)
  7489. if msgerrkonde == gorm.ErrRecordNotFound {
  7490. AddNewSigleCancelStock(&cancelStock, tx)
  7491. }
  7492. cancel, _ := GetNewLastCancelStockById(orgID, tx)
  7493. manufacturer, _ := GetNewManufactureById(warehouse.Manufacturer, tx)
  7494. deaerler, _ := GetNewDealerById(warehouse.Dealer, tx)
  7495. cancelStockInfo := models.CancelStockInfo{
  7496. GoodId: goods.GoodId,
  7497. CancelStockId: cancel.ID,
  7498. GoodTypeId: goods.GoodTypeId,
  7499. Count: out_count - out_count_one - count,
  7500. Price: warehouse.PackingPrice,
  7501. Total: 0,
  7502. ProductDate: warehouse.ProductDate,
  7503. ExpiryDate: warehouse.ExpiryDate,
  7504. Ctime: time.Now().Unix(),
  7505. Status: 1,
  7506. OrgId: orgID,
  7507. OrderNumber: cancel.OrderNumber,
  7508. Type: 0,
  7509. Dealer: deaerler.DealerName,
  7510. Manufacturer: manufacturer.ManufacturerName,
  7511. Number: warehouse.Number,
  7512. RegisterAccount: "",
  7513. Remark: "",
  7514. WarehouseInfoId: warehouse.ID,
  7515. PatientId: patient_id,
  7516. RecordDate: record_time,
  7517. StorehouseId: goods.StorehouseId,
  7518. IsCheck: 1,
  7519. }
  7520. if orgID == 9671 || orgID == 10265 {
  7521. goodsInfo, _ := FindeNewGoodInfo(orgID, goods.ProjectId, tx)
  7522. cancelStockInfo.Price = goodsInfo.PackingPrice
  7523. }
  7524. CreateNewCancelStockInfoOne(&cancelStockInfo, tx)
  7525. cancelInfo, _ := GetLastNewCancelStockInfoByGoodId(goods.GoodId, tx)
  7526. flow := models.VmStockFlow{
  7527. WarehousingId: warehouse.ID,
  7528. GoodId: goods.GoodId,
  7529. Number: warehouse.Number,
  7530. LicenseNumber: warehouse.LicenseNumber,
  7531. Count: out_count - count,
  7532. UserOrgId: orgID,
  7533. PatientId: patient_id,
  7534. SystemTime: record_time,
  7535. ConsumableType: 7,
  7536. IsSys: 1,
  7537. WarehousingOrder: "",
  7538. WarehouseOutId: lastOut.WarehouseOutId,
  7539. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  7540. IsEdit: 0,
  7541. CancelStockId: cancel.ID,
  7542. CancelOrderNumber: cancel.OrderNumber,
  7543. Manufacturer: manufacturer.ID,
  7544. Dealer: 0,
  7545. Creator: warehouseOut.Creater,
  7546. UpdateCreator: 0,
  7547. Status: 1,
  7548. Ctime: time.Now().Unix(),
  7549. Mtime: 0,
  7550. Price: warehouse.Price,
  7551. WarehousingDetailId: warehouse.ID,
  7552. WarehouseOutDetailId: lastOut.ID,
  7553. CancelOutDetailId: cancelInfo.ID,
  7554. ProductDate: warehouse.ProductDate,
  7555. ExpireDate: warehouse.ExpiryDate,
  7556. StorehouseId: goods.StorehouseId,
  7557. BuyPrice: warehouse.Price,
  7558. ProjectId: goods.ProjectId,
  7559. OverCount: sum_count,
  7560. RegisterNumber: warehouse.RegisterNumber,
  7561. OperateTime: time.Now().Unix(),
  7562. }
  7563. if orgID == 9671 || orgID == 10265 {
  7564. goodsInfo, _ := FindeNewGoodInfo(orgID, goods.ProjectId, tx)
  7565. flow.Price = goodsInfo.PackingPrice
  7566. flow.BuyPrice = goodsInfo.BuyPrice
  7567. }
  7568. CreateNewStockFlowOne(flow, tx)
  7569. //退库数量增加
  7570. UpdateNewSumAddCancelCount(orgID, goods.GoodId, goods.StorehouseId, flow.Count, tx)
  7571. //实际出库减少
  7572. ModifyNewAddGoodSumCount(goods.StorehouseId, flow.Count, orgID, goods.GoodId, tx)
  7573. }
  7574. errWarehouse := UpdateNewGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count, tx)
  7575. if errWarehouse != nil {
  7576. goodErrcode := models.XtGoodErrcode{
  7577. UserOrgId: orgID,
  7578. Errcode: "添加入库ID报错",
  7579. GoodId: goods.GoodId,
  7580. Status: 1,
  7581. Ctime: time.Now().Unix(),
  7582. Mtime: 0,
  7583. Count: 0,
  7584. StockCount: 0,
  7585. Creater: creator,
  7586. BatchNumberId: warehouse.ID,
  7587. WarehouseOutId: 0,
  7588. }
  7589. CreateGoodErrcode(goodErrcode)
  7590. }
  7591. }
  7592. goods.Count = deliver_number - stock_number
  7593. prepare := &models.DialysisBeforePrepare{
  7594. Count: deliver_number - stock_number,
  7595. GoodTypeId: goods.GoodTypeId,
  7596. GoodId: goods.GoodId,
  7597. PatientId: goods.PatientId,
  7598. RecordDate: goods.RecordDate,
  7599. UserOrgId: orgID,
  7600. Ctime: time.Now().Unix(),
  7601. Creater: creator,
  7602. Status: 1,
  7603. StorehouseId: goods.StorehouseId,
  7604. ProjectId: goods.ProjectId,
  7605. }
  7606. ConsumableNewPrescriptionDelivery(orgID, patient_id, record_time, prepare, warehouseOut, creator, count, tx)
  7607. return nil
  7608. }
  7609. // 保存处方出库
  7610. func ConsumableNewHisPrescriptionDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, creator int64, tx *gorm.DB) (err error) {
  7611. var deliver_number int64 = 0
  7612. var stock_number int64 = 0
  7613. var maxNumber int64 = 0
  7614. deliver_number = goods.Count
  7615. // 根据先进先出原则,查询最先入库的批次,进行出库
  7616. // 如果没有对应的库存,则报错
  7617. warehouse, _ := FindNewFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId, tx)
  7618. stock_number = warehouse.StockCount
  7619. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  7620. if stock_number >= deliver_number {
  7621. maxNumber = goods.Count
  7622. warehouse.StockCount = warehouse.StockCount - maxNumber
  7623. warehouse.Mtime = time.Now().Unix()
  7624. //扣减库存
  7625. UpDateNewWarehouseInfoByStock(&warehouse, tx)
  7626. //查询剩余库存
  7627. goodList, _ := GetNewAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId, tx)
  7628. var sum_count int64
  7629. for _, item := range goodList {
  7630. sum_count += item.StockCount
  7631. }
  7632. //创建出库单
  7633. OutInfo := &models.WarehouseOutInfo{
  7634. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7635. WarehouseOutId: warehouseOut.ID,
  7636. WarehouseInfotId: warehouse.ID,
  7637. Status: 1,
  7638. Ctime: time.Now().Unix(),
  7639. Remark: warehouse.Remark,
  7640. OrgId: orgID,
  7641. Type: 1,
  7642. Manufacturer: warehouse.Manufacturer,
  7643. Dealer: warehouse.Dealer,
  7644. IsSys: 1,
  7645. SysRecordTime: record_time,
  7646. GoodTypeId: goods.GoodTypeId,
  7647. GoodId: goods.GoodId,
  7648. PatientId: patient_id,
  7649. Number: warehouse.Number,
  7650. LicenseNumber: warehouse.LicenseNumber,
  7651. Price: warehouse.PackingPrice,
  7652. ExpiryDate: warehouse.ExpiryDate,
  7653. ProductDate: warehouse.ProductDate,
  7654. ProjectId: goods.ProjectId,
  7655. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7656. StorehouseId: goods.StorehouseId,
  7657. IsCheck: 1,
  7658. OverCount: sum_count,
  7659. RegisterNumber: warehouse.RegisterNumber,
  7660. Count: goods.Count,
  7661. }
  7662. AddNewSigleWarehouseOutInfo(OutInfo, tx)
  7663. stockFlow := models.VmStockFlow{
  7664. WarehousingId: warehouse.ID,
  7665. GoodId: goods.GoodId,
  7666. Number: warehouse.Number,
  7667. LicenseNumber: warehouse.LicenseNumber,
  7668. Count: goods.Count,
  7669. UserOrgId: orgID,
  7670. PatientId: goods.PatientId,
  7671. SystemTime: record_time,
  7672. ConsumableType: 3,
  7673. IsSys: 1,
  7674. WarehousingOrder: warehouse.WarehousingOrder,
  7675. WarehouseOutId: warehouseOut.ID,
  7676. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7677. IsEdit: 1,
  7678. CancelStockId: 0,
  7679. CancelOrderNumber: "",
  7680. Manufacturer: warehouse.Manufacturer,
  7681. Dealer: warehouse.Dealer,
  7682. Creator: creator,
  7683. UpdateCreator: 0,
  7684. Status: 1,
  7685. Ctime: time.Now().Unix(),
  7686. Mtime: 0,
  7687. Price: warehouse.PackingPrice, //零售价
  7688. WarehousingDetailId: warehouse.ID,
  7689. WarehouseOutDetailId: goods.ID,
  7690. CancelOutDetailId: 0,
  7691. ProductDate: warehouse.ProductDate,
  7692. ExpireDate: warehouse.ExpiryDate,
  7693. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7694. StorehouseId: warehouse.StorehouseId,
  7695. AdminUserId: creator,
  7696. BuyPrice: warehouse.Price, //进货价
  7697. StockCount: "",
  7698. BatchNumberCount: warehouse.StockCount - goods.Count,
  7699. IsCheck: 1,
  7700. OverCount: sum_count,
  7701. RegisterNumber: warehouse.RegisterNumber,
  7702. ProjectId: goods.ProjectId,
  7703. OperateTime: time.Now().Unix(),
  7704. }
  7705. //插入流水
  7706. CreateNewStockFlowOne(stockFlow, tx)
  7707. //更新出库数量
  7708. ModifyNewGoodSumCount(goods.StorehouseId, stockFlow.Count, orgID, goods.GoodId, tx)
  7709. goodObj, _ := GetNewGoodInformationByGoodIdThirty(goods.GoodId, tx)
  7710. autoMatic := &models.AutomaticReduceDetail{
  7711. Count: goods.Count,
  7712. GoodTypeId: goodObj.GoodTypeId,
  7713. GoodId: goods.GoodId,
  7714. PatientId: goods.PatientId,
  7715. RecordTime: goods.RecordDate,
  7716. OrgId: orgID,
  7717. Ctime: time.Now().Unix(),
  7718. Status: 1,
  7719. StorehouseId: goods.StorehouseId,
  7720. ProjectId: goods.ProjectId,
  7721. }
  7722. CreatedDialysisPrepare(autoMatic, tx)
  7723. return nil
  7724. } else {
  7725. // 出库完成后,要将该批次库存清零
  7726. warehouse.StockCount = 0
  7727. warehouse.Mtime = time.Now().Unix()
  7728. UpDateNewWarehouseInfoByStock(&warehouse, tx)
  7729. //查询剩余库存
  7730. goodList, _ := GetNewAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId, tx)
  7731. var sum_count int64
  7732. for _, item := range goodList {
  7733. sum_count += item.StockCount
  7734. }
  7735. OutInfo := &models.WarehouseOutInfo{
  7736. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7737. WarehouseOutId: warehouseOut.ID,
  7738. WarehouseInfotId: warehouse.ID,
  7739. Status: 1,
  7740. Ctime: time.Now().Unix(),
  7741. Remark: warehouse.Remark,
  7742. OrgId: orgID,
  7743. Type: 1,
  7744. Manufacturer: warehouse.Manufacturer,
  7745. Dealer: warehouse.Dealer,
  7746. IsSys: 1,
  7747. SysRecordTime: record_time,
  7748. GoodTypeId: goods.GoodTypeId,
  7749. GoodId: goods.GoodId,
  7750. PatientId: patient_id,
  7751. Number: warehouse.Number,
  7752. LicenseNumber: warehouse.LicenseNumber,
  7753. Price: warehouse.PackingPrice,
  7754. ExpiryDate: warehouse.ExpiryDate,
  7755. ProductDate: warehouse.ProductDate,
  7756. ProjectId: goods.ProjectId,
  7757. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7758. StorehouseId: goods.StorehouseId,
  7759. IsCheck: 1,
  7760. OverCount: sum_count,
  7761. RegisterNumber: warehouse.RegisterNumber,
  7762. }
  7763. OutInfo.Count = stock_number
  7764. AddNewSigleWarehouseOutInfo(OutInfo, tx)
  7765. //如果出库数量 大于 历史出库数据 新增1条流水
  7766. stockFlow := models.VmStockFlow{
  7767. WarehousingId: warehouse.ID,
  7768. GoodId: goods.GoodId,
  7769. Number: warehouse.Number,
  7770. LicenseNumber: warehouse.LicenseNumber,
  7771. Count: stock_number,
  7772. UserOrgId: orgID,
  7773. PatientId: goods.PatientId,
  7774. SystemTime: record_time,
  7775. ConsumableType: 3,
  7776. IsSys: 1,
  7777. WarehousingOrder: warehouse.WarehousingOrder,
  7778. WarehouseOutId: warehouseOut.ID,
  7779. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7780. IsEdit: 1,
  7781. CancelStockId: 0,
  7782. CancelOrderNumber: "",
  7783. Manufacturer: warehouse.Manufacturer,
  7784. Dealer: warehouse.Dealer,
  7785. Creator: creator,
  7786. UpdateCreator: 0,
  7787. Status: 1,
  7788. Ctime: time.Now().Unix(),
  7789. Mtime: 0,
  7790. Price: warehouse.PackingPrice, //零售价
  7791. WarehousingDetailId: warehouse.ID,
  7792. WarehouseOutDetailId: goods.ID,
  7793. CancelOutDetailId: 0,
  7794. ProductDate: warehouse.ProductDate,
  7795. ExpireDate: warehouse.ExpiryDate,
  7796. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7797. StorehouseId: warehouse.StorehouseId,
  7798. AdminUserId: creator,
  7799. BuyPrice: warehouse.Price, //进货价
  7800. StockCount: "",
  7801. BatchNumberCount: 0,
  7802. IsCheck: 1,
  7803. OverCount: sum_count,
  7804. RegisterNumber: warehouse.RegisterNumber,
  7805. ProjectId: goods.ProjectId,
  7806. OperateTime: time.Now().Unix(),
  7807. }
  7808. //创建出库流水
  7809. CreateNewStockFlowOne(stockFlow, tx)
  7810. ModifyNewGoodSumCount(goods.StorehouseId, stock_number, orgID, goods.GoodId, tx)
  7811. UpdateNewGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count, tx)
  7812. }
  7813. dialyPrepareOne := &models.DialysisBeforePrepare{
  7814. GoodTypeId: goods.GoodTypeId,
  7815. GoodId: goods.GoodId,
  7816. PatientId: goods.PatientId,
  7817. RecordDate: goods.RecordDate,
  7818. UserOrgId: orgID,
  7819. Count: goods.Count,
  7820. Ctime: time.Now().Unix(),
  7821. Creater: creator,
  7822. Status: 1,
  7823. StorehouseId: goods.StorehouseId,
  7824. ProjectId: goods.ID,
  7825. }
  7826. autoMatic := &models.AutomaticReduceDetail{
  7827. Count: stock_number,
  7828. GoodTypeId: goods.GoodTypeId,
  7829. GoodId: goods.GoodId,
  7830. PatientId: goods.PatientId,
  7831. RecordTime: goods.RecordDate,
  7832. OrgId: orgID,
  7833. Ctime: time.Now().Unix(),
  7834. Status: 1,
  7835. StorehouseId: goods.StorehouseId,
  7836. ProjectId: goods.ProjectId,
  7837. }
  7838. CreatedDialysisPrepare(autoMatic, tx)
  7839. goods.Count = deliver_number - stock_number
  7840. ConsumableNewHisPrescriptionDelivery(orgID, patient_id, record_time, dialyPrepareOne, warehouseOut, creator, tx)
  7841. return nil
  7842. }
  7843. func CreatedDialysisPrepare(prepare *models.AutomaticReduceDetail, tx *gorm.DB) error {
  7844. err := tx.Create(&prepare).Error
  7845. if err != nil {
  7846. tx.Rollback()
  7847. return err
  7848. }
  7849. return err
  7850. }
  7851. func NewHisGoodCancelInfo(orgID int64, creater int64, project *models.HisPrescriptionProject, cha_count int64, good models.GoodInformationThirty, tx *gorm.DB) (err error) {
  7852. storeConfig, _ := GetNewAllStoreHouseConfig(orgID, tx)
  7853. //查找患者最后一条的退库数据是否满足要求
  7854. var out_count int64
  7855. GoodOutInfo, _ := GetGoodWarehouseOutInfoByProjectIdTwo(project.PatientId, project.UserOrgId, project.RecordDate, project.ProjectId, project.ID, tx)
  7856. out_count = GoodOutInfo.Count
  7857. //如果最后1条出库数量大于退库数量,则直接退库,满足退库要求
  7858. if out_count >= cha_count {
  7859. //退库 退回 cha_count 的数量
  7860. ModifyNewGoodWarehouseInfoStockMinNumber(cha_count, GoodOutInfo.GoodId, GoodOutInfo.OrgId, GoodOutInfo.WarehouseInfotId, tx)
  7861. if out_count == cha_count {
  7862. //删除该出库单
  7863. DeleteGoodWarehouseOutInfoByNumber(GoodOutInfo.ID, tx)
  7864. }
  7865. //减去出库单里面的数量
  7866. ModifyGoodWarehouseOutInfoByNumber(GoodOutInfo.ID, cha_count, tx)
  7867. //创建退库单
  7868. goodList, _ := GetNewAllGoodSumCount(project.ProjectId, orgID, storeConfig.StorehouseOutInfo, tx)
  7869. var sum_count int64
  7870. for _, item := range goodList {
  7871. sum_count += item.StockCount
  7872. }
  7873. operation_time := time.Now().Unix()
  7874. //创建退库单
  7875. timeStr := time.Now().Format("2006-01-02")
  7876. timeArr := strings.Split(timeStr, "-")
  7877. total, _ := FindNewAllCancelStockTotal(orgID, tx)
  7878. total = total + 1
  7879. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  7880. cancelStock := models.CancelStock{
  7881. OrderNumber: orderNumber,
  7882. OperaTime: operation_time,
  7883. OrgId: orgID,
  7884. Creater: creater,
  7885. Ctime: time.Now().Unix(),
  7886. Status: 1,
  7887. ReturnTime: project.RecordDate,
  7888. Type: 1,
  7889. StorehouseId: storeConfig.StorehouseOutInfo,
  7890. IsCheck: 1,
  7891. }
  7892. _, msgerrkonde := GetNewCancelStockDetailByOrderNumberOne(project.RecordDate, orgID, tx)
  7893. if msgerrkonde == gorm.ErrRecordNotFound {
  7894. AddNewSigleCancelStock(&cancelStock, tx)
  7895. }
  7896. manufactureName, _ := GetNewManufactureById(GoodOutInfo.Manufacturer, tx)
  7897. dealer, _ := GetNewDealerById(GoodOutInfo.Dealer, tx)
  7898. cancelInfo, _ := GetNewLastCancelStockById(orgID, tx)
  7899. cancelStockInfo := models.CancelStockInfo{
  7900. GoodId: project.ProjectId,
  7901. CancelStockId: cancelInfo.ID,
  7902. GoodTypeId: good.GoodTypeId,
  7903. Count: cha_count,
  7904. Price: project.Price,
  7905. Total: 0,
  7906. ProductDate: GoodOutInfo.ProductDate,
  7907. ExpiryDate: GoodOutInfo.ExpiryDate,
  7908. Ctime: time.Now().Unix(),
  7909. Status: 1,
  7910. OrgId: orgID,
  7911. OrderNumber: cancelInfo.OrderNumber,
  7912. Type: 0,
  7913. Dealer: dealer.DealerName,
  7914. Manufacturer: manufactureName.ManufacturerName,
  7915. Number: GoodOutInfo.Number,
  7916. RegisterAccount: "",
  7917. Remark: "",
  7918. WarehouseInfoId: GoodOutInfo.WarehouseInfotId,
  7919. PatientId: project.ProjectId,
  7920. RecordDate: project.RecordDate,
  7921. StorehouseId: storeConfig.StorehouseOutInfo,
  7922. IsCheck: 1,
  7923. }
  7924. CreateNewCancelStockInfoOne(&cancelStockInfo, tx)
  7925. flow := models.VmStockFlow{
  7926. WarehousingId: GoodOutInfo.WarehouseInfotId,
  7927. GoodId: project.ProjectId,
  7928. Number: GoodOutInfo.Number,
  7929. LicenseNumber: GoodOutInfo.LicenseNumber,
  7930. Count: cha_count,
  7931. UserOrgId: orgID,
  7932. PatientId: project.PatientId,
  7933. SystemTime: project.RecordDate,
  7934. ConsumableType: 7,
  7935. IsSys: 1,
  7936. WarehousingOrder: "",
  7937. WarehouseOutId: GoodOutInfo.WarehouseOutId,
  7938. WarehouseOutOrderNumber: GoodOutInfo.WarehouseOutOrderNumber,
  7939. IsEdit: 0,
  7940. CancelStockId: cancelInfo.ID,
  7941. CancelOrderNumber: cancelInfo.OrderNumber,
  7942. Manufacturer: manufactureName.ID,
  7943. Dealer: 0,
  7944. Creator: creater,
  7945. UpdateCreator: 0,
  7946. Status: 1,
  7947. Ctime: time.Now().Unix(),
  7948. Mtime: 0,
  7949. Price: GoodOutInfo.Price,
  7950. WarehousingDetailId: GoodOutInfo.WarehouseInfotId,
  7951. WarehouseOutDetailId: GoodOutInfo.ID,
  7952. CancelOutDetailId: cancelInfo.ID,
  7953. ProductDate: GoodOutInfo.ProductDate,
  7954. ExpireDate: GoodOutInfo.ExpiryDate,
  7955. StorehouseId: storeConfig.StorehouseOutInfo,
  7956. BuyPrice: project.Price,
  7957. ProjectId: project.ID,
  7958. OverCount: sum_count,
  7959. RegisterNumber: GoodOutInfo.RegisterNumber,
  7960. OperateTime: time.Now().Unix(),
  7961. }
  7962. CreateNewStockFlowOne(flow, tx)
  7963. AddGoodNewCancelSumCountOne(storeConfig.StorehouseOutInfo, project.ProjectId, orgID, flow.Count, tx)
  7964. ReduceNewGoodCountTwo(project.ProjectId, orgID, storeConfig.StorehouseOutInfo, flow.Count, tx)
  7965. }
  7966. //如果出库的数据小于这个退库的数量差,则先该批次出库数量出完
  7967. if out_count < cha_count {
  7968. //把该出库单的数量退完
  7969. ModifyNewGoodWarehouseInfoStockMinNumber(out_count, GoodOutInfo.GoodId, GoodOutInfo.OrgId, GoodOutInfo.WarehouseInfotId, tx)
  7970. //删除该出库单
  7971. DeleteGoodWarehouseOutInfoByNumber(GoodOutInfo.ID, tx)
  7972. //查询剩余库存
  7973. goodList, _ := GetNewAllGoodSumCount(project.ProjectId, orgID, storeConfig.StorehouseOutInfo, tx)
  7974. var sum_count int64
  7975. for _, item := range goodList {
  7976. sum_count += item.StockCount
  7977. }
  7978. operation_time := time.Now().Unix()
  7979. //创建退库单
  7980. timeStr := time.Now().Format("2006-01-02")
  7981. timeArr := strings.Split(timeStr, "-")
  7982. total, _ := FindNewAllCancelStockTotal(orgID, tx)
  7983. total = total + 1
  7984. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  7985. cancelStock := models.CancelStock{
  7986. OrderNumber: orderNumber,
  7987. OperaTime: operation_time,
  7988. OrgId: orgID,
  7989. Creater: creater,
  7990. Ctime: time.Now().Unix(),
  7991. Status: 1,
  7992. ReturnTime: project.RecordDate,
  7993. Type: 1,
  7994. StorehouseId: storeConfig.StorehouseOutInfo,
  7995. IsCheck: 1,
  7996. }
  7997. _, msgerrkonde := GetNewCancelStockDetailByOrderNumberOne(project.RecordDate, orgID, tx)
  7998. if msgerrkonde == gorm.ErrRecordNotFound {
  7999. AddNewSigleCancelStock(&cancelStock, tx)
  8000. }
  8001. manufactureName, _ := GetNewManufactureById(GoodOutInfo.Manufacturer, tx)
  8002. dealer, _ := GetNewDealerById(GoodOutInfo.Dealer, tx)
  8003. cancelInfo, _ := GetNewLastCancelStockById(orgID, tx)
  8004. cancelStockInfo := models.CancelStockInfo{
  8005. GoodId: project.ProjectId,
  8006. CancelStockId: cancelInfo.ID,
  8007. GoodTypeId: good.GoodTypeId,
  8008. Count: out_count,
  8009. Price: project.Price,
  8010. Total: 0,
  8011. ProductDate: GoodOutInfo.ProductDate,
  8012. ExpiryDate: GoodOutInfo.ExpiryDate,
  8013. Ctime: time.Now().Unix(),
  8014. Status: 1,
  8015. OrgId: orgID,
  8016. OrderNumber: cancelInfo.OrderNumber,
  8017. Type: 0,
  8018. Dealer: dealer.DealerName,
  8019. Manufacturer: manufactureName.ManufacturerName,
  8020. Number: GoodOutInfo.Number,
  8021. RegisterAccount: "",
  8022. Remark: "",
  8023. WarehouseInfoId: GoodOutInfo.WarehouseInfotId,
  8024. PatientId: project.ProjectId,
  8025. RecordDate: project.RecordDate,
  8026. StorehouseId: storeConfig.StorehouseOutInfo,
  8027. IsCheck: 1,
  8028. }
  8029. CreateNewCancelStockInfoOne(&cancelStockInfo, tx)
  8030. flow := models.VmStockFlow{
  8031. WarehousingId: GoodOutInfo.WarehouseInfotId,
  8032. GoodId: project.ProjectId,
  8033. Number: GoodOutInfo.Number,
  8034. LicenseNumber: GoodOutInfo.LicenseNumber,
  8035. Count: out_count,
  8036. UserOrgId: orgID,
  8037. PatientId: project.PatientId,
  8038. SystemTime: project.RecordDate,
  8039. ConsumableType: 7,
  8040. IsSys: 1,
  8041. WarehousingOrder: "",
  8042. WarehouseOutId: GoodOutInfo.WarehouseOutId,
  8043. WarehouseOutOrderNumber: GoodOutInfo.WarehouseOutOrderNumber,
  8044. IsEdit: 0,
  8045. CancelStockId: cancelInfo.ID,
  8046. CancelOrderNumber: cancelInfo.OrderNumber,
  8047. Manufacturer: manufactureName.ID,
  8048. Dealer: 0,
  8049. Creator: creater,
  8050. UpdateCreator: 0,
  8051. Status: 1,
  8052. Ctime: time.Now().Unix(),
  8053. Mtime: 0,
  8054. Price: GoodOutInfo.Price,
  8055. WarehousingDetailId: GoodOutInfo.WarehouseInfotId,
  8056. WarehouseOutDetailId: GoodOutInfo.ID,
  8057. CancelOutDetailId: cancelInfo.ID,
  8058. ProductDate: GoodOutInfo.ProductDate,
  8059. ExpireDate: GoodOutInfo.ExpiryDate,
  8060. StorehouseId: storeConfig.StorehouseOutInfo,
  8061. BuyPrice: project.Price,
  8062. ProjectId: project.ID,
  8063. OverCount: sum_count,
  8064. RegisterNumber: GoodOutInfo.RegisterNumber,
  8065. OperateTime: time.Now().Unix(),
  8066. }
  8067. CreateNewStockFlowOne(flow, tx)
  8068. AddGoodNewCancelSumCountOne(storeConfig.StorehouseOutInfo, project.ProjectId, orgID, flow.Count, tx)
  8069. ReduceNewGoodCountTwo(project.ProjectId, orgID, storeConfig.StorehouseOutInfo, flow.Count, tx)
  8070. //计算还有多少没有退库
  8071. var over_count = cha_count - out_count
  8072. NewHisGoodCancelInfo(orgID, creater, project, over_count, good, tx)
  8073. }
  8074. return err
  8075. }
  8076. func NewHisGoodAutoCancelInfo(orgID int64, creater int64, project *models.HisPrescriptionProject, cha_count int64, good models.GoodInformationThirty, tx *gorm.DB) error {
  8077. //查找患者最后一条的退库数据是否满足要求
  8078. var out_count int64
  8079. goodOutInfo, _ := GetGoodWarehouseOutInfoByProjectIdTwo(project.PatientId, project.UserOrgId, project.RecordDate, project.ProjectId, project.ID, tx)
  8080. out_count = goodOutInfo.Count
  8081. //如果最后1条出库数量大于退库数量,则直接退库,满足退库要求
  8082. if out_count >= cha_count {
  8083. //减去出库单里面的数量
  8084. ModifyGoodAuToWarehouseOut(goodOutInfo.ID, cha_count, tx)
  8085. if out_count == cha_count {
  8086. //删除该出库单
  8087. DeleteGoodWarehouseAutoByNumberTwo(goodOutInfo.ID, tx)
  8088. }
  8089. }
  8090. //如果出库的数据小于这个退库的数量差,则先该批次出库数量出完
  8091. if out_count < cha_count {
  8092. //删除该出库单
  8093. DeleteGoodWarehouseAutoByNumberTwo(goodOutInfo.ID, tx)
  8094. //计算还有多少没有退库
  8095. var over_count = cha_count - out_count
  8096. NewHisGoodAutoCancelInfo(orgID, creater, project, over_count, good, tx)
  8097. }
  8098. return err
  8099. }
  8100. func NewHisDrugsDeliveryOne(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo, tx *gorm.DB) (err error) {
  8101. // 1.判断药品是否来自专用字典的药品库
  8102. // 2.判断当天当前机构有没有创建出库单,没有则创建
  8103. // 3.创建出库流程
  8104. // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  8105. isHasWay := false
  8106. record_time := int64(0)
  8107. isHasWay = true
  8108. record_time = advice.RecordDate
  8109. if isHasWay {
  8110. //判断当天当前机构有没有创建出库单,没有则创建
  8111. out, err := FindNewDrugStockOutByIsSys(orgID, 1, record_time, tx)
  8112. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  8113. if err == gorm.ErrRecordNotFound {
  8114. timeStr := time.Now().Format("2006-01-02")
  8115. timeArr := strings.Split(timeStr, "-")
  8116. total, _ := FindAllDrugWarehouseOut(orgID)
  8117. total = total + 1
  8118. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  8119. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  8120. number = number + total
  8121. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  8122. warehouseOut := models.DrugWarehouseOut{
  8123. WarehouseOutOrderNumber: warehousing_out_order,
  8124. OperationTime: time.Now().Unix(),
  8125. OrgId: orgID,
  8126. Creater: creater,
  8127. Ctime: time.Now().Unix(),
  8128. Status: 1,
  8129. WarehouseOutTime: record_time,
  8130. Dealer: 0,
  8131. Manufacturer: 0,
  8132. Type: 1,
  8133. IsSys: 1,
  8134. StorehouseId: houseConfig.DrugStorehouseOut,
  8135. IsCheck: 1,
  8136. }
  8137. err := AddNewSigleDrugWarehouseOut(&warehouseOut, tx)
  8138. if err != nil {
  8139. utils.TraceLog("创建出库单失败 err = %v", err)
  8140. return err
  8141. } else {
  8142. out = warehouseOut
  8143. }
  8144. }
  8145. //出库流程
  8146. //1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  8147. drup, _ := FindNewBaseDrugLibRecord(orgID, advice.DrugId, tx)
  8148. if drup.ID > 0 {
  8149. prescribingNumber := advice.PrescribingNumber
  8150. HisStoryBloodHisDrugDeliverInfoOne(orgID, prescribingNumber, &out, &drup, advice, prescribingNumber, advice.PrescribingNumberUnit, tx)
  8151. } else {
  8152. return errors.New("药品信息不存在")
  8153. }
  8154. }
  8155. return
  8156. }
  8157. func HisFaDrugsDelivery(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo) (err error) {
  8158. // 1.判断药品是否来自专用字典的药品库
  8159. // 2.判断当天当前机构有没有创建出库单,没有则创建
  8160. // 3.创建出库流程
  8161. // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  8162. isHasWay := false
  8163. record_time := int64(0)
  8164. isHasWay = true
  8165. record_time = advice.RecordDate
  8166. if isHasWay {
  8167. //判断当天当前机构有没有创建出库单,没有则创建
  8168. out, err := FindDrugStockOutByIsSys(orgID, 1, record_time)
  8169. fmt.Println(out)
  8170. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  8171. if err == gorm.ErrRecordNotFound {
  8172. timeStr := time.Now().Format("2006-01-02")
  8173. timeArr := strings.Split(timeStr, "-")
  8174. total, _ := FindAllDrugWarehouseOut(orgID)
  8175. total = total + 1
  8176. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  8177. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  8178. number = number + total
  8179. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  8180. warehouseOut := models.DrugWarehouseOut{
  8181. WarehouseOutOrderNumber: warehousing_out_order,
  8182. OperationTime: time.Now().Unix(),
  8183. OrgId: orgID,
  8184. Creater: creater,
  8185. Ctime: time.Now().Unix(),
  8186. Status: 1,
  8187. WarehouseOutTime: record_time,
  8188. Dealer: 0,
  8189. Manufacturer: 0,
  8190. Type: 1,
  8191. IsSys: 1,
  8192. StorehouseId: houseConfig.DrugStorehouseOut,
  8193. IsCheck: 1,
  8194. }
  8195. err := AddSigleDrugWarehouseOut(&warehouseOut)
  8196. if err != nil {
  8197. utils.TraceLog("创建出库单失败 err = %v", err)
  8198. return err
  8199. } else {
  8200. out = warehouseOut
  8201. }
  8202. }
  8203. // 出库流程
  8204. // 1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  8205. drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
  8206. if drup.ID > 0 {
  8207. //var now_count int64
  8208. ////prescribingNumber_temp := strconv.FormatFloat(math.Abs(advice.PrescribingNumber), 'f', 0, 64)
  8209. ////now_count, _ = strconv.ParseInt(prescribingNumber_temp, 10, 64)
  8210. var now_count int64
  8211. if advice.PrescribingNumberUnit == drup.MaxUnit {
  8212. prescribingNumberOne := strconv.FormatFloat(advice.PrescribingNumber, 'f', -1, 64)
  8213. prescribingNumberInt, _ := strconv.ParseInt(prescribingNumberOne, 10, 64)
  8214. now_count = prescribingNumberInt * drup.MinNumber
  8215. }
  8216. if advice.PrescribingNumberUnit == drup.MinUnit {
  8217. prescribingNumberOne := strconv.FormatFloat(advice.PrescribingNumber, 'f', -1, 64)
  8218. prescribingNumberInt, _ := strconv.ParseInt(prescribingNumberOne, 10, 64)
  8219. now_count = prescribingNumberInt
  8220. //回退库存
  8221. }
  8222. outListExport, _ := GetWarehouseOutListExport(advice.DrugId, advice.PatientId, advice.AdviceDate, advice.ID)
  8223. var out_count int64
  8224. if len(outListExport) > 0 {
  8225. for _, item := range outListExport {
  8226. if item.CountUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  8227. out_count = item.Count * drup.MinNumber
  8228. }
  8229. if item.CountUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  8230. out_count = item.Count
  8231. }
  8232. if item.CountUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  8233. out_count = item.Count * drup.MinNumber
  8234. }
  8235. }
  8236. }
  8237. fmt.Println("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhwode", advice.DrugCode)
  8238. newadviceInfo := &models.HisDoctorAdviceInfo{
  8239. ID: advice.ID,
  8240. UserOrgId: advice.UserOrgId,
  8241. PatientId: advice.PatientId,
  8242. HisPatientId: advice.HisPatientId,
  8243. AdviceType: advice.AdviceType,
  8244. AdviceDate: advice.AdviceDate,
  8245. StartTime: advice.StartTime,
  8246. AdviceName: advice.AdviceName,
  8247. AdviceDesc: advice.AdviceDesc,
  8248. ReminderDate: advice.ReminderDate,
  8249. SingleDose: advice.SingleDose,
  8250. SingleDoseUnit: advice.SingleDoseUnit,
  8251. PrescribingNumber: advice.PrescribingNumber,
  8252. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  8253. DeliveryWay: advice.DeliveryWay,
  8254. ExecutionFrequency: advice.ExecutionFrequency,
  8255. AdviceDoctor: advice.AdviceDoctor,
  8256. Status: advice.Status,
  8257. CreatedTime: advice.CreatedTime,
  8258. UpdatedTime: advice.UpdatedTime,
  8259. AdviceAffirm: advice.AdviceAffirm,
  8260. Remark: advice.Remark,
  8261. StopTime: advice.StopTime,
  8262. StopReason: advice.StopReason,
  8263. StopDoctor: advice.StopDoctor,
  8264. StopState: advice.StopState,
  8265. ParentId: advice.ParentId,
  8266. ExecutionTime: advice.ExecutionTime,
  8267. ExecutionStaff: advice.ExecutionStaff,
  8268. ExecutionState: advice.ExecutionState,
  8269. Checker: advice.Checker,
  8270. RecordDate: advice.RecordDate,
  8271. DialysisOrderId: advice.DialysisOrderId,
  8272. CheckTime: advice.CheckTime,
  8273. CheckState: advice.CheckState,
  8274. DrugSpec: advice.DrugSpec,
  8275. DrugSpecUnit: advice.DrugSpecUnit,
  8276. Groupno: advice.Groupno,
  8277. RemindType: advice.RemindType,
  8278. FrequencyType: advice.FrequencyType,
  8279. DayCount: advice.DayCount,
  8280. WeekDay: advice.WeekDay,
  8281. TemplateId: advice.TemplateId,
  8282. Modifier: advice.Modifier,
  8283. DrugId: advice.DrugId,
  8284. Price: advice.Price,
  8285. PrescriptionId: advice.PrescriptionId,
  8286. MedListCodg: advice.MedListCodg,
  8287. FeedetlSn: advice.FeedetlSn,
  8288. Day: advice.DayCount,
  8289. ChildDoctorAdvice: nil,
  8290. Drug: models.Drug{},
  8291. Diagnosis: advice.Diagnosis,
  8292. Way: advice.Way,
  8293. HospApprFlag: advice.HospApprFlag,
  8294. LmtUsedFlag: advice.LmtUsedFlag,
  8295. HisOrderInfo: models.HisOrderInfo{},
  8296. IsMedicine: advice.IsMedicine,
  8297. ExecutionFrequencyId: advice.ExecutionFrequencyId,
  8298. Child: nil,
  8299. IsSettle: advice.IsSettle,
  8300. IsMobile: advice.IsMobile,
  8301. IsSelfDrug: advice.IsSelfDrug,
  8302. DrugWayCount: advice.DrugWayCount,
  8303. DrugCode: advice.DrugCode,
  8304. }
  8305. db := XTWriteDB()
  8306. tx := db.Begin()
  8307. // 在函数结束时处理事务回滚
  8308. defer func() {
  8309. if r := recover(); r != nil {
  8310. tx.Rollback()
  8311. } else {
  8312. tx.Commit()
  8313. }
  8314. }()
  8315. //如果当前医嘱数量大于历史出库数据则需要出库
  8316. if now_count > out_count {
  8317. //按最小单位出库
  8318. var cha_count = now_count - out_count
  8319. newadviceInfo.PrescribingNumber = float64(cha_count)
  8320. newadviceInfo.PrescribingNumberUnit = drup.MinUnit
  8321. NewHisDrugsDelivery(advice.UserOrgId, creater, newadviceInfo, tx)
  8322. }
  8323. //如果当前医嘱数量小于历史出库数据则需要退库
  8324. if now_count < out_count {
  8325. var cha_count = out_count - now_count
  8326. drug, _ := FindBaseDrugLibRecordSeven(advice.UserOrgId, advice.DrugId)
  8327. NewHisDrugCancelInfo(advice.UserOrgId, creater, newadviceInfo, cha_count, drug, tx)
  8328. NewHisDrugAutoCancelInfo(advice.UserOrgId, creater, newadviceInfo, cha_count, drug, tx)
  8329. }
  8330. if now_count == out_count {
  8331. if out_count == 0 {
  8332. NewHisDrugsDelivery(advice.UserOrgId, creater, newadviceInfo, tx)
  8333. }
  8334. }
  8335. } else {
  8336. return errors.New("药品信息不存在")
  8337. }
  8338. }
  8339. return
  8340. }
  8341. func NewMobileHisGoodCancelInfo(orgID int64, creater int64, project *models.HisPrescriptionProject, cha_count int64, good models.GoodInformationThirty, tx *gorm.DB) (err error) {
  8342. storeConfig, _ := GetNewAllStoreHouseConfig(orgID, tx)
  8343. //查找患者最后一条的退库数据是否满足要求
  8344. var out_count int64
  8345. GoodOutInfo, _ := GetGoodWarehouseOutInfoByProjectIdThree(project.PatientId, project.UserOrgId, project.RecordDate, project.ProjectId, tx)
  8346. out_count = GoodOutInfo.Count
  8347. //如果最后1条出库数量大于退库数量,则直接退库,满足退库要求
  8348. if out_count >= cha_count {
  8349. //退库 退回 cha_count 的数量
  8350. ModifyNewGoodWarehouseInfoStockMinNumber(cha_count, GoodOutInfo.GoodId, GoodOutInfo.OrgId, GoodOutInfo.WarehouseInfotId, tx)
  8351. if out_count == cha_count {
  8352. //删除该出库单
  8353. DeleteGoodWarehouseOutInfoByNumber(GoodOutInfo.ID, tx)
  8354. } else {
  8355. //减去出库单里面的数量
  8356. ModifyGoodWarehouseOutInfoByNumber(GoodOutInfo.ID, cha_count, tx)
  8357. }
  8358. //创建退库单
  8359. goodList, _ := GetNewAllGoodSumCount(project.ProjectId, orgID, storeConfig.StorehouseOutInfo, tx)
  8360. var sum_count int64
  8361. for _, item := range goodList {
  8362. sum_count += item.StockCount
  8363. }
  8364. operation_time := time.Now().Unix()
  8365. //创建退库单
  8366. timeStr := time.Now().Format("2006-01-02")
  8367. timeArr := strings.Split(timeStr, "-")
  8368. total, _ := FindNewAllCancelStockTotal(orgID, tx)
  8369. total = total + 1
  8370. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  8371. cancelStock := models.CancelStock{
  8372. OrderNumber: orderNumber,
  8373. OperaTime: operation_time,
  8374. OrgId: orgID,
  8375. Creater: creater,
  8376. Ctime: time.Now().Unix(),
  8377. Status: 1,
  8378. ReturnTime: project.RecordDate,
  8379. Type: 1,
  8380. StorehouseId: storeConfig.StorehouseOutInfo,
  8381. IsCheck: 1,
  8382. }
  8383. _, msgerrkonde := GetNewCancelStockDetailByOrderNumberOne(project.RecordDate, orgID, tx)
  8384. if msgerrkonde == gorm.ErrRecordNotFound {
  8385. AddNewSigleCancelStock(&cancelStock, tx)
  8386. }
  8387. manufactureName, _ := GetNewManufactureById(GoodOutInfo.Manufacturer, tx)
  8388. dealer, _ := GetNewDealerById(GoodOutInfo.Dealer, tx)
  8389. cancelInfo, _ := GetNewLastCancelStockById(orgID, tx)
  8390. cancelStockInfo := models.CancelStockInfo{
  8391. GoodId: project.ProjectId,
  8392. CancelStockId: cancelInfo.ID,
  8393. GoodTypeId: good.GoodTypeId,
  8394. Count: cha_count,
  8395. Price: project.Price,
  8396. Total: 0,
  8397. ProductDate: GoodOutInfo.ProductDate,
  8398. ExpiryDate: GoodOutInfo.ExpiryDate,
  8399. Ctime: time.Now().Unix(),
  8400. Status: 1,
  8401. OrgId: orgID,
  8402. OrderNumber: cancelInfo.OrderNumber,
  8403. Type: 0,
  8404. Dealer: dealer.DealerName,
  8405. Manufacturer: manufactureName.ManufacturerName,
  8406. Number: GoodOutInfo.Number,
  8407. RegisterAccount: "",
  8408. Remark: "",
  8409. WarehouseInfoId: GoodOutInfo.WarehouseInfotId,
  8410. PatientId: project.ProjectId,
  8411. RecordDate: project.RecordDate,
  8412. StorehouseId: storeConfig.StorehouseOutInfo,
  8413. IsCheck: 1,
  8414. }
  8415. CreateNewCancelStockInfoOne(&cancelStockInfo, tx)
  8416. flow := models.VmStockFlow{
  8417. WarehousingId: GoodOutInfo.WarehouseInfotId,
  8418. GoodId: project.ProjectId,
  8419. Number: GoodOutInfo.Number,
  8420. LicenseNumber: GoodOutInfo.LicenseNumber,
  8421. Count: cha_count,
  8422. UserOrgId: orgID,
  8423. PatientId: project.PatientId,
  8424. SystemTime: project.RecordDate,
  8425. ConsumableType: 7,
  8426. IsSys: 1,
  8427. WarehousingOrder: "",
  8428. WarehouseOutId: GoodOutInfo.WarehouseOutId,
  8429. WarehouseOutOrderNumber: GoodOutInfo.WarehouseOutOrderNumber,
  8430. IsEdit: 0,
  8431. CancelStockId: cancelInfo.ID,
  8432. CancelOrderNumber: cancelInfo.OrderNumber,
  8433. Manufacturer: manufactureName.ID,
  8434. Dealer: 0,
  8435. Creator: creater,
  8436. UpdateCreator: 0,
  8437. Status: 1,
  8438. Ctime: time.Now().Unix(),
  8439. Mtime: 0,
  8440. Price: GoodOutInfo.Price,
  8441. WarehousingDetailId: GoodOutInfo.WarehouseInfotId,
  8442. WarehouseOutDetailId: GoodOutInfo.ID,
  8443. CancelOutDetailId: cancelInfo.ID,
  8444. ProductDate: GoodOutInfo.ProductDate,
  8445. ExpireDate: GoodOutInfo.ExpiryDate,
  8446. StorehouseId: storeConfig.StorehouseOutInfo,
  8447. BuyPrice: project.Price,
  8448. ProjectId: project.ID,
  8449. OverCount: sum_count,
  8450. RegisterNumber: GoodOutInfo.RegisterNumber,
  8451. OperateTime: time.Now().Unix(),
  8452. }
  8453. CreateNewStockFlowOne(flow, tx)
  8454. AddGoodNewCancelSumCountOne(storeConfig.StorehouseOutInfo, project.ProjectId, orgID, flow.Count, tx)
  8455. ReduceNewGoodCountTwo(project.ProjectId, orgID, storeConfig.StorehouseOutInfo, flow.Count, tx)
  8456. }
  8457. //如果出库的数据小于这个退库的数量差,则先该批次出库数量出完
  8458. if out_count < cha_count {
  8459. //把该出库单的数量退完
  8460. ModifyNewGoodWarehouseInfoStockMinNumber(out_count, GoodOutInfo.GoodId, GoodOutInfo.OrgId, GoodOutInfo.WarehouseInfotId, tx)
  8461. //删除该出库单
  8462. DeleteGoodWarehouseOutInfoByNumber(GoodOutInfo.ID, tx)
  8463. //查询剩余库存
  8464. goodList, _ := GetNewAllGoodSumCount(project.ProjectId, orgID, storeConfig.StorehouseOutInfo, tx)
  8465. var sum_count int64
  8466. for _, item := range goodList {
  8467. sum_count += item.StockCount
  8468. }
  8469. operation_time := time.Now().Unix()
  8470. //创建退库单
  8471. timeStr := time.Now().Format("2006-01-02")
  8472. timeArr := strings.Split(timeStr, "-")
  8473. total, _ := FindNewAllCancelStockTotal(orgID, tx)
  8474. total = total + 1
  8475. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  8476. cancelStock := models.CancelStock{
  8477. OrderNumber: orderNumber,
  8478. OperaTime: operation_time,
  8479. OrgId: orgID,
  8480. Creater: creater,
  8481. Ctime: time.Now().Unix(),
  8482. Status: 1,
  8483. ReturnTime: project.RecordDate,
  8484. Type: 1,
  8485. StorehouseId: storeConfig.StorehouseOutInfo,
  8486. IsCheck: 1,
  8487. }
  8488. _, msgerrkonde := GetNewCancelStockDetailByOrderNumberOne(project.RecordDate, orgID, tx)
  8489. if msgerrkonde == gorm.ErrRecordNotFound {
  8490. AddNewSigleCancelStock(&cancelStock, tx)
  8491. }
  8492. manufactureName, _ := GetNewManufactureById(GoodOutInfo.Manufacturer, tx)
  8493. dealer, _ := GetNewDealerById(GoodOutInfo.Dealer, tx)
  8494. cancelInfo, _ := GetNewLastCancelStockById(orgID, tx)
  8495. cancelStockInfo := models.CancelStockInfo{
  8496. GoodId: project.ProjectId,
  8497. CancelStockId: cancelInfo.ID,
  8498. GoodTypeId: good.GoodTypeId,
  8499. Count: out_count,
  8500. Price: project.Price,
  8501. Total: 0,
  8502. ProductDate: GoodOutInfo.ProductDate,
  8503. ExpiryDate: GoodOutInfo.ExpiryDate,
  8504. Ctime: time.Now().Unix(),
  8505. Status: 1,
  8506. OrgId: orgID,
  8507. OrderNumber: cancelInfo.OrderNumber,
  8508. Type: 0,
  8509. Dealer: dealer.DealerName,
  8510. Manufacturer: manufactureName.ManufacturerName,
  8511. Number: GoodOutInfo.Number,
  8512. RegisterAccount: "",
  8513. Remark: "",
  8514. WarehouseInfoId: GoodOutInfo.WarehouseInfotId,
  8515. PatientId: project.ProjectId,
  8516. RecordDate: project.RecordDate,
  8517. StorehouseId: storeConfig.StorehouseOutInfo,
  8518. IsCheck: 1,
  8519. }
  8520. CreateNewCancelStockInfoOne(&cancelStockInfo, tx)
  8521. flow := models.VmStockFlow{
  8522. WarehousingId: GoodOutInfo.WarehouseInfotId,
  8523. GoodId: project.ProjectId,
  8524. Number: GoodOutInfo.Number,
  8525. LicenseNumber: GoodOutInfo.LicenseNumber,
  8526. Count: out_count,
  8527. UserOrgId: orgID,
  8528. PatientId: project.PatientId,
  8529. SystemTime: project.RecordDate,
  8530. ConsumableType: 7,
  8531. IsSys: 1,
  8532. WarehousingOrder: "",
  8533. WarehouseOutId: GoodOutInfo.WarehouseOutId,
  8534. WarehouseOutOrderNumber: GoodOutInfo.WarehouseOutOrderNumber,
  8535. IsEdit: 0,
  8536. CancelStockId: cancelInfo.ID,
  8537. CancelOrderNumber: cancelInfo.OrderNumber,
  8538. Manufacturer: manufactureName.ID,
  8539. Dealer: 0,
  8540. Creator: creater,
  8541. UpdateCreator: 0,
  8542. Status: 1,
  8543. Ctime: time.Now().Unix(),
  8544. Mtime: 0,
  8545. Price: GoodOutInfo.Price,
  8546. WarehousingDetailId: GoodOutInfo.WarehouseInfotId,
  8547. WarehouseOutDetailId: GoodOutInfo.ID,
  8548. CancelOutDetailId: cancelInfo.ID,
  8549. ProductDate: GoodOutInfo.ProductDate,
  8550. ExpireDate: GoodOutInfo.ExpiryDate,
  8551. StorehouseId: storeConfig.StorehouseOutInfo,
  8552. BuyPrice: project.Price,
  8553. ProjectId: project.ID,
  8554. OverCount: sum_count,
  8555. RegisterNumber: GoodOutInfo.RegisterNumber,
  8556. OperateTime: time.Now().Unix(),
  8557. }
  8558. CreateNewStockFlowOne(flow, tx)
  8559. AddGoodNewCancelSumCountOne(storeConfig.StorehouseOutInfo, project.ProjectId, orgID, flow.Count, tx)
  8560. ReduceNewGoodCountTwo(project.ProjectId, orgID, storeConfig.StorehouseOutInfo, flow.Count, tx)
  8561. //计算还有多少没有退库
  8562. var over_count = cha_count - out_count
  8563. NewHisGoodCancelInfo(orgID, creater, project, over_count, good, tx)
  8564. }
  8565. return err
  8566. }
  8567. func NewMobileHisGoodAutoCancelInfo(orgID int64, creater int64, project *models.HisPrescriptionProject, cha_count int64, good models.GoodInformationThirty, tx *gorm.DB) error {
  8568. //查找患者最后一条的退库数据是否满足要求
  8569. var out_count int64
  8570. goodOutInfo, _ := GetGoodWarehouseOutInfoByProjectIdThree(project.PatientId, project.UserOrgId, project.RecordDate, project.ProjectId, tx)
  8571. out_count = goodOutInfo.Count
  8572. //如果最后1条出库数量大于退库数量,则直接退库,满足退库要求
  8573. if out_count >= cha_count {
  8574. //减去出库单里面的数量
  8575. ModifyGoodAuToWarehouseOut(goodOutInfo.ID, cha_count, tx)
  8576. if out_count == cha_count {
  8577. //删除该出库单
  8578. DeleteGoodWarehouseAutoByNumberTwo(goodOutInfo.ID, tx)
  8579. }
  8580. }
  8581. //如果出库的数据小于这个退库的数量差,则先该批次出库数量出完
  8582. if out_count < cha_count {
  8583. //删除该出库单
  8584. DeleteGoodWarehouseAutoByNumberTwo(goodOutInfo.ID, tx)
  8585. //计算还有多少没有退库
  8586. var over_count = cha_count - out_count
  8587. NewHisGoodAutoCancelInfo(orgID, creater, project, over_count, good, tx)
  8588. }
  8589. return err
  8590. }
  8591. // 药品手动出库 递归方式
  8592. func AutoSelfDrugDeliverInfoFourtyOne(orgID int64, prescribingNumber int64, warehouseout *models.XtSelfDrugWarehouseOut, drup *models.BaseDrugLib, advice *models.XtSelfDrugWarehouseOutInfo, unit string, tx *gorm.DB) (err error) {
  8593. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  8594. var deliver_number int64 = 0
  8595. var stock_number int64 = 0
  8596. var drug_price float64
  8597. if advice.CountUnit == drup.MaxUnit {
  8598. deliver_number = prescribingNumber * drup.MinNumber
  8599. } else {
  8600. deliver_number = prescribingNumber
  8601. }
  8602. if advice.CountUnit == drup.MaxUnit {
  8603. drug_price = drup.RetailPrice
  8604. }
  8605. fmt.Println(drug_price)
  8606. if advice.CountUnit == drup.MinUnit {
  8607. drug_price = drup.MinPrice
  8608. }
  8609. if advice.CountUnit != drup.MaxUnit || advice.CountUnit != drup.MinUnit {
  8610. drug_price = drup.RetailPrice
  8611. }
  8612. // 根据先进先出原则,查询最先入库的批次,进行出库
  8613. // 如果没有对应的库存,则报错
  8614. lastWarehouse, _ := FindSelfLastDrugWarehousingInfoByID(advice.DrugId, advice.PatientId, tx)
  8615. if lastWarehouse.MaxUnit != lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  8616. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  8617. var stockMax int64
  8618. var stockMin int64
  8619. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  8620. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  8621. ChangeSelfMaxNumber(lastWarehouse.ID, stockMax, tx)
  8622. UpdateSelfMinNumber(lastWarehouse.ID, stockMin, tx)
  8623. }
  8624. }
  8625. if lastWarehouse.MaxUnit == lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  8626. lastWarehouse.StockMaxNumber = lastWarehouse.StockMaxNumber + lastWarehouse.StockMinNumber
  8627. ChangeSelfMaxNumberOne(lastWarehouse.ID, lastWarehouse.StockMaxNumber, tx)
  8628. }
  8629. warehouse, err := FindSelfLastDrugWarehousingInfoByID(advice.DrugId, advice.PatientId, tx)
  8630. // 将该批次的剩余库存数量转换为拆零数量
  8631. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  8632. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  8633. }
  8634. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  8635. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  8636. }
  8637. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  8638. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  8639. }
  8640. if stock_number >= deliver_number {
  8641. var maxNumber int64 = 0
  8642. var minNumber int64 = 0
  8643. maxNumber = deliver_number / drup.MinNumber
  8644. minNumber = deliver_number % drup.MinNumber
  8645. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  8646. minNumber = maxNumber
  8647. }
  8648. if drup.MaxUnit != drup.MinUnit {
  8649. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  8650. return errors.New("库存数量不足")
  8651. }
  8652. }
  8653. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  8654. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  8655. }
  8656. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  8657. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  8658. }
  8659. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  8660. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  8661. }
  8662. if warehouse.StockMaxNumber < 0 {
  8663. warehouse.StockMaxNumber = 0
  8664. }
  8665. if warehouse.StockMinNumber < 0 {
  8666. warehouse.StockMinNumber = 0
  8667. }
  8668. warehouse.Mtime = time.Now().Unix()
  8669. if warehouse.StockMinNumber < minNumber {
  8670. //fmt.Println("金4")
  8671. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  8672. if warehouse.MaxUnit != warehouse.MinUnit {
  8673. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  8674. }
  8675. if minNumber == 2 && warehouse.MaxUnit == warehouse.MinUnit && warehouse.StockMinNumber == 0 {
  8676. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  8677. }
  8678. } else {
  8679. //fmt.Println("金5")
  8680. if minNumber > 0 {
  8681. if (warehouse.StockMinNumber - minNumber) >= 0 {
  8682. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  8683. }
  8684. }
  8685. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  8686. //fmt.Println("6")
  8687. if warehouse.StockMinNumber > 0 {
  8688. if warehouse.StockMinNumber-deliver_number >= 0 {
  8689. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  8690. }
  8691. }
  8692. }
  8693. }
  8694. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit && warehouse.MaxUnit != warehouse.MinUnit {
  8695. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  8696. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  8697. }
  8698. }
  8699. if drup.MaxUnit != drup.MinUnit {
  8700. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  8701. return errors.New("库存数量不足")
  8702. }
  8703. }
  8704. if warehouse.StockMinNumber <= 0 {
  8705. warehouse.StockMinNumber = 0
  8706. }
  8707. errThree := UpDateSelfDrugWarehouseInfoByStock(&warehouse, tx)
  8708. if errThree != nil {
  8709. return errors.New("扣减库存失败")
  8710. }
  8711. //查询该药品剩余库存
  8712. infolist, _ := FindSelfDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId, tx)
  8713. var over_count int64
  8714. for _, its := range infolist {
  8715. if its.MaxUnit == drup.MaxUnit {
  8716. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  8717. }
  8718. over_count += its.StockMaxNumber + its.StockMinNumber
  8719. }
  8720. //更新出库id
  8721. //查询今日该药品该患者是否有出库数据
  8722. lastDrugOutInfo, _ := GetSelfNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.SysRecordTime, advice.OrgId, advice.AdviceId, warehouse.ID, tx)
  8723. if lastDrugOutInfo.ID == 0 {
  8724. advice.WarehouseInfoId = lastWarehouse.ID
  8725. AddSelfSigleDrugWarehouseOutInfo(advice, tx)
  8726. }
  8727. if lastDrugOutInfo.ID > 0 {
  8728. advice.WarehouseInfoId = lastWarehouse.ID
  8729. //删除在新增
  8730. UpdateSelfDrugWarehouseOutInfoTen(lastDrugOutInfo.PatientId, lastDrugOutInfo.SysRecordTime, advice.ID, tx)
  8731. AddSelfSigleDrugWarehouseOutInfo(advice, tx)
  8732. }
  8733. drugWareInfo, _ := GetSelfLastWarehouseOutInfoById(advice.DrugId, tx)
  8734. drugflow := models.XtSelfDrugFlow{
  8735. WarehouseOutId: warehouseout.ID,
  8736. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8737. DrugId: advice.DrugId,
  8738. Number: warehouse.Number,
  8739. ProductDate: advice.ProductDate,
  8740. ExpireDate: advice.ExpiryDate,
  8741. Count: deliver_number,
  8742. Price: warehouse.RetailPrice,
  8743. Status: 1,
  8744. Ctime: advice.SysRecordTime,
  8745. Mtime: time.Now().Unix(),
  8746. UserOrgId: advice.OrgId,
  8747. Manufacturer: advice.Manufacturer,
  8748. Dealer: advice.Dealer,
  8749. BatchNumber: warehouse.BatchNumber,
  8750. MaxUnit: drup.MinUnit,
  8751. ConsumableType: 2,
  8752. IsEdit: 1,
  8753. Creator: 0,
  8754. IsSys: 0,
  8755. WarehouseOutDetailId: drugWareInfo.ID,
  8756. AdviceId: advice.ID,
  8757. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  8758. StorehouseId: advice.StorehouseId,
  8759. AdminUserId: advice.AdminUserId,
  8760. LastPrice: warehouse.Price,
  8761. StockCount: advice.StockCount,
  8762. WarehousingDetailId: warehouse.ID,
  8763. OverCount: over_count,
  8764. RetailPrice: drug_price, //手动出库出库价格
  8765. WarehousingId: warehouse.ID,
  8766. SystemTime: advice.SysRecordTime,
  8767. PatientId: advice.PatientId,
  8768. Remark: advice.Remark,
  8769. OperateTime: time.Now().Unix(),
  8770. }
  8771. if warehouse.RetailPrice == 0 {
  8772. drugflow.Price = advice.Price
  8773. }
  8774. CreateSelfDrugFlowOne(drugflow, tx)
  8775. //出库数量相加
  8776. AddSelfDrugCount(advice.DrugId, orgID, advice.PatientId, drugflow.Count, tx)
  8777. return nil
  8778. } else {
  8779. stock_number_int := strconv.FormatInt(stock_number, 10)
  8780. stock_number_int_count, _ := strconv.ParseFloat(stock_number_int, 64)
  8781. drug_price = stock_number_int_count * advice.Price
  8782. //扣减库存
  8783. warehouse.StockMaxNumber = 0
  8784. warehouse.StockMinNumber = 0
  8785. warehouse.Mtime = time.Now().Unix()
  8786. errThree := UpDateSelfDrugWarehouseInfoByStock(&warehouse, tx)
  8787. if errThree != nil {
  8788. return errThree
  8789. }
  8790. //查询该药品剩余库存
  8791. infolist, _ := FindDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId)
  8792. var over_count int64
  8793. for _, its := range infolist {
  8794. if its.MaxUnit == drup.MaxUnit {
  8795. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  8796. }
  8797. over_count += its.StockMaxNumber + its.StockMinNumber
  8798. }
  8799. //查询今日该药品该患者是否有出库数据
  8800. lastDrugOutInfo, _ := GetSelfNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.SysRecordTime, advice.OrgId, advice.AdviceId, warehouse.ID, tx)
  8801. if lastDrugOutInfo.ID == 0 {
  8802. advice.WarehouseInfoId = lastWarehouse.ID
  8803. AddSelfSigleDrugWarehouseOutInfo(advice, tx)
  8804. }
  8805. if lastDrugOutInfo.ID > 0 {
  8806. advice.WarehouseInfoId = lastWarehouse.ID
  8807. //删除在新增
  8808. UpdateSelfDrugWarehouseOutInfoTen(lastDrugOutInfo.PatientId, lastDrugOutInfo.SysRecordTime, advice.ID, tx)
  8809. AddSelfSigleDrugWarehouseOutInfo(advice, tx)
  8810. }
  8811. drugWareInfo, _ := GetSelfLastWarehouseOutInfoById(advice.DrugId, tx)
  8812. drugflow := models.XtSelfDrugFlow{
  8813. WarehouseOutId: warehouseout.ID,
  8814. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8815. DrugId: advice.DrugId,
  8816. Number: warehouse.Number,
  8817. ProductDate: advice.ProductDate,
  8818. ExpireDate: advice.ExpiryDate,
  8819. Count: stock_number,
  8820. Price: warehouse.RetailPrice,
  8821. Status: 1,
  8822. Ctime: advice.SysRecordTime,
  8823. Mtime: time.Now().Unix(),
  8824. UserOrgId: advice.OrgId,
  8825. Manufacturer: advice.Manufacturer,
  8826. Dealer: advice.Dealer,
  8827. BatchNumber: warehouse.BatchNumber,
  8828. MaxUnit: drup.MinUnit,
  8829. ConsumableType: 2,
  8830. IsEdit: 1,
  8831. Creator: 0,
  8832. IsSys: 0,
  8833. WarehouseOutDetailId: drugWareInfo.ID,
  8834. AdviceId: advice.ID,
  8835. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  8836. StorehouseId: advice.StorehouseId,
  8837. AdminUserId: advice.AdminUserId,
  8838. LastPrice: warehouse.Price,
  8839. StockCount: advice.StockCount,
  8840. OverCount: over_count,
  8841. RetailPrice: drug_price, //手动出库出库价格
  8842. WarehousingId: warehouse.ID,
  8843. WarehousingDetailId: warehouse.ID,
  8844. SystemTime: advice.SysRecordTime,
  8845. PatientId: advice.PatientId,
  8846. Remark: advice.Remark,
  8847. OperateTime: time.Now().Unix(),
  8848. }
  8849. CreateSelfDrugFlowOne(drugflow, tx)
  8850. AddSelfDrugCount(advice.DrugId, orgID, advice.PatientId, drugflow.Count, tx)
  8851. // 出库完成后,要将该批次库存清零
  8852. prescribingNumber_two_temp := deliver_number - stock_number
  8853. advice.CountUnit = drup.MinUnit
  8854. AutoSelfDrugDeliverInfoFourtyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, unit, tx)
  8855. }
  8856. return
  8857. }
  8858. // 药品手动出库 递归方式
  8859. func AutoSelfDrugDeliverInfoFourtyTwo(orgID int64, prescribingNumber int64, warehouseout *models.XtSelfDrugWarehouseOut, drup *models.BaseDrugLib, advice *models.XtSelfDrugWarehouseOutInfo, unit string, tx *gorm.DB) (err error) {
  8860. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  8861. var deliver_number int64 = 0
  8862. var stock_number int64 = 0
  8863. var drug_price float64
  8864. if advice.CountUnit == drup.MaxUnit {
  8865. deliver_number = prescribingNumber * drup.MinNumber
  8866. } else {
  8867. deliver_number = prescribingNumber
  8868. }
  8869. if advice.CountUnit == drup.MaxUnit {
  8870. drug_price = drup.RetailPrice
  8871. }
  8872. fmt.Println(drug_price)
  8873. if advice.CountUnit == drup.MinUnit {
  8874. drug_price = drup.MinPrice
  8875. }
  8876. if advice.CountUnit != drup.MaxUnit || advice.CountUnit != drup.MinUnit {
  8877. drug_price = drup.RetailPrice
  8878. }
  8879. // 根据先进先出原则,查询最先入库的批次,进行出库
  8880. // 如果没有对应的库存,则报错
  8881. lastWarehouse, _ := FindSelfLastDrugWarehousingInfoByID(advice.DrugId, advice.PatientId, tx)
  8882. if lastWarehouse.MaxUnit != lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  8883. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  8884. var stockMax int64
  8885. var stockMin int64
  8886. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  8887. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  8888. ChangeSelfMaxNumber(lastWarehouse.ID, stockMax, tx)
  8889. UpdateSelfMinNumber(lastWarehouse.ID, stockMin, tx)
  8890. }
  8891. }
  8892. if lastWarehouse.MaxUnit == lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  8893. lastWarehouse.StockMaxNumber = lastWarehouse.StockMaxNumber + lastWarehouse.StockMinNumber
  8894. ChangeSelfMaxNumberOne(lastWarehouse.ID, lastWarehouse.StockMaxNumber, tx)
  8895. }
  8896. warehouse, err := FindSelfLastDrugWarehousingInfoByID(advice.DrugId, advice.PatientId, tx)
  8897. // 将该批次的剩余库存数量转换为拆零数量
  8898. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  8899. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  8900. }
  8901. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  8902. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  8903. }
  8904. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  8905. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  8906. }
  8907. if stock_number >= deliver_number {
  8908. var maxNumber int64 = 0
  8909. var minNumber int64 = 0
  8910. maxNumber = deliver_number / drup.MinNumber
  8911. minNumber = deliver_number % drup.MinNumber
  8912. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  8913. minNumber = maxNumber
  8914. }
  8915. if drup.MaxUnit != drup.MinUnit {
  8916. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  8917. return errors.New("库存数量不足")
  8918. }
  8919. }
  8920. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  8921. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  8922. }
  8923. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  8924. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  8925. }
  8926. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  8927. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  8928. }
  8929. if warehouse.StockMaxNumber < 0 {
  8930. warehouse.StockMaxNumber = 0
  8931. }
  8932. if warehouse.StockMinNumber < 0 {
  8933. warehouse.StockMinNumber = 0
  8934. }
  8935. warehouse.Mtime = time.Now().Unix()
  8936. if warehouse.StockMinNumber < minNumber {
  8937. //fmt.Println("金4")
  8938. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  8939. if warehouse.MaxUnit != warehouse.MinUnit {
  8940. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  8941. }
  8942. if minNumber == 2 && warehouse.MaxUnit == warehouse.MinUnit && warehouse.StockMinNumber == 0 {
  8943. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  8944. }
  8945. } else {
  8946. //fmt.Println("金5")
  8947. if minNumber > 0 {
  8948. if (warehouse.StockMinNumber - minNumber) >= 0 {
  8949. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  8950. }
  8951. }
  8952. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  8953. //fmt.Println("6")
  8954. if warehouse.StockMinNumber > 0 {
  8955. if warehouse.StockMinNumber-deliver_number >= 0 {
  8956. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  8957. }
  8958. }
  8959. }
  8960. }
  8961. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit && warehouse.MaxUnit != warehouse.MinUnit {
  8962. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  8963. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  8964. }
  8965. }
  8966. if drup.MaxUnit != drup.MinUnit {
  8967. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  8968. return errors.New("库存数量不足")
  8969. }
  8970. }
  8971. if warehouse.StockMinNumber <= 0 {
  8972. warehouse.StockMinNumber = 0
  8973. }
  8974. errThree := UpDateSelfDrugWarehouseInfoByStock(&warehouse, tx)
  8975. if errThree != nil {
  8976. return errors.New("扣减库存失败")
  8977. }
  8978. //查询该药品剩余库存
  8979. infolist, _ := FindSelfDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId, tx)
  8980. var over_count int64
  8981. for _, its := range infolist {
  8982. if its.MaxUnit == drup.MaxUnit {
  8983. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  8984. }
  8985. over_count += its.StockMaxNumber + its.StockMinNumber
  8986. }
  8987. //更新出库id
  8988. //查询今日该药品该患者是否有出库数据
  8989. lastDrugOutInfo, _ := GetSelfNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.SysRecordTime, advice.OrgId, advice.AdviceId, warehouse.ID, tx)
  8990. if lastDrugOutInfo.ID == 0 {
  8991. advice.WarehouseInfoId = lastWarehouse.ID
  8992. AddSelfSigleDrugWarehouseOutInfo(advice, tx)
  8993. }
  8994. if lastDrugOutInfo.ID > 0 {
  8995. advice.WarehouseInfoId = lastWarehouse.ID
  8996. //删除在新增
  8997. UpdateSelfDrugWarehouseOutInfoTen(lastDrugOutInfo.PatientId, lastDrugOutInfo.SysRecordTime, advice.ID, tx)
  8998. AddSelfSigleDrugWarehouseOutInfo(advice, tx)
  8999. }
  9000. drugWareInfo, _ := GetSelfLastWarehouseOutInfoById(advice.DrugId, tx)
  9001. drugflow := models.XtSelfDrugFlow{
  9002. WarehouseOutId: warehouseout.ID,
  9003. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  9004. DrugId: advice.DrugId,
  9005. Number: warehouse.Number,
  9006. ProductDate: advice.ProductDate,
  9007. ExpireDate: advice.ExpiryDate,
  9008. Count: deliver_number,
  9009. Price: warehouse.RetailPrice,
  9010. Status: 1,
  9011. Ctime: advice.SysRecordTime,
  9012. Mtime: time.Now().Unix(),
  9013. UserOrgId: advice.OrgId,
  9014. Manufacturer: advice.Manufacturer,
  9015. Dealer: advice.Dealer,
  9016. BatchNumber: warehouse.BatchNumber,
  9017. MaxUnit: drup.MinUnit,
  9018. ConsumableType: 3,
  9019. IsEdit: 1,
  9020. Creator: 0,
  9021. IsSys: 0,
  9022. WarehouseOutDetailId: drugWareInfo.ID,
  9023. AdviceId: advice.ID,
  9024. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9025. StorehouseId: advice.StorehouseId,
  9026. AdminUserId: advice.AdminUserId,
  9027. LastPrice: warehouse.Price,
  9028. StockCount: advice.StockCount,
  9029. WarehousingDetailId: warehouse.ID,
  9030. OverCount: over_count,
  9031. RetailPrice: drug_price, //手动出库出库价格
  9032. WarehousingId: warehouse.ID,
  9033. SystemTime: advice.SysRecordTime,
  9034. PatientId: advice.PatientId,
  9035. Remark: advice.Remark,
  9036. OperateTime: time.Now().Unix(),
  9037. }
  9038. if warehouse.RetailPrice == 0 {
  9039. drugflow.Price = advice.Price
  9040. }
  9041. CreateSelfDrugFlowOne(drugflow, tx)
  9042. //出库数量相加
  9043. AddSelfDrugCount(advice.DrugId, orgID, advice.PatientId, drugflow.Count, tx)
  9044. //fmt.Println("advice-------------------------------------", advice.ID)
  9045. //fmt.Println("warehouse-------------------------------------", warehouse.ID)
  9046. //fmt.Println("over_count====================================", over_count)
  9047. return nil
  9048. } else {
  9049. stock_number_int := strconv.FormatInt(stock_number, 10)
  9050. stock_number_int_count, _ := strconv.ParseFloat(stock_number_int, 64)
  9051. drug_price = stock_number_int_count * advice.Price
  9052. //扣减库存
  9053. warehouse.StockMaxNumber = 0
  9054. warehouse.StockMinNumber = 0
  9055. warehouse.Mtime = time.Now().Unix()
  9056. errThree := UpDateSelfDrugWarehouseInfoByStock(&warehouse, tx)
  9057. if errThree != nil {
  9058. return errThree
  9059. }
  9060. //查询该药品剩余库存
  9061. infolist, _ := FindDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId)
  9062. var over_count int64
  9063. for _, its := range infolist {
  9064. if its.MaxUnit == drup.MaxUnit {
  9065. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  9066. }
  9067. over_count += its.StockMaxNumber + its.StockMinNumber
  9068. }
  9069. //查询今日该药品该患者是否有出库数据
  9070. lastDrugOutInfo, _ := GetSelfNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.SysRecordTime, advice.OrgId, advice.AdviceId, warehouse.ID, tx)
  9071. if lastDrugOutInfo.ID == 0 {
  9072. advice.WarehouseInfoId = lastWarehouse.ID
  9073. AddSelfSigleDrugWarehouseOutInfo(advice, tx)
  9074. }
  9075. if lastDrugOutInfo.ID > 0 {
  9076. advice.WarehouseInfoId = lastWarehouse.ID
  9077. //删除在新增
  9078. UpdateSelfDrugWarehouseOutInfoTen(lastDrugOutInfo.PatientId, lastDrugOutInfo.SysRecordTime, advice.ID, tx)
  9079. AddSelfSigleDrugWarehouseOutInfo(advice, tx)
  9080. }
  9081. drugWareInfo, _ := GetSelfLastWarehouseOutInfoById(advice.DrugId, tx)
  9082. drugflow := models.XtSelfDrugFlow{
  9083. WarehouseOutId: warehouseout.ID,
  9084. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  9085. DrugId: advice.DrugId,
  9086. Number: warehouse.Number,
  9087. ProductDate: advice.ProductDate,
  9088. ExpireDate: advice.ExpiryDate,
  9089. Count: stock_number,
  9090. Price: warehouse.RetailPrice,
  9091. Status: 1,
  9092. Ctime: advice.SysRecordTime,
  9093. Mtime: time.Now().Unix(),
  9094. UserOrgId: advice.OrgId,
  9095. Manufacturer: advice.Manufacturer,
  9096. Dealer: advice.Dealer,
  9097. BatchNumber: warehouse.BatchNumber,
  9098. MaxUnit: drup.MinUnit,
  9099. ConsumableType: 3,
  9100. IsEdit: 1,
  9101. Creator: 0,
  9102. IsSys: 0,
  9103. WarehouseOutDetailId: drugWareInfo.ID,
  9104. AdviceId: advice.ID,
  9105. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9106. StorehouseId: advice.StorehouseId,
  9107. AdminUserId: advice.AdminUserId,
  9108. LastPrice: warehouse.Price,
  9109. StockCount: advice.StockCount,
  9110. OverCount: over_count,
  9111. RetailPrice: drug_price, //手动出库出库价格
  9112. WarehousingId: warehouse.ID,
  9113. WarehousingDetailId: warehouse.ID,
  9114. SystemTime: advice.SysRecordTime,
  9115. PatientId: advice.PatientId,
  9116. Remark: advice.Remark,
  9117. OperateTime: time.Now().Unix(),
  9118. }
  9119. CreateSelfDrugFlowOne(drugflow, tx)
  9120. AddSelfDrugCount(advice.DrugId, orgID, advice.PatientId, drugflow.Count, tx)
  9121. // 出库完成后,要将该批次库存清零
  9122. prescribingNumber_two_temp := deliver_number - stock_number
  9123. advice.CountUnit = drup.MinUnit
  9124. AutoSelfDrugDeliverInfoFourtyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, unit, tx)
  9125. }
  9126. return
  9127. }
  9128. func AutoSelfDrugDeliverInfoFourtyThree(orgID int64, prescribingNumber int64, warehouseout *models.XtSelfDrugWarehouseOut, drup *models.BaseDrugLib, advice *models.XtSelfDrugWarehouseOutInfo, unit string, tx *gorm.DB) (err error) {
  9129. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  9130. var deliver_number int64 = 0
  9131. var stock_number int64 = 0
  9132. var drug_price float64
  9133. if advice.CountUnit == drup.MaxUnit {
  9134. deliver_number = prescribingNumber * drup.MinNumber
  9135. } else {
  9136. deliver_number = prescribingNumber
  9137. }
  9138. if advice.CountUnit == drup.MaxUnit {
  9139. drug_price = drup.RetailPrice
  9140. }
  9141. fmt.Println(drug_price)
  9142. if advice.CountUnit == drup.MinUnit {
  9143. drug_price = drup.MinPrice
  9144. }
  9145. if advice.CountUnit != drup.MaxUnit || advice.CountUnit != drup.MinUnit {
  9146. drug_price = drup.RetailPrice
  9147. }
  9148. // 根据先进先出原则,查询最先入库的批次,进行出库
  9149. // 如果没有对应的库存,则报错
  9150. lastWarehouse, _ := FindSelfLastDrugWarehousingInfoByID(advice.DrugId, advice.PatientId, tx)
  9151. if lastWarehouse.MaxUnit != lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  9152. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  9153. var stockMax int64
  9154. var stockMin int64
  9155. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  9156. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  9157. ChangeSelfMaxNumber(lastWarehouse.ID, stockMax, tx)
  9158. UpdateSelfMinNumber(lastWarehouse.ID, stockMin, tx)
  9159. }
  9160. }
  9161. if lastWarehouse.MaxUnit == lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  9162. lastWarehouse.StockMaxNumber = lastWarehouse.StockMaxNumber + lastWarehouse.StockMinNumber
  9163. ChangeSelfMaxNumberOne(lastWarehouse.ID, lastWarehouse.StockMaxNumber, tx)
  9164. }
  9165. warehouse, err := FindSelfLastDrugWarehousingInfoByID(advice.DrugId, advice.PatientId, tx)
  9166. // 将该批次的剩余库存数量转换为拆零数量
  9167. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  9168. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  9169. }
  9170. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  9171. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  9172. }
  9173. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  9174. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  9175. }
  9176. if stock_number >= deliver_number {
  9177. var maxNumber int64 = 0
  9178. var minNumber int64 = 0
  9179. maxNumber = deliver_number / drup.MinNumber
  9180. minNumber = deliver_number % drup.MinNumber
  9181. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  9182. minNumber = maxNumber
  9183. }
  9184. if drup.MaxUnit != drup.MinUnit {
  9185. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  9186. return errors.New("库存数量不足")
  9187. }
  9188. }
  9189. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  9190. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  9191. }
  9192. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  9193. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  9194. }
  9195. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  9196. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  9197. }
  9198. if warehouse.StockMaxNumber < 0 {
  9199. warehouse.StockMaxNumber = 0
  9200. }
  9201. if warehouse.StockMinNumber < 0 {
  9202. warehouse.StockMinNumber = 0
  9203. }
  9204. warehouse.Mtime = time.Now().Unix()
  9205. if warehouse.StockMinNumber < minNumber {
  9206. //fmt.Println("金4")
  9207. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  9208. if warehouse.MaxUnit != warehouse.MinUnit {
  9209. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  9210. }
  9211. if minNumber == 2 && warehouse.MaxUnit == warehouse.MinUnit && warehouse.StockMinNumber == 0 {
  9212. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  9213. }
  9214. } else {
  9215. //fmt.Println("金5")
  9216. if minNumber > 0 {
  9217. if (warehouse.StockMinNumber - minNumber) >= 0 {
  9218. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  9219. }
  9220. }
  9221. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  9222. //fmt.Println("6")
  9223. if warehouse.StockMinNumber > 0 {
  9224. if warehouse.StockMinNumber-deliver_number >= 0 {
  9225. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  9226. }
  9227. }
  9228. }
  9229. }
  9230. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit && warehouse.MaxUnit != warehouse.MinUnit {
  9231. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  9232. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  9233. }
  9234. }
  9235. if drup.MaxUnit != drup.MinUnit {
  9236. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  9237. return errors.New("库存数量不足")
  9238. }
  9239. }
  9240. if warehouse.StockMinNumber <= 0 {
  9241. warehouse.StockMinNumber = 0
  9242. }
  9243. errThree := UpDateSelfDrugWarehouseInfoByStock(&warehouse, tx)
  9244. if errThree != nil {
  9245. return errors.New("扣减库存失败")
  9246. }
  9247. //查询该药品剩余库存
  9248. infolist, _ := FindSelfDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId, tx)
  9249. var over_count int64
  9250. for _, its := range infolist {
  9251. if its.MaxUnit == drup.MaxUnit {
  9252. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  9253. }
  9254. over_count += its.StockMaxNumber + its.StockMinNumber
  9255. }
  9256. //drugWareInfo, _ := GetSelfLastWarehouseOutInfoById(advice.DrugId, tx)
  9257. drugflow := models.XtSelfDrugFlow{
  9258. WarehouseOutId: warehouseout.ID,
  9259. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  9260. DrugId: advice.DrugId,
  9261. Number: warehouse.Number,
  9262. ProductDate: advice.ProductDate,
  9263. ExpireDate: advice.ExpiryDate,
  9264. Count: deliver_number,
  9265. Price: warehouse.RetailPrice,
  9266. Status: 1,
  9267. Ctime: advice.SysRecordTime,
  9268. Mtime: time.Now().Unix(),
  9269. UserOrgId: advice.OrgId,
  9270. Manufacturer: advice.Manufacturer,
  9271. Dealer: advice.Dealer,
  9272. BatchNumber: warehouse.BatchNumber,
  9273. MaxUnit: drup.MinUnit,
  9274. ConsumableType: 2,
  9275. IsEdit: 1,
  9276. Creator: 0,
  9277. IsSys: 0,
  9278. WarehouseOutDetailId: advice.ID,
  9279. AdviceId: advice.ID,
  9280. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9281. StorehouseId: advice.StorehouseId,
  9282. AdminUserId: advice.AdminUserId,
  9283. LastPrice: warehouse.Price,
  9284. StockCount: advice.StockCount,
  9285. WarehousingDetailId: warehouse.ID,
  9286. OverCount: over_count,
  9287. RetailPrice: drug_price, //手动出库出库价格
  9288. WarehousingId: warehouse.ID,
  9289. SystemTime: advice.SysRecordTime,
  9290. PatientId: advice.PatientId,
  9291. Remark: advice.Remark,
  9292. OperateTime: time.Now().Unix(),
  9293. }
  9294. if warehouse.RetailPrice == 0 {
  9295. drugflow.Price = advice.Price
  9296. }
  9297. CreateSelfDrugFlowOne(drugflow, tx)
  9298. //更新出库id
  9299. UpdateSelfDrugWarehouseOutInfo(advice.ID, warehouse.ID, advice.OrgId, over_count, drug_price, tx)
  9300. //出库数量相加
  9301. AddSelfDrugCount(advice.DrugId, orgID, advice.PatientId, drugflow.Count, tx)
  9302. return nil
  9303. } else {
  9304. stock_number_int := strconv.FormatInt(stock_number, 10)
  9305. stock_number_int_count, _ := strconv.ParseFloat(stock_number_int, 64)
  9306. drug_price = stock_number_int_count * advice.Price
  9307. //扣减库存
  9308. warehouse.StockMaxNumber = 0
  9309. warehouse.StockMinNumber = 0
  9310. warehouse.Mtime = time.Now().Unix()
  9311. errThree := UpDateSelfDrugWarehouseInfoByStock(&warehouse, tx)
  9312. if errThree != nil {
  9313. return errThree
  9314. }
  9315. //查询该药品剩余库存
  9316. infolist, _ := FindDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId)
  9317. var over_count int64
  9318. for _, its := range infolist {
  9319. if its.MaxUnit == drup.MaxUnit {
  9320. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  9321. }
  9322. over_count += its.StockMaxNumber + its.StockMinNumber
  9323. }
  9324. //查询今日该药品该患者是否有出库数据
  9325. lastDrugOutInfo, _ := GetSelfNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.SysRecordTime, advice.OrgId, advice.AdviceId, warehouse.ID, tx)
  9326. if lastDrugOutInfo.ID == 0 {
  9327. AddSelfSigleDrugWarehouseOutInfo(advice, tx)
  9328. }
  9329. if lastDrugOutInfo.ID > 0 {
  9330. //删除在新增
  9331. UpdateSelfDrugWarehouseOutInfoTen(lastDrugOutInfo.PatientId, lastDrugOutInfo.SysRecordTime, advice.ID, tx)
  9332. AddSelfSigleDrugWarehouseOutInfo(advice, tx)
  9333. }
  9334. //drugWareInfo, _ := GetSelfLastWarehouseOutInfoById(advice.DrugId, tx)
  9335. drugflow := models.XtSelfDrugFlow{
  9336. WarehouseOutId: warehouseout.ID,
  9337. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  9338. DrugId: advice.DrugId,
  9339. Number: warehouse.Number,
  9340. ProductDate: advice.ProductDate,
  9341. ExpireDate: advice.ExpiryDate,
  9342. Count: stock_number,
  9343. Price: warehouse.RetailPrice,
  9344. Status: 1,
  9345. Ctime: advice.SysRecordTime,
  9346. Mtime: time.Now().Unix(),
  9347. UserOrgId: advice.OrgId,
  9348. Manufacturer: advice.Manufacturer,
  9349. Dealer: advice.Dealer,
  9350. BatchNumber: warehouse.BatchNumber,
  9351. MaxUnit: drup.MinUnit,
  9352. ConsumableType: 2,
  9353. IsEdit: 1,
  9354. Creator: 0,
  9355. IsSys: 0,
  9356. WarehouseOutDetailId: advice.ID,
  9357. AdviceId: advice.ID,
  9358. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9359. StorehouseId: advice.StorehouseId,
  9360. AdminUserId: advice.AdminUserId,
  9361. LastPrice: warehouse.Price,
  9362. StockCount: advice.StockCount,
  9363. OverCount: over_count,
  9364. RetailPrice: drug_price, //手动出库出库价格
  9365. WarehousingId: warehouse.ID,
  9366. WarehousingDetailId: warehouse.ID,
  9367. SystemTime: advice.SysRecordTime,
  9368. PatientId: advice.PatientId,
  9369. Remark: advice.Remark,
  9370. OperateTime: time.Now().Unix(),
  9371. }
  9372. CreateSelfDrugFlowOne(drugflow, tx)
  9373. AddSelfDrugCount(advice.DrugId, orgID, advice.PatientId, drugflow.Count, tx)
  9374. // 出库完成后,要将该批次库存清零
  9375. prescribingNumber_two_temp := deliver_number - stock_number
  9376. advice.CountUnit = drup.MinUnit
  9377. AutoSelfDrugDeliverInfoFourtyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, unit, tx)
  9378. }
  9379. return
  9380. }