warhouse_service.go 269KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702
  1. package service
  2. import (
  3. "XT_New/models"
  4. "XT_New/utils"
  5. "errors"
  6. "fmt"
  7. _ "fmt"
  8. "github.com/jinzhu/gorm"
  9. "math"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // 药品出库
  15. func DrugsDelivery(orgID int64, creater int64, advice *models.DoctorAdvice) (err error) {
  16. // 1.判断药品是否来自专用字典的药品库
  17. // 2.判断当天当前机构有没有创建出库单,没有则创建
  18. // 3.创建出库流程
  19. // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  20. isHasWay := false
  21. record_time := int64(0)
  22. if advice.Way == 1 {
  23. isHasWay = true
  24. record_time = advice.RecordDate
  25. }
  26. if isHasWay {
  27. //判断当天当前机构有没有创建出库单,没有则创建
  28. out, err := FindDrugStockOutByIsSys(orgID, 1, record_time)
  29. if err == gorm.ErrRecordNotFound {
  30. timeStr := time.Now().Format("2006-01-02")
  31. timeArr := strings.Split(timeStr, "-")
  32. total, _ := FindAllDrugWarehouseOut(orgID)
  33. total = total + 1
  34. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  35. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  36. number = number + total
  37. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  38. storeConfig, _ := GetAllStoreHouseConfig(orgID)
  39. warehouseOut := models.DrugWarehouseOut{
  40. WarehouseOutOrderNumber: warehousing_out_order,
  41. OperationTime: time.Now().Unix(),
  42. OrgId: orgID,
  43. Creater: creater,
  44. Ctime: time.Now().Unix(),
  45. Status: 1,
  46. WarehouseOutTime: record_time,
  47. Dealer: 0,
  48. Manufacturer: 0,
  49. Type: 1,
  50. IsSys: 1,
  51. StorehouseId: storeConfig.DrugStorehouseOut,
  52. IsCheck: 1,
  53. }
  54. err := AddSigleDrugWarehouseOut(&warehouseOut)
  55. if err != nil {
  56. utils.TraceLog("创建出库单失败 err = %v", err)
  57. return err
  58. } else {
  59. out = warehouseOut
  60. }
  61. }
  62. // 出库流程
  63. // 1.查询该药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  64. drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
  65. if drup.ID > 0 {
  66. prescribingNumber := advice.PrescribingNumber
  67. DrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice)
  68. } else {
  69. return errors.New("药品信息不存在")
  70. }
  71. }
  72. return
  73. }
  74. // 药品自动出库 递归方式
  75. func DrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DoctorAdvice) (err error) {
  76. 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. }
  308. CreateDrugFlowOne(drugflow)
  309. //查询是否存在数据
  310. details := &models.DrugAutomaticReduceDetail{
  311. WarehouseOutId: warehouseout.ID,
  312. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  313. PatientId: advice.PatientId,
  314. Ctime: time.Now().Unix(),
  315. Mtime: time.Now().Unix(),
  316. Status: 1,
  317. RecordTime: advice.RecordDate,
  318. OrgId: orgID,
  319. DrugId: advice.DrugId,
  320. Count: deliver_number,
  321. CountUnit: drup.MinUnit,
  322. WarehouseInfoId: warehouse.ID,
  323. StorehouseId: storeConfig.DrugStorehouseOut,
  324. AdviceId: advice.ID,
  325. }
  326. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  327. if errTwo != nil {
  328. return errTwo
  329. }
  330. var out_count int64
  331. //查询出库数据
  332. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  333. for _, item := range infoCountList {
  334. out_count += item.Count
  335. }
  336. //出库数量相加
  337. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  338. return nil
  339. } else {
  340. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  341. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  342. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  343. WarehouseOutId: warehouseout.ID,
  344. Status: 1,
  345. Ctime: time.Now().Unix(),
  346. Remark: warehouse.Remark,
  347. OrgId: orgID,
  348. Type: 1,
  349. Manufacturer: warehouse.Manufacturer,
  350. Dealer: warehouse.Dealer,
  351. IsSys: 1,
  352. SysRecordTime: advice.RecordDate,
  353. DrugId: advice.DrugId,
  354. Number: warehouse.Number,
  355. BatchNumber: warehouse.BatchNumber,
  356. Price: warehouse.RetailPrice,
  357. CountUnit: drup.MinUnit,
  358. ProductDate: warehouse.ProductDate,
  359. ExpiryDate: warehouse.ExpiryDate,
  360. PatientId: advice.PatientId,
  361. Count: stock_number,
  362. WarehouseInfoId: warehouse.ID,
  363. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  364. StorehouseId: storeConfig.DrugStorehouseOut,
  365. IsCheck: 1,
  366. AdviceId: advice.ID,
  367. }
  368. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  369. if errOne != nil {
  370. return errOne
  371. }
  372. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  373. details := &models.DrugAutomaticReduceDetail{
  374. WarehouseOutId: warehouseout.ID,
  375. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  376. PatientId: advice.PatientId,
  377. Ctime: time.Now().Unix(),
  378. Mtime: time.Now().Unix(),
  379. Status: 1,
  380. RecordTime: advice.RecordDate,
  381. OrgId: orgID,
  382. DrugId: advice.DrugId,
  383. Count: stock_number,
  384. CountUnit: drup.MinUnit,
  385. WarehouseInfoId: warehouse.ID,
  386. StorehouseId: storeConfig.DrugStorehouseOut,
  387. AdviceId: advice.ID,
  388. }
  389. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  390. if errTwo != nil {
  391. return errTwo
  392. }
  393. info := models.XtDrugWarehouseInfo{
  394. ID: warehouse.ID,
  395. WarehousingId: warehouse.WarehousingId,
  396. DrugId: warehouse.DrugId,
  397. Number: warehouse.Number,
  398. ProductDate: warehouse.ProductDate,
  399. ExpiryDate: warehouse.ExpiryDate,
  400. WarehousingCount: warehouse.WarehousingCount,
  401. Price: warehouse.RetailPrice,
  402. TotalPrice: warehouse.TotalPrice,
  403. Dealer: warehouse.Dealer,
  404. Manufacturer: warehouse.Manufacturer,
  405. Remark: warehouse.Remark,
  406. Ctime: warehouse.Ctime,
  407. Mtime: warehouse.Mtime,
  408. Status: 1,
  409. OrgId: warehouse.OrgId,
  410. IsReturn: warehouse.IsReturn,
  411. WarehousingOrder: warehouse.WarehousingOrder,
  412. Type: warehouse.Type,
  413. RetailPrice: warehouse.RetailPrice,
  414. RetailTotalPrice: warehouse.RetailPrice,
  415. StockMaxNumber: 0,
  416. StockMinNumber: 0,
  417. BatchNumber: warehouse.BatchNumber,
  418. MaxUnit: warehouse.MaxUnit,
  419. WarehousingInfoId: warehouse.WarehousingInfoId,
  420. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  421. StorehouseId: storeConfig.DrugStorehouseOut,
  422. IsCheck: 1,
  423. }
  424. //扣减库存
  425. errThree := UpDateDrugWarehouseInfoByStock(&info)
  426. if errThree != nil {
  427. return errThree
  428. }
  429. //查询剩余库存
  430. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  431. var sum_count int64
  432. for _, its := range stockInfo {
  433. if its.MaxUnit == drup.MaxUnit {
  434. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  435. }
  436. sum_count += its.StockMaxNumber + its.StockMinNumber
  437. }
  438. var out_count int64
  439. //查询出库数据
  440. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  441. for _, item := range infoCountList {
  442. out_count += item.Count
  443. }
  444. drugflow := models.DrugFlow{
  445. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  446. WarehouseOutId: warehouseout.ID,
  447. DrugId: advice.DrugId,
  448. Number: warehouse.Number,
  449. ProductDate: warehouse.ProductDate,
  450. ExpireDate: warehouse.ExpiryDate,
  451. Count: stock_number,
  452. Price: warehouse.RetailPrice,
  453. Status: 1,
  454. Ctime: time.Now().Unix(),
  455. UserOrgId: orgID,
  456. Manufacturer: warehouse.Manufacturer,
  457. Dealer: warehouse.Dealer,
  458. BatchNumber: warehouse.BatchNumber,
  459. MaxUnit: drup.MinUnit,
  460. ConsumableType: 3,
  461. IsEdit: 1,
  462. Creator: advice.ExecutionStaff,
  463. IsSys: 1,
  464. PatientId: advice.PatientId,
  465. WarehousingDetailId: warehouse.ID,
  466. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  467. StorehouseId: storeConfig.DrugStorehouseOut,
  468. WarehouseOutDetailId: lastDrugOutInfo.ID,
  469. LastPrice: warehouse.Price,
  470. OverCount: sum_count,
  471. AdviceId: advice.ID,
  472. }
  473. CreateDrugFlowOne(drugflow)
  474. //出库数量相加
  475. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  476. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  477. prescribingNumber_two_temp := deliver_number - stock_number
  478. overPlusNumber := float64(prescribingNumber_two_temp)
  479. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  480. advice.PrescribingNumberUnit = drup.MinUnit
  481. DrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  482. }
  483. return
  484. }
  485. // 药品出库
  486. func HisDrugsDelivery(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo) (err error) {
  487. // 1.判断药品是否来自专用字典的药品库
  488. // 2.判断当天当前机构有没有创建出库单,没有则创建
  489. // 3.创建出库流程
  490. // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  491. isHasWay := false
  492. record_time := int64(0)
  493. isHasWay = true
  494. record_time = advice.RecordDate
  495. if isHasWay {
  496. //判断当天当前机构有没有创建出库单,没有则创建
  497. out, err := FindDrugStockOutByIsSys(orgID, 1, record_time)
  498. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  499. if err == gorm.ErrRecordNotFound {
  500. timeStr := time.Now().Format("2006-01-02")
  501. timeArr := strings.Split(timeStr, "-")
  502. total, _ := FindAllDrugWarehouseOut(orgID)
  503. total = total + 1
  504. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  505. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  506. number = number + total
  507. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  508. warehouseOut := models.DrugWarehouseOut{
  509. WarehouseOutOrderNumber: warehousing_out_order,
  510. OperationTime: time.Now().Unix(),
  511. OrgId: orgID,
  512. Creater: creater,
  513. Ctime: time.Now().Unix(),
  514. Status: 1,
  515. WarehouseOutTime: record_time,
  516. Dealer: 0,
  517. Manufacturer: 0,
  518. Type: 1,
  519. IsSys: 1,
  520. StorehouseId: houseConfig.DrugStorehouseOut,
  521. IsCheck: 1,
  522. }
  523. err := AddSigleDrugWarehouseOut(&warehouseOut)
  524. if err != nil {
  525. utils.TraceLog("创建出库单失败 err = %v", err)
  526. return err
  527. } else {
  528. out = warehouseOut
  529. }
  530. }
  531. // 出库流程
  532. // 1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  533. drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
  534. if drup.ID > 0 {
  535. prescribingNumber := advice.PrescribingNumber
  536. BloodHisDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice, prescribingNumber, advice.PrescribingNumberUnit)
  537. ////查询今日出库的数据
  538. //if orgID == 10188 || orgID == 10217 || orgID == 10344 {
  539. // //查询这个患者今日用过的总数量
  540. // listDrugFlow, _ := GetDrugFlowByWatch(advice.PatientId, advice.DrugId, advice.AdviceDate, advice.UserOrgId)
  541. // for _, item := range listDrugFlow {
  542. //
  543. // outInfor, _ := GetDrugWarehouseOutInfor(item.WarehouseOutDetailId, item.UserOrgId)
  544. // if item.MaxUnit == outInfor.CountUnit {
  545. // if item.Count != outInfor.Count {
  546. // UpdateDrugWarehouseOutInfoById(outInfor.ID, item.Count)
  547. // }
  548. // }
  549. //
  550. // }
  551. //}
  552. } else {
  553. return errors.New("药品信息不存在")
  554. }
  555. }
  556. return
  557. }
  558. func GetDrugFlowByWatch(patient_id int64, drug_id int64, advice_date int64, user_org_id int64) (list []*models.DrugFlow, err error) {
  559. 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
  560. return list, err
  561. }
  562. func GetDrugWarehouseOutInfor(id int64, user_org_id int64) (models.DrugWarehouseOutInfo, error) {
  563. outInfo := models.DrugWarehouseOutInfo{}
  564. err := XTReadDB().Where("id = ? and org_id = ?", id, user_org_id).Find(&outInfo).Error
  565. return outInfo, err
  566. }
  567. func UpdateDrugWarehouseOutInfoById(id int64, count int64) error {
  568. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"count": count}).Error
  569. return err
  570. }
  571. // 药品出库 递归方式
  572. func HisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  573. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  574. var deliver_number int64 = 0
  575. var stock_number int64 = 0
  576. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  577. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  578. if advice.PrescribingNumberUnit == drup.MaxUnit {
  579. deliver_number = count * drup.MinNumber
  580. } else {
  581. deliver_number = count
  582. }
  583. var min_price float64
  584. if advice.PrescribingNumberUnit == drup.MaxUnit {
  585. min_price = drup.RetailPrice
  586. }
  587. if advice.PrescribingNumberUnit == drup.MinUnit {
  588. min_price = drup.MinPrice
  589. }
  590. if advice.PrescribingNumberUnit != drup.MaxUnit && advice.PrescribingNumberUnit != drup.MinUnit {
  591. min_price = drup.RetailPrice
  592. }
  593. fmt.Println(min_price)
  594. // 根据先进先出原则,查询最先入库的批次,进行出库
  595. // 如果没有对应的库存,则报错
  596. //查询默认仓库
  597. storeConfig, _ := GetAllStoreHouseConfig(orgID)
  598. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  599. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  600. var stockMax int64
  601. var stockMin int64
  602. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  603. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  604. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  605. UpdateMinNumber(lastWarehouse.ID, stockMin)
  606. }
  607. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  608. if err != nil {
  609. return err
  610. }
  611. // 将该批次的剩余库存数量转换为拆零数量
  612. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  613. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  614. if stock_number >= deliver_number {
  615. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  616. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  617. WarehouseOutId: warehouseout.ID,
  618. Status: 1,
  619. Ctime: time.Now().Unix(),
  620. Remark: warehouse.Remark,
  621. OrgId: orgID,
  622. Type: 1,
  623. Manufacturer: warehouse.Manufacturer,
  624. Dealer: warehouse.Dealer,
  625. IsSys: 1,
  626. SysRecordTime: advice.RecordDate,
  627. DrugId: advice.DrugId,
  628. Number: warehouse.Number,
  629. BatchNumber: warehouse.BatchNumber,
  630. Price: warehouse.RetailPrice,
  631. CountUnit: drup.MinUnit,
  632. RetailPrice: warehouse.RetailPrice,
  633. ProductDate: warehouse.ProductDate,
  634. ExpiryDate: warehouse.ExpiryDate,
  635. PatientId: advice.PatientId,
  636. WarehouseInfoId: warehouse.ID,
  637. Count: deliver_number,
  638. AdviceId: advice.ID,
  639. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  640. StorehouseId: storeConfig.DrugStorehouseOut,
  641. IsCheck: 1,
  642. LastPrice: warehouse.Price,
  643. }
  644. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  645. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  646. if errOne != nil {
  647. return errOne
  648. }
  649. drugflow := models.DrugFlow{
  650. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  651. WarehouseOutId: warehouseout.ID,
  652. DrugId: advice.DrugId,
  653. Number: warehouse.Number,
  654. ProductDate: warehouse.ProductDate,
  655. ExpireDate: warehouse.ExpiryDate,
  656. Count: deliver_number,
  657. Price: warehouse.RetailPrice,
  658. Status: 1,
  659. Ctime: time.Now().Unix(),
  660. UserOrgId: orgID,
  661. Manufacturer: warehouse.Manufacturer,
  662. Dealer: warehouse.Dealer,
  663. BatchNumber: warehouse.BatchNumber,
  664. MaxUnit: drup.MinUnit,
  665. ConsumableType: 3,
  666. IsEdit: 1,
  667. Creator: advice.ExecutionStaff,
  668. IsSys: 1,
  669. PatientId: advice.PatientId,
  670. SystemTime: advice.AdviceDate,
  671. WarehousingDetailId: warehouse.ID,
  672. AdviceId: advice.ID,
  673. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  674. StorehouseId: storeConfig.DrugStorehouseOut,
  675. WarehouseOutDetailId: lastDrugOutInfo.ID,
  676. LastPrice: warehouse.Price,
  677. }
  678. CreateDrugFlowOne(drugflow)
  679. details := &models.DrugAutomaticReduceDetail{
  680. WarehouseOutId: warehouseout.ID,
  681. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  682. PatientId: advice.PatientId,
  683. Ctime: time.Now().Unix(),
  684. Mtime: time.Now().Unix(),
  685. Status: 1,
  686. RecordTime: advice.RecordDate,
  687. OrgId: orgID,
  688. DrugId: advice.DrugId,
  689. Count: deliver_number,
  690. CountUnit: drup.MinUnit,
  691. WarehouseInfoId: warehouse.ID,
  692. AdviceId: advice.ID,
  693. StorehouseId: storeConfig.DrugStorehouseOut,
  694. }
  695. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  696. if errTwo != nil {
  697. return errTwo
  698. }
  699. // 出库完成后,要减去对应批次的库存数量
  700. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  701. var maxNumber int64 = 0
  702. var minNumber int64 = 0
  703. maxNumber = deliver_number / drup.MinNumber
  704. minNumber = deliver_number % drup.MinNumber
  705. fmt.Println("maxNumber2323233232323232323232", maxNumber)
  706. fmt.Println("minNumber2323233232323232323232", minNumber)
  707. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  708. minNumber = maxNumber
  709. }
  710. if drup.MaxUnit != drup.MinUnit {
  711. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  712. return errors.New("库存数量不足")
  713. }
  714. }
  715. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  716. if warehouse.StockMaxNumber < 0 {
  717. warehouse.StockMaxNumber = 0
  718. }
  719. if warehouse.StockMinNumber < 0 {
  720. warehouse.StockMinNumber = 0
  721. }
  722. warehouse.Mtime = time.Now().Unix()
  723. fmt.Println("warehouse.StockMinNumber", warehouse.StockMinNumber)
  724. fmt.Println("minNumber", minNumber)
  725. fmt.Println("maxNumber", maxNumber)
  726. if warehouse.StockMinNumber < minNumber {
  727. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  728. if warehouse.MaxUnit != warehouse.MinUnit {
  729. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  730. }
  731. } else {
  732. if minNumber > 0 {
  733. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  734. }
  735. }
  736. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  737. if (warehouse.StockMinNumber - deliver_number) > 0 {
  738. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  739. }
  740. }
  741. if drup.MaxUnit != drup.MinUnit {
  742. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  743. return errors.New("库存数量不足")
  744. }
  745. }
  746. if warehouse.StockMinNumber <= 0 {
  747. warehouse.StockMinNumber = 0
  748. }
  749. //扣减库存
  750. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  751. if errThree != nil {
  752. return errThree
  753. }
  754. drugInfoList, _ := FindDrugWarehouseInfoList(advice.DrugId, orgID)
  755. var drug_max_number int64
  756. var drug_min_number int64
  757. for _, item := range drugInfoList {
  758. drug_max_number += item.StockMaxNumber
  759. drug_min_number += item.StockMinNumber
  760. }
  761. drugFlushInfo := models.XtDrugWarehouseFlushInfo{
  762. DrugMaxNumber: drug_max_number,
  763. UserOrgId: orgID,
  764. WarehouseOutId: warehouseout.ID,
  765. Type: 1,
  766. SystemTime: advice.RecordDate,
  767. PatientId: advice.PatientId,
  768. Ctime: time.Now().Unix(),
  769. Mtime: 0,
  770. BatchNumberId: warehouse.ID,
  771. DrugId: advice.DrugId,
  772. Count: deliver_number,
  773. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  774. Creater: warehouseout.Creater,
  775. DrugMinNumber: drug_min_number,
  776. Unit: drup.MinUnit,
  777. AdviceId: advice.ID,
  778. Status: 1,
  779. WarehouseOutDetailId: lastDrugOutInfo.ID,
  780. }
  781. CreatedDrugFlushInfo(drugFlushInfo)
  782. return nil
  783. } else {
  784. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  785. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  786. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  787. WarehouseOutId: warehouseout.ID,
  788. Status: 1,
  789. Ctime: time.Now().Unix(),
  790. Remark: warehouse.Remark,
  791. OrgId: orgID,
  792. Type: 1,
  793. Manufacturer: warehouse.Manufacturer,
  794. Dealer: warehouse.Dealer,
  795. IsSys: 1,
  796. SysRecordTime: advice.RecordDate,
  797. DrugId: advice.DrugId,
  798. Number: warehouse.Number,
  799. BatchNumber: warehouse.BatchNumber,
  800. Price: warehouse.RetailPrice,
  801. RetailPrice: warehouse.RetailPrice,
  802. ProductDate: warehouse.ProductDate,
  803. ExpiryDate: warehouse.ExpiryDate,
  804. PatientId: advice.PatientId,
  805. WarehouseInfoId: warehouse.ID,
  806. Count: stock_number,
  807. CountUnit: drup.MinUnit,
  808. AdviceId: advice.ID,
  809. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  810. StorehouseId: storeConfig.DrugStorehouseOut,
  811. IsCheck: 1,
  812. }
  813. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  814. if errOne != nil {
  815. return errOne
  816. }
  817. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  818. drugflow := models.DrugFlow{
  819. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  820. WarehouseOutId: warehouseout.ID,
  821. DrugId: advice.DrugId,
  822. Number: warehouse.Number,
  823. ProductDate: warehouse.ProductDate,
  824. ExpireDate: warehouse.ExpiryDate,
  825. Count: stock_number,
  826. Price: warehouse.RetailPrice,
  827. Status: 1,
  828. Ctime: time.Now().Unix(),
  829. UserOrgId: orgID,
  830. Manufacturer: warehouse.Manufacturer,
  831. Dealer: warehouse.Dealer,
  832. BatchNumber: warehouse.BatchNumber,
  833. MaxUnit: drup.MinUnit,
  834. ConsumableType: 3,
  835. IsEdit: 1,
  836. Creator: advice.ExecutionStaff,
  837. IsSys: 1,
  838. PatientId: advice.PatientId,
  839. SystemTime: advice.AdviceDate,
  840. WarehousingDetailId: warehouse.ID,
  841. AdviceId: advice.ID,
  842. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  843. StorehouseId: storeConfig.DrugStorehouseOut,
  844. WarehouseOutDetailId: lastDrugOutInfo.ID,
  845. LastPrice: warehouse.Price,
  846. }
  847. CreateDrugFlowOne(drugflow)
  848. //查询是否存在数据
  849. details := &models.DrugAutomaticReduceDetail{
  850. WarehouseOutId: warehouseout.ID,
  851. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  852. PatientId: advice.PatientId,
  853. Ctime: time.Now().Unix(),
  854. Mtime: time.Now().Unix(),
  855. Status: 1,
  856. RecordTime: advice.RecordDate,
  857. OrgId: orgID,
  858. DrugId: advice.DrugId,
  859. Count: stock_number,
  860. CountUnit: drup.MinUnit,
  861. WarehouseInfoId: warehouse.ID,
  862. AdviceId: advice.ID,
  863. StorehouseId: storeConfig.DrugStorehouseOut,
  864. }
  865. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  866. if errTwo != nil {
  867. return errTwo
  868. }
  869. info := models.XtDrugWarehouseInfo{
  870. ID: warehouse.ID,
  871. WarehousingId: warehouse.WarehousingId,
  872. DrugId: warehouse.DrugId,
  873. Number: warehouse.Number,
  874. ProductDate: warehouse.ProductDate,
  875. ExpiryDate: warehouse.ExpiryDate,
  876. WarehousingCount: warehouse.WarehousingCount,
  877. Price: warehouse.Price,
  878. TotalPrice: warehouse.TotalPrice,
  879. Dealer: warehouse.Dealer,
  880. Manufacturer: warehouse.Manufacturer,
  881. Remark: warehouse.Remark,
  882. Ctime: warehouse.Ctime,
  883. Mtime: warehouse.Mtime,
  884. Status: 1,
  885. OrgId: warehouse.OrgId,
  886. IsReturn: warehouse.IsReturn,
  887. WarehousingOrder: warehouse.WarehousingOrder,
  888. Type: warehouse.Type,
  889. RetailPrice: warehouse.RetailPrice,
  890. RetailTotalPrice: warehouse.RetailPrice,
  891. StockMaxNumber: 0,
  892. StockMinNumber: 0,
  893. BatchNumber: warehouse.BatchNumber,
  894. MaxUnit: warehouse.MaxUnit,
  895. MinUnit: warehouse.MinUnit,
  896. WarehousingInfoId: warehouse.WarehousingInfoId,
  897. StorehouseId: storeConfig.DrugStorehouseOut,
  898. IsCheck: 1,
  899. }
  900. errThree := UpDateDrugWarehouseInfoByStock(&info)
  901. if errThree != nil {
  902. return errThree
  903. }
  904. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  905. prescribingNumber_two_temp := deliver_number - stock_number
  906. overPlusNumber := float64(prescribingNumber_two_temp)
  907. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  908. advice.PrescribingNumberUnit = drup.MinUnit
  909. HisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  910. }
  911. return
  912. }
  913. // 药品出库 递归方式
  914. func AutoDrugDeliverInfo(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  915. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  916. var deliver_number int64 = 0
  917. var stock_number int64 = 0
  918. if advice.CountUnit == drup.MaxUnit {
  919. deliver_number = prescribingNumber * drup.MinNumber
  920. } else {
  921. deliver_number = prescribingNumber
  922. }
  923. // 根据先进先出原则,查询最先入库的批次,进行出库
  924. // 如果没有对应的库存,则报错
  925. //开启事物
  926. storeConfig, _ := GetAllStoreHouseConfig(orgID)
  927. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  928. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  929. var stockMax int64
  930. var stockMin int64
  931. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  932. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  933. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  934. UpdateMinNumber(lastWarehouse.ID, stockMin)
  935. }
  936. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  937. if err != nil {
  938. return err
  939. }
  940. // 将该批次的剩余库存数量转换为拆零数量
  941. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  942. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  943. if stock_number >= deliver_number {
  944. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  945. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  946. WarehouseOutId: warehouseout.ID,
  947. Status: 1,
  948. Ctime: time.Now().Unix(),
  949. Remark: advice.Remark,
  950. OrgId: orgID,
  951. Type: 1,
  952. Manufacturer: advice.Manufacturer,
  953. Dealer: warehouse.Dealer,
  954. IsSys: 0,
  955. SysRecordTime: advice.Ctime,
  956. DrugId: advice.DrugId,
  957. ExpiryDate: advice.ExpiryDate,
  958. ProductDate: advice.ProductDate,
  959. Number: advice.Number,
  960. BatchNumber: warehouse.BatchNumber,
  961. Count: deliver_number,
  962. RetailPrice: advice.RetailPrice,
  963. Price: warehouse.RetailPrice,
  964. WarehouseInfoId: warehouse.ID,
  965. CountUnit: drup.MinUnit,
  966. AdviceId: advice.ID,
  967. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  968. StorehouseId: advice.StorehouseId,
  969. AdminUserId: advice.AdminUserId,
  970. IsCheck: 1,
  971. }
  972. warehouseOutInfo.Count = prescribingNumber
  973. warehouseOutInfo.CountUnit = advice.CountUnit
  974. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  975. if errOne != nil {
  976. return errOne
  977. }
  978. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  979. drugflow := models.DrugFlow{
  980. WarehouseOutId: warehouseout.ID,
  981. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  982. DrugId: advice.DrugId,
  983. Number: warehouse.Number,
  984. ProductDate: advice.ProductDate,
  985. ExpireDate: advice.ExpiryDate,
  986. Count: deliver_number,
  987. Price: warehouse.RetailPrice,
  988. Status: 1,
  989. Ctime: time.Now().Unix(),
  990. UserOrgId: advice.OrgId,
  991. Manufacturer: advice.Manufacturer,
  992. Dealer: advice.Dealer,
  993. BatchNumber: warehouse.BatchNumber,
  994. MaxUnit: drup.MinUnit,
  995. ConsumableType: 2,
  996. IsEdit: 1,
  997. Creator: 0,
  998. IsSys: 0,
  999. WarehouseOutDetailId: drugWareInfo.ID,
  1000. AdviceId: advice.ID,
  1001. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1002. StorehouseId: advice.StorehouseId,
  1003. AdminUserId: advice.AdminUserId,
  1004. }
  1005. CreateDrugFlowOne(drugflow)
  1006. // 出库完成后,要减去对应批次的库存数量
  1007. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  1008. var maxNumber int64 = 0
  1009. var minNumber int64 = 0
  1010. if advice.CountUnit == drup.MinUnit {
  1011. maxNumber = prescribingNumber / drup.MinNumber
  1012. minNumber = prescribingNumber % drup.MinNumber
  1013. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  1014. minNumber = maxNumber
  1015. }
  1016. } else {
  1017. maxNumber = prescribingNumber
  1018. }
  1019. if warehouse.StockMaxNumber < maxNumber {
  1020. return errors.New("库存数量不足")
  1021. }
  1022. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  1023. warehouse.Mtime = time.Now().Unix()
  1024. if warehouse.StockMinNumber < minNumber {
  1025. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  1026. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  1027. } else {
  1028. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  1029. }
  1030. if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
  1031. warehouse.StockMinNumber = 0
  1032. }
  1033. if warehouse.StockMaxNumber < 0 {
  1034. return errors.New("库存数量不足")
  1035. }
  1036. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  1037. if errThree != nil {
  1038. return errThree
  1039. }
  1040. //查询默认仓库
  1041. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  1042. //查询默认仓库剩余多少库存
  1043. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  1044. var sum_count int64
  1045. var sum_in_count int64
  1046. for _, it := range list {
  1047. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  1048. if it.MaxUnit == baseDrug.MaxUnit {
  1049. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  1050. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  1051. }
  1052. sum_count += it.StockMaxNumber + it.StockMinNumber
  1053. sum_in_count += it.WarehousingCount
  1054. }
  1055. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  1056. return nil
  1057. } else {
  1058. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  1059. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1060. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1061. WarehouseOutId: warehouseout.ID,
  1062. Status: 1,
  1063. Ctime: time.Now().Unix(),
  1064. Remark: advice.Remark,
  1065. OrgId: orgID,
  1066. Type: 1,
  1067. Manufacturer: advice.Manufacturer,
  1068. Dealer: warehouse.Dealer,
  1069. IsSys: 0,
  1070. SysRecordTime: advice.Ctime,
  1071. DrugId: advice.DrugId,
  1072. ExpiryDate: warehouse.ExpiryDate,
  1073. ProductDate: warehouse.ProductDate,
  1074. Number: warehouse.Number,
  1075. BatchNumber: warehouse.BatchNumber,
  1076. Count: stock_number,
  1077. RetailPrice: advice.RetailPrice,
  1078. Price: warehouse.RetailPrice,
  1079. WarehouseInfoId: warehouse.ID,
  1080. CountUnit: drup.MinUnit,
  1081. AdviceId: advice.ID,
  1082. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1083. StorehouseId: advice.StorehouseId,
  1084. AdminUserId: advice.AdminUserId,
  1085. IsCheck: 1,
  1086. }
  1087. warehouseOutInfo.Count = stock_number
  1088. //warehouseOutInfo.CountUnit = advice.CountUnit
  1089. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1090. if errOne != nil {
  1091. return errOne
  1092. }
  1093. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  1094. drugflow := models.DrugFlow{
  1095. WarehouseOutId: warehouseout.ID,
  1096. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1097. DrugId: advice.DrugId,
  1098. Number: warehouse.Number,
  1099. ProductDate: advice.ProductDate,
  1100. ExpireDate: advice.ExpiryDate,
  1101. Count: stock_number,
  1102. Price: warehouse.RetailPrice,
  1103. Status: 1,
  1104. Ctime: time.Now().Unix(),
  1105. UserOrgId: advice.OrgId,
  1106. Manufacturer: advice.Manufacturer,
  1107. Dealer: advice.Dealer,
  1108. BatchNumber: warehouse.BatchNumber,
  1109. MaxUnit: drup.MinUnit,
  1110. ConsumableType: 2,
  1111. IsEdit: 1,
  1112. Creator: 0,
  1113. IsSys: 0,
  1114. WarehouseOutDetailId: drugWareInfo.ID,
  1115. AdviceId: advice.ID,
  1116. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1117. StorehouseId: advice.StorehouseId,
  1118. AdminUserId: advice.AdminUserId,
  1119. }
  1120. CreateDrugFlowOne(drugflow)
  1121. // 出库完成后,要将该批次库存清零
  1122. //扣减库存
  1123. warehouse.StockMaxNumber = 0
  1124. warehouse.StockMinNumber = 0
  1125. warehouse.Mtime = time.Now().Unix()
  1126. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  1127. //查询默认仓库
  1128. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  1129. //查询默认仓库剩余多少库存
  1130. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  1131. var sum_count int64
  1132. var sum_in_count int64
  1133. for _, it := range list {
  1134. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  1135. if it.MaxUnit == baseDrug.MaxUnit {
  1136. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  1137. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  1138. sum_count += it.StockMaxNumber + it.StockMinNumber
  1139. sum_in_count += it.WarehousingCount
  1140. }
  1141. }
  1142. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  1143. if errThree != nil {
  1144. return errThree
  1145. }
  1146. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1147. prescribingNumber_two_temp := deliver_number - stock_number
  1148. advice.CountUnit = drup.MinUnit
  1149. AutoDrugDeliverInfo(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  1150. }
  1151. return
  1152. }
  1153. // 耗材出库
  1154. func ConsumablesDeliveryTotal(orgID int64, patient_id int64, record_time int64, goods []*models.DialysisBeforePrepareGoods, goodOne []*models.NewDialysisBeforePrepareGoods, creator int64) (err error) {
  1155. //查询该患者当天已经出库的耗材信息
  1156. goods_yc, _ := FindConsumablesByDateTwo(orgID, patient_id, record_time)
  1157. // 和新请求的出库数据进行对比,分出那些是继续出库的,那些是需要删除出库的
  1158. for i := len(goods_yc) - 1; i >= 0; i-- {
  1159. goods_yc_temp := goods_yc[i]
  1160. for j := len(goods) - 1; j >= 0; j-- {
  1161. goods_temp := goods[j]
  1162. // 已经出库和新请求出库都存在该耗材,则判断出库数量,分成是继续出库,还是删除出库
  1163. if goods_yc_temp.GoodTypeId == goods_temp.GoodTypeId && goods_yc_temp.GoodId == goods_temp.GoodId {
  1164. // 已经出库和新请求出库的出库数量一致,则清除两个结构体里的数据(既不出库,也不删除出库)
  1165. if goods_yc_temp.Count == goods_temp.Count {
  1166. goods_yc = append(goods_yc[:i], goods_yc[i+1:]...)
  1167. goods = append(goods[:j], goods[j+1:]...)
  1168. break
  1169. }
  1170. // 如果已经出库的数量 大于 新请求出库的数量,则代表需要删除出库
  1171. if goods_yc_temp.Count > goods_temp.Count {
  1172. temp_count := goods_yc_temp.Count - goods_temp.Count
  1173. goods_yc[i].Count = temp_count
  1174. goods = append(goods[:j], goods[j+1:]...)
  1175. break
  1176. }
  1177. // 如果已经出库的数量 小于 新请求出库的梳理,则代表需要增加出库
  1178. if goods_yc_temp.Count < goods_temp.Count {
  1179. temp_count := goods_temp.Count - goods_yc_temp.Count
  1180. goods[j].Count = temp_count
  1181. goods_yc = append(goods_yc[:i], goods_yc[i+1:]...)
  1182. break
  1183. }
  1184. }
  1185. }
  1186. }
  1187. // goods_yc 这个数据就是需要已经出库了,但是现在需要删除出库的耗材数据
  1188. // goods 这个数据就是需要出库的耗材的数据(新增的数据)
  1189. fmt.Println("len233333333333333w", len(goods_yc))
  1190. fmt.Println("需要出库的数据", len(goods))
  1191. if len(goods) > 0 {
  1192. out, err := FindStockOutByIsSys(orgID, 1, record_time)
  1193. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  1194. if err == gorm.ErrRecordNotFound {
  1195. //没有记录,则创建出库单
  1196. timeStr := time.Now().Format("2006-01-02")
  1197. timeArr := strings.Split(timeStr, "-")
  1198. total, _ := FindAllWarehouseOut(orgID)
  1199. total = total + 1
  1200. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  1201. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  1202. number = number + total
  1203. warehousing_out_order = "CKD" + strconv.FormatInt(number, 10)
  1204. warehouseOut := models.WarehouseOut{
  1205. WarehouseOutOrderNumber: warehousing_out_order,
  1206. OperationTime: time.Now().Unix(),
  1207. OrgId: orgID,
  1208. Creater: creator,
  1209. Ctime: time.Now().Unix(),
  1210. Status: 1,
  1211. WarehouseOutTime: record_time,
  1212. Dealer: 0,
  1213. Manufacturer: 0,
  1214. Type: 1,
  1215. IsSys: 1,
  1216. StorehouseId: houseConfig.StorehouseOutInfo,
  1217. IsCheck: 1,
  1218. }
  1219. err := AddSigleWarehouseOut(&warehouseOut)
  1220. if err != nil {
  1221. utils.TraceLog("创建出库单失败 err = %v", err)
  1222. return err
  1223. } else {
  1224. out = warehouseOut
  1225. }
  1226. }
  1227. for _, item := range goods {
  1228. var newCount int64 = 0
  1229. for _, it := range goodOne {
  1230. if item.GoodTypeId == it.GoodTypeId && item.GoodId == it.GoodId {
  1231. newCount = it.Count
  1232. }
  1233. }
  1234. var cha_count int64
  1235. var cha_count_two int64
  1236. //查询已经出库的数量
  1237. flowGood, _ := GetStockFlowIsBatchNumberTwo(patient_id, record_time, item.GoodId)
  1238. var out_count int64
  1239. for _, item := range flowGood {
  1240. out_count += item.Count
  1241. }
  1242. //已经出库的数量 减掉目前需要出库的数据
  1243. cha_count = item.Count - out_count
  1244. cha_count_two = out_count - item.Count
  1245. prepare := models.DialysisBeforePrepare{
  1246. GoodTypeId: item.GoodTypeId,
  1247. GoodId: item.GoodId,
  1248. Count: cha_count,
  1249. ProjectId: item.ProjectId,
  1250. StorehouseId: houseConfig.StorehouseOutInfo,
  1251. }
  1252. ConsumablesGoodDelivery(orgID, patient_id, record_time, &prepare, &out, newCount)
  1253. //数量增加
  1254. if cha_count > 0 {
  1255. //出库数量累加
  1256. ModifyGoodSumCount(houseConfig.StorehouseOutInfo, cha_count, orgID, item.GoodId)
  1257. }
  1258. //已经出库的数据 大于 目前需要出库的数据
  1259. if cha_count_two > 0 {
  1260. ModifyAddGoodSumCount(houseConfig.StorehouseOutInfo, cha_count_two, orgID, item.GoodId)
  1261. //退库数量增加
  1262. UpdateSumAddCancelCount(orgID, item.GoodId, houseConfig.StorehouseOutInfo, cha_count_two)
  1263. //生成退库单
  1264. }
  1265. //更新剩余库存
  1266. goodListSix, _ := GetSumGoodList(orgID, houseConfig.StorehouseOutInfo, item.GoodId)
  1267. var flush_count int64
  1268. for _, it := range goodListSix {
  1269. flush_count += it.StockCount
  1270. }
  1271. UpdateSumGood(orgID, houseConfig.StorehouseOutInfo, item.GoodId, flush_count)
  1272. }
  1273. }
  1274. if len(goods_yc) > 0 {
  1275. for _, good_yc := range goods_yc {
  1276. out, _ := FindStockOutByIsSys(orgID, 1, record_time)
  1277. ConsumablesDeliveryDelete(orgID, patient_id, record_time, good_yc, &out)
  1278. }
  1279. }
  1280. return nil
  1281. }
  1282. // 耗材出库
  1283. func ConsumablesDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, count int64) (err error) {
  1284. //开事务
  1285. var deliver_number int64 = 0
  1286. var stock_number int64 = 0
  1287. var maxNumber int64 = 0
  1288. deliver_number = goods.Count
  1289. // 根据先进先出原则,查询最先入库的批次,进行出库
  1290. // 如果没有对应的库存,则报错
  1291. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  1292. if err != nil {
  1293. return errors.New("库存数量不足")
  1294. }
  1295. stock_number = warehouse.StockCount
  1296. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1297. if stock_number >= deliver_number {
  1298. maxNumber = goods.Count
  1299. //出库
  1300. warehouse.StockCount = warehouse.StockCount - maxNumber
  1301. warehouse.Mtime = time.Now().Unix()
  1302. if warehouse.StockCount < 0 {
  1303. return errors.New("库存数量不足")
  1304. }
  1305. errThree := UpDateWarehouseInfoByStock(&warehouse)
  1306. if errThree != nil {
  1307. return errThree
  1308. }
  1309. //查询剩余库存
  1310. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  1311. var sum_count int64
  1312. for _, item := range goodList {
  1313. sum_count += item.StockCount
  1314. }
  1315. warehouseOutInfo := &models.WarehouseOutInfo{
  1316. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1317. WarehouseOutId: warehouseOut.ID,
  1318. WarehouseInfotId: warehouse.ID,
  1319. Status: 1,
  1320. Ctime: record_time,
  1321. Mtime: time.Now().Unix(),
  1322. Remark: warehouse.Remark,
  1323. OrgId: orgID,
  1324. Type: 1,
  1325. Manufacturer: warehouse.Manufacturer,
  1326. Dealer: warehouse.Dealer,
  1327. IsSys: 1,
  1328. SysRecordTime: record_time,
  1329. GoodTypeId: goods.GoodTypeId,
  1330. GoodId: goods.GoodId,
  1331. PatientId: patient_id,
  1332. Number: warehouse.Number,
  1333. LicenseNumber: warehouse.LicenseNumber,
  1334. Price: warehouse.PackingPrice,
  1335. ExpiryDate: warehouse.ExpiryDate,
  1336. ProductDate: warehouse.ProductDate,
  1337. ProjectId: goods.ProjectId,
  1338. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1339. StorehouseId: goods.StorehouseId,
  1340. IsCheck: 1,
  1341. }
  1342. warehouseOutInfo.Count = count
  1343. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
  1344. if errcodes == gorm.ErrRecordNotFound {
  1345. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  1346. if errOne != nil {
  1347. return errOne
  1348. }
  1349. } else if errcodes == nil {
  1350. UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId)
  1351. }
  1352. details := models.BloodAutomaticReduceDetail{
  1353. WarehouseOutId: warehouseOutInfo.ID,
  1354. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  1355. PatientId: patient_id,
  1356. Ctime: record_time,
  1357. Mtime: time.Now().Unix(),
  1358. Status: 1,
  1359. RecordTime: record_time,
  1360. OrgId: orgID,
  1361. GoodId: goods.GoodId,
  1362. GoodTypeId: goods.GoodTypeId,
  1363. Count: count,
  1364. ProjectId: goods.ProjectId,
  1365. StorehouseId: goods.StorehouseId,
  1366. }
  1367. //查询当天耗材是否已经存在数据
  1368. _, errcode := GetAutoMaticReduceDetail(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  1369. if errcode == gorm.ErrRecordNotFound {
  1370. errTwo := CreateAutoReduceRecord(&details)
  1371. if errTwo != nil {
  1372. return errTwo
  1373. }
  1374. //插入库存流水表
  1375. flow := models.VmStockFlow{
  1376. WarehouseOutId: warehouseOut.ID,
  1377. WarehousingId: warehouse.ID,
  1378. GoodId: goods.GoodId,
  1379. Number: warehouse.Number,
  1380. ProductDate: warehouse.ProductDate,
  1381. ExpireDate: warehouse.ExpiryDate,
  1382. Count: deliver_number,
  1383. Price: warehouse.PackingPrice,
  1384. Status: 1,
  1385. Ctime: record_time,
  1386. Mtime: time.Now().Unix(),
  1387. UserOrgId: orgID,
  1388. Manufacturer: warehouse.Manufacturer,
  1389. Dealer: warehouse.Dealer,
  1390. LicenseNumber: warehouse.LicenseNumber,
  1391. IsEdit: 2,
  1392. Creator: warehouseOut.Creater,
  1393. SystemTime: record_time,
  1394. ConsumableType: 3,
  1395. WarehouseOutDetailId: warehouseOutInfo.ID,
  1396. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1397. IsSys: 1,
  1398. PatientId: patient_id,
  1399. ProjectId: goods.ProjectId,
  1400. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1401. StorehouseId: goods.StorehouseId,
  1402. OverCount: sum_count,
  1403. }
  1404. //查询流水是否存在
  1405. exsit, errflow := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  1406. if errflow == gorm.ErrRecordNotFound {
  1407. //创建数据
  1408. errThre := CreateStockFlowOne(flow)
  1409. if errThre != nil {
  1410. return errThre
  1411. }
  1412. } else if errflow == nil {
  1413. flow := models.VmStockFlow{
  1414. ID: exsit.ID,
  1415. WarehouseOutId: warehouseOut.ID,
  1416. WarehousingId: warehouse.ID,
  1417. GoodId: goods.GoodId,
  1418. Number: warehouse.Number,
  1419. ProductDate: warehouse.ProductDate,
  1420. ExpireDate: warehouse.ExpiryDate,
  1421. Count: exsit.Count + goods.Count,
  1422. Price: warehouse.PackingPrice,
  1423. Status: 1,
  1424. Ctime: record_time,
  1425. Mtime: time.Now().Unix(),
  1426. UserOrgId: orgID,
  1427. Manufacturer: warehouse.Manufacturer,
  1428. Dealer: warehouse.Dealer,
  1429. LicenseNumber: warehouse.LicenseNumber,
  1430. IsEdit: 2,
  1431. Creator: warehouseOut.Creater,
  1432. SystemTime: record_time,
  1433. ConsumableType: 3,
  1434. WarehouseOutDetailId: warehouseOutInfo.ID,
  1435. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1436. IsSys: 1,
  1437. PatientId: patient_id,
  1438. ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  1439. ProjectId: goods.ProjectId,
  1440. StorehouseId: goods.StorehouseId,
  1441. OverCount: sum_count,
  1442. }
  1443. errFour := UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  1444. if errFour != nil {
  1445. return errFour
  1446. }
  1447. }
  1448. } else if errcode == nil {
  1449. flow := models.VmStockFlow{
  1450. WarehouseOutId: warehouseOut.ID,
  1451. WarehousingId: warehouse.ID,
  1452. GoodId: goods.GoodId,
  1453. Number: warehouse.Number,
  1454. ProductDate: warehouse.ProductDate,
  1455. ExpireDate: warehouse.ExpiryDate,
  1456. Count: deliver_number,
  1457. Price: warehouse.PackingPrice,
  1458. Status: 1,
  1459. Ctime: record_time,
  1460. Mtime: time.Now().Unix(),
  1461. UserOrgId: orgID,
  1462. Manufacturer: warehouse.Manufacturer,
  1463. Dealer: warehouse.Dealer,
  1464. LicenseNumber: warehouse.LicenseNumber,
  1465. IsEdit: 2,
  1466. Creator: warehouseOut.Creater,
  1467. SystemTime: record_time,
  1468. ConsumableType: 3,
  1469. WarehouseOutDetailId: warehouseOutInfo.ID,
  1470. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1471. IsSys: 1,
  1472. PatientId: patient_id,
  1473. ProjectId: goods.ProjectId,
  1474. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1475. StorehouseId: goods.StorehouseId,
  1476. OverCount: sum_count,
  1477. }
  1478. //查询流水是否存在
  1479. exsit, errflow := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  1480. if errflow == gorm.ErrRecordNotFound {
  1481. CreateStockFlowOne(flow)
  1482. DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  1483. CreateAutoReduceRecord(&details)
  1484. } else if errflow == nil {
  1485. flow := models.VmStockFlow{
  1486. ID: exsit.ID,
  1487. WarehouseOutId: warehouseOut.ID,
  1488. WarehousingId: warehouse.ID,
  1489. GoodId: goods.GoodId,
  1490. Number: warehouse.Number,
  1491. ProductDate: warehouse.ProductDate,
  1492. ExpireDate: warehouse.ExpiryDate,
  1493. Count: exsit.Count + goods.Count,
  1494. Price: warehouse.PackingPrice,
  1495. Status: 1,
  1496. Ctime: record_time,
  1497. Mtime: time.Now().Unix(),
  1498. UserOrgId: orgID,
  1499. Manufacturer: warehouse.Manufacturer,
  1500. Dealer: warehouse.Dealer,
  1501. LicenseNumber: warehouse.LicenseNumber,
  1502. IsEdit: 2,
  1503. Creator: warehouseOut.Creater,
  1504. SystemTime: record_time,
  1505. ConsumableType: 3,
  1506. WarehouseOutDetailId: warehouseOutInfo.ID,
  1507. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1508. IsSys: 1,
  1509. PatientId: patient_id,
  1510. ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  1511. ProjectId: goods.ProjectId,
  1512. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1513. StorehouseId: goods.StorehouseId,
  1514. OverCount: sum_count,
  1515. }
  1516. UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  1517. DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  1518. CreateAutoReduceRecord(&details)
  1519. }
  1520. }
  1521. return nil
  1522. } else {
  1523. // 当刚批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  1524. warehouse.StockCount = 0
  1525. warehouse.Mtime = time.Now().Unix()
  1526. errThree := UpDateWarehouseInfoByStock(&warehouse)
  1527. if errThree != nil {
  1528. return errThree
  1529. }
  1530. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  1531. var sum_count int64
  1532. for _, item := range goodList {
  1533. sum_count += item.StockCount
  1534. }
  1535. warehouseOutInfo := &models.WarehouseOutInfo{
  1536. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1537. WarehouseOutId: warehouseOut.ID,
  1538. WarehouseInfotId: warehouse.ID,
  1539. Status: 1,
  1540. Ctime: record_time,
  1541. Mtime: time.Now().Unix(),
  1542. Remark: warehouse.Remark,
  1543. OrgId: orgID,
  1544. Type: 1,
  1545. Manufacturer: warehouse.Manufacturer,
  1546. Dealer: warehouse.Dealer,
  1547. IsSys: 1,
  1548. SysRecordTime: record_time,
  1549. GoodTypeId: goods.GoodTypeId,
  1550. GoodId: goods.GoodId,
  1551. PatientId: patient_id,
  1552. Number: warehouse.Number,
  1553. LicenseNumber: warehouse.LicenseNumber,
  1554. Price: warehouse.PackingPrice,
  1555. ExpiryDate: warehouse.ExpiryDate,
  1556. ProductDate: warehouse.ProductDate,
  1557. ProjectId: goods.ProjectId,
  1558. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1559. StorehouseId: goods.StorehouseId,
  1560. IsCheck: 1,
  1561. }
  1562. warehouseOutInfo.Count = stock_number
  1563. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, goods.PatientId, record_time, goods.ProjectId)
  1564. if errcodes == gorm.ErrRecordNotFound {
  1565. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  1566. if errOne != nil {
  1567. return errOne
  1568. }
  1569. //插入库存流水表
  1570. flow := models.VmStockFlow{
  1571. WarehouseOutId: warehouseOut.ID,
  1572. WarehousingId: warehouse.ID,
  1573. GoodId: goods.GoodId,
  1574. Number: warehouse.Number,
  1575. ProductDate: warehouse.ProductDate,
  1576. ExpireDate: warehouse.ExpiryDate,
  1577. Count: stock_number,
  1578. Price: warehouse.PackingPrice,
  1579. Status: 1,
  1580. Ctime: record_time,
  1581. Mtime: time.Now().Unix(),
  1582. UserOrgId: orgID,
  1583. Manufacturer: warehouse.Manufacturer,
  1584. Dealer: warehouse.Dealer,
  1585. LicenseNumber: warehouse.LicenseNumber,
  1586. IsEdit: 2,
  1587. Creator: warehouseOut.Creater,
  1588. SystemTime: record_time,
  1589. ConsumableType: 3,
  1590. WarehouseOutDetailId: warehouseOutInfo.ID,
  1591. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1592. IsSys: 1,
  1593. PatientId: patient_id,
  1594. ProjectId: goods.ProjectId,
  1595. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1596. StorehouseId: goods.StorehouseId,
  1597. OverCount: sum_count,
  1598. }
  1599. //查询流水是否存在
  1600. exsit, errflows := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  1601. if errflows == gorm.ErrRecordNotFound {
  1602. CreateStockFlowOne(flow)
  1603. } else if errflows == nil {
  1604. flow := models.VmStockFlow{
  1605. ID: exsit.ID,
  1606. WarehouseOutId: warehouseOut.ID,
  1607. WarehousingId: warehouse.ID,
  1608. GoodId: goods.GoodId,
  1609. Number: warehouse.Number,
  1610. ProductDate: warehouse.ProductDate,
  1611. ExpireDate: warehouse.ExpiryDate,
  1612. Count: exsit.Count + goods.Count,
  1613. Price: warehouse.PackingPrice,
  1614. Status: 1,
  1615. Ctime: record_time,
  1616. Mtime: time.Now().Unix(),
  1617. UserOrgId: orgID,
  1618. Manufacturer: warehouse.Manufacturer,
  1619. Dealer: warehouse.Dealer,
  1620. LicenseNumber: warehouse.LicenseNumber,
  1621. IsEdit: 2,
  1622. Creator: warehouseOut.Creater,
  1623. SystemTime: record_time,
  1624. ConsumableType: 3,
  1625. WarehouseOutDetailId: warehouseOutInfo.ID,
  1626. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1627. IsSys: 1,
  1628. PatientId: patient_id,
  1629. ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  1630. ProjectId: goods.ProjectId,
  1631. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1632. StorehouseId: goods.StorehouseId,
  1633. OverCount: sum_count,
  1634. }
  1635. errOne := UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  1636. if errOne == nil {
  1637. }
  1638. }
  1639. } else if errcodes == nil {
  1640. goods.Count = deliver_number - stock_number
  1641. //更新数量为 该批次剩余数量 + 还有未出的数量
  1642. warehouseOutInfo.Count = stock_number
  1643. UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, goods.PatientId, record_time, goods.ProjectId)
  1644. //插入库存流水表
  1645. flow := models.VmStockFlow{
  1646. WarehouseOutId: warehouseOut.ID,
  1647. WarehousingId: warehouse.ID,
  1648. GoodId: goods.GoodId,
  1649. Number: warehouse.Number,
  1650. ProductDate: warehouse.ProductDate,
  1651. ExpireDate: warehouse.ExpiryDate,
  1652. Count: stock_number,
  1653. Price: warehouse.PackingPrice,
  1654. Status: 1,
  1655. Ctime: record_time,
  1656. Mtime: time.Now().Unix(),
  1657. UserOrgId: orgID,
  1658. Manufacturer: warehouse.Manufacturer,
  1659. Dealer: warehouse.Dealer,
  1660. LicenseNumber: warehouse.LicenseNumber,
  1661. IsEdit: 2,
  1662. Creator: warehouseOut.Creater,
  1663. SystemTime: record_time,
  1664. ConsumableType: 3,
  1665. WarehouseOutDetailId: warehouseOutInfo.ID,
  1666. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1667. IsSys: 1,
  1668. PatientId: patient_id,
  1669. ProjectId: goods.ProjectId,
  1670. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1671. StorehouseId: goods.StorehouseId,
  1672. OverCount: sum_count,
  1673. }
  1674. //查询流水是否存在
  1675. exsit, errflows := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  1676. if errflows == gorm.ErrRecordNotFound {
  1677. CreateStockFlowOne(flow)
  1678. } else if errflows == nil {
  1679. flow := models.VmStockFlow{
  1680. ID: exsit.ID,
  1681. WarehouseOutId: warehouseOut.ID,
  1682. WarehousingId: warehouse.ID,
  1683. GoodId: goods.GoodId,
  1684. Number: warehouse.Number,
  1685. ProductDate: warehouse.ProductDate,
  1686. ExpireDate: warehouse.ExpiryDate,
  1687. Count: exsit.Count + goods.Count,
  1688. Price: warehouse.PackingPrice,
  1689. Status: 1,
  1690. Ctime: record_time,
  1691. Mtime: time.Now().Unix(),
  1692. UserOrgId: orgID,
  1693. Manufacturer: warehouse.Manufacturer,
  1694. Dealer: warehouse.Dealer,
  1695. LicenseNumber: warehouse.LicenseNumber,
  1696. IsEdit: 2,
  1697. Creator: warehouseOut.Creater,
  1698. SystemTime: record_time,
  1699. ConsumableType: 3,
  1700. WarehouseOutDetailId: warehouseOutInfo.ID,
  1701. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1702. IsSys: 1,
  1703. PatientId: patient_id,
  1704. ProjectId: goods.ProjectId,
  1705. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1706. StorehouseId: goods.StorehouseId,
  1707. OverCount: sum_count,
  1708. }
  1709. UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  1710. }
  1711. }
  1712. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1713. goods.Count = deliver_number - stock_number
  1714. ConsumablesDelivery(orgID, patient_id, record_time, goods, warehouseOut, count)
  1715. }
  1716. return nil
  1717. }
  1718. // 耗材出库删除
  1719. func ConsumablesDeliveryDelete(orgID int64, patient_id int64, record_time int64, good_yc *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut) (err error) {
  1720. // 先根据相关信息查询当天该耗材的出库信息
  1721. warehouseOutInfos, err := FindStockOutInfoByStock(orgID, good_yc.GoodTypeId, good_yc.GoodId, record_time, patient_id)
  1722. if err != nil {
  1723. return err
  1724. }
  1725. var delete_count int64 = 0
  1726. for _, ware := range warehouseOutInfos {
  1727. // 删除出库完成后,要增加对应批次的库存数量
  1728. errThree := UpDateWarehouseInfoByStockDelete(ware.WarehouseInfotId, delete_count, patient_id, record_time, good_yc.GoodId)
  1729. if errThree != nil {
  1730. return errThree
  1731. }
  1732. //查询剩余库存
  1733. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  1734. goodList, _ := GetAllGoodSumCount(good_yc.GoodId, orgID, houseConfig.StorehouseOutInfo)
  1735. var sum_count int64
  1736. for _, item := range goodList {
  1737. sum_count += item.StockCount
  1738. }
  1739. // 判断当前出库的数据和删除出库数量
  1740. if good_yc.Count <= ware.Count {
  1741. delete_count = good_yc.Count
  1742. } else {
  1743. delete_count = ware.Count
  1744. }
  1745. // 在出库记录表里记录退库详情
  1746. warehouseOutInfo := &models.WarehouseOutInfo{
  1747. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1748. WarehouseOutId: warehouseOut.ID,
  1749. //WarehouseInfoId: warehouse.ID,
  1750. Status: 1,
  1751. Ctime: record_time,
  1752. Mtime: time.Now().Unix(),
  1753. Remark: "",
  1754. OrgId: orgID,
  1755. Type: 1,
  1756. Manufacturer: 0,
  1757. Dealer: 0,
  1758. IsSys: 2,
  1759. SysRecordTime: record_time,
  1760. GoodTypeId: good_yc.GoodTypeId,
  1761. GoodId: good_yc.GoodId,
  1762. PatientId: patient_id,
  1763. StorehouseId: houseConfig.StorehouseOutInfo,
  1764. OverCount: sum_count,
  1765. }
  1766. warehouseOutInfo.Count = ware.Count - delete_count
  1767. stockInInfo, _ := FindLastStockInInfoRecord(good_yc.GoodId, orgID)
  1768. warehouseOutInfo.Price = stockInInfo.Price
  1769. errOne := UpdateAutoMaticReduceDetail(good_yc.GoodId, good_yc.GoodTypeId, record_time, patient_id, orgID, warehouseOutInfo)
  1770. if errOne != nil {
  1771. return errOne
  1772. } else {
  1773. details := &models.AutomaticReduceDetail{
  1774. WarehouseOutId: warehouseOutInfo.ID,
  1775. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  1776. PatientId: patient_id,
  1777. Ctime: record_time,
  1778. Mtime: time.Now().Unix(),
  1779. Status: 1,
  1780. RecordTime: record_time,
  1781. OrgId: orgID,
  1782. GoodId: good_yc.GoodId,
  1783. GoodTypeId: good_yc.GoodTypeId,
  1784. Count: warehouseOutInfo.Count,
  1785. Type: 2,
  1786. StorehouseId: houseConfig.StorehouseOutInfo,
  1787. }
  1788. //查询是否当天已经存在数据
  1789. _, errcode := GetAutoMaticReduceDetail(orgID, patient_id, record_time, good_yc.GoodId, good_yc.GoodTypeId)
  1790. if errcode == gorm.ErrRecordNotFound {
  1791. errTwo := AddSigleAutoReduceRecordInfo(details)
  1792. if errTwo != nil {
  1793. return errTwo
  1794. }
  1795. } else if errcode == nil {
  1796. DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, good_yc.GoodId, good_yc.GoodTypeId)
  1797. AddSigleAutoReduceRecordInfo(details)
  1798. }
  1799. }
  1800. //扣减数量相加
  1801. ModifyAddGoodSumCount(houseConfig.StorehouseOutInfo, delete_count, good_yc.UserOrgId, good_yc.GoodId)
  1802. //退库数量相加
  1803. UpdateSumAddCancelCount(good_yc.UserOrgId, good_yc.GoodId, houseConfig.StorehouseOutInfo, delete_count)
  1804. operation_time := time.Now().Unix()
  1805. //创建退库单
  1806. timeStr := time.Now().Format("2006-01-02")
  1807. timeArr := strings.Split(timeStr, "-")
  1808. total, _ := FindAllCancelStockTotal(good_yc.UserOrgId)
  1809. total = total + 1
  1810. orderNumber := "CKTKD" + strconv.FormatInt(good_yc.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  1811. cancelStock := models.CancelStock{
  1812. OrderNumber: orderNumber,
  1813. OperaTime: operation_time,
  1814. OrgId: good_yc.UserOrgId,
  1815. Creater: warehouseOut.Creater,
  1816. Ctime: time.Now().Unix(),
  1817. Status: 1,
  1818. ReturnTime: record_time,
  1819. Type: 1,
  1820. StorehouseId: houseConfig.StorehouseOutInfo,
  1821. IsCheck: 1,
  1822. }
  1823. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, good_yc.UserOrgId)
  1824. if msgerrkonde == gorm.ErrRecordNotFound {
  1825. AddSigleCancelStock(&cancelStock)
  1826. }
  1827. cancel, _ := GetLastCancelStockById(good_yc.UserOrgId)
  1828. manufacturer, _ := GetManufactureById(ware.Manufacturer)
  1829. deaerler, _ := GetDealerById(ware.Dealer)
  1830. cancelStockInfo := models.CancelStockInfo{
  1831. GoodId: ware.GoodId,
  1832. CancelStockId: cancel.ID,
  1833. GoodTypeId: ware.GoodTypeId,
  1834. Count: delete_count,
  1835. Price: ware.Price,
  1836. Total: 0,
  1837. ProductDate: ware.ProductDate,
  1838. ExpiryDate: ware.ExpiryDate,
  1839. Ctime: record_time,
  1840. Mtime: time.Now().Unix(),
  1841. Status: 1,
  1842. OrgId: good_yc.UserOrgId,
  1843. OrderNumber: cancel.OrderNumber,
  1844. Type: 0,
  1845. Dealer: deaerler.DealerName,
  1846. Manufacturer: manufacturer.ManufacturerName,
  1847. Number: ware.Number,
  1848. RegisterAccount: "",
  1849. Remark: "",
  1850. WarehouseInfoId: ware.WarehouseInfotId,
  1851. PatientId: ware.PatientId,
  1852. RecordDate: record_time,
  1853. StorehouseId: houseConfig.StorehouseOutInfo,
  1854. IsCheck: 1,
  1855. }
  1856. CreateCancelStockInfoOne(&cancelStockInfo)
  1857. cancelInfo, _ := GetLastCancelStockInfoByGoodId(ware.GoodId)
  1858. goodListOne, _ := GetSumGoodList(good_yc.UserOrgId, houseConfig.StorehouseOutInfo, good_yc.GoodId)
  1859. var over_count int64
  1860. for _, it := range goodListOne {
  1861. over_count += it.StockCount
  1862. }
  1863. flow := models.VmStockFlow{
  1864. WarehousingId: ware.WarehouseInfotId,
  1865. GoodId: ware.GoodId,
  1866. Number: ware.Number,
  1867. LicenseNumber: ware.LicenseNumber,
  1868. Count: ware.Count,
  1869. UserOrgId: ware.OrgId,
  1870. PatientId: ware.PatientId,
  1871. SystemTime: record_time,
  1872. ConsumableType: 7,
  1873. IsSys: 0,
  1874. WarehousingOrder: "",
  1875. WarehouseOutId: ware.WarehouseOutId,
  1876. WarehouseOutOrderNumber: ware.WarehouseOutOrderNumber,
  1877. IsEdit: 0,
  1878. CancelStockId: cancel.ID,
  1879. CancelOrderNumber: cancel.OrderNumber,
  1880. Manufacturer: manufacturer.ID,
  1881. Dealer: 0,
  1882. Creator: warehouseOut.Creater,
  1883. UpdateCreator: 0,
  1884. Status: 1,
  1885. Ctime: record_time,
  1886. Mtime: time.Now().Unix(),
  1887. Price: ware.Price,
  1888. WarehousingDetailId: ware.WarehouseInfotId,
  1889. WarehouseOutDetailId: ware.ID,
  1890. CancelOutDetailId: cancelInfo.ID,
  1891. ProductDate: ware.ProductDate,
  1892. ExpireDate: ware.ExpiryDate,
  1893. StorehouseId: houseConfig.StorehouseOutInfo,
  1894. OverCount: over_count,
  1895. }
  1896. CreateStockFlowOne(flow)
  1897. // 增加了对应的库存后,看看还有多少需要退库的
  1898. good_yc.Count = good_yc.Count - delete_count
  1899. if good_yc.Count == 0 {
  1900. return nil
  1901. }
  1902. }
  1903. if good_yc.Count == 0 {
  1904. return nil
  1905. } else {
  1906. return errors.New("退库和出库数据不匹配")
  1907. }
  1908. }
  1909. func GetAutoReduceRecordInfoByPatientId(orgid int64, patient_id int64, recordTime int64) (autoReduce []*models.AutomaticReduceDetail, err error) {
  1910. err = XTReadDB().Model(&autoReduce).Where("org_id = ? and patient_id = ? and record_time = ? and status = 1", orgid, patient_id, recordTime).Find(&autoReduce).Error
  1911. return autoReduce, err
  1912. }
  1913. func DeleteDialysisBefor(orgid int64, patient_id int64, record_date int64, goodid int64, goodtypeid int64) error {
  1914. prepare := models.DialysisBeforePrepare{}
  1915. 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
  1916. return err
  1917. }
  1918. func DeleteDialysisBeforThree(orgid int64, patient_id int64, record_date int64) error {
  1919. prepare := models.DialysisBeforePrepare{}
  1920. 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
  1921. return err
  1922. }
  1923. func GetWarehouseById(id int64) (models.WarehouseOut, error) {
  1924. out := models.WarehouseOut{}
  1925. err := XTReadDB().Where("id=? and status = 1", id).Find(&out).Error
  1926. return out, err
  1927. }
  1928. // 耗材出库
  1929. func ConsumablesDeliveryOne(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64) (err error) {
  1930. ////开事务
  1931. //tx := XTWriteDB().Begin()
  1932. var deliver_number int64 = 0
  1933. var stock_number int64 = 0
  1934. var maxNumber int64 = 0
  1935. deliver_number = goods.Count
  1936. // 根据先进先出原则,查询最先入库的批次,进行出库
  1937. // 如果没有对应的库存,则报错
  1938. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  1939. if err != nil {
  1940. return err
  1941. }
  1942. stock_number = warehouse.StockCount
  1943. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1944. if stock_number >= deliver_number {
  1945. warehouseOutInfo := &models.WarehouseOutInfo{
  1946. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1947. WarehouseOutId: warehouseOut.ID,
  1948. WarehouseInfotId: warehouse.ID,
  1949. Status: 1,
  1950. Ctime: time.Now().Unix(),
  1951. Remark: goods.Remark,
  1952. OrgId: orgID,
  1953. Type: 1,
  1954. Manufacturer: goods.Manufacturer,
  1955. Dealer: goods.Dealer,
  1956. IsSys: 0,
  1957. SysRecordTime: record_time,
  1958. GoodTypeId: goods.GoodTypeId,
  1959. GoodId: goods.GoodId,
  1960. ExpiryDate: goods.ExpiryDate,
  1961. ProductDate: goods.ProductDate,
  1962. Number: warehouse.Number,
  1963. Price: goods.Price,
  1964. LicenseNumber: goods.LicenseNumber,
  1965. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1966. StorehouseId: goods.StorehouseId,
  1967. AdminUserId: goods.AdminUserId,
  1968. BuyPrice: warehouse.Price,
  1969. StockCount: goods.StockCount,
  1970. }
  1971. warehouseOutInfo.Count = goods.Count
  1972. //添加出库单详情
  1973. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  1974. if errOne != nil {
  1975. return errOne
  1976. }
  1977. stockFlow := models.VmStockFlow{
  1978. WarehousingId: warehouse.ID,
  1979. GoodId: goods.GoodId,
  1980. Number: warehouse.Number,
  1981. LicenseNumber: goods.LicenseNumber,
  1982. Count: goods.Count,
  1983. UserOrgId: goods.OrgId,
  1984. PatientId: goods.PatientId,
  1985. SystemTime: record_time,
  1986. ConsumableType: 2,
  1987. IsSys: goods.IsSys,
  1988. WarehousingOrder: "",
  1989. WarehouseOutId: warehouseOut.ID,
  1990. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1991. IsEdit: 1,
  1992. CancelStockId: 0,
  1993. CancelOrderNumber: "",
  1994. Manufacturer: goods.Manufacturer,
  1995. Dealer: goods.Dealer,
  1996. Creator: creator,
  1997. UpdateCreator: 0,
  1998. Status: 1,
  1999. Ctime: time.Now().Unix(),
  2000. Mtime: 0,
  2001. Price: warehouse.PackingPrice,
  2002. WarehousingDetailId: 0,
  2003. WarehouseOutDetailId: warehouseOutInfo.ID,
  2004. CancelOutDetailId: 0,
  2005. ProductDate: goods.ProductDate,
  2006. ExpireDate: goods.ExpiryDate,
  2007. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2008. StorehouseId: warehouse.StorehouseId,
  2009. AdminUserId: goods.AdminUserId,
  2010. BuyPrice: warehouse.Price,
  2011. StockCount: goods.StockCount,
  2012. }
  2013. //创建出库流水
  2014. CreateStockFlowOne(stockFlow)
  2015. maxNumber = goods.Count
  2016. if warehouse.StockCount < maxNumber {
  2017. return errors.New("库存数量不足")
  2018. }
  2019. warehouse.StockCount = warehouse.StockCount - maxNumber
  2020. warehouse.Mtime = time.Now().Unix()
  2021. //扣减库存
  2022. errThree := UpDateWarehouseInfoByStock(&warehouse)
  2023. fmt.Println(errThree)
  2024. //if errThree != nil {
  2025. // utils.ErrorLog("事务失败,原因为: %v", errThree.Error())
  2026. // tx.Rollback()
  2027. //} else {
  2028. // tx.Commit()
  2029. //}
  2030. //查询该机构默认仓库
  2031. storeConfig, _ := GetAllStoreHouseConfig(goods.OrgId)
  2032. //查询剩余库存
  2033. goodList, _ := GetGoodSumCountByStoreId(storeConfig.StorehouseOutInfo, goods.GoodId, goods.OrgId)
  2034. var sum_count int64
  2035. var sum_in_count int64
  2036. for _, item := range goodList {
  2037. sum_count += item.StockCount
  2038. sum_in_count += item.WarehousingCount
  2039. }
  2040. UpdateGoodByGoodId(goods.GoodId, sum_count, sum_in_count, goods.OrgId)
  2041. if errThree != nil {
  2042. return errThree
  2043. }
  2044. } else {
  2045. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2046. warehouseOutInfo := &models.WarehouseOutInfo{
  2047. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2048. WarehouseOutId: warehouseOut.ID,
  2049. WarehouseInfotId: warehouse.ID,
  2050. Status: 1,
  2051. Ctime: time.Now().Unix(),
  2052. Remark: goods.Remark,
  2053. OrgId: orgID,
  2054. Type: 1,
  2055. Manufacturer: goods.Manufacturer,
  2056. Dealer: goods.Dealer,
  2057. IsSys: 0,
  2058. SysRecordTime: record_time,
  2059. GoodTypeId: goods.GoodTypeId,
  2060. GoodId: goods.GoodId,
  2061. ExpiryDate: goods.ExpiryDate,
  2062. ProductDate: goods.ProductDate,
  2063. Number: warehouse.Number,
  2064. Price: warehouse.PackingPrice,
  2065. LicenseNumber: goods.LicenseNumber,
  2066. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2067. StorehouseId: warehouse.StorehouseId,
  2068. AdminUserId: goods.AdminUserId,
  2069. BuyPrice: warehouse.Price,
  2070. StockCount: goods.StockCount,
  2071. IsCheck: 1,
  2072. }
  2073. warehouseOutInfo.Count = stock_number
  2074. //查询是否已经生成出库单
  2075. info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  2076. if errcodes == gorm.ErrRecordNotFound {
  2077. //创建出库单
  2078. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  2079. if errOne != nil {
  2080. return errOne
  2081. }
  2082. } else {
  2083. warehouseOutInfo.Count = info.Count + stock_number
  2084. UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  2085. }
  2086. stockFlow := models.VmStockFlow{
  2087. WarehousingId: warehouse.ID,
  2088. GoodId: goods.GoodId,
  2089. Number: warehouse.Number,
  2090. LicenseNumber: goods.LicenseNumber,
  2091. Count: stock_number,
  2092. UserOrgId: goods.OrgId,
  2093. PatientId: 0,
  2094. SystemTime: record_time,
  2095. ConsumableType: 2,
  2096. IsSys: 0,
  2097. WarehousingOrder: "",
  2098. WarehouseOutId: warehouseOut.ID,
  2099. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2100. IsEdit: 1,
  2101. CancelStockId: 0,
  2102. CancelOrderNumber: "",
  2103. Manufacturer: goods.Manufacturer,
  2104. Dealer: goods.Dealer,
  2105. Creator: creator,
  2106. UpdateCreator: 0,
  2107. Status: 1,
  2108. Ctime: time.Now().Unix(),
  2109. Mtime: 0,
  2110. Price: warehouse.PackingPrice,
  2111. WarehousingDetailId: 0,
  2112. WarehouseOutDetailId: warehouseOutInfo.ID,
  2113. CancelOutDetailId: 0,
  2114. ProductDate: goods.ProductDate,
  2115. ExpireDate: goods.ExpiryDate,
  2116. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2117. StorehouseId: goods.StorehouseId,
  2118. AdminUserId: goods.AdminUserId,
  2119. BuyPrice: warehouse.Price,
  2120. StockCount: goods.StockCount,
  2121. }
  2122. //创建出库明细
  2123. CreateStockFlowOne(stockFlow)
  2124. //查询该机构默认仓库
  2125. storeConfig, _ := GetAllStoreHouseConfig(goods.OrgId)
  2126. //查询剩余库存
  2127. goodList, _ := GetGoodSumCountByStoreId(storeConfig.StorehouseOutInfo, goods.GoodId, goods.OrgId)
  2128. var sum_count int64
  2129. var sum_in_count int64
  2130. for _, item := range goodList {
  2131. sum_count += item.StockCount
  2132. sum_in_count += item.WarehousingCount
  2133. }
  2134. UpdateGoodByGoodId(goods.GoodId, sum_count, sum_in_count, goods.OrgId)
  2135. // 出库完成后,要将该批次库存清零
  2136. warehouse.StockCount = 0
  2137. warehouse.Mtime = time.Now().Unix()
  2138. errThree := UpDateWarehouseInfoByStock(&warehouse)
  2139. fmt.Println(errThree)
  2140. //if errThree != nil {
  2141. // utils.ErrorLog("事务失败,原因为: %v", errThree.Error())
  2142. // tx.Rollback()
  2143. //} else {
  2144. // tx.Commit()
  2145. //}
  2146. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2147. goods.Count = deliver_number - stock_number
  2148. ConsumablesDeliveryOne(orgID, record_time, goods, warehouseOut, count, creator)
  2149. }
  2150. return nil
  2151. }
  2152. // 退库逻辑
  2153. func ConsumablesDeliveryTwo(orgID int64, goods *models.WarehouseOutInfo, count int64) (err error) {
  2154. ////开事务
  2155. //tx := XTWriteDB().Begin()
  2156. var deliver_number int64 = 0
  2157. var stock_number int64 = 0
  2158. var maxNumber int64 = 0
  2159. deliver_number = goods.Count
  2160. // 根据先进先出原则,查询最先入库的批次,进行出库
  2161. warehouse, err := FindFirstWarehousingInfoByGoodId(goods.GoodId, goods.ID, goods.StorehouseId)
  2162. if err != nil {
  2163. return err
  2164. }
  2165. stock_number = warehouse.StockCount
  2166. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  2167. if stock_number >= deliver_number {
  2168. warehouseOutInfo := &models.WarehouseOutInfo{
  2169. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  2170. WarehouseOutId: goods.ID,
  2171. WarehouseInfotId: warehouse.ID,
  2172. Status: 1,
  2173. Ctime: time.Now().Unix(),
  2174. Remark: goods.Remark,
  2175. OrgId: orgID,
  2176. Type: 1,
  2177. Manufacturer: goods.Manufacturer,
  2178. Dealer: goods.Dealer,
  2179. IsSys: 0,
  2180. GoodTypeId: goods.GoodTypeId,
  2181. GoodId: goods.GoodId,
  2182. ExpiryDate: goods.ExpiryDate,
  2183. ProductDate: goods.ProductDate,
  2184. Number: goods.Number,
  2185. Price: warehouse.PackingPrice,
  2186. LicenseNumber: goods.LicenseNumber,
  2187. ConsumableType: 2,
  2188. }
  2189. warehouseOutInfo.Count = goods.Count
  2190. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  2191. if errOne != nil {
  2192. return errOne
  2193. }
  2194. maxNumber = goods.Count
  2195. if warehouse.StockCount < maxNumber { //这段逻辑根本不会执行,因为当库存数量大于或等于出库数量才会进入这个分支
  2196. return errors.New("库存数量不足")
  2197. }
  2198. warehouse.StockCount = warehouse.StockCount - maxNumber
  2199. warehouse.Mtime = time.Now().Unix()
  2200. if warehouse.StockCount <= 0 {
  2201. return errors.New("库存数量不足")
  2202. }
  2203. errThree := UpDateWarehouseInfoByStock(&warehouse)
  2204. fmt.Println(errThree)
  2205. //if errThree != nil {
  2206. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  2207. // tx.Rollback()
  2208. //} else {
  2209. // tx.Commit()
  2210. //}
  2211. return
  2212. } else {
  2213. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2214. warehouseOutInfo := &models.WarehouseOutInfo{
  2215. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  2216. WarehouseOutId: goods.ID,
  2217. WarehouseInfotId: warehouse.ID,
  2218. Status: 1,
  2219. Ctime: time.Now().Unix(),
  2220. Remark: goods.Remark,
  2221. OrgId: orgID,
  2222. Type: 1,
  2223. Manufacturer: goods.Manufacturer,
  2224. Dealer: goods.Dealer,
  2225. IsSys: 0,
  2226. GoodTypeId: goods.GoodTypeId,
  2227. GoodId: goods.GoodId,
  2228. ExpiryDate: goods.ExpiryDate,
  2229. ProductDate: goods.ProductDate,
  2230. Number: goods.Number,
  2231. Price: warehouse.PackingPrice,
  2232. LicenseNumber: goods.LicenseNumber,
  2233. ConsumableType: 2,
  2234. IsCheck: 1,
  2235. StorehouseId: goods.StorehouseId,
  2236. }
  2237. warehouseOutInfo.Count = stock_number
  2238. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  2239. if errOne != nil {
  2240. return errOne
  2241. }
  2242. // 出库完成后,要将该批次库存清零
  2243. warehouse.StockCount = 0
  2244. warehouse.Mtime = time.Now().Unix()
  2245. errThree := UpDateWarehouseInfoByStock(&warehouse)
  2246. //if errThree != nil {
  2247. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  2248. // tx.Rollback()
  2249. //} else {
  2250. // tx.Commit()
  2251. //}
  2252. fmt.Println(errThree)
  2253. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2254. goods.Count = deliver_number - stock_number
  2255. ConsumablesDeliveryTwo(orgID, goods, count)
  2256. }
  2257. return nil
  2258. }
  2259. func DeleteWarehouseInfo(id int64) error {
  2260. err := XTWriteDB().Model(models.WarehousingInfo{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": 0}).Error
  2261. return err
  2262. }
  2263. // 药品出库 递归方式
  2264. func NewDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DoctorAdvice) (err error) {
  2265. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  2266. var deliver_number int64 = 0
  2267. var stock_number int64 = 0
  2268. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  2269. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  2270. var drug_price float64
  2271. //按最小单位数据计算
  2272. deliver_number = count
  2273. if advice.PrescribingNumberUnit == drup.MaxUnit {
  2274. drug_price = drup.RetailPrice
  2275. }
  2276. if advice.PrescribingNumberUnit == drup.MinUnit {
  2277. drug_price = drup.MinPrice
  2278. }
  2279. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  2280. drug_price = drup.RetailPrice
  2281. }
  2282. // 根据先进先出原则,查询最先入库的批次,进行出库
  2283. // 如果没有对应的库存,则报错
  2284. //查询默认仓库
  2285. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  2286. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2287. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  2288. var stockMax int64
  2289. var stockMin int64
  2290. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  2291. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  2292. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  2293. UpdateMinNumber(lastWarehouse.ID, stockMin)
  2294. }
  2295. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2296. if err != nil {
  2297. return err
  2298. }
  2299. // 将该批次的剩余库存数量转换为拆零数量
  2300. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  2301. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  2302. if stock_number >= deliver_number {
  2303. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2304. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2305. WarehouseOutId: warehouseout.ID,
  2306. Status: 1,
  2307. Ctime: time.Now().Unix(),
  2308. Remark: warehouse.Remark,
  2309. OrgId: orgID,
  2310. Type: 1,
  2311. Manufacturer: warehouse.Manufacturer,
  2312. Dealer: warehouse.Dealer,
  2313. IsSys: 1,
  2314. SysRecordTime: advice.RecordDate,
  2315. DrugId: advice.DrugId,
  2316. Number: warehouse.Number,
  2317. BatchNumber: warehouse.BatchNumber,
  2318. Price: warehouse.RetailPrice,
  2319. CountUnit: drup.MinUnit,
  2320. RetailPrice: drug_price,
  2321. ProductDate: warehouse.ProductDate,
  2322. ExpiryDate: warehouse.ExpiryDate,
  2323. PatientId: advice.PatientId,
  2324. Count: deliver_number, //出最小单位
  2325. WarehouseInfoId: warehouse.ID,
  2326. AdviceId: advice.ID,
  2327. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2328. StorehouseId: storeConfig.DrugStorehouseOut,
  2329. IsCheck: 1,
  2330. }
  2331. drugflow := models.DrugFlow{
  2332. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2333. WarehouseOutId: warehouseout.ID,
  2334. DrugId: advice.DrugId,
  2335. Number: warehouse.Number,
  2336. ProductDate: warehouse.ProductDate,
  2337. ExpireDate: warehouse.ExpiryDate,
  2338. Count: deliver_number, //按最小单位计算,
  2339. Price: warehouse.RetailPrice,
  2340. Status: 1,
  2341. Ctime: time.Now().Unix(),
  2342. UserOrgId: orgID,
  2343. Manufacturer: warehouse.Manufacturer,
  2344. Dealer: warehouse.Dealer,
  2345. BatchNumber: warehouse.BatchNumber,
  2346. MaxUnit: drup.MinUnit,
  2347. ConsumableType: 3,
  2348. IsEdit: 1,
  2349. Creator: advice.ExecutionStaff,
  2350. IsSys: 1,
  2351. PatientId: advice.PatientId,
  2352. WarehousingDetailId: warehouse.ID,
  2353. AdviceId: advice.ID,
  2354. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2355. StorehouseId: storeConfig.DrugStorehouseOut,
  2356. }
  2357. CreateDrugFlowOne(drugflow)
  2358. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2359. if errOne != nil {
  2360. return errOne
  2361. }
  2362. //查询是否存在数据
  2363. details := &models.DrugAutomaticReduceDetail{
  2364. WarehouseOutId: warehouseout.ID,
  2365. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2366. PatientId: advice.PatientId,
  2367. Ctime: time.Now().Unix(),
  2368. Mtime: time.Now().Unix(),
  2369. Status: 1,
  2370. RecordTime: advice.RecordDate,
  2371. OrgId: orgID,
  2372. DrugId: advice.DrugId,
  2373. Count: deliver_number,
  2374. CountUnit: drup.MinUnit,
  2375. WarehouseInfoId: warehouse.ID,
  2376. StorehouseId: storeConfig.DrugStorehouseOut,
  2377. }
  2378. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2379. if errTwo != nil {
  2380. return errTwo
  2381. }
  2382. // 出库完成后,要减去对应批次的库存数量
  2383. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  2384. var maxNumber int64 = 0
  2385. var minNumber int64 = 0
  2386. //var stock_max_number int64 = 0
  2387. //stock_max_number = warehouse.StockMaxNumber
  2388. maxNumber = deliver_number / drup.MinNumber
  2389. minNumber = deliver_number % drup.MinNumber
  2390. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  2391. minNumber = maxNumber
  2392. }
  2393. if drup.MaxUnit != drup.MinUnit {
  2394. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  2395. return errors.New("库存数量不足")
  2396. }
  2397. }
  2398. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  2399. if warehouse.StockMaxNumber < 0 {
  2400. warehouse.StockMaxNumber = 0
  2401. }
  2402. if warehouse.StockMinNumber < 0 {
  2403. warehouse.StockMinNumber = 0
  2404. }
  2405. warehouse.Mtime = time.Now().Unix()
  2406. if warehouse.StockMinNumber < minNumber {
  2407. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  2408. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  2409. } else {
  2410. if minNumber > 0 {
  2411. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2412. }
  2413. if minNumber == 0 && maxNumber != 1 {
  2414. if warehouse.StockMinNumber > 0 {
  2415. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2416. }
  2417. }
  2418. }
  2419. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  2420. if (warehouse.StockMinNumber - deliver_number) > 0 {
  2421. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2422. //if warehouse.StockMinNumber == 0 {
  2423. // warehouse.StockMaxNumber = stock_max_number
  2424. //}
  2425. }
  2426. }
  2427. if drup.MaxUnit != drup.MinUnit {
  2428. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  2429. return errors.New("库存数量不足")
  2430. }
  2431. }
  2432. if warehouse.StockMinNumber <= 0 {
  2433. warehouse.StockMinNumber = 0
  2434. }
  2435. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  2436. if errThree != nil {
  2437. return errThree
  2438. }
  2439. return nil
  2440. } else {
  2441. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2442. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2443. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2444. WarehouseOutId: warehouseout.ID,
  2445. Status: 1,
  2446. Ctime: time.Now().Unix(),
  2447. Remark: warehouse.Remark,
  2448. OrgId: orgID,
  2449. Type: 1,
  2450. Manufacturer: warehouse.Manufacturer,
  2451. Dealer: warehouse.Dealer,
  2452. IsSys: 1,
  2453. SysRecordTime: advice.RecordDate,
  2454. DrugId: advice.DrugId,
  2455. Number: warehouse.Number,
  2456. BatchNumber: warehouse.BatchNumber,
  2457. Price: warehouse.RetailPrice,
  2458. CountUnit: drup.MinUnit,
  2459. ProductDate: warehouse.ProductDate,
  2460. ExpiryDate: warehouse.ExpiryDate,
  2461. PatientId: advice.PatientId,
  2462. Count: deliver_number,
  2463. WarehouseInfoId: warehouse.ID,
  2464. AdviceId: advice.ID,
  2465. StorehouseId: storeConfig.DrugStorehouseOut,
  2466. IsCheck: 1,
  2467. }
  2468. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2469. if errOne != nil {
  2470. return errOne
  2471. }
  2472. details := &models.DrugAutomaticReduceDetail{
  2473. WarehouseOutId: warehouseout.ID,
  2474. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  2475. PatientId: advice.PatientId,
  2476. Ctime: time.Now().Unix(),
  2477. Mtime: time.Now().Unix(),
  2478. Status: 1,
  2479. RecordTime: advice.RecordDate,
  2480. OrgId: orgID,
  2481. DrugId: advice.DrugId,
  2482. Count: deliver_number,
  2483. CountUnit: drup.MinUnit,
  2484. WarehouseInfoId: warehouse.ID,
  2485. StorehouseId: storeConfig.DrugStorehouseOut,
  2486. }
  2487. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2488. if errTwo != nil {
  2489. return errTwo
  2490. }
  2491. // 出库完成后,要将该批次库存清零
  2492. warehouse.StockMaxNumber = 0
  2493. warehouse.StockMinNumber = 0
  2494. warehouse.Mtime = time.Now().Unix()
  2495. info := models.XtDrugWarehouseInfo{
  2496. ID: warehouse.ID,
  2497. WarehousingId: warehouse.WarehousingId,
  2498. DrugId: warehouse.DrugId,
  2499. Number: warehouse.Number,
  2500. ProductDate: warehouse.ProductDate,
  2501. ExpiryDate: warehouse.ExpiryDate,
  2502. WarehousingCount: warehouse.WarehousingCount,
  2503. Price: warehouse.Price,
  2504. TotalPrice: warehouse.TotalPrice,
  2505. Dealer: warehouse.Dealer,
  2506. Manufacturer: warehouse.Manufacturer,
  2507. Remark: warehouse.Remark,
  2508. Ctime: warehouse.Ctime,
  2509. Mtime: warehouse.Mtime,
  2510. Status: 1,
  2511. OrgId: warehouse.OrgId,
  2512. IsReturn: warehouse.IsReturn,
  2513. WarehousingOrder: warehouse.WarehousingOrder,
  2514. Type: warehouse.Type,
  2515. RetailPrice: warehouse.RetailPrice,
  2516. RetailTotalPrice: warehouse.RetailPrice,
  2517. StockMaxNumber: 0,
  2518. StockMinNumber: 0,
  2519. BatchNumber: warehouse.BatchNumber,
  2520. MaxUnit: warehouse.MaxUnit,
  2521. WarehousingInfoId: warehouse.WarehousingInfoId,
  2522. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2523. StorehouseId: storeConfig.DrugStorehouseOut,
  2524. IsCheck: 1,
  2525. }
  2526. //扣减库存
  2527. errThree := UpDateDrugWarehouseInfoByStock(&info)
  2528. if errThree != nil {
  2529. return errThree
  2530. }
  2531. drugflow := models.DrugFlow{
  2532. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2533. WarehouseOutId: warehouseout.ID,
  2534. DrugId: advice.DrugId,
  2535. Number: warehouse.Number,
  2536. ProductDate: warehouse.ProductDate,
  2537. ExpireDate: warehouse.ExpiryDate,
  2538. Count: deliver_number,
  2539. Price: warehouse.RetailPrice,
  2540. Status: 1,
  2541. Ctime: time.Now().Unix(),
  2542. UserOrgId: orgID,
  2543. Manufacturer: warehouse.Manufacturer,
  2544. Dealer: warehouse.Dealer,
  2545. BatchNumber: warehouse.BatchNumber,
  2546. MaxUnit: drup.MinUnit,
  2547. ConsumableType: 3,
  2548. IsEdit: 1,
  2549. Creator: advice.ExecutionStaff,
  2550. IsSys: 1,
  2551. PatientId: advice.PatientId,
  2552. WarehousingDetailId: warehouse.ID,
  2553. AdviceId: advice.ID,
  2554. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2555. StorehouseId: storeConfig.DrugStorehouseOut,
  2556. }
  2557. CreateDrugFlowOne(drugflow)
  2558. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2559. prescribingNumber_two_temp := deliver_number - stock_number
  2560. overPlusNumber := float64(prescribingNumber_two_temp)
  2561. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  2562. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  2563. NewDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  2564. }
  2565. return
  2566. }
  2567. // 药品出库 递归方式
  2568. func NewHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  2569. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  2570. var deliver_number int64 = 0
  2571. var stock_number int64 = 0
  2572. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  2573. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  2574. deliver_number = count
  2575. var min_price float64
  2576. if advice.PrescribingNumberUnit == drup.MaxUnit {
  2577. min_price = drup.RetailPrice
  2578. }
  2579. if advice.PrescribingNumberUnit == drup.MinUnit {
  2580. min_price = drup.MinPrice
  2581. }
  2582. if advice.PrescribingNumberUnit != drup.MaxUnit && advice.PrescribingNumberUnit != drup.MinUnit {
  2583. min_price = drup.RetailPrice
  2584. }
  2585. fmt.Println(min_price)
  2586. // 根据先进先出原则,查询最先入库的批次,进行出库
  2587. // 如果没有对应的库存,则报错
  2588. //开启事物
  2589. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  2590. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2591. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  2592. var stockMax int64
  2593. var stockMin int64
  2594. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  2595. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  2596. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  2597. UpdateMinNumber(lastWarehouse.ID, stockMin)
  2598. }
  2599. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2600. //查找药品信息
  2601. if err != nil {
  2602. return err
  2603. }
  2604. // 将该批次的剩余库存数量转换为拆零数量
  2605. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  2606. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  2607. if stock_number >= deliver_number {
  2608. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2609. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2610. WarehouseOutId: warehouseout.ID,
  2611. Status: 1,
  2612. Ctime: time.Now().Unix(),
  2613. Remark: warehouse.Remark,
  2614. OrgId: orgID,
  2615. Type: 1,
  2616. Manufacturer: warehouse.Manufacturer,
  2617. Dealer: warehouse.Dealer,
  2618. IsSys: 1,
  2619. SysRecordTime: advice.RecordDate,
  2620. DrugId: advice.DrugId,
  2621. Number: warehouse.Number,
  2622. BatchNumber: warehouse.BatchNumber,
  2623. Price: warehouse.RetailPrice,
  2624. CountUnit: drup.MinUnit,
  2625. RetailPrice: warehouse.RetailPrice,
  2626. ProductDate: warehouse.ProductDate,
  2627. ExpiryDate: warehouse.ExpiryDate,
  2628. PatientId: advice.PatientId,
  2629. WarehouseInfoId: warehouse.ID,
  2630. Count: deliver_number,
  2631. AdviceId: advice.ID,
  2632. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2633. StorehouseId: storeConfig.DrugStorehouseOut,
  2634. IsCheck: 1,
  2635. }
  2636. drugflow := models.DrugFlow{
  2637. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2638. WarehouseOutId: warehouseout.ID,
  2639. DrugId: advice.DrugId,
  2640. Number: warehouse.Number,
  2641. ProductDate: warehouse.ProductDate,
  2642. ExpireDate: warehouse.ExpiryDate,
  2643. Count: deliver_number,
  2644. Price: warehouse.RetailPrice,
  2645. Status: 1,
  2646. Ctime: time.Now().Unix(),
  2647. UserOrgId: orgID,
  2648. Manufacturer: warehouse.Manufacturer,
  2649. Dealer: warehouse.Dealer,
  2650. BatchNumber: warehouse.BatchNumber,
  2651. MaxUnit: drup.MinUnit,
  2652. ConsumableType: 3,
  2653. IsEdit: 1,
  2654. Creator: advice.ExecutionStaff,
  2655. IsSys: 1,
  2656. PatientId: advice.PatientId,
  2657. SystemTime: advice.RecordDate,
  2658. WarehousingDetailId: warehouse.ID,
  2659. AdviceId: advice.ID,
  2660. StorehouseId: storeConfig.DrugStorehouseOut,
  2661. }
  2662. CreateDrugFlowOne(drugflow)
  2663. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2664. if errOne != nil {
  2665. return errOne
  2666. }
  2667. details := &models.DrugAutomaticReduceDetail{
  2668. WarehouseOutId: warehouseout.ID,
  2669. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2670. PatientId: advice.PatientId,
  2671. Ctime: time.Now().Unix(),
  2672. Mtime: time.Now().Unix(),
  2673. Status: 1,
  2674. RecordTime: advice.RecordDate,
  2675. OrgId: orgID,
  2676. DrugId: advice.DrugId,
  2677. Count: deliver_number,
  2678. CountUnit: drup.MinUnit,
  2679. WarehouseInfoId: warehouse.ID,
  2680. AdviceId: advice.ID,
  2681. StorehouseId: storeConfig.DrugStorehouseOut,
  2682. }
  2683. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2684. if errTwo != nil {
  2685. return errTwo
  2686. }
  2687. // 出库完成后,要减去对应批次的库存数量
  2688. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  2689. var maxNumber int64 = 0
  2690. var minNumber int64 = 0
  2691. //var stock_max_number int64 = 0
  2692. //stock_max_number = warehouse.StockMaxNumber
  2693. maxNumber = deliver_number / drup.MinNumber
  2694. minNumber = deliver_number % drup.MinNumber
  2695. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  2696. minNumber = maxNumber
  2697. }
  2698. if drup.MaxUnit != drup.MinUnit {
  2699. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  2700. return errors.New("库存数量不足")
  2701. }
  2702. }
  2703. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  2704. if warehouse.StockMaxNumber < 0 {
  2705. warehouse.StockMaxNumber = 0
  2706. }
  2707. if warehouse.StockMinNumber < 0 {
  2708. warehouse.StockMinNumber = 0
  2709. }
  2710. warehouse.Mtime = time.Now().Unix()
  2711. if warehouse.StockMinNumber < minNumber {
  2712. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  2713. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  2714. } else {
  2715. if minNumber > 0 {
  2716. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2717. }
  2718. if minNumber == 0 && maxNumber != 1 {
  2719. if warehouse.StockMinNumber > 0 {
  2720. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2721. }
  2722. }
  2723. }
  2724. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  2725. if (warehouse.StockMinNumber - deliver_number) > 0 {
  2726. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2727. //if warehouse.StockMinNumber == 0 {
  2728. // warehouse.StockMaxNumber = stock_max_number
  2729. //}
  2730. }
  2731. }
  2732. if drup.MaxUnit != drup.MinUnit {
  2733. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  2734. return errors.New("库存数量不足")
  2735. }
  2736. }
  2737. if warehouse.StockMinNumber <= 0 {
  2738. warehouse.StockMinNumber = 0
  2739. }
  2740. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  2741. if errThree != nil {
  2742. return errThree
  2743. }
  2744. return nil
  2745. } else {
  2746. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2747. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2748. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2749. WarehouseOutId: warehouseout.ID,
  2750. Status: 1,
  2751. Ctime: time.Now().Unix(),
  2752. Remark: warehouse.Remark,
  2753. OrgId: orgID,
  2754. Type: 1,
  2755. Manufacturer: warehouse.Manufacturer,
  2756. Dealer: warehouse.Dealer,
  2757. IsSys: 1,
  2758. SysRecordTime: advice.RecordDate,
  2759. DrugId: advice.DrugId,
  2760. Number: warehouse.Number,
  2761. BatchNumber: warehouse.BatchNumber,
  2762. Price: warehouse.RetailPrice,
  2763. RetailPrice: warehouse.RetailPrice,
  2764. ProductDate: warehouse.ProductDate,
  2765. ExpiryDate: warehouse.ExpiryDate,
  2766. PatientId: advice.PatientId,
  2767. WarehouseInfoId: warehouse.ID,
  2768. Count: stock_number,
  2769. CountUnit: drup.MinUnit,
  2770. AdviceId: advice.ID,
  2771. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2772. StorehouseId: storeConfig.DrugStorehouseOut,
  2773. IsCheck: 1,
  2774. }
  2775. drugflow := models.DrugFlow{
  2776. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2777. WarehouseOutId: warehouseout.ID,
  2778. DrugId: advice.DrugId,
  2779. Number: warehouse.Number,
  2780. ProductDate: warehouse.ProductDate,
  2781. ExpireDate: warehouse.ExpiryDate,
  2782. Count: stock_number,
  2783. Price: warehouse.RetailPrice,
  2784. Status: 1,
  2785. Ctime: time.Now().Unix(),
  2786. UserOrgId: orgID,
  2787. Manufacturer: warehouse.Manufacturer,
  2788. Dealer: warehouse.Dealer,
  2789. BatchNumber: warehouse.BatchNumber,
  2790. MaxUnit: drup.MinUnit,
  2791. ConsumableType: 3,
  2792. IsEdit: 1,
  2793. Creator: advice.ExecutionStaff,
  2794. IsSys: 1,
  2795. PatientId: advice.PatientId,
  2796. SystemTime: advice.RecordDate,
  2797. WarehousingDetailId: warehouse.ID,
  2798. AdviceId: advice.ID,
  2799. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2800. StorehouseId: storeConfig.DrugStorehouseOut,
  2801. }
  2802. CreateDrugFlowOne(drugflow)
  2803. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2804. if errOne != nil {
  2805. return errOne
  2806. }
  2807. details := &models.DrugAutomaticReduceDetail{
  2808. WarehouseOutId: warehouseout.ID,
  2809. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2810. PatientId: advice.PatientId,
  2811. Ctime: time.Now().Unix(),
  2812. Mtime: time.Now().Unix(),
  2813. Status: 1,
  2814. RecordTime: advice.RecordDate,
  2815. OrgId: orgID,
  2816. DrugId: advice.DrugId,
  2817. Count: stock_number,
  2818. CountUnit: drup.MinUnit,
  2819. WarehouseInfoId: warehouse.ID,
  2820. AdviceId: advice.ID,
  2821. StorehouseId: storeConfig.DrugStorehouseOut,
  2822. }
  2823. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2824. if errTwo != nil {
  2825. return errTwo
  2826. }
  2827. info := models.XtDrugWarehouseInfo{
  2828. ID: warehouse.ID,
  2829. WarehousingId: warehouse.WarehousingId,
  2830. DrugId: warehouse.DrugId,
  2831. Number: warehouse.Number,
  2832. ProductDate: warehouse.ProductDate,
  2833. ExpiryDate: warehouse.ExpiryDate,
  2834. WarehousingCount: warehouse.WarehousingCount,
  2835. Price: warehouse.RetailPrice,
  2836. TotalPrice: warehouse.TotalPrice,
  2837. Dealer: warehouse.Dealer,
  2838. Manufacturer: warehouse.Manufacturer,
  2839. Remark: warehouse.Remark,
  2840. Ctime: warehouse.Ctime,
  2841. Mtime: warehouse.Mtime,
  2842. Status: 1,
  2843. OrgId: warehouse.OrgId,
  2844. IsReturn: warehouse.IsReturn,
  2845. WarehousingOrder: warehouse.WarehousingOrder,
  2846. Type: warehouse.Type,
  2847. RetailPrice: warehouse.RetailPrice,
  2848. RetailTotalPrice: warehouse.RetailPrice,
  2849. StockMaxNumber: 0,
  2850. StockMinNumber: 0,
  2851. BatchNumber: warehouse.BatchNumber,
  2852. MaxUnit: warehouse.MaxUnit,
  2853. WarehousingInfoId: warehouse.WarehousingInfoId,
  2854. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2855. StorehouseId: storeConfig.DrugStorehouseOut,
  2856. IsCheck: 1,
  2857. }
  2858. errThree := UpDateDrugWarehouseInfoByStock(&info)
  2859. if errThree != nil {
  2860. return errThree
  2861. }
  2862. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2863. prescribingNumber_two_temp := deliver_number - stock_number
  2864. overPlusNumber := float64(prescribingNumber_two_temp)
  2865. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  2866. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  2867. NewHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  2868. }
  2869. return
  2870. }
  2871. // 药品出库 递归方式
  2872. func HisAuToReduce(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  2873. //查询最后一次出库数据
  2874. drugInfo, _ := GetDrugWarehouseOutInfoIsExit(advice.DrugId, advice.PatientId, advice.RecordDate)
  2875. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  2876. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  2877. var count_number int64
  2878. var device_number int64
  2879. if advice.PrescribingNumberUnit == drup.MaxUnit {
  2880. device_number = count * drup.MinNumber
  2881. }
  2882. if advice.PrescribingNumberUnit == drup.MinUnit {
  2883. device_number = count
  2884. }
  2885. if drugInfo.CountUnit == drup.MaxUnit {
  2886. count_number = drugInfo.Count * drup.MinNumber
  2887. }
  2888. if drugInfo.CountUnit == drup.MinUnit {
  2889. count_number = drugInfo.Count
  2890. }
  2891. // 比较数据大小
  2892. //如果当期医嘱数量小于最后一次出库则退库
  2893. if device_number < count_number {
  2894. var cha_number int64
  2895. var maxNumber int64
  2896. var minNumber int64
  2897. cha_number = count_number - device_number
  2898. maxNumber = cha_number / drup.MinNumber
  2899. minNumber = cha_number % drup.MinNumber
  2900. //更改库存
  2901. ModifyDrugWarehouse(drugInfo.WarehouseInfoId, maxNumber, minNumber)
  2902. //修改出库表
  2903. info := models.DrugWarehouseOutInfo{
  2904. Count: device_number,
  2905. CountUnit: advice.PrescribingNumberUnit,
  2906. }
  2907. ModifyDrugWarehouseOutInfo(info, advice.DrugId, advice.PatientId, advice.RecordDate)
  2908. //修改自动出库表
  2909. detail := models.DrugAutomaticReduceDetail{
  2910. Count: device_number,
  2911. CountUnit: advice.PrescribingNumberUnit,
  2912. }
  2913. ModifyAutoMaticReduce(detail, advice.PatientId, advice.RecordDate, advice.DrugId)
  2914. //修改流水
  2915. flow := models.DrugFlow{
  2916. Count: device_number,
  2917. MaxUnit: drup.MinUnit,
  2918. }
  2919. ModifyDrugFlow(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId)
  2920. }
  2921. //如果当期医嘱数量大于最后一次出库则需要出库
  2922. if device_number > count_number {
  2923. var cha_number int64
  2924. cha_number = device_number - count_number
  2925. // 根据先进先出原则,查询最先入库的批次,进行出库
  2926. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  2927. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2928. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  2929. var stockMax int64
  2930. var stockMin int64
  2931. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  2932. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  2933. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  2934. UpdateMinNumber(lastWarehouse.ID, stockMin)
  2935. }
  2936. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2937. //查找药品信息
  2938. //baseInfo, _ := FindBaseDrugLibRecord(advice.UserOrgId, advice.DrugId)
  2939. if err != nil {
  2940. return err
  2941. }
  2942. // 将该批次的剩余库存数量转换为拆零数量
  2943. var stock_number int64
  2944. var maxNumber int64
  2945. var minNumber int64
  2946. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  2947. //如果库存数量大于出库数据,正常出库
  2948. if stock_number >= cha_number {
  2949. //扣减库存
  2950. maxNumber = cha_number / drup.MinNumber
  2951. minNumber = cha_number % drup.MinNumber
  2952. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  2953. minNumber = maxNumber
  2954. }
  2955. if drup.MaxUnit != drup.MinUnit {
  2956. if warehouse.StockMaxNumber < maxNumber {
  2957. return errors.New("库存数量不足")
  2958. }
  2959. }
  2960. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  2961. if warehouse.StockMaxNumber < 0 {
  2962. warehouse.StockMaxNumber = 0
  2963. }
  2964. warehouse.Mtime = time.Now().Unix()
  2965. if warehouse.StockMinNumber < minNumber {
  2966. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  2967. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  2968. } else {
  2969. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2970. }
  2971. if drup.MaxUnit != drup.MinUnit {
  2972. if warehouse.StockMaxNumber < 0 {
  2973. return errors.New("库存数量不足")
  2974. }
  2975. }
  2976. //扣减库存232332332332
  2977. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  2978. if errThree != nil {
  2979. return errThree
  2980. }
  2981. //修改出库表
  2982. info := models.DrugWarehouseOutInfo{
  2983. Count: device_number,
  2984. CountUnit: advice.PrescribingNumberUnit,
  2985. }
  2986. ModifyDrugWarehouseOutInfoOne(info, advice.DrugId, advice.PatientId, advice.RecordDate, warehouse.ID)
  2987. //修改自动出库表
  2988. detail := models.DrugAutomaticReduceDetail{
  2989. Count: device_number,
  2990. CountUnit: advice.PrescribingNumberUnit,
  2991. }
  2992. ModifyAutoMaticReduceOne(detail, advice.PatientId, advice.RecordDate, advice.DrugId, warehouse.ID)
  2993. //修改流水
  2994. flow := models.DrugFlow{
  2995. Count: device_number,
  2996. MaxUnit: advice.PrescribingNumberUnit,
  2997. }
  2998. ModifyDrugFlowOne(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId, warehouse.ID)
  2999. }
  3000. //如果库存数量小于出库数据,则需要出库到下一个批次
  3001. if stock_number < cha_number {
  3002. //修改出库表
  3003. info := models.DrugWarehouseOutInfo{
  3004. Count: cha_number,
  3005. CountUnit: advice.PrescribingNumberUnit,
  3006. }
  3007. ModifyDrugWarehouseOutInfoOne(info, advice.DrugId, advice.PatientId, advice.RecordDate, warehouse.ID)
  3008. //修改自动出库表
  3009. detail := models.DrugAutomaticReduceDetail{
  3010. Count: cha_number,
  3011. CountUnit: advice.PrescribingNumberUnit,
  3012. }
  3013. ModifyAutoMaticReduceOne(detail, advice.PatientId, advice.RecordDate, advice.DrugId, warehouse.ID)
  3014. //修改流水
  3015. flow := models.DrugFlow{
  3016. Count: cha_number,
  3017. MaxUnit: advice.PrescribingNumberUnit,
  3018. }
  3019. ModifyDrugFlowOne(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId, warehouse.ID)
  3020. druginfo := &models.XtDrugWarehouseInfo{
  3021. ID: warehouse.ID,
  3022. WarehousingId: warehouse.WarehousingId,
  3023. DrugId: warehouse.DrugId,
  3024. Number: warehouse.Number,
  3025. ProductDate: warehouse.ProductDate,
  3026. ExpiryDate: warehouse.ExpiryDate,
  3027. WarehousingCount: warehouse.WarehousingCount,
  3028. Price: warehouse.Price,
  3029. TotalPrice: warehouse.TotalPrice,
  3030. Dealer: warehouse.Dealer,
  3031. Manufacturer: warehouse.Manufacturer,
  3032. Remark: warehouse.Remark,
  3033. Ctime: warehouse.Ctime,
  3034. Mtime: warehouse.Mtime,
  3035. Status: 1,
  3036. OrgId: warehouse.OrgId,
  3037. IsReturn: warehouse.IsReturn,
  3038. WarehousingOrder: warehouse.WarehousingOrder,
  3039. Type: warehouse.Type,
  3040. RetailPrice: warehouse.RetailPrice,
  3041. RetailTotalPrice: warehouse.RetailPrice,
  3042. StockMaxNumber: 0,
  3043. StockMinNumber: 0,
  3044. BatchNumber: warehouse.BatchNumber,
  3045. MaxUnit: warehouse.MaxUnit,
  3046. WarehousingInfoId: warehouse.WarehousingInfoId,
  3047. StorehouseId: storeConfig.DrugStorehouseOut,
  3048. IsCheck: 1,
  3049. }
  3050. errThree := UpDateDrugWarehouseInfoByStock(druginfo)
  3051. if errThree != nil {
  3052. return errThree
  3053. }
  3054. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3055. prescribingNumber_two_temp := cha_number - stock_number
  3056. overPlusNumber := float64(prescribingNumber_two_temp)
  3057. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  3058. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  3059. NewHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  3060. }
  3061. }
  3062. return
  3063. }
  3064. // 采购管理耗材出库逻辑
  3065. 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) {
  3066. var deliver_number int64 = 0
  3067. var stock_number int64 = 0
  3068. var maxNumber int64 = 0
  3069. deliver_number = goods.Count
  3070. // 根据先进先出原则,查询最先入库的批次,进行出库
  3071. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  3072. // 如果没有对应的库存,则报错
  3073. warehouse, err := FindFirstWarehousingInfoByStockNight(goods.GoodId, goods.GoodTypeId, warehouse_out_id, goods.IsSource, houseConfig.StorehouseOutInfo)
  3074. if err != nil {
  3075. return err
  3076. }
  3077. stock_number = warehouse.StockCount
  3078. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3079. if stock_number >= deliver_number {
  3080. warehouseOutInfo := &models.WarehouseOutInfo{
  3081. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3082. WarehouseOutId: goods.WarehouseOutId,
  3083. WarehouseInfotId: warehouse.ID,
  3084. Status: 1,
  3085. Ctime: time.Now().Unix(),
  3086. Remark: goods.Remark,
  3087. OrgId: orgID,
  3088. Type: 1,
  3089. Manufacturer: goods.Manufacturer,
  3090. Dealer: goods.Dealer,
  3091. IsSys: 0,
  3092. SysRecordTime: record_time,
  3093. GoodTypeId: goods.GoodTypeId,
  3094. GoodId: goods.GoodId,
  3095. ExpiryDate: goods.ExpiryDate,
  3096. ProductDate: goods.ProductDate,
  3097. Number: warehouse.Number,
  3098. Price: goods.Price,
  3099. LicenseNumber: goods.LicenseNumber,
  3100. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  3101. StorehouseId: warehouse.StorehouseId,
  3102. IsCheck: 1,
  3103. }
  3104. warehouseOutInfo.Count = goods.Count
  3105. //添加出库单详情
  3106. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  3107. if errOne != nil {
  3108. return errOne
  3109. }
  3110. stockFlow := models.VmStockFlow{
  3111. WarehousingId: warehouse.ID,
  3112. GoodId: goods.GoodId,
  3113. Number: warehouse.Number,
  3114. LicenseNumber: goods.LicenseNumber,
  3115. Count: goods.Count,
  3116. UserOrgId: goods.OrgId,
  3117. PatientId: goods.PatientId,
  3118. SystemTime: record_time,
  3119. ConsumableType: 2,
  3120. IsSys: goods.IsSys,
  3121. WarehousingOrder: "",
  3122. WarehouseOutId: goods.WarehouseOutId,
  3123. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3124. IsEdit: 1,
  3125. CancelStockId: 0,
  3126. CancelOrderNumber: "",
  3127. Manufacturer: goods.Manufacturer,
  3128. Dealer: goods.Dealer,
  3129. Creator: creator,
  3130. UpdateCreator: 0,
  3131. Status: 1,
  3132. Ctime: time.Now().Unix(),
  3133. Mtime: 0,
  3134. Price: warehouse.PackingPrice,
  3135. WarehousingDetailId: 0,
  3136. WarehouseOutDetailId: warehouseOutInfo.ID,
  3137. CancelOutDetailId: 0,
  3138. ProductDate: goods.ProductDate,
  3139. ExpireDate: goods.ExpiryDate,
  3140. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  3141. SupplyWarehouseId: warehouseOut.SupplyWarehouseId,
  3142. StorehouseId: warehouse.StorehouseId,
  3143. }
  3144. //创建出库流水
  3145. CreateStockFlowOne(stockFlow)
  3146. maxNumber = goods.Count
  3147. if warehouse.StockCount < maxNumber {
  3148. return errors.New("库存数量不足")
  3149. }
  3150. warehouse.StockCount = warehouse.StockCount - maxNumber
  3151. warehouse.Mtime = time.Now().Unix()
  3152. //扣减库存
  3153. errThree := UpDateWarehouseInfoByStock(&warehouse)
  3154. //出库数量增加
  3155. AddGoodSumOutCount(houseConfig.StorehouseOutInfo, warehouse.GoodId, warehouse.OrgId, goods.Count)
  3156. ModifyGoodReduceInformationSix(warehouse.GoodId, maxNumber, warehouse.OrgId, warehouse.PackingPrice)
  3157. if errThree != nil {
  3158. return errThree
  3159. }
  3160. } else {
  3161. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  3162. warehouseOutInfo := &models.WarehouseOutInfo{
  3163. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3164. WarehouseOutId: goods.WarehouseOutId,
  3165. WarehouseInfotId: warehouse.ID,
  3166. Status: 1,
  3167. Ctime: time.Now().Unix(),
  3168. Remark: goods.Remark,
  3169. OrgId: orgID,
  3170. Type: 1,
  3171. Manufacturer: goods.Manufacturer,
  3172. Dealer: goods.Dealer,
  3173. IsSys: 0,
  3174. SysRecordTime: record_time,
  3175. GoodTypeId: goods.GoodTypeId,
  3176. GoodId: goods.GoodId,
  3177. ExpiryDate: goods.ExpiryDate,
  3178. ProductDate: goods.ProductDate,
  3179. Number: warehouse.Number,
  3180. Price: warehouse.PackingPrice,
  3181. LicenseNumber: goods.LicenseNumber,
  3182. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  3183. StorehouseId: warehouse.StorehouseId,
  3184. IsCheck: 1,
  3185. }
  3186. warehouseOutInfo.Count = stock_number
  3187. //查询是否已经生成出库单
  3188. info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  3189. if errcodes == gorm.ErrRecordNotFound {
  3190. //创建出库单
  3191. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  3192. if errOne != nil {
  3193. return errOne
  3194. }
  3195. } else {
  3196. warehouseOutInfo.Count = info.Count + stock_number
  3197. UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  3198. }
  3199. stockFlow := models.VmStockFlow{
  3200. WarehousingId: warehouse.ID,
  3201. GoodId: goods.GoodId,
  3202. Number: warehouse.Number,
  3203. LicenseNumber: goods.LicenseNumber,
  3204. Count: stock_number,
  3205. UserOrgId: goods.OrgId,
  3206. PatientId: 0,
  3207. SystemTime: record_time,
  3208. ConsumableType: 2,
  3209. IsSys: 0,
  3210. WarehousingOrder: "",
  3211. WarehouseOutId: goods.WarehouseOutId,
  3212. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3213. IsEdit: 1,
  3214. CancelStockId: 0,
  3215. CancelOrderNumber: "",
  3216. Manufacturer: goods.Manufacturer,
  3217. Dealer: goods.Dealer,
  3218. Creator: creator,
  3219. UpdateCreator: 0,
  3220. Status: 1,
  3221. Ctime: time.Now().Unix(),
  3222. Mtime: 0,
  3223. Price: warehouse.PackingPrice,
  3224. WarehousingDetailId: 0,
  3225. WarehouseOutDetailId: warehouseOutInfo.ID,
  3226. CancelOutDetailId: 0,
  3227. ProductDate: goods.ProductDate,
  3228. ExpireDate: goods.ExpiryDate,
  3229. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  3230. SupplyWarehouseId: warehouseOut.SupplyWarehouseId,
  3231. StorehouseId: warehouse.StorehouseId,
  3232. }
  3233. //创建出库明细
  3234. CreateStockFlowOne(stockFlow)
  3235. // 出库完成后,要将该批次库存清零
  3236. ModifyGoodReduceInformationSix(warehouse.GoodId, warehouse.StockCount, warehouse.OrgId, warehouse.PackingPrice)
  3237. warehouse.StockCount = 0
  3238. warehouse.Mtime = time.Now().Unix()
  3239. errThree := UpDateWarehouseInfoByStock(&warehouse)
  3240. if errThree != nil {
  3241. return errThree
  3242. }
  3243. //出库数量增加
  3244. AddGoodSumOutCount(houseConfig.StorehouseOutInfo, warehouse.GoodId, warehouse.OrgId, warehouse.StockCount)
  3245. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3246. goods.Count = deliver_number - stock_number
  3247. ConsumablesDeliveryNight(orgID, record_time, goods, warehouseOut, count, creator, warehouse_out_id, cancel_out_id)
  3248. }
  3249. return nil
  3250. }
  3251. func FindFirstWarehousingInfoByStockNight(good_id int64, good_type_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  3252. if is_source == 1 {
  3253. 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
  3254. }
  3255. if is_source == 2 || is_source == 0 {
  3256. 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
  3257. }
  3258. return info, err
  3259. }
  3260. // 药品出库 递归方式
  3261. 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) {
  3262. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  3263. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  3264. var deliver_number int64 = 0
  3265. var stock_number int64 = 0
  3266. var retail_price float64
  3267. if advice.CountUnit == drup.MaxUnit {
  3268. deliver_number = prescribingNumber * drup.MinNumber
  3269. formatInt := strconv.FormatInt(prescribingNumber, 10)
  3270. floatcount, _ := strconv.ParseFloat(formatInt, 64)
  3271. retail_price = floatcount * advice.Price
  3272. } else {
  3273. deliver_number = prescribingNumber
  3274. formatInt := strconv.FormatInt(prescribingNumber, 10)
  3275. floatcount, _ := strconv.ParseFloat(formatInt, 64)
  3276. retail_price = floatcount * advice.Price
  3277. }
  3278. // 根据先进先出原则,查询最先入库的批次,进行出库
  3279. // 如果没有对应的库存,则报错
  3280. //开启事物
  3281. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, houseConfig.DrugStorehouseOut)
  3282. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  3283. var stockMax int64
  3284. var stockMin int64
  3285. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  3286. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  3287. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  3288. UpdateMinNumber(lastWarehouse.ID, stockMin)
  3289. }
  3290. warehouse, err := FindLastDrugWarehousingInfoByIDSix(advice.DrugId, warehouse_out_id, advice.IsSource, houseConfig.DrugStorehouseOut)
  3291. if err != nil {
  3292. return err
  3293. }
  3294. // 将该批次的剩余库存数量转换为拆零数量
  3295. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  3296. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3297. if stock_number >= deliver_number {
  3298. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  3299. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3300. WarehouseOutId: advice.WarehouseOutId,
  3301. Status: 1,
  3302. Ctime: time.Now().Unix(),
  3303. Remark: advice.Remark,
  3304. OrgId: orgID,
  3305. Type: 1,
  3306. Manufacturer: advice.Manufacturer,
  3307. Dealer: warehouse.Dealer,
  3308. IsSys: 0,
  3309. SysRecordTime: advice.SysRecordTime,
  3310. DrugId: advice.DrugId,
  3311. ExpiryDate: advice.ExpiryDate,
  3312. ProductDate: advice.ProductDate,
  3313. Number: advice.Number,
  3314. BatchNumber: warehouse.BatchNumber,
  3315. Count: deliver_number,
  3316. RetailPrice: advice.RetailPrice,
  3317. Price: advice.Price,
  3318. WarehouseInfoId: warehouse.ID,
  3319. CountUnit: drup.MinUnit,
  3320. AdviceId: advice.ID,
  3321. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  3322. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  3323. StorehouseId: warehouse.StorehouseId,
  3324. IsCheck: 1,
  3325. }
  3326. warehouseOutInfo.Count = prescribingNumber
  3327. warehouseOutInfo.CountUnit = advice.CountUnit
  3328. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  3329. if errOne != nil {
  3330. return errOne
  3331. }
  3332. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  3333. drugflow := models.DrugFlow{
  3334. WarehouseOutId: advice.WarehouseOutId,
  3335. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3336. DrugId: advice.DrugId,
  3337. Number: warehouse.Number,
  3338. ProductDate: advice.ProductDate,
  3339. ExpireDate: advice.ExpiryDate,
  3340. Count: deliver_number,
  3341. Price: warehouse.RetailPrice,
  3342. Status: 1,
  3343. Ctime: time.Now().Unix(),
  3344. UserOrgId: advice.OrgId,
  3345. Manufacturer: advice.Manufacturer,
  3346. Dealer: advice.Dealer,
  3347. BatchNumber: warehouse.BatchNumber,
  3348. MaxUnit: drup.MinUnit,
  3349. ConsumableType: 2,
  3350. IsEdit: 1,
  3351. Creator: 0,
  3352. IsSys: 0,
  3353. WarehouseOutDetailId: drugWareInfo.ID,
  3354. AdviceId: advice.ID,
  3355. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  3356. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  3357. SystemTime: advice.SysRecordTime,
  3358. StorehouseId: warehouse.StorehouseId,
  3359. WarehousingDetailId: warehouse.ID,
  3360. WarehousingId: warehouse.ID,
  3361. RetailPrice: retail_price,
  3362. LastPrice: warehouse.Price,
  3363. }
  3364. CreateDrugFlowOne(drugflow)
  3365. // 出库完成后,要减去对应批次的库存数量
  3366. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  3367. var maxNumber int64 = 0
  3368. var minNumber int64 = 0
  3369. if advice.CountUnit == drup.MinUnit {
  3370. maxNumber = prescribingNumber / drup.MinNumber
  3371. minNumber = prescribingNumber % drup.MinNumber
  3372. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  3373. minNumber = maxNumber
  3374. }
  3375. } else {
  3376. maxNumber = prescribingNumber
  3377. }
  3378. if warehouse.StockMaxNumber < maxNumber {
  3379. return errors.New("库存数量不足")
  3380. }
  3381. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  3382. if warehouse.StockMaxNumber < 0 {
  3383. warehouse.StockMaxNumber = 0
  3384. }
  3385. warehouse.Mtime = time.Now().Unix()
  3386. if warehouse.StockMinNumber < minNumber {
  3387. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  3388. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  3389. } else {
  3390. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  3391. }
  3392. if warehouse.StockMaxNumber < 0 {
  3393. return errors.New("库存数量不足")
  3394. }
  3395. if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
  3396. warehouse.StockMinNumber = 0
  3397. }
  3398. if warehouse.StockMaxNumber > 0 {
  3399. var sum_count int64
  3400. sum_count = maxNumber * drup.MinNumber
  3401. ModifyDrugReduceInformationSix(warehouse.DrugId, sum_count, warehouse.OrgId, warehouse.RetailPrice)
  3402. //出库数量增加
  3403. AddDrugSumOutCount(houseConfig.DrugStorehouseOut, sum_count, warehouse.OrgId, warehouse.DrugId)
  3404. }
  3405. if warehouse.StockMinNumber > 0 {
  3406. ModifyDrugReduceInformationSix(warehouse.DrugId, minNumber, warehouse.OrgId, warehouse.RetailPrice)
  3407. //出库数量增加
  3408. AddDrugSumOutCount(houseConfig.DrugStorehouseOut, minNumber, warehouse.OrgId, warehouse.DrugId)
  3409. }
  3410. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  3411. if errThree != nil {
  3412. return errThree
  3413. }
  3414. return nil
  3415. } else {
  3416. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  3417. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  3418. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3419. WarehouseOutId: advice.WarehouseOutId,
  3420. Status: 1,
  3421. Ctime: time.Now().Unix(),
  3422. Remark: advice.Remark,
  3423. OrgId: orgID,
  3424. Type: 1,
  3425. Manufacturer: advice.Manufacturer,
  3426. Dealer: warehouse.Dealer,
  3427. IsSys: 0,
  3428. SysRecordTime: advice.SysRecordTime,
  3429. DrugId: advice.DrugId,
  3430. ExpiryDate: warehouse.ExpiryDate,
  3431. ProductDate: warehouse.ProductDate,
  3432. Number: warehouse.Number,
  3433. BatchNumber: warehouse.BatchNumber,
  3434. Count: stock_number,
  3435. RetailPrice: advice.RetailPrice,
  3436. Price: advice.Price,
  3437. WarehouseInfoId: warehouse.ID,
  3438. CountUnit: drup.MinUnit,
  3439. AdviceId: advice.ID,
  3440. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  3441. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  3442. StorehouseId: warehouse.StorehouseId,
  3443. IsCheck: 1,
  3444. }
  3445. warehouseOutInfo.Count = stock_number
  3446. //warehouseOutInfo.CountUnit = advice.CountUnit
  3447. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  3448. if errOne != nil {
  3449. return errOne
  3450. }
  3451. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  3452. drugflow := models.DrugFlow{
  3453. WarehouseOutId: advice.WarehouseOutId,
  3454. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3455. DrugId: advice.DrugId,
  3456. Number: warehouse.Number,
  3457. ProductDate: advice.ProductDate,
  3458. ExpireDate: advice.ExpiryDate,
  3459. Count: stock_number,
  3460. Price: warehouse.RetailPrice,
  3461. Status: 1,
  3462. Ctime: time.Now().Unix(),
  3463. UserOrgId: advice.OrgId,
  3464. Manufacturer: advice.Manufacturer,
  3465. Dealer: advice.Dealer,
  3466. BatchNumber: warehouse.BatchNumber,
  3467. MaxUnit: drup.MinUnit,
  3468. ConsumableType: 2,
  3469. IsEdit: 1,
  3470. Creator: 0,
  3471. IsSys: 0,
  3472. WarehouseOutDetailId: drugWareInfo.ID,
  3473. AdviceId: advice.ID,
  3474. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  3475. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  3476. SystemTime: advice.SysRecordTime,
  3477. StorehouseId: warehouse.StorehouseId,
  3478. WarehousingDetailId: warehouse.ID,
  3479. WarehousingId: warehouse.ID,
  3480. RetailPrice: retail_price,
  3481. LastPrice: warehouse.Price,
  3482. }
  3483. CreateDrugFlowOne(drugflow)
  3484. if warehouse.StockMaxNumber > 0 {
  3485. var sum_count int64
  3486. sum_count = warehouse.StockMaxNumber * drup.MinNumber
  3487. ModifyDrugReduceInformationSix(warehouse.DrugId, sum_count, warehouse.OrgId, warehouse.RetailPrice)
  3488. AddDrugSumOutCount(houseConfig.DrugStorehouseOut, sum_count, warehouse.OrgId, warehouse.DrugId)
  3489. }
  3490. if warehouse.StockMinNumber > 0 {
  3491. ModifyDrugReduceInformationSix(warehouse.DrugId, warehouse.StockMinNumber, warehouse.OrgId, warehouse.RetailPrice)
  3492. AddDrugSumOutCount(houseConfig.DrugStorehouseOut, warehouse.StockMinNumber, warehouse.OrgId, warehouse.DrugId)
  3493. }
  3494. // 出库完成后,要将该批次库存清零
  3495. warehouse.StockMaxNumber = 0
  3496. warehouse.StockMinNumber = 0
  3497. warehouse.Mtime = time.Now().Unix()
  3498. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  3499. if errThree != nil {
  3500. return errThree
  3501. }
  3502. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3503. prescribingNumber_two_temp := deliver_number - stock_number
  3504. advice.CountUnit = drup.MinUnit
  3505. AutoDrugDeliverInfoNight(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, warehouse_out_id, cancel_out_id)
  3506. }
  3507. return
  3508. }
  3509. // 调拨出库逻辑
  3510. func ConsumablesDeliveryTwelve(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64, warehouseInfo *models.WarehousingInfo) (err error) {
  3511. var deliver_number int64 = 0
  3512. var stock_number int64 = 0
  3513. var maxNumber int64 = 0
  3514. deliver_number = goods.Count
  3515. // 根据先进先出原则,查询最先入库的批次,进行出库
  3516. // 如果没有对应的库存,则报错
  3517. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  3518. if err != nil {
  3519. return err
  3520. }
  3521. stock_number = warehouse.StockCount
  3522. maxNumber = goods.Count
  3523. //if warehouse.StockCount < maxNumber {
  3524. // return errors.New("库存数量不足")
  3525. //}
  3526. if warehouse.StockCount-maxNumber >= 0 {
  3527. warehouse.StockCount = warehouse.StockCount - maxNumber
  3528. } else {
  3529. warehouse.StockCount = warehouse.StockCount
  3530. }
  3531. warehouse.Mtime = time.Now().Unix()
  3532. //扣减库存
  3533. errThree := UpDateWarehouseInfoByStock(&warehouse)
  3534. //查询剩余库存
  3535. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  3536. var sum_count int64
  3537. for _, item := range goodList {
  3538. sum_count += item.StockCount
  3539. }
  3540. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3541. if stock_number >= deliver_number {
  3542. warehouseOutInfo := &models.WarehouseOutInfo{
  3543. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  3544. WarehouseOutId: goods.WarehouseOutId,
  3545. WarehouseInfotId: warehouse.ID,
  3546. Status: 1,
  3547. Ctime: time.Now().Unix(),
  3548. Remark: goods.Remark,
  3549. OrgId: orgID,
  3550. Type: 1,
  3551. Manufacturer: warehouse.Manufacturer,
  3552. Dealer: warehouse.Dealer,
  3553. IsSys: 0,
  3554. SysRecordTime: record_time,
  3555. GoodTypeId: warehouse.GoodTypeId,
  3556. GoodId: warehouse.GoodId,
  3557. ExpiryDate: warehouse.ExpiryDate,
  3558. ProductDate: warehouse.ProductDate,
  3559. Number: warehouse.Number,
  3560. Price: warehouse.PackingPrice,
  3561. LicenseNumber: warehouse.LicenseNumber,
  3562. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3563. StorehouseId: goods.StorehouseId,
  3564. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  3565. IsCheck: 1,
  3566. OverCount: sum_count,
  3567. }
  3568. warehouseOutInfo.Count = goods.Count
  3569. //添加出库单详情
  3570. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  3571. if errOne != nil {
  3572. return errOne
  3573. }
  3574. stockFlow := models.VmStockFlow{
  3575. WarehousingId: warehouse.ID,
  3576. GoodId: goods.GoodId,
  3577. Number: warehouse.Number,
  3578. LicenseNumber: goods.LicenseNumber,
  3579. Count: goods.Count,
  3580. UserOrgId: goods.OrgId,
  3581. PatientId: goods.PatientId,
  3582. SystemTime: record_time,
  3583. ConsumableType: 12,
  3584. IsSys: goods.IsSys,
  3585. WarehousingOrder: "",
  3586. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  3587. WarehouseOutId: goods.WarehouseOutId,
  3588. IsEdit: 1,
  3589. CancelStockId: 0,
  3590. CancelOrderNumber: "",
  3591. Manufacturer: warehouse.Manufacturer,
  3592. Dealer: warehouse.Dealer,
  3593. Creator: creator,
  3594. UpdateCreator: 0,
  3595. Status: 1,
  3596. Ctime: time.Now().Unix(),
  3597. Mtime: 0,
  3598. Price: warehouse.PackingPrice,
  3599. WarehousingDetailId: 0,
  3600. WarehouseOutDetailId: warehouseOutInfo.ID,
  3601. CancelOutDetailId: 0,
  3602. ProductDate: warehouse.ProductDate,
  3603. ExpireDate: warehouse.ExpiryDate,
  3604. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3605. StorehouseId: warehouse.StorehouseId,
  3606. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  3607. OverCount: sum_count,
  3608. }
  3609. //创建出库流水
  3610. CreateStockFlowOne(stockFlow)
  3611. //入库
  3612. warehouseInfo := &models.WarehousingInfo{
  3613. WarehousingOrder: warehouseInfo.WarehousingOrder,
  3614. WarehousingId: warehouseInfo.WarehousingId,
  3615. GoodId: warehouse.GoodId,
  3616. Number: warehouse.Number,
  3617. GoodTypeId: warehouse.GoodTypeId,
  3618. ProductDate: warehouse.ProductDate,
  3619. ExpiryDate: warehouse.ExpiryDate,
  3620. WarehousingCount: goods.Count,
  3621. Price: warehouse.Price,
  3622. TotalPrice: 0,
  3623. Status: 1,
  3624. Ctime: warehouse.Ctime,
  3625. Remark: goods.Remark,
  3626. OrgId: warehouse.OrgId,
  3627. Type: 1,
  3628. Manufacturer: warehouse.Manufacturer,
  3629. StockCount: goods.Count,
  3630. Dealer: warehouse.Dealer,
  3631. LicenseNumber: "",
  3632. PackingPrice: warehouse.PackingPrice,
  3633. StorehouseId: warehouseInfo.StorehouseId,
  3634. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  3635. IsCheck: 1,
  3636. }
  3637. AddWarehosingInfo(warehouseInfo)
  3638. //查询剩余库存
  3639. goodListOne, _ := GetAllGoodSumCount(goods.GoodId, orgID, warehouseInfo.StorehouseId)
  3640. var sum_count_one int64
  3641. for _, item := range goodListOne {
  3642. sum_count_one += item.StockCount
  3643. }
  3644. stockInfoFlow := models.VmStockFlow{
  3645. WarehousingId: warehouseInfo.WarehousingId,
  3646. GoodId: goods.GoodId,
  3647. Number: warehouse.Number,
  3648. LicenseNumber: goods.LicenseNumber,
  3649. Count: goods.Count,
  3650. UserOrgId: goods.OrgId,
  3651. PatientId: goods.PatientId,
  3652. SystemTime: record_time,
  3653. ConsumableType: 13,
  3654. IsSys: goods.IsSys,
  3655. WarehousingOrder: warehouseInfo.WarehousingOrder,
  3656. WarehouseOutId: 0,
  3657. WarehouseOutOrderNumber: "",
  3658. IsEdit: 1,
  3659. CancelStockId: 0,
  3660. CancelOrderNumber: "",
  3661. Manufacturer: warehouse.Manufacturer,
  3662. Dealer: warehouse.Dealer,
  3663. Creator: creator,
  3664. UpdateCreator: 0,
  3665. Status: 1,
  3666. Ctime: time.Now().Unix(),
  3667. Mtime: 0,
  3668. Price: warehouse.Price,
  3669. WarehousingDetailId: 0,
  3670. WarehouseOutDetailId: warehouseOutInfo.ID,
  3671. CancelOutDetailId: 0,
  3672. ProductDate: warehouse.ProductDate,
  3673. ExpireDate: warehouse.ExpiryDate,
  3674. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3675. StorehouseId: warehouseInfo.StorehouseId,
  3676. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  3677. OverCount: sum_count_one,
  3678. }
  3679. //创建入库流水
  3680. CreateStockFlowOne(stockInfoFlow)
  3681. if errThree != nil {
  3682. return errThree
  3683. }
  3684. } else {
  3685. // 出库完成后,要将该批次库存清零
  3686. warehouse.StockCount = 0
  3687. warehouse.Mtime = time.Now().Unix()
  3688. errThree := UpDateWarehouseInfoByStock(&warehouse)
  3689. if errThree != nil {
  3690. return errThree
  3691. }
  3692. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3693. goods.Count = deliver_number - stock_number
  3694. //查询剩余库存
  3695. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  3696. var sum_count int64
  3697. for _, item := range goodList {
  3698. sum_count += item.StockCount
  3699. }
  3700. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  3701. warehouseOutInfo := &models.WarehouseOutInfo{
  3702. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  3703. WarehouseOutId: goods.WarehouseOutId,
  3704. WarehouseInfotId: warehouse.ID,
  3705. Status: 1,
  3706. Ctime: time.Now().Unix(),
  3707. Remark: goods.Remark,
  3708. OrgId: orgID,
  3709. Type: 1,
  3710. Manufacturer: warehouse.Manufacturer,
  3711. Dealer: warehouse.Dealer,
  3712. IsSys: 0,
  3713. SysRecordTime: record_time,
  3714. GoodTypeId: warehouse.GoodTypeId,
  3715. GoodId: warehouse.GoodId,
  3716. ExpiryDate: warehouse.ExpiryDate,
  3717. ProductDate: warehouse.ProductDate,
  3718. Number: warehouse.Number,
  3719. Price: warehouse.PackingPrice,
  3720. LicenseNumber: warehouse.LicenseNumber,
  3721. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3722. StorehouseId: warehouse.StorehouseId,
  3723. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  3724. IsCheck: 1,
  3725. OverCount: sum_count,
  3726. }
  3727. warehouseOutInfo.Count = stock_number
  3728. //查询是否已经生成出库单
  3729. info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  3730. if errcodes == gorm.ErrRecordNotFound {
  3731. //创建出库单
  3732. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  3733. if errOne != nil {
  3734. return errOne
  3735. }
  3736. } else {
  3737. warehouseOutInfo.Count = info.Count + stock_number
  3738. UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  3739. }
  3740. stockFlow := models.VmStockFlow{
  3741. WarehousingId: warehouse.ID,
  3742. GoodId: goods.GoodId,
  3743. Number: warehouse.Number,
  3744. LicenseNumber: goods.LicenseNumber,
  3745. Count: stock_number,
  3746. UserOrgId: goods.OrgId,
  3747. PatientId: 0,
  3748. SystemTime: record_time,
  3749. ConsumableType: 12,
  3750. IsSys: 0,
  3751. WarehousingOrder: "",
  3752. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  3753. WarehouseOutId: goods.WarehouseOutId,
  3754. IsEdit: 1,
  3755. CancelStockId: 0,
  3756. CancelOrderNumber: "",
  3757. Manufacturer: warehouse.Manufacturer,
  3758. Dealer: warehouse.Dealer,
  3759. Creator: creator,
  3760. UpdateCreator: 0,
  3761. Status: 1,
  3762. Ctime: time.Now().Unix(),
  3763. Mtime: 0,
  3764. Price: warehouse.PackingPrice,
  3765. WarehousingDetailId: 0,
  3766. WarehouseOutDetailId: warehouseOutInfo.ID,
  3767. CancelOutDetailId: 0,
  3768. ProductDate: warehouse.ProductDate,
  3769. ExpireDate: warehouse.ExpiryDate,
  3770. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3771. StorehouseId: goods.StorehouseId,
  3772. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  3773. OverCount: sum_count,
  3774. }
  3775. //创建出库明细
  3776. CreateStockFlowOne(stockFlow)
  3777. //入库
  3778. warehouseInfo := &models.WarehousingInfo{
  3779. WarehousingOrder: warehouseInfo.WarehousingOrder,
  3780. WarehousingId: warehouseInfo.WarehousingId,
  3781. GoodId: warehouse.GoodId,
  3782. Number: warehouse.Number,
  3783. GoodTypeId: warehouse.GoodTypeId,
  3784. ProductDate: warehouse.ProductDate,
  3785. ExpiryDate: warehouse.ExpiryDate,
  3786. WarehousingCount: stock_number,
  3787. Price: warehouse.Price,
  3788. TotalPrice: 0,
  3789. Status: 1,
  3790. Ctime: warehouse.Ctime,
  3791. Remark: goods.Remark,
  3792. OrgId: warehouse.OrgId,
  3793. Type: 1,
  3794. Manufacturer: warehouse.Manufacturer,
  3795. StockCount: stock_number,
  3796. Dealer: warehouse.Dealer,
  3797. LicenseNumber: "",
  3798. PackingPrice: warehouse.PackingPrice,
  3799. StorehouseId: warehouseInfo.StorehouseId,
  3800. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  3801. IsCheck: 1,
  3802. }
  3803. AddWarehosingInfo(warehouseInfo)
  3804. //查询剩余库存
  3805. goodListOne, _ := GetAllGoodSumCount(goods.GoodId, orgID, warehouseInfo.StorehouseId)
  3806. var sum_count_one int64
  3807. for _, item := range goodListOne {
  3808. sum_count_one += item.StockCount
  3809. }
  3810. stockInfoFlow := models.VmStockFlow{
  3811. WarehousingId: warehouseInfo.WarehousingId,
  3812. GoodId: goods.GoodId,
  3813. Number: warehouse.Number,
  3814. LicenseNumber: goods.LicenseNumber,
  3815. Count: stock_number,
  3816. UserOrgId: goods.OrgId,
  3817. PatientId: goods.PatientId,
  3818. SystemTime: record_time,
  3819. ConsumableType: 13,
  3820. IsSys: goods.IsSys,
  3821. WarehousingOrder: warehouseInfo.WarehousingOrder,
  3822. WarehouseOutId: warehouseOut.ID,
  3823. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3824. IsEdit: 1,
  3825. CancelStockId: 0,
  3826. CancelOrderNumber: "",
  3827. Manufacturer: warehouse.Manufacturer,
  3828. Dealer: warehouse.Dealer,
  3829. Creator: creator,
  3830. UpdateCreator: 0,
  3831. Status: 1,
  3832. Ctime: time.Now().Unix(),
  3833. Mtime: 0,
  3834. Price: warehouse.Price,
  3835. WarehousingDetailId: 0,
  3836. WarehouseOutDetailId: warehouseOutInfo.ID,
  3837. CancelOutDetailId: 0,
  3838. ProductDate: warehouse.ProductDate,
  3839. ExpireDate: warehouse.ExpiryDate,
  3840. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3841. StorehouseId: warehouseInfo.StorehouseId,
  3842. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  3843. OverCount: sum_count_one,
  3844. }
  3845. //创建出库流水
  3846. CreateStockFlowOne(stockInfoFlow)
  3847. ConsumablesDeliveryTwelve(orgID, record_time, goods, warehouseOut, count, creator, warehouseInfo)
  3848. }
  3849. return nil
  3850. }
  3851. func AddWarehosingInfo(info *models.WarehousingInfo) error {
  3852. err = XTWriteDB().Create(&info).Error
  3853. return err
  3854. }
  3855. // 药品出库 递归方式
  3856. func AutoDrugDeliverInfoTwelve(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo, drugWarehouse *models.DrugWarehouseInfo) (err error) {
  3857. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  3858. var deliver_number int64 = 0
  3859. var stock_number int64 = 0
  3860. if advice.CountUnit == drup.MaxUnit {
  3861. deliver_number = prescribingNumber * drup.MinNumber
  3862. } else {
  3863. deliver_number = prescribingNumber
  3864. }
  3865. // 根据先进先出原则,查询最先入库的批次,进行出库
  3866. // 如果没有对应的库存,则报错
  3867. //开启事物
  3868. //storeConfig, _ := GetAllStoreHouseConfig(orgID)
  3869. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  3870. //fmt.Println("lastWarehouse.StockMinNumber", lastWarehouse.StockMinNumber)
  3871. //fmt.Println("drup.MinNumber", drup.MinNumber)
  3872. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  3873. var stockMax int64
  3874. var stockMin int64
  3875. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  3876. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  3877. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  3878. UpdateMinNumber(lastWarehouse.ID, stockMin)
  3879. }
  3880. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  3881. if err != nil {
  3882. return err
  3883. }
  3884. // 将该批次的剩余库存数量转换为拆零数量
  3885. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  3886. //fmt.Println("stock23233233232", stock_number)
  3887. //fmt.Println("deliver_number", deliver_number)
  3888. // 出库完成后,要减去对应批次的库存数量
  3889. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  3890. var maxNumber int64 = 0
  3891. var minNumber int64 = 0
  3892. if advice.CountUnit == drup.MinUnit {
  3893. maxNumber = prescribingNumber / drup.MinNumber
  3894. minNumber = prescribingNumber % drup.MinNumber
  3895. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  3896. minNumber = maxNumber
  3897. }
  3898. } else {
  3899. maxNumber = prescribingNumber
  3900. }
  3901. //fmt.Println("warehouse.StockMaxNumber",warehouse.StockMaxNumber)
  3902. //fmt.Println("maxNumber9899923323232wo",maxNumber)
  3903. //if warehouse.StockMaxNumber < maxNumber {
  3904. //
  3905. // return errors.New("库存数量不足")
  3906. //}
  3907. if warehouse.StockMaxNumber-maxNumber > 0 {
  3908. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  3909. }
  3910. warehouse.Mtime = time.Now().Unix()
  3911. if warehouse.StockMinNumber < minNumber {
  3912. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  3913. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  3914. } else {
  3915. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  3916. }
  3917. if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
  3918. warehouse.StockMinNumber = 0
  3919. }
  3920. if warehouse.StockMaxNumber < 0 {
  3921. return errors.New("库存数量不足")
  3922. }
  3923. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  3924. if errThree != nil {
  3925. return errThree
  3926. }
  3927. //查询剩余库存
  3928. stockInfo, _ := GetDrugAllStockInfo(advice.StorehouseId, orgID, advice.DrugId)
  3929. var sum_count int64
  3930. for _, its := range stockInfo {
  3931. if its.MaxUnit == drup.MaxUnit {
  3932. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  3933. }
  3934. sum_count += its.StockMaxNumber + its.StockMinNumber
  3935. }
  3936. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3937. if stock_number >= deliver_number {
  3938. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  3939. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  3940. WarehouseOutId: advice.WarehouseOutId,
  3941. Status: 1,
  3942. Ctime: time.Now().Unix(),
  3943. Remark: advice.Remark,
  3944. OrgId: orgID,
  3945. Type: 1,
  3946. Manufacturer: warehouse.Manufacturer,
  3947. Dealer: warehouse.Dealer,
  3948. IsSys: 12,
  3949. SysRecordTime: advice.Ctime,
  3950. DrugId: advice.DrugId,
  3951. ExpiryDate: warehouse.ExpiryDate,
  3952. ProductDate: warehouse.ProductDate,
  3953. Number: warehouse.Number,
  3954. BatchNumber: warehouse.BatchNumber,
  3955. Count: deliver_number,
  3956. RetailPrice: warehouse.RetailPrice,
  3957. Price: warehouse.RetailPrice,
  3958. WarehouseInfoId: warehouse.ID,
  3959. CountUnit: drup.MinUnit,
  3960. AdviceId: advice.ID,
  3961. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3962. StorehouseId: advice.StorehouseId,
  3963. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  3964. IsCheck: 1,
  3965. OverCount: sum_count,
  3966. }
  3967. warehouseOutInfo.Count = prescribingNumber
  3968. warehouseOutInfo.CountUnit = advice.CountUnit
  3969. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  3970. if errOne != nil {
  3971. return errOne
  3972. }
  3973. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  3974. drugflow := models.DrugFlow{
  3975. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  3976. WarehouseOutId: advice.WarehouseOutId,
  3977. DrugId: advice.DrugId,
  3978. Number: warehouse.Number,
  3979. ProductDate: warehouse.ProductDate,
  3980. ExpireDate: warehouse.ExpiryDate,
  3981. Count: deliver_number,
  3982. Price: warehouse.RetailPrice,
  3983. Status: 1,
  3984. Ctime: time.Now().Unix(),
  3985. UserOrgId: advice.OrgId,
  3986. Manufacturer: warehouse.Manufacturer,
  3987. Dealer: warehouse.Dealer,
  3988. BatchNumber: warehouse.BatchNumber,
  3989. MaxUnit: drup.MinUnit,
  3990. ConsumableType: 12,
  3991. IsEdit: 1,
  3992. Creator: 0,
  3993. IsSys: 12,
  3994. WarehouseOutDetailId: drugWareInfo.ID,
  3995. AdviceId: advice.ID,
  3996. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3997. StorehouseId: advice.StorehouseId,
  3998. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  3999. WarehousingDetailId: warehouse.ID,
  4000. OverCount: sum_count,
  4001. }
  4002. CreateDrugFlowOne(drugflow)
  4003. //入库
  4004. drugInfo := models.DrugWarehouseInfo{
  4005. WarehousingOrder: drugWarehouse.WarehousingOrder,
  4006. WarehousingId: drugWarehouse.WarehousingId,
  4007. DrugId: warehouse.DrugId,
  4008. Number: warehouse.Number,
  4009. ProductDate: warehouse.ProductDate,
  4010. ExpiryDate: warehouse.ExpiryDate,
  4011. WarehousingCount: deliver_number,
  4012. Price: warehouse.RetailPrice,
  4013. TotalPrice: warehouse.TotalPrice,
  4014. Status: 1,
  4015. Ctime: warehouse.Ctime,
  4016. Remark: "",
  4017. OrgId: warehouse.OrgId,
  4018. Type: 13,
  4019. Manufacturer: warehouse.Manufacturer,
  4020. Dealer: warehouse.Dealer,
  4021. StockMaxNumber: 0,
  4022. StockMinNumber: deliver_number,
  4023. BatchNumber: warehouse.BatchNumber,
  4024. MaxUnit: drup.MinUnit,
  4025. MinUnit: drup.MinUnit,
  4026. RetailPrice: warehouse.RetailPrice,
  4027. StorehouseId: drugWarehouse.StorehouseId,
  4028. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  4029. IsCheck: 1,
  4030. }
  4031. CreateDrugWarehouseInfo(drugInfo)
  4032. //查询剩余库存
  4033. stockInfoOne, _ := GetDrugAllStockInfo(drugWarehouse.StorehouseId, orgID, advice.DrugId)
  4034. var sum_count_one int64
  4035. for _, its := range stockInfoOne {
  4036. if its.MaxUnit == drup.MaxUnit {
  4037. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  4038. }
  4039. sum_count_one += its.StockMaxNumber + its.StockMinNumber
  4040. }
  4041. flow := models.DrugFlow{
  4042. WarehousingId: drugWarehouse.WarehousingId,
  4043. DrugId: warehouse.DrugId,
  4044. Number: warehouse.Number,
  4045. BatchNumber: warehouse.BatchNumber,
  4046. Count: deliver_number,
  4047. UserOrgId: warehouse.OrgId,
  4048. PatientId: 0,
  4049. SystemTime: 0,
  4050. ConsumableType: 13,
  4051. IsSys: 13,
  4052. WarehousingOrder: drugWarehouse.WarehousingOrder,
  4053. WarehouseOutId: 0,
  4054. WarehouseOutOrderNumber: "",
  4055. IsEdit: 0,
  4056. CancelStockId: 0,
  4057. CancelOrderNumber: "",
  4058. Manufacturer: warehouse.Manufacturer,
  4059. Dealer: warehouse.Dealer,
  4060. Creator: 0,
  4061. UpdateCreator: 0,
  4062. Status: 1,
  4063. Ctime: time.Now().Unix(),
  4064. Mtime: 0,
  4065. Price: warehouse.RetailPrice,
  4066. WarehousingDetailId: warehouse.ID,
  4067. WarehouseOutDetailId: 0,
  4068. CancelOutDetailId: 0,
  4069. ExpireDate: warehouse.ExpiryDate,
  4070. ProductDate: warehouse.ProductDate,
  4071. MaxUnit: drup.MinUnit,
  4072. MinUnit: drup.MinUnit,
  4073. StockMaxNumber: 0,
  4074. StockMinNumber: deliver_number,
  4075. StorehouseId: drugWarehouse.StorehouseId,
  4076. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  4077. OverCount: sum_count_one,
  4078. }
  4079. CreateDrugFlowOne(flow)
  4080. return nil
  4081. } else {
  4082. warehouse.StockMaxNumber = 0
  4083. warehouse.StockMinNumber = 0
  4084. warehouse.Mtime = time.Now().Unix()
  4085. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4086. if errThree != nil {
  4087. return errThree
  4088. }
  4089. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  4090. prescribingNumber_two_temp := deliver_number - stock_number
  4091. advice.CountUnit = drup.MinUnit
  4092. //查询剩余库存
  4093. stockInfo, _ := GetDrugAllStockInfo(advice.StorehouseId, orgID, advice.DrugId)
  4094. var sum_count int64
  4095. for _, its := range stockInfo {
  4096. if its.MaxUnit == drup.MaxUnit {
  4097. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  4098. }
  4099. sum_count += its.StockMaxNumber + its.StockMinNumber
  4100. }
  4101. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  4102. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4103. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  4104. WarehouseOutId: advice.WarehouseOutId,
  4105. Status: 1,
  4106. Ctime: time.Now().Unix(),
  4107. Remark: advice.Remark,
  4108. OrgId: orgID,
  4109. Type: 1,
  4110. Manufacturer: advice.Manufacturer,
  4111. Dealer: warehouse.Dealer,
  4112. IsSys: 12,
  4113. SysRecordTime: advice.Ctime,
  4114. DrugId: advice.DrugId,
  4115. ExpiryDate: warehouse.ExpiryDate,
  4116. ProductDate: warehouse.ProductDate,
  4117. Number: warehouse.Number,
  4118. BatchNumber: warehouse.BatchNumber,
  4119. Count: stock_number,
  4120. RetailPrice: advice.RetailPrice,
  4121. Price: warehouse.RetailPrice,
  4122. WarehouseInfoId: warehouse.ID,
  4123. CountUnit: drup.MinUnit,
  4124. AdviceId: advice.ID,
  4125. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4126. StorehouseId: advice.StorehouseId,
  4127. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  4128. IsCheck: 1,
  4129. OverCount: sum_count,
  4130. }
  4131. warehouseOutInfo.Count = stock_number
  4132. //warehouseOutInfo.CountUnit = advice.CountUnit
  4133. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  4134. if errOne != nil {
  4135. return errOne
  4136. }
  4137. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4138. drugflow := models.DrugFlow{
  4139. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  4140. WarehouseOutId: advice.WarehouseOutId,
  4141. DrugId: advice.DrugId,
  4142. Number: warehouse.Number,
  4143. ProductDate: warehouse.ProductDate,
  4144. ExpireDate: warehouse.ExpiryDate,
  4145. Count: stock_number,
  4146. Price: warehouse.RetailPrice,
  4147. Status: 1,
  4148. Ctime: time.Now().Unix(),
  4149. UserOrgId: advice.OrgId,
  4150. Manufacturer: warehouse.Manufacturer,
  4151. Dealer: warehouse.Dealer,
  4152. BatchNumber: warehouse.BatchNumber,
  4153. MaxUnit: drup.MinUnit,
  4154. ConsumableType: 12,
  4155. IsEdit: 1,
  4156. Creator: 0,
  4157. IsSys: 12,
  4158. WarehouseOutDetailId: drugWareInfo.ID,
  4159. AdviceId: advice.ID,
  4160. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4161. StorehouseId: advice.StorehouseId,
  4162. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  4163. WarehousingDetailId: warehouse.ID,
  4164. OverCount: sum_count,
  4165. }
  4166. CreateDrugFlowOne(drugflow)
  4167. // 出库完成后,要将该批次库存清零
  4168. //入库
  4169. drugInfo := models.DrugWarehouseInfo{
  4170. WarehousingOrder: drugWarehouse.WarehousingOrder,
  4171. WarehousingId: drugWarehouse.WarehousingId,
  4172. DrugId: warehouse.DrugId,
  4173. Number: warehouse.Number,
  4174. ProductDate: warehouse.ProductDate,
  4175. ExpiryDate: warehouse.ExpiryDate,
  4176. WarehousingCount: stock_number,
  4177. Price: warehouse.RetailPrice,
  4178. TotalPrice: warehouse.TotalPrice,
  4179. Status: 1,
  4180. Ctime: warehouse.Ctime,
  4181. Remark: "",
  4182. OrgId: warehouse.OrgId,
  4183. Type: 13,
  4184. Manufacturer: warehouse.Manufacturer,
  4185. Dealer: warehouse.Dealer,
  4186. StockMaxNumber: 0,
  4187. StockMinNumber: stock_number,
  4188. RetailTotalPrice: 0,
  4189. BatchNumber: warehouse.BatchNumber,
  4190. MaxUnit: drup.MinUnit,
  4191. MinUnit: drup.MinUnit,
  4192. RetailPrice: warehouse.RetailPrice,
  4193. StorehouseId: drugWarehouse.StorehouseId,
  4194. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  4195. }
  4196. CreateDrugWarehouseInfo(drugInfo)
  4197. //查询剩余库存
  4198. stockInfoTwo, _ := GetDrugAllStockInfo(drugWarehouse.StorehouseId, orgID, advice.DrugId)
  4199. var sum_count_one int64
  4200. for _, its := range stockInfoTwo {
  4201. if its.MaxUnit == drup.MaxUnit {
  4202. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  4203. }
  4204. sum_count_one += its.StockMaxNumber + its.StockMinNumber
  4205. }
  4206. flow := models.DrugFlow{
  4207. WarehousingId: drugWarehouse.WarehousingId,
  4208. DrugId: warehouse.DrugId,
  4209. Number: warehouse.Number,
  4210. BatchNumber: warehouse.BatchNumber,
  4211. Count: stock_number,
  4212. UserOrgId: warehouse.OrgId,
  4213. PatientId: 0,
  4214. SystemTime: 0,
  4215. ConsumableType: 13,
  4216. IsSys: 13,
  4217. WarehousingOrder: drugWarehouse.WarehousingOrder,
  4218. WarehouseOutId: 0,
  4219. WarehouseOutOrderNumber: "",
  4220. IsEdit: 0,
  4221. CancelStockId: 0,
  4222. CancelOrderNumber: "",
  4223. Manufacturer: warehouse.Manufacturer,
  4224. Dealer: warehouse.Dealer,
  4225. Creator: 0,
  4226. UpdateCreator: 0,
  4227. Status: 1,
  4228. Ctime: time.Now().Unix(),
  4229. Mtime: 0,
  4230. Price: warehouse.RetailPrice,
  4231. WarehousingDetailId: warehouse.ID,
  4232. WarehouseOutDetailId: 0,
  4233. CancelOutDetailId: 0,
  4234. ExpireDate: warehouse.ExpiryDate,
  4235. ProductDate: warehouse.ProductDate,
  4236. MaxUnit: drup.MinUnit,
  4237. MinUnit: drup.MinUnit,
  4238. StockMaxNumber: 0,
  4239. StockMinNumber: stock_number,
  4240. StorehouseId: drugWarehouse.StorehouseId,
  4241. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  4242. OverCount: sum_count,
  4243. }
  4244. CreateDrugFlowOne(flow)
  4245. AutoDrugDeliverInfoTwelve(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, drugWarehouse)
  4246. }
  4247. return
  4248. }
  4249. // 药品手动出库 递归方式
  4250. func AutoDrugDeliverInfoTwenty(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  4251. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  4252. var deliver_number int64 = 0
  4253. var stock_number int64 = 0
  4254. if advice.CountUnit == drup.MaxUnit {
  4255. deliver_number = prescribingNumber * drup.MinNumber
  4256. } else {
  4257. deliver_number = prescribingNumber
  4258. }
  4259. // 根据先进先出原则,查询最先入库的批次,进行出库
  4260. // 如果没有对应的库存,则报错
  4261. //开启事物
  4262. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  4263. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  4264. var stockMax int64
  4265. var stockMin int64
  4266. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  4267. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  4268. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  4269. UpdateMinNumber(lastWarehouse.ID, stockMin)
  4270. }
  4271. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  4272. if err != nil {
  4273. return err
  4274. }
  4275. // 将该批次的剩余库存数量转换为拆零数量
  4276. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  4277. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  4278. if stock_number >= deliver_number {
  4279. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4280. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4281. WarehouseOutId: warehouseout.ID,
  4282. Status: 1,
  4283. Ctime: time.Now().Unix(),
  4284. Remark: advice.Remark,
  4285. OrgId: orgID,
  4286. Type: 1,
  4287. Manufacturer: advice.Manufacturer,
  4288. Dealer: warehouse.Dealer,
  4289. IsSys: 0,
  4290. SysRecordTime: advice.Ctime,
  4291. DrugId: advice.DrugId,
  4292. ExpiryDate: advice.ExpiryDate,
  4293. ProductDate: advice.ProductDate,
  4294. Number: advice.Number,
  4295. BatchNumber: warehouse.BatchNumber,
  4296. Count: deliver_number,
  4297. RetailPrice: advice.RetailPrice,
  4298. Price: warehouse.RetailPrice,
  4299. WarehouseInfoId: warehouse.ID,
  4300. CountUnit: drup.MinUnit,
  4301. AdviceId: advice.ID,
  4302. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4303. StorehouseId: advice.StorehouseId,
  4304. AdminUserId: advice.AdminUserId,
  4305. IsCheck: 1,
  4306. }
  4307. if warehouse.RetailPrice == 0 {
  4308. warehouseOutInfo.Price = advice.Price
  4309. }
  4310. warehouseOutInfo.Count = prescribingNumber
  4311. warehouseOutInfo.CountUnit = advice.CountUnit
  4312. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  4313. if errOne != nil {
  4314. return errOne
  4315. }
  4316. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4317. drugflow := models.DrugFlow{
  4318. WarehouseOutId: warehouseout.ID,
  4319. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4320. DrugId: advice.DrugId,
  4321. Number: warehouse.Number,
  4322. ProductDate: advice.ProductDate,
  4323. ExpireDate: advice.ExpiryDate,
  4324. Count: deliver_number,
  4325. Price: warehouse.RetailPrice,
  4326. Status: 1,
  4327. Ctime: time.Now().Unix(),
  4328. UserOrgId: advice.OrgId,
  4329. Manufacturer: advice.Manufacturer,
  4330. Dealer: advice.Dealer,
  4331. BatchNumber: warehouse.BatchNumber,
  4332. MaxUnit: drup.MinUnit,
  4333. ConsumableType: 2,
  4334. IsEdit: 1,
  4335. Creator: 0,
  4336. IsSys: 0,
  4337. WarehouseOutDetailId: drugWareInfo.ID,
  4338. AdviceId: advice.ID,
  4339. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4340. StorehouseId: advice.StorehouseId,
  4341. AdminUserId: advice.AdminUserId,
  4342. }
  4343. if warehouse.RetailPrice == 0 {
  4344. drugflow.Price = advice.Price
  4345. }
  4346. CreateDrugFlowOne(drugflow)
  4347. // 出库完成后,要减去对应批次的库存数量
  4348. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  4349. var maxNumber int64 = 0
  4350. var minNumber int64 = 0
  4351. //var stock_max_number int64 = 0
  4352. //stock_max_number = warehouse.StockMaxNumber
  4353. maxNumber = deliver_number / drup.MinNumber
  4354. minNumber = deliver_number % drup.MinNumber
  4355. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  4356. minNumber = maxNumber
  4357. }
  4358. if drup.MaxUnit != drup.MinUnit {
  4359. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  4360. return errors.New("库存数量不足")
  4361. }
  4362. }
  4363. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  4364. if warehouse.StockMaxNumber < 0 {
  4365. warehouse.StockMaxNumber = 0
  4366. }
  4367. if warehouse.StockMinNumber < 0 {
  4368. warehouse.StockMinNumber = 0
  4369. }
  4370. warehouse.Mtime = time.Now().Unix()
  4371. if warehouse.StockMinNumber < minNumber {
  4372. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  4373. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  4374. } else {
  4375. if minNumber > 0 {
  4376. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  4377. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4378. } else {
  4379. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  4380. }
  4381. }
  4382. if minNumber == 0 && maxNumber != 1 {
  4383. if warehouse.StockMinNumber > 0 {
  4384. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4385. }
  4386. }
  4387. }
  4388. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  4389. if (warehouse.StockMinNumber - deliver_number) > 0 {
  4390. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4391. //if warehouse.StockMinNumber == 0 {
  4392. // warehouse.StockMaxNumber = stock_max_number
  4393. //}
  4394. }
  4395. }
  4396. if drup.MaxUnit != drup.MinUnit {
  4397. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  4398. return errors.New("库存数量不足")
  4399. }
  4400. }
  4401. if warehouse.StockMinNumber <= 0 {
  4402. warehouse.StockMinNumber = 0
  4403. }
  4404. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4405. if errThree != nil {
  4406. return errThree
  4407. }
  4408. //查询默认仓库
  4409. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  4410. //查询默认仓库剩余多少库存
  4411. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  4412. var sum_count int64
  4413. var sum_in_count int64
  4414. for _, it := range list {
  4415. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  4416. if it.MaxUnit == baseDrug.MaxUnit {
  4417. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  4418. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  4419. sum_count += it.StockMaxNumber + it.StockMinNumber
  4420. sum_in_count += it.WarehousingCount
  4421. }
  4422. }
  4423. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  4424. return nil
  4425. } else {
  4426. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  4427. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4428. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4429. WarehouseOutId: warehouseout.ID,
  4430. Status: 1,
  4431. Ctime: time.Now().Unix(),
  4432. Remark: advice.Remark,
  4433. OrgId: orgID,
  4434. Type: 1,
  4435. Manufacturer: advice.Manufacturer,
  4436. Dealer: warehouse.Dealer,
  4437. IsSys: 0,
  4438. SysRecordTime: advice.Ctime,
  4439. DrugId: advice.DrugId,
  4440. ExpiryDate: warehouse.ExpiryDate,
  4441. ProductDate: warehouse.ProductDate,
  4442. Number: warehouse.Number,
  4443. BatchNumber: warehouse.BatchNumber,
  4444. Count: stock_number,
  4445. RetailPrice: advice.RetailPrice,
  4446. Price: warehouse.RetailPrice,
  4447. WarehouseInfoId: warehouse.ID,
  4448. CountUnit: drup.MinUnit,
  4449. AdviceId: advice.ID,
  4450. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4451. StorehouseId: advice.StorehouseId,
  4452. AdminUserId: advice.AdminUserId,
  4453. IsCheck: 1,
  4454. }
  4455. if warehouse.RetailPrice == 0 {
  4456. warehouseOutInfo.Price = advice.Price
  4457. }
  4458. warehouseOutInfo.Count = stock_number
  4459. //warehouseOutInfo.CountUnit = advice.CountUnit
  4460. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  4461. if errOne != nil {
  4462. return errOne
  4463. }
  4464. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4465. drugflow := models.DrugFlow{
  4466. WarehouseOutId: warehouseout.ID,
  4467. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4468. DrugId: advice.DrugId,
  4469. Number: warehouse.Number,
  4470. ProductDate: advice.ProductDate,
  4471. ExpireDate: advice.ExpiryDate,
  4472. Count: stock_number,
  4473. Price: warehouse.RetailPrice,
  4474. Status: 1,
  4475. Ctime: time.Now().Unix(),
  4476. UserOrgId: advice.OrgId,
  4477. Manufacturer: advice.Manufacturer,
  4478. Dealer: advice.Dealer,
  4479. BatchNumber: warehouse.BatchNumber,
  4480. MaxUnit: drup.MinUnit,
  4481. ConsumableType: 2,
  4482. IsEdit: 1,
  4483. Creator: 0,
  4484. IsSys: 0,
  4485. WarehouseOutDetailId: drugWareInfo.ID,
  4486. AdviceId: advice.ID,
  4487. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4488. StorehouseId: advice.StorehouseId,
  4489. AdminUserId: advice.AdminUserId,
  4490. }
  4491. if warehouse.RetailPrice == 0 {
  4492. drugflow.Price = advice.Price
  4493. }
  4494. CreateDrugFlowOne(drugflow)
  4495. // 出库完成后,要将该批次库存清零
  4496. //扣减库存
  4497. warehouse.StockMaxNumber = 0
  4498. warehouse.StockMinNumber = 0
  4499. warehouse.Mtime = time.Now().Unix()
  4500. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4501. //查询默认仓库
  4502. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  4503. //查询默认仓库剩余多少库存
  4504. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  4505. var sum_count int64
  4506. var sum_in_count int64
  4507. for _, it := range list {
  4508. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  4509. if it.MaxUnit == baseDrug.MaxUnit {
  4510. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  4511. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  4512. sum_count += it.StockMaxNumber + it.StockMinNumber
  4513. sum_in_count += it.WarehousingCount
  4514. }
  4515. }
  4516. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  4517. if errThree != nil {
  4518. return errThree
  4519. }
  4520. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  4521. prescribingNumber_two_temp := deliver_number - stock_number
  4522. advice.CountUnit = drup.MinUnit
  4523. AutoDrugDeliverInfoTwenty(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  4524. }
  4525. return
  4526. }
  4527. // 药品手动出库 递归方式
  4528. func AutoDrugDeliverInfoTwentyOne(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  4529. //开事务
  4530. //tx := XTWriteDB().Begin()
  4531. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  4532. var deliver_number int64 = 0
  4533. var stock_number int64 = 0
  4534. if advice.CountUnit == drup.MaxUnit {
  4535. deliver_number = prescribingNumber * drup.MinNumber
  4536. } else {
  4537. deliver_number = prescribingNumber
  4538. }
  4539. // 根据先进先出原则,查询最先入库的批次,进行出库
  4540. // 如果没有对应的库存,则报错
  4541. //开启事物
  4542. storeConfig, _ := GetAllStoreHouseConfig(orgID)
  4543. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  4544. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  4545. var stockMax int64
  4546. var stockMin int64
  4547. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  4548. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  4549. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  4550. UpdateMinNumber(lastWarehouse.ID, stockMin)
  4551. }
  4552. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  4553. if err != nil {
  4554. return err
  4555. }
  4556. // 将该批次的剩余库存数量转换为拆零数量
  4557. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  4558. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  4559. if stock_number >= deliver_number {
  4560. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4561. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4562. WarehouseOutId: warehouseout.ID,
  4563. Status: 1,
  4564. Ctime: time.Now().Unix(),
  4565. Remark: advice.Remark,
  4566. OrgId: orgID,
  4567. Type: 1,
  4568. Manufacturer: advice.Manufacturer,
  4569. Dealer: warehouse.Dealer,
  4570. IsSys: 0,
  4571. SysRecordTime: advice.Ctime,
  4572. DrugId: advice.DrugId,
  4573. ExpiryDate: advice.ExpiryDate,
  4574. ProductDate: advice.ProductDate,
  4575. Number: advice.Number,
  4576. BatchNumber: warehouse.BatchNumber,
  4577. Count: deliver_number,
  4578. RetailPrice: advice.RetailPrice,
  4579. Price: advice.Price,
  4580. WarehouseInfoId: warehouse.ID,
  4581. CountUnit: drup.MinUnit,
  4582. AdviceId: advice.ID,
  4583. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4584. StorehouseId: advice.StorehouseId,
  4585. AdminUserId: advice.AdminUserId,
  4586. LastPrice: warehouse.Price,
  4587. StockCount: advice.StockCount,
  4588. IsCheck: 1,
  4589. }
  4590. warehouseOutInfo.Count = prescribingNumber
  4591. warehouseOutInfo.CountUnit = advice.CountUnit
  4592. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4593. drugflow := models.DrugFlow{
  4594. WarehouseOutId: warehouseout.ID,
  4595. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4596. DrugId: advice.DrugId,
  4597. Number: warehouse.Number,
  4598. ProductDate: advice.ProductDate,
  4599. ExpireDate: advice.ExpiryDate,
  4600. Count: deliver_number,
  4601. Price: advice.Price,
  4602. Status: 1,
  4603. Ctime: time.Now().Unix(),
  4604. UserOrgId: advice.OrgId,
  4605. Manufacturer: advice.Manufacturer,
  4606. Dealer: advice.Dealer,
  4607. BatchNumber: warehouse.BatchNumber,
  4608. MaxUnit: drup.MinUnit,
  4609. ConsumableType: 2,
  4610. IsEdit: 1,
  4611. Creator: 0,
  4612. IsSys: 0,
  4613. WarehouseOutDetailId: drugWareInfo.ID,
  4614. AdviceId: advice.ID,
  4615. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4616. StorehouseId: advice.StorehouseId,
  4617. AdminUserId: advice.AdminUserId,
  4618. LastPrice: warehouse.Price,
  4619. StockCount: advice.StockCount,
  4620. }
  4621. if warehouse.RetailPrice == 0 {
  4622. drugflow.Price = advice.Price
  4623. }
  4624. CreateDrugFlowOne(drugflow)
  4625. // 出库完成后,要减去对应批次的库存数量
  4626. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  4627. var maxNumber int64 = 0
  4628. var minNumber int64 = 0
  4629. //var stock_max_number int64 = 0
  4630. //stock_max_number = warehouse.StockMaxNumber
  4631. maxNumber = deliver_number / drup.MinNumber
  4632. minNumber = deliver_number % drup.MinNumber
  4633. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  4634. minNumber = maxNumber
  4635. }
  4636. if drup.MaxUnit != drup.MinUnit {
  4637. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  4638. return errors.New("库存数量不足")
  4639. }
  4640. }
  4641. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  4642. if warehouse.StockMaxNumber < 0 {
  4643. warehouse.StockMaxNumber = 0
  4644. }
  4645. if warehouse.StockMinNumber < 0 {
  4646. warehouse.StockMinNumber = 0
  4647. }
  4648. warehouse.Mtime = time.Now().Unix()
  4649. if warehouse.StockMinNumber < minNumber {
  4650. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  4651. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  4652. } else {
  4653. if minNumber > 0 {
  4654. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  4655. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4656. } else {
  4657. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  4658. }
  4659. }
  4660. if minNumber == 0 && maxNumber != 1 {
  4661. if warehouse.StockMinNumber > 0 {
  4662. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4663. }
  4664. }
  4665. }
  4666. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  4667. if (warehouse.StockMinNumber - deliver_number) > 0 {
  4668. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4669. //if warehouse.StockMinNumber == 0 {
  4670. // warehouse.StockMaxNumber = stock_max_number
  4671. //}
  4672. }
  4673. }
  4674. if drup.MaxUnit != drup.MinUnit {
  4675. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  4676. return errors.New("库存数量不足")
  4677. }
  4678. }
  4679. if warehouse.StockMinNumber <= 0 {
  4680. warehouse.StockMinNumber = 0
  4681. }
  4682. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4683. fmt.Println(errThree)
  4684. //if errThree != nil {
  4685. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  4686. // tx.Rollback()
  4687. //} else {
  4688. // tx.Commit()
  4689. //}
  4690. //查询默认仓库
  4691. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  4692. //查询默认仓库剩余多少库存
  4693. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  4694. var sum_count int64
  4695. var sum_in_count int64
  4696. for _, it := range list {
  4697. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  4698. if it.MaxUnit == baseDrug.MaxUnit {
  4699. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  4700. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  4701. sum_count += it.StockMaxNumber + it.StockMinNumber
  4702. sum_in_count += it.WarehousingCount
  4703. }
  4704. }
  4705. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  4706. return nil
  4707. } else {
  4708. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  4709. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4710. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4711. WarehouseOutId: warehouseout.ID,
  4712. Status: 1,
  4713. Ctime: time.Now().Unix(),
  4714. Remark: advice.Remark,
  4715. OrgId: orgID,
  4716. Type: 1,
  4717. Manufacturer: advice.Manufacturer,
  4718. Dealer: warehouse.Dealer,
  4719. IsSys: 0,
  4720. SysRecordTime: advice.Ctime,
  4721. DrugId: advice.DrugId,
  4722. ExpiryDate: warehouse.ExpiryDate,
  4723. ProductDate: warehouse.ProductDate,
  4724. Number: warehouse.Number,
  4725. BatchNumber: warehouse.BatchNumber,
  4726. Count: stock_number,
  4727. RetailPrice: advice.RetailPrice,
  4728. Price: advice.Price,
  4729. WarehouseInfoId: warehouse.ID,
  4730. CountUnit: drup.MinUnit,
  4731. AdviceId: advice.ID,
  4732. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4733. StorehouseId: advice.StorehouseId,
  4734. AdminUserId: advice.AdminUserId,
  4735. LastPrice: warehouse.Price,
  4736. StockCount: advice.StockCount,
  4737. IsCheck: 1,
  4738. }
  4739. warehouseOutInfo.Count = stock_number
  4740. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4741. drugflow := models.DrugFlow{
  4742. WarehouseOutId: warehouseout.ID,
  4743. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4744. DrugId: advice.DrugId,
  4745. Number: warehouse.Number,
  4746. ProductDate: advice.ProductDate,
  4747. ExpireDate: advice.ExpiryDate,
  4748. Count: stock_number,
  4749. Price: advice.Price,
  4750. Status: 1,
  4751. Ctime: time.Now().Unix(),
  4752. UserOrgId: advice.OrgId,
  4753. Manufacturer: advice.Manufacturer,
  4754. Dealer: advice.Dealer,
  4755. BatchNumber: warehouse.BatchNumber,
  4756. MaxUnit: drup.MinUnit,
  4757. ConsumableType: 2,
  4758. IsEdit: 1,
  4759. Creator: 0,
  4760. IsSys: 0,
  4761. WarehouseOutDetailId: drugWareInfo.ID,
  4762. AdviceId: advice.ID,
  4763. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4764. StorehouseId: advice.StorehouseId,
  4765. AdminUserId: advice.AdminUserId,
  4766. LastPrice: warehouse.Price,
  4767. StockCount: advice.StockCount,
  4768. }
  4769. CreateDrugFlowOne(drugflow)
  4770. // 出库完成后,要将该批次库存清零
  4771. //扣减库存
  4772. warehouse.StockMaxNumber = 0
  4773. warehouse.StockMinNumber = 0
  4774. warehouse.Mtime = time.Now().Unix()
  4775. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4776. fmt.Println(errThree)
  4777. //if errThree != nil {
  4778. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  4779. // tx.Rollback()
  4780. //} else {
  4781. // tx.Commit()
  4782. //}
  4783. //查询默认仓库
  4784. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  4785. //查询默认仓库剩余多少库存
  4786. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  4787. var sum_count int64
  4788. var sum_in_count int64
  4789. for _, it := range list {
  4790. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  4791. if it.MaxUnit == baseDrug.MaxUnit {
  4792. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  4793. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  4794. sum_count += it.StockMaxNumber + it.StockMinNumber
  4795. sum_in_count += it.WarehousingCount
  4796. }
  4797. }
  4798. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  4799. if errThree != nil {
  4800. return errThree
  4801. }
  4802. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  4803. prescribingNumber_two_temp := deliver_number - stock_number
  4804. advice.CountUnit = drup.MinUnit
  4805. AutoDrugDeliverInfoTwentyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  4806. }
  4807. return
  4808. }
  4809. // 耗材手动出库
  4810. func ConsumablesDeliveryThirty(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64) (err error) {
  4811. var deliver_number int64 = 0
  4812. var stock_number int64 = 0
  4813. var maxNumber int64 = 0
  4814. deliver_number = goods.Count
  4815. // 根据先进先出原则,查询最先入库的批次,进行出库
  4816. // 如果没有对应的库存,则报错
  4817. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  4818. if err != nil {
  4819. goodErrcode := models.XtGoodErrcode{
  4820. UserOrgId: goods.OrgId,
  4821. Errcode: "查询库存信息报错",
  4822. GoodId: goods.GoodId,
  4823. Status: 1,
  4824. Ctime: time.Now().Unix(),
  4825. Mtime: 0,
  4826. Count: 0,
  4827. StockCount: 0,
  4828. Creater: creator,
  4829. BatchNumberId: warehouse.ID,
  4830. WarehouseOutId: 0,
  4831. }
  4832. CreateGoodErrcode(goodErrcode)
  4833. return err
  4834. }
  4835. stock_number = warehouse.StockCount
  4836. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  4837. if stock_number >= deliver_number {
  4838. maxNumber = goods.Count
  4839. warehouse.StockCount = warehouse.StockCount - maxNumber
  4840. warehouse.Mtime = time.Now().Unix()
  4841. //扣减库存
  4842. errThree := UpDateWarehouseInfoByStock(&warehouse)
  4843. if errThree != nil {
  4844. goodErrcode := models.XtGoodErrcode{
  4845. UserOrgId: goods.OrgId,
  4846. Errcode: "扣减库存失败",
  4847. GoodId: goods.GoodId,
  4848. Status: 1,
  4849. Ctime: time.Now().Unix(),
  4850. Mtime: 0,
  4851. Count: 0,
  4852. StockCount: 0,
  4853. Creater: creator,
  4854. BatchNumberId: warehouse.ID,
  4855. WarehouseOutId: 0,
  4856. }
  4857. CreateGoodErrcode(goodErrcode)
  4858. return errThree
  4859. }
  4860. //查询剩余库存
  4861. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  4862. var sum_count int64
  4863. for _, item := range goodList {
  4864. sum_count += item.StockCount
  4865. }
  4866. warehouseOutInfo := &models.WarehouseOutInfo{
  4867. WarehouseInfotId: warehouse.ID,
  4868. }
  4869. //添加入库单详情ID
  4870. errOne := UpdatedSigleWarehouseOutInfo(goods.ID, warehouseOutInfo)
  4871. if errOne != nil {
  4872. goodErrcode := models.XtGoodErrcode{
  4873. UserOrgId: goods.OrgId,
  4874. Errcode: "添加入库单详情ID",
  4875. GoodId: goods.GoodId,
  4876. Status: 1,
  4877. Ctime: time.Now().Unix(),
  4878. Mtime: 0,
  4879. Count: 0,
  4880. StockCount: 0,
  4881. Creater: creator,
  4882. BatchNumberId: warehouse.ID,
  4883. WarehouseOutId: 0,
  4884. }
  4885. CreateGoodErrcode(goodErrcode)
  4886. return errOne
  4887. }
  4888. stockFlow := models.VmStockFlow{
  4889. WarehousingId: warehouse.ID,
  4890. GoodId: goods.GoodId,
  4891. Number: warehouse.Number,
  4892. LicenseNumber: goods.LicenseNumber,
  4893. Count: goods.Count,
  4894. UserOrgId: goods.OrgId,
  4895. PatientId: goods.PatientId,
  4896. SystemTime: record_time,
  4897. ConsumableType: 2,
  4898. IsSys: goods.IsSys,
  4899. WarehousingOrder: warehouse.WarehousingOrder,
  4900. WarehouseOutId: warehouseOut.ID,
  4901. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  4902. IsEdit: 1,
  4903. CancelStockId: 0,
  4904. CancelOrderNumber: "",
  4905. Manufacturer: goods.Manufacturer,
  4906. Dealer: goods.Dealer,
  4907. Creator: creator,
  4908. UpdateCreator: 0,
  4909. Status: 1,
  4910. Ctime: record_time,
  4911. Mtime: time.Now().Unix(),
  4912. Price: warehouse.PackingPrice, //零售价
  4913. WarehousingDetailId: warehouse.ID,
  4914. WarehouseOutDetailId: goods.ID,
  4915. CancelOutDetailId: 0,
  4916. ProductDate: goods.ProductDate,
  4917. ExpireDate: goods.ExpiryDate,
  4918. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4919. StorehouseId: warehouse.StorehouseId,
  4920. AdminUserId: goods.AdminUserId,
  4921. BuyPrice: warehouse.Price, //进货价
  4922. StockCount: goods.StockCount,
  4923. BatchNumberCount: warehouse.StockCount - goods.Count,
  4924. IsCheck: 1,
  4925. OverCount: sum_count,
  4926. RegisterNumber: goods.RegisterNumber,
  4927. Remark: goods.Remark,
  4928. }
  4929. //创建出库流水
  4930. errflow := CreateStockFlowOne(stockFlow)
  4931. if errflow != nil {
  4932. goodErrcode := models.XtGoodErrcode{
  4933. UserOrgId: goods.OrgId,
  4934. Errcode: "创建流水报错",
  4935. GoodId: goods.GoodId,
  4936. Status: 1,
  4937. Ctime: time.Now().Unix(),
  4938. Mtime: 0,
  4939. Count: 0,
  4940. StockCount: 0,
  4941. Creater: creator,
  4942. BatchNumberId: warehouse.ID,
  4943. WarehouseOutId: 0,
  4944. }
  4945. CreateGoodErrcode(goodErrcode)
  4946. }
  4947. //添加入库ID
  4948. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, goods.OrgId, sum_count)
  4949. if errWarehouse != nil {
  4950. goodErrcode := models.XtGoodErrcode{
  4951. UserOrgId: goods.OrgId,
  4952. Errcode: "添加入库ID报错",
  4953. GoodId: goods.GoodId,
  4954. Status: 1,
  4955. Ctime: time.Now().Unix(),
  4956. Mtime: 0,
  4957. Count: 0,
  4958. StockCount: 0,
  4959. Creater: creator,
  4960. BatchNumberId: warehouse.ID,
  4961. WarehouseOutId: 0,
  4962. }
  4963. CreateGoodErrcode(goodErrcode)
  4964. }
  4965. //更新出库数量
  4966. errsumcode := ModifyGoodSumCount(goods.StorehouseId, goods.Count, goods.OrgId, goods.GoodId)
  4967. if errsumcode != nil {
  4968. goodErrcode := models.XtGoodErrcode{
  4969. UserOrgId: goods.OrgId,
  4970. Errcode: "更新出库数量报错",
  4971. GoodId: goods.GoodId,
  4972. Status: 1,
  4973. Ctime: time.Now().Unix(),
  4974. Mtime: 0,
  4975. Count: 0,
  4976. StockCount: 0,
  4977. Creater: creator,
  4978. BatchNumberId: warehouse.ID,
  4979. WarehouseOutId: 0,
  4980. }
  4981. CreateGoodErrcode(goodErrcode)
  4982. }
  4983. if warehouse.StockCount < maxNumber {
  4984. goodErrcode := models.XtGoodErrcode{
  4985. UserOrgId: goods.OrgId,
  4986. Errcode: "出库接口库存数量不足",
  4987. GoodId: goods.GoodId,
  4988. Status: 1,
  4989. Ctime: time.Now().Unix(),
  4990. Mtime: 0,
  4991. Count: 0,
  4992. StockCount: 0,
  4993. Creater: creator,
  4994. BatchNumberId: warehouse.ID,
  4995. WarehouseOutId: 0,
  4996. }
  4997. CreateGoodErrcode(goodErrcode)
  4998. return errors.New("库存数量不足")
  4999. }
  5000. return nil
  5001. } else {
  5002. // 出库完成后,要将该批次库存清零
  5003. warehouse.StockCount = 0
  5004. warehouse.Mtime = time.Now().Unix()
  5005. errThree := UpDateWarehouseInfoByStock(&warehouse)
  5006. if errThree != nil {
  5007. goodErrcode := models.XtGoodErrcode{
  5008. UserOrgId: goods.OrgId,
  5009. Errcode: "扣减库存报错",
  5010. GoodId: goods.GoodId,
  5011. Status: 1,
  5012. Ctime: time.Now().Unix(),
  5013. Mtime: 0,
  5014. Count: 0,
  5015. StockCount: 0,
  5016. Creater: creator,
  5017. BatchNumberId: warehouse.ID,
  5018. WarehouseOutId: 0,
  5019. }
  5020. CreateGoodErrcode(goodErrcode)
  5021. return errThree
  5022. }
  5023. //查询剩余库存
  5024. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  5025. var sum_count int64
  5026. for _, item := range goodList {
  5027. sum_count += item.StockCount
  5028. }
  5029. stockFlow := models.VmStockFlow{
  5030. WarehousingId: warehouse.ID,
  5031. GoodId: goods.GoodId,
  5032. Number: warehouse.Number,
  5033. LicenseNumber: goods.LicenseNumber,
  5034. Count: stock_number,
  5035. UserOrgId: goods.OrgId,
  5036. PatientId: goods.PatientId,
  5037. SystemTime: record_time,
  5038. ConsumableType: 2,
  5039. IsSys: goods.IsSys,
  5040. WarehousingOrder: warehouse.WarehousingOrder,
  5041. WarehouseOutId: warehouseOut.ID,
  5042. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  5043. IsEdit: 1,
  5044. CancelStockId: 0,
  5045. CancelOrderNumber: "",
  5046. Manufacturer: goods.Manufacturer,
  5047. Dealer: goods.Dealer,
  5048. Creator: creator,
  5049. UpdateCreator: 0,
  5050. Status: 1,
  5051. Ctime: record_time,
  5052. Mtime: time.Now().Unix(),
  5053. Price: warehouse.PackingPrice, //零售价
  5054. WarehousingDetailId: warehouse.ID,
  5055. WarehouseOutDetailId: goods.ID,
  5056. CancelOutDetailId: 0,
  5057. ProductDate: goods.ProductDate,
  5058. ExpireDate: goods.ExpiryDate,
  5059. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5060. StorehouseId: warehouse.StorehouseId,
  5061. AdminUserId: goods.AdminUserId,
  5062. BuyPrice: warehouse.Price, //进货价
  5063. StockCount: goods.StockCount,
  5064. BatchNumberCount: 0,
  5065. IsCheck: 1,
  5066. OverCount: sum_count,
  5067. RegisterNumber: goods.RegisterNumber,
  5068. Remark: goods.Remark,
  5069. }
  5070. //创建出库流水
  5071. errflow := CreateStockFlowOne(stockFlow)
  5072. if errflow != nil {
  5073. goodErrcode := models.XtGoodErrcode{
  5074. UserOrgId: goods.OrgId,
  5075. Errcode: "创建出库流水报错",
  5076. GoodId: goods.GoodId,
  5077. Status: 1,
  5078. Ctime: time.Now().Unix(),
  5079. Mtime: 0,
  5080. Count: 0,
  5081. StockCount: 0,
  5082. Creater: creator,
  5083. BatchNumberId: warehouse.ID,
  5084. WarehouseOutId: 0,
  5085. }
  5086. CreateGoodErrcode(goodErrcode)
  5087. }
  5088. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, goods.OrgId, sum_count)
  5089. if errWarehouse != nil {
  5090. goodErrcode := models.XtGoodErrcode{
  5091. UserOrgId: goods.OrgId,
  5092. Errcode: "添加入库ID报错",
  5093. GoodId: goods.GoodId,
  5094. Status: 1,
  5095. Ctime: time.Now().Unix(),
  5096. Mtime: 0,
  5097. Count: 0,
  5098. StockCount: 0,
  5099. Creater: creator,
  5100. BatchNumberId: warehouse.ID,
  5101. WarehouseOutId: 0,
  5102. }
  5103. CreateGoodErrcode(goodErrcode)
  5104. }
  5105. //更新出库数量
  5106. errcodecout := ModifyGoodSumCount(goods.StorehouseId, stock_number, goods.OrgId, goods.GoodId)
  5107. if errcodecout != nil {
  5108. goodErrcode := models.XtGoodErrcode{
  5109. UserOrgId: goods.OrgId,
  5110. Errcode: "更新出库数量报错",
  5111. GoodId: goods.GoodId,
  5112. Status: 1,
  5113. Ctime: time.Now().Unix(),
  5114. Mtime: 0,
  5115. Count: 0,
  5116. StockCount: 0,
  5117. Creater: creator,
  5118. BatchNumberId: warehouse.ID,
  5119. WarehouseOutId: 0,
  5120. }
  5121. CreateGoodErrcode(goodErrcode)
  5122. }
  5123. goods.Count = deliver_number - stock_number
  5124. ConsumablesDeliveryThirty(orgID, record_time, goods, warehouseOut, count, creator)
  5125. }
  5126. return nil
  5127. }
  5128. // 药品手动出库 递归方式
  5129. func AutoDrugDeliverInfoFourtyOne(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo, unit string) (err error) {
  5130. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  5131. var deliver_number int64 = 0
  5132. var stock_number int64 = 0
  5133. var drug_price float64
  5134. if advice.CountUnit == drup.MaxUnit {
  5135. deliver_number = prescribingNumber * drup.MinNumber
  5136. } else {
  5137. deliver_number = prescribingNumber
  5138. }
  5139. if advice.CountUnit == drup.MaxUnit {
  5140. drug_price = drup.RetailPrice
  5141. }
  5142. fmt.Println(drug_price)
  5143. if advice.CountUnit == drup.MinUnit {
  5144. drug_price = drup.MinPrice
  5145. }
  5146. if advice.CountUnit != drup.MaxUnit || advice.CountUnit != drup.MinUnit {
  5147. drug_price = drup.RetailPrice
  5148. }
  5149. // 根据先进先出原则,查询最先入库的批次,进行出库
  5150. // 如果没有对应的库存,则报错
  5151. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, advice.StorehouseId)
  5152. if lastWarehouse.MaxUnit != lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  5153. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  5154. var stockMax int64
  5155. var stockMin int64
  5156. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  5157. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  5158. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  5159. UpdateMinNumber(lastWarehouse.ID, stockMin)
  5160. }
  5161. }
  5162. if lastWarehouse.MaxUnit == lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  5163. lastWarehouse.StockMaxNumber = lastWarehouse.StockMaxNumber + lastWarehouse.StockMinNumber
  5164. ChangeMaxNumberOne(lastWarehouse.ID, lastWarehouse.StockMaxNumber)
  5165. }
  5166. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, advice.StorehouseId)
  5167. // 将该批次的剩余库存数量转换为拆零数量
  5168. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  5169. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  5170. }
  5171. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  5172. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  5173. }
  5174. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  5175. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  5176. }
  5177. if stock_number >= deliver_number {
  5178. var maxNumber int64 = 0
  5179. var minNumber int64 = 0
  5180. maxNumber = deliver_number / drup.MinNumber
  5181. minNumber = deliver_number % drup.MinNumber
  5182. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  5183. minNumber = maxNumber
  5184. }
  5185. if drup.MaxUnit != drup.MinUnit {
  5186. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  5187. return errors.New("库存数量不足")
  5188. }
  5189. }
  5190. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  5191. //fmt.Println("金1")
  5192. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  5193. }
  5194. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  5195. //fmt.Println("金2")
  5196. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  5197. }
  5198. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  5199. //fmt.Println("金3")
  5200. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  5201. }
  5202. if warehouse.StockMaxNumber < 0 {
  5203. warehouse.StockMaxNumber = 0
  5204. }
  5205. if warehouse.StockMinNumber < 0 {
  5206. warehouse.StockMinNumber = 0
  5207. }
  5208. warehouse.Mtime = time.Now().Unix()
  5209. //fmt.Println("stockmin", warehouse.StockMinNumber)
  5210. //fmt.Println("minnubmerr", minNumber)
  5211. if warehouse.StockMinNumber < minNumber {
  5212. //fmt.Println("金4")
  5213. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  5214. if warehouse.MaxUnit != warehouse.MinUnit {
  5215. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  5216. }
  5217. if minNumber == 2 && warehouse.MaxUnit == warehouse.MinUnit && warehouse.StockMinNumber == 0 {
  5218. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  5219. }
  5220. } else {
  5221. //fmt.Println("金5")
  5222. if minNumber > 0 {
  5223. if (warehouse.StockMinNumber - minNumber) >= 0 {
  5224. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  5225. }
  5226. }
  5227. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  5228. //fmt.Println("6")
  5229. if warehouse.StockMinNumber > 0 {
  5230. if warehouse.StockMinNumber-deliver_number >= 0 {
  5231. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  5232. }
  5233. }
  5234. }
  5235. }
  5236. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit && warehouse.MaxUnit != warehouse.MinUnit {
  5237. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  5238. //fmt.Println("金7")
  5239. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  5240. }
  5241. }
  5242. if drup.MaxUnit != drup.MinUnit {
  5243. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  5244. return errors.New("库存数量不足")
  5245. }
  5246. }
  5247. if warehouse.StockMinNumber <= 0 {
  5248. warehouse.StockMinNumber = 0
  5249. }
  5250. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  5251. if errThree != nil {
  5252. return errors.New("扣减库存失败")
  5253. }
  5254. //查询该药品剩余库存
  5255. infolist, _ := FindDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId)
  5256. var over_count int64
  5257. for _, its := range infolist {
  5258. if its.MaxUnit == drup.MaxUnit {
  5259. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  5260. }
  5261. over_count += its.StockMaxNumber + its.StockMinNumber
  5262. }
  5263. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  5264. drugflow := models.DrugFlow{
  5265. WarehouseOutId: warehouseout.ID,
  5266. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  5267. DrugId: advice.DrugId,
  5268. Number: warehouse.Number,
  5269. ProductDate: advice.ProductDate,
  5270. ExpireDate: advice.ExpiryDate,
  5271. Count: deliver_number,
  5272. Price: warehouse.RetailPrice,
  5273. Status: 1,
  5274. Ctime: advice.SysRecordTime,
  5275. Mtime: time.Now().Unix(),
  5276. UserOrgId: advice.OrgId,
  5277. Manufacturer: advice.Manufacturer,
  5278. Dealer: advice.Dealer,
  5279. BatchNumber: warehouse.BatchNumber,
  5280. MaxUnit: drup.MinUnit,
  5281. ConsumableType: 2,
  5282. IsEdit: 1,
  5283. Creator: 0,
  5284. IsSys: 0,
  5285. WarehouseOutDetailId: drugWareInfo.ID,
  5286. AdviceId: advice.ID,
  5287. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5288. StorehouseId: advice.StorehouseId,
  5289. AdminUserId: advice.AdminUserId,
  5290. LastPrice: warehouse.Price,
  5291. StockCount: advice.StockCount,
  5292. WarehousingDetailId: warehouse.ID,
  5293. OverCount: over_count,
  5294. RetailPrice: drug_price, //手动出库出库价格
  5295. WarehousingId: warehouse.ID,
  5296. SystemTime: advice.SysRecordTime,
  5297. PatientId: advice.PatientId,
  5298. Remark: advice.Remark,
  5299. }
  5300. if warehouse.RetailPrice == 0 {
  5301. drugflow.Price = advice.Price
  5302. }
  5303. CreateDrugFlowOne(drugflow)
  5304. //更新出库id
  5305. UpdateDrugWarehouseOutInfo(advice.ID, warehouse.ID, advice.OrgId, over_count)
  5306. return nil
  5307. } else {
  5308. stock_number_int := strconv.FormatInt(stock_number, 10)
  5309. stock_number_int_count, _ := strconv.ParseFloat(stock_number_int, 64)
  5310. drug_price = stock_number_int_count * advice.Price
  5311. //扣减库存
  5312. warehouse.StockMaxNumber = 0
  5313. warehouse.StockMinNumber = 0
  5314. warehouse.Mtime = time.Now().Unix()
  5315. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  5316. if errThree != nil {
  5317. return errThree
  5318. }
  5319. //查询该药品剩余库存
  5320. infolist, _ := FindDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId)
  5321. var over_count int64
  5322. for _, its := range infolist {
  5323. if its.MaxUnit == drup.MaxUnit {
  5324. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  5325. }
  5326. over_count += its.StockMaxNumber + its.StockMinNumber
  5327. }
  5328. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  5329. drugflow := models.DrugFlow{
  5330. WarehouseOutId: warehouseout.ID,
  5331. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  5332. DrugId: advice.DrugId,
  5333. Number: warehouse.Number,
  5334. ProductDate: advice.ProductDate,
  5335. ExpireDate: advice.ExpiryDate,
  5336. Count: stock_number,
  5337. Price: warehouse.RetailPrice,
  5338. Status: 1,
  5339. Ctime: advice.SysRecordTime,
  5340. Mtime: time.Now().Unix(),
  5341. UserOrgId: advice.OrgId,
  5342. Manufacturer: advice.Manufacturer,
  5343. Dealer: advice.Dealer,
  5344. BatchNumber: warehouse.BatchNumber,
  5345. MaxUnit: drup.MinUnit,
  5346. ConsumableType: 2,
  5347. IsEdit: 1,
  5348. Creator: 0,
  5349. IsSys: 0,
  5350. WarehouseOutDetailId: drugWareInfo.ID,
  5351. AdviceId: advice.ID,
  5352. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5353. StorehouseId: advice.StorehouseId,
  5354. AdminUserId: advice.AdminUserId,
  5355. LastPrice: warehouse.Price,
  5356. StockCount: advice.StockCount,
  5357. OverCount: over_count,
  5358. RetailPrice: drug_price, //手动出库出库价格
  5359. WarehousingId: warehouse.ID,
  5360. WarehousingDetailId: warehouse.ID,
  5361. SystemTime: advice.SysRecordTime,
  5362. PatientId: advice.PatientId,
  5363. Remark: advice.Remark,
  5364. }
  5365. CreateDrugFlowOne(drugflow)
  5366. //更新出库id
  5367. UpdateDrugWarehouseOutInfo(advice.ID, warehouse.ID, advice.OrgId, over_count)
  5368. // 出库完成后,要将该批次库存清零
  5369. prescribingNumber_two_temp := deliver_number - stock_number
  5370. advice.CountUnit = drup.MinUnit
  5371. AutoDrugDeliverInfoFourtyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, unit)
  5372. }
  5373. return
  5374. }
  5375. func UpdateDrugWarehouseOutInfo(id int64, warehouse_info_id int64, org_id int64, over_count int64) error {
  5376. ut := XTWriteDB().Begin()
  5377. err = ut.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and org_id = ? and status = 1", id, org_id).Update(map[string]interface{}{"warehouse_info_id": warehouse_info_id, "over_count": over_count}).Error
  5378. if err != nil {
  5379. ut.Rollback()
  5380. return err
  5381. }
  5382. ut.Commit()
  5383. return err
  5384. }
  5385. func UpdateGoodWarehouseOutById(id int64, warehouse_info_id int64, org_id int64, over_count int64) error {
  5386. utx := XTWriteDB().Begin()
  5387. 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
  5388. if err != nil {
  5389. utx.Rollback()
  5390. return err
  5391. }
  5392. utx.Commit()
  5393. return err
  5394. }
  5395. func ChangeMaxNumber(id int64, count int64) error {
  5396. 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
  5397. return err
  5398. }
  5399. func ChangeMaxNumberOne(id int64, count int64) error {
  5400. 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
  5401. return err
  5402. }
  5403. func UpdateMinNumber(id int64, count int64) error {
  5404. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1 and is_check = 1", id).Update(map[string]interface{}{"stock_min_number": count}).Error
  5405. return err
  5406. }
  5407. func FindLastDrugWarehouseOutInfo(orgid int64) (models.DrugWarehouseOutInfo, error) {
  5408. info := models.DrugWarehouseOutInfo{}
  5409. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&info).Error
  5410. return info, err
  5411. }
  5412. func GetAllGoodSumCount(goodid int64, orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  5413. 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
  5414. return info, err
  5415. }
  5416. func CreatedFlushInfo(info models.XtWarehouseFlushInfo) error {
  5417. ut := writeDb.Begin()
  5418. err := ut.Create(&info).Error
  5419. if err != nil {
  5420. ut.Rollback()
  5421. return err
  5422. }
  5423. ut.Commit()
  5424. return err
  5425. }
  5426. func FindDrugWarehouseInfoList(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5427. 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
  5428. return info, err
  5429. }
  5430. func CreatedDrugFlushInfo(info models.XtDrugWarehouseFlushInfo) error {
  5431. ut := writeDb.Begin()
  5432. err := ut.Create(&info).Error
  5433. if err != nil {
  5434. ut.Rollback()
  5435. return err
  5436. }
  5437. ut.Commit()
  5438. return err
  5439. }
  5440. 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 {
  5441. 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
  5442. return err
  5443. }
  5444. func FindLastWarehouseOutInfo(orgId int64) (models.WarehouseOutInfo, error) {
  5445. info := models.WarehouseOutInfo{}
  5446. err := XTReadDB().Model(&info).Where("org_id = ? and status = 1", orgId).Find(&info).Error
  5447. return info, err
  5448. }
  5449. func ModifyAddGoodInformation(count int64, id int64) error {
  5450. ut := XTWriteDB().Begin()
  5451. err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", count)).Error
  5452. if err != nil {
  5453. ut.Rollback()
  5454. return err
  5455. }
  5456. ut.Commit()
  5457. return err
  5458. }
  5459. func ModifyReduceGoodInformation(count int64, id int64) error {
  5460. ut := XTWriteDB().Begin()
  5461. err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", count)).Error
  5462. if err != nil {
  5463. ut.Rollback()
  5464. return err
  5465. }
  5466. ut.Commit()
  5467. return err
  5468. }
  5469. func ModifyAddCancelCount(count int64, id int64) error {
  5470. ut := XTWriteDB().Begin()
  5471. err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", count)).Error
  5472. if err != nil {
  5473. ut.Rollback()
  5474. return err
  5475. }
  5476. ut.Commit()
  5477. return err
  5478. }
  5479. func ModifyReduceCancelCount(count int64, id int64) error {
  5480. ut := XTWriteDB().Begin()
  5481. err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", count)).Error
  5482. if err != nil {
  5483. ut.Rollback()
  5484. return err
  5485. }
  5486. ut.Commit()
  5487. return err
  5488. }
  5489. func ModifyAddDrugInformation(id int64, count int64, orgid int64) error {
  5490. ut := XTWriteDB().Begin()
  5491. 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
  5492. if err != nil {
  5493. ut.Rollback()
  5494. return err
  5495. }
  5496. ut.Commit()
  5497. return err
  5498. }
  5499. func ModifyReduceDrugInformation(id int64, count int64, orgid int64) error {
  5500. ut := XTWriteDB().Begin()
  5501. 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
  5502. if err != nil {
  5503. ut.Rollback()
  5504. return err
  5505. }
  5506. ut.Commit()
  5507. return err
  5508. }
  5509. func ModifyAddCancelAddInfomation(id int64, count int64, orgid int64, storehouse_id int64) error {
  5510. ut := XTWriteDB().Begin()
  5511. 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
  5512. if err != nil {
  5513. ut.Rollback()
  5514. return err
  5515. }
  5516. ut.Commit()
  5517. return err
  5518. }
  5519. func ModifyAddDrugOutReduceInfomation(id int64, count int64, orgid int64, storehouse_id int64) error {
  5520. ut := XTWriteDB().Begin()
  5521. 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
  5522. if err != nil {
  5523. ut.Rollback()
  5524. return err
  5525. }
  5526. ut.Commit()
  5527. return err
  5528. }
  5529. func ModifyAddDrugOutAddInfomation(id int64, count int64, orgid int64, storehouse_id int64) error {
  5530. ut := XTWriteDB().Begin()
  5531. 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
  5532. if err != nil {
  5533. ut.Rollback()
  5534. return err
  5535. }
  5536. ut.Commit()
  5537. return err
  5538. }
  5539. func ModifyDrugCancelWarehouseInfo(id int64, count int64, orgid int64, storehouse_id int64) error {
  5540. ut := XTWriteDB().Begin()
  5541. 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
  5542. if err != nil {
  5543. ut.Rollback()
  5544. return err
  5545. }
  5546. ut.Commit()
  5547. return err
  5548. }
  5549. func ModifyGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
  5550. ut := XTWriteDB().Begin()
  5551. 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
  5552. if err != nil {
  5553. ut.Rollback()
  5554. return err
  5555. }
  5556. 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
  5557. if err != nil {
  5558. ut.Rollback()
  5559. return err
  5560. }
  5561. ut.Commit()
  5562. return err
  5563. }
  5564. func ModifyReduceGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
  5565. ut := XTWriteDB().Begin()
  5566. 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
  5567. if err != nil {
  5568. ut.Rollback()
  5569. return err
  5570. }
  5571. 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
  5572. if err != nil {
  5573. ut.Rollback()
  5574. return err
  5575. }
  5576. ut.Commit()
  5577. return err
  5578. }
  5579. func ModifyReduceGoodSumCountTwenty(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
  5580. ut := XTWriteDB().Begin()
  5581. 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
  5582. if err != nil {
  5583. ut.Rollback()
  5584. return err
  5585. }
  5586. ut.Commit()
  5587. return err
  5588. }
  5589. func ModifyAddGoodSumCount(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. ut.Commit()
  5597. return err
  5598. }
  5599. func UpdateSumAddCancelCount(user_org_id int64, good_id int64, storehouse_id int64, count int64) error {
  5600. ut := XTWriteDB().Begin()
  5601. 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
  5602. if err != nil {
  5603. ut.Rollback()
  5604. return err
  5605. }
  5606. ut.Commit()
  5607. return err
  5608. }
  5609. func UpdateSumReduceCancelCount(user_org_id int64, good_id int64, storehouse_id int64, count int64) error {
  5610. ut := XTWriteDB().Begin()
  5611. 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
  5612. if err != nil {
  5613. ut.Rollback()
  5614. return err
  5615. }
  5616. ut.Commit()
  5617. return err
  5618. }
  5619. func UpdateSumAddOutCount(user_org_id int64, good_id int64, storehouse_id int64, count int64) error {
  5620. ut := XTWriteDB().Begin()
  5621. 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
  5622. if err != nil {
  5623. ut.Rollback()
  5624. return err
  5625. }
  5626. ut.Commit()
  5627. return err
  5628. }
  5629. func FindDrugWarehouseInfoFlushCount(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5630. err = XTReadDB().Where("drug_id = ? and org_id = ? and status = 1 and is_check = 1", drug_id, orgid).Find(&info).Error
  5631. return info, err
  5632. }
  5633. func FindDrugWarehouseInfoFlushCountByStorehouse(drug_id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
  5634. 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
  5635. return info, err
  5636. }
  5637. // 结算出库
  5638. func HisSettleDrugsDelivery(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo, order_id int64) (err error) {
  5639. // 1.判断药品是否来自专用字典的药品库
  5640. // 2.判断当天当前机构有没有创建出库单,没有则创建
  5641. // 3.创建出库流程
  5642. // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  5643. isHasWay := false
  5644. record_time := int64(0)
  5645. isHasWay = true
  5646. record_time = advice.RecordDate
  5647. if isHasWay {
  5648. //判断当天当前机构有没有创建结算出库单,没有则创建
  5649. out, err := FindDrugSettleStockOutByIsSys(orgID, 5, record_time)
  5650. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  5651. if err == gorm.ErrRecordNotFound {
  5652. timeStr := time.Now().Format("2006-01-02")
  5653. timeArr := strings.Split(timeStr, "-")
  5654. total, _ := FindAllDrugWarehouseOut(orgID)
  5655. total = total + 1
  5656. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  5657. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  5658. number = number + total
  5659. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  5660. warehouseOut := models.DrugWarehouseOut{
  5661. WarehouseOutOrderNumber: warehousing_out_order,
  5662. OperationTime: time.Now().Unix(),
  5663. OrgId: orgID,
  5664. Creater: creater,
  5665. Ctime: time.Now().Unix(),
  5666. Status: 1,
  5667. WarehouseOutTime: record_time,
  5668. Dealer: 0,
  5669. Manufacturer: 0,
  5670. Type: 1, //结算出库
  5671. IsSys: 5,
  5672. StorehouseId: houseConfig.DrugStorehouseOut,
  5673. IsCheck: 1,
  5674. OrderId: order_id,
  5675. }
  5676. err := AddSigleDrugWarehouseOut(&warehouseOut)
  5677. if err != nil {
  5678. utils.TraceLog("创建出库单失败 err = %v", err)
  5679. return err
  5680. } else {
  5681. out = warehouseOut
  5682. }
  5683. }
  5684. // 出库流程
  5685. // 1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  5686. drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
  5687. if drup.ID > 0 {
  5688. prescribingNumber := advice.PrescribingNumber
  5689. var prescribingNumberOne int64
  5690. prescribingCount_temp := strconv.FormatFloat(math.Abs(advice.PrescribingNumber), 'f', 0, 64)
  5691. prescribe_count, _ := strconv.ParseInt(prescribingCount_temp, 10, 64)
  5692. if advice.PrescribingNumberUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  5693. prescribingNumberOne = prescribe_count * drup.MinNumber
  5694. }
  5695. if advice.PrescribingNumberUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  5696. prescribingNumberOne = prescribe_count
  5697. }
  5698. if advice.PrescribingNumberUnit == drup.MinUnit && drup.MaxUnit == drup.MinUnit {
  5699. prescribingNumberOne = prescribe_count
  5700. }
  5701. HisSettleDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice, prescribingNumberOne)
  5702. } else {
  5703. return errors.New("药品信息不存在")
  5704. }
  5705. }
  5706. return
  5707. }
  5708. // 耗材结算出库
  5709. func ConsumableSettleDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, creator int64, order_id int64) (err error) {
  5710. var deliver_number int64 = 0
  5711. var stock_number int64 = 0
  5712. var maxNumber int64 = 0
  5713. fmt.Println("223323233233223233223233223woo", goods.Count)
  5714. deliver_number = goods.Count
  5715. // 根据先进先出原则,查询最先入库的批次,进行出库
  5716. // 如果没有对应的库存,则报错
  5717. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  5718. if err != nil {
  5719. goodErrcode := models.XtGoodErrcode{
  5720. UserOrgId: goods.UserOrgId,
  5721. Errcode: "查询库存信息报错",
  5722. GoodId: goods.GoodId,
  5723. Status: 1,
  5724. Ctime: time.Now().Unix(),
  5725. Mtime: 0,
  5726. Count: 0,
  5727. StockCount: 0,
  5728. Creater: creator,
  5729. BatchNumberId: warehouse.ID,
  5730. WarehouseOutId: 0,
  5731. }
  5732. CreateGoodErrcode(goodErrcode)
  5733. return err
  5734. }
  5735. stock_number = warehouse.StockCount
  5736. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  5737. fmt.Println("stock_numnber233232232323", stock_number)
  5738. fmt.Println("deliver_nmuberwooeooo2oo23", deliver_number)
  5739. if stock_number >= deliver_number {
  5740. maxNumber = goods.Count
  5741. warehouse.StockCount = warehouse.StockCount - maxNumber
  5742. warehouse.Mtime = time.Now().Unix()
  5743. //扣减库存
  5744. errThree := UpDateWarehouseInfoByStock(&warehouse)
  5745. if errThree != nil {
  5746. goodErrcode := models.XtGoodErrcode{
  5747. UserOrgId: goods.UserOrgId,
  5748. Errcode: "扣减库存失败",
  5749. GoodId: goods.GoodId,
  5750. Status: 1,
  5751. Ctime: time.Now().Unix(),
  5752. Mtime: 0,
  5753. Count: 0,
  5754. StockCount: 0,
  5755. Creater: creator,
  5756. BatchNumberId: warehouse.ID,
  5757. WarehouseOutId: 0,
  5758. }
  5759. CreateGoodErrcode(goodErrcode)
  5760. return errThree
  5761. }
  5762. //查询剩余库存
  5763. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  5764. var sum_count int64
  5765. for _, item := range goodList {
  5766. sum_count += item.StockCount
  5767. }
  5768. warehouseOutInfo := &models.WarehouseOutInfo{
  5769. WarehouseInfotId: warehouse.ID,
  5770. }
  5771. //添加入库单详情ID
  5772. errOne := UpdatedSigleWarehouseOutInfo(goods.ID, warehouseOutInfo)
  5773. if errOne != nil {
  5774. goodErrcode := models.XtGoodErrcode{
  5775. UserOrgId: goods.UserOrgId,
  5776. Errcode: "添加入库单详情ID",
  5777. GoodId: goods.GoodId,
  5778. Status: 1,
  5779. Ctime: time.Now().Unix(),
  5780. Mtime: 0,
  5781. Count: 0,
  5782. StockCount: 0,
  5783. Creater: creator,
  5784. BatchNumberId: warehouse.ID,
  5785. WarehouseOutId: 0,
  5786. }
  5787. CreateGoodErrcode(goodErrcode)
  5788. return errOne
  5789. }
  5790. OutInfo := &models.WarehouseOutInfo{
  5791. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  5792. WarehouseOutId: warehouseOut.ID,
  5793. WarehouseInfotId: warehouse.ID,
  5794. Status: 1,
  5795. Ctime: time.Now().Unix(),
  5796. Remark: warehouse.Remark,
  5797. OrgId: orgID,
  5798. Type: 1,
  5799. Manufacturer: warehouse.Manufacturer,
  5800. Dealer: warehouse.Dealer,
  5801. IsSys: 5,
  5802. SysRecordTime: record_time,
  5803. GoodTypeId: goods.GoodTypeId,
  5804. GoodId: goods.GoodId,
  5805. PatientId: patient_id,
  5806. Number: warehouse.Number,
  5807. LicenseNumber: warehouse.LicenseNumber,
  5808. Price: warehouse.PackingPrice,
  5809. ExpiryDate: warehouse.ExpiryDate,
  5810. ProductDate: warehouse.ProductDate,
  5811. ProjectId: goods.ProjectId,
  5812. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5813. StorehouseId: goods.StorehouseId,
  5814. IsCheck: 1,
  5815. OverCount: sum_count,
  5816. RegisterNumber: warehouse.RegisterNumber,
  5817. OrderId: order_id,
  5818. }
  5819. fmt.Println("goods.count2323232233232233232322332322332", goods.Count)
  5820. OutInfo.Count = goods.Count
  5821. AddSigleWarehouseOutInfo(OutInfo)
  5822. stockFlow := models.VmStockFlow{
  5823. WarehousingId: warehouse.ID,
  5824. GoodId: goods.GoodId,
  5825. Number: warehouse.Number,
  5826. LicenseNumber: warehouse.LicenseNumber,
  5827. Count: goods.Count,
  5828. UserOrgId: orgID,
  5829. PatientId: patient_id,
  5830. SystemTime: record_time,
  5831. ConsumableType: 15,
  5832. IsSys: 5,
  5833. WarehousingOrder: warehouse.WarehousingOrder,
  5834. WarehouseOutId: warehouseOut.ID,
  5835. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  5836. IsEdit: 1,
  5837. CancelStockId: 0,
  5838. CancelOrderNumber: "",
  5839. Manufacturer: warehouse.Manufacturer,
  5840. Dealer: warehouse.Dealer,
  5841. Creator: creator,
  5842. UpdateCreator: 0,
  5843. Status: 1,
  5844. Ctime: time.Now().Unix(),
  5845. Mtime: 0,
  5846. Price: warehouse.PackingPrice, //零售价
  5847. WarehousingDetailId: warehouse.ID,
  5848. WarehouseOutDetailId: goods.ID,
  5849. CancelOutDetailId: 0,
  5850. ProductDate: warehouse.ProductDate,
  5851. ExpireDate: warehouse.ExpiryDate,
  5852. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5853. StorehouseId: warehouse.StorehouseId,
  5854. AdminUserId: creator,
  5855. BuyPrice: warehouse.Price, //进货价
  5856. StockCount: "",
  5857. BatchNumberCount: warehouse.StockCount - goods.Count,
  5858. IsCheck: 1,
  5859. OverCount: sum_count,
  5860. RegisterNumber: warehouse.RegisterNumber,
  5861. OrderId: order_id,
  5862. ProjectId: goods.ProjectId,
  5863. }
  5864. //创建出库流水
  5865. errflow := CreateStockFlowOne(stockFlow)
  5866. if errflow != nil {
  5867. goodErrcode := models.XtGoodErrcode{
  5868. UserOrgId: orgID,
  5869. Errcode: "创建流水报错",
  5870. GoodId: goods.GoodId,
  5871. Status: 1,
  5872. Ctime: time.Now().Unix(),
  5873. Mtime: 0,
  5874. Count: 0,
  5875. StockCount: 0,
  5876. Creater: creator,
  5877. BatchNumberId: warehouse.ID,
  5878. WarehouseOutId: 0,
  5879. }
  5880. CreateGoodErrcode(goodErrcode)
  5881. }
  5882. //添加入库ID
  5883. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count)
  5884. if errWarehouse != nil {
  5885. goodErrcode := models.XtGoodErrcode{
  5886. UserOrgId: orgID,
  5887. Errcode: "添加入库ID报错",
  5888. GoodId: goods.GoodId,
  5889. Status: 1,
  5890. Ctime: time.Now().Unix(),
  5891. Mtime: 0,
  5892. Count: 0,
  5893. StockCount: 0,
  5894. Creater: creator,
  5895. BatchNumberId: warehouse.ID,
  5896. WarehouseOutId: 0,
  5897. }
  5898. CreateGoodErrcode(goodErrcode)
  5899. }
  5900. //更新出库数量
  5901. errsumcode := ModifyGoodSumCount(goods.StorehouseId, goods.Count, orgID, goods.GoodId)
  5902. if errsumcode != nil {
  5903. goodErrcode := models.XtGoodErrcode{
  5904. UserOrgId: orgID,
  5905. Errcode: "更新出库数量报错",
  5906. GoodId: goods.GoodId,
  5907. Status: 1,
  5908. Ctime: time.Now().Unix(),
  5909. Mtime: 0,
  5910. Count: 0,
  5911. StockCount: 0,
  5912. Creater: creator,
  5913. BatchNumberId: warehouse.ID,
  5914. WarehouseOutId: 0,
  5915. }
  5916. CreateGoodErrcode(goodErrcode)
  5917. }
  5918. if warehouse.StockCount < maxNumber {
  5919. goodErrcode := models.XtGoodErrcode{
  5920. UserOrgId: orgID,
  5921. Errcode: "出库接口库存数量不足",
  5922. GoodId: goods.GoodId,
  5923. Status: 1,
  5924. Ctime: time.Now().Unix(),
  5925. Mtime: 0,
  5926. Count: 0,
  5927. StockCount: 0,
  5928. Creater: creator,
  5929. BatchNumberId: warehouse.ID,
  5930. WarehouseOutId: 0,
  5931. }
  5932. CreateGoodErrcode(goodErrcode)
  5933. return errors.New("库存数量不足")
  5934. }
  5935. return nil
  5936. } else {
  5937. // 出库完成后,要将该批次库存清零
  5938. warehouse.StockCount = 0
  5939. warehouse.Mtime = time.Now().Unix()
  5940. errThree := UpDateWarehouseInfoByStock(&warehouse)
  5941. if errThree != nil {
  5942. goodErrcode := models.XtGoodErrcode{
  5943. UserOrgId: orgID,
  5944. Errcode: "扣减库存报错",
  5945. GoodId: goods.GoodId,
  5946. Status: 1,
  5947. Ctime: time.Now().Unix(),
  5948. Mtime: 0,
  5949. Count: 0,
  5950. StockCount: 0,
  5951. Creater: creator,
  5952. BatchNumberId: warehouse.ID,
  5953. WarehouseOutId: 0,
  5954. }
  5955. CreateGoodErrcode(goodErrcode)
  5956. return errThree
  5957. }
  5958. //查询剩余库存
  5959. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  5960. var sum_count int64
  5961. for _, item := range goodList {
  5962. sum_count += item.StockCount
  5963. }
  5964. OutInfo := &models.WarehouseOutInfo{
  5965. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  5966. WarehouseOutId: warehouseOut.ID,
  5967. WarehouseInfotId: warehouse.ID,
  5968. Status: 1,
  5969. Ctime: time.Now().Unix(),
  5970. Remark: warehouse.Remark,
  5971. OrgId: orgID,
  5972. Type: 1,
  5973. Manufacturer: warehouse.Manufacturer,
  5974. Dealer: warehouse.Dealer,
  5975. IsSys: 5,
  5976. SysRecordTime: record_time,
  5977. GoodTypeId: goods.GoodTypeId,
  5978. GoodId: goods.GoodId,
  5979. PatientId: patient_id,
  5980. Number: warehouse.Number,
  5981. LicenseNumber: warehouse.LicenseNumber,
  5982. Price: warehouse.PackingPrice,
  5983. ExpiryDate: warehouse.ExpiryDate,
  5984. ProductDate: warehouse.ProductDate,
  5985. ProjectId: goods.ProjectId,
  5986. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5987. StorehouseId: goods.StorehouseId,
  5988. IsCheck: 1,
  5989. OverCount: sum_count,
  5990. RegisterNumber: warehouse.RegisterNumber,
  5991. OrderId: order_id,
  5992. }
  5993. OutInfo.Count = stock_number
  5994. AddSigleWarehouseOutInfo(OutInfo)
  5995. stockFlow := models.VmStockFlow{
  5996. WarehousingId: warehouse.ID,
  5997. GoodId: goods.GoodId,
  5998. Number: warehouse.Number,
  5999. LicenseNumber: warehouse.LicenseNumber,
  6000. Count: stock_number,
  6001. UserOrgId: orgID,
  6002. PatientId: patient_id,
  6003. SystemTime: record_time,
  6004. ConsumableType: 15,
  6005. IsSys: 5,
  6006. WarehousingOrder: warehouse.WarehousingOrder,
  6007. WarehouseOutId: warehouseOut.ID,
  6008. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  6009. IsEdit: 1,
  6010. CancelStockId: 0,
  6011. CancelOrderNumber: "",
  6012. Manufacturer: warehouse.Manufacturer,
  6013. Dealer: warehouse.Dealer,
  6014. Creator: creator,
  6015. UpdateCreator: 0,
  6016. Status: 1,
  6017. Ctime: time.Now().Unix(),
  6018. Mtime: 0,
  6019. Price: warehouse.PackingPrice, //零售价
  6020. WarehousingDetailId: warehouse.ID,
  6021. WarehouseOutDetailId: goods.ID,
  6022. CancelOutDetailId: 0,
  6023. ProductDate: warehouse.ProductDate,
  6024. ExpireDate: warehouse.ExpiryDate,
  6025. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6026. StorehouseId: warehouse.StorehouseId,
  6027. AdminUserId: creator,
  6028. BuyPrice: warehouse.Price, //进货价
  6029. StockCount: "",
  6030. BatchNumberCount: 0,
  6031. IsCheck: 1,
  6032. OverCount: sum_count,
  6033. RegisterNumber: warehouse.RegisterNumber,
  6034. OrderId: order_id,
  6035. ProjectId: goods.ProjectId,
  6036. }
  6037. //创建出库流水
  6038. errflow := CreateStockFlowOne(stockFlow)
  6039. if errflow != nil {
  6040. goodErrcode := models.XtGoodErrcode{
  6041. UserOrgId: orgID,
  6042. Errcode: "创建出库流水报错",
  6043. GoodId: goods.GoodId,
  6044. Status: 1,
  6045. Ctime: time.Now().Unix(),
  6046. Mtime: 0,
  6047. Count: 0,
  6048. StockCount: 0,
  6049. Creater: creator,
  6050. BatchNumberId: warehouse.ID,
  6051. WarehouseOutId: 0,
  6052. }
  6053. CreateGoodErrcode(goodErrcode)
  6054. }
  6055. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count)
  6056. if errWarehouse != nil {
  6057. goodErrcode := models.XtGoodErrcode{
  6058. UserOrgId: orgID,
  6059. Errcode: "添加入库ID报错",
  6060. GoodId: goods.GoodId,
  6061. Status: 1,
  6062. Ctime: time.Now().Unix(),
  6063. Mtime: 0,
  6064. Count: 0,
  6065. StockCount: 0,
  6066. Creater: creator,
  6067. BatchNumberId: warehouse.ID,
  6068. WarehouseOutId: 0,
  6069. }
  6070. CreateGoodErrcode(goodErrcode)
  6071. }
  6072. //更新出库数量
  6073. errcodecout := ModifyGoodSumCount(goods.StorehouseId, stock_number, orgID, goods.GoodId)
  6074. if errcodecout != nil {
  6075. goodErrcode := models.XtGoodErrcode{
  6076. UserOrgId: orgID,
  6077. Errcode: "更新出库数量报错",
  6078. GoodId: goods.GoodId,
  6079. Status: 1,
  6080. Ctime: time.Now().Unix(),
  6081. Mtime: 0,
  6082. Count: 0,
  6083. StockCount: 0,
  6084. Creater: creator,
  6085. BatchNumberId: warehouse.ID,
  6086. WarehouseOutId: 0,
  6087. }
  6088. CreateGoodErrcode(goodErrcode)
  6089. }
  6090. }
  6091. goods.Count = deliver_number - stock_number
  6092. prepare := &models.DialysisBeforePrepare{
  6093. Count: deliver_number - stock_number,
  6094. GoodTypeId: goods.GoodTypeId,
  6095. GoodId: goods.GoodId,
  6096. PatientId: goods.PatientId,
  6097. RecordDate: goods.RecordDate,
  6098. UserOrgId: orgID,
  6099. Ctime: time.Now().Unix(),
  6100. Creater: goods.Creater,
  6101. CommdityCode: goods.CommdityCode,
  6102. Status: 1,
  6103. StorehouseId: goods.StorehouseId,
  6104. }
  6105. ConsumableSettleDelivery(orgID, patient_id, record_time, prepare, warehouseOut, creator, order_id)
  6106. return nil
  6107. }
  6108. // 保存处方出库
  6109. func ConsumablePrescriptionDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, creator int64, count int64) (err error) {
  6110. var deliver_number int64 = 0
  6111. var stock_number int64 = 0
  6112. var maxNumber int64 = 0
  6113. deliver_number = goods.Count
  6114. // 根据先进先出原则,查询最先入库的批次,进行出库
  6115. // 如果没有对应的库存,则报错
  6116. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  6117. if err != nil {
  6118. goodErrcode := models.XtGoodErrcode{
  6119. UserOrgId: goods.UserOrgId,
  6120. Errcode: "查询库存信息报错",
  6121. GoodId: goods.GoodId,
  6122. Status: 1,
  6123. Ctime: time.Now().Unix(),
  6124. Mtime: 0,
  6125. Count: 0,
  6126. StockCount: 0,
  6127. Creater: creator,
  6128. BatchNumberId: warehouse.ID,
  6129. WarehouseOutId: 0,
  6130. }
  6131. CreateGoodErrcode(goodErrcode)
  6132. return err
  6133. }
  6134. stock_number = warehouse.StockCount
  6135. fmt.Println("sotck_number233223322o", stock_number)
  6136. fmt.Println("deliver_number", deliver_number)
  6137. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6138. if stock_number >= deliver_number {
  6139. maxNumber = goods.Count
  6140. warehouse.StockCount = warehouse.StockCount - maxNumber
  6141. warehouse.Mtime = time.Now().Unix()
  6142. //扣减库存
  6143. errThree := UpDateWarehouseInfoByStock(&warehouse)
  6144. if errThree != nil {
  6145. goodErrcode := models.XtGoodErrcode{
  6146. UserOrgId: goods.UserOrgId,
  6147. Errcode: "扣减库存失败",
  6148. GoodId: goods.GoodId,
  6149. Status: 1,
  6150. Ctime: time.Now().Unix(),
  6151. Mtime: 0,
  6152. Count: 0,
  6153. StockCount: 0,
  6154. Creater: creator,
  6155. BatchNumberId: warehouse.ID,
  6156. WarehouseOutId: 0,
  6157. }
  6158. CreateGoodErrcode(goodErrcode)
  6159. return errThree
  6160. }
  6161. //查询剩余库存
  6162. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  6163. var sum_count int64
  6164. for _, item := range goodList {
  6165. sum_count += item.StockCount
  6166. }
  6167. //查询已经出库的数据
  6168. flowGood, _ := GetStockFlowIsBatchNumberSix(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6169. var out_count int64
  6170. var out_count_one int64
  6171. for _, item := range flowGood {
  6172. out_count += item.Count
  6173. }
  6174. //查询退库数据
  6175. flowGoodTwo, _ := GetStockFlowIsBatchNumberFour(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6176. for _, item := range flowGoodTwo {
  6177. out_count_one += item.Count
  6178. }
  6179. //flowGood, _ := GetStockFlowIsBatchNumberSeventy(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6180. //
  6181. //var out_count int64
  6182. //var out_count_one int64
  6183. var out_count_two int64
  6184. //
  6185. //for _, item := range flowGood {
  6186. // out_count += item.Count
  6187. //}
  6188. //查询退库数据
  6189. //flowGoodTwo, _ := GetStockFlowIsBatchNumberFourty(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6190. //for _, item := range flowGoodTwo {
  6191. // out_count_one += item.Count
  6192. //}
  6193. wareOutList, _ := GetStockFlowIsBatchNumberSixty(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6194. for _, item := range wareOutList {
  6195. out_count_two += item.Count
  6196. }
  6197. OutInfo := &models.WarehouseOutInfo{
  6198. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  6199. WarehouseOutId: warehouseOut.ID,
  6200. WarehouseInfotId: warehouse.ID,
  6201. Status: 1,
  6202. Ctime: time.Now().Unix(),
  6203. Remark: warehouse.Remark,
  6204. OrgId: orgID,
  6205. Type: 1,
  6206. Manufacturer: warehouse.Manufacturer,
  6207. Dealer: warehouse.Dealer,
  6208. IsSys: 1,
  6209. SysRecordTime: record_time,
  6210. GoodTypeId: goods.GoodTypeId,
  6211. GoodId: goods.GoodId,
  6212. PatientId: patient_id,
  6213. Number: warehouse.Number,
  6214. LicenseNumber: warehouse.LicenseNumber,
  6215. Price: warehouse.PackingPrice,
  6216. ExpiryDate: warehouse.ExpiryDate,
  6217. ProductDate: warehouse.ProductDate,
  6218. ProjectId: goods.ProjectId,
  6219. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6220. StorehouseId: goods.StorehouseId,
  6221. IsCheck: 1,
  6222. OverCount: sum_count,
  6223. RegisterNumber: warehouse.RegisterNumber,
  6224. }
  6225. OutInfo.Count = count - out_count_two + out_count_one
  6226. _, errcodes := GetNewWarehouseOutInfoIsExistSix(goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID)
  6227. if errcodes == gorm.ErrRecordNotFound {
  6228. errOne := AddSigleWarehouseOutInfo(OutInfo)
  6229. if errOne != nil {
  6230. return errOne
  6231. }
  6232. } else if errcodes == nil {
  6233. outInfoOne, _ := GetWarehouseOutInfoIsExistThree(goods.GoodId, patient_id, record_time, goods.ProjectId)
  6234. if count != outInfoOne.Count {
  6235. UpdatedWarehouseOutInfo(OutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId)
  6236. }
  6237. }
  6238. lastOut, _ := FindWarehouseOutInfoByPatientIdTwo(patient_id, record_time, goods.GoodId, orgID, goods.ProjectId)
  6239. //如果本次出库数据大于历史出库数据 新增1条流水
  6240. fmt.Println("本次出库数据", count)
  6241. fmt.Println("历史出库数据", out_count)
  6242. fmt.Println("退库数据", out_count_one)
  6243. if count > (out_count - out_count_one) {
  6244. stockFlow := models.VmStockFlow{
  6245. WarehousingId: warehouse.ID,
  6246. GoodId: goods.GoodId,
  6247. Number: warehouse.Number,
  6248. LicenseNumber: warehouse.LicenseNumber,
  6249. Count: count - out_count + out_count_one,
  6250. UserOrgId: orgID,
  6251. PatientId: goods.PatientId,
  6252. SystemTime: record_time,
  6253. ConsumableType: 3,
  6254. IsSys: 1,
  6255. WarehousingOrder: warehouse.WarehousingOrder,
  6256. WarehouseOutId: warehouseOut.ID,
  6257. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  6258. IsEdit: 1,
  6259. CancelStockId: 0,
  6260. CancelOrderNumber: "",
  6261. Manufacturer: warehouse.Manufacturer,
  6262. Dealer: warehouse.Dealer,
  6263. Creator: creator,
  6264. UpdateCreator: 0,
  6265. Status: 1,
  6266. Ctime: time.Now().Unix(),
  6267. Mtime: 0,
  6268. Price: warehouse.PackingPrice, //零售价
  6269. WarehousingDetailId: warehouse.ID,
  6270. WarehouseOutDetailId: goods.ID,
  6271. CancelOutDetailId: 0,
  6272. ProductDate: warehouse.ProductDate,
  6273. ExpireDate: warehouse.ExpiryDate,
  6274. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6275. StorehouseId: warehouse.StorehouseId,
  6276. AdminUserId: creator,
  6277. BuyPrice: warehouse.Price, //进货价
  6278. StockCount: "",
  6279. BatchNumberCount: warehouse.StockCount - goods.Count,
  6280. IsCheck: 1,
  6281. OverCount: sum_count,
  6282. RegisterNumber: warehouse.RegisterNumber,
  6283. ProjectId: goods.ProjectId,
  6284. }
  6285. //创建出库流水
  6286. errflow := CreateStockFlowOne(stockFlow)
  6287. if errflow != nil {
  6288. goodErrcode := models.XtGoodErrcode{
  6289. UserOrgId: orgID,
  6290. Errcode: "创建流水报错",
  6291. GoodId: goods.GoodId,
  6292. Status: 1,
  6293. Ctime: time.Now().Unix(),
  6294. Mtime: 0,
  6295. Count: 0,
  6296. StockCount: 0,
  6297. Creater: creator,
  6298. BatchNumberId: warehouse.ID,
  6299. WarehouseOutId: 0,
  6300. }
  6301. CreateGoodErrcode(goodErrcode)
  6302. }
  6303. fmt.Println("stockFlow.Count233232323232322323232332wo0-----------------------", stockFlow.Count)
  6304. //更新出库数量
  6305. errsumcode := ModifyGoodSumCount(goods.StorehouseId, stockFlow.Count, orgID, goods.GoodId)
  6306. if errsumcode != nil {
  6307. goodErrcode := models.XtGoodErrcode{
  6308. UserOrgId: orgID,
  6309. Errcode: "更新出库数量报错",
  6310. GoodId: goods.GoodId,
  6311. Status: 1,
  6312. Ctime: time.Now().Unix(),
  6313. Mtime: 0,
  6314. Count: 0,
  6315. StockCount: 0,
  6316. Creater: creator,
  6317. BatchNumberId: warehouse.ID,
  6318. WarehouseOutId: 0,
  6319. }
  6320. CreateGoodErrcode(goodErrcode)
  6321. }
  6322. }
  6323. //如果本次出库数据小于历史出库数据 新增1条退库流水
  6324. if count < (out_count - out_count_one) {
  6325. operation_time := time.Now().Unix()
  6326. //创建退库单
  6327. timeStr := time.Now().Format("2006-01-02")
  6328. timeArr := strings.Split(timeStr, "-")
  6329. total, _ := FindAllCancelStockTotal(orgID)
  6330. total = total + 1
  6331. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  6332. cancelStock := models.CancelStock{
  6333. OrderNumber: orderNumber,
  6334. OperaTime: operation_time,
  6335. OrgId: orgID,
  6336. Creater: warehouseOut.Creater,
  6337. Ctime: time.Now().Unix(),
  6338. Status: 1,
  6339. ReturnTime: record_time,
  6340. Type: 1,
  6341. StorehouseId: goods.StorehouseId,
  6342. IsCheck: 1,
  6343. }
  6344. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, orgID)
  6345. if msgerrkonde == gorm.ErrRecordNotFound {
  6346. AddSigleCancelStock(&cancelStock)
  6347. }
  6348. cancel, _ := GetLastCancelStockById(orgID)
  6349. manufacturer, _ := GetManufactureById(warehouse.Manufacturer)
  6350. deaerler, _ := GetDealerById(warehouse.Dealer)
  6351. cancelStockInfo := models.CancelStockInfo{
  6352. GoodId: goods.GoodId,
  6353. CancelStockId: cancel.ID,
  6354. GoodTypeId: goods.GoodTypeId,
  6355. Count: out_count - out_count_one - count,
  6356. Price: warehouse.PackingPrice,
  6357. Total: 0,
  6358. ProductDate: warehouse.ProductDate,
  6359. ExpiryDate: warehouse.ExpiryDate,
  6360. Ctime: time.Now().Unix(),
  6361. Status: 1,
  6362. OrgId: orgID,
  6363. OrderNumber: cancel.OrderNumber,
  6364. Type: 0,
  6365. Dealer: deaerler.DealerName,
  6366. Manufacturer: manufacturer.ManufacturerName,
  6367. Number: warehouse.Number,
  6368. RegisterAccount: "",
  6369. Remark: "",
  6370. WarehouseInfoId: warehouse.ID,
  6371. PatientId: patient_id,
  6372. RecordDate: record_time,
  6373. StorehouseId: goods.StorehouseId,
  6374. IsCheck: 1,
  6375. }
  6376. if orgID == 9671 || orgID == 10265 {
  6377. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  6378. cancelStockInfo.Price = goodsInfo.PackingPrice
  6379. }
  6380. CreateCancelStockInfoOne(&cancelStockInfo)
  6381. cancelInfo, _ := GetLastCancelStockInfoByGoodId(goods.GoodId)
  6382. flow := models.VmStockFlow{
  6383. WarehousingId: warehouse.ID,
  6384. GoodId: goods.GoodId,
  6385. Number: warehouse.Number,
  6386. LicenseNumber: warehouse.LicenseNumber,
  6387. Count: out_count - out_count_one - count,
  6388. UserOrgId: orgID,
  6389. PatientId: patient_id,
  6390. SystemTime: record_time,
  6391. ConsumableType: 7,
  6392. IsSys: 1,
  6393. WarehousingOrder: "",
  6394. WarehouseOutId: lastOut.WarehouseOutId,
  6395. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  6396. IsEdit: 0,
  6397. CancelStockId: cancel.ID,
  6398. CancelOrderNumber: cancel.OrderNumber,
  6399. Manufacturer: manufacturer.ID,
  6400. Dealer: 0,
  6401. Creator: warehouseOut.Creater,
  6402. UpdateCreator: 0,
  6403. Status: 1,
  6404. Ctime: time.Now().Unix(),
  6405. Mtime: 0,
  6406. Price: warehouse.PackingPrice,
  6407. WarehousingDetailId: warehouse.ID,
  6408. WarehouseOutDetailId: lastOut.ID,
  6409. CancelOutDetailId: cancelInfo.ID,
  6410. ProductDate: warehouse.ProductDate,
  6411. ExpireDate: warehouse.ExpiryDate,
  6412. StorehouseId: goods.StorehouseId,
  6413. BuyPrice: warehouse.Price,
  6414. ProjectId: goods.ProjectId,
  6415. OverCount: sum_count,
  6416. RegisterNumber: warehouse.RegisterNumber,
  6417. }
  6418. if orgID == 9671 || orgID == 10265 {
  6419. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  6420. flow.Price = goodsInfo.PackingPrice
  6421. flow.BuyPrice = goodsInfo.BuyPrice
  6422. }
  6423. CreateStockFlowOne(flow)
  6424. //退库数量增加
  6425. UpdateSumAddCancelCount(orgID, goods.GoodId, goods.StorehouseId, flow.Count)
  6426. ModifyAddGoodSumCount(goods.StorehouseId, flow.Count, orgID, goods.GoodId)
  6427. }
  6428. //添加入库ID
  6429. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count)
  6430. if errWarehouse != nil {
  6431. goodErrcode := models.XtGoodErrcode{
  6432. UserOrgId: orgID,
  6433. Errcode: "添加入库ID报错",
  6434. GoodId: goods.GoodId,
  6435. Status: 1,
  6436. Ctime: time.Now().Unix(),
  6437. Mtime: 0,
  6438. Count: 0,
  6439. StockCount: 0,
  6440. Creater: creator,
  6441. BatchNumberId: warehouse.ID,
  6442. WarehouseOutId: 0,
  6443. }
  6444. CreateGoodErrcode(goodErrcode)
  6445. }
  6446. if warehouse.StockCount < maxNumber {
  6447. goodErrcode := models.XtGoodErrcode{
  6448. UserOrgId: orgID,
  6449. Errcode: "出库接口库存数量不足",
  6450. GoodId: goods.GoodId,
  6451. Status: 1,
  6452. Ctime: time.Now().Unix(),
  6453. Mtime: 0,
  6454. Count: 0,
  6455. StockCount: 0,
  6456. Creater: creator,
  6457. BatchNumberId: warehouse.ID,
  6458. WarehouseOutId: 0,
  6459. }
  6460. CreateGoodErrcode(goodErrcode)
  6461. return errors.New("库存数量不足")
  6462. }
  6463. return nil
  6464. } else {
  6465. // 出库完成后,要将该批次库存清零
  6466. warehouse.StockCount = 0
  6467. warehouse.Mtime = time.Now().Unix()
  6468. errThree := UpDateWarehouseInfoByStock(&warehouse)
  6469. if errThree != nil {
  6470. goodErrcode := models.XtGoodErrcode{
  6471. UserOrgId: orgID,
  6472. Errcode: "扣减库存报错",
  6473. GoodId: goods.GoodId,
  6474. Status: 1,
  6475. Ctime: time.Now().Unix(),
  6476. Mtime: 0,
  6477. Count: 0,
  6478. StockCount: 0,
  6479. Creater: creator,
  6480. BatchNumberId: warehouse.ID,
  6481. WarehouseOutId: 0,
  6482. }
  6483. CreateGoodErrcode(goodErrcode)
  6484. return errThree
  6485. }
  6486. //查询剩余库存
  6487. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  6488. var sum_count int64
  6489. for _, item := range goodList {
  6490. sum_count += item.StockCount
  6491. }
  6492. fmt.Println("剩余库存", sum_count)
  6493. OutInfo := &models.WarehouseOutInfo{
  6494. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  6495. WarehouseOutId: warehouseOut.ID,
  6496. WarehouseInfotId: warehouse.ID,
  6497. Status: 1,
  6498. Ctime: time.Now().Unix(),
  6499. Remark: warehouse.Remark,
  6500. OrgId: orgID,
  6501. Type: 1,
  6502. Manufacturer: warehouse.Manufacturer,
  6503. Dealer: warehouse.Dealer,
  6504. IsSys: 1,
  6505. SysRecordTime: record_time,
  6506. GoodTypeId: goods.GoodTypeId,
  6507. GoodId: goods.GoodId,
  6508. PatientId: patient_id,
  6509. Number: warehouse.Number,
  6510. LicenseNumber: warehouse.LicenseNumber,
  6511. Price: warehouse.PackingPrice,
  6512. ExpiryDate: warehouse.ExpiryDate,
  6513. ProductDate: warehouse.ProductDate,
  6514. ProjectId: goods.ProjectId,
  6515. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6516. StorehouseId: goods.StorehouseId,
  6517. IsCheck: 1,
  6518. OverCount: sum_count,
  6519. RegisterNumber: warehouse.RegisterNumber,
  6520. }
  6521. OutInfo.Count = stock_number
  6522. _, errcodes := GetNewWarehouseOutInfoIsExistSix(goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID)
  6523. if errcodes == gorm.ErrRecordNotFound {
  6524. errOne := AddSigleWarehouseOutInfo(OutInfo)
  6525. if errOne != nil {
  6526. return errOne
  6527. }
  6528. } else if errcodes == nil {
  6529. outInfoOne, _ := GetWarehouseOutInfoIsExistTen(goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID)
  6530. if count != outInfoOne.Count {
  6531. UpdatedWarehouseOutInfoSix(OutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID)
  6532. }
  6533. }
  6534. lastOut, _ := FindWarehouseOutInfoByPatientIdTwo(patient_id, record_time, goods.GoodId, orgID, goods.ProjectId)
  6535. //查询该该批次已经出库的数据
  6536. //flowGood, _ := GetStockFlowIsBatchNumber(warehouse.ID, patient_id, record_time, goods.GoodId)
  6537. //查询已经出库的数据
  6538. //flowGood, _ := GetStockFlowIsBatchNumberSeventy(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6539. //var out_count int64
  6540. //var out_count_one int64
  6541. //for _, item := range flowGood {
  6542. // out_count += item.Count
  6543. //}
  6544. //flowGoodTwo, _ := GetStockFlowIsBatchNumberSix(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6545. //for _, item := range flowGoodTwo {
  6546. // out_count_one += item.Count
  6547. //}
  6548. //查询该该批次已经出库的数据
  6549. flowGood, _ := GetStockFlowIsBatchNumberSix(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6550. var out_count int64
  6551. var out_count_one int64
  6552. for _, item := range flowGood {
  6553. out_count += item.Count
  6554. }
  6555. flowGoodTwo, _ := GetStockFlowIsBatchNumberFour(patient_id, record_time, goods.GoodId, goods.ProjectId)
  6556. for _, item := range flowGoodTwo {
  6557. out_count_one += item.Count
  6558. }
  6559. fmt.Println("count----------------", count)
  6560. fmt.Println("out_count----------", out_count)
  6561. fmt.Println("out_count_one--------", out_count_one)
  6562. //如果出库数量 大于 历史出库数据 新增1条流水
  6563. if count > out_count-out_count_one {
  6564. stockFlow := models.VmStockFlow{
  6565. WarehousingId: warehouse.ID,
  6566. GoodId: goods.GoodId,
  6567. Number: warehouse.Number,
  6568. LicenseNumber: warehouse.LicenseNumber,
  6569. Count: stock_number,
  6570. UserOrgId: orgID,
  6571. PatientId: goods.PatientId,
  6572. SystemTime: record_time,
  6573. ConsumableType: 3,
  6574. IsSys: 1,
  6575. WarehousingOrder: warehouse.WarehousingOrder,
  6576. WarehouseOutId: warehouseOut.ID,
  6577. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  6578. IsEdit: 1,
  6579. CancelStockId: 0,
  6580. CancelOrderNumber: "",
  6581. Manufacturer: warehouse.Manufacturer,
  6582. Dealer: warehouse.Dealer,
  6583. Creator: creator,
  6584. UpdateCreator: 0,
  6585. Status: 1,
  6586. Ctime: time.Now().Unix(),
  6587. Mtime: 0,
  6588. Price: warehouse.PackingPrice, //零售价
  6589. WarehousingDetailId: warehouse.ID,
  6590. WarehouseOutDetailId: goods.ID,
  6591. CancelOutDetailId: 0,
  6592. ProductDate: warehouse.ProductDate,
  6593. ExpireDate: warehouse.ExpiryDate,
  6594. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6595. StorehouseId: warehouse.StorehouseId,
  6596. AdminUserId: creator,
  6597. BuyPrice: warehouse.Price, //进货价
  6598. StockCount: "",
  6599. BatchNumberCount: 0,
  6600. IsCheck: 1,
  6601. OverCount: sum_count,
  6602. RegisterNumber: warehouse.RegisterNumber,
  6603. ProjectId: goods.ProjectId,
  6604. }
  6605. //创建出库流水
  6606. errflow := CreateStockFlowOne(stockFlow)
  6607. if errflow != nil {
  6608. goodErrcode := models.XtGoodErrcode{
  6609. UserOrgId: orgID,
  6610. Errcode: "创建出库流水报错",
  6611. GoodId: goods.GoodId,
  6612. Status: 1,
  6613. Ctime: time.Now().Unix(),
  6614. Mtime: 0,
  6615. Count: 0,
  6616. StockCount: 0,
  6617. Creater: creator,
  6618. BatchNumberId: warehouse.ID,
  6619. WarehouseOutId: 0,
  6620. }
  6621. CreateGoodErrcode(goodErrcode)
  6622. }
  6623. //更新出库数量
  6624. errcodecout := ModifyGoodSumCount(goods.StorehouseId, stock_number, orgID, goods.GoodId)
  6625. if errcodecout != nil {
  6626. goodErrcode := models.XtGoodErrcode{
  6627. UserOrgId: orgID,
  6628. Errcode: "更新出库数量报错",
  6629. GoodId: goods.GoodId,
  6630. Status: 1,
  6631. Ctime: time.Now().Unix(),
  6632. Mtime: 0,
  6633. Count: 0,
  6634. StockCount: 0,
  6635. Creater: creator,
  6636. BatchNumberId: warehouse.ID,
  6637. WarehouseOutId: 0,
  6638. }
  6639. CreateGoodErrcode(goodErrcode)
  6640. }
  6641. }
  6642. //退库
  6643. if count < out_count-out_count_one {
  6644. operation_time := time.Now().Unix()
  6645. //创建退库单
  6646. timeStr := time.Now().Format("2006-01-02")
  6647. timeArr := strings.Split(timeStr, "-")
  6648. total, _ := FindAllCancelStockTotal(orgID)
  6649. total = total + 1
  6650. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  6651. cancelStock := models.CancelStock{
  6652. OrderNumber: orderNumber,
  6653. OperaTime: operation_time,
  6654. OrgId: orgID,
  6655. Creater: warehouseOut.Creater,
  6656. Ctime: time.Now().Unix(),
  6657. Status: 1,
  6658. ReturnTime: record_time,
  6659. Type: 1,
  6660. StorehouseId: goods.StorehouseId,
  6661. IsCheck: 1,
  6662. }
  6663. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, orgID)
  6664. if msgerrkonde == gorm.ErrRecordNotFound {
  6665. AddSigleCancelStock(&cancelStock)
  6666. }
  6667. cancel, _ := GetLastCancelStockById(orgID)
  6668. manufacturer, _ := GetManufactureById(warehouse.Manufacturer)
  6669. deaerler, _ := GetDealerById(warehouse.Dealer)
  6670. cancelStockInfo := models.CancelStockInfo{
  6671. GoodId: goods.GoodId,
  6672. CancelStockId: cancel.ID,
  6673. GoodTypeId: goods.GoodTypeId,
  6674. Count: out_count - out_count_one - count,
  6675. Price: warehouse.PackingPrice,
  6676. Total: 0,
  6677. ProductDate: warehouse.ProductDate,
  6678. ExpiryDate: warehouse.ExpiryDate,
  6679. Ctime: time.Now().Unix(),
  6680. Status: 1,
  6681. OrgId: orgID,
  6682. OrderNumber: cancel.OrderNumber,
  6683. Type: 0,
  6684. Dealer: deaerler.DealerName,
  6685. Manufacturer: manufacturer.ManufacturerName,
  6686. Number: warehouse.Number,
  6687. RegisterAccount: "",
  6688. Remark: "",
  6689. WarehouseInfoId: warehouse.ID,
  6690. PatientId: patient_id,
  6691. RecordDate: record_time,
  6692. StorehouseId: goods.StorehouseId,
  6693. IsCheck: 1,
  6694. }
  6695. if orgID == 9671 || orgID == 10265 {
  6696. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  6697. cancelStockInfo.Price = goodsInfo.PackingPrice
  6698. }
  6699. CreateCancelStockInfoOne(&cancelStockInfo)
  6700. cancelInfo, _ := GetLastCancelStockInfoByGoodId(goods.GoodId)
  6701. flow := models.VmStockFlow{
  6702. WarehousingId: warehouse.ID,
  6703. GoodId: goods.GoodId,
  6704. Number: warehouse.Number,
  6705. LicenseNumber: warehouse.LicenseNumber,
  6706. Count: out_count - count,
  6707. UserOrgId: orgID,
  6708. PatientId: patient_id,
  6709. SystemTime: record_time,
  6710. ConsumableType: 7,
  6711. IsSys: 1,
  6712. WarehousingOrder: "",
  6713. WarehouseOutId: lastOut.WarehouseOutId,
  6714. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  6715. IsEdit: 0,
  6716. CancelStockId: cancel.ID,
  6717. CancelOrderNumber: cancel.OrderNumber,
  6718. Manufacturer: manufacturer.ID,
  6719. Dealer: 0,
  6720. Creator: warehouseOut.Creater,
  6721. UpdateCreator: 0,
  6722. Status: 1,
  6723. Ctime: time.Now().Unix(),
  6724. Mtime: 0,
  6725. Price: warehouse.Price,
  6726. WarehousingDetailId: warehouse.ID,
  6727. WarehouseOutDetailId: lastOut.ID,
  6728. CancelOutDetailId: cancelInfo.ID,
  6729. ProductDate: warehouse.ProductDate,
  6730. ExpireDate: warehouse.ExpiryDate,
  6731. StorehouseId: goods.StorehouseId,
  6732. BuyPrice: warehouse.Price,
  6733. ProjectId: goods.ProjectId,
  6734. OverCount: sum_count,
  6735. RegisterNumber: warehouse.RegisterNumber,
  6736. }
  6737. if orgID == 9671 || orgID == 10265 {
  6738. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  6739. flow.Price = goodsInfo.PackingPrice
  6740. flow.BuyPrice = goodsInfo.BuyPrice
  6741. }
  6742. CreateStockFlowOne(flow)
  6743. //退库数量增加
  6744. UpdateSumAddCancelCount(orgID, goods.GoodId, goods.StorehouseId, flow.Count)
  6745. //实际出库减少
  6746. ModifyAddGoodSumCount(goods.StorehouseId, flow.Count, orgID, goods.GoodId)
  6747. }
  6748. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count)
  6749. if errWarehouse != nil {
  6750. goodErrcode := models.XtGoodErrcode{
  6751. UserOrgId: orgID,
  6752. Errcode: "添加入库ID报错",
  6753. GoodId: goods.GoodId,
  6754. Status: 1,
  6755. Ctime: time.Now().Unix(),
  6756. Mtime: 0,
  6757. Count: 0,
  6758. StockCount: 0,
  6759. Creater: creator,
  6760. BatchNumberId: warehouse.ID,
  6761. WarehouseOutId: 0,
  6762. }
  6763. CreateGoodErrcode(goodErrcode)
  6764. }
  6765. }
  6766. goods.Count = deliver_number - stock_number
  6767. prepare := &models.DialysisBeforePrepare{
  6768. Count: deliver_number - stock_number,
  6769. GoodTypeId: goods.GoodTypeId,
  6770. GoodId: goods.GoodId,
  6771. PatientId: goods.PatientId,
  6772. RecordDate: goods.RecordDate,
  6773. UserOrgId: orgID,
  6774. Ctime: time.Now().Unix(),
  6775. Creater: creator,
  6776. Status: 1,
  6777. StorehouseId: goods.StorehouseId,
  6778. ProjectId: goods.ProjectId,
  6779. }
  6780. fmt.Println("剩余出库数量", prepare.Count)
  6781. ConsumablePrescriptionDelivery(orgID, patient_id, record_time, prepare, warehouseOut, creator, count)
  6782. return nil
  6783. }
  6784. func CreateAutoDetail(detail models.AutomaticReduceDetail) error {
  6785. err := XTWriteDB().Create(&detail).Error
  6786. return err
  6787. }
  6788. func UpdateAutomaticReduce(patient_id int64, record_date int64, good_id int64, project_id int64) error {
  6789. 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
  6790. return err
  6791. }
  6792. func GetDrugFLowByAdviceById(drug_id int64, patient_id int64, user_org_id int64, advice_date int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  6793. 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
  6794. return outInfo, err
  6795. }
  6796. func UpdateHisAdviceById(id int64) error {
  6797. err := XTWriteDB().Model(&models.HisDoctorAdviceInfo{}).Where("id = ?", id).Updates(map[string]interface{}{"execution_time": "", "execution_staff": "", "execution_state": 2}).Error
  6798. return err
  6799. }
  6800. func GetAdviceIsOut(advice_id int64, user_org_id int64, patient_id int64, advice_date int64, drug_id int64) (models.DrugWarehouseOutInfo, error) {
  6801. advice := models.DrugWarehouseOutInfo{}
  6802. 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
  6803. return advice, err
  6804. }