warhouse_service.go 374KB

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