warhouse_service.go 393KB

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