warhouse_service.go 498KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344
  1. package service
  2. import (
  3. "XT_New/models"
  4. "XT_New/utils"
  5. "errors"
  6. "fmt"
  7. _ "fmt"
  8. "github.com/jinzhu/gorm"
  9. "math"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // 药品出库
  15. func DrugsDelivery(orgID int64, creater int64, advice *models.DoctorAdvice) (err error) {
  16. // 1.判断药品是否来自专用字典的药品库
  17. // 2.判断当天当前机构有没有创建出库单,没有则创建
  18. // 3.创建出库流程
  19. // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  20. isHasWay := false
  21. record_time := int64(0)
  22. if advice.Way == 1 {
  23. isHasWay = true
  24. record_time = advice.RecordDate
  25. }
  26. if isHasWay {
  27. //判断当天当前机构有没有创建出库单,没有则创建
  28. out, err := FindDrugStockOutByIsSys(orgID, 1, record_time)
  29. if err == gorm.ErrRecordNotFound {
  30. timeStr := time.Now().Format("2006-01-02")
  31. timeArr := strings.Split(timeStr, "-")
  32. total, _ := FindAllDrugWarehouseOut(orgID)
  33. total = total + 1
  34. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  35. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  36. number = number + total
  37. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  38. storeConfig, _ := GetAllStoreHouseConfig(orgID)
  39. warehouseOut := models.DrugWarehouseOut{
  40. WarehouseOutOrderNumber: warehousing_out_order,
  41. OperationTime: time.Now().Unix(),
  42. OrgId: orgID,
  43. Creater: creater,
  44. Ctime: time.Now().Unix(),
  45. Status: 1,
  46. WarehouseOutTime: record_time,
  47. Dealer: 0,
  48. Manufacturer: 0,
  49. Type: 1,
  50. IsSys: 1,
  51. StorehouseId: storeConfig.DrugStorehouseOut,
  52. 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. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  77. var deliver_number int64 = 0
  78. var stock_number int64 = 0
  79. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  80. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  81. var drug_price float64
  82. if advice.PrescribingNumberUnit == drup.MaxUnit {
  83. deliver_number = count * drup.MinNumber
  84. } else {
  85. deliver_number = count
  86. }
  87. if advice.PrescribingNumberUnit == drup.MaxUnit {
  88. drug_price = drup.RetailPrice
  89. }
  90. fmt.Println(drug_price)
  91. if advice.PrescribingNumberUnit == drup.MinUnit {
  92. drug_price = drup.MinPrice
  93. }
  94. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  95. drug_price = drup.RetailPrice
  96. }
  97. // 根据先进先出原则,查询最先入库的批次,进行出库
  98. // 如果没有对应的库存,则报错
  99. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  100. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  101. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  102. var stockMax int64
  103. var stockMin int64
  104. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  105. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  106. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  107. UpdateMinNumber(lastWarehouse.ID, stockMin)
  108. }
  109. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  110. if err != nil {
  111. return err
  112. }
  113. // 将该批次的剩余库存数量转换为拆零数量
  114. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  115. //查找药品信息
  116. //baseInfo, _ := FindBaseDrugLibRecord(advice.UserOrgId, advice.DrugId)
  117. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  118. if stock_number >= deliver_number {
  119. var maxNumber int64 = 0
  120. var minNumber int64 = 0
  121. //var stock_max_number int64 = 0
  122. //stock_max_number = warehouse.StockMaxNumber
  123. maxNumber = deliver_number / drup.MinNumber
  124. minNumber = deliver_number % drup.MinNumber
  125. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  126. minNumber = maxNumber
  127. }
  128. if drup.MaxUnit != drup.MinUnit {
  129. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  130. return errors.New("库存数量不足")
  131. }
  132. }
  133. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  134. if warehouse.StockMaxNumber < 0 {
  135. warehouse.StockMaxNumber = 0
  136. }
  137. if warehouse.StockMinNumber < 0 {
  138. warehouse.StockMinNumber = 0
  139. }
  140. warehouse.Mtime = time.Now().Unix()
  141. if warehouse.StockMinNumber < minNumber {
  142. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  143. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  144. } else {
  145. if minNumber > 0 {
  146. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  147. }
  148. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  149. if warehouse.StockMinNumber > 0 {
  150. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  151. }
  152. }
  153. }
  154. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  155. if (warehouse.StockMinNumber - deliver_number) > 0 {
  156. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  157. }
  158. }
  159. if drup.MaxUnit != drup.MinUnit {
  160. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  161. return errors.New("库存数量不足")
  162. }
  163. }
  164. if warehouse.StockMinNumber <= 0 {
  165. warehouse.StockMinNumber = 0
  166. }
  167. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  168. if errThree != nil {
  169. return errThree
  170. }
  171. //查询剩余库存
  172. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  173. var sum_count int64
  174. for _, its := range stockInfo {
  175. if its.MaxUnit == drup.MaxUnit {
  176. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  177. }
  178. sum_count += its.StockMaxNumber + its.StockMinNumber
  179. }
  180. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  181. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  182. WarehouseOutId: warehouseout.ID,
  183. Status: 1,
  184. Ctime: time.Now().Unix(),
  185. Remark: warehouse.Remark,
  186. OrgId: orgID,
  187. Type: 1,
  188. Manufacturer: warehouse.Manufacturer,
  189. Dealer: warehouse.Dealer,
  190. IsSys: 1,
  191. SysRecordTime: advice.RecordDate,
  192. DrugId: advice.DrugId,
  193. Number: warehouse.Number,
  194. BatchNumber: warehouse.BatchNumber,
  195. Price: warehouse.RetailPrice,
  196. CountUnit: drup.MinUnit,
  197. RetailPrice: warehouse.RetailPrice,
  198. ProductDate: warehouse.ProductDate,
  199. ExpiryDate: warehouse.ExpiryDate,
  200. PatientId: advice.PatientId,
  201. Count: deliver_number, //出最小单位
  202. WarehouseInfoId: warehouse.ID,
  203. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  204. StorehouseId: storeConfig.DrugStorehouseOut,
  205. IsCheck: 1,
  206. OverCount: sum_count,
  207. }
  208. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  209. fmt.Println(errOne)
  210. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  211. drugflow := models.DrugFlow{
  212. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  213. WarehouseOutId: warehouseout.ID,
  214. DrugId: advice.DrugId,
  215. Number: warehouse.Number,
  216. ProductDate: warehouse.ProductDate,
  217. ExpireDate: warehouse.ExpiryDate,
  218. Count: deliver_number, //按最小单位计算,
  219. Price: warehouse.RetailPrice,
  220. Status: 1,
  221. Ctime: time.Now().Unix(),
  222. UserOrgId: orgID,
  223. Manufacturer: warehouse.Manufacturer,
  224. Dealer: warehouse.Dealer,
  225. BatchNumber: warehouse.BatchNumber,
  226. MaxUnit: drup.MinUnit,
  227. ConsumableType: 3,
  228. IsEdit: 1,
  229. Creator: advice.ExecutionStaff,
  230. IsSys: 1,
  231. PatientId: advice.PatientId,
  232. SystemTime: advice.AdviceDate,
  233. WarehousingDetailId: warehouse.ID,
  234. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  235. StorehouseId: storeConfig.DrugStorehouseOut,
  236. WarehouseOutDetailId: lastDrugOutInfo.ID,
  237. LastPrice: warehouse.Price,
  238. OverCount: sum_count,
  239. }
  240. CreateDrugFlowOne(drugflow)
  241. //查询是否存在数据
  242. details := &models.DrugAutomaticReduceDetail{
  243. WarehouseOutId: warehouseout.ID,
  244. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  245. PatientId: advice.PatientId,
  246. Ctime: time.Now().Unix(),
  247. Mtime: time.Now().Unix(),
  248. Status: 1,
  249. RecordTime: advice.RecordDate,
  250. OrgId: orgID,
  251. DrugId: advice.DrugId,
  252. Count: deliver_number,
  253. CountUnit: drup.MinUnit,
  254. WarehouseInfoId: warehouse.ID,
  255. StorehouseId: storeConfig.DrugStorehouseOut,
  256. }
  257. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  258. if errTwo != nil {
  259. return errTwo
  260. }
  261. var out_count int64
  262. //查询出库数据
  263. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  264. for _, item := range infoCountList {
  265. out_count += item.Count
  266. }
  267. //出库数量相加
  268. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  269. return nil
  270. } else {
  271. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  272. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  273. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  274. WarehouseOutId: warehouseout.ID,
  275. Status: 1,
  276. Ctime: time.Now().Unix(),
  277. Remark: warehouse.Remark,
  278. OrgId: orgID,
  279. Type: 1,
  280. Manufacturer: warehouse.Manufacturer,
  281. Dealer: warehouse.Dealer,
  282. IsSys: 1,
  283. SysRecordTime: advice.RecordDate,
  284. DrugId: advice.DrugId,
  285. Number: warehouse.Number,
  286. BatchNumber: warehouse.BatchNumber,
  287. Price: warehouse.RetailPrice,
  288. CountUnit: drup.MinUnit,
  289. ProductDate: warehouse.ProductDate,
  290. ExpiryDate: warehouse.ExpiryDate,
  291. PatientId: advice.PatientId,
  292. Count: stock_number,
  293. WarehouseInfoId: warehouse.ID,
  294. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  295. StorehouseId: storeConfig.DrugStorehouseOut,
  296. IsCheck: 1,
  297. }
  298. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  299. if errOne != nil {
  300. return errOne
  301. }
  302. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  303. details := &models.DrugAutomaticReduceDetail{
  304. WarehouseOutId: warehouseout.ID,
  305. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  306. PatientId: advice.PatientId,
  307. Ctime: time.Now().Unix(),
  308. Mtime: time.Now().Unix(),
  309. Status: 1,
  310. RecordTime: advice.RecordDate,
  311. OrgId: orgID,
  312. DrugId: advice.DrugId,
  313. Count: stock_number,
  314. CountUnit: drup.MinUnit,
  315. WarehouseInfoId: warehouse.ID,
  316. StorehouseId: storeConfig.DrugStorehouseOut,
  317. }
  318. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  319. if errTwo != nil {
  320. return errTwo
  321. }
  322. info := models.XtDrugWarehouseInfo{
  323. ID: warehouse.ID,
  324. WarehousingId: warehouse.WarehousingId,
  325. DrugId: warehouse.DrugId,
  326. Number: warehouse.Number,
  327. ProductDate: warehouse.ProductDate,
  328. ExpiryDate: warehouse.ExpiryDate,
  329. WarehousingCount: warehouse.WarehousingCount,
  330. Price: warehouse.RetailPrice,
  331. TotalPrice: warehouse.TotalPrice,
  332. Dealer: warehouse.Dealer,
  333. Manufacturer: warehouse.Manufacturer,
  334. Remark: warehouse.Remark,
  335. Ctime: warehouse.Ctime,
  336. Mtime: warehouse.Mtime,
  337. Status: 1,
  338. OrgId: warehouse.OrgId,
  339. IsReturn: warehouse.IsReturn,
  340. WarehousingOrder: warehouse.WarehousingOrder,
  341. Type: warehouse.Type,
  342. RetailPrice: warehouse.RetailPrice,
  343. RetailTotalPrice: warehouse.RetailPrice,
  344. StockMaxNumber: 0,
  345. StockMinNumber: 0,
  346. BatchNumber: warehouse.BatchNumber,
  347. MaxUnit: warehouse.MaxUnit,
  348. WarehousingInfoId: warehouse.WarehousingInfoId,
  349. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  350. StorehouseId: storeConfig.DrugStorehouseOut,
  351. IsCheck: 1,
  352. }
  353. //扣减库存
  354. errThree := UpDateDrugWarehouseInfoByStock(&info)
  355. if errThree != nil {
  356. return errThree
  357. }
  358. //查询剩余库存
  359. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  360. var sum_count int64
  361. for _, its := range stockInfo {
  362. if its.MaxUnit == drup.MaxUnit {
  363. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  364. }
  365. sum_count += its.StockMaxNumber + its.StockMinNumber
  366. }
  367. var out_count int64
  368. //查询出库数据
  369. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  370. for _, item := range infoCountList {
  371. out_count += item.Count
  372. }
  373. drugflow := models.DrugFlow{
  374. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  375. WarehouseOutId: warehouseout.ID,
  376. DrugId: advice.DrugId,
  377. Number: warehouse.Number,
  378. ProductDate: warehouse.ProductDate,
  379. ExpireDate: warehouse.ExpiryDate,
  380. Count: stock_number,
  381. Price: warehouse.RetailPrice,
  382. Status: 1,
  383. Ctime: time.Now().Unix(),
  384. UserOrgId: orgID,
  385. Manufacturer: warehouse.Manufacturer,
  386. Dealer: warehouse.Dealer,
  387. BatchNumber: warehouse.BatchNumber,
  388. MaxUnit: drup.MinUnit,
  389. ConsumableType: 3,
  390. IsEdit: 1,
  391. Creator: advice.ExecutionStaff,
  392. IsSys: 1,
  393. PatientId: advice.PatientId,
  394. WarehousingDetailId: warehouse.ID,
  395. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  396. StorehouseId: storeConfig.DrugStorehouseOut,
  397. WarehouseOutDetailId: lastDrugOutInfo.ID,
  398. LastPrice: warehouse.Price,
  399. OverCount: sum_count,
  400. }
  401. CreateDrugFlowOne(drugflow)
  402. //出库数量相加
  403. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  404. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  405. prescribingNumber_two_temp := deliver_number - stock_number
  406. overPlusNumber := float64(prescribingNumber_two_temp)
  407. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  408. advice.PrescribingNumberUnit = drup.MinUnit
  409. DrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  410. }
  411. return
  412. }
  413. // 药品出库
  414. func HisDrugsDelivery(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo) (err error) {
  415. // 1.判断药品是否来自专用字典的药品库
  416. // 2.判断当天当前机构有没有创建出库单,没有则创建
  417. // 3.创建出库流程
  418. // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  419. isHasWay := false
  420. record_time := int64(0)
  421. isHasWay = true
  422. record_time = advice.RecordDate
  423. if isHasWay {
  424. //判断当天当前机构有没有创建出库单,没有则创建
  425. out, err := FindDrugStockOutByIsSys(orgID, 1, record_time)
  426. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  427. if err == gorm.ErrRecordNotFound {
  428. timeStr := time.Now().Format("2006-01-02")
  429. timeArr := strings.Split(timeStr, "-")
  430. total, _ := FindAllDrugWarehouseOut(orgID)
  431. total = total + 1
  432. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  433. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  434. number = number + total
  435. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  436. warehouseOut := models.DrugWarehouseOut{
  437. WarehouseOutOrderNumber: warehousing_out_order,
  438. OperationTime: time.Now().Unix(),
  439. OrgId: orgID,
  440. Creater: creater,
  441. Ctime: time.Now().Unix(),
  442. Status: 1,
  443. WarehouseOutTime: record_time,
  444. Dealer: 0,
  445. Manufacturer: 0,
  446. Type: 1,
  447. IsSys: 1,
  448. StorehouseId: houseConfig.DrugStorehouseOut,
  449. IsCheck: 1,
  450. }
  451. err := AddSigleDrugWarehouseOut(&warehouseOut)
  452. if err != nil {
  453. utils.TraceLog("创建出库单失败 err = %v", err)
  454. return err
  455. } else {
  456. out = warehouseOut
  457. }
  458. }
  459. // 出库流程
  460. // 1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  461. drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
  462. if drup.ID > 0 {
  463. prescribingNumber := advice.PrescribingNumber
  464. //HisDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice)
  465. BloodHisDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice)
  466. //查询该患者今日总的出库数量
  467. //advicelist, _ := GetAllSumDrugWarehouseOut(advice.PatientId, advice.UserOrgId, advice.AdviceDate)
  468. //var total_count int64
  469. //for _,item :=range advicelist {
  470. // if(item.PrescribingNumberUnit == drup.MaxUnit && drup.MaxUnit!=drup.MinUnit){
  471. // prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  472. // count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  473. // total_count += count * drup.MinNumber
  474. // }
  475. // if(item.PrescribingNumberUnit == drup.MinUnit && drup.MaxUnit!=drup.MinUnit ){
  476. // prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  477. // count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  478. // total_count += count
  479. // }
  480. //package service
  481. //
  482. //import (
  483. // "XT_New/models"
  484. // "XT_New/utils"
  485. // "errors"
  486. // "fmt"
  487. // _ "fmt"
  488. // "github.com/jinzhu/gorm"
  489. // "math"
  490. // "strconv"
  491. // "strings"
  492. // "time"
  493. //)
  494. //
  495. //// 药品出库
  496. //
  497. //func DrugsDelivery(orgID int64, creater int64, advice *models.DoctorAdvice) (err error) {
  498. // // 1.判断药品是否来自专用字典的药品库
  499. // // 2.判断当天当前机构有没有创建出库单,没有则创建
  500. // // 3.创建出库流程
  501. // // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  502. // isHasWay := false
  503. // record_time := int64(0)
  504. // if advice.Way == 1 {
  505. // isHasWay = true
  506. // record_time = advice.RecordDate
  507. // }
  508. //
  509. // if isHasWay {
  510. // //判断当天当前机构有没有创建出库单,没有则创建
  511. // out, err := FindDrugStockOutByIsSys(orgID, 1, record_time)
  512. // if err == gorm.ErrRecordNotFound {
  513. // timeStr := time.Now().Format("2006-01-02")
  514. // timeArr := strings.Split(timeStr, "-")
  515. // total, _ := FindAllDrugWarehouseOut(orgID)
  516. // total = total + 1
  517. // warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  518. // number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  519. // number = number + total
  520. // warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  521. // storeConfig, _ := GetAllStoreHouseConfig(orgID)
  522. // warehouseOut := models.DrugWarehouseOut{
  523. // WarehouseOutOrderNumber: warehousing_out_order,
  524. // OperationTime: time.Now().Unix(),
  525. // OrgId: orgID,
  526. // Creater: creater,
  527. // Ctime: time.Now().Unix(),
  528. // Status: 1,
  529. // WarehouseOutTime: record_time,
  530. // Dealer: 0,
  531. // Manufacturer: 0,
  532. // Type: 1,
  533. // IsSys: 1,
  534. // StorehouseId: storeConfig.DrugStorehouseOut,
  535. // IsCheck: 1,
  536. // }
  537. // err := AddSigleDrugWarehouseOut(&warehouseOut)
  538. // if err != nil {
  539. // utils.TraceLog("创建出库单失败 err = %v", err)
  540. // return err
  541. // } else {
  542. // out = warehouseOut
  543. // }
  544. // }
  545. //
  546. // // 出库流程
  547. // // 1.查询该药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  548. // drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
  549. // if drup.ID > 0 {
  550. // prescribingNumber := advice.PrescribingNumber
  551. // DrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice)
  552. //
  553. // } else {
  554. // return errors.New("药品信息不存在")
  555. // }
  556. //
  557. // }
  558. //
  559. // return
  560. //}
  561. //
  562. //// 药品自动出库 递归方式
  563. //func DrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DoctorAdvice) (err error) {
  564. //
  565. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  566. // var deliver_number int64 = 0
  567. // var stock_number int64 = 0
  568. //
  569. // prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  570. // count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  571. //
  572. // var drug_price float64
  573. // if advice.PrescribingNumberUnit == drup.MaxUnit {
  574. // deliver_number = count * drup.MinNumber
  575. //
  576. // } else {
  577. // deliver_number = count
  578. //
  579. // }
  580. //
  581. // if advice.PrescribingNumberUnit == drup.MaxUnit {
  582. // drug_price = drup.RetailPrice
  583. // }
  584. // fmt.Println(drug_price)
  585. //
  586. // if advice.PrescribingNumberUnit == drup.MinUnit {
  587. // drug_price = drup.MinPrice
  588. // }
  589. //
  590. // if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  591. // drug_price = drup.RetailPrice
  592. // }
  593. //
  594. // // 根据先进先出原则,查询最先入库的批次,进行出库
  595. // // 如果没有对应的库存,则报错
  596. // storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  597. // lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  598. // if lastWarehouse.StockMinNumber >= drup.MinNumber {
  599. // var stockMax int64
  600. // var stockMin int64
  601. // stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  602. // stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  603. //
  604. // ChangeMaxNumber(lastWarehouse.ID, stockMax)
  605. // UpdateMinNumber(lastWarehouse.ID, stockMin)
  606. // }
  607. // warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  608. //
  609. // if err != nil {
  610. //
  611. // return err
  612. // }
  613. //
  614. // // 将该批次的剩余库存数量转换为拆零数量
  615. //
  616. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  617. //
  618. // //查找药品信息
  619. // //baseInfo, _ := FindBaseDrugLibRecord(advice.UserOrgId, advice.DrugId)
  620. //
  621. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  622. //
  623. // if stock_number >= deliver_number {
  624. //
  625. // var maxNumber int64 = 0
  626. // var minNumber int64 = 0
  627. // //var stock_max_number int64 = 0
  628. // //stock_max_number = warehouse.StockMaxNumber
  629. //
  630. // maxNumber = deliver_number / drup.MinNumber
  631. // minNumber = deliver_number % drup.MinNumber
  632. //
  633. // if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  634. // minNumber = maxNumber
  635. // }
  636. //
  637. // if drup.MaxUnit != drup.MinUnit {
  638. // if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  639. // return errors.New("库存数量不足")
  640. // }
  641. // }
  642. //
  643. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  644. //
  645. // if warehouse.StockMaxNumber < 0 {
  646. // warehouse.StockMaxNumber = 0
  647. // }
  648. // if warehouse.StockMinNumber < 0 {
  649. // warehouse.StockMinNumber = 0
  650. // }
  651. //
  652. // warehouse.Mtime = time.Now().Unix()
  653. //
  654. // if warehouse.StockMinNumber < minNumber {
  655. //
  656. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  657. // warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  658. // } else {
  659. // if minNumber > 0 {
  660. // warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  661. // }
  662. //
  663. // if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  664. // if warehouse.StockMinNumber > 0 {
  665. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  666. // }
  667. //
  668. // }
  669. //
  670. // }
  671. //
  672. // if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  673. // if (warehouse.StockMinNumber - deliver_number) > 0 {
  674. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  675. // }
  676. //
  677. // }
  678. //
  679. // if drup.MaxUnit != drup.MinUnit {
  680. // if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  681. // return errors.New("库存数量不足")
  682. // }
  683. // }
  684. // if warehouse.StockMinNumber <= 0 {
  685. // warehouse.StockMinNumber = 0
  686. // }
  687. //
  688. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  689. //
  690. // if errThree != nil {
  691. // return errThree
  692. // }
  693. //
  694. // //查询剩余库存
  695. // stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  696. // var sum_count int64
  697. // for _, its := range stockInfo {
  698. // if its.MaxUnit == drup.MaxUnit {
  699. // its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  700. // }
  701. // sum_count += its.StockMaxNumber + its.StockMinNumber
  702. // }
  703. //
  704. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  705. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  706. // WarehouseOutId: warehouseout.ID,
  707. // Status: 1,
  708. // Ctime: time.Now().Unix(),
  709. // Remark: warehouse.Remark,
  710. // OrgId: orgID,
  711. // Type: 1,
  712. // Manufacturer: warehouse.Manufacturer,
  713. // Dealer: warehouse.Dealer,
  714. // IsSys: 1,
  715. // SysRecordTime: advice.RecordDate,
  716. // DrugId: advice.DrugId,
  717. // Number: warehouse.Number,
  718. // BatchNumber: warehouse.BatchNumber,
  719. // Price: warehouse.RetailPrice,
  720. // CountUnit: drup.MinUnit,
  721. // RetailPrice: warehouse.RetailPrice,
  722. // ProductDate: warehouse.ProductDate,
  723. // ExpiryDate: warehouse.ExpiryDate,
  724. // PatientId: advice.PatientId,
  725. // Count: deliver_number, //出最小单位
  726. // WarehouseInfoId: warehouse.ID,
  727. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  728. // StorehouseId: storeConfig.DrugStorehouseOut,
  729. // IsCheck: 1,
  730. // OverCount: sum_count,
  731. // }
  732. //
  733. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  734. // fmt.Println(errOne)
  735. // lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  736. // drugflow := models.DrugFlow{
  737. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  738. // WarehouseOutId: warehouseout.ID,
  739. // DrugId: advice.DrugId,
  740. // Number: warehouse.Number,
  741. // ProductDate: warehouse.ProductDate,
  742. // ExpireDate: warehouse.ExpiryDate,
  743. // Count: deliver_number, //按最小单位计算,
  744. // Price: warehouse.RetailPrice,
  745. // Status: 1,
  746. // Ctime: time.Now().Unix(),
  747. // UserOrgId: orgID,
  748. // Manufacturer: warehouse.Manufacturer,
  749. // Dealer: warehouse.Dealer,
  750. // BatchNumber: warehouse.BatchNumber,
  751. // MaxUnit: drup.MinUnit,
  752. // ConsumableType: 3,
  753. // IsEdit: 1,
  754. // Creator: advice.ExecutionStaff,
  755. // IsSys: 1,
  756. // PatientId: advice.PatientId,
  757. // SystemTime: advice.AdviceDate,
  758. // WarehousingDetailId: warehouse.ID,
  759. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  760. // StorehouseId: storeConfig.DrugStorehouseOut,
  761. // WarehouseOutDetailId: lastDrugOutInfo.ID,
  762. // LastPrice: warehouse.Price,
  763. // OverCount: sum_count,
  764. // }
  765. //
  766. // CreateDrugFlowOne(drugflow)
  767. //
  768. // //查询是否存在数据
  769. //
  770. // details := &models.DrugAutomaticReduceDetail{
  771. // WarehouseOutId: warehouseout.ID,
  772. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  773. // PatientId: advice.PatientId,
  774. // Ctime: time.Now().Unix(),
  775. // Mtime: time.Now().Unix(),
  776. // Status: 1,
  777. // RecordTime: advice.RecordDate,
  778. // OrgId: orgID,
  779. // DrugId: advice.DrugId,
  780. // Count: deliver_number,
  781. // CountUnit: drup.MinUnit,
  782. // WarehouseInfoId: warehouse.ID,
  783. // StorehouseId: storeConfig.DrugStorehouseOut,
  784. // }
  785. // errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  786. // if errTwo != nil {
  787. // return errTwo
  788. // }
  789. //
  790. // var out_count int64
  791. // //查询出库数据
  792. // infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  793. // for _, item := range infoCountList {
  794. // out_count += item.Count
  795. // }
  796. // //出库数量相加
  797. // AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  798. //
  799. // return nil
  800. // } else {
  801. //
  802. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  803. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  804. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  805. // WarehouseOutId: warehouseout.ID,
  806. // Status: 1,
  807. // Ctime: time.Now().Unix(),
  808. // Remark: warehouse.Remark,
  809. // OrgId: orgID,
  810. // Type: 1,
  811. // Manufacturer: warehouse.Manufacturer,
  812. // Dealer: warehouse.Dealer,
  813. // IsSys: 1,
  814. // SysRecordTime: advice.RecordDate,
  815. // DrugId: advice.DrugId,
  816. // Number: warehouse.Number,
  817. // BatchNumber: warehouse.BatchNumber,
  818. // Price: warehouse.RetailPrice,
  819. // CountUnit: drup.MinUnit,
  820. // ProductDate: warehouse.ProductDate,
  821. // ExpiryDate: warehouse.ExpiryDate,
  822. // PatientId: advice.PatientId,
  823. // Count: stock_number,
  824. // WarehouseInfoId: warehouse.ID,
  825. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  826. // StorehouseId: storeConfig.DrugStorehouseOut,
  827. // IsCheck: 1,
  828. // }
  829. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  830. // if errOne != nil {
  831. // return errOne
  832. // }
  833. // lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  834. // details := &models.DrugAutomaticReduceDetail{
  835. // WarehouseOutId: warehouseout.ID,
  836. // WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  837. // PatientId: advice.PatientId,
  838. // Ctime: time.Now().Unix(),
  839. // Mtime: time.Now().Unix(),
  840. // Status: 1,
  841. // RecordTime: advice.RecordDate,
  842. // OrgId: orgID,
  843. // DrugId: advice.DrugId,
  844. // Count: stock_number,
  845. // CountUnit: drup.MinUnit,
  846. // WarehouseInfoId: warehouse.ID,
  847. // StorehouseId: storeConfig.DrugStorehouseOut,
  848. // }
  849. // errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  850. // if errTwo != nil {
  851. // return errTwo
  852. // }
  853. //
  854. // info := models.XtDrugWarehouseInfo{
  855. // ID: warehouse.ID,
  856. // WarehousingId: warehouse.WarehousingId,
  857. // DrugId: warehouse.DrugId,
  858. // Number: warehouse.Number,
  859. // ProductDate: warehouse.ProductDate,
  860. // ExpiryDate: warehouse.ExpiryDate,
  861. // WarehousingCount: warehouse.WarehousingCount,
  862. // Price: warehouse.RetailPrice,
  863. // TotalPrice: warehouse.TotalPrice,
  864. // Dealer: warehouse.Dealer,
  865. // Manufacturer: warehouse.Manufacturer,
  866. // Remark: warehouse.Remark,
  867. // Ctime: warehouse.Ctime,
  868. // Mtime: warehouse.Mtime,
  869. // Status: 1,
  870. // OrgId: warehouse.OrgId,
  871. // IsReturn: warehouse.IsReturn,
  872. // WarehousingOrder: warehouse.WarehousingOrder,
  873. // Type: warehouse.Type,
  874. // RetailPrice: warehouse.RetailPrice,
  875. // RetailTotalPrice: warehouse.RetailPrice,
  876. // StockMaxNumber: 0,
  877. // StockMinNumber: 0,
  878. // BatchNumber: warehouse.BatchNumber,
  879. // MaxUnit: warehouse.MaxUnit,
  880. // WarehousingInfoId: warehouse.WarehousingInfoId,
  881. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  882. // StorehouseId: storeConfig.DrugStorehouseOut,
  883. // IsCheck: 1,
  884. // }
  885. // //扣减库存
  886. // errThree := UpDateDrugWarehouseInfoByStock(&info)
  887. //
  888. // if errThree != nil {
  889. // return errThree
  890. // }
  891. //
  892. // //查询剩余库存
  893. // stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  894. // var sum_count int64
  895. // for _, its := range stockInfo {
  896. // if its.MaxUnit == drup.MaxUnit {
  897. // its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  898. // }
  899. // sum_count += its.StockMaxNumber + its.StockMinNumber
  900. // }
  901. //
  902. // var out_count int64
  903. //
  904. // //查询出库数据
  905. // infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  906. // for _, item := range infoCountList {
  907. // out_count += item.Count
  908. // }
  909. //
  910. // drugflow := models.DrugFlow{
  911. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  912. // WarehouseOutId: warehouseout.ID,
  913. // DrugId: advice.DrugId,
  914. // Number: warehouse.Number,
  915. // ProductDate: warehouse.ProductDate,
  916. // ExpireDate: warehouse.ExpiryDate,
  917. // Count: stock_number,
  918. // Price: warehouse.RetailPrice,
  919. // Status: 1,
  920. // Ctime: time.Now().Unix(),
  921. // UserOrgId: orgID,
  922. // Manufacturer: warehouse.Manufacturer,
  923. // Dealer: warehouse.Dealer,
  924. // BatchNumber: warehouse.BatchNumber,
  925. // MaxUnit: drup.MinUnit,
  926. // ConsumableType: 3,
  927. // IsEdit: 1,
  928. // Creator: advice.ExecutionStaff,
  929. // IsSys: 1,
  930. // PatientId: advice.PatientId,
  931. // WarehousingDetailId: warehouse.ID,
  932. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  933. // StorehouseId: storeConfig.DrugStorehouseOut,
  934. // WarehouseOutDetailId: lastDrugOutInfo.ID,
  935. // LastPrice: warehouse.Price,
  936. // OverCount: sum_count,
  937. // }
  938. //
  939. // CreateDrugFlowOne(drugflow)
  940. // //出库数量相加
  941. // AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  942. //
  943. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  944. // prescribingNumber_two_temp := deliver_number - stock_number
  945. //
  946. // overPlusNumber := float64(prescribingNumber_two_temp)
  947. //
  948. // advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  949. // advice.PrescribingNumberUnit = drup.MinUnit
  950. //
  951. // DrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  952. // }
  953. //
  954. // return
  955. //}
  956. //
  957. //// 药品出库
  958. //
  959. //func HisDrugsDelivery(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo) (err error) {
  960. //
  961. // // 1.判断药品是否来自专用字典的药品库
  962. // // 2.判断当天当前机构有没有创建出库单,没有则创建
  963. // // 3.创建出库流程
  964. // // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  965. // isHasWay := false
  966. // record_time := int64(0)
  967. // isHasWay = true
  968. // record_time = advice.RecordDate
  969. //
  970. // if isHasWay {
  971. // //判断当天当前机构有没有创建出库单,没有则创建
  972. // out, err := FindDrugStockOutByIsSys(orgID, 1, record_time)
  973. // houseConfig, _ := GetAllStoreHouseConfig(orgID)
  974. // if err == gorm.ErrRecordNotFound {
  975. // timeStr := time.Now().Format("2006-01-02")
  976. // timeArr := strings.Split(timeStr, "-")
  977. // total, _ := FindAllDrugWarehouseOut(orgID)
  978. // total = total + 1
  979. // warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  980. // number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  981. // number = number + total
  982. // warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  983. //
  984. // warehouseOut := models.DrugWarehouseOut{
  985. // WarehouseOutOrderNumber: warehousing_out_order,
  986. // OperationTime: time.Now().Unix(),
  987. // OrgId: orgID,
  988. // Creater: creater,
  989. // Ctime: time.Now().Unix(),
  990. // Status: 1,
  991. // WarehouseOutTime: record_time,
  992. // Dealer: 0,
  993. // Manufacturer: 0,
  994. // Type: 1,
  995. // IsSys: 1,
  996. // StorehouseId: houseConfig.DrugStorehouseOut,
  997. // IsCheck: 1,
  998. // }
  999. // err := AddSigleDrugWarehouseOut(&warehouseOut)
  1000. // if err != nil {
  1001. // utils.TraceLog("创建出库单失败 err = %v", err)
  1002. // return err
  1003. // } else {
  1004. // out = warehouseOut
  1005. // }
  1006. // }
  1007. //
  1008. // // 出库流程
  1009. // // 1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  1010. // drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
  1011. // if drup.ID > 0 {
  1012. // prescribingNumber := advice.PrescribingNumber
  1013. // //HisDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice)
  1014. //
  1015. // BloodHisDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice)
  1016. //
  1017. // //查询该患者今日总的出库数量
  1018. // //advicelist, _ := GetAllSumDrugWarehouseOut(advice.PatientId, advice.UserOrgId, advice.AdviceDate)
  1019. // //var total_count int64
  1020. // //for _,item :=range advicelist {
  1021. // // if(item.PrescribingNumberUnit == drup.MaxUnit && drup.MaxUnit!=drup.MinUnit){
  1022. // // prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  1023. // // count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1024. // // total_count += count * drup.MinNumber
  1025. // // }
  1026. // // if(item.PrescribingNumberUnit == drup.MinUnit && drup.MaxUnit!=drup.MinUnit ){
  1027. // // prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  1028. // // count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1029. // // total_count += count
  1030. // // }
  1031. // //
  1032. // // if(item.PrescribingNumberUnit == drup.MaxUnit && drup.MaxUnit ==drup.MinUnit ){
  1033. // // prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  1034. // // count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1035. // // total_count += count
  1036. // // }
  1037. // // info := models.DrugWarehouseOutInfo{
  1038. // // Count: total_count,
  1039. // // CountUnit: drup.MinUnit,
  1040. // // }
  1041. // //
  1042. // // fmt.Println("total_coutn 233232322323232323233222322wo",total_count)
  1043. // // //更新出库单数据
  1044. // // UpdateDrugWarehouseOutByPatientId(advice.PatientId,advice.AdviceDate,advice.UserOrgId,info)
  1045. // //}
  1046. //
  1047. // } else {
  1048. // return errors.New("药品信息不存在")
  1049. // }
  1050. //
  1051. // }
  1052. //
  1053. // return
  1054. //}
  1055. //
  1056. //// 药品出库 递归方式
  1057. //func HisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  1058. //
  1059. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  1060. // var deliver_number int64 = 0
  1061. // var stock_number int64 = 0
  1062. //
  1063. // prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  1064. // count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1065. //
  1066. // if advice.PrescribingNumberUnit == drup.MaxUnit {
  1067. // deliver_number = count * drup.MinNumber
  1068. // } else {
  1069. // deliver_number = count
  1070. // }
  1071. //
  1072. // var min_price float64
  1073. //
  1074. // if advice.PrescribingNumberUnit == drup.MaxUnit {
  1075. // min_price = drup.RetailPrice
  1076. // }
  1077. // if advice.PrescribingNumberUnit == drup.MinUnit {
  1078. // min_price = drup.MinPrice
  1079. // }
  1080. //
  1081. // if advice.PrescribingNumberUnit != drup.MaxUnit && advice.PrescribingNumberUnit != drup.MinUnit {
  1082. // min_price = drup.RetailPrice
  1083. // }
  1084. // fmt.Println(min_price)
  1085. // // 根据先进先出原则,查询最先入库的批次,进行出库
  1086. // // 如果没有对应的库存,则报错
  1087. //
  1088. // //查询默认仓库
  1089. // storeConfig, _ := GetAllStoreHouseConfig(orgID)
  1090. // lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1091. // if lastWarehouse.StockMinNumber >= drup.MinNumber {
  1092. // var stockMax int64
  1093. // var stockMin int64
  1094. // stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  1095. // stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  1096. //
  1097. // ChangeMaxNumber(lastWarehouse.ID, stockMax)
  1098. // UpdateMinNumber(lastWarehouse.ID, stockMin)
  1099. // }
  1100. // warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1101. //
  1102. // if err != nil {
  1103. //
  1104. // return err
  1105. // }
  1106. //
  1107. // // 将该批次的剩余库存数量转换为拆零数量
  1108. //
  1109. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  1110. //
  1111. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1112. // if stock_number >= deliver_number {
  1113. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1114. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1115. // WarehouseOutId: warehouseout.ID,
  1116. // Status: 1,
  1117. // Ctime: time.Now().Unix(),
  1118. // Remark: warehouse.Remark,
  1119. // OrgId: orgID,
  1120. // Type: 1,
  1121. // Manufacturer: warehouse.Manufacturer,
  1122. // Dealer: warehouse.Dealer,
  1123. // IsSys: 1,
  1124. // SysRecordTime: advice.RecordDate,
  1125. // DrugId: advice.DrugId,
  1126. // Number: warehouse.Number,
  1127. // BatchNumber: warehouse.BatchNumber,
  1128. // Price: warehouse.RetailPrice,
  1129. // CountUnit: drup.MinUnit,
  1130. // RetailPrice: warehouse.RetailPrice,
  1131. // ProductDate: warehouse.ProductDate,
  1132. // ExpiryDate: warehouse.ExpiryDate,
  1133. // PatientId: advice.PatientId,
  1134. // WarehouseInfoId: warehouse.ID,
  1135. // Count: deliver_number,
  1136. // AdviceId: advice.ID,
  1137. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1138. // StorehouseId: storeConfig.DrugStorehouseOut,
  1139. // IsCheck: 1,
  1140. // LastPrice: warehouse.Price,
  1141. // }
  1142. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1143. // lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  1144. // if errOne != nil {
  1145. // return errOne
  1146. // }
  1147. // drugflow := models.DrugFlow{
  1148. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1149. // WarehouseOutId: warehouseout.ID,
  1150. // DrugId: advice.DrugId,
  1151. // Number: warehouse.Number,
  1152. // ProductDate: warehouse.ProductDate,
  1153. // ExpireDate: warehouse.ExpiryDate,
  1154. // Count: deliver_number,
  1155. // Price: warehouse.RetailPrice,
  1156. // Status: 1,
  1157. // Ctime: time.Now().Unix(),
  1158. // UserOrgId: orgID,
  1159. // Manufacturer: warehouse.Manufacturer,
  1160. // Dealer: warehouse.Dealer,
  1161. // BatchNumber: warehouse.BatchNumber,
  1162. // MaxUnit: drup.MinUnit,
  1163. // ConsumableType: 3,
  1164. // IsEdit: 1,
  1165. // Creator: advice.ExecutionStaff,
  1166. // IsSys: 1,
  1167. // PatientId: advice.PatientId,
  1168. // SystemTime: advice.AdviceDate,
  1169. // WarehousingDetailId: warehouse.ID,
  1170. // AdviceId: advice.ID,
  1171. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1172. // StorehouseId: storeConfig.DrugStorehouseOut,
  1173. // WarehouseOutDetailId: lastDrugOutInfo.ID,
  1174. // LastPrice: warehouse.Price,
  1175. // }
  1176. //
  1177. // CreateDrugFlowOne(drugflow)
  1178. //
  1179. // details := &models.DrugAutomaticReduceDetail{
  1180. // WarehouseOutId: warehouseout.ID,
  1181. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1182. // PatientId: advice.PatientId,
  1183. // Ctime: time.Now().Unix(),
  1184. // Mtime: time.Now().Unix(),
  1185. // Status: 1,
  1186. // RecordTime: advice.RecordDate,
  1187. // OrgId: orgID,
  1188. // DrugId: advice.DrugId,
  1189. // Count: deliver_number,
  1190. // CountUnit: drup.MinUnit,
  1191. // WarehouseInfoId: warehouse.ID,
  1192. // AdviceId: advice.ID,
  1193. // StorehouseId: storeConfig.DrugStorehouseOut,
  1194. // }
  1195. // errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  1196. // if errTwo != nil {
  1197. // return errTwo
  1198. // }
  1199. //
  1200. // // 出库完成后,要减去对应批次的库存数量
  1201. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  1202. // var maxNumber int64 = 0
  1203. // var minNumber int64 = 0
  1204. //
  1205. // maxNumber = deliver_number / drup.MinNumber
  1206. // minNumber = deliver_number % drup.MinNumber
  1207. //
  1208. // fmt.Println("maxNumber2323233232323232323232", maxNumber)
  1209. // fmt.Println("minNumber2323233232323232323232", minNumber)
  1210. // if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  1211. // minNumber = maxNumber
  1212. // }
  1213. //
  1214. // if drup.MaxUnit != drup.MinUnit {
  1215. // if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  1216. // return errors.New("库存数量不足")
  1217. // }
  1218. // }
  1219. //
  1220. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  1221. //
  1222. // if warehouse.StockMaxNumber < 0 {
  1223. // warehouse.StockMaxNumber = 0
  1224. // }
  1225. // if warehouse.StockMinNumber < 0 {
  1226. // warehouse.StockMinNumber = 0
  1227. // }
  1228. //
  1229. // warehouse.Mtime = time.Now().Unix()
  1230. // fmt.Println("warehouse.StockMinNumber", warehouse.StockMinNumber)
  1231. // fmt.Println("minNumber", minNumber)
  1232. // fmt.Println("maxNumber", maxNumber)
  1233. // if warehouse.StockMinNumber < minNumber {
  1234. //
  1235. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  1236. // if warehouse.MaxUnit != warehouse.MinUnit {
  1237. //
  1238. // warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  1239. // }
  1240. // } else {
  1241. //
  1242. // if minNumber > 0 {
  1243. // warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  1244. // }
  1245. //
  1246. // }
  1247. //
  1248. // if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  1249. // if (warehouse.StockMinNumber - deliver_number) > 0 {
  1250. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  1251. // }
  1252. // }
  1253. //
  1254. // if drup.MaxUnit != drup.MinUnit {
  1255. // if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  1256. // return errors.New("库存数量不足")
  1257. // }
  1258. // }
  1259. // if warehouse.StockMinNumber <= 0 {
  1260. // warehouse.StockMinNumber = 0
  1261. // }
  1262. // //扣减库存
  1263. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  1264. //
  1265. // if errThree != nil {
  1266. // return errThree
  1267. // }
  1268. // drugInfoList, _ := FindDrugWarehouseInfoList(advice.DrugId, orgID)
  1269. // var drug_max_number int64
  1270. // var drug_min_number int64
  1271. // for _, item := range drugInfoList {
  1272. // drug_max_number += item.StockMaxNumber
  1273. // drug_min_number += item.StockMinNumber
  1274. // }
  1275. // drugFlushInfo := models.XtDrugWarehouseFlushInfo{
  1276. // DrugMaxNumber: drug_max_number,
  1277. // UserOrgId: orgID,
  1278. // WarehouseOutId: warehouseout.ID,
  1279. // Type: 1,
  1280. // SystemTime: advice.RecordDate,
  1281. // PatientId: advice.PatientId,
  1282. // Ctime: time.Now().Unix(),
  1283. // Mtime: 0,
  1284. // BatchNumberId: warehouse.ID,
  1285. // DrugId: advice.DrugId,
  1286. // Count: deliver_number,
  1287. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1288. // Creater: warehouseout.Creater,
  1289. // DrugMinNumber: drug_min_number,
  1290. // Unit: drup.MinUnit,
  1291. // AdviceId: advice.ID,
  1292. // Status: 1,
  1293. // WarehouseOutDetailId: lastDrugOutInfo.ID,
  1294. // }
  1295. // CreatedDrugFlushInfo(drugFlushInfo)
  1296. //
  1297. // return nil
  1298. // } else {
  1299. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  1300. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1301. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1302. // WarehouseOutId: warehouseout.ID,
  1303. // Status: 1,
  1304. // Ctime: time.Now().Unix(),
  1305. // Remark: warehouse.Remark,
  1306. // OrgId: orgID,
  1307. // Type: 1,
  1308. // Manufacturer: warehouse.Manufacturer,
  1309. // Dealer: warehouse.Dealer,
  1310. // IsSys: 1,
  1311. // SysRecordTime: advice.RecordDate,
  1312. // DrugId: advice.DrugId,
  1313. // Number: warehouse.Number,
  1314. // BatchNumber: warehouse.BatchNumber,
  1315. // Price: warehouse.RetailPrice,
  1316. // RetailPrice: warehouse.RetailPrice,
  1317. // ProductDate: warehouse.ProductDate,
  1318. // ExpiryDate: warehouse.ExpiryDate,
  1319. // PatientId: advice.PatientId,
  1320. // WarehouseInfoId: warehouse.ID,
  1321. // Count: stock_number,
  1322. // CountUnit: drup.MinUnit,
  1323. // AdviceId: advice.ID,
  1324. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1325. // StorehouseId: storeConfig.DrugStorehouseOut,
  1326. // IsCheck: 1,
  1327. // }
  1328. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1329. //
  1330. // if errOne != nil {
  1331. // return errOne
  1332. // }
  1333. // lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  1334. // drugflow := models.DrugFlow{
  1335. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1336. // WarehouseOutId: warehouseout.ID,
  1337. // DrugId: advice.DrugId,
  1338. // Number: warehouse.Number,
  1339. // ProductDate: warehouse.ProductDate,
  1340. // ExpireDate: warehouse.ExpiryDate,
  1341. // Count: stock_number,
  1342. // Price: warehouse.RetailPrice,
  1343. // Status: 1,
  1344. // Ctime: time.Now().Unix(),
  1345. // UserOrgId: orgID,
  1346. // Manufacturer: warehouse.Manufacturer,
  1347. // Dealer: warehouse.Dealer,
  1348. // BatchNumber: warehouse.BatchNumber,
  1349. // MaxUnit: drup.MinUnit,
  1350. // ConsumableType: 3,
  1351. // IsEdit: 1,
  1352. // Creator: advice.ExecutionStaff,
  1353. // IsSys: 1,
  1354. // PatientId: advice.PatientId,
  1355. // SystemTime: advice.AdviceDate,
  1356. // WarehousingDetailId: warehouse.ID,
  1357. // AdviceId: advice.ID,
  1358. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1359. // StorehouseId: storeConfig.DrugStorehouseOut,
  1360. // WarehouseOutDetailId: lastDrugOutInfo.ID,
  1361. // LastPrice: warehouse.Price,
  1362. // }
  1363. //
  1364. // CreateDrugFlowOne(drugflow)
  1365. //
  1366. // //查询是否存在数据
  1367. //
  1368. // details := &models.DrugAutomaticReduceDetail{
  1369. // WarehouseOutId: warehouseout.ID,
  1370. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1371. // PatientId: advice.PatientId,
  1372. // Ctime: time.Now().Unix(),
  1373. // Mtime: time.Now().Unix(),
  1374. // Status: 1,
  1375. // RecordTime: advice.RecordDate,
  1376. // OrgId: orgID,
  1377. // DrugId: advice.DrugId,
  1378. // Count: stock_number,
  1379. // CountUnit: drup.MinUnit,
  1380. // WarehouseInfoId: warehouse.ID,
  1381. // AdviceId: advice.ID,
  1382. // StorehouseId: storeConfig.DrugStorehouseOut,
  1383. // }
  1384. // errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  1385. // if errTwo != nil {
  1386. // return errTwo
  1387. // }
  1388. //
  1389. // info := models.XtDrugWarehouseInfo{
  1390. // ID: warehouse.ID,
  1391. // WarehousingId: warehouse.WarehousingId,
  1392. // DrugId: warehouse.DrugId,
  1393. // Number: warehouse.Number,
  1394. // ProductDate: warehouse.ProductDate,
  1395. // ExpiryDate: warehouse.ExpiryDate,
  1396. // WarehousingCount: warehouse.WarehousingCount,
  1397. // Price: warehouse.Price,
  1398. // TotalPrice: warehouse.TotalPrice,
  1399. // Dealer: warehouse.Dealer,
  1400. // Manufacturer: warehouse.Manufacturer,
  1401. // Remark: warehouse.Remark,
  1402. // Ctime: warehouse.Ctime,
  1403. // Mtime: warehouse.Mtime,
  1404. // Status: 1,
  1405. // OrgId: warehouse.OrgId,
  1406. // IsReturn: warehouse.IsReturn,
  1407. // WarehousingOrder: warehouse.WarehousingOrder,
  1408. // Type: warehouse.Type,
  1409. // RetailPrice: warehouse.RetailPrice,
  1410. // RetailTotalPrice: warehouse.RetailPrice,
  1411. // StockMaxNumber: 0,
  1412. // StockMinNumber: 0,
  1413. // BatchNumber: warehouse.BatchNumber,
  1414. // MaxUnit: warehouse.MaxUnit,
  1415. // MinUnit: warehouse.MinUnit,
  1416. // WarehousingInfoId: warehouse.WarehousingInfoId,
  1417. // StorehouseId: storeConfig.DrugStorehouseOut,
  1418. // IsCheck: 1,
  1419. // }
  1420. // errThree := UpDateDrugWarehouseInfoByStock(&info)
  1421. //
  1422. // if errThree != nil {
  1423. // return errThree
  1424. // }
  1425. //
  1426. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1427. // prescribingNumber_two_temp := deliver_number - stock_number
  1428. //
  1429. // overPlusNumber := float64(prescribingNumber_two_temp)
  1430. //
  1431. // advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  1432. //
  1433. // advice.PrescribingNumberUnit = drup.MinUnit
  1434. //
  1435. // HisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  1436. // }
  1437. //
  1438. // return
  1439. //}
  1440. //
  1441. //// 药品出库 递归方式
  1442. //func AutoDrugDeliverInfo(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  1443. //
  1444. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  1445. // var deliver_number int64 = 0
  1446. // var stock_number int64 = 0
  1447. //
  1448. // if advice.CountUnit == drup.MaxUnit {
  1449. // deliver_number = prescribingNumber * drup.MinNumber
  1450. // } else {
  1451. // deliver_number = prescribingNumber
  1452. // }
  1453. //
  1454. // // 根据先进先出原则,查询最先入库的批次,进行出库
  1455. // // 如果没有对应的库存,则报错
  1456. // //开启事物
  1457. //
  1458. // storeConfig, _ := GetAllStoreHouseConfig(orgID)
  1459. // lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1460. // if lastWarehouse.StockMinNumber >= drup.MinNumber {
  1461. // var stockMax int64
  1462. // var stockMin int64
  1463. // stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  1464. // stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  1465. // ChangeMaxNumber(lastWarehouse.ID, stockMax)
  1466. // UpdateMinNumber(lastWarehouse.ID, stockMin)
  1467. // }
  1468. //
  1469. // warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1470. //
  1471. // if err != nil {
  1472. //
  1473. // return err
  1474. // }
  1475. //
  1476. // // 将该批次的剩余库存数量转换为拆零数量
  1477. //
  1478. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  1479. //
  1480. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1481. // if stock_number >= deliver_number {
  1482. //
  1483. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1484. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1485. // WarehouseOutId: warehouseout.ID,
  1486. // Status: 1,
  1487. // Ctime: time.Now().Unix(),
  1488. // Remark: advice.Remark,
  1489. // OrgId: orgID,
  1490. // Type: 1,
  1491. // Manufacturer: advice.Manufacturer,
  1492. // Dealer: warehouse.Dealer,
  1493. // IsSys: 0,
  1494. // SysRecordTime: advice.Ctime,
  1495. // DrugId: advice.DrugId,
  1496. // ExpiryDate: advice.ExpiryDate,
  1497. // ProductDate: advice.ProductDate,
  1498. // Number: advice.Number,
  1499. // BatchNumber: warehouse.BatchNumber,
  1500. // Count: deliver_number,
  1501. // RetailPrice: advice.RetailPrice,
  1502. // Price: warehouse.RetailPrice,
  1503. // WarehouseInfoId: warehouse.ID,
  1504. // CountUnit: drup.MinUnit,
  1505. // AdviceId: advice.ID,
  1506. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1507. // StorehouseId: advice.StorehouseId,
  1508. // AdminUserId: advice.AdminUserId,
  1509. // IsCheck: 1,
  1510. // }
  1511. //
  1512. // warehouseOutInfo.Count = prescribingNumber
  1513. // warehouseOutInfo.CountUnit = advice.CountUnit
  1514. //
  1515. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1516. // if errOne != nil {
  1517. // return errOne
  1518. // }
  1519. //
  1520. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  1521. // drugflow := models.DrugFlow{
  1522. // WarehouseOutId: warehouseout.ID,
  1523. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1524. // DrugId: advice.DrugId,
  1525. // Number: warehouse.Number,
  1526. // ProductDate: advice.ProductDate,
  1527. // ExpireDate: advice.ExpiryDate,
  1528. // Count: deliver_number,
  1529. // Price: warehouse.RetailPrice,
  1530. // Status: 1,
  1531. // Ctime: time.Now().Unix(),
  1532. // UserOrgId: advice.OrgId,
  1533. // Manufacturer: advice.Manufacturer,
  1534. // Dealer: advice.Dealer,
  1535. // BatchNumber: warehouse.BatchNumber,
  1536. // MaxUnit: drup.MinUnit,
  1537. // ConsumableType: 2,
  1538. // IsEdit: 1,
  1539. // Creator: 0,
  1540. // IsSys: 0,
  1541. // WarehouseOutDetailId: drugWareInfo.ID,
  1542. // AdviceId: advice.ID,
  1543. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1544. // StorehouseId: advice.StorehouseId,
  1545. // AdminUserId: advice.AdminUserId,
  1546. // }
  1547. //
  1548. // CreateDrugFlowOne(drugflow)
  1549. // // 出库完成后,要减去对应批次的库存数量
  1550. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  1551. // var maxNumber int64 = 0
  1552. // var minNumber int64 = 0
  1553. // if advice.CountUnit == drup.MinUnit {
  1554. // maxNumber = prescribingNumber / drup.MinNumber
  1555. // minNumber = prescribingNumber % drup.MinNumber
  1556. // if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  1557. // minNumber = maxNumber
  1558. // }
  1559. // } else {
  1560. // maxNumber = prescribingNumber
  1561. // }
  1562. //
  1563. // if warehouse.StockMaxNumber < maxNumber {
  1564. //
  1565. // return errors.New("库存数量不足")
  1566. // }
  1567. //
  1568. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  1569. // warehouse.Mtime = time.Now().Unix()
  1570. //
  1571. // if warehouse.StockMinNumber < minNumber {
  1572. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  1573. // warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  1574. // } else {
  1575. // warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  1576. // }
  1577. //
  1578. // if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
  1579. // warehouse.StockMinNumber = 0
  1580. // }
  1581. //
  1582. // if warehouse.StockMaxNumber < 0 {
  1583. // return errors.New("库存数量不足")
  1584. // }
  1585. //
  1586. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  1587. // if errThree != nil {
  1588. // return errThree
  1589. // }
  1590. //
  1591. // //查询默认仓库
  1592. // houseConfig, _ := GetAllStoreHouseConfig(orgID)
  1593. // //查询默认仓库剩余多少库存
  1594. // list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  1595. // var sum_count int64
  1596. // var sum_in_count int64
  1597. // for _, it := range list {
  1598. // baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  1599. // if it.MaxUnit == baseDrug.MaxUnit {
  1600. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  1601. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  1602. // }
  1603. // sum_count += it.StockMaxNumber + it.StockMinNumber
  1604. // sum_in_count += it.WarehousingCount
  1605. // }
  1606. // UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  1607. //
  1608. // return nil
  1609. //
  1610. // } else {
  1611. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  1612. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1613. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1614. // WarehouseOutId: warehouseout.ID,
  1615. // Status: 1,
  1616. // Ctime: time.Now().Unix(),
  1617. // Remark: advice.Remark,
  1618. // OrgId: orgID,
  1619. // Type: 1,
  1620. // Manufacturer: advice.Manufacturer,
  1621. // Dealer: warehouse.Dealer,
  1622. // IsSys: 0,
  1623. // SysRecordTime: advice.Ctime,
  1624. // DrugId: advice.DrugId,
  1625. // ExpiryDate: warehouse.ExpiryDate,
  1626. // ProductDate: warehouse.ProductDate,
  1627. // Number: warehouse.Number,
  1628. // BatchNumber: warehouse.BatchNumber,
  1629. // Count: stock_number,
  1630. // RetailPrice: advice.RetailPrice,
  1631. // Price: warehouse.RetailPrice,
  1632. // WarehouseInfoId: warehouse.ID,
  1633. // CountUnit: drup.MinUnit,
  1634. // AdviceId: advice.ID,
  1635. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1636. // StorehouseId: advice.StorehouseId,
  1637. // AdminUserId: advice.AdminUserId,
  1638. // IsCheck: 1,
  1639. // }
  1640. // warehouseOutInfo.Count = stock_number
  1641. //
  1642. // //warehouseOutInfo.CountUnit = advice.CountUnit
  1643. //
  1644. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1645. // if errOne != nil {
  1646. // return errOne
  1647. // }
  1648. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  1649. // drugflow := models.DrugFlow{
  1650. // WarehouseOutId: warehouseout.ID,
  1651. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1652. // DrugId: advice.DrugId,
  1653. // Number: warehouse.Number,
  1654. // ProductDate: advice.ProductDate,
  1655. // ExpireDate: advice.ExpiryDate,
  1656. // Count: stock_number,
  1657. // Price: warehouse.RetailPrice,
  1658. // Status: 1,
  1659. // Ctime: time.Now().Unix(),
  1660. // UserOrgId: advice.OrgId,
  1661. // Manufacturer: advice.Manufacturer,
  1662. // Dealer: advice.Dealer,
  1663. // BatchNumber: warehouse.BatchNumber,
  1664. // MaxUnit: drup.MinUnit,
  1665. // ConsumableType: 2,
  1666. // IsEdit: 1,
  1667. // Creator: 0,
  1668. // IsSys: 0,
  1669. // WarehouseOutDetailId: drugWareInfo.ID,
  1670. // AdviceId: advice.ID,
  1671. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1672. // StorehouseId: advice.StorehouseId,
  1673. // AdminUserId: advice.AdminUserId,
  1674. // }
  1675. // CreateDrugFlowOne(drugflow)
  1676. // // 出库完成后,要将该批次库存清零
  1677. // //扣减库存
  1678. //
  1679. // warehouse.StockMaxNumber = 0
  1680. // warehouse.StockMinNumber = 0
  1681. // warehouse.Mtime = time.Now().Unix()
  1682. //
  1683. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  1684. //
  1685. // //查询默认仓库
  1686. // houseConfig, _ := GetAllStoreHouseConfig(orgID)
  1687. // //查询默认仓库剩余多少库存
  1688. // list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  1689. // var sum_count int64
  1690. // var sum_in_count int64
  1691. // for _, it := range list {
  1692. // baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  1693. // if it.MaxUnit == baseDrug.MaxUnit {
  1694. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  1695. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  1696. // sum_count += it.StockMaxNumber + it.StockMinNumber
  1697. // sum_in_count += it.WarehousingCount
  1698. // }
  1699. // }
  1700. // UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  1701. // if errThree != nil {
  1702. // return errThree
  1703. // }
  1704. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1705. // prescribingNumber_two_temp := deliver_number - stock_number
  1706. //
  1707. // advice.CountUnit = drup.MinUnit
  1708. //
  1709. // AutoDrugDeliverInfo(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  1710. // }
  1711. //
  1712. // return
  1713. //}
  1714. //
  1715. //// 耗材出库
  1716. //func ConsumablesDeliveryTotal(orgID int64, patient_id int64, record_time int64, goods []*models.DialysisBeforePrepareGoods, goodOne []*models.NewDialysisBeforePrepareGoods, creator int64) (err error) {
  1717. //
  1718. // //查询该患者当天已经出库的耗材信息
  1719. // goods_yc, _ := FindConsumablesByDateTwo(orgID, patient_id, record_time)
  1720. // // 和新请求的出库数据进行对比,分出那些是继续出库的,那些是需要删除出库的
  1721. // for i := len(goods_yc) - 1; i >= 0; i-- {
  1722. // goods_yc_temp := goods_yc[i]
  1723. // for j := len(goods) - 1; j >= 0; j-- {
  1724. // goods_temp := goods[j]
  1725. // // 已经出库和新请求出库都存在该耗材,则判断出库数量,分成是继续出库,还是删除出库
  1726. // if goods_yc_temp.GoodTypeId == goods_temp.GoodTypeId && goods_yc_temp.GoodId == goods_temp.GoodId {
  1727. // // 已经出库和新请求出库的出库数量一致,则清除两个结构体里的数据(既不出库,也不删除出库)
  1728. // if goods_yc_temp.Count == goods_temp.Count {
  1729. // goods_yc = append(goods_yc[:i], goods_yc[i+1:]...)
  1730. // goods = append(goods[:j], goods[j+1:]...)
  1731. // break
  1732. // }
  1733. // // 如果已经出库的数量 大于 新请求出库的数量,则代表需要删除出库
  1734. // if goods_yc_temp.Count > goods_temp.Count {
  1735. //
  1736. // temp_count := goods_yc_temp.Count - goods_temp.Count
  1737. // goods_yc[i].Count = temp_count
  1738. // goods = append(goods[:j], goods[j+1:]...)
  1739. // break
  1740. // }
  1741. // // 如果已经出库的数量 小于 新请求出库的梳理,则代表需要增加出库
  1742. // if goods_yc_temp.Count < goods_temp.Count {
  1743. // temp_count := goods_temp.Count - goods_yc_temp.Count
  1744. //
  1745. // goods[j].Count = temp_count
  1746. // goods_yc = append(goods_yc[:i], goods_yc[i+1:]...)
  1747. //
  1748. // break
  1749. // }
  1750. // }
  1751. // }
  1752. // }
  1753. //
  1754. // // goods_yc 这个数据就是需要已经出库了,但是现在需要删除出库的耗材数据
  1755. // // goods 这个数据就是需要出库的耗材的数据(新增的数据)
  1756. //
  1757. // if len(goods) > 0 {
  1758. // out, err := FindStockOutByIsSys(orgID, 1, record_time)
  1759. // houseConfig, _ := GetAllStoreHouseConfig(orgID)
  1760. // if err == gorm.ErrRecordNotFound {
  1761. // //没有记录,则创建出库单
  1762. // timeStr := time.Now().Format("2006-01-02")
  1763. // timeArr := strings.Split(timeStr, "-")
  1764. // total, _ := FindAllWarehouseOut(orgID)
  1765. // total = total + 1
  1766. // warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  1767. // number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  1768. // number = number + total
  1769. // warehousing_out_order = "CKD" + strconv.FormatInt(number, 10)
  1770. //
  1771. // warehouseOut := models.WarehouseOut{
  1772. // WarehouseOutOrderNumber: warehousing_out_order,
  1773. // OperationTime: time.Now().Unix(),
  1774. // OrgId: orgID,
  1775. // Creater: creator,
  1776. // Ctime: time.Now().Unix(),
  1777. // Status: 1,
  1778. // WarehouseOutTime: record_time,
  1779. // Dealer: 0,
  1780. // Manufacturer: 0,
  1781. // Type: 1,
  1782. // IsSys: 1,
  1783. // StorehouseId: houseConfig.StorehouseOutInfo,
  1784. // IsCheck: 1,
  1785. // }
  1786. // err := AddSigleWarehouseOut(&warehouseOut)
  1787. // if err != nil {
  1788. // utils.TraceLog("创建出库单失败 err = %v", err)
  1789. // return err
  1790. // } else {
  1791. // out = warehouseOut
  1792. // }
  1793. // }
  1794. //
  1795. // for _, item := range goods {
  1796. // var newCount int64 = 0
  1797. // for _, it := range goodOne {
  1798. // if item.GoodTypeId == it.GoodTypeId && item.GoodId == it.GoodId {
  1799. // newCount = it.Count
  1800. // }
  1801. // }
  1802. //
  1803. // var cha_count int64
  1804. // var cha_count_two int64
  1805. // flowGood, _ := GetStockFlowIsBatchNumberTwo(patient_id, record_time, item.GoodId)
  1806. // var out_count int64
  1807. // for _, item := range flowGood {
  1808. // out_count += item.Count
  1809. // }
  1810. // cha_count = item.Count - out_count
  1811. // //fmt.Println("item.Count", item.Count)
  1812. // //fmt.Println("item.Count", out_count)
  1813. // //fmt.Println("cha_count2323223232323223", cha_count)
  1814. // cha_count_two = out_count - item.Count
  1815. //
  1816. // prepare := models.DialysisBeforePrepare{
  1817. // GoodTypeId: item.GoodTypeId,
  1818. // GoodId: item.GoodId,
  1819. // Count: cha_count,
  1820. // ProjectId: item.ProjectId,
  1821. // StorehouseId: houseConfig.StorehouseOutInfo,
  1822. // }
  1823. //
  1824. // ConsumablesGoodDelivery(orgID, patient_id, record_time, &prepare, &out, newCount)
  1825. // //数量增加
  1826. // if cha_count > 0 {
  1827. // //出库数量累加
  1828. // ModifyGoodSumCount(houseConfig.StorehouseOutInfo, cha_count, orgID, item.GoodId)
  1829. // }
  1830. // //数量减少
  1831. // if cha_count_two > 0 {
  1832. // ModifyAddGoodSumCount(houseConfig.StorehouseOutInfo, cha_count_two, orgID, item.GoodId)
  1833. // //退库数量增加
  1834. // UpdateSumAddCancelCount(orgID, item.GoodId, houseConfig.StorehouseOutInfo, cha_count_two)
  1835. //
  1836. // }
  1837. // //更新剩余库存
  1838. // goodListSix, _ := GetSumGoodList(orgID, houseConfig.StorehouseOutInfo, item.GoodId)
  1839. // var flush_count int64
  1840. // for _, it := range goodListSix {
  1841. // flush_count += it.StockCount
  1842. // }
  1843. // UpdateSumGood(orgID, houseConfig.StorehouseOutInfo, item.GoodId, flush_count)
  1844. //
  1845. // }
  1846. //
  1847. // }
  1848. // fmt.Println("goods_yc2323323223323223232323232332wode", goods_yc)
  1849. // if len(goods_yc) > 0 {
  1850. // for _, good_yc := range goods_yc {
  1851. // out, _ := FindStockOutByIsSys(orgID, 1, record_time)
  1852. // ConsumablesDeliveryDelete(orgID, patient_id, record_time, good_yc, &out)
  1853. //
  1854. // }
  1855. // }
  1856. //
  1857. // return nil
  1858. //}
  1859. //
  1860. ////耗材出库
  1861. //func ConsumablesDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, count int64) (err error) {
  1862. //
  1863. // //开事务
  1864. // var deliver_number int64 = 0
  1865. // var stock_number int64 = 0
  1866. // var maxNumber int64 = 0
  1867. //
  1868. // deliver_number = goods.Count
  1869. //
  1870. // // 根据先进先出原则,查询最先入库的批次,进行出库
  1871. // // 如果没有对应的库存,则报错
  1872. //
  1873. // warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  1874. // if err != nil {
  1875. // return errors.New("库存数量不足")
  1876. // }
  1877. //
  1878. // stock_number = warehouse.StockCount
  1879. //
  1880. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1881. // if stock_number >= deliver_number {
  1882. //
  1883. // maxNumber = goods.Count
  1884. // //出库
  1885. // warehouse.StockCount = warehouse.StockCount - maxNumber
  1886. // warehouse.Mtime = time.Now().Unix()
  1887. // if warehouse.StockCount < 0 {
  1888. // return errors.New("库存数量不足")
  1889. // }
  1890. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  1891. //
  1892. // if errThree != nil {
  1893. // return errThree
  1894. // }
  1895. // //查询剩余库存
  1896. // goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID)
  1897. // var sum_count int64
  1898. // for _, item := range goodList {
  1899. // sum_count += item.StockCount
  1900. // }
  1901. //
  1902. // warehouseOutInfo := &models.WarehouseOutInfo{
  1903. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1904. // WarehouseOutId: warehouseOut.ID,
  1905. // WarehouseInfotId: warehouse.ID,
  1906. // Status: 1,
  1907. // Ctime: time.Now().Unix(),
  1908. // Remark: warehouse.Remark,
  1909. // OrgId: orgID,
  1910. // Type: 1,
  1911. // Manufacturer: warehouse.Manufacturer,
  1912. // Dealer: warehouse.Dealer,
  1913. // IsSys: 1,
  1914. // SysRecordTime: record_time,
  1915. // GoodTypeId: goods.GoodTypeId,
  1916. // GoodId: goods.GoodId,
  1917. // PatientId: patient_id,
  1918. // Number: warehouse.Number,
  1919. // LicenseNumber: warehouse.LicenseNumber,
  1920. // Price: warehouse.PackingPrice,
  1921. // ExpiryDate: warehouse.ExpiryDate,
  1922. // ProductDate: warehouse.ProductDate,
  1923. // ProjectId: goods.ProjectId,
  1924. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1925. // StorehouseId: goods.StorehouseId,
  1926. // IsCheck: 1,
  1927. // }
  1928. // warehouseOutInfo.Count = count
  1929. //
  1930. // _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
  1931. // if errcodes == gorm.ErrRecordNotFound {
  1932. // errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  1933. // if errOne != nil {
  1934. // return errOne
  1935. // }
  1936. // } else if errcodes == nil {
  1937. // UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId)
  1938. // }
  1939. //
  1940. // details := models.BloodAutomaticReduceDetail{
  1941. // WarehouseOutId: warehouseOutInfo.ID,
  1942. // WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  1943. // PatientId: patient_id,
  1944. // Ctime: time.Now().Unix(),
  1945. // Mtime: time.Now().Unix(),
  1946. // Status: 1,
  1947. // RecordTime: record_time,
  1948. // OrgId: orgID,
  1949. // GoodId: goods.GoodId,
  1950. // GoodTypeId: goods.GoodTypeId,
  1951. // Count: count,
  1952. // ProjectId: goods.ProjectId,
  1953. // StorehouseId: goods.StorehouseId,
  1954. // }
  1955. // //查询当天耗材是否已经存在数据
  1956. // _, errcode := GetAutoMaticReduceDetail(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  1957. // if errcode == gorm.ErrRecordNotFound {
  1958. // errTwo := CreateAutoReduceRecord(&details)
  1959. // if errTwo != nil {
  1960. // return errTwo
  1961. // }
  1962. // //插入库存流水表
  1963. // flow := models.VmStockFlow{
  1964. // WarehouseOutId: warehouseOut.ID,
  1965. // WarehousingId: warehouse.ID,
  1966. // GoodId: goods.GoodId,
  1967. // Number: warehouse.Number,
  1968. // ProductDate: warehouse.ProductDate,
  1969. // ExpireDate: warehouse.ExpiryDate,
  1970. // Count: deliver_number,
  1971. // Price: warehouse.PackingPrice,
  1972. // Status: 1,
  1973. // Ctime: time.Now().Unix(),
  1974. // UserOrgId: orgID,
  1975. // Manufacturer: warehouse.Manufacturer,
  1976. // Dealer: warehouse.Dealer,
  1977. // LicenseNumber: warehouse.LicenseNumber,
  1978. // IsEdit: 2,
  1979. // Creator: warehouseOut.Creater,
  1980. // SystemTime: record_time,
  1981. // ConsumableType: 3,
  1982. // WarehouseOutDetailId: warehouseOutInfo.ID,
  1983. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1984. // IsSys: 1,
  1985. // PatientId: patient_id,
  1986. // ProjectId: goods.ProjectId,
  1987. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1988. // StorehouseId: goods.StorehouseId,
  1989. // OverCount: sum_count,
  1990. // }
  1991. //
  1992. // //查询流水是否存在
  1993. // exsit, errflow := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  1994. //
  1995. // if errflow == gorm.ErrRecordNotFound {
  1996. //
  1997. // //创建数据
  1998. // errThre := CreateStockFlowOne(flow)
  1999. // if errThre != nil {
  2000. // return errThre
  2001. // }
  2002. // } else if errflow == nil {
  2003. // flow := models.VmStockFlow{
  2004. // ID: exsit.ID,
  2005. // WarehouseOutId: warehouseOut.ID,
  2006. // WarehousingId: warehouse.ID,
  2007. // GoodId: goods.GoodId,
  2008. // Number: warehouse.Number,
  2009. // ProductDate: warehouse.ProductDate,
  2010. // ExpireDate: warehouse.ExpiryDate,
  2011. // Count: exsit.Count + goods.Count,
  2012. // Price: warehouse.PackingPrice,
  2013. // Status: 1,
  2014. // Ctime: time.Now().Unix(),
  2015. // UserOrgId: orgID,
  2016. // Manufacturer: warehouse.Manufacturer,
  2017. // Dealer: warehouse.Dealer,
  2018. // LicenseNumber: warehouse.LicenseNumber,
  2019. // IsEdit: 2,
  2020. // Creator: warehouseOut.Creater,
  2021. // SystemTime: record_time,
  2022. // ConsumableType: 3,
  2023. // WarehouseOutDetailId: warehouseOutInfo.ID,
  2024. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2025. // IsSys: 1,
  2026. // PatientId: patient_id,
  2027. // ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  2028. // ProjectId: goods.ProjectId,
  2029. // StorehouseId: goods.StorehouseId,
  2030. // OverCount: sum_count,
  2031. // }
  2032. //
  2033. // errFour := UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  2034. // if errFour != nil {
  2035. // return errFour
  2036. // }
  2037. // }
  2038. //
  2039. // } else if errcode == nil {
  2040. // flow := models.VmStockFlow{
  2041. // WarehouseOutId: warehouseOut.ID,
  2042. // WarehousingId: warehouse.ID,
  2043. // GoodId: goods.GoodId,
  2044. // Number: warehouse.Number,
  2045. // ProductDate: warehouse.ProductDate,
  2046. // ExpireDate: warehouse.ExpiryDate,
  2047. // Count: deliver_number,
  2048. // Price: warehouse.PackingPrice,
  2049. // Status: 1,
  2050. // Ctime: time.Now().Unix(),
  2051. // UserOrgId: orgID,
  2052. // Manufacturer: warehouse.Manufacturer,
  2053. // Dealer: warehouse.Dealer,
  2054. // LicenseNumber: warehouse.LicenseNumber,
  2055. // IsEdit: 2,
  2056. // Creator: warehouseOut.Creater,
  2057. // SystemTime: record_time,
  2058. // ConsumableType: 3,
  2059. // WarehouseOutDetailId: warehouseOutInfo.ID,
  2060. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2061. // IsSys: 1,
  2062. // PatientId: patient_id,
  2063. // ProjectId: goods.ProjectId,
  2064. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2065. // StorehouseId: goods.StorehouseId,
  2066. // OverCount: sum_count,
  2067. // }
  2068. // //查询流水是否存在
  2069. // exsit, errflow := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  2070. // if errflow == gorm.ErrRecordNotFound {
  2071. //
  2072. // CreateStockFlowOne(flow)
  2073. // DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  2074. // CreateAutoReduceRecord(&details)
  2075. //
  2076. // } else if errflow == nil {
  2077. // flow := models.VmStockFlow{
  2078. // ID: exsit.ID,
  2079. // WarehouseOutId: warehouseOut.ID,
  2080. // WarehousingId: warehouse.ID,
  2081. // GoodId: goods.GoodId,
  2082. // Number: warehouse.Number,
  2083. // ProductDate: warehouse.ProductDate,
  2084. // ExpireDate: warehouse.ExpiryDate,
  2085. // Count: exsit.Count + goods.Count,
  2086. // Price: warehouse.PackingPrice,
  2087. // Status: 1,
  2088. // Ctime: time.Now().Unix(),
  2089. // UserOrgId: orgID,
  2090. // Manufacturer: warehouse.Manufacturer,
  2091. // Dealer: warehouse.Dealer,
  2092. // LicenseNumber: warehouse.LicenseNumber,
  2093. // IsEdit: 2,
  2094. // Creator: warehouseOut.Creater,
  2095. // SystemTime: record_time,
  2096. // ConsumableType: 3,
  2097. // WarehouseOutDetailId: warehouseOutInfo.ID,
  2098. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2099. // IsSys: 1,
  2100. // PatientId: patient_id,
  2101. // ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  2102. // ProjectId: goods.ProjectId,
  2103. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2104. // StorehouseId: goods.StorehouseId,
  2105. // OverCount: sum_count,
  2106. // }
  2107. // UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  2108. // DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  2109. // CreateAutoReduceRecord(&details)
  2110. // }
  2111. //
  2112. // }
  2113. //
  2114. // return nil
  2115. // } else {
  2116. //
  2117. // // 当刚批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2118. // warehouse.StockCount = 0
  2119. // warehouse.Mtime = time.Now().Unix()
  2120. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  2121. // if errThree != nil {
  2122. // return errThree
  2123. // }
  2124. // goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID)
  2125. // var sum_count int64
  2126. // for _, item := range goodList {
  2127. // sum_count += item.StockCount
  2128. // }
  2129. // warehouseOutInfo := &models.WarehouseOutInfo{
  2130. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2131. // WarehouseOutId: warehouseOut.ID,
  2132. // WarehouseInfotId: warehouse.ID,
  2133. // Status: 1,
  2134. // Ctime: time.Now().Unix(),
  2135. // Remark: warehouse.Remark,
  2136. // OrgId: orgID,
  2137. // Type: 1,
  2138. // Manufacturer: warehouse.Manufacturer,
  2139. // Dealer: warehouse.Dealer,
  2140. // IsSys: 1,
  2141. // SysRecordTime: record_time,
  2142. // GoodTypeId: goods.GoodTypeId,
  2143. // GoodId: goods.GoodId,
  2144. // PatientId: patient_id,
  2145. // Number: warehouse.Number,
  2146. // LicenseNumber: warehouse.LicenseNumber,
  2147. // Price: warehouse.PackingPrice,
  2148. // ExpiryDate: warehouse.ExpiryDate,
  2149. // ProductDate: warehouse.ProductDate,
  2150. // ProjectId: goods.ProjectId,
  2151. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2152. // StorehouseId: goods.StorehouseId,
  2153. // IsCheck: 1,
  2154. // }
  2155. // warehouseOutInfo.Count = stock_number
  2156. //
  2157. // _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, goods.PatientId, record_time, goods.ProjectId)
  2158. //
  2159. // if errcodes == gorm.ErrRecordNotFound {
  2160. // errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  2161. //
  2162. // if errOne != nil {
  2163. // return errOne
  2164. // }
  2165. // //插入库存流水表
  2166. // flow := models.VmStockFlow{
  2167. // WarehouseOutId: warehouseOut.ID,
  2168. // WarehousingId: warehouse.ID,
  2169. // GoodId: goods.GoodId,
  2170. // Number: warehouse.Number,
  2171. // ProductDate: warehouse.ProductDate,
  2172. // ExpireDate: warehouse.ExpiryDate,
  2173. // Count: stock_number,
  2174. // Price: warehouse.PackingPrice,
  2175. // Status: 1,
  2176. // Ctime: time.Now().Unix(),
  2177. // UserOrgId: orgID,
  2178. // Manufacturer: warehouse.Manufacturer,
  2179. // Dealer: warehouse.Dealer,
  2180. // LicenseNumber: warehouse.LicenseNumber,
  2181. // IsEdit: 2,
  2182. // Creator: warehouseOut.Creater,
  2183. // SystemTime: record_time,
  2184. // ConsumableType: 3,
  2185. // WarehouseOutDetailId: warehouseOutInfo.ID,
  2186. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2187. // IsSys: 1,
  2188. // PatientId: patient_id,
  2189. // ProjectId: goods.ProjectId,
  2190. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2191. // StorehouseId: goods.StorehouseId,
  2192. // OverCount: sum_count,
  2193. // }
  2194. // //查询流水是否存在
  2195. // exsit, errflows := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  2196. //
  2197. // if errflows == gorm.ErrRecordNotFound {
  2198. //
  2199. // CreateStockFlowOne(flow)
  2200. // } else if errflows == nil {
  2201. // flow := models.VmStockFlow{
  2202. // ID: exsit.ID,
  2203. // WarehouseOutId: warehouseOut.ID,
  2204. // WarehousingId: warehouse.ID,
  2205. // GoodId: goods.GoodId,
  2206. // Number: warehouse.Number,
  2207. // ProductDate: warehouse.ProductDate,
  2208. // ExpireDate: warehouse.ExpiryDate,
  2209. // Count: exsit.Count + goods.Count,
  2210. // Price: warehouse.PackingPrice,
  2211. // Status: 1,
  2212. // Ctime: time.Now().Unix(),
  2213. // UserOrgId: orgID,
  2214. // Manufacturer: warehouse.Manufacturer,
  2215. // Dealer: warehouse.Dealer,
  2216. // LicenseNumber: warehouse.LicenseNumber,
  2217. // IsEdit: 2,
  2218. // Creator: warehouseOut.Creater,
  2219. // SystemTime: record_time,
  2220. // ConsumableType: 3,
  2221. // WarehouseOutDetailId: warehouseOutInfo.ID,
  2222. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2223. // IsSys: 1,
  2224. // PatientId: patient_id,
  2225. // ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  2226. // ProjectId: goods.ProjectId,
  2227. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2228. // StorehouseId: goods.StorehouseId,
  2229. // OverCount: sum_count,
  2230. // }
  2231. // errOne := UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  2232. // if errOne == nil {
  2233. //
  2234. // }
  2235. // }
  2236. //
  2237. // } else if errcodes == nil {
  2238. //
  2239. // goods.Count = deliver_number - stock_number
  2240. // //更新数量为 该批次剩余数量 + 还有未出的数量
  2241. // warehouseOutInfo.Count = stock_number
  2242. // UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, goods.PatientId, record_time, goods.ProjectId)
  2243. // //插入库存流水表
  2244. // flow := models.VmStockFlow{
  2245. // WarehouseOutId: warehouseOut.ID,
  2246. // WarehousingId: warehouse.ID,
  2247. // GoodId: goods.GoodId,
  2248. // Number: warehouse.Number,
  2249. // ProductDate: warehouse.ProductDate,
  2250. // ExpireDate: warehouse.ExpiryDate,
  2251. // Count: stock_number,
  2252. // Price: warehouse.PackingPrice,
  2253. // Status: 1,
  2254. // Ctime: time.Now().Unix(),
  2255. // UserOrgId: orgID,
  2256. // Manufacturer: warehouse.Manufacturer,
  2257. // Dealer: warehouse.Dealer,
  2258. // LicenseNumber: warehouse.LicenseNumber,
  2259. // IsEdit: 2,
  2260. // Creator: warehouseOut.Creater,
  2261. // SystemTime: record_time,
  2262. // ConsumableType: 3,
  2263. // WarehouseOutDetailId: warehouseOutInfo.ID,
  2264. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2265. // IsSys: 1,
  2266. // PatientId: patient_id,
  2267. // ProjectId: goods.ProjectId,
  2268. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2269. // StorehouseId: goods.StorehouseId,
  2270. // OverCount: sum_count,
  2271. // }
  2272. // //查询流水是否存在
  2273. // exsit, errflows := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  2274. // if errflows == gorm.ErrRecordNotFound {
  2275. //
  2276. // CreateStockFlowOne(flow)
  2277. // } else if errflows == nil {
  2278. //
  2279. // flow := models.VmStockFlow{
  2280. // ID: exsit.ID,
  2281. // WarehouseOutId: warehouseOut.ID,
  2282. // WarehousingId: warehouse.ID,
  2283. // GoodId: goods.GoodId,
  2284. // Number: warehouse.Number,
  2285. // ProductDate: warehouse.ProductDate,
  2286. // ExpireDate: warehouse.ExpiryDate,
  2287. // Count: exsit.Count + goods.Count,
  2288. // Price: warehouse.PackingPrice,
  2289. // Status: 1,
  2290. // Ctime: time.Now().Unix(),
  2291. // UserOrgId: orgID,
  2292. // Manufacturer: warehouse.Manufacturer,
  2293. // Dealer: warehouse.Dealer,
  2294. // LicenseNumber: warehouse.LicenseNumber,
  2295. // IsEdit: 2,
  2296. // Creator: warehouseOut.Creater,
  2297. // SystemTime: record_time,
  2298. // ConsumableType: 3,
  2299. // WarehouseOutDetailId: warehouseOutInfo.ID,
  2300. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2301. // IsSys: 1,
  2302. // PatientId: patient_id,
  2303. // ProjectId: goods.ProjectId,
  2304. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2305. // StorehouseId: goods.StorehouseId,
  2306. // OverCount: sum_count,
  2307. // }
  2308. // UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  2309. // }
  2310. // }
  2311. //
  2312. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2313. // goods.Count = deliver_number - stock_number
  2314. //
  2315. // ConsumablesDelivery(orgID, patient_id, record_time, goods, warehouseOut, count)
  2316. //
  2317. // }
  2318. // return nil
  2319. //}
  2320. //
  2321. ////耗材出库删除
  2322. //func ConsumablesDeliveryDelete(orgID int64, patient_id int64, record_time int64, good_yc *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut) (err error) {
  2323. //
  2324. // // 先根据相关信息查询当天该耗材的出库信息
  2325. // warehouseOutInfos, err := FindStockOutInfoByStock(orgID, good_yc.GoodTypeId, good_yc.GoodId, record_time, patient_id)
  2326. // if err != nil {
  2327. // return err
  2328. // }
  2329. //
  2330. // var delete_count int64 = 0
  2331. //
  2332. // for _, ware := range warehouseOutInfos {
  2333. // // 删除出库完成后,要增加对应批次的库存数量
  2334. // errThree := UpDateWarehouseInfoByStockDelete(ware.WarehouseInfotId, delete_count, patient_id, record_time, good_yc.GoodId)
  2335. // if errThree != nil {
  2336. // return errThree
  2337. // }
  2338. // //查询剩余库存
  2339. // goodList, _ := GetAllGoodSumCount(good_yc.GoodId, orgID)
  2340. // var sum_count int64
  2341. // for _, item := range goodList {
  2342. // sum_count += item.StockCount
  2343. // }
  2344. // // 判断当前出库的数据和删除出库数量
  2345. // if good_yc.Count <= ware.Count {
  2346. // delete_count = good_yc.Count
  2347. // } else {
  2348. // delete_count = ware.Count
  2349. // }
  2350. // houseConfig, _ := GetAllStoreHouseConfig(orgID)
  2351. // // 在出库记录表里记录退库详情
  2352. // warehouseOutInfo := &models.WarehouseOutInfo{
  2353. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2354. // WarehouseOutId: warehouseOut.ID,
  2355. // //WarehouseInfoId: warehouse.ID,
  2356. // Status: 1,
  2357. // Ctime: time.Now().Unix(),
  2358. // Remark: "",
  2359. // OrgId: orgID,
  2360. // Type: 1,
  2361. // Manufacturer: 0,
  2362. // Dealer: 0,
  2363. // IsSys: 2,
  2364. // SysRecordTime: record_time,
  2365. // GoodTypeId: good_yc.GoodTypeId,
  2366. // GoodId: good_yc.GoodId,
  2367. // PatientId: patient_id,
  2368. // StorehouseId: houseConfig.StorehouseOutInfo,
  2369. // OverCount: sum_count,
  2370. // }
  2371. // warehouseOutInfo.Count = ware.Count - delete_count
  2372. // stockInInfo, _ := FindLastStockInInfoRecord(good_yc.GoodId, orgID)
  2373. // warehouseOutInfo.Price = stockInInfo.Price
  2374. // errOne := UpdateAutoMaticReduceDetail(good_yc.GoodId, good_yc.GoodTypeId, record_time, patient_id, orgID, warehouseOutInfo)
  2375. // if errOne != nil {
  2376. // return errOne
  2377. // } else {
  2378. // details := &models.AutomaticReduceDetail{
  2379. // WarehouseOutId: warehouseOutInfo.ID,
  2380. // WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  2381. // PatientId: patient_id,
  2382. // Ctime: time.Now().Unix(),
  2383. // Mtime: time.Now().Unix(),
  2384. // Status: 1,
  2385. // RecordTime: record_time,
  2386. // OrgId: orgID,
  2387. // GoodId: good_yc.GoodId,
  2388. // GoodTypeId: good_yc.GoodTypeId,
  2389. // Count: warehouseOutInfo.Count,
  2390. // Type: 2,
  2391. // StorehouseId: houseConfig.StorehouseOutInfo,
  2392. // }
  2393. // //查询是否当天已经存在数据
  2394. // _, errcode := GetAutoMaticReduceDetail(orgID, patient_id, record_time, good_yc.GoodId, good_yc.GoodTypeId)
  2395. // if errcode == gorm.ErrRecordNotFound {
  2396. // errTwo := AddSigleAutoReduceRecordInfo(details)
  2397. // if errTwo != nil {
  2398. // return errTwo
  2399. // }
  2400. // } else if errcode == nil {
  2401. // DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, good_yc.GoodId, good_yc.GoodTypeId)
  2402. //
  2403. // AddSigleAutoReduceRecordInfo(details)
  2404. // }
  2405. // }
  2406. //
  2407. // //扣减数量相加
  2408. // ModifyAddGoodSumCount(houseConfig.StorehouseOutInfo, delete_count, good_yc.UserOrgId, good_yc.GoodId)
  2409. //
  2410. // //退库数量相加
  2411. // UpdateSumAddCancelCount(good_yc.UserOrgId, good_yc.GoodId, houseConfig.StorehouseOutInfo, delete_count)
  2412. //
  2413. // // 增加了对应的库存后,看看还有多少需要退库的
  2414. // good_yc.Count = good_yc.Count - delete_count
  2415. // if good_yc.Count == 0 {
  2416. // return nil
  2417. // }
  2418. // }
  2419. //
  2420. // if good_yc.Count == 0 {
  2421. // return nil
  2422. // } else {
  2423. // return errors.New("退库和出库数据不匹配")
  2424. // }
  2425. //}
  2426. //
  2427. //func GetAutoReduceRecordInfoByPatientId(orgid int64, patient_id int64, recordTime int64) (autoReduce []*models.AutomaticReduceDetail, err error) {
  2428. //
  2429. // err = XTReadDB().Model(&autoReduce).Where("org_id = ? and patient_id = ? and record_time = ? and status = 1", orgid, patient_id, recordTime).Find(&autoReduce).Error
  2430. // return autoReduce, err
  2431. //}
  2432. //
  2433. //func DeleteDialysisBefor(orgid int64, patient_id int64, record_date int64, goodid int64, goodtypeid int64) error {
  2434. // prepare := models.DialysisBeforePrepare{}
  2435. // 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
  2436. // return err
  2437. //}
  2438. //
  2439. //func DeleteDialysisBeforThree(orgid int64, patient_id int64, record_date int64) error {
  2440. // prepare := models.DialysisBeforePrepare{}
  2441. // 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
  2442. // return err
  2443. //}
  2444. //
  2445. //func GetWarehouseById(id int64) (models.WarehouseOut, error) {
  2446. // out := models.WarehouseOut{}
  2447. // err := XTReadDB().Where("id=? and status = 1", id).Find(&out).Error
  2448. // return out, err
  2449. //}
  2450. //
  2451. ////耗材出库
  2452. //func ConsumablesDeliveryOne(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64) (err error) {
  2453. //
  2454. // ////开事务
  2455. // //tx := XTWriteDB().Begin()
  2456. //
  2457. // var deliver_number int64 = 0
  2458. // var stock_number int64 = 0
  2459. // var maxNumber int64 = 0
  2460. //
  2461. // deliver_number = goods.Count
  2462. //
  2463. // // 根据先进先出原则,查询最先入库的批次,进行出库
  2464. //
  2465. // // 如果没有对应的库存,则报错
  2466. // warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  2467. // if err != nil {
  2468. //
  2469. // return err
  2470. // }
  2471. //
  2472. // stock_number = warehouse.StockCount
  2473. //
  2474. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  2475. // if stock_number >= deliver_number {
  2476. //
  2477. // warehouseOutInfo := &models.WarehouseOutInfo{
  2478. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2479. // WarehouseOutId: warehouseOut.ID,
  2480. // WarehouseInfotId: warehouse.ID,
  2481. // Status: 1,
  2482. // Ctime: time.Now().Unix(),
  2483. // Remark: goods.Remark,
  2484. // OrgId: orgID,
  2485. // Type: 1,
  2486. // Manufacturer: goods.Manufacturer,
  2487. // Dealer: goods.Dealer,
  2488. // IsSys: 0,
  2489. // SysRecordTime: record_time,
  2490. // GoodTypeId: goods.GoodTypeId,
  2491. // GoodId: goods.GoodId,
  2492. // ExpiryDate: goods.ExpiryDate,
  2493. // ProductDate: goods.ProductDate,
  2494. // Number: warehouse.Number,
  2495. // Price: goods.Price,
  2496. // LicenseNumber: goods.LicenseNumber,
  2497. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2498. // StorehouseId: goods.StorehouseId,
  2499. // AdminUserId: goods.AdminUserId,
  2500. // BuyPrice: warehouse.Price,
  2501. // StockCount: goods.StockCount,
  2502. // }
  2503. // warehouseOutInfo.Count = goods.Count
  2504. // //添加出库单详情
  2505. // errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  2506. // if errOne != nil {
  2507. // return errOne
  2508. // }
  2509. //
  2510. // stockFlow := models.VmStockFlow{
  2511. // WarehousingId: warehouse.ID,
  2512. // GoodId: goods.GoodId,
  2513. // Number: warehouse.Number,
  2514. // LicenseNumber: goods.LicenseNumber,
  2515. // Count: goods.Count,
  2516. // UserOrgId: goods.OrgId,
  2517. // PatientId: goods.PatientId,
  2518. // SystemTime: record_time,
  2519. // ConsumableType: 2,
  2520. // IsSys: goods.IsSys,
  2521. // WarehousingOrder: "",
  2522. // WarehouseOutId: warehouseOut.ID,
  2523. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2524. // IsEdit: 1,
  2525. // CancelStockId: 0,
  2526. // CancelOrderNumber: "",
  2527. // Manufacturer: goods.Manufacturer,
  2528. // Dealer: goods.Dealer,
  2529. // Creator: creator,
  2530. // UpdateCreator: 0,
  2531. // Status: 1,
  2532. // Ctime: time.Now().Unix(),
  2533. // Mtime: 0,
  2534. // Price: warehouse.PackingPrice,
  2535. // WarehousingDetailId: 0,
  2536. // WarehouseOutDetailId: warehouseOutInfo.ID,
  2537. // CancelOutDetailId: 0,
  2538. // ProductDate: goods.ProductDate,
  2539. // ExpireDate: goods.ExpiryDate,
  2540. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2541. // StorehouseId: warehouse.StorehouseId,
  2542. // AdminUserId: goods.AdminUserId,
  2543. // BuyPrice: warehouse.Price,
  2544. // StockCount: goods.StockCount,
  2545. // }
  2546. //
  2547. // //创建出库流水
  2548. // CreateStockFlowOne(stockFlow)
  2549. //
  2550. // maxNumber = goods.Count
  2551. //
  2552. // if warehouse.StockCount < maxNumber {
  2553. // return errors.New("库存数量不足")
  2554. // }
  2555. //
  2556. // warehouse.StockCount = warehouse.StockCount - maxNumber
  2557. // warehouse.Mtime = time.Now().Unix()
  2558. //
  2559. // //扣减库存
  2560. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  2561. // fmt.Println(errThree)
  2562. // //if errThree != nil {
  2563. // // utils.ErrorLog("事务失败,原因为: %v", errThree.Error())
  2564. // // tx.Rollback()
  2565. // //} else {
  2566. // // tx.Commit()
  2567. // //}
  2568. //
  2569. // //查询该机构默认仓库
  2570. // storeConfig, _ := GetAllStoreHouseConfig(goods.OrgId)
  2571. // //查询剩余库存
  2572. // goodList, _ := GetGoodSumCountByStoreId(storeConfig.StorehouseOutInfo, goods.GoodId, goods.OrgId)
  2573. // var sum_count int64
  2574. // var sum_in_count int64
  2575. // for _, item := range goodList {
  2576. // sum_count += item.StockCount
  2577. // sum_in_count += item.WarehousingCount
  2578. // }
  2579. // UpdateGoodByGoodId(goods.GoodId, sum_count, sum_in_count, goods.OrgId)
  2580. // if errThree != nil {
  2581. // return errThree
  2582. // }
  2583. //
  2584. // } else {
  2585. //
  2586. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2587. // warehouseOutInfo := &models.WarehouseOutInfo{
  2588. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2589. // WarehouseOutId: warehouseOut.ID,
  2590. // WarehouseInfotId: warehouse.ID,
  2591. // Status: 1,
  2592. // Ctime: time.Now().Unix(),
  2593. // Remark: goods.Remark,
  2594. // OrgId: orgID,
  2595. // Type: 1,
  2596. // Manufacturer: goods.Manufacturer,
  2597. // Dealer: goods.Dealer,
  2598. // IsSys: 0,
  2599. // SysRecordTime: record_time,
  2600. // GoodTypeId: goods.GoodTypeId,
  2601. // GoodId: goods.GoodId,
  2602. // ExpiryDate: goods.ExpiryDate,
  2603. // ProductDate: goods.ProductDate,
  2604. // Number: warehouse.Number,
  2605. // Price: warehouse.PackingPrice,
  2606. // LicenseNumber: goods.LicenseNumber,
  2607. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2608. // StorehouseId: warehouse.StorehouseId,
  2609. // AdminUserId: goods.AdminUserId,
  2610. // BuyPrice: warehouse.Price,
  2611. // StockCount: goods.StockCount,
  2612. // IsCheck: 1,
  2613. // }
  2614. // warehouseOutInfo.Count = stock_number
  2615. //
  2616. // //查询是否已经生成出库单
  2617. // info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  2618. // if errcodes == gorm.ErrRecordNotFound {
  2619. // //创建出库单
  2620. // errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  2621. // if errOne != nil {
  2622. // return errOne
  2623. // }
  2624. // } else {
  2625. // warehouseOutInfo.Count = info.Count + stock_number
  2626. //
  2627. // UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  2628. // }
  2629. //
  2630. // stockFlow := models.VmStockFlow{
  2631. // WarehousingId: warehouse.ID,
  2632. // GoodId: goods.GoodId,
  2633. // Number: warehouse.Number,
  2634. // LicenseNumber: goods.LicenseNumber,
  2635. // Count: stock_number,
  2636. // UserOrgId: goods.OrgId,
  2637. // PatientId: 0,
  2638. // SystemTime: record_time,
  2639. // ConsumableType: 2,
  2640. // IsSys: 0,
  2641. // WarehousingOrder: "",
  2642. // WarehouseOutId: warehouseOut.ID,
  2643. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2644. // IsEdit: 1,
  2645. // CancelStockId: 0,
  2646. // CancelOrderNumber: "",
  2647. // Manufacturer: goods.Manufacturer,
  2648. // Dealer: goods.Dealer,
  2649. // Creator: creator,
  2650. // UpdateCreator: 0,
  2651. // Status: 1,
  2652. // Ctime: time.Now().Unix(),
  2653. // Mtime: 0,
  2654. // Price: warehouse.PackingPrice,
  2655. // WarehousingDetailId: 0,
  2656. // WarehouseOutDetailId: warehouseOutInfo.ID,
  2657. // CancelOutDetailId: 0,
  2658. // ProductDate: goods.ProductDate,
  2659. // ExpireDate: goods.ExpiryDate,
  2660. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2661. // StorehouseId: goods.StorehouseId,
  2662. // AdminUserId: goods.AdminUserId,
  2663. // BuyPrice: warehouse.Price,
  2664. // StockCount: goods.StockCount,
  2665. // }
  2666. //
  2667. // //创建出库明细
  2668. // CreateStockFlowOne(stockFlow)
  2669. // //查询该机构默认仓库
  2670. // storeConfig, _ := GetAllStoreHouseConfig(goods.OrgId)
  2671. // //查询剩余库存
  2672. // goodList, _ := GetGoodSumCountByStoreId(storeConfig.StorehouseOutInfo, goods.GoodId, goods.OrgId)
  2673. // var sum_count int64
  2674. // var sum_in_count int64
  2675. // for _, item := range goodList {
  2676. // sum_count += item.StockCount
  2677. // sum_in_count += item.WarehousingCount
  2678. // }
  2679. // UpdateGoodByGoodId(goods.GoodId, sum_count, sum_in_count, goods.OrgId)
  2680. // // 出库完成后,要将该批次库存清零
  2681. // warehouse.StockCount = 0
  2682. // warehouse.Mtime = time.Now().Unix()
  2683. //
  2684. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  2685. // fmt.Println(errThree)
  2686. // //if errThree != nil {
  2687. // // utils.ErrorLog("事务失败,原因为: %v", errThree.Error())
  2688. // // tx.Rollback()
  2689. // //} else {
  2690. // // tx.Commit()
  2691. // //}
  2692. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2693. // goods.Count = deliver_number - stock_number
  2694. // ConsumablesDeliveryOne(orgID, record_time, goods, warehouseOut, count, creator)
  2695. //
  2696. // }
  2697. // return nil
  2698. //}
  2699. //
  2700. ////退库逻辑
  2701. //func ConsumablesDeliveryTwo(orgID int64, goods *models.WarehouseOutInfo, count int64) (err error) {
  2702. //
  2703. // ////开事务
  2704. // //tx := XTWriteDB().Begin()
  2705. // var deliver_number int64 = 0
  2706. // var stock_number int64 = 0
  2707. // var maxNumber int64 = 0
  2708. //
  2709. // deliver_number = goods.Count
  2710. //
  2711. // // 根据先进先出原则,查询最先入库的批次,进行出库
  2712. // warehouse, err := FindFirstWarehousingInfoByGoodId(goods.GoodId, goods.ID, goods.StorehouseId)
  2713. //
  2714. // if err != nil {
  2715. // return err
  2716. // }
  2717. //
  2718. // stock_number = warehouse.StockCount
  2719. //
  2720. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  2721. // if stock_number >= deliver_number {
  2722. // warehouseOutInfo := &models.WarehouseOutInfo{
  2723. // WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  2724. // WarehouseOutId: goods.ID,
  2725. // WarehouseInfotId: warehouse.ID,
  2726. // Status: 1,
  2727. // Ctime: time.Now().Unix(),
  2728. // Remark: goods.Remark,
  2729. // OrgId: orgID,
  2730. // Type: 1,
  2731. // Manufacturer: goods.Manufacturer,
  2732. // Dealer: goods.Dealer,
  2733. // IsSys: 0,
  2734. // GoodTypeId: goods.GoodTypeId,
  2735. // GoodId: goods.GoodId,
  2736. // ExpiryDate: goods.ExpiryDate,
  2737. // ProductDate: goods.ProductDate,
  2738. // Number: goods.Number,
  2739. // Price: warehouse.PackingPrice,
  2740. // LicenseNumber: goods.LicenseNumber,
  2741. // ConsumableType: 2,
  2742. // }
  2743. // warehouseOutInfo.Count = goods.Count
  2744. //
  2745. // errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  2746. // if errOne != nil {
  2747. // return errOne
  2748. // }
  2749. //
  2750. // maxNumber = goods.Count
  2751. //
  2752. // if warehouse.StockCount < maxNumber { //这段逻辑根本不会执行,因为当库存数量大于或等于出库数量才会进入这个分支
  2753. // return errors.New("库存数量不足")
  2754. // }
  2755. //
  2756. // warehouse.StockCount = warehouse.StockCount - maxNumber
  2757. // warehouse.Mtime = time.Now().Unix()
  2758. //
  2759. // if warehouse.StockCount <= 0 {
  2760. //
  2761. // return errors.New("库存数量不足")
  2762. // }
  2763. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  2764. // fmt.Println(errThree)
  2765. // //if errThree != nil {
  2766. // // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  2767. // // tx.Rollback()
  2768. // //} else {
  2769. // // tx.Commit()
  2770. // //}
  2771. //
  2772. // return
  2773. // } else {
  2774. //
  2775. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2776. // warehouseOutInfo := &models.WarehouseOutInfo{
  2777. // WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  2778. // WarehouseOutId: goods.ID,
  2779. // WarehouseInfotId: warehouse.ID,
  2780. // Status: 1,
  2781. // Ctime: time.Now().Unix(),
  2782. // Remark: goods.Remark,
  2783. // OrgId: orgID,
  2784. // Type: 1,
  2785. // Manufacturer: goods.Manufacturer,
  2786. // Dealer: goods.Dealer,
  2787. // IsSys: 0,
  2788. // GoodTypeId: goods.GoodTypeId,
  2789. // GoodId: goods.GoodId,
  2790. // ExpiryDate: goods.ExpiryDate,
  2791. // ProductDate: goods.ProductDate,
  2792. // Number: goods.Number,
  2793. // Price: warehouse.PackingPrice,
  2794. // LicenseNumber: goods.LicenseNumber,
  2795. // ConsumableType: 2,
  2796. // IsCheck: 1,
  2797. // StorehouseId: goods.StorehouseId,
  2798. // }
  2799. // warehouseOutInfo.Count = stock_number
  2800. //
  2801. // errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  2802. // if errOne != nil {
  2803. // return errOne
  2804. // }
  2805. // // 出库完成后,要将该批次库存清零
  2806. // warehouse.StockCount = 0
  2807. // warehouse.Mtime = time.Now().Unix()
  2808. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  2809. // //if errThree != nil {
  2810. // // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  2811. // // tx.Rollback()
  2812. // //} else {
  2813. // // tx.Commit()
  2814. // //}
  2815. //
  2816. // fmt.Println(errThree)
  2817. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2818. // goods.Count = deliver_number - stock_number
  2819. //
  2820. // ConsumablesDeliveryTwo(orgID, goods, count)
  2821. //
  2822. // }
  2823. // return nil
  2824. //}
  2825. //
  2826. //func DeleteWarehouseInfo(id int64) error {
  2827. // err := XTWriteDB().Model(models.WarehousingInfo{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": 0}).Error
  2828. // return err
  2829. //}
  2830. //
  2831. //// 药品出库 递归方式
  2832. //func NewDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DoctorAdvice) (err error) {
  2833. //
  2834. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  2835. // var deliver_number int64 = 0
  2836. // var stock_number int64 = 0
  2837. //
  2838. // prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  2839. // count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  2840. //
  2841. // var drug_price float64
  2842. //
  2843. // //按最小单位数据计算
  2844. // deliver_number = count
  2845. //
  2846. // if advice.PrescribingNumberUnit == drup.MaxUnit {
  2847. // drug_price = drup.RetailPrice
  2848. // }
  2849. //
  2850. // if advice.PrescribingNumberUnit == drup.MinUnit {
  2851. // drug_price = drup.MinPrice
  2852. // }
  2853. //
  2854. // if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  2855. // drug_price = drup.RetailPrice
  2856. // }
  2857. //
  2858. // // 根据先进先出原则,查询最先入库的批次,进行出库
  2859. // // 如果没有对应的库存,则报错
  2860. // //查询默认仓库
  2861. // storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  2862. // lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2863. // if lastWarehouse.StockMinNumber >= drup.MinNumber {
  2864. // var stockMax int64
  2865. // var stockMin int64
  2866. // stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  2867. // stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  2868. // ChangeMaxNumber(lastWarehouse.ID, stockMax)
  2869. // UpdateMinNumber(lastWarehouse.ID, stockMin)
  2870. // }
  2871. //
  2872. // warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2873. //
  2874. // if err != nil {
  2875. //
  2876. // return err
  2877. // }
  2878. //
  2879. // // 将该批次的剩余库存数量转换为拆零数量
  2880. //
  2881. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  2882. //
  2883. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  2884. // if stock_number >= deliver_number {
  2885. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2886. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2887. // WarehouseOutId: warehouseout.ID,
  2888. // Status: 1,
  2889. // Ctime: time.Now().Unix(),
  2890. // Remark: warehouse.Remark,
  2891. // OrgId: orgID,
  2892. // Type: 1,
  2893. // Manufacturer: warehouse.Manufacturer,
  2894. // Dealer: warehouse.Dealer,
  2895. // IsSys: 1,
  2896. // SysRecordTime: advice.RecordDate,
  2897. // DrugId: advice.DrugId,
  2898. // Number: warehouse.Number,
  2899. // BatchNumber: warehouse.BatchNumber,
  2900. // Price: warehouse.RetailPrice,
  2901. // CountUnit: drup.MinUnit,
  2902. // RetailPrice: drug_price,
  2903. // ProductDate: warehouse.ProductDate,
  2904. // ExpiryDate: warehouse.ExpiryDate,
  2905. // PatientId: advice.PatientId,
  2906. // Count: deliver_number, //出最小单位
  2907. // WarehouseInfoId: warehouse.ID,
  2908. // AdviceId: advice.ID,
  2909. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2910. // StorehouseId: storeConfig.DrugStorehouseOut,
  2911. // IsCheck: 1,
  2912. // }
  2913. //
  2914. // drugflow := models.DrugFlow{
  2915. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2916. // WarehouseOutId: warehouseout.ID,
  2917. // DrugId: advice.DrugId,
  2918. // Number: warehouse.Number,
  2919. // ProductDate: warehouse.ProductDate,
  2920. // ExpireDate: warehouse.ExpiryDate,
  2921. // Count: deliver_number, //按最小单位计算,
  2922. // Price: warehouse.RetailPrice,
  2923. // Status: 1,
  2924. // Ctime: time.Now().Unix(),
  2925. // UserOrgId: orgID,
  2926. // Manufacturer: warehouse.Manufacturer,
  2927. // Dealer: warehouse.Dealer,
  2928. // BatchNumber: warehouse.BatchNumber,
  2929. // MaxUnit: drup.MinUnit,
  2930. // ConsumableType: 3,
  2931. // IsEdit: 1,
  2932. // Creator: advice.ExecutionStaff,
  2933. // IsSys: 1,
  2934. // PatientId: advice.PatientId,
  2935. // WarehousingDetailId: warehouse.ID,
  2936. // AdviceId: advice.ID,
  2937. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2938. // StorehouseId: storeConfig.DrugStorehouseOut,
  2939. // }
  2940. //
  2941. // CreateDrugFlowOne(drugflow)
  2942. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2943. // if errOne != nil {
  2944. // return errOne
  2945. // }
  2946. // //查询是否存在数据
  2947. //
  2948. // details := &models.DrugAutomaticReduceDetail{
  2949. // WarehouseOutId: warehouseout.ID,
  2950. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2951. // PatientId: advice.PatientId,
  2952. // Ctime: time.Now().Unix(),
  2953. // Mtime: time.Now().Unix(),
  2954. // Status: 1,
  2955. // RecordTime: advice.RecordDate,
  2956. // OrgId: orgID,
  2957. // DrugId: advice.DrugId,
  2958. // Count: deliver_number,
  2959. // CountUnit: drup.MinUnit,
  2960. // WarehouseInfoId: warehouse.ID,
  2961. // StorehouseId: storeConfig.DrugStorehouseOut,
  2962. // }
  2963. // errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2964. // if errTwo != nil {
  2965. // return errTwo
  2966. // }
  2967. //
  2968. // // 出库完成后,要减去对应批次的库存数量
  2969. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  2970. // var maxNumber int64 = 0
  2971. // var minNumber int64 = 0
  2972. // //var stock_max_number int64 = 0
  2973. // //stock_max_number = warehouse.StockMaxNumber
  2974. //
  2975. // maxNumber = deliver_number / drup.MinNumber
  2976. // minNumber = deliver_number % drup.MinNumber
  2977. //
  2978. // if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  2979. // minNumber = maxNumber
  2980. // }
  2981. //
  2982. // if drup.MaxUnit != drup.MinUnit {
  2983. // if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  2984. // return errors.New("库存数量不足")
  2985. // }
  2986. // }
  2987. //
  2988. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  2989. //
  2990. // if warehouse.StockMaxNumber < 0 {
  2991. // warehouse.StockMaxNumber = 0
  2992. // }
  2993. // if warehouse.StockMinNumber < 0 {
  2994. // warehouse.StockMinNumber = 0
  2995. // }
  2996. //
  2997. // warehouse.Mtime = time.Now().Unix()
  2998. //
  2999. // if warehouse.StockMinNumber < minNumber {
  3000. //
  3001. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  3002. // warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  3003. // } else {
  3004. //
  3005. // if minNumber > 0 {
  3006. // warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  3007. // }
  3008. //
  3009. // if minNumber == 0 && maxNumber != 1 {
  3010. // if warehouse.StockMinNumber > 0 {
  3011. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  3012. // }
  3013. //
  3014. // }
  3015. // }
  3016. //
  3017. // if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  3018. // if (warehouse.StockMinNumber - deliver_number) > 0 {
  3019. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  3020. //
  3021. // //if warehouse.StockMinNumber == 0 {
  3022. // // warehouse.StockMaxNumber = stock_max_number
  3023. // //}
  3024. // }
  3025. //
  3026. // }
  3027. //
  3028. // if drup.MaxUnit != drup.MinUnit {
  3029. // if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  3030. // return errors.New("库存数量不足")
  3031. // }
  3032. // }
  3033. // if warehouse.StockMinNumber <= 0 {
  3034. // warehouse.StockMinNumber = 0
  3035. // }
  3036. //
  3037. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  3038. //
  3039. // if errThree != nil {
  3040. // return errThree
  3041. // }
  3042. //
  3043. // return nil
  3044. // } else {
  3045. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  3046. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  3047. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3048. // WarehouseOutId: warehouseout.ID,
  3049. // Status: 1,
  3050. // Ctime: time.Now().Unix(),
  3051. // Remark: warehouse.Remark,
  3052. // OrgId: orgID,
  3053. // Type: 1,
  3054. // Manufacturer: warehouse.Manufacturer,
  3055. // Dealer: warehouse.Dealer,
  3056. // IsSys: 1,
  3057. // SysRecordTime: advice.RecordDate,
  3058. // DrugId: advice.DrugId,
  3059. // Number: warehouse.Number,
  3060. // BatchNumber: warehouse.BatchNumber,
  3061. // Price: warehouse.RetailPrice,
  3062. // CountUnit: drup.MinUnit,
  3063. // ProductDate: warehouse.ProductDate,
  3064. // ExpiryDate: warehouse.ExpiryDate,
  3065. // PatientId: advice.PatientId,
  3066. // Count: deliver_number,
  3067. // WarehouseInfoId: warehouse.ID,
  3068. // AdviceId: advice.ID,
  3069. // StorehouseId: storeConfig.DrugStorehouseOut,
  3070. // IsCheck: 1,
  3071. // }
  3072. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  3073. // if errOne != nil {
  3074. // return errOne
  3075. // }
  3076. //
  3077. // details := &models.DrugAutomaticReduceDetail{
  3078. // WarehouseOutId: warehouseout.ID,
  3079. // WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  3080. // PatientId: advice.PatientId,
  3081. // Ctime: time.Now().Unix(),
  3082. // Mtime: time.Now().Unix(),
  3083. // Status: 1,
  3084. // RecordTime: advice.RecordDate,
  3085. // OrgId: orgID,
  3086. // DrugId: advice.DrugId,
  3087. // Count: deliver_number,
  3088. // CountUnit: drup.MinUnit,
  3089. // WarehouseInfoId: warehouse.ID,
  3090. // StorehouseId: storeConfig.DrugStorehouseOut,
  3091. // }
  3092. // errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  3093. // if errTwo != nil {
  3094. // return errTwo
  3095. // }
  3096. //
  3097. // // 出库完成后,要将该批次库存清零
  3098. // warehouse.StockMaxNumber = 0
  3099. // warehouse.StockMinNumber = 0
  3100. // warehouse.Mtime = time.Now().Unix()
  3101. //
  3102. // info := models.XtDrugWarehouseInfo{
  3103. // ID: warehouse.ID,
  3104. // WarehousingId: warehouse.WarehousingId,
  3105. // DrugId: warehouse.DrugId,
  3106. // Number: warehouse.Number,
  3107. // ProductDate: warehouse.ProductDate,
  3108. // ExpiryDate: warehouse.ExpiryDate,
  3109. // WarehousingCount: warehouse.WarehousingCount,
  3110. // Price: warehouse.Price,
  3111. // TotalPrice: warehouse.TotalPrice,
  3112. // Dealer: warehouse.Dealer,
  3113. // Manufacturer: warehouse.Manufacturer,
  3114. // Remark: warehouse.Remark,
  3115. // Ctime: warehouse.Ctime,
  3116. // Mtime: warehouse.Mtime,
  3117. // Status: 1,
  3118. // OrgId: warehouse.OrgId,
  3119. // IsReturn: warehouse.IsReturn,
  3120. // WarehousingOrder: warehouse.WarehousingOrder,
  3121. // Type: warehouse.Type,
  3122. // RetailPrice: warehouse.RetailPrice,
  3123. // RetailTotalPrice: warehouse.RetailPrice,
  3124. // StockMaxNumber: 0,
  3125. // StockMinNumber: 0,
  3126. // BatchNumber: warehouse.BatchNumber,
  3127. // MaxUnit: warehouse.MaxUnit,
  3128. // WarehousingInfoId: warehouse.WarehousingInfoId,
  3129. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3130. // StorehouseId: storeConfig.DrugStorehouseOut,
  3131. // IsCheck: 1,
  3132. // }
  3133. // //扣减库存
  3134. // errThree := UpDateDrugWarehouseInfoByStock(&info)
  3135. //
  3136. // if errThree != nil {
  3137. // return errThree
  3138. // }
  3139. //
  3140. // drugflow := models.DrugFlow{
  3141. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3142. // WarehouseOutId: warehouseout.ID,
  3143. // DrugId: advice.DrugId,
  3144. // Number: warehouse.Number,
  3145. // ProductDate: warehouse.ProductDate,
  3146. // ExpireDate: warehouse.ExpiryDate,
  3147. // Count: deliver_number,
  3148. // Price: warehouse.RetailPrice,
  3149. // Status: 1,
  3150. // Ctime: time.Now().Unix(),
  3151. // UserOrgId: orgID,
  3152. // Manufacturer: warehouse.Manufacturer,
  3153. // Dealer: warehouse.Dealer,
  3154. // BatchNumber: warehouse.BatchNumber,
  3155. // MaxUnit: drup.MinUnit,
  3156. // ConsumableType: 3,
  3157. // IsEdit: 1,
  3158. // Creator: advice.ExecutionStaff,
  3159. // IsSys: 1,
  3160. // PatientId: advice.PatientId,
  3161. // WarehousingDetailId: warehouse.ID,
  3162. // AdviceId: advice.ID,
  3163. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3164. // StorehouseId: storeConfig.DrugStorehouseOut,
  3165. // }
  3166. //
  3167. // CreateDrugFlowOne(drugflow)
  3168. //
  3169. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3170. // prescribingNumber_two_temp := deliver_number - stock_number
  3171. //
  3172. // overPlusNumber := float64(prescribingNumber_two_temp)
  3173. //
  3174. // advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  3175. // advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  3176. //
  3177. // NewDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  3178. // }
  3179. //
  3180. // return
  3181. //}
  3182. //
  3183. //// 药品出库 递归方式
  3184. //func NewHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  3185. //
  3186. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  3187. // var deliver_number int64 = 0
  3188. // var stock_number int64 = 0
  3189. //
  3190. // prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  3191. // count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  3192. //
  3193. // deliver_number = count
  3194. //
  3195. // var min_price float64
  3196. //
  3197. // if advice.PrescribingNumberUnit == drup.MaxUnit {
  3198. // min_price = drup.RetailPrice
  3199. // }
  3200. // if advice.PrescribingNumberUnit == drup.MinUnit {
  3201. // min_price = drup.MinPrice
  3202. // }
  3203. //
  3204. // if advice.PrescribingNumberUnit != drup.MaxUnit && advice.PrescribingNumberUnit != drup.MinUnit {
  3205. // min_price = drup.RetailPrice
  3206. // }
  3207. //
  3208. // fmt.Println(min_price)
  3209. // // 根据先进先出原则,查询最先入库的批次,进行出库
  3210. // // 如果没有对应的库存,则报错
  3211. // //开启事物
  3212. // storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  3213. // lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  3214. // if lastWarehouse.StockMinNumber >= drup.MinNumber {
  3215. // var stockMax int64
  3216. // var stockMin int64
  3217. // stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  3218. // stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  3219. // ChangeMaxNumber(lastWarehouse.ID, stockMax)
  3220. // UpdateMinNumber(lastWarehouse.ID, stockMin)
  3221. // }
  3222. // warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  3223. // //查找药品信息
  3224. //
  3225. // if err != nil {
  3226. //
  3227. // return err
  3228. // }
  3229. //
  3230. // // 将该批次的剩余库存数量转换为拆零数量
  3231. //
  3232. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  3233. //
  3234. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3235. // if stock_number >= deliver_number {
  3236. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  3237. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3238. // WarehouseOutId: warehouseout.ID,
  3239. // Status: 1,
  3240. // Ctime: time.Now().Unix(),
  3241. // Remark: warehouse.Remark,
  3242. // OrgId: orgID,
  3243. // Type: 1,
  3244. // Manufacturer: warehouse.Manufacturer,
  3245. // Dealer: warehouse.Dealer,
  3246. // IsSys: 1,
  3247. // SysRecordTime: advice.RecordDate,
  3248. // DrugId: advice.DrugId,
  3249. // Number: warehouse.Number,
  3250. // BatchNumber: warehouse.BatchNumber,
  3251. // Price: warehouse.RetailPrice,
  3252. // CountUnit: drup.MinUnit,
  3253. // RetailPrice: warehouse.RetailPrice,
  3254. // ProductDate: warehouse.ProductDate,
  3255. // ExpiryDate: warehouse.ExpiryDate,
  3256. // PatientId: advice.PatientId,
  3257. // WarehouseInfoId: warehouse.ID,
  3258. // Count: deliver_number,
  3259. // AdviceId: advice.ID,
  3260. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3261. // StorehouseId: storeConfig.DrugStorehouseOut,
  3262. // IsCheck: 1,
  3263. // }
  3264. //
  3265. // drugflow := models.DrugFlow{
  3266. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3267. // WarehouseOutId: warehouseout.ID,
  3268. // DrugId: advice.DrugId,
  3269. // Number: warehouse.Number,
  3270. // ProductDate: warehouse.ProductDate,
  3271. // ExpireDate: warehouse.ExpiryDate,
  3272. // Count: deliver_number,
  3273. // Price: warehouse.RetailPrice,
  3274. // Status: 1,
  3275. // Ctime: time.Now().Unix(),
  3276. // UserOrgId: orgID,
  3277. // Manufacturer: warehouse.Manufacturer,
  3278. // Dealer: warehouse.Dealer,
  3279. // BatchNumber: warehouse.BatchNumber,
  3280. // MaxUnit: drup.MinUnit,
  3281. // ConsumableType: 3,
  3282. // IsEdit: 1,
  3283. // Creator: advice.ExecutionStaff,
  3284. // IsSys: 1,
  3285. // PatientId: advice.PatientId,
  3286. // SystemTime: advice.RecordDate,
  3287. // WarehousingDetailId: warehouse.ID,
  3288. // AdviceId: advice.ID,
  3289. // StorehouseId: storeConfig.DrugStorehouseOut,
  3290. // }
  3291. //
  3292. // CreateDrugFlowOne(drugflow)
  3293. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  3294. // if errOne != nil {
  3295. // return errOne
  3296. // }
  3297. //
  3298. // details := &models.DrugAutomaticReduceDetail{
  3299. // WarehouseOutId: warehouseout.ID,
  3300. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3301. // PatientId: advice.PatientId,
  3302. // Ctime: time.Now().Unix(),
  3303. // Mtime: time.Now().Unix(),
  3304. // Status: 1,
  3305. // RecordTime: advice.RecordDate,
  3306. // OrgId: orgID,
  3307. // DrugId: advice.DrugId,
  3308. // Count: deliver_number,
  3309. // CountUnit: drup.MinUnit,
  3310. // WarehouseInfoId: warehouse.ID,
  3311. // AdviceId: advice.ID,
  3312. // StorehouseId: storeConfig.DrugStorehouseOut,
  3313. // }
  3314. // errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  3315. // if errTwo != nil {
  3316. // return errTwo
  3317. // }
  3318. //
  3319. // // 出库完成后,要减去对应批次的库存数量
  3320. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  3321. //
  3322. // var maxNumber int64 = 0
  3323. // var minNumber int64 = 0
  3324. // //var stock_max_number int64 = 0
  3325. // //stock_max_number = warehouse.StockMaxNumber
  3326. //
  3327. // maxNumber = deliver_number / drup.MinNumber
  3328. // minNumber = deliver_number % drup.MinNumber
  3329. //
  3330. // if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  3331. // minNumber = maxNumber
  3332. // }
  3333. //
  3334. // if drup.MaxUnit != drup.MinUnit {
  3335. // if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  3336. // return errors.New("库存数量不足")
  3337. // }
  3338. // }
  3339. //
  3340. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  3341. //
  3342. // if warehouse.StockMaxNumber < 0 {
  3343. // warehouse.StockMaxNumber = 0
  3344. // }
  3345. // if warehouse.StockMinNumber < 0 {
  3346. // warehouse.StockMinNumber = 0
  3347. // }
  3348. //
  3349. // warehouse.Mtime = time.Now().Unix()
  3350. //
  3351. // if warehouse.StockMinNumber < minNumber {
  3352. //
  3353. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  3354. // warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  3355. // } else {
  3356. //
  3357. // if minNumber > 0 {
  3358. // warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  3359. // }
  3360. //
  3361. // if minNumber == 0 && maxNumber != 1 {
  3362. // if warehouse.StockMinNumber > 0 {
  3363. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  3364. // }
  3365. //
  3366. // }
  3367. // }
  3368. //
  3369. // if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  3370. // if (warehouse.StockMinNumber - deliver_number) > 0 {
  3371. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  3372. //
  3373. // //if warehouse.StockMinNumber == 0 {
  3374. // // warehouse.StockMaxNumber = stock_max_number
  3375. // //}
  3376. // }
  3377. //
  3378. // }
  3379. //
  3380. // if drup.MaxUnit != drup.MinUnit {
  3381. // if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  3382. // return errors.New("库存数量不足")
  3383. // }
  3384. // }
  3385. // if warehouse.StockMinNumber <= 0 {
  3386. // warehouse.StockMinNumber = 0
  3387. // }
  3388. //
  3389. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  3390. //
  3391. // if errThree != nil {
  3392. // return errThree
  3393. // }
  3394. //
  3395. // return nil
  3396. // } else {
  3397. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  3398. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  3399. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3400. // WarehouseOutId: warehouseout.ID,
  3401. // Status: 1,
  3402. // Ctime: time.Now().Unix(),
  3403. // Remark: warehouse.Remark,
  3404. // OrgId: orgID,
  3405. // Type: 1,
  3406. // Manufacturer: warehouse.Manufacturer,
  3407. // Dealer: warehouse.Dealer,
  3408. // IsSys: 1,
  3409. // SysRecordTime: advice.RecordDate,
  3410. // DrugId: advice.DrugId,
  3411. // Number: warehouse.Number,
  3412. // BatchNumber: warehouse.BatchNumber,
  3413. // Price: warehouse.RetailPrice,
  3414. // RetailPrice: warehouse.RetailPrice,
  3415. // ProductDate: warehouse.ProductDate,
  3416. // ExpiryDate: warehouse.ExpiryDate,
  3417. // PatientId: advice.PatientId,
  3418. // WarehouseInfoId: warehouse.ID,
  3419. // Count: stock_number,
  3420. // CountUnit: drup.MinUnit,
  3421. // AdviceId: advice.ID,
  3422. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3423. // StorehouseId: storeConfig.DrugStorehouseOut,
  3424. // IsCheck: 1,
  3425. // }
  3426. //
  3427. // drugflow := models.DrugFlow{
  3428. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3429. // WarehouseOutId: warehouseout.ID,
  3430. // DrugId: advice.DrugId,
  3431. // Number: warehouse.Number,
  3432. // ProductDate: warehouse.ProductDate,
  3433. // ExpireDate: warehouse.ExpiryDate,
  3434. // Count: stock_number,
  3435. // Price: warehouse.RetailPrice,
  3436. // Status: 1,
  3437. // Ctime: time.Now().Unix(),
  3438. // UserOrgId: orgID,
  3439. // Manufacturer: warehouse.Manufacturer,
  3440. // Dealer: warehouse.Dealer,
  3441. // BatchNumber: warehouse.BatchNumber,
  3442. // MaxUnit: drup.MinUnit,
  3443. // ConsumableType: 3,
  3444. // IsEdit: 1,
  3445. // Creator: advice.ExecutionStaff,
  3446. // IsSys: 1,
  3447. // PatientId: advice.PatientId,
  3448. // SystemTime: advice.RecordDate,
  3449. // WarehousingDetailId: warehouse.ID,
  3450. // AdviceId: advice.ID,
  3451. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3452. // StorehouseId: storeConfig.DrugStorehouseOut,
  3453. // }
  3454. //
  3455. // CreateDrugFlowOne(drugflow)
  3456. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  3457. // if errOne != nil {
  3458. // return errOne
  3459. // }
  3460. //
  3461. // details := &models.DrugAutomaticReduceDetail{
  3462. // WarehouseOutId: warehouseout.ID,
  3463. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3464. // PatientId: advice.PatientId,
  3465. // Ctime: time.Now().Unix(),
  3466. // Mtime: time.Now().Unix(),
  3467. // Status: 1,
  3468. // RecordTime: advice.RecordDate,
  3469. // OrgId: orgID,
  3470. // DrugId: advice.DrugId,
  3471. // Count: stock_number,
  3472. // CountUnit: drup.MinUnit,
  3473. // WarehouseInfoId: warehouse.ID,
  3474. // AdviceId: advice.ID,
  3475. // StorehouseId: storeConfig.DrugStorehouseOut,
  3476. // }
  3477. // errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  3478. // if errTwo != nil {
  3479. // return errTwo
  3480. // }
  3481. //
  3482. // info := models.XtDrugWarehouseInfo{
  3483. // ID: warehouse.ID,
  3484. // WarehousingId: warehouse.WarehousingId,
  3485. // DrugId: warehouse.DrugId,
  3486. // Number: warehouse.Number,
  3487. // ProductDate: warehouse.ProductDate,
  3488. // ExpiryDate: warehouse.ExpiryDate,
  3489. // WarehousingCount: warehouse.WarehousingCount,
  3490. // Price: warehouse.RetailPrice,
  3491. // TotalPrice: warehouse.TotalPrice,
  3492. // Dealer: warehouse.Dealer,
  3493. // Manufacturer: warehouse.Manufacturer,
  3494. // Remark: warehouse.Remark,
  3495. // Ctime: warehouse.Ctime,
  3496. // Mtime: warehouse.Mtime,
  3497. // Status: 1,
  3498. // OrgId: warehouse.OrgId,
  3499. // IsReturn: warehouse.IsReturn,
  3500. // WarehousingOrder: warehouse.WarehousingOrder,
  3501. // Type: warehouse.Type,
  3502. // RetailPrice: warehouse.RetailPrice,
  3503. // RetailTotalPrice: warehouse.RetailPrice,
  3504. // StockMaxNumber: 0,
  3505. // StockMinNumber: 0,
  3506. // BatchNumber: warehouse.BatchNumber,
  3507. // MaxUnit: warehouse.MaxUnit,
  3508. // WarehousingInfoId: warehouse.WarehousingInfoId,
  3509. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3510. // StorehouseId: storeConfig.DrugStorehouseOut,
  3511. // IsCheck: 1,
  3512. // }
  3513. // errThree := UpDateDrugWarehouseInfoByStock(&info)
  3514. // if errThree != nil {
  3515. // return errThree
  3516. // }
  3517. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3518. // prescribingNumber_two_temp := deliver_number - stock_number
  3519. // overPlusNumber := float64(prescribingNumber_two_temp)
  3520. // advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  3521. // advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  3522. //
  3523. // NewHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  3524. // }
  3525. //
  3526. // return
  3527. //}
  3528. //
  3529. //// 药品出库 递归方式
  3530. //func HisAuToReduce(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  3531. //
  3532. // //查询最后一次出库数据
  3533. // drugInfo, _ := GetDrugWarehouseOutInfoIsExit(advice.DrugId, advice.PatientId, advice.RecordDate)
  3534. //
  3535. // prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  3536. // count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  3537. // var count_number int64
  3538. // var device_number int64
  3539. // if advice.PrescribingNumberUnit == drup.MaxUnit {
  3540. // device_number = count * drup.MinNumber
  3541. // }
  3542. // if advice.PrescribingNumberUnit == drup.MinUnit {
  3543. // device_number = count
  3544. // }
  3545. //
  3546. // if drugInfo.CountUnit == drup.MaxUnit {
  3547. // count_number = drugInfo.Count * drup.MinNumber
  3548. // }
  3549. // if drugInfo.CountUnit == drup.MinUnit {
  3550. // count_number = drugInfo.Count
  3551. // }
  3552. //
  3553. // // 比较数据大小
  3554. // //如果当期医嘱数量小于最后一次出库则退库
  3555. // if device_number < count_number {
  3556. // var cha_number int64
  3557. // var maxNumber int64
  3558. // var minNumber int64
  3559. // cha_number = count_number - device_number
  3560. // maxNumber = cha_number / drup.MinNumber
  3561. // minNumber = cha_number % drup.MinNumber
  3562. // //更改库存
  3563. // ModifyDrugWarehouse(drugInfo.WarehouseInfoId, maxNumber, minNumber)
  3564. //
  3565. // //修改出库表
  3566. // info := models.DrugWarehouseOutInfo{
  3567. // Count: device_number,
  3568. // CountUnit: advice.PrescribingNumberUnit,
  3569. // }
  3570. //
  3571. // ModifyDrugWarehouseOutInfo(info, advice.DrugId, advice.PatientId, advice.RecordDate)
  3572. //
  3573. // //修改自动出库表
  3574. // detail := models.DrugAutomaticReduceDetail{
  3575. // Count: device_number,
  3576. // CountUnit: advice.PrescribingNumberUnit,
  3577. // }
  3578. // ModifyAutoMaticReduce(detail, advice.PatientId, advice.RecordDate, advice.DrugId)
  3579. // //修改流水
  3580. // flow := models.DrugFlow{
  3581. // Count: device_number,
  3582. // MaxUnit: drup.MinUnit,
  3583. // }
  3584. // ModifyDrugFlow(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId)
  3585. //
  3586. // }
  3587. //
  3588. // //如果当期医嘱数量大于最后一次出库则需要出库
  3589. // if device_number > count_number {
  3590. // var cha_number int64
  3591. // cha_number = device_number - count_number
  3592. //
  3593. // // 根据先进先出原则,查询最先入库的批次,进行出库
  3594. //
  3595. // storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  3596. // lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  3597. // if lastWarehouse.StockMinNumber >= drup.MinNumber {
  3598. // var stockMax int64
  3599. // var stockMin int64
  3600. // stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  3601. // stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  3602. // ChangeMaxNumber(lastWarehouse.ID, stockMax)
  3603. // UpdateMinNumber(lastWarehouse.ID, stockMin)
  3604. // }
  3605. // warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  3606. // //查找药品信息
  3607. // //baseInfo, _ := FindBaseDrugLibRecord(advice.UserOrgId, advice.DrugId)
  3608. // if err != nil {
  3609. // return err
  3610. // }
  3611. //
  3612. // // 将该批次的剩余库存数量转换为拆零数量
  3613. // var stock_number int64
  3614. // var maxNumber int64
  3615. // var minNumber int64
  3616. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  3617. //
  3618. // //如果库存数量大于出库数据,正常出库
  3619. // if stock_number >= cha_number {
  3620. // //扣减库存
  3621. // maxNumber = cha_number / drup.MinNumber
  3622. // minNumber = cha_number % drup.MinNumber
  3623. //
  3624. // if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  3625. // minNumber = maxNumber
  3626. // }
  3627. //
  3628. // if drup.MaxUnit != drup.MinUnit {
  3629. // if warehouse.StockMaxNumber < maxNumber {
  3630. // return errors.New("库存数量不足")
  3631. // }
  3632. // }
  3633. //
  3634. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  3635. // if warehouse.StockMaxNumber < 0 {
  3636. // warehouse.StockMaxNumber = 0
  3637. // }
  3638. //
  3639. // warehouse.Mtime = time.Now().Unix()
  3640. //
  3641. // if warehouse.StockMinNumber < minNumber {
  3642. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  3643. // warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  3644. // } else {
  3645. //
  3646. // warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  3647. //
  3648. // }
  3649. //
  3650. // if drup.MaxUnit != drup.MinUnit {
  3651. // if warehouse.StockMaxNumber < 0 {
  3652. // return errors.New("库存数量不足")
  3653. // }
  3654. // }
  3655. //
  3656. // //扣减库存232332332332
  3657. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  3658. //
  3659. // if errThree != nil {
  3660. // return errThree
  3661. // }
  3662. //
  3663. // //修改出库表
  3664. // info := models.DrugWarehouseOutInfo{
  3665. // Count: device_number,
  3666. // CountUnit: advice.PrescribingNumberUnit,
  3667. // }
  3668. // ModifyDrugWarehouseOutInfoOne(info, advice.DrugId, advice.PatientId, advice.RecordDate, warehouse.ID)
  3669. // //修改自动出库表
  3670. // detail := models.DrugAutomaticReduceDetail{
  3671. // Count: device_number,
  3672. // CountUnit: advice.PrescribingNumberUnit,
  3673. // }
  3674. // ModifyAutoMaticReduceOne(detail, advice.PatientId, advice.RecordDate, advice.DrugId, warehouse.ID)
  3675. // //修改流水
  3676. // flow := models.DrugFlow{
  3677. // Count: device_number,
  3678. // MaxUnit: advice.PrescribingNumberUnit,
  3679. // }
  3680. // ModifyDrugFlowOne(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId, warehouse.ID)
  3681. // }
  3682. // //如果库存数量小于出库数据,则需要出库到下一个批次
  3683. // if stock_number < cha_number {
  3684. //
  3685. // //修改出库表
  3686. // info := models.DrugWarehouseOutInfo{
  3687. // Count: cha_number,
  3688. // CountUnit: advice.PrescribingNumberUnit,
  3689. // }
  3690. // ModifyDrugWarehouseOutInfoOne(info, advice.DrugId, advice.PatientId, advice.RecordDate, warehouse.ID)
  3691. // //修改自动出库表
  3692. // detail := models.DrugAutomaticReduceDetail{
  3693. // Count: cha_number,
  3694. // CountUnit: advice.PrescribingNumberUnit,
  3695. // }
  3696. // ModifyAutoMaticReduceOne(detail, advice.PatientId, advice.RecordDate, advice.DrugId, warehouse.ID)
  3697. // //修改流水
  3698. // flow := models.DrugFlow{
  3699. // Count: cha_number,
  3700. // MaxUnit: advice.PrescribingNumberUnit,
  3701. // }
  3702. // ModifyDrugFlowOne(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId, warehouse.ID)
  3703. //
  3704. // druginfo := &models.XtDrugWarehouseInfo{
  3705. // ID: warehouse.ID,
  3706. // WarehousingId: warehouse.WarehousingId,
  3707. // DrugId: warehouse.DrugId,
  3708. // Number: warehouse.Number,
  3709. // ProductDate: warehouse.ProductDate,
  3710. // ExpiryDate: warehouse.ExpiryDate,
  3711. // WarehousingCount: warehouse.WarehousingCount,
  3712. // Price: warehouse.Price,
  3713. // TotalPrice: warehouse.TotalPrice,
  3714. // Dealer: warehouse.Dealer,
  3715. // Manufacturer: warehouse.Manufacturer,
  3716. // Remark: warehouse.Remark,
  3717. // Ctime: warehouse.Ctime,
  3718. // Mtime: warehouse.Mtime,
  3719. // Status: 1,
  3720. // OrgId: warehouse.OrgId,
  3721. // IsReturn: warehouse.IsReturn,
  3722. // WarehousingOrder: warehouse.WarehousingOrder,
  3723. // Type: warehouse.Type,
  3724. // RetailPrice: warehouse.RetailPrice,
  3725. // RetailTotalPrice: warehouse.RetailPrice,
  3726. // StockMaxNumber: 0,
  3727. // StockMinNumber: 0,
  3728. // BatchNumber: warehouse.BatchNumber,
  3729. // MaxUnit: warehouse.MaxUnit,
  3730. // WarehousingInfoId: warehouse.WarehousingInfoId,
  3731. // StorehouseId: storeConfig.DrugStorehouseOut,
  3732. // IsCheck: 1,
  3733. // }
  3734. // errThree := UpDateDrugWarehouseInfoByStock(druginfo)
  3735. // if errThree != nil {
  3736. // return errThree
  3737. // }
  3738. //
  3739. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3740. // prescribingNumber_two_temp := cha_number - stock_number
  3741. //
  3742. // overPlusNumber := float64(prescribingNumber_two_temp)
  3743. //
  3744. // advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  3745. //
  3746. // advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  3747. //
  3748. // NewHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  3749. //
  3750. // }
  3751. // }
  3752. //
  3753. // return
  3754. //}
  3755. //
  3756. ////采购管理耗材出库逻辑
  3757. //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) {
  3758. //
  3759. // var deliver_number int64 = 0
  3760. // var stock_number int64 = 0
  3761. // var maxNumber int64 = 0
  3762. //
  3763. // deliver_number = goods.Count
  3764. //
  3765. // // 根据先进先出原则,查询最先入库的批次,进行出库
  3766. // houseConfig, _ := GetAllStoreHouseConfig(orgID)
  3767. // // 如果没有对应的库存,则报错
  3768. // warehouse, err := FindFirstWarehousingInfoByStockNight(goods.GoodId, goods.GoodTypeId, warehouse_out_id, goods.IsSource, houseConfig.StorehouseOutInfo)
  3769. // if err != nil {
  3770. //
  3771. // return err
  3772. // }
  3773. //
  3774. // stock_number = warehouse.StockCount
  3775. //
  3776. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3777. // if stock_number >= deliver_number {
  3778. //
  3779. // warehouseOutInfo := &models.WarehouseOutInfo{
  3780. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3781. // WarehouseOutId: goods.WarehouseOutId,
  3782. // WarehouseInfotId: warehouse.ID,
  3783. // Status: 1,
  3784. // Ctime: time.Now().Unix(),
  3785. // Remark: goods.Remark,
  3786. // OrgId: orgID,
  3787. // Type: 1,
  3788. // Manufacturer: goods.Manufacturer,
  3789. // Dealer: goods.Dealer,
  3790. // IsSys: 0,
  3791. // SysRecordTime: record_time,
  3792. // GoodTypeId: goods.GoodTypeId,
  3793. // GoodId: goods.GoodId,
  3794. // ExpiryDate: goods.ExpiryDate,
  3795. // ProductDate: goods.ProductDate,
  3796. // Number: warehouse.Number,
  3797. // Price: goods.Price,
  3798. // LicenseNumber: goods.LicenseNumber,
  3799. // SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  3800. // StorehouseId: warehouse.StorehouseId,
  3801. // IsCheck: 1,
  3802. // }
  3803. // warehouseOutInfo.Count = goods.Count
  3804. // //添加出库单详情
  3805. // errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  3806. // if errOne != nil {
  3807. // return errOne
  3808. // }
  3809. //
  3810. // stockFlow := models.VmStockFlow{
  3811. // WarehousingId: warehouse.ID,
  3812. // GoodId: goods.GoodId,
  3813. // Number: warehouse.Number,
  3814. // LicenseNumber: goods.LicenseNumber,
  3815. // Count: goods.Count,
  3816. // UserOrgId: goods.OrgId,
  3817. // PatientId: goods.PatientId,
  3818. // SystemTime: record_time,
  3819. // ConsumableType: 2,
  3820. // IsSys: goods.IsSys,
  3821. // WarehousingOrder: "",
  3822. // WarehouseOutId: goods.WarehouseOutId,
  3823. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3824. // IsEdit: 1,
  3825. // CancelStockId: 0,
  3826. // CancelOrderNumber: "",
  3827. // Manufacturer: goods.Manufacturer,
  3828. // Dealer: goods.Dealer,
  3829. // Creator: creator,
  3830. // UpdateCreator: 0,
  3831. // Status: 1,
  3832. // Ctime: time.Now().Unix(),
  3833. // Mtime: 0,
  3834. // Price: warehouse.PackingPrice,
  3835. // WarehousingDetailId: 0,
  3836. // WarehouseOutDetailId: warehouseOutInfo.ID,
  3837. // CancelOutDetailId: 0,
  3838. // ProductDate: goods.ProductDate,
  3839. // ExpireDate: goods.ExpiryDate,
  3840. // SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  3841. // SupplyWarehouseId: warehouseOut.SupplyWarehouseId,
  3842. // StorehouseId: warehouse.StorehouseId,
  3843. // }
  3844. //
  3845. // //创建出库流水
  3846. // CreateStockFlowOne(stockFlow)
  3847. //
  3848. // maxNumber = goods.Count
  3849. //
  3850. // if warehouse.StockCount < maxNumber {
  3851. // return errors.New("库存数量不足")
  3852. // }
  3853. //
  3854. // warehouse.StockCount = warehouse.StockCount - maxNumber
  3855. // warehouse.Mtime = time.Now().Unix()
  3856. //
  3857. // //扣减库存
  3858. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  3859. // //出库数量增加
  3860. // AddGoodSumOutCount(houseConfig.StorehouseOutInfo, warehouse.GoodId, warehouse.OrgId, goods.Count)
  3861. // ModifyGoodReduceInformationSix(warehouse.GoodId, maxNumber, warehouse.OrgId, warehouse.PackingPrice)
  3862. //
  3863. // if errThree != nil {
  3864. // return errThree
  3865. // }
  3866. //
  3867. // } else {
  3868. //
  3869. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  3870. // warehouseOutInfo := &models.WarehouseOutInfo{
  3871. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3872. // WarehouseOutId: goods.WarehouseOutId,
  3873. // WarehouseInfotId: warehouse.ID,
  3874. // Status: 1,
  3875. // Ctime: time.Now().Unix(),
  3876. // Remark: goods.Remark,
  3877. // OrgId: orgID,
  3878. // Type: 1,
  3879. // Manufacturer: goods.Manufacturer,
  3880. // Dealer: goods.Dealer,
  3881. // IsSys: 0,
  3882. // SysRecordTime: record_time,
  3883. // GoodTypeId: goods.GoodTypeId,
  3884. // GoodId: goods.GoodId,
  3885. // ExpiryDate: goods.ExpiryDate,
  3886. // ProductDate: goods.ProductDate,
  3887. // Number: warehouse.Number,
  3888. // Price: warehouse.PackingPrice,
  3889. // LicenseNumber: goods.LicenseNumber,
  3890. // SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  3891. // StorehouseId: warehouse.StorehouseId,
  3892. // IsCheck: 1,
  3893. // }
  3894. // warehouseOutInfo.Count = stock_number
  3895. //
  3896. // //查询是否已经生成出库单
  3897. // info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  3898. // if errcodes == gorm.ErrRecordNotFound {
  3899. // //创建出库单
  3900. // errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  3901. // if errOne != nil {
  3902. // return errOne
  3903. // }
  3904. // } else {
  3905. // warehouseOutInfo.Count = info.Count + stock_number
  3906. //
  3907. // UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  3908. // }
  3909. //
  3910. // stockFlow := models.VmStockFlow{
  3911. // WarehousingId: warehouse.ID,
  3912. // GoodId: goods.GoodId,
  3913. // Number: warehouse.Number,
  3914. // LicenseNumber: goods.LicenseNumber,
  3915. // Count: stock_number,
  3916. // UserOrgId: goods.OrgId,
  3917. // PatientId: 0,
  3918. // SystemTime: record_time,
  3919. // ConsumableType: 2,
  3920. // IsSys: 0,
  3921. // WarehousingOrder: "",
  3922. // WarehouseOutId: goods.WarehouseOutId,
  3923. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3924. // IsEdit: 1,
  3925. // CancelStockId: 0,
  3926. // CancelOrderNumber: "",
  3927. // Manufacturer: goods.Manufacturer,
  3928. // Dealer: goods.Dealer,
  3929. // Creator: creator,
  3930. // UpdateCreator: 0,
  3931. // Status: 1,
  3932. // Ctime: time.Now().Unix(),
  3933. // Mtime: 0,
  3934. // Price: warehouse.PackingPrice,
  3935. // WarehousingDetailId: 0,
  3936. // WarehouseOutDetailId: warehouseOutInfo.ID,
  3937. // CancelOutDetailId: 0,
  3938. // ProductDate: goods.ProductDate,
  3939. // ExpireDate: goods.ExpiryDate,
  3940. // SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  3941. // SupplyWarehouseId: warehouseOut.SupplyWarehouseId,
  3942. // StorehouseId: warehouse.StorehouseId,
  3943. // }
  3944. //
  3945. // //创建出库明细
  3946. // CreateStockFlowOne(stockFlow)
  3947. // // 出库完成后,要将该批次库存清零
  3948. // ModifyGoodReduceInformationSix(warehouse.GoodId, warehouse.StockCount, warehouse.OrgId, warehouse.PackingPrice)
  3949. // warehouse.StockCount = 0
  3950. // warehouse.Mtime = time.Now().Unix()
  3951. //
  3952. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  3953. //
  3954. // if errThree != nil {
  3955. // return errThree
  3956. // }
  3957. // //出库数量增加
  3958. // AddGoodSumOutCount(houseConfig.StorehouseOutInfo, warehouse.GoodId, warehouse.OrgId, warehouse.StockCount)
  3959. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3960. // goods.Count = deliver_number - stock_number
  3961. //
  3962. // ConsumablesDeliveryNight(orgID, record_time, goods, warehouseOut, count, creator, warehouse_out_id, cancel_out_id)
  3963. //
  3964. // }
  3965. // return nil
  3966. //}
  3967. //
  3968. //func FindFirstWarehousingInfoByStockNight(good_id int64, good_type_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  3969. //
  3970. // if is_source == 1 {
  3971. // 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
  3972. //
  3973. // }
  3974. // if is_source == 2 || is_source == 0 {
  3975. // 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
  3976. // }
  3977. // return info, err
  3978. //}
  3979. //
  3980. //// 药品出库 递归方式
  3981. //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) {
  3982. //
  3983. // houseConfig, _ := GetAllStoreHouseConfig(orgID)
  3984. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  3985. // var deliver_number int64 = 0
  3986. // var stock_number int64 = 0
  3987. // var retail_price float64
  3988. // if advice.CountUnit == drup.MaxUnit {
  3989. // deliver_number = prescribingNumber * drup.MinNumber
  3990. // formatInt := strconv.FormatInt(prescribingNumber, 10)
  3991. // floatcount, _ := strconv.ParseFloat(formatInt, 64)
  3992. // retail_price = floatcount * advice.Price
  3993. // } else {
  3994. // deliver_number = prescribingNumber
  3995. // formatInt := strconv.FormatInt(prescribingNumber, 10)
  3996. // floatcount, _ := strconv.ParseFloat(formatInt, 64)
  3997. // retail_price = floatcount * advice.Price
  3998. // }
  3999. //
  4000. // // 根据先进先出原则,查询最先入库的批次,进行出库
  4001. // // 如果没有对应的库存,则报错
  4002. // //开启事物
  4003. // lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, houseConfig.DrugStorehouseOut)
  4004. // if lastWarehouse.StockMinNumber >= drup.MinNumber {
  4005. // var stockMax int64
  4006. // var stockMin int64
  4007. // stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  4008. // stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  4009. // ChangeMaxNumber(lastWarehouse.ID, stockMax)
  4010. // UpdateMinNumber(lastWarehouse.ID, stockMin)
  4011. // }
  4012. //
  4013. // warehouse, err := FindLastDrugWarehousingInfoByIDSix(advice.DrugId, warehouse_out_id, advice.IsSource, houseConfig.DrugStorehouseOut)
  4014. //
  4015. // if err != nil {
  4016. //
  4017. // return err
  4018. // }
  4019. //
  4020. // // 将该批次的剩余库存数量转换为拆零数量
  4021. //
  4022. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  4023. //
  4024. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  4025. // if stock_number >= deliver_number {
  4026. //
  4027. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4028. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4029. // WarehouseOutId: advice.WarehouseOutId,
  4030. // Status: 1,
  4031. // Ctime: time.Now().Unix(),
  4032. // Remark: advice.Remark,
  4033. // OrgId: orgID,
  4034. // Type: 1,
  4035. // Manufacturer: advice.Manufacturer,
  4036. // Dealer: warehouse.Dealer,
  4037. // IsSys: 0,
  4038. // SysRecordTime: advice.SysRecordTime,
  4039. // DrugId: advice.DrugId,
  4040. // ExpiryDate: advice.ExpiryDate,
  4041. // ProductDate: advice.ProductDate,
  4042. // Number: advice.Number,
  4043. // BatchNumber: warehouse.BatchNumber,
  4044. // Count: deliver_number,
  4045. // RetailPrice: advice.RetailPrice,
  4046. // Price: advice.Price,
  4047. // WarehouseInfoId: warehouse.ID,
  4048. // CountUnit: drup.MinUnit,
  4049. // AdviceId: advice.ID,
  4050. // SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  4051. // SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  4052. // StorehouseId: warehouse.StorehouseId,
  4053. // IsCheck: 1,
  4054. // }
  4055. //
  4056. // warehouseOutInfo.Count = prescribingNumber
  4057. // warehouseOutInfo.CountUnit = advice.CountUnit
  4058. //
  4059. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  4060. // if errOne != nil {
  4061. // return errOne
  4062. // }
  4063. //
  4064. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4065. // drugflow := models.DrugFlow{
  4066. // WarehouseOutId: advice.WarehouseOutId,
  4067. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4068. // DrugId: advice.DrugId,
  4069. // Number: warehouse.Number,
  4070. // ProductDate: advice.ProductDate,
  4071. // ExpireDate: advice.ExpiryDate,
  4072. // Count: deliver_number,
  4073. // Price: warehouse.RetailPrice,
  4074. // Status: 1,
  4075. // Ctime: time.Now().Unix(),
  4076. // UserOrgId: advice.OrgId,
  4077. // Manufacturer: advice.Manufacturer,
  4078. // Dealer: advice.Dealer,
  4079. // BatchNumber: warehouse.BatchNumber,
  4080. // MaxUnit: drup.MinUnit,
  4081. // ConsumableType: 2,
  4082. // IsEdit: 1,
  4083. // Creator: 0,
  4084. // IsSys: 0,
  4085. // WarehouseOutDetailId: drugWareInfo.ID,
  4086. // AdviceId: advice.ID,
  4087. // SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  4088. // SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  4089. // SystemTime: advice.SysRecordTime,
  4090. // StorehouseId: warehouse.StorehouseId,
  4091. // WarehousingDetailId: warehouse.ID,
  4092. // WarehousingId: warehouse.ID,
  4093. // RetailPrice: retail_price,
  4094. // LastPrice: warehouse.Price,
  4095. // }
  4096. //
  4097. // CreateDrugFlowOne(drugflow)
  4098. // // 出库完成后,要减去对应批次的库存数量
  4099. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  4100. // var maxNumber int64 = 0
  4101. // var minNumber int64 = 0
  4102. // if advice.CountUnit == drup.MinUnit {
  4103. // maxNumber = prescribingNumber / drup.MinNumber
  4104. // minNumber = prescribingNumber % drup.MinNumber
  4105. // if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  4106. // minNumber = maxNumber
  4107. // }
  4108. // } else {
  4109. // maxNumber = prescribingNumber
  4110. // }
  4111. //
  4112. // if warehouse.StockMaxNumber < maxNumber {
  4113. //
  4114. // return errors.New("库存数量不足")
  4115. // }
  4116. //
  4117. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  4118. // if warehouse.StockMaxNumber < 0 {
  4119. // warehouse.StockMaxNumber = 0
  4120. // }
  4121. //
  4122. // warehouse.Mtime = time.Now().Unix()
  4123. //
  4124. // if warehouse.StockMinNumber < minNumber {
  4125. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  4126. // warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  4127. // } else {
  4128. // warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  4129. // }
  4130. //
  4131. // if warehouse.StockMaxNumber < 0 {
  4132. // return errors.New("库存数量不足")
  4133. // }
  4134. // if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
  4135. // warehouse.StockMinNumber = 0
  4136. // }
  4137. //
  4138. // if warehouse.StockMaxNumber > 0 {
  4139. // var sum_count int64
  4140. // sum_count = maxNumber * drup.MinNumber
  4141. // ModifyDrugReduceInformationSix(warehouse.DrugId, sum_count, warehouse.OrgId, warehouse.RetailPrice)
  4142. // //出库数量增加
  4143. // AddDrugSumOutCount(houseConfig.DrugStorehouseOut, sum_count, warehouse.OrgId, warehouse.DrugId)
  4144. // }
  4145. //
  4146. // if warehouse.StockMinNumber > 0 {
  4147. // ModifyDrugReduceInformationSix(warehouse.DrugId, minNumber, warehouse.OrgId, warehouse.RetailPrice)
  4148. // //出库数量增加
  4149. // AddDrugSumOutCount(houseConfig.DrugStorehouseOut, minNumber, warehouse.OrgId, warehouse.DrugId)
  4150. // }
  4151. //
  4152. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4153. // if errThree != nil {
  4154. // return errThree
  4155. // }
  4156. //
  4157. // return nil
  4158. // } else {
  4159. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  4160. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4161. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4162. // WarehouseOutId: advice.WarehouseOutId,
  4163. // Status: 1,
  4164. // Ctime: time.Now().Unix(),
  4165. // Remark: advice.Remark,
  4166. // OrgId: orgID,
  4167. // Type: 1,
  4168. // Manufacturer: advice.Manufacturer,
  4169. // Dealer: warehouse.Dealer,
  4170. // IsSys: 0,
  4171. // SysRecordTime: advice.SysRecordTime,
  4172. // DrugId: advice.DrugId,
  4173. // ExpiryDate: warehouse.ExpiryDate,
  4174. // ProductDate: warehouse.ProductDate,
  4175. // Number: warehouse.Number,
  4176. // BatchNumber: warehouse.BatchNumber,
  4177. // Count: stock_number,
  4178. // RetailPrice: advice.RetailPrice,
  4179. // Price: advice.Price,
  4180. // WarehouseInfoId: warehouse.ID,
  4181. // CountUnit: drup.MinUnit,
  4182. // AdviceId: advice.ID,
  4183. // SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  4184. // SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  4185. // StorehouseId: warehouse.StorehouseId,
  4186. // IsCheck: 1,
  4187. // }
  4188. // warehouseOutInfo.Count = stock_number
  4189. //
  4190. // //warehouseOutInfo.CountUnit = advice.CountUnit
  4191. //
  4192. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  4193. // if errOne != nil {
  4194. // return errOne
  4195. // }
  4196. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4197. // drugflow := models.DrugFlow{
  4198. // WarehouseOutId: advice.WarehouseOutId,
  4199. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4200. // DrugId: advice.DrugId,
  4201. // Number: warehouse.Number,
  4202. // ProductDate: advice.ProductDate,
  4203. // ExpireDate: advice.ExpiryDate,
  4204. // Count: stock_number,
  4205. // Price: warehouse.RetailPrice,
  4206. // Status: 1,
  4207. // Ctime: time.Now().Unix(),
  4208. // UserOrgId: advice.OrgId,
  4209. // Manufacturer: advice.Manufacturer,
  4210. // Dealer: advice.Dealer,
  4211. // BatchNumber: warehouse.BatchNumber,
  4212. // MaxUnit: drup.MinUnit,
  4213. // ConsumableType: 2,
  4214. // IsEdit: 1,
  4215. // Creator: 0,
  4216. // IsSys: 0,
  4217. // WarehouseOutDetailId: drugWareInfo.ID,
  4218. // AdviceId: advice.ID,
  4219. // SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  4220. // SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  4221. // SystemTime: advice.SysRecordTime,
  4222. // StorehouseId: warehouse.StorehouseId,
  4223. // WarehousingDetailId: warehouse.ID,
  4224. // WarehousingId: warehouse.ID,
  4225. // RetailPrice: retail_price,
  4226. // LastPrice: warehouse.Price,
  4227. // }
  4228. // CreateDrugFlowOne(drugflow)
  4229. // if warehouse.StockMaxNumber > 0 {
  4230. // var sum_count int64
  4231. // sum_count = warehouse.StockMaxNumber * drup.MinNumber
  4232. // ModifyDrugReduceInformationSix(warehouse.DrugId, sum_count, warehouse.OrgId, warehouse.RetailPrice)
  4233. // AddDrugSumOutCount(houseConfig.DrugStorehouseOut, sum_count, warehouse.OrgId, warehouse.DrugId)
  4234. // }
  4235. //
  4236. // if warehouse.StockMinNumber > 0 {
  4237. // ModifyDrugReduceInformationSix(warehouse.DrugId, warehouse.StockMinNumber, warehouse.OrgId, warehouse.RetailPrice)
  4238. // AddDrugSumOutCount(houseConfig.DrugStorehouseOut, warehouse.StockMinNumber, warehouse.OrgId, warehouse.DrugId)
  4239. // }
  4240. //
  4241. // // 出库完成后,要将该批次库存清零
  4242. // warehouse.StockMaxNumber = 0
  4243. // warehouse.StockMinNumber = 0
  4244. // warehouse.Mtime = time.Now().Unix()
  4245. //
  4246. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4247. // if errThree != nil {
  4248. // return errThree
  4249. // }
  4250. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  4251. // prescribingNumber_two_temp := deliver_number - stock_number
  4252. // advice.CountUnit = drup.MinUnit
  4253. //
  4254. // AutoDrugDeliverInfoNight(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, warehouse_out_id, cancel_out_id)
  4255. // }
  4256. //
  4257. // return
  4258. //}
  4259. //
  4260. ////调拨出库逻辑
  4261. //func ConsumablesDeliveryTwelve(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64, warehouseInfo *models.WarehousingInfo) (err error) {
  4262. //
  4263. // var deliver_number int64 = 0
  4264. // var stock_number int64 = 0
  4265. // var maxNumber int64 = 0
  4266. //
  4267. // deliver_number = goods.Count
  4268. //
  4269. // // 根据先进先出原则,查询最先入库的批次,进行出库
  4270. //
  4271. // // 如果没有对应的库存,则报错
  4272. // warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  4273. // if err != nil {
  4274. //
  4275. // return err
  4276. // }
  4277. //
  4278. // stock_number = warehouse.StockCount
  4279. //
  4280. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  4281. // if stock_number >= deliver_number {
  4282. //
  4283. // warehouseOutInfo := &models.WarehouseOutInfo{
  4284. // WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  4285. // WarehouseOutId: goods.WarehouseOutId,
  4286. // WarehouseInfotId: warehouse.ID,
  4287. // Status: 1,
  4288. // Ctime: time.Now().Unix(),
  4289. // Remark: goods.Remark,
  4290. // OrgId: orgID,
  4291. // Type: 1,
  4292. // Manufacturer: warehouse.Manufacturer,
  4293. // Dealer: warehouse.Dealer,
  4294. // IsSys: 0,
  4295. // SysRecordTime: record_time,
  4296. // GoodTypeId: warehouse.GoodTypeId,
  4297. // GoodId: warehouse.GoodId,
  4298. // ExpiryDate: warehouse.ExpiryDate,
  4299. // ProductDate: warehouse.ProductDate,
  4300. // Number: warehouse.Number,
  4301. // Price: warehouse.PackingPrice,
  4302. // LicenseNumber: warehouse.LicenseNumber,
  4303. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4304. // StorehouseId: goods.StorehouseId,
  4305. // SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  4306. // IsCheck: 1,
  4307. // }
  4308. //
  4309. // warehouseOutInfo.Count = goods.Count
  4310. // //添加出库单详情
  4311. // errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  4312. // if errOne != nil {
  4313. // return errOne
  4314. // }
  4315. //
  4316. // stockFlow := models.VmStockFlow{
  4317. // WarehousingId: warehouse.ID,
  4318. // GoodId: goods.GoodId,
  4319. // Number: warehouse.Number,
  4320. // LicenseNumber: goods.LicenseNumber,
  4321. // Count: goods.Count,
  4322. // UserOrgId: goods.OrgId,
  4323. // PatientId: goods.PatientId,
  4324. // SystemTime: record_time,
  4325. // ConsumableType: 12,
  4326. // IsSys: goods.IsSys,
  4327. // WarehousingOrder: "",
  4328. // WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  4329. // WarehouseOutId: goods.WarehouseOutId,
  4330. // IsEdit: 1,
  4331. // CancelStockId: 0,
  4332. // CancelOrderNumber: "",
  4333. // Manufacturer: warehouse.Manufacturer,
  4334. // Dealer: warehouse.Dealer,
  4335. // Creator: creator,
  4336. // UpdateCreator: 0,
  4337. // Status: 1,
  4338. // Ctime: time.Now().Unix(),
  4339. // Mtime: 0,
  4340. // Price: warehouse.PackingPrice,
  4341. // WarehousingDetailId: 0,
  4342. // WarehouseOutDetailId: warehouseOutInfo.ID,
  4343. // CancelOutDetailId: 0,
  4344. // ProductDate: warehouse.ProductDate,
  4345. // ExpireDate: warehouse.ExpiryDate,
  4346. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4347. // StorehouseId: warehouse.StorehouseId,
  4348. // SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  4349. // }
  4350. //
  4351. // //创建出库流水
  4352. // CreateStockFlowOne(stockFlow)
  4353. //
  4354. // maxNumber = goods.Count
  4355. //
  4356. // if warehouse.StockCount < maxNumber {
  4357. // return errors.New("库存数量不足")
  4358. // }
  4359. //
  4360. // warehouse.StockCount = warehouse.StockCount - maxNumber
  4361. // warehouse.Mtime = time.Now().Unix()
  4362. //
  4363. // //扣减库存
  4364. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  4365. //
  4366. // //入库
  4367. // warehouseInfo := &models.WarehousingInfo{
  4368. // WarehousingOrder: warehouseInfo.WarehousingOrder,
  4369. // WarehousingId: warehouseInfo.WarehousingId,
  4370. // GoodId: warehouse.GoodId,
  4371. // Number: warehouse.Number,
  4372. // GoodTypeId: warehouse.GoodTypeId,
  4373. // ProductDate: warehouse.ProductDate,
  4374. // ExpiryDate: warehouse.ExpiryDate,
  4375. // WarehousingCount: goods.Count,
  4376. // Price: warehouse.Price,
  4377. // TotalPrice: 0,
  4378. // Status: 1,
  4379. // Ctime: warehouse.Ctime,
  4380. // Remark: goods.Remark,
  4381. // OrgId: warehouse.OrgId,
  4382. // Type: 1,
  4383. // Manufacturer: warehouse.Manufacturer,
  4384. // StockCount: goods.Count,
  4385. // Dealer: warehouse.Dealer,
  4386. // LicenseNumber: "",
  4387. // PackingPrice: warehouse.PackingPrice,
  4388. // StorehouseId: warehouseInfo.StorehouseId,
  4389. // SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  4390. // IsCheck: 1,
  4391. // }
  4392. //
  4393. // AddWarehosingInfo(warehouseInfo)
  4394. //
  4395. // stockInfoFlow := models.VmStockFlow{
  4396. // WarehousingId: warehouseInfo.WarehousingId,
  4397. // GoodId: goods.GoodId,
  4398. // Number: warehouse.Number,
  4399. // LicenseNumber: goods.LicenseNumber,
  4400. // Count: goods.Count,
  4401. // UserOrgId: goods.OrgId,
  4402. // PatientId: goods.PatientId,
  4403. // SystemTime: record_time,
  4404. // ConsumableType: 13,
  4405. // IsSys: goods.IsSys,
  4406. // WarehousingOrder: warehouseInfo.WarehousingOrder,
  4407. // WarehouseOutId: 0,
  4408. // WarehouseOutOrderNumber: "",
  4409. // IsEdit: 1,
  4410. // CancelStockId: 0,
  4411. // CancelOrderNumber: "",
  4412. // Manufacturer: warehouse.Manufacturer,
  4413. // Dealer: warehouse.Dealer,
  4414. // Creator: creator,
  4415. // UpdateCreator: 0,
  4416. // Status: 1,
  4417. // Ctime: time.Now().Unix(),
  4418. // Mtime: 0,
  4419. // Price: warehouse.Price,
  4420. // WarehousingDetailId: 0,
  4421. // WarehouseOutDetailId: warehouseOutInfo.ID,
  4422. // CancelOutDetailId: 0,
  4423. // ProductDate: warehouse.ProductDate,
  4424. // ExpireDate: warehouse.ExpiryDate,
  4425. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4426. // StorehouseId: warehouseInfo.StorehouseId,
  4427. // SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  4428. // }
  4429. //
  4430. // //创建入库流水
  4431. // CreateStockFlowOne(stockInfoFlow)
  4432. //
  4433. // if errThree != nil {
  4434. // return errThree
  4435. // }
  4436. //
  4437. // } else {
  4438. //
  4439. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  4440. // warehouseOutInfo := &models.WarehouseOutInfo{
  4441. // WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  4442. // WarehouseOutId: goods.WarehouseOutId,
  4443. // WarehouseInfotId: warehouse.ID,
  4444. // Status: 1,
  4445. // Ctime: time.Now().Unix(),
  4446. // Remark: goods.Remark,
  4447. // OrgId: orgID,
  4448. // Type: 1,
  4449. // Manufacturer: warehouse.Manufacturer,
  4450. // Dealer: warehouse.Dealer,
  4451. // IsSys: 0,
  4452. // SysRecordTime: record_time,
  4453. // GoodTypeId: warehouse.GoodTypeId,
  4454. // GoodId: warehouse.GoodId,
  4455. // ExpiryDate: warehouse.ExpiryDate,
  4456. // ProductDate: warehouse.ProductDate,
  4457. // Number: warehouse.Number,
  4458. // Price: warehouse.PackingPrice,
  4459. // LicenseNumber: warehouse.LicenseNumber,
  4460. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4461. // StorehouseId: warehouse.StorehouseId,
  4462. // SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  4463. // IsCheck: 1,
  4464. // }
  4465. // warehouseOutInfo.Count = warehouse.StockCount
  4466. //
  4467. // //查询是否已经生成出库单
  4468. // info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  4469. // if errcodes == gorm.ErrRecordNotFound {
  4470. // //创建出库单
  4471. // errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  4472. // if errOne != nil {
  4473. // return errOne
  4474. // }
  4475. // } else {
  4476. // warehouseOutInfo.Count = info.Count + stock_number
  4477. //
  4478. // UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  4479. // }
  4480. //
  4481. // stockFlow := models.VmStockFlow{
  4482. // WarehousingId: warehouse.ID,
  4483. // GoodId: goods.GoodId,
  4484. // Number: warehouse.Number,
  4485. // LicenseNumber: goods.LicenseNumber,
  4486. // Count: warehouse.StockCount,
  4487. // UserOrgId: goods.OrgId,
  4488. // PatientId: 0,
  4489. // SystemTime: record_time,
  4490. // ConsumableType: 12,
  4491. // IsSys: 0,
  4492. // WarehousingOrder: "",
  4493. // WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  4494. // WarehouseOutId: goods.WarehouseOutId,
  4495. // IsEdit: 1,
  4496. // CancelStockId: 0,
  4497. // CancelOrderNumber: "",
  4498. // Manufacturer: warehouse.Manufacturer,
  4499. // Dealer: warehouse.Dealer,
  4500. // Creator: creator,
  4501. // UpdateCreator: 0,
  4502. // Status: 1,
  4503. // Ctime: time.Now().Unix(),
  4504. // Mtime: 0,
  4505. // Price: warehouse.PackingPrice,
  4506. // WarehousingDetailId: 0,
  4507. // WarehouseOutDetailId: warehouseOutInfo.ID,
  4508. // CancelOutDetailId: 0,
  4509. // ProductDate: warehouse.ProductDate,
  4510. // ExpireDate: warehouse.ExpiryDate,
  4511. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4512. // StorehouseId: goods.StorehouseId,
  4513. // SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  4514. // }
  4515. //
  4516. // //创建出库明细
  4517. // CreateStockFlowOne(stockFlow)
  4518. //
  4519. // //入库
  4520. // warehouseInfo := &models.WarehousingInfo{
  4521. // WarehousingOrder: warehouseInfo.WarehousingOrder,
  4522. // WarehousingId: warehouseInfo.WarehousingId,
  4523. // GoodId: warehouse.GoodId,
  4524. // Number: warehouse.Number,
  4525. // GoodTypeId: warehouse.GoodTypeId,
  4526. // ProductDate: warehouse.ProductDate,
  4527. // ExpiryDate: warehouse.ExpiryDate,
  4528. // WarehousingCount: warehouse.StockCount,
  4529. // Price: warehouse.Price,
  4530. // TotalPrice: 0,
  4531. // Status: 1,
  4532. // Ctime: warehouse.Ctime,
  4533. // Remark: goods.Remark,
  4534. // OrgId: warehouse.OrgId,
  4535. // Type: 1,
  4536. // Manufacturer: warehouse.Manufacturer,
  4537. // StockCount: warehouse.StockCount,
  4538. // Dealer: warehouse.Dealer,
  4539. // LicenseNumber: "",
  4540. // PackingPrice: warehouse.PackingPrice,
  4541. // StorehouseId: warehouseInfo.StorehouseId,
  4542. // SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  4543. // IsCheck: 1,
  4544. // }
  4545. //
  4546. // AddWarehosingInfo(warehouseInfo)
  4547. //
  4548. // stockInfoFlow := models.VmStockFlow{
  4549. // WarehousingId: warehouseInfo.WarehousingId,
  4550. // GoodId: goods.GoodId,
  4551. // Number: warehouse.Number,
  4552. // LicenseNumber: goods.LicenseNumber,
  4553. // Count: warehouse.StockCount,
  4554. // UserOrgId: goods.OrgId,
  4555. // PatientId: goods.PatientId,
  4556. // SystemTime: record_time,
  4557. // ConsumableType: 13,
  4558. // IsSys: goods.IsSys,
  4559. // WarehousingOrder: warehouseInfo.WarehousingOrder,
  4560. // WarehouseOutId: warehouseOut.ID,
  4561. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  4562. // IsEdit: 1,
  4563. // CancelStockId: 0,
  4564. // CancelOrderNumber: "",
  4565. // Manufacturer: warehouse.Manufacturer,
  4566. // Dealer: warehouse.Dealer,
  4567. // Creator: creator,
  4568. // UpdateCreator: 0,
  4569. // Status: 1,
  4570. // Ctime: time.Now().Unix(),
  4571. // Mtime: 0,
  4572. // Price: warehouse.Price,
  4573. // WarehousingDetailId: 0,
  4574. // WarehouseOutDetailId: warehouseOutInfo.ID,
  4575. // CancelOutDetailId: 0,
  4576. // ProductDate: warehouse.ProductDate,
  4577. // ExpireDate: warehouse.ExpiryDate,
  4578. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4579. // StorehouseId: warehouseInfo.StorehouseId,
  4580. // SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  4581. // }
  4582. //
  4583. // //创建出库流水
  4584. // CreateStockFlowOne(stockInfoFlow)
  4585. // // 出库完成后,要将该批次库存清零
  4586. // warehouse.StockCount = 0
  4587. // warehouse.Mtime = time.Now().Unix()
  4588. //
  4589. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  4590. //
  4591. // if errThree != nil {
  4592. // return errThree
  4593. // }
  4594. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  4595. // goods.Count = deliver_number - stock_number
  4596. //
  4597. // ConsumablesDeliveryTwelve(orgID, record_time, goods, warehouseOut, count, creator, warehouseInfo)
  4598. //
  4599. // }
  4600. // return nil
  4601. //}
  4602. //
  4603. //func AddWarehosingInfo(info *models.WarehousingInfo) error {
  4604. //
  4605. // err = XTWriteDB().Create(&info).Error
  4606. // return err
  4607. //}
  4608. //
  4609. //// 药品出库 递归方式
  4610. //func AutoDrugDeliverInfoTwelve(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo, drugWarehouse *models.DrugWarehouseInfo) (err error) {
  4611. //
  4612. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  4613. // var deliver_number int64 = 0
  4614. // var stock_number int64 = 0
  4615. //
  4616. // if advice.CountUnit == drup.MaxUnit {
  4617. // deliver_number = prescribingNumber * drup.MinNumber
  4618. // } else {
  4619. // deliver_number = prescribingNumber
  4620. // }
  4621. //
  4622. // // 根据先进先出原则,查询最先入库的批次,进行出库
  4623. // // 如果没有对应的库存,则报错
  4624. // //开启事物
  4625. // storeConfig, _ := GetAllStoreHouseConfig(orgID)
  4626. // lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  4627. // if lastWarehouse.StockMinNumber >= drup.MinNumber {
  4628. // var stockMax int64
  4629. // var stockMin int64
  4630. // stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  4631. // stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  4632. // ChangeMaxNumber(lastWarehouse.ID, stockMax)
  4633. // UpdateMinNumber(lastWarehouse.ID, stockMin)
  4634. // }
  4635. //
  4636. // warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  4637. //
  4638. // if err != nil {
  4639. //
  4640. // return err
  4641. // }
  4642. //
  4643. // // 将该批次的剩余库存数量转换为拆零数量
  4644. //
  4645. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  4646. //
  4647. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  4648. // if stock_number >= deliver_number {
  4649. //
  4650. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4651. // WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  4652. // WarehouseOutId: advice.WarehouseOutId,
  4653. // Status: 1,
  4654. // Ctime: time.Now().Unix(),
  4655. // Remark: advice.Remark,
  4656. // OrgId: orgID,
  4657. // Type: 1,
  4658. // Manufacturer: warehouse.Manufacturer,
  4659. // Dealer: warehouse.Dealer,
  4660. // IsSys: 12,
  4661. // SysRecordTime: advice.Ctime,
  4662. // DrugId: advice.DrugId,
  4663. // ExpiryDate: warehouse.ExpiryDate,
  4664. // ProductDate: warehouse.ProductDate,
  4665. // Number: warehouse.Number,
  4666. // BatchNumber: warehouse.BatchNumber,
  4667. // Count: deliver_number,
  4668. // RetailPrice: warehouse.RetailPrice,
  4669. // Price: warehouse.RetailPrice,
  4670. // WarehouseInfoId: warehouse.ID,
  4671. // CountUnit: drup.MinUnit,
  4672. // AdviceId: advice.ID,
  4673. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4674. // StorehouseId: advice.StorehouseId,
  4675. // SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  4676. // IsCheck: 1,
  4677. // }
  4678. //
  4679. // warehouseOutInfo.Count = prescribingNumber
  4680. // warehouseOutInfo.CountUnit = advice.CountUnit
  4681. //
  4682. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  4683. // if errOne != nil {
  4684. // return errOne
  4685. // }
  4686. //
  4687. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4688. // drugflow := models.DrugFlow{
  4689. // WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  4690. // WarehouseOutId: advice.WarehouseOutId,
  4691. // DrugId: advice.DrugId,
  4692. // Number: warehouse.Number,
  4693. // ProductDate: warehouse.ProductDate,
  4694. // ExpireDate: warehouse.ExpiryDate,
  4695. // Count: deliver_number,
  4696. // Price: warehouse.RetailPrice,
  4697. // Status: 1,
  4698. // Ctime: time.Now().Unix(),
  4699. // UserOrgId: advice.OrgId,
  4700. // Manufacturer: warehouse.Manufacturer,
  4701. // Dealer: warehouse.Dealer,
  4702. // BatchNumber: warehouse.BatchNumber,
  4703. // MaxUnit: drup.MinUnit,
  4704. // ConsumableType: 12,
  4705. // IsEdit: 1,
  4706. // Creator: 0,
  4707. // IsSys: 12,
  4708. // WarehouseOutDetailId: drugWareInfo.ID,
  4709. // AdviceId: advice.ID,
  4710. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4711. // StorehouseId: advice.StorehouseId,
  4712. // SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  4713. // }
  4714. //
  4715. // CreateDrugFlowOne(drugflow)
  4716. // // 出库完成后,要减去对应批次的库存数量
  4717. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  4718. // var maxNumber int64 = 0
  4719. // var minNumber int64 = 0
  4720. // if advice.CountUnit == drup.MinUnit {
  4721. // maxNumber = prescribingNumber / drup.MinNumber
  4722. // minNumber = prescribingNumber % drup.MinNumber
  4723. // if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  4724. // minNumber = maxNumber
  4725. // }
  4726. // } else {
  4727. // maxNumber = prescribingNumber
  4728. // }
  4729. //
  4730. // if warehouse.StockMaxNumber < maxNumber {
  4731. //
  4732. // return errors.New("库存数量不足")
  4733. // }
  4734. //
  4735. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  4736. // warehouse.Mtime = time.Now().Unix()
  4737. //
  4738. // if warehouse.StockMinNumber < minNumber {
  4739. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  4740. // warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  4741. // } else {
  4742. // warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  4743. // }
  4744. //
  4745. // if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
  4746. // warehouse.StockMinNumber = 0
  4747. // }
  4748. //
  4749. // if warehouse.StockMaxNumber < 0 {
  4750. // return errors.New("库存数量不足")
  4751. // }
  4752. //
  4753. // ////扣减库存
  4754. // //if(warehouse.StockMaxNumber > 0){
  4755. // // var sum_count int64
  4756. // // sum_count = maxNumber * drup.MinNumber
  4757. // // ModifyDrugReduceInformationSix(warehouse.DrugId,sum_count,warehouse.OrgId,warehouse.RetailPrice)
  4758. // //}
  4759. // //if(warehouse.StockMinNumber > 0){
  4760. // // var sum_count int64
  4761. // // sum_count = minNumber
  4762. // // ModifyDrugReduceInformationSix(warehouse.DrugId,sum_count,warehouse.OrgId,warehouse.RetailPrice)
  4763. // //}
  4764. //
  4765. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4766. //
  4767. // if errThree != nil {
  4768. // return errThree
  4769. // }
  4770. //
  4771. // //入库
  4772. // drugInfo := models.DrugWarehouseInfo{
  4773. // WarehousingOrder: drugWarehouse.WarehousingOrder,
  4774. // WarehousingId: drugWarehouse.WarehousingId,
  4775. // DrugId: warehouse.DrugId,
  4776. // Number: warehouse.Number,
  4777. // ProductDate: warehouse.ProductDate,
  4778. // ExpiryDate: warehouse.ExpiryDate,
  4779. // WarehousingCount: deliver_number,
  4780. // Price: warehouse.RetailPrice,
  4781. // TotalPrice: warehouse.TotalPrice,
  4782. // Status: 1,
  4783. // Ctime: warehouse.Ctime,
  4784. // Remark: "",
  4785. // OrgId: warehouse.OrgId,
  4786. // Type: 13,
  4787. // Manufacturer: warehouse.Manufacturer,
  4788. // Dealer: warehouse.Dealer,
  4789. // StockMaxNumber: 0,
  4790. // StockMinNumber: deliver_number,
  4791. // BatchNumber: warehouse.BatchNumber,
  4792. // MaxUnit: drup.MinUnit,
  4793. // MinUnit: drup.MinUnit,
  4794. // RetailPrice: warehouse.RetailPrice,
  4795. // StorehouseId: drugWarehouse.StorehouseId,
  4796. // SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  4797. // IsCheck: 1,
  4798. // }
  4799. //
  4800. // CreateDrugWarehouseInfo(drugInfo)
  4801. //
  4802. // flow := models.DrugFlow{
  4803. // WarehousingId: drugWarehouse.WarehousingId,
  4804. // DrugId: warehouse.DrugId,
  4805. // Number: warehouse.Number,
  4806. // BatchNumber: warehouse.BatchNumber,
  4807. // Count: deliver_number,
  4808. // UserOrgId: warehouse.OrgId,
  4809. // PatientId: 0,
  4810. // SystemTime: 0,
  4811. // ConsumableType: 13,
  4812. // IsSys: 13,
  4813. // WarehousingOrder: drugWarehouse.WarehousingOrder,
  4814. // WarehouseOutId: 0,
  4815. // WarehouseOutOrderNumber: "",
  4816. // IsEdit: 0,
  4817. // CancelStockId: 0,
  4818. // CancelOrderNumber: "",
  4819. // Manufacturer: warehouse.Manufacturer,
  4820. // Dealer: warehouse.Dealer,
  4821. // Creator: 0,
  4822. // UpdateCreator: 0,
  4823. // Status: 1,
  4824. // Ctime: time.Now().Unix(),
  4825. // Mtime: 0,
  4826. // Price: warehouse.RetailPrice,
  4827. // WarehousingDetailId: warehouse.ID,
  4828. // WarehouseOutDetailId: 0,
  4829. // CancelOutDetailId: 0,
  4830. // ExpireDate: warehouse.ExpiryDate,
  4831. // ProductDate: warehouse.ProductDate,
  4832. // MaxUnit: drup.MinUnit,
  4833. // MinUnit: drup.MinUnit,
  4834. // StockMaxNumber: 0,
  4835. // StockMinNumber: deliver_number,
  4836. // StorehouseId: drugWarehouse.StorehouseId,
  4837. // SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  4838. // }
  4839. //
  4840. // CreateDrugFlowOne(flow)
  4841. //
  4842. // return nil
  4843. // } else {
  4844. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  4845. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4846. // WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  4847. // WarehouseOutId: advice.WarehouseOutId,
  4848. // Status: 1,
  4849. // Ctime: time.Now().Unix(),
  4850. // Remark: advice.Remark,
  4851. // OrgId: orgID,
  4852. // Type: 1,
  4853. // Manufacturer: advice.Manufacturer,
  4854. // Dealer: warehouse.Dealer,
  4855. // IsSys: 12,
  4856. // SysRecordTime: advice.Ctime,
  4857. // DrugId: advice.DrugId,
  4858. // ExpiryDate: warehouse.ExpiryDate,
  4859. // ProductDate: warehouse.ProductDate,
  4860. // Number: warehouse.Number,
  4861. // BatchNumber: warehouse.BatchNumber,
  4862. // Count: stock_number,
  4863. // RetailPrice: advice.RetailPrice,
  4864. // Price: warehouse.RetailPrice,
  4865. // WarehouseInfoId: warehouse.ID,
  4866. // CountUnit: drup.MinUnit,
  4867. // AdviceId: advice.ID,
  4868. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4869. // StorehouseId: advice.StorehouseId,
  4870. // SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  4871. // IsCheck: 1,
  4872. // }
  4873. // warehouseOutInfo.Count = stock_number
  4874. //
  4875. // //warehouseOutInfo.CountUnit = advice.CountUnit
  4876. //
  4877. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  4878. // if errOne != nil {
  4879. // return errOne
  4880. // }
  4881. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4882. // drugflow := models.DrugFlow{
  4883. // WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  4884. // WarehouseOutId: advice.WarehouseOutId,
  4885. // DrugId: advice.DrugId,
  4886. // Number: warehouse.Number,
  4887. // ProductDate: warehouse.ProductDate,
  4888. // ExpireDate: warehouse.ExpiryDate,
  4889. // Count: stock_number,
  4890. // Price: warehouse.RetailPrice,
  4891. // Status: 1,
  4892. // Ctime: time.Now().Unix(),
  4893. // UserOrgId: advice.OrgId,
  4894. // Manufacturer: warehouse.Manufacturer,
  4895. // Dealer: warehouse.Dealer,
  4896. // BatchNumber: warehouse.BatchNumber,
  4897. // MaxUnit: drup.MinUnit,
  4898. // ConsumableType: 12,
  4899. // IsEdit: 1,
  4900. // Creator: 0,
  4901. // IsSys: 12,
  4902. // WarehouseOutDetailId: drugWareInfo.ID,
  4903. // AdviceId: advice.ID,
  4904. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4905. // StorehouseId: advice.StorehouseId,
  4906. // SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  4907. // }
  4908. // CreateDrugFlowOne(drugflow)
  4909. // // 出库完成后,要将该批次库存清零
  4910. //
  4911. // //入库
  4912. // drugInfo := models.DrugWarehouseInfo{
  4913. // WarehousingOrder: drugWarehouse.WarehousingOrder,
  4914. // WarehousingId: drugWarehouse.WarehousingId,
  4915. // DrugId: warehouse.DrugId,
  4916. // Number: warehouse.Number,
  4917. // ProductDate: warehouse.ProductDate,
  4918. // ExpiryDate: warehouse.ExpiryDate,
  4919. // WarehousingCount: stock_number,
  4920. // Price: warehouse.RetailPrice,
  4921. // TotalPrice: warehouse.TotalPrice,
  4922. // Status: 1,
  4923. // Ctime: warehouse.Ctime,
  4924. // Remark: "",
  4925. // OrgId: warehouse.OrgId,
  4926. // Type: 13,
  4927. // Manufacturer: warehouse.Manufacturer,
  4928. // Dealer: warehouse.Dealer,
  4929. // StockMaxNumber: 0,
  4930. // StockMinNumber: stock_number,
  4931. // RetailTotalPrice: 0,
  4932. // BatchNumber: warehouse.BatchNumber,
  4933. // MaxUnit: drup.MinUnit,
  4934. // MinUnit: drup.MinUnit,
  4935. // RetailPrice: warehouse.RetailPrice,
  4936. // StorehouseId: drugWarehouse.StorehouseId,
  4937. // SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  4938. // }
  4939. //
  4940. // CreateDrugWarehouseInfo(drugInfo)
  4941. //
  4942. // flow := models.DrugFlow{
  4943. // WarehousingId: drugWarehouse.WarehousingId,
  4944. // DrugId: warehouse.DrugId,
  4945. // Number: warehouse.Number,
  4946. // BatchNumber: warehouse.BatchNumber,
  4947. // Count: stock_number,
  4948. // UserOrgId: warehouse.OrgId,
  4949. // PatientId: 0,
  4950. // SystemTime: 0,
  4951. // ConsumableType: 13,
  4952. // IsSys: 13,
  4953. // WarehousingOrder: drugWarehouse.WarehousingOrder,
  4954. // WarehouseOutId: 0,
  4955. // WarehouseOutOrderNumber: "",
  4956. // IsEdit: 0,
  4957. // CancelStockId: 0,
  4958. // CancelOrderNumber: "",
  4959. // Manufacturer: warehouse.Manufacturer,
  4960. // Dealer: warehouse.Dealer,
  4961. // Creator: 0,
  4962. // UpdateCreator: 0,
  4963. // Status: 1,
  4964. // Ctime: time.Now().Unix(),
  4965. // Mtime: 0,
  4966. // Price: warehouse.RetailPrice,
  4967. // WarehousingDetailId: warehouse.ID,
  4968. // WarehouseOutDetailId: 0,
  4969. // CancelOutDetailId: 0,
  4970. // ExpireDate: warehouse.ExpiryDate,
  4971. // ProductDate: warehouse.ProductDate,
  4972. // MaxUnit: drup.MinUnit,
  4973. // MinUnit: drup.MinUnit,
  4974. // StockMaxNumber: 0,
  4975. // StockMinNumber: stock_number,
  4976. // StorehouseId: drugWarehouse.StorehouseId,
  4977. // SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  4978. // }
  4979. //
  4980. // CreateDrugFlowOne(flow)
  4981. //
  4982. // warehouse.StockMaxNumber = 0
  4983. // warehouse.StockMinNumber = 0
  4984. // warehouse.Mtime = time.Now().Unix()
  4985. //
  4986. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4987. // if errThree != nil {
  4988. // return errThree
  4989. // }
  4990. //
  4991. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  4992. // prescribingNumber_two_temp := deliver_number - stock_number
  4993. //
  4994. // advice.CountUnit = drup.MinUnit
  4995. //
  4996. // AutoDrugDeliverInfoTwelve(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, drugWarehouse)
  4997. // }
  4998. //
  4999. // return
  5000. //}
  5001. //
  5002. //// 药品手动出库 递归方式
  5003. //func AutoDrugDeliverInfoTwenty(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  5004. //
  5005. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  5006. // var deliver_number int64 = 0
  5007. // var stock_number int64 = 0
  5008. //
  5009. // if advice.CountUnit == drup.MaxUnit {
  5010. // deliver_number = prescribingNumber * drup.MinNumber
  5011. // } else {
  5012. // deliver_number = prescribingNumber
  5013. // }
  5014. //
  5015. // // 根据先进先出原则,查询最先入库的批次,进行出库
  5016. // // 如果没有对应的库存,则报错
  5017. // //开启事物
  5018. // lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  5019. // if lastWarehouse.StockMinNumber >= drup.MinNumber {
  5020. // var stockMax int64
  5021. // var stockMin int64
  5022. // stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  5023. // stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  5024. // ChangeMaxNumber(lastWarehouse.ID, stockMax)
  5025. // UpdateMinNumber(lastWarehouse.ID, stockMin)
  5026. // }
  5027. //
  5028. // warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  5029. //
  5030. // if err != nil {
  5031. //
  5032. // return err
  5033. // }
  5034. //
  5035. // // 将该批次的剩余库存数量转换为拆零数量
  5036. //
  5037. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  5038. //
  5039. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  5040. // if stock_number >= deliver_number {
  5041. //
  5042. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  5043. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  5044. // WarehouseOutId: warehouseout.ID,
  5045. // Status: 1,
  5046. // Ctime: time.Now().Unix(),
  5047. // Remark: advice.Remark,
  5048. // OrgId: orgID,
  5049. // Type: 1,
  5050. // Manufacturer: advice.Manufacturer,
  5051. // Dealer: warehouse.Dealer,
  5052. // IsSys: 0,
  5053. // SysRecordTime: advice.Ctime,
  5054. // DrugId: advice.DrugId,
  5055. // ExpiryDate: advice.ExpiryDate,
  5056. // ProductDate: advice.ProductDate,
  5057. // Number: advice.Number,
  5058. // BatchNumber: warehouse.BatchNumber,
  5059. // Count: deliver_number,
  5060. // RetailPrice: advice.RetailPrice,
  5061. // Price: warehouse.RetailPrice,
  5062. // WarehouseInfoId: warehouse.ID,
  5063. // CountUnit: drup.MinUnit,
  5064. // AdviceId: advice.ID,
  5065. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5066. // StorehouseId: advice.StorehouseId,
  5067. // AdminUserId: advice.AdminUserId,
  5068. // IsCheck: 1,
  5069. // }
  5070. //
  5071. // if warehouse.RetailPrice == 0 {
  5072. // warehouseOutInfo.Price = advice.Price
  5073. // }
  5074. //
  5075. // warehouseOutInfo.Count = prescribingNumber
  5076. // warehouseOutInfo.CountUnit = advice.CountUnit
  5077. //
  5078. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  5079. // if errOne != nil {
  5080. // return errOne
  5081. // }
  5082. //
  5083. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  5084. // drugflow := models.DrugFlow{
  5085. // WarehouseOutId: warehouseout.ID,
  5086. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  5087. // DrugId: advice.DrugId,
  5088. // Number: warehouse.Number,
  5089. // ProductDate: advice.ProductDate,
  5090. // ExpireDate: advice.ExpiryDate,
  5091. // Count: deliver_number,
  5092. // Price: warehouse.RetailPrice,
  5093. // Status: 1,
  5094. // Ctime: time.Now().Unix(),
  5095. // UserOrgId: advice.OrgId,
  5096. // Manufacturer: advice.Manufacturer,
  5097. // Dealer: advice.Dealer,
  5098. // BatchNumber: warehouse.BatchNumber,
  5099. // MaxUnit: drup.MinUnit,
  5100. // ConsumableType: 2,
  5101. // IsEdit: 1,
  5102. // Creator: 0,
  5103. // IsSys: 0,
  5104. // WarehouseOutDetailId: drugWareInfo.ID,
  5105. // AdviceId: advice.ID,
  5106. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5107. // StorehouseId: advice.StorehouseId,
  5108. // AdminUserId: advice.AdminUserId,
  5109. // }
  5110. // if warehouse.RetailPrice == 0 {
  5111. // drugflow.Price = advice.Price
  5112. // }
  5113. //
  5114. // CreateDrugFlowOne(drugflow)
  5115. // // 出库完成后,要减去对应批次的库存数量
  5116. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  5117. // var maxNumber int64 = 0
  5118. // var minNumber int64 = 0
  5119. // //var stock_max_number int64 = 0
  5120. // //stock_max_number = warehouse.StockMaxNumber
  5121. //
  5122. // maxNumber = deliver_number / drup.MinNumber
  5123. // minNumber = deliver_number % drup.MinNumber
  5124. //
  5125. // if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  5126. // minNumber = maxNumber
  5127. // }
  5128. //
  5129. // if drup.MaxUnit != drup.MinUnit {
  5130. // if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  5131. // return errors.New("库存数量不足")
  5132. // }
  5133. // }
  5134. //
  5135. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  5136. //
  5137. // if warehouse.StockMaxNumber < 0 {
  5138. // warehouse.StockMaxNumber = 0
  5139. // }
  5140. // if warehouse.StockMinNumber < 0 {
  5141. // warehouse.StockMinNumber = 0
  5142. // }
  5143. //
  5144. // warehouse.Mtime = time.Now().Unix()
  5145. //
  5146. // if warehouse.StockMinNumber < minNumber {
  5147. //
  5148. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  5149. // warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  5150. // } else {
  5151. // if minNumber > 0 {
  5152. // if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  5153. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  5154. // } else {
  5155. // warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  5156. // }
  5157. // }
  5158. //
  5159. // if minNumber == 0 && maxNumber != 1 {
  5160. // if warehouse.StockMinNumber > 0 {
  5161. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  5162. // }
  5163. //
  5164. // }
  5165. //
  5166. // }
  5167. //
  5168. // if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  5169. // if (warehouse.StockMinNumber - deliver_number) > 0 {
  5170. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  5171. //
  5172. // //if warehouse.StockMinNumber == 0 {
  5173. // // warehouse.StockMaxNumber = stock_max_number
  5174. // //}
  5175. // }
  5176. //
  5177. // }
  5178. //
  5179. // if drup.MaxUnit != drup.MinUnit {
  5180. // if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  5181. // return errors.New("库存数量不足")
  5182. // }
  5183. // }
  5184. // if warehouse.StockMinNumber <= 0 {
  5185. // warehouse.StockMinNumber = 0
  5186. // }
  5187. //
  5188. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  5189. //
  5190. // if errThree != nil {
  5191. // return errThree
  5192. // }
  5193. //
  5194. // //查询默认仓库
  5195. // houseConfig, _ := GetAllStoreHouseConfig(orgID)
  5196. // //查询默认仓库剩余多少库存
  5197. // list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  5198. // var sum_count int64
  5199. // var sum_in_count int64
  5200. // for _, it := range list {
  5201. // baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  5202. // if it.MaxUnit == baseDrug.MaxUnit {
  5203. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  5204. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  5205. // sum_count += it.StockMaxNumber + it.StockMinNumber
  5206. // sum_in_count += it.WarehousingCount
  5207. // }
  5208. // }
  5209. // UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  5210. //
  5211. // return nil
  5212. //
  5213. // } else {
  5214. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  5215. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  5216. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  5217. // WarehouseOutId: warehouseout.ID,
  5218. // Status: 1,
  5219. // Ctime: time.Now().Unix(),
  5220. // Remark: advice.Remark,
  5221. // OrgId: orgID,
  5222. // Type: 1,
  5223. // Manufacturer: advice.Manufacturer,
  5224. // Dealer: warehouse.Dealer,
  5225. // IsSys: 0,
  5226. // SysRecordTime: advice.Ctime,
  5227. // DrugId: advice.DrugId,
  5228. // ExpiryDate: warehouse.ExpiryDate,
  5229. // ProductDate: warehouse.ProductDate,
  5230. // Number: warehouse.Number,
  5231. // BatchNumber: warehouse.BatchNumber,
  5232. // Count: stock_number,
  5233. // RetailPrice: advice.RetailPrice,
  5234. // Price: warehouse.RetailPrice,
  5235. // WarehouseInfoId: warehouse.ID,
  5236. // CountUnit: drup.MinUnit,
  5237. // AdviceId: advice.ID,
  5238. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5239. // StorehouseId: advice.StorehouseId,
  5240. // AdminUserId: advice.AdminUserId,
  5241. // IsCheck: 1,
  5242. // }
  5243. // if warehouse.RetailPrice == 0 {
  5244. // warehouseOutInfo.Price = advice.Price
  5245. // }
  5246. // warehouseOutInfo.Count = stock_number
  5247. //
  5248. // //warehouseOutInfo.CountUnit = advice.CountUnit
  5249. //
  5250. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  5251. // if errOne != nil {
  5252. // return errOne
  5253. // }
  5254. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  5255. // drugflow := models.DrugFlow{
  5256. // WarehouseOutId: warehouseout.ID,
  5257. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  5258. // DrugId: advice.DrugId,
  5259. // Number: warehouse.Number,
  5260. // ProductDate: advice.ProductDate,
  5261. // ExpireDate: advice.ExpiryDate,
  5262. // Count: stock_number,
  5263. // Price: warehouse.RetailPrice,
  5264. // Status: 1,
  5265. // Ctime: time.Now().Unix(),
  5266. // UserOrgId: advice.OrgId,
  5267. // Manufacturer: advice.Manufacturer,
  5268. // Dealer: advice.Dealer,
  5269. // BatchNumber: warehouse.BatchNumber,
  5270. // MaxUnit: drup.MinUnit,
  5271. // ConsumableType: 2,
  5272. // IsEdit: 1,
  5273. // Creator: 0,
  5274. // IsSys: 0,
  5275. // WarehouseOutDetailId: drugWareInfo.ID,
  5276. // AdviceId: advice.ID,
  5277. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5278. // StorehouseId: advice.StorehouseId,
  5279. // AdminUserId: advice.AdminUserId,
  5280. // }
  5281. // if warehouse.RetailPrice == 0 {
  5282. // drugflow.Price = advice.Price
  5283. // }
  5284. // CreateDrugFlowOne(drugflow)
  5285. // // 出库完成后,要将该批次库存清零
  5286. // //扣减库存
  5287. //
  5288. // warehouse.StockMaxNumber = 0
  5289. // warehouse.StockMinNumber = 0
  5290. // warehouse.Mtime = time.Now().Unix()
  5291. //
  5292. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  5293. //
  5294. // //查询默认仓库
  5295. // houseConfig, _ := GetAllStoreHouseConfig(orgID)
  5296. // //查询默认仓库剩余多少库存
  5297. // list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  5298. // var sum_count int64
  5299. // var sum_in_count int64
  5300. // for _, it := range list {
  5301. // baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  5302. // if it.MaxUnit == baseDrug.MaxUnit {
  5303. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  5304. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  5305. // sum_count += it.StockMaxNumber + it.StockMinNumber
  5306. // sum_in_count += it.WarehousingCount
  5307. // }
  5308. // }
  5309. // UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  5310. // if errThree != nil {
  5311. // return errThree
  5312. // }
  5313. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  5314. // prescribingNumber_two_temp := deliver_number - stock_number
  5315. //
  5316. // advice.CountUnit = drup.MinUnit
  5317. //
  5318. // AutoDrugDeliverInfoTwenty(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  5319. // }
  5320. //
  5321. // return
  5322. //}
  5323. //
  5324. //// 药品手动出库 递归方式
  5325. //func AutoDrugDeliverInfoTwentyOne(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  5326. // //开事务
  5327. // //tx := XTWriteDB().Begin()
  5328. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  5329. // var deliver_number int64 = 0
  5330. // var stock_number int64 = 0
  5331. //
  5332. // if advice.CountUnit == drup.MaxUnit {
  5333. // deliver_number = prescribingNumber * drup.MinNumber
  5334. // } else {
  5335. // deliver_number = prescribingNumber
  5336. // }
  5337. //
  5338. // // 根据先进先出原则,查询最先入库的批次,进行出库
  5339. // // 如果没有对应的库存,则报错
  5340. // //开启事物
  5341. // storeConfig, _ := GetAllStoreHouseConfig(orgID)
  5342. // lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  5343. // if lastWarehouse.StockMinNumber >= drup.MinNumber {
  5344. // var stockMax int64
  5345. // var stockMin int64
  5346. // stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  5347. // stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  5348. // ChangeMaxNumber(lastWarehouse.ID, stockMax)
  5349. // UpdateMinNumber(lastWarehouse.ID, stockMin)
  5350. // }
  5351. // warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  5352. //
  5353. // if err != nil {
  5354. //
  5355. // return err
  5356. // }
  5357. //
  5358. // // 将该批次的剩余库存数量转换为拆零数量
  5359. //
  5360. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  5361. //
  5362. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  5363. // if stock_number >= deliver_number {
  5364. //
  5365. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  5366. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  5367. // WarehouseOutId: warehouseout.ID,
  5368. // Status: 1,
  5369. // Ctime: time.Now().Unix(),
  5370. // Remark: advice.Remark,
  5371. // OrgId: orgID,
  5372. // Type: 1,
  5373. // Manufacturer: advice.Manufacturer,
  5374. // Dealer: warehouse.Dealer,
  5375. // IsSys: 0,
  5376. // SysRecordTime: advice.Ctime,
  5377. // DrugId: advice.DrugId,
  5378. // ExpiryDate: advice.ExpiryDate,
  5379. // ProductDate: advice.ProductDate,
  5380. // Number: advice.Number,
  5381. // BatchNumber: warehouse.BatchNumber,
  5382. // Count: deliver_number,
  5383. // RetailPrice: advice.RetailPrice,
  5384. // Price: advice.Price,
  5385. // WarehouseInfoId: warehouse.ID,
  5386. // CountUnit: drup.MinUnit,
  5387. // AdviceId: advice.ID,
  5388. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5389. // StorehouseId: advice.StorehouseId,
  5390. // AdminUserId: advice.AdminUserId,
  5391. // LastPrice: warehouse.Price,
  5392. // StockCount: advice.StockCount,
  5393. // IsCheck: 1,
  5394. // }
  5395. //
  5396. // warehouseOutInfo.Count = prescribingNumber
  5397. // warehouseOutInfo.CountUnit = advice.CountUnit
  5398. //
  5399. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  5400. // drugflow := models.DrugFlow{
  5401. // WarehouseOutId: warehouseout.ID,
  5402. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  5403. // DrugId: advice.DrugId,
  5404. // Number: warehouse.Number,
  5405. // ProductDate: advice.ProductDate,
  5406. // ExpireDate: advice.ExpiryDate,
  5407. // Count: deliver_number,
  5408. // Price: advice.Price,
  5409. // Status: 1,
  5410. // Ctime: time.Now().Unix(),
  5411. // UserOrgId: advice.OrgId,
  5412. // Manufacturer: advice.Manufacturer,
  5413. // Dealer: advice.Dealer,
  5414. // BatchNumber: warehouse.BatchNumber,
  5415. // MaxUnit: drup.MinUnit,
  5416. // ConsumableType: 2,
  5417. // IsEdit: 1,
  5418. // Creator: 0,
  5419. // IsSys: 0,
  5420. // WarehouseOutDetailId: drugWareInfo.ID,
  5421. // AdviceId: advice.ID,
  5422. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5423. // StorehouseId: advice.StorehouseId,
  5424. // AdminUserId: advice.AdminUserId,
  5425. // LastPrice: warehouse.Price,
  5426. // StockCount: advice.StockCount,
  5427. // }
  5428. // if warehouse.RetailPrice == 0 {
  5429. // drugflow.Price = advice.Price
  5430. // }
  5431. //
  5432. // CreateDrugFlowOne(drugflow)
  5433. // // 出库完成后,要减去对应批次的库存数量
  5434. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  5435. // var maxNumber int64 = 0
  5436. // var minNumber int64 = 0
  5437. // //var stock_max_number int64 = 0
  5438. // //stock_max_number = warehouse.StockMaxNumber
  5439. //
  5440. // maxNumber = deliver_number / drup.MinNumber
  5441. // minNumber = deliver_number % drup.MinNumber
  5442. //
  5443. // if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  5444. // minNumber = maxNumber
  5445. // }
  5446. //
  5447. // if drup.MaxUnit != drup.MinUnit {
  5448. // if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  5449. // return errors.New("库存数量不足")
  5450. // }
  5451. // }
  5452. //
  5453. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  5454. //
  5455. // if warehouse.StockMaxNumber < 0 {
  5456. // warehouse.StockMaxNumber = 0
  5457. // }
  5458. // if warehouse.StockMinNumber < 0 {
  5459. // warehouse.StockMinNumber = 0
  5460. // }
  5461. //
  5462. // warehouse.Mtime = time.Now().Unix()
  5463. //
  5464. // if warehouse.StockMinNumber < minNumber {
  5465. //
  5466. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  5467. // warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  5468. // } else {
  5469. // if minNumber > 0 {
  5470. // if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  5471. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  5472. // } else {
  5473. // warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  5474. // }
  5475. // }
  5476. //
  5477. // if minNumber == 0 && maxNumber != 1 {
  5478. // if warehouse.StockMinNumber > 0 {
  5479. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  5480. // }
  5481. //
  5482. // }
  5483. //
  5484. // }
  5485. //
  5486. // if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  5487. // if (warehouse.StockMinNumber - deliver_number) > 0 {
  5488. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  5489. //
  5490. // //if warehouse.StockMinNumber == 0 {
  5491. // // warehouse.StockMaxNumber = stock_max_number
  5492. // //}
  5493. // }
  5494. //
  5495. // }
  5496. //
  5497. // if drup.MaxUnit != drup.MinUnit {
  5498. // if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  5499. // return errors.New("库存数量不足")
  5500. // }
  5501. // }
  5502. // if warehouse.StockMinNumber <= 0 {
  5503. // warehouse.StockMinNumber = 0
  5504. // }
  5505. //
  5506. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  5507. // fmt.Println(errThree)
  5508. // //if errThree != nil {
  5509. // // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  5510. // // tx.Rollback()
  5511. // //} else {
  5512. // // tx.Commit()
  5513. // //}
  5514. //
  5515. // //查询默认仓库
  5516. // houseConfig, _ := GetAllStoreHouseConfig(orgID)
  5517. // //查询默认仓库剩余多少库存
  5518. // list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  5519. // var sum_count int64
  5520. // var sum_in_count int64
  5521. // for _, it := range list {
  5522. // baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  5523. // if it.MaxUnit == baseDrug.MaxUnit {
  5524. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  5525. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  5526. // sum_count += it.StockMaxNumber + it.StockMinNumber
  5527. // sum_in_count += it.WarehousingCount
  5528. // }
  5529. // }
  5530. // UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  5531. //
  5532. // return nil
  5533. //
  5534. // } else {
  5535. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  5536. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  5537. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  5538. // WarehouseOutId: warehouseout.ID,
  5539. // Status: 1,
  5540. // Ctime: time.Now().Unix(),
  5541. // Remark: advice.Remark,
  5542. // OrgId: orgID,
  5543. // Type: 1,
  5544. // Manufacturer: advice.Manufacturer,
  5545. // Dealer: warehouse.Dealer,
  5546. // IsSys: 0,
  5547. // SysRecordTime: advice.Ctime,
  5548. // DrugId: advice.DrugId,
  5549. // ExpiryDate: warehouse.ExpiryDate,
  5550. // ProductDate: warehouse.ProductDate,
  5551. // Number: warehouse.Number,
  5552. // BatchNumber: warehouse.BatchNumber,
  5553. // Count: stock_number,
  5554. // RetailPrice: advice.RetailPrice,
  5555. // Price: advice.Price,
  5556. // WarehouseInfoId: warehouse.ID,
  5557. // CountUnit: drup.MinUnit,
  5558. // AdviceId: advice.ID,
  5559. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5560. // StorehouseId: advice.StorehouseId,
  5561. // AdminUserId: advice.AdminUserId,
  5562. // LastPrice: warehouse.Price,
  5563. // StockCount: advice.StockCount,
  5564. // IsCheck: 1,
  5565. // }
  5566. //
  5567. // warehouseOutInfo.Count = stock_number
  5568. //
  5569. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  5570. //
  5571. // drugflow := models.DrugFlow{
  5572. // WarehouseOutId: warehouseout.ID,
  5573. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  5574. // DrugId: advice.DrugId,
  5575. // Number: warehouse.Number,
  5576. // ProductDate: advice.ProductDate,
  5577. // ExpireDate: advice.ExpiryDate,
  5578. // Count: stock_number,
  5579. // Price: advice.Price,
  5580. // Status: 1,
  5581. // Ctime: time.Now().Unix(),
  5582. // UserOrgId: advice.OrgId,
  5583. // Manufacturer: advice.Manufacturer,
  5584. // Dealer: advice.Dealer,
  5585. // BatchNumber: warehouse.BatchNumber,
  5586. // MaxUnit: drup.MinUnit,
  5587. // ConsumableType: 2,
  5588. // IsEdit: 1,
  5589. // Creator: 0,
  5590. // IsSys: 0,
  5591. // WarehouseOutDetailId: drugWareInfo.ID,
  5592. // AdviceId: advice.ID,
  5593. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5594. // StorehouseId: advice.StorehouseId,
  5595. // AdminUserId: advice.AdminUserId,
  5596. // LastPrice: warehouse.Price,
  5597. // StockCount: advice.StockCount,
  5598. // }
  5599. //
  5600. // CreateDrugFlowOne(drugflow)
  5601. // // 出库完成后,要将该批次库存清零
  5602. // //扣减库存
  5603. //
  5604. // warehouse.StockMaxNumber = 0
  5605. // warehouse.StockMinNumber = 0
  5606. // warehouse.Mtime = time.Now().Unix()
  5607. //
  5608. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  5609. // fmt.Println(errThree)
  5610. // //if errThree != nil {
  5611. // // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  5612. // // tx.Rollback()
  5613. // //} else {
  5614. // // tx.Commit()
  5615. // //}
  5616. //
  5617. // //查询默认仓库
  5618. // houseConfig, _ := GetAllStoreHouseConfig(orgID)
  5619. // //查询默认仓库剩余多少库存
  5620. // list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  5621. // var sum_count int64
  5622. // var sum_in_count int64
  5623. // for _, it := range list {
  5624. // baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  5625. // if it.MaxUnit == baseDrug.MaxUnit {
  5626. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  5627. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  5628. // sum_count += it.StockMaxNumber + it.StockMinNumber
  5629. // sum_in_count += it.WarehousingCount
  5630. // }
  5631. // }
  5632. // UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  5633. // if errThree != nil {
  5634. // return errThree
  5635. // }
  5636. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  5637. // prescribingNumber_two_temp := deliver_number - stock_number
  5638. //
  5639. // advice.CountUnit = drup.MinUnit
  5640. //
  5641. // AutoDrugDeliverInfoTwentyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  5642. // }
  5643. //
  5644. // return
  5645. //}
  5646. //
  5647. ////耗材手动出库
  5648. //func ConsumablesDeliveryThirty(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64) (err error) {
  5649. //
  5650. // var deliver_number int64 = 0
  5651. // var stock_number int64 = 0
  5652. // var maxNumber int64 = 0
  5653. //
  5654. // deliver_number = goods.Count
  5655. //
  5656. // // 根据先进先出原则,查询最先入库的批次,进行出库
  5657. //
  5658. // // 如果没有对应的库存,则报错
  5659. // warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  5660. // if err != nil {
  5661. // goodErrcode := models.XtGoodErrcode{
  5662. // UserOrgId: goods.OrgId,
  5663. // Errcode: "查询库存信息报错",
  5664. // GoodId: goods.GoodId,
  5665. // Status: 1,
  5666. // Ctime: time.Now().Unix(),
  5667. // Mtime: 0,
  5668. // Count: 0,
  5669. // StockCount: 0,
  5670. // Creater: creator,
  5671. // BatchNumberId: warehouse.ID,
  5672. // WarehouseOutId: 0,
  5673. // }
  5674. // CreateGoodErrcode(goodErrcode)
  5675. // return err
  5676. // }
  5677. //
  5678. // stock_number = warehouse.StockCount
  5679. //
  5680. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  5681. // if stock_number >= deliver_number {
  5682. // maxNumber = goods.Count
  5683. // warehouse.StockCount = warehouse.StockCount - maxNumber
  5684. // warehouse.Mtime = time.Now().Unix()
  5685. //
  5686. // //扣减库存
  5687. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  5688. // if errThree != nil {
  5689. // goodErrcode := models.XtGoodErrcode{
  5690. // UserOrgId: goods.OrgId,
  5691. // Errcode: "扣减库存失败",
  5692. // GoodId: goods.GoodId,
  5693. // Status: 1,
  5694. // Ctime: time.Now().Unix(),
  5695. // Mtime: 0,
  5696. // Count: 0,
  5697. // StockCount: 0,
  5698. // Creater: creator,
  5699. // BatchNumberId: warehouse.ID,
  5700. // WarehouseOutId: 0,
  5701. // }
  5702. // CreateGoodErrcode(goodErrcode)
  5703. // return errThree
  5704. // }
  5705. //
  5706. // //查询剩余库存
  5707. // goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID)
  5708. // var sum_count int64
  5709. // for _, item := range goodList {
  5710. // sum_count += item.StockCount
  5711. // }
  5712. //
  5713. // warehouseOutInfo := &models.WarehouseOutInfo{
  5714. // WarehouseInfotId: warehouse.ID,
  5715. // }
  5716. // //添加入库单详情ID
  5717. // errOne := UpdatedSigleWarehouseOutInfo(goods.ID, warehouseOutInfo)
  5718. // if errOne != nil {
  5719. // goodErrcode := models.XtGoodErrcode{
  5720. // UserOrgId: goods.OrgId,
  5721. // Errcode: "添加入库单详情ID",
  5722. // GoodId: goods.GoodId,
  5723. // Status: 1,
  5724. // Ctime: time.Now().Unix(),
  5725. // Mtime: 0,
  5726. // Count: 0,
  5727. // StockCount: 0,
  5728. // Creater: creator,
  5729. // BatchNumberId: warehouse.ID,
  5730. // WarehouseOutId: 0,
  5731. // }
  5732. // CreateGoodErrcode(goodErrcode)
  5733. // return errOne
  5734. // }
  5735. //
  5736. // stockFlow := models.VmStockFlow{
  5737. // WarehousingId: warehouse.ID,
  5738. // GoodId: goods.GoodId,
  5739. // Number: warehouse.Number,
  5740. // LicenseNumber: goods.LicenseNumber,
  5741. // Count: goods.Count,
  5742. // UserOrgId: goods.OrgId,
  5743. // PatientId: goods.PatientId,
  5744. // SystemTime: record_time,
  5745. // ConsumableType: 2,
  5746. // IsSys: goods.IsSys,
  5747. // WarehousingOrder: warehouse.WarehousingOrder,
  5748. // WarehouseOutId: warehouseOut.ID,
  5749. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  5750. // IsEdit: 1,
  5751. // CancelStockId: 0,
  5752. // CancelOrderNumber: "",
  5753. // Manufacturer: goods.Manufacturer,
  5754. // Dealer: goods.Dealer,
  5755. // Creator: creator,
  5756. // UpdateCreator: 0,
  5757. // Status: 1,
  5758. // Ctime: time.Now().Unix(),
  5759. // Mtime: 0,
  5760. // Price: warehouse.PackingPrice, //零售价
  5761. // WarehousingDetailId: warehouse.ID,
  5762. // WarehouseOutDetailId: goods.ID,
  5763. // CancelOutDetailId: 0,
  5764. // ProductDate: goods.ProductDate,
  5765. // ExpireDate: goods.ExpiryDate,
  5766. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5767. // StorehouseId: warehouse.StorehouseId,
  5768. // AdminUserId: goods.AdminUserId,
  5769. // BuyPrice: warehouse.Price, //进货价
  5770. // StockCount: goods.StockCount,
  5771. // BatchNumberCount: warehouse.StockCount - goods.Count,
  5772. // IsCheck: 1,
  5773. // OverCount: sum_count,
  5774. // RegisterNumber: goods.RegisterNumber,
  5775. // }
  5776. // //创建出库流水
  5777. // errflow := CreateStockFlowOne(stockFlow)
  5778. // if errflow != nil {
  5779. // goodErrcode := models.XtGoodErrcode{
  5780. // UserOrgId: goods.OrgId,
  5781. // Errcode: "创建流水报错",
  5782. // GoodId: goods.GoodId,
  5783. // Status: 1,
  5784. // Ctime: time.Now().Unix(),
  5785. // Mtime: 0,
  5786. // Count: 0,
  5787. // StockCount: 0,
  5788. // Creater: creator,
  5789. // BatchNumberId: warehouse.ID,
  5790. // WarehouseOutId: 0,
  5791. // }
  5792. // CreateGoodErrcode(goodErrcode)
  5793. // }
  5794. //
  5795. // //添加入库ID
  5796. // errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, goods.OrgId, sum_count)
  5797. // if errWarehouse != nil {
  5798. // goodErrcode := models.XtGoodErrcode{
  5799. // UserOrgId: goods.OrgId,
  5800. // Errcode: "添加入库ID报错",
  5801. // GoodId: goods.GoodId,
  5802. // Status: 1,
  5803. // Ctime: time.Now().Unix(),
  5804. // Mtime: 0,
  5805. // Count: 0,
  5806. // StockCount: 0,
  5807. // Creater: creator,
  5808. // BatchNumberId: warehouse.ID,
  5809. // WarehouseOutId: 0,
  5810. // }
  5811. // CreateGoodErrcode(goodErrcode)
  5812. // }
  5813. //
  5814. // //更新出库数量
  5815. // errsumcode := ModifyGoodSumCount(goods.StorehouseId, goods.Count, goods.OrgId, goods.GoodId)
  5816. // if errsumcode != nil {
  5817. // goodErrcode := models.XtGoodErrcode{
  5818. // UserOrgId: goods.OrgId,
  5819. // Errcode: "更新出库数量报错",
  5820. // GoodId: goods.GoodId,
  5821. // Status: 1,
  5822. // Ctime: time.Now().Unix(),
  5823. // Mtime: 0,
  5824. // Count: 0,
  5825. // StockCount: 0,
  5826. // Creater: creator,
  5827. // BatchNumberId: warehouse.ID,
  5828. // WarehouseOutId: 0,
  5829. // }
  5830. // CreateGoodErrcode(goodErrcode)
  5831. // }
  5832. //
  5833. // if warehouse.StockCount < maxNumber {
  5834. // goodErrcode := models.XtGoodErrcode{
  5835. // UserOrgId: goods.OrgId,
  5836. // Errcode: "出库接口库存数量不足",
  5837. // GoodId: goods.GoodId,
  5838. // Status: 1,
  5839. // Ctime: time.Now().Unix(),
  5840. // Mtime: 0,
  5841. // Count: 0,
  5842. // StockCount: 0,
  5843. // Creater: creator,
  5844. // BatchNumberId: warehouse.ID,
  5845. // WarehouseOutId: 0,
  5846. // }
  5847. // CreateGoodErrcode(goodErrcode)
  5848. // return errors.New("库存数量不足")
  5849. //
  5850. // }
  5851. //
  5852. // return nil
  5853. // } else {
  5854. // // 出库完成后,要将该批次库存清零
  5855. // warehouse.StockCount = 0
  5856. // warehouse.Mtime = time.Now().Unix()
  5857. //
  5858. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  5859. // if errThree != nil {
  5860. // goodErrcode := models.XtGoodErrcode{
  5861. // UserOrgId: goods.OrgId,
  5862. // Errcode: "扣减库存报错",
  5863. // GoodId: goods.GoodId,
  5864. // Status: 1,
  5865. // Ctime: time.Now().Unix(),
  5866. // Mtime: 0,
  5867. // Count: 0,
  5868. // StockCount: 0,
  5869. // Creater: creator,
  5870. // BatchNumberId: warehouse.ID,
  5871. // WarehouseOutId: 0,
  5872. // }
  5873. // CreateGoodErrcode(goodErrcode)
  5874. // return errThree
  5875. // }
  5876. //
  5877. // //查询剩余库存
  5878. // goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID)
  5879. // var sum_count int64
  5880. // for _, item := range goodList {
  5881. // sum_count += item.StockCount
  5882. // }
  5883. //
  5884. // stockFlow := models.VmStockFlow{
  5885. // WarehousingId: warehouse.ID,
  5886. // GoodId: goods.GoodId,
  5887. // Number: warehouse.Number,
  5888. // LicenseNumber: goods.LicenseNumber,
  5889. // Count: stock_number,
  5890. // UserOrgId: goods.OrgId,
  5891. // PatientId: goods.PatientId,
  5892. // SystemTime: record_time,
  5893. // ConsumableType: 2,
  5894. // IsSys: goods.IsSys,
  5895. // WarehousingOrder: warehouse.WarehousingOrder,
  5896. // WarehouseOutId: warehouseOut.ID,
  5897. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  5898. // IsEdit: 1,
  5899. // CancelStockId: 0,
  5900. // CancelOrderNumber: "",
  5901. // Manufacturer: goods.Manufacturer,
  5902. // Dealer: goods.Dealer,
  5903. // Creator: creator,
  5904. // UpdateCreator: 0,
  5905. // Status: 1,
  5906. // Ctime: time.Now().Unix(),
  5907. // Mtime: 0,
  5908. // Price: warehouse.PackingPrice, //零售价
  5909. // WarehousingDetailId: warehouse.ID,
  5910. // WarehouseOutDetailId: goods.ID,
  5911. // CancelOutDetailId: 0,
  5912. // ProductDate: goods.ProductDate,
  5913. // ExpireDate: goods.ExpiryDate,
  5914. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5915. // StorehouseId: warehouse.StorehouseId,
  5916. // AdminUserId: goods.AdminUserId,
  5917. // BuyPrice: warehouse.Price, //进货价
  5918. // StockCount: goods.StockCount,
  5919. // BatchNumberCount: 0,
  5920. // IsCheck: 1,
  5921. // OverCount: sum_count,
  5922. // RegisterNumber: goods.RegisterNumber,
  5923. // }
  5924. // //创建出库流水
  5925. // errflow := CreateStockFlowOne(stockFlow)
  5926. // if errflow != nil {
  5927. // goodErrcode := models.XtGoodErrcode{
  5928. // UserOrgId: goods.OrgId,
  5929. // Errcode: "创建出库流水报错",
  5930. // GoodId: goods.GoodId,
  5931. // Status: 1,
  5932. // Ctime: time.Now().Unix(),
  5933. // Mtime: 0,
  5934. // Count: 0,
  5935. // StockCount: 0,
  5936. // Creater: creator,
  5937. // BatchNumberId: warehouse.ID,
  5938. // WarehouseOutId: 0,
  5939. // }
  5940. // CreateGoodErrcode(goodErrcode)
  5941. // }
  5942. //
  5943. // errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, goods.OrgId, sum_count)
  5944. // if errWarehouse != nil {
  5945. // goodErrcode := models.XtGoodErrcode{
  5946. // UserOrgId: goods.OrgId,
  5947. // Errcode: "添加入库ID报错",
  5948. // GoodId: goods.GoodId,
  5949. // Status: 1,
  5950. // Ctime: time.Now().Unix(),
  5951. // Mtime: 0,
  5952. // Count: 0,
  5953. // StockCount: 0,
  5954. // Creater: creator,
  5955. // BatchNumberId: warehouse.ID,
  5956. // WarehouseOutId: 0,
  5957. // }
  5958. // CreateGoodErrcode(goodErrcode)
  5959. // }
  5960. //
  5961. // //更新出库数量
  5962. // errcodecout := ModifyGoodSumCount(goods.StorehouseId, stock_number, goods.OrgId, goods.GoodId)
  5963. //
  5964. // if errcodecout != nil {
  5965. // goodErrcode := models.XtGoodErrcode{
  5966. // UserOrgId: goods.OrgId,
  5967. // Errcode: "更新出库数量报错",
  5968. // GoodId: goods.GoodId,
  5969. // Status: 1,
  5970. // Ctime: time.Now().Unix(),
  5971. // Mtime: 0,
  5972. // Count: 0,
  5973. // StockCount: 0,
  5974. // Creater: creator,
  5975. // BatchNumberId: warehouse.ID,
  5976. // WarehouseOutId: 0,
  5977. // }
  5978. // CreateGoodErrcode(goodErrcode)
  5979. // }
  5980. // }
  5981. //
  5982. // goods.Count = deliver_number - stock_number
  5983. // ConsumablesDeliveryThirty(orgID, record_time, goods, warehouseOut, count, creator)
  5984. //
  5985. // return nil
  5986. //}
  5987. //
  5988. //// 药品手动出库 递归方式
  5989. //func AutoDrugDeliverInfoFourtyOne(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  5990. //
  5991. // fmt.Println("出库数量", prescribingNumber)
  5992. //
  5993. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  5994. // var deliver_number int64 = 0
  5995. // var stock_number int64 = 0
  5996. // var retail_price float64
  5997. // if advice.CountUnit == drup.MaxUnit {
  5998. // deliver_number = prescribingNumber * drup.MinNumber
  5999. // formatInt := strconv.FormatInt(prescribingNumber, 10)
  6000. // floatcount, _ := strconv.ParseFloat(formatInt, 64)
  6001. // retail_price = floatcount * advice.Price
  6002. // } else {
  6003. // deliver_number = prescribingNumber
  6004. // formatInt := strconv.FormatInt(prescribingNumber, 10)
  6005. // floatcount, _ := strconv.ParseFloat(formatInt, 64)
  6006. // retail_price = floatcount * advice.Price
  6007. // }
  6008. //
  6009. // lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  6010. // if lastWarehouse.StockMinNumber >= drup.MinNumber {
  6011. // var stockMax int64
  6012. // var stockMin int64
  6013. // stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  6014. // stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  6015. // fmt.Println("stockmax", stockMax)
  6016. // fmt.Println("stockMin", stockMin)
  6017. // ChangeMaxNumber(lastWarehouse.ID, stockMax)
  6018. // UpdateMinNumber(lastWarehouse.ID, stockMin)
  6019. // }
  6020. //
  6021. // warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  6022. //
  6023. // if err != nil {
  6024. //
  6025. // return err
  6026. // }
  6027. //
  6028. // // 将该批次的剩余库存数量转换为拆零数量
  6029. // if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  6030. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  6031. // }
  6032. // if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  6033. // stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  6034. // }
  6035. // if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  6036. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  6037. // }
  6038. //
  6039. // fmt.Println("stock_number", stock_number)
  6040. // fmt.Println("deliver_number", deliver_number)
  6041. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6042. // if stock_number >= deliver_number {
  6043. //
  6044. // // 出库完成后,要减去对应批次的库存数量
  6045. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  6046. // var maxNumber int64 = 0
  6047. // var minNumber int64 = 0
  6048. //
  6049. // maxNumber = deliver_number / drup.MinNumber
  6050. // minNumber = deliver_number % drup.MinNumber
  6051. //
  6052. // if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  6053. // minNumber = maxNumber
  6054. // }
  6055. //
  6056. // if drup.MaxUnit != drup.MinUnit {
  6057. // if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  6058. // return errors.New("库存数量不足")
  6059. // }
  6060. // }
  6061. //
  6062. // if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  6063. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  6064. // }
  6065. // if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  6066. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  6067. // }
  6068. // if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  6069. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  6070. // }
  6071. //
  6072. // if warehouse.StockMaxNumber < 0 {
  6073. // warehouse.StockMaxNumber = 0
  6074. // }
  6075. // if warehouse.StockMinNumber < 0 {
  6076. // warehouse.StockMinNumber = 0
  6077. // }
  6078. //
  6079. // warehouse.Mtime = time.Now().Unix()
  6080. //
  6081. // if warehouse.StockMinNumber < minNumber {
  6082. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  6083. // warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  6084. // } else {
  6085. // fmt.Println("maxNumber23323323332322323w9", maxNumber)
  6086. // fmt.Println("maxNumber23323323332322323w9", minNumber)
  6087. // if minNumber > 0 {
  6088. // if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  6089. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6090. // } else {
  6091. // if (warehouse.StockMinNumber - minNumber) >= 0 {
  6092. // warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  6093. // }
  6094. //
  6095. // }
  6096. // }
  6097. //
  6098. // if minNumber == 0 && maxNumber != 1 {
  6099. // if warehouse.StockMinNumber > 0 {
  6100. // if (warehouse.StockMinNumber - deliver_number) >= 0 {
  6101. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6102. // }
  6103. // }
  6104. // }
  6105. // }
  6106. //
  6107. // if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  6108. // if (warehouse.StockMinNumber - deliver_number) >= 0 {
  6109. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6110. // }
  6111. //
  6112. // }
  6113. //
  6114. // if drup.MaxUnit != drup.MinUnit {
  6115. // if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  6116. // return errors.New("库存数量不足")
  6117. // }
  6118. // }
  6119. // if warehouse.StockMinNumber <= 0 {
  6120. // warehouse.StockMinNumber = 0
  6121. // }
  6122. //
  6123. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6124. //
  6125. // if errThree != nil {
  6126. // return errors.New("扣减库存失败")
  6127. // }
  6128. //
  6129. // //查询该药品剩余库存
  6130. // infolist, _ := FindDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId)
  6131. // var over_count int64
  6132. // for _, its := range infolist {
  6133. // if its.MaxUnit == drup.MaxUnit {
  6134. // its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  6135. // }
  6136. // over_count += its.StockMaxNumber + its.StockMinNumber
  6137. // }
  6138. //
  6139. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  6140. // drugflow := models.DrugFlow{
  6141. // WarehouseOutId: warehouseout.ID,
  6142. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  6143. // DrugId: advice.DrugId,
  6144. // Number: warehouse.Number,
  6145. // ProductDate: advice.ProductDate,
  6146. // ExpireDate: advice.ExpiryDate,
  6147. // Count: deliver_number,
  6148. // Price: warehouse.RetailPrice,
  6149. // Status: 1,
  6150. // Ctime: time.Now().Unix(),
  6151. // UserOrgId: advice.OrgId,
  6152. // Manufacturer: advice.Manufacturer,
  6153. // Dealer: advice.Dealer,
  6154. // BatchNumber: warehouse.BatchNumber,
  6155. // MaxUnit: drup.MinUnit,
  6156. // ConsumableType: 2,
  6157. // IsEdit: 1,
  6158. // Creator: 0,
  6159. // IsSys: 0,
  6160. // WarehouseOutDetailId: drugWareInfo.ID,
  6161. // AdviceId: advice.ID,
  6162. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6163. // StorehouseId: advice.StorehouseId,
  6164. // AdminUserId: advice.AdminUserId,
  6165. // LastPrice: warehouse.Price,
  6166. // StockCount: advice.StockCount,
  6167. // WarehousingDetailId: warehouse.ID,
  6168. // OverCount: over_count,
  6169. // RetailPrice: retail_price, //手动出库出库价格
  6170. // WarehousingId: warehouse.ID,
  6171. // }
  6172. // if warehouse.RetailPrice == 0 {
  6173. // drugflow.Price = advice.Price
  6174. // }
  6175. //
  6176. // CreateDrugFlowOne(drugflow)
  6177. //
  6178. // //更新出库id
  6179. // UpdateDrugWarehouseOutInfo(advice.ID, warehouse.ID, advice.OrgId, over_count)
  6180. //
  6181. // return nil
  6182. //
  6183. // } else {
  6184. // stock_number_int := strconv.FormatInt(stock_number, 10)
  6185. // stock_number_int_count, _ := strconv.ParseFloat(stock_number_int, 64)
  6186. // retail_price = stock_number_int_count * advice.Price
  6187. // //扣减库存
  6188. // warehouse.StockMaxNumber = 0
  6189. // warehouse.StockMinNumber = 0
  6190. // warehouse.Mtime = time.Now().Unix()
  6191. //
  6192. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6193. // if errThree != nil {
  6194. // return errThree
  6195. // }
  6196. //
  6197. // //查询该药品剩余库存
  6198. // infolist, _ := FindDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId)
  6199. // var over_count int64
  6200. // for _, its := range infolist {
  6201. // if its.MaxUnit == drup.MaxUnit {
  6202. // its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  6203. // }
  6204. // over_count += its.StockMaxNumber + its.StockMinNumber
  6205. // }
  6206. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  6207. // drugflow := models.DrugFlow{
  6208. // WarehouseOutId: warehouseout.ID,
  6209. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  6210. // DrugId: advice.DrugId,
  6211. // Number: warehouse.Number,
  6212. // ProductDate: advice.ProductDate,
  6213. // ExpireDate: advice.ExpiryDate,
  6214. // Count: stock_number,
  6215. // Price: warehouse.RetailPrice,
  6216. // Status: 1,
  6217. // Ctime: time.Now().Unix(),
  6218. // UserOrgId: advice.OrgId,
  6219. // Manufacturer: advice.Manufacturer,
  6220. // Dealer: advice.Dealer,
  6221. // BatchNumber: warehouse.BatchNumber,
  6222. // MaxUnit: drup.MinUnit,
  6223. // ConsumableType: 2,
  6224. // IsEdit: 1,
  6225. // Creator: 0,
  6226. // IsSys: 0,
  6227. // WarehouseOutDetailId: drugWareInfo.ID,
  6228. // AdviceId: advice.ID,
  6229. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6230. // StorehouseId: advice.StorehouseId,
  6231. // AdminUserId: advice.AdminUserId,
  6232. // LastPrice: warehouse.Price,
  6233. // StockCount: advice.StockCount,
  6234. // OverCount: over_count,
  6235. // RetailPrice: retail_price, //手动出库出库价格
  6236. // WarehousingId: warehouse.ID,
  6237. // WarehousingDetailId: warehouse.ID,
  6238. // }
  6239. //
  6240. // CreateDrugFlowOne(drugflow)
  6241. // //更新出库id
  6242. // UpdateDrugWarehouseOutInfo(advice.ID, warehouse.ID, advice.OrgId, over_count)
  6243. // // 出库完成后,要将该批次库存清零
  6244. //
  6245. // prescribingNumber_two_temp := deliver_number - stock_number
  6246. //
  6247. // advice.CountUnit = drup.MinUnit
  6248. //
  6249. // AutoDrugDeliverInfoFourtyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  6250. // }
  6251. //
  6252. // return
  6253. //}
  6254. //
  6255. //func UpdateDrugWarehouseOutInfo(id int64, warehouse_info_id int64, org_id int64, over_count int64) error {
  6256. // ut := XTWriteDB().Begin()
  6257. // 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}).Error
  6258. // if err != nil {
  6259. // ut.Rollback()
  6260. // return err
  6261. // }
  6262. // ut.Commit()
  6263. // return err
  6264. //}
  6265. //
  6266. //func UpdateGoodWarehouseOutById(id int64, warehouse_info_id int64, org_id int64, over_count int64) error {
  6267. // utx := XTWriteDB().Begin()
  6268. // 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
  6269. // if err != nil {
  6270. // utx.Rollback()
  6271. // return err
  6272. // }
  6273. // utx.Commit()
  6274. // return err
  6275. //}
  6276. //
  6277. //func ChangeMaxNumber(id int64, count int64) error {
  6278. //
  6279. // 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
  6280. // return err
  6281. //
  6282. //}
  6283. //
  6284. //func UpdateMinNumber(id int64, count int64) error {
  6285. //
  6286. // err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1 and is_check = 1", id).Update(map[string]interface{}{"stock_min_number": count}).Error
  6287. // return err
  6288. //}
  6289. //
  6290. //func FindLastDrugWarehouseOutInfo(orgid int64) (models.DrugWarehouseOutInfo, error) {
  6291. // info := models.DrugWarehouseOutInfo{}
  6292. // err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&info).Error
  6293. // return info, err
  6294. //}
  6295. //
  6296. //func GetAllGoodSumCount(goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  6297. //
  6298. // err = XTReadDB().Where("good_id = ? and org_id = ? and is_check =1 and status= 1 and stock_count > 0", goodid, orgid).Find(&info).Error
  6299. // return info, err
  6300. //}
  6301. //
  6302. //func CreatedFlushInfo(info models.XtWarehouseFlushInfo) error {
  6303. // ut := writeDb.Begin()
  6304. // err := ut.Create(&info).Error
  6305. // if err != nil {
  6306. // ut.Rollback()
  6307. // return err
  6308. // }
  6309. // ut.Commit()
  6310. // return err
  6311. //
  6312. //}
  6313. //
  6314. //func FindDrugWarehouseInfoList(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6315. //
  6316. // 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
  6317. // return info, err
  6318. //}
  6319. //
  6320. //func CreatedDrugFlushInfo(info models.XtDrugWarehouseFlushInfo) error {
  6321. // ut := writeDb.Begin()
  6322. // err := ut.Create(&info).Error
  6323. // if err != nil {
  6324. // ut.Rollback()
  6325. // return err
  6326. // }
  6327. // ut.Commit()
  6328. // return err
  6329. //}
  6330. //
  6331. //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 {
  6332. //
  6333. // 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
  6334. // return err
  6335. //}
  6336. //
  6337. //func FindLastWarehouseOutInfo(orgId int64) (models.WarehouseOutInfo, error) {
  6338. //
  6339. // info := models.WarehouseOutInfo{}
  6340. // err := XTReadDB().Model(&info).Where("org_id = ? and status = 1", orgId).Find(&info).Error
  6341. // return info, err
  6342. //}
  6343. //
  6344. //func ModifyAddGoodInformation(count int64, id int64) error {
  6345. //
  6346. // ut := XTWriteDB().Begin()
  6347. // err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", count)).Error
  6348. // if err != nil {
  6349. // ut.Rollback()
  6350. // return err
  6351. // }
  6352. // ut.Commit()
  6353. // return err
  6354. //}
  6355. //
  6356. //func ModifyReduceGoodInformation(count int64, id int64) error {
  6357. //
  6358. // ut := XTWriteDB().Begin()
  6359. // err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", count)).Error
  6360. // if err != nil {
  6361. // ut.Rollback()
  6362. // return err
  6363. // }
  6364. // ut.Commit()
  6365. // return err
  6366. //}
  6367. //
  6368. //func ModifyAddCancelCount(count int64, id int64) error {
  6369. //
  6370. // ut := XTWriteDB().Begin()
  6371. // err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", count)).Error
  6372. // if err != nil {
  6373. // ut.Rollback()
  6374. // return err
  6375. // }
  6376. // ut.Commit()
  6377. // return err
  6378. //}
  6379. //
  6380. //func ModifyReduceCancelCount(count int64, id int64) error {
  6381. //
  6382. // ut := XTWriteDB().Begin()
  6383. // err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", count)).Error
  6384. // if err != nil {
  6385. // ut.Rollback()
  6386. // return err
  6387. // }
  6388. // ut.Commit()
  6389. // return err
  6390. //}
  6391. //
  6392. //func ModifyAddDrugInformation(id int64, count int64, orgid int64) error {
  6393. //
  6394. // ut := XTWriteDB().Begin()
  6395. // 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
  6396. // if err != nil {
  6397. // ut.Rollback()
  6398. // return err
  6399. // }
  6400. // ut.Commit()
  6401. // return err
  6402. //}
  6403. //
  6404. //func ModifyReduceDrugInformation(id int64, count int64, orgid int64) error {
  6405. //
  6406. // ut := XTWriteDB().Begin()
  6407. // 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
  6408. // if err != nil {
  6409. // ut.Rollback()
  6410. // return err
  6411. // }
  6412. // ut.Commit()
  6413. // return err
  6414. //}
  6415. //
  6416. //func ModifyAddCancelAddInfomation(id int64, count int64, orgid int64, storehouse_id int64) error {
  6417. // ut := XTWriteDB().Begin()
  6418. // 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
  6419. // if err != nil {
  6420. // ut.Rollback()
  6421. // return err
  6422. // }
  6423. // ut.Commit()
  6424. // return err
  6425. //}
  6426. //
  6427. //func ModifyAddDrugOutReduceInfomation(id int64, count int64, orgid int64, storehouse_id int64) error {
  6428. // ut := XTWriteDB().Begin()
  6429. // 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
  6430. // if err != nil {
  6431. // ut.Rollback()
  6432. // return err
  6433. // }
  6434. // ut.Commit()
  6435. // return err
  6436. //}
  6437. //
  6438. //func ModifyAddDrugOutAddInfomation(id int64, count int64, orgid int64, storehouse_id int64) error {
  6439. // ut := XTWriteDB().Begin()
  6440. // 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
  6441. // if err != nil {
  6442. // ut.Rollback()
  6443. // return err
  6444. // }
  6445. // ut.Commit()
  6446. // return err
  6447. //}
  6448. //
  6449. //func ModifyDrugCancelWarehouseInfo(id int64, count int64, orgid int64, storehouse_id int64) error {
  6450. // ut := XTWriteDB().Begin()
  6451. // 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
  6452. // if err != nil {
  6453. // ut.Rollback()
  6454. // return err
  6455. // }
  6456. // ut.Commit()
  6457. // return err
  6458. //}
  6459. //
  6460. //func ModifyGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
  6461. //
  6462. // ut := XTWriteDB().Begin()
  6463. //
  6464. // 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
  6465. // if err != nil {
  6466. // ut.Rollback()
  6467. // return err
  6468. // }
  6469. //
  6470. // 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
  6471. // if err != nil {
  6472. // ut.Rollback()
  6473. // return err
  6474. // }
  6475. //
  6476. // ut.Commit()
  6477. // return err
  6478. //}
  6479. //
  6480. //func ModifyReduceGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
  6481. //
  6482. // ut := XTWriteDB().Begin()
  6483. //
  6484. // 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
  6485. // if err != nil {
  6486. // ut.Rollback()
  6487. // return err
  6488. // }
  6489. //
  6490. // 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
  6491. // if err != nil {
  6492. // ut.Rollback()
  6493. // return err
  6494. // }
  6495. //
  6496. // ut.Commit()
  6497. // return err
  6498. //}
  6499. //
  6500. //func ModifyAddGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
  6501. // ut := XTWriteDB().Begin()
  6502. //
  6503. // 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
  6504. // if err != nil {
  6505. // ut.Rollback()
  6506. // return err
  6507. // }
  6508. //
  6509. // ut.Commit()
  6510. // return err
  6511. //}
  6512. //
  6513. //func UpdateSumAddCancelCount(user_org_id int64, good_id int64, storehouse_id int64, count int64) error {
  6514. //
  6515. // ut := XTWriteDB().Begin()
  6516. // 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
  6517. // if err != nil {
  6518. // ut.Rollback()
  6519. // return err
  6520. // }
  6521. // ut.Commit()
  6522. // return err
  6523. //}
  6524. //
  6525. //func UpdateSumReduceCancelCount(user_org_id int64, good_id int64, storehouse_id int64, count int64) error {
  6526. //
  6527. // ut := XTWriteDB().Begin()
  6528. // 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
  6529. // if err != nil {
  6530. // ut.Rollback()
  6531. // return err
  6532. // }
  6533. // ut.Commit()
  6534. // return err
  6535. //}
  6536. //
  6537. //func UpdateSumAddOutCount(user_org_id int64, good_id int64, storehouse_id int64, count int64) error {
  6538. //
  6539. // ut := XTWriteDB().Begin()
  6540. // 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
  6541. // if err != nil {
  6542. // ut.Rollback()
  6543. // return err
  6544. // }
  6545. // ut.Commit()
  6546. // return err
  6547. //}
  6548. //
  6549. //func FindDrugWarehouseInfoFlushCount(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6550. //
  6551. // err = XTReadDB().Where("drug_id = ? and org_id = ? and status = 1 and is_check = 1", drug_id, orgid).Find(&info).Error
  6552. // return info, err
  6553. //}
  6554. //
  6555. //func FindDrugWarehouseInfoFlushCountByStorehouse(drug_id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
  6556. //
  6557. // 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
  6558. // return info, err
  6559. //}
  6560. // if(item.PrescribingNumberUnit == drup.MaxUnit && drup.MaxUnit ==drup.MinUnit ){
  6561. // prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  6562. // count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  6563. // total_count += count
  6564. // }
  6565. // info := models.DrugWarehouseOutInfo{
  6566. // Count: total_count,
  6567. // CountUnit: drup.MinUnit,
  6568. // }
  6569. //
  6570. // fmt.Println("total_coutn 233232322323232323233222322wo",total_count)
  6571. // //更新出库单数据
  6572. // UpdateDrugWarehouseOutByPatientId(advice.PatientId,advice.AdviceDate,advice.UserOrgId,info)
  6573. //}
  6574. } else {
  6575. return errors.New("药品信息不存在")
  6576. }
  6577. }
  6578. return
  6579. }
  6580. // 药品出库 递归方式
  6581. func HisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  6582. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  6583. var deliver_number int64 = 0
  6584. var stock_number int64 = 0
  6585. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  6586. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  6587. if advice.PrescribingNumberUnit == drup.MaxUnit {
  6588. deliver_number = count * drup.MinNumber
  6589. } else {
  6590. deliver_number = count
  6591. }
  6592. var min_price float64
  6593. if advice.PrescribingNumberUnit == drup.MaxUnit {
  6594. min_price = drup.RetailPrice
  6595. }
  6596. if advice.PrescribingNumberUnit == drup.MinUnit {
  6597. min_price = drup.MinPrice
  6598. }
  6599. if advice.PrescribingNumberUnit != drup.MaxUnit && advice.PrescribingNumberUnit != drup.MinUnit {
  6600. min_price = drup.RetailPrice
  6601. }
  6602. fmt.Println(min_price)
  6603. // 根据先进先出原则,查询最先入库的批次,进行出库
  6604. // 如果没有对应的库存,则报错
  6605. //查询默认仓库
  6606. storeConfig, _ := GetAllStoreHouseConfig(orgID)
  6607. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  6608. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  6609. var stockMax int64
  6610. var stockMin int64
  6611. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  6612. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  6613. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  6614. UpdateMinNumber(lastWarehouse.ID, stockMin)
  6615. }
  6616. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  6617. if err != nil {
  6618. return err
  6619. }
  6620. // 将该批次的剩余库存数量转换为拆零数量
  6621. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  6622. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6623. if stock_number >= deliver_number {
  6624. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  6625. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  6626. WarehouseOutId: warehouseout.ID,
  6627. Status: 1,
  6628. Ctime: time.Now().Unix(),
  6629. Remark: warehouse.Remark,
  6630. OrgId: orgID,
  6631. Type: 1,
  6632. Manufacturer: warehouse.Manufacturer,
  6633. Dealer: warehouse.Dealer,
  6634. IsSys: 1,
  6635. SysRecordTime: advice.RecordDate,
  6636. DrugId: advice.DrugId,
  6637. Number: warehouse.Number,
  6638. BatchNumber: warehouse.BatchNumber,
  6639. Price: warehouse.RetailPrice,
  6640. CountUnit: drup.MinUnit,
  6641. RetailPrice: warehouse.RetailPrice,
  6642. ProductDate: warehouse.ProductDate,
  6643. ExpiryDate: warehouse.ExpiryDate,
  6644. PatientId: advice.PatientId,
  6645. WarehouseInfoId: warehouse.ID,
  6646. Count: deliver_number,
  6647. AdviceId: advice.ID,
  6648. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6649. StorehouseId: storeConfig.DrugStorehouseOut,
  6650. IsCheck: 1,
  6651. LastPrice: warehouse.Price,
  6652. }
  6653. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  6654. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  6655. if errOne != nil {
  6656. return errOne
  6657. }
  6658. drugflow := models.DrugFlow{
  6659. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  6660. WarehouseOutId: warehouseout.ID,
  6661. DrugId: advice.DrugId,
  6662. Number: warehouse.Number,
  6663. ProductDate: warehouse.ProductDate,
  6664. ExpireDate: warehouse.ExpiryDate,
  6665. Count: deliver_number,
  6666. Price: warehouse.RetailPrice,
  6667. Status: 1,
  6668. Ctime: time.Now().Unix(),
  6669. UserOrgId: orgID,
  6670. Manufacturer: warehouse.Manufacturer,
  6671. Dealer: warehouse.Dealer,
  6672. BatchNumber: warehouse.BatchNumber,
  6673. MaxUnit: drup.MinUnit,
  6674. ConsumableType: 3,
  6675. IsEdit: 1,
  6676. Creator: advice.ExecutionStaff,
  6677. IsSys: 1,
  6678. PatientId: advice.PatientId,
  6679. SystemTime: advice.AdviceDate,
  6680. WarehousingDetailId: warehouse.ID,
  6681. AdviceId: advice.ID,
  6682. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6683. StorehouseId: storeConfig.DrugStorehouseOut,
  6684. WarehouseOutDetailId: lastDrugOutInfo.ID,
  6685. LastPrice: warehouse.Price,
  6686. }
  6687. CreateDrugFlowOne(drugflow)
  6688. details := &models.DrugAutomaticReduceDetail{
  6689. WarehouseOutId: warehouseout.ID,
  6690. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  6691. PatientId: advice.PatientId,
  6692. Ctime: time.Now().Unix(),
  6693. Mtime: time.Now().Unix(),
  6694. Status: 1,
  6695. RecordTime: advice.RecordDate,
  6696. OrgId: orgID,
  6697. DrugId: advice.DrugId,
  6698. Count: deliver_number,
  6699. CountUnit: drup.MinUnit,
  6700. WarehouseInfoId: warehouse.ID,
  6701. AdviceId: advice.ID,
  6702. StorehouseId: storeConfig.DrugStorehouseOut,
  6703. }
  6704. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  6705. if errTwo != nil {
  6706. return errTwo
  6707. }
  6708. // 出库完成后,要减去对应批次的库存数量
  6709. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  6710. var maxNumber int64 = 0
  6711. var minNumber int64 = 0
  6712. maxNumber = deliver_number / drup.MinNumber
  6713. minNumber = deliver_number % drup.MinNumber
  6714. fmt.Println("maxNumber2323233232323232323232", maxNumber)
  6715. fmt.Println("minNumber2323233232323232323232", minNumber)
  6716. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  6717. minNumber = maxNumber
  6718. }
  6719. if drup.MaxUnit != drup.MinUnit {
  6720. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  6721. return errors.New("库存数量不足")
  6722. }
  6723. }
  6724. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  6725. if warehouse.StockMaxNumber < 0 {
  6726. warehouse.StockMaxNumber = 0
  6727. }
  6728. if warehouse.StockMinNumber < 0 {
  6729. warehouse.StockMinNumber = 0
  6730. }
  6731. warehouse.Mtime = time.Now().Unix()
  6732. fmt.Println("warehouse.StockMinNumber", warehouse.StockMinNumber)
  6733. fmt.Println("minNumber", minNumber)
  6734. fmt.Println("maxNumber", maxNumber)
  6735. if warehouse.StockMinNumber < minNumber {
  6736. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  6737. if warehouse.MaxUnit != warehouse.MinUnit {
  6738. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  6739. }
  6740. } else {
  6741. if minNumber > 0 {
  6742. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  6743. }
  6744. }
  6745. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  6746. if (warehouse.StockMinNumber - deliver_number) > 0 {
  6747. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6748. }
  6749. }
  6750. if drup.MaxUnit != drup.MinUnit {
  6751. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  6752. return errors.New("库存数量不足")
  6753. }
  6754. }
  6755. if warehouse.StockMinNumber <= 0 {
  6756. warehouse.StockMinNumber = 0
  6757. }
  6758. //扣减库存
  6759. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6760. if errThree != nil {
  6761. return errThree
  6762. }
  6763. drugInfoList, _ := FindDrugWarehouseInfoList(advice.DrugId, orgID)
  6764. var drug_max_number int64
  6765. var drug_min_number int64
  6766. for _, item := range drugInfoList {
  6767. drug_max_number += item.StockMaxNumber
  6768. drug_min_number += item.StockMinNumber
  6769. }
  6770. drugFlushInfo := models.XtDrugWarehouseFlushInfo{
  6771. DrugMaxNumber: drug_max_number,
  6772. UserOrgId: orgID,
  6773. WarehouseOutId: warehouseout.ID,
  6774. Type: 1,
  6775. SystemTime: advice.RecordDate,
  6776. PatientId: advice.PatientId,
  6777. Ctime: time.Now().Unix(),
  6778. Mtime: 0,
  6779. BatchNumberId: warehouse.ID,
  6780. DrugId: advice.DrugId,
  6781. Count: deliver_number,
  6782. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  6783. Creater: warehouseout.Creater,
  6784. DrugMinNumber: drug_min_number,
  6785. Unit: drup.MinUnit,
  6786. AdviceId: advice.ID,
  6787. Status: 1,
  6788. WarehouseOutDetailId: lastDrugOutInfo.ID,
  6789. }
  6790. CreatedDrugFlushInfo(drugFlushInfo)
  6791. return nil
  6792. } else {
  6793. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  6794. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  6795. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  6796. WarehouseOutId: warehouseout.ID,
  6797. Status: 1,
  6798. Ctime: time.Now().Unix(),
  6799. Remark: warehouse.Remark,
  6800. OrgId: orgID,
  6801. Type: 1,
  6802. Manufacturer: warehouse.Manufacturer,
  6803. Dealer: warehouse.Dealer,
  6804. IsSys: 1,
  6805. SysRecordTime: advice.RecordDate,
  6806. DrugId: advice.DrugId,
  6807. Number: warehouse.Number,
  6808. BatchNumber: warehouse.BatchNumber,
  6809. Price: warehouse.RetailPrice,
  6810. RetailPrice: warehouse.RetailPrice,
  6811. ProductDate: warehouse.ProductDate,
  6812. ExpiryDate: warehouse.ExpiryDate,
  6813. PatientId: advice.PatientId,
  6814. WarehouseInfoId: warehouse.ID,
  6815. Count: stock_number,
  6816. CountUnit: drup.MinUnit,
  6817. AdviceId: advice.ID,
  6818. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6819. StorehouseId: storeConfig.DrugStorehouseOut,
  6820. IsCheck: 1,
  6821. }
  6822. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  6823. if errOne != nil {
  6824. return errOne
  6825. }
  6826. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  6827. drugflow := models.DrugFlow{
  6828. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  6829. WarehouseOutId: warehouseout.ID,
  6830. DrugId: advice.DrugId,
  6831. Number: warehouse.Number,
  6832. ProductDate: warehouse.ProductDate,
  6833. ExpireDate: warehouse.ExpiryDate,
  6834. Count: stock_number,
  6835. Price: warehouse.RetailPrice,
  6836. Status: 1,
  6837. Ctime: time.Now().Unix(),
  6838. UserOrgId: orgID,
  6839. Manufacturer: warehouse.Manufacturer,
  6840. Dealer: warehouse.Dealer,
  6841. BatchNumber: warehouse.BatchNumber,
  6842. MaxUnit: drup.MinUnit,
  6843. ConsumableType: 3,
  6844. IsEdit: 1,
  6845. Creator: advice.ExecutionStaff,
  6846. IsSys: 1,
  6847. PatientId: advice.PatientId,
  6848. SystemTime: advice.AdviceDate,
  6849. WarehousingDetailId: warehouse.ID,
  6850. AdviceId: advice.ID,
  6851. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6852. StorehouseId: storeConfig.DrugStorehouseOut,
  6853. WarehouseOutDetailId: lastDrugOutInfo.ID,
  6854. LastPrice: warehouse.Price,
  6855. }
  6856. CreateDrugFlowOne(drugflow)
  6857. //查询是否存在数据
  6858. details := &models.DrugAutomaticReduceDetail{
  6859. WarehouseOutId: warehouseout.ID,
  6860. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  6861. PatientId: advice.PatientId,
  6862. Ctime: time.Now().Unix(),
  6863. Mtime: time.Now().Unix(),
  6864. Status: 1,
  6865. RecordTime: advice.RecordDate,
  6866. OrgId: orgID,
  6867. DrugId: advice.DrugId,
  6868. Count: stock_number,
  6869. CountUnit: drup.MinUnit,
  6870. WarehouseInfoId: warehouse.ID,
  6871. AdviceId: advice.ID,
  6872. StorehouseId: storeConfig.DrugStorehouseOut,
  6873. }
  6874. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  6875. if errTwo != nil {
  6876. return errTwo
  6877. }
  6878. info := models.XtDrugWarehouseInfo{
  6879. ID: warehouse.ID,
  6880. WarehousingId: warehouse.WarehousingId,
  6881. DrugId: warehouse.DrugId,
  6882. Number: warehouse.Number,
  6883. ProductDate: warehouse.ProductDate,
  6884. ExpiryDate: warehouse.ExpiryDate,
  6885. WarehousingCount: warehouse.WarehousingCount,
  6886. Price: warehouse.Price,
  6887. TotalPrice: warehouse.TotalPrice,
  6888. Dealer: warehouse.Dealer,
  6889. Manufacturer: warehouse.Manufacturer,
  6890. Remark: warehouse.Remark,
  6891. Ctime: warehouse.Ctime,
  6892. Mtime: warehouse.Mtime,
  6893. Status: 1,
  6894. OrgId: warehouse.OrgId,
  6895. IsReturn: warehouse.IsReturn,
  6896. WarehousingOrder: warehouse.WarehousingOrder,
  6897. Type: warehouse.Type,
  6898. RetailPrice: warehouse.RetailPrice,
  6899. RetailTotalPrice: warehouse.RetailPrice,
  6900. StockMaxNumber: 0,
  6901. StockMinNumber: 0,
  6902. BatchNumber: warehouse.BatchNumber,
  6903. MaxUnit: warehouse.MaxUnit,
  6904. MinUnit: warehouse.MinUnit,
  6905. WarehousingInfoId: warehouse.WarehousingInfoId,
  6906. StorehouseId: storeConfig.DrugStorehouseOut,
  6907. IsCheck: 1,
  6908. }
  6909. errThree := UpDateDrugWarehouseInfoByStock(&info)
  6910. if errThree != nil {
  6911. return errThree
  6912. }
  6913. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  6914. prescribingNumber_two_temp := deliver_number - stock_number
  6915. overPlusNumber := float64(prescribingNumber_two_temp)
  6916. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  6917. advice.PrescribingNumberUnit = drup.MinUnit
  6918. HisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  6919. }
  6920. return
  6921. }
  6922. // 药品出库 递归方式
  6923. func AutoDrugDeliverInfo(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  6924. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  6925. var deliver_number int64 = 0
  6926. var stock_number int64 = 0
  6927. if advice.CountUnit == drup.MaxUnit {
  6928. deliver_number = prescribingNumber * drup.MinNumber
  6929. } else {
  6930. deliver_number = prescribingNumber
  6931. }
  6932. // 根据先进先出原则,查询最先入库的批次,进行出库
  6933. // 如果没有对应的库存,则报错
  6934. //开启事物
  6935. storeConfig, _ := GetAllStoreHouseConfig(orgID)
  6936. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  6937. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  6938. var stockMax int64
  6939. var stockMin int64
  6940. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  6941. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  6942. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  6943. UpdateMinNumber(lastWarehouse.ID, stockMin)
  6944. }
  6945. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  6946. if err != nil {
  6947. return err
  6948. }
  6949. // 将该批次的剩余库存数量转换为拆零数量
  6950. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  6951. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6952. if stock_number >= deliver_number {
  6953. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  6954. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  6955. WarehouseOutId: warehouseout.ID,
  6956. Status: 1,
  6957. Ctime: time.Now().Unix(),
  6958. Remark: advice.Remark,
  6959. OrgId: orgID,
  6960. Type: 1,
  6961. Manufacturer: advice.Manufacturer,
  6962. Dealer: warehouse.Dealer,
  6963. IsSys: 0,
  6964. SysRecordTime: advice.Ctime,
  6965. DrugId: advice.DrugId,
  6966. ExpiryDate: advice.ExpiryDate,
  6967. ProductDate: advice.ProductDate,
  6968. Number: advice.Number,
  6969. BatchNumber: warehouse.BatchNumber,
  6970. Count: deliver_number,
  6971. RetailPrice: advice.RetailPrice,
  6972. Price: warehouse.RetailPrice,
  6973. WarehouseInfoId: warehouse.ID,
  6974. CountUnit: drup.MinUnit,
  6975. AdviceId: advice.ID,
  6976. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6977. StorehouseId: advice.StorehouseId,
  6978. AdminUserId: advice.AdminUserId,
  6979. IsCheck: 1,
  6980. }
  6981. warehouseOutInfo.Count = prescribingNumber
  6982. warehouseOutInfo.CountUnit = advice.CountUnit
  6983. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  6984. if errOne != nil {
  6985. return errOne
  6986. }
  6987. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  6988. drugflow := models.DrugFlow{
  6989. WarehouseOutId: warehouseout.ID,
  6990. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  6991. DrugId: advice.DrugId,
  6992. Number: warehouse.Number,
  6993. ProductDate: advice.ProductDate,
  6994. ExpireDate: advice.ExpiryDate,
  6995. Count: deliver_number,
  6996. Price: warehouse.RetailPrice,
  6997. Status: 1,
  6998. Ctime: time.Now().Unix(),
  6999. UserOrgId: advice.OrgId,
  7000. Manufacturer: advice.Manufacturer,
  7001. Dealer: advice.Dealer,
  7002. BatchNumber: warehouse.BatchNumber,
  7003. MaxUnit: drup.MinUnit,
  7004. ConsumableType: 2,
  7005. IsEdit: 1,
  7006. Creator: 0,
  7007. IsSys: 0,
  7008. WarehouseOutDetailId: drugWareInfo.ID,
  7009. AdviceId: advice.ID,
  7010. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7011. StorehouseId: advice.StorehouseId,
  7012. AdminUserId: advice.AdminUserId,
  7013. }
  7014. CreateDrugFlowOne(drugflow)
  7015. // 出库完成后,要减去对应批次的库存数量
  7016. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  7017. var maxNumber int64 = 0
  7018. var minNumber int64 = 0
  7019. if advice.CountUnit == drup.MinUnit {
  7020. maxNumber = prescribingNumber / drup.MinNumber
  7021. minNumber = prescribingNumber % drup.MinNumber
  7022. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  7023. minNumber = maxNumber
  7024. }
  7025. } else {
  7026. maxNumber = prescribingNumber
  7027. }
  7028. if warehouse.StockMaxNumber < maxNumber {
  7029. return errors.New("库存数量不足")
  7030. }
  7031. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  7032. warehouse.Mtime = time.Now().Unix()
  7033. if warehouse.StockMinNumber < minNumber {
  7034. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  7035. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  7036. } else {
  7037. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  7038. }
  7039. if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
  7040. warehouse.StockMinNumber = 0
  7041. }
  7042. if warehouse.StockMaxNumber < 0 {
  7043. return errors.New("库存数量不足")
  7044. }
  7045. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  7046. if errThree != nil {
  7047. return errThree
  7048. }
  7049. //查询默认仓库
  7050. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  7051. //查询默认仓库剩余多少库存
  7052. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  7053. var sum_count int64
  7054. var sum_in_count int64
  7055. for _, it := range list {
  7056. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  7057. if it.MaxUnit == baseDrug.MaxUnit {
  7058. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  7059. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  7060. }
  7061. sum_count += it.StockMaxNumber + it.StockMinNumber
  7062. sum_in_count += it.WarehousingCount
  7063. }
  7064. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  7065. return nil
  7066. } else {
  7067. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  7068. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  7069. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  7070. WarehouseOutId: warehouseout.ID,
  7071. Status: 1,
  7072. Ctime: time.Now().Unix(),
  7073. Remark: advice.Remark,
  7074. OrgId: orgID,
  7075. Type: 1,
  7076. Manufacturer: advice.Manufacturer,
  7077. Dealer: warehouse.Dealer,
  7078. IsSys: 0,
  7079. SysRecordTime: advice.Ctime,
  7080. DrugId: advice.DrugId,
  7081. ExpiryDate: warehouse.ExpiryDate,
  7082. ProductDate: warehouse.ProductDate,
  7083. Number: warehouse.Number,
  7084. BatchNumber: warehouse.BatchNumber,
  7085. Count: stock_number,
  7086. RetailPrice: advice.RetailPrice,
  7087. Price: warehouse.RetailPrice,
  7088. WarehouseInfoId: warehouse.ID,
  7089. CountUnit: drup.MinUnit,
  7090. AdviceId: advice.ID,
  7091. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7092. StorehouseId: advice.StorehouseId,
  7093. AdminUserId: advice.AdminUserId,
  7094. IsCheck: 1,
  7095. }
  7096. warehouseOutInfo.Count = stock_number
  7097. //warehouseOutInfo.CountUnit = advice.CountUnit
  7098. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  7099. if errOne != nil {
  7100. return errOne
  7101. }
  7102. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  7103. drugflow := models.DrugFlow{
  7104. WarehouseOutId: warehouseout.ID,
  7105. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  7106. DrugId: advice.DrugId,
  7107. Number: warehouse.Number,
  7108. ProductDate: advice.ProductDate,
  7109. ExpireDate: advice.ExpiryDate,
  7110. Count: stock_number,
  7111. Price: warehouse.RetailPrice,
  7112. Status: 1,
  7113. Ctime: time.Now().Unix(),
  7114. UserOrgId: advice.OrgId,
  7115. Manufacturer: advice.Manufacturer,
  7116. Dealer: advice.Dealer,
  7117. BatchNumber: warehouse.BatchNumber,
  7118. MaxUnit: drup.MinUnit,
  7119. ConsumableType: 2,
  7120. IsEdit: 1,
  7121. Creator: 0,
  7122. IsSys: 0,
  7123. WarehouseOutDetailId: drugWareInfo.ID,
  7124. AdviceId: advice.ID,
  7125. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7126. StorehouseId: advice.StorehouseId,
  7127. AdminUserId: advice.AdminUserId,
  7128. }
  7129. CreateDrugFlowOne(drugflow)
  7130. // 出库完成后,要将该批次库存清零
  7131. //扣减库存
  7132. warehouse.StockMaxNumber = 0
  7133. warehouse.StockMinNumber = 0
  7134. warehouse.Mtime = time.Now().Unix()
  7135. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  7136. //查询默认仓库
  7137. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  7138. //查询默认仓库剩余多少库存
  7139. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  7140. var sum_count int64
  7141. var sum_in_count int64
  7142. for _, it := range list {
  7143. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  7144. if it.MaxUnit == baseDrug.MaxUnit {
  7145. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  7146. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  7147. sum_count += it.StockMaxNumber + it.StockMinNumber
  7148. sum_in_count += it.WarehousingCount
  7149. }
  7150. }
  7151. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  7152. if errThree != nil {
  7153. return errThree
  7154. }
  7155. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  7156. prescribingNumber_two_temp := deliver_number - stock_number
  7157. advice.CountUnit = drup.MinUnit
  7158. AutoDrugDeliverInfo(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  7159. }
  7160. return
  7161. }
  7162. // 耗材出库
  7163. func ConsumablesDeliveryTotal(orgID int64, patient_id int64, record_time int64, goods []*models.DialysisBeforePrepareGoods, goodOne []*models.NewDialysisBeforePrepareGoods, creator int64) (err error) {
  7164. //查询该患者当天已经出库的耗材信息
  7165. goods_yc, _ := FindConsumablesByDateTwo(orgID, patient_id, record_time)
  7166. // 和新请求的出库数据进行对比,分出那些是继续出库的,那些是需要删除出库的
  7167. for i := len(goods_yc) - 1; i >= 0; i-- {
  7168. goods_yc_temp := goods_yc[i]
  7169. for j := len(goods) - 1; j >= 0; j-- {
  7170. goods_temp := goods[j]
  7171. // 已经出库和新请求出库都存在该耗材,则判断出库数量,分成是继续出库,还是删除出库
  7172. if goods_yc_temp.GoodTypeId == goods_temp.GoodTypeId && goods_yc_temp.GoodId == goods_temp.GoodId {
  7173. // 已经出库和新请求出库的出库数量一致,则清除两个结构体里的数据(既不出库,也不删除出库)
  7174. if goods_yc_temp.Count == goods_temp.Count {
  7175. goods_yc = append(goods_yc[:i], goods_yc[i+1:]...)
  7176. goods = append(goods[:j], goods[j+1:]...)
  7177. break
  7178. }
  7179. // 如果已经出库的数量 大于 新请求出库的数量,则代表需要删除出库
  7180. if goods_yc_temp.Count > goods_temp.Count {
  7181. temp_count := goods_yc_temp.Count - goods_temp.Count
  7182. goods_yc[i].Count = temp_count
  7183. goods = append(goods[:j], goods[j+1:]...)
  7184. break
  7185. }
  7186. // 如果已经出库的数量 小于 新请求出库的梳理,则代表需要增加出库
  7187. if goods_yc_temp.Count < goods_temp.Count {
  7188. temp_count := goods_temp.Count - goods_yc_temp.Count
  7189. goods[j].Count = temp_count
  7190. goods_yc = append(goods_yc[:i], goods_yc[i+1:]...)
  7191. break
  7192. }
  7193. }
  7194. }
  7195. }
  7196. // goods_yc 这个数据就是需要已经出库了,但是现在需要删除出库的耗材数据
  7197. // goods 这个数据就是需要出库的耗材的数据(新增的数据)
  7198. if len(goods) > 0 {
  7199. out, err := FindStockOutByIsSys(orgID, 1, record_time)
  7200. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  7201. if err == gorm.ErrRecordNotFound {
  7202. //没有记录,则创建出库单
  7203. timeStr := time.Now().Format("2006-01-02")
  7204. timeArr := strings.Split(timeStr, "-")
  7205. total, _ := FindAllWarehouseOut(orgID)
  7206. total = total + 1
  7207. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  7208. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  7209. number = number + total
  7210. warehousing_out_order = "CKD" + strconv.FormatInt(number, 10)
  7211. warehouseOut := models.WarehouseOut{
  7212. WarehouseOutOrderNumber: warehousing_out_order,
  7213. OperationTime: time.Now().Unix(),
  7214. OrgId: orgID,
  7215. Creater: creator,
  7216. Ctime: time.Now().Unix(),
  7217. Status: 1,
  7218. WarehouseOutTime: record_time,
  7219. Dealer: 0,
  7220. Manufacturer: 0,
  7221. Type: 1,
  7222. IsSys: 1,
  7223. StorehouseId: houseConfig.StorehouseOutInfo,
  7224. IsCheck: 1,
  7225. }
  7226. err := AddSigleWarehouseOut(&warehouseOut)
  7227. if err != nil {
  7228. utils.TraceLog("创建出库单失败 err = %v", err)
  7229. return err
  7230. } else {
  7231. out = warehouseOut
  7232. }
  7233. }
  7234. for _, item := range goods {
  7235. var newCount int64 = 0
  7236. for _, it := range goodOne {
  7237. if item.GoodTypeId == it.GoodTypeId && item.GoodId == it.GoodId {
  7238. newCount = it.Count
  7239. }
  7240. }
  7241. var cha_count int64
  7242. var cha_count_two int64
  7243. flowGood, _ := GetStockFlowIsBatchNumberTwo(patient_id, record_time, item.GoodId)
  7244. var out_count int64
  7245. for _, item := range flowGood {
  7246. out_count += item.Count
  7247. }
  7248. cha_count = item.Count - out_count
  7249. //fmt.Println("item.Count", item.Count)
  7250. //fmt.Println("item.Count", out_count)
  7251. //fmt.Println("cha_count2323223232323223", cha_count)
  7252. cha_count_two = out_count - item.Count
  7253. prepare := models.DialysisBeforePrepare{
  7254. GoodTypeId: item.GoodTypeId,
  7255. GoodId: item.GoodId,
  7256. Count: cha_count,
  7257. ProjectId: item.ProjectId,
  7258. StorehouseId: houseConfig.StorehouseOutInfo,
  7259. }
  7260. ConsumablesGoodDelivery(orgID, patient_id, record_time, &prepare, &out, newCount)
  7261. //数量增加
  7262. if cha_count > 0 {
  7263. //出库数量累加
  7264. ModifyGoodSumCount(houseConfig.StorehouseOutInfo, cha_count, orgID, item.GoodId)
  7265. }
  7266. //数量减少
  7267. if cha_count_two > 0 {
  7268. ModifyAddGoodSumCount(houseConfig.StorehouseOutInfo, cha_count_two, orgID, item.GoodId)
  7269. //退库数量增加
  7270. UpdateSumAddCancelCount(orgID, item.GoodId, houseConfig.StorehouseOutInfo, cha_count_two)
  7271. }
  7272. //更新剩余库存
  7273. goodListSix, _ := GetSumGoodList(orgID, houseConfig.StorehouseOutInfo, item.GoodId)
  7274. var flush_count int64
  7275. for _, it := range goodListSix {
  7276. flush_count += it.StockCount
  7277. }
  7278. UpdateSumGood(orgID, houseConfig.StorehouseOutInfo, item.GoodId, flush_count)
  7279. }
  7280. }
  7281. fmt.Println("goods_yc2323323223323223232323232332wode", goods_yc)
  7282. if len(goods_yc) > 0 {
  7283. for _, good_yc := range goods_yc {
  7284. out, _ := FindStockOutByIsSys(orgID, 1, record_time)
  7285. ConsumablesDeliveryDelete(orgID, patient_id, record_time, good_yc, &out)
  7286. }
  7287. }
  7288. return nil
  7289. }
  7290. //耗材出库
  7291. func ConsumablesDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, count int64) (err error) {
  7292. //开事务
  7293. var deliver_number int64 = 0
  7294. var stock_number int64 = 0
  7295. var maxNumber int64 = 0
  7296. deliver_number = goods.Count
  7297. // 根据先进先出原则,查询最先入库的批次,进行出库
  7298. // 如果没有对应的库存,则报错
  7299. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  7300. if err != nil {
  7301. return errors.New("库存数量不足")
  7302. }
  7303. stock_number = warehouse.StockCount
  7304. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  7305. if stock_number >= deliver_number {
  7306. maxNumber = goods.Count
  7307. //出库
  7308. warehouse.StockCount = warehouse.StockCount - maxNumber
  7309. warehouse.Mtime = time.Now().Unix()
  7310. if warehouse.StockCount < 0 {
  7311. return errors.New("库存数量不足")
  7312. }
  7313. errThree := UpDateWarehouseInfoByStock(&warehouse)
  7314. if errThree != nil {
  7315. return errThree
  7316. }
  7317. //查询剩余库存
  7318. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID)
  7319. var sum_count int64
  7320. for _, item := range goodList {
  7321. sum_count += item.StockCount
  7322. }
  7323. warehouseOutInfo := &models.WarehouseOutInfo{
  7324. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7325. WarehouseOutId: warehouseOut.ID,
  7326. WarehouseInfotId: warehouse.ID,
  7327. Status: 1,
  7328. Ctime: time.Now().Unix(),
  7329. Remark: warehouse.Remark,
  7330. OrgId: orgID,
  7331. Type: 1,
  7332. Manufacturer: warehouse.Manufacturer,
  7333. Dealer: warehouse.Dealer,
  7334. IsSys: 1,
  7335. SysRecordTime: record_time,
  7336. GoodTypeId: goods.GoodTypeId,
  7337. GoodId: goods.GoodId,
  7338. PatientId: patient_id,
  7339. Number: warehouse.Number,
  7340. LicenseNumber: warehouse.LicenseNumber,
  7341. Price: warehouse.PackingPrice,
  7342. ExpiryDate: warehouse.ExpiryDate,
  7343. ProductDate: warehouse.ProductDate,
  7344. ProjectId: goods.ProjectId,
  7345. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7346. StorehouseId: goods.StorehouseId,
  7347. IsCheck: 1,
  7348. }
  7349. warehouseOutInfo.Count = count
  7350. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
  7351. if errcodes == gorm.ErrRecordNotFound {
  7352. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  7353. if errOne != nil {
  7354. return errOne
  7355. }
  7356. } else if errcodes == nil {
  7357. UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId)
  7358. }
  7359. details := models.BloodAutomaticReduceDetail{
  7360. WarehouseOutId: warehouseOutInfo.ID,
  7361. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  7362. PatientId: patient_id,
  7363. Ctime: time.Now().Unix(),
  7364. Mtime: time.Now().Unix(),
  7365. Status: 1,
  7366. RecordTime: record_time,
  7367. OrgId: orgID,
  7368. GoodId: goods.GoodId,
  7369. GoodTypeId: goods.GoodTypeId,
  7370. Count: count,
  7371. ProjectId: goods.ProjectId,
  7372. StorehouseId: goods.StorehouseId,
  7373. }
  7374. //查询当天耗材是否已经存在数据
  7375. _, errcode := GetAutoMaticReduceDetail(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  7376. if errcode == gorm.ErrRecordNotFound {
  7377. errTwo := CreateAutoReduceRecord(&details)
  7378. if errTwo != nil {
  7379. return errTwo
  7380. }
  7381. //插入库存流水表
  7382. flow := models.VmStockFlow{
  7383. WarehouseOutId: warehouseOut.ID,
  7384. WarehousingId: warehouse.ID,
  7385. GoodId: goods.GoodId,
  7386. Number: warehouse.Number,
  7387. ProductDate: warehouse.ProductDate,
  7388. ExpireDate: warehouse.ExpiryDate,
  7389. Count: deliver_number,
  7390. Price: warehouse.PackingPrice,
  7391. Status: 1,
  7392. Ctime: time.Now().Unix(),
  7393. UserOrgId: orgID,
  7394. Manufacturer: warehouse.Manufacturer,
  7395. Dealer: warehouse.Dealer,
  7396. LicenseNumber: warehouse.LicenseNumber,
  7397. IsEdit: 2,
  7398. Creator: warehouseOut.Creater,
  7399. SystemTime: record_time,
  7400. ConsumableType: 3,
  7401. WarehouseOutDetailId: warehouseOutInfo.ID,
  7402. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7403. IsSys: 1,
  7404. PatientId: patient_id,
  7405. ProjectId: goods.ProjectId,
  7406. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7407. StorehouseId: goods.StorehouseId,
  7408. OverCount: sum_count,
  7409. }
  7410. //查询流水是否存在
  7411. exsit, errflow := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  7412. if errflow == gorm.ErrRecordNotFound {
  7413. //创建数据
  7414. errThre := CreateStockFlowOne(flow)
  7415. if errThre != nil {
  7416. return errThre
  7417. }
  7418. } else if errflow == nil {
  7419. flow := models.VmStockFlow{
  7420. ID: exsit.ID,
  7421. WarehouseOutId: warehouseOut.ID,
  7422. WarehousingId: warehouse.ID,
  7423. GoodId: goods.GoodId,
  7424. Number: warehouse.Number,
  7425. ProductDate: warehouse.ProductDate,
  7426. ExpireDate: warehouse.ExpiryDate,
  7427. Count: exsit.Count + goods.Count,
  7428. Price: warehouse.PackingPrice,
  7429. Status: 1,
  7430. Ctime: time.Now().Unix(),
  7431. UserOrgId: orgID,
  7432. Manufacturer: warehouse.Manufacturer,
  7433. Dealer: warehouse.Dealer,
  7434. LicenseNumber: warehouse.LicenseNumber,
  7435. IsEdit: 2,
  7436. Creator: warehouseOut.Creater,
  7437. SystemTime: record_time,
  7438. ConsumableType: 3,
  7439. WarehouseOutDetailId: warehouseOutInfo.ID,
  7440. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7441. IsSys: 1,
  7442. PatientId: patient_id,
  7443. ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  7444. ProjectId: goods.ProjectId,
  7445. StorehouseId: goods.StorehouseId,
  7446. OverCount: sum_count,
  7447. }
  7448. errFour := UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  7449. if errFour != nil {
  7450. return errFour
  7451. }
  7452. }
  7453. } else if errcode == nil {
  7454. flow := models.VmStockFlow{
  7455. WarehouseOutId: warehouseOut.ID,
  7456. WarehousingId: warehouse.ID,
  7457. GoodId: goods.GoodId,
  7458. Number: warehouse.Number,
  7459. ProductDate: warehouse.ProductDate,
  7460. ExpireDate: warehouse.ExpiryDate,
  7461. Count: deliver_number,
  7462. Price: warehouse.PackingPrice,
  7463. Status: 1,
  7464. Ctime: time.Now().Unix(),
  7465. UserOrgId: orgID,
  7466. Manufacturer: warehouse.Manufacturer,
  7467. Dealer: warehouse.Dealer,
  7468. LicenseNumber: warehouse.LicenseNumber,
  7469. IsEdit: 2,
  7470. Creator: warehouseOut.Creater,
  7471. SystemTime: record_time,
  7472. ConsumableType: 3,
  7473. WarehouseOutDetailId: warehouseOutInfo.ID,
  7474. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7475. IsSys: 1,
  7476. PatientId: patient_id,
  7477. ProjectId: goods.ProjectId,
  7478. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7479. StorehouseId: goods.StorehouseId,
  7480. OverCount: sum_count,
  7481. }
  7482. //查询流水是否存在
  7483. exsit, errflow := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  7484. if errflow == gorm.ErrRecordNotFound {
  7485. CreateStockFlowOne(flow)
  7486. DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  7487. CreateAutoReduceRecord(&details)
  7488. } else if errflow == nil {
  7489. flow := models.VmStockFlow{
  7490. ID: exsit.ID,
  7491. WarehouseOutId: warehouseOut.ID,
  7492. WarehousingId: warehouse.ID,
  7493. GoodId: goods.GoodId,
  7494. Number: warehouse.Number,
  7495. ProductDate: warehouse.ProductDate,
  7496. ExpireDate: warehouse.ExpiryDate,
  7497. Count: exsit.Count + goods.Count,
  7498. Price: warehouse.PackingPrice,
  7499. Status: 1,
  7500. Ctime: time.Now().Unix(),
  7501. UserOrgId: orgID,
  7502. Manufacturer: warehouse.Manufacturer,
  7503. Dealer: warehouse.Dealer,
  7504. LicenseNumber: warehouse.LicenseNumber,
  7505. IsEdit: 2,
  7506. Creator: warehouseOut.Creater,
  7507. SystemTime: record_time,
  7508. ConsumableType: 3,
  7509. WarehouseOutDetailId: warehouseOutInfo.ID,
  7510. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7511. IsSys: 1,
  7512. PatientId: patient_id,
  7513. ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  7514. ProjectId: goods.ProjectId,
  7515. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7516. StorehouseId: goods.StorehouseId,
  7517. OverCount: sum_count,
  7518. }
  7519. UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  7520. DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  7521. CreateAutoReduceRecord(&details)
  7522. }
  7523. }
  7524. return nil
  7525. } else {
  7526. // 当刚批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  7527. warehouse.StockCount = 0
  7528. warehouse.Mtime = time.Now().Unix()
  7529. errThree := UpDateWarehouseInfoByStock(&warehouse)
  7530. if errThree != nil {
  7531. return errThree
  7532. }
  7533. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID)
  7534. var sum_count int64
  7535. for _, item := range goodList {
  7536. sum_count += item.StockCount
  7537. }
  7538. warehouseOutInfo := &models.WarehouseOutInfo{
  7539. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7540. WarehouseOutId: warehouseOut.ID,
  7541. WarehouseInfotId: warehouse.ID,
  7542. Status: 1,
  7543. Ctime: time.Now().Unix(),
  7544. Remark: warehouse.Remark,
  7545. OrgId: orgID,
  7546. Type: 1,
  7547. Manufacturer: warehouse.Manufacturer,
  7548. Dealer: warehouse.Dealer,
  7549. IsSys: 1,
  7550. SysRecordTime: record_time,
  7551. GoodTypeId: goods.GoodTypeId,
  7552. GoodId: goods.GoodId,
  7553. PatientId: patient_id,
  7554. Number: warehouse.Number,
  7555. LicenseNumber: warehouse.LicenseNumber,
  7556. Price: warehouse.PackingPrice,
  7557. ExpiryDate: warehouse.ExpiryDate,
  7558. ProductDate: warehouse.ProductDate,
  7559. ProjectId: goods.ProjectId,
  7560. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7561. StorehouseId: goods.StorehouseId,
  7562. IsCheck: 1,
  7563. }
  7564. warehouseOutInfo.Count = stock_number
  7565. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, goods.PatientId, record_time, goods.ProjectId)
  7566. if errcodes == gorm.ErrRecordNotFound {
  7567. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  7568. if errOne != nil {
  7569. return errOne
  7570. }
  7571. //插入库存流水表
  7572. flow := models.VmStockFlow{
  7573. WarehouseOutId: warehouseOut.ID,
  7574. WarehousingId: warehouse.ID,
  7575. GoodId: goods.GoodId,
  7576. Number: warehouse.Number,
  7577. ProductDate: warehouse.ProductDate,
  7578. ExpireDate: warehouse.ExpiryDate,
  7579. Count: stock_number,
  7580. Price: warehouse.PackingPrice,
  7581. Status: 1,
  7582. Ctime: time.Now().Unix(),
  7583. UserOrgId: orgID,
  7584. Manufacturer: warehouse.Manufacturer,
  7585. Dealer: warehouse.Dealer,
  7586. LicenseNumber: warehouse.LicenseNumber,
  7587. IsEdit: 2,
  7588. Creator: warehouseOut.Creater,
  7589. SystemTime: record_time,
  7590. ConsumableType: 3,
  7591. WarehouseOutDetailId: warehouseOutInfo.ID,
  7592. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7593. IsSys: 1,
  7594. PatientId: patient_id,
  7595. ProjectId: goods.ProjectId,
  7596. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7597. StorehouseId: goods.StorehouseId,
  7598. OverCount: sum_count,
  7599. }
  7600. //查询流水是否存在
  7601. exsit, errflows := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  7602. if errflows == gorm.ErrRecordNotFound {
  7603. CreateStockFlowOne(flow)
  7604. } else if errflows == nil {
  7605. flow := models.VmStockFlow{
  7606. ID: exsit.ID,
  7607. WarehouseOutId: warehouseOut.ID,
  7608. WarehousingId: warehouse.ID,
  7609. GoodId: goods.GoodId,
  7610. Number: warehouse.Number,
  7611. ProductDate: warehouse.ProductDate,
  7612. ExpireDate: warehouse.ExpiryDate,
  7613. Count: exsit.Count + goods.Count,
  7614. Price: warehouse.PackingPrice,
  7615. Status: 1,
  7616. Ctime: time.Now().Unix(),
  7617. UserOrgId: orgID,
  7618. Manufacturer: warehouse.Manufacturer,
  7619. Dealer: warehouse.Dealer,
  7620. LicenseNumber: warehouse.LicenseNumber,
  7621. IsEdit: 2,
  7622. Creator: warehouseOut.Creater,
  7623. SystemTime: record_time,
  7624. ConsumableType: 3,
  7625. WarehouseOutDetailId: warehouseOutInfo.ID,
  7626. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7627. IsSys: 1,
  7628. PatientId: patient_id,
  7629. ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  7630. ProjectId: goods.ProjectId,
  7631. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7632. StorehouseId: goods.StorehouseId,
  7633. OverCount: sum_count,
  7634. }
  7635. errOne := UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  7636. if errOne == nil {
  7637. }
  7638. }
  7639. } else if errcodes == nil {
  7640. goods.Count = deliver_number - stock_number
  7641. //更新数量为 该批次剩余数量 + 还有未出的数量
  7642. warehouseOutInfo.Count = stock_number
  7643. UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, goods.PatientId, record_time, goods.ProjectId)
  7644. //插入库存流水表
  7645. flow := models.VmStockFlow{
  7646. WarehouseOutId: warehouseOut.ID,
  7647. WarehousingId: warehouse.ID,
  7648. GoodId: goods.GoodId,
  7649. Number: warehouse.Number,
  7650. ProductDate: warehouse.ProductDate,
  7651. ExpireDate: warehouse.ExpiryDate,
  7652. Count: stock_number,
  7653. Price: warehouse.PackingPrice,
  7654. Status: 1,
  7655. Ctime: time.Now().Unix(),
  7656. UserOrgId: orgID,
  7657. Manufacturer: warehouse.Manufacturer,
  7658. Dealer: warehouse.Dealer,
  7659. LicenseNumber: warehouse.LicenseNumber,
  7660. IsEdit: 2,
  7661. Creator: warehouseOut.Creater,
  7662. SystemTime: record_time,
  7663. ConsumableType: 3,
  7664. WarehouseOutDetailId: warehouseOutInfo.ID,
  7665. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7666. IsSys: 1,
  7667. PatientId: patient_id,
  7668. ProjectId: goods.ProjectId,
  7669. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7670. StorehouseId: goods.StorehouseId,
  7671. OverCount: sum_count,
  7672. }
  7673. //查询流水是否存在
  7674. exsit, errflows := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  7675. if errflows == gorm.ErrRecordNotFound {
  7676. CreateStockFlowOne(flow)
  7677. } else if errflows == nil {
  7678. flow := models.VmStockFlow{
  7679. ID: exsit.ID,
  7680. WarehouseOutId: warehouseOut.ID,
  7681. WarehousingId: warehouse.ID,
  7682. GoodId: goods.GoodId,
  7683. Number: warehouse.Number,
  7684. ProductDate: warehouse.ProductDate,
  7685. ExpireDate: warehouse.ExpiryDate,
  7686. Count: exsit.Count + goods.Count,
  7687. Price: warehouse.PackingPrice,
  7688. Status: 1,
  7689. Ctime: time.Now().Unix(),
  7690. UserOrgId: orgID,
  7691. Manufacturer: warehouse.Manufacturer,
  7692. Dealer: warehouse.Dealer,
  7693. LicenseNumber: warehouse.LicenseNumber,
  7694. IsEdit: 2,
  7695. Creator: warehouseOut.Creater,
  7696. SystemTime: record_time,
  7697. ConsumableType: 3,
  7698. WarehouseOutDetailId: warehouseOutInfo.ID,
  7699. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7700. IsSys: 1,
  7701. PatientId: patient_id,
  7702. ProjectId: goods.ProjectId,
  7703. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7704. StorehouseId: goods.StorehouseId,
  7705. OverCount: sum_count,
  7706. }
  7707. UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  7708. }
  7709. }
  7710. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  7711. goods.Count = deliver_number - stock_number
  7712. ConsumablesDelivery(orgID, patient_id, record_time, goods, warehouseOut, count)
  7713. }
  7714. return nil
  7715. }
  7716. //耗材出库删除
  7717. func ConsumablesDeliveryDelete(orgID int64, patient_id int64, record_time int64, good_yc *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut) (err error) {
  7718. // 先根据相关信息查询当天该耗材的出库信息
  7719. warehouseOutInfos, err := FindStockOutInfoByStock(orgID, good_yc.GoodTypeId, good_yc.GoodId, record_time, patient_id)
  7720. if err != nil {
  7721. return err
  7722. }
  7723. var delete_count int64 = 0
  7724. for _, ware := range warehouseOutInfos {
  7725. // 删除出库完成后,要增加对应批次的库存数量
  7726. errThree := UpDateWarehouseInfoByStockDelete(ware.WarehouseInfotId, delete_count, patient_id, record_time, good_yc.GoodId)
  7727. if errThree != nil {
  7728. return errThree
  7729. }
  7730. //查询剩余库存
  7731. goodList, _ := GetAllGoodSumCount(good_yc.GoodId, orgID)
  7732. var sum_count int64
  7733. for _, item := range goodList {
  7734. sum_count += item.StockCount
  7735. }
  7736. // 判断当前出库的数据和删除出库数量
  7737. if good_yc.Count <= ware.Count {
  7738. delete_count = good_yc.Count
  7739. } else {
  7740. delete_count = ware.Count
  7741. }
  7742. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  7743. // 在出库记录表里记录退库详情
  7744. warehouseOutInfo := &models.WarehouseOutInfo{
  7745. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7746. WarehouseOutId: warehouseOut.ID,
  7747. //WarehouseInfoId: warehouse.ID,
  7748. Status: 1,
  7749. Ctime: time.Now().Unix(),
  7750. Remark: "",
  7751. OrgId: orgID,
  7752. Type: 1,
  7753. Manufacturer: 0,
  7754. Dealer: 0,
  7755. IsSys: 2,
  7756. SysRecordTime: record_time,
  7757. GoodTypeId: good_yc.GoodTypeId,
  7758. GoodId: good_yc.GoodId,
  7759. PatientId: patient_id,
  7760. StorehouseId: houseConfig.StorehouseOutInfo,
  7761. OverCount: sum_count,
  7762. }
  7763. warehouseOutInfo.Count = ware.Count - delete_count
  7764. stockInInfo, _ := FindLastStockInInfoRecord(good_yc.GoodId, orgID)
  7765. warehouseOutInfo.Price = stockInInfo.Price
  7766. errOne := UpdateAutoMaticReduceDetail(good_yc.GoodId, good_yc.GoodTypeId, record_time, patient_id, orgID, warehouseOutInfo)
  7767. if errOne != nil {
  7768. return errOne
  7769. } else {
  7770. details := &models.AutomaticReduceDetail{
  7771. WarehouseOutId: warehouseOutInfo.ID,
  7772. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  7773. PatientId: patient_id,
  7774. Ctime: time.Now().Unix(),
  7775. Mtime: time.Now().Unix(),
  7776. Status: 1,
  7777. RecordTime: record_time,
  7778. OrgId: orgID,
  7779. GoodId: good_yc.GoodId,
  7780. GoodTypeId: good_yc.GoodTypeId,
  7781. Count: warehouseOutInfo.Count,
  7782. Type: 2,
  7783. StorehouseId: houseConfig.StorehouseOutInfo,
  7784. }
  7785. //查询是否当天已经存在数据
  7786. _, errcode := GetAutoMaticReduceDetail(orgID, patient_id, record_time, good_yc.GoodId, good_yc.GoodTypeId)
  7787. if errcode == gorm.ErrRecordNotFound {
  7788. errTwo := AddSigleAutoReduceRecordInfo(details)
  7789. if errTwo != nil {
  7790. return errTwo
  7791. }
  7792. } else if errcode == nil {
  7793. DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, good_yc.GoodId, good_yc.GoodTypeId)
  7794. AddSigleAutoReduceRecordInfo(details)
  7795. }
  7796. }
  7797. //扣减数量相加
  7798. ModifyAddGoodSumCount(houseConfig.StorehouseOutInfo, delete_count, good_yc.UserOrgId, good_yc.GoodId)
  7799. //退库数量相加
  7800. UpdateSumAddCancelCount(good_yc.UserOrgId, good_yc.GoodId, houseConfig.StorehouseOutInfo, delete_count)
  7801. // 增加了对应的库存后,看看还有多少需要退库的
  7802. good_yc.Count = good_yc.Count - delete_count
  7803. if good_yc.Count == 0 {
  7804. return nil
  7805. }
  7806. }
  7807. if good_yc.Count == 0 {
  7808. return nil
  7809. } else {
  7810. return errors.New("退库和出库数据不匹配")
  7811. }
  7812. }
  7813. func GetAutoReduceRecordInfoByPatientId(orgid int64, patient_id int64, recordTime int64) (autoReduce []*models.AutomaticReduceDetail, err error) {
  7814. err = XTReadDB().Model(&autoReduce).Where("org_id = ? and patient_id = ? and record_time = ? and status = 1", orgid, patient_id, recordTime).Find(&autoReduce).Error
  7815. return autoReduce, err
  7816. }
  7817. func DeleteDialysisBefor(orgid int64, patient_id int64, record_date int64, goodid int64, goodtypeid int64) error {
  7818. prepare := models.DialysisBeforePrepare{}
  7819. 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
  7820. return err
  7821. }
  7822. func DeleteDialysisBeforThree(orgid int64, patient_id int64, record_date int64) error {
  7823. prepare := models.DialysisBeforePrepare{}
  7824. 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
  7825. return err
  7826. }
  7827. func GetWarehouseById(id int64) (models.WarehouseOut, error) {
  7828. out := models.WarehouseOut{}
  7829. err := XTReadDB().Where("id=? and status = 1", id).Find(&out).Error
  7830. return out, err
  7831. }
  7832. //耗材出库
  7833. func ConsumablesDeliveryOne(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64) (err error) {
  7834. ////开事务
  7835. //tx := XTWriteDB().Begin()
  7836. var deliver_number int64 = 0
  7837. var stock_number int64 = 0
  7838. var maxNumber int64 = 0
  7839. deliver_number = goods.Count
  7840. // 根据先进先出原则,查询最先入库的批次,进行出库
  7841. // 如果没有对应的库存,则报错
  7842. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  7843. if err != nil {
  7844. return err
  7845. }
  7846. stock_number = warehouse.StockCount
  7847. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  7848. if stock_number >= deliver_number {
  7849. warehouseOutInfo := &models.WarehouseOutInfo{
  7850. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7851. WarehouseOutId: warehouseOut.ID,
  7852. WarehouseInfotId: warehouse.ID,
  7853. Status: 1,
  7854. Ctime: time.Now().Unix(),
  7855. Remark: goods.Remark,
  7856. OrgId: orgID,
  7857. Type: 1,
  7858. Manufacturer: goods.Manufacturer,
  7859. Dealer: goods.Dealer,
  7860. IsSys: 0,
  7861. SysRecordTime: record_time,
  7862. GoodTypeId: goods.GoodTypeId,
  7863. GoodId: goods.GoodId,
  7864. ExpiryDate: goods.ExpiryDate,
  7865. ProductDate: goods.ProductDate,
  7866. Number: warehouse.Number,
  7867. Price: goods.Price,
  7868. LicenseNumber: goods.LicenseNumber,
  7869. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7870. StorehouseId: goods.StorehouseId,
  7871. AdminUserId: goods.AdminUserId,
  7872. BuyPrice: warehouse.Price,
  7873. StockCount: goods.StockCount,
  7874. }
  7875. warehouseOutInfo.Count = goods.Count
  7876. //添加出库单详情
  7877. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  7878. if errOne != nil {
  7879. return errOne
  7880. }
  7881. stockFlow := models.VmStockFlow{
  7882. WarehousingId: warehouse.ID,
  7883. GoodId: goods.GoodId,
  7884. Number: warehouse.Number,
  7885. LicenseNumber: goods.LicenseNumber,
  7886. Count: goods.Count,
  7887. UserOrgId: goods.OrgId,
  7888. PatientId: goods.PatientId,
  7889. SystemTime: record_time,
  7890. ConsumableType: 2,
  7891. IsSys: goods.IsSys,
  7892. WarehousingOrder: "",
  7893. WarehouseOutId: warehouseOut.ID,
  7894. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7895. IsEdit: 1,
  7896. CancelStockId: 0,
  7897. CancelOrderNumber: "",
  7898. Manufacturer: goods.Manufacturer,
  7899. Dealer: goods.Dealer,
  7900. Creator: creator,
  7901. UpdateCreator: 0,
  7902. Status: 1,
  7903. Ctime: time.Now().Unix(),
  7904. Mtime: 0,
  7905. Price: warehouse.PackingPrice,
  7906. WarehousingDetailId: 0,
  7907. WarehouseOutDetailId: warehouseOutInfo.ID,
  7908. CancelOutDetailId: 0,
  7909. ProductDate: goods.ProductDate,
  7910. ExpireDate: goods.ExpiryDate,
  7911. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7912. StorehouseId: warehouse.StorehouseId,
  7913. AdminUserId: goods.AdminUserId,
  7914. BuyPrice: warehouse.Price,
  7915. StockCount: goods.StockCount,
  7916. }
  7917. //创建出库流水
  7918. CreateStockFlowOne(stockFlow)
  7919. maxNumber = goods.Count
  7920. if warehouse.StockCount < maxNumber {
  7921. return errors.New("库存数量不足")
  7922. }
  7923. warehouse.StockCount = warehouse.StockCount - maxNumber
  7924. warehouse.Mtime = time.Now().Unix()
  7925. //扣减库存
  7926. errThree := UpDateWarehouseInfoByStock(&warehouse)
  7927. fmt.Println(errThree)
  7928. //if errThree != nil {
  7929. // utils.ErrorLog("事务失败,原因为: %v", errThree.Error())
  7930. // tx.Rollback()
  7931. //} else {
  7932. // tx.Commit()
  7933. //}
  7934. //查询该机构默认仓库
  7935. storeConfig, _ := GetAllStoreHouseConfig(goods.OrgId)
  7936. //查询剩余库存
  7937. goodList, _ := GetGoodSumCountByStoreId(storeConfig.StorehouseOutInfo, goods.GoodId, goods.OrgId)
  7938. var sum_count int64
  7939. var sum_in_count int64
  7940. for _, item := range goodList {
  7941. sum_count += item.StockCount
  7942. sum_in_count += item.WarehousingCount
  7943. }
  7944. UpdateGoodByGoodId(goods.GoodId, sum_count, sum_in_count, goods.OrgId)
  7945. if errThree != nil {
  7946. return errThree
  7947. }
  7948. } else {
  7949. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  7950. warehouseOutInfo := &models.WarehouseOutInfo{
  7951. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7952. WarehouseOutId: warehouseOut.ID,
  7953. WarehouseInfotId: warehouse.ID,
  7954. Status: 1,
  7955. Ctime: time.Now().Unix(),
  7956. Remark: goods.Remark,
  7957. OrgId: orgID,
  7958. Type: 1,
  7959. Manufacturer: goods.Manufacturer,
  7960. Dealer: goods.Dealer,
  7961. IsSys: 0,
  7962. SysRecordTime: record_time,
  7963. GoodTypeId: goods.GoodTypeId,
  7964. GoodId: goods.GoodId,
  7965. ExpiryDate: goods.ExpiryDate,
  7966. ProductDate: goods.ProductDate,
  7967. Number: warehouse.Number,
  7968. Price: warehouse.PackingPrice,
  7969. LicenseNumber: goods.LicenseNumber,
  7970. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7971. StorehouseId: warehouse.StorehouseId,
  7972. AdminUserId: goods.AdminUserId,
  7973. BuyPrice: warehouse.Price,
  7974. StockCount: goods.StockCount,
  7975. IsCheck: 1,
  7976. }
  7977. warehouseOutInfo.Count = stock_number
  7978. //查询是否已经生成出库单
  7979. info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  7980. if errcodes == gorm.ErrRecordNotFound {
  7981. //创建出库单
  7982. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  7983. if errOne != nil {
  7984. return errOne
  7985. }
  7986. } else {
  7987. warehouseOutInfo.Count = info.Count + stock_number
  7988. UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  7989. }
  7990. stockFlow := models.VmStockFlow{
  7991. WarehousingId: warehouse.ID,
  7992. GoodId: goods.GoodId,
  7993. Number: warehouse.Number,
  7994. LicenseNumber: goods.LicenseNumber,
  7995. Count: stock_number,
  7996. UserOrgId: goods.OrgId,
  7997. PatientId: 0,
  7998. SystemTime: record_time,
  7999. ConsumableType: 2,
  8000. IsSys: 0,
  8001. WarehousingOrder: "",
  8002. WarehouseOutId: warehouseOut.ID,
  8003. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  8004. IsEdit: 1,
  8005. CancelStockId: 0,
  8006. CancelOrderNumber: "",
  8007. Manufacturer: goods.Manufacturer,
  8008. Dealer: goods.Dealer,
  8009. Creator: creator,
  8010. UpdateCreator: 0,
  8011. Status: 1,
  8012. Ctime: time.Now().Unix(),
  8013. Mtime: 0,
  8014. Price: warehouse.PackingPrice,
  8015. WarehousingDetailId: 0,
  8016. WarehouseOutDetailId: warehouseOutInfo.ID,
  8017. CancelOutDetailId: 0,
  8018. ProductDate: goods.ProductDate,
  8019. ExpireDate: goods.ExpiryDate,
  8020. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  8021. StorehouseId: goods.StorehouseId,
  8022. AdminUserId: goods.AdminUserId,
  8023. BuyPrice: warehouse.Price,
  8024. StockCount: goods.StockCount,
  8025. }
  8026. //创建出库明细
  8027. CreateStockFlowOne(stockFlow)
  8028. //查询该机构默认仓库
  8029. storeConfig, _ := GetAllStoreHouseConfig(goods.OrgId)
  8030. //查询剩余库存
  8031. goodList, _ := GetGoodSumCountByStoreId(storeConfig.StorehouseOutInfo, goods.GoodId, goods.OrgId)
  8032. var sum_count int64
  8033. var sum_in_count int64
  8034. for _, item := range goodList {
  8035. sum_count += item.StockCount
  8036. sum_in_count += item.WarehousingCount
  8037. }
  8038. UpdateGoodByGoodId(goods.GoodId, sum_count, sum_in_count, goods.OrgId)
  8039. // 出库完成后,要将该批次库存清零
  8040. warehouse.StockCount = 0
  8041. warehouse.Mtime = time.Now().Unix()
  8042. errThree := UpDateWarehouseInfoByStock(&warehouse)
  8043. fmt.Println(errThree)
  8044. //if errThree != nil {
  8045. // utils.ErrorLog("事务失败,原因为: %v", errThree.Error())
  8046. // tx.Rollback()
  8047. //} else {
  8048. // tx.Commit()
  8049. //}
  8050. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  8051. goods.Count = deliver_number - stock_number
  8052. ConsumablesDeliveryOne(orgID, record_time, goods, warehouseOut, count, creator)
  8053. }
  8054. return nil
  8055. }
  8056. //退库逻辑
  8057. func ConsumablesDeliveryTwo(orgID int64, goods *models.WarehouseOutInfo, count int64) (err error) {
  8058. ////开事务
  8059. //tx := XTWriteDB().Begin()
  8060. var deliver_number int64 = 0
  8061. var stock_number int64 = 0
  8062. var maxNumber int64 = 0
  8063. deliver_number = goods.Count
  8064. // 根据先进先出原则,查询最先入库的批次,进行出库
  8065. warehouse, err := FindFirstWarehousingInfoByGoodId(goods.GoodId, goods.ID, goods.StorehouseId)
  8066. if err != nil {
  8067. return err
  8068. }
  8069. stock_number = warehouse.StockCount
  8070. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  8071. if stock_number >= deliver_number {
  8072. warehouseOutInfo := &models.WarehouseOutInfo{
  8073. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  8074. WarehouseOutId: goods.ID,
  8075. WarehouseInfotId: warehouse.ID,
  8076. Status: 1,
  8077. Ctime: time.Now().Unix(),
  8078. Remark: goods.Remark,
  8079. OrgId: orgID,
  8080. Type: 1,
  8081. Manufacturer: goods.Manufacturer,
  8082. Dealer: goods.Dealer,
  8083. IsSys: 0,
  8084. GoodTypeId: goods.GoodTypeId,
  8085. GoodId: goods.GoodId,
  8086. ExpiryDate: goods.ExpiryDate,
  8087. ProductDate: goods.ProductDate,
  8088. Number: goods.Number,
  8089. Price: warehouse.PackingPrice,
  8090. LicenseNumber: goods.LicenseNumber,
  8091. ConsumableType: 2,
  8092. }
  8093. warehouseOutInfo.Count = goods.Count
  8094. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  8095. if errOne != nil {
  8096. return errOne
  8097. }
  8098. maxNumber = goods.Count
  8099. if warehouse.StockCount < maxNumber { //这段逻辑根本不会执行,因为当库存数量大于或等于出库数量才会进入这个分支
  8100. return errors.New("库存数量不足")
  8101. }
  8102. warehouse.StockCount = warehouse.StockCount - maxNumber
  8103. warehouse.Mtime = time.Now().Unix()
  8104. if warehouse.StockCount <= 0 {
  8105. return errors.New("库存数量不足")
  8106. }
  8107. errThree := UpDateWarehouseInfoByStock(&warehouse)
  8108. fmt.Println(errThree)
  8109. //if errThree != nil {
  8110. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  8111. // tx.Rollback()
  8112. //} else {
  8113. // tx.Commit()
  8114. //}
  8115. return
  8116. } else {
  8117. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  8118. warehouseOutInfo := &models.WarehouseOutInfo{
  8119. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  8120. WarehouseOutId: goods.ID,
  8121. WarehouseInfotId: warehouse.ID,
  8122. Status: 1,
  8123. Ctime: time.Now().Unix(),
  8124. Remark: goods.Remark,
  8125. OrgId: orgID,
  8126. Type: 1,
  8127. Manufacturer: goods.Manufacturer,
  8128. Dealer: goods.Dealer,
  8129. IsSys: 0,
  8130. GoodTypeId: goods.GoodTypeId,
  8131. GoodId: goods.GoodId,
  8132. ExpiryDate: goods.ExpiryDate,
  8133. ProductDate: goods.ProductDate,
  8134. Number: goods.Number,
  8135. Price: warehouse.PackingPrice,
  8136. LicenseNumber: goods.LicenseNumber,
  8137. ConsumableType: 2,
  8138. IsCheck: 1,
  8139. StorehouseId: goods.StorehouseId,
  8140. }
  8141. warehouseOutInfo.Count = stock_number
  8142. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  8143. if errOne != nil {
  8144. return errOne
  8145. }
  8146. // 出库完成后,要将该批次库存清零
  8147. warehouse.StockCount = 0
  8148. warehouse.Mtime = time.Now().Unix()
  8149. errThree := UpDateWarehouseInfoByStock(&warehouse)
  8150. //if errThree != nil {
  8151. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  8152. // tx.Rollback()
  8153. //} else {
  8154. // tx.Commit()
  8155. //}
  8156. fmt.Println(errThree)
  8157. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  8158. goods.Count = deliver_number - stock_number
  8159. ConsumablesDeliveryTwo(orgID, goods, count)
  8160. }
  8161. return nil
  8162. }
  8163. func DeleteWarehouseInfo(id int64) error {
  8164. err := XTWriteDB().Model(models.WarehousingInfo{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": 0}).Error
  8165. return err
  8166. }
  8167. // 药品出库 递归方式
  8168. func NewDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DoctorAdvice) (err error) {
  8169. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  8170. var deliver_number int64 = 0
  8171. var stock_number int64 = 0
  8172. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  8173. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  8174. var drug_price float64
  8175. //按最小单位数据计算
  8176. deliver_number = count
  8177. if advice.PrescribingNumberUnit == drup.MaxUnit {
  8178. drug_price = drup.RetailPrice
  8179. }
  8180. if advice.PrescribingNumberUnit == drup.MinUnit {
  8181. drug_price = drup.MinPrice
  8182. }
  8183. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  8184. drug_price = drup.RetailPrice
  8185. }
  8186. // 根据先进先出原则,查询最先入库的批次,进行出库
  8187. // 如果没有对应的库存,则报错
  8188. //查询默认仓库
  8189. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  8190. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  8191. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  8192. var stockMax int64
  8193. var stockMin int64
  8194. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  8195. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  8196. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  8197. UpdateMinNumber(lastWarehouse.ID, stockMin)
  8198. }
  8199. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  8200. if err != nil {
  8201. return err
  8202. }
  8203. // 将该批次的剩余库存数量转换为拆零数量
  8204. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  8205. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  8206. if stock_number >= deliver_number {
  8207. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  8208. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8209. WarehouseOutId: warehouseout.ID,
  8210. Status: 1,
  8211. Ctime: time.Now().Unix(),
  8212. Remark: warehouse.Remark,
  8213. OrgId: orgID,
  8214. Type: 1,
  8215. Manufacturer: warehouse.Manufacturer,
  8216. Dealer: warehouse.Dealer,
  8217. IsSys: 1,
  8218. SysRecordTime: advice.RecordDate,
  8219. DrugId: advice.DrugId,
  8220. Number: warehouse.Number,
  8221. BatchNumber: warehouse.BatchNumber,
  8222. Price: warehouse.RetailPrice,
  8223. CountUnit: drup.MinUnit,
  8224. RetailPrice: drug_price,
  8225. ProductDate: warehouse.ProductDate,
  8226. ExpiryDate: warehouse.ExpiryDate,
  8227. PatientId: advice.PatientId,
  8228. Count: deliver_number, //出最小单位
  8229. WarehouseInfoId: warehouse.ID,
  8230. AdviceId: advice.ID,
  8231. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  8232. StorehouseId: storeConfig.DrugStorehouseOut,
  8233. IsCheck: 1,
  8234. }
  8235. drugflow := models.DrugFlow{
  8236. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8237. WarehouseOutId: warehouseout.ID,
  8238. DrugId: advice.DrugId,
  8239. Number: warehouse.Number,
  8240. ProductDate: warehouse.ProductDate,
  8241. ExpireDate: warehouse.ExpiryDate,
  8242. Count: deliver_number, //按最小单位计算,
  8243. Price: warehouse.RetailPrice,
  8244. Status: 1,
  8245. Ctime: time.Now().Unix(),
  8246. UserOrgId: orgID,
  8247. Manufacturer: warehouse.Manufacturer,
  8248. Dealer: warehouse.Dealer,
  8249. BatchNumber: warehouse.BatchNumber,
  8250. MaxUnit: drup.MinUnit,
  8251. ConsumableType: 3,
  8252. IsEdit: 1,
  8253. Creator: advice.ExecutionStaff,
  8254. IsSys: 1,
  8255. PatientId: advice.PatientId,
  8256. WarehousingDetailId: warehouse.ID,
  8257. AdviceId: advice.ID,
  8258. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  8259. StorehouseId: storeConfig.DrugStorehouseOut,
  8260. }
  8261. CreateDrugFlowOne(drugflow)
  8262. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  8263. if errOne != nil {
  8264. return errOne
  8265. }
  8266. //查询是否存在数据
  8267. details := &models.DrugAutomaticReduceDetail{
  8268. WarehouseOutId: warehouseout.ID,
  8269. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8270. PatientId: advice.PatientId,
  8271. Ctime: time.Now().Unix(),
  8272. Mtime: time.Now().Unix(),
  8273. Status: 1,
  8274. RecordTime: advice.RecordDate,
  8275. OrgId: orgID,
  8276. DrugId: advice.DrugId,
  8277. Count: deliver_number,
  8278. CountUnit: drup.MinUnit,
  8279. WarehouseInfoId: warehouse.ID,
  8280. StorehouseId: storeConfig.DrugStorehouseOut,
  8281. }
  8282. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  8283. if errTwo != nil {
  8284. return errTwo
  8285. }
  8286. // 出库完成后,要减去对应批次的库存数量
  8287. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  8288. var maxNumber int64 = 0
  8289. var minNumber int64 = 0
  8290. //var stock_max_number int64 = 0
  8291. //stock_max_number = warehouse.StockMaxNumber
  8292. maxNumber = deliver_number / drup.MinNumber
  8293. minNumber = deliver_number % drup.MinNumber
  8294. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  8295. minNumber = maxNumber
  8296. }
  8297. if drup.MaxUnit != drup.MinUnit {
  8298. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  8299. return errors.New("库存数量不足")
  8300. }
  8301. }
  8302. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  8303. if warehouse.StockMaxNumber < 0 {
  8304. warehouse.StockMaxNumber = 0
  8305. }
  8306. if warehouse.StockMinNumber < 0 {
  8307. warehouse.StockMinNumber = 0
  8308. }
  8309. warehouse.Mtime = time.Now().Unix()
  8310. if warehouse.StockMinNumber < minNumber {
  8311. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  8312. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  8313. } else {
  8314. if minNumber > 0 {
  8315. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  8316. }
  8317. if minNumber == 0 && maxNumber != 1 {
  8318. if warehouse.StockMinNumber > 0 {
  8319. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  8320. }
  8321. }
  8322. }
  8323. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  8324. if (warehouse.StockMinNumber - deliver_number) > 0 {
  8325. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  8326. //if warehouse.StockMinNumber == 0 {
  8327. // warehouse.StockMaxNumber = stock_max_number
  8328. //}
  8329. }
  8330. }
  8331. if drup.MaxUnit != drup.MinUnit {
  8332. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  8333. return errors.New("库存数量不足")
  8334. }
  8335. }
  8336. if warehouse.StockMinNumber <= 0 {
  8337. warehouse.StockMinNumber = 0
  8338. }
  8339. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  8340. if errThree != nil {
  8341. return errThree
  8342. }
  8343. return nil
  8344. } else {
  8345. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  8346. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  8347. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8348. WarehouseOutId: warehouseout.ID,
  8349. Status: 1,
  8350. Ctime: time.Now().Unix(),
  8351. Remark: warehouse.Remark,
  8352. OrgId: orgID,
  8353. Type: 1,
  8354. Manufacturer: warehouse.Manufacturer,
  8355. Dealer: warehouse.Dealer,
  8356. IsSys: 1,
  8357. SysRecordTime: advice.RecordDate,
  8358. DrugId: advice.DrugId,
  8359. Number: warehouse.Number,
  8360. BatchNumber: warehouse.BatchNumber,
  8361. Price: warehouse.RetailPrice,
  8362. CountUnit: drup.MinUnit,
  8363. ProductDate: warehouse.ProductDate,
  8364. ExpiryDate: warehouse.ExpiryDate,
  8365. PatientId: advice.PatientId,
  8366. Count: deliver_number,
  8367. WarehouseInfoId: warehouse.ID,
  8368. AdviceId: advice.ID,
  8369. StorehouseId: storeConfig.DrugStorehouseOut,
  8370. IsCheck: 1,
  8371. }
  8372. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  8373. if errOne != nil {
  8374. return errOne
  8375. }
  8376. details := &models.DrugAutomaticReduceDetail{
  8377. WarehouseOutId: warehouseout.ID,
  8378. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  8379. PatientId: advice.PatientId,
  8380. Ctime: time.Now().Unix(),
  8381. Mtime: time.Now().Unix(),
  8382. Status: 1,
  8383. RecordTime: advice.RecordDate,
  8384. OrgId: orgID,
  8385. DrugId: advice.DrugId,
  8386. Count: deliver_number,
  8387. CountUnit: drup.MinUnit,
  8388. WarehouseInfoId: warehouse.ID,
  8389. StorehouseId: storeConfig.DrugStorehouseOut,
  8390. }
  8391. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  8392. if errTwo != nil {
  8393. return errTwo
  8394. }
  8395. // 出库完成后,要将该批次库存清零
  8396. warehouse.StockMaxNumber = 0
  8397. warehouse.StockMinNumber = 0
  8398. warehouse.Mtime = time.Now().Unix()
  8399. info := models.XtDrugWarehouseInfo{
  8400. ID: warehouse.ID,
  8401. WarehousingId: warehouse.WarehousingId,
  8402. DrugId: warehouse.DrugId,
  8403. Number: warehouse.Number,
  8404. ProductDate: warehouse.ProductDate,
  8405. ExpiryDate: warehouse.ExpiryDate,
  8406. WarehousingCount: warehouse.WarehousingCount,
  8407. Price: warehouse.Price,
  8408. TotalPrice: warehouse.TotalPrice,
  8409. Dealer: warehouse.Dealer,
  8410. Manufacturer: warehouse.Manufacturer,
  8411. Remark: warehouse.Remark,
  8412. Ctime: warehouse.Ctime,
  8413. Mtime: warehouse.Mtime,
  8414. Status: 1,
  8415. OrgId: warehouse.OrgId,
  8416. IsReturn: warehouse.IsReturn,
  8417. WarehousingOrder: warehouse.WarehousingOrder,
  8418. Type: warehouse.Type,
  8419. RetailPrice: warehouse.RetailPrice,
  8420. RetailTotalPrice: warehouse.RetailPrice,
  8421. StockMaxNumber: 0,
  8422. StockMinNumber: 0,
  8423. BatchNumber: warehouse.BatchNumber,
  8424. MaxUnit: warehouse.MaxUnit,
  8425. WarehousingInfoId: warehouse.WarehousingInfoId,
  8426. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  8427. StorehouseId: storeConfig.DrugStorehouseOut,
  8428. IsCheck: 1,
  8429. }
  8430. //扣减库存
  8431. errThree := UpDateDrugWarehouseInfoByStock(&info)
  8432. if errThree != nil {
  8433. return errThree
  8434. }
  8435. drugflow := models.DrugFlow{
  8436. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8437. WarehouseOutId: warehouseout.ID,
  8438. DrugId: advice.DrugId,
  8439. Number: warehouse.Number,
  8440. ProductDate: warehouse.ProductDate,
  8441. ExpireDate: warehouse.ExpiryDate,
  8442. Count: deliver_number,
  8443. Price: warehouse.RetailPrice,
  8444. Status: 1,
  8445. Ctime: time.Now().Unix(),
  8446. UserOrgId: orgID,
  8447. Manufacturer: warehouse.Manufacturer,
  8448. Dealer: warehouse.Dealer,
  8449. BatchNumber: warehouse.BatchNumber,
  8450. MaxUnit: drup.MinUnit,
  8451. ConsumableType: 3,
  8452. IsEdit: 1,
  8453. Creator: advice.ExecutionStaff,
  8454. IsSys: 1,
  8455. PatientId: advice.PatientId,
  8456. WarehousingDetailId: warehouse.ID,
  8457. AdviceId: advice.ID,
  8458. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  8459. StorehouseId: storeConfig.DrugStorehouseOut,
  8460. }
  8461. CreateDrugFlowOne(drugflow)
  8462. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  8463. prescribingNumber_two_temp := deliver_number - stock_number
  8464. overPlusNumber := float64(prescribingNumber_two_temp)
  8465. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  8466. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  8467. NewDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  8468. }
  8469. return
  8470. }
  8471. // 药品出库 递归方式
  8472. func NewHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  8473. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  8474. var deliver_number int64 = 0
  8475. var stock_number int64 = 0
  8476. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  8477. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  8478. deliver_number = count
  8479. var min_price float64
  8480. if advice.PrescribingNumberUnit == drup.MaxUnit {
  8481. min_price = drup.RetailPrice
  8482. }
  8483. if advice.PrescribingNumberUnit == drup.MinUnit {
  8484. min_price = drup.MinPrice
  8485. }
  8486. if advice.PrescribingNumberUnit != drup.MaxUnit && advice.PrescribingNumberUnit != drup.MinUnit {
  8487. min_price = drup.RetailPrice
  8488. }
  8489. fmt.Println(min_price)
  8490. // 根据先进先出原则,查询最先入库的批次,进行出库
  8491. // 如果没有对应的库存,则报错
  8492. //开启事物
  8493. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  8494. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  8495. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  8496. var stockMax int64
  8497. var stockMin int64
  8498. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  8499. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  8500. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  8501. UpdateMinNumber(lastWarehouse.ID, stockMin)
  8502. }
  8503. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  8504. //查找药品信息
  8505. if err != nil {
  8506. return err
  8507. }
  8508. // 将该批次的剩余库存数量转换为拆零数量
  8509. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  8510. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  8511. if stock_number >= deliver_number {
  8512. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  8513. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8514. WarehouseOutId: warehouseout.ID,
  8515. Status: 1,
  8516. Ctime: time.Now().Unix(),
  8517. Remark: warehouse.Remark,
  8518. OrgId: orgID,
  8519. Type: 1,
  8520. Manufacturer: warehouse.Manufacturer,
  8521. Dealer: warehouse.Dealer,
  8522. IsSys: 1,
  8523. SysRecordTime: advice.RecordDate,
  8524. DrugId: advice.DrugId,
  8525. Number: warehouse.Number,
  8526. BatchNumber: warehouse.BatchNumber,
  8527. Price: warehouse.RetailPrice,
  8528. CountUnit: drup.MinUnit,
  8529. RetailPrice: warehouse.RetailPrice,
  8530. ProductDate: warehouse.ProductDate,
  8531. ExpiryDate: warehouse.ExpiryDate,
  8532. PatientId: advice.PatientId,
  8533. WarehouseInfoId: warehouse.ID,
  8534. Count: deliver_number,
  8535. AdviceId: advice.ID,
  8536. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  8537. StorehouseId: storeConfig.DrugStorehouseOut,
  8538. IsCheck: 1,
  8539. }
  8540. drugflow := models.DrugFlow{
  8541. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8542. WarehouseOutId: warehouseout.ID,
  8543. DrugId: advice.DrugId,
  8544. Number: warehouse.Number,
  8545. ProductDate: warehouse.ProductDate,
  8546. ExpireDate: warehouse.ExpiryDate,
  8547. Count: deliver_number,
  8548. Price: warehouse.RetailPrice,
  8549. Status: 1,
  8550. Ctime: time.Now().Unix(),
  8551. UserOrgId: orgID,
  8552. Manufacturer: warehouse.Manufacturer,
  8553. Dealer: warehouse.Dealer,
  8554. BatchNumber: warehouse.BatchNumber,
  8555. MaxUnit: drup.MinUnit,
  8556. ConsumableType: 3,
  8557. IsEdit: 1,
  8558. Creator: advice.ExecutionStaff,
  8559. IsSys: 1,
  8560. PatientId: advice.PatientId,
  8561. SystemTime: advice.RecordDate,
  8562. WarehousingDetailId: warehouse.ID,
  8563. AdviceId: advice.ID,
  8564. StorehouseId: storeConfig.DrugStorehouseOut,
  8565. }
  8566. CreateDrugFlowOne(drugflow)
  8567. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  8568. if errOne != nil {
  8569. return errOne
  8570. }
  8571. details := &models.DrugAutomaticReduceDetail{
  8572. WarehouseOutId: warehouseout.ID,
  8573. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8574. PatientId: advice.PatientId,
  8575. Ctime: time.Now().Unix(),
  8576. Mtime: time.Now().Unix(),
  8577. Status: 1,
  8578. RecordTime: advice.RecordDate,
  8579. OrgId: orgID,
  8580. DrugId: advice.DrugId,
  8581. Count: deliver_number,
  8582. CountUnit: drup.MinUnit,
  8583. WarehouseInfoId: warehouse.ID,
  8584. AdviceId: advice.ID,
  8585. StorehouseId: storeConfig.DrugStorehouseOut,
  8586. }
  8587. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  8588. if errTwo != nil {
  8589. return errTwo
  8590. }
  8591. // 出库完成后,要减去对应批次的库存数量
  8592. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  8593. var maxNumber int64 = 0
  8594. var minNumber int64 = 0
  8595. //var stock_max_number int64 = 0
  8596. //stock_max_number = warehouse.StockMaxNumber
  8597. maxNumber = deliver_number / drup.MinNumber
  8598. minNumber = deliver_number % drup.MinNumber
  8599. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  8600. minNumber = maxNumber
  8601. }
  8602. if drup.MaxUnit != drup.MinUnit {
  8603. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  8604. return errors.New("库存数量不足")
  8605. }
  8606. }
  8607. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  8608. if warehouse.StockMaxNumber < 0 {
  8609. warehouse.StockMaxNumber = 0
  8610. }
  8611. if warehouse.StockMinNumber < 0 {
  8612. warehouse.StockMinNumber = 0
  8613. }
  8614. warehouse.Mtime = time.Now().Unix()
  8615. if warehouse.StockMinNumber < minNumber {
  8616. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  8617. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  8618. } else {
  8619. if minNumber > 0 {
  8620. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  8621. }
  8622. if minNumber == 0 && maxNumber != 1 {
  8623. if warehouse.StockMinNumber > 0 {
  8624. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  8625. }
  8626. }
  8627. }
  8628. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  8629. if (warehouse.StockMinNumber - deliver_number) > 0 {
  8630. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  8631. //if warehouse.StockMinNumber == 0 {
  8632. // warehouse.StockMaxNumber = stock_max_number
  8633. //}
  8634. }
  8635. }
  8636. if drup.MaxUnit != drup.MinUnit {
  8637. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  8638. return errors.New("库存数量不足")
  8639. }
  8640. }
  8641. if warehouse.StockMinNumber <= 0 {
  8642. warehouse.StockMinNumber = 0
  8643. }
  8644. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  8645. if errThree != nil {
  8646. return errThree
  8647. }
  8648. return nil
  8649. } else {
  8650. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  8651. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  8652. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8653. WarehouseOutId: warehouseout.ID,
  8654. Status: 1,
  8655. Ctime: time.Now().Unix(),
  8656. Remark: warehouse.Remark,
  8657. OrgId: orgID,
  8658. Type: 1,
  8659. Manufacturer: warehouse.Manufacturer,
  8660. Dealer: warehouse.Dealer,
  8661. IsSys: 1,
  8662. SysRecordTime: advice.RecordDate,
  8663. DrugId: advice.DrugId,
  8664. Number: warehouse.Number,
  8665. BatchNumber: warehouse.BatchNumber,
  8666. Price: warehouse.RetailPrice,
  8667. RetailPrice: warehouse.RetailPrice,
  8668. ProductDate: warehouse.ProductDate,
  8669. ExpiryDate: warehouse.ExpiryDate,
  8670. PatientId: advice.PatientId,
  8671. WarehouseInfoId: warehouse.ID,
  8672. Count: stock_number,
  8673. CountUnit: drup.MinUnit,
  8674. AdviceId: advice.ID,
  8675. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  8676. StorehouseId: storeConfig.DrugStorehouseOut,
  8677. IsCheck: 1,
  8678. }
  8679. drugflow := models.DrugFlow{
  8680. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8681. WarehouseOutId: warehouseout.ID,
  8682. DrugId: advice.DrugId,
  8683. Number: warehouse.Number,
  8684. ProductDate: warehouse.ProductDate,
  8685. ExpireDate: warehouse.ExpiryDate,
  8686. Count: stock_number,
  8687. Price: warehouse.RetailPrice,
  8688. Status: 1,
  8689. Ctime: time.Now().Unix(),
  8690. UserOrgId: orgID,
  8691. Manufacturer: warehouse.Manufacturer,
  8692. Dealer: warehouse.Dealer,
  8693. BatchNumber: warehouse.BatchNumber,
  8694. MaxUnit: drup.MinUnit,
  8695. ConsumableType: 3,
  8696. IsEdit: 1,
  8697. Creator: advice.ExecutionStaff,
  8698. IsSys: 1,
  8699. PatientId: advice.PatientId,
  8700. SystemTime: advice.RecordDate,
  8701. WarehousingDetailId: warehouse.ID,
  8702. AdviceId: advice.ID,
  8703. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  8704. StorehouseId: storeConfig.DrugStorehouseOut,
  8705. }
  8706. CreateDrugFlowOne(drugflow)
  8707. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  8708. if errOne != nil {
  8709. return errOne
  8710. }
  8711. details := &models.DrugAutomaticReduceDetail{
  8712. WarehouseOutId: warehouseout.ID,
  8713. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8714. PatientId: advice.PatientId,
  8715. Ctime: time.Now().Unix(),
  8716. Mtime: time.Now().Unix(),
  8717. Status: 1,
  8718. RecordTime: advice.RecordDate,
  8719. OrgId: orgID,
  8720. DrugId: advice.DrugId,
  8721. Count: stock_number,
  8722. CountUnit: drup.MinUnit,
  8723. WarehouseInfoId: warehouse.ID,
  8724. AdviceId: advice.ID,
  8725. StorehouseId: storeConfig.DrugStorehouseOut,
  8726. }
  8727. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  8728. if errTwo != nil {
  8729. return errTwo
  8730. }
  8731. info := models.XtDrugWarehouseInfo{
  8732. ID: warehouse.ID,
  8733. WarehousingId: warehouse.WarehousingId,
  8734. DrugId: warehouse.DrugId,
  8735. Number: warehouse.Number,
  8736. ProductDate: warehouse.ProductDate,
  8737. ExpiryDate: warehouse.ExpiryDate,
  8738. WarehousingCount: warehouse.WarehousingCount,
  8739. Price: warehouse.RetailPrice,
  8740. TotalPrice: warehouse.TotalPrice,
  8741. Dealer: warehouse.Dealer,
  8742. Manufacturer: warehouse.Manufacturer,
  8743. Remark: warehouse.Remark,
  8744. Ctime: warehouse.Ctime,
  8745. Mtime: warehouse.Mtime,
  8746. Status: 1,
  8747. OrgId: warehouse.OrgId,
  8748. IsReturn: warehouse.IsReturn,
  8749. WarehousingOrder: warehouse.WarehousingOrder,
  8750. Type: warehouse.Type,
  8751. RetailPrice: warehouse.RetailPrice,
  8752. RetailTotalPrice: warehouse.RetailPrice,
  8753. StockMaxNumber: 0,
  8754. StockMinNumber: 0,
  8755. BatchNumber: warehouse.BatchNumber,
  8756. MaxUnit: warehouse.MaxUnit,
  8757. WarehousingInfoId: warehouse.WarehousingInfoId,
  8758. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  8759. StorehouseId: storeConfig.DrugStorehouseOut,
  8760. IsCheck: 1,
  8761. }
  8762. errThree := UpDateDrugWarehouseInfoByStock(&info)
  8763. if errThree != nil {
  8764. return errThree
  8765. }
  8766. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  8767. prescribingNumber_two_temp := deliver_number - stock_number
  8768. overPlusNumber := float64(prescribingNumber_two_temp)
  8769. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  8770. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  8771. NewHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  8772. }
  8773. return
  8774. }
  8775. // 药品出库 递归方式
  8776. func HisAuToReduce(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  8777. //查询最后一次出库数据
  8778. drugInfo, _ := GetDrugWarehouseOutInfoIsExit(advice.DrugId, advice.PatientId, advice.RecordDate)
  8779. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  8780. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  8781. var count_number int64
  8782. var device_number int64
  8783. if advice.PrescribingNumberUnit == drup.MaxUnit {
  8784. device_number = count * drup.MinNumber
  8785. }
  8786. if advice.PrescribingNumberUnit == drup.MinUnit {
  8787. device_number = count
  8788. }
  8789. if drugInfo.CountUnit == drup.MaxUnit {
  8790. count_number = drugInfo.Count * drup.MinNumber
  8791. }
  8792. if drugInfo.CountUnit == drup.MinUnit {
  8793. count_number = drugInfo.Count
  8794. }
  8795. // 比较数据大小
  8796. //如果当期医嘱数量小于最后一次出库则退库
  8797. if device_number < count_number {
  8798. var cha_number int64
  8799. var maxNumber int64
  8800. var minNumber int64
  8801. cha_number = count_number - device_number
  8802. maxNumber = cha_number / drup.MinNumber
  8803. minNumber = cha_number % drup.MinNumber
  8804. //更改库存
  8805. ModifyDrugWarehouse(drugInfo.WarehouseInfoId, maxNumber, minNumber)
  8806. //修改出库表
  8807. info := models.DrugWarehouseOutInfo{
  8808. Count: device_number,
  8809. CountUnit: advice.PrescribingNumberUnit,
  8810. }
  8811. ModifyDrugWarehouseOutInfo(info, advice.DrugId, advice.PatientId, advice.RecordDate)
  8812. //修改自动出库表
  8813. detail := models.DrugAutomaticReduceDetail{
  8814. Count: device_number,
  8815. CountUnit: advice.PrescribingNumberUnit,
  8816. }
  8817. ModifyAutoMaticReduce(detail, advice.PatientId, advice.RecordDate, advice.DrugId)
  8818. //修改流水
  8819. flow := models.DrugFlow{
  8820. Count: device_number,
  8821. MaxUnit: drup.MinUnit,
  8822. }
  8823. ModifyDrugFlow(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId)
  8824. }
  8825. //如果当期医嘱数量大于最后一次出库则需要出库
  8826. if device_number > count_number {
  8827. var cha_number int64
  8828. cha_number = device_number - count_number
  8829. // 根据先进先出原则,查询最先入库的批次,进行出库
  8830. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  8831. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  8832. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  8833. var stockMax int64
  8834. var stockMin int64
  8835. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  8836. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  8837. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  8838. UpdateMinNumber(lastWarehouse.ID, stockMin)
  8839. }
  8840. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  8841. //查找药品信息
  8842. //baseInfo, _ := FindBaseDrugLibRecord(advice.UserOrgId, advice.DrugId)
  8843. if err != nil {
  8844. return err
  8845. }
  8846. // 将该批次的剩余库存数量转换为拆零数量
  8847. var stock_number int64
  8848. var maxNumber int64
  8849. var minNumber int64
  8850. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  8851. //如果库存数量大于出库数据,正常出库
  8852. if stock_number >= cha_number {
  8853. //扣减库存
  8854. maxNumber = cha_number / drup.MinNumber
  8855. minNumber = cha_number % drup.MinNumber
  8856. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  8857. minNumber = maxNumber
  8858. }
  8859. if drup.MaxUnit != drup.MinUnit {
  8860. if warehouse.StockMaxNumber < maxNumber {
  8861. return errors.New("库存数量不足")
  8862. }
  8863. }
  8864. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  8865. if warehouse.StockMaxNumber < 0 {
  8866. warehouse.StockMaxNumber = 0
  8867. }
  8868. warehouse.Mtime = time.Now().Unix()
  8869. if warehouse.StockMinNumber < minNumber {
  8870. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  8871. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  8872. } else {
  8873. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  8874. }
  8875. if drup.MaxUnit != drup.MinUnit {
  8876. if warehouse.StockMaxNumber < 0 {
  8877. return errors.New("库存数量不足")
  8878. }
  8879. }
  8880. //扣减库存232332332332
  8881. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  8882. if errThree != nil {
  8883. return errThree
  8884. }
  8885. //修改出库表
  8886. info := models.DrugWarehouseOutInfo{
  8887. Count: device_number,
  8888. CountUnit: advice.PrescribingNumberUnit,
  8889. }
  8890. ModifyDrugWarehouseOutInfoOne(info, advice.DrugId, advice.PatientId, advice.RecordDate, warehouse.ID)
  8891. //修改自动出库表
  8892. detail := models.DrugAutomaticReduceDetail{
  8893. Count: device_number,
  8894. CountUnit: advice.PrescribingNumberUnit,
  8895. }
  8896. ModifyAutoMaticReduceOne(detail, advice.PatientId, advice.RecordDate, advice.DrugId, warehouse.ID)
  8897. //修改流水
  8898. flow := models.DrugFlow{
  8899. Count: device_number,
  8900. MaxUnit: advice.PrescribingNumberUnit,
  8901. }
  8902. ModifyDrugFlowOne(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId, warehouse.ID)
  8903. }
  8904. //如果库存数量小于出库数据,则需要出库到下一个批次
  8905. if stock_number < cha_number {
  8906. //修改出库表
  8907. info := models.DrugWarehouseOutInfo{
  8908. Count: cha_number,
  8909. CountUnit: advice.PrescribingNumberUnit,
  8910. }
  8911. ModifyDrugWarehouseOutInfoOne(info, advice.DrugId, advice.PatientId, advice.RecordDate, warehouse.ID)
  8912. //修改自动出库表
  8913. detail := models.DrugAutomaticReduceDetail{
  8914. Count: cha_number,
  8915. CountUnit: advice.PrescribingNumberUnit,
  8916. }
  8917. ModifyAutoMaticReduceOne(detail, advice.PatientId, advice.RecordDate, advice.DrugId, warehouse.ID)
  8918. //修改流水
  8919. flow := models.DrugFlow{
  8920. Count: cha_number,
  8921. MaxUnit: advice.PrescribingNumberUnit,
  8922. }
  8923. ModifyDrugFlowOne(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId, warehouse.ID)
  8924. druginfo := &models.XtDrugWarehouseInfo{
  8925. ID: warehouse.ID,
  8926. WarehousingId: warehouse.WarehousingId,
  8927. DrugId: warehouse.DrugId,
  8928. Number: warehouse.Number,
  8929. ProductDate: warehouse.ProductDate,
  8930. ExpiryDate: warehouse.ExpiryDate,
  8931. WarehousingCount: warehouse.WarehousingCount,
  8932. Price: warehouse.Price,
  8933. TotalPrice: warehouse.TotalPrice,
  8934. Dealer: warehouse.Dealer,
  8935. Manufacturer: warehouse.Manufacturer,
  8936. Remark: warehouse.Remark,
  8937. Ctime: warehouse.Ctime,
  8938. Mtime: warehouse.Mtime,
  8939. Status: 1,
  8940. OrgId: warehouse.OrgId,
  8941. IsReturn: warehouse.IsReturn,
  8942. WarehousingOrder: warehouse.WarehousingOrder,
  8943. Type: warehouse.Type,
  8944. RetailPrice: warehouse.RetailPrice,
  8945. RetailTotalPrice: warehouse.RetailPrice,
  8946. StockMaxNumber: 0,
  8947. StockMinNumber: 0,
  8948. BatchNumber: warehouse.BatchNumber,
  8949. MaxUnit: warehouse.MaxUnit,
  8950. WarehousingInfoId: warehouse.WarehousingInfoId,
  8951. StorehouseId: storeConfig.DrugStorehouseOut,
  8952. IsCheck: 1,
  8953. }
  8954. errThree := UpDateDrugWarehouseInfoByStock(druginfo)
  8955. if errThree != nil {
  8956. return errThree
  8957. }
  8958. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  8959. prescribingNumber_two_temp := cha_number - stock_number
  8960. overPlusNumber := float64(prescribingNumber_two_temp)
  8961. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  8962. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  8963. NewHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  8964. }
  8965. }
  8966. return
  8967. }
  8968. //采购管理耗材出库逻辑
  8969. 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) {
  8970. var deliver_number int64 = 0
  8971. var stock_number int64 = 0
  8972. var maxNumber int64 = 0
  8973. deliver_number = goods.Count
  8974. // 根据先进先出原则,查询最先入库的批次,进行出库
  8975. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  8976. // 如果没有对应的库存,则报错
  8977. warehouse, err := FindFirstWarehousingInfoByStockNight(goods.GoodId, goods.GoodTypeId, warehouse_out_id, goods.IsSource, houseConfig.StorehouseOutInfo)
  8978. if err != nil {
  8979. return err
  8980. }
  8981. stock_number = warehouse.StockCount
  8982. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  8983. if stock_number >= deliver_number {
  8984. warehouseOutInfo := &models.WarehouseOutInfo{
  8985. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  8986. WarehouseOutId: goods.WarehouseOutId,
  8987. WarehouseInfotId: warehouse.ID,
  8988. Status: 1,
  8989. Ctime: time.Now().Unix(),
  8990. Remark: goods.Remark,
  8991. OrgId: orgID,
  8992. Type: 1,
  8993. Manufacturer: goods.Manufacturer,
  8994. Dealer: goods.Dealer,
  8995. IsSys: 0,
  8996. SysRecordTime: record_time,
  8997. GoodTypeId: goods.GoodTypeId,
  8998. GoodId: goods.GoodId,
  8999. ExpiryDate: goods.ExpiryDate,
  9000. ProductDate: goods.ProductDate,
  9001. Number: warehouse.Number,
  9002. Price: goods.Price,
  9003. LicenseNumber: goods.LicenseNumber,
  9004. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  9005. StorehouseId: warehouse.StorehouseId,
  9006. IsCheck: 1,
  9007. }
  9008. warehouseOutInfo.Count = goods.Count
  9009. //添加出库单详情
  9010. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  9011. if errOne != nil {
  9012. return errOne
  9013. }
  9014. stockFlow := models.VmStockFlow{
  9015. WarehousingId: warehouse.ID,
  9016. GoodId: goods.GoodId,
  9017. Number: warehouse.Number,
  9018. LicenseNumber: goods.LicenseNumber,
  9019. Count: goods.Count,
  9020. UserOrgId: goods.OrgId,
  9021. PatientId: goods.PatientId,
  9022. SystemTime: record_time,
  9023. ConsumableType: 2,
  9024. IsSys: goods.IsSys,
  9025. WarehousingOrder: "",
  9026. WarehouseOutId: goods.WarehouseOutId,
  9027. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  9028. IsEdit: 1,
  9029. CancelStockId: 0,
  9030. CancelOrderNumber: "",
  9031. Manufacturer: goods.Manufacturer,
  9032. Dealer: goods.Dealer,
  9033. Creator: creator,
  9034. UpdateCreator: 0,
  9035. Status: 1,
  9036. Ctime: time.Now().Unix(),
  9037. Mtime: 0,
  9038. Price: warehouse.PackingPrice,
  9039. WarehousingDetailId: 0,
  9040. WarehouseOutDetailId: warehouseOutInfo.ID,
  9041. CancelOutDetailId: 0,
  9042. ProductDate: goods.ProductDate,
  9043. ExpireDate: goods.ExpiryDate,
  9044. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  9045. SupplyWarehouseId: warehouseOut.SupplyWarehouseId,
  9046. StorehouseId: warehouse.StorehouseId,
  9047. }
  9048. //创建出库流水
  9049. CreateStockFlowOne(stockFlow)
  9050. maxNumber = goods.Count
  9051. if warehouse.StockCount < maxNumber {
  9052. return errors.New("库存数量不足")
  9053. }
  9054. warehouse.StockCount = warehouse.StockCount - maxNumber
  9055. warehouse.Mtime = time.Now().Unix()
  9056. //扣减库存
  9057. errThree := UpDateWarehouseInfoByStock(&warehouse)
  9058. //出库数量增加
  9059. AddGoodSumOutCount(houseConfig.StorehouseOutInfo, warehouse.GoodId, warehouse.OrgId, goods.Count)
  9060. ModifyGoodReduceInformationSix(warehouse.GoodId, maxNumber, warehouse.OrgId, warehouse.PackingPrice)
  9061. if errThree != nil {
  9062. return errThree
  9063. }
  9064. } else {
  9065. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  9066. warehouseOutInfo := &models.WarehouseOutInfo{
  9067. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  9068. WarehouseOutId: goods.WarehouseOutId,
  9069. WarehouseInfotId: warehouse.ID,
  9070. Status: 1,
  9071. Ctime: time.Now().Unix(),
  9072. Remark: goods.Remark,
  9073. OrgId: orgID,
  9074. Type: 1,
  9075. Manufacturer: goods.Manufacturer,
  9076. Dealer: goods.Dealer,
  9077. IsSys: 0,
  9078. SysRecordTime: record_time,
  9079. GoodTypeId: goods.GoodTypeId,
  9080. GoodId: goods.GoodId,
  9081. ExpiryDate: goods.ExpiryDate,
  9082. ProductDate: goods.ProductDate,
  9083. Number: warehouse.Number,
  9084. Price: warehouse.PackingPrice,
  9085. LicenseNumber: goods.LicenseNumber,
  9086. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  9087. StorehouseId: warehouse.StorehouseId,
  9088. IsCheck: 1,
  9089. }
  9090. warehouseOutInfo.Count = stock_number
  9091. //查询是否已经生成出库单
  9092. info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  9093. if errcodes == gorm.ErrRecordNotFound {
  9094. //创建出库单
  9095. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  9096. if errOne != nil {
  9097. return errOne
  9098. }
  9099. } else {
  9100. warehouseOutInfo.Count = info.Count + stock_number
  9101. UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  9102. }
  9103. stockFlow := models.VmStockFlow{
  9104. WarehousingId: warehouse.ID,
  9105. GoodId: goods.GoodId,
  9106. Number: warehouse.Number,
  9107. LicenseNumber: goods.LicenseNumber,
  9108. Count: stock_number,
  9109. UserOrgId: goods.OrgId,
  9110. PatientId: 0,
  9111. SystemTime: record_time,
  9112. ConsumableType: 2,
  9113. IsSys: 0,
  9114. WarehousingOrder: "",
  9115. WarehouseOutId: goods.WarehouseOutId,
  9116. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  9117. IsEdit: 1,
  9118. CancelStockId: 0,
  9119. CancelOrderNumber: "",
  9120. Manufacturer: goods.Manufacturer,
  9121. Dealer: goods.Dealer,
  9122. Creator: creator,
  9123. UpdateCreator: 0,
  9124. Status: 1,
  9125. Ctime: time.Now().Unix(),
  9126. Mtime: 0,
  9127. Price: warehouse.PackingPrice,
  9128. WarehousingDetailId: 0,
  9129. WarehouseOutDetailId: warehouseOutInfo.ID,
  9130. CancelOutDetailId: 0,
  9131. ProductDate: goods.ProductDate,
  9132. ExpireDate: goods.ExpiryDate,
  9133. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  9134. SupplyWarehouseId: warehouseOut.SupplyWarehouseId,
  9135. StorehouseId: warehouse.StorehouseId,
  9136. }
  9137. //创建出库明细
  9138. CreateStockFlowOne(stockFlow)
  9139. // 出库完成后,要将该批次库存清零
  9140. ModifyGoodReduceInformationSix(warehouse.GoodId, warehouse.StockCount, warehouse.OrgId, warehouse.PackingPrice)
  9141. warehouse.StockCount = 0
  9142. warehouse.Mtime = time.Now().Unix()
  9143. errThree := UpDateWarehouseInfoByStock(&warehouse)
  9144. if errThree != nil {
  9145. return errThree
  9146. }
  9147. //出库数量增加
  9148. AddGoodSumOutCount(houseConfig.StorehouseOutInfo, warehouse.GoodId, warehouse.OrgId, warehouse.StockCount)
  9149. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  9150. goods.Count = deliver_number - stock_number
  9151. ConsumablesDeliveryNight(orgID, record_time, goods, warehouseOut, count, creator, warehouse_out_id, cancel_out_id)
  9152. }
  9153. return nil
  9154. }
  9155. func FindFirstWarehousingInfoByStockNight(good_id int64, good_type_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  9156. if is_source == 1 {
  9157. 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
  9158. }
  9159. if is_source == 2 || is_source == 0 {
  9160. 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
  9161. }
  9162. return info, err
  9163. }
  9164. // 药品出库 递归方式
  9165. 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) {
  9166. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  9167. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  9168. var deliver_number int64 = 0
  9169. var stock_number int64 = 0
  9170. var retail_price float64
  9171. if advice.CountUnit == drup.MaxUnit {
  9172. deliver_number = prescribingNumber * drup.MinNumber
  9173. formatInt := strconv.FormatInt(prescribingNumber, 10)
  9174. floatcount, _ := strconv.ParseFloat(formatInt, 64)
  9175. retail_price = floatcount * advice.Price
  9176. } else {
  9177. deliver_number = prescribingNumber
  9178. formatInt := strconv.FormatInt(prescribingNumber, 10)
  9179. floatcount, _ := strconv.ParseFloat(formatInt, 64)
  9180. retail_price = floatcount * advice.Price
  9181. }
  9182. // 根据先进先出原则,查询最先入库的批次,进行出库
  9183. // 如果没有对应的库存,则报错
  9184. //开启事物
  9185. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, houseConfig.DrugStorehouseOut)
  9186. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  9187. var stockMax int64
  9188. var stockMin int64
  9189. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  9190. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  9191. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  9192. UpdateMinNumber(lastWarehouse.ID, stockMin)
  9193. }
  9194. warehouse, err := FindLastDrugWarehousingInfoByIDSix(advice.DrugId, warehouse_out_id, advice.IsSource, houseConfig.DrugStorehouseOut)
  9195. if err != nil {
  9196. return err
  9197. }
  9198. // 将该批次的剩余库存数量转换为拆零数量
  9199. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  9200. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  9201. if stock_number >= deliver_number {
  9202. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  9203. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  9204. WarehouseOutId: advice.WarehouseOutId,
  9205. Status: 1,
  9206. Ctime: time.Now().Unix(),
  9207. Remark: advice.Remark,
  9208. OrgId: orgID,
  9209. Type: 1,
  9210. Manufacturer: advice.Manufacturer,
  9211. Dealer: warehouse.Dealer,
  9212. IsSys: 0,
  9213. SysRecordTime: advice.SysRecordTime,
  9214. DrugId: advice.DrugId,
  9215. ExpiryDate: advice.ExpiryDate,
  9216. ProductDate: advice.ProductDate,
  9217. Number: advice.Number,
  9218. BatchNumber: warehouse.BatchNumber,
  9219. Count: deliver_number,
  9220. RetailPrice: advice.RetailPrice,
  9221. Price: advice.Price,
  9222. WarehouseInfoId: warehouse.ID,
  9223. CountUnit: drup.MinUnit,
  9224. AdviceId: advice.ID,
  9225. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  9226. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  9227. StorehouseId: warehouse.StorehouseId,
  9228. IsCheck: 1,
  9229. }
  9230. warehouseOutInfo.Count = prescribingNumber
  9231. warehouseOutInfo.CountUnit = advice.CountUnit
  9232. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  9233. if errOne != nil {
  9234. return errOne
  9235. }
  9236. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  9237. drugflow := models.DrugFlow{
  9238. WarehouseOutId: advice.WarehouseOutId,
  9239. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  9240. DrugId: advice.DrugId,
  9241. Number: warehouse.Number,
  9242. ProductDate: advice.ProductDate,
  9243. ExpireDate: advice.ExpiryDate,
  9244. Count: deliver_number,
  9245. Price: warehouse.RetailPrice,
  9246. Status: 1,
  9247. Ctime: time.Now().Unix(),
  9248. UserOrgId: advice.OrgId,
  9249. Manufacturer: advice.Manufacturer,
  9250. Dealer: advice.Dealer,
  9251. BatchNumber: warehouse.BatchNumber,
  9252. MaxUnit: drup.MinUnit,
  9253. ConsumableType: 2,
  9254. IsEdit: 1,
  9255. Creator: 0,
  9256. IsSys: 0,
  9257. WarehouseOutDetailId: drugWareInfo.ID,
  9258. AdviceId: advice.ID,
  9259. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  9260. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  9261. SystemTime: advice.SysRecordTime,
  9262. StorehouseId: warehouse.StorehouseId,
  9263. WarehousingDetailId: warehouse.ID,
  9264. WarehousingId: warehouse.ID,
  9265. RetailPrice: retail_price,
  9266. LastPrice: warehouse.Price,
  9267. }
  9268. CreateDrugFlowOne(drugflow)
  9269. // 出库完成后,要减去对应批次的库存数量
  9270. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  9271. var maxNumber int64 = 0
  9272. var minNumber int64 = 0
  9273. if advice.CountUnit == drup.MinUnit {
  9274. maxNumber = prescribingNumber / drup.MinNumber
  9275. minNumber = prescribingNumber % drup.MinNumber
  9276. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  9277. minNumber = maxNumber
  9278. }
  9279. } else {
  9280. maxNumber = prescribingNumber
  9281. }
  9282. if warehouse.StockMaxNumber < maxNumber {
  9283. return errors.New("库存数量不足")
  9284. }
  9285. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  9286. if warehouse.StockMaxNumber < 0 {
  9287. warehouse.StockMaxNumber = 0
  9288. }
  9289. warehouse.Mtime = time.Now().Unix()
  9290. if warehouse.StockMinNumber < minNumber {
  9291. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  9292. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  9293. } else {
  9294. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  9295. }
  9296. if warehouse.StockMaxNumber < 0 {
  9297. return errors.New("库存数量不足")
  9298. }
  9299. if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
  9300. warehouse.StockMinNumber = 0
  9301. }
  9302. if warehouse.StockMaxNumber > 0 {
  9303. var sum_count int64
  9304. sum_count = maxNumber * drup.MinNumber
  9305. ModifyDrugReduceInformationSix(warehouse.DrugId, sum_count, warehouse.OrgId, warehouse.RetailPrice)
  9306. //出库数量增加
  9307. AddDrugSumOutCount(houseConfig.DrugStorehouseOut, sum_count, warehouse.OrgId, warehouse.DrugId)
  9308. }
  9309. if warehouse.StockMinNumber > 0 {
  9310. ModifyDrugReduceInformationSix(warehouse.DrugId, minNumber, warehouse.OrgId, warehouse.RetailPrice)
  9311. //出库数量增加
  9312. AddDrugSumOutCount(houseConfig.DrugStorehouseOut, minNumber, warehouse.OrgId, warehouse.DrugId)
  9313. }
  9314. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  9315. if errThree != nil {
  9316. return errThree
  9317. }
  9318. return nil
  9319. } else {
  9320. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  9321. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  9322. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  9323. WarehouseOutId: advice.WarehouseOutId,
  9324. Status: 1,
  9325. Ctime: time.Now().Unix(),
  9326. Remark: advice.Remark,
  9327. OrgId: orgID,
  9328. Type: 1,
  9329. Manufacturer: advice.Manufacturer,
  9330. Dealer: warehouse.Dealer,
  9331. IsSys: 0,
  9332. SysRecordTime: advice.SysRecordTime,
  9333. DrugId: advice.DrugId,
  9334. ExpiryDate: warehouse.ExpiryDate,
  9335. ProductDate: warehouse.ProductDate,
  9336. Number: warehouse.Number,
  9337. BatchNumber: warehouse.BatchNumber,
  9338. Count: stock_number,
  9339. RetailPrice: advice.RetailPrice,
  9340. Price: advice.Price,
  9341. WarehouseInfoId: warehouse.ID,
  9342. CountUnit: drup.MinUnit,
  9343. AdviceId: advice.ID,
  9344. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  9345. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  9346. StorehouseId: warehouse.StorehouseId,
  9347. IsCheck: 1,
  9348. }
  9349. warehouseOutInfo.Count = stock_number
  9350. //warehouseOutInfo.CountUnit = advice.CountUnit
  9351. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  9352. if errOne != nil {
  9353. return errOne
  9354. }
  9355. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  9356. drugflow := models.DrugFlow{
  9357. WarehouseOutId: advice.WarehouseOutId,
  9358. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  9359. DrugId: advice.DrugId,
  9360. Number: warehouse.Number,
  9361. ProductDate: advice.ProductDate,
  9362. ExpireDate: advice.ExpiryDate,
  9363. Count: stock_number,
  9364. Price: warehouse.RetailPrice,
  9365. Status: 1,
  9366. Ctime: time.Now().Unix(),
  9367. UserOrgId: advice.OrgId,
  9368. Manufacturer: advice.Manufacturer,
  9369. Dealer: advice.Dealer,
  9370. BatchNumber: warehouse.BatchNumber,
  9371. MaxUnit: drup.MinUnit,
  9372. ConsumableType: 2,
  9373. IsEdit: 1,
  9374. Creator: 0,
  9375. IsSys: 0,
  9376. WarehouseOutDetailId: drugWareInfo.ID,
  9377. AdviceId: advice.ID,
  9378. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  9379. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  9380. SystemTime: advice.SysRecordTime,
  9381. StorehouseId: warehouse.StorehouseId,
  9382. WarehousingDetailId: warehouse.ID,
  9383. WarehousingId: warehouse.ID,
  9384. RetailPrice: retail_price,
  9385. LastPrice: warehouse.Price,
  9386. }
  9387. CreateDrugFlowOne(drugflow)
  9388. if warehouse.StockMaxNumber > 0 {
  9389. var sum_count int64
  9390. sum_count = warehouse.StockMaxNumber * drup.MinNumber
  9391. ModifyDrugReduceInformationSix(warehouse.DrugId, sum_count, warehouse.OrgId, warehouse.RetailPrice)
  9392. AddDrugSumOutCount(houseConfig.DrugStorehouseOut, sum_count, warehouse.OrgId, warehouse.DrugId)
  9393. }
  9394. if warehouse.StockMinNumber > 0 {
  9395. ModifyDrugReduceInformationSix(warehouse.DrugId, warehouse.StockMinNumber, warehouse.OrgId, warehouse.RetailPrice)
  9396. AddDrugSumOutCount(houseConfig.DrugStorehouseOut, warehouse.StockMinNumber, warehouse.OrgId, warehouse.DrugId)
  9397. }
  9398. // 出库完成后,要将该批次库存清零
  9399. warehouse.StockMaxNumber = 0
  9400. warehouse.StockMinNumber = 0
  9401. warehouse.Mtime = time.Now().Unix()
  9402. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  9403. if errThree != nil {
  9404. return errThree
  9405. }
  9406. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  9407. prescribingNumber_two_temp := deliver_number - stock_number
  9408. advice.CountUnit = drup.MinUnit
  9409. AutoDrugDeliverInfoNight(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, warehouse_out_id, cancel_out_id)
  9410. }
  9411. return
  9412. }
  9413. //调拨出库逻辑
  9414. func ConsumablesDeliveryTwelve(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64, warehouseInfo *models.WarehousingInfo) (err error) {
  9415. var deliver_number int64 = 0
  9416. var stock_number int64 = 0
  9417. var maxNumber int64 = 0
  9418. deliver_number = goods.Count
  9419. // 根据先进先出原则,查询最先入库的批次,进行出库
  9420. // 如果没有对应的库存,则报错
  9421. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  9422. if err != nil {
  9423. return err
  9424. }
  9425. stock_number = warehouse.StockCount
  9426. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  9427. if stock_number >= deliver_number {
  9428. warehouseOutInfo := &models.WarehouseOutInfo{
  9429. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  9430. WarehouseOutId: goods.WarehouseOutId,
  9431. WarehouseInfotId: warehouse.ID,
  9432. Status: 1,
  9433. Ctime: time.Now().Unix(),
  9434. Remark: goods.Remark,
  9435. OrgId: orgID,
  9436. Type: 1,
  9437. Manufacturer: warehouse.Manufacturer,
  9438. Dealer: warehouse.Dealer,
  9439. IsSys: 0,
  9440. SysRecordTime: record_time,
  9441. GoodTypeId: warehouse.GoodTypeId,
  9442. GoodId: warehouse.GoodId,
  9443. ExpiryDate: warehouse.ExpiryDate,
  9444. ProductDate: warehouse.ProductDate,
  9445. Number: warehouse.Number,
  9446. Price: warehouse.PackingPrice,
  9447. LicenseNumber: warehouse.LicenseNumber,
  9448. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9449. StorehouseId: goods.StorehouseId,
  9450. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  9451. IsCheck: 1,
  9452. }
  9453. warehouseOutInfo.Count = goods.Count
  9454. //添加出库单详情
  9455. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  9456. if errOne != nil {
  9457. return errOne
  9458. }
  9459. stockFlow := models.VmStockFlow{
  9460. WarehousingId: warehouse.ID,
  9461. GoodId: goods.GoodId,
  9462. Number: warehouse.Number,
  9463. LicenseNumber: goods.LicenseNumber,
  9464. Count: goods.Count,
  9465. UserOrgId: goods.OrgId,
  9466. PatientId: goods.PatientId,
  9467. SystemTime: record_time,
  9468. ConsumableType: 12,
  9469. IsSys: goods.IsSys,
  9470. WarehousingOrder: "",
  9471. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  9472. WarehouseOutId: goods.WarehouseOutId,
  9473. IsEdit: 1,
  9474. CancelStockId: 0,
  9475. CancelOrderNumber: "",
  9476. Manufacturer: warehouse.Manufacturer,
  9477. Dealer: warehouse.Dealer,
  9478. Creator: creator,
  9479. UpdateCreator: 0,
  9480. Status: 1,
  9481. Ctime: time.Now().Unix(),
  9482. Mtime: 0,
  9483. Price: warehouse.PackingPrice,
  9484. WarehousingDetailId: 0,
  9485. WarehouseOutDetailId: warehouseOutInfo.ID,
  9486. CancelOutDetailId: 0,
  9487. ProductDate: warehouse.ProductDate,
  9488. ExpireDate: warehouse.ExpiryDate,
  9489. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9490. StorehouseId: warehouse.StorehouseId,
  9491. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  9492. }
  9493. //创建出库流水
  9494. CreateStockFlowOne(stockFlow)
  9495. maxNumber = goods.Count
  9496. if warehouse.StockCount < maxNumber {
  9497. return errors.New("库存数量不足")
  9498. }
  9499. warehouse.StockCount = warehouse.StockCount - maxNumber
  9500. warehouse.Mtime = time.Now().Unix()
  9501. //扣减库存
  9502. errThree := UpDateWarehouseInfoByStock(&warehouse)
  9503. //入库
  9504. warehouseInfo := &models.WarehousingInfo{
  9505. WarehousingOrder: warehouseInfo.WarehousingOrder,
  9506. WarehousingId: warehouseInfo.WarehousingId,
  9507. GoodId: warehouse.GoodId,
  9508. Number: warehouse.Number,
  9509. GoodTypeId: warehouse.GoodTypeId,
  9510. ProductDate: warehouse.ProductDate,
  9511. ExpiryDate: warehouse.ExpiryDate,
  9512. WarehousingCount: goods.Count,
  9513. Price: warehouse.Price,
  9514. TotalPrice: 0,
  9515. Status: 1,
  9516. Ctime: warehouse.Ctime,
  9517. Remark: goods.Remark,
  9518. OrgId: warehouse.OrgId,
  9519. Type: 1,
  9520. Manufacturer: warehouse.Manufacturer,
  9521. StockCount: goods.Count,
  9522. Dealer: warehouse.Dealer,
  9523. LicenseNumber: "",
  9524. PackingPrice: warehouse.PackingPrice,
  9525. StorehouseId: warehouseInfo.StorehouseId,
  9526. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  9527. IsCheck: 1,
  9528. }
  9529. AddWarehosingInfo(warehouseInfo)
  9530. stockInfoFlow := models.VmStockFlow{
  9531. WarehousingId: warehouseInfo.WarehousingId,
  9532. GoodId: goods.GoodId,
  9533. Number: warehouse.Number,
  9534. LicenseNumber: goods.LicenseNumber,
  9535. Count: goods.Count,
  9536. UserOrgId: goods.OrgId,
  9537. PatientId: goods.PatientId,
  9538. SystemTime: record_time,
  9539. ConsumableType: 13,
  9540. IsSys: goods.IsSys,
  9541. WarehousingOrder: warehouseInfo.WarehousingOrder,
  9542. WarehouseOutId: 0,
  9543. WarehouseOutOrderNumber: "",
  9544. IsEdit: 1,
  9545. CancelStockId: 0,
  9546. CancelOrderNumber: "",
  9547. Manufacturer: warehouse.Manufacturer,
  9548. Dealer: warehouse.Dealer,
  9549. Creator: creator,
  9550. UpdateCreator: 0,
  9551. Status: 1,
  9552. Ctime: time.Now().Unix(),
  9553. Mtime: 0,
  9554. Price: warehouse.Price,
  9555. WarehousingDetailId: 0,
  9556. WarehouseOutDetailId: warehouseOutInfo.ID,
  9557. CancelOutDetailId: 0,
  9558. ProductDate: warehouse.ProductDate,
  9559. ExpireDate: warehouse.ExpiryDate,
  9560. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9561. StorehouseId: warehouseInfo.StorehouseId,
  9562. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  9563. }
  9564. //创建入库流水
  9565. CreateStockFlowOne(stockInfoFlow)
  9566. if errThree != nil {
  9567. return errThree
  9568. }
  9569. } else {
  9570. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  9571. warehouseOutInfo := &models.WarehouseOutInfo{
  9572. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  9573. WarehouseOutId: goods.WarehouseOutId,
  9574. WarehouseInfotId: warehouse.ID,
  9575. Status: 1,
  9576. Ctime: time.Now().Unix(),
  9577. Remark: goods.Remark,
  9578. OrgId: orgID,
  9579. Type: 1,
  9580. Manufacturer: warehouse.Manufacturer,
  9581. Dealer: warehouse.Dealer,
  9582. IsSys: 0,
  9583. SysRecordTime: record_time,
  9584. GoodTypeId: warehouse.GoodTypeId,
  9585. GoodId: warehouse.GoodId,
  9586. ExpiryDate: warehouse.ExpiryDate,
  9587. ProductDate: warehouse.ProductDate,
  9588. Number: warehouse.Number,
  9589. Price: warehouse.PackingPrice,
  9590. LicenseNumber: warehouse.LicenseNumber,
  9591. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9592. StorehouseId: warehouse.StorehouseId,
  9593. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  9594. IsCheck: 1,
  9595. }
  9596. warehouseOutInfo.Count = warehouse.StockCount
  9597. //查询是否已经生成出库单
  9598. info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  9599. if errcodes == gorm.ErrRecordNotFound {
  9600. //创建出库单
  9601. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  9602. if errOne != nil {
  9603. return errOne
  9604. }
  9605. } else {
  9606. warehouseOutInfo.Count = info.Count + stock_number
  9607. UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  9608. }
  9609. stockFlow := models.VmStockFlow{
  9610. WarehousingId: warehouse.ID,
  9611. GoodId: goods.GoodId,
  9612. Number: warehouse.Number,
  9613. LicenseNumber: goods.LicenseNumber,
  9614. Count: warehouse.StockCount,
  9615. UserOrgId: goods.OrgId,
  9616. PatientId: 0,
  9617. SystemTime: record_time,
  9618. ConsumableType: 12,
  9619. IsSys: 0,
  9620. WarehousingOrder: "",
  9621. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  9622. WarehouseOutId: goods.WarehouseOutId,
  9623. IsEdit: 1,
  9624. CancelStockId: 0,
  9625. CancelOrderNumber: "",
  9626. Manufacturer: warehouse.Manufacturer,
  9627. Dealer: warehouse.Dealer,
  9628. Creator: creator,
  9629. UpdateCreator: 0,
  9630. Status: 1,
  9631. Ctime: time.Now().Unix(),
  9632. Mtime: 0,
  9633. Price: warehouse.PackingPrice,
  9634. WarehousingDetailId: 0,
  9635. WarehouseOutDetailId: warehouseOutInfo.ID,
  9636. CancelOutDetailId: 0,
  9637. ProductDate: warehouse.ProductDate,
  9638. ExpireDate: warehouse.ExpiryDate,
  9639. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9640. StorehouseId: goods.StorehouseId,
  9641. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  9642. }
  9643. //创建出库明细
  9644. CreateStockFlowOne(stockFlow)
  9645. //入库
  9646. warehouseInfo := &models.WarehousingInfo{
  9647. WarehousingOrder: warehouseInfo.WarehousingOrder,
  9648. WarehousingId: warehouseInfo.WarehousingId,
  9649. GoodId: warehouse.GoodId,
  9650. Number: warehouse.Number,
  9651. GoodTypeId: warehouse.GoodTypeId,
  9652. ProductDate: warehouse.ProductDate,
  9653. ExpiryDate: warehouse.ExpiryDate,
  9654. WarehousingCount: warehouse.StockCount,
  9655. Price: warehouse.Price,
  9656. TotalPrice: 0,
  9657. Status: 1,
  9658. Ctime: warehouse.Ctime,
  9659. Remark: goods.Remark,
  9660. OrgId: warehouse.OrgId,
  9661. Type: 1,
  9662. Manufacturer: warehouse.Manufacturer,
  9663. StockCount: warehouse.StockCount,
  9664. Dealer: warehouse.Dealer,
  9665. LicenseNumber: "",
  9666. PackingPrice: warehouse.PackingPrice,
  9667. StorehouseId: warehouseInfo.StorehouseId,
  9668. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  9669. IsCheck: 1,
  9670. }
  9671. AddWarehosingInfo(warehouseInfo)
  9672. stockInfoFlow := models.VmStockFlow{
  9673. WarehousingId: warehouseInfo.WarehousingId,
  9674. GoodId: goods.GoodId,
  9675. Number: warehouse.Number,
  9676. LicenseNumber: goods.LicenseNumber,
  9677. Count: warehouse.StockCount,
  9678. UserOrgId: goods.OrgId,
  9679. PatientId: goods.PatientId,
  9680. SystemTime: record_time,
  9681. ConsumableType: 13,
  9682. IsSys: goods.IsSys,
  9683. WarehousingOrder: warehouseInfo.WarehousingOrder,
  9684. WarehouseOutId: warehouseOut.ID,
  9685. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  9686. IsEdit: 1,
  9687. CancelStockId: 0,
  9688. CancelOrderNumber: "",
  9689. Manufacturer: warehouse.Manufacturer,
  9690. Dealer: warehouse.Dealer,
  9691. Creator: creator,
  9692. UpdateCreator: 0,
  9693. Status: 1,
  9694. Ctime: time.Now().Unix(),
  9695. Mtime: 0,
  9696. Price: warehouse.Price,
  9697. WarehousingDetailId: 0,
  9698. WarehouseOutDetailId: warehouseOutInfo.ID,
  9699. CancelOutDetailId: 0,
  9700. ProductDate: warehouse.ProductDate,
  9701. ExpireDate: warehouse.ExpiryDate,
  9702. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9703. StorehouseId: warehouseInfo.StorehouseId,
  9704. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  9705. }
  9706. //创建出库流水
  9707. CreateStockFlowOne(stockInfoFlow)
  9708. // 出库完成后,要将该批次库存清零
  9709. warehouse.StockCount = 0
  9710. warehouse.Mtime = time.Now().Unix()
  9711. errThree := UpDateWarehouseInfoByStock(&warehouse)
  9712. if errThree != nil {
  9713. return errThree
  9714. }
  9715. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  9716. goods.Count = deliver_number - stock_number
  9717. ConsumablesDeliveryTwelve(orgID, record_time, goods, warehouseOut, count, creator, warehouseInfo)
  9718. }
  9719. return nil
  9720. }
  9721. func AddWarehosingInfo(info *models.WarehousingInfo) error {
  9722. err = XTWriteDB().Create(&info).Error
  9723. return err
  9724. }
  9725. // 药品出库 递归方式
  9726. func AutoDrugDeliverInfoTwelve(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo, drugWarehouse *models.DrugWarehouseInfo) (err error) {
  9727. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  9728. var deliver_number int64 = 0
  9729. var stock_number int64 = 0
  9730. if advice.CountUnit == drup.MaxUnit {
  9731. deliver_number = prescribingNumber * drup.MinNumber
  9732. } else {
  9733. deliver_number = prescribingNumber
  9734. }
  9735. // 根据先进先出原则,查询最先入库的批次,进行出库
  9736. // 如果没有对应的库存,则报错
  9737. //开启事物
  9738. storeConfig, _ := GetAllStoreHouseConfig(orgID)
  9739. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  9740. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  9741. var stockMax int64
  9742. var stockMin int64
  9743. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  9744. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  9745. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  9746. UpdateMinNumber(lastWarehouse.ID, stockMin)
  9747. }
  9748. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  9749. if err != nil {
  9750. return err
  9751. }
  9752. // 将该批次的剩余库存数量转换为拆零数量
  9753. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  9754. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  9755. if stock_number >= deliver_number {
  9756. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  9757. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  9758. WarehouseOutId: advice.WarehouseOutId,
  9759. Status: 1,
  9760. Ctime: time.Now().Unix(),
  9761. Remark: advice.Remark,
  9762. OrgId: orgID,
  9763. Type: 1,
  9764. Manufacturer: warehouse.Manufacturer,
  9765. Dealer: warehouse.Dealer,
  9766. IsSys: 12,
  9767. SysRecordTime: advice.Ctime,
  9768. DrugId: advice.DrugId,
  9769. ExpiryDate: warehouse.ExpiryDate,
  9770. ProductDate: warehouse.ProductDate,
  9771. Number: warehouse.Number,
  9772. BatchNumber: warehouse.BatchNumber,
  9773. Count: deliver_number,
  9774. RetailPrice: warehouse.RetailPrice,
  9775. Price: warehouse.RetailPrice,
  9776. WarehouseInfoId: warehouse.ID,
  9777. CountUnit: drup.MinUnit,
  9778. AdviceId: advice.ID,
  9779. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9780. StorehouseId: advice.StorehouseId,
  9781. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  9782. IsCheck: 1,
  9783. }
  9784. warehouseOutInfo.Count = prescribingNumber
  9785. warehouseOutInfo.CountUnit = advice.CountUnit
  9786. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  9787. if errOne != nil {
  9788. return errOne
  9789. }
  9790. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  9791. drugflow := models.DrugFlow{
  9792. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  9793. WarehouseOutId: advice.WarehouseOutId,
  9794. DrugId: advice.DrugId,
  9795. Number: warehouse.Number,
  9796. ProductDate: warehouse.ProductDate,
  9797. ExpireDate: warehouse.ExpiryDate,
  9798. Count: deliver_number,
  9799. Price: warehouse.RetailPrice,
  9800. Status: 1,
  9801. Ctime: time.Now().Unix(),
  9802. UserOrgId: advice.OrgId,
  9803. Manufacturer: warehouse.Manufacturer,
  9804. Dealer: warehouse.Dealer,
  9805. BatchNumber: warehouse.BatchNumber,
  9806. MaxUnit: drup.MinUnit,
  9807. ConsumableType: 12,
  9808. IsEdit: 1,
  9809. Creator: 0,
  9810. IsSys: 12,
  9811. WarehouseOutDetailId: drugWareInfo.ID,
  9812. AdviceId: advice.ID,
  9813. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9814. StorehouseId: advice.StorehouseId,
  9815. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  9816. WarehousingDetailId: warehouse.ID,
  9817. }
  9818. CreateDrugFlowOne(drugflow)
  9819. // 出库完成后,要减去对应批次的库存数量
  9820. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  9821. var maxNumber int64 = 0
  9822. var minNumber int64 = 0
  9823. if advice.CountUnit == drup.MinUnit {
  9824. maxNumber = prescribingNumber / drup.MinNumber
  9825. minNumber = prescribingNumber % drup.MinNumber
  9826. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  9827. minNumber = maxNumber
  9828. }
  9829. } else {
  9830. maxNumber = prescribingNumber
  9831. }
  9832. if warehouse.StockMaxNumber < maxNumber {
  9833. return errors.New("库存数量不足")
  9834. }
  9835. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  9836. warehouse.Mtime = time.Now().Unix()
  9837. if warehouse.StockMinNumber < minNumber {
  9838. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  9839. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  9840. } else {
  9841. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  9842. }
  9843. if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
  9844. warehouse.StockMinNumber = 0
  9845. }
  9846. if warehouse.StockMaxNumber < 0 {
  9847. return errors.New("库存数量不足")
  9848. }
  9849. ////扣减库存
  9850. //if(warehouse.StockMaxNumber > 0){
  9851. // var sum_count int64
  9852. // sum_count = maxNumber * drup.MinNumber
  9853. // ModifyDrugReduceInformationSix(warehouse.DrugId,sum_count,warehouse.OrgId,warehouse.RetailPrice)
  9854. //}
  9855. //if(warehouse.StockMinNumber > 0){
  9856. // var sum_count int64
  9857. // sum_count = minNumber
  9858. // ModifyDrugReduceInformationSix(warehouse.DrugId,sum_count,warehouse.OrgId,warehouse.RetailPrice)
  9859. //}
  9860. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  9861. if errThree != nil {
  9862. return errThree
  9863. }
  9864. //入库
  9865. drugInfo := models.DrugWarehouseInfo{
  9866. WarehousingOrder: drugWarehouse.WarehousingOrder,
  9867. WarehousingId: drugWarehouse.WarehousingId,
  9868. DrugId: warehouse.DrugId,
  9869. Number: warehouse.Number,
  9870. ProductDate: warehouse.ProductDate,
  9871. ExpiryDate: warehouse.ExpiryDate,
  9872. WarehousingCount: deliver_number,
  9873. Price: warehouse.RetailPrice,
  9874. TotalPrice: warehouse.TotalPrice,
  9875. Status: 1,
  9876. Ctime: warehouse.Ctime,
  9877. Remark: "",
  9878. OrgId: warehouse.OrgId,
  9879. Type: 13,
  9880. Manufacturer: warehouse.Manufacturer,
  9881. Dealer: warehouse.Dealer,
  9882. StockMaxNumber: 0,
  9883. StockMinNumber: deliver_number,
  9884. BatchNumber: warehouse.BatchNumber,
  9885. MaxUnit: drup.MinUnit,
  9886. MinUnit: drup.MinUnit,
  9887. RetailPrice: warehouse.RetailPrice,
  9888. StorehouseId: drugWarehouse.StorehouseId,
  9889. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  9890. IsCheck: 1,
  9891. }
  9892. CreateDrugWarehouseInfo(drugInfo)
  9893. flow := models.DrugFlow{
  9894. WarehousingId: drugWarehouse.WarehousingId,
  9895. DrugId: warehouse.DrugId,
  9896. Number: warehouse.Number,
  9897. BatchNumber: warehouse.BatchNumber,
  9898. Count: deliver_number,
  9899. UserOrgId: warehouse.OrgId,
  9900. PatientId: 0,
  9901. SystemTime: 0,
  9902. ConsumableType: 13,
  9903. IsSys: 13,
  9904. WarehousingOrder: drugWarehouse.WarehousingOrder,
  9905. WarehouseOutId: 0,
  9906. WarehouseOutOrderNumber: "",
  9907. IsEdit: 0,
  9908. CancelStockId: 0,
  9909. CancelOrderNumber: "",
  9910. Manufacturer: warehouse.Manufacturer,
  9911. Dealer: warehouse.Dealer,
  9912. Creator: 0,
  9913. UpdateCreator: 0,
  9914. Status: 1,
  9915. Ctime: time.Now().Unix(),
  9916. Mtime: 0,
  9917. Price: warehouse.RetailPrice,
  9918. WarehousingDetailId: warehouse.ID,
  9919. WarehouseOutDetailId: 0,
  9920. CancelOutDetailId: 0,
  9921. ExpireDate: warehouse.ExpiryDate,
  9922. ProductDate: warehouse.ProductDate,
  9923. MaxUnit: drup.MinUnit,
  9924. MinUnit: drup.MinUnit,
  9925. StockMaxNumber: 0,
  9926. StockMinNumber: deliver_number,
  9927. StorehouseId: drugWarehouse.StorehouseId,
  9928. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  9929. }
  9930. CreateDrugFlowOne(flow)
  9931. return nil
  9932. } else {
  9933. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  9934. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  9935. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  9936. WarehouseOutId: advice.WarehouseOutId,
  9937. Status: 1,
  9938. Ctime: time.Now().Unix(),
  9939. Remark: advice.Remark,
  9940. OrgId: orgID,
  9941. Type: 1,
  9942. Manufacturer: advice.Manufacturer,
  9943. Dealer: warehouse.Dealer,
  9944. IsSys: 12,
  9945. SysRecordTime: advice.Ctime,
  9946. DrugId: advice.DrugId,
  9947. ExpiryDate: warehouse.ExpiryDate,
  9948. ProductDate: warehouse.ProductDate,
  9949. Number: warehouse.Number,
  9950. BatchNumber: warehouse.BatchNumber,
  9951. Count: stock_number,
  9952. RetailPrice: advice.RetailPrice,
  9953. Price: warehouse.RetailPrice,
  9954. WarehouseInfoId: warehouse.ID,
  9955. CountUnit: drup.MinUnit,
  9956. AdviceId: advice.ID,
  9957. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9958. StorehouseId: advice.StorehouseId,
  9959. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  9960. IsCheck: 1,
  9961. }
  9962. warehouseOutInfo.Count = stock_number
  9963. //warehouseOutInfo.CountUnit = advice.CountUnit
  9964. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  9965. if errOne != nil {
  9966. return errOne
  9967. }
  9968. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  9969. drugflow := models.DrugFlow{
  9970. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  9971. WarehouseOutId: advice.WarehouseOutId,
  9972. DrugId: advice.DrugId,
  9973. Number: warehouse.Number,
  9974. ProductDate: warehouse.ProductDate,
  9975. ExpireDate: warehouse.ExpiryDate,
  9976. Count: stock_number,
  9977. Price: warehouse.RetailPrice,
  9978. Status: 1,
  9979. Ctime: time.Now().Unix(),
  9980. UserOrgId: advice.OrgId,
  9981. Manufacturer: warehouse.Manufacturer,
  9982. Dealer: warehouse.Dealer,
  9983. BatchNumber: warehouse.BatchNumber,
  9984. MaxUnit: drup.MinUnit,
  9985. ConsumableType: 12,
  9986. IsEdit: 1,
  9987. Creator: 0,
  9988. IsSys: 12,
  9989. WarehouseOutDetailId: drugWareInfo.ID,
  9990. AdviceId: advice.ID,
  9991. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9992. StorehouseId: advice.StorehouseId,
  9993. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  9994. WarehousingDetailId: warehouse.ID,
  9995. }
  9996. CreateDrugFlowOne(drugflow)
  9997. // 出库完成后,要将该批次库存清零
  9998. //入库
  9999. drugInfo := models.DrugWarehouseInfo{
  10000. WarehousingOrder: drugWarehouse.WarehousingOrder,
  10001. WarehousingId: drugWarehouse.WarehousingId,
  10002. DrugId: warehouse.DrugId,
  10003. Number: warehouse.Number,
  10004. ProductDate: warehouse.ProductDate,
  10005. ExpiryDate: warehouse.ExpiryDate,
  10006. WarehousingCount: stock_number,
  10007. Price: warehouse.RetailPrice,
  10008. TotalPrice: warehouse.TotalPrice,
  10009. Status: 1,
  10010. Ctime: warehouse.Ctime,
  10011. Remark: "",
  10012. OrgId: warehouse.OrgId,
  10013. Type: 13,
  10014. Manufacturer: warehouse.Manufacturer,
  10015. Dealer: warehouse.Dealer,
  10016. StockMaxNumber: 0,
  10017. StockMinNumber: stock_number,
  10018. RetailTotalPrice: 0,
  10019. BatchNumber: warehouse.BatchNumber,
  10020. MaxUnit: drup.MinUnit,
  10021. MinUnit: drup.MinUnit,
  10022. RetailPrice: warehouse.RetailPrice,
  10023. StorehouseId: drugWarehouse.StorehouseId,
  10024. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  10025. }
  10026. CreateDrugWarehouseInfo(drugInfo)
  10027. flow := models.DrugFlow{
  10028. WarehousingId: drugWarehouse.WarehousingId,
  10029. DrugId: warehouse.DrugId,
  10030. Number: warehouse.Number,
  10031. BatchNumber: warehouse.BatchNumber,
  10032. Count: stock_number,
  10033. UserOrgId: warehouse.OrgId,
  10034. PatientId: 0,
  10035. SystemTime: 0,
  10036. ConsumableType: 13,
  10037. IsSys: 13,
  10038. WarehousingOrder: drugWarehouse.WarehousingOrder,
  10039. WarehouseOutId: 0,
  10040. WarehouseOutOrderNumber: "",
  10041. IsEdit: 0,
  10042. CancelStockId: 0,
  10043. CancelOrderNumber: "",
  10044. Manufacturer: warehouse.Manufacturer,
  10045. Dealer: warehouse.Dealer,
  10046. Creator: 0,
  10047. UpdateCreator: 0,
  10048. Status: 1,
  10049. Ctime: time.Now().Unix(),
  10050. Mtime: 0,
  10051. Price: warehouse.RetailPrice,
  10052. WarehousingDetailId: warehouse.ID,
  10053. WarehouseOutDetailId: 0,
  10054. CancelOutDetailId: 0,
  10055. ExpireDate: warehouse.ExpiryDate,
  10056. ProductDate: warehouse.ProductDate,
  10057. MaxUnit: drup.MinUnit,
  10058. MinUnit: drup.MinUnit,
  10059. StockMaxNumber: 0,
  10060. StockMinNumber: stock_number,
  10061. StorehouseId: drugWarehouse.StorehouseId,
  10062. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  10063. }
  10064. CreateDrugFlowOne(flow)
  10065. warehouse.StockMaxNumber = 0
  10066. warehouse.StockMinNumber = 0
  10067. warehouse.Mtime = time.Now().Unix()
  10068. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  10069. if errThree != nil {
  10070. return errThree
  10071. }
  10072. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  10073. prescribingNumber_two_temp := deliver_number - stock_number
  10074. advice.CountUnit = drup.MinUnit
  10075. AutoDrugDeliverInfoTwelve(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, drugWarehouse)
  10076. }
  10077. return
  10078. }
  10079. // 药品手动出库 递归方式
  10080. func AutoDrugDeliverInfoTwenty(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  10081. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  10082. var deliver_number int64 = 0
  10083. var stock_number int64 = 0
  10084. if advice.CountUnit == drup.MaxUnit {
  10085. deliver_number = prescribingNumber * drup.MinNumber
  10086. } else {
  10087. deliver_number = prescribingNumber
  10088. }
  10089. // 根据先进先出原则,查询最先入库的批次,进行出库
  10090. // 如果没有对应的库存,则报错
  10091. //开启事物
  10092. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  10093. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  10094. var stockMax int64
  10095. var stockMin int64
  10096. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  10097. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  10098. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  10099. UpdateMinNumber(lastWarehouse.ID, stockMin)
  10100. }
  10101. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  10102. if err != nil {
  10103. return err
  10104. }
  10105. // 将该批次的剩余库存数量转换为拆零数量
  10106. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  10107. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  10108. if stock_number >= deliver_number {
  10109. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  10110. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  10111. WarehouseOutId: warehouseout.ID,
  10112. Status: 1,
  10113. Ctime: time.Now().Unix(),
  10114. Remark: advice.Remark,
  10115. OrgId: orgID,
  10116. Type: 1,
  10117. Manufacturer: advice.Manufacturer,
  10118. Dealer: warehouse.Dealer,
  10119. IsSys: 0,
  10120. SysRecordTime: advice.Ctime,
  10121. DrugId: advice.DrugId,
  10122. ExpiryDate: advice.ExpiryDate,
  10123. ProductDate: advice.ProductDate,
  10124. Number: advice.Number,
  10125. BatchNumber: warehouse.BatchNumber,
  10126. Count: deliver_number,
  10127. RetailPrice: advice.RetailPrice,
  10128. Price: warehouse.RetailPrice,
  10129. WarehouseInfoId: warehouse.ID,
  10130. CountUnit: drup.MinUnit,
  10131. AdviceId: advice.ID,
  10132. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  10133. StorehouseId: advice.StorehouseId,
  10134. AdminUserId: advice.AdminUserId,
  10135. IsCheck: 1,
  10136. }
  10137. if warehouse.RetailPrice == 0 {
  10138. warehouseOutInfo.Price = advice.Price
  10139. }
  10140. warehouseOutInfo.Count = prescribingNumber
  10141. warehouseOutInfo.CountUnit = advice.CountUnit
  10142. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  10143. if errOne != nil {
  10144. return errOne
  10145. }
  10146. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  10147. drugflow := models.DrugFlow{
  10148. WarehouseOutId: warehouseout.ID,
  10149. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  10150. DrugId: advice.DrugId,
  10151. Number: warehouse.Number,
  10152. ProductDate: advice.ProductDate,
  10153. ExpireDate: advice.ExpiryDate,
  10154. Count: deliver_number,
  10155. Price: warehouse.RetailPrice,
  10156. Status: 1,
  10157. Ctime: time.Now().Unix(),
  10158. UserOrgId: advice.OrgId,
  10159. Manufacturer: advice.Manufacturer,
  10160. Dealer: advice.Dealer,
  10161. BatchNumber: warehouse.BatchNumber,
  10162. MaxUnit: drup.MinUnit,
  10163. ConsumableType: 2,
  10164. IsEdit: 1,
  10165. Creator: 0,
  10166. IsSys: 0,
  10167. WarehouseOutDetailId: drugWareInfo.ID,
  10168. AdviceId: advice.ID,
  10169. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  10170. StorehouseId: advice.StorehouseId,
  10171. AdminUserId: advice.AdminUserId,
  10172. }
  10173. if warehouse.RetailPrice == 0 {
  10174. drugflow.Price = advice.Price
  10175. }
  10176. CreateDrugFlowOne(drugflow)
  10177. // 出库完成后,要减去对应批次的库存数量
  10178. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  10179. var maxNumber int64 = 0
  10180. var minNumber int64 = 0
  10181. //var stock_max_number int64 = 0
  10182. //stock_max_number = warehouse.StockMaxNumber
  10183. maxNumber = deliver_number / drup.MinNumber
  10184. minNumber = deliver_number % drup.MinNumber
  10185. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  10186. minNumber = maxNumber
  10187. }
  10188. if drup.MaxUnit != drup.MinUnit {
  10189. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  10190. return errors.New("库存数量不足")
  10191. }
  10192. }
  10193. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  10194. if warehouse.StockMaxNumber < 0 {
  10195. warehouse.StockMaxNumber = 0
  10196. }
  10197. if warehouse.StockMinNumber < 0 {
  10198. warehouse.StockMinNumber = 0
  10199. }
  10200. warehouse.Mtime = time.Now().Unix()
  10201. if warehouse.StockMinNumber < minNumber {
  10202. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  10203. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  10204. } else {
  10205. if minNumber > 0 {
  10206. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  10207. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  10208. } else {
  10209. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  10210. }
  10211. }
  10212. if minNumber == 0 && maxNumber != 1 {
  10213. if warehouse.StockMinNumber > 0 {
  10214. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  10215. }
  10216. }
  10217. }
  10218. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  10219. if (warehouse.StockMinNumber - deliver_number) > 0 {
  10220. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  10221. //if warehouse.StockMinNumber == 0 {
  10222. // warehouse.StockMaxNumber = stock_max_number
  10223. //}
  10224. }
  10225. }
  10226. if drup.MaxUnit != drup.MinUnit {
  10227. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  10228. return errors.New("库存数量不足")
  10229. }
  10230. }
  10231. if warehouse.StockMinNumber <= 0 {
  10232. warehouse.StockMinNumber = 0
  10233. }
  10234. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  10235. if errThree != nil {
  10236. return errThree
  10237. }
  10238. //查询默认仓库
  10239. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  10240. //查询默认仓库剩余多少库存
  10241. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  10242. var sum_count int64
  10243. var sum_in_count int64
  10244. for _, it := range list {
  10245. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  10246. if it.MaxUnit == baseDrug.MaxUnit {
  10247. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  10248. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  10249. sum_count += it.StockMaxNumber + it.StockMinNumber
  10250. sum_in_count += it.WarehousingCount
  10251. }
  10252. }
  10253. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  10254. return nil
  10255. } else {
  10256. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  10257. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  10258. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  10259. WarehouseOutId: warehouseout.ID,
  10260. Status: 1,
  10261. Ctime: time.Now().Unix(),
  10262. Remark: advice.Remark,
  10263. OrgId: orgID,
  10264. Type: 1,
  10265. Manufacturer: advice.Manufacturer,
  10266. Dealer: warehouse.Dealer,
  10267. IsSys: 0,
  10268. SysRecordTime: advice.Ctime,
  10269. DrugId: advice.DrugId,
  10270. ExpiryDate: warehouse.ExpiryDate,
  10271. ProductDate: warehouse.ProductDate,
  10272. Number: warehouse.Number,
  10273. BatchNumber: warehouse.BatchNumber,
  10274. Count: stock_number,
  10275. RetailPrice: advice.RetailPrice,
  10276. Price: warehouse.RetailPrice,
  10277. WarehouseInfoId: warehouse.ID,
  10278. CountUnit: drup.MinUnit,
  10279. AdviceId: advice.ID,
  10280. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  10281. StorehouseId: advice.StorehouseId,
  10282. AdminUserId: advice.AdminUserId,
  10283. IsCheck: 1,
  10284. }
  10285. if warehouse.RetailPrice == 0 {
  10286. warehouseOutInfo.Price = advice.Price
  10287. }
  10288. warehouseOutInfo.Count = stock_number
  10289. //warehouseOutInfo.CountUnit = advice.CountUnit
  10290. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  10291. if errOne != nil {
  10292. return errOne
  10293. }
  10294. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  10295. drugflow := models.DrugFlow{
  10296. WarehouseOutId: warehouseout.ID,
  10297. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  10298. DrugId: advice.DrugId,
  10299. Number: warehouse.Number,
  10300. ProductDate: advice.ProductDate,
  10301. ExpireDate: advice.ExpiryDate,
  10302. Count: stock_number,
  10303. Price: warehouse.RetailPrice,
  10304. Status: 1,
  10305. Ctime: time.Now().Unix(),
  10306. UserOrgId: advice.OrgId,
  10307. Manufacturer: advice.Manufacturer,
  10308. Dealer: advice.Dealer,
  10309. BatchNumber: warehouse.BatchNumber,
  10310. MaxUnit: drup.MinUnit,
  10311. ConsumableType: 2,
  10312. IsEdit: 1,
  10313. Creator: 0,
  10314. IsSys: 0,
  10315. WarehouseOutDetailId: drugWareInfo.ID,
  10316. AdviceId: advice.ID,
  10317. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  10318. StorehouseId: advice.StorehouseId,
  10319. AdminUserId: advice.AdminUserId,
  10320. }
  10321. if warehouse.RetailPrice == 0 {
  10322. drugflow.Price = advice.Price
  10323. }
  10324. CreateDrugFlowOne(drugflow)
  10325. // 出库完成后,要将该批次库存清零
  10326. //扣减库存
  10327. warehouse.StockMaxNumber = 0
  10328. warehouse.StockMinNumber = 0
  10329. warehouse.Mtime = time.Now().Unix()
  10330. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  10331. //查询默认仓库
  10332. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  10333. //查询默认仓库剩余多少库存
  10334. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  10335. var sum_count int64
  10336. var sum_in_count int64
  10337. for _, it := range list {
  10338. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  10339. if it.MaxUnit == baseDrug.MaxUnit {
  10340. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  10341. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  10342. sum_count += it.StockMaxNumber + it.StockMinNumber
  10343. sum_in_count += it.WarehousingCount
  10344. }
  10345. }
  10346. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  10347. if errThree != nil {
  10348. return errThree
  10349. }
  10350. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  10351. prescribingNumber_two_temp := deliver_number - stock_number
  10352. advice.CountUnit = drup.MinUnit
  10353. AutoDrugDeliverInfoTwenty(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  10354. }
  10355. return
  10356. }
  10357. // 药品手动出库 递归方式
  10358. func AutoDrugDeliverInfoTwentyOne(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  10359. //开事务
  10360. //tx := XTWriteDB().Begin()
  10361. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  10362. var deliver_number int64 = 0
  10363. var stock_number int64 = 0
  10364. if advice.CountUnit == drup.MaxUnit {
  10365. deliver_number = prescribingNumber * drup.MinNumber
  10366. } else {
  10367. deliver_number = prescribingNumber
  10368. }
  10369. // 根据先进先出原则,查询最先入库的批次,进行出库
  10370. // 如果没有对应的库存,则报错
  10371. //开启事物
  10372. storeConfig, _ := GetAllStoreHouseConfig(orgID)
  10373. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  10374. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  10375. var stockMax int64
  10376. var stockMin int64
  10377. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  10378. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  10379. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  10380. UpdateMinNumber(lastWarehouse.ID, stockMin)
  10381. }
  10382. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  10383. if err != nil {
  10384. return err
  10385. }
  10386. // 将该批次的剩余库存数量转换为拆零数量
  10387. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  10388. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  10389. if stock_number >= deliver_number {
  10390. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  10391. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  10392. WarehouseOutId: warehouseout.ID,
  10393. Status: 1,
  10394. Ctime: time.Now().Unix(),
  10395. Remark: advice.Remark,
  10396. OrgId: orgID,
  10397. Type: 1,
  10398. Manufacturer: advice.Manufacturer,
  10399. Dealer: warehouse.Dealer,
  10400. IsSys: 0,
  10401. SysRecordTime: advice.Ctime,
  10402. DrugId: advice.DrugId,
  10403. ExpiryDate: advice.ExpiryDate,
  10404. ProductDate: advice.ProductDate,
  10405. Number: advice.Number,
  10406. BatchNumber: warehouse.BatchNumber,
  10407. Count: deliver_number,
  10408. RetailPrice: advice.RetailPrice,
  10409. Price: advice.Price,
  10410. WarehouseInfoId: warehouse.ID,
  10411. CountUnit: drup.MinUnit,
  10412. AdviceId: advice.ID,
  10413. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  10414. StorehouseId: advice.StorehouseId,
  10415. AdminUserId: advice.AdminUserId,
  10416. LastPrice: warehouse.Price,
  10417. StockCount: advice.StockCount,
  10418. IsCheck: 1,
  10419. }
  10420. warehouseOutInfo.Count = prescribingNumber
  10421. warehouseOutInfo.CountUnit = advice.CountUnit
  10422. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  10423. drugflow := models.DrugFlow{
  10424. WarehouseOutId: warehouseout.ID,
  10425. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  10426. DrugId: advice.DrugId,
  10427. Number: warehouse.Number,
  10428. ProductDate: advice.ProductDate,
  10429. ExpireDate: advice.ExpiryDate,
  10430. Count: deliver_number,
  10431. Price: advice.Price,
  10432. Status: 1,
  10433. Ctime: time.Now().Unix(),
  10434. UserOrgId: advice.OrgId,
  10435. Manufacturer: advice.Manufacturer,
  10436. Dealer: advice.Dealer,
  10437. BatchNumber: warehouse.BatchNumber,
  10438. MaxUnit: drup.MinUnit,
  10439. ConsumableType: 2,
  10440. IsEdit: 1,
  10441. Creator: 0,
  10442. IsSys: 0,
  10443. WarehouseOutDetailId: drugWareInfo.ID,
  10444. AdviceId: advice.ID,
  10445. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  10446. StorehouseId: advice.StorehouseId,
  10447. AdminUserId: advice.AdminUserId,
  10448. LastPrice: warehouse.Price,
  10449. StockCount: advice.StockCount,
  10450. }
  10451. if warehouse.RetailPrice == 0 {
  10452. drugflow.Price = advice.Price
  10453. }
  10454. CreateDrugFlowOne(drugflow)
  10455. // 出库完成后,要减去对应批次的库存数量
  10456. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  10457. var maxNumber int64 = 0
  10458. var minNumber int64 = 0
  10459. //var stock_max_number int64 = 0
  10460. //stock_max_number = warehouse.StockMaxNumber
  10461. maxNumber = deliver_number / drup.MinNumber
  10462. minNumber = deliver_number % drup.MinNumber
  10463. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  10464. minNumber = maxNumber
  10465. }
  10466. if drup.MaxUnit != drup.MinUnit {
  10467. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  10468. return errors.New("库存数量不足")
  10469. }
  10470. }
  10471. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  10472. if warehouse.StockMaxNumber < 0 {
  10473. warehouse.StockMaxNumber = 0
  10474. }
  10475. if warehouse.StockMinNumber < 0 {
  10476. warehouse.StockMinNumber = 0
  10477. }
  10478. warehouse.Mtime = time.Now().Unix()
  10479. if warehouse.StockMinNumber < minNumber {
  10480. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  10481. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  10482. } else {
  10483. if minNumber > 0 {
  10484. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  10485. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  10486. } else {
  10487. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  10488. }
  10489. }
  10490. if minNumber == 0 && maxNumber != 1 {
  10491. if warehouse.StockMinNumber > 0 {
  10492. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  10493. }
  10494. }
  10495. }
  10496. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  10497. if (warehouse.StockMinNumber - deliver_number) > 0 {
  10498. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  10499. //if warehouse.StockMinNumber == 0 {
  10500. // warehouse.StockMaxNumber = stock_max_number
  10501. //}
  10502. }
  10503. }
  10504. if drup.MaxUnit != drup.MinUnit {
  10505. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  10506. return errors.New("库存数量不足")
  10507. }
  10508. }
  10509. if warehouse.StockMinNumber <= 0 {
  10510. warehouse.StockMinNumber = 0
  10511. }
  10512. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  10513. fmt.Println(errThree)
  10514. //if errThree != nil {
  10515. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  10516. // tx.Rollback()
  10517. //} else {
  10518. // tx.Commit()
  10519. //}
  10520. //查询默认仓库
  10521. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  10522. //查询默认仓库剩余多少库存
  10523. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  10524. var sum_count int64
  10525. var sum_in_count int64
  10526. for _, it := range list {
  10527. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  10528. if it.MaxUnit == baseDrug.MaxUnit {
  10529. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  10530. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  10531. sum_count += it.StockMaxNumber + it.StockMinNumber
  10532. sum_in_count += it.WarehousingCount
  10533. }
  10534. }
  10535. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  10536. return nil
  10537. } else {
  10538. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  10539. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  10540. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  10541. WarehouseOutId: warehouseout.ID,
  10542. Status: 1,
  10543. Ctime: time.Now().Unix(),
  10544. Remark: advice.Remark,
  10545. OrgId: orgID,
  10546. Type: 1,
  10547. Manufacturer: advice.Manufacturer,
  10548. Dealer: warehouse.Dealer,
  10549. IsSys: 0,
  10550. SysRecordTime: advice.Ctime,
  10551. DrugId: advice.DrugId,
  10552. ExpiryDate: warehouse.ExpiryDate,
  10553. ProductDate: warehouse.ProductDate,
  10554. Number: warehouse.Number,
  10555. BatchNumber: warehouse.BatchNumber,
  10556. Count: stock_number,
  10557. RetailPrice: advice.RetailPrice,
  10558. Price: advice.Price,
  10559. WarehouseInfoId: warehouse.ID,
  10560. CountUnit: drup.MinUnit,
  10561. AdviceId: advice.ID,
  10562. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  10563. StorehouseId: advice.StorehouseId,
  10564. AdminUserId: advice.AdminUserId,
  10565. LastPrice: warehouse.Price,
  10566. StockCount: advice.StockCount,
  10567. IsCheck: 1,
  10568. }
  10569. warehouseOutInfo.Count = stock_number
  10570. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  10571. drugflow := models.DrugFlow{
  10572. WarehouseOutId: warehouseout.ID,
  10573. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  10574. DrugId: advice.DrugId,
  10575. Number: warehouse.Number,
  10576. ProductDate: advice.ProductDate,
  10577. ExpireDate: advice.ExpiryDate,
  10578. Count: stock_number,
  10579. Price: advice.Price,
  10580. Status: 1,
  10581. Ctime: time.Now().Unix(),
  10582. UserOrgId: advice.OrgId,
  10583. Manufacturer: advice.Manufacturer,
  10584. Dealer: advice.Dealer,
  10585. BatchNumber: warehouse.BatchNumber,
  10586. MaxUnit: drup.MinUnit,
  10587. ConsumableType: 2,
  10588. IsEdit: 1,
  10589. Creator: 0,
  10590. IsSys: 0,
  10591. WarehouseOutDetailId: drugWareInfo.ID,
  10592. AdviceId: advice.ID,
  10593. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  10594. StorehouseId: advice.StorehouseId,
  10595. AdminUserId: advice.AdminUserId,
  10596. LastPrice: warehouse.Price,
  10597. StockCount: advice.StockCount,
  10598. }
  10599. CreateDrugFlowOne(drugflow)
  10600. // 出库完成后,要将该批次库存清零
  10601. //扣减库存
  10602. warehouse.StockMaxNumber = 0
  10603. warehouse.StockMinNumber = 0
  10604. warehouse.Mtime = time.Now().Unix()
  10605. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  10606. fmt.Println(errThree)
  10607. //if errThree != nil {
  10608. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  10609. // tx.Rollback()
  10610. //} else {
  10611. // tx.Commit()
  10612. //}
  10613. //查询默认仓库
  10614. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  10615. //查询默认仓库剩余多少库存
  10616. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  10617. var sum_count int64
  10618. var sum_in_count int64
  10619. for _, it := range list {
  10620. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  10621. if it.MaxUnit == baseDrug.MaxUnit {
  10622. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  10623. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  10624. sum_count += it.StockMaxNumber + it.StockMinNumber
  10625. sum_in_count += it.WarehousingCount
  10626. }
  10627. }
  10628. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  10629. if errThree != nil {
  10630. return errThree
  10631. }
  10632. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  10633. prescribingNumber_two_temp := deliver_number - stock_number
  10634. advice.CountUnit = drup.MinUnit
  10635. AutoDrugDeliverInfoTwentyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  10636. }
  10637. return
  10638. }
  10639. //耗材手动出库
  10640. func ConsumablesDeliveryThirty(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64) (err error) {
  10641. var deliver_number int64 = 0
  10642. var stock_number int64 = 0
  10643. var maxNumber int64 = 0
  10644. deliver_number = goods.Count
  10645. // 根据先进先出原则,查询最先入库的批次,进行出库
  10646. // 如果没有对应的库存,则报错
  10647. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  10648. if err != nil {
  10649. goodErrcode := models.XtGoodErrcode{
  10650. UserOrgId: goods.OrgId,
  10651. Errcode: "查询库存信息报错",
  10652. GoodId: goods.GoodId,
  10653. Status: 1,
  10654. Ctime: time.Now().Unix(),
  10655. Mtime: 0,
  10656. Count: 0,
  10657. StockCount: 0,
  10658. Creater: creator,
  10659. BatchNumberId: warehouse.ID,
  10660. WarehouseOutId: 0,
  10661. }
  10662. CreateGoodErrcode(goodErrcode)
  10663. return err
  10664. }
  10665. stock_number = warehouse.StockCount
  10666. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  10667. if stock_number >= deliver_number {
  10668. maxNumber = goods.Count
  10669. warehouse.StockCount = warehouse.StockCount - maxNumber
  10670. warehouse.Mtime = time.Now().Unix()
  10671. //扣减库存
  10672. errThree := UpDateWarehouseInfoByStock(&warehouse)
  10673. if errThree != nil {
  10674. goodErrcode := models.XtGoodErrcode{
  10675. UserOrgId: goods.OrgId,
  10676. Errcode: "扣减库存失败",
  10677. GoodId: goods.GoodId,
  10678. Status: 1,
  10679. Ctime: time.Now().Unix(),
  10680. Mtime: 0,
  10681. Count: 0,
  10682. StockCount: 0,
  10683. Creater: creator,
  10684. BatchNumberId: warehouse.ID,
  10685. WarehouseOutId: 0,
  10686. }
  10687. CreateGoodErrcode(goodErrcode)
  10688. return errThree
  10689. }
  10690. //查询剩余库存
  10691. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID)
  10692. var sum_count int64
  10693. for _, item := range goodList {
  10694. sum_count += item.StockCount
  10695. }
  10696. warehouseOutInfo := &models.WarehouseOutInfo{
  10697. WarehouseInfotId: warehouse.ID,
  10698. }
  10699. //添加入库单详情ID
  10700. errOne := UpdatedSigleWarehouseOutInfo(goods.ID, warehouseOutInfo)
  10701. if errOne != nil {
  10702. goodErrcode := models.XtGoodErrcode{
  10703. UserOrgId: goods.OrgId,
  10704. Errcode: "添加入库单详情ID",
  10705. GoodId: goods.GoodId,
  10706. Status: 1,
  10707. Ctime: time.Now().Unix(),
  10708. Mtime: 0,
  10709. Count: 0,
  10710. StockCount: 0,
  10711. Creater: creator,
  10712. BatchNumberId: warehouse.ID,
  10713. WarehouseOutId: 0,
  10714. }
  10715. CreateGoodErrcode(goodErrcode)
  10716. return errOne
  10717. }
  10718. stockFlow := models.VmStockFlow{
  10719. WarehousingId: warehouse.ID,
  10720. GoodId: goods.GoodId,
  10721. Number: warehouse.Number,
  10722. LicenseNumber: goods.LicenseNumber,
  10723. Count: goods.Count,
  10724. UserOrgId: goods.OrgId,
  10725. PatientId: goods.PatientId,
  10726. SystemTime: record_time,
  10727. ConsumableType: 2,
  10728. IsSys: goods.IsSys,
  10729. WarehousingOrder: warehouse.WarehousingOrder,
  10730. WarehouseOutId: warehouseOut.ID,
  10731. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  10732. IsEdit: 1,
  10733. CancelStockId: 0,
  10734. CancelOrderNumber: "",
  10735. Manufacturer: goods.Manufacturer,
  10736. Dealer: goods.Dealer,
  10737. Creator: creator,
  10738. UpdateCreator: 0,
  10739. Status: 1,
  10740. Ctime: time.Now().Unix(),
  10741. Mtime: 0,
  10742. Price: warehouse.PackingPrice, //零售价
  10743. WarehousingDetailId: warehouse.ID,
  10744. WarehouseOutDetailId: goods.ID,
  10745. CancelOutDetailId: 0,
  10746. ProductDate: goods.ProductDate,
  10747. ExpireDate: goods.ExpiryDate,
  10748. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  10749. StorehouseId: warehouse.StorehouseId,
  10750. AdminUserId: goods.AdminUserId,
  10751. BuyPrice: warehouse.Price, //进货价
  10752. StockCount: goods.StockCount,
  10753. BatchNumberCount: warehouse.StockCount - goods.Count,
  10754. IsCheck: 1,
  10755. OverCount: sum_count,
  10756. RegisterNumber: goods.RegisterNumber,
  10757. }
  10758. //创建出库流水
  10759. errflow := CreateStockFlowOne(stockFlow)
  10760. if errflow != nil {
  10761. goodErrcode := models.XtGoodErrcode{
  10762. UserOrgId: goods.OrgId,
  10763. Errcode: "创建流水报错",
  10764. GoodId: goods.GoodId,
  10765. Status: 1,
  10766. Ctime: time.Now().Unix(),
  10767. Mtime: 0,
  10768. Count: 0,
  10769. StockCount: 0,
  10770. Creater: creator,
  10771. BatchNumberId: warehouse.ID,
  10772. WarehouseOutId: 0,
  10773. }
  10774. CreateGoodErrcode(goodErrcode)
  10775. }
  10776. //添加入库ID
  10777. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, goods.OrgId, sum_count)
  10778. if errWarehouse != nil {
  10779. goodErrcode := models.XtGoodErrcode{
  10780. UserOrgId: goods.OrgId,
  10781. Errcode: "添加入库ID报错",
  10782. GoodId: goods.GoodId,
  10783. Status: 1,
  10784. Ctime: time.Now().Unix(),
  10785. Mtime: 0,
  10786. Count: 0,
  10787. StockCount: 0,
  10788. Creater: creator,
  10789. BatchNumberId: warehouse.ID,
  10790. WarehouseOutId: 0,
  10791. }
  10792. CreateGoodErrcode(goodErrcode)
  10793. }
  10794. //更新出库数量
  10795. errsumcode := ModifyGoodSumCount(goods.StorehouseId, goods.Count, goods.OrgId, goods.GoodId)
  10796. if errsumcode != nil {
  10797. goodErrcode := models.XtGoodErrcode{
  10798. UserOrgId: goods.OrgId,
  10799. Errcode: "更新出库数量报错",
  10800. GoodId: goods.GoodId,
  10801. Status: 1,
  10802. Ctime: time.Now().Unix(),
  10803. Mtime: 0,
  10804. Count: 0,
  10805. StockCount: 0,
  10806. Creater: creator,
  10807. BatchNumberId: warehouse.ID,
  10808. WarehouseOutId: 0,
  10809. }
  10810. CreateGoodErrcode(goodErrcode)
  10811. }
  10812. if warehouse.StockCount < maxNumber {
  10813. goodErrcode := models.XtGoodErrcode{
  10814. UserOrgId: goods.OrgId,
  10815. Errcode: "出库接口库存数量不足",
  10816. GoodId: goods.GoodId,
  10817. Status: 1,
  10818. Ctime: time.Now().Unix(),
  10819. Mtime: 0,
  10820. Count: 0,
  10821. StockCount: 0,
  10822. Creater: creator,
  10823. BatchNumberId: warehouse.ID,
  10824. WarehouseOutId: 0,
  10825. }
  10826. CreateGoodErrcode(goodErrcode)
  10827. return errors.New("库存数量不足")
  10828. }
  10829. return nil
  10830. } else {
  10831. // 出库完成后,要将该批次库存清零
  10832. warehouse.StockCount = 0
  10833. warehouse.Mtime = time.Now().Unix()
  10834. errThree := UpDateWarehouseInfoByStock(&warehouse)
  10835. if errThree != nil {
  10836. goodErrcode := models.XtGoodErrcode{
  10837. UserOrgId: goods.OrgId,
  10838. Errcode: "扣减库存报错",
  10839. GoodId: goods.GoodId,
  10840. Status: 1,
  10841. Ctime: time.Now().Unix(),
  10842. Mtime: 0,
  10843. Count: 0,
  10844. StockCount: 0,
  10845. Creater: creator,
  10846. BatchNumberId: warehouse.ID,
  10847. WarehouseOutId: 0,
  10848. }
  10849. CreateGoodErrcode(goodErrcode)
  10850. return errThree
  10851. }
  10852. //查询剩余库存
  10853. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID)
  10854. var sum_count int64
  10855. for _, item := range goodList {
  10856. sum_count += item.StockCount
  10857. }
  10858. stockFlow := models.VmStockFlow{
  10859. WarehousingId: warehouse.ID,
  10860. GoodId: goods.GoodId,
  10861. Number: warehouse.Number,
  10862. LicenseNumber: goods.LicenseNumber,
  10863. Count: stock_number,
  10864. UserOrgId: goods.OrgId,
  10865. PatientId: goods.PatientId,
  10866. SystemTime: record_time,
  10867. ConsumableType: 2,
  10868. IsSys: goods.IsSys,
  10869. WarehousingOrder: warehouse.WarehousingOrder,
  10870. WarehouseOutId: warehouseOut.ID,
  10871. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  10872. IsEdit: 1,
  10873. CancelStockId: 0,
  10874. CancelOrderNumber: "",
  10875. Manufacturer: goods.Manufacturer,
  10876. Dealer: goods.Dealer,
  10877. Creator: creator,
  10878. UpdateCreator: 0,
  10879. Status: 1,
  10880. Ctime: time.Now().Unix(),
  10881. Mtime: 0,
  10882. Price: warehouse.PackingPrice, //零售价
  10883. WarehousingDetailId: warehouse.ID,
  10884. WarehouseOutDetailId: goods.ID,
  10885. CancelOutDetailId: 0,
  10886. ProductDate: goods.ProductDate,
  10887. ExpireDate: goods.ExpiryDate,
  10888. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  10889. StorehouseId: warehouse.StorehouseId,
  10890. AdminUserId: goods.AdminUserId,
  10891. BuyPrice: warehouse.Price, //进货价
  10892. StockCount: goods.StockCount,
  10893. BatchNumberCount: 0,
  10894. IsCheck: 1,
  10895. OverCount: sum_count,
  10896. RegisterNumber: goods.RegisterNumber,
  10897. }
  10898. //创建出库流水
  10899. errflow := CreateStockFlowOne(stockFlow)
  10900. if errflow != nil {
  10901. goodErrcode := models.XtGoodErrcode{
  10902. UserOrgId: goods.OrgId,
  10903. Errcode: "创建出库流水报错",
  10904. GoodId: goods.GoodId,
  10905. Status: 1,
  10906. Ctime: time.Now().Unix(),
  10907. Mtime: 0,
  10908. Count: 0,
  10909. StockCount: 0,
  10910. Creater: creator,
  10911. BatchNumberId: warehouse.ID,
  10912. WarehouseOutId: 0,
  10913. }
  10914. CreateGoodErrcode(goodErrcode)
  10915. }
  10916. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, goods.OrgId, sum_count)
  10917. if errWarehouse != nil {
  10918. goodErrcode := models.XtGoodErrcode{
  10919. UserOrgId: goods.OrgId,
  10920. Errcode: "添加入库ID报错",
  10921. GoodId: goods.GoodId,
  10922. Status: 1,
  10923. Ctime: time.Now().Unix(),
  10924. Mtime: 0,
  10925. Count: 0,
  10926. StockCount: 0,
  10927. Creater: creator,
  10928. BatchNumberId: warehouse.ID,
  10929. WarehouseOutId: 0,
  10930. }
  10931. CreateGoodErrcode(goodErrcode)
  10932. }
  10933. //更新出库数量
  10934. errcodecout := ModifyGoodSumCount(goods.StorehouseId, stock_number, goods.OrgId, goods.GoodId)
  10935. if errcodecout != nil {
  10936. goodErrcode := models.XtGoodErrcode{
  10937. UserOrgId: goods.OrgId,
  10938. Errcode: "更新出库数量报错",
  10939. GoodId: goods.GoodId,
  10940. Status: 1,
  10941. Ctime: time.Now().Unix(),
  10942. Mtime: 0,
  10943. Count: 0,
  10944. StockCount: 0,
  10945. Creater: creator,
  10946. BatchNumberId: warehouse.ID,
  10947. WarehouseOutId: 0,
  10948. }
  10949. CreateGoodErrcode(goodErrcode)
  10950. }
  10951. }
  10952. goods.Count = deliver_number - stock_number
  10953. ConsumablesDeliveryThirty(orgID, record_time, goods, warehouseOut, count, creator)
  10954. return nil
  10955. }
  10956. // 药品手动出库 递归方式
  10957. func AutoDrugDeliverInfoFourtyOne(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  10958. fmt.Println("出库数量", prescribingNumber)
  10959. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  10960. var deliver_number int64 = 0
  10961. var stock_number int64 = 0
  10962. var retail_price float64
  10963. if advice.CountUnit == drup.MaxUnit {
  10964. deliver_number = prescribingNumber * drup.MinNumber
  10965. formatInt := strconv.FormatInt(prescribingNumber, 10)
  10966. floatcount, _ := strconv.ParseFloat(formatInt, 64)
  10967. retail_price = floatcount * advice.Price
  10968. } else {
  10969. deliver_number = prescribingNumber
  10970. formatInt := strconv.FormatInt(prescribingNumber, 10)
  10971. floatcount, _ := strconv.ParseFloat(formatInt, 64)
  10972. retail_price = floatcount * advice.Price
  10973. }
  10974. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  10975. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  10976. var stockMax int64
  10977. var stockMin int64
  10978. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  10979. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  10980. //fmt.Println("stockmax", stockMax)
  10981. //fmt.Println("stockMin", stockMin)
  10982. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  10983. UpdateMinNumber(lastWarehouse.ID, stockMin)
  10984. }
  10985. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  10986. if err != nil {
  10987. return err
  10988. }
  10989. // 将该批次的剩余库存数量转换为拆零数量
  10990. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  10991. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  10992. }
  10993. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  10994. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  10995. }
  10996. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  10997. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  10998. }
  10999. fmt.Println("stock_number", stock_number)
  11000. fmt.Println("deliver_number", deliver_number)
  11001. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  11002. if stock_number >= deliver_number {
  11003. // 出库完成后,要减去对应批次的库存数量
  11004. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  11005. var maxNumber int64 = 0
  11006. var minNumber int64 = 0
  11007. maxNumber = deliver_number / drup.MinNumber
  11008. minNumber = deliver_number % drup.MinNumber
  11009. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  11010. minNumber = maxNumber
  11011. }
  11012. if drup.MaxUnit != drup.MinUnit {
  11013. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  11014. return errors.New("库存数量不足")
  11015. }
  11016. }
  11017. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  11018. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  11019. }
  11020. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  11021. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  11022. }
  11023. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  11024. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  11025. }
  11026. if warehouse.StockMaxNumber < 0 {
  11027. warehouse.StockMaxNumber = 0
  11028. }
  11029. if warehouse.StockMinNumber < 0 {
  11030. warehouse.StockMinNumber = 0
  11031. }
  11032. warehouse.Mtime = time.Now().Unix()
  11033. if warehouse.StockMinNumber < minNumber {
  11034. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  11035. if warehouse.MaxUnit != warehouse.MinUnit {
  11036. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  11037. }
  11038. } else {
  11039. //fmt.Println("maxNumber23323323332322323w9", maxNumber)
  11040. //fmt.Println("maxNumber23323323332322323w9", minNumber)
  11041. if minNumber > 0 {
  11042. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  11043. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  11044. } else {
  11045. if (warehouse.StockMinNumber - minNumber) >= 0 {
  11046. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  11047. }
  11048. }
  11049. }
  11050. if minNumber == 0 && maxNumber != 1 {
  11051. if warehouse.StockMinNumber > 0 {
  11052. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  11053. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  11054. }
  11055. }
  11056. }
  11057. }
  11058. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  11059. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  11060. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  11061. }
  11062. }
  11063. if drup.MaxUnit != drup.MinUnit {
  11064. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  11065. return errors.New("库存数量不足")
  11066. }
  11067. }
  11068. if warehouse.StockMinNumber <= 0 {
  11069. warehouse.StockMinNumber = 0
  11070. }
  11071. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  11072. if errThree != nil {
  11073. return errors.New("扣减库存失败")
  11074. }
  11075. //查询该药品剩余库存
  11076. infolist, _ := FindDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId)
  11077. var over_count int64
  11078. for _, its := range infolist {
  11079. if its.MaxUnit == drup.MaxUnit {
  11080. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  11081. }
  11082. over_count += its.StockMaxNumber + its.StockMinNumber
  11083. }
  11084. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  11085. drugflow := models.DrugFlow{
  11086. WarehouseOutId: warehouseout.ID,
  11087. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  11088. DrugId: advice.DrugId,
  11089. Number: warehouse.Number,
  11090. ProductDate: advice.ProductDate,
  11091. ExpireDate: advice.ExpiryDate,
  11092. Count: deliver_number,
  11093. Price: warehouse.RetailPrice,
  11094. Status: 1,
  11095. Ctime: time.Now().Unix(),
  11096. UserOrgId: advice.OrgId,
  11097. Manufacturer: advice.Manufacturer,
  11098. Dealer: advice.Dealer,
  11099. BatchNumber: warehouse.BatchNumber,
  11100. MaxUnit: drup.MinUnit,
  11101. ConsumableType: 2,
  11102. IsEdit: 1,
  11103. Creator: 0,
  11104. IsSys: 0,
  11105. WarehouseOutDetailId: drugWareInfo.ID,
  11106. AdviceId: advice.ID,
  11107. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  11108. StorehouseId: advice.StorehouseId,
  11109. AdminUserId: advice.AdminUserId,
  11110. LastPrice: warehouse.Price,
  11111. StockCount: advice.StockCount,
  11112. WarehousingDetailId: warehouse.ID,
  11113. OverCount: over_count,
  11114. RetailPrice: retail_price, //手动出库出库价格
  11115. WarehousingId: warehouse.ID,
  11116. }
  11117. if warehouse.RetailPrice == 0 {
  11118. drugflow.Price = advice.Price
  11119. }
  11120. CreateDrugFlowOne(drugflow)
  11121. //更新出库id
  11122. UpdateDrugWarehouseOutInfo(advice.ID, warehouse.ID, advice.OrgId, over_count)
  11123. return nil
  11124. } else {
  11125. stock_number_int := strconv.FormatInt(stock_number, 10)
  11126. stock_number_int_count, _ := strconv.ParseFloat(stock_number_int, 64)
  11127. retail_price = stock_number_int_count * advice.Price
  11128. //扣减库存
  11129. warehouse.StockMaxNumber = 0
  11130. warehouse.StockMinNumber = 0
  11131. warehouse.Mtime = time.Now().Unix()
  11132. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  11133. if errThree != nil {
  11134. return errThree
  11135. }
  11136. //查询该药品剩余库存
  11137. infolist, _ := FindDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId)
  11138. var over_count int64
  11139. for _, its := range infolist {
  11140. if its.MaxUnit == drup.MaxUnit {
  11141. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  11142. }
  11143. over_count += its.StockMaxNumber + its.StockMinNumber
  11144. }
  11145. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  11146. drugflow := models.DrugFlow{
  11147. WarehouseOutId: warehouseout.ID,
  11148. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  11149. DrugId: advice.DrugId,
  11150. Number: warehouse.Number,
  11151. ProductDate: advice.ProductDate,
  11152. ExpireDate: advice.ExpiryDate,
  11153. Count: stock_number,
  11154. Price: warehouse.RetailPrice,
  11155. Status: 1,
  11156. Ctime: time.Now().Unix(),
  11157. UserOrgId: advice.OrgId,
  11158. Manufacturer: advice.Manufacturer,
  11159. Dealer: advice.Dealer,
  11160. BatchNumber: warehouse.BatchNumber,
  11161. MaxUnit: drup.MinUnit,
  11162. ConsumableType: 2,
  11163. IsEdit: 1,
  11164. Creator: 0,
  11165. IsSys: 0,
  11166. WarehouseOutDetailId: drugWareInfo.ID,
  11167. AdviceId: advice.ID,
  11168. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  11169. StorehouseId: advice.StorehouseId,
  11170. AdminUserId: advice.AdminUserId,
  11171. LastPrice: warehouse.Price,
  11172. StockCount: advice.StockCount,
  11173. OverCount: over_count,
  11174. RetailPrice: retail_price, //手动出库出库价格
  11175. WarehousingId: warehouse.ID,
  11176. WarehousingDetailId: warehouse.ID,
  11177. }
  11178. CreateDrugFlowOne(drugflow)
  11179. //更新出库id
  11180. UpdateDrugWarehouseOutInfo(advice.ID, warehouse.ID, advice.OrgId, over_count)
  11181. // 出库完成后,要将该批次库存清零
  11182. prescribingNumber_two_temp := deliver_number - stock_number
  11183. advice.CountUnit = drup.MinUnit
  11184. AutoDrugDeliverInfoFourtyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  11185. }
  11186. return
  11187. }
  11188. func UpdateDrugWarehouseOutInfo(id int64, warehouse_info_id int64, org_id int64, over_count int64) error {
  11189. ut := XTWriteDB().Begin()
  11190. 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}).Error
  11191. if err != nil {
  11192. ut.Rollback()
  11193. return err
  11194. }
  11195. ut.Commit()
  11196. return err
  11197. }
  11198. func UpdateGoodWarehouseOutById(id int64, warehouse_info_id int64, org_id int64, over_count int64) error {
  11199. utx := XTWriteDB().Begin()
  11200. 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
  11201. if err != nil {
  11202. utx.Rollback()
  11203. return err
  11204. }
  11205. utx.Commit()
  11206. return err
  11207. }
  11208. func ChangeMaxNumber(id int64, count int64) error {
  11209. 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
  11210. return err
  11211. }
  11212. func UpdateMinNumber(id int64, count int64) error {
  11213. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1 and is_check = 1", id).Update(map[string]interface{}{"stock_min_number": count}).Error
  11214. return err
  11215. }
  11216. func FindLastDrugWarehouseOutInfo(orgid int64) (models.DrugWarehouseOutInfo, error) {
  11217. info := models.DrugWarehouseOutInfo{}
  11218. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&info).Error
  11219. return info, err
  11220. }
  11221. func GetAllGoodSumCount(goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  11222. err = XTReadDB().Where("good_id = ? and org_id = ? and is_check =1 and status= 1 and stock_count > 0", goodid, orgid).Find(&info).Error
  11223. return info, err
  11224. }
  11225. func CreatedFlushInfo(info models.XtWarehouseFlushInfo) error {
  11226. ut := writeDb.Begin()
  11227. err := ut.Create(&info).Error
  11228. if err != nil {
  11229. ut.Rollback()
  11230. return err
  11231. }
  11232. ut.Commit()
  11233. return err
  11234. }
  11235. func FindDrugWarehouseInfoList(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  11236. 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
  11237. return info, err
  11238. }
  11239. func CreatedDrugFlushInfo(info models.XtDrugWarehouseFlushInfo) error {
  11240. ut := writeDb.Begin()
  11241. err := ut.Create(&info).Error
  11242. if err != nil {
  11243. ut.Rollback()
  11244. return err
  11245. }
  11246. ut.Commit()
  11247. return err
  11248. }
  11249. 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 {
  11250. 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
  11251. return err
  11252. }
  11253. func FindLastWarehouseOutInfo(orgId int64) (models.WarehouseOutInfo, error) {
  11254. info := models.WarehouseOutInfo{}
  11255. err := XTReadDB().Model(&info).Where("org_id = ? and status = 1", orgId).Find(&info).Error
  11256. return info, err
  11257. }
  11258. func ModifyAddGoodInformation(count int64, id int64) error {
  11259. ut := XTWriteDB().Begin()
  11260. err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", count)).Error
  11261. if err != nil {
  11262. ut.Rollback()
  11263. return err
  11264. }
  11265. ut.Commit()
  11266. return err
  11267. }
  11268. func ModifyReduceGoodInformation(count int64, id int64) error {
  11269. ut := XTWriteDB().Begin()
  11270. err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", count)).Error
  11271. if err != nil {
  11272. ut.Rollback()
  11273. return err
  11274. }
  11275. ut.Commit()
  11276. return err
  11277. }
  11278. func ModifyAddCancelCount(count int64, id int64) error {
  11279. ut := XTWriteDB().Begin()
  11280. err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", count)).Error
  11281. if err != nil {
  11282. ut.Rollback()
  11283. return err
  11284. }
  11285. ut.Commit()
  11286. return err
  11287. }
  11288. func ModifyReduceCancelCount(count int64, id int64) error {
  11289. ut := XTWriteDB().Begin()
  11290. err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", count)).Error
  11291. if err != nil {
  11292. ut.Rollback()
  11293. return err
  11294. }
  11295. ut.Commit()
  11296. return err
  11297. }
  11298. func ModifyAddDrugInformation(id int64, count int64, orgid int64) error {
  11299. ut := XTWriteDB().Begin()
  11300. 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
  11301. if err != nil {
  11302. ut.Rollback()
  11303. return err
  11304. }
  11305. ut.Commit()
  11306. return err
  11307. }
  11308. func ModifyReduceDrugInformation(id int64, count int64, orgid int64) error {
  11309. ut := XTWriteDB().Begin()
  11310. 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
  11311. if err != nil {
  11312. ut.Rollback()
  11313. return err
  11314. }
  11315. ut.Commit()
  11316. return err
  11317. }
  11318. func ModifyAddCancelAddInfomation(id int64, count int64, orgid int64, storehouse_id int64) error {
  11319. ut := XTWriteDB().Begin()
  11320. 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
  11321. if err != nil {
  11322. ut.Rollback()
  11323. return err
  11324. }
  11325. ut.Commit()
  11326. return err
  11327. }
  11328. func ModifyAddDrugOutReduceInfomation(id int64, count int64, orgid int64, storehouse_id int64) error {
  11329. ut := XTWriteDB().Begin()
  11330. 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
  11331. if err != nil {
  11332. ut.Rollback()
  11333. return err
  11334. }
  11335. ut.Commit()
  11336. return err
  11337. }
  11338. func ModifyAddDrugOutAddInfomation(id int64, count int64, orgid int64, storehouse_id int64) error {
  11339. ut := XTWriteDB().Begin()
  11340. 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
  11341. if err != nil {
  11342. ut.Rollback()
  11343. return err
  11344. }
  11345. ut.Commit()
  11346. return err
  11347. }
  11348. func ModifyDrugCancelWarehouseInfo(id int64, count int64, orgid int64, storehouse_id int64) error {
  11349. ut := XTWriteDB().Begin()
  11350. 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
  11351. if err != nil {
  11352. ut.Rollback()
  11353. return err
  11354. }
  11355. ut.Commit()
  11356. return err
  11357. }
  11358. func ModifyGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
  11359. ut := XTWriteDB().Begin()
  11360. 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
  11361. if err != nil {
  11362. ut.Rollback()
  11363. return err
  11364. }
  11365. 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
  11366. if err != nil {
  11367. ut.Rollback()
  11368. return err
  11369. }
  11370. ut.Commit()
  11371. return err
  11372. }
  11373. func ModifyReduceGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
  11374. ut := XTWriteDB().Begin()
  11375. 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
  11376. if err != nil {
  11377. ut.Rollback()
  11378. return err
  11379. }
  11380. 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
  11381. if err != nil {
  11382. ut.Rollback()
  11383. return err
  11384. }
  11385. ut.Commit()
  11386. return err
  11387. }
  11388. func ModifyAddGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
  11389. ut := XTWriteDB().Begin()
  11390. 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
  11391. if err != nil {
  11392. ut.Rollback()
  11393. return err
  11394. }
  11395. ut.Commit()
  11396. return err
  11397. }
  11398. func UpdateSumAddCancelCount(user_org_id int64, good_id int64, storehouse_id int64, count int64) error {
  11399. ut := XTWriteDB().Begin()
  11400. 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
  11401. if err != nil {
  11402. ut.Rollback()
  11403. return err
  11404. }
  11405. ut.Commit()
  11406. return err
  11407. }
  11408. func UpdateSumReduceCancelCount(user_org_id int64, good_id int64, storehouse_id int64, count int64) error {
  11409. ut := XTWriteDB().Begin()
  11410. 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
  11411. if err != nil {
  11412. ut.Rollback()
  11413. return err
  11414. }
  11415. ut.Commit()
  11416. return err
  11417. }
  11418. func UpdateSumAddOutCount(user_org_id int64, good_id int64, storehouse_id int64, count int64) error {
  11419. ut := XTWriteDB().Begin()
  11420. 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
  11421. if err != nil {
  11422. ut.Rollback()
  11423. return err
  11424. }
  11425. ut.Commit()
  11426. return err
  11427. }
  11428. func FindDrugWarehouseInfoFlushCount(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  11429. err = XTReadDB().Where("drug_id = ? and org_id = ? and status = 1 and is_check = 1", drug_id, orgid).Find(&info).Error
  11430. return info, err
  11431. }
  11432. func FindDrugWarehouseInfoFlushCountByStorehouse(drug_id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
  11433. 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
  11434. return info, err
  11435. }
  11436. //结算出库
  11437. func HisSettleDrugsDelivery(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo, order_id int64) (err error) {
  11438. // 1.判断药品是否来自专用字典的药品库
  11439. // 2.判断当天当前机构有没有创建出库单,没有则创建
  11440. // 3.创建出库流程
  11441. // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  11442. isHasWay := false
  11443. record_time := int64(0)
  11444. isHasWay = true
  11445. record_time = advice.RecordDate
  11446. if isHasWay {
  11447. //判断当天当前机构有没有创建结算出库单,没有则创建
  11448. out, err := FindDrugSettleStockOutByIsSys(orgID, 5, record_time)
  11449. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  11450. if err == gorm.ErrRecordNotFound {
  11451. timeStr := time.Now().Format("2006-01-02")
  11452. timeArr := strings.Split(timeStr, "-")
  11453. total, _ := FindAllDrugWarehouseOut(orgID)
  11454. total = total + 1
  11455. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  11456. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  11457. number = number + total
  11458. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  11459. warehouseOut := models.DrugWarehouseOut{
  11460. WarehouseOutOrderNumber: warehousing_out_order,
  11461. OperationTime: time.Now().Unix(),
  11462. OrgId: orgID,
  11463. Creater: creater,
  11464. Ctime: time.Now().Unix(),
  11465. Status: 1,
  11466. WarehouseOutTime: record_time,
  11467. Dealer: 0,
  11468. Manufacturer: 0,
  11469. Type: 1, //结算出库
  11470. IsSys: 5,
  11471. StorehouseId: houseConfig.DrugStorehouseOut,
  11472. IsCheck: 1,
  11473. OrderId: order_id,
  11474. }
  11475. err := AddSigleDrugWarehouseOut(&warehouseOut)
  11476. if err != nil {
  11477. utils.TraceLog("创建出库单失败 err = %v", err)
  11478. return err
  11479. } else {
  11480. out = warehouseOut
  11481. }
  11482. }
  11483. // 出库流程
  11484. // 1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  11485. drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
  11486. if drup.ID > 0 {
  11487. prescribingNumber := advice.PrescribingNumber
  11488. HisSettleDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice)
  11489. } else {
  11490. return errors.New("药品信息不存在")
  11491. }
  11492. }
  11493. return
  11494. }
  11495. //耗材结算出库
  11496. func ConsumableSettleDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, creator int64, order_id int64) (err error) {
  11497. var deliver_number int64 = 0
  11498. var stock_number int64 = 0
  11499. var maxNumber int64 = 0
  11500. fmt.Println("223323233233223233223233223woo", goods.Count)
  11501. deliver_number = goods.Count
  11502. // 根据先进先出原则,查询最先入库的批次,进行出库
  11503. // 如果没有对应的库存,则报错
  11504. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  11505. if err != nil {
  11506. goodErrcode := models.XtGoodErrcode{
  11507. UserOrgId: goods.UserOrgId,
  11508. Errcode: "查询库存信息报错",
  11509. GoodId: goods.GoodId,
  11510. Status: 1,
  11511. Ctime: time.Now().Unix(),
  11512. Mtime: 0,
  11513. Count: 0,
  11514. StockCount: 0,
  11515. Creater: creator,
  11516. BatchNumberId: warehouse.ID,
  11517. WarehouseOutId: 0,
  11518. }
  11519. CreateGoodErrcode(goodErrcode)
  11520. return err
  11521. }
  11522. stock_number = warehouse.StockCount
  11523. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  11524. if stock_number >= deliver_number {
  11525. maxNumber = goods.Count
  11526. warehouse.StockCount = warehouse.StockCount - maxNumber
  11527. warehouse.Mtime = time.Now().Unix()
  11528. //扣减库存
  11529. errThree := UpDateWarehouseInfoByStock(&warehouse)
  11530. if errThree != nil {
  11531. goodErrcode := models.XtGoodErrcode{
  11532. UserOrgId: goods.UserOrgId,
  11533. Errcode: "扣减库存失败",
  11534. GoodId: goods.GoodId,
  11535. Status: 1,
  11536. Ctime: time.Now().Unix(),
  11537. Mtime: 0,
  11538. Count: 0,
  11539. StockCount: 0,
  11540. Creater: creator,
  11541. BatchNumberId: warehouse.ID,
  11542. WarehouseOutId: 0,
  11543. }
  11544. CreateGoodErrcode(goodErrcode)
  11545. return errThree
  11546. }
  11547. //查询剩余库存
  11548. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID)
  11549. var sum_count int64
  11550. for _, item := range goodList {
  11551. sum_count += item.StockCount
  11552. }
  11553. warehouseOutInfo := &models.WarehouseOutInfo{
  11554. WarehouseInfotId: warehouse.ID,
  11555. }
  11556. //添加入库单详情ID
  11557. errOne := UpdatedSigleWarehouseOutInfo(goods.ID, warehouseOutInfo)
  11558. if errOne != nil {
  11559. goodErrcode := models.XtGoodErrcode{
  11560. UserOrgId: goods.UserOrgId,
  11561. Errcode: "添加入库单详情ID",
  11562. GoodId: goods.GoodId,
  11563. Status: 1,
  11564. Ctime: time.Now().Unix(),
  11565. Mtime: 0,
  11566. Count: 0,
  11567. StockCount: 0,
  11568. Creater: creator,
  11569. BatchNumberId: warehouse.ID,
  11570. WarehouseOutId: 0,
  11571. }
  11572. CreateGoodErrcode(goodErrcode)
  11573. return errOne
  11574. }
  11575. OutInfo := &models.WarehouseOutInfo{
  11576. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  11577. WarehouseOutId: warehouseOut.ID,
  11578. WarehouseInfotId: warehouse.ID,
  11579. Status: 1,
  11580. Ctime: time.Now().Unix(),
  11581. Remark: warehouse.Remark,
  11582. OrgId: orgID,
  11583. Type: 1,
  11584. Manufacturer: warehouse.Manufacturer,
  11585. Dealer: warehouse.Dealer,
  11586. IsSys: 5,
  11587. SysRecordTime: record_time,
  11588. GoodTypeId: goods.GoodTypeId,
  11589. GoodId: goods.GoodId,
  11590. PatientId: patient_id,
  11591. Number: warehouse.Number,
  11592. LicenseNumber: warehouse.LicenseNumber,
  11593. Price: warehouse.PackingPrice,
  11594. ExpiryDate: warehouse.ExpiryDate,
  11595. ProductDate: warehouse.ProductDate,
  11596. ProjectId: goods.ProjectId,
  11597. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  11598. StorehouseId: goods.StorehouseId,
  11599. IsCheck: 1,
  11600. OverCount: sum_count,
  11601. RegisterNumber: warehouse.RegisterNumber,
  11602. OrderId: order_id,
  11603. }
  11604. fmt.Println("goods.count2323232233232233232322332322332", goods.Count)
  11605. OutInfo.Count = goods.Count
  11606. AddSigleWarehouseOutInfo(OutInfo)
  11607. stockFlow := models.VmStockFlow{
  11608. WarehousingId: warehouse.ID,
  11609. GoodId: goods.GoodId,
  11610. Number: warehouse.Number,
  11611. LicenseNumber: warehouse.LicenseNumber,
  11612. Count: goods.Count,
  11613. UserOrgId: orgID,
  11614. PatientId: patient_id,
  11615. SystemTime: record_time,
  11616. ConsumableType: 15,
  11617. IsSys: 5,
  11618. WarehousingOrder: warehouse.WarehousingOrder,
  11619. WarehouseOutId: warehouseOut.ID,
  11620. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  11621. IsEdit: 1,
  11622. CancelStockId: 0,
  11623. CancelOrderNumber: "",
  11624. Manufacturer: warehouse.Manufacturer,
  11625. Dealer: warehouse.Dealer,
  11626. Creator: creator,
  11627. UpdateCreator: 0,
  11628. Status: 1,
  11629. Ctime: time.Now().Unix(),
  11630. Mtime: 0,
  11631. Price: warehouse.PackingPrice, //零售价
  11632. WarehousingDetailId: warehouse.ID,
  11633. WarehouseOutDetailId: goods.ID,
  11634. CancelOutDetailId: 0,
  11635. ProductDate: warehouse.ProductDate,
  11636. ExpireDate: warehouse.ExpiryDate,
  11637. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  11638. StorehouseId: warehouse.StorehouseId,
  11639. AdminUserId: creator,
  11640. BuyPrice: warehouse.Price, //进货价
  11641. StockCount: "",
  11642. BatchNumberCount: warehouse.StockCount - goods.Count,
  11643. IsCheck: 1,
  11644. OverCount: sum_count,
  11645. RegisterNumber: warehouse.RegisterNumber,
  11646. OrderId: order_id,
  11647. }
  11648. //创建出库流水
  11649. errflow := CreateStockFlowOne(stockFlow)
  11650. if errflow != nil {
  11651. goodErrcode := models.XtGoodErrcode{
  11652. UserOrgId: orgID,
  11653. Errcode: "创建流水报错",
  11654. GoodId: goods.GoodId,
  11655. Status: 1,
  11656. Ctime: time.Now().Unix(),
  11657. Mtime: 0,
  11658. Count: 0,
  11659. StockCount: 0,
  11660. Creater: creator,
  11661. BatchNumberId: warehouse.ID,
  11662. WarehouseOutId: 0,
  11663. }
  11664. CreateGoodErrcode(goodErrcode)
  11665. }
  11666. //添加入库ID
  11667. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count)
  11668. if errWarehouse != nil {
  11669. goodErrcode := models.XtGoodErrcode{
  11670. UserOrgId: orgID,
  11671. Errcode: "添加入库ID报错",
  11672. GoodId: goods.GoodId,
  11673. Status: 1,
  11674. Ctime: time.Now().Unix(),
  11675. Mtime: 0,
  11676. Count: 0,
  11677. StockCount: 0,
  11678. Creater: creator,
  11679. BatchNumberId: warehouse.ID,
  11680. WarehouseOutId: 0,
  11681. }
  11682. CreateGoodErrcode(goodErrcode)
  11683. }
  11684. //更新出库数量
  11685. errsumcode := ModifyGoodSumCount(goods.StorehouseId, goods.Count, orgID, goods.GoodId)
  11686. if errsumcode != nil {
  11687. goodErrcode := models.XtGoodErrcode{
  11688. UserOrgId: orgID,
  11689. Errcode: "更新出库数量报错",
  11690. GoodId: goods.GoodId,
  11691. Status: 1,
  11692. Ctime: time.Now().Unix(),
  11693. Mtime: 0,
  11694. Count: 0,
  11695. StockCount: 0,
  11696. Creater: creator,
  11697. BatchNumberId: warehouse.ID,
  11698. WarehouseOutId: 0,
  11699. }
  11700. CreateGoodErrcode(goodErrcode)
  11701. }
  11702. if warehouse.StockCount < maxNumber {
  11703. goodErrcode := models.XtGoodErrcode{
  11704. UserOrgId: orgID,
  11705. Errcode: "出库接口库存数量不足",
  11706. GoodId: goods.GoodId,
  11707. Status: 1,
  11708. Ctime: time.Now().Unix(),
  11709. Mtime: 0,
  11710. Count: 0,
  11711. StockCount: 0,
  11712. Creater: creator,
  11713. BatchNumberId: warehouse.ID,
  11714. WarehouseOutId: 0,
  11715. }
  11716. CreateGoodErrcode(goodErrcode)
  11717. return errors.New("库存数量不足")
  11718. }
  11719. return nil
  11720. } else {
  11721. // 出库完成后,要将该批次库存清零
  11722. warehouse.StockCount = 0
  11723. warehouse.Mtime = time.Now().Unix()
  11724. errThree := UpDateWarehouseInfoByStock(&warehouse)
  11725. if errThree != nil {
  11726. goodErrcode := models.XtGoodErrcode{
  11727. UserOrgId: orgID,
  11728. Errcode: "扣减库存报错",
  11729. GoodId: goods.GoodId,
  11730. Status: 1,
  11731. Ctime: time.Now().Unix(),
  11732. Mtime: 0,
  11733. Count: 0,
  11734. StockCount: 0,
  11735. Creater: creator,
  11736. BatchNumberId: warehouse.ID,
  11737. WarehouseOutId: 0,
  11738. }
  11739. CreateGoodErrcode(goodErrcode)
  11740. return errThree
  11741. }
  11742. //查询剩余库存
  11743. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID)
  11744. var sum_count int64
  11745. for _, item := range goodList {
  11746. sum_count += item.StockCount
  11747. }
  11748. OutInfo := &models.WarehouseOutInfo{
  11749. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  11750. WarehouseOutId: warehouseOut.ID,
  11751. WarehouseInfotId: warehouse.ID,
  11752. Status: 1,
  11753. Ctime: time.Now().Unix(),
  11754. Remark: warehouse.Remark,
  11755. OrgId: orgID,
  11756. Type: 1,
  11757. Manufacturer: warehouse.Manufacturer,
  11758. Dealer: warehouse.Dealer,
  11759. IsSys: 5,
  11760. SysRecordTime: record_time,
  11761. GoodTypeId: goods.GoodTypeId,
  11762. GoodId: goods.GoodId,
  11763. PatientId: patient_id,
  11764. Number: warehouse.Number,
  11765. LicenseNumber: warehouse.LicenseNumber,
  11766. Price: warehouse.PackingPrice,
  11767. ExpiryDate: warehouse.ExpiryDate,
  11768. ProductDate: warehouse.ProductDate,
  11769. ProjectId: goods.ProjectId,
  11770. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  11771. StorehouseId: goods.StorehouseId,
  11772. IsCheck: 1,
  11773. OverCount: sum_count,
  11774. RegisterNumber: warehouse.RegisterNumber,
  11775. OrderId: order_id,
  11776. }
  11777. OutInfo.Count = goods.Count
  11778. AddSigleWarehouseOutInfo(OutInfo)
  11779. stockFlow := models.VmStockFlow{
  11780. WarehousingId: warehouse.ID,
  11781. GoodId: goods.GoodId,
  11782. Number: warehouse.Number,
  11783. LicenseNumber: warehouse.LicenseNumber,
  11784. Count: stock_number,
  11785. UserOrgId: orgID,
  11786. PatientId: patient_id,
  11787. SystemTime: record_time,
  11788. ConsumableType: 15,
  11789. IsSys: 5,
  11790. WarehousingOrder: warehouse.WarehousingOrder,
  11791. WarehouseOutId: warehouseOut.ID,
  11792. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  11793. IsEdit: 1,
  11794. CancelStockId: 0,
  11795. CancelOrderNumber: "",
  11796. Manufacturer: warehouse.Manufacturer,
  11797. Dealer: warehouse.Dealer,
  11798. Creator: creator,
  11799. UpdateCreator: 0,
  11800. Status: 1,
  11801. Ctime: time.Now().Unix(),
  11802. Mtime: 0,
  11803. Price: warehouse.PackingPrice, //零售价
  11804. WarehousingDetailId: warehouse.ID,
  11805. WarehouseOutDetailId: goods.ID,
  11806. CancelOutDetailId: 0,
  11807. ProductDate: warehouse.ProductDate,
  11808. ExpireDate: warehouse.ExpiryDate,
  11809. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  11810. StorehouseId: warehouse.StorehouseId,
  11811. AdminUserId: creator,
  11812. BuyPrice: warehouse.Price, //进货价
  11813. StockCount: "",
  11814. BatchNumberCount: 0,
  11815. IsCheck: 1,
  11816. OverCount: sum_count,
  11817. RegisterNumber: warehouse.RegisterNumber,
  11818. OrderId: order_id,
  11819. }
  11820. //创建出库流水
  11821. errflow := CreateStockFlowOne(stockFlow)
  11822. if errflow != nil {
  11823. goodErrcode := models.XtGoodErrcode{
  11824. UserOrgId: orgID,
  11825. Errcode: "创建出库流水报错",
  11826. GoodId: goods.GoodId,
  11827. Status: 1,
  11828. Ctime: time.Now().Unix(),
  11829. Mtime: 0,
  11830. Count: 0,
  11831. StockCount: 0,
  11832. Creater: creator,
  11833. BatchNumberId: warehouse.ID,
  11834. WarehouseOutId: 0,
  11835. }
  11836. CreateGoodErrcode(goodErrcode)
  11837. }
  11838. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count)
  11839. if errWarehouse != nil {
  11840. goodErrcode := models.XtGoodErrcode{
  11841. UserOrgId: orgID,
  11842. Errcode: "添加入库ID报错",
  11843. GoodId: goods.GoodId,
  11844. Status: 1,
  11845. Ctime: time.Now().Unix(),
  11846. Mtime: 0,
  11847. Count: 0,
  11848. StockCount: 0,
  11849. Creater: creator,
  11850. BatchNumberId: warehouse.ID,
  11851. WarehouseOutId: 0,
  11852. }
  11853. CreateGoodErrcode(goodErrcode)
  11854. }
  11855. //更新出库数量
  11856. errcodecout := ModifyGoodSumCount(goods.StorehouseId, stock_number, orgID, goods.GoodId)
  11857. if errcodecout != nil {
  11858. goodErrcode := models.XtGoodErrcode{
  11859. UserOrgId: orgID,
  11860. Errcode: "更新出库数量报错",
  11861. GoodId: goods.GoodId,
  11862. Status: 1,
  11863. Ctime: time.Now().Unix(),
  11864. Mtime: 0,
  11865. Count: 0,
  11866. StockCount: 0,
  11867. Creater: creator,
  11868. BatchNumberId: warehouse.ID,
  11869. WarehouseOutId: 0,
  11870. }
  11871. CreateGoodErrcode(goodErrcode)
  11872. }
  11873. }
  11874. goods.Count = deliver_number - stock_number
  11875. prepare := &models.DialysisBeforePrepare{
  11876. Count: deliver_number - stock_number,
  11877. }
  11878. ConsumableSettleDelivery(orgID, patient_id, record_time, prepare, warehouseOut, creator, order_id)
  11879. return nil
  11880. }
  11881. //保存处方出库
  11882. func ConsumablePrescriptionDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, creator int64) (err error) {
  11883. var deliver_number int64 = 0
  11884. var stock_number int64 = 0
  11885. var maxNumber int64 = 0
  11886. fmt.Println("223323233233223233223233223woo", goods.Count)
  11887. deliver_number = goods.Count
  11888. // 根据先进先出原则,查询最先入库的批次,进行出库
  11889. // 如果没有对应的库存,则报错
  11890. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  11891. if err != nil {
  11892. goodErrcode := models.XtGoodErrcode{
  11893. UserOrgId: goods.UserOrgId,
  11894. Errcode: "查询库存信息报错",
  11895. GoodId: goods.GoodId,
  11896. Status: 1,
  11897. Ctime: time.Now().Unix(),
  11898. Mtime: 0,
  11899. Count: 0,
  11900. StockCount: 0,
  11901. Creater: creator,
  11902. BatchNumberId: warehouse.ID,
  11903. WarehouseOutId: 0,
  11904. }
  11905. CreateGoodErrcode(goodErrcode)
  11906. return err
  11907. }
  11908. stock_number = warehouse.StockCount
  11909. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  11910. if stock_number >= deliver_number {
  11911. maxNumber = goods.Count
  11912. warehouse.StockCount = warehouse.StockCount - maxNumber
  11913. warehouse.Mtime = time.Now().Unix()
  11914. //扣减库存
  11915. errThree := UpDateWarehouseInfoByStock(&warehouse)
  11916. if errThree != nil {
  11917. goodErrcode := models.XtGoodErrcode{
  11918. UserOrgId: goods.UserOrgId,
  11919. Errcode: "扣减库存失败",
  11920. GoodId: goods.GoodId,
  11921. Status: 1,
  11922. Ctime: time.Now().Unix(),
  11923. Mtime: 0,
  11924. Count: 0,
  11925. StockCount: 0,
  11926. Creater: creator,
  11927. BatchNumberId: warehouse.ID,
  11928. WarehouseOutId: 0,
  11929. }
  11930. CreateGoodErrcode(goodErrcode)
  11931. return errThree
  11932. }
  11933. //查询剩余库存
  11934. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID)
  11935. var sum_count int64
  11936. for _, item := range goodList {
  11937. sum_count += item.StockCount
  11938. }
  11939. warehouseOutInfo := &models.WarehouseOutInfo{
  11940. WarehouseInfotId: warehouse.ID,
  11941. }
  11942. //添加入库单详情ID
  11943. errOne := UpdatedSigleWarehouseOutInfo(goods.ID, warehouseOutInfo)
  11944. if errOne != nil {
  11945. goodErrcode := models.XtGoodErrcode{
  11946. UserOrgId: goods.UserOrgId,
  11947. Errcode: "添加入库单详情ID",
  11948. GoodId: goods.GoodId,
  11949. Status: 1,
  11950. Ctime: time.Now().Unix(),
  11951. Mtime: 0,
  11952. Count: 0,
  11953. StockCount: 0,
  11954. Creater: creator,
  11955. BatchNumberId: warehouse.ID,
  11956. WarehouseOutId: 0,
  11957. }
  11958. CreateGoodErrcode(goodErrcode)
  11959. return errOne
  11960. }
  11961. OutInfo := &models.WarehouseOutInfo{
  11962. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  11963. WarehouseOutId: warehouseOut.ID,
  11964. WarehouseInfotId: warehouse.ID,
  11965. Status: 1,
  11966. Ctime: time.Now().Unix(),
  11967. Remark: warehouse.Remark,
  11968. OrgId: orgID,
  11969. Type: 1,
  11970. Manufacturer: warehouse.Manufacturer,
  11971. Dealer: warehouse.Dealer,
  11972. IsSys: 1,
  11973. SysRecordTime: record_time,
  11974. GoodTypeId: goods.GoodTypeId,
  11975. GoodId: goods.GoodId,
  11976. PatientId: patient_id,
  11977. Number: warehouse.Number,
  11978. LicenseNumber: warehouse.LicenseNumber,
  11979. Price: warehouse.PackingPrice,
  11980. ExpiryDate: warehouse.ExpiryDate,
  11981. ProductDate: warehouse.ProductDate,
  11982. ProjectId: goods.ProjectId,
  11983. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  11984. StorehouseId: goods.StorehouseId,
  11985. IsCheck: 1,
  11986. OverCount: sum_count,
  11987. RegisterNumber: warehouse.RegisterNumber,
  11988. }
  11989. fmt.Println("goods.count2323232233232233232322332322332", goods.Count)
  11990. OutInfo.Count = goods.Count
  11991. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
  11992. if errcodes == gorm.ErrRecordNotFound {
  11993. errOne := AddSigleWarehouseOutInfo(OutInfo)
  11994. if errOne != nil {
  11995. return errOne
  11996. }
  11997. } else if errcodes == nil {
  11998. outInfoOne, _ := GetWarehouseOutInfoIsExistThree(goods.GoodId, patient_id, record_time, goods.ProjectId)
  11999. if goods.Count != outInfoOne.Count {
  12000. UpdatedWarehouseOutInfo(OutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId)
  12001. }
  12002. }
  12003. lastOut, _ := FindWarehouseOutInfoByPatientIdTwo(patient_id, record_time, goods.GoodId, orgID, goods.ProjectId)
  12004. //查询已经出库的数据
  12005. flowGood, _ := GetStockFlowIsBatchNumberSix(patient_id, record_time, goods.GoodId, goods.ProjectId)
  12006. var out_count int64
  12007. var out_count_one int64
  12008. for _, item := range flowGood {
  12009. out_count += item.Count
  12010. }
  12011. //查询退库数据
  12012. flowGoodTwo, _ := GetStockFlowIsBatchNumberThree(patient_id, record_time, goods.GoodId)
  12013. for _, item := range flowGoodTwo {
  12014. out_count_one += item.Count
  12015. }
  12016. //如果本次出库数据大于历史出库数据 新增1条流水
  12017. fmt.Println("goods_count233232323223", goods.Count)
  12018. fmt.Println("out_count - out_count_one", out_count-out_count_one)
  12019. if goods.Count > (out_count - out_count_one) {
  12020. stockFlow := models.VmStockFlow{
  12021. WarehousingId: warehouse.ID,
  12022. GoodId: goods.GoodId,
  12023. Number: warehouse.Number,
  12024. LicenseNumber: warehouse.LicenseNumber,
  12025. Count: goods.Count - out_count + out_count_one,
  12026. UserOrgId: orgID,
  12027. PatientId: goods.PatientId,
  12028. SystemTime: record_time,
  12029. ConsumableType: 3,
  12030. IsSys: 1,
  12031. WarehousingOrder: warehouse.WarehousingOrder,
  12032. WarehouseOutId: warehouseOut.ID,
  12033. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  12034. IsEdit: 1,
  12035. CancelStockId: 0,
  12036. CancelOrderNumber: "",
  12037. Manufacturer: warehouse.Manufacturer,
  12038. Dealer: warehouse.Dealer,
  12039. Creator: creator,
  12040. UpdateCreator: 0,
  12041. Status: 1,
  12042. Ctime: time.Now().Unix(),
  12043. Mtime: 0,
  12044. Price: warehouse.PackingPrice, //零售价
  12045. WarehousingDetailId: warehouse.ID,
  12046. WarehouseOutDetailId: goods.ID,
  12047. CancelOutDetailId: 0,
  12048. ProductDate: warehouse.ProductDate,
  12049. ExpireDate: warehouse.ExpiryDate,
  12050. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  12051. StorehouseId: warehouse.StorehouseId,
  12052. AdminUserId: creator,
  12053. BuyPrice: warehouse.Price, //进货价
  12054. StockCount: "",
  12055. BatchNumberCount: warehouse.StockCount - goods.Count,
  12056. IsCheck: 1,
  12057. OverCount: sum_count,
  12058. RegisterNumber: warehouse.RegisterNumber,
  12059. ProjectId: goods.ProjectId,
  12060. }
  12061. //创建出库流水
  12062. errflow := CreateStockFlowOne(stockFlow)
  12063. if errflow != nil {
  12064. goodErrcode := models.XtGoodErrcode{
  12065. UserOrgId: orgID,
  12066. Errcode: "创建流水报错",
  12067. GoodId: goods.GoodId,
  12068. Status: 1,
  12069. Ctime: time.Now().Unix(),
  12070. Mtime: 0,
  12071. Count: 0,
  12072. StockCount: 0,
  12073. Creater: creator,
  12074. BatchNumberId: warehouse.ID,
  12075. WarehouseOutId: 0,
  12076. }
  12077. CreateGoodErrcode(goodErrcode)
  12078. }
  12079. //更新出库数量
  12080. errsumcode := ModifyGoodSumCount(goods.StorehouseId, stockFlow.Count, orgID, goods.GoodId)
  12081. if errsumcode != nil {
  12082. goodErrcode := models.XtGoodErrcode{
  12083. UserOrgId: orgID,
  12084. Errcode: "更新出库数量报错",
  12085. GoodId: goods.GoodId,
  12086. Status: 1,
  12087. Ctime: time.Now().Unix(),
  12088. Mtime: 0,
  12089. Count: 0,
  12090. StockCount: 0,
  12091. Creater: creator,
  12092. BatchNumberId: warehouse.ID,
  12093. WarehouseOutId: 0,
  12094. }
  12095. CreateGoodErrcode(goodErrcode)
  12096. }
  12097. }
  12098. //如果本次出库数据小于历史出库数据 新增1条退库流水
  12099. if goods.Count < (out_count - out_count_one) {
  12100. operation_time := time.Now().Unix()
  12101. //创建退库单
  12102. timeStr := time.Now().Format("2006-01-02")
  12103. timeArr := strings.Split(timeStr, "-")
  12104. total, _ := FindAllCancelStockTotal(orgID)
  12105. total = total + 1
  12106. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  12107. cancelStock := models.CancelStock{
  12108. OrderNumber: orderNumber,
  12109. OperaTime: operation_time,
  12110. OrgId: orgID,
  12111. Creater: warehouseOut.Creater,
  12112. Ctime: time.Now().Unix(),
  12113. Status: 1,
  12114. ReturnTime: record_time,
  12115. Type: 1,
  12116. StorehouseId: goods.StorehouseId,
  12117. IsCheck: 1,
  12118. }
  12119. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, orgID)
  12120. if msgerrkonde == gorm.ErrRecordNotFound {
  12121. AddSigleCancelStock(&cancelStock)
  12122. }
  12123. cancel, _ := GetLastCancelStockById(orgID)
  12124. manufacturer, _ := GetManufactureById(warehouse.Manufacturer)
  12125. deaerler, _ := GetDealerById(warehouse.Dealer)
  12126. cancelStockInfo := models.CancelStockInfo{
  12127. GoodId: goods.GoodId,
  12128. CancelStockId: cancel.ID,
  12129. GoodTypeId: goods.GoodTypeId,
  12130. Count: out_count - out_count_one - goods.Count,
  12131. Price: warehouse.PackingPrice,
  12132. Total: 0,
  12133. ProductDate: warehouse.ProductDate,
  12134. ExpiryDate: warehouse.ExpiryDate,
  12135. Ctime: time.Now().Unix(),
  12136. Status: 1,
  12137. OrgId: orgID,
  12138. OrderNumber: cancel.OrderNumber,
  12139. Type: 0,
  12140. Dealer: deaerler.DealerName,
  12141. Manufacturer: manufacturer.ManufacturerName,
  12142. Number: warehouse.Number,
  12143. RegisterAccount: "",
  12144. Remark: "",
  12145. WarehouseInfoId: warehouse.ID,
  12146. PatientId: patient_id,
  12147. RecordDate: record_time,
  12148. StorehouseId: goods.StorehouseId,
  12149. IsCheck: 1,
  12150. }
  12151. if orgID == 9671 || orgID == 10265 {
  12152. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  12153. cancelStockInfo.Price = goodsInfo.PackingPrice
  12154. }
  12155. CreateCancelStockInfoOne(&cancelStockInfo)
  12156. cancelInfo, _ := GetLastCancelStockInfoByGoodId(goods.GoodId)
  12157. flow := models.VmStockFlow{
  12158. WarehousingId: warehouse.ID,
  12159. GoodId: goods.GoodId,
  12160. Number: warehouse.Number,
  12161. LicenseNumber: warehouse.LicenseNumber,
  12162. Count: out_count - out_count_one - goods.Count,
  12163. UserOrgId: orgID,
  12164. PatientId: patient_id,
  12165. SystemTime: record_time,
  12166. ConsumableType: 7,
  12167. IsSys: 1,
  12168. WarehousingOrder: "",
  12169. WarehouseOutId: lastOut.WarehouseOutId,
  12170. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  12171. IsEdit: 0,
  12172. CancelStockId: cancel.ID,
  12173. CancelOrderNumber: cancel.OrderNumber,
  12174. Manufacturer: manufacturer.ID,
  12175. Dealer: 0,
  12176. Creator: warehouseOut.Creater,
  12177. UpdateCreator: 0,
  12178. Status: 1,
  12179. Ctime: time.Now().Unix(),
  12180. Mtime: 0,
  12181. Price: warehouse.PackingPrice,
  12182. WarehousingDetailId: warehouse.ID,
  12183. WarehouseOutDetailId: lastOut.ID,
  12184. CancelOutDetailId: cancelInfo.ID,
  12185. ProductDate: warehouse.ProductDate,
  12186. ExpireDate: warehouse.ExpiryDate,
  12187. StorehouseId: goods.StorehouseId,
  12188. BuyPrice: warehouse.Price,
  12189. ProjectId: goods.ProjectId,
  12190. OverCount: sum_count,
  12191. RegisterNumber: warehouse.RegisterNumber,
  12192. }
  12193. if orgID == 9671 || orgID == 10265 {
  12194. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  12195. flow.Price = goodsInfo.PackingPrice
  12196. flow.BuyPrice = goodsInfo.BuyPrice
  12197. }
  12198. CreateStockFlowOne(flow)
  12199. //退库数量增加
  12200. UpdateSumAddCancelCount(orgID, goods.GoodId, goods.StorehouseId, flow.Count)
  12201. }
  12202. //添加入库ID
  12203. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count)
  12204. if errWarehouse != nil {
  12205. goodErrcode := models.XtGoodErrcode{
  12206. UserOrgId: orgID,
  12207. Errcode: "添加入库ID报错",
  12208. GoodId: goods.GoodId,
  12209. Status: 1,
  12210. Ctime: time.Now().Unix(),
  12211. Mtime: 0,
  12212. Count: 0,
  12213. StockCount: 0,
  12214. Creater: creator,
  12215. BatchNumberId: warehouse.ID,
  12216. WarehouseOutId: 0,
  12217. }
  12218. CreateGoodErrcode(goodErrcode)
  12219. }
  12220. if warehouse.StockCount < maxNumber {
  12221. goodErrcode := models.XtGoodErrcode{
  12222. UserOrgId: orgID,
  12223. Errcode: "出库接口库存数量不足",
  12224. GoodId: goods.GoodId,
  12225. Status: 1,
  12226. Ctime: time.Now().Unix(),
  12227. Mtime: 0,
  12228. Count: 0,
  12229. StockCount: 0,
  12230. Creater: creator,
  12231. BatchNumberId: warehouse.ID,
  12232. WarehouseOutId: 0,
  12233. }
  12234. CreateGoodErrcode(goodErrcode)
  12235. return errors.New("库存数量不足")
  12236. }
  12237. return nil
  12238. } else {
  12239. // 出库完成后,要将该批次库存清零
  12240. warehouse.StockCount = 0
  12241. warehouse.Mtime = time.Now().Unix()
  12242. errThree := UpDateWarehouseInfoByStock(&warehouse)
  12243. if errThree != nil {
  12244. goodErrcode := models.XtGoodErrcode{
  12245. UserOrgId: orgID,
  12246. Errcode: "扣减库存报错",
  12247. GoodId: goods.GoodId,
  12248. Status: 1,
  12249. Ctime: time.Now().Unix(),
  12250. Mtime: 0,
  12251. Count: 0,
  12252. StockCount: 0,
  12253. Creater: creator,
  12254. BatchNumberId: warehouse.ID,
  12255. WarehouseOutId: 0,
  12256. }
  12257. CreateGoodErrcode(goodErrcode)
  12258. return errThree
  12259. }
  12260. //查询剩余库存
  12261. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID)
  12262. var sum_count int64
  12263. for _, item := range goodList {
  12264. sum_count += item.StockCount
  12265. }
  12266. OutInfo := &models.WarehouseOutInfo{
  12267. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  12268. WarehouseOutId: warehouseOut.ID,
  12269. WarehouseInfotId: warehouse.ID,
  12270. Status: 1,
  12271. Ctime: time.Now().Unix(),
  12272. Remark: warehouse.Remark,
  12273. OrgId: orgID,
  12274. Type: 1,
  12275. Manufacturer: warehouse.Manufacturer,
  12276. Dealer: warehouse.Dealer,
  12277. IsSys: 1,
  12278. SysRecordTime: record_time,
  12279. GoodTypeId: goods.GoodTypeId,
  12280. GoodId: goods.GoodId,
  12281. PatientId: patient_id,
  12282. Number: warehouse.Number,
  12283. LicenseNumber: warehouse.LicenseNumber,
  12284. Price: warehouse.PackingPrice,
  12285. ExpiryDate: warehouse.ExpiryDate,
  12286. ProductDate: warehouse.ProductDate,
  12287. ProjectId: goods.ProjectId,
  12288. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  12289. StorehouseId: goods.StorehouseId,
  12290. IsCheck: 1,
  12291. OverCount: sum_count,
  12292. RegisterNumber: warehouse.RegisterNumber,
  12293. }
  12294. OutInfo.Count = goods.Count
  12295. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
  12296. if errcodes == gorm.ErrRecordNotFound {
  12297. errOne := AddSigleWarehouseOutInfo(OutInfo)
  12298. if errOne != nil {
  12299. return errOne
  12300. }
  12301. } else if errcodes == nil {
  12302. outInfoOne, _ := GetWarehouseOutInfoIsExistThree(goods.GoodId, patient_id, record_time, goods.ProjectId)
  12303. if goods.Count != outInfoOne.Count {
  12304. UpdatedWarehouseOutInfo(OutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId)
  12305. }
  12306. }
  12307. lastOut, _ := FindWarehouseOutInfoByPatientIdTwo(patient_id, record_time, goods.GoodId, orgID, goods.ProjectId)
  12308. //查询该该批次已经出库的数据
  12309. flowGood, _ := GetStockFlowIsBatchNumber(warehouse.ID, patient_id, record_time, goods.GoodId)
  12310. var out_count int64
  12311. var out_count_one int64
  12312. for _, item := range flowGood {
  12313. out_count += item.Count
  12314. }
  12315. flowGoodTwo, _ := GetStockFlowIsBatchNumberSix(patient_id, record_time, goods.GoodId, goods.ProjectId)
  12316. for _, item := range flowGoodTwo {
  12317. out_count_one += item.Count
  12318. }
  12319. //如果出库数量 大于 历史出库数据 新增1条流水
  12320. if goods.Count > out_count-out_count_one {
  12321. stockFlow := models.VmStockFlow{
  12322. WarehousingId: warehouse.ID,
  12323. GoodId: goods.GoodId,
  12324. Number: warehouse.Number,
  12325. LicenseNumber: warehouse.LicenseNumber,
  12326. Count: stock_number,
  12327. UserOrgId: orgID,
  12328. PatientId: goods.PatientId,
  12329. SystemTime: record_time,
  12330. ConsumableType: 3,
  12331. IsSys: 1,
  12332. WarehousingOrder: warehouse.WarehousingOrder,
  12333. WarehouseOutId: warehouseOut.ID,
  12334. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  12335. IsEdit: 1,
  12336. CancelStockId: 0,
  12337. CancelOrderNumber: "",
  12338. Manufacturer: warehouse.Manufacturer,
  12339. Dealer: warehouse.Dealer,
  12340. Creator: creator,
  12341. UpdateCreator: 0,
  12342. Status: 1,
  12343. Ctime: time.Now().Unix(),
  12344. Mtime: 0,
  12345. Price: warehouse.PackingPrice, //零售价
  12346. WarehousingDetailId: warehouse.ID,
  12347. WarehouseOutDetailId: goods.ID,
  12348. CancelOutDetailId: 0,
  12349. ProductDate: warehouse.ProductDate,
  12350. ExpireDate: warehouse.ExpiryDate,
  12351. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  12352. StorehouseId: warehouse.StorehouseId,
  12353. AdminUserId: creator,
  12354. BuyPrice: warehouse.Price, //进货价
  12355. StockCount: "",
  12356. BatchNumberCount: 0,
  12357. IsCheck: 1,
  12358. OverCount: sum_count,
  12359. RegisterNumber: warehouse.RegisterNumber,
  12360. ProjectId: goods.ProjectId,
  12361. }
  12362. //创建出库流水
  12363. errflow := CreateStockFlowOne(stockFlow)
  12364. if errflow != nil {
  12365. goodErrcode := models.XtGoodErrcode{
  12366. UserOrgId: orgID,
  12367. Errcode: "创建出库流水报错",
  12368. GoodId: goods.GoodId,
  12369. Status: 1,
  12370. Ctime: time.Now().Unix(),
  12371. Mtime: 0,
  12372. Count: 0,
  12373. StockCount: 0,
  12374. Creater: creator,
  12375. BatchNumberId: warehouse.ID,
  12376. WarehouseOutId: 0,
  12377. }
  12378. CreateGoodErrcode(goodErrcode)
  12379. }
  12380. //更新出库数量
  12381. errcodecout := ModifyGoodSumCount(goods.StorehouseId, stock_number, orgID, goods.GoodId)
  12382. if errcodecout != nil {
  12383. goodErrcode := models.XtGoodErrcode{
  12384. UserOrgId: orgID,
  12385. Errcode: "更新出库数量报错",
  12386. GoodId: goods.GoodId,
  12387. Status: 1,
  12388. Ctime: time.Now().Unix(),
  12389. Mtime: 0,
  12390. Count: 0,
  12391. StockCount: 0,
  12392. Creater: creator,
  12393. BatchNumberId: warehouse.ID,
  12394. WarehouseOutId: 0,
  12395. }
  12396. CreateGoodErrcode(goodErrcode)
  12397. }
  12398. }
  12399. //退库
  12400. if goods.Count < out_count-out_count_one {
  12401. operation_time := time.Now().Unix()
  12402. //创建退库单
  12403. timeStr := time.Now().Format("2006-01-02")
  12404. timeArr := strings.Split(timeStr, "-")
  12405. total, _ := FindAllCancelStockTotal(orgID)
  12406. total = total + 1
  12407. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  12408. cancelStock := models.CancelStock{
  12409. OrderNumber: orderNumber,
  12410. OperaTime: operation_time,
  12411. OrgId: orgID,
  12412. Creater: warehouseOut.Creater,
  12413. Ctime: time.Now().Unix(),
  12414. Status: 1,
  12415. ReturnTime: record_time,
  12416. Type: 1,
  12417. StorehouseId: goods.StorehouseId,
  12418. IsCheck: 1,
  12419. }
  12420. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, orgID)
  12421. if msgerrkonde == gorm.ErrRecordNotFound {
  12422. AddSigleCancelStock(&cancelStock)
  12423. }
  12424. cancel, _ := GetLastCancelStockById(orgID)
  12425. manufacturer, _ := GetManufactureById(warehouse.Manufacturer)
  12426. deaerler, _ := GetDealerById(warehouse.Dealer)
  12427. cancelStockInfo := models.CancelStockInfo{
  12428. GoodId: goods.GoodId,
  12429. CancelStockId: cancel.ID,
  12430. GoodTypeId: goods.GoodTypeId,
  12431. Count: out_count - out_count_one - goods.Count,
  12432. Price: warehouse.PackingPrice,
  12433. Total: 0,
  12434. ProductDate: warehouse.ProductDate,
  12435. ExpiryDate: warehouse.ExpiryDate,
  12436. Ctime: time.Now().Unix(),
  12437. Status: 1,
  12438. OrgId: orgID,
  12439. OrderNumber: cancel.OrderNumber,
  12440. Type: 0,
  12441. Dealer: deaerler.DealerName,
  12442. Manufacturer: manufacturer.ManufacturerName,
  12443. Number: warehouse.Number,
  12444. RegisterAccount: "",
  12445. Remark: "",
  12446. WarehouseInfoId: warehouse.ID,
  12447. PatientId: patient_id,
  12448. RecordDate: record_time,
  12449. StorehouseId: goods.StorehouseId,
  12450. IsCheck: 1,
  12451. }
  12452. if orgID == 9671 || orgID == 10265 {
  12453. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  12454. cancelStockInfo.Price = goodsInfo.PackingPrice
  12455. }
  12456. CreateCancelStockInfoOne(&cancelStockInfo)
  12457. cancelInfo, _ := GetLastCancelStockInfoByGoodId(goods.GoodId)
  12458. flow := models.VmStockFlow{
  12459. WarehousingId: warehouse.ID,
  12460. GoodId: goods.GoodId,
  12461. Number: warehouse.Number,
  12462. LicenseNumber: warehouse.LicenseNumber,
  12463. Count: out_count - goods.Count,
  12464. UserOrgId: orgID,
  12465. PatientId: patient_id,
  12466. SystemTime: record_time,
  12467. ConsumableType: 7,
  12468. IsSys: 1,
  12469. WarehousingOrder: "",
  12470. WarehouseOutId: lastOut.WarehouseOutId,
  12471. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  12472. IsEdit: 0,
  12473. CancelStockId: cancel.ID,
  12474. CancelOrderNumber: cancel.OrderNumber,
  12475. Manufacturer: manufacturer.ID,
  12476. Dealer: 0,
  12477. Creator: warehouseOut.Creater,
  12478. UpdateCreator: 0,
  12479. Status: 1,
  12480. Ctime: time.Now().Unix(),
  12481. Mtime: 0,
  12482. Price: warehouse.PackingPrice,
  12483. WarehousingDetailId: warehouse.ID,
  12484. WarehouseOutDetailId: lastOut.ID,
  12485. CancelOutDetailId: cancelInfo.ID,
  12486. ProductDate: warehouse.ProductDate,
  12487. ExpireDate: warehouse.ExpiryDate,
  12488. StorehouseId: goods.StorehouseId,
  12489. BuyPrice: warehouse.Price,
  12490. ProjectId: goods.ProjectId,
  12491. OverCount: sum_count,
  12492. RegisterNumber: warehouse.RegisterNumber,
  12493. }
  12494. if orgID == 9671 || orgID == 10265 {
  12495. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  12496. flow.Price = goodsInfo.PackingPrice
  12497. flow.BuyPrice = goodsInfo.BuyPrice
  12498. }
  12499. CreateStockFlowOne(flow)
  12500. //退库数量增加
  12501. UpdateSumAddCancelCount(orgID, goods.GoodId, goods.StorehouseId, flow.Count)
  12502. }
  12503. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count)
  12504. if errWarehouse != nil {
  12505. goodErrcode := models.XtGoodErrcode{
  12506. UserOrgId: orgID,
  12507. Errcode: "添加入库ID报错",
  12508. GoodId: goods.GoodId,
  12509. Status: 1,
  12510. Ctime: time.Now().Unix(),
  12511. Mtime: 0,
  12512. Count: 0,
  12513. StockCount: 0,
  12514. Creater: creator,
  12515. BatchNumberId: warehouse.ID,
  12516. WarehouseOutId: 0,
  12517. }
  12518. CreateGoodErrcode(goodErrcode)
  12519. }
  12520. }
  12521. goods.Count = deliver_number - stock_number
  12522. prepare := &models.DialysisBeforePrepare{
  12523. Count: deliver_number - stock_number,
  12524. }
  12525. ConsumablePrescriptionDelivery(orgID, patient_id, record_time, prepare, warehouseOut, creator)
  12526. return nil
  12527. }