warhouse_service.go 374KB

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