warhouse_service.go 393KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284
  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 := AddSigleWarehouseOutOne(&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 := AddSigleWarehouseOutInfoOne(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 := AddSigleWarehouseOutInfoOne(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. func GetNewWarehouseById(id int64, tx *gorm.DB) (models.WarehouseOut, error) {
  1916. out := models.WarehouseOut{}
  1917. err := tx.Where("id=? and status = 1", id).Find(&out).Error
  1918. if err != gorm.ErrRecordNotFound {
  1919. if err != nil {
  1920. return out, err
  1921. }
  1922. }
  1923. return out, err
  1924. }
  1925. // 耗材出库
  1926. func ConsumablesDeliveryOne(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64) (err error) {
  1927. ////开事务
  1928. //tx := XTWriteDB().Begin()
  1929. var deliver_number int64 = 0
  1930. var stock_number int64 = 0
  1931. var maxNumber int64 = 0
  1932. deliver_number = goods.Count
  1933. // 根据先进先出原则,查询最先入库的批次,进行出库
  1934. // 如果没有对应的库存,则报错
  1935. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  1936. if err != nil {
  1937. return err
  1938. }
  1939. stock_number = warehouse.StockCount
  1940. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1941. if stock_number >= deliver_number {
  1942. warehouseOutInfo := &models.WarehouseOutInfo{
  1943. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1944. WarehouseOutId: warehouseOut.ID,
  1945. WarehouseInfotId: warehouse.ID,
  1946. Status: 1,
  1947. Ctime: time.Now().Unix(),
  1948. Remark: goods.Remark,
  1949. OrgId: orgID,
  1950. Type: 1,
  1951. Manufacturer: goods.Manufacturer,
  1952. Dealer: goods.Dealer,
  1953. IsSys: 0,
  1954. SysRecordTime: record_time,
  1955. GoodTypeId: goods.GoodTypeId,
  1956. GoodId: goods.GoodId,
  1957. ExpiryDate: goods.ExpiryDate,
  1958. ProductDate: goods.ProductDate,
  1959. Number: warehouse.Number,
  1960. Price: goods.Price,
  1961. LicenseNumber: goods.LicenseNumber,
  1962. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1963. StorehouseId: goods.StorehouseId,
  1964. AdminUserId: goods.AdminUserId,
  1965. BuyPrice: warehouse.Price,
  1966. StockCount: goods.StockCount,
  1967. }
  1968. warehouseOutInfo.Count = goods.Count
  1969. //添加出库单详情
  1970. errOne := AddSigleWarehouseOutInfoOne(warehouseOutInfo)
  1971. if errOne != nil {
  1972. return errOne
  1973. }
  1974. stockFlow := models.VmStockFlow{
  1975. WarehousingId: warehouse.ID,
  1976. GoodId: goods.GoodId,
  1977. Number: warehouse.Number,
  1978. LicenseNumber: goods.LicenseNumber,
  1979. Count: goods.Count,
  1980. UserOrgId: goods.OrgId,
  1981. PatientId: goods.PatientId,
  1982. SystemTime: record_time,
  1983. ConsumableType: 2,
  1984. IsSys: goods.IsSys,
  1985. WarehousingOrder: "",
  1986. WarehouseOutId: warehouseOut.ID,
  1987. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1988. IsEdit: 1,
  1989. CancelStockId: 0,
  1990. CancelOrderNumber: "",
  1991. Manufacturer: goods.Manufacturer,
  1992. Dealer: goods.Dealer,
  1993. Creator: creator,
  1994. UpdateCreator: 0,
  1995. Status: 1,
  1996. Ctime: time.Now().Unix(),
  1997. Mtime: 0,
  1998. Price: warehouse.PackingPrice,
  1999. WarehousingDetailId: 0,
  2000. WarehouseOutDetailId: warehouseOutInfo.ID,
  2001. CancelOutDetailId: 0,
  2002. ProductDate: goods.ProductDate,
  2003. ExpireDate: goods.ExpiryDate,
  2004. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2005. StorehouseId: warehouse.StorehouseId,
  2006. AdminUserId: goods.AdminUserId,
  2007. BuyPrice: warehouse.Price,
  2008. StockCount: goods.StockCount,
  2009. }
  2010. //创建出库流水
  2011. CreateStockFlowOne(stockFlow)
  2012. maxNumber = goods.Count
  2013. if warehouse.StockCount < maxNumber {
  2014. return errors.New("库存数量不足")
  2015. }
  2016. warehouse.StockCount = warehouse.StockCount - maxNumber
  2017. warehouse.Mtime = time.Now().Unix()
  2018. //扣减库存
  2019. errThree := UpDateWarehouseInfoByStock(&warehouse)
  2020. fmt.Println(errThree)
  2021. //if errThree != nil {
  2022. // utils.ErrorLog("事务失败,原因为: %v", errThree.Error())
  2023. // tx.Rollback()
  2024. //} else {
  2025. // tx.Commit()
  2026. //}
  2027. //查询该机构默认仓库
  2028. storeConfig, _ := GetAllStoreHouseConfig(goods.OrgId)
  2029. //查询剩余库存
  2030. goodList, _ := GetGoodSumCountByStoreId(storeConfig.StorehouseOutInfo, goods.GoodId, goods.OrgId)
  2031. var sum_count int64
  2032. var sum_in_count int64
  2033. for _, item := range goodList {
  2034. sum_count += item.StockCount
  2035. sum_in_count += item.WarehousingCount
  2036. }
  2037. UpdateGoodByGoodId(goods.GoodId, sum_count, sum_in_count, goods.OrgId)
  2038. if errThree != nil {
  2039. return errThree
  2040. }
  2041. } else {
  2042. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2043. warehouseOutInfo := &models.WarehouseOutInfo{
  2044. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2045. WarehouseOutId: warehouseOut.ID,
  2046. WarehouseInfotId: warehouse.ID,
  2047. Status: 1,
  2048. Ctime: time.Now().Unix(),
  2049. Remark: goods.Remark,
  2050. OrgId: orgID,
  2051. Type: 1,
  2052. Manufacturer: goods.Manufacturer,
  2053. Dealer: goods.Dealer,
  2054. IsSys: 0,
  2055. SysRecordTime: record_time,
  2056. GoodTypeId: goods.GoodTypeId,
  2057. GoodId: goods.GoodId,
  2058. ExpiryDate: goods.ExpiryDate,
  2059. ProductDate: goods.ProductDate,
  2060. Number: warehouse.Number,
  2061. Price: warehouse.PackingPrice,
  2062. LicenseNumber: goods.LicenseNumber,
  2063. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2064. StorehouseId: warehouse.StorehouseId,
  2065. AdminUserId: goods.AdminUserId,
  2066. BuyPrice: warehouse.Price,
  2067. StockCount: goods.StockCount,
  2068. IsCheck: 1,
  2069. }
  2070. warehouseOutInfo.Count = stock_number
  2071. //查询是否已经生成出库单
  2072. info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  2073. if errcodes == gorm.ErrRecordNotFound {
  2074. //创建出库单
  2075. errOne := AddSigleWarehouseOutInfoOne(warehouseOutInfo)
  2076. if errOne != nil {
  2077. return errOne
  2078. }
  2079. } else {
  2080. warehouseOutInfo.Count = info.Count + stock_number
  2081. UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  2082. }
  2083. stockFlow := models.VmStockFlow{
  2084. WarehousingId: warehouse.ID,
  2085. GoodId: goods.GoodId,
  2086. Number: warehouse.Number,
  2087. LicenseNumber: goods.LicenseNumber,
  2088. Count: stock_number,
  2089. UserOrgId: goods.OrgId,
  2090. PatientId: 0,
  2091. SystemTime: record_time,
  2092. ConsumableType: 2,
  2093. IsSys: 0,
  2094. WarehousingOrder: "",
  2095. WarehouseOutId: warehouseOut.ID,
  2096. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2097. IsEdit: 1,
  2098. CancelStockId: 0,
  2099. CancelOrderNumber: "",
  2100. Manufacturer: goods.Manufacturer,
  2101. Dealer: goods.Dealer,
  2102. Creator: creator,
  2103. UpdateCreator: 0,
  2104. Status: 1,
  2105. Ctime: time.Now().Unix(),
  2106. Mtime: 0,
  2107. Price: warehouse.PackingPrice,
  2108. WarehousingDetailId: 0,
  2109. WarehouseOutDetailId: warehouseOutInfo.ID,
  2110. CancelOutDetailId: 0,
  2111. ProductDate: goods.ProductDate,
  2112. ExpireDate: goods.ExpiryDate,
  2113. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2114. StorehouseId: goods.StorehouseId,
  2115. AdminUserId: goods.AdminUserId,
  2116. BuyPrice: warehouse.Price,
  2117. StockCount: goods.StockCount,
  2118. }
  2119. //创建出库明细
  2120. CreateStockFlowOne(stockFlow)
  2121. //查询该机构默认仓库
  2122. storeConfig, _ := GetAllStoreHouseConfig(goods.OrgId)
  2123. //查询剩余库存
  2124. goodList, _ := GetGoodSumCountByStoreId(storeConfig.StorehouseOutInfo, goods.GoodId, goods.OrgId)
  2125. var sum_count int64
  2126. var sum_in_count int64
  2127. for _, item := range goodList {
  2128. sum_count += item.StockCount
  2129. sum_in_count += item.WarehousingCount
  2130. }
  2131. UpdateGoodByGoodId(goods.GoodId, sum_count, sum_in_count, goods.OrgId)
  2132. // 出库完成后,要将该批次库存清零
  2133. warehouse.StockCount = 0
  2134. warehouse.Mtime = time.Now().Unix()
  2135. errThree := UpDateWarehouseInfoByStock(&warehouse)
  2136. fmt.Println(errThree)
  2137. //if errThree != nil {
  2138. // utils.ErrorLog("事务失败,原因为: %v", errThree.Error())
  2139. // tx.Rollback()
  2140. //} else {
  2141. // tx.Commit()
  2142. //}
  2143. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2144. goods.Count = deliver_number - stock_number
  2145. ConsumablesDeliveryOne(orgID, record_time, goods, warehouseOut, count, creator)
  2146. }
  2147. return nil
  2148. }
  2149. // 退库逻辑
  2150. func ConsumablesDeliveryTwo(orgID int64, goods *models.WarehouseOutInfo, count int64) (err error) {
  2151. ////开事务
  2152. //tx := XTWriteDB().Begin()
  2153. var deliver_number int64 = 0
  2154. var stock_number int64 = 0
  2155. var maxNumber int64 = 0
  2156. deliver_number = goods.Count
  2157. // 根据先进先出原则,查询最先入库的批次,进行出库
  2158. warehouse, err := FindFirstWarehousingInfoByGoodId(goods.GoodId, goods.ID, goods.StorehouseId)
  2159. if err != nil {
  2160. return err
  2161. }
  2162. stock_number = warehouse.StockCount
  2163. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  2164. if stock_number >= deliver_number {
  2165. warehouseOutInfo := &models.WarehouseOutInfo{
  2166. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  2167. WarehouseOutId: goods.ID,
  2168. WarehouseInfotId: warehouse.ID,
  2169. Status: 1,
  2170. Ctime: time.Now().Unix(),
  2171. Remark: goods.Remark,
  2172. OrgId: orgID,
  2173. Type: 1,
  2174. Manufacturer: goods.Manufacturer,
  2175. Dealer: goods.Dealer,
  2176. IsSys: 0,
  2177. GoodTypeId: goods.GoodTypeId,
  2178. GoodId: goods.GoodId,
  2179. ExpiryDate: goods.ExpiryDate,
  2180. ProductDate: goods.ProductDate,
  2181. Number: goods.Number,
  2182. Price: warehouse.PackingPrice,
  2183. LicenseNumber: goods.LicenseNumber,
  2184. ConsumableType: 2,
  2185. }
  2186. warehouseOutInfo.Count = goods.Count
  2187. errOne := AddSigleWarehouseOutInfoOne(warehouseOutInfo)
  2188. if errOne != nil {
  2189. return errOne
  2190. }
  2191. maxNumber = goods.Count
  2192. if warehouse.StockCount < maxNumber { //这段逻辑根本不会执行,因为当库存数量大于或等于出库数量才会进入这个分支
  2193. return errors.New("库存数量不足")
  2194. }
  2195. warehouse.StockCount = warehouse.StockCount - maxNumber
  2196. warehouse.Mtime = time.Now().Unix()
  2197. if warehouse.StockCount <= 0 {
  2198. return errors.New("库存数量不足")
  2199. }
  2200. errThree := UpDateWarehouseInfoByStock(&warehouse)
  2201. fmt.Println(errThree)
  2202. //if errThree != nil {
  2203. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  2204. // tx.Rollback()
  2205. //} else {
  2206. // tx.Commit()
  2207. //}
  2208. return
  2209. } else {
  2210. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2211. warehouseOutInfo := &models.WarehouseOutInfo{
  2212. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  2213. WarehouseOutId: goods.ID,
  2214. WarehouseInfotId: warehouse.ID,
  2215. Status: 1,
  2216. Ctime: time.Now().Unix(),
  2217. Remark: goods.Remark,
  2218. OrgId: orgID,
  2219. Type: 1,
  2220. Manufacturer: goods.Manufacturer,
  2221. Dealer: goods.Dealer,
  2222. IsSys: 0,
  2223. GoodTypeId: goods.GoodTypeId,
  2224. GoodId: goods.GoodId,
  2225. ExpiryDate: goods.ExpiryDate,
  2226. ProductDate: goods.ProductDate,
  2227. Number: goods.Number,
  2228. Price: warehouse.PackingPrice,
  2229. LicenseNumber: goods.LicenseNumber,
  2230. ConsumableType: 2,
  2231. IsCheck: 1,
  2232. StorehouseId: goods.StorehouseId,
  2233. }
  2234. warehouseOutInfo.Count = stock_number
  2235. errOne := AddSigleWarehouseOutInfoOne(warehouseOutInfo)
  2236. if errOne != nil {
  2237. return errOne
  2238. }
  2239. // 出库完成后,要将该批次库存清零
  2240. warehouse.StockCount = 0
  2241. warehouse.Mtime = time.Now().Unix()
  2242. errThree := UpDateWarehouseInfoByStock(&warehouse)
  2243. //if errThree != nil {
  2244. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  2245. // tx.Rollback()
  2246. //} else {
  2247. // tx.Commit()
  2248. //}
  2249. fmt.Println(errThree)
  2250. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2251. goods.Count = deliver_number - stock_number
  2252. ConsumablesDeliveryTwo(orgID, goods, count)
  2253. }
  2254. return nil
  2255. }
  2256. func DeleteWarehouseInfo(id int64) error {
  2257. err := XTWriteDB().Model(models.WarehousingInfo{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": 0}).Error
  2258. return err
  2259. }
  2260. // 药品出库 递归方式
  2261. func NewDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DoctorAdvice) (err error) {
  2262. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  2263. var deliver_number int64 = 0
  2264. var stock_number int64 = 0
  2265. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  2266. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  2267. var drug_price float64
  2268. //按最小单位数据计算
  2269. deliver_number = count
  2270. if advice.PrescribingNumberUnit == drup.MaxUnit {
  2271. drug_price = drup.RetailPrice
  2272. }
  2273. if advice.PrescribingNumberUnit == drup.MinUnit {
  2274. drug_price = drup.MinPrice
  2275. }
  2276. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  2277. drug_price = drup.RetailPrice
  2278. }
  2279. // 根据先进先出原则,查询最先入库的批次,进行出库
  2280. // 如果没有对应的库存,则报错
  2281. //查询默认仓库
  2282. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  2283. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2284. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  2285. var stockMax int64
  2286. var stockMin int64
  2287. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  2288. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  2289. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  2290. UpdateMinNumber(lastWarehouse.ID, stockMin)
  2291. }
  2292. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2293. if err != nil {
  2294. return err
  2295. }
  2296. // 将该批次的剩余库存数量转换为拆零数量
  2297. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  2298. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  2299. if stock_number >= deliver_number {
  2300. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2301. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2302. WarehouseOutId: warehouseout.ID,
  2303. Status: 1,
  2304. Ctime: time.Now().Unix(),
  2305. Remark: warehouse.Remark,
  2306. OrgId: orgID,
  2307. Type: 1,
  2308. Manufacturer: warehouse.Manufacturer,
  2309. Dealer: warehouse.Dealer,
  2310. IsSys: 1,
  2311. SysRecordTime: advice.RecordDate,
  2312. DrugId: advice.DrugId,
  2313. Number: warehouse.Number,
  2314. BatchNumber: warehouse.BatchNumber,
  2315. Price: warehouse.RetailPrice,
  2316. CountUnit: drup.MinUnit,
  2317. RetailPrice: drug_price,
  2318. ProductDate: warehouse.ProductDate,
  2319. ExpiryDate: warehouse.ExpiryDate,
  2320. PatientId: advice.PatientId,
  2321. Count: deliver_number, //出最小单位
  2322. WarehouseInfoId: warehouse.ID,
  2323. AdviceId: advice.ID,
  2324. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2325. StorehouseId: storeConfig.DrugStorehouseOut,
  2326. IsCheck: 1,
  2327. }
  2328. drugflow := models.DrugFlow{
  2329. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2330. WarehouseOutId: warehouseout.ID,
  2331. DrugId: advice.DrugId,
  2332. Number: warehouse.Number,
  2333. ProductDate: warehouse.ProductDate,
  2334. ExpireDate: warehouse.ExpiryDate,
  2335. Count: deliver_number, //按最小单位计算,
  2336. Price: warehouse.RetailPrice,
  2337. Status: 1,
  2338. Ctime: time.Now().Unix(),
  2339. UserOrgId: orgID,
  2340. Manufacturer: warehouse.Manufacturer,
  2341. Dealer: warehouse.Dealer,
  2342. BatchNumber: warehouse.BatchNumber,
  2343. MaxUnit: drup.MinUnit,
  2344. ConsumableType: 3,
  2345. IsEdit: 1,
  2346. Creator: advice.ExecutionStaff,
  2347. IsSys: 1,
  2348. PatientId: advice.PatientId,
  2349. WarehousingDetailId: warehouse.ID,
  2350. AdviceId: advice.ID,
  2351. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2352. StorehouseId: storeConfig.DrugStorehouseOut,
  2353. }
  2354. CreateDrugFlowOne(drugflow)
  2355. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2356. if errOne != nil {
  2357. return errOne
  2358. }
  2359. //查询是否存在数据
  2360. details := &models.DrugAutomaticReduceDetail{
  2361. WarehouseOutId: warehouseout.ID,
  2362. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2363. PatientId: advice.PatientId,
  2364. Ctime: time.Now().Unix(),
  2365. Mtime: time.Now().Unix(),
  2366. Status: 1,
  2367. RecordTime: advice.RecordDate,
  2368. OrgId: orgID,
  2369. DrugId: advice.DrugId,
  2370. Count: deliver_number,
  2371. CountUnit: drup.MinUnit,
  2372. WarehouseInfoId: warehouse.ID,
  2373. StorehouseId: storeConfig.DrugStorehouseOut,
  2374. }
  2375. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2376. if errTwo != nil {
  2377. return errTwo
  2378. }
  2379. // 出库完成后,要减去对应批次的库存数量
  2380. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  2381. var maxNumber int64 = 0
  2382. var minNumber int64 = 0
  2383. //var stock_max_number int64 = 0
  2384. //stock_max_number = warehouse.StockMaxNumber
  2385. maxNumber = deliver_number / drup.MinNumber
  2386. minNumber = deliver_number % drup.MinNumber
  2387. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  2388. minNumber = maxNumber
  2389. }
  2390. if drup.MaxUnit != drup.MinUnit {
  2391. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  2392. return errors.New("库存数量不足")
  2393. }
  2394. }
  2395. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  2396. if warehouse.StockMaxNumber < 0 {
  2397. warehouse.StockMaxNumber = 0
  2398. }
  2399. if warehouse.StockMinNumber < 0 {
  2400. warehouse.StockMinNumber = 0
  2401. }
  2402. warehouse.Mtime = time.Now().Unix()
  2403. if warehouse.StockMinNumber < minNumber {
  2404. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  2405. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  2406. } else {
  2407. if minNumber > 0 {
  2408. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2409. }
  2410. if minNumber == 0 && maxNumber != 1 {
  2411. if warehouse.StockMinNumber > 0 {
  2412. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2413. }
  2414. }
  2415. }
  2416. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  2417. if (warehouse.StockMinNumber - deliver_number) > 0 {
  2418. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2419. //if warehouse.StockMinNumber == 0 {
  2420. // warehouse.StockMaxNumber = stock_max_number
  2421. //}
  2422. }
  2423. }
  2424. if drup.MaxUnit != drup.MinUnit {
  2425. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  2426. return errors.New("库存数量不足")
  2427. }
  2428. }
  2429. if warehouse.StockMinNumber <= 0 {
  2430. warehouse.StockMinNumber = 0
  2431. }
  2432. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  2433. if errThree != nil {
  2434. return errThree
  2435. }
  2436. return nil
  2437. } else {
  2438. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2439. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2440. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2441. WarehouseOutId: warehouseout.ID,
  2442. Status: 1,
  2443. Ctime: time.Now().Unix(),
  2444. Remark: warehouse.Remark,
  2445. OrgId: orgID,
  2446. Type: 1,
  2447. Manufacturer: warehouse.Manufacturer,
  2448. Dealer: warehouse.Dealer,
  2449. IsSys: 1,
  2450. SysRecordTime: advice.RecordDate,
  2451. DrugId: advice.DrugId,
  2452. Number: warehouse.Number,
  2453. BatchNumber: warehouse.BatchNumber,
  2454. Price: warehouse.RetailPrice,
  2455. CountUnit: drup.MinUnit,
  2456. ProductDate: warehouse.ProductDate,
  2457. ExpiryDate: warehouse.ExpiryDate,
  2458. PatientId: advice.PatientId,
  2459. Count: deliver_number,
  2460. WarehouseInfoId: warehouse.ID,
  2461. AdviceId: advice.ID,
  2462. StorehouseId: storeConfig.DrugStorehouseOut,
  2463. IsCheck: 1,
  2464. }
  2465. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2466. if errOne != nil {
  2467. return errOne
  2468. }
  2469. details := &models.DrugAutomaticReduceDetail{
  2470. WarehouseOutId: warehouseout.ID,
  2471. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  2472. PatientId: advice.PatientId,
  2473. Ctime: time.Now().Unix(),
  2474. Mtime: time.Now().Unix(),
  2475. Status: 1,
  2476. RecordTime: advice.RecordDate,
  2477. OrgId: orgID,
  2478. DrugId: advice.DrugId,
  2479. Count: deliver_number,
  2480. CountUnit: drup.MinUnit,
  2481. WarehouseInfoId: warehouse.ID,
  2482. StorehouseId: storeConfig.DrugStorehouseOut,
  2483. }
  2484. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2485. if errTwo != nil {
  2486. return errTwo
  2487. }
  2488. // 出库完成后,要将该批次库存清零
  2489. warehouse.StockMaxNumber = 0
  2490. warehouse.StockMinNumber = 0
  2491. warehouse.Mtime = time.Now().Unix()
  2492. info := models.XtDrugWarehouseInfo{
  2493. ID: warehouse.ID,
  2494. WarehousingId: warehouse.WarehousingId,
  2495. DrugId: warehouse.DrugId,
  2496. Number: warehouse.Number,
  2497. ProductDate: warehouse.ProductDate,
  2498. ExpiryDate: warehouse.ExpiryDate,
  2499. WarehousingCount: warehouse.WarehousingCount,
  2500. Price: warehouse.Price,
  2501. TotalPrice: warehouse.TotalPrice,
  2502. Dealer: warehouse.Dealer,
  2503. Manufacturer: warehouse.Manufacturer,
  2504. Remark: warehouse.Remark,
  2505. Ctime: warehouse.Ctime,
  2506. Mtime: warehouse.Mtime,
  2507. Status: 1,
  2508. OrgId: warehouse.OrgId,
  2509. IsReturn: warehouse.IsReturn,
  2510. WarehousingOrder: warehouse.WarehousingOrder,
  2511. Type: warehouse.Type,
  2512. RetailPrice: warehouse.RetailPrice,
  2513. RetailTotalPrice: warehouse.RetailPrice,
  2514. StockMaxNumber: 0,
  2515. StockMinNumber: 0,
  2516. BatchNumber: warehouse.BatchNumber,
  2517. MaxUnit: warehouse.MaxUnit,
  2518. WarehousingInfoId: warehouse.WarehousingInfoId,
  2519. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2520. StorehouseId: storeConfig.DrugStorehouseOut,
  2521. IsCheck: 1,
  2522. }
  2523. //扣减库存
  2524. errThree := UpDateDrugWarehouseInfoByStock(&info)
  2525. if errThree != nil {
  2526. return errThree
  2527. }
  2528. drugflow := models.DrugFlow{
  2529. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2530. WarehouseOutId: warehouseout.ID,
  2531. DrugId: advice.DrugId,
  2532. Number: warehouse.Number,
  2533. ProductDate: warehouse.ProductDate,
  2534. ExpireDate: warehouse.ExpiryDate,
  2535. Count: deliver_number,
  2536. Price: warehouse.RetailPrice,
  2537. Status: 1,
  2538. Ctime: time.Now().Unix(),
  2539. UserOrgId: orgID,
  2540. Manufacturer: warehouse.Manufacturer,
  2541. Dealer: warehouse.Dealer,
  2542. BatchNumber: warehouse.BatchNumber,
  2543. MaxUnit: drup.MinUnit,
  2544. ConsumableType: 3,
  2545. IsEdit: 1,
  2546. Creator: advice.ExecutionStaff,
  2547. IsSys: 1,
  2548. PatientId: advice.PatientId,
  2549. WarehousingDetailId: warehouse.ID,
  2550. AdviceId: advice.ID,
  2551. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2552. StorehouseId: storeConfig.DrugStorehouseOut,
  2553. }
  2554. CreateDrugFlowOne(drugflow)
  2555. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2556. prescribingNumber_two_temp := deliver_number - stock_number
  2557. overPlusNumber := float64(prescribingNumber_two_temp)
  2558. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  2559. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  2560. NewDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  2561. }
  2562. return
  2563. }
  2564. // 药品出库 递归方式
  2565. func NewHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  2566. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  2567. var deliver_number int64 = 0
  2568. var stock_number int64 = 0
  2569. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  2570. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  2571. deliver_number = count
  2572. var min_price float64
  2573. if advice.PrescribingNumberUnit == drup.MaxUnit {
  2574. min_price = drup.RetailPrice
  2575. }
  2576. if advice.PrescribingNumberUnit == drup.MinUnit {
  2577. min_price = drup.MinPrice
  2578. }
  2579. if advice.PrescribingNumberUnit != drup.MaxUnit && advice.PrescribingNumberUnit != drup.MinUnit {
  2580. min_price = drup.RetailPrice
  2581. }
  2582. fmt.Println(min_price)
  2583. // 根据先进先出原则,查询最先入库的批次,进行出库
  2584. // 如果没有对应的库存,则报错
  2585. //开启事物
  2586. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  2587. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2588. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  2589. var stockMax int64
  2590. var stockMin int64
  2591. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  2592. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  2593. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  2594. UpdateMinNumber(lastWarehouse.ID, stockMin)
  2595. }
  2596. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2597. //查找药品信息
  2598. if err != nil {
  2599. return err
  2600. }
  2601. // 将该批次的剩余库存数量转换为拆零数量
  2602. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  2603. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  2604. if stock_number >= deliver_number {
  2605. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2606. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2607. WarehouseOutId: warehouseout.ID,
  2608. Status: 1,
  2609. Ctime: time.Now().Unix(),
  2610. Remark: warehouse.Remark,
  2611. OrgId: orgID,
  2612. Type: 1,
  2613. Manufacturer: warehouse.Manufacturer,
  2614. Dealer: warehouse.Dealer,
  2615. IsSys: 1,
  2616. SysRecordTime: advice.RecordDate,
  2617. DrugId: advice.DrugId,
  2618. Number: warehouse.Number,
  2619. BatchNumber: warehouse.BatchNumber,
  2620. Price: warehouse.RetailPrice,
  2621. CountUnit: drup.MinUnit,
  2622. RetailPrice: warehouse.RetailPrice,
  2623. ProductDate: warehouse.ProductDate,
  2624. ExpiryDate: warehouse.ExpiryDate,
  2625. PatientId: advice.PatientId,
  2626. WarehouseInfoId: warehouse.ID,
  2627. Count: deliver_number,
  2628. AdviceId: advice.ID,
  2629. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2630. StorehouseId: storeConfig.DrugStorehouseOut,
  2631. IsCheck: 1,
  2632. }
  2633. drugflow := models.DrugFlow{
  2634. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2635. WarehouseOutId: warehouseout.ID,
  2636. DrugId: advice.DrugId,
  2637. Number: warehouse.Number,
  2638. ProductDate: warehouse.ProductDate,
  2639. ExpireDate: warehouse.ExpiryDate,
  2640. Count: deliver_number,
  2641. Price: warehouse.RetailPrice,
  2642. Status: 1,
  2643. Ctime: time.Now().Unix(),
  2644. UserOrgId: orgID,
  2645. Manufacturer: warehouse.Manufacturer,
  2646. Dealer: warehouse.Dealer,
  2647. BatchNumber: warehouse.BatchNumber,
  2648. MaxUnit: drup.MinUnit,
  2649. ConsumableType: 3,
  2650. IsEdit: 1,
  2651. Creator: advice.ExecutionStaff,
  2652. IsSys: 1,
  2653. PatientId: advice.PatientId,
  2654. SystemTime: advice.RecordDate,
  2655. WarehousingDetailId: warehouse.ID,
  2656. AdviceId: advice.ID,
  2657. StorehouseId: storeConfig.DrugStorehouseOut,
  2658. }
  2659. CreateDrugFlowOne(drugflow)
  2660. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2661. if errOne != nil {
  2662. return errOne
  2663. }
  2664. details := &models.DrugAutomaticReduceDetail{
  2665. WarehouseOutId: warehouseout.ID,
  2666. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2667. PatientId: advice.PatientId,
  2668. Ctime: time.Now().Unix(),
  2669. Mtime: time.Now().Unix(),
  2670. Status: 1,
  2671. RecordTime: advice.RecordDate,
  2672. OrgId: orgID,
  2673. DrugId: advice.DrugId,
  2674. Count: deliver_number,
  2675. CountUnit: drup.MinUnit,
  2676. WarehouseInfoId: warehouse.ID,
  2677. AdviceId: advice.ID,
  2678. StorehouseId: storeConfig.DrugStorehouseOut,
  2679. }
  2680. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2681. if errTwo != nil {
  2682. return errTwo
  2683. }
  2684. // 出库完成后,要减去对应批次的库存数量
  2685. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  2686. var maxNumber int64 = 0
  2687. var minNumber int64 = 0
  2688. //var stock_max_number int64 = 0
  2689. //stock_max_number = warehouse.StockMaxNumber
  2690. maxNumber = deliver_number / drup.MinNumber
  2691. minNumber = deliver_number % drup.MinNumber
  2692. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  2693. minNumber = maxNumber
  2694. }
  2695. if drup.MaxUnit != drup.MinUnit {
  2696. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  2697. return errors.New("库存数量不足")
  2698. }
  2699. }
  2700. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  2701. if warehouse.StockMaxNumber < 0 {
  2702. warehouse.StockMaxNumber = 0
  2703. }
  2704. if warehouse.StockMinNumber < 0 {
  2705. warehouse.StockMinNumber = 0
  2706. }
  2707. warehouse.Mtime = time.Now().Unix()
  2708. if warehouse.StockMinNumber < minNumber {
  2709. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  2710. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  2711. } else {
  2712. if minNumber > 0 {
  2713. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2714. }
  2715. if minNumber == 0 && maxNumber != 1 {
  2716. if warehouse.StockMinNumber > 0 {
  2717. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2718. }
  2719. }
  2720. }
  2721. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  2722. if (warehouse.StockMinNumber - deliver_number) > 0 {
  2723. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2724. //if warehouse.StockMinNumber == 0 {
  2725. // warehouse.StockMaxNumber = stock_max_number
  2726. //}
  2727. }
  2728. }
  2729. if drup.MaxUnit != drup.MinUnit {
  2730. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  2731. return errors.New("库存数量不足")
  2732. }
  2733. }
  2734. if warehouse.StockMinNumber <= 0 {
  2735. warehouse.StockMinNumber = 0
  2736. }
  2737. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  2738. if errThree != nil {
  2739. return errThree
  2740. }
  2741. return nil
  2742. } else {
  2743. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2744. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2745. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2746. WarehouseOutId: warehouseout.ID,
  2747. Status: 1,
  2748. Ctime: time.Now().Unix(),
  2749. Remark: warehouse.Remark,
  2750. OrgId: orgID,
  2751. Type: 1,
  2752. Manufacturer: warehouse.Manufacturer,
  2753. Dealer: warehouse.Dealer,
  2754. IsSys: 1,
  2755. SysRecordTime: advice.RecordDate,
  2756. DrugId: advice.DrugId,
  2757. Number: warehouse.Number,
  2758. BatchNumber: warehouse.BatchNumber,
  2759. Price: warehouse.RetailPrice,
  2760. RetailPrice: warehouse.RetailPrice,
  2761. ProductDate: warehouse.ProductDate,
  2762. ExpiryDate: warehouse.ExpiryDate,
  2763. PatientId: advice.PatientId,
  2764. WarehouseInfoId: warehouse.ID,
  2765. Count: stock_number,
  2766. CountUnit: drup.MinUnit,
  2767. AdviceId: advice.ID,
  2768. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2769. StorehouseId: storeConfig.DrugStorehouseOut,
  2770. IsCheck: 1,
  2771. }
  2772. drugflow := models.DrugFlow{
  2773. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2774. WarehouseOutId: warehouseout.ID,
  2775. DrugId: advice.DrugId,
  2776. Number: warehouse.Number,
  2777. ProductDate: warehouse.ProductDate,
  2778. ExpireDate: warehouse.ExpiryDate,
  2779. Count: stock_number,
  2780. Price: warehouse.RetailPrice,
  2781. Status: 1,
  2782. Ctime: time.Now().Unix(),
  2783. UserOrgId: orgID,
  2784. Manufacturer: warehouse.Manufacturer,
  2785. Dealer: warehouse.Dealer,
  2786. BatchNumber: warehouse.BatchNumber,
  2787. MaxUnit: drup.MinUnit,
  2788. ConsumableType: 3,
  2789. IsEdit: 1,
  2790. Creator: advice.ExecutionStaff,
  2791. IsSys: 1,
  2792. PatientId: advice.PatientId,
  2793. SystemTime: advice.RecordDate,
  2794. WarehousingDetailId: warehouse.ID,
  2795. AdviceId: advice.ID,
  2796. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2797. StorehouseId: storeConfig.DrugStorehouseOut,
  2798. }
  2799. CreateDrugFlowOne(drugflow)
  2800. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2801. if errOne != nil {
  2802. return errOne
  2803. }
  2804. details := &models.DrugAutomaticReduceDetail{
  2805. WarehouseOutId: warehouseout.ID,
  2806. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2807. PatientId: advice.PatientId,
  2808. Ctime: time.Now().Unix(),
  2809. Mtime: time.Now().Unix(),
  2810. Status: 1,
  2811. RecordTime: advice.RecordDate,
  2812. OrgId: orgID,
  2813. DrugId: advice.DrugId,
  2814. Count: stock_number,
  2815. CountUnit: drup.MinUnit,
  2816. WarehouseInfoId: warehouse.ID,
  2817. AdviceId: advice.ID,
  2818. StorehouseId: storeConfig.DrugStorehouseOut,
  2819. }
  2820. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2821. if errTwo != nil {
  2822. return errTwo
  2823. }
  2824. info := models.XtDrugWarehouseInfo{
  2825. ID: warehouse.ID,
  2826. WarehousingId: warehouse.WarehousingId,
  2827. DrugId: warehouse.DrugId,
  2828. Number: warehouse.Number,
  2829. ProductDate: warehouse.ProductDate,
  2830. ExpiryDate: warehouse.ExpiryDate,
  2831. WarehousingCount: warehouse.WarehousingCount,
  2832. Price: warehouse.RetailPrice,
  2833. TotalPrice: warehouse.TotalPrice,
  2834. Dealer: warehouse.Dealer,
  2835. Manufacturer: warehouse.Manufacturer,
  2836. Remark: warehouse.Remark,
  2837. Ctime: warehouse.Ctime,
  2838. Mtime: warehouse.Mtime,
  2839. Status: 1,
  2840. OrgId: warehouse.OrgId,
  2841. IsReturn: warehouse.IsReturn,
  2842. WarehousingOrder: warehouse.WarehousingOrder,
  2843. Type: warehouse.Type,
  2844. RetailPrice: warehouse.RetailPrice,
  2845. RetailTotalPrice: warehouse.RetailPrice,
  2846. StockMaxNumber: 0,
  2847. StockMinNumber: 0,
  2848. BatchNumber: warehouse.BatchNumber,
  2849. MaxUnit: warehouse.MaxUnit,
  2850. WarehousingInfoId: warehouse.WarehousingInfoId,
  2851. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2852. StorehouseId: storeConfig.DrugStorehouseOut,
  2853. IsCheck: 1,
  2854. }
  2855. errThree := UpDateDrugWarehouseInfoByStock(&info)
  2856. if errThree != nil {
  2857. return errThree
  2858. }
  2859. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2860. prescribingNumber_two_temp := deliver_number - stock_number
  2861. overPlusNumber := float64(prescribingNumber_two_temp)
  2862. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  2863. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  2864. NewHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  2865. }
  2866. return
  2867. }
  2868. // 药品出库 递归方式
  2869. func HisAuToReduce(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  2870. //查询最后一次出库数据
  2871. drugInfo, _ := GetDrugWarehouseOutInfoIsExit(advice.DrugId, advice.PatientId, advice.RecordDate)
  2872. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  2873. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  2874. var count_number int64
  2875. var device_number int64
  2876. if advice.PrescribingNumberUnit == drup.MaxUnit {
  2877. device_number = count * drup.MinNumber
  2878. }
  2879. if advice.PrescribingNumberUnit == drup.MinUnit {
  2880. device_number = count
  2881. }
  2882. if drugInfo.CountUnit == drup.MaxUnit {
  2883. count_number = drugInfo.Count * drup.MinNumber
  2884. }
  2885. if drugInfo.CountUnit == drup.MinUnit {
  2886. count_number = drugInfo.Count
  2887. }
  2888. // 比较数据大小
  2889. //如果当期医嘱数量小于最后一次出库则退库
  2890. if device_number < count_number {
  2891. var cha_number int64
  2892. var maxNumber int64
  2893. var minNumber int64
  2894. cha_number = count_number - device_number
  2895. maxNumber = cha_number / drup.MinNumber
  2896. minNumber = cha_number % drup.MinNumber
  2897. //更改库存
  2898. ModifyDrugWarehouse(drugInfo.WarehouseInfoId, maxNumber, minNumber)
  2899. //修改出库表
  2900. info := models.DrugWarehouseOutInfo{
  2901. Count: device_number,
  2902. CountUnit: advice.PrescribingNumberUnit,
  2903. }
  2904. ModifyDrugWarehouseOutInfo(info, advice.DrugId, advice.PatientId, advice.RecordDate)
  2905. //修改自动出库表
  2906. detail := models.DrugAutomaticReduceDetail{
  2907. Count: device_number,
  2908. CountUnit: advice.PrescribingNumberUnit,
  2909. }
  2910. ModifyAutoMaticReduce(detail, advice.PatientId, advice.RecordDate, advice.DrugId)
  2911. //修改流水
  2912. flow := models.DrugFlow{
  2913. Count: device_number,
  2914. MaxUnit: drup.MinUnit,
  2915. }
  2916. ModifyDrugFlow(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId)
  2917. }
  2918. //如果当期医嘱数量大于最后一次出库则需要出库
  2919. if device_number > count_number {
  2920. var cha_number int64
  2921. cha_number = device_number - count_number
  2922. // 根据先进先出原则,查询最先入库的批次,进行出库
  2923. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  2924. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2925. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  2926. var stockMax int64
  2927. var stockMin int64
  2928. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  2929. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  2930. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  2931. UpdateMinNumber(lastWarehouse.ID, stockMin)
  2932. }
  2933. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2934. //查找药品信息
  2935. //baseInfo, _ := FindBaseDrugLibRecord(advice.UserOrgId, advice.DrugId)
  2936. if err != nil {
  2937. return err
  2938. }
  2939. // 将该批次的剩余库存数量转换为拆零数量
  2940. var stock_number int64
  2941. var maxNumber int64
  2942. var minNumber int64
  2943. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  2944. //如果库存数量大于出库数据,正常出库
  2945. if stock_number >= cha_number {
  2946. //扣减库存
  2947. maxNumber = cha_number / drup.MinNumber
  2948. minNumber = cha_number % drup.MinNumber
  2949. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  2950. minNumber = maxNumber
  2951. }
  2952. if drup.MaxUnit != drup.MinUnit {
  2953. if warehouse.StockMaxNumber < maxNumber {
  2954. return errors.New("库存数量不足")
  2955. }
  2956. }
  2957. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  2958. if warehouse.StockMaxNumber < 0 {
  2959. warehouse.StockMaxNumber = 0
  2960. }
  2961. warehouse.Mtime = time.Now().Unix()
  2962. if warehouse.StockMinNumber < minNumber {
  2963. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  2964. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  2965. } else {
  2966. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2967. }
  2968. if drup.MaxUnit != drup.MinUnit {
  2969. if warehouse.StockMaxNumber < 0 {
  2970. return errors.New("库存数量不足")
  2971. }
  2972. }
  2973. //扣减库存232332332332
  2974. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  2975. if errThree != nil {
  2976. return errThree
  2977. }
  2978. //修改出库表
  2979. info := models.DrugWarehouseOutInfo{
  2980. Count: device_number,
  2981. CountUnit: advice.PrescribingNumberUnit,
  2982. }
  2983. ModifyDrugWarehouseOutInfoOne(info, advice.DrugId, advice.PatientId, advice.RecordDate, warehouse.ID)
  2984. //修改自动出库表
  2985. detail := models.DrugAutomaticReduceDetail{
  2986. Count: device_number,
  2987. CountUnit: advice.PrescribingNumberUnit,
  2988. }
  2989. ModifyAutoMaticReduceOne(detail, advice.PatientId, advice.RecordDate, advice.DrugId, warehouse.ID)
  2990. //修改流水
  2991. flow := models.DrugFlow{
  2992. Count: device_number,
  2993. MaxUnit: advice.PrescribingNumberUnit,
  2994. }
  2995. ModifyDrugFlowOne(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId, warehouse.ID)
  2996. }
  2997. //如果库存数量小于出库数据,则需要出库到下一个批次
  2998. if stock_number < cha_number {
  2999. //修改出库表
  3000. info := models.DrugWarehouseOutInfo{
  3001. Count: cha_number,
  3002. CountUnit: advice.PrescribingNumberUnit,
  3003. }
  3004. ModifyDrugWarehouseOutInfoOne(info, advice.DrugId, advice.PatientId, advice.RecordDate, warehouse.ID)
  3005. //修改自动出库表
  3006. detail := models.DrugAutomaticReduceDetail{
  3007. Count: cha_number,
  3008. CountUnit: advice.PrescribingNumberUnit,
  3009. }
  3010. ModifyAutoMaticReduceOne(detail, advice.PatientId, advice.RecordDate, advice.DrugId, warehouse.ID)
  3011. //修改流水
  3012. flow := models.DrugFlow{
  3013. Count: cha_number,
  3014. MaxUnit: advice.PrescribingNumberUnit,
  3015. }
  3016. ModifyDrugFlowOne(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId, warehouse.ID)
  3017. druginfo := &models.XtDrugWarehouseInfo{
  3018. ID: warehouse.ID,
  3019. WarehousingId: warehouse.WarehousingId,
  3020. DrugId: warehouse.DrugId,
  3021. Number: warehouse.Number,
  3022. ProductDate: warehouse.ProductDate,
  3023. ExpiryDate: warehouse.ExpiryDate,
  3024. WarehousingCount: warehouse.WarehousingCount,
  3025. Price: warehouse.Price,
  3026. TotalPrice: warehouse.TotalPrice,
  3027. Dealer: warehouse.Dealer,
  3028. Manufacturer: warehouse.Manufacturer,
  3029. Remark: warehouse.Remark,
  3030. Ctime: warehouse.Ctime,
  3031. Mtime: warehouse.Mtime,
  3032. Status: 1,
  3033. OrgId: warehouse.OrgId,
  3034. IsReturn: warehouse.IsReturn,
  3035. WarehousingOrder: warehouse.WarehousingOrder,
  3036. Type: warehouse.Type,
  3037. RetailPrice: warehouse.RetailPrice,
  3038. RetailTotalPrice: warehouse.RetailPrice,
  3039. StockMaxNumber: 0,
  3040. StockMinNumber: 0,
  3041. BatchNumber: warehouse.BatchNumber,
  3042. MaxUnit: warehouse.MaxUnit,
  3043. WarehousingInfoId: warehouse.WarehousingInfoId,
  3044. StorehouseId: storeConfig.DrugStorehouseOut,
  3045. IsCheck: 1,
  3046. }
  3047. errThree := UpDateDrugWarehouseInfoByStock(druginfo)
  3048. if errThree != nil {
  3049. return errThree
  3050. }
  3051. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3052. prescribingNumber_two_temp := cha_number - stock_number
  3053. overPlusNumber := float64(prescribingNumber_two_temp)
  3054. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  3055. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  3056. NewHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  3057. }
  3058. }
  3059. return
  3060. }
  3061. // 采购管理耗材出库逻辑
  3062. 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) {
  3063. var deliver_number int64 = 0
  3064. var stock_number int64 = 0
  3065. var maxNumber int64 = 0
  3066. deliver_number = goods.Count
  3067. // 根据先进先出原则,查询最先入库的批次,进行出库
  3068. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  3069. // 如果没有对应的库存,则报错
  3070. warehouse, err := FindFirstWarehousingInfoByStockNight(goods.GoodId, goods.GoodTypeId, warehouse_out_id, goods.IsSource, houseConfig.StorehouseOutInfo)
  3071. if err != nil {
  3072. return err
  3073. }
  3074. stock_number = warehouse.StockCount
  3075. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3076. if stock_number >= deliver_number {
  3077. warehouseOutInfo := &models.WarehouseOutInfo{
  3078. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3079. WarehouseOutId: goods.WarehouseOutId,
  3080. WarehouseInfotId: warehouse.ID,
  3081. Status: 1,
  3082. Ctime: time.Now().Unix(),
  3083. Remark: goods.Remark,
  3084. OrgId: orgID,
  3085. Type: 1,
  3086. Manufacturer: goods.Manufacturer,
  3087. Dealer: goods.Dealer,
  3088. IsSys: 0,
  3089. SysRecordTime: record_time,
  3090. GoodTypeId: goods.GoodTypeId,
  3091. GoodId: goods.GoodId,
  3092. ExpiryDate: goods.ExpiryDate,
  3093. ProductDate: goods.ProductDate,
  3094. Number: warehouse.Number,
  3095. Price: goods.Price,
  3096. LicenseNumber: goods.LicenseNumber,
  3097. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  3098. StorehouseId: warehouse.StorehouseId,
  3099. IsCheck: 1,
  3100. }
  3101. warehouseOutInfo.Count = goods.Count
  3102. //添加出库单详情
  3103. errOne := AddSigleWarehouseOutInfoOne(warehouseOutInfo)
  3104. if errOne != nil {
  3105. return errOne
  3106. }
  3107. stockFlow := models.VmStockFlow{
  3108. WarehousingId: warehouse.ID,
  3109. GoodId: goods.GoodId,
  3110. Number: warehouse.Number,
  3111. LicenseNumber: goods.LicenseNumber,
  3112. Count: goods.Count,
  3113. UserOrgId: goods.OrgId,
  3114. PatientId: goods.PatientId,
  3115. SystemTime: record_time,
  3116. ConsumableType: 2,
  3117. IsSys: goods.IsSys,
  3118. WarehousingOrder: "",
  3119. WarehouseOutId: goods.WarehouseOutId,
  3120. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3121. IsEdit: 1,
  3122. CancelStockId: 0,
  3123. CancelOrderNumber: "",
  3124. Manufacturer: goods.Manufacturer,
  3125. Dealer: goods.Dealer,
  3126. Creator: creator,
  3127. UpdateCreator: 0,
  3128. Status: 1,
  3129. Ctime: time.Now().Unix(),
  3130. Mtime: 0,
  3131. Price: warehouse.PackingPrice,
  3132. WarehousingDetailId: 0,
  3133. WarehouseOutDetailId: warehouseOutInfo.ID,
  3134. CancelOutDetailId: 0,
  3135. ProductDate: goods.ProductDate,
  3136. ExpireDate: goods.ExpiryDate,
  3137. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  3138. SupplyWarehouseId: warehouseOut.SupplyWarehouseId,
  3139. StorehouseId: warehouse.StorehouseId,
  3140. }
  3141. //创建出库流水
  3142. CreateStockFlowOne(stockFlow)
  3143. maxNumber = goods.Count
  3144. if warehouse.StockCount < maxNumber {
  3145. return errors.New("库存数量不足")
  3146. }
  3147. warehouse.StockCount = warehouse.StockCount - maxNumber
  3148. warehouse.Mtime = time.Now().Unix()
  3149. //扣减库存
  3150. errThree := UpDateWarehouseInfoByStock(&warehouse)
  3151. //出库数量增加
  3152. AddGoodSumOutCount(houseConfig.StorehouseOutInfo, warehouse.GoodId, warehouse.OrgId, goods.Count)
  3153. ModifyGoodReduceInformationSix(warehouse.GoodId, maxNumber, warehouse.OrgId, warehouse.PackingPrice)
  3154. if errThree != nil {
  3155. return errThree
  3156. }
  3157. } else {
  3158. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  3159. warehouseOutInfo := &models.WarehouseOutInfo{
  3160. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3161. WarehouseOutId: goods.WarehouseOutId,
  3162. WarehouseInfotId: warehouse.ID,
  3163. Status: 1,
  3164. Ctime: time.Now().Unix(),
  3165. Remark: goods.Remark,
  3166. OrgId: orgID,
  3167. Type: 1,
  3168. Manufacturer: goods.Manufacturer,
  3169. Dealer: goods.Dealer,
  3170. IsSys: 0,
  3171. SysRecordTime: record_time,
  3172. GoodTypeId: goods.GoodTypeId,
  3173. GoodId: goods.GoodId,
  3174. ExpiryDate: goods.ExpiryDate,
  3175. ProductDate: goods.ProductDate,
  3176. Number: warehouse.Number,
  3177. Price: warehouse.PackingPrice,
  3178. LicenseNumber: goods.LicenseNumber,
  3179. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  3180. StorehouseId: warehouse.StorehouseId,
  3181. IsCheck: 1,
  3182. }
  3183. warehouseOutInfo.Count = stock_number
  3184. //查询是否已经生成出库单
  3185. info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  3186. if errcodes == gorm.ErrRecordNotFound {
  3187. //创建出库单
  3188. errOne := AddSigleWarehouseOutInfoOne(warehouseOutInfo)
  3189. if errOne != nil {
  3190. return errOne
  3191. }
  3192. } else {
  3193. warehouseOutInfo.Count = info.Count + stock_number
  3194. UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  3195. }
  3196. stockFlow := models.VmStockFlow{
  3197. WarehousingId: warehouse.ID,
  3198. GoodId: goods.GoodId,
  3199. Number: warehouse.Number,
  3200. LicenseNumber: goods.LicenseNumber,
  3201. Count: stock_number,
  3202. UserOrgId: goods.OrgId,
  3203. PatientId: 0,
  3204. SystemTime: record_time,
  3205. ConsumableType: 2,
  3206. IsSys: 0,
  3207. WarehousingOrder: "",
  3208. WarehouseOutId: goods.WarehouseOutId,
  3209. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3210. IsEdit: 1,
  3211. CancelStockId: 0,
  3212. CancelOrderNumber: "",
  3213. Manufacturer: goods.Manufacturer,
  3214. Dealer: goods.Dealer,
  3215. Creator: creator,
  3216. UpdateCreator: 0,
  3217. Status: 1,
  3218. Ctime: time.Now().Unix(),
  3219. Mtime: 0,
  3220. Price: warehouse.PackingPrice,
  3221. WarehousingDetailId: 0,
  3222. WarehouseOutDetailId: warehouseOutInfo.ID,
  3223. CancelOutDetailId: 0,
  3224. ProductDate: goods.ProductDate,
  3225. ExpireDate: goods.ExpiryDate,
  3226. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  3227. SupplyWarehouseId: warehouseOut.SupplyWarehouseId,
  3228. StorehouseId: warehouse.StorehouseId,
  3229. }
  3230. //创建出库明细
  3231. CreateStockFlowOne(stockFlow)
  3232. // 出库完成后,要将该批次库存清零
  3233. ModifyGoodReduceInformationSix(warehouse.GoodId, warehouse.StockCount, warehouse.OrgId, warehouse.PackingPrice)
  3234. warehouse.StockCount = 0
  3235. warehouse.Mtime = time.Now().Unix()
  3236. errThree := UpDateWarehouseInfoByStock(&warehouse)
  3237. if errThree != nil {
  3238. return errThree
  3239. }
  3240. //出库数量增加
  3241. AddGoodSumOutCount(houseConfig.StorehouseOutInfo, warehouse.GoodId, warehouse.OrgId, warehouse.StockCount)
  3242. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3243. goods.Count = deliver_number - stock_number
  3244. ConsumablesDeliveryNight(orgID, record_time, goods, warehouseOut, count, creator, warehouse_out_id, cancel_out_id)
  3245. }
  3246. return nil
  3247. }
  3248. func FindFirstWarehousingInfoByStockNight(good_id int64, good_type_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  3249. if is_source == 1 {
  3250. 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
  3251. }
  3252. if is_source == 2 || is_source == 0 {
  3253. 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
  3254. }
  3255. return info, err
  3256. }
  3257. // 药品出库 递归方式
  3258. 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) {
  3259. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  3260. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  3261. var deliver_number int64 = 0
  3262. var stock_number int64 = 0
  3263. var retail_price float64
  3264. if advice.CountUnit == drup.MaxUnit {
  3265. deliver_number = prescribingNumber * drup.MinNumber
  3266. formatInt := strconv.FormatInt(prescribingNumber, 10)
  3267. floatcount, _ := strconv.ParseFloat(formatInt, 64)
  3268. retail_price = floatcount * advice.Price
  3269. } else {
  3270. deliver_number = prescribingNumber
  3271. formatInt := strconv.FormatInt(prescribingNumber, 10)
  3272. floatcount, _ := strconv.ParseFloat(formatInt, 64)
  3273. retail_price = floatcount * advice.Price
  3274. }
  3275. // 根据先进先出原则,查询最先入库的批次,进行出库
  3276. // 如果没有对应的库存,则报错
  3277. //开启事物
  3278. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, houseConfig.DrugStorehouseOut)
  3279. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  3280. var stockMax int64
  3281. var stockMin int64
  3282. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  3283. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  3284. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  3285. UpdateMinNumber(lastWarehouse.ID, stockMin)
  3286. }
  3287. warehouse, err := FindLastDrugWarehousingInfoByIDSix(advice.DrugId, warehouse_out_id, advice.IsSource, houseConfig.DrugStorehouseOut)
  3288. if err != nil {
  3289. return err
  3290. }
  3291. // 将该批次的剩余库存数量转换为拆零数量
  3292. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  3293. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3294. if stock_number >= deliver_number {
  3295. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  3296. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3297. WarehouseOutId: advice.WarehouseOutId,
  3298. Status: 1,
  3299. Ctime: time.Now().Unix(),
  3300. Remark: advice.Remark,
  3301. OrgId: orgID,
  3302. Type: 1,
  3303. Manufacturer: advice.Manufacturer,
  3304. Dealer: warehouse.Dealer,
  3305. IsSys: 0,
  3306. SysRecordTime: advice.SysRecordTime,
  3307. DrugId: advice.DrugId,
  3308. ExpiryDate: advice.ExpiryDate,
  3309. ProductDate: advice.ProductDate,
  3310. Number: advice.Number,
  3311. BatchNumber: warehouse.BatchNumber,
  3312. Count: deliver_number,
  3313. RetailPrice: advice.RetailPrice,
  3314. Price: advice.Price,
  3315. WarehouseInfoId: warehouse.ID,
  3316. CountUnit: drup.MinUnit,
  3317. AdviceId: advice.ID,
  3318. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  3319. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  3320. StorehouseId: warehouse.StorehouseId,
  3321. IsCheck: 1,
  3322. }
  3323. warehouseOutInfo.Count = prescribingNumber
  3324. warehouseOutInfo.CountUnit = advice.CountUnit
  3325. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  3326. if errOne != nil {
  3327. return errOne
  3328. }
  3329. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  3330. drugflow := models.DrugFlow{
  3331. WarehouseOutId: advice.WarehouseOutId,
  3332. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3333. DrugId: advice.DrugId,
  3334. Number: warehouse.Number,
  3335. ProductDate: advice.ProductDate,
  3336. ExpireDate: advice.ExpiryDate,
  3337. Count: deliver_number,
  3338. Price: warehouse.RetailPrice,
  3339. Status: 1,
  3340. Ctime: time.Now().Unix(),
  3341. UserOrgId: advice.OrgId,
  3342. Manufacturer: advice.Manufacturer,
  3343. Dealer: advice.Dealer,
  3344. BatchNumber: warehouse.BatchNumber,
  3345. MaxUnit: drup.MinUnit,
  3346. ConsumableType: 2,
  3347. IsEdit: 1,
  3348. Creator: 0,
  3349. IsSys: 0,
  3350. WarehouseOutDetailId: drugWareInfo.ID,
  3351. AdviceId: advice.ID,
  3352. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  3353. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  3354. SystemTime: advice.SysRecordTime,
  3355. StorehouseId: warehouse.StorehouseId,
  3356. WarehousingDetailId: warehouse.ID,
  3357. WarehousingId: warehouse.ID,
  3358. RetailPrice: retail_price,
  3359. LastPrice: warehouse.Price,
  3360. }
  3361. CreateDrugFlowOne(drugflow)
  3362. // 出库完成后,要减去对应批次的库存数量
  3363. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  3364. var maxNumber int64 = 0
  3365. var minNumber int64 = 0
  3366. if advice.CountUnit == drup.MinUnit {
  3367. maxNumber = prescribingNumber / drup.MinNumber
  3368. minNumber = prescribingNumber % drup.MinNumber
  3369. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  3370. minNumber = maxNumber
  3371. }
  3372. } else {
  3373. maxNumber = prescribingNumber
  3374. }
  3375. if warehouse.StockMaxNumber < maxNumber {
  3376. return errors.New("库存数量不足")
  3377. }
  3378. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  3379. if warehouse.StockMaxNumber < 0 {
  3380. warehouse.StockMaxNumber = 0
  3381. }
  3382. warehouse.Mtime = time.Now().Unix()
  3383. if warehouse.StockMinNumber < minNumber {
  3384. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  3385. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  3386. } else {
  3387. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  3388. }
  3389. if warehouse.StockMaxNumber < 0 {
  3390. return errors.New("库存数量不足")
  3391. }
  3392. if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
  3393. warehouse.StockMinNumber = 0
  3394. }
  3395. if warehouse.StockMaxNumber > 0 {
  3396. var sum_count int64
  3397. sum_count = maxNumber * drup.MinNumber
  3398. ModifyDrugReduceInformationSix(warehouse.DrugId, sum_count, warehouse.OrgId, warehouse.RetailPrice)
  3399. //出库数量增加
  3400. AddDrugSumOutCount(houseConfig.DrugStorehouseOut, sum_count, warehouse.OrgId, warehouse.DrugId)
  3401. }
  3402. if warehouse.StockMinNumber > 0 {
  3403. ModifyDrugReduceInformationSix(warehouse.DrugId, minNumber, warehouse.OrgId, warehouse.RetailPrice)
  3404. //出库数量增加
  3405. AddDrugSumOutCount(houseConfig.DrugStorehouseOut, minNumber, warehouse.OrgId, warehouse.DrugId)
  3406. }
  3407. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  3408. if errThree != nil {
  3409. return errThree
  3410. }
  3411. return nil
  3412. } else {
  3413. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  3414. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  3415. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3416. WarehouseOutId: advice.WarehouseOutId,
  3417. Status: 1,
  3418. Ctime: time.Now().Unix(),
  3419. Remark: advice.Remark,
  3420. OrgId: orgID,
  3421. Type: 1,
  3422. Manufacturer: advice.Manufacturer,
  3423. Dealer: warehouse.Dealer,
  3424. IsSys: 0,
  3425. SysRecordTime: advice.SysRecordTime,
  3426. DrugId: advice.DrugId,
  3427. ExpiryDate: warehouse.ExpiryDate,
  3428. ProductDate: warehouse.ProductDate,
  3429. Number: warehouse.Number,
  3430. BatchNumber: warehouse.BatchNumber,
  3431. Count: stock_number,
  3432. RetailPrice: advice.RetailPrice,
  3433. Price: advice.Price,
  3434. WarehouseInfoId: warehouse.ID,
  3435. CountUnit: drup.MinUnit,
  3436. AdviceId: advice.ID,
  3437. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  3438. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  3439. StorehouseId: warehouse.StorehouseId,
  3440. IsCheck: 1,
  3441. }
  3442. warehouseOutInfo.Count = stock_number
  3443. //warehouseOutInfo.CountUnit = advice.CountUnit
  3444. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  3445. if errOne != nil {
  3446. return errOne
  3447. }
  3448. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  3449. drugflow := models.DrugFlow{
  3450. WarehouseOutId: advice.WarehouseOutId,
  3451. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3452. DrugId: advice.DrugId,
  3453. Number: warehouse.Number,
  3454. ProductDate: advice.ProductDate,
  3455. ExpireDate: advice.ExpiryDate,
  3456. Count: stock_number,
  3457. Price: warehouse.RetailPrice,
  3458. Status: 1,
  3459. Ctime: time.Now().Unix(),
  3460. UserOrgId: advice.OrgId,
  3461. Manufacturer: advice.Manufacturer,
  3462. Dealer: advice.Dealer,
  3463. BatchNumber: warehouse.BatchNumber,
  3464. MaxUnit: drup.MinUnit,
  3465. ConsumableType: 2,
  3466. IsEdit: 1,
  3467. Creator: 0,
  3468. IsSys: 0,
  3469. WarehouseOutDetailId: drugWareInfo.ID,
  3470. AdviceId: advice.ID,
  3471. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  3472. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  3473. SystemTime: advice.SysRecordTime,
  3474. StorehouseId: warehouse.StorehouseId,
  3475. WarehousingDetailId: warehouse.ID,
  3476. WarehousingId: warehouse.ID,
  3477. RetailPrice: retail_price,
  3478. LastPrice: warehouse.Price,
  3479. }
  3480. CreateDrugFlowOne(drugflow)
  3481. if warehouse.StockMaxNumber > 0 {
  3482. var sum_count int64
  3483. sum_count = warehouse.StockMaxNumber * drup.MinNumber
  3484. ModifyDrugReduceInformationSix(warehouse.DrugId, sum_count, warehouse.OrgId, warehouse.RetailPrice)
  3485. AddDrugSumOutCount(houseConfig.DrugStorehouseOut, sum_count, warehouse.OrgId, warehouse.DrugId)
  3486. }
  3487. if warehouse.StockMinNumber > 0 {
  3488. ModifyDrugReduceInformationSix(warehouse.DrugId, warehouse.StockMinNumber, warehouse.OrgId, warehouse.RetailPrice)
  3489. AddDrugSumOutCount(houseConfig.DrugStorehouseOut, warehouse.StockMinNumber, warehouse.OrgId, warehouse.DrugId)
  3490. }
  3491. // 出库完成后,要将该批次库存清零
  3492. warehouse.StockMaxNumber = 0
  3493. warehouse.StockMinNumber = 0
  3494. warehouse.Mtime = time.Now().Unix()
  3495. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  3496. if errThree != nil {
  3497. return errThree
  3498. }
  3499. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3500. prescribingNumber_two_temp := deliver_number - stock_number
  3501. advice.CountUnit = drup.MinUnit
  3502. AutoDrugDeliverInfoNight(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, warehouse_out_id, cancel_out_id)
  3503. }
  3504. return
  3505. }
  3506. // 调拨出库逻辑
  3507. func ConsumablesDeliveryTwelve(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64, warehouseInfo *models.WarehousingInfo) (err error) {
  3508. var deliver_number int64 = 0
  3509. var stock_number int64 = 0
  3510. var maxNumber int64 = 0
  3511. deliver_number = goods.Count
  3512. // 根据先进先出原则,查询最先入库的批次,进行出库
  3513. // 如果没有对应的库存,则报错
  3514. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  3515. if err != nil {
  3516. return err
  3517. }
  3518. stock_number = warehouse.StockCount
  3519. maxNumber = goods.Count
  3520. //if warehouse.StockCount < maxNumber {
  3521. // return errors.New("库存数量不足")
  3522. //}
  3523. if warehouse.StockCount-maxNumber >= 0 {
  3524. warehouse.StockCount = warehouse.StockCount - maxNumber
  3525. } else {
  3526. warehouse.StockCount = warehouse.StockCount
  3527. }
  3528. warehouse.Mtime = time.Now().Unix()
  3529. //扣减库存
  3530. errThree := UpDateWarehouseInfoByStock(&warehouse)
  3531. //查询剩余库存
  3532. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  3533. var sum_count int64
  3534. for _, item := range goodList {
  3535. sum_count += item.StockCount
  3536. }
  3537. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3538. if stock_number >= deliver_number {
  3539. warehouseOutInfo := &models.WarehouseOutInfo{
  3540. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  3541. WarehouseOutId: goods.WarehouseOutId,
  3542. WarehouseInfotId: warehouse.ID,
  3543. Status: 1,
  3544. Ctime: time.Now().Unix(),
  3545. Remark: goods.Remark,
  3546. OrgId: orgID,
  3547. Type: 1,
  3548. Manufacturer: warehouse.Manufacturer,
  3549. Dealer: warehouse.Dealer,
  3550. IsSys: 0,
  3551. SysRecordTime: record_time,
  3552. GoodTypeId: warehouse.GoodTypeId,
  3553. GoodId: warehouse.GoodId,
  3554. ExpiryDate: warehouse.ExpiryDate,
  3555. ProductDate: warehouse.ProductDate,
  3556. Number: warehouse.Number,
  3557. Price: warehouse.PackingPrice,
  3558. LicenseNumber: warehouse.LicenseNumber,
  3559. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3560. StorehouseId: goods.StorehouseId,
  3561. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  3562. IsCheck: 1,
  3563. OverCount: sum_count,
  3564. }
  3565. warehouseOutInfo.Count = goods.Count
  3566. //添加出库单详情
  3567. errOne := AddSigleWarehouseOutInfoOne(warehouseOutInfo)
  3568. if errOne != nil {
  3569. return errOne
  3570. }
  3571. stockFlow := models.VmStockFlow{
  3572. WarehousingId: warehouse.ID,
  3573. GoodId: goods.GoodId,
  3574. Number: warehouse.Number,
  3575. LicenseNumber: goods.LicenseNumber,
  3576. Count: goods.Count,
  3577. UserOrgId: goods.OrgId,
  3578. PatientId: goods.PatientId,
  3579. SystemTime: record_time,
  3580. ConsumableType: 12,
  3581. IsSys: goods.IsSys,
  3582. WarehousingOrder: "",
  3583. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  3584. WarehouseOutId: goods.WarehouseOutId,
  3585. IsEdit: 1,
  3586. CancelStockId: 0,
  3587. CancelOrderNumber: "",
  3588. Manufacturer: warehouse.Manufacturer,
  3589. Dealer: warehouse.Dealer,
  3590. Creator: creator,
  3591. UpdateCreator: 0,
  3592. Status: 1,
  3593. Ctime: time.Now().Unix(),
  3594. Mtime: 0,
  3595. Price: warehouse.PackingPrice,
  3596. WarehousingDetailId: 0,
  3597. WarehouseOutDetailId: warehouseOutInfo.ID,
  3598. CancelOutDetailId: 0,
  3599. ProductDate: warehouse.ProductDate,
  3600. ExpireDate: warehouse.ExpiryDate,
  3601. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3602. StorehouseId: warehouse.StorehouseId,
  3603. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  3604. OverCount: sum_count,
  3605. }
  3606. //创建出库流水
  3607. CreateStockFlowOne(stockFlow)
  3608. //入库
  3609. warehouseInfo := &models.WarehousingInfo{
  3610. WarehousingOrder: warehouseInfo.WarehousingOrder,
  3611. WarehousingId: warehouseInfo.WarehousingId,
  3612. GoodId: warehouse.GoodId,
  3613. Number: warehouse.Number,
  3614. GoodTypeId: warehouse.GoodTypeId,
  3615. ProductDate: warehouse.ProductDate,
  3616. ExpiryDate: warehouse.ExpiryDate,
  3617. WarehousingCount: goods.Count,
  3618. Price: warehouse.Price,
  3619. TotalPrice: 0,
  3620. Status: 1,
  3621. Ctime: warehouse.Ctime,
  3622. Remark: goods.Remark,
  3623. OrgId: warehouse.OrgId,
  3624. Type: 1,
  3625. Manufacturer: warehouse.Manufacturer,
  3626. StockCount: goods.Count,
  3627. Dealer: warehouse.Dealer,
  3628. LicenseNumber: "",
  3629. PackingPrice: warehouse.PackingPrice,
  3630. StorehouseId: warehouseInfo.StorehouseId,
  3631. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  3632. IsCheck: 1,
  3633. }
  3634. AddWarehosingInfo(warehouseInfo)
  3635. //查询剩余库存
  3636. goodListOne, _ := GetAllGoodSumCount(goods.GoodId, orgID, warehouseInfo.StorehouseId)
  3637. var sum_count_one int64
  3638. for _, item := range goodListOne {
  3639. sum_count_one += item.StockCount
  3640. }
  3641. stockInfoFlow := models.VmStockFlow{
  3642. WarehousingId: warehouseInfo.WarehousingId,
  3643. GoodId: goods.GoodId,
  3644. Number: warehouse.Number,
  3645. LicenseNumber: goods.LicenseNumber,
  3646. Count: goods.Count,
  3647. UserOrgId: goods.OrgId,
  3648. PatientId: goods.PatientId,
  3649. SystemTime: record_time,
  3650. ConsumableType: 13,
  3651. IsSys: goods.IsSys,
  3652. WarehousingOrder: warehouseInfo.WarehousingOrder,
  3653. WarehouseOutId: 0,
  3654. WarehouseOutOrderNumber: "",
  3655. IsEdit: 1,
  3656. CancelStockId: 0,
  3657. CancelOrderNumber: "",
  3658. Manufacturer: warehouse.Manufacturer,
  3659. Dealer: warehouse.Dealer,
  3660. Creator: creator,
  3661. UpdateCreator: 0,
  3662. Status: 1,
  3663. Ctime: time.Now().Unix(),
  3664. Mtime: 0,
  3665. Price: warehouse.Price,
  3666. WarehousingDetailId: 0,
  3667. WarehouseOutDetailId: warehouseOutInfo.ID,
  3668. CancelOutDetailId: 0,
  3669. ProductDate: warehouse.ProductDate,
  3670. ExpireDate: warehouse.ExpiryDate,
  3671. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3672. StorehouseId: warehouseInfo.StorehouseId,
  3673. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  3674. OverCount: sum_count_one,
  3675. }
  3676. //创建入库流水
  3677. CreateStockFlowOne(stockInfoFlow)
  3678. if errThree != nil {
  3679. return errThree
  3680. }
  3681. } else {
  3682. // 出库完成后,要将该批次库存清零
  3683. warehouse.StockCount = 0
  3684. warehouse.Mtime = time.Now().Unix()
  3685. errThree := UpDateWarehouseInfoByStock(&warehouse)
  3686. if errThree != nil {
  3687. return errThree
  3688. }
  3689. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3690. goods.Count = deliver_number - stock_number
  3691. //查询剩余库存
  3692. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  3693. var sum_count int64
  3694. for _, item := range goodList {
  3695. sum_count += item.StockCount
  3696. }
  3697. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  3698. warehouseOutInfo := &models.WarehouseOutInfo{
  3699. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  3700. WarehouseOutId: goods.WarehouseOutId,
  3701. WarehouseInfotId: warehouse.ID,
  3702. Status: 1,
  3703. Ctime: time.Now().Unix(),
  3704. Remark: goods.Remark,
  3705. OrgId: orgID,
  3706. Type: 1,
  3707. Manufacturer: warehouse.Manufacturer,
  3708. Dealer: warehouse.Dealer,
  3709. IsSys: 0,
  3710. SysRecordTime: record_time,
  3711. GoodTypeId: warehouse.GoodTypeId,
  3712. GoodId: warehouse.GoodId,
  3713. ExpiryDate: warehouse.ExpiryDate,
  3714. ProductDate: warehouse.ProductDate,
  3715. Number: warehouse.Number,
  3716. Price: warehouse.PackingPrice,
  3717. LicenseNumber: warehouse.LicenseNumber,
  3718. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3719. StorehouseId: warehouse.StorehouseId,
  3720. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  3721. IsCheck: 1,
  3722. OverCount: sum_count,
  3723. }
  3724. warehouseOutInfo.Count = stock_number
  3725. //查询是否已经生成出库单
  3726. info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  3727. if errcodes == gorm.ErrRecordNotFound {
  3728. //创建出库单
  3729. errOne := AddSigleWarehouseOutInfoOne(warehouseOutInfo)
  3730. if errOne != nil {
  3731. return errOne
  3732. }
  3733. } else {
  3734. warehouseOutInfo.Count = info.Count + stock_number
  3735. UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  3736. }
  3737. stockFlow := models.VmStockFlow{
  3738. WarehousingId: warehouse.ID,
  3739. GoodId: goods.GoodId,
  3740. Number: warehouse.Number,
  3741. LicenseNumber: goods.LicenseNumber,
  3742. Count: stock_number,
  3743. UserOrgId: goods.OrgId,
  3744. PatientId: 0,
  3745. SystemTime: record_time,
  3746. ConsumableType: 12,
  3747. IsSys: 0,
  3748. WarehousingOrder: "",
  3749. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  3750. WarehouseOutId: goods.WarehouseOutId,
  3751. IsEdit: 1,
  3752. CancelStockId: 0,
  3753. CancelOrderNumber: "",
  3754. Manufacturer: warehouse.Manufacturer,
  3755. Dealer: warehouse.Dealer,
  3756. Creator: creator,
  3757. UpdateCreator: 0,
  3758. Status: 1,
  3759. Ctime: time.Now().Unix(),
  3760. Mtime: 0,
  3761. Price: warehouse.PackingPrice,
  3762. WarehousingDetailId: 0,
  3763. WarehouseOutDetailId: warehouseOutInfo.ID,
  3764. CancelOutDetailId: 0,
  3765. ProductDate: warehouse.ProductDate,
  3766. ExpireDate: warehouse.ExpiryDate,
  3767. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3768. StorehouseId: goods.StorehouseId,
  3769. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  3770. OverCount: sum_count,
  3771. }
  3772. //创建出库明细
  3773. CreateStockFlowOne(stockFlow)
  3774. //入库
  3775. warehouseInfo := &models.WarehousingInfo{
  3776. WarehousingOrder: warehouseInfo.WarehousingOrder,
  3777. WarehousingId: warehouseInfo.WarehousingId,
  3778. GoodId: warehouse.GoodId,
  3779. Number: warehouse.Number,
  3780. GoodTypeId: warehouse.GoodTypeId,
  3781. ProductDate: warehouse.ProductDate,
  3782. ExpiryDate: warehouse.ExpiryDate,
  3783. WarehousingCount: stock_number,
  3784. Price: warehouse.Price,
  3785. TotalPrice: 0,
  3786. Status: 1,
  3787. Ctime: warehouse.Ctime,
  3788. Remark: goods.Remark,
  3789. OrgId: warehouse.OrgId,
  3790. Type: 1,
  3791. Manufacturer: warehouse.Manufacturer,
  3792. StockCount: stock_number,
  3793. Dealer: warehouse.Dealer,
  3794. LicenseNumber: "",
  3795. PackingPrice: warehouse.PackingPrice,
  3796. StorehouseId: warehouseInfo.StorehouseId,
  3797. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  3798. IsCheck: 1,
  3799. }
  3800. AddWarehosingInfo(warehouseInfo)
  3801. //查询剩余库存
  3802. goodListOne, _ := GetAllGoodSumCount(goods.GoodId, orgID, warehouseInfo.StorehouseId)
  3803. var sum_count_one int64
  3804. for _, item := range goodListOne {
  3805. sum_count_one += item.StockCount
  3806. }
  3807. stockInfoFlow := models.VmStockFlow{
  3808. WarehousingId: warehouseInfo.WarehousingId,
  3809. GoodId: goods.GoodId,
  3810. Number: warehouse.Number,
  3811. LicenseNumber: goods.LicenseNumber,
  3812. Count: stock_number,
  3813. UserOrgId: goods.OrgId,
  3814. PatientId: goods.PatientId,
  3815. SystemTime: record_time,
  3816. ConsumableType: 13,
  3817. IsSys: goods.IsSys,
  3818. WarehousingOrder: warehouseInfo.WarehousingOrder,
  3819. WarehouseOutId: warehouseOut.ID,
  3820. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3821. IsEdit: 1,
  3822. CancelStockId: 0,
  3823. CancelOrderNumber: "",
  3824. Manufacturer: warehouse.Manufacturer,
  3825. Dealer: warehouse.Dealer,
  3826. Creator: creator,
  3827. UpdateCreator: 0,
  3828. Status: 1,
  3829. Ctime: time.Now().Unix(),
  3830. Mtime: 0,
  3831. Price: warehouse.Price,
  3832. WarehousingDetailId: 0,
  3833. WarehouseOutDetailId: warehouseOutInfo.ID,
  3834. CancelOutDetailId: 0,
  3835. ProductDate: warehouse.ProductDate,
  3836. ExpireDate: warehouse.ExpiryDate,
  3837. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3838. StorehouseId: warehouseInfo.StorehouseId,
  3839. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  3840. OverCount: sum_count_one,
  3841. }
  3842. //创建出库流水
  3843. CreateStockFlowOne(stockInfoFlow)
  3844. ConsumablesDeliveryTwelve(orgID, record_time, goods, warehouseOut, count, creator, warehouseInfo)
  3845. }
  3846. return nil
  3847. }
  3848. func AddWarehosingInfo(info *models.WarehousingInfo) error {
  3849. err = XTWriteDB().Create(&info).Error
  3850. return err
  3851. }
  3852. // 药品出库 递归方式
  3853. func AutoDrugDeliverInfoTwelve(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo, drugWarehouse *models.DrugWarehouseInfo) (err error) {
  3854. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  3855. var deliver_number int64 = 0
  3856. var stock_number int64 = 0
  3857. if advice.CountUnit == drup.MaxUnit {
  3858. deliver_number = prescribingNumber * drup.MinNumber
  3859. } else {
  3860. deliver_number = prescribingNumber
  3861. }
  3862. // 根据先进先出原则,查询最先入库的批次,进行出库
  3863. // 如果没有对应的库存,则报错
  3864. //开启事物
  3865. //storeConfig, _ := GetAllStoreHouseConfig(orgID)
  3866. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  3867. //fmt.Println("lastWarehouse.StockMinNumber", lastWarehouse.StockMinNumber)
  3868. //fmt.Println("drup.MinNumber", drup.MinNumber)
  3869. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  3870. var stockMax int64
  3871. var stockMin int64
  3872. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  3873. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  3874. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  3875. UpdateMinNumber(lastWarehouse.ID, stockMin)
  3876. }
  3877. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  3878. if err != nil {
  3879. return err
  3880. }
  3881. // 将该批次的剩余库存数量转换为拆零数量
  3882. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  3883. //fmt.Println("stock23233233232", stock_number)
  3884. //fmt.Println("deliver_number", deliver_number)
  3885. // 出库完成后,要减去对应批次的库存数量
  3886. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  3887. var maxNumber int64 = 0
  3888. var minNumber int64 = 0
  3889. if advice.CountUnit == drup.MinUnit {
  3890. maxNumber = prescribingNumber / drup.MinNumber
  3891. minNumber = prescribingNumber % drup.MinNumber
  3892. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  3893. minNumber = maxNumber
  3894. }
  3895. } else {
  3896. maxNumber = prescribingNumber
  3897. }
  3898. //fmt.Println("warehouse.StockMaxNumber",warehouse.StockMaxNumber)
  3899. //fmt.Println("maxNumber9899923323232wo",maxNumber)
  3900. //if warehouse.StockMaxNumber < maxNumber {
  3901. //
  3902. // return errors.New("库存数量不足")
  3903. //}
  3904. if warehouse.StockMaxNumber-maxNumber > 0 {
  3905. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  3906. }
  3907. warehouse.Mtime = time.Now().Unix()
  3908. if warehouse.StockMinNumber < minNumber {
  3909. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  3910. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  3911. } else {
  3912. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  3913. }
  3914. if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
  3915. warehouse.StockMinNumber = 0
  3916. }
  3917. if warehouse.StockMaxNumber < 0 {
  3918. return errors.New("库存数量不足")
  3919. }
  3920. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  3921. if errThree != nil {
  3922. return errThree
  3923. }
  3924. //查询剩余库存
  3925. stockInfo, _ := GetDrugAllStockInfo(advice.StorehouseId, orgID, advice.DrugId)
  3926. var sum_count int64
  3927. for _, its := range stockInfo {
  3928. if its.MaxUnit == drup.MaxUnit {
  3929. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  3930. }
  3931. sum_count += its.StockMaxNumber + its.StockMinNumber
  3932. }
  3933. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3934. if stock_number >= deliver_number {
  3935. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  3936. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  3937. WarehouseOutId: advice.WarehouseOutId,
  3938. Status: 1,
  3939. Ctime: time.Now().Unix(),
  3940. Remark: advice.Remark,
  3941. OrgId: orgID,
  3942. Type: 1,
  3943. Manufacturer: warehouse.Manufacturer,
  3944. Dealer: warehouse.Dealer,
  3945. IsSys: 12,
  3946. SysRecordTime: advice.Ctime,
  3947. DrugId: advice.DrugId,
  3948. ExpiryDate: warehouse.ExpiryDate,
  3949. ProductDate: warehouse.ProductDate,
  3950. Number: warehouse.Number,
  3951. BatchNumber: warehouse.BatchNumber,
  3952. Count: deliver_number,
  3953. RetailPrice: warehouse.RetailPrice,
  3954. Price: warehouse.RetailPrice,
  3955. WarehouseInfoId: warehouse.ID,
  3956. CountUnit: drup.MinUnit,
  3957. AdviceId: advice.ID,
  3958. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3959. StorehouseId: advice.StorehouseId,
  3960. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  3961. IsCheck: 1,
  3962. OverCount: sum_count,
  3963. }
  3964. warehouseOutInfo.Count = prescribingNumber
  3965. warehouseOutInfo.CountUnit = advice.CountUnit
  3966. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  3967. if errOne != nil {
  3968. return errOne
  3969. }
  3970. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  3971. drugflow := models.DrugFlow{
  3972. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  3973. WarehouseOutId: advice.WarehouseOutId,
  3974. DrugId: advice.DrugId,
  3975. Number: warehouse.Number,
  3976. ProductDate: warehouse.ProductDate,
  3977. ExpireDate: warehouse.ExpiryDate,
  3978. Count: deliver_number,
  3979. Price: warehouse.RetailPrice,
  3980. Status: 1,
  3981. Ctime: time.Now().Unix(),
  3982. UserOrgId: advice.OrgId,
  3983. Manufacturer: warehouse.Manufacturer,
  3984. Dealer: warehouse.Dealer,
  3985. BatchNumber: warehouse.BatchNumber,
  3986. MaxUnit: drup.MinUnit,
  3987. ConsumableType: 12,
  3988. IsEdit: 1,
  3989. Creator: 0,
  3990. IsSys: 12,
  3991. WarehouseOutDetailId: drugWareInfo.ID,
  3992. AdviceId: advice.ID,
  3993. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3994. StorehouseId: advice.StorehouseId,
  3995. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  3996. WarehousingDetailId: warehouse.ID,
  3997. OverCount: sum_count,
  3998. }
  3999. CreateDrugFlowOne(drugflow)
  4000. //入库
  4001. drugInfo := models.DrugWarehouseInfo{
  4002. WarehousingOrder: drugWarehouse.WarehousingOrder,
  4003. WarehousingId: drugWarehouse.WarehousingId,
  4004. DrugId: warehouse.DrugId,
  4005. Number: warehouse.Number,
  4006. ProductDate: warehouse.ProductDate,
  4007. ExpiryDate: warehouse.ExpiryDate,
  4008. WarehousingCount: deliver_number,
  4009. Price: warehouse.Price,
  4010. TotalPrice: warehouse.TotalPrice,
  4011. Status: 1,
  4012. Ctime: warehouse.Ctime,
  4013. Remark: "",
  4014. OrgId: warehouse.OrgId,
  4015. Type: 13,
  4016. Manufacturer: warehouse.Manufacturer,
  4017. Dealer: warehouse.Dealer,
  4018. StockMaxNumber: 0,
  4019. StockMinNumber: deliver_number,
  4020. BatchNumber: warehouse.BatchNumber,
  4021. MaxUnit: drup.MinUnit,
  4022. MinUnit: drup.MinUnit,
  4023. RetailPrice: warehouse.RetailPrice,
  4024. StorehouseId: drugWarehouse.StorehouseId,
  4025. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  4026. IsCheck: 1,
  4027. }
  4028. CreateDrugWarehouseInfo(drugInfo)
  4029. //查询剩余库存
  4030. stockInfoOne, _ := GetDrugAllStockInfo(drugWarehouse.StorehouseId, orgID, advice.DrugId)
  4031. var sum_count_one int64
  4032. for _, its := range stockInfoOne {
  4033. if its.MaxUnit == drup.MaxUnit {
  4034. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  4035. }
  4036. sum_count_one += its.StockMaxNumber + its.StockMinNumber
  4037. }
  4038. flow := models.DrugFlow{
  4039. WarehousingId: drugWarehouse.WarehousingId,
  4040. DrugId: warehouse.DrugId,
  4041. Number: warehouse.Number,
  4042. BatchNumber: warehouse.BatchNumber,
  4043. Count: deliver_number,
  4044. UserOrgId: warehouse.OrgId,
  4045. PatientId: 0,
  4046. SystemTime: 0,
  4047. ConsumableType: 13,
  4048. IsSys: 13,
  4049. WarehousingOrder: drugWarehouse.WarehousingOrder,
  4050. WarehouseOutId: 0,
  4051. WarehouseOutOrderNumber: "",
  4052. IsEdit: 0,
  4053. CancelStockId: 0,
  4054. CancelOrderNumber: "",
  4055. Manufacturer: warehouse.Manufacturer,
  4056. Dealer: warehouse.Dealer,
  4057. Creator: 0,
  4058. UpdateCreator: 0,
  4059. Status: 1,
  4060. Ctime: time.Now().Unix(),
  4061. Mtime: 0,
  4062. Price: warehouse.RetailPrice,
  4063. WarehousingDetailId: warehouse.ID,
  4064. WarehouseOutDetailId: 0,
  4065. CancelOutDetailId: 0,
  4066. ExpireDate: warehouse.ExpiryDate,
  4067. ProductDate: warehouse.ProductDate,
  4068. MaxUnit: drup.MinUnit,
  4069. MinUnit: drup.MinUnit,
  4070. StockMaxNumber: 0,
  4071. StockMinNumber: deliver_number,
  4072. StorehouseId: drugWarehouse.StorehouseId,
  4073. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  4074. OverCount: sum_count_one,
  4075. }
  4076. CreateDrugFlowOne(flow)
  4077. return nil
  4078. } else {
  4079. warehouse.StockMaxNumber = 0
  4080. warehouse.StockMinNumber = 0
  4081. warehouse.Mtime = time.Now().Unix()
  4082. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4083. if errThree != nil {
  4084. return errThree
  4085. }
  4086. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  4087. prescribingNumber_two_temp := deliver_number - stock_number
  4088. advice.CountUnit = drup.MinUnit
  4089. //查询剩余库存
  4090. stockInfo, _ := GetDrugAllStockInfo(advice.StorehouseId, orgID, advice.DrugId)
  4091. var sum_count int64
  4092. for _, its := range stockInfo {
  4093. if its.MaxUnit == drup.MaxUnit {
  4094. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  4095. }
  4096. sum_count += its.StockMaxNumber + its.StockMinNumber
  4097. }
  4098. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  4099. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4100. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  4101. WarehouseOutId: advice.WarehouseOutId,
  4102. Status: 1,
  4103. Ctime: time.Now().Unix(),
  4104. Remark: advice.Remark,
  4105. OrgId: orgID,
  4106. Type: 1,
  4107. Manufacturer: advice.Manufacturer,
  4108. Dealer: warehouse.Dealer,
  4109. IsSys: 12,
  4110. SysRecordTime: advice.Ctime,
  4111. DrugId: advice.DrugId,
  4112. ExpiryDate: warehouse.ExpiryDate,
  4113. ProductDate: warehouse.ProductDate,
  4114. Number: warehouse.Number,
  4115. BatchNumber: warehouse.BatchNumber,
  4116. Count: stock_number,
  4117. RetailPrice: advice.RetailPrice,
  4118. Price: warehouse.RetailPrice,
  4119. WarehouseInfoId: warehouse.ID,
  4120. CountUnit: drup.MinUnit,
  4121. AdviceId: advice.ID,
  4122. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4123. StorehouseId: advice.StorehouseId,
  4124. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  4125. IsCheck: 1,
  4126. OverCount: sum_count,
  4127. }
  4128. warehouseOutInfo.Count = stock_number
  4129. //warehouseOutInfo.CountUnit = advice.CountUnit
  4130. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  4131. if errOne != nil {
  4132. return errOne
  4133. }
  4134. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4135. drugflow := models.DrugFlow{
  4136. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  4137. WarehouseOutId: advice.WarehouseOutId,
  4138. DrugId: advice.DrugId,
  4139. Number: warehouse.Number,
  4140. ProductDate: warehouse.ProductDate,
  4141. ExpireDate: warehouse.ExpiryDate,
  4142. Count: stock_number,
  4143. Price: warehouse.RetailPrice,
  4144. Status: 1,
  4145. Ctime: time.Now().Unix(),
  4146. UserOrgId: advice.OrgId,
  4147. Manufacturer: warehouse.Manufacturer,
  4148. Dealer: warehouse.Dealer,
  4149. BatchNumber: warehouse.BatchNumber,
  4150. MaxUnit: drup.MinUnit,
  4151. ConsumableType: 12,
  4152. IsEdit: 1,
  4153. Creator: 0,
  4154. IsSys: 12,
  4155. WarehouseOutDetailId: drugWareInfo.ID,
  4156. AdviceId: advice.ID,
  4157. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4158. StorehouseId: advice.StorehouseId,
  4159. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  4160. WarehousingDetailId: warehouse.ID,
  4161. OverCount: sum_count,
  4162. }
  4163. CreateDrugFlowOne(drugflow)
  4164. // 出库完成后,要将该批次库存清零
  4165. //入库
  4166. drugInfo := models.DrugWarehouseInfo{
  4167. WarehousingOrder: drugWarehouse.WarehousingOrder,
  4168. WarehousingId: drugWarehouse.WarehousingId,
  4169. DrugId: warehouse.DrugId,
  4170. Number: warehouse.Number,
  4171. ProductDate: warehouse.ProductDate,
  4172. ExpiryDate: warehouse.ExpiryDate,
  4173. WarehousingCount: stock_number,
  4174. Price: warehouse.Price,
  4175. TotalPrice: warehouse.TotalPrice,
  4176. Status: 1,
  4177. Ctime: warehouse.Ctime,
  4178. Remark: "",
  4179. OrgId: warehouse.OrgId,
  4180. Type: 13,
  4181. Manufacturer: warehouse.Manufacturer,
  4182. Dealer: warehouse.Dealer,
  4183. StockMaxNumber: 0,
  4184. StockMinNumber: stock_number,
  4185. RetailTotalPrice: 0,
  4186. BatchNumber: warehouse.BatchNumber,
  4187. MaxUnit: drup.MinUnit,
  4188. MinUnit: drup.MinUnit,
  4189. RetailPrice: warehouse.RetailPrice,
  4190. StorehouseId: drugWarehouse.StorehouseId,
  4191. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  4192. }
  4193. CreateDrugWarehouseInfo(drugInfo)
  4194. //查询剩余库存
  4195. stockInfoTwo, _ := GetDrugAllStockInfo(drugWarehouse.StorehouseId, orgID, advice.DrugId)
  4196. var sum_count_one int64
  4197. for _, its := range stockInfoTwo {
  4198. if its.MaxUnit == drup.MaxUnit {
  4199. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  4200. }
  4201. sum_count_one += its.StockMaxNumber + its.StockMinNumber
  4202. }
  4203. flow := models.DrugFlow{
  4204. WarehousingId: drugWarehouse.WarehousingId,
  4205. DrugId: warehouse.DrugId,
  4206. Number: warehouse.Number,
  4207. BatchNumber: warehouse.BatchNumber,
  4208. Count: stock_number,
  4209. UserOrgId: warehouse.OrgId,
  4210. PatientId: 0,
  4211. SystemTime: 0,
  4212. ConsumableType: 13,
  4213. IsSys: 13,
  4214. WarehousingOrder: drugWarehouse.WarehousingOrder,
  4215. WarehouseOutId: 0,
  4216. WarehouseOutOrderNumber: "",
  4217. IsEdit: 0,
  4218. CancelStockId: 0,
  4219. CancelOrderNumber: "",
  4220. Manufacturer: warehouse.Manufacturer,
  4221. Dealer: warehouse.Dealer,
  4222. Creator: 0,
  4223. UpdateCreator: 0,
  4224. Status: 1,
  4225. Ctime: time.Now().Unix(),
  4226. Mtime: 0,
  4227. Price: warehouse.RetailPrice,
  4228. WarehousingDetailId: warehouse.ID,
  4229. WarehouseOutDetailId: 0,
  4230. CancelOutDetailId: 0,
  4231. ExpireDate: warehouse.ExpiryDate,
  4232. ProductDate: warehouse.ProductDate,
  4233. MaxUnit: drup.MinUnit,
  4234. MinUnit: drup.MinUnit,
  4235. StockMaxNumber: 0,
  4236. StockMinNumber: stock_number,
  4237. StorehouseId: drugWarehouse.StorehouseId,
  4238. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  4239. OverCount: sum_count,
  4240. }
  4241. CreateDrugFlowOne(flow)
  4242. AutoDrugDeliverInfoTwelve(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, drugWarehouse)
  4243. }
  4244. return
  4245. }
  4246. // 药品手动出库 递归方式
  4247. func AutoDrugDeliverInfoTwenty(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  4248. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  4249. var deliver_number int64 = 0
  4250. var stock_number int64 = 0
  4251. if advice.CountUnit == drup.MaxUnit {
  4252. deliver_number = prescribingNumber * drup.MinNumber
  4253. } else {
  4254. deliver_number = prescribingNumber
  4255. }
  4256. // 根据先进先出原则,查询最先入库的批次,进行出库
  4257. // 如果没有对应的库存,则报错
  4258. //开启事物
  4259. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  4260. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  4261. var stockMax int64
  4262. var stockMin int64
  4263. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  4264. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  4265. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  4266. UpdateMinNumber(lastWarehouse.ID, stockMin)
  4267. }
  4268. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  4269. if err != nil {
  4270. return err
  4271. }
  4272. // 将该批次的剩余库存数量转换为拆零数量
  4273. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  4274. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  4275. if stock_number >= deliver_number {
  4276. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4277. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4278. WarehouseOutId: warehouseout.ID,
  4279. Status: 1,
  4280. Ctime: time.Now().Unix(),
  4281. Remark: advice.Remark,
  4282. OrgId: orgID,
  4283. Type: 1,
  4284. Manufacturer: advice.Manufacturer,
  4285. Dealer: warehouse.Dealer,
  4286. IsSys: 0,
  4287. SysRecordTime: advice.Ctime,
  4288. DrugId: advice.DrugId,
  4289. ExpiryDate: advice.ExpiryDate,
  4290. ProductDate: advice.ProductDate,
  4291. Number: advice.Number,
  4292. BatchNumber: warehouse.BatchNumber,
  4293. Count: deliver_number,
  4294. RetailPrice: advice.RetailPrice,
  4295. Price: warehouse.RetailPrice,
  4296. WarehouseInfoId: warehouse.ID,
  4297. CountUnit: drup.MinUnit,
  4298. AdviceId: advice.ID,
  4299. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4300. StorehouseId: advice.StorehouseId,
  4301. AdminUserId: advice.AdminUserId,
  4302. IsCheck: 1,
  4303. }
  4304. if warehouse.RetailPrice == 0 {
  4305. warehouseOutInfo.Price = advice.Price
  4306. }
  4307. warehouseOutInfo.Count = prescribingNumber
  4308. warehouseOutInfo.CountUnit = advice.CountUnit
  4309. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  4310. if errOne != nil {
  4311. return errOne
  4312. }
  4313. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4314. drugflow := models.DrugFlow{
  4315. WarehouseOutId: warehouseout.ID,
  4316. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4317. DrugId: advice.DrugId,
  4318. Number: warehouse.Number,
  4319. ProductDate: advice.ProductDate,
  4320. ExpireDate: advice.ExpiryDate,
  4321. Count: deliver_number,
  4322. Price: warehouse.RetailPrice,
  4323. Status: 1,
  4324. Ctime: time.Now().Unix(),
  4325. UserOrgId: advice.OrgId,
  4326. Manufacturer: advice.Manufacturer,
  4327. Dealer: advice.Dealer,
  4328. BatchNumber: warehouse.BatchNumber,
  4329. MaxUnit: drup.MinUnit,
  4330. ConsumableType: 2,
  4331. IsEdit: 1,
  4332. Creator: 0,
  4333. IsSys: 0,
  4334. WarehouseOutDetailId: drugWareInfo.ID,
  4335. AdviceId: advice.ID,
  4336. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4337. StorehouseId: advice.StorehouseId,
  4338. AdminUserId: advice.AdminUserId,
  4339. }
  4340. if warehouse.RetailPrice == 0 {
  4341. drugflow.Price = advice.Price
  4342. }
  4343. CreateDrugFlowOne(drugflow)
  4344. // 出库完成后,要减去对应批次的库存数量
  4345. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  4346. var maxNumber int64 = 0
  4347. var minNumber int64 = 0
  4348. //var stock_max_number int64 = 0
  4349. //stock_max_number = warehouse.StockMaxNumber
  4350. maxNumber = deliver_number / drup.MinNumber
  4351. minNumber = deliver_number % drup.MinNumber
  4352. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  4353. minNumber = maxNumber
  4354. }
  4355. if drup.MaxUnit != drup.MinUnit {
  4356. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  4357. return errors.New("库存数量不足")
  4358. }
  4359. }
  4360. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  4361. if warehouse.StockMaxNumber < 0 {
  4362. warehouse.StockMaxNumber = 0
  4363. }
  4364. if warehouse.StockMinNumber < 0 {
  4365. warehouse.StockMinNumber = 0
  4366. }
  4367. warehouse.Mtime = time.Now().Unix()
  4368. if warehouse.StockMinNumber < minNumber {
  4369. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  4370. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  4371. } else {
  4372. if minNumber > 0 {
  4373. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  4374. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4375. } else {
  4376. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  4377. }
  4378. }
  4379. if minNumber == 0 && maxNumber != 1 {
  4380. if warehouse.StockMinNumber > 0 {
  4381. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4382. }
  4383. }
  4384. }
  4385. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  4386. if (warehouse.StockMinNumber - deliver_number) > 0 {
  4387. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4388. //if warehouse.StockMinNumber == 0 {
  4389. // warehouse.StockMaxNumber = stock_max_number
  4390. //}
  4391. }
  4392. }
  4393. if drup.MaxUnit != drup.MinUnit {
  4394. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  4395. return errors.New("库存数量不足")
  4396. }
  4397. }
  4398. if warehouse.StockMinNumber <= 0 {
  4399. warehouse.StockMinNumber = 0
  4400. }
  4401. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4402. if errThree != nil {
  4403. return errThree
  4404. }
  4405. //查询默认仓库
  4406. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  4407. //查询默认仓库剩余多少库存
  4408. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  4409. var sum_count int64
  4410. var sum_in_count int64
  4411. for _, it := range list {
  4412. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  4413. if it.MaxUnit == baseDrug.MaxUnit {
  4414. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  4415. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  4416. sum_count += it.StockMaxNumber + it.StockMinNumber
  4417. sum_in_count += it.WarehousingCount
  4418. }
  4419. }
  4420. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  4421. return nil
  4422. } else {
  4423. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  4424. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4425. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4426. WarehouseOutId: warehouseout.ID,
  4427. Status: 1,
  4428. Ctime: time.Now().Unix(),
  4429. Remark: advice.Remark,
  4430. OrgId: orgID,
  4431. Type: 1,
  4432. Manufacturer: advice.Manufacturer,
  4433. Dealer: warehouse.Dealer,
  4434. IsSys: 0,
  4435. SysRecordTime: advice.Ctime,
  4436. DrugId: advice.DrugId,
  4437. ExpiryDate: warehouse.ExpiryDate,
  4438. ProductDate: warehouse.ProductDate,
  4439. Number: warehouse.Number,
  4440. BatchNumber: warehouse.BatchNumber,
  4441. Count: stock_number,
  4442. RetailPrice: advice.RetailPrice,
  4443. Price: warehouse.RetailPrice,
  4444. WarehouseInfoId: warehouse.ID,
  4445. CountUnit: drup.MinUnit,
  4446. AdviceId: advice.ID,
  4447. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4448. StorehouseId: advice.StorehouseId,
  4449. AdminUserId: advice.AdminUserId,
  4450. IsCheck: 1,
  4451. }
  4452. if warehouse.RetailPrice == 0 {
  4453. warehouseOutInfo.Price = advice.Price
  4454. }
  4455. warehouseOutInfo.Count = stock_number
  4456. //warehouseOutInfo.CountUnit = advice.CountUnit
  4457. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  4458. if errOne != nil {
  4459. return errOne
  4460. }
  4461. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4462. drugflow := models.DrugFlow{
  4463. WarehouseOutId: warehouseout.ID,
  4464. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4465. DrugId: advice.DrugId,
  4466. Number: warehouse.Number,
  4467. ProductDate: advice.ProductDate,
  4468. ExpireDate: advice.ExpiryDate,
  4469. Count: stock_number,
  4470. Price: warehouse.RetailPrice,
  4471. Status: 1,
  4472. Ctime: time.Now().Unix(),
  4473. UserOrgId: advice.OrgId,
  4474. Manufacturer: advice.Manufacturer,
  4475. Dealer: advice.Dealer,
  4476. BatchNumber: warehouse.BatchNumber,
  4477. MaxUnit: drup.MinUnit,
  4478. ConsumableType: 2,
  4479. IsEdit: 1,
  4480. Creator: 0,
  4481. IsSys: 0,
  4482. WarehouseOutDetailId: drugWareInfo.ID,
  4483. AdviceId: advice.ID,
  4484. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4485. StorehouseId: advice.StorehouseId,
  4486. AdminUserId: advice.AdminUserId,
  4487. }
  4488. if warehouse.RetailPrice == 0 {
  4489. drugflow.Price = advice.Price
  4490. }
  4491. CreateDrugFlowOne(drugflow)
  4492. // 出库完成后,要将该批次库存清零
  4493. //扣减库存
  4494. warehouse.StockMaxNumber = 0
  4495. warehouse.StockMinNumber = 0
  4496. warehouse.Mtime = time.Now().Unix()
  4497. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4498. //查询默认仓库
  4499. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  4500. //查询默认仓库剩余多少库存
  4501. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  4502. var sum_count int64
  4503. var sum_in_count int64
  4504. for _, it := range list {
  4505. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  4506. if it.MaxUnit == baseDrug.MaxUnit {
  4507. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  4508. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  4509. sum_count += it.StockMaxNumber + it.StockMinNumber
  4510. sum_in_count += it.WarehousingCount
  4511. }
  4512. }
  4513. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  4514. if errThree != nil {
  4515. return errThree
  4516. }
  4517. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  4518. prescribingNumber_two_temp := deliver_number - stock_number
  4519. advice.CountUnit = drup.MinUnit
  4520. AutoDrugDeliverInfoTwenty(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  4521. }
  4522. return
  4523. }
  4524. // 药品手动出库 递归方式
  4525. func AutoDrugDeliverInfoTwentyOne(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  4526. //开事务
  4527. //tx := XTWriteDB().Begin()
  4528. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  4529. var deliver_number int64 = 0
  4530. var stock_number int64 = 0
  4531. if advice.CountUnit == drup.MaxUnit {
  4532. deliver_number = prescribingNumber * drup.MinNumber
  4533. } else {
  4534. deliver_number = prescribingNumber
  4535. }
  4536. // 根据先进先出原则,查询最先入库的批次,进行出库
  4537. // 如果没有对应的库存,则报错
  4538. //开启事物
  4539. storeConfig, _ := GetAllStoreHouseConfig(orgID)
  4540. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  4541. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  4542. var stockMax int64
  4543. var stockMin int64
  4544. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  4545. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  4546. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  4547. UpdateMinNumber(lastWarehouse.ID, stockMin)
  4548. }
  4549. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  4550. if err != nil {
  4551. return err
  4552. }
  4553. // 将该批次的剩余库存数量转换为拆零数量
  4554. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  4555. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  4556. if stock_number >= deliver_number {
  4557. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4558. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4559. WarehouseOutId: warehouseout.ID,
  4560. Status: 1,
  4561. Ctime: time.Now().Unix(),
  4562. Remark: advice.Remark,
  4563. OrgId: orgID,
  4564. Type: 1,
  4565. Manufacturer: advice.Manufacturer,
  4566. Dealer: warehouse.Dealer,
  4567. IsSys: 0,
  4568. SysRecordTime: advice.Ctime,
  4569. DrugId: advice.DrugId,
  4570. ExpiryDate: advice.ExpiryDate,
  4571. ProductDate: advice.ProductDate,
  4572. Number: advice.Number,
  4573. BatchNumber: warehouse.BatchNumber,
  4574. Count: deliver_number,
  4575. RetailPrice: advice.RetailPrice,
  4576. Price: advice.Price,
  4577. WarehouseInfoId: warehouse.ID,
  4578. CountUnit: drup.MinUnit,
  4579. AdviceId: advice.ID,
  4580. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4581. StorehouseId: advice.StorehouseId,
  4582. AdminUserId: advice.AdminUserId,
  4583. LastPrice: warehouse.Price,
  4584. StockCount: advice.StockCount,
  4585. IsCheck: 1,
  4586. }
  4587. warehouseOutInfo.Count = prescribingNumber
  4588. warehouseOutInfo.CountUnit = advice.CountUnit
  4589. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4590. drugflow := models.DrugFlow{
  4591. WarehouseOutId: warehouseout.ID,
  4592. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4593. DrugId: advice.DrugId,
  4594. Number: warehouse.Number,
  4595. ProductDate: advice.ProductDate,
  4596. ExpireDate: advice.ExpiryDate,
  4597. Count: deliver_number,
  4598. Price: advice.Price,
  4599. Status: 1,
  4600. Ctime: time.Now().Unix(),
  4601. UserOrgId: advice.OrgId,
  4602. Manufacturer: advice.Manufacturer,
  4603. Dealer: advice.Dealer,
  4604. BatchNumber: warehouse.BatchNumber,
  4605. MaxUnit: drup.MinUnit,
  4606. ConsumableType: 2,
  4607. IsEdit: 1,
  4608. Creator: 0,
  4609. IsSys: 0,
  4610. WarehouseOutDetailId: drugWareInfo.ID,
  4611. AdviceId: advice.ID,
  4612. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4613. StorehouseId: advice.StorehouseId,
  4614. AdminUserId: advice.AdminUserId,
  4615. LastPrice: warehouse.Price,
  4616. StockCount: advice.StockCount,
  4617. }
  4618. if warehouse.RetailPrice == 0 {
  4619. drugflow.Price = advice.Price
  4620. }
  4621. CreateDrugFlowOne(drugflow)
  4622. // 出库完成后,要减去对应批次的库存数量
  4623. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  4624. var maxNumber int64 = 0
  4625. var minNumber int64 = 0
  4626. //var stock_max_number int64 = 0
  4627. //stock_max_number = warehouse.StockMaxNumber
  4628. maxNumber = deliver_number / drup.MinNumber
  4629. minNumber = deliver_number % drup.MinNumber
  4630. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  4631. minNumber = maxNumber
  4632. }
  4633. if drup.MaxUnit != drup.MinUnit {
  4634. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  4635. return errors.New("库存数量不足")
  4636. }
  4637. }
  4638. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  4639. if warehouse.StockMaxNumber < 0 {
  4640. warehouse.StockMaxNumber = 0
  4641. }
  4642. if warehouse.StockMinNumber < 0 {
  4643. warehouse.StockMinNumber = 0
  4644. }
  4645. warehouse.Mtime = time.Now().Unix()
  4646. if warehouse.StockMinNumber < minNumber {
  4647. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  4648. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  4649. } else {
  4650. if minNumber > 0 {
  4651. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  4652. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4653. } else {
  4654. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  4655. }
  4656. }
  4657. if minNumber == 0 && maxNumber != 1 {
  4658. if warehouse.StockMinNumber > 0 {
  4659. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4660. }
  4661. }
  4662. }
  4663. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  4664. if (warehouse.StockMinNumber - deliver_number) > 0 {
  4665. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4666. //if warehouse.StockMinNumber == 0 {
  4667. // warehouse.StockMaxNumber = stock_max_number
  4668. //}
  4669. }
  4670. }
  4671. if drup.MaxUnit != drup.MinUnit {
  4672. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  4673. return errors.New("库存数量不足")
  4674. }
  4675. }
  4676. if warehouse.StockMinNumber <= 0 {
  4677. warehouse.StockMinNumber = 0
  4678. }
  4679. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4680. fmt.Println(errThree)
  4681. //if errThree != nil {
  4682. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  4683. // tx.Rollback()
  4684. //} else {
  4685. // tx.Commit()
  4686. //}
  4687. //查询默认仓库
  4688. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  4689. //查询默认仓库剩余多少库存
  4690. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  4691. var sum_count int64
  4692. var sum_in_count int64
  4693. for _, it := range list {
  4694. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  4695. if it.MaxUnit == baseDrug.MaxUnit {
  4696. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  4697. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  4698. sum_count += it.StockMaxNumber + it.StockMinNumber
  4699. sum_in_count += it.WarehousingCount
  4700. }
  4701. }
  4702. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  4703. return nil
  4704. } else {
  4705. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  4706. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4707. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4708. WarehouseOutId: warehouseout.ID,
  4709. Status: 1,
  4710. Ctime: time.Now().Unix(),
  4711. Remark: advice.Remark,
  4712. OrgId: orgID,
  4713. Type: 1,
  4714. Manufacturer: advice.Manufacturer,
  4715. Dealer: warehouse.Dealer,
  4716. IsSys: 0,
  4717. SysRecordTime: advice.Ctime,
  4718. DrugId: advice.DrugId,
  4719. ExpiryDate: warehouse.ExpiryDate,
  4720. ProductDate: warehouse.ProductDate,
  4721. Number: warehouse.Number,
  4722. BatchNumber: warehouse.BatchNumber,
  4723. Count: stock_number,
  4724. RetailPrice: advice.RetailPrice,
  4725. Price: advice.Price,
  4726. WarehouseInfoId: warehouse.ID,
  4727. CountUnit: drup.MinUnit,
  4728. AdviceId: advice.ID,
  4729. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4730. StorehouseId: advice.StorehouseId,
  4731. AdminUserId: advice.AdminUserId,
  4732. LastPrice: warehouse.Price,
  4733. StockCount: advice.StockCount,
  4734. IsCheck: 1,
  4735. }
  4736. warehouseOutInfo.Count = stock_number
  4737. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4738. drugflow := models.DrugFlow{
  4739. WarehouseOutId: warehouseout.ID,
  4740. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4741. DrugId: advice.DrugId,
  4742. Number: warehouse.Number,
  4743. ProductDate: advice.ProductDate,
  4744. ExpireDate: advice.ExpiryDate,
  4745. Count: stock_number,
  4746. Price: advice.Price,
  4747. Status: 1,
  4748. Ctime: time.Now().Unix(),
  4749. UserOrgId: advice.OrgId,
  4750. Manufacturer: advice.Manufacturer,
  4751. Dealer: advice.Dealer,
  4752. BatchNumber: warehouse.BatchNumber,
  4753. MaxUnit: drup.MinUnit,
  4754. ConsumableType: 2,
  4755. IsEdit: 1,
  4756. Creator: 0,
  4757. IsSys: 0,
  4758. WarehouseOutDetailId: drugWareInfo.ID,
  4759. AdviceId: advice.ID,
  4760. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4761. StorehouseId: advice.StorehouseId,
  4762. AdminUserId: advice.AdminUserId,
  4763. LastPrice: warehouse.Price,
  4764. StockCount: advice.StockCount,
  4765. }
  4766. CreateDrugFlowOne(drugflow)
  4767. // 出库完成后,要将该批次库存清零
  4768. //扣减库存
  4769. warehouse.StockMaxNumber = 0
  4770. warehouse.StockMinNumber = 0
  4771. warehouse.Mtime = time.Now().Unix()
  4772. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4773. fmt.Println(errThree)
  4774. //if errThree != nil {
  4775. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  4776. // tx.Rollback()
  4777. //} else {
  4778. // tx.Commit()
  4779. //}
  4780. //查询默认仓库
  4781. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  4782. //查询默认仓库剩余多少库存
  4783. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  4784. var sum_count int64
  4785. var sum_in_count int64
  4786. for _, it := range list {
  4787. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  4788. if it.MaxUnit == baseDrug.MaxUnit {
  4789. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  4790. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  4791. sum_count += it.StockMaxNumber + it.StockMinNumber
  4792. sum_in_count += it.WarehousingCount
  4793. }
  4794. }
  4795. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  4796. if errThree != nil {
  4797. return errThree
  4798. }
  4799. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  4800. prescribingNumber_two_temp := deliver_number - stock_number
  4801. advice.CountUnit = drup.MinUnit
  4802. AutoDrugDeliverInfoTwentyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  4803. }
  4804. return
  4805. }
  4806. // 耗材手动出库
  4807. func ConsumablesDeliveryThirty(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64) (err error) {
  4808. var deliver_number int64 = 0
  4809. var stock_number int64 = 0
  4810. var maxNumber int64 = 0
  4811. deliver_number = goods.Count
  4812. // 根据先进先出原则,查询最先入库的批次,进行出库
  4813. // 如果没有对应的库存,则报错
  4814. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  4815. if err != nil {
  4816. goodErrcode := models.XtGoodErrcode{
  4817. UserOrgId: goods.OrgId,
  4818. Errcode: "查询库存信息报错",
  4819. GoodId: goods.GoodId,
  4820. Status: 1,
  4821. Ctime: time.Now().Unix(),
  4822. Mtime: 0,
  4823. Count: 0,
  4824. StockCount: 0,
  4825. Creater: creator,
  4826. BatchNumberId: warehouse.ID,
  4827. WarehouseOutId: 0,
  4828. }
  4829. CreateGoodErrcode(goodErrcode)
  4830. return err
  4831. }
  4832. stock_number = warehouse.StockCount
  4833. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  4834. if stock_number >= deliver_number {
  4835. maxNumber = goods.Count
  4836. warehouse.StockCount = warehouse.StockCount - maxNumber
  4837. warehouse.Mtime = time.Now().Unix()
  4838. //扣减库存
  4839. errThree := UpDateWarehouseInfoByStock(&warehouse)
  4840. if errThree != nil {
  4841. goodErrcode := models.XtGoodErrcode{
  4842. UserOrgId: goods.OrgId,
  4843. Errcode: "扣减库存失败",
  4844. GoodId: goods.GoodId,
  4845. Status: 1,
  4846. Ctime: time.Now().Unix(),
  4847. Mtime: 0,
  4848. Count: 0,
  4849. StockCount: 0,
  4850. Creater: creator,
  4851. BatchNumberId: warehouse.ID,
  4852. WarehouseOutId: 0,
  4853. }
  4854. CreateGoodErrcode(goodErrcode)
  4855. return errThree
  4856. }
  4857. //查询剩余库存
  4858. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  4859. var sum_count int64
  4860. for _, item := range goodList {
  4861. sum_count += item.StockCount
  4862. }
  4863. warehouseOutInfo := &models.WarehouseOutInfo{
  4864. WarehouseInfotId: warehouse.ID,
  4865. }
  4866. //添加入库单详情ID
  4867. errOne := UpdatedSigleWarehouseOutInfo(goods.ID, warehouseOutInfo)
  4868. if errOne != nil {
  4869. goodErrcode := models.XtGoodErrcode{
  4870. UserOrgId: goods.OrgId,
  4871. Errcode: "添加入库单详情ID",
  4872. GoodId: goods.GoodId,
  4873. Status: 1,
  4874. Ctime: time.Now().Unix(),
  4875. Mtime: 0,
  4876. Count: 0,
  4877. StockCount: 0,
  4878. Creater: creator,
  4879. BatchNumberId: warehouse.ID,
  4880. WarehouseOutId: 0,
  4881. }
  4882. CreateGoodErrcode(goodErrcode)
  4883. return errOne
  4884. }
  4885. stockFlow := models.VmStockFlow{
  4886. WarehousingId: warehouse.ID,
  4887. GoodId: goods.GoodId,
  4888. Number: warehouse.Number,
  4889. LicenseNumber: goods.LicenseNumber,
  4890. Count: goods.Count,
  4891. UserOrgId: goods.OrgId,
  4892. PatientId: goods.PatientId,
  4893. SystemTime: record_time,
  4894. ConsumableType: 2,
  4895. IsSys: goods.IsSys,
  4896. WarehousingOrder: warehouse.WarehousingOrder,
  4897. WarehouseOutId: warehouseOut.ID,
  4898. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  4899. IsEdit: 1,
  4900. CancelStockId: 0,
  4901. CancelOrderNumber: "",
  4902. Manufacturer: goods.Manufacturer,
  4903. Dealer: goods.Dealer,
  4904. Creator: creator,
  4905. UpdateCreator: 0,
  4906. Status: 1,
  4907. Ctime: record_time,
  4908. Mtime: time.Now().Unix(),
  4909. Price: warehouse.PackingPrice, //零售价
  4910. WarehousingDetailId: warehouse.ID,
  4911. WarehouseOutDetailId: goods.ID,
  4912. CancelOutDetailId: 0,
  4913. ProductDate: goods.ProductDate,
  4914. ExpireDate: goods.ExpiryDate,
  4915. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4916. StorehouseId: warehouse.StorehouseId,
  4917. AdminUserId: goods.AdminUserId,
  4918. BuyPrice: warehouse.Price, //进货价
  4919. StockCount: goods.StockCount,
  4920. BatchNumberCount: warehouse.StockCount - goods.Count,
  4921. IsCheck: 1,
  4922. OverCount: sum_count,
  4923. RegisterNumber: goods.RegisterNumber,
  4924. Remark: goods.Remark,
  4925. }
  4926. //创建出库流水
  4927. errflow := CreateStockFlowOne(stockFlow)
  4928. if errflow != nil {
  4929. goodErrcode := models.XtGoodErrcode{
  4930. UserOrgId: goods.OrgId,
  4931. Errcode: "创建流水报错",
  4932. GoodId: goods.GoodId,
  4933. Status: 1,
  4934. Ctime: time.Now().Unix(),
  4935. Mtime: 0,
  4936. Count: 0,
  4937. StockCount: 0,
  4938. Creater: creator,
  4939. BatchNumberId: warehouse.ID,
  4940. WarehouseOutId: 0,
  4941. }
  4942. CreateGoodErrcode(goodErrcode)
  4943. }
  4944. //添加入库ID
  4945. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, goods.OrgId, sum_count)
  4946. if errWarehouse != nil {
  4947. goodErrcode := models.XtGoodErrcode{
  4948. UserOrgId: goods.OrgId,
  4949. Errcode: "添加入库ID报错",
  4950. GoodId: goods.GoodId,
  4951. Status: 1,
  4952. Ctime: time.Now().Unix(),
  4953. Mtime: 0,
  4954. Count: 0,
  4955. StockCount: 0,
  4956. Creater: creator,
  4957. BatchNumberId: warehouse.ID,
  4958. WarehouseOutId: 0,
  4959. }
  4960. CreateGoodErrcode(goodErrcode)
  4961. }
  4962. //更新出库数量
  4963. errsumcode := ModifyGoodSumCount(goods.StorehouseId, goods.Count, goods.OrgId, goods.GoodId)
  4964. if errsumcode != nil {
  4965. goodErrcode := models.XtGoodErrcode{
  4966. UserOrgId: goods.OrgId,
  4967. Errcode: "更新出库数量报错",
  4968. GoodId: goods.GoodId,
  4969. Status: 1,
  4970. Ctime: time.Now().Unix(),
  4971. Mtime: 0,
  4972. Count: 0,
  4973. StockCount: 0,
  4974. Creater: creator,
  4975. BatchNumberId: warehouse.ID,
  4976. WarehouseOutId: 0,
  4977. }
  4978. CreateGoodErrcode(goodErrcode)
  4979. }
  4980. if warehouse.StockCount < maxNumber {
  4981. goodErrcode := models.XtGoodErrcode{
  4982. UserOrgId: goods.OrgId,
  4983. Errcode: "出库接口库存数量不足",
  4984. GoodId: goods.GoodId,
  4985. Status: 1,
  4986. Ctime: time.Now().Unix(),
  4987. Mtime: 0,
  4988. Count: 0,
  4989. StockCount: 0,
  4990. Creater: creator,
  4991. BatchNumberId: warehouse.ID,
  4992. WarehouseOutId: 0,
  4993. }
  4994. CreateGoodErrcode(goodErrcode)
  4995. return errors.New("库存数量不足")
  4996. }
  4997. return nil
  4998. } else {
  4999. // 出库完成后,要将该批次库存清零
  5000. warehouse.StockCount = 0
  5001. warehouse.Mtime = time.Now().Unix()
  5002. errThree := UpDateWarehouseInfoByStock(&warehouse)
  5003. if errThree != nil {
  5004. goodErrcode := models.XtGoodErrcode{
  5005. UserOrgId: goods.OrgId,
  5006. Errcode: "扣减库存报错",
  5007. GoodId: goods.GoodId,
  5008. Status: 1,
  5009. Ctime: time.Now().Unix(),
  5010. Mtime: 0,
  5011. Count: 0,
  5012. StockCount: 0,
  5013. Creater: creator,
  5014. BatchNumberId: warehouse.ID,
  5015. WarehouseOutId: 0,
  5016. }
  5017. CreateGoodErrcode(goodErrcode)
  5018. return errThree
  5019. }
  5020. //查询剩余库存
  5021. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  5022. var sum_count int64
  5023. for _, item := range goodList {
  5024. sum_count += item.StockCount
  5025. }
  5026. stockFlow := models.VmStockFlow{
  5027. WarehousingId: warehouse.ID,
  5028. GoodId: goods.GoodId,
  5029. Number: warehouse.Number,
  5030. LicenseNumber: goods.LicenseNumber,
  5031. Count: stock_number,
  5032. UserOrgId: goods.OrgId,
  5033. PatientId: goods.PatientId,
  5034. SystemTime: record_time,
  5035. ConsumableType: 2,
  5036. IsSys: goods.IsSys,
  5037. WarehousingOrder: warehouse.WarehousingOrder,
  5038. WarehouseOutId: warehouseOut.ID,
  5039. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  5040. IsEdit: 1,
  5041. CancelStockId: 0,
  5042. CancelOrderNumber: "",
  5043. Manufacturer: goods.Manufacturer,
  5044. Dealer: goods.Dealer,
  5045. Creator: creator,
  5046. UpdateCreator: 0,
  5047. Status: 1,
  5048. Ctime: record_time,
  5049. Mtime: time.Now().Unix(),
  5050. Price: warehouse.PackingPrice, //零售价
  5051. WarehousingDetailId: warehouse.ID,
  5052. WarehouseOutDetailId: goods.ID,
  5053. CancelOutDetailId: 0,
  5054. ProductDate: goods.ProductDate,
  5055. ExpireDate: goods.ExpiryDate,
  5056. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5057. StorehouseId: warehouse.StorehouseId,
  5058. AdminUserId: goods.AdminUserId,
  5059. BuyPrice: warehouse.Price, //进货价
  5060. StockCount: goods.StockCount,
  5061. BatchNumberCount: 0,
  5062. IsCheck: 1,
  5063. OverCount: sum_count,
  5064. RegisterNumber: goods.RegisterNumber,
  5065. Remark: goods.Remark,
  5066. }
  5067. //创建出库流水
  5068. errflow := CreateStockFlowOne(stockFlow)
  5069. if errflow != nil {
  5070. goodErrcode := models.XtGoodErrcode{
  5071. UserOrgId: goods.OrgId,
  5072. Errcode: "创建出库流水报错",
  5073. GoodId: goods.GoodId,
  5074. Status: 1,
  5075. Ctime: time.Now().Unix(),
  5076. Mtime: 0,
  5077. Count: 0,
  5078. StockCount: 0,
  5079. Creater: creator,
  5080. BatchNumberId: warehouse.ID,
  5081. WarehouseOutId: 0,
  5082. }
  5083. CreateGoodErrcode(goodErrcode)
  5084. }
  5085. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, goods.OrgId, sum_count)
  5086. if errWarehouse != nil {
  5087. goodErrcode := models.XtGoodErrcode{
  5088. UserOrgId: goods.OrgId,
  5089. Errcode: "添加入库ID报错",
  5090. GoodId: goods.GoodId,
  5091. Status: 1,
  5092. Ctime: time.Now().Unix(),
  5093. Mtime: 0,
  5094. Count: 0,
  5095. StockCount: 0,
  5096. Creater: creator,
  5097. BatchNumberId: warehouse.ID,
  5098. WarehouseOutId: 0,
  5099. }
  5100. CreateGoodErrcode(goodErrcode)
  5101. }
  5102. //更新出库数量
  5103. errcodecout := ModifyGoodSumCount(goods.StorehouseId, stock_number, goods.OrgId, goods.GoodId)
  5104. if errcodecout != nil {
  5105. goodErrcode := models.XtGoodErrcode{
  5106. UserOrgId: goods.OrgId,
  5107. Errcode: "更新出库数量报错",
  5108. GoodId: goods.GoodId,
  5109. Status: 1,
  5110. Ctime: time.Now().Unix(),
  5111. Mtime: 0,
  5112. Count: 0,
  5113. StockCount: 0,
  5114. Creater: creator,
  5115. BatchNumberId: warehouse.ID,
  5116. WarehouseOutId: 0,
  5117. }
  5118. CreateGoodErrcode(goodErrcode)
  5119. }
  5120. goods.Count = deliver_number - stock_number
  5121. ConsumablesDeliveryThirty(orgID, record_time, goods, warehouseOut, count, creator)
  5122. }
  5123. return nil
  5124. }
  5125. // 药品手动出库 递归方式
  5126. func AutoDrugDeliverInfoFourtyOne(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo, unit string) (err error) {
  5127. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  5128. var deliver_number int64 = 0
  5129. var stock_number int64 = 0
  5130. var drug_price float64
  5131. if advice.CountUnit == drup.MaxUnit {
  5132. deliver_number = prescribingNumber * drup.MinNumber
  5133. } else {
  5134. deliver_number = prescribingNumber
  5135. }
  5136. if advice.CountUnit == drup.MaxUnit {
  5137. drug_price = drup.RetailPrice
  5138. }
  5139. fmt.Println(drug_price)
  5140. if advice.CountUnit == drup.MinUnit {
  5141. drug_price = drup.MinPrice
  5142. }
  5143. if advice.CountUnit != drup.MaxUnit || advice.CountUnit != drup.MinUnit {
  5144. drug_price = drup.RetailPrice
  5145. }
  5146. // 根据先进先出原则,查询最先入库的批次,进行出库
  5147. // 如果没有对应的库存,则报错
  5148. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, advice.StorehouseId)
  5149. if lastWarehouse.MaxUnit != lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  5150. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  5151. var stockMax int64
  5152. var stockMin int64
  5153. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  5154. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  5155. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  5156. UpdateMinNumber(lastWarehouse.ID, stockMin)
  5157. }
  5158. }
  5159. if lastWarehouse.MaxUnit == lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  5160. lastWarehouse.StockMaxNumber = lastWarehouse.StockMaxNumber + lastWarehouse.StockMinNumber
  5161. ChangeMaxNumberOne(lastWarehouse.ID, lastWarehouse.StockMaxNumber)
  5162. }
  5163. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, advice.StorehouseId)
  5164. // 将该批次的剩余库存数量转换为拆零数量
  5165. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  5166. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  5167. }
  5168. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  5169. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  5170. }
  5171. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  5172. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  5173. }
  5174. if stock_number >= deliver_number {
  5175. var maxNumber int64 = 0
  5176. var minNumber int64 = 0
  5177. maxNumber = deliver_number / drup.MinNumber
  5178. minNumber = deliver_number % drup.MinNumber
  5179. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  5180. minNumber = maxNumber
  5181. }
  5182. if drup.MaxUnit != drup.MinUnit {
  5183. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  5184. return errors.New("库存数量不足")
  5185. }
  5186. }
  5187. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  5188. //fmt.Println("金1")
  5189. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  5190. }
  5191. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  5192. //fmt.Println("金2")
  5193. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  5194. }
  5195. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  5196. //fmt.Println("金3")
  5197. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  5198. }
  5199. if warehouse.StockMaxNumber < 0 {
  5200. warehouse.StockMaxNumber = 0
  5201. }
  5202. if warehouse.StockMinNumber < 0 {
  5203. warehouse.StockMinNumber = 0
  5204. }
  5205. warehouse.Mtime = time.Now().Unix()
  5206. //fmt.Println("stockmin", warehouse.StockMinNumber)
  5207. //fmt.Println("minnubmerr", minNumber)
  5208. if warehouse.StockMinNumber < minNumber {
  5209. //fmt.Println("金4")
  5210. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  5211. if warehouse.MaxUnit != warehouse.MinUnit {
  5212. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  5213. }
  5214. if minNumber == 2 && warehouse.MaxUnit == warehouse.MinUnit && warehouse.StockMinNumber == 0 {
  5215. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  5216. }
  5217. } else {
  5218. //fmt.Println("金5")
  5219. if minNumber > 0 {
  5220. if (warehouse.StockMinNumber - minNumber) >= 0 {
  5221. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  5222. }
  5223. }
  5224. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  5225. //fmt.Println("6")
  5226. if warehouse.StockMinNumber > 0 {
  5227. if warehouse.StockMinNumber-deliver_number >= 0 {
  5228. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  5229. }
  5230. }
  5231. }
  5232. }
  5233. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit && warehouse.MaxUnit != warehouse.MinUnit {
  5234. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  5235. //fmt.Println("金7")
  5236. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  5237. }
  5238. }
  5239. if drup.MaxUnit != drup.MinUnit {
  5240. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  5241. return errors.New("库存数量不足")
  5242. }
  5243. }
  5244. if warehouse.StockMinNumber <= 0 {
  5245. warehouse.StockMinNumber = 0
  5246. }
  5247. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  5248. if errThree != nil {
  5249. return errors.New("扣减库存失败")
  5250. }
  5251. //查询该药品剩余库存
  5252. infolist, _ := FindDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId)
  5253. var over_count int64
  5254. for _, its := range infolist {
  5255. if its.MaxUnit == drup.MaxUnit {
  5256. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  5257. }
  5258. over_count += its.StockMaxNumber + its.StockMinNumber
  5259. }
  5260. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  5261. drugflow := models.DrugFlow{
  5262. WarehouseOutId: warehouseout.ID,
  5263. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  5264. DrugId: advice.DrugId,
  5265. Number: warehouse.Number,
  5266. ProductDate: advice.ProductDate,
  5267. ExpireDate: advice.ExpiryDate,
  5268. Count: deliver_number,
  5269. Price: warehouse.RetailPrice,
  5270. Status: 1,
  5271. Ctime: advice.SysRecordTime,
  5272. Mtime: time.Now().Unix(),
  5273. UserOrgId: advice.OrgId,
  5274. Manufacturer: advice.Manufacturer,
  5275. Dealer: advice.Dealer,
  5276. BatchNumber: warehouse.BatchNumber,
  5277. MaxUnit: drup.MinUnit,
  5278. ConsumableType: 2,
  5279. IsEdit: 1,
  5280. Creator: 0,
  5281. IsSys: 0,
  5282. WarehouseOutDetailId: drugWareInfo.ID,
  5283. AdviceId: advice.ID,
  5284. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5285. StorehouseId: advice.StorehouseId,
  5286. AdminUserId: advice.AdminUserId,
  5287. LastPrice: warehouse.Price,
  5288. StockCount: advice.StockCount,
  5289. WarehousingDetailId: warehouse.ID,
  5290. OverCount: over_count,
  5291. RetailPrice: drug_price, //手动出库出库价格
  5292. WarehousingId: warehouse.ID,
  5293. SystemTime: advice.SysRecordTime,
  5294. PatientId: advice.PatientId,
  5295. Remark: advice.Remark,
  5296. }
  5297. if warehouse.RetailPrice == 0 {
  5298. drugflow.Price = advice.Price
  5299. }
  5300. CreateDrugFlowOne(drugflow)
  5301. //更新出库id
  5302. UpdateDrugWarehouseOutInfo(advice.ID, warehouse.ID, advice.OrgId, over_count, drug_price)
  5303. return nil
  5304. } else {
  5305. stock_number_int := strconv.FormatInt(stock_number, 10)
  5306. stock_number_int_count, _ := strconv.ParseFloat(stock_number_int, 64)
  5307. drug_price = stock_number_int_count * advice.Price
  5308. //扣减库存
  5309. warehouse.StockMaxNumber = 0
  5310. warehouse.StockMinNumber = 0
  5311. warehouse.Mtime = time.Now().Unix()
  5312. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  5313. if errThree != nil {
  5314. return errThree
  5315. }
  5316. //查询该药品剩余库存
  5317. infolist, _ := FindDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId)
  5318. var over_count int64
  5319. for _, its := range infolist {
  5320. if its.MaxUnit == drup.MaxUnit {
  5321. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  5322. }
  5323. over_count += its.StockMaxNumber + its.StockMinNumber
  5324. }
  5325. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  5326. drugflow := models.DrugFlow{
  5327. WarehouseOutId: warehouseout.ID,
  5328. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  5329. DrugId: advice.DrugId,
  5330. Number: warehouse.Number,
  5331. ProductDate: advice.ProductDate,
  5332. ExpireDate: advice.ExpiryDate,
  5333. Count: stock_number,
  5334. Price: warehouse.RetailPrice,
  5335. Status: 1,
  5336. Ctime: advice.SysRecordTime,
  5337. Mtime: time.Now().Unix(),
  5338. UserOrgId: advice.OrgId,
  5339. Manufacturer: advice.Manufacturer,
  5340. Dealer: advice.Dealer,
  5341. BatchNumber: warehouse.BatchNumber,
  5342. MaxUnit: drup.MinUnit,
  5343. ConsumableType: 2,
  5344. IsEdit: 1,
  5345. Creator: 0,
  5346. IsSys: 0,
  5347. WarehouseOutDetailId: drugWareInfo.ID,
  5348. AdviceId: advice.ID,
  5349. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5350. StorehouseId: advice.StorehouseId,
  5351. AdminUserId: advice.AdminUserId,
  5352. LastPrice: warehouse.Price,
  5353. StockCount: advice.StockCount,
  5354. OverCount: over_count,
  5355. RetailPrice: drug_price, //手动出库出库价格
  5356. WarehousingId: warehouse.ID,
  5357. WarehousingDetailId: warehouse.ID,
  5358. SystemTime: advice.SysRecordTime,
  5359. PatientId: advice.PatientId,
  5360. Remark: advice.Remark,
  5361. }
  5362. CreateDrugFlowOne(drugflow)
  5363. //更新出库id
  5364. UpdateDrugWarehouseOutInfo(advice.ID, warehouse.ID, advice.OrgId, over_count, drug_price)
  5365. // 出库完成后,要将该批次库存清零
  5366. prescribingNumber_two_temp := deliver_number - stock_number
  5367. advice.CountUnit = drup.MinUnit
  5368. AutoDrugDeliverInfoFourtyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, unit)
  5369. }
  5370. return
  5371. }
  5372. func UpdateDrugWarehouseOutInfo(id int64, warehouse_info_id int64, org_id int64, over_count int64, drug_price float64) error {
  5373. ut := XTWriteDB().Begin()
  5374. 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
  5375. if err != nil {
  5376. ut.Rollback()
  5377. return err
  5378. }
  5379. ut.Commit()
  5380. return err
  5381. }
  5382. func UpdateNewDrugWarehouseOutInfo(id int64, warehouse_info_id int64, org_id int64, over_count int64, drug_price float64, tx *gorm.DB) error {
  5383. err = tx.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
  5384. if err != nil {
  5385. tx.Rollback()
  5386. return err
  5387. }
  5388. return err
  5389. }
  5390. func UpdateGoodWarehouseOutById(id int64, warehouse_info_id int64, org_id int64, over_count int64) error {
  5391. utx := XTWriteDB().Begin()
  5392. 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
  5393. if err != nil {
  5394. utx.Rollback()
  5395. return err
  5396. }
  5397. utx.Commit()
  5398. return err
  5399. }
  5400. func UpdateNewGoodWarehouseOutById(id int64, warehouse_info_id int64, org_id int64, over_count int64, tx *gorm.DB) error {
  5401. 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
  5402. if err != nil {
  5403. tx.Rollback()
  5404. return err
  5405. }
  5406. return err
  5407. }
  5408. func ChangeMaxNumber(id int64, count int64) error {
  5409. 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
  5410. return err
  5411. }
  5412. func ChangeNewMaxNumber(id int64, count int64, tx *gorm.DB) error {
  5413. 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
  5414. if err != nil {
  5415. tx.Rollback()
  5416. return err
  5417. }
  5418. return err
  5419. }
  5420. func ChangeMaxNumberOne(id int64, count int64) error {
  5421. 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
  5422. return err
  5423. }
  5424. func ChangeNewMaxNumberOne(id int64, count int64, tx *gorm.DB) error {
  5425. 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
  5426. if err != nil {
  5427. tx.Rollback()
  5428. return err
  5429. }
  5430. return err
  5431. }
  5432. func UpdateMinNumber(id int64, count int64) error {
  5433. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1 and is_check = 1", id).Update(map[string]interface{}{"stock_min_number": count}).Error
  5434. return err
  5435. }
  5436. func UpdateNewMinNumber(id int64, count int64, tx *gorm.DB) error {
  5437. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1 and is_check = 1", id).Update(map[string]interface{}{"stock_min_number": count}).Error
  5438. if err != nil {
  5439. tx.Rollback()
  5440. return err
  5441. }
  5442. return err
  5443. }
  5444. func FindLastDrugWarehouseOutInfo(orgid int64) (models.DrugWarehouseOutInfo, error) {
  5445. info := models.DrugWarehouseOutInfo{}
  5446. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&info).Error
  5447. return info, err
  5448. }
  5449. func FindNewLastDrugWarehouseOutInfo(orgid int64, tx *gorm.DB) (models.DrugWarehouseOutInfo, error) {
  5450. info := models.DrugWarehouseOutInfo{}
  5451. err := tx.Where("org_id = ? and status = 1", orgid).Last(&info).Error
  5452. if err != gorm.ErrRecordNotFound {
  5453. if err != nil {
  5454. tx.Rollback()
  5455. return info, err
  5456. }
  5457. }
  5458. return info, err
  5459. }
  5460. func GetAllGoodSumCount(goodid int64, orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  5461. 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
  5462. return info, err
  5463. }
  5464. func GetNewAllGoodSumCount(goodid int64, orgid int64, storehouse_id int64, tx *gorm.DB) (info []*models.WarehousingInfo, err error) {
  5465. 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
  5466. if err != gorm.ErrRecordNotFound {
  5467. if err != nil {
  5468. tx.Rollback()
  5469. return info, err
  5470. }
  5471. }
  5472. return info, err
  5473. }
  5474. func CreatedFlushInfo(info models.XtWarehouseFlushInfo) error {
  5475. ut := writeDb.Begin()
  5476. err := ut.Create(&info).Error
  5477. if err != nil {
  5478. ut.Rollback()
  5479. return err
  5480. }
  5481. ut.Commit()
  5482. return err
  5483. }
  5484. func FindDrugWarehouseInfoList(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5485. 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
  5486. return info, err
  5487. }
  5488. func FindNewDrugWarehouseInfoList(drugid int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  5489. err = tx.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
  5490. if err != gorm.ErrRecordNotFound {
  5491. if err != nil {
  5492. tx.Rollback()
  5493. return info, err
  5494. }
  5495. }
  5496. return info, err
  5497. }
  5498. func CreatedDrugFlushInfo(info models.XtDrugWarehouseFlushInfo) error {
  5499. ut := writeDb.Begin()
  5500. err := ut.Create(&info).Error
  5501. if err != nil {
  5502. ut.Rollback()
  5503. return err
  5504. }
  5505. ut.Commit()
  5506. return err
  5507. }
  5508. func CreatedNewDrugFlushInfo(info models.XtDrugWarehouseFlushInfo, tx *gorm.DB) error {
  5509. err := tx.Create(&info).Error
  5510. if err != nil {
  5511. tx.Rollback()
  5512. return err
  5513. }
  5514. return err
  5515. }
  5516. 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 {
  5517. 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
  5518. return err
  5519. }
  5520. func FindLastWarehouseOutInfo(orgId int64) (models.WarehouseOutInfo, error) {
  5521. info := models.WarehouseOutInfo{}
  5522. err := XTReadDB().Model(&info).Where("org_id = ? and status = 1", orgId).Find(&info).Error
  5523. return info, err
  5524. }
  5525. func ModifyAddGoodInformation(count int64, id int64) error {
  5526. ut := XTWriteDB().Begin()
  5527. err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", count)).Error
  5528. if err != nil {
  5529. ut.Rollback()
  5530. return err
  5531. }
  5532. ut.Commit()
  5533. return err
  5534. }
  5535. func ModifyReduceGoodInformation(count int64, id int64) error {
  5536. ut := XTWriteDB().Begin()
  5537. err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", count)).Error
  5538. if err != nil {
  5539. ut.Rollback()
  5540. return err
  5541. }
  5542. ut.Commit()
  5543. return err
  5544. }
  5545. func ModifyAddCancelCount(count int64, id int64) error {
  5546. ut := XTWriteDB().Begin()
  5547. err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", count)).Error
  5548. if err != nil {
  5549. ut.Rollback()
  5550. return err
  5551. }
  5552. ut.Commit()
  5553. return err
  5554. }
  5555. func ModifyReduceCancelCount(count int64, id int64) error {
  5556. ut := XTWriteDB().Begin()
  5557. err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", count)).Error
  5558. if err != nil {
  5559. ut.Rollback()
  5560. return err
  5561. }
  5562. ut.Commit()
  5563. return err
  5564. }
  5565. func ModifyAddDrugInformation(id int64, count int64, orgid int64) error {
  5566. ut := XTWriteDB().Begin()
  5567. 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
  5568. if err != nil {
  5569. ut.Rollback()
  5570. return err
  5571. }
  5572. ut.Commit()
  5573. return err
  5574. }
  5575. func ModifyReduceDrugInformation(id int64, count int64, orgid int64) error {
  5576. ut := XTWriteDB().Begin()
  5577. 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
  5578. if err != nil {
  5579. ut.Rollback()
  5580. return err
  5581. }
  5582. ut.Commit()
  5583. return err
  5584. }
  5585. func ModifyNewReduceDrugInformation(id int64, count int64, orgid int64, tx *gorm.DB) error {
  5586. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", count)).Error
  5587. if err != nil {
  5588. tx.Rollback()
  5589. return err
  5590. }
  5591. return err
  5592. }
  5593. func ModifyAddCancelAddInfomation(id int64, count int64, orgid int64, storehouse_id int64) error {
  5594. ut := XTWriteDB().Begin()
  5595. 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
  5596. if err != nil {
  5597. ut.Rollback()
  5598. return err
  5599. }
  5600. ut.Commit()
  5601. return err
  5602. }
  5603. func ModifyAddDrugOutReduceInfomation(id int64, count int64, orgid int64, storehouse_id int64) error {
  5604. ut := XTWriteDB().Begin()
  5605. 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
  5606. if err != nil {
  5607. ut.Rollback()
  5608. return err
  5609. }
  5610. ut.Commit()
  5611. return err
  5612. }
  5613. func ModifyAddDrugOutAddInfomation(id int64, count int64, orgid int64, storehouse_id int64) error {
  5614. ut := XTWriteDB().Begin()
  5615. 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
  5616. if err != nil {
  5617. ut.Rollback()
  5618. return err
  5619. }
  5620. ut.Commit()
  5621. return err
  5622. }
  5623. func ModifyDrugCancelWarehouseInfo(id int64, count int64, orgid int64, storehouse_id int64) error {
  5624. ut := XTWriteDB().Begin()
  5625. 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
  5626. if err != nil {
  5627. ut.Rollback()
  5628. return err
  5629. }
  5630. ut.Commit()
  5631. return err
  5632. }
  5633. func ModifyGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
  5634. ut := XTWriteDB().Begin()
  5635. 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
  5636. if err != nil {
  5637. ut.Rollback()
  5638. return err
  5639. }
  5640. 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
  5641. if err != nil {
  5642. ut.Rollback()
  5643. return err
  5644. }
  5645. ut.Commit()
  5646. return err
  5647. }
  5648. func ModifyNewGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64, tx *gorm.DB) error {
  5649. 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
  5650. if err != nil {
  5651. tx.Rollback()
  5652. return err
  5653. }
  5654. 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
  5655. if err != nil {
  5656. tx.Rollback()
  5657. return err
  5658. }
  5659. return err
  5660. }
  5661. func ModifyReduceGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
  5662. ut := XTWriteDB().Begin()
  5663. 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
  5664. if err != nil {
  5665. ut.Rollback()
  5666. return err
  5667. }
  5668. 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
  5669. if err != nil {
  5670. ut.Rollback()
  5671. return err
  5672. }
  5673. ut.Commit()
  5674. return err
  5675. }
  5676. func ModifyNewReduceGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64, tx *gorm.DB) error {
  5677. 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
  5678. if err != nil {
  5679. tx.Rollback()
  5680. return err
  5681. }
  5682. 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
  5683. if err != nil {
  5684. tx.Rollback()
  5685. return err
  5686. }
  5687. return err
  5688. }
  5689. func ModifyReduceGoodSumCountTwenty(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
  5690. ut := XTWriteDB().Begin()
  5691. 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
  5692. if err != nil {
  5693. ut.Rollback()
  5694. return err
  5695. }
  5696. ut.Commit()
  5697. return err
  5698. }
  5699. func ModifyAddGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
  5700. ut := XTWriteDB().Begin()
  5701. 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
  5702. if err != nil {
  5703. ut.Rollback()
  5704. return err
  5705. }
  5706. ut.Commit()
  5707. return err
  5708. }
  5709. func ModifyNewAddGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64, tx *gorm.DB) error {
  5710. 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
  5711. if err != nil {
  5712. tx.Rollback()
  5713. return err
  5714. }
  5715. return err
  5716. }
  5717. func UpdateSumAddCancelCount(user_org_id int64, good_id int64, storehouse_id int64, count int64) error {
  5718. ut := XTWriteDB().Begin()
  5719. 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
  5720. if err != nil {
  5721. ut.Rollback()
  5722. return err
  5723. }
  5724. ut.Commit()
  5725. return err
  5726. }
  5727. func UpdateNewSumAddCancelCount(user_org_id int64, good_id int64, storehouse_id int64, count int64, tx *gorm.DB) error {
  5728. 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
  5729. if err != nil {
  5730. tx.Rollback()
  5731. return err
  5732. }
  5733. return err
  5734. }
  5735. func UpdateSumReduceCancelCount(user_org_id int64, good_id int64, storehouse_id int64, count int64) error {
  5736. ut := XTWriteDB().Begin()
  5737. 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
  5738. if err != nil {
  5739. ut.Rollback()
  5740. return err
  5741. }
  5742. ut.Commit()
  5743. return err
  5744. }
  5745. func UpdateSumAddOutCount(user_org_id int64, good_id int64, storehouse_id int64, count int64) error {
  5746. ut := XTWriteDB().Begin()
  5747. 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
  5748. if err != nil {
  5749. ut.Rollback()
  5750. return err
  5751. }
  5752. ut.Commit()
  5753. return err
  5754. }
  5755. func FindDrugWarehouseInfoFlushCount(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5756. err = XTReadDB().Where("drug_id = ? and org_id = ? and status = 1 and is_check = 1", drug_id, orgid).Find(&info).Error
  5757. return info, err
  5758. }
  5759. func FindNewDrugWarehouseInfoFlushCount(drug_id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  5760. err = tx.Where("drug_id = ? and org_id = ? and status = 1 and is_check = 1", drug_id, orgid).Find(&info).Error
  5761. if err != gorm.ErrRecordNotFound {
  5762. if err != nil {
  5763. tx.Rollback()
  5764. return info, err
  5765. }
  5766. }
  5767. return info, err
  5768. }
  5769. func FindDrugWarehouseInfoFlushCountByStorehouse(drug_id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
  5770. 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
  5771. return info, err
  5772. }
  5773. func FindNewDrugWarehouseInfoFlushCountByStorehouse(drug_id int64, orgid int64, storehouse_id int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  5774. err = tx.Where("drug_id = ? and org_id = ? and status = 1 and is_check = 1 and storehouse_id =?", drug_id, orgid, storehouse_id).Find(&info).Error
  5775. if err != gorm.ErrRecordNotFound {
  5776. if err != nil {
  5777. tx.Rollback()
  5778. return info, err
  5779. }
  5780. }
  5781. return info, err
  5782. }
  5783. // 结算出库
  5784. func HisSettleDrugsDelivery(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo, order_id int64) (err error) {
  5785. // 1.判断药品是否来自专用字典的药品库
  5786. // 2.判断当天当前机构有没有创建出库单,没有则创建
  5787. // 3.创建出库流程
  5788. // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  5789. isHasWay := false
  5790. record_time := int64(0)
  5791. isHasWay = true
  5792. record_time = advice.RecordDate
  5793. if isHasWay {
  5794. //判断当天当前机构有没有创建结算出库单,没有则创建
  5795. out, err := FindDrugSettleStockOutByIsSys(orgID, 5, record_time)
  5796. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  5797. if err == gorm.ErrRecordNotFound {
  5798. timeStr := time.Now().Format("2006-01-02")
  5799. timeArr := strings.Split(timeStr, "-")
  5800. total, _ := FindAllDrugWarehouseOut(orgID)
  5801. total = total + 1
  5802. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  5803. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  5804. number = number + total
  5805. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  5806. warehouseOut := models.DrugWarehouseOut{
  5807. WarehouseOutOrderNumber: warehousing_out_order,
  5808. OperationTime: time.Now().Unix(),
  5809. OrgId: orgID,
  5810. Creater: creater,
  5811. Ctime: time.Now().Unix(),
  5812. Status: 1,
  5813. WarehouseOutTime: record_time,
  5814. Dealer: 0,
  5815. Manufacturer: 0,
  5816. Type: 1, //结算出库
  5817. IsSys: 5,
  5818. StorehouseId: houseConfig.DrugStorehouseOut,
  5819. IsCheck: 1,
  5820. OrderId: order_id,
  5821. }
  5822. err := AddSigleDrugWarehouseOut(&warehouseOut)
  5823. if err != nil {
  5824. utils.TraceLog("创建出库单失败 err = %v", err)
  5825. return err
  5826. } else {
  5827. out = warehouseOut
  5828. }
  5829. }
  5830. // 出库流程
  5831. // 1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  5832. drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
  5833. if drup.ID > 0 {
  5834. prescribingNumber := advice.PrescribingNumber
  5835. var prescribingNumberOne int64
  5836. prescribingCount_temp := strconv.FormatFloat(math.Abs(advice.PrescribingNumber), 'f', 0, 64)
  5837. prescribe_count, _ := strconv.ParseInt(prescribingCount_temp, 10, 64)
  5838. if advice.PrescribingNumberUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  5839. prescribingNumberOne = prescribe_count * drup.MinNumber
  5840. }
  5841. if advice.PrescribingNumberUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  5842. prescribingNumberOne = prescribe_count
  5843. }
  5844. if advice.PrescribingNumberUnit == drup.MinUnit && drup.MaxUnit == drup.MinUnit {
  5845. prescribingNumberOne = prescribe_count
  5846. }
  5847. HisSettleDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice, prescribingNumberOne)
  5848. } else {
  5849. return errors.New("药品信息不存在")
  5850. }
  5851. }
  5852. return
  5853. }
  5854. // 耗材结算出库
  5855. func ConsumableSettleDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, creator int64, order_id int64) (err error) {
  5856. var deliver_number int64 = 0
  5857. var stock_number int64 = 0
  5858. var maxNumber int64 = 0
  5859. fmt.Println("223323233233223233223233223woo", goods.Count)
  5860. deliver_number = goods.Count
  5861. // 根据先进先出原则,查询最先入库的批次,进行出库
  5862. // 如果没有对应的库存,则报错
  5863. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  5864. if err != nil {
  5865. goodErrcode := models.XtGoodErrcode{
  5866. UserOrgId: goods.UserOrgId,
  5867. Errcode: "查询库存信息报错",
  5868. GoodId: goods.GoodId,
  5869. Status: 1,
  5870. Ctime: time.Now().Unix(),
  5871. Mtime: 0,
  5872. Count: 0,
  5873. StockCount: 0,
  5874. Creater: creator,
  5875. BatchNumberId: warehouse.ID,
  5876. WarehouseOutId: 0,
  5877. }
  5878. CreateGoodErrcode(goodErrcode)
  5879. return err
  5880. }
  5881. stock_number = warehouse.StockCount
  5882. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  5883. fmt.Println("stock_numnber233232232323", stock_number)
  5884. fmt.Println("deliver_nmuberwooeooo2oo23", deliver_number)
  5885. if stock_number >= deliver_number {
  5886. maxNumber = goods.Count
  5887. warehouse.StockCount = warehouse.StockCount - maxNumber
  5888. warehouse.Mtime = time.Now().Unix()
  5889. //扣减库存
  5890. errThree := UpDateWarehouseInfoByStock(&warehouse)
  5891. if errThree != nil {
  5892. goodErrcode := models.XtGoodErrcode{
  5893. UserOrgId: goods.UserOrgId,
  5894. Errcode: "扣减库存失败",
  5895. GoodId: goods.GoodId,
  5896. Status: 1,
  5897. Ctime: time.Now().Unix(),
  5898. Mtime: 0,
  5899. Count: 0,
  5900. StockCount: 0,
  5901. Creater: creator,
  5902. BatchNumberId: warehouse.ID,
  5903. WarehouseOutId: 0,
  5904. }
  5905. CreateGoodErrcode(goodErrcode)
  5906. return errThree
  5907. }
  5908. //查询剩余库存
  5909. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  5910. var sum_count int64
  5911. for _, item := range goodList {
  5912. sum_count += item.StockCount
  5913. }
  5914. warehouseOutInfo := &models.WarehouseOutInfo{
  5915. WarehouseInfotId: warehouse.ID,
  5916. }
  5917. //添加入库单详情ID
  5918. errOne := UpdatedSigleWarehouseOutInfo(goods.ID, warehouseOutInfo)
  5919. if errOne != nil {
  5920. goodErrcode := models.XtGoodErrcode{
  5921. UserOrgId: goods.UserOrgId,
  5922. Errcode: "添加入库单详情ID",
  5923. GoodId: goods.GoodId,
  5924. Status: 1,
  5925. Ctime: time.Now().Unix(),
  5926. Mtime: 0,
  5927. Count: 0,
  5928. StockCount: 0,
  5929. Creater: creator,
  5930. BatchNumberId: warehouse.ID,
  5931. WarehouseOutId: 0,
  5932. }
  5933. CreateGoodErrcode(goodErrcode)
  5934. return errOne
  5935. }
  5936. OutInfo := &models.WarehouseOutInfo{
  5937. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  5938. WarehouseOutId: warehouseOut.ID,
  5939. WarehouseInfotId: warehouse.ID,
  5940. Status: 1,
  5941. Ctime: time.Now().Unix(),
  5942. Remark: warehouse.Remark,
  5943. OrgId: orgID,
  5944. Type: 1,
  5945. Manufacturer: warehouse.Manufacturer,
  5946. Dealer: warehouse.Dealer,
  5947. IsSys: 5,
  5948. SysRecordTime: record_time,
  5949. GoodTypeId: goods.GoodTypeId,
  5950. GoodId: goods.GoodId,
  5951. PatientId: patient_id,
  5952. Number: warehouse.Number,
  5953. LicenseNumber: warehouse.LicenseNumber,
  5954. Price: warehouse.PackingPrice,
  5955. ExpiryDate: warehouse.ExpiryDate,
  5956. ProductDate: warehouse.ProductDate,
  5957. ProjectId: goods.ProjectId,
  5958. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5959. StorehouseId: goods.StorehouseId,
  5960. IsCheck: 1,
  5961. OverCount: sum_count,
  5962. RegisterNumber: warehouse.RegisterNumber,
  5963. OrderId: order_id,
  5964. }
  5965. OutInfo.Count = goods.Count
  5966. AddSigleWarehouseOutInfoOne(OutInfo)
  5967. stockFlow := models.VmStockFlow{
  5968. WarehousingId: warehouse.ID,
  5969. GoodId: goods.GoodId,
  5970. Number: warehouse.Number,
  5971. LicenseNumber: warehouse.LicenseNumber,
  5972. Count: goods.Count,
  5973. UserOrgId: orgID,
  5974. PatientId: patient_id,
  5975. SystemTime: record_time,
  5976. ConsumableType: 15,
  5977. IsSys: 5,
  5978. WarehousingOrder: warehouse.WarehousingOrder,
  5979. WarehouseOutId: warehouseOut.ID,
  5980. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  5981. IsEdit: 1,
  5982. CancelStockId: 0,
  5983. CancelOrderNumber: "",
  5984. Manufacturer: warehouse.Manufacturer,
  5985. Dealer: warehouse.Dealer,
  5986. Creator: creator,
  5987. UpdateCreator: 0,
  5988. Status: 1,
  5989. Ctime: time.Now().Unix(),
  5990. Mtime: 0,
  5991. Price: warehouse.PackingPrice, //零售价
  5992. WarehousingDetailId: warehouse.ID,
  5993. WarehouseOutDetailId: goods.ID,
  5994. CancelOutDetailId: 0,
  5995. ProductDate: warehouse.ProductDate,
  5996. ExpireDate: warehouse.ExpiryDate,
  5997. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5998. StorehouseId: warehouse.StorehouseId,
  5999. AdminUserId: creator,
  6000. BuyPrice: warehouse.Price, //进货价
  6001. StockCount: "",
  6002. BatchNumberCount: warehouse.StockCount - goods.Count,
  6003. IsCheck: 1,
  6004. OverCount: sum_count,
  6005. RegisterNumber: warehouse.RegisterNumber,
  6006. OrderId: order_id,
  6007. ProjectId: goods.ProjectId,
  6008. }
  6009. //创建出库流水
  6010. errflow := CreateStockFlowOne(stockFlow)
  6011. if errflow != nil {
  6012. goodErrcode := models.XtGoodErrcode{
  6013. UserOrgId: orgID,
  6014. Errcode: "创建流水报错",
  6015. GoodId: goods.GoodId,
  6016. Status: 1,
  6017. Ctime: time.Now().Unix(),
  6018. Mtime: 0,
  6019. Count: 0,
  6020. StockCount: 0,
  6021. Creater: creator,
  6022. BatchNumberId: warehouse.ID,
  6023. WarehouseOutId: 0,
  6024. }
  6025. CreateGoodErrcode(goodErrcode)
  6026. }
  6027. //添加入库ID
  6028. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count)
  6029. if errWarehouse != nil {
  6030. goodErrcode := models.XtGoodErrcode{
  6031. UserOrgId: orgID,
  6032. Errcode: "添加入库ID报错",
  6033. GoodId: goods.GoodId,
  6034. Status: 1,
  6035. Ctime: time.Now().Unix(),
  6036. Mtime: 0,
  6037. Count: 0,
  6038. StockCount: 0,
  6039. Creater: creator,
  6040. BatchNumberId: warehouse.ID,
  6041. WarehouseOutId: 0,
  6042. }
  6043. CreateGoodErrcode(goodErrcode)
  6044. }
  6045. //更新出库数量
  6046. errsumcode := ModifyGoodSumCount(goods.StorehouseId, goods.Count, orgID, goods.GoodId)
  6047. if errsumcode != nil {
  6048. goodErrcode := models.XtGoodErrcode{
  6049. UserOrgId: orgID,
  6050. Errcode: "更新出库数量报错",
  6051. GoodId: goods.GoodId,
  6052. Status: 1,
  6053. Ctime: time.Now().Unix(),
  6054. Mtime: 0,
  6055. Count: 0,
  6056. StockCount: 0,
  6057. Creater: creator,
  6058. BatchNumberId: warehouse.ID,
  6059. WarehouseOutId: 0,
  6060. }
  6061. CreateGoodErrcode(goodErrcode)
  6062. }
  6063. if warehouse.StockCount < maxNumber {
  6064. goodErrcode := models.XtGoodErrcode{
  6065. UserOrgId: orgID,
  6066. Errcode: "出库接口库存数量不足",
  6067. GoodId: goods.GoodId,
  6068. Status: 1,
  6069. Ctime: time.Now().Unix(),
  6070. Mtime: 0,
  6071. Count: 0,
  6072. StockCount: 0,
  6073. Creater: creator,
  6074. BatchNumberId: warehouse.ID,
  6075. WarehouseOutId: 0,
  6076. }
  6077. CreateGoodErrcode(goodErrcode)
  6078. return errors.New("库存数量不足")
  6079. }
  6080. return nil
  6081. } else {
  6082. // 出库完成后,要将该批次库存清零
  6083. warehouse.StockCount = 0
  6084. warehouse.Mtime = time.Now().Unix()
  6085. errThree := UpDateWarehouseInfoByStock(&warehouse)
  6086. if errThree != nil {
  6087. goodErrcode := models.XtGoodErrcode{
  6088. UserOrgId: orgID,
  6089. Errcode: "扣减库存报错",
  6090. GoodId: goods.GoodId,
  6091. Status: 1,
  6092. Ctime: time.Now().Unix(),
  6093. Mtime: 0,
  6094. Count: 0,
  6095. StockCount: 0,
  6096. Creater: creator,
  6097. BatchNumberId: warehouse.ID,
  6098. WarehouseOutId: 0,
  6099. }
  6100. CreateGoodErrcode(goodErrcode)
  6101. return errThree
  6102. }
  6103. //查询剩余库存
  6104. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  6105. var sum_count int64
  6106. for _, item := range goodList {
  6107. sum_count += item.StockCount
  6108. }
  6109. OutInfo := &models.WarehouseOutInfo{
  6110. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  6111. WarehouseOutId: warehouseOut.ID,
  6112. WarehouseInfotId: warehouse.ID,
  6113. Status: 1,
  6114. Ctime: time.Now().Unix(),
  6115. Remark: warehouse.Remark,
  6116. OrgId: orgID,
  6117. Type: 1,
  6118. Manufacturer: warehouse.Manufacturer,
  6119. Dealer: warehouse.Dealer,
  6120. IsSys: 5,
  6121. SysRecordTime: record_time,
  6122. GoodTypeId: goods.GoodTypeId,
  6123. GoodId: goods.GoodId,
  6124. PatientId: patient_id,
  6125. Number: warehouse.Number,
  6126. LicenseNumber: warehouse.LicenseNumber,
  6127. Price: warehouse.PackingPrice,
  6128. ExpiryDate: warehouse.ExpiryDate,
  6129. ProductDate: warehouse.ProductDate,
  6130. ProjectId: goods.ProjectId,
  6131. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6132. StorehouseId: goods.StorehouseId,
  6133. IsCheck: 1,
  6134. OverCount: sum_count,
  6135. RegisterNumber: warehouse.RegisterNumber,
  6136. OrderId: order_id,
  6137. }
  6138. OutInfo.Count = stock_number
  6139. AddSigleWarehouseOutInfoOne(OutInfo)
  6140. stockFlow := models.VmStockFlow{
  6141. WarehousingId: warehouse.ID,
  6142. GoodId: goods.GoodId,
  6143. Number: warehouse.Number,
  6144. LicenseNumber: warehouse.LicenseNumber,
  6145. Count: stock_number,
  6146. UserOrgId: orgID,
  6147. PatientId: patient_id,
  6148. SystemTime: record_time,
  6149. ConsumableType: 15,
  6150. IsSys: 5,
  6151. WarehousingOrder: warehouse.WarehousingOrder,
  6152. WarehouseOutId: warehouseOut.ID,
  6153. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  6154. IsEdit: 1,
  6155. CancelStockId: 0,
  6156. CancelOrderNumber: "",
  6157. Manufacturer: warehouse.Manufacturer,
  6158. Dealer: warehouse.Dealer,
  6159. Creator: creator,
  6160. UpdateCreator: 0,
  6161. Status: 1,
  6162. Ctime: time.Now().Unix(),
  6163. Mtime: 0,
  6164. Price: warehouse.PackingPrice, //零售价
  6165. WarehousingDetailId: warehouse.ID,
  6166. WarehouseOutDetailId: goods.ID,
  6167. CancelOutDetailId: 0,
  6168. ProductDate: warehouse.ProductDate,
  6169. ExpireDate: warehouse.ExpiryDate,
  6170. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6171. StorehouseId: warehouse.StorehouseId,
  6172. AdminUserId: creator,
  6173. BuyPrice: warehouse.Price, //进货价
  6174. StockCount: "",
  6175. BatchNumberCount: 0,
  6176. IsCheck: 1,
  6177. OverCount: sum_count,
  6178. RegisterNumber: warehouse.RegisterNumber,
  6179. OrderId: order_id,
  6180. ProjectId: goods.ProjectId,
  6181. }
  6182. //创建出库流水
  6183. errflow := CreateStockFlowOne(stockFlow)
  6184. if errflow != nil {
  6185. goodErrcode := models.XtGoodErrcode{
  6186. UserOrgId: orgID,
  6187. Errcode: "创建出库流水报错",
  6188. GoodId: goods.GoodId,
  6189. Status: 1,
  6190. Ctime: time.Now().Unix(),
  6191. Mtime: 0,
  6192. Count: 0,
  6193. StockCount: 0,
  6194. Creater: creator,
  6195. BatchNumberId: warehouse.ID,
  6196. WarehouseOutId: 0,
  6197. }
  6198. CreateGoodErrcode(goodErrcode)
  6199. }
  6200. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count)
  6201. if errWarehouse != nil {
  6202. goodErrcode := models.XtGoodErrcode{
  6203. UserOrgId: orgID,
  6204. Errcode: "添加入库ID报错",
  6205. GoodId: goods.GoodId,
  6206. Status: 1,
  6207. Ctime: time.Now().Unix(),
  6208. Mtime: 0,
  6209. Count: 0,
  6210. StockCount: 0,
  6211. Creater: creator,
  6212. BatchNumberId: warehouse.ID,
  6213. WarehouseOutId: 0,
  6214. }
  6215. CreateGoodErrcode(goodErrcode)
  6216. }
  6217. //更新出库数量
  6218. errcodecout := ModifyGoodSumCount(goods.StorehouseId, stock_number, orgID, goods.GoodId)
  6219. if errcodecout != nil {
  6220. goodErrcode := models.XtGoodErrcode{
  6221. UserOrgId: orgID,
  6222. Errcode: "更新出库数量报错",
  6223. GoodId: goods.GoodId,
  6224. Status: 1,
  6225. Ctime: time.Now().Unix(),
  6226. Mtime: 0,
  6227. Count: 0,
  6228. StockCount: 0,
  6229. Creater: creator,
  6230. BatchNumberId: warehouse.ID,
  6231. WarehouseOutId: 0,
  6232. }
  6233. CreateGoodErrcode(goodErrcode)
  6234. }
  6235. }
  6236. goods.Count = deliver_number - stock_number
  6237. prepare := &models.DialysisBeforePrepare{
  6238. Count: deliver_number - stock_number,
  6239. GoodTypeId: goods.GoodTypeId,
  6240. GoodId: goods.GoodId,
  6241. PatientId: goods.PatientId,
  6242. RecordDate: goods.RecordDate,
  6243. UserOrgId: orgID,
  6244. Ctime: time.Now().Unix(),
  6245. Creater: goods.Creater,
  6246. CommdityCode: goods.CommdityCode,
  6247. Status: 1,
  6248. StorehouseId: goods.StorehouseId,
  6249. }
  6250. ConsumableSettleDelivery(orgID, patient_id, record_time, prepare, warehouseOut, creator, order_id)
  6251. return nil
  6252. }
  6253. // 保存处方出库
  6254. func ConsumablePrescriptionDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, creator int64, count int64) (err error) {
  6255. var deliver_number int64 = 0
  6256. var stock_number int64 = 0
  6257. var maxNumber int64 = 0
  6258. deliver_number = goods.Count
  6259. // 根据先进先出原则,查询最先入库的批次,进行出库
  6260. // 如果没有对应的库存,则报错
  6261. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  6262. if err != nil {
  6263. goodErrcode := models.XtGoodErrcode{
  6264. UserOrgId: goods.UserOrgId,
  6265. Errcode: "查询库存信息报错",
  6266. GoodId: goods.GoodId,
  6267. Status: 1,
  6268. Ctime: time.Now().Unix(),
  6269. Mtime: 0,
  6270. Count: 0,
  6271. StockCount: 0,
  6272. Creater: creator,
  6273. BatchNumberId: warehouse.ID,
  6274. WarehouseOutId: 0,
  6275. }
  6276. CreateGoodErrcode(goodErrcode)
  6277. return err
  6278. }
  6279. stock_number = warehouse.StockCount
  6280. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6281. if stock_number >= deliver_number {
  6282. maxNumber = goods.Count
  6283. warehouse.StockCount = warehouse.StockCount - maxNumber
  6284. warehouse.Mtime = time.Now().Unix()
  6285. //扣减库存
  6286. errThree := UpDateWarehouseInfoByStock(&warehouse)
  6287. if errThree != nil {
  6288. goodErrcode := models.XtGoodErrcode{
  6289. UserOrgId: goods.UserOrgId,
  6290. Errcode: "扣减库存失败",
  6291. GoodId: goods.GoodId,
  6292. Status: 1,
  6293. Ctime: time.Now().Unix(),
  6294. Mtime: 0,
  6295. Count: 0,
  6296. StockCount: 0,
  6297. Creater: creator,
  6298. BatchNumberId: warehouse.ID,
  6299. WarehouseOutId: 0,
  6300. }
  6301. CreateGoodErrcode(goodErrcode)
  6302. return errThree
  6303. }
  6304. //查询剩余库存
  6305. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  6306. var sum_count int64
  6307. for _, item := range goodList {
  6308. sum_count += item.StockCount
  6309. }
  6310. //查询已经出库的数据
  6311. flowGood, _ := GetStockFlowIsBatchNumberSix(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6312. var out_count int64
  6313. var out_count_one int64
  6314. for _, item := range flowGood {
  6315. out_count += item.Count
  6316. }
  6317. //查询退库数据
  6318. flowGoodTwo, _ := GetStockFlowIsBatchNumberFour(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6319. for _, item := range flowGoodTwo {
  6320. out_count_one += item.Count
  6321. }
  6322. //flowGood, _ := GetStockFlowIsBatchNumberSeventy(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6323. //
  6324. //var out_count int64
  6325. //var out_count_one int64
  6326. var out_count_two int64
  6327. //
  6328. //for _, item := range flowGood {
  6329. // out_count += item.Count
  6330. //}
  6331. //查询退库数据
  6332. //flowGoodTwo, _ := GetStockFlowIsBatchNumberFourty(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6333. //for _, item := range flowGoodTwo {
  6334. // out_count_one += item.Count
  6335. //}
  6336. wareOutList, _ := GetStockFlowIsBatchNumberSixty(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6337. for _, item := range wareOutList {
  6338. out_count_two += item.Count
  6339. }
  6340. OutInfo := &models.WarehouseOutInfo{
  6341. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  6342. WarehouseOutId: warehouseOut.ID,
  6343. WarehouseInfotId: warehouse.ID,
  6344. Status: 1,
  6345. Ctime: time.Now().Unix(),
  6346. Remark: warehouse.Remark,
  6347. OrgId: orgID,
  6348. Type: 1,
  6349. Manufacturer: warehouse.Manufacturer,
  6350. Dealer: warehouse.Dealer,
  6351. IsSys: 1,
  6352. SysRecordTime: record_time,
  6353. GoodTypeId: goods.GoodTypeId,
  6354. GoodId: goods.GoodId,
  6355. PatientId: patient_id,
  6356. Number: warehouse.Number,
  6357. LicenseNumber: warehouse.LicenseNumber,
  6358. Price: warehouse.PackingPrice,
  6359. ExpiryDate: warehouse.ExpiryDate,
  6360. ProductDate: warehouse.ProductDate,
  6361. ProjectId: goods.ProjectId,
  6362. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6363. StorehouseId: goods.StorehouseId,
  6364. IsCheck: 1,
  6365. OverCount: sum_count,
  6366. RegisterNumber: warehouse.RegisterNumber,
  6367. }
  6368. OutInfo.Count = count - out_count_two + out_count_one
  6369. _, errcodes := GetNewWarehouseOutInfoIsExistSix(goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID)
  6370. if errcodes == gorm.ErrRecordNotFound {
  6371. errOne := AddSigleWarehouseOutInfoOne(OutInfo)
  6372. if errOne != nil {
  6373. return errOne
  6374. }
  6375. } else if errcodes == nil {
  6376. outInfoOne, _ := GetWarehouseOutInfoIsExistThree(goods.GoodId, patient_id, record_time, goods.ProjectId)
  6377. if count != outInfoOne.Count {
  6378. UpdatedWarehouseOutInfo(OutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId)
  6379. }
  6380. }
  6381. lastOut, _ := FindWarehouseOutInfoByPatientIdTwo(patient_id, record_time, goods.GoodId, orgID, goods.ProjectId)
  6382. //如果本次出库数据大于历史出库数据 新增1条流水
  6383. if count > (out_count - out_count_one) {
  6384. stockFlow := models.VmStockFlow{
  6385. WarehousingId: warehouse.ID,
  6386. GoodId: goods.GoodId,
  6387. Number: warehouse.Number,
  6388. LicenseNumber: warehouse.LicenseNumber,
  6389. Count: count - out_count + out_count_one,
  6390. UserOrgId: orgID,
  6391. PatientId: goods.PatientId,
  6392. SystemTime: record_time,
  6393. ConsumableType: 3,
  6394. IsSys: 1,
  6395. WarehousingOrder: warehouse.WarehousingOrder,
  6396. WarehouseOutId: warehouseOut.ID,
  6397. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  6398. IsEdit: 1,
  6399. CancelStockId: 0,
  6400. CancelOrderNumber: "",
  6401. Manufacturer: warehouse.Manufacturer,
  6402. Dealer: warehouse.Dealer,
  6403. Creator: creator,
  6404. UpdateCreator: 0,
  6405. Status: 1,
  6406. Ctime: time.Now().Unix(),
  6407. Mtime: 0,
  6408. Price: warehouse.PackingPrice, //零售价
  6409. WarehousingDetailId: warehouse.ID,
  6410. WarehouseOutDetailId: goods.ID,
  6411. CancelOutDetailId: 0,
  6412. ProductDate: warehouse.ProductDate,
  6413. ExpireDate: warehouse.ExpiryDate,
  6414. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6415. StorehouseId: warehouse.StorehouseId,
  6416. AdminUserId: creator,
  6417. BuyPrice: warehouse.Price, //进货价
  6418. StockCount: "",
  6419. BatchNumberCount: warehouse.StockCount - goods.Count,
  6420. IsCheck: 1,
  6421. OverCount: sum_count,
  6422. RegisterNumber: warehouse.RegisterNumber,
  6423. ProjectId: goods.ProjectId,
  6424. OperateTime: time.Now().Unix(),
  6425. }
  6426. //创建出库流水
  6427. errflow := CreateStockFlowOne(stockFlow)
  6428. if errflow != nil {
  6429. goodErrcode := models.XtGoodErrcode{
  6430. UserOrgId: orgID,
  6431. Errcode: "创建流水报错",
  6432. GoodId: goods.GoodId,
  6433. Status: 1,
  6434. Ctime: time.Now().Unix(),
  6435. Mtime: 0,
  6436. Count: 0,
  6437. StockCount: 0,
  6438. Creater: creator,
  6439. BatchNumberId: warehouse.ID,
  6440. WarehouseOutId: 0,
  6441. }
  6442. CreateGoodErrcode(goodErrcode)
  6443. }
  6444. fmt.Println("stockFlow.Count233232323232322323232332wo0-----------------------", stockFlow.Count)
  6445. //更新出库数量
  6446. errsumcode := ModifyGoodSumCount(goods.StorehouseId, stockFlow.Count, orgID, goods.GoodId)
  6447. if errsumcode != nil {
  6448. goodErrcode := models.XtGoodErrcode{
  6449. UserOrgId: orgID,
  6450. Errcode: "更新出库数量报错",
  6451. GoodId: goods.GoodId,
  6452. Status: 1,
  6453. Ctime: time.Now().Unix(),
  6454. Mtime: 0,
  6455. Count: 0,
  6456. StockCount: 0,
  6457. Creater: creator,
  6458. BatchNumberId: warehouse.ID,
  6459. WarehouseOutId: 0,
  6460. }
  6461. CreateGoodErrcode(goodErrcode)
  6462. }
  6463. }
  6464. //如果本次出库数据小于历史出库数据 新增1条退库流水
  6465. if count < (out_count - out_count_one) {
  6466. operation_time := time.Now().Unix()
  6467. //创建退库单
  6468. timeStr := time.Now().Format("2006-01-02")
  6469. timeArr := strings.Split(timeStr, "-")
  6470. total, _ := FindAllCancelStockTotal(orgID)
  6471. total = total + 1
  6472. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  6473. cancelStock := models.CancelStock{
  6474. OrderNumber: orderNumber,
  6475. OperaTime: operation_time,
  6476. OrgId: orgID,
  6477. Creater: warehouseOut.Creater,
  6478. Ctime: time.Now().Unix(),
  6479. Status: 1,
  6480. ReturnTime: record_time,
  6481. Type: 1,
  6482. StorehouseId: goods.StorehouseId,
  6483. IsCheck: 1,
  6484. }
  6485. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, orgID)
  6486. if msgerrkonde == gorm.ErrRecordNotFound {
  6487. AddSigleCancelStock(&cancelStock)
  6488. }
  6489. cancel, _ := GetLastCancelStockById(orgID)
  6490. manufacturer, _ := GetManufactureById(warehouse.Manufacturer)
  6491. deaerler, _ := GetDealerById(warehouse.Dealer)
  6492. cancelStockInfo := models.CancelStockInfo{
  6493. GoodId: goods.GoodId,
  6494. CancelStockId: cancel.ID,
  6495. GoodTypeId: goods.GoodTypeId,
  6496. Count: out_count - out_count_one - count,
  6497. Price: warehouse.PackingPrice,
  6498. Total: 0,
  6499. ProductDate: warehouse.ProductDate,
  6500. ExpiryDate: warehouse.ExpiryDate,
  6501. Ctime: time.Now().Unix(),
  6502. Status: 1,
  6503. OrgId: orgID,
  6504. OrderNumber: cancel.OrderNumber,
  6505. Type: 0,
  6506. Dealer: deaerler.DealerName,
  6507. Manufacturer: manufacturer.ManufacturerName,
  6508. Number: warehouse.Number,
  6509. RegisterAccount: "",
  6510. Remark: "",
  6511. WarehouseInfoId: warehouse.ID,
  6512. PatientId: patient_id,
  6513. RecordDate: record_time,
  6514. StorehouseId: goods.StorehouseId,
  6515. IsCheck: 1,
  6516. }
  6517. if orgID == 9671 || orgID == 10265 {
  6518. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  6519. cancelStockInfo.Price = goodsInfo.PackingPrice
  6520. }
  6521. CreateCancelStockInfoOne(&cancelStockInfo)
  6522. cancelInfo, _ := GetLastCancelStockInfoByGoodId(goods.GoodId)
  6523. flow := models.VmStockFlow{
  6524. WarehousingId: warehouse.ID,
  6525. GoodId: goods.GoodId,
  6526. Number: warehouse.Number,
  6527. LicenseNumber: warehouse.LicenseNumber,
  6528. Count: out_count - out_count_one - count,
  6529. UserOrgId: orgID,
  6530. PatientId: patient_id,
  6531. SystemTime: record_time,
  6532. ConsumableType: 7,
  6533. IsSys: 1,
  6534. WarehousingOrder: "",
  6535. WarehouseOutId: lastOut.WarehouseOutId,
  6536. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  6537. IsEdit: 0,
  6538. CancelStockId: cancel.ID,
  6539. CancelOrderNumber: cancel.OrderNumber,
  6540. Manufacturer: manufacturer.ID,
  6541. Dealer: 0,
  6542. Creator: warehouseOut.Creater,
  6543. UpdateCreator: 0,
  6544. Status: 1,
  6545. Ctime: time.Now().Unix(),
  6546. Mtime: 0,
  6547. Price: warehouse.PackingPrice,
  6548. WarehousingDetailId: warehouse.ID,
  6549. WarehouseOutDetailId: lastOut.ID,
  6550. CancelOutDetailId: cancelInfo.ID,
  6551. ProductDate: warehouse.ProductDate,
  6552. ExpireDate: warehouse.ExpiryDate,
  6553. StorehouseId: goods.StorehouseId,
  6554. BuyPrice: warehouse.Price,
  6555. ProjectId: goods.ProjectId,
  6556. OverCount: sum_count,
  6557. RegisterNumber: warehouse.RegisterNumber,
  6558. OperateTime: time.Now().Unix(),
  6559. }
  6560. if orgID == 9671 || orgID == 10265 {
  6561. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  6562. flow.Price = goodsInfo.PackingPrice
  6563. flow.BuyPrice = goodsInfo.BuyPrice
  6564. }
  6565. CreateStockFlowOne(flow)
  6566. //退库数量增加
  6567. UpdateSumAddCancelCount(orgID, goods.GoodId, goods.StorehouseId, flow.Count)
  6568. ModifyAddGoodSumCount(goods.StorehouseId, flow.Count, orgID, goods.GoodId)
  6569. }
  6570. //添加入库ID
  6571. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count)
  6572. if errWarehouse != nil {
  6573. goodErrcode := models.XtGoodErrcode{
  6574. UserOrgId: orgID,
  6575. Errcode: "添加入库ID报错",
  6576. GoodId: goods.GoodId,
  6577. Status: 1,
  6578. Ctime: time.Now().Unix(),
  6579. Mtime: 0,
  6580. Count: 0,
  6581. StockCount: 0,
  6582. Creater: creator,
  6583. BatchNumberId: warehouse.ID,
  6584. WarehouseOutId: 0,
  6585. }
  6586. CreateGoodErrcode(goodErrcode)
  6587. }
  6588. if warehouse.StockCount < maxNumber {
  6589. goodErrcode := models.XtGoodErrcode{
  6590. UserOrgId: orgID,
  6591. Errcode: "出库接口库存数量不足",
  6592. GoodId: goods.GoodId,
  6593. Status: 1,
  6594. Ctime: time.Now().Unix(),
  6595. Mtime: 0,
  6596. Count: 0,
  6597. StockCount: 0,
  6598. Creater: creator,
  6599. BatchNumberId: warehouse.ID,
  6600. WarehouseOutId: 0,
  6601. }
  6602. CreateGoodErrcode(goodErrcode)
  6603. return errors.New("库存数量不足")
  6604. }
  6605. return nil
  6606. } else {
  6607. // 出库完成后,要将该批次库存清零
  6608. warehouse.StockCount = 0
  6609. warehouse.Mtime = time.Now().Unix()
  6610. errThree := UpDateWarehouseInfoByStock(&warehouse)
  6611. if errThree != nil {
  6612. goodErrcode := models.XtGoodErrcode{
  6613. UserOrgId: orgID,
  6614. Errcode: "扣减库存报错",
  6615. GoodId: goods.GoodId,
  6616. Status: 1,
  6617. Ctime: time.Now().Unix(),
  6618. Mtime: 0,
  6619. Count: 0,
  6620. StockCount: 0,
  6621. Creater: creator,
  6622. BatchNumberId: warehouse.ID,
  6623. WarehouseOutId: 0,
  6624. }
  6625. CreateGoodErrcode(goodErrcode)
  6626. return errThree
  6627. }
  6628. //查询剩余库存
  6629. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  6630. var sum_count int64
  6631. for _, item := range goodList {
  6632. sum_count += item.StockCount
  6633. }
  6634. fmt.Println("剩余库存", sum_count)
  6635. OutInfo := &models.WarehouseOutInfo{
  6636. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  6637. WarehouseOutId: warehouseOut.ID,
  6638. WarehouseInfotId: warehouse.ID,
  6639. Status: 1,
  6640. Ctime: time.Now().Unix(),
  6641. Remark: warehouse.Remark,
  6642. OrgId: orgID,
  6643. Type: 1,
  6644. Manufacturer: warehouse.Manufacturer,
  6645. Dealer: warehouse.Dealer,
  6646. IsSys: 1,
  6647. SysRecordTime: record_time,
  6648. GoodTypeId: goods.GoodTypeId,
  6649. GoodId: goods.GoodId,
  6650. PatientId: patient_id,
  6651. Number: warehouse.Number,
  6652. LicenseNumber: warehouse.LicenseNumber,
  6653. Price: warehouse.PackingPrice,
  6654. ExpiryDate: warehouse.ExpiryDate,
  6655. ProductDate: warehouse.ProductDate,
  6656. ProjectId: goods.ProjectId,
  6657. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6658. StorehouseId: goods.StorehouseId,
  6659. IsCheck: 1,
  6660. OverCount: sum_count,
  6661. RegisterNumber: warehouse.RegisterNumber,
  6662. }
  6663. OutInfo.Count = stock_number
  6664. _, errcodes := GetNewWarehouseOutInfoIsExistSix(goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID)
  6665. if errcodes == gorm.ErrRecordNotFound {
  6666. errOne := AddSigleWarehouseOutInfoOne(OutInfo)
  6667. if errOne != nil {
  6668. return errOne
  6669. }
  6670. } else if errcodes == nil {
  6671. outInfoOne, _ := GetWarehouseOutInfoIsExistTen(goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID)
  6672. if count != outInfoOne.Count {
  6673. UpdatedWarehouseOutInfoSix(OutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID)
  6674. }
  6675. }
  6676. lastOut, _ := FindWarehouseOutInfoByPatientIdTwo(patient_id, record_time, goods.GoodId, orgID, goods.ProjectId)
  6677. //查询该该批次已经出库的数据
  6678. //flowGood, _ := GetStockFlowIsBatchNumber(warehouse.ID, patient_id, record_time, goods.GoodId)
  6679. //查询已经出库的数据
  6680. //flowGood, _ := GetStockFlowIsBatchNumberSeventy(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6681. //var out_count int64
  6682. //var out_count_one int64
  6683. //for _, item := range flowGood {
  6684. // out_count += item.Count
  6685. //}
  6686. //flowGoodTwo, _ := GetStockFlowIsBatchNumberSix(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6687. //for _, item := range flowGoodTwo {
  6688. // out_count_one += item.Count
  6689. //}
  6690. //查询该该批次已经出库的数据
  6691. flowGood, _ := GetStockFlowIsBatchNumberSix(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6692. var out_count int64
  6693. var out_count_one int64
  6694. for _, item := range flowGood {
  6695. out_count += item.Count
  6696. }
  6697. flowGoodTwo, _ := GetStockFlowIsBatchNumberFour(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6698. for _, item := range flowGoodTwo {
  6699. out_count_one += item.Count
  6700. }
  6701. //如果出库数量 大于 历史出库数据 新增1条流水
  6702. if count > out_count-out_count_one {
  6703. stockFlow := models.VmStockFlow{
  6704. WarehousingId: warehouse.ID,
  6705. GoodId: goods.GoodId,
  6706. Number: warehouse.Number,
  6707. LicenseNumber: warehouse.LicenseNumber,
  6708. Count: stock_number,
  6709. UserOrgId: orgID,
  6710. PatientId: goods.PatientId,
  6711. SystemTime: record_time,
  6712. ConsumableType: 3,
  6713. IsSys: 1,
  6714. WarehousingOrder: warehouse.WarehousingOrder,
  6715. WarehouseOutId: warehouseOut.ID,
  6716. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  6717. IsEdit: 1,
  6718. CancelStockId: 0,
  6719. CancelOrderNumber: "",
  6720. Manufacturer: warehouse.Manufacturer,
  6721. Dealer: warehouse.Dealer,
  6722. Creator: creator,
  6723. UpdateCreator: 0,
  6724. Status: 1,
  6725. Ctime: time.Now().Unix(),
  6726. Mtime: 0,
  6727. Price: warehouse.PackingPrice, //零售价
  6728. WarehousingDetailId: warehouse.ID,
  6729. WarehouseOutDetailId: goods.ID,
  6730. CancelOutDetailId: 0,
  6731. ProductDate: warehouse.ProductDate,
  6732. ExpireDate: warehouse.ExpiryDate,
  6733. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6734. StorehouseId: warehouse.StorehouseId,
  6735. AdminUserId: creator,
  6736. BuyPrice: warehouse.Price, //进货价
  6737. StockCount: "",
  6738. BatchNumberCount: 0,
  6739. IsCheck: 1,
  6740. OverCount: sum_count,
  6741. RegisterNumber: warehouse.RegisterNumber,
  6742. ProjectId: goods.ProjectId,
  6743. OperateTime: time.Now().Unix(),
  6744. }
  6745. //创建出库流水
  6746. errflow := CreateStockFlowOne(stockFlow)
  6747. if errflow != nil {
  6748. goodErrcode := models.XtGoodErrcode{
  6749. UserOrgId: orgID,
  6750. Errcode: "创建出库流水报错",
  6751. GoodId: goods.GoodId,
  6752. Status: 1,
  6753. Ctime: time.Now().Unix(),
  6754. Mtime: 0,
  6755. Count: 0,
  6756. StockCount: 0,
  6757. Creater: creator,
  6758. BatchNumberId: warehouse.ID,
  6759. WarehouseOutId: 0,
  6760. }
  6761. CreateGoodErrcode(goodErrcode)
  6762. }
  6763. //更新出库数量
  6764. errcodecout := ModifyGoodSumCount(goods.StorehouseId, stock_number, orgID, goods.GoodId)
  6765. if errcodecout != nil {
  6766. goodErrcode := models.XtGoodErrcode{
  6767. UserOrgId: orgID,
  6768. Errcode: "更新出库数量报错",
  6769. GoodId: goods.GoodId,
  6770. Status: 1,
  6771. Ctime: time.Now().Unix(),
  6772. Mtime: 0,
  6773. Count: 0,
  6774. StockCount: 0,
  6775. Creater: creator,
  6776. BatchNumberId: warehouse.ID,
  6777. WarehouseOutId: 0,
  6778. }
  6779. CreateGoodErrcode(goodErrcode)
  6780. }
  6781. }
  6782. //退库
  6783. if count < out_count-out_count_one {
  6784. operation_time := time.Now().Unix()
  6785. //创建退库单
  6786. timeStr := time.Now().Format("2006-01-02")
  6787. timeArr := strings.Split(timeStr, "-")
  6788. total, _ := FindAllCancelStockTotal(orgID)
  6789. total = total + 1
  6790. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  6791. cancelStock := models.CancelStock{
  6792. OrderNumber: orderNumber,
  6793. OperaTime: operation_time,
  6794. OrgId: orgID,
  6795. Creater: warehouseOut.Creater,
  6796. Ctime: time.Now().Unix(),
  6797. Status: 1,
  6798. ReturnTime: record_time,
  6799. Type: 1,
  6800. StorehouseId: goods.StorehouseId,
  6801. IsCheck: 1,
  6802. }
  6803. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, orgID)
  6804. if msgerrkonde == gorm.ErrRecordNotFound {
  6805. AddSigleCancelStock(&cancelStock)
  6806. }
  6807. cancel, _ := GetLastCancelStockById(orgID)
  6808. manufacturer, _ := GetManufactureById(warehouse.Manufacturer)
  6809. deaerler, _ := GetDealerById(warehouse.Dealer)
  6810. cancelStockInfo := models.CancelStockInfo{
  6811. GoodId: goods.GoodId,
  6812. CancelStockId: cancel.ID,
  6813. GoodTypeId: goods.GoodTypeId,
  6814. Count: out_count - out_count_one - count,
  6815. Price: warehouse.PackingPrice,
  6816. Total: 0,
  6817. ProductDate: warehouse.ProductDate,
  6818. ExpiryDate: warehouse.ExpiryDate,
  6819. Ctime: time.Now().Unix(),
  6820. Status: 1,
  6821. OrgId: orgID,
  6822. OrderNumber: cancel.OrderNumber,
  6823. Type: 0,
  6824. Dealer: deaerler.DealerName,
  6825. Manufacturer: manufacturer.ManufacturerName,
  6826. Number: warehouse.Number,
  6827. RegisterAccount: "",
  6828. Remark: "",
  6829. WarehouseInfoId: warehouse.ID,
  6830. PatientId: patient_id,
  6831. RecordDate: record_time,
  6832. StorehouseId: goods.StorehouseId,
  6833. IsCheck: 1,
  6834. }
  6835. if orgID == 9671 || orgID == 10265 {
  6836. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  6837. cancelStockInfo.Price = goodsInfo.PackingPrice
  6838. }
  6839. CreateCancelStockInfoOne(&cancelStockInfo)
  6840. cancelInfo, _ := GetLastCancelStockInfoByGoodId(goods.GoodId)
  6841. flow := models.VmStockFlow{
  6842. WarehousingId: warehouse.ID,
  6843. GoodId: goods.GoodId,
  6844. Number: warehouse.Number,
  6845. LicenseNumber: warehouse.LicenseNumber,
  6846. Count: out_count - count,
  6847. UserOrgId: orgID,
  6848. PatientId: patient_id,
  6849. SystemTime: record_time,
  6850. ConsumableType: 7,
  6851. IsSys: 1,
  6852. WarehousingOrder: "",
  6853. WarehouseOutId: lastOut.WarehouseOutId,
  6854. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  6855. IsEdit: 0,
  6856. CancelStockId: cancel.ID,
  6857. CancelOrderNumber: cancel.OrderNumber,
  6858. Manufacturer: manufacturer.ID,
  6859. Dealer: 0,
  6860. Creator: warehouseOut.Creater,
  6861. UpdateCreator: 0,
  6862. Status: 1,
  6863. Ctime: time.Now().Unix(),
  6864. Mtime: 0,
  6865. Price: warehouse.Price,
  6866. WarehousingDetailId: warehouse.ID,
  6867. WarehouseOutDetailId: lastOut.ID,
  6868. CancelOutDetailId: cancelInfo.ID,
  6869. ProductDate: warehouse.ProductDate,
  6870. ExpireDate: warehouse.ExpiryDate,
  6871. StorehouseId: goods.StorehouseId,
  6872. BuyPrice: warehouse.Price,
  6873. ProjectId: goods.ProjectId,
  6874. OverCount: sum_count,
  6875. RegisterNumber: warehouse.RegisterNumber,
  6876. OperateTime: time.Now().Unix(),
  6877. }
  6878. if orgID == 9671 || orgID == 10265 {
  6879. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  6880. flow.Price = goodsInfo.PackingPrice
  6881. flow.BuyPrice = goodsInfo.BuyPrice
  6882. }
  6883. CreateStockFlowOne(flow)
  6884. //退库数量增加
  6885. UpdateSumAddCancelCount(orgID, goods.GoodId, goods.StorehouseId, flow.Count)
  6886. //实际出库减少
  6887. ModifyAddGoodSumCount(goods.StorehouseId, flow.Count, orgID, goods.GoodId)
  6888. }
  6889. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count)
  6890. if errWarehouse != nil {
  6891. goodErrcode := models.XtGoodErrcode{
  6892. UserOrgId: orgID,
  6893. Errcode: "添加入库ID报错",
  6894. GoodId: goods.GoodId,
  6895. Status: 1,
  6896. Ctime: time.Now().Unix(),
  6897. Mtime: 0,
  6898. Count: 0,
  6899. StockCount: 0,
  6900. Creater: creator,
  6901. BatchNumberId: warehouse.ID,
  6902. WarehouseOutId: 0,
  6903. }
  6904. CreateGoodErrcode(goodErrcode)
  6905. }
  6906. }
  6907. goods.Count = deliver_number - stock_number
  6908. prepare := &models.DialysisBeforePrepare{
  6909. Count: deliver_number - stock_number,
  6910. GoodTypeId: goods.GoodTypeId,
  6911. GoodId: goods.GoodId,
  6912. PatientId: goods.PatientId,
  6913. RecordDate: goods.RecordDate,
  6914. UserOrgId: orgID,
  6915. Ctime: time.Now().Unix(),
  6916. Creater: creator,
  6917. Status: 1,
  6918. StorehouseId: goods.StorehouseId,
  6919. ProjectId: goods.ProjectId,
  6920. }
  6921. fmt.Println("剩余出库数量", prepare.Count)
  6922. ConsumablePrescriptionDelivery(orgID, patient_id, record_time, prepare, warehouseOut, creator, count)
  6923. return nil
  6924. }
  6925. func CreateAutoDetail(detail models.AutomaticReduceDetail) error {
  6926. err := XTWriteDB().Create(&detail).Error
  6927. return err
  6928. }
  6929. func CreateNewAutoDetail(detail models.AutomaticReduceDetail, tx *gorm.DB) error {
  6930. err := tx.Create(&detail).Error
  6931. if err != nil {
  6932. tx.Rollback()
  6933. return err
  6934. }
  6935. return err
  6936. }
  6937. func UpdateAutomaticReduce(patient_id int64, record_date int64, good_id int64, project_id int64) error {
  6938. 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
  6939. return err
  6940. }
  6941. func UpdateNewAutomaticReduce(patient_id int64, record_date int64, good_id int64, project_id int64, tx *gorm.DB) error {
  6942. 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
  6943. if err != nil {
  6944. tx.Rollback()
  6945. return err
  6946. }
  6947. return err
  6948. }
  6949. func GetDrugFLowByAdviceById(drug_id int64, patient_id int64, user_org_id int64, advice_date int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  6950. 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
  6951. return outInfo, err
  6952. }
  6953. func GetDrugFLowByAdviceByIdOne(drug_id int64, patient_id int64, user_org_id int64, advice_date int64, tx *gorm.DB) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  6954. 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
  6955. if err != gorm.ErrRecordNotFound {
  6956. if err != nil {
  6957. tx.Rollback()
  6958. return outInfo, err
  6959. }
  6960. }
  6961. return outInfo, err
  6962. }
  6963. func UpdateHisAdviceById(id int64) error {
  6964. err := XTWriteDB().Model(&models.HisDoctorAdviceInfo{}).Where("id = ?", id).Updates(map[string]interface{}{"execution_time": "", "execution_staff": "", "execution_state": 2}).Error
  6965. return err
  6966. }
  6967. func UpdateHisAdviceByIdOne(id int64, tx *gorm.DB) error {
  6968. err := tx.Model(&models.HisDoctorAdviceInfo{}).Where("id = ?", id).Updates(map[string]interface{}{"execution_time": "", "execution_staff": "", "execution_state": 2}).Error
  6969. if err != nil {
  6970. tx.Rollback()
  6971. return err
  6972. }
  6973. return err
  6974. }
  6975. func GetAdviceIsOut(advice_id int64, user_org_id int64, patient_id int64, advice_date int64, drug_id int64) (models.DrugWarehouseOutInfo, error) {
  6976. advice := models.DrugWarehouseOutInfo{}
  6977. 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
  6978. return advice, err
  6979. }
  6980. // 药品出库
  6981. func NewHisDrugsDelivery(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo, tx *gorm.DB) (err error) {
  6982. // 1.判断药品是否来自专用字典的药品库
  6983. // 2.判断当天当前机构有没有创建出库单,没有则创建
  6984. // 3.创建出库流程
  6985. // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  6986. isHasWay := false
  6987. record_time := int64(0)
  6988. isHasWay = true
  6989. record_time = advice.RecordDate
  6990. if isHasWay {
  6991. //判断当天当前机构有没有创建出库单,没有则创建
  6992. out, err := FindNewDrugStockOutByIsSys(orgID, 1, record_time, tx)
  6993. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6994. if err == gorm.ErrRecordNotFound {
  6995. timeStr := time.Now().Format("2006-01-02")
  6996. timeArr := strings.Split(timeStr, "-")
  6997. total, _ := FindAllDrugWarehouseOut(orgID)
  6998. total = total + 1
  6999. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  7000. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  7001. number = number + total
  7002. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  7003. warehouseOut := models.DrugWarehouseOut{
  7004. WarehouseOutOrderNumber: warehousing_out_order,
  7005. OperationTime: time.Now().Unix(),
  7006. OrgId: orgID,
  7007. Creater: creater,
  7008. Ctime: time.Now().Unix(),
  7009. Status: 1,
  7010. WarehouseOutTime: record_time,
  7011. Dealer: 0,
  7012. Manufacturer: 0,
  7013. Type: 1,
  7014. IsSys: 1,
  7015. StorehouseId: houseConfig.DrugStorehouseOut,
  7016. IsCheck: 1,
  7017. }
  7018. err := AddNewSigleDrugWarehouseOut(&warehouseOut, tx)
  7019. if err != nil {
  7020. utils.TraceLog("创建出库单失败 err = %v", err)
  7021. return err
  7022. } else {
  7023. out = warehouseOut
  7024. }
  7025. }
  7026. //出库流程
  7027. //1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  7028. drup, _ := FindNewBaseDrugLibRecord(orgID, advice.DrugId, tx)
  7029. if drup.ID > 0 {
  7030. prescribingNumber := advice.PrescribingNumber
  7031. HisStoryBloodHisDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice, prescribingNumber, advice.PrescribingNumberUnit, tx)
  7032. } else {
  7033. return errors.New("药品信息不存在")
  7034. }
  7035. }
  7036. return
  7037. }
  7038. // 保存处方出库
  7039. 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) {
  7040. var deliver_number int64 = 0
  7041. var stock_number int64 = 0
  7042. var maxNumber int64 = 0
  7043. deliver_number = goods.Count
  7044. // 根据先进先出原则,查询最先入库的批次,进行出库
  7045. // 如果没有对应的库存,则报错
  7046. warehouse, err := FindNewFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId, tx)
  7047. if err != nil {
  7048. goodErrcode := models.XtGoodErrcode{
  7049. UserOrgId: goods.UserOrgId,
  7050. Errcode: "查询库存信息报错",
  7051. GoodId: goods.GoodId,
  7052. Status: 1,
  7053. Ctime: time.Now().Unix(),
  7054. Mtime: 0,
  7055. Count: 0,
  7056. StockCount: 0,
  7057. Creater: creator,
  7058. BatchNumberId: warehouse.ID,
  7059. WarehouseOutId: 0,
  7060. }
  7061. CreateGoodErrcode(goodErrcode)
  7062. return err
  7063. }
  7064. stock_number = warehouse.StockCount
  7065. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  7066. if stock_number >= deliver_number {
  7067. maxNumber = goods.Count
  7068. warehouse.StockCount = warehouse.StockCount - maxNumber
  7069. warehouse.Mtime = time.Now().Unix()
  7070. //扣减库存
  7071. errThree := UpDateNewWarehouseInfoByStock(&warehouse, tx)
  7072. if errThree != nil {
  7073. goodErrcode := models.XtGoodErrcode{
  7074. UserOrgId: goods.UserOrgId,
  7075. Errcode: "扣减库存失败",
  7076. GoodId: goods.GoodId,
  7077. Status: 1,
  7078. Ctime: time.Now().Unix(),
  7079. Mtime: 0,
  7080. Count: 0,
  7081. StockCount: 0,
  7082. Creater: creator,
  7083. BatchNumberId: warehouse.ID,
  7084. WarehouseOutId: 0,
  7085. }
  7086. CreateGoodErrcode(goodErrcode)
  7087. return errThree
  7088. }
  7089. //查询剩余库存
  7090. goodList, _ := GetNewAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId, tx)
  7091. var sum_count int64
  7092. for _, item := range goodList {
  7093. sum_count += item.StockCount
  7094. }
  7095. //查询已经出库的数据
  7096. flowGood, _ := GetNewStockFlowIsBatchNumberSix(patient_id, record_time, goods.GoodId, goods.ProjectId, tx)
  7097. var out_count int64
  7098. var out_count_one int64
  7099. for _, item := range flowGood {
  7100. out_count += item.Count
  7101. }
  7102. //查询退库数据
  7103. flowGoodTwo, _ := GetNewStockFlowIsBatchNumberFour(patient_id, record_time, goods.GoodId, goods.ProjectId, tx)
  7104. for _, item := range flowGoodTwo {
  7105. out_count_one += item.Count
  7106. }
  7107. var out_count_two int64
  7108. wareOutList, _ := GetNewStockFlowIsBatchNumberSixty(patient_id, record_time, goods.GoodId, goods.ProjectId, tx)
  7109. for _, item := range wareOutList {
  7110. out_count_two += item.Count
  7111. }
  7112. OutInfo := &models.WarehouseOutInfo{
  7113. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7114. WarehouseOutId: warehouseOut.ID,
  7115. WarehouseInfotId: warehouse.ID,
  7116. Status: 1,
  7117. Ctime: time.Now().Unix(),
  7118. Remark: warehouse.Remark,
  7119. OrgId: orgID,
  7120. Type: 1,
  7121. Manufacturer: warehouse.Manufacturer,
  7122. Dealer: warehouse.Dealer,
  7123. IsSys: 1,
  7124. SysRecordTime: record_time,
  7125. GoodTypeId: goods.GoodTypeId,
  7126. GoodId: goods.GoodId,
  7127. PatientId: patient_id,
  7128. Number: warehouse.Number,
  7129. LicenseNumber: warehouse.LicenseNumber,
  7130. Price: warehouse.PackingPrice,
  7131. ExpiryDate: warehouse.ExpiryDate,
  7132. ProductDate: warehouse.ProductDate,
  7133. ProjectId: goods.ProjectId,
  7134. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7135. StorehouseId: goods.StorehouseId,
  7136. IsCheck: 1,
  7137. OverCount: sum_count,
  7138. RegisterNumber: warehouse.RegisterNumber,
  7139. }
  7140. OutInfo.Count = count - out_count_two + out_count_one
  7141. _, errcodes := GetNewNewWarehouseOutInfoIsExistSix(goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID, tx)
  7142. if errcodes == gorm.ErrRecordNotFound {
  7143. errOne := AddNewSigleWarehouseOutInfo(OutInfo, tx)
  7144. if errOne != nil {
  7145. return errOne
  7146. }
  7147. } else if errcodes == nil {
  7148. outInfoOne, _ := GetNewWarehouseOutInfoIsExistThree(goods.GoodId, patient_id, record_time, goods.ProjectId, tx)
  7149. if count != outInfoOne.Count {
  7150. UpdatedNewWarehouseOutInfo(OutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId, tx)
  7151. }
  7152. }
  7153. lastOut, _ := FindNewWarehouseOutInfoByPatientIdTwo(patient_id, record_time, goods.GoodId, orgID, goods.ProjectId, tx)
  7154. //如果本次出库数据大于历史出库数据 新增1条流水
  7155. fmt.Println("本次出库数据", count)
  7156. fmt.Println("历史出库数据", out_count)
  7157. fmt.Println("退库数据", out_count_one)
  7158. if count > (out_count - out_count_one) {
  7159. stockFlow := models.VmStockFlow{
  7160. WarehousingId: warehouse.ID,
  7161. GoodId: goods.GoodId,
  7162. Number: warehouse.Number,
  7163. LicenseNumber: warehouse.LicenseNumber,
  7164. Count: count - out_count + out_count_one,
  7165. UserOrgId: orgID,
  7166. PatientId: goods.PatientId,
  7167. SystemTime: record_time,
  7168. ConsumableType: 3,
  7169. IsSys: 1,
  7170. WarehousingOrder: warehouse.WarehousingOrder,
  7171. WarehouseOutId: warehouseOut.ID,
  7172. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7173. IsEdit: 1,
  7174. CancelStockId: 0,
  7175. CancelOrderNumber: "",
  7176. Manufacturer: warehouse.Manufacturer,
  7177. Dealer: warehouse.Dealer,
  7178. Creator: creator,
  7179. UpdateCreator: 0,
  7180. Status: 1,
  7181. Ctime: time.Now().Unix(),
  7182. Mtime: 0,
  7183. Price: warehouse.PackingPrice, //零售价
  7184. WarehousingDetailId: warehouse.ID,
  7185. WarehouseOutDetailId: goods.ID,
  7186. CancelOutDetailId: 0,
  7187. ProductDate: warehouse.ProductDate,
  7188. ExpireDate: warehouse.ExpiryDate,
  7189. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7190. StorehouseId: warehouse.StorehouseId,
  7191. AdminUserId: creator,
  7192. BuyPrice: warehouse.Price, //进货价
  7193. StockCount: "",
  7194. BatchNumberCount: warehouse.StockCount - goods.Count,
  7195. IsCheck: 1,
  7196. OverCount: sum_count,
  7197. RegisterNumber: warehouse.RegisterNumber,
  7198. ProjectId: goods.ProjectId,
  7199. OperateTime: time.Now().Unix(),
  7200. }
  7201. //创建出库流水
  7202. errflow := CreateNewStockFlowOne(stockFlow, tx)
  7203. if errflow != nil {
  7204. goodErrcode := models.XtGoodErrcode{
  7205. UserOrgId: orgID,
  7206. Errcode: "创建流水报错",
  7207. GoodId: goods.GoodId,
  7208. Status: 1,
  7209. Ctime: time.Now().Unix(),
  7210. Mtime: 0,
  7211. Count: 0,
  7212. StockCount: 0,
  7213. Creater: creator,
  7214. BatchNumberId: warehouse.ID,
  7215. WarehouseOutId: 0,
  7216. }
  7217. CreateGoodErrcode(goodErrcode)
  7218. }
  7219. //更新出库数量
  7220. errsumcode := ModifyNewGoodSumCount(goods.StorehouseId, stockFlow.Count, orgID, goods.GoodId, tx)
  7221. if errsumcode != nil {
  7222. goodErrcode := models.XtGoodErrcode{
  7223. UserOrgId: orgID,
  7224. Errcode: "更新出库数量报错",
  7225. GoodId: goods.GoodId,
  7226. Status: 1,
  7227. Ctime: time.Now().Unix(),
  7228. Mtime: 0,
  7229. Count: 0,
  7230. StockCount: 0,
  7231. Creater: creator,
  7232. BatchNumberId: warehouse.ID,
  7233. WarehouseOutId: 0,
  7234. }
  7235. CreateGoodErrcode(goodErrcode)
  7236. }
  7237. }
  7238. //如果本次出库数据小于历史出库数据 新增1条退库流水
  7239. if count < (out_count - out_count_one) {
  7240. operation_time := time.Now().Unix()
  7241. //创建退库单
  7242. timeStr := time.Now().Format("2006-01-02")
  7243. timeArr := strings.Split(timeStr, "-")
  7244. total, _ := FindNewAllCancelStockTotal(orgID, tx)
  7245. total = total + 1
  7246. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  7247. cancelStock := models.CancelStock{
  7248. OrderNumber: orderNumber,
  7249. OperaTime: operation_time,
  7250. OrgId: orgID,
  7251. Creater: warehouseOut.Creater,
  7252. Ctime: time.Now().Unix(),
  7253. Status: 1,
  7254. ReturnTime: record_time,
  7255. Type: 1,
  7256. StorehouseId: goods.StorehouseId,
  7257. IsCheck: 1,
  7258. }
  7259. _, msgerrkonde := GetNewCancelStockDetailByOrderNumberOne(record_time, orgID, tx)
  7260. if msgerrkonde == gorm.ErrRecordNotFound {
  7261. AddNewSigleCancelStock(&cancelStock, tx)
  7262. }
  7263. cancel, _ := GetNewLastCancelStockById(orgID, tx)
  7264. manufacturer, _ := GetNewManufactureById(warehouse.Manufacturer, tx)
  7265. deaerler, _ := GetNewDealerById(warehouse.Dealer, tx)
  7266. cancelStockInfo := models.CancelStockInfo{
  7267. GoodId: goods.GoodId,
  7268. CancelStockId: cancel.ID,
  7269. GoodTypeId: goods.GoodTypeId,
  7270. Count: out_count - out_count_one - count,
  7271. Price: warehouse.PackingPrice,
  7272. Total: 0,
  7273. ProductDate: warehouse.ProductDate,
  7274. ExpiryDate: warehouse.ExpiryDate,
  7275. Ctime: time.Now().Unix(),
  7276. Status: 1,
  7277. OrgId: orgID,
  7278. OrderNumber: cancel.OrderNumber,
  7279. Type: 0,
  7280. Dealer: deaerler.DealerName,
  7281. Manufacturer: manufacturer.ManufacturerName,
  7282. Number: warehouse.Number,
  7283. RegisterAccount: "",
  7284. Remark: "",
  7285. WarehouseInfoId: warehouse.ID,
  7286. PatientId: patient_id,
  7287. RecordDate: record_time,
  7288. StorehouseId: goods.StorehouseId,
  7289. IsCheck: 1,
  7290. }
  7291. if orgID == 9671 || orgID == 10265 {
  7292. goodsInfo, _ := FindeNewGoodInfo(orgID, goods.ProjectId, tx)
  7293. cancelStockInfo.Price = goodsInfo.PackingPrice
  7294. }
  7295. CreateNewCancelStockInfoOne(&cancelStockInfo, tx)
  7296. cancelInfo, _ := GetLastNewCancelStockInfoByGoodId(goods.GoodId, tx)
  7297. flow := models.VmStockFlow{
  7298. WarehousingId: warehouse.ID,
  7299. GoodId: goods.GoodId,
  7300. Number: warehouse.Number,
  7301. LicenseNumber: warehouse.LicenseNumber,
  7302. Count: out_count - out_count_one - count,
  7303. UserOrgId: orgID,
  7304. PatientId: patient_id,
  7305. SystemTime: record_time,
  7306. ConsumableType: 7,
  7307. IsSys: 1,
  7308. WarehousingOrder: "",
  7309. WarehouseOutId: lastOut.WarehouseOutId,
  7310. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  7311. IsEdit: 0,
  7312. CancelStockId: cancel.ID,
  7313. CancelOrderNumber: cancel.OrderNumber,
  7314. Manufacturer: manufacturer.ID,
  7315. Dealer: 0,
  7316. Creator: warehouseOut.Creater,
  7317. UpdateCreator: 0,
  7318. Status: 1,
  7319. Ctime: time.Now().Unix(),
  7320. Mtime: 0,
  7321. Price: warehouse.PackingPrice,
  7322. WarehousingDetailId: warehouse.ID,
  7323. WarehouseOutDetailId: lastOut.ID,
  7324. CancelOutDetailId: cancelInfo.ID,
  7325. ProductDate: warehouse.ProductDate,
  7326. ExpireDate: warehouse.ExpiryDate,
  7327. StorehouseId: goods.StorehouseId,
  7328. BuyPrice: warehouse.Price,
  7329. ProjectId: goods.ProjectId,
  7330. OverCount: sum_count,
  7331. RegisterNumber: warehouse.RegisterNumber,
  7332. OperateTime: time.Now().Unix(),
  7333. }
  7334. if orgID == 9671 || orgID == 10265 {
  7335. goodsInfo, _ := FindeNewGoodInfo(orgID, goods.ProjectId, tx)
  7336. flow.Price = goodsInfo.PackingPrice
  7337. flow.BuyPrice = goodsInfo.BuyPrice
  7338. }
  7339. CreateNewStockFlowOne(flow, tx)
  7340. //退库数量增加
  7341. UpdateNewSumAddCancelCount(orgID, goods.GoodId, goods.StorehouseId, flow.Count, tx)
  7342. ModifyNewAddGoodSumCount(goods.StorehouseId, flow.Count, orgID, goods.GoodId, tx)
  7343. }
  7344. //添加入库ID
  7345. errWarehouse := UpdateNewGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count, tx)
  7346. if errWarehouse != nil {
  7347. goodErrcode := models.XtGoodErrcode{
  7348. UserOrgId: orgID,
  7349. Errcode: "添加入库ID报错",
  7350. GoodId: goods.GoodId,
  7351. Status: 1,
  7352. Ctime: time.Now().Unix(),
  7353. Mtime: 0,
  7354. Count: 0,
  7355. StockCount: 0,
  7356. Creater: creator,
  7357. BatchNumberId: warehouse.ID,
  7358. WarehouseOutId: 0,
  7359. }
  7360. CreateGoodErrcode(goodErrcode)
  7361. }
  7362. if warehouse.StockCount < maxNumber {
  7363. goodErrcode := models.XtGoodErrcode{
  7364. UserOrgId: orgID,
  7365. Errcode: "出库接口库存数量不足",
  7366. GoodId: goods.GoodId,
  7367. Status: 1,
  7368. Ctime: time.Now().Unix(),
  7369. Mtime: 0,
  7370. Count: 0,
  7371. StockCount: 0,
  7372. Creater: creator,
  7373. BatchNumberId: warehouse.ID,
  7374. WarehouseOutId: 0,
  7375. }
  7376. CreateGoodErrcode(goodErrcode)
  7377. return errors.New("库存数量不足")
  7378. }
  7379. return nil
  7380. } else {
  7381. // 出库完成后,要将该批次库存清零
  7382. warehouse.StockCount = 0
  7383. warehouse.Mtime = time.Now().Unix()
  7384. errThree := UpDateNewWarehouseInfoByStock(&warehouse, tx)
  7385. if errThree != nil {
  7386. goodErrcode := models.XtGoodErrcode{
  7387. UserOrgId: orgID,
  7388. Errcode: "扣减库存报错",
  7389. GoodId: goods.GoodId,
  7390. Status: 1,
  7391. Ctime: time.Now().Unix(),
  7392. Mtime: 0,
  7393. Count: 0,
  7394. StockCount: 0,
  7395. Creater: creator,
  7396. BatchNumberId: warehouse.ID,
  7397. WarehouseOutId: 0,
  7398. }
  7399. CreateGoodErrcode(goodErrcode)
  7400. return errThree
  7401. }
  7402. //查询剩余库存
  7403. goodList, _ := GetNewAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId, tx)
  7404. var sum_count int64
  7405. for _, item := range goodList {
  7406. sum_count += item.StockCount
  7407. }
  7408. fmt.Println("剩余库存", sum_count)
  7409. OutInfo := &models.WarehouseOutInfo{
  7410. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7411. WarehouseOutId: warehouseOut.ID,
  7412. WarehouseInfotId: warehouse.ID,
  7413. Status: 1,
  7414. Ctime: time.Now().Unix(),
  7415. Remark: warehouse.Remark,
  7416. OrgId: orgID,
  7417. Type: 1,
  7418. Manufacturer: warehouse.Manufacturer,
  7419. Dealer: warehouse.Dealer,
  7420. IsSys: 1,
  7421. SysRecordTime: record_time,
  7422. GoodTypeId: goods.GoodTypeId,
  7423. GoodId: goods.GoodId,
  7424. PatientId: patient_id,
  7425. Number: warehouse.Number,
  7426. LicenseNumber: warehouse.LicenseNumber,
  7427. Price: warehouse.PackingPrice,
  7428. ExpiryDate: warehouse.ExpiryDate,
  7429. ProductDate: warehouse.ProductDate,
  7430. ProjectId: goods.ProjectId,
  7431. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7432. StorehouseId: goods.StorehouseId,
  7433. IsCheck: 1,
  7434. OverCount: sum_count,
  7435. RegisterNumber: warehouse.RegisterNumber,
  7436. }
  7437. OutInfo.Count = stock_number
  7438. _, errcodes := GetNewNewWarehouseOutInfoIsExistSix(goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID, tx)
  7439. if errcodes == gorm.ErrRecordNotFound {
  7440. errOne := AddNewSigleWarehouseOutInfo(OutInfo, tx)
  7441. if errOne != nil {
  7442. return errOne
  7443. }
  7444. } else if errcodes == nil {
  7445. outInfoOne, _ := GetNewWarehouseOutInfoIsExistTen(goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID, tx)
  7446. if count != outInfoOne.Count {
  7447. UpdatedNewWarehouseOutInfoSix(OutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID, tx)
  7448. }
  7449. }
  7450. lastOut, _ := FindNewWarehouseOutInfoByPatientIdTwo(patient_id, record_time, goods.GoodId, orgID, goods.ProjectId, tx)
  7451. //查询该该批次已经出库的数据
  7452. flowGood, _ := GetNewStockFlowIsBatchNumberSix(patient_id, record_time, goods.GoodId, goods.ProjectId, tx)
  7453. var out_count int64
  7454. var out_count_one int64
  7455. for _, item := range flowGood {
  7456. out_count += item.Count
  7457. }
  7458. flowGoodTwo, _ := GetNewStockFlowIsBatchNumberFour(patient_id, record_time, goods.GoodId, goods.ProjectId, tx)
  7459. for _, item := range flowGoodTwo {
  7460. out_count_one += item.Count
  7461. }
  7462. //如果出库数量 大于 历史出库数据 新增1条流水
  7463. if count > out_count-out_count_one {
  7464. stockFlow := models.VmStockFlow{
  7465. WarehousingId: warehouse.ID,
  7466. GoodId: goods.GoodId,
  7467. Number: warehouse.Number,
  7468. LicenseNumber: warehouse.LicenseNumber,
  7469. Count: stock_number,
  7470. UserOrgId: orgID,
  7471. PatientId: goods.PatientId,
  7472. SystemTime: record_time,
  7473. ConsumableType: 3,
  7474. IsSys: 1,
  7475. WarehousingOrder: warehouse.WarehousingOrder,
  7476. WarehouseOutId: warehouseOut.ID,
  7477. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7478. IsEdit: 1,
  7479. CancelStockId: 0,
  7480. CancelOrderNumber: "",
  7481. Manufacturer: warehouse.Manufacturer,
  7482. Dealer: warehouse.Dealer,
  7483. Creator: creator,
  7484. UpdateCreator: 0,
  7485. Status: 1,
  7486. Ctime: time.Now().Unix(),
  7487. Mtime: 0,
  7488. Price: warehouse.PackingPrice, //零售价
  7489. WarehousingDetailId: warehouse.ID,
  7490. WarehouseOutDetailId: goods.ID,
  7491. CancelOutDetailId: 0,
  7492. ProductDate: warehouse.ProductDate,
  7493. ExpireDate: warehouse.ExpiryDate,
  7494. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7495. StorehouseId: warehouse.StorehouseId,
  7496. AdminUserId: creator,
  7497. BuyPrice: warehouse.Price, //进货价
  7498. StockCount: "",
  7499. BatchNumberCount: 0,
  7500. IsCheck: 1,
  7501. OverCount: sum_count,
  7502. RegisterNumber: warehouse.RegisterNumber,
  7503. ProjectId: goods.ProjectId,
  7504. OperateTime: time.Now().Unix(),
  7505. }
  7506. //创建出库流水
  7507. errflow := CreateNewStockFlowOne(stockFlow, tx)
  7508. if errflow != nil {
  7509. goodErrcode := models.XtGoodErrcode{
  7510. UserOrgId: orgID,
  7511. Errcode: "创建出库流水报错",
  7512. GoodId: goods.GoodId,
  7513. Status: 1,
  7514. Ctime: time.Now().Unix(),
  7515. Mtime: 0,
  7516. Count: 0,
  7517. StockCount: 0,
  7518. Creater: creator,
  7519. BatchNumberId: warehouse.ID,
  7520. WarehouseOutId: 0,
  7521. }
  7522. CreateGoodErrcode(goodErrcode)
  7523. }
  7524. //更新出库数量
  7525. errcodecout := ModifyNewGoodSumCount(goods.StorehouseId, stock_number, orgID, goods.GoodId, tx)
  7526. if errcodecout != nil {
  7527. goodErrcode := models.XtGoodErrcode{
  7528. UserOrgId: orgID,
  7529. Errcode: "更新出库数量报错",
  7530. GoodId: goods.GoodId,
  7531. Status: 1,
  7532. Ctime: time.Now().Unix(),
  7533. Mtime: 0,
  7534. Count: 0,
  7535. StockCount: 0,
  7536. Creater: creator,
  7537. BatchNumberId: warehouse.ID,
  7538. WarehouseOutId: 0,
  7539. }
  7540. CreateGoodErrcode(goodErrcode)
  7541. }
  7542. }
  7543. //退库
  7544. if count < out_count-out_count_one {
  7545. operation_time := time.Now().Unix()
  7546. //创建退库单
  7547. timeStr := time.Now().Format("2006-01-02")
  7548. timeArr := strings.Split(timeStr, "-")
  7549. total, _ := FindNewAllCancelStockTotal(orgID, tx)
  7550. total = total + 1
  7551. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  7552. cancelStock := models.CancelStock{
  7553. OrderNumber: orderNumber,
  7554. OperaTime: operation_time,
  7555. OrgId: orgID,
  7556. Creater: warehouseOut.Creater,
  7557. Ctime: time.Now().Unix(),
  7558. Status: 1,
  7559. ReturnTime: record_time,
  7560. Type: 1,
  7561. StorehouseId: goods.StorehouseId,
  7562. IsCheck: 1,
  7563. }
  7564. _, msgerrkonde := GetNewCancelStockDetailByOrderNumberOne(record_time, orgID, tx)
  7565. if msgerrkonde == gorm.ErrRecordNotFound {
  7566. AddNewSigleCancelStock(&cancelStock, tx)
  7567. }
  7568. cancel, _ := GetNewLastCancelStockById(orgID, tx)
  7569. manufacturer, _ := GetNewManufactureById(warehouse.Manufacturer, tx)
  7570. deaerler, _ := GetNewDealerById(warehouse.Dealer, tx)
  7571. cancelStockInfo := models.CancelStockInfo{
  7572. GoodId: goods.GoodId,
  7573. CancelStockId: cancel.ID,
  7574. GoodTypeId: goods.GoodTypeId,
  7575. Count: out_count - out_count_one - count,
  7576. Price: warehouse.PackingPrice,
  7577. Total: 0,
  7578. ProductDate: warehouse.ProductDate,
  7579. ExpiryDate: warehouse.ExpiryDate,
  7580. Ctime: time.Now().Unix(),
  7581. Status: 1,
  7582. OrgId: orgID,
  7583. OrderNumber: cancel.OrderNumber,
  7584. Type: 0,
  7585. Dealer: deaerler.DealerName,
  7586. Manufacturer: manufacturer.ManufacturerName,
  7587. Number: warehouse.Number,
  7588. RegisterAccount: "",
  7589. Remark: "",
  7590. WarehouseInfoId: warehouse.ID,
  7591. PatientId: patient_id,
  7592. RecordDate: record_time,
  7593. StorehouseId: goods.StorehouseId,
  7594. IsCheck: 1,
  7595. }
  7596. if orgID == 9671 || orgID == 10265 {
  7597. goodsInfo, _ := FindeNewGoodInfo(orgID, goods.ProjectId, tx)
  7598. cancelStockInfo.Price = goodsInfo.PackingPrice
  7599. }
  7600. CreateNewCancelStockInfoOne(&cancelStockInfo, tx)
  7601. cancelInfo, _ := GetLastNewCancelStockInfoByGoodId(goods.GoodId, tx)
  7602. flow := models.VmStockFlow{
  7603. WarehousingId: warehouse.ID,
  7604. GoodId: goods.GoodId,
  7605. Number: warehouse.Number,
  7606. LicenseNumber: warehouse.LicenseNumber,
  7607. Count: out_count - count,
  7608. UserOrgId: orgID,
  7609. PatientId: patient_id,
  7610. SystemTime: record_time,
  7611. ConsumableType: 7,
  7612. IsSys: 1,
  7613. WarehousingOrder: "",
  7614. WarehouseOutId: lastOut.WarehouseOutId,
  7615. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  7616. IsEdit: 0,
  7617. CancelStockId: cancel.ID,
  7618. CancelOrderNumber: cancel.OrderNumber,
  7619. Manufacturer: manufacturer.ID,
  7620. Dealer: 0,
  7621. Creator: warehouseOut.Creater,
  7622. UpdateCreator: 0,
  7623. Status: 1,
  7624. Ctime: time.Now().Unix(),
  7625. Mtime: 0,
  7626. Price: warehouse.Price,
  7627. WarehousingDetailId: warehouse.ID,
  7628. WarehouseOutDetailId: lastOut.ID,
  7629. CancelOutDetailId: cancelInfo.ID,
  7630. ProductDate: warehouse.ProductDate,
  7631. ExpireDate: warehouse.ExpiryDate,
  7632. StorehouseId: goods.StorehouseId,
  7633. BuyPrice: warehouse.Price,
  7634. ProjectId: goods.ProjectId,
  7635. OverCount: sum_count,
  7636. RegisterNumber: warehouse.RegisterNumber,
  7637. OperateTime: time.Now().Unix(),
  7638. }
  7639. if orgID == 9671 || orgID == 10265 {
  7640. goodsInfo, _ := FindeNewGoodInfo(orgID, goods.ProjectId, tx)
  7641. flow.Price = goodsInfo.PackingPrice
  7642. flow.BuyPrice = goodsInfo.BuyPrice
  7643. }
  7644. CreateNewStockFlowOne(flow, tx)
  7645. //退库数量增加
  7646. UpdateNewSumAddCancelCount(orgID, goods.GoodId, goods.StorehouseId, flow.Count, tx)
  7647. //实际出库减少
  7648. ModifyNewAddGoodSumCount(goods.StorehouseId, flow.Count, orgID, goods.GoodId, tx)
  7649. }
  7650. errWarehouse := UpdateNewGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count, tx)
  7651. if errWarehouse != nil {
  7652. goodErrcode := models.XtGoodErrcode{
  7653. UserOrgId: orgID,
  7654. Errcode: "添加入库ID报错",
  7655. GoodId: goods.GoodId,
  7656. Status: 1,
  7657. Ctime: time.Now().Unix(),
  7658. Mtime: 0,
  7659. Count: 0,
  7660. StockCount: 0,
  7661. Creater: creator,
  7662. BatchNumberId: warehouse.ID,
  7663. WarehouseOutId: 0,
  7664. }
  7665. CreateGoodErrcode(goodErrcode)
  7666. }
  7667. }
  7668. goods.Count = deliver_number - stock_number
  7669. prepare := &models.DialysisBeforePrepare{
  7670. Count: deliver_number - stock_number,
  7671. GoodTypeId: goods.GoodTypeId,
  7672. GoodId: goods.GoodId,
  7673. PatientId: goods.PatientId,
  7674. RecordDate: goods.RecordDate,
  7675. UserOrgId: orgID,
  7676. Ctime: time.Now().Unix(),
  7677. Creater: creator,
  7678. Status: 1,
  7679. StorehouseId: goods.StorehouseId,
  7680. ProjectId: goods.ProjectId,
  7681. }
  7682. ConsumableNewPrescriptionDelivery(orgID, patient_id, record_time, prepare, warehouseOut, creator, count, tx)
  7683. return nil
  7684. }
  7685. // 保存处方出库
  7686. func ConsumableNewHisPrescriptionDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, creator int64, tx *gorm.DB) (err error) {
  7687. var deliver_number int64 = 0
  7688. var stock_number int64 = 0
  7689. var maxNumber int64 = 0
  7690. deliver_number = goods.Count
  7691. // 根据先进先出原则,查询最先入库的批次,进行出库
  7692. // 如果没有对应的库存,则报错
  7693. warehouse, _ := FindNewFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId, tx)
  7694. stock_number = warehouse.StockCount
  7695. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  7696. if stock_number >= deliver_number {
  7697. maxNumber = goods.Count
  7698. warehouse.StockCount = warehouse.StockCount - maxNumber
  7699. warehouse.Mtime = time.Now().Unix()
  7700. //扣减库存
  7701. UpDateNewWarehouseInfoByStock(&warehouse, tx)
  7702. //查询剩余库存
  7703. goodList, _ := GetNewAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId, tx)
  7704. var sum_count int64
  7705. for _, item := range goodList {
  7706. sum_count += item.StockCount
  7707. }
  7708. //创建出库单
  7709. OutInfo := &models.WarehouseOutInfo{
  7710. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7711. WarehouseOutId: warehouseOut.ID,
  7712. WarehouseInfotId: warehouse.ID,
  7713. Status: 1,
  7714. Ctime: time.Now().Unix(),
  7715. Remark: warehouse.Remark,
  7716. OrgId: orgID,
  7717. Type: 1,
  7718. Manufacturer: warehouse.Manufacturer,
  7719. Dealer: warehouse.Dealer,
  7720. IsSys: 1,
  7721. SysRecordTime: record_time,
  7722. GoodTypeId: goods.GoodTypeId,
  7723. GoodId: goods.GoodId,
  7724. PatientId: patient_id,
  7725. Number: warehouse.Number,
  7726. LicenseNumber: warehouse.LicenseNumber,
  7727. Price: warehouse.PackingPrice,
  7728. ExpiryDate: warehouse.ExpiryDate,
  7729. ProductDate: warehouse.ProductDate,
  7730. ProjectId: goods.ProjectId,
  7731. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7732. StorehouseId: goods.StorehouseId,
  7733. IsCheck: 1,
  7734. OverCount: sum_count,
  7735. RegisterNumber: warehouse.RegisterNumber,
  7736. Count: goods.Count,
  7737. }
  7738. AddNewSigleWarehouseOutInfo(OutInfo, tx)
  7739. stockFlow := models.VmStockFlow{
  7740. WarehousingId: warehouse.ID,
  7741. GoodId: goods.GoodId,
  7742. Number: warehouse.Number,
  7743. LicenseNumber: warehouse.LicenseNumber,
  7744. Count: goods.Count,
  7745. UserOrgId: orgID,
  7746. PatientId: goods.PatientId,
  7747. SystemTime: record_time,
  7748. ConsumableType: 3,
  7749. IsSys: 1,
  7750. WarehousingOrder: warehouse.WarehousingOrder,
  7751. WarehouseOutId: warehouseOut.ID,
  7752. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7753. IsEdit: 1,
  7754. CancelStockId: 0,
  7755. CancelOrderNumber: "",
  7756. Manufacturer: warehouse.Manufacturer,
  7757. Dealer: warehouse.Dealer,
  7758. Creator: creator,
  7759. UpdateCreator: 0,
  7760. Status: 1,
  7761. Ctime: time.Now().Unix(),
  7762. Mtime: 0,
  7763. Price: warehouse.PackingPrice, //零售价
  7764. WarehousingDetailId: warehouse.ID,
  7765. WarehouseOutDetailId: goods.ID,
  7766. CancelOutDetailId: 0,
  7767. ProductDate: warehouse.ProductDate,
  7768. ExpireDate: warehouse.ExpiryDate,
  7769. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7770. StorehouseId: warehouse.StorehouseId,
  7771. AdminUserId: creator,
  7772. BuyPrice: warehouse.Price, //进货价
  7773. StockCount: "",
  7774. BatchNumberCount: warehouse.StockCount - goods.Count,
  7775. IsCheck: 1,
  7776. OverCount: sum_count,
  7777. RegisterNumber: warehouse.RegisterNumber,
  7778. ProjectId: goods.ProjectId,
  7779. OperateTime: time.Now().Unix(),
  7780. }
  7781. //插入流水
  7782. CreateNewStockFlowOne(stockFlow, tx)
  7783. //更新出库数量
  7784. ModifyNewGoodSumCount(goods.StorehouseId, stockFlow.Count, orgID, goods.GoodId, tx)
  7785. goodObj, _ := GetNewGoodInformationByGoodIdThirty(goods.GoodId, tx)
  7786. autoMatic := &models.AutomaticReduceDetail{
  7787. Count: goods.Count,
  7788. GoodTypeId: goodObj.GoodTypeId,
  7789. GoodId: goods.GoodId,
  7790. PatientId: goods.PatientId,
  7791. RecordTime: goods.RecordDate,
  7792. OrgId: orgID,
  7793. Ctime: time.Now().Unix(),
  7794. Status: 1,
  7795. StorehouseId: goods.StorehouseId,
  7796. ProjectId: goods.ProjectId,
  7797. }
  7798. CreatedDialysisPrepare(autoMatic, tx)
  7799. return nil
  7800. } else {
  7801. // 出库完成后,要将该批次库存清零
  7802. warehouse.StockCount = 0
  7803. warehouse.Mtime = time.Now().Unix()
  7804. UpDateNewWarehouseInfoByStock(&warehouse, tx)
  7805. //查询剩余库存
  7806. goodList, _ := GetNewAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId, tx)
  7807. var sum_count int64
  7808. for _, item := range goodList {
  7809. sum_count += item.StockCount
  7810. }
  7811. OutInfo := &models.WarehouseOutInfo{
  7812. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7813. WarehouseOutId: warehouseOut.ID,
  7814. WarehouseInfotId: warehouse.ID,
  7815. Status: 1,
  7816. Ctime: time.Now().Unix(),
  7817. Remark: warehouse.Remark,
  7818. OrgId: orgID,
  7819. Type: 1,
  7820. Manufacturer: warehouse.Manufacturer,
  7821. Dealer: warehouse.Dealer,
  7822. IsSys: 1,
  7823. SysRecordTime: record_time,
  7824. GoodTypeId: goods.GoodTypeId,
  7825. GoodId: goods.GoodId,
  7826. PatientId: patient_id,
  7827. Number: warehouse.Number,
  7828. LicenseNumber: warehouse.LicenseNumber,
  7829. Price: warehouse.PackingPrice,
  7830. ExpiryDate: warehouse.ExpiryDate,
  7831. ProductDate: warehouse.ProductDate,
  7832. ProjectId: goods.ProjectId,
  7833. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7834. StorehouseId: goods.StorehouseId,
  7835. IsCheck: 1,
  7836. OverCount: sum_count,
  7837. RegisterNumber: warehouse.RegisterNumber,
  7838. }
  7839. OutInfo.Count = stock_number
  7840. AddNewSigleWarehouseOutInfo(OutInfo, tx)
  7841. //如果出库数量 大于 历史出库数据 新增1条流水
  7842. stockFlow := models.VmStockFlow{
  7843. WarehousingId: warehouse.ID,
  7844. GoodId: goods.GoodId,
  7845. Number: warehouse.Number,
  7846. LicenseNumber: warehouse.LicenseNumber,
  7847. Count: stock_number,
  7848. UserOrgId: orgID,
  7849. PatientId: goods.PatientId,
  7850. SystemTime: record_time,
  7851. ConsumableType: 3,
  7852. IsSys: 1,
  7853. WarehousingOrder: warehouse.WarehousingOrder,
  7854. WarehouseOutId: warehouseOut.ID,
  7855. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7856. IsEdit: 1,
  7857. CancelStockId: 0,
  7858. CancelOrderNumber: "",
  7859. Manufacturer: warehouse.Manufacturer,
  7860. Dealer: warehouse.Dealer,
  7861. Creator: creator,
  7862. UpdateCreator: 0,
  7863. Status: 1,
  7864. Ctime: time.Now().Unix(),
  7865. Mtime: 0,
  7866. Price: warehouse.PackingPrice, //零售价
  7867. WarehousingDetailId: warehouse.ID,
  7868. WarehouseOutDetailId: goods.ID,
  7869. CancelOutDetailId: 0,
  7870. ProductDate: warehouse.ProductDate,
  7871. ExpireDate: warehouse.ExpiryDate,
  7872. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7873. StorehouseId: warehouse.StorehouseId,
  7874. AdminUserId: creator,
  7875. BuyPrice: warehouse.Price, //进货价
  7876. StockCount: "",
  7877. BatchNumberCount: 0,
  7878. IsCheck: 1,
  7879. OverCount: sum_count,
  7880. RegisterNumber: warehouse.RegisterNumber,
  7881. ProjectId: goods.ProjectId,
  7882. OperateTime: time.Now().Unix(),
  7883. }
  7884. //创建出库流水
  7885. CreateNewStockFlowOne(stockFlow, tx)
  7886. ModifyNewGoodSumCount(goods.StorehouseId, stock_number, orgID, goods.GoodId, tx)
  7887. UpdateNewGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count, tx)
  7888. }
  7889. dialyPrepareOne := &models.DialysisBeforePrepare{
  7890. GoodTypeId: goods.GoodTypeId,
  7891. GoodId: goods.GoodId,
  7892. PatientId: goods.PatientId,
  7893. RecordDate: goods.RecordDate,
  7894. UserOrgId: orgID,
  7895. Count: goods.Count,
  7896. Ctime: time.Now().Unix(),
  7897. Creater: creator,
  7898. Status: 1,
  7899. StorehouseId: goods.StorehouseId,
  7900. ProjectId: goods.ID,
  7901. }
  7902. autoMatic := &models.AutomaticReduceDetail{
  7903. Count: stock_number,
  7904. GoodTypeId: goods.GoodTypeId,
  7905. GoodId: goods.GoodId,
  7906. PatientId: goods.PatientId,
  7907. RecordTime: goods.RecordDate,
  7908. OrgId: orgID,
  7909. Ctime: time.Now().Unix(),
  7910. Status: 1,
  7911. StorehouseId: goods.StorehouseId,
  7912. ProjectId: goods.ProjectId,
  7913. }
  7914. CreatedDialysisPrepare(autoMatic, tx)
  7915. goods.Count = deliver_number - stock_number
  7916. ConsumableNewHisPrescriptionDelivery(orgID, patient_id, record_time, dialyPrepareOne, warehouseOut, creator, tx)
  7917. return nil
  7918. }
  7919. func CreatedDialysisPrepare(prepare *models.AutomaticReduceDetail, tx *gorm.DB) error {
  7920. err := tx.Create(&prepare).Error
  7921. if err != nil {
  7922. tx.Rollback()
  7923. return err
  7924. }
  7925. return err
  7926. }
  7927. func NewHisGoodCancelInfo(orgID int64, creater int64, project *models.HisPrescriptionProject, cha_count int64, good models.GoodInformationThirty, tx *gorm.DB) (err error) {
  7928. storeConfig, _ := GetNewAllStoreHouseConfig(orgID, tx)
  7929. //查找患者最后一条的退库数据是否满足要求
  7930. var out_count int64
  7931. GoodOutInfo, _ := GetGoodWarehouseOutInfoByProjectIdTwo(project.PatientId, project.UserOrgId, project.RecordDate, project.ProjectId, project.ID, tx)
  7932. out_count = GoodOutInfo.Count
  7933. //如果最后1条出库数量大于退库数量,则直接退库,满足退库要求
  7934. if out_count >= cha_count {
  7935. //退库 退回 cha_count 的数量
  7936. ModifyNewGoodWarehouseInfoStockMinNumber(cha_count, GoodOutInfo.GoodId, GoodOutInfo.OrgId, GoodOutInfo.WarehouseInfotId, tx)
  7937. if out_count == cha_count {
  7938. //删除该出库单
  7939. DeleteGoodWarehouseOutInfoByNumber(GoodOutInfo.ID, tx)
  7940. }
  7941. //减去出库单里面的数量
  7942. ModifyGoodWarehouseOutInfoByNumber(GoodOutInfo.ID, cha_count, tx)
  7943. //创建退库单
  7944. goodList, _ := GetNewAllGoodSumCount(project.ProjectId, orgID, storeConfig.StorehouseOutInfo, tx)
  7945. var sum_count int64
  7946. for _, item := range goodList {
  7947. sum_count += item.StockCount
  7948. }
  7949. operation_time := time.Now().Unix()
  7950. //创建退库单
  7951. timeStr := time.Now().Format("2006-01-02")
  7952. timeArr := strings.Split(timeStr, "-")
  7953. total, _ := FindNewAllCancelStockTotal(orgID, tx)
  7954. total = total + 1
  7955. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  7956. cancelStock := models.CancelStock{
  7957. OrderNumber: orderNumber,
  7958. OperaTime: operation_time,
  7959. OrgId: orgID,
  7960. Creater: creater,
  7961. Ctime: time.Now().Unix(),
  7962. Status: 1,
  7963. ReturnTime: project.RecordDate,
  7964. Type: 1,
  7965. StorehouseId: storeConfig.StorehouseOutInfo,
  7966. IsCheck: 1,
  7967. }
  7968. _, msgerrkonde := GetNewCancelStockDetailByOrderNumberOne(project.RecordDate, orgID, tx)
  7969. if msgerrkonde == gorm.ErrRecordNotFound {
  7970. AddNewSigleCancelStock(&cancelStock, tx)
  7971. }
  7972. manufactureName, _ := GetNewManufactureById(GoodOutInfo.Manufacturer, tx)
  7973. dealer, _ := GetNewDealerById(GoodOutInfo.Dealer, tx)
  7974. cancelInfo, _ := GetNewLastCancelStockById(orgID, tx)
  7975. cancelStockInfo := models.CancelStockInfo{
  7976. GoodId: project.ProjectId,
  7977. CancelStockId: cancelInfo.ID,
  7978. GoodTypeId: good.GoodTypeId,
  7979. Count: cha_count,
  7980. Price: project.Price,
  7981. Total: 0,
  7982. ProductDate: GoodOutInfo.ProductDate,
  7983. ExpiryDate: GoodOutInfo.ExpiryDate,
  7984. Ctime: time.Now().Unix(),
  7985. Status: 1,
  7986. OrgId: orgID,
  7987. OrderNumber: cancelInfo.OrderNumber,
  7988. Type: 0,
  7989. Dealer: dealer.DealerName,
  7990. Manufacturer: manufactureName.ManufacturerName,
  7991. Number: GoodOutInfo.Number,
  7992. RegisterAccount: "",
  7993. Remark: "",
  7994. WarehouseInfoId: GoodOutInfo.WarehouseInfotId,
  7995. PatientId: project.ProjectId,
  7996. RecordDate: project.RecordDate,
  7997. StorehouseId: storeConfig.StorehouseOutInfo,
  7998. IsCheck: 1,
  7999. }
  8000. CreateNewCancelStockInfoOne(&cancelStockInfo, tx)
  8001. flow := models.VmStockFlow{
  8002. WarehousingId: GoodOutInfo.WarehouseInfotId,
  8003. GoodId: project.ProjectId,
  8004. Number: GoodOutInfo.Number,
  8005. LicenseNumber: GoodOutInfo.LicenseNumber,
  8006. Count: cha_count,
  8007. UserOrgId: orgID,
  8008. PatientId: project.PatientId,
  8009. SystemTime: project.RecordDate,
  8010. ConsumableType: 7,
  8011. IsSys: 1,
  8012. WarehousingOrder: "",
  8013. WarehouseOutId: GoodOutInfo.WarehouseOutId,
  8014. WarehouseOutOrderNumber: GoodOutInfo.WarehouseOutOrderNumber,
  8015. IsEdit: 0,
  8016. CancelStockId: cancelInfo.ID,
  8017. CancelOrderNumber: cancelInfo.OrderNumber,
  8018. Manufacturer: manufactureName.ID,
  8019. Dealer: 0,
  8020. Creator: creater,
  8021. UpdateCreator: 0,
  8022. Status: 1,
  8023. Ctime: time.Now().Unix(),
  8024. Mtime: 0,
  8025. Price: GoodOutInfo.Price,
  8026. WarehousingDetailId: GoodOutInfo.WarehouseInfotId,
  8027. WarehouseOutDetailId: GoodOutInfo.ID,
  8028. CancelOutDetailId: cancelInfo.ID,
  8029. ProductDate: GoodOutInfo.ProductDate,
  8030. ExpireDate: GoodOutInfo.ExpiryDate,
  8031. StorehouseId: storeConfig.StorehouseOutInfo,
  8032. BuyPrice: project.Price,
  8033. ProjectId: project.ID,
  8034. OverCount: sum_count,
  8035. RegisterNumber: GoodOutInfo.RegisterNumber,
  8036. OperateTime: time.Now().Unix(),
  8037. }
  8038. CreateNewStockFlowOne(flow, tx)
  8039. AddGoodNewCancelSumCountOne(storeConfig.StorehouseOutInfo, project.ProjectId, orgID, flow.Count, tx)
  8040. ReduceNewGoodCountTwo(project.ProjectId, orgID, storeConfig.StorehouseOutInfo, flow.Count, tx)
  8041. }
  8042. //如果出库的数据小于这个退库的数量差,则先该批次出库数量出完
  8043. if out_count < cha_count {
  8044. //把该出库单的数量退完
  8045. ModifyNewGoodWarehouseInfoStockMinNumber(out_count, GoodOutInfo.GoodId, GoodOutInfo.OrgId, GoodOutInfo.WarehouseInfotId, tx)
  8046. //删除该出库单
  8047. DeleteGoodWarehouseOutInfoByNumber(GoodOutInfo.ID, tx)
  8048. //查询剩余库存
  8049. goodList, _ := GetNewAllGoodSumCount(project.ProjectId, orgID, storeConfig.StorehouseOutInfo, tx)
  8050. var sum_count int64
  8051. for _, item := range goodList {
  8052. sum_count += item.StockCount
  8053. }
  8054. operation_time := time.Now().Unix()
  8055. //创建退库单
  8056. timeStr := time.Now().Format("2006-01-02")
  8057. timeArr := strings.Split(timeStr, "-")
  8058. total, _ := FindNewAllCancelStockTotal(orgID, tx)
  8059. total = total + 1
  8060. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  8061. cancelStock := models.CancelStock{
  8062. OrderNumber: orderNumber,
  8063. OperaTime: operation_time,
  8064. OrgId: orgID,
  8065. Creater: creater,
  8066. Ctime: time.Now().Unix(),
  8067. Status: 1,
  8068. ReturnTime: project.RecordDate,
  8069. Type: 1,
  8070. StorehouseId: storeConfig.StorehouseOutInfo,
  8071. IsCheck: 1,
  8072. }
  8073. _, msgerrkonde := GetNewCancelStockDetailByOrderNumberOne(project.RecordDate, orgID, tx)
  8074. if msgerrkonde == gorm.ErrRecordNotFound {
  8075. AddNewSigleCancelStock(&cancelStock, tx)
  8076. }
  8077. manufactureName, _ := GetNewManufactureById(GoodOutInfo.Manufacturer, tx)
  8078. dealer, _ := GetNewDealerById(GoodOutInfo.Dealer, tx)
  8079. cancelInfo, _ := GetNewLastCancelStockById(orgID, tx)
  8080. cancelStockInfo := models.CancelStockInfo{
  8081. GoodId: project.ProjectId,
  8082. CancelStockId: cancelInfo.ID,
  8083. GoodTypeId: good.GoodTypeId,
  8084. Count: out_count,
  8085. Price: project.Price,
  8086. Total: 0,
  8087. ProductDate: GoodOutInfo.ProductDate,
  8088. ExpiryDate: GoodOutInfo.ExpiryDate,
  8089. Ctime: time.Now().Unix(),
  8090. Status: 1,
  8091. OrgId: orgID,
  8092. OrderNumber: cancelInfo.OrderNumber,
  8093. Type: 0,
  8094. Dealer: dealer.DealerName,
  8095. Manufacturer: manufactureName.ManufacturerName,
  8096. Number: GoodOutInfo.Number,
  8097. RegisterAccount: "",
  8098. Remark: "",
  8099. WarehouseInfoId: GoodOutInfo.WarehouseInfotId,
  8100. PatientId: project.ProjectId,
  8101. RecordDate: project.RecordDate,
  8102. StorehouseId: storeConfig.StorehouseOutInfo,
  8103. IsCheck: 1,
  8104. }
  8105. CreateNewCancelStockInfoOne(&cancelStockInfo, tx)
  8106. flow := models.VmStockFlow{
  8107. WarehousingId: GoodOutInfo.WarehouseInfotId,
  8108. GoodId: project.ProjectId,
  8109. Number: GoodOutInfo.Number,
  8110. LicenseNumber: GoodOutInfo.LicenseNumber,
  8111. Count: out_count,
  8112. UserOrgId: orgID,
  8113. PatientId: project.PatientId,
  8114. SystemTime: project.RecordDate,
  8115. ConsumableType: 7,
  8116. IsSys: 1,
  8117. WarehousingOrder: "",
  8118. WarehouseOutId: GoodOutInfo.WarehouseOutId,
  8119. WarehouseOutOrderNumber: GoodOutInfo.WarehouseOutOrderNumber,
  8120. IsEdit: 0,
  8121. CancelStockId: cancelInfo.ID,
  8122. CancelOrderNumber: cancelInfo.OrderNumber,
  8123. Manufacturer: manufactureName.ID,
  8124. Dealer: 0,
  8125. Creator: creater,
  8126. UpdateCreator: 0,
  8127. Status: 1,
  8128. Ctime: time.Now().Unix(),
  8129. Mtime: 0,
  8130. Price: GoodOutInfo.Price,
  8131. WarehousingDetailId: GoodOutInfo.WarehouseInfotId,
  8132. WarehouseOutDetailId: GoodOutInfo.ID,
  8133. CancelOutDetailId: cancelInfo.ID,
  8134. ProductDate: GoodOutInfo.ProductDate,
  8135. ExpireDate: GoodOutInfo.ExpiryDate,
  8136. StorehouseId: storeConfig.StorehouseOutInfo,
  8137. BuyPrice: project.Price,
  8138. ProjectId: project.ID,
  8139. OverCount: sum_count,
  8140. RegisterNumber: GoodOutInfo.RegisterNumber,
  8141. OperateTime: time.Now().Unix(),
  8142. }
  8143. CreateNewStockFlowOne(flow, tx)
  8144. AddGoodNewCancelSumCountOne(storeConfig.StorehouseOutInfo, project.ProjectId, orgID, flow.Count, tx)
  8145. ReduceNewGoodCountTwo(project.ProjectId, orgID, storeConfig.StorehouseOutInfo, flow.Count, tx)
  8146. //计算还有多少没有退库
  8147. var over_count = cha_count - out_count
  8148. NewHisGoodCancelInfo(orgID, creater, project, over_count, good, tx)
  8149. }
  8150. return err
  8151. }
  8152. func NewHisGoodAutoCancelInfo(orgID int64, creater int64, project *models.HisPrescriptionProject, cha_count int64, good models.GoodInformationThirty, tx *gorm.DB) error {
  8153. //查找患者最后一条的退库数据是否满足要求
  8154. var out_count int64
  8155. goodOutInfo, _ := GetGoodWarehouseOutInfoByProjectIdTwo(project.PatientId, project.UserOrgId, project.RecordDate, project.ProjectId, project.ID, tx)
  8156. out_count = goodOutInfo.Count
  8157. //如果最后1条出库数量大于退库数量,则直接退库,满足退库要求
  8158. if out_count >= cha_count {
  8159. //减去出库单里面的数量
  8160. ModifyGoodAuToWarehouseOut(goodOutInfo.ID, cha_count, tx)
  8161. if out_count == cha_count {
  8162. //删除该出库单
  8163. DeleteGoodWarehouseAutoByNumberTwo(goodOutInfo.ID, tx)
  8164. }
  8165. }
  8166. //如果出库的数据小于这个退库的数量差,则先该批次出库数量出完
  8167. if out_count < cha_count {
  8168. //删除该出库单
  8169. DeleteGoodWarehouseAutoByNumberTwo(goodOutInfo.ID, tx)
  8170. //计算还有多少没有退库
  8171. var over_count = cha_count - out_count
  8172. NewHisGoodAutoCancelInfo(orgID, creater, project, over_count, good, tx)
  8173. }
  8174. return err
  8175. }
  8176. func NewHisDrugsDeliveryOne(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo, tx *gorm.DB) (err error) {
  8177. // 1.判断药品是否来自专用字典的药品库
  8178. // 2.判断当天当前机构有没有创建出库单,没有则创建
  8179. // 3.创建出库流程
  8180. // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  8181. isHasWay := false
  8182. record_time := int64(0)
  8183. isHasWay = true
  8184. record_time = advice.RecordDate
  8185. if isHasWay {
  8186. //判断当天当前机构有没有创建出库单,没有则创建
  8187. out, err := FindNewDrugStockOutByIsSys(orgID, 1, record_time, tx)
  8188. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  8189. if err == gorm.ErrRecordNotFound {
  8190. timeStr := time.Now().Format("2006-01-02")
  8191. timeArr := strings.Split(timeStr, "-")
  8192. total, _ := FindAllDrugWarehouseOut(orgID)
  8193. total = total + 1
  8194. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  8195. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  8196. number = number + total
  8197. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  8198. warehouseOut := models.DrugWarehouseOut{
  8199. WarehouseOutOrderNumber: warehousing_out_order,
  8200. OperationTime: time.Now().Unix(),
  8201. OrgId: orgID,
  8202. Creater: creater,
  8203. Ctime: time.Now().Unix(),
  8204. Status: 1,
  8205. WarehouseOutTime: record_time,
  8206. Dealer: 0,
  8207. Manufacturer: 0,
  8208. Type: 1,
  8209. IsSys: 1,
  8210. StorehouseId: houseConfig.DrugStorehouseOut,
  8211. IsCheck: 1,
  8212. }
  8213. err := AddNewSigleDrugWarehouseOut(&warehouseOut, tx)
  8214. if err != nil {
  8215. utils.TraceLog("创建出库单失败 err = %v", err)
  8216. return err
  8217. } else {
  8218. out = warehouseOut
  8219. }
  8220. }
  8221. //出库流程
  8222. //1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  8223. drup, _ := FindNewBaseDrugLibRecord(orgID, advice.DrugId, tx)
  8224. if drup.ID > 0 {
  8225. prescribingNumber := advice.PrescribingNumber
  8226. HisStoryBloodHisDrugDeliverInfoOne(orgID, prescribingNumber, &out, &drup, advice, prescribingNumber, advice.PrescribingNumberUnit, tx)
  8227. } else {
  8228. return errors.New("药品信息不存在")
  8229. }
  8230. }
  8231. return
  8232. }
  8233. func HisFaDrugsDelivery(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo) (err error) {
  8234. // 1.判断药品是否来自专用字典的药品库
  8235. // 2.判断当天当前机构有没有创建出库单,没有则创建
  8236. // 3.创建出库流程
  8237. // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  8238. isHasWay := false
  8239. record_time := int64(0)
  8240. isHasWay = true
  8241. record_time = advice.RecordDate
  8242. if isHasWay {
  8243. //判断当天当前机构有没有创建出库单,没有则创建
  8244. out, err := FindDrugStockOutByIsSys(orgID, 1, record_time)
  8245. fmt.Println(out)
  8246. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  8247. if err == gorm.ErrRecordNotFound {
  8248. timeStr := time.Now().Format("2006-01-02")
  8249. timeArr := strings.Split(timeStr, "-")
  8250. total, _ := FindAllDrugWarehouseOut(orgID)
  8251. total = total + 1
  8252. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  8253. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  8254. number = number + total
  8255. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  8256. warehouseOut := models.DrugWarehouseOut{
  8257. WarehouseOutOrderNumber: warehousing_out_order,
  8258. OperationTime: time.Now().Unix(),
  8259. OrgId: orgID,
  8260. Creater: creater,
  8261. Ctime: time.Now().Unix(),
  8262. Status: 1,
  8263. WarehouseOutTime: record_time,
  8264. Dealer: 0,
  8265. Manufacturer: 0,
  8266. Type: 1,
  8267. IsSys: 1,
  8268. StorehouseId: houseConfig.DrugStorehouseOut,
  8269. IsCheck: 1,
  8270. }
  8271. err := AddSigleDrugWarehouseOut(&warehouseOut)
  8272. if err != nil {
  8273. utils.TraceLog("创建出库单失败 err = %v", err)
  8274. return err
  8275. } else {
  8276. out = warehouseOut
  8277. }
  8278. }
  8279. // 出库流程
  8280. // 1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  8281. drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
  8282. if drup.ID > 0 {
  8283. //var now_count int64
  8284. ////prescribingNumber_temp := strconv.FormatFloat(math.Abs(advice.PrescribingNumber), 'f', 0, 64)
  8285. ////now_count, _ = strconv.ParseInt(prescribingNumber_temp, 10, 64)
  8286. var now_count int64
  8287. if advice.PrescribingNumberUnit == drup.MaxUnit {
  8288. prescribingNumberOne := strconv.FormatFloat(advice.PrescribingNumber, 'f', -1, 64)
  8289. prescribingNumberInt, _ := strconv.ParseInt(prescribingNumberOne, 10, 64)
  8290. now_count = prescribingNumberInt * drup.MinNumber
  8291. }
  8292. if advice.PrescribingNumberUnit == drup.MinUnit {
  8293. prescribingNumberOne := strconv.FormatFloat(advice.PrescribingNumber, 'f', -1, 64)
  8294. prescribingNumberInt, _ := strconv.ParseInt(prescribingNumberOne, 10, 64)
  8295. now_count = prescribingNumberInt
  8296. //回退库存
  8297. }
  8298. outListExport, _ := GetWarehouseOutListExport(advice.DrugId, advice.PatientId, advice.AdviceDate, advice.ID)
  8299. var out_count int64
  8300. if len(outListExport) > 0 {
  8301. for _, item := range outListExport {
  8302. if item.CountUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  8303. out_count = item.Count * drup.MinNumber
  8304. }
  8305. if item.CountUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  8306. out_count = item.Count
  8307. }
  8308. if item.CountUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  8309. out_count = item.Count * drup.MinNumber
  8310. }
  8311. }
  8312. }
  8313. newadviceInfo := &models.HisDoctorAdviceInfo{
  8314. ID: advice.ID,
  8315. UserOrgId: advice.UserOrgId,
  8316. PatientId: advice.PatientId,
  8317. HisPatientId: advice.HisPatientId,
  8318. AdviceType: advice.AdviceType,
  8319. AdviceDate: advice.AdviceDate,
  8320. StartTime: advice.StartTime,
  8321. AdviceName: advice.AdviceName,
  8322. AdviceDesc: advice.AdviceDesc,
  8323. ReminderDate: advice.ReminderDate,
  8324. SingleDose: advice.SingleDose,
  8325. SingleDoseUnit: advice.SingleDoseUnit,
  8326. PrescribingNumber: advice.PrescribingNumber,
  8327. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  8328. DeliveryWay: advice.DeliveryWay,
  8329. ExecutionFrequency: advice.ExecutionFrequency,
  8330. AdviceDoctor: advice.AdviceDoctor,
  8331. Status: advice.Status,
  8332. CreatedTime: advice.CreatedTime,
  8333. UpdatedTime: advice.UpdatedTime,
  8334. AdviceAffirm: advice.AdviceAffirm,
  8335. Remark: advice.Remark,
  8336. StopTime: advice.StopTime,
  8337. StopReason: advice.StopReason,
  8338. StopDoctor: advice.StopDoctor,
  8339. StopState: advice.StopState,
  8340. ParentId: advice.ParentId,
  8341. ExecutionTime: advice.ExecutionTime,
  8342. ExecutionStaff: advice.ExecutionStaff,
  8343. ExecutionState: advice.ExecutionState,
  8344. Checker: advice.Checker,
  8345. RecordDate: advice.RecordDate,
  8346. DialysisOrderId: advice.DialysisOrderId,
  8347. CheckTime: advice.CheckTime,
  8348. CheckState: advice.CheckState,
  8349. DrugSpec: advice.DrugSpec,
  8350. DrugSpecUnit: advice.DrugSpecUnit,
  8351. Groupno: advice.Groupno,
  8352. RemindType: advice.RemindType,
  8353. FrequencyType: advice.FrequencyType,
  8354. DayCount: advice.DayCount,
  8355. WeekDay: advice.WeekDay,
  8356. TemplateId: advice.TemplateId,
  8357. Modifier: advice.Modifier,
  8358. DrugId: advice.DrugId,
  8359. Price: advice.Price,
  8360. PrescriptionId: advice.PrescriptionId,
  8361. MedListCodg: advice.MedListCodg,
  8362. FeedetlSn: advice.FeedetlSn,
  8363. Day: advice.DayCount,
  8364. ChildDoctorAdvice: nil,
  8365. Drug: models.Drug{},
  8366. Diagnosis: advice.Diagnosis,
  8367. Way: advice.Way,
  8368. HospApprFlag: advice.HospApprFlag,
  8369. LmtUsedFlag: advice.LmtUsedFlag,
  8370. HisOrderInfo: models.HisOrderInfo{},
  8371. IsMedicine: advice.IsMedicine,
  8372. ExecutionFrequencyId: advice.ExecutionFrequencyId,
  8373. Child: nil,
  8374. IsSettle: advice.IsSettle,
  8375. IsMobile: advice.IsMobile,
  8376. IsSelfDrug: advice.IsSelfDrug,
  8377. DrugWayCount: advice.DrugWayCount,
  8378. DrugCode: advice.DrugCode,
  8379. }
  8380. db := XTWriteDB()
  8381. tx := db.Begin()
  8382. // 在函数结束时处理事务回滚
  8383. defer func() {
  8384. if r := recover(); r != nil {
  8385. tx.Rollback()
  8386. } else {
  8387. tx.Commit()
  8388. }
  8389. }()
  8390. //如果当前医嘱数量大于历史出库数据则需要出库
  8391. if now_count > out_count {
  8392. //按最小单位出库
  8393. var cha_count = now_count - out_count
  8394. newadviceInfo.PrescribingNumber = float64(cha_count)
  8395. newadviceInfo.PrescribingNumberUnit = drup.MinUnit
  8396. NewHisDrugsDelivery(advice.UserOrgId, creater, newadviceInfo, tx)
  8397. }
  8398. //如果当前医嘱数量小于历史出库数据则需要退库
  8399. if now_count < out_count {
  8400. var cha_count = out_count - now_count
  8401. drug, _ := FindBaseDrugLibRecordSeven(advice.UserOrgId, advice.DrugId)
  8402. NewHisDrugCancelInfo(advice.UserOrgId, creater, newadviceInfo, cha_count, drug, tx)
  8403. NewHisDrugAutoCancelInfo(advice.UserOrgId, creater, newadviceInfo, cha_count, drug, tx)
  8404. }
  8405. if now_count == out_count {
  8406. if out_count == 0 {
  8407. NewHisDrugsDelivery(advice.UserOrgId, creater, newadviceInfo, tx)
  8408. }
  8409. }
  8410. } else {
  8411. return errors.New("药品信息不存在")
  8412. }
  8413. }
  8414. return
  8415. }
  8416. func NewMobileHisGoodCancelInfo(orgID int64, creater int64, project *models.HisPrescriptionProject, cha_count int64, good models.GoodInformationThirty, tx *gorm.DB) (err error) {
  8417. storeConfig, _ := GetNewAllStoreHouseConfig(orgID, tx)
  8418. //查找患者最后一条的退库数据是否满足要求
  8419. var out_count int64
  8420. GoodOutInfo, _ := GetGoodWarehouseOutInfoByProjectIdThree(project.PatientId, project.UserOrgId, project.RecordDate, project.ProjectId, tx)
  8421. out_count = GoodOutInfo.Count
  8422. //如果最后1条出库数量大于退库数量,则直接退库,满足退库要求
  8423. if out_count >= cha_count {
  8424. //退库 退回 cha_count 的数量
  8425. ModifyNewGoodWarehouseInfoStockMinNumber(cha_count, GoodOutInfo.GoodId, GoodOutInfo.OrgId, GoodOutInfo.WarehouseInfotId, tx)
  8426. if out_count == cha_count {
  8427. //删除该出库单
  8428. DeleteGoodWarehouseOutInfoByNumber(GoodOutInfo.ID, tx)
  8429. } else {
  8430. //减去出库单里面的数量
  8431. ModifyGoodWarehouseOutInfoByNumber(GoodOutInfo.ID, cha_count, tx)
  8432. }
  8433. //创建退库单
  8434. goodList, _ := GetNewAllGoodSumCount(project.ProjectId, orgID, storeConfig.StorehouseOutInfo, tx)
  8435. var sum_count int64
  8436. for _, item := range goodList {
  8437. sum_count += item.StockCount
  8438. }
  8439. operation_time := time.Now().Unix()
  8440. //创建退库单
  8441. timeStr := time.Now().Format("2006-01-02")
  8442. timeArr := strings.Split(timeStr, "-")
  8443. total, _ := FindNewAllCancelStockTotal(orgID, tx)
  8444. total = total + 1
  8445. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  8446. cancelStock := models.CancelStock{
  8447. OrderNumber: orderNumber,
  8448. OperaTime: operation_time,
  8449. OrgId: orgID,
  8450. Creater: creater,
  8451. Ctime: time.Now().Unix(),
  8452. Status: 1,
  8453. ReturnTime: project.RecordDate,
  8454. Type: 1,
  8455. StorehouseId: storeConfig.StorehouseOutInfo,
  8456. IsCheck: 1,
  8457. }
  8458. _, msgerrkonde := GetNewCancelStockDetailByOrderNumberOne(project.RecordDate, orgID, tx)
  8459. if msgerrkonde == gorm.ErrRecordNotFound {
  8460. AddNewSigleCancelStock(&cancelStock, tx)
  8461. }
  8462. manufactureName, _ := GetNewManufactureById(GoodOutInfo.Manufacturer, tx)
  8463. dealer, _ := GetNewDealerById(GoodOutInfo.Dealer, tx)
  8464. cancelInfo, _ := GetNewLastCancelStockById(orgID, tx)
  8465. cancelStockInfo := models.CancelStockInfo{
  8466. GoodId: project.ProjectId,
  8467. CancelStockId: cancelInfo.ID,
  8468. GoodTypeId: good.GoodTypeId,
  8469. Count: cha_count,
  8470. Price: project.Price,
  8471. Total: 0,
  8472. ProductDate: GoodOutInfo.ProductDate,
  8473. ExpiryDate: GoodOutInfo.ExpiryDate,
  8474. Ctime: time.Now().Unix(),
  8475. Status: 1,
  8476. OrgId: orgID,
  8477. OrderNumber: cancelInfo.OrderNumber,
  8478. Type: 0,
  8479. Dealer: dealer.DealerName,
  8480. Manufacturer: manufactureName.ManufacturerName,
  8481. Number: GoodOutInfo.Number,
  8482. RegisterAccount: "",
  8483. Remark: "",
  8484. WarehouseInfoId: GoodOutInfo.WarehouseInfotId,
  8485. PatientId: project.ProjectId,
  8486. RecordDate: project.RecordDate,
  8487. StorehouseId: storeConfig.StorehouseOutInfo,
  8488. IsCheck: 1,
  8489. }
  8490. CreateNewCancelStockInfoOne(&cancelStockInfo, tx)
  8491. flow := models.VmStockFlow{
  8492. WarehousingId: GoodOutInfo.WarehouseInfotId,
  8493. GoodId: project.ProjectId,
  8494. Number: GoodOutInfo.Number,
  8495. LicenseNumber: GoodOutInfo.LicenseNumber,
  8496. Count: cha_count,
  8497. UserOrgId: orgID,
  8498. PatientId: project.PatientId,
  8499. SystemTime: project.RecordDate,
  8500. ConsumableType: 7,
  8501. IsSys: 1,
  8502. WarehousingOrder: "",
  8503. WarehouseOutId: GoodOutInfo.WarehouseOutId,
  8504. WarehouseOutOrderNumber: GoodOutInfo.WarehouseOutOrderNumber,
  8505. IsEdit: 0,
  8506. CancelStockId: cancelInfo.ID,
  8507. CancelOrderNumber: cancelInfo.OrderNumber,
  8508. Manufacturer: manufactureName.ID,
  8509. Dealer: 0,
  8510. Creator: creater,
  8511. UpdateCreator: 0,
  8512. Status: 1,
  8513. Ctime: time.Now().Unix(),
  8514. Mtime: 0,
  8515. Price: GoodOutInfo.Price,
  8516. WarehousingDetailId: GoodOutInfo.WarehouseInfotId,
  8517. WarehouseOutDetailId: GoodOutInfo.ID,
  8518. CancelOutDetailId: cancelInfo.ID,
  8519. ProductDate: GoodOutInfo.ProductDate,
  8520. ExpireDate: GoodOutInfo.ExpiryDate,
  8521. StorehouseId: storeConfig.StorehouseOutInfo,
  8522. BuyPrice: project.Price,
  8523. ProjectId: project.ID,
  8524. OverCount: sum_count,
  8525. RegisterNumber: GoodOutInfo.RegisterNumber,
  8526. OperateTime: time.Now().Unix(),
  8527. }
  8528. CreateNewStockFlowOne(flow, tx)
  8529. AddGoodNewCancelSumCountOne(storeConfig.StorehouseOutInfo, project.ProjectId, orgID, flow.Count, tx)
  8530. ReduceNewGoodCountTwo(project.ProjectId, orgID, storeConfig.StorehouseOutInfo, flow.Count, tx)
  8531. }
  8532. //如果出库的数据小于这个退库的数量差,则先该批次出库数量出完
  8533. if out_count < cha_count {
  8534. //把该出库单的数量退完
  8535. ModifyNewGoodWarehouseInfoStockMinNumber(out_count, GoodOutInfo.GoodId, GoodOutInfo.OrgId, GoodOutInfo.WarehouseInfotId, tx)
  8536. //删除该出库单
  8537. DeleteGoodWarehouseOutInfoByNumber(GoodOutInfo.ID, tx)
  8538. //查询剩余库存
  8539. goodList, _ := GetNewAllGoodSumCount(project.ProjectId, orgID, storeConfig.StorehouseOutInfo, tx)
  8540. var sum_count int64
  8541. for _, item := range goodList {
  8542. sum_count += item.StockCount
  8543. }
  8544. operation_time := time.Now().Unix()
  8545. //创建退库单
  8546. timeStr := time.Now().Format("2006-01-02")
  8547. timeArr := strings.Split(timeStr, "-")
  8548. total, _ := FindNewAllCancelStockTotal(orgID, tx)
  8549. total = total + 1
  8550. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  8551. cancelStock := models.CancelStock{
  8552. OrderNumber: orderNumber,
  8553. OperaTime: operation_time,
  8554. OrgId: orgID,
  8555. Creater: creater,
  8556. Ctime: time.Now().Unix(),
  8557. Status: 1,
  8558. ReturnTime: project.RecordDate,
  8559. Type: 1,
  8560. StorehouseId: storeConfig.StorehouseOutInfo,
  8561. IsCheck: 1,
  8562. }
  8563. _, msgerrkonde := GetNewCancelStockDetailByOrderNumberOne(project.RecordDate, orgID, tx)
  8564. if msgerrkonde == gorm.ErrRecordNotFound {
  8565. AddNewSigleCancelStock(&cancelStock, tx)
  8566. }
  8567. manufactureName, _ := GetNewManufactureById(GoodOutInfo.Manufacturer, tx)
  8568. dealer, _ := GetNewDealerById(GoodOutInfo.Dealer, tx)
  8569. cancelInfo, _ := GetNewLastCancelStockById(orgID, tx)
  8570. cancelStockInfo := models.CancelStockInfo{
  8571. GoodId: project.ProjectId,
  8572. CancelStockId: cancelInfo.ID,
  8573. GoodTypeId: good.GoodTypeId,
  8574. Count: out_count,
  8575. Price: project.Price,
  8576. Total: 0,
  8577. ProductDate: GoodOutInfo.ProductDate,
  8578. ExpiryDate: GoodOutInfo.ExpiryDate,
  8579. Ctime: time.Now().Unix(),
  8580. Status: 1,
  8581. OrgId: orgID,
  8582. OrderNumber: cancelInfo.OrderNumber,
  8583. Type: 0,
  8584. Dealer: dealer.DealerName,
  8585. Manufacturer: manufactureName.ManufacturerName,
  8586. Number: GoodOutInfo.Number,
  8587. RegisterAccount: "",
  8588. Remark: "",
  8589. WarehouseInfoId: GoodOutInfo.WarehouseInfotId,
  8590. PatientId: project.ProjectId,
  8591. RecordDate: project.RecordDate,
  8592. StorehouseId: storeConfig.StorehouseOutInfo,
  8593. IsCheck: 1,
  8594. }
  8595. CreateNewCancelStockInfoOne(&cancelStockInfo, tx)
  8596. flow := models.VmStockFlow{
  8597. WarehousingId: GoodOutInfo.WarehouseInfotId,
  8598. GoodId: project.ProjectId,
  8599. Number: GoodOutInfo.Number,
  8600. LicenseNumber: GoodOutInfo.LicenseNumber,
  8601. Count: out_count,
  8602. UserOrgId: orgID,
  8603. PatientId: project.PatientId,
  8604. SystemTime: project.RecordDate,
  8605. ConsumableType: 7,
  8606. IsSys: 1,
  8607. WarehousingOrder: "",
  8608. WarehouseOutId: GoodOutInfo.WarehouseOutId,
  8609. WarehouseOutOrderNumber: GoodOutInfo.WarehouseOutOrderNumber,
  8610. IsEdit: 0,
  8611. CancelStockId: cancelInfo.ID,
  8612. CancelOrderNumber: cancelInfo.OrderNumber,
  8613. Manufacturer: manufactureName.ID,
  8614. Dealer: 0,
  8615. Creator: creater,
  8616. UpdateCreator: 0,
  8617. Status: 1,
  8618. Ctime: time.Now().Unix(),
  8619. Mtime: 0,
  8620. Price: GoodOutInfo.Price,
  8621. WarehousingDetailId: GoodOutInfo.WarehouseInfotId,
  8622. WarehouseOutDetailId: GoodOutInfo.ID,
  8623. CancelOutDetailId: cancelInfo.ID,
  8624. ProductDate: GoodOutInfo.ProductDate,
  8625. ExpireDate: GoodOutInfo.ExpiryDate,
  8626. StorehouseId: storeConfig.StorehouseOutInfo,
  8627. BuyPrice: project.Price,
  8628. ProjectId: project.ID,
  8629. OverCount: sum_count,
  8630. RegisterNumber: GoodOutInfo.RegisterNumber,
  8631. OperateTime: time.Now().Unix(),
  8632. }
  8633. CreateNewStockFlowOne(flow, tx)
  8634. AddGoodNewCancelSumCountOne(storeConfig.StorehouseOutInfo, project.ProjectId, orgID, flow.Count, tx)
  8635. ReduceNewGoodCountTwo(project.ProjectId, orgID, storeConfig.StorehouseOutInfo, flow.Count, tx)
  8636. //计算还有多少没有退库
  8637. var over_count = cha_count - out_count
  8638. NewHisGoodCancelInfo(orgID, creater, project, over_count, good, tx)
  8639. }
  8640. return err
  8641. }
  8642. func NewMobileHisGoodAutoCancelInfo(orgID int64, creater int64, project *models.HisPrescriptionProject, cha_count int64, good models.GoodInformationThirty, tx *gorm.DB) error {
  8643. //查找患者最后一条的退库数据是否满足要求
  8644. var out_count int64
  8645. goodOutInfo, _ := GetGoodWarehouseOutInfoByProjectIdThree(project.PatientId, project.UserOrgId, project.RecordDate, project.ProjectId, tx)
  8646. out_count = goodOutInfo.Count
  8647. //如果最后1条出库数量大于退库数量,则直接退库,满足退库要求
  8648. if out_count >= cha_count {
  8649. //减去出库单里面的数量
  8650. ModifyGoodAuToWarehouseOut(goodOutInfo.ID, cha_count, tx)
  8651. if out_count == cha_count {
  8652. //删除该出库单
  8653. DeleteGoodWarehouseAutoByNumberTwo(goodOutInfo.ID, tx)
  8654. }
  8655. }
  8656. //如果出库的数据小于这个退库的数量差,则先该批次出库数量出完
  8657. if out_count < cha_count {
  8658. //删除该出库单
  8659. DeleteGoodWarehouseAutoByNumberTwo(goodOutInfo.ID, tx)
  8660. //计算还有多少没有退库
  8661. var over_count = cha_count - out_count
  8662. NewHisGoodAutoCancelInfo(orgID, creater, project, over_count, good, tx)
  8663. }
  8664. return err
  8665. }
  8666. // 药品手动出库 递归方式
  8667. func AutoSelfDrugDeliverInfoFourtyOne(orgID int64, prescribingNumber int64, warehouseout *models.XtSelfDrugWarehouseOut, drup *models.BaseDrugLib, advice *models.XtSelfDrugWarehouseOutInfo, unit string, tx *gorm.DB) (err error) {
  8668. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  8669. var deliver_number int64 = 0
  8670. var stock_number int64 = 0
  8671. var drug_price float64
  8672. if advice.CountUnit == drup.MaxUnit {
  8673. deliver_number = prescribingNumber * drup.MinNumber
  8674. } else {
  8675. deliver_number = prescribingNumber
  8676. }
  8677. if advice.CountUnit == drup.MaxUnit {
  8678. drug_price = drup.RetailPrice
  8679. }
  8680. fmt.Println(drug_price)
  8681. if advice.CountUnit == drup.MinUnit {
  8682. drug_price = drup.MinPrice
  8683. }
  8684. if advice.CountUnit != drup.MaxUnit || advice.CountUnit != drup.MinUnit {
  8685. drug_price = drup.RetailPrice
  8686. }
  8687. // 根据先进先出原则,查询最先入库的批次,进行出库
  8688. // 如果没有对应的库存,则报错
  8689. lastWarehouse, _ := FindSelfLastDrugWarehousingInfoByID(advice.DrugId, advice.PatientId, tx)
  8690. if lastWarehouse.MaxUnit != lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  8691. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  8692. var stockMax int64
  8693. var stockMin int64
  8694. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  8695. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  8696. ChangeSelfMaxNumber(lastWarehouse.ID, stockMax, tx)
  8697. UpdateSelfMinNumber(lastWarehouse.ID, stockMin, tx)
  8698. }
  8699. }
  8700. if lastWarehouse.MaxUnit == lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  8701. lastWarehouse.StockMaxNumber = lastWarehouse.StockMaxNumber + lastWarehouse.StockMinNumber
  8702. ChangeSelfMaxNumberOne(lastWarehouse.ID, lastWarehouse.StockMaxNumber, tx)
  8703. }
  8704. warehouse, err := FindSelfLastDrugWarehousingInfoByID(advice.DrugId, advice.PatientId, tx)
  8705. // 将该批次的剩余库存数量转换为拆零数量
  8706. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  8707. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  8708. }
  8709. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  8710. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  8711. }
  8712. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  8713. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  8714. }
  8715. if stock_number >= deliver_number {
  8716. var maxNumber int64 = 0
  8717. var minNumber int64 = 0
  8718. maxNumber = deliver_number / drup.MinNumber
  8719. minNumber = deliver_number % drup.MinNumber
  8720. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  8721. minNumber = maxNumber
  8722. }
  8723. if drup.MaxUnit != drup.MinUnit {
  8724. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  8725. return errors.New("库存数量不足")
  8726. }
  8727. }
  8728. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  8729. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  8730. }
  8731. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  8732. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  8733. }
  8734. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  8735. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  8736. }
  8737. if warehouse.StockMaxNumber < 0 {
  8738. warehouse.StockMaxNumber = 0
  8739. }
  8740. if warehouse.StockMinNumber < 0 {
  8741. warehouse.StockMinNumber = 0
  8742. }
  8743. warehouse.Mtime = time.Now().Unix()
  8744. if warehouse.StockMinNumber < minNumber {
  8745. //fmt.Println("金4")
  8746. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  8747. if warehouse.MaxUnit != warehouse.MinUnit {
  8748. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  8749. }
  8750. if minNumber == 2 && warehouse.MaxUnit == warehouse.MinUnit && warehouse.StockMinNumber == 0 {
  8751. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  8752. }
  8753. } else {
  8754. //fmt.Println("金5")
  8755. if minNumber > 0 {
  8756. if (warehouse.StockMinNumber - minNumber) >= 0 {
  8757. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  8758. }
  8759. }
  8760. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  8761. //fmt.Println("6")
  8762. if warehouse.StockMinNumber > 0 {
  8763. if warehouse.StockMinNumber-deliver_number >= 0 {
  8764. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  8765. }
  8766. }
  8767. }
  8768. }
  8769. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit && warehouse.MaxUnit != warehouse.MinUnit {
  8770. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  8771. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  8772. }
  8773. }
  8774. if drup.MaxUnit != drup.MinUnit {
  8775. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  8776. return errors.New("库存数量不足")
  8777. }
  8778. }
  8779. if warehouse.StockMinNumber <= 0 {
  8780. warehouse.StockMinNumber = 0
  8781. }
  8782. errThree := UpDateSelfDrugWarehouseInfoByStock(&warehouse, tx)
  8783. if errThree != nil {
  8784. return errors.New("扣减库存失败")
  8785. }
  8786. //查询该药品剩余库存
  8787. infolist, _ := FindSelfDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId, tx)
  8788. var over_count int64
  8789. for _, its := range infolist {
  8790. if its.MaxUnit == drup.MaxUnit {
  8791. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  8792. }
  8793. over_count += its.StockMaxNumber + its.StockMinNumber
  8794. }
  8795. //更新出库id
  8796. //查询今日该药品该患者是否有出库数据
  8797. lastDrugOutInfo, _ := GetSelfNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.SysRecordTime, advice.OrgId, advice.AdviceId, warehouse.ID, tx)
  8798. if lastDrugOutInfo.ID == 0 {
  8799. advice.WarehouseInfoId = lastWarehouse.ID
  8800. AddSelfSigleDrugWarehouseOutInfo(advice, tx)
  8801. }
  8802. if lastDrugOutInfo.ID > 0 {
  8803. advice.WarehouseInfoId = lastWarehouse.ID
  8804. //删除在新增
  8805. UpdateSelfDrugWarehouseOutInfoTen(lastDrugOutInfo.PatientId, lastDrugOutInfo.SysRecordTime, advice.ID, tx)
  8806. AddSelfSigleDrugWarehouseOutInfo(advice, tx)
  8807. }
  8808. drugWareInfo, _ := GetSelfLastWarehouseOutInfoById(advice.DrugId, tx)
  8809. drugflow := models.XtSelfDrugFlow{
  8810. WarehouseOutId: warehouseout.ID,
  8811. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8812. DrugId: advice.DrugId,
  8813. Number: warehouse.Number,
  8814. ProductDate: advice.ProductDate,
  8815. ExpireDate: advice.ExpiryDate,
  8816. Count: deliver_number,
  8817. Price: warehouse.RetailPrice,
  8818. Status: 1,
  8819. Ctime: advice.SysRecordTime,
  8820. Mtime: time.Now().Unix(),
  8821. UserOrgId: advice.OrgId,
  8822. Manufacturer: advice.Manufacturer,
  8823. Dealer: advice.Dealer,
  8824. BatchNumber: warehouse.BatchNumber,
  8825. MaxUnit: drup.MinUnit,
  8826. ConsumableType: 2,
  8827. IsEdit: 1,
  8828. Creator: 0,
  8829. IsSys: 0,
  8830. WarehouseOutDetailId: drugWareInfo.ID,
  8831. AdviceId: advice.ID,
  8832. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  8833. StorehouseId: advice.StorehouseId,
  8834. AdminUserId: advice.AdminUserId,
  8835. LastPrice: warehouse.Price,
  8836. StockCount: advice.StockCount,
  8837. WarehousingDetailId: warehouse.ID,
  8838. OverCount: over_count,
  8839. RetailPrice: drug_price, //手动出库出库价格
  8840. WarehousingId: warehouse.ID,
  8841. SystemTime: advice.SysRecordTime,
  8842. PatientId: advice.PatientId,
  8843. Remark: advice.Remark,
  8844. OperateTime: time.Now().Unix(),
  8845. }
  8846. if warehouse.RetailPrice == 0 {
  8847. drugflow.Price = advice.Price
  8848. }
  8849. CreateSelfDrugFlowOne(drugflow, tx)
  8850. //出库数量相加
  8851. AddSelfDrugCount(advice.DrugId, orgID, advice.PatientId, drugflow.Count, tx)
  8852. return nil
  8853. } else {
  8854. stock_number_int := strconv.FormatInt(stock_number, 10)
  8855. stock_number_int_count, _ := strconv.ParseFloat(stock_number_int, 64)
  8856. drug_price = stock_number_int_count * advice.Price
  8857. //扣减库存
  8858. warehouse.StockMaxNumber = 0
  8859. warehouse.StockMinNumber = 0
  8860. warehouse.Mtime = time.Now().Unix()
  8861. errThree := UpDateSelfDrugWarehouseInfoByStock(&warehouse, tx)
  8862. if errThree != nil {
  8863. return errThree
  8864. }
  8865. //查询该药品剩余库存
  8866. infolist, _ := FindDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId)
  8867. var over_count int64
  8868. for _, its := range infolist {
  8869. if its.MaxUnit == drup.MaxUnit {
  8870. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  8871. }
  8872. over_count += its.StockMaxNumber + its.StockMinNumber
  8873. }
  8874. //查询今日该药品该患者是否有出库数据
  8875. lastDrugOutInfo, _ := GetSelfNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.SysRecordTime, advice.OrgId, advice.AdviceId, warehouse.ID, tx)
  8876. if lastDrugOutInfo.ID == 0 {
  8877. advice.WarehouseInfoId = lastWarehouse.ID
  8878. AddSelfSigleDrugWarehouseOutInfo(advice, tx)
  8879. }
  8880. if lastDrugOutInfo.ID > 0 {
  8881. advice.WarehouseInfoId = lastWarehouse.ID
  8882. //删除在新增
  8883. UpdateSelfDrugWarehouseOutInfoTen(lastDrugOutInfo.PatientId, lastDrugOutInfo.SysRecordTime, advice.ID, tx)
  8884. AddSelfSigleDrugWarehouseOutInfo(advice, tx)
  8885. }
  8886. drugWareInfo, _ := GetSelfLastWarehouseOutInfoById(advice.DrugId, tx)
  8887. drugflow := models.XtSelfDrugFlow{
  8888. WarehouseOutId: warehouseout.ID,
  8889. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8890. DrugId: advice.DrugId,
  8891. Number: warehouse.Number,
  8892. ProductDate: advice.ProductDate,
  8893. ExpireDate: advice.ExpiryDate,
  8894. Count: stock_number,
  8895. Price: warehouse.RetailPrice,
  8896. Status: 1,
  8897. Ctime: advice.SysRecordTime,
  8898. Mtime: time.Now().Unix(),
  8899. UserOrgId: advice.OrgId,
  8900. Manufacturer: advice.Manufacturer,
  8901. Dealer: advice.Dealer,
  8902. BatchNumber: warehouse.BatchNumber,
  8903. MaxUnit: drup.MinUnit,
  8904. ConsumableType: 2,
  8905. IsEdit: 1,
  8906. Creator: 0,
  8907. IsSys: 0,
  8908. WarehouseOutDetailId: drugWareInfo.ID,
  8909. AdviceId: advice.ID,
  8910. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  8911. StorehouseId: advice.StorehouseId,
  8912. AdminUserId: advice.AdminUserId,
  8913. LastPrice: warehouse.Price,
  8914. StockCount: advice.StockCount,
  8915. OverCount: over_count,
  8916. RetailPrice: drug_price, //手动出库出库价格
  8917. WarehousingId: warehouse.ID,
  8918. WarehousingDetailId: warehouse.ID,
  8919. SystemTime: advice.SysRecordTime,
  8920. PatientId: advice.PatientId,
  8921. Remark: advice.Remark,
  8922. OperateTime: time.Now().Unix(),
  8923. }
  8924. CreateSelfDrugFlowOne(drugflow, tx)
  8925. AddSelfDrugCount(advice.DrugId, orgID, advice.PatientId, drugflow.Count, tx)
  8926. // 出库完成后,要将该批次库存清零
  8927. prescribingNumber_two_temp := deliver_number - stock_number
  8928. advice.CountUnit = drup.MinUnit
  8929. AutoSelfDrugDeliverInfoFourtyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, unit, tx)
  8930. }
  8931. return
  8932. }
  8933. // 药品手动出库 递归方式
  8934. func AutoSelfDrugDeliverInfoFourtyTwo(orgID int64, prescribingNumber int64, warehouseout *models.XtSelfDrugWarehouseOut, drup *models.BaseDrugLib, advice *models.XtSelfDrugWarehouseOutInfo, unit string, tx *gorm.DB) (err error) {
  8935. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  8936. var deliver_number int64 = 0
  8937. var stock_number int64 = 0
  8938. var drug_price float64
  8939. if advice.CountUnit == drup.MaxUnit {
  8940. deliver_number = prescribingNumber * drup.MinNumber
  8941. } else {
  8942. deliver_number = prescribingNumber
  8943. }
  8944. if advice.CountUnit == drup.MaxUnit {
  8945. drug_price = drup.RetailPrice
  8946. }
  8947. fmt.Println(drug_price)
  8948. if advice.CountUnit == drup.MinUnit {
  8949. drug_price = drup.MinPrice
  8950. }
  8951. if advice.CountUnit != drup.MaxUnit || advice.CountUnit != drup.MinUnit {
  8952. drug_price = drup.RetailPrice
  8953. }
  8954. // 根据先进先出原则,查询最先入库的批次,进行出库
  8955. // 如果没有对应的库存,则报错
  8956. lastWarehouse, _ := FindSelfLastDrugWarehousingInfoByID(advice.DrugId, advice.PatientId, tx)
  8957. if lastWarehouse.MaxUnit != lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  8958. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  8959. var stockMax int64
  8960. var stockMin int64
  8961. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  8962. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  8963. ChangeSelfMaxNumber(lastWarehouse.ID, stockMax, tx)
  8964. UpdateSelfMinNumber(lastWarehouse.ID, stockMin, tx)
  8965. }
  8966. }
  8967. if lastWarehouse.MaxUnit == lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  8968. lastWarehouse.StockMaxNumber = lastWarehouse.StockMaxNumber + lastWarehouse.StockMinNumber
  8969. ChangeSelfMaxNumberOne(lastWarehouse.ID, lastWarehouse.StockMaxNumber, tx)
  8970. }
  8971. warehouse, err := FindSelfLastDrugWarehousingInfoByID(advice.DrugId, advice.PatientId, tx)
  8972. // 将该批次的剩余库存数量转换为拆零数量
  8973. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  8974. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  8975. }
  8976. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  8977. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  8978. }
  8979. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  8980. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  8981. }
  8982. if stock_number >= deliver_number {
  8983. var maxNumber int64 = 0
  8984. var minNumber int64 = 0
  8985. maxNumber = deliver_number / drup.MinNumber
  8986. minNumber = deliver_number % drup.MinNumber
  8987. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  8988. minNumber = maxNumber
  8989. }
  8990. if drup.MaxUnit != drup.MinUnit {
  8991. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  8992. return errors.New("库存数量不足")
  8993. }
  8994. }
  8995. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  8996. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  8997. }
  8998. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  8999. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  9000. }
  9001. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  9002. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  9003. }
  9004. if warehouse.StockMaxNumber < 0 {
  9005. warehouse.StockMaxNumber = 0
  9006. }
  9007. if warehouse.StockMinNumber < 0 {
  9008. warehouse.StockMinNumber = 0
  9009. }
  9010. warehouse.Mtime = time.Now().Unix()
  9011. if warehouse.StockMinNumber < minNumber {
  9012. //fmt.Println("金4")
  9013. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  9014. if warehouse.MaxUnit != warehouse.MinUnit {
  9015. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  9016. }
  9017. if minNumber == 2 && warehouse.MaxUnit == warehouse.MinUnit && warehouse.StockMinNumber == 0 {
  9018. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  9019. }
  9020. } else {
  9021. //fmt.Println("金5")
  9022. if minNumber > 0 {
  9023. if (warehouse.StockMinNumber - minNumber) >= 0 {
  9024. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  9025. }
  9026. }
  9027. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  9028. //fmt.Println("6")
  9029. if warehouse.StockMinNumber > 0 {
  9030. if warehouse.StockMinNumber-deliver_number >= 0 {
  9031. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  9032. }
  9033. }
  9034. }
  9035. }
  9036. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit && warehouse.MaxUnit != warehouse.MinUnit {
  9037. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  9038. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  9039. }
  9040. }
  9041. if drup.MaxUnit != drup.MinUnit {
  9042. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  9043. return errors.New("库存数量不足")
  9044. }
  9045. }
  9046. if warehouse.StockMinNumber <= 0 {
  9047. warehouse.StockMinNumber = 0
  9048. }
  9049. errThree := UpDateSelfDrugWarehouseInfoByStock(&warehouse, tx)
  9050. if errThree != nil {
  9051. return errors.New("扣减库存失败")
  9052. }
  9053. //查询该药品剩余库存
  9054. infolist, _ := FindSelfDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId, tx)
  9055. var over_count int64
  9056. for _, its := range infolist {
  9057. if its.MaxUnit == drup.MaxUnit {
  9058. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  9059. }
  9060. over_count += its.StockMaxNumber + its.StockMinNumber
  9061. }
  9062. //更新出库id
  9063. //查询今日该药品该患者是否有出库数据
  9064. lastDrugOutInfo, _ := GetSelfNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.SysRecordTime, advice.OrgId, advice.AdviceId, warehouse.ID, tx)
  9065. if lastDrugOutInfo.ID == 0 {
  9066. advice.WarehouseInfoId = lastWarehouse.ID
  9067. AddSelfSigleDrugWarehouseOutInfo(advice, tx)
  9068. }
  9069. if lastDrugOutInfo.ID > 0 {
  9070. advice.WarehouseInfoId = lastWarehouse.ID
  9071. //删除在新增
  9072. UpdateSelfDrugWarehouseOutInfoTen(lastDrugOutInfo.PatientId, lastDrugOutInfo.SysRecordTime, advice.ID, tx)
  9073. AddSelfSigleDrugWarehouseOutInfo(advice, tx)
  9074. }
  9075. drugWareInfo, _ := GetSelfLastWarehouseOutInfoById(advice.DrugId, tx)
  9076. drugflow := models.XtSelfDrugFlow{
  9077. WarehouseOutId: warehouseout.ID,
  9078. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  9079. DrugId: advice.DrugId,
  9080. Number: warehouse.Number,
  9081. ProductDate: advice.ProductDate,
  9082. ExpireDate: advice.ExpiryDate,
  9083. Count: deliver_number,
  9084. Price: warehouse.RetailPrice,
  9085. Status: 1,
  9086. Ctime: advice.SysRecordTime,
  9087. Mtime: time.Now().Unix(),
  9088. UserOrgId: advice.OrgId,
  9089. Manufacturer: advice.Manufacturer,
  9090. Dealer: advice.Dealer,
  9091. BatchNumber: warehouse.BatchNumber,
  9092. MaxUnit: drup.MinUnit,
  9093. ConsumableType: 3,
  9094. IsEdit: 1,
  9095. Creator: 0,
  9096. IsSys: 0,
  9097. WarehouseOutDetailId: drugWareInfo.ID,
  9098. AdviceId: advice.ID,
  9099. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9100. StorehouseId: advice.StorehouseId,
  9101. AdminUserId: advice.AdminUserId,
  9102. LastPrice: warehouse.Price,
  9103. StockCount: advice.StockCount,
  9104. WarehousingDetailId: warehouse.ID,
  9105. OverCount: over_count,
  9106. RetailPrice: drug_price, //手动出库出库价格
  9107. WarehousingId: warehouse.ID,
  9108. SystemTime: advice.SysRecordTime,
  9109. PatientId: advice.PatientId,
  9110. Remark: advice.Remark,
  9111. OperateTime: time.Now().Unix(),
  9112. }
  9113. if warehouse.RetailPrice == 0 {
  9114. drugflow.Price = advice.Price
  9115. }
  9116. CreateSelfDrugFlowOne(drugflow, tx)
  9117. //出库数量相加
  9118. AddSelfDrugCount(advice.DrugId, orgID, advice.PatientId, drugflow.Count, tx)
  9119. //fmt.Println("advice-------------------------------------", advice.ID)
  9120. //fmt.Println("warehouse-------------------------------------", warehouse.ID)
  9121. //fmt.Println("over_count====================================", over_count)
  9122. return nil
  9123. } else {
  9124. stock_number_int := strconv.FormatInt(stock_number, 10)
  9125. stock_number_int_count, _ := strconv.ParseFloat(stock_number_int, 64)
  9126. drug_price = stock_number_int_count * advice.Price
  9127. //扣减库存
  9128. warehouse.StockMaxNumber = 0
  9129. warehouse.StockMinNumber = 0
  9130. warehouse.Mtime = time.Now().Unix()
  9131. errThree := UpDateSelfDrugWarehouseInfoByStock(&warehouse, tx)
  9132. if errThree != nil {
  9133. return errThree
  9134. }
  9135. //查询该药品剩余库存
  9136. infolist, _ := FindDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId)
  9137. var over_count int64
  9138. for _, its := range infolist {
  9139. if its.MaxUnit == drup.MaxUnit {
  9140. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  9141. }
  9142. over_count += its.StockMaxNumber + its.StockMinNumber
  9143. }
  9144. //查询今日该药品该患者是否有出库数据
  9145. lastDrugOutInfo, _ := GetSelfNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.SysRecordTime, advice.OrgId, advice.AdviceId, warehouse.ID, tx)
  9146. if lastDrugOutInfo.ID == 0 {
  9147. advice.WarehouseInfoId = lastWarehouse.ID
  9148. AddSelfSigleDrugWarehouseOutInfo(advice, tx)
  9149. }
  9150. if lastDrugOutInfo.ID > 0 {
  9151. advice.WarehouseInfoId = lastWarehouse.ID
  9152. //删除在新增
  9153. UpdateSelfDrugWarehouseOutInfoTen(lastDrugOutInfo.PatientId, lastDrugOutInfo.SysRecordTime, advice.ID, tx)
  9154. AddSelfSigleDrugWarehouseOutInfo(advice, tx)
  9155. }
  9156. drugWareInfo, _ := GetSelfLastWarehouseOutInfoById(advice.DrugId, tx)
  9157. drugflow := models.XtSelfDrugFlow{
  9158. WarehouseOutId: warehouseout.ID,
  9159. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  9160. DrugId: advice.DrugId,
  9161. Number: warehouse.Number,
  9162. ProductDate: advice.ProductDate,
  9163. ExpireDate: advice.ExpiryDate,
  9164. Count: stock_number,
  9165. Price: warehouse.RetailPrice,
  9166. Status: 1,
  9167. Ctime: advice.SysRecordTime,
  9168. Mtime: time.Now().Unix(),
  9169. UserOrgId: advice.OrgId,
  9170. Manufacturer: advice.Manufacturer,
  9171. Dealer: advice.Dealer,
  9172. BatchNumber: warehouse.BatchNumber,
  9173. MaxUnit: drup.MinUnit,
  9174. ConsumableType: 3,
  9175. IsEdit: 1,
  9176. Creator: 0,
  9177. IsSys: 0,
  9178. WarehouseOutDetailId: drugWareInfo.ID,
  9179. AdviceId: advice.ID,
  9180. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9181. StorehouseId: advice.StorehouseId,
  9182. AdminUserId: advice.AdminUserId,
  9183. LastPrice: warehouse.Price,
  9184. StockCount: advice.StockCount,
  9185. OverCount: over_count,
  9186. RetailPrice: drug_price, //手动出库出库价格
  9187. WarehousingId: warehouse.ID,
  9188. WarehousingDetailId: warehouse.ID,
  9189. SystemTime: advice.SysRecordTime,
  9190. PatientId: advice.PatientId,
  9191. Remark: advice.Remark,
  9192. OperateTime: time.Now().Unix(),
  9193. }
  9194. CreateSelfDrugFlowOne(drugflow, tx)
  9195. AddSelfDrugCount(advice.DrugId, orgID, advice.PatientId, drugflow.Count, tx)
  9196. // 出库完成后,要将该批次库存清零
  9197. prescribingNumber_two_temp := deliver_number - stock_number
  9198. advice.CountUnit = drup.MinUnit
  9199. AutoSelfDrugDeliverInfoFourtyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, unit, tx)
  9200. }
  9201. return
  9202. }
  9203. func AutoSelfDrugDeliverInfoFourtyThree(orgID int64, prescribingNumber int64, warehouseout *models.XtSelfDrugWarehouseOut, drup *models.BaseDrugLib, advice *models.XtSelfDrugWarehouseOutInfo, unit string, tx *gorm.DB) (err error) {
  9204. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  9205. var deliver_number int64 = 0
  9206. var stock_number int64 = 0
  9207. var drug_price float64
  9208. if advice.CountUnit == drup.MaxUnit {
  9209. deliver_number = prescribingNumber * drup.MinNumber
  9210. } else {
  9211. deliver_number = prescribingNumber
  9212. }
  9213. if advice.CountUnit == drup.MaxUnit {
  9214. drug_price = drup.RetailPrice
  9215. }
  9216. fmt.Println(drug_price)
  9217. if advice.CountUnit == drup.MinUnit {
  9218. drug_price = drup.MinPrice
  9219. }
  9220. if advice.CountUnit != drup.MaxUnit || advice.CountUnit != drup.MinUnit {
  9221. drug_price = drup.RetailPrice
  9222. }
  9223. // 根据先进先出原则,查询最先入库的批次,进行出库
  9224. // 如果没有对应的库存,则报错
  9225. lastWarehouse, _ := FindSelfLastDrugWarehousingInfoByID(advice.DrugId, advice.PatientId, tx)
  9226. if lastWarehouse.MaxUnit != lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  9227. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  9228. var stockMax int64
  9229. var stockMin int64
  9230. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  9231. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  9232. ChangeSelfMaxNumber(lastWarehouse.ID, stockMax, tx)
  9233. UpdateSelfMinNumber(lastWarehouse.ID, stockMin, tx)
  9234. }
  9235. }
  9236. if lastWarehouse.MaxUnit == lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  9237. lastWarehouse.StockMaxNumber = lastWarehouse.StockMaxNumber + lastWarehouse.StockMinNumber
  9238. ChangeSelfMaxNumberOne(lastWarehouse.ID, lastWarehouse.StockMaxNumber, tx)
  9239. }
  9240. warehouse, err := FindSelfLastDrugWarehousingInfoByID(advice.DrugId, advice.PatientId, tx)
  9241. // 将该批次的剩余库存数量转换为拆零数量
  9242. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  9243. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  9244. }
  9245. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  9246. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  9247. }
  9248. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  9249. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  9250. }
  9251. if stock_number >= deliver_number {
  9252. var maxNumber int64 = 0
  9253. var minNumber int64 = 0
  9254. maxNumber = deliver_number / drup.MinNumber
  9255. minNumber = deliver_number % drup.MinNumber
  9256. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  9257. minNumber = maxNumber
  9258. }
  9259. if drup.MaxUnit != drup.MinUnit {
  9260. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  9261. return errors.New("库存数量不足")
  9262. }
  9263. }
  9264. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  9265. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  9266. }
  9267. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  9268. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  9269. }
  9270. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  9271. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  9272. }
  9273. if warehouse.StockMaxNumber < 0 {
  9274. warehouse.StockMaxNumber = 0
  9275. }
  9276. if warehouse.StockMinNumber < 0 {
  9277. warehouse.StockMinNumber = 0
  9278. }
  9279. warehouse.Mtime = time.Now().Unix()
  9280. if warehouse.StockMinNumber < minNumber {
  9281. //fmt.Println("金4")
  9282. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  9283. if warehouse.MaxUnit != warehouse.MinUnit {
  9284. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  9285. }
  9286. if minNumber == 2 && warehouse.MaxUnit == warehouse.MinUnit && warehouse.StockMinNumber == 0 {
  9287. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  9288. }
  9289. } else {
  9290. //fmt.Println("金5")
  9291. if minNumber > 0 {
  9292. if (warehouse.StockMinNumber - minNumber) >= 0 {
  9293. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  9294. }
  9295. }
  9296. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  9297. //fmt.Println("6")
  9298. if warehouse.StockMinNumber > 0 {
  9299. if warehouse.StockMinNumber-deliver_number >= 0 {
  9300. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  9301. }
  9302. }
  9303. }
  9304. }
  9305. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit && warehouse.MaxUnit != warehouse.MinUnit {
  9306. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  9307. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  9308. }
  9309. }
  9310. if drup.MaxUnit != drup.MinUnit {
  9311. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  9312. return errors.New("库存数量不足")
  9313. }
  9314. }
  9315. if warehouse.StockMinNumber <= 0 {
  9316. warehouse.StockMinNumber = 0
  9317. }
  9318. errThree := UpDateSelfDrugWarehouseInfoByStock(&warehouse, tx)
  9319. if errThree != nil {
  9320. return errors.New("扣减库存失败")
  9321. }
  9322. //查询该药品剩余库存
  9323. infolist, _ := FindSelfDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId, tx)
  9324. var over_count int64
  9325. for _, its := range infolist {
  9326. if its.MaxUnit == drup.MaxUnit {
  9327. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  9328. }
  9329. over_count += its.StockMaxNumber + its.StockMinNumber
  9330. }
  9331. //drugWareInfo, _ := GetSelfLastWarehouseOutInfoById(advice.DrugId, tx)
  9332. drugflow := models.XtSelfDrugFlow{
  9333. WarehouseOutId: warehouseout.ID,
  9334. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  9335. DrugId: advice.DrugId,
  9336. Number: warehouse.Number,
  9337. ProductDate: advice.ProductDate,
  9338. ExpireDate: advice.ExpiryDate,
  9339. Count: deliver_number,
  9340. Price: warehouse.RetailPrice,
  9341. Status: 1,
  9342. Ctime: advice.SysRecordTime,
  9343. Mtime: time.Now().Unix(),
  9344. UserOrgId: advice.OrgId,
  9345. Manufacturer: advice.Manufacturer,
  9346. Dealer: advice.Dealer,
  9347. BatchNumber: warehouse.BatchNumber,
  9348. MaxUnit: drup.MinUnit,
  9349. ConsumableType: 2,
  9350. IsEdit: 1,
  9351. Creator: 0,
  9352. IsSys: 0,
  9353. WarehouseOutDetailId: advice.ID,
  9354. AdviceId: advice.ID,
  9355. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9356. StorehouseId: advice.StorehouseId,
  9357. AdminUserId: advice.AdminUserId,
  9358. LastPrice: warehouse.Price,
  9359. StockCount: advice.StockCount,
  9360. WarehousingDetailId: warehouse.ID,
  9361. OverCount: over_count,
  9362. RetailPrice: drug_price, //手动出库出库价格
  9363. WarehousingId: warehouse.ID,
  9364. SystemTime: advice.SysRecordTime,
  9365. PatientId: advice.PatientId,
  9366. Remark: advice.Remark,
  9367. OperateTime: time.Now().Unix(),
  9368. }
  9369. if warehouse.RetailPrice == 0 {
  9370. drugflow.Price = advice.Price
  9371. }
  9372. CreateSelfDrugFlowOne(drugflow, tx)
  9373. //更新出库id
  9374. UpdateSelfDrugWarehouseOutInfo(advice.ID, warehouse.ID, advice.OrgId, over_count, drug_price, tx)
  9375. //出库数量相加
  9376. AddSelfDrugCount(advice.DrugId, orgID, advice.PatientId, drugflow.Count, tx)
  9377. return nil
  9378. } else {
  9379. stock_number_int := strconv.FormatInt(stock_number, 10)
  9380. stock_number_int_count, _ := strconv.ParseFloat(stock_number_int, 64)
  9381. drug_price = stock_number_int_count * advice.Price
  9382. //扣减库存
  9383. warehouse.StockMaxNumber = 0
  9384. warehouse.StockMinNumber = 0
  9385. warehouse.Mtime = time.Now().Unix()
  9386. errThree := UpDateSelfDrugWarehouseInfoByStock(&warehouse, tx)
  9387. if errThree != nil {
  9388. return errThree
  9389. }
  9390. //查询该药品剩余库存
  9391. infolist, _ := FindDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId)
  9392. var over_count int64
  9393. for _, its := range infolist {
  9394. if its.MaxUnit == drup.MaxUnit {
  9395. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  9396. }
  9397. over_count += its.StockMaxNumber + its.StockMinNumber
  9398. }
  9399. //查询今日该药品该患者是否有出库数据
  9400. lastDrugOutInfo, _ := GetSelfNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.SysRecordTime, advice.OrgId, advice.AdviceId, warehouse.ID, tx)
  9401. if lastDrugOutInfo.ID == 0 {
  9402. AddSelfSigleDrugWarehouseOutInfo(advice, tx)
  9403. }
  9404. if lastDrugOutInfo.ID > 0 {
  9405. //删除在新增
  9406. UpdateSelfDrugWarehouseOutInfoTen(lastDrugOutInfo.PatientId, lastDrugOutInfo.SysRecordTime, advice.ID, tx)
  9407. AddSelfSigleDrugWarehouseOutInfo(advice, tx)
  9408. }
  9409. //drugWareInfo, _ := GetSelfLastWarehouseOutInfoById(advice.DrugId, tx)
  9410. drugflow := models.XtSelfDrugFlow{
  9411. WarehouseOutId: warehouseout.ID,
  9412. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  9413. DrugId: advice.DrugId,
  9414. Number: warehouse.Number,
  9415. ProductDate: advice.ProductDate,
  9416. ExpireDate: advice.ExpiryDate,
  9417. Count: stock_number,
  9418. Price: warehouse.RetailPrice,
  9419. Status: 1,
  9420. Ctime: advice.SysRecordTime,
  9421. Mtime: time.Now().Unix(),
  9422. UserOrgId: advice.OrgId,
  9423. Manufacturer: advice.Manufacturer,
  9424. Dealer: advice.Dealer,
  9425. BatchNumber: warehouse.BatchNumber,
  9426. MaxUnit: drup.MinUnit,
  9427. ConsumableType: 2,
  9428. IsEdit: 1,
  9429. Creator: 0,
  9430. IsSys: 0,
  9431. WarehouseOutDetailId: advice.ID,
  9432. AdviceId: advice.ID,
  9433. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9434. StorehouseId: advice.StorehouseId,
  9435. AdminUserId: advice.AdminUserId,
  9436. LastPrice: warehouse.Price,
  9437. StockCount: advice.StockCount,
  9438. OverCount: over_count,
  9439. RetailPrice: drug_price, //手动出库出库价格
  9440. WarehousingId: warehouse.ID,
  9441. WarehousingDetailId: warehouse.ID,
  9442. SystemTime: advice.SysRecordTime,
  9443. PatientId: advice.PatientId,
  9444. Remark: advice.Remark,
  9445. OperateTime: time.Now().Unix(),
  9446. }
  9447. CreateSelfDrugFlowOne(drugflow, tx)
  9448. AddSelfDrugCount(advice.DrugId, orgID, advice.PatientId, drugflow.Count, tx)
  9449. // 出库完成后,要将该批次库存清零
  9450. prescribingNumber_two_temp := deliver_number - stock_number
  9451. advice.CountUnit = drup.MinUnit
  9452. AutoSelfDrugDeliverInfoFourtyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, unit, tx)
  9453. }
  9454. return
  9455. }
  9456. // 耗材手动出库
  9457. func ConsumablesNewDeliveryThirty(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64, tx *gorm.DB) (err error) {
  9458. var deliver_number int64 = 0
  9459. var stock_number int64 = 0
  9460. var maxNumber int64 = 0
  9461. deliver_number = goods.Count
  9462. // 根据先进先出原则,查询最先入库的批次,进行出库
  9463. // 如果没有对应的库存,则报错
  9464. warehouse, err := FindNewFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId, tx)
  9465. stock_number = warehouse.StockCount
  9466. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  9467. if stock_number >= deliver_number {
  9468. maxNumber = goods.Count
  9469. warehouse.StockCount = warehouse.StockCount - maxNumber
  9470. warehouse.Mtime = time.Now().Unix()
  9471. //扣减库存
  9472. UpDateNewWarehouseInfoByStock(&warehouse, tx)
  9473. //查询剩余库存
  9474. goodList, _ := GetNewAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId, tx)
  9475. var sum_count int64
  9476. for _, item := range goodList {
  9477. sum_count += item.StockCount
  9478. }
  9479. warehouseOutInfo := &models.WarehouseOutInfo{
  9480. WarehouseInfotId: warehouse.ID,
  9481. }
  9482. //添加入库单详情ID
  9483. UpdatedNewSigleWarehouseOutInfo(goods.ID, warehouseOutInfo, tx)
  9484. stockFlow := models.VmStockFlow{
  9485. WarehousingId: warehouse.ID,
  9486. GoodId: goods.GoodId,
  9487. Number: warehouse.Number,
  9488. LicenseNumber: goods.LicenseNumber,
  9489. Count: goods.Count,
  9490. UserOrgId: goods.OrgId,
  9491. PatientId: goods.PatientId,
  9492. SystemTime: record_time,
  9493. ConsumableType: 2,
  9494. IsSys: goods.IsSys,
  9495. WarehousingOrder: warehouse.WarehousingOrder,
  9496. WarehouseOutId: warehouseOut.ID,
  9497. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  9498. IsEdit: 1,
  9499. CancelStockId: 0,
  9500. CancelOrderNumber: "",
  9501. Manufacturer: goods.Manufacturer,
  9502. Dealer: goods.Dealer,
  9503. Creator: creator,
  9504. UpdateCreator: 0,
  9505. Status: 1,
  9506. Ctime: record_time,
  9507. Mtime: time.Now().Unix(),
  9508. Price: warehouse.PackingPrice, //零售价
  9509. WarehousingDetailId: warehouse.ID,
  9510. WarehouseOutDetailId: goods.ID,
  9511. CancelOutDetailId: 0,
  9512. ProductDate: goods.ProductDate,
  9513. ExpireDate: goods.ExpiryDate,
  9514. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9515. StorehouseId: warehouse.StorehouseId,
  9516. AdminUserId: goods.AdminUserId,
  9517. BuyPrice: warehouse.Price, //进货价
  9518. StockCount: goods.StockCount,
  9519. BatchNumberCount: warehouse.StockCount - goods.Count,
  9520. IsCheck: 1,
  9521. OverCount: sum_count,
  9522. RegisterNumber: goods.RegisterNumber,
  9523. Remark: goods.Remark,
  9524. }
  9525. //创建出库流水
  9526. CreateNewStockFlowOne(stockFlow, tx)
  9527. //添加入库ID
  9528. UpdateNewGoodWarehouseOutById(goods.ID, warehouse.ID, goods.OrgId, sum_count, tx)
  9529. //更新出库数量
  9530. ModifyNewGoodSumCount(goods.StorehouseId, goods.Count, goods.OrgId, goods.GoodId, tx)
  9531. if warehouse.StockCount < maxNumber {
  9532. tx.Rollback()
  9533. return errors.New("库存数量不足")
  9534. }
  9535. return nil
  9536. } else {
  9537. // 出库完成后,要将该批次库存清零
  9538. warehouse.StockCount = 0
  9539. warehouse.Mtime = time.Now().Unix()
  9540. UpDateNewWarehouseInfoByStock(&warehouse, tx)
  9541. //查询剩余库存
  9542. goodList, _ := GetNewAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId, tx)
  9543. var sum_count int64
  9544. for _, item := range goodList {
  9545. sum_count += item.StockCount
  9546. }
  9547. stockFlow := models.VmStockFlow{
  9548. WarehousingId: warehouse.ID,
  9549. GoodId: goods.GoodId,
  9550. Number: warehouse.Number,
  9551. LicenseNumber: goods.LicenseNumber,
  9552. Count: stock_number,
  9553. UserOrgId: goods.OrgId,
  9554. PatientId: goods.PatientId,
  9555. SystemTime: record_time,
  9556. ConsumableType: 2,
  9557. IsSys: goods.IsSys,
  9558. WarehousingOrder: warehouse.WarehousingOrder,
  9559. WarehouseOutId: warehouseOut.ID,
  9560. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  9561. IsEdit: 1,
  9562. CancelStockId: 0,
  9563. CancelOrderNumber: "",
  9564. Manufacturer: goods.Manufacturer,
  9565. Dealer: goods.Dealer,
  9566. Creator: creator,
  9567. UpdateCreator: 0,
  9568. Status: 1,
  9569. Ctime: record_time,
  9570. Mtime: time.Now().Unix(),
  9571. Price: warehouse.PackingPrice, //零售价
  9572. WarehousingDetailId: warehouse.ID,
  9573. WarehouseOutDetailId: goods.ID,
  9574. CancelOutDetailId: 0,
  9575. ProductDate: goods.ProductDate,
  9576. ExpireDate: goods.ExpiryDate,
  9577. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9578. StorehouseId: warehouse.StorehouseId,
  9579. AdminUserId: goods.AdminUserId,
  9580. BuyPrice: warehouse.Price, //进货价
  9581. StockCount: goods.StockCount,
  9582. BatchNumberCount: 0,
  9583. IsCheck: 1,
  9584. OverCount: sum_count,
  9585. RegisterNumber: goods.RegisterNumber,
  9586. Remark: goods.Remark,
  9587. }
  9588. //创建出库流水
  9589. CreateNewStockFlowOne(stockFlow, tx)
  9590. UpdateNewGoodWarehouseOutById(goods.ID, warehouse.ID, goods.OrgId, sum_count, tx)
  9591. //更新出库数量
  9592. ModifyNewGoodSumCount(goods.StorehouseId, stock_number, goods.OrgId, goods.GoodId, tx)
  9593. goods.Count = deliver_number - stock_number
  9594. ConsumablesNewDeliveryThirty(orgID, record_time, goods, warehouseOut, count, creator, tx)
  9595. }
  9596. return nil
  9597. }
  9598. // 药品手动出库 递归方式
  9599. func AutoNewDrugDeliverInfoFourtyOne(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo, unit string, tx *gorm.DB) (err error) {
  9600. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  9601. var deliver_number int64 = 0
  9602. var stock_number int64 = 0
  9603. var drug_price float64
  9604. if advice.CountUnit == drup.MaxUnit {
  9605. deliver_number = prescribingNumber * drup.MinNumber
  9606. } else {
  9607. deliver_number = prescribingNumber
  9608. }
  9609. if advice.CountUnit == drup.MaxUnit {
  9610. drug_price = drup.RetailPrice
  9611. }
  9612. fmt.Println(drug_price)
  9613. if advice.CountUnit == drup.MinUnit {
  9614. drug_price = drup.MinPrice
  9615. }
  9616. if advice.CountUnit != drup.MaxUnit || advice.CountUnit != drup.MinUnit {
  9617. drug_price = drup.RetailPrice
  9618. }
  9619. // 根据先进先出原则,查询最先入库的批次,进行出库
  9620. // 如果没有对应的库存,则报错
  9621. lastWarehouse, _ := FindNewLastDrugWarehousingInfoByID(advice.DrugId, advice.StorehouseId, tx)
  9622. if lastWarehouse.MaxUnit != lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  9623. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  9624. var stockMax int64
  9625. var stockMin int64
  9626. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  9627. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  9628. ChangeNewMaxNumberOne(lastWarehouse.ID, stockMax, tx)
  9629. UpdateNewMinNumber(lastWarehouse.ID, stockMin, tx)
  9630. }
  9631. }
  9632. if lastWarehouse.MaxUnit == lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  9633. lastWarehouse.StockMaxNumber = lastWarehouse.StockMaxNumber + lastWarehouse.StockMinNumber
  9634. ChangeNewMaxNumberOne(lastWarehouse.ID, lastWarehouse.StockMaxNumber, tx)
  9635. }
  9636. warehouse, err := FindNewLastDrugWarehousingInfoByID(advice.DrugId, advice.StorehouseId, tx)
  9637. // 将该批次的剩余库存数量转换为拆零数量
  9638. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  9639. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  9640. }
  9641. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  9642. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  9643. }
  9644. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  9645. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  9646. }
  9647. if stock_number >= deliver_number {
  9648. var maxNumber int64 = 0
  9649. var minNumber int64 = 0
  9650. maxNumber = deliver_number / drup.MinNumber
  9651. minNumber = deliver_number % drup.MinNumber
  9652. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  9653. minNumber = maxNumber
  9654. }
  9655. if drup.MaxUnit != drup.MinUnit {
  9656. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  9657. return errors.New("库存数量不足")
  9658. }
  9659. }
  9660. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  9661. //fmt.Println("金1")
  9662. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  9663. }
  9664. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  9665. //fmt.Println("金2")
  9666. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  9667. }
  9668. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  9669. //fmt.Println("金3")
  9670. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  9671. }
  9672. if warehouse.StockMaxNumber < 0 {
  9673. warehouse.StockMaxNumber = 0
  9674. }
  9675. if warehouse.StockMinNumber < 0 {
  9676. warehouse.StockMinNumber = 0
  9677. }
  9678. warehouse.Mtime = time.Now().Unix()
  9679. if warehouse.StockMinNumber < minNumber {
  9680. //fmt.Println("金4")
  9681. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  9682. if warehouse.MaxUnit != warehouse.MinUnit {
  9683. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  9684. }
  9685. if minNumber == 2 && warehouse.MaxUnit == warehouse.MinUnit && warehouse.StockMinNumber == 0 {
  9686. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  9687. }
  9688. } else {
  9689. //fmt.Println("金5")
  9690. if minNumber > 0 {
  9691. if (warehouse.StockMinNumber - minNumber) >= 0 {
  9692. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  9693. }
  9694. }
  9695. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  9696. //fmt.Println("6")
  9697. if warehouse.StockMinNumber > 0 {
  9698. if warehouse.StockMinNumber-deliver_number >= 0 {
  9699. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  9700. }
  9701. }
  9702. }
  9703. }
  9704. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit && warehouse.MaxUnit != warehouse.MinUnit {
  9705. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  9706. //fmt.Println("金7")
  9707. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  9708. }
  9709. }
  9710. if drup.MaxUnit != drup.MinUnit {
  9711. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  9712. return errors.New("库存数量不足")
  9713. }
  9714. }
  9715. if warehouse.StockMinNumber <= 0 {
  9716. warehouse.StockMinNumber = 0
  9717. }
  9718. errThree := UpDateNewDrugWarehouseInfoByStock(&warehouse, tx)
  9719. if errThree != nil {
  9720. return errors.New("扣减库存失败")
  9721. }
  9722. //查询该药品剩余库存
  9723. infolist, _ := FindNewDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId, tx)
  9724. var over_count int64
  9725. for _, its := range infolist {
  9726. if its.MaxUnit == drup.MaxUnit {
  9727. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  9728. }
  9729. over_count += its.StockMaxNumber + its.StockMinNumber
  9730. }
  9731. drugWareInfo, _ := GetLastNewWarehouseOutInfoById(advice.DrugId, tx)
  9732. drugflow := models.DrugFlow{
  9733. WarehouseOutId: warehouseout.ID,
  9734. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  9735. DrugId: advice.DrugId,
  9736. Number: warehouse.Number,
  9737. ProductDate: advice.ProductDate,
  9738. ExpireDate: advice.ExpiryDate,
  9739. Count: deliver_number,
  9740. Price: warehouse.RetailPrice,
  9741. Status: 1,
  9742. Ctime: advice.SysRecordTime,
  9743. Mtime: time.Now().Unix(),
  9744. UserOrgId: advice.OrgId,
  9745. Manufacturer: advice.Manufacturer,
  9746. Dealer: advice.Dealer,
  9747. BatchNumber: warehouse.BatchNumber,
  9748. MaxUnit: drup.MinUnit,
  9749. ConsumableType: 2,
  9750. IsEdit: 1,
  9751. Creator: 0,
  9752. IsSys: 0,
  9753. WarehouseOutDetailId: drugWareInfo.ID,
  9754. AdviceId: advice.ID,
  9755. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9756. StorehouseId: advice.StorehouseId,
  9757. AdminUserId: advice.AdminUserId,
  9758. LastPrice: warehouse.Price,
  9759. StockCount: advice.StockCount,
  9760. WarehousingDetailId: warehouse.ID,
  9761. OverCount: over_count,
  9762. RetailPrice: drug_price, //手动出库出库价格
  9763. WarehousingId: warehouse.ID,
  9764. SystemTime: advice.SysRecordTime,
  9765. PatientId: advice.PatientId,
  9766. Remark: advice.Remark,
  9767. }
  9768. if warehouse.RetailPrice == 0 {
  9769. drugflow.Price = advice.Price
  9770. }
  9771. CreateDrugFlowOne(drugflow)
  9772. //更新出库id
  9773. UpdateNewDrugWarehouseOutInfo(advice.ID, warehouse.ID, advice.OrgId, over_count, drug_price, tx)
  9774. return nil
  9775. } else {
  9776. stock_number_int := strconv.FormatInt(stock_number, 10)
  9777. stock_number_int_count, _ := strconv.ParseFloat(stock_number_int, 64)
  9778. drug_price = stock_number_int_count * advice.Price
  9779. //扣减库存
  9780. warehouse.StockMaxNumber = 0
  9781. warehouse.StockMinNumber = 0
  9782. warehouse.Mtime = time.Now().Unix()
  9783. errThree := UpDateNewDrugWarehouseInfoByStock(&warehouse, tx)
  9784. if errThree != nil {
  9785. return errThree
  9786. }
  9787. //查询该药品剩余库存
  9788. infolist, _ := FindNewDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId, tx)
  9789. var over_count int64
  9790. for _, its := range infolist {
  9791. if its.MaxUnit == drup.MaxUnit {
  9792. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  9793. }
  9794. over_count += its.StockMaxNumber + its.StockMinNumber
  9795. }
  9796. drugWareInfo, _ := GetLastNewWarehouseOutInfoById(advice.DrugId, tx)
  9797. drugflow := models.DrugFlow{
  9798. WarehouseOutId: warehouseout.ID,
  9799. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  9800. DrugId: advice.DrugId,
  9801. Number: warehouse.Number,
  9802. ProductDate: advice.ProductDate,
  9803. ExpireDate: advice.ExpiryDate,
  9804. Count: stock_number,
  9805. Price: warehouse.RetailPrice,
  9806. Status: 1,
  9807. Ctime: advice.SysRecordTime,
  9808. Mtime: time.Now().Unix(),
  9809. UserOrgId: advice.OrgId,
  9810. Manufacturer: advice.Manufacturer,
  9811. Dealer: advice.Dealer,
  9812. BatchNumber: warehouse.BatchNumber,
  9813. MaxUnit: drup.MinUnit,
  9814. ConsumableType: 2,
  9815. IsEdit: 1,
  9816. Creator: 0,
  9817. IsSys: 0,
  9818. WarehouseOutDetailId: drugWareInfo.ID,
  9819. AdviceId: advice.ID,
  9820. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9821. StorehouseId: advice.StorehouseId,
  9822. AdminUserId: advice.AdminUserId,
  9823. LastPrice: warehouse.Price,
  9824. StockCount: advice.StockCount,
  9825. OverCount: over_count,
  9826. RetailPrice: drug_price, //手动出库出库价格
  9827. WarehousingId: warehouse.ID,
  9828. WarehousingDetailId: warehouse.ID,
  9829. SystemTime: advice.SysRecordTime,
  9830. PatientId: advice.PatientId,
  9831. Remark: advice.Remark,
  9832. }
  9833. CreateNewDrugFlowOne(drugflow, tx)
  9834. //更新出库id
  9835. UpdateNewDrugWarehouseOutInfo(advice.ID, warehouse.ID, advice.OrgId, over_count, drug_price, tx)
  9836. // 出库完成后,要将该批次库存清零
  9837. prescribingNumber_two_temp := deliver_number - stock_number
  9838. advice.CountUnit = drup.MinUnit
  9839. AutoNewDrugDeliverInfoFourtyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, unit, tx)
  9840. }
  9841. return
  9842. }