warhouse_service.go 503KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469
  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. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  77. var deliver_number int64 = 0
  78. var stock_number int64 = 0
  79. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  80. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  81. var drug_price float64
  82. if advice.PrescribingNumberUnit == drup.MaxUnit {
  83. deliver_number = count * drup.MinNumber
  84. } else {
  85. deliver_number = count
  86. }
  87. if advice.PrescribingNumberUnit == drup.MaxUnit {
  88. drug_price = drup.RetailPrice
  89. }
  90. fmt.Println(drug_price)
  91. if advice.PrescribingNumberUnit == drup.MinUnit {
  92. drug_price = drup.MinPrice
  93. }
  94. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  95. drug_price = drup.RetailPrice
  96. }
  97. // 根据先进先出原则,查询最先入库的批次,进行出库
  98. // 如果没有对应的库存,则报错
  99. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  100. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  101. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  102. var stockMax int64
  103. var stockMin int64
  104. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  105. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  106. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  107. UpdateMinNumber(lastWarehouse.ID, stockMin)
  108. }
  109. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  110. if err != nil {
  111. return err
  112. }
  113. // 将该批次的剩余库存数量转换为拆零数量
  114. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  115. //查找药品信息
  116. //baseInfo, _ := FindBaseDrugLibRecord(advice.UserOrgId, advice.DrugId)
  117. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  118. if stock_number >= deliver_number {
  119. var maxNumber int64 = 0
  120. var minNumber int64 = 0
  121. //var stock_max_number int64 = 0
  122. //stock_max_number = warehouse.StockMaxNumber
  123. maxNumber = deliver_number / drup.MinNumber
  124. minNumber = deliver_number % drup.MinNumber
  125. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  126. minNumber = maxNumber
  127. }
  128. if drup.MaxUnit != drup.MinUnit {
  129. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  130. return errors.New("库存数量不足")
  131. }
  132. }
  133. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  134. if warehouse.StockMaxNumber < 0 {
  135. warehouse.StockMaxNumber = 0
  136. }
  137. if warehouse.StockMinNumber < 0 {
  138. warehouse.StockMinNumber = 0
  139. }
  140. warehouse.Mtime = time.Now().Unix()
  141. if warehouse.StockMinNumber < minNumber {
  142. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  143. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  144. } else {
  145. if minNumber > 0 {
  146. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  147. }
  148. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  149. if warehouse.StockMinNumber > 0 {
  150. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  151. }
  152. }
  153. }
  154. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  155. if (warehouse.StockMinNumber - deliver_number) > 0 {
  156. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  157. }
  158. }
  159. if drup.MaxUnit != drup.MinUnit {
  160. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  161. return errors.New("库存数量不足")
  162. }
  163. }
  164. if warehouse.StockMinNumber <= 0 {
  165. warehouse.StockMinNumber = 0
  166. }
  167. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  168. if errThree != nil {
  169. return errThree
  170. }
  171. //查询剩余库存
  172. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  173. var sum_count int64
  174. for _, its := range stockInfo {
  175. if its.MaxUnit == drup.MaxUnit {
  176. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  177. }
  178. sum_count += its.StockMaxNumber + its.StockMinNumber
  179. }
  180. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  181. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  182. WarehouseOutId: warehouseout.ID,
  183. Status: 1,
  184. Ctime: time.Now().Unix(),
  185. Remark: warehouse.Remark,
  186. OrgId: orgID,
  187. Type: 1,
  188. Manufacturer: warehouse.Manufacturer,
  189. Dealer: warehouse.Dealer,
  190. IsSys: 1,
  191. SysRecordTime: advice.RecordDate,
  192. DrugId: advice.DrugId,
  193. Number: warehouse.Number,
  194. BatchNumber: warehouse.BatchNumber,
  195. Price: warehouse.RetailPrice,
  196. CountUnit: drup.MinUnit,
  197. RetailPrice: warehouse.RetailPrice,
  198. ProductDate: warehouse.ProductDate,
  199. ExpiryDate: warehouse.ExpiryDate,
  200. PatientId: advice.PatientId,
  201. Count: deliver_number, //出最小单位
  202. WarehouseInfoId: warehouse.ID,
  203. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  204. StorehouseId: storeConfig.DrugStorehouseOut,
  205. IsCheck: 1,
  206. OverCount: sum_count,
  207. }
  208. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  209. fmt.Println(errOne)
  210. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  211. drugflow := models.DrugFlow{
  212. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  213. WarehouseOutId: warehouseout.ID,
  214. DrugId: advice.DrugId,
  215. Number: warehouse.Number,
  216. ProductDate: warehouse.ProductDate,
  217. ExpireDate: warehouse.ExpiryDate,
  218. Count: deliver_number, //按最小单位计算,
  219. Price: warehouse.RetailPrice,
  220. Status: 1,
  221. Ctime: time.Now().Unix(),
  222. UserOrgId: orgID,
  223. Manufacturer: warehouse.Manufacturer,
  224. Dealer: warehouse.Dealer,
  225. BatchNumber: warehouse.BatchNumber,
  226. MaxUnit: drup.MinUnit,
  227. ConsumableType: 3,
  228. IsEdit: 1,
  229. Creator: advice.ExecutionStaff,
  230. IsSys: 1,
  231. PatientId: advice.PatientId,
  232. SystemTime: advice.AdviceDate,
  233. WarehousingDetailId: warehouse.ID,
  234. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  235. StorehouseId: storeConfig.DrugStorehouseOut,
  236. WarehouseOutDetailId: lastDrugOutInfo.ID,
  237. LastPrice: warehouse.Price,
  238. OverCount: sum_count,
  239. }
  240. CreateDrugFlowOne(drugflow)
  241. //查询是否存在数据
  242. details := &models.DrugAutomaticReduceDetail{
  243. WarehouseOutId: warehouseout.ID,
  244. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  245. PatientId: advice.PatientId,
  246. Ctime: time.Now().Unix(),
  247. Mtime: time.Now().Unix(),
  248. Status: 1,
  249. RecordTime: advice.RecordDate,
  250. OrgId: orgID,
  251. DrugId: advice.DrugId,
  252. Count: deliver_number,
  253. CountUnit: drup.MinUnit,
  254. WarehouseInfoId: warehouse.ID,
  255. StorehouseId: storeConfig.DrugStorehouseOut,
  256. }
  257. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  258. if errTwo != nil {
  259. return errTwo
  260. }
  261. var out_count int64
  262. //查询出库数据
  263. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  264. for _, item := range infoCountList {
  265. out_count += item.Count
  266. }
  267. //出库数量相加
  268. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  269. return nil
  270. } else {
  271. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  272. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  273. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  274. WarehouseOutId: warehouseout.ID,
  275. Status: 1,
  276. Ctime: time.Now().Unix(),
  277. Remark: warehouse.Remark,
  278. OrgId: orgID,
  279. Type: 1,
  280. Manufacturer: warehouse.Manufacturer,
  281. Dealer: warehouse.Dealer,
  282. IsSys: 1,
  283. SysRecordTime: advice.RecordDate,
  284. DrugId: advice.DrugId,
  285. Number: warehouse.Number,
  286. BatchNumber: warehouse.BatchNumber,
  287. Price: warehouse.RetailPrice,
  288. CountUnit: drup.MinUnit,
  289. ProductDate: warehouse.ProductDate,
  290. ExpiryDate: warehouse.ExpiryDate,
  291. PatientId: advice.PatientId,
  292. Count: stock_number,
  293. WarehouseInfoId: warehouse.ID,
  294. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  295. StorehouseId: storeConfig.DrugStorehouseOut,
  296. IsCheck: 1,
  297. }
  298. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  299. if errOne != nil {
  300. return errOne
  301. }
  302. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  303. details := &models.DrugAutomaticReduceDetail{
  304. WarehouseOutId: warehouseout.ID,
  305. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  306. PatientId: advice.PatientId,
  307. Ctime: time.Now().Unix(),
  308. Mtime: time.Now().Unix(),
  309. Status: 1,
  310. RecordTime: advice.RecordDate,
  311. OrgId: orgID,
  312. DrugId: advice.DrugId,
  313. Count: stock_number,
  314. CountUnit: drup.MinUnit,
  315. WarehouseInfoId: warehouse.ID,
  316. StorehouseId: storeConfig.DrugStorehouseOut,
  317. }
  318. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  319. if errTwo != nil {
  320. return errTwo
  321. }
  322. info := models.XtDrugWarehouseInfo{
  323. ID: warehouse.ID,
  324. WarehousingId: warehouse.WarehousingId,
  325. DrugId: warehouse.DrugId,
  326. Number: warehouse.Number,
  327. ProductDate: warehouse.ProductDate,
  328. ExpiryDate: warehouse.ExpiryDate,
  329. WarehousingCount: warehouse.WarehousingCount,
  330. Price: warehouse.RetailPrice,
  331. TotalPrice: warehouse.TotalPrice,
  332. Dealer: warehouse.Dealer,
  333. Manufacturer: warehouse.Manufacturer,
  334. Remark: warehouse.Remark,
  335. Ctime: warehouse.Ctime,
  336. Mtime: warehouse.Mtime,
  337. Status: 1,
  338. OrgId: warehouse.OrgId,
  339. IsReturn: warehouse.IsReturn,
  340. WarehousingOrder: warehouse.WarehousingOrder,
  341. Type: warehouse.Type,
  342. RetailPrice: warehouse.RetailPrice,
  343. RetailTotalPrice: warehouse.RetailPrice,
  344. StockMaxNumber: 0,
  345. StockMinNumber: 0,
  346. BatchNumber: warehouse.BatchNumber,
  347. MaxUnit: warehouse.MaxUnit,
  348. WarehousingInfoId: warehouse.WarehousingInfoId,
  349. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  350. StorehouseId: storeConfig.DrugStorehouseOut,
  351. IsCheck: 1,
  352. }
  353. //扣减库存
  354. errThree := UpDateDrugWarehouseInfoByStock(&info)
  355. if errThree != nil {
  356. return errThree
  357. }
  358. //查询剩余库存
  359. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  360. var sum_count int64
  361. for _, its := range stockInfo {
  362. if its.MaxUnit == drup.MaxUnit {
  363. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  364. }
  365. sum_count += its.StockMaxNumber + its.StockMinNumber
  366. }
  367. var out_count int64
  368. //查询出库数据
  369. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  370. for _, item := range infoCountList {
  371. out_count += item.Count
  372. }
  373. drugflow := models.DrugFlow{
  374. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  375. WarehouseOutId: warehouseout.ID,
  376. DrugId: advice.DrugId,
  377. Number: warehouse.Number,
  378. ProductDate: warehouse.ProductDate,
  379. ExpireDate: warehouse.ExpiryDate,
  380. Count: stock_number,
  381. Price: warehouse.RetailPrice,
  382. Status: 1,
  383. Ctime: time.Now().Unix(),
  384. UserOrgId: orgID,
  385. Manufacturer: warehouse.Manufacturer,
  386. Dealer: warehouse.Dealer,
  387. BatchNumber: warehouse.BatchNumber,
  388. MaxUnit: drup.MinUnit,
  389. ConsumableType: 3,
  390. IsEdit: 1,
  391. Creator: advice.ExecutionStaff,
  392. IsSys: 1,
  393. PatientId: advice.PatientId,
  394. WarehousingDetailId: warehouse.ID,
  395. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  396. StorehouseId: storeConfig.DrugStorehouseOut,
  397. WarehouseOutDetailId: lastDrugOutInfo.ID,
  398. LastPrice: warehouse.Price,
  399. OverCount: sum_count,
  400. }
  401. CreateDrugFlowOne(drugflow)
  402. //出库数量相加
  403. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  404. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  405. prescribingNumber_two_temp := deliver_number - stock_number
  406. overPlusNumber := float64(prescribingNumber_two_temp)
  407. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  408. advice.PrescribingNumberUnit = drup.MinUnit
  409. DrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  410. }
  411. return
  412. }
  413. // 药品出库
  414. func HisDrugsDelivery(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo) (err error) {
  415. // 1.判断药品是否来自专用字典的药品库
  416. // 2.判断当天当前机构有没有创建出库单,没有则创建
  417. // 3.创建出库流程
  418. // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  419. isHasWay := false
  420. record_time := int64(0)
  421. isHasWay = true
  422. record_time = advice.RecordDate
  423. if isHasWay {
  424. //判断当天当前机构有没有创建出库单,没有则创建
  425. out, err := FindDrugStockOutByIsSys(orgID, 1, record_time)
  426. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  427. if err == gorm.ErrRecordNotFound {
  428. timeStr := time.Now().Format("2006-01-02")
  429. timeArr := strings.Split(timeStr, "-")
  430. total, _ := FindAllDrugWarehouseOut(orgID)
  431. total = total + 1
  432. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  433. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  434. number = number + total
  435. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  436. warehouseOut := models.DrugWarehouseOut{
  437. WarehouseOutOrderNumber: warehousing_out_order,
  438. OperationTime: time.Now().Unix(),
  439. OrgId: orgID,
  440. Creater: creater,
  441. Ctime: time.Now().Unix(),
  442. Status: 1,
  443. WarehouseOutTime: record_time,
  444. Dealer: 0,
  445. Manufacturer: 0,
  446. Type: 1,
  447. IsSys: 1,
  448. StorehouseId: houseConfig.DrugStorehouseOut,
  449. IsCheck: 1,
  450. }
  451. err := AddSigleDrugWarehouseOut(&warehouseOut)
  452. if err != nil {
  453. utils.TraceLog("创建出库单失败 err = %v", err)
  454. return err
  455. } else {
  456. out = warehouseOut
  457. }
  458. }
  459. // 出库流程
  460. // 1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  461. drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
  462. if drup.ID > 0 {
  463. prescribingNumber := advice.PrescribingNumber
  464. //HisDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice)
  465. BloodHisDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice)
  466. //查询该患者今日总的出库数量
  467. //advicelist, _ := GetAllSumDrugWarehouseOut(advice.PatientId, advice.UserOrgId, advice.AdviceDate)
  468. //var total_count int64
  469. //for _,item :=range advicelist {
  470. // if(item.PrescribingNumberUnit == drup.MaxUnit && drup.MaxUnit!=drup.MinUnit){
  471. // prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  472. // count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  473. // total_count += count * drup.MinNumber
  474. // }
  475. // if(item.PrescribingNumberUnit == drup.MinUnit && drup.MaxUnit!=drup.MinUnit ){
  476. // prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  477. // count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  478. // total_count += count
  479. // }
  480. //package service
  481. //
  482. //import (
  483. // "XT_New/models"
  484. // "XT_New/utils"
  485. // "errors"
  486. // "fmt"
  487. // _ "fmt"
  488. // "github.com/jinzhu/gorm"
  489. // "math"
  490. // "strconv"
  491. // "strings"
  492. // "time"
  493. //)
  494. //
  495. //// 药品出库
  496. //
  497. //func DrugsDelivery(orgID int64, creater int64, advice *models.DoctorAdvice) (err error) {
  498. // // 1.判断药品是否来自专用字典的药品库
  499. // // 2.判断当天当前机构有没有创建出库单,没有则创建
  500. // // 3.创建出库流程
  501. // // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  502. // isHasWay := false
  503. // record_time := int64(0)
  504. // if advice.Way == 1 {
  505. // isHasWay = true
  506. // record_time = advice.RecordDate
  507. // }
  508. //
  509. // if isHasWay {
  510. // //判断当天当前机构有没有创建出库单,没有则创建
  511. // out, err := FindDrugStockOutByIsSys(orgID, 1, record_time)
  512. // if err == gorm.ErrRecordNotFound {
  513. // timeStr := time.Now().Format("2006-01-02")
  514. // timeArr := strings.Split(timeStr, "-")
  515. // total, _ := FindAllDrugWarehouseOut(orgID)
  516. // total = total + 1
  517. // warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  518. // number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  519. // number = number + total
  520. // warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  521. // storeConfig, _ := GetAllStoreHouseConfig(orgID)
  522. // warehouseOut := models.DrugWarehouseOut{
  523. // WarehouseOutOrderNumber: warehousing_out_order,
  524. // OperationTime: time.Now().Unix(),
  525. // OrgId: orgID,
  526. // Creater: creater,
  527. // Ctime: time.Now().Unix(),
  528. // Status: 1,
  529. // WarehouseOutTime: record_time,
  530. // Dealer: 0,
  531. // Manufacturer: 0,
  532. // Type: 1,
  533. // IsSys: 1,
  534. // StorehouseId: storeConfig.DrugStorehouseOut,
  535. // IsCheck: 1,
  536. // }
  537. // err := AddSigleDrugWarehouseOut(&warehouseOut)
  538. // if err != nil {
  539. // utils.TraceLog("创建出库单失败 err = %v", err)
  540. // return err
  541. // } else {
  542. // out = warehouseOut
  543. // }
  544. // }
  545. //
  546. // // 出库流程
  547. // // 1.查询该药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  548. // drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
  549. // if drup.ID > 0 {
  550. // prescribingNumber := advice.PrescribingNumber
  551. // DrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice)
  552. //
  553. // } else {
  554. // return errors.New("药品信息不存在")
  555. // }
  556. //
  557. // }
  558. //
  559. // return
  560. //}
  561. //
  562. //// 药品自动出库 递归方式
  563. //func DrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DoctorAdvice) (err error) {
  564. //
  565. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  566. // var deliver_number int64 = 0
  567. // var stock_number int64 = 0
  568. //
  569. // prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  570. // count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  571. //
  572. // var drug_price float64
  573. // if advice.PrescribingNumberUnit == drup.MaxUnit {
  574. // deliver_number = count * drup.MinNumber
  575. //
  576. // } else {
  577. // deliver_number = count
  578. //
  579. // }
  580. //
  581. // if advice.PrescribingNumberUnit == drup.MaxUnit {
  582. // drug_price = drup.RetailPrice
  583. // }
  584. // fmt.Println(drug_price)
  585. //
  586. // if advice.PrescribingNumberUnit == drup.MinUnit {
  587. // drug_price = drup.MinPrice
  588. // }
  589. //
  590. // if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  591. // drug_price = drup.RetailPrice
  592. // }
  593. //
  594. // // 根据先进先出原则,查询最先入库的批次,进行出库
  595. // // 如果没有对应的库存,则报错
  596. // storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  597. // lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  598. // if lastWarehouse.StockMinNumber >= drup.MinNumber {
  599. // var stockMax int64
  600. // var stockMin int64
  601. // stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  602. // stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  603. //
  604. // ChangeMaxNumber(lastWarehouse.ID, stockMax)
  605. // UpdateMinNumber(lastWarehouse.ID, stockMin)
  606. // }
  607. // warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  608. //
  609. // if err != nil {
  610. //
  611. // return err
  612. // }
  613. //
  614. // // 将该批次的剩余库存数量转换为拆零数量
  615. //
  616. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  617. //
  618. // //查找药品信息
  619. // //baseInfo, _ := FindBaseDrugLibRecord(advice.UserOrgId, advice.DrugId)
  620. //
  621. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  622. //
  623. // if stock_number >= deliver_number {
  624. //
  625. // var maxNumber int64 = 0
  626. // var minNumber int64 = 0
  627. // //var stock_max_number int64 = 0
  628. // //stock_max_number = warehouse.StockMaxNumber
  629. //
  630. // maxNumber = deliver_number / drup.MinNumber
  631. // minNumber = deliver_number % drup.MinNumber
  632. //
  633. // if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  634. // minNumber = maxNumber
  635. // }
  636. //
  637. // if drup.MaxUnit != drup.MinUnit {
  638. // if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  639. // return errors.New("库存数量不足")
  640. // }
  641. // }
  642. //
  643. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  644. //
  645. // if warehouse.StockMaxNumber < 0 {
  646. // warehouse.StockMaxNumber = 0
  647. // }
  648. // if warehouse.StockMinNumber < 0 {
  649. // warehouse.StockMinNumber = 0
  650. // }
  651. //
  652. // warehouse.Mtime = time.Now().Unix()
  653. //
  654. // if warehouse.StockMinNumber < minNumber {
  655. //
  656. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  657. // warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  658. // } else {
  659. // if minNumber > 0 {
  660. // warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  661. // }
  662. //
  663. // if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  664. // if warehouse.StockMinNumber > 0 {
  665. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  666. // }
  667. //
  668. // }
  669. //
  670. // }
  671. //
  672. // if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  673. // if (warehouse.StockMinNumber - deliver_number) > 0 {
  674. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  675. // }
  676. //
  677. // }
  678. //
  679. // if drup.MaxUnit != drup.MinUnit {
  680. // if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  681. // return errors.New("库存数量不足")
  682. // }
  683. // }
  684. // if warehouse.StockMinNumber <= 0 {
  685. // warehouse.StockMinNumber = 0
  686. // }
  687. //
  688. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  689. //
  690. // if errThree != nil {
  691. // return errThree
  692. // }
  693. //
  694. // //查询剩余库存
  695. // stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  696. // var sum_count int64
  697. // for _, its := range stockInfo {
  698. // if its.MaxUnit == drup.MaxUnit {
  699. // its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  700. // }
  701. // sum_count += its.StockMaxNumber + its.StockMinNumber
  702. // }
  703. //
  704. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  705. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  706. // WarehouseOutId: warehouseout.ID,
  707. // Status: 1,
  708. // Ctime: time.Now().Unix(),
  709. // Remark: warehouse.Remark,
  710. // OrgId: orgID,
  711. // Type: 1,
  712. // Manufacturer: warehouse.Manufacturer,
  713. // Dealer: warehouse.Dealer,
  714. // IsSys: 1,
  715. // SysRecordTime: advice.RecordDate,
  716. // DrugId: advice.DrugId,
  717. // Number: warehouse.Number,
  718. // BatchNumber: warehouse.BatchNumber,
  719. // Price: warehouse.RetailPrice,
  720. // CountUnit: drup.MinUnit,
  721. // RetailPrice: warehouse.RetailPrice,
  722. // ProductDate: warehouse.ProductDate,
  723. // ExpiryDate: warehouse.ExpiryDate,
  724. // PatientId: advice.PatientId,
  725. // Count: deliver_number, //出最小单位
  726. // WarehouseInfoId: warehouse.ID,
  727. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  728. // StorehouseId: storeConfig.DrugStorehouseOut,
  729. // IsCheck: 1,
  730. // OverCount: sum_count,
  731. // }
  732. //
  733. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  734. // fmt.Println(errOne)
  735. // lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  736. // drugflow := models.DrugFlow{
  737. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  738. // WarehouseOutId: warehouseout.ID,
  739. // DrugId: advice.DrugId,
  740. // Number: warehouse.Number,
  741. // ProductDate: warehouse.ProductDate,
  742. // ExpireDate: warehouse.ExpiryDate,
  743. // Count: deliver_number, //按最小单位计算,
  744. // Price: warehouse.RetailPrice,
  745. // Status: 1,
  746. // Ctime: time.Now().Unix(),
  747. // UserOrgId: orgID,
  748. // Manufacturer: warehouse.Manufacturer,
  749. // Dealer: warehouse.Dealer,
  750. // BatchNumber: warehouse.BatchNumber,
  751. // MaxUnit: drup.MinUnit,
  752. // ConsumableType: 3,
  753. // IsEdit: 1,
  754. // Creator: advice.ExecutionStaff,
  755. // IsSys: 1,
  756. // PatientId: advice.PatientId,
  757. // SystemTime: advice.AdviceDate,
  758. // WarehousingDetailId: warehouse.ID,
  759. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  760. // StorehouseId: storeConfig.DrugStorehouseOut,
  761. // WarehouseOutDetailId: lastDrugOutInfo.ID,
  762. // LastPrice: warehouse.Price,
  763. // OverCount: sum_count,
  764. // }
  765. //
  766. // CreateDrugFlowOne(drugflow)
  767. //
  768. // //查询是否存在数据
  769. //
  770. // details := &models.DrugAutomaticReduceDetail{
  771. // WarehouseOutId: warehouseout.ID,
  772. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  773. // PatientId: advice.PatientId,
  774. // Ctime: time.Now().Unix(),
  775. // Mtime: time.Now().Unix(),
  776. // Status: 1,
  777. // RecordTime: advice.RecordDate,
  778. // OrgId: orgID,
  779. // DrugId: advice.DrugId,
  780. // Count: deliver_number,
  781. // CountUnit: drup.MinUnit,
  782. // WarehouseInfoId: warehouse.ID,
  783. // StorehouseId: storeConfig.DrugStorehouseOut,
  784. // }
  785. // errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  786. // if errTwo != nil {
  787. // return errTwo
  788. // }
  789. //
  790. // var out_count int64
  791. // //查询出库数据
  792. // infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  793. // for _, item := range infoCountList {
  794. // out_count += item.Count
  795. // }
  796. // //出库数量相加
  797. // AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  798. //
  799. // return nil
  800. // } else {
  801. //
  802. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  803. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  804. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  805. // WarehouseOutId: warehouseout.ID,
  806. // Status: 1,
  807. // Ctime: time.Now().Unix(),
  808. // Remark: warehouse.Remark,
  809. // OrgId: orgID,
  810. // Type: 1,
  811. // Manufacturer: warehouse.Manufacturer,
  812. // Dealer: warehouse.Dealer,
  813. // IsSys: 1,
  814. // SysRecordTime: advice.RecordDate,
  815. // DrugId: advice.DrugId,
  816. // Number: warehouse.Number,
  817. // BatchNumber: warehouse.BatchNumber,
  818. // Price: warehouse.RetailPrice,
  819. // CountUnit: drup.MinUnit,
  820. // ProductDate: warehouse.ProductDate,
  821. // ExpiryDate: warehouse.ExpiryDate,
  822. // PatientId: advice.PatientId,
  823. // Count: stock_number,
  824. // WarehouseInfoId: warehouse.ID,
  825. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  826. // StorehouseId: storeConfig.DrugStorehouseOut,
  827. // IsCheck: 1,
  828. // }
  829. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  830. // if errOne != nil {
  831. // return errOne
  832. // }
  833. // lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  834. // details := &models.DrugAutomaticReduceDetail{
  835. // WarehouseOutId: warehouseout.ID,
  836. // WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  837. // PatientId: advice.PatientId,
  838. // Ctime: time.Now().Unix(),
  839. // Mtime: time.Now().Unix(),
  840. // Status: 1,
  841. // RecordTime: advice.RecordDate,
  842. // OrgId: orgID,
  843. // DrugId: advice.DrugId,
  844. // Count: stock_number,
  845. // CountUnit: drup.MinUnit,
  846. // WarehouseInfoId: warehouse.ID,
  847. // StorehouseId: storeConfig.DrugStorehouseOut,
  848. // }
  849. // errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  850. // if errTwo != nil {
  851. // return errTwo
  852. // }
  853. //
  854. // info := models.XtDrugWarehouseInfo{
  855. // ID: warehouse.ID,
  856. // WarehousingId: warehouse.WarehousingId,
  857. // DrugId: warehouse.DrugId,
  858. // Number: warehouse.Number,
  859. // ProductDate: warehouse.ProductDate,
  860. // ExpiryDate: warehouse.ExpiryDate,
  861. // WarehousingCount: warehouse.WarehousingCount,
  862. // Price: warehouse.RetailPrice,
  863. // TotalPrice: warehouse.TotalPrice,
  864. // Dealer: warehouse.Dealer,
  865. // Manufacturer: warehouse.Manufacturer,
  866. // Remark: warehouse.Remark,
  867. // Ctime: warehouse.Ctime,
  868. // Mtime: warehouse.Mtime,
  869. // Status: 1,
  870. // OrgId: warehouse.OrgId,
  871. // IsReturn: warehouse.IsReturn,
  872. // WarehousingOrder: warehouse.WarehousingOrder,
  873. // Type: warehouse.Type,
  874. // RetailPrice: warehouse.RetailPrice,
  875. // RetailTotalPrice: warehouse.RetailPrice,
  876. // StockMaxNumber: 0,
  877. // StockMinNumber: 0,
  878. // BatchNumber: warehouse.BatchNumber,
  879. // MaxUnit: warehouse.MaxUnit,
  880. // WarehousingInfoId: warehouse.WarehousingInfoId,
  881. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  882. // StorehouseId: storeConfig.DrugStorehouseOut,
  883. // IsCheck: 1,
  884. // }
  885. // //扣减库存
  886. // errThree := UpDateDrugWarehouseInfoByStock(&info)
  887. //
  888. // if errThree != nil {
  889. // return errThree
  890. // }
  891. //
  892. // //查询剩余库存
  893. // stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  894. // var sum_count int64
  895. // for _, its := range stockInfo {
  896. // if its.MaxUnit == drup.MaxUnit {
  897. // its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  898. // }
  899. // sum_count += its.StockMaxNumber + its.StockMinNumber
  900. // }
  901. //
  902. // var out_count int64
  903. //
  904. // //查询出库数据
  905. // infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  906. // for _, item := range infoCountList {
  907. // out_count += item.Count
  908. // }
  909. //
  910. // drugflow := models.DrugFlow{
  911. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  912. // WarehouseOutId: warehouseout.ID,
  913. // DrugId: advice.DrugId,
  914. // Number: warehouse.Number,
  915. // ProductDate: warehouse.ProductDate,
  916. // ExpireDate: warehouse.ExpiryDate,
  917. // Count: stock_number,
  918. // Price: warehouse.RetailPrice,
  919. // Status: 1,
  920. // Ctime: time.Now().Unix(),
  921. // UserOrgId: orgID,
  922. // Manufacturer: warehouse.Manufacturer,
  923. // Dealer: warehouse.Dealer,
  924. // BatchNumber: warehouse.BatchNumber,
  925. // MaxUnit: drup.MinUnit,
  926. // ConsumableType: 3,
  927. // IsEdit: 1,
  928. // Creator: advice.ExecutionStaff,
  929. // IsSys: 1,
  930. // PatientId: advice.PatientId,
  931. // WarehousingDetailId: warehouse.ID,
  932. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  933. // StorehouseId: storeConfig.DrugStorehouseOut,
  934. // WarehouseOutDetailId: lastDrugOutInfo.ID,
  935. // LastPrice: warehouse.Price,
  936. // OverCount: sum_count,
  937. // }
  938. //
  939. // CreateDrugFlowOne(drugflow)
  940. // //出库数量相加
  941. // AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  942. //
  943. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  944. // prescribingNumber_two_temp := deliver_number - stock_number
  945. //
  946. // overPlusNumber := float64(prescribingNumber_two_temp)
  947. //
  948. // advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  949. // advice.PrescribingNumberUnit = drup.MinUnit
  950. //
  951. // DrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  952. // }
  953. //
  954. // return
  955. //}
  956. //
  957. //// 药品出库
  958. //
  959. //func HisDrugsDelivery(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo) (err error) {
  960. //
  961. // // 1.判断药品是否来自专用字典的药品库
  962. // // 2.判断当天当前机构有没有创建出库单,没有则创建
  963. // // 3.创建出库流程
  964. // // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  965. // isHasWay := false
  966. // record_time := int64(0)
  967. // isHasWay = true
  968. // record_time = advice.RecordDate
  969. //
  970. // if isHasWay {
  971. // //判断当天当前机构有没有创建出库单,没有则创建
  972. // out, err := FindDrugStockOutByIsSys(orgID, 1, record_time)
  973. // houseConfig, _ := GetAllStoreHouseConfig(orgID)
  974. // if err == gorm.ErrRecordNotFound {
  975. // timeStr := time.Now().Format("2006-01-02")
  976. // timeArr := strings.Split(timeStr, "-")
  977. // total, _ := FindAllDrugWarehouseOut(orgID)
  978. // total = total + 1
  979. // warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  980. // number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  981. // number = number + total
  982. // warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  983. //
  984. // warehouseOut := models.DrugWarehouseOut{
  985. // WarehouseOutOrderNumber: warehousing_out_order,
  986. // OperationTime: time.Now().Unix(),
  987. // OrgId: orgID,
  988. // Creater: creater,
  989. // Ctime: time.Now().Unix(),
  990. // Status: 1,
  991. // WarehouseOutTime: record_time,
  992. // Dealer: 0,
  993. // Manufacturer: 0,
  994. // Type: 1,
  995. // IsSys: 1,
  996. // StorehouseId: houseConfig.DrugStorehouseOut,
  997. // IsCheck: 1,
  998. // }
  999. // err := AddSigleDrugWarehouseOut(&warehouseOut)
  1000. // if err != nil {
  1001. // utils.TraceLog("创建出库单失败 err = %v", err)
  1002. // return err
  1003. // } else {
  1004. // out = warehouseOut
  1005. // }
  1006. // }
  1007. //
  1008. // // 出库流程
  1009. // // 1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  1010. // drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
  1011. // if drup.ID > 0 {
  1012. // prescribingNumber := advice.PrescribingNumber
  1013. // //HisDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice)
  1014. //
  1015. // BloodHisDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice)
  1016. //
  1017. // //查询该患者今日总的出库数量
  1018. // //advicelist, _ := GetAllSumDrugWarehouseOut(advice.PatientId, advice.UserOrgId, advice.AdviceDate)
  1019. // //var total_count int64
  1020. // //for _,item :=range advicelist {
  1021. // // if(item.PrescribingNumberUnit == drup.MaxUnit && drup.MaxUnit!=drup.MinUnit){
  1022. // // prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  1023. // // count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1024. // // total_count += count * drup.MinNumber
  1025. // // }
  1026. // // if(item.PrescribingNumberUnit == drup.MinUnit && drup.MaxUnit!=drup.MinUnit ){
  1027. // // prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  1028. // // count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1029. // // total_count += count
  1030. // // }
  1031. // //
  1032. // // if(item.PrescribingNumberUnit == drup.MaxUnit && drup.MaxUnit ==drup.MinUnit ){
  1033. // // prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  1034. // // count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1035. // // total_count += count
  1036. // // }
  1037. // // info := models.DrugWarehouseOutInfo{
  1038. // // Count: total_count,
  1039. // // CountUnit: drup.MinUnit,
  1040. // // }
  1041. // //
  1042. // // fmt.Println("total_coutn 233232322323232323233222322wo",total_count)
  1043. // // //更新出库单数据
  1044. // // UpdateDrugWarehouseOutByPatientId(advice.PatientId,advice.AdviceDate,advice.UserOrgId,info)
  1045. // //}
  1046. //
  1047. // } else {
  1048. // return errors.New("药品信息不存在")
  1049. // }
  1050. //
  1051. // }
  1052. //
  1053. // return
  1054. //}
  1055. //
  1056. //// 药品出库 递归方式
  1057. //func HisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  1058. //
  1059. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  1060. // var deliver_number int64 = 0
  1061. // var stock_number int64 = 0
  1062. //
  1063. // prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  1064. // count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1065. //
  1066. // if advice.PrescribingNumberUnit == drup.MaxUnit {
  1067. // deliver_number = count * drup.MinNumber
  1068. // } else {
  1069. // deliver_number = count
  1070. // }
  1071. //
  1072. // var min_price float64
  1073. //
  1074. // if advice.PrescribingNumberUnit == drup.MaxUnit {
  1075. // min_price = drup.RetailPrice
  1076. // }
  1077. // if advice.PrescribingNumberUnit == drup.MinUnit {
  1078. // min_price = drup.MinPrice
  1079. // }
  1080. //
  1081. // if advice.PrescribingNumberUnit != drup.MaxUnit && advice.PrescribingNumberUnit != drup.MinUnit {
  1082. // min_price = drup.RetailPrice
  1083. // }
  1084. // fmt.Println(min_price)
  1085. // // 根据先进先出原则,查询最先入库的批次,进行出库
  1086. // // 如果没有对应的库存,则报错
  1087. //
  1088. // //查询默认仓库
  1089. // storeConfig, _ := GetAllStoreHouseConfig(orgID)
  1090. // lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1091. // if lastWarehouse.StockMinNumber >= drup.MinNumber {
  1092. // var stockMax int64
  1093. // var stockMin int64
  1094. // stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  1095. // stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  1096. //
  1097. // ChangeMaxNumber(lastWarehouse.ID, stockMax)
  1098. // UpdateMinNumber(lastWarehouse.ID, stockMin)
  1099. // }
  1100. // warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1101. //
  1102. // if err != nil {
  1103. //
  1104. // return err
  1105. // }
  1106. //
  1107. // // 将该批次的剩余库存数量转换为拆零数量
  1108. //
  1109. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  1110. //
  1111. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1112. // if stock_number >= deliver_number {
  1113. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1114. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1115. // WarehouseOutId: warehouseout.ID,
  1116. // Status: 1,
  1117. // Ctime: time.Now().Unix(),
  1118. // Remark: warehouse.Remark,
  1119. // OrgId: orgID,
  1120. // Type: 1,
  1121. // Manufacturer: warehouse.Manufacturer,
  1122. // Dealer: warehouse.Dealer,
  1123. // IsSys: 1,
  1124. // SysRecordTime: advice.RecordDate,
  1125. // DrugId: advice.DrugId,
  1126. // Number: warehouse.Number,
  1127. // BatchNumber: warehouse.BatchNumber,
  1128. // Price: warehouse.RetailPrice,
  1129. // CountUnit: drup.MinUnit,
  1130. // RetailPrice: warehouse.RetailPrice,
  1131. // ProductDate: warehouse.ProductDate,
  1132. // ExpiryDate: warehouse.ExpiryDate,
  1133. // PatientId: advice.PatientId,
  1134. // WarehouseInfoId: warehouse.ID,
  1135. // Count: deliver_number,
  1136. // AdviceId: advice.ID,
  1137. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1138. // StorehouseId: storeConfig.DrugStorehouseOut,
  1139. // IsCheck: 1,
  1140. // LastPrice: warehouse.Price,
  1141. // }
  1142. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1143. // lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  1144. // if errOne != nil {
  1145. // return errOne
  1146. // }
  1147. // drugflow := models.DrugFlow{
  1148. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1149. // WarehouseOutId: warehouseout.ID,
  1150. // DrugId: advice.DrugId,
  1151. // Number: warehouse.Number,
  1152. // ProductDate: warehouse.ProductDate,
  1153. // ExpireDate: warehouse.ExpiryDate,
  1154. // Count: deliver_number,
  1155. // Price: warehouse.RetailPrice,
  1156. // Status: 1,
  1157. // Ctime: time.Now().Unix(),
  1158. // UserOrgId: orgID,
  1159. // Manufacturer: warehouse.Manufacturer,
  1160. // Dealer: warehouse.Dealer,
  1161. // BatchNumber: warehouse.BatchNumber,
  1162. // MaxUnit: drup.MinUnit,
  1163. // ConsumableType: 3,
  1164. // IsEdit: 1,
  1165. // Creator: advice.ExecutionStaff,
  1166. // IsSys: 1,
  1167. // PatientId: advice.PatientId,
  1168. // SystemTime: advice.AdviceDate,
  1169. // WarehousingDetailId: warehouse.ID,
  1170. // AdviceId: advice.ID,
  1171. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1172. // StorehouseId: storeConfig.DrugStorehouseOut,
  1173. // WarehouseOutDetailId: lastDrugOutInfo.ID,
  1174. // LastPrice: warehouse.Price,
  1175. // }
  1176. //
  1177. // CreateDrugFlowOne(drugflow)
  1178. //
  1179. // details := &models.DrugAutomaticReduceDetail{
  1180. // WarehouseOutId: warehouseout.ID,
  1181. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1182. // PatientId: advice.PatientId,
  1183. // Ctime: time.Now().Unix(),
  1184. // Mtime: time.Now().Unix(),
  1185. // Status: 1,
  1186. // RecordTime: advice.RecordDate,
  1187. // OrgId: orgID,
  1188. // DrugId: advice.DrugId,
  1189. // Count: deliver_number,
  1190. // CountUnit: drup.MinUnit,
  1191. // WarehouseInfoId: warehouse.ID,
  1192. // AdviceId: advice.ID,
  1193. // StorehouseId: storeConfig.DrugStorehouseOut,
  1194. // }
  1195. // errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  1196. // if errTwo != nil {
  1197. // return errTwo
  1198. // }
  1199. //
  1200. // // 出库完成后,要减去对应批次的库存数量
  1201. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  1202. // var maxNumber int64 = 0
  1203. // var minNumber int64 = 0
  1204. //
  1205. // maxNumber = deliver_number / drup.MinNumber
  1206. // minNumber = deliver_number % drup.MinNumber
  1207. //
  1208. // fmt.Println("maxNumber2323233232323232323232", maxNumber)
  1209. // fmt.Println("minNumber2323233232323232323232", minNumber)
  1210. // if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  1211. // minNumber = maxNumber
  1212. // }
  1213. //
  1214. // if drup.MaxUnit != drup.MinUnit {
  1215. // if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  1216. // return errors.New("库存数量不足")
  1217. // }
  1218. // }
  1219. //
  1220. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  1221. //
  1222. // if warehouse.StockMaxNumber < 0 {
  1223. // warehouse.StockMaxNumber = 0
  1224. // }
  1225. // if warehouse.StockMinNumber < 0 {
  1226. // warehouse.StockMinNumber = 0
  1227. // }
  1228. //
  1229. // warehouse.Mtime = time.Now().Unix()
  1230. // fmt.Println("warehouse.StockMinNumber", warehouse.StockMinNumber)
  1231. // fmt.Println("minNumber", minNumber)
  1232. // fmt.Println("maxNumber", maxNumber)
  1233. // if warehouse.StockMinNumber < minNumber {
  1234. //
  1235. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  1236. // if warehouse.MaxUnit != warehouse.MinUnit {
  1237. //
  1238. // warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  1239. // }
  1240. // } else {
  1241. //
  1242. // if minNumber > 0 {
  1243. // warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  1244. // }
  1245. //
  1246. // }
  1247. //
  1248. // if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  1249. // if (warehouse.StockMinNumber - deliver_number) > 0 {
  1250. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  1251. // }
  1252. // }
  1253. //
  1254. // if drup.MaxUnit != drup.MinUnit {
  1255. // if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  1256. // return errors.New("库存数量不足")
  1257. // }
  1258. // }
  1259. // if warehouse.StockMinNumber <= 0 {
  1260. // warehouse.StockMinNumber = 0
  1261. // }
  1262. // //扣减库存
  1263. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  1264. //
  1265. // if errThree != nil {
  1266. // return errThree
  1267. // }
  1268. // drugInfoList, _ := FindDrugWarehouseInfoList(advice.DrugId, orgID)
  1269. // var drug_max_number int64
  1270. // var drug_min_number int64
  1271. // for _, item := range drugInfoList {
  1272. // drug_max_number += item.StockMaxNumber
  1273. // drug_min_number += item.StockMinNumber
  1274. // }
  1275. // drugFlushInfo := models.XtDrugWarehouseFlushInfo{
  1276. // DrugMaxNumber: drug_max_number,
  1277. // UserOrgId: orgID,
  1278. // WarehouseOutId: warehouseout.ID,
  1279. // Type: 1,
  1280. // SystemTime: advice.RecordDate,
  1281. // PatientId: advice.PatientId,
  1282. // Ctime: time.Now().Unix(),
  1283. // Mtime: 0,
  1284. // BatchNumberId: warehouse.ID,
  1285. // DrugId: advice.DrugId,
  1286. // Count: deliver_number,
  1287. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1288. // Creater: warehouseout.Creater,
  1289. // DrugMinNumber: drug_min_number,
  1290. // Unit: drup.MinUnit,
  1291. // AdviceId: advice.ID,
  1292. // Status: 1,
  1293. // WarehouseOutDetailId: lastDrugOutInfo.ID,
  1294. // }
  1295. // CreatedDrugFlushInfo(drugFlushInfo)
  1296. //
  1297. // return nil
  1298. // } else {
  1299. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  1300. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1301. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1302. // WarehouseOutId: warehouseout.ID,
  1303. // Status: 1,
  1304. // Ctime: time.Now().Unix(),
  1305. // Remark: warehouse.Remark,
  1306. // OrgId: orgID,
  1307. // Type: 1,
  1308. // Manufacturer: warehouse.Manufacturer,
  1309. // Dealer: warehouse.Dealer,
  1310. // IsSys: 1,
  1311. // SysRecordTime: advice.RecordDate,
  1312. // DrugId: advice.DrugId,
  1313. // Number: warehouse.Number,
  1314. // BatchNumber: warehouse.BatchNumber,
  1315. // Price: warehouse.RetailPrice,
  1316. // RetailPrice: warehouse.RetailPrice,
  1317. // ProductDate: warehouse.ProductDate,
  1318. // ExpiryDate: warehouse.ExpiryDate,
  1319. // PatientId: advice.PatientId,
  1320. // WarehouseInfoId: warehouse.ID,
  1321. // Count: stock_number,
  1322. // CountUnit: drup.MinUnit,
  1323. // AdviceId: advice.ID,
  1324. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1325. // StorehouseId: storeConfig.DrugStorehouseOut,
  1326. // IsCheck: 1,
  1327. // }
  1328. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1329. //
  1330. // if errOne != nil {
  1331. // return errOne
  1332. // }
  1333. // lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  1334. // drugflow := models.DrugFlow{
  1335. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1336. // WarehouseOutId: warehouseout.ID,
  1337. // DrugId: advice.DrugId,
  1338. // Number: warehouse.Number,
  1339. // ProductDate: warehouse.ProductDate,
  1340. // ExpireDate: warehouse.ExpiryDate,
  1341. // Count: stock_number,
  1342. // Price: warehouse.RetailPrice,
  1343. // Status: 1,
  1344. // Ctime: time.Now().Unix(),
  1345. // UserOrgId: orgID,
  1346. // Manufacturer: warehouse.Manufacturer,
  1347. // Dealer: warehouse.Dealer,
  1348. // BatchNumber: warehouse.BatchNumber,
  1349. // MaxUnit: drup.MinUnit,
  1350. // ConsumableType: 3,
  1351. // IsEdit: 1,
  1352. // Creator: advice.ExecutionStaff,
  1353. // IsSys: 1,
  1354. // PatientId: advice.PatientId,
  1355. // SystemTime: advice.AdviceDate,
  1356. // WarehousingDetailId: warehouse.ID,
  1357. // AdviceId: advice.ID,
  1358. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1359. // StorehouseId: storeConfig.DrugStorehouseOut,
  1360. // WarehouseOutDetailId: lastDrugOutInfo.ID,
  1361. // LastPrice: warehouse.Price,
  1362. // }
  1363. //
  1364. // CreateDrugFlowOne(drugflow)
  1365. //
  1366. // //查询是否存在数据
  1367. //
  1368. // details := &models.DrugAutomaticReduceDetail{
  1369. // WarehouseOutId: warehouseout.ID,
  1370. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1371. // PatientId: advice.PatientId,
  1372. // Ctime: time.Now().Unix(),
  1373. // Mtime: time.Now().Unix(),
  1374. // Status: 1,
  1375. // RecordTime: advice.RecordDate,
  1376. // OrgId: orgID,
  1377. // DrugId: advice.DrugId,
  1378. // Count: stock_number,
  1379. // CountUnit: drup.MinUnit,
  1380. // WarehouseInfoId: warehouse.ID,
  1381. // AdviceId: advice.ID,
  1382. // StorehouseId: storeConfig.DrugStorehouseOut,
  1383. // }
  1384. // errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  1385. // if errTwo != nil {
  1386. // return errTwo
  1387. // }
  1388. //
  1389. // info := models.XtDrugWarehouseInfo{
  1390. // ID: warehouse.ID,
  1391. // WarehousingId: warehouse.WarehousingId,
  1392. // DrugId: warehouse.DrugId,
  1393. // Number: warehouse.Number,
  1394. // ProductDate: warehouse.ProductDate,
  1395. // ExpiryDate: warehouse.ExpiryDate,
  1396. // WarehousingCount: warehouse.WarehousingCount,
  1397. // Price: warehouse.Price,
  1398. // TotalPrice: warehouse.TotalPrice,
  1399. // Dealer: warehouse.Dealer,
  1400. // Manufacturer: warehouse.Manufacturer,
  1401. // Remark: warehouse.Remark,
  1402. // Ctime: warehouse.Ctime,
  1403. // Mtime: warehouse.Mtime,
  1404. // Status: 1,
  1405. // OrgId: warehouse.OrgId,
  1406. // IsReturn: warehouse.IsReturn,
  1407. // WarehousingOrder: warehouse.WarehousingOrder,
  1408. // Type: warehouse.Type,
  1409. // RetailPrice: warehouse.RetailPrice,
  1410. // RetailTotalPrice: warehouse.RetailPrice,
  1411. // StockMaxNumber: 0,
  1412. // StockMinNumber: 0,
  1413. // BatchNumber: warehouse.BatchNumber,
  1414. // MaxUnit: warehouse.MaxUnit,
  1415. // MinUnit: warehouse.MinUnit,
  1416. // WarehousingInfoId: warehouse.WarehousingInfoId,
  1417. // StorehouseId: storeConfig.DrugStorehouseOut,
  1418. // IsCheck: 1,
  1419. // }
  1420. // errThree := UpDateDrugWarehouseInfoByStock(&info)
  1421. //
  1422. // if errThree != nil {
  1423. // return errThree
  1424. // }
  1425. //
  1426. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1427. // prescribingNumber_two_temp := deliver_number - stock_number
  1428. //
  1429. // overPlusNumber := float64(prescribingNumber_two_temp)
  1430. //
  1431. // advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  1432. //
  1433. // advice.PrescribingNumberUnit = drup.MinUnit
  1434. //
  1435. // HisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  1436. // }
  1437. //
  1438. // return
  1439. //}
  1440. //
  1441. //// 药品出库 递归方式
  1442. //func AutoDrugDeliverInfo(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  1443. //
  1444. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  1445. // var deliver_number int64 = 0
  1446. // var stock_number int64 = 0
  1447. //
  1448. // if advice.CountUnit == drup.MaxUnit {
  1449. // deliver_number = prescribingNumber * drup.MinNumber
  1450. // } else {
  1451. // deliver_number = prescribingNumber
  1452. // }
  1453. //
  1454. // // 根据先进先出原则,查询最先入库的批次,进行出库
  1455. // // 如果没有对应的库存,则报错
  1456. // //开启事物
  1457. //
  1458. // storeConfig, _ := GetAllStoreHouseConfig(orgID)
  1459. // lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1460. // if lastWarehouse.StockMinNumber >= drup.MinNumber {
  1461. // var stockMax int64
  1462. // var stockMin int64
  1463. // stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  1464. // stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  1465. // ChangeMaxNumber(lastWarehouse.ID, stockMax)
  1466. // UpdateMinNumber(lastWarehouse.ID, stockMin)
  1467. // }
  1468. //
  1469. // warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1470. //
  1471. // if err != nil {
  1472. //
  1473. // return err
  1474. // }
  1475. //
  1476. // // 将该批次的剩余库存数量转换为拆零数量
  1477. //
  1478. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  1479. //
  1480. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1481. // if stock_number >= deliver_number {
  1482. //
  1483. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1484. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1485. // WarehouseOutId: warehouseout.ID,
  1486. // Status: 1,
  1487. // Ctime: time.Now().Unix(),
  1488. // Remark: advice.Remark,
  1489. // OrgId: orgID,
  1490. // Type: 1,
  1491. // Manufacturer: advice.Manufacturer,
  1492. // Dealer: warehouse.Dealer,
  1493. // IsSys: 0,
  1494. // SysRecordTime: advice.Ctime,
  1495. // DrugId: advice.DrugId,
  1496. // ExpiryDate: advice.ExpiryDate,
  1497. // ProductDate: advice.ProductDate,
  1498. // Number: advice.Number,
  1499. // BatchNumber: warehouse.BatchNumber,
  1500. // Count: deliver_number,
  1501. // RetailPrice: advice.RetailPrice,
  1502. // Price: warehouse.RetailPrice,
  1503. // WarehouseInfoId: warehouse.ID,
  1504. // CountUnit: drup.MinUnit,
  1505. // AdviceId: advice.ID,
  1506. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1507. // StorehouseId: advice.StorehouseId,
  1508. // AdminUserId: advice.AdminUserId,
  1509. // IsCheck: 1,
  1510. // }
  1511. //
  1512. // warehouseOutInfo.Count = prescribingNumber
  1513. // warehouseOutInfo.CountUnit = advice.CountUnit
  1514. //
  1515. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1516. // if errOne != nil {
  1517. // return errOne
  1518. // }
  1519. //
  1520. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  1521. // drugflow := models.DrugFlow{
  1522. // WarehouseOutId: warehouseout.ID,
  1523. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1524. // DrugId: advice.DrugId,
  1525. // Number: warehouse.Number,
  1526. // ProductDate: advice.ProductDate,
  1527. // ExpireDate: advice.ExpiryDate,
  1528. // Count: deliver_number,
  1529. // Price: warehouse.RetailPrice,
  1530. // Status: 1,
  1531. // Ctime: time.Now().Unix(),
  1532. // UserOrgId: advice.OrgId,
  1533. // Manufacturer: advice.Manufacturer,
  1534. // Dealer: advice.Dealer,
  1535. // BatchNumber: warehouse.BatchNumber,
  1536. // MaxUnit: drup.MinUnit,
  1537. // ConsumableType: 2,
  1538. // IsEdit: 1,
  1539. // Creator: 0,
  1540. // IsSys: 0,
  1541. // WarehouseOutDetailId: drugWareInfo.ID,
  1542. // AdviceId: advice.ID,
  1543. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1544. // StorehouseId: advice.StorehouseId,
  1545. // AdminUserId: advice.AdminUserId,
  1546. // }
  1547. //
  1548. // CreateDrugFlowOne(drugflow)
  1549. // // 出库完成后,要减去对应批次的库存数量
  1550. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  1551. // var maxNumber int64 = 0
  1552. // var minNumber int64 = 0
  1553. // if advice.CountUnit == drup.MinUnit {
  1554. // maxNumber = prescribingNumber / drup.MinNumber
  1555. // minNumber = prescribingNumber % drup.MinNumber
  1556. // if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  1557. // minNumber = maxNumber
  1558. // }
  1559. // } else {
  1560. // maxNumber = prescribingNumber
  1561. // }
  1562. //
  1563. // if warehouse.StockMaxNumber < maxNumber {
  1564. //
  1565. // return errors.New("库存数量不足")
  1566. // }
  1567. //
  1568. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  1569. // warehouse.Mtime = time.Now().Unix()
  1570. //
  1571. // if warehouse.StockMinNumber < minNumber {
  1572. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  1573. // warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  1574. // } else {
  1575. // warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  1576. // }
  1577. //
  1578. // if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
  1579. // warehouse.StockMinNumber = 0
  1580. // }
  1581. //
  1582. // if warehouse.StockMaxNumber < 0 {
  1583. // return errors.New("库存数量不足")
  1584. // }
  1585. //
  1586. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  1587. // if errThree != nil {
  1588. // return errThree
  1589. // }
  1590. //
  1591. // //查询默认仓库
  1592. // houseConfig, _ := GetAllStoreHouseConfig(orgID)
  1593. // //查询默认仓库剩余多少库存
  1594. // list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  1595. // var sum_count int64
  1596. // var sum_in_count int64
  1597. // for _, it := range list {
  1598. // baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  1599. // if it.MaxUnit == baseDrug.MaxUnit {
  1600. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  1601. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  1602. // }
  1603. // sum_count += it.StockMaxNumber + it.StockMinNumber
  1604. // sum_in_count += it.WarehousingCount
  1605. // }
  1606. // UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  1607. //
  1608. // return nil
  1609. //
  1610. // } else {
  1611. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  1612. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1613. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1614. // WarehouseOutId: warehouseout.ID,
  1615. // Status: 1,
  1616. // Ctime: time.Now().Unix(),
  1617. // Remark: advice.Remark,
  1618. // OrgId: orgID,
  1619. // Type: 1,
  1620. // Manufacturer: advice.Manufacturer,
  1621. // Dealer: warehouse.Dealer,
  1622. // IsSys: 0,
  1623. // SysRecordTime: advice.Ctime,
  1624. // DrugId: advice.DrugId,
  1625. // ExpiryDate: warehouse.ExpiryDate,
  1626. // ProductDate: warehouse.ProductDate,
  1627. // Number: warehouse.Number,
  1628. // BatchNumber: warehouse.BatchNumber,
  1629. // Count: stock_number,
  1630. // RetailPrice: advice.RetailPrice,
  1631. // Price: warehouse.RetailPrice,
  1632. // WarehouseInfoId: warehouse.ID,
  1633. // CountUnit: drup.MinUnit,
  1634. // AdviceId: advice.ID,
  1635. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1636. // StorehouseId: advice.StorehouseId,
  1637. // AdminUserId: advice.AdminUserId,
  1638. // IsCheck: 1,
  1639. // }
  1640. // warehouseOutInfo.Count = stock_number
  1641. //
  1642. // //warehouseOutInfo.CountUnit = advice.CountUnit
  1643. //
  1644. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1645. // if errOne != nil {
  1646. // return errOne
  1647. // }
  1648. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  1649. // drugflow := models.DrugFlow{
  1650. // WarehouseOutId: warehouseout.ID,
  1651. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1652. // DrugId: advice.DrugId,
  1653. // Number: warehouse.Number,
  1654. // ProductDate: advice.ProductDate,
  1655. // ExpireDate: advice.ExpiryDate,
  1656. // Count: stock_number,
  1657. // Price: warehouse.RetailPrice,
  1658. // Status: 1,
  1659. // Ctime: time.Now().Unix(),
  1660. // UserOrgId: advice.OrgId,
  1661. // Manufacturer: advice.Manufacturer,
  1662. // Dealer: advice.Dealer,
  1663. // BatchNumber: warehouse.BatchNumber,
  1664. // MaxUnit: drup.MinUnit,
  1665. // ConsumableType: 2,
  1666. // IsEdit: 1,
  1667. // Creator: 0,
  1668. // IsSys: 0,
  1669. // WarehouseOutDetailId: drugWareInfo.ID,
  1670. // AdviceId: advice.ID,
  1671. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1672. // StorehouseId: advice.StorehouseId,
  1673. // AdminUserId: advice.AdminUserId,
  1674. // }
  1675. // CreateDrugFlowOne(drugflow)
  1676. // // 出库完成后,要将该批次库存清零
  1677. // //扣减库存
  1678. //
  1679. // warehouse.StockMaxNumber = 0
  1680. // warehouse.StockMinNumber = 0
  1681. // warehouse.Mtime = time.Now().Unix()
  1682. //
  1683. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  1684. //
  1685. // //查询默认仓库
  1686. // houseConfig, _ := GetAllStoreHouseConfig(orgID)
  1687. // //查询默认仓库剩余多少库存
  1688. // list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  1689. // var sum_count int64
  1690. // var sum_in_count int64
  1691. // for _, it := range list {
  1692. // baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  1693. // if it.MaxUnit == baseDrug.MaxUnit {
  1694. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  1695. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  1696. // sum_count += it.StockMaxNumber + it.StockMinNumber
  1697. // sum_in_count += it.WarehousingCount
  1698. // }
  1699. // }
  1700. // UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  1701. // if errThree != nil {
  1702. // return errThree
  1703. // }
  1704. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1705. // prescribingNumber_two_temp := deliver_number - stock_number
  1706. //
  1707. // advice.CountUnit = drup.MinUnit
  1708. //
  1709. // AutoDrugDeliverInfo(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  1710. // }
  1711. //
  1712. // return
  1713. //}
  1714. //
  1715. //// 耗材出库
  1716. //func ConsumablesDeliveryTotal(orgID int64, patient_id int64, record_time int64, goods []*models.DialysisBeforePrepareGoods, goodOne []*models.NewDialysisBeforePrepareGoods, creator int64) (err error) {
  1717. //
  1718. // //查询该患者当天已经出库的耗材信息
  1719. // goods_yc, _ := FindConsumablesByDateTwo(orgID, patient_id, record_time)
  1720. // // 和新请求的出库数据进行对比,分出那些是继续出库的,那些是需要删除出库的
  1721. // for i := len(goods_yc) - 1; i >= 0; i-- {
  1722. // goods_yc_temp := goods_yc[i]
  1723. // for j := len(goods) - 1; j >= 0; j-- {
  1724. // goods_temp := goods[j]
  1725. // // 已经出库和新请求出库都存在该耗材,则判断出库数量,分成是继续出库,还是删除出库
  1726. // if goods_yc_temp.GoodTypeId == goods_temp.GoodTypeId && goods_yc_temp.GoodId == goods_temp.GoodId {
  1727. // // 已经出库和新请求出库的出库数量一致,则清除两个结构体里的数据(既不出库,也不删除出库)
  1728. // if goods_yc_temp.Count == goods_temp.Count {
  1729. // goods_yc = append(goods_yc[:i], goods_yc[i+1:]...)
  1730. // goods = append(goods[:j], goods[j+1:]...)
  1731. // break
  1732. // }
  1733. // // 如果已经出库的数量 大于 新请求出库的数量,则代表需要删除出库
  1734. // if goods_yc_temp.Count > goods_temp.Count {
  1735. //
  1736. // temp_count := goods_yc_temp.Count - goods_temp.Count
  1737. // goods_yc[i].Count = temp_count
  1738. // goods = append(goods[:j], goods[j+1:]...)
  1739. // break
  1740. // }
  1741. // // 如果已经出库的数量 小于 新请求出库的梳理,则代表需要增加出库
  1742. // if goods_yc_temp.Count < goods_temp.Count {
  1743. // temp_count := goods_temp.Count - goods_yc_temp.Count
  1744. //
  1745. // goods[j].Count = temp_count
  1746. // goods_yc = append(goods_yc[:i], goods_yc[i+1:]...)
  1747. //
  1748. // break
  1749. // }
  1750. // }
  1751. // }
  1752. // }
  1753. //
  1754. // // goods_yc 这个数据就是需要已经出库了,但是现在需要删除出库的耗材数据
  1755. // // goods 这个数据就是需要出库的耗材的数据(新增的数据)
  1756. //
  1757. // if len(goods) > 0 {
  1758. // out, err := FindStockOutByIsSys(orgID, 1, record_time)
  1759. // houseConfig, _ := GetAllStoreHouseConfig(orgID)
  1760. // if err == gorm.ErrRecordNotFound {
  1761. // //没有记录,则创建出库单
  1762. // timeStr := time.Now().Format("2006-01-02")
  1763. // timeArr := strings.Split(timeStr, "-")
  1764. // total, _ := FindAllWarehouseOut(orgID)
  1765. // total = total + 1
  1766. // warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  1767. // number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  1768. // number = number + total
  1769. // warehousing_out_order = "CKD" + strconv.FormatInt(number, 10)
  1770. //
  1771. // warehouseOut := models.WarehouseOut{
  1772. // WarehouseOutOrderNumber: warehousing_out_order,
  1773. // OperationTime: time.Now().Unix(),
  1774. // OrgId: orgID,
  1775. // Creater: creator,
  1776. // Ctime: time.Now().Unix(),
  1777. // Status: 1,
  1778. // WarehouseOutTime: record_time,
  1779. // Dealer: 0,
  1780. // Manufacturer: 0,
  1781. // Type: 1,
  1782. // IsSys: 1,
  1783. // StorehouseId: houseConfig.StorehouseOutInfo,
  1784. // IsCheck: 1,
  1785. // }
  1786. // err := AddSigleWarehouseOut(&warehouseOut)
  1787. // if err != nil {
  1788. // utils.TraceLog("创建出库单失败 err = %v", err)
  1789. // return err
  1790. // } else {
  1791. // out = warehouseOut
  1792. // }
  1793. // }
  1794. //
  1795. // for _, item := range goods {
  1796. // var newCount int64 = 0
  1797. // for _, it := range goodOne {
  1798. // if item.GoodTypeId == it.GoodTypeId && item.GoodId == it.GoodId {
  1799. // newCount = it.Count
  1800. // }
  1801. // }
  1802. //
  1803. // var cha_count int64
  1804. // var cha_count_two int64
  1805. // flowGood, _ := GetStockFlowIsBatchNumberTwo(patient_id, record_time, item.GoodId)
  1806. // var out_count int64
  1807. // for _, item := range flowGood {
  1808. // out_count += item.Count
  1809. // }
  1810. // cha_count = item.Count - out_count
  1811. // //fmt.Println("item.Count", item.Count)
  1812. // //fmt.Println("item.Count", out_count)
  1813. // //fmt.Println("cha_count2323223232323223", cha_count)
  1814. // cha_count_two = out_count - item.Count
  1815. //
  1816. // prepare := models.DialysisBeforePrepare{
  1817. // GoodTypeId: item.GoodTypeId,
  1818. // GoodId: item.GoodId,
  1819. // Count: cha_count,
  1820. // ProjectId: item.ProjectId,
  1821. // StorehouseId: houseConfig.StorehouseOutInfo,
  1822. // }
  1823. //
  1824. // ConsumablesGoodDelivery(orgID, patient_id, record_time, &prepare, &out, newCount)
  1825. // //数量增加
  1826. // if cha_count > 0 {
  1827. // //出库数量累加
  1828. // ModifyGoodSumCount(houseConfig.StorehouseOutInfo, cha_count, orgID, item.GoodId)
  1829. // }
  1830. // //数量减少
  1831. // if cha_count_two > 0 {
  1832. // ModifyAddGoodSumCount(houseConfig.StorehouseOutInfo, cha_count_two, orgID, item.GoodId)
  1833. // //退库数量增加
  1834. // UpdateSumAddCancelCount(orgID, item.GoodId, houseConfig.StorehouseOutInfo, cha_count_two)
  1835. //
  1836. // }
  1837. // //更新剩余库存
  1838. // goodListSix, _ := GetSumGoodList(orgID, houseConfig.StorehouseOutInfo, item.GoodId)
  1839. // var flush_count int64
  1840. // for _, it := range goodListSix {
  1841. // flush_count += it.StockCount
  1842. // }
  1843. // UpdateSumGood(orgID, houseConfig.StorehouseOutInfo, item.GoodId, flush_count)
  1844. //
  1845. // }
  1846. //
  1847. // }
  1848. // fmt.Println("goods_yc2323323223323223232323232332wode", goods_yc)
  1849. // if len(goods_yc) > 0 {
  1850. // for _, good_yc := range goods_yc {
  1851. // out, _ := FindStockOutByIsSys(orgID, 1, record_time)
  1852. // ConsumablesDeliveryDelete(orgID, patient_id, record_time, good_yc, &out)
  1853. //
  1854. // }
  1855. // }
  1856. //
  1857. // return nil
  1858. //}
  1859. //
  1860. ////耗材出库
  1861. //func ConsumablesDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, count int64) (err error) {
  1862. //
  1863. // //开事务
  1864. // var deliver_number int64 = 0
  1865. // var stock_number int64 = 0
  1866. // var maxNumber int64 = 0
  1867. //
  1868. // deliver_number = goods.Count
  1869. //
  1870. // // 根据先进先出原则,查询最先入库的批次,进行出库
  1871. // // 如果没有对应的库存,则报错
  1872. //
  1873. // warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  1874. // if err != nil {
  1875. // return errors.New("库存数量不足")
  1876. // }
  1877. //
  1878. // stock_number = warehouse.StockCount
  1879. //
  1880. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1881. // if stock_number >= deliver_number {
  1882. //
  1883. // maxNumber = goods.Count
  1884. // //出库
  1885. // warehouse.StockCount = warehouse.StockCount - maxNumber
  1886. // warehouse.Mtime = time.Now().Unix()
  1887. // if warehouse.StockCount < 0 {
  1888. // return errors.New("库存数量不足")
  1889. // }
  1890. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  1891. //
  1892. // if errThree != nil {
  1893. // return errThree
  1894. // }
  1895. // //查询剩余库存
  1896. // goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID)
  1897. // var sum_count int64
  1898. // for _, item := range goodList {
  1899. // sum_count += item.StockCount
  1900. // }
  1901. //
  1902. // warehouseOutInfo := &models.WarehouseOutInfo{
  1903. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1904. // WarehouseOutId: warehouseOut.ID,
  1905. // WarehouseInfotId: warehouse.ID,
  1906. // Status: 1,
  1907. // Ctime: time.Now().Unix(),
  1908. // Remark: warehouse.Remark,
  1909. // OrgId: orgID,
  1910. // Type: 1,
  1911. // Manufacturer: warehouse.Manufacturer,
  1912. // Dealer: warehouse.Dealer,
  1913. // IsSys: 1,
  1914. // SysRecordTime: record_time,
  1915. // GoodTypeId: goods.GoodTypeId,
  1916. // GoodId: goods.GoodId,
  1917. // PatientId: patient_id,
  1918. // Number: warehouse.Number,
  1919. // LicenseNumber: warehouse.LicenseNumber,
  1920. // Price: warehouse.PackingPrice,
  1921. // ExpiryDate: warehouse.ExpiryDate,
  1922. // ProductDate: warehouse.ProductDate,
  1923. // ProjectId: goods.ProjectId,
  1924. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1925. // StorehouseId: goods.StorehouseId,
  1926. // IsCheck: 1,
  1927. // }
  1928. // warehouseOutInfo.Count = count
  1929. //
  1930. // _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
  1931. // if errcodes == gorm.ErrRecordNotFound {
  1932. // errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  1933. // if errOne != nil {
  1934. // return errOne
  1935. // }
  1936. // } else if errcodes == nil {
  1937. // UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId)
  1938. // }
  1939. //
  1940. // details := models.BloodAutomaticReduceDetail{
  1941. // WarehouseOutId: warehouseOutInfo.ID,
  1942. // WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  1943. // PatientId: patient_id,
  1944. // Ctime: time.Now().Unix(),
  1945. // Mtime: time.Now().Unix(),
  1946. // Status: 1,
  1947. // RecordTime: record_time,
  1948. // OrgId: orgID,
  1949. // GoodId: goods.GoodId,
  1950. // GoodTypeId: goods.GoodTypeId,
  1951. // Count: count,
  1952. // ProjectId: goods.ProjectId,
  1953. // StorehouseId: goods.StorehouseId,
  1954. // }
  1955. // //查询当天耗材是否已经存在数据
  1956. // _, errcode := GetAutoMaticReduceDetail(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  1957. // if errcode == gorm.ErrRecordNotFound {
  1958. // errTwo := CreateAutoReduceRecord(&details)
  1959. // if errTwo != nil {
  1960. // return errTwo
  1961. // }
  1962. // //插入库存流水表
  1963. // flow := models.VmStockFlow{
  1964. // WarehouseOutId: warehouseOut.ID,
  1965. // WarehousingId: warehouse.ID,
  1966. // GoodId: goods.GoodId,
  1967. // Number: warehouse.Number,
  1968. // ProductDate: warehouse.ProductDate,
  1969. // ExpireDate: warehouse.ExpiryDate,
  1970. // Count: deliver_number,
  1971. // Price: warehouse.PackingPrice,
  1972. // Status: 1,
  1973. // Ctime: time.Now().Unix(),
  1974. // UserOrgId: orgID,
  1975. // Manufacturer: warehouse.Manufacturer,
  1976. // Dealer: warehouse.Dealer,
  1977. // LicenseNumber: warehouse.LicenseNumber,
  1978. // IsEdit: 2,
  1979. // Creator: warehouseOut.Creater,
  1980. // SystemTime: record_time,
  1981. // ConsumableType: 3,
  1982. // WarehouseOutDetailId: warehouseOutInfo.ID,
  1983. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1984. // IsSys: 1,
  1985. // PatientId: patient_id,
  1986. // ProjectId: goods.ProjectId,
  1987. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1988. // StorehouseId: goods.StorehouseId,
  1989. // OverCount: sum_count,
  1990. // }
  1991. //
  1992. // //查询流水是否存在
  1993. // exsit, errflow := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  1994. //
  1995. // if errflow == gorm.ErrRecordNotFound {
  1996. //
  1997. // //创建数据
  1998. // errThre := CreateStockFlowOne(flow)
  1999. // if errThre != nil {
  2000. // return errThre
  2001. // }
  2002. // } else if errflow == nil {
  2003. // flow := models.VmStockFlow{
  2004. // ID: exsit.ID,
  2005. // WarehouseOutId: warehouseOut.ID,
  2006. // WarehousingId: warehouse.ID,
  2007. // GoodId: goods.GoodId,
  2008. // Number: warehouse.Number,
  2009. // ProductDate: warehouse.ProductDate,
  2010. // ExpireDate: warehouse.ExpiryDate,
  2011. // Count: exsit.Count + goods.Count,
  2012. // Price: warehouse.PackingPrice,
  2013. // Status: 1,
  2014. // Ctime: time.Now().Unix(),
  2015. // UserOrgId: orgID,
  2016. // Manufacturer: warehouse.Manufacturer,
  2017. // Dealer: warehouse.Dealer,
  2018. // LicenseNumber: warehouse.LicenseNumber,
  2019. // IsEdit: 2,
  2020. // Creator: warehouseOut.Creater,
  2021. // SystemTime: record_time,
  2022. // ConsumableType: 3,
  2023. // WarehouseOutDetailId: warehouseOutInfo.ID,
  2024. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2025. // IsSys: 1,
  2026. // PatientId: patient_id,
  2027. // ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  2028. // ProjectId: goods.ProjectId,
  2029. // StorehouseId: goods.StorehouseId,
  2030. // OverCount: sum_count,
  2031. // }
  2032. //
  2033. // errFour := UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  2034. // if errFour != nil {
  2035. // return errFour
  2036. // }
  2037. // }
  2038. //
  2039. // } else if errcode == nil {
  2040. // flow := models.VmStockFlow{
  2041. // WarehouseOutId: warehouseOut.ID,
  2042. // WarehousingId: warehouse.ID,
  2043. // GoodId: goods.GoodId,
  2044. // Number: warehouse.Number,
  2045. // ProductDate: warehouse.ProductDate,
  2046. // ExpireDate: warehouse.ExpiryDate,
  2047. // Count: deliver_number,
  2048. // Price: warehouse.PackingPrice,
  2049. // Status: 1,
  2050. // Ctime: time.Now().Unix(),
  2051. // UserOrgId: orgID,
  2052. // Manufacturer: warehouse.Manufacturer,
  2053. // Dealer: warehouse.Dealer,
  2054. // LicenseNumber: warehouse.LicenseNumber,
  2055. // IsEdit: 2,
  2056. // Creator: warehouseOut.Creater,
  2057. // SystemTime: record_time,
  2058. // ConsumableType: 3,
  2059. // WarehouseOutDetailId: warehouseOutInfo.ID,
  2060. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2061. // IsSys: 1,
  2062. // PatientId: patient_id,
  2063. // ProjectId: goods.ProjectId,
  2064. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2065. // StorehouseId: goods.StorehouseId,
  2066. // OverCount: sum_count,
  2067. // }
  2068. // //查询流水是否存在
  2069. // exsit, errflow := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  2070. // if errflow == gorm.ErrRecordNotFound {
  2071. //
  2072. // CreateStockFlowOne(flow)
  2073. // DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  2074. // CreateAutoReduceRecord(&details)
  2075. //
  2076. // } else if errflow == nil {
  2077. // flow := models.VmStockFlow{
  2078. // ID: exsit.ID,
  2079. // WarehouseOutId: warehouseOut.ID,
  2080. // WarehousingId: warehouse.ID,
  2081. // GoodId: goods.GoodId,
  2082. // Number: warehouse.Number,
  2083. // ProductDate: warehouse.ProductDate,
  2084. // ExpireDate: warehouse.ExpiryDate,
  2085. // Count: exsit.Count + goods.Count,
  2086. // Price: warehouse.PackingPrice,
  2087. // Status: 1,
  2088. // Ctime: time.Now().Unix(),
  2089. // UserOrgId: orgID,
  2090. // Manufacturer: warehouse.Manufacturer,
  2091. // Dealer: warehouse.Dealer,
  2092. // LicenseNumber: warehouse.LicenseNumber,
  2093. // IsEdit: 2,
  2094. // Creator: warehouseOut.Creater,
  2095. // SystemTime: record_time,
  2096. // ConsumableType: 3,
  2097. // WarehouseOutDetailId: warehouseOutInfo.ID,
  2098. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2099. // IsSys: 1,
  2100. // PatientId: patient_id,
  2101. // ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  2102. // ProjectId: goods.ProjectId,
  2103. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2104. // StorehouseId: goods.StorehouseId,
  2105. // OverCount: sum_count,
  2106. // }
  2107. // UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  2108. // DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  2109. // CreateAutoReduceRecord(&details)
  2110. // }
  2111. //
  2112. // }
  2113. //
  2114. // return nil
  2115. // } else {
  2116. //
  2117. // // 当刚批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2118. // warehouse.StockCount = 0
  2119. // warehouse.Mtime = time.Now().Unix()
  2120. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  2121. // if errThree != nil {
  2122. // return errThree
  2123. // }
  2124. // goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID)
  2125. // var sum_count int64
  2126. // for _, item := range goodList {
  2127. // sum_count += item.StockCount
  2128. // }
  2129. // warehouseOutInfo := &models.WarehouseOutInfo{
  2130. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2131. // WarehouseOutId: warehouseOut.ID,
  2132. // WarehouseInfotId: warehouse.ID,
  2133. // Status: 1,
  2134. // Ctime: time.Now().Unix(),
  2135. // Remark: warehouse.Remark,
  2136. // OrgId: orgID,
  2137. // Type: 1,
  2138. // Manufacturer: warehouse.Manufacturer,
  2139. // Dealer: warehouse.Dealer,
  2140. // IsSys: 1,
  2141. // SysRecordTime: record_time,
  2142. // GoodTypeId: goods.GoodTypeId,
  2143. // GoodId: goods.GoodId,
  2144. // PatientId: patient_id,
  2145. // Number: warehouse.Number,
  2146. // LicenseNumber: warehouse.LicenseNumber,
  2147. // Price: warehouse.PackingPrice,
  2148. // ExpiryDate: warehouse.ExpiryDate,
  2149. // ProductDate: warehouse.ProductDate,
  2150. // ProjectId: goods.ProjectId,
  2151. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2152. // StorehouseId: goods.StorehouseId,
  2153. // IsCheck: 1,
  2154. // }
  2155. // warehouseOutInfo.Count = stock_number
  2156. //
  2157. // _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, goods.PatientId, record_time, goods.ProjectId)
  2158. //
  2159. // if errcodes == gorm.ErrRecordNotFound {
  2160. // errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  2161. //
  2162. // if errOne != nil {
  2163. // return errOne
  2164. // }
  2165. // //插入库存流水表
  2166. // flow := models.VmStockFlow{
  2167. // WarehouseOutId: warehouseOut.ID,
  2168. // WarehousingId: warehouse.ID,
  2169. // GoodId: goods.GoodId,
  2170. // Number: warehouse.Number,
  2171. // ProductDate: warehouse.ProductDate,
  2172. // ExpireDate: warehouse.ExpiryDate,
  2173. // Count: stock_number,
  2174. // Price: warehouse.PackingPrice,
  2175. // Status: 1,
  2176. // Ctime: time.Now().Unix(),
  2177. // UserOrgId: orgID,
  2178. // Manufacturer: warehouse.Manufacturer,
  2179. // Dealer: warehouse.Dealer,
  2180. // LicenseNumber: warehouse.LicenseNumber,
  2181. // IsEdit: 2,
  2182. // Creator: warehouseOut.Creater,
  2183. // SystemTime: record_time,
  2184. // ConsumableType: 3,
  2185. // WarehouseOutDetailId: warehouseOutInfo.ID,
  2186. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2187. // IsSys: 1,
  2188. // PatientId: patient_id,
  2189. // ProjectId: goods.ProjectId,
  2190. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2191. // StorehouseId: goods.StorehouseId,
  2192. // OverCount: sum_count,
  2193. // }
  2194. // //查询流水是否存在
  2195. // exsit, errflows := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  2196. //
  2197. // if errflows == gorm.ErrRecordNotFound {
  2198. //
  2199. // CreateStockFlowOne(flow)
  2200. // } else if errflows == nil {
  2201. // flow := models.VmStockFlow{
  2202. // ID: exsit.ID,
  2203. // WarehouseOutId: warehouseOut.ID,
  2204. // WarehousingId: warehouse.ID,
  2205. // GoodId: goods.GoodId,
  2206. // Number: warehouse.Number,
  2207. // ProductDate: warehouse.ProductDate,
  2208. // ExpireDate: warehouse.ExpiryDate,
  2209. // Count: exsit.Count + goods.Count,
  2210. // Price: warehouse.PackingPrice,
  2211. // Status: 1,
  2212. // Ctime: time.Now().Unix(),
  2213. // UserOrgId: orgID,
  2214. // Manufacturer: warehouse.Manufacturer,
  2215. // Dealer: warehouse.Dealer,
  2216. // LicenseNumber: warehouse.LicenseNumber,
  2217. // IsEdit: 2,
  2218. // Creator: warehouseOut.Creater,
  2219. // SystemTime: record_time,
  2220. // ConsumableType: 3,
  2221. // WarehouseOutDetailId: warehouseOutInfo.ID,
  2222. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2223. // IsSys: 1,
  2224. // PatientId: patient_id,
  2225. // ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  2226. // ProjectId: goods.ProjectId,
  2227. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2228. // StorehouseId: goods.StorehouseId,
  2229. // OverCount: sum_count,
  2230. // }
  2231. // errOne := UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  2232. // if errOne == nil {
  2233. //
  2234. // }
  2235. // }
  2236. //
  2237. // } else if errcodes == nil {
  2238. //
  2239. // goods.Count = deliver_number - stock_number
  2240. // //更新数量为 该批次剩余数量 + 还有未出的数量
  2241. // warehouseOutInfo.Count = stock_number
  2242. // UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, goods.PatientId, record_time, goods.ProjectId)
  2243. // //插入库存流水表
  2244. // flow := models.VmStockFlow{
  2245. // WarehouseOutId: warehouseOut.ID,
  2246. // WarehousingId: warehouse.ID,
  2247. // GoodId: goods.GoodId,
  2248. // Number: warehouse.Number,
  2249. // ProductDate: warehouse.ProductDate,
  2250. // ExpireDate: warehouse.ExpiryDate,
  2251. // Count: stock_number,
  2252. // Price: warehouse.PackingPrice,
  2253. // Status: 1,
  2254. // Ctime: time.Now().Unix(),
  2255. // UserOrgId: orgID,
  2256. // Manufacturer: warehouse.Manufacturer,
  2257. // Dealer: warehouse.Dealer,
  2258. // LicenseNumber: warehouse.LicenseNumber,
  2259. // IsEdit: 2,
  2260. // Creator: warehouseOut.Creater,
  2261. // SystemTime: record_time,
  2262. // ConsumableType: 3,
  2263. // WarehouseOutDetailId: warehouseOutInfo.ID,
  2264. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2265. // IsSys: 1,
  2266. // PatientId: patient_id,
  2267. // ProjectId: goods.ProjectId,
  2268. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2269. // StorehouseId: goods.StorehouseId,
  2270. // OverCount: sum_count,
  2271. // }
  2272. // //查询流水是否存在
  2273. // exsit, errflows := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  2274. // if errflows == gorm.ErrRecordNotFound {
  2275. //
  2276. // CreateStockFlowOne(flow)
  2277. // } else if errflows == nil {
  2278. //
  2279. // flow := models.VmStockFlow{
  2280. // ID: exsit.ID,
  2281. // WarehouseOutId: warehouseOut.ID,
  2282. // WarehousingId: warehouse.ID,
  2283. // GoodId: goods.GoodId,
  2284. // Number: warehouse.Number,
  2285. // ProductDate: warehouse.ProductDate,
  2286. // ExpireDate: warehouse.ExpiryDate,
  2287. // Count: exsit.Count + goods.Count,
  2288. // Price: warehouse.PackingPrice,
  2289. // Status: 1,
  2290. // Ctime: time.Now().Unix(),
  2291. // UserOrgId: orgID,
  2292. // Manufacturer: warehouse.Manufacturer,
  2293. // Dealer: warehouse.Dealer,
  2294. // LicenseNumber: warehouse.LicenseNumber,
  2295. // IsEdit: 2,
  2296. // Creator: warehouseOut.Creater,
  2297. // SystemTime: record_time,
  2298. // ConsumableType: 3,
  2299. // WarehouseOutDetailId: warehouseOutInfo.ID,
  2300. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2301. // IsSys: 1,
  2302. // PatientId: patient_id,
  2303. // ProjectId: goods.ProjectId,
  2304. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2305. // StorehouseId: goods.StorehouseId,
  2306. // OverCount: sum_count,
  2307. // }
  2308. // UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  2309. // }
  2310. // }
  2311. //
  2312. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2313. // goods.Count = deliver_number - stock_number
  2314. //
  2315. // ConsumablesDelivery(orgID, patient_id, record_time, goods, warehouseOut, count)
  2316. //
  2317. // }
  2318. // return nil
  2319. //}
  2320. //
  2321. ////耗材出库删除
  2322. //func ConsumablesDeliveryDelete(orgID int64, patient_id int64, record_time int64, good_yc *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut) (err error) {
  2323. //
  2324. // // 先根据相关信息查询当天该耗材的出库信息
  2325. // warehouseOutInfos, err := FindStockOutInfoByStock(orgID, good_yc.GoodTypeId, good_yc.GoodId, record_time, patient_id)
  2326. // if err != nil {
  2327. // return err
  2328. // }
  2329. //
  2330. // var delete_count int64 = 0
  2331. //
  2332. // for _, ware := range warehouseOutInfos {
  2333. // // 删除出库完成后,要增加对应批次的库存数量
  2334. // errThree := UpDateWarehouseInfoByStockDelete(ware.WarehouseInfotId, delete_count, patient_id, record_time, good_yc.GoodId)
  2335. // if errThree != nil {
  2336. // return errThree
  2337. // }
  2338. // //查询剩余库存
  2339. // goodList, _ := GetAllGoodSumCount(good_yc.GoodId, orgID)
  2340. // var sum_count int64
  2341. // for _, item := range goodList {
  2342. // sum_count += item.StockCount
  2343. // }
  2344. // // 判断当前出库的数据和删除出库数量
  2345. // if good_yc.Count <= ware.Count {
  2346. // delete_count = good_yc.Count
  2347. // } else {
  2348. // delete_count = ware.Count
  2349. // }
  2350. // houseConfig, _ := GetAllStoreHouseConfig(orgID)
  2351. // // 在出库记录表里记录退库详情
  2352. // warehouseOutInfo := &models.WarehouseOutInfo{
  2353. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2354. // WarehouseOutId: warehouseOut.ID,
  2355. // //WarehouseInfoId: warehouse.ID,
  2356. // Status: 1,
  2357. // Ctime: time.Now().Unix(),
  2358. // Remark: "",
  2359. // OrgId: orgID,
  2360. // Type: 1,
  2361. // Manufacturer: 0,
  2362. // Dealer: 0,
  2363. // IsSys: 2,
  2364. // SysRecordTime: record_time,
  2365. // GoodTypeId: good_yc.GoodTypeId,
  2366. // GoodId: good_yc.GoodId,
  2367. // PatientId: patient_id,
  2368. // StorehouseId: houseConfig.StorehouseOutInfo,
  2369. // OverCount: sum_count,
  2370. // }
  2371. // warehouseOutInfo.Count = ware.Count - delete_count
  2372. // stockInInfo, _ := FindLastStockInInfoRecord(good_yc.GoodId, orgID)
  2373. // warehouseOutInfo.Price = stockInInfo.Price
  2374. // errOne := UpdateAutoMaticReduceDetail(good_yc.GoodId, good_yc.GoodTypeId, record_time, patient_id, orgID, warehouseOutInfo)
  2375. // if errOne != nil {
  2376. // return errOne
  2377. // } else {
  2378. // details := &models.AutomaticReduceDetail{
  2379. // WarehouseOutId: warehouseOutInfo.ID,
  2380. // WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  2381. // PatientId: patient_id,
  2382. // Ctime: time.Now().Unix(),
  2383. // Mtime: time.Now().Unix(),
  2384. // Status: 1,
  2385. // RecordTime: record_time,
  2386. // OrgId: orgID,
  2387. // GoodId: good_yc.GoodId,
  2388. // GoodTypeId: good_yc.GoodTypeId,
  2389. // Count: warehouseOutInfo.Count,
  2390. // Type: 2,
  2391. // StorehouseId: houseConfig.StorehouseOutInfo,
  2392. // }
  2393. // //查询是否当天已经存在数据
  2394. // _, errcode := GetAutoMaticReduceDetail(orgID, patient_id, record_time, good_yc.GoodId, good_yc.GoodTypeId)
  2395. // if errcode == gorm.ErrRecordNotFound {
  2396. // errTwo := AddSigleAutoReduceRecordInfo(details)
  2397. // if errTwo != nil {
  2398. // return errTwo
  2399. // }
  2400. // } else if errcode == nil {
  2401. // DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, good_yc.GoodId, good_yc.GoodTypeId)
  2402. //
  2403. // AddSigleAutoReduceRecordInfo(details)
  2404. // }
  2405. // }
  2406. //
  2407. // //扣减数量相加
  2408. // ModifyAddGoodSumCount(houseConfig.StorehouseOutInfo, delete_count, good_yc.UserOrgId, good_yc.GoodId)
  2409. //
  2410. // //退库数量相加
  2411. // UpdateSumAddCancelCount(good_yc.UserOrgId, good_yc.GoodId, houseConfig.StorehouseOutInfo, delete_count)
  2412. //
  2413. // // 增加了对应的库存后,看看还有多少需要退库的
  2414. // good_yc.Count = good_yc.Count - delete_count
  2415. // if good_yc.Count == 0 {
  2416. // return nil
  2417. // }
  2418. // }
  2419. //
  2420. // if good_yc.Count == 0 {
  2421. // return nil
  2422. // } else {
  2423. // return errors.New("退库和出库数据不匹配")
  2424. // }
  2425. //}
  2426. //
  2427. //func GetAutoReduceRecordInfoByPatientId(orgid int64, patient_id int64, recordTime int64) (autoReduce []*models.AutomaticReduceDetail, err error) {
  2428. //
  2429. // err = XTReadDB().Model(&autoReduce).Where("org_id = ? and patient_id = ? and record_time = ? and status = 1", orgid, patient_id, recordTime).Find(&autoReduce).Error
  2430. // return autoReduce, err
  2431. //}
  2432. //
  2433. //func DeleteDialysisBefor(orgid int64, patient_id int64, record_date int64, goodid int64, goodtypeid int64) error {
  2434. // prepare := models.DialysisBeforePrepare{}
  2435. // 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
  2436. // return err
  2437. //}
  2438. //
  2439. //func DeleteDialysisBeforThree(orgid int64, patient_id int64, record_date int64) error {
  2440. // prepare := models.DialysisBeforePrepare{}
  2441. // 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
  2442. // return err
  2443. //}
  2444. //
  2445. //func GetWarehouseById(id int64) (models.WarehouseOut, error) {
  2446. // out := models.WarehouseOut{}
  2447. // err := XTReadDB().Where("id=? and status = 1", id).Find(&out).Error
  2448. // return out, err
  2449. //}
  2450. //
  2451. ////耗材出库
  2452. //func ConsumablesDeliveryOne(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64) (err error) {
  2453. //
  2454. // ////开事务
  2455. // //tx := XTWriteDB().Begin()
  2456. //
  2457. // var deliver_number int64 = 0
  2458. // var stock_number int64 = 0
  2459. // var maxNumber int64 = 0
  2460. //
  2461. // deliver_number = goods.Count
  2462. //
  2463. // // 根据先进先出原则,查询最先入库的批次,进行出库
  2464. //
  2465. // // 如果没有对应的库存,则报错
  2466. // warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  2467. // if err != nil {
  2468. //
  2469. // return err
  2470. // }
  2471. //
  2472. // stock_number = warehouse.StockCount
  2473. //
  2474. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  2475. // if stock_number >= deliver_number {
  2476. //
  2477. // warehouseOutInfo := &models.WarehouseOutInfo{
  2478. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2479. // WarehouseOutId: warehouseOut.ID,
  2480. // WarehouseInfotId: warehouse.ID,
  2481. // Status: 1,
  2482. // Ctime: time.Now().Unix(),
  2483. // Remark: goods.Remark,
  2484. // OrgId: orgID,
  2485. // Type: 1,
  2486. // Manufacturer: goods.Manufacturer,
  2487. // Dealer: goods.Dealer,
  2488. // IsSys: 0,
  2489. // SysRecordTime: record_time,
  2490. // GoodTypeId: goods.GoodTypeId,
  2491. // GoodId: goods.GoodId,
  2492. // ExpiryDate: goods.ExpiryDate,
  2493. // ProductDate: goods.ProductDate,
  2494. // Number: warehouse.Number,
  2495. // Price: goods.Price,
  2496. // LicenseNumber: goods.LicenseNumber,
  2497. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2498. // StorehouseId: goods.StorehouseId,
  2499. // AdminUserId: goods.AdminUserId,
  2500. // BuyPrice: warehouse.Price,
  2501. // StockCount: goods.StockCount,
  2502. // }
  2503. // warehouseOutInfo.Count = goods.Count
  2504. // //添加出库单详情
  2505. // errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  2506. // if errOne != nil {
  2507. // return errOne
  2508. // }
  2509. //
  2510. // stockFlow := models.VmStockFlow{
  2511. // WarehousingId: warehouse.ID,
  2512. // GoodId: goods.GoodId,
  2513. // Number: warehouse.Number,
  2514. // LicenseNumber: goods.LicenseNumber,
  2515. // Count: goods.Count,
  2516. // UserOrgId: goods.OrgId,
  2517. // PatientId: goods.PatientId,
  2518. // SystemTime: record_time,
  2519. // ConsumableType: 2,
  2520. // IsSys: goods.IsSys,
  2521. // WarehousingOrder: "",
  2522. // WarehouseOutId: warehouseOut.ID,
  2523. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2524. // IsEdit: 1,
  2525. // CancelStockId: 0,
  2526. // CancelOrderNumber: "",
  2527. // Manufacturer: goods.Manufacturer,
  2528. // Dealer: goods.Dealer,
  2529. // Creator: creator,
  2530. // UpdateCreator: 0,
  2531. // Status: 1,
  2532. // Ctime: time.Now().Unix(),
  2533. // Mtime: 0,
  2534. // Price: warehouse.PackingPrice,
  2535. // WarehousingDetailId: 0,
  2536. // WarehouseOutDetailId: warehouseOutInfo.ID,
  2537. // CancelOutDetailId: 0,
  2538. // ProductDate: goods.ProductDate,
  2539. // ExpireDate: goods.ExpiryDate,
  2540. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2541. // StorehouseId: warehouse.StorehouseId,
  2542. // AdminUserId: goods.AdminUserId,
  2543. // BuyPrice: warehouse.Price,
  2544. // StockCount: goods.StockCount,
  2545. // }
  2546. //
  2547. // //创建出库流水
  2548. // CreateStockFlowOne(stockFlow)
  2549. //
  2550. // maxNumber = goods.Count
  2551. //
  2552. // if warehouse.StockCount < maxNumber {
  2553. // return errors.New("库存数量不足")
  2554. // }
  2555. //
  2556. // warehouse.StockCount = warehouse.StockCount - maxNumber
  2557. // warehouse.Mtime = time.Now().Unix()
  2558. //
  2559. // //扣减库存
  2560. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  2561. // fmt.Println(errThree)
  2562. // //if errThree != nil {
  2563. // // utils.ErrorLog("事务失败,原因为: %v", errThree.Error())
  2564. // // tx.Rollback()
  2565. // //} else {
  2566. // // tx.Commit()
  2567. // //}
  2568. //
  2569. // //查询该机构默认仓库
  2570. // storeConfig, _ := GetAllStoreHouseConfig(goods.OrgId)
  2571. // //查询剩余库存
  2572. // goodList, _ := GetGoodSumCountByStoreId(storeConfig.StorehouseOutInfo, goods.GoodId, goods.OrgId)
  2573. // var sum_count int64
  2574. // var sum_in_count int64
  2575. // for _, item := range goodList {
  2576. // sum_count += item.StockCount
  2577. // sum_in_count += item.WarehousingCount
  2578. // }
  2579. // UpdateGoodByGoodId(goods.GoodId, sum_count, sum_in_count, goods.OrgId)
  2580. // if errThree != nil {
  2581. // return errThree
  2582. // }
  2583. //
  2584. // } else {
  2585. //
  2586. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2587. // warehouseOutInfo := &models.WarehouseOutInfo{
  2588. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2589. // WarehouseOutId: warehouseOut.ID,
  2590. // WarehouseInfotId: warehouse.ID,
  2591. // Status: 1,
  2592. // Ctime: time.Now().Unix(),
  2593. // Remark: goods.Remark,
  2594. // OrgId: orgID,
  2595. // Type: 1,
  2596. // Manufacturer: goods.Manufacturer,
  2597. // Dealer: goods.Dealer,
  2598. // IsSys: 0,
  2599. // SysRecordTime: record_time,
  2600. // GoodTypeId: goods.GoodTypeId,
  2601. // GoodId: goods.GoodId,
  2602. // ExpiryDate: goods.ExpiryDate,
  2603. // ProductDate: goods.ProductDate,
  2604. // Number: warehouse.Number,
  2605. // Price: warehouse.PackingPrice,
  2606. // LicenseNumber: goods.LicenseNumber,
  2607. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2608. // StorehouseId: warehouse.StorehouseId,
  2609. // AdminUserId: goods.AdminUserId,
  2610. // BuyPrice: warehouse.Price,
  2611. // StockCount: goods.StockCount,
  2612. // IsCheck: 1,
  2613. // }
  2614. // warehouseOutInfo.Count = stock_number
  2615. //
  2616. // //查询是否已经生成出库单
  2617. // info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  2618. // if errcodes == gorm.ErrRecordNotFound {
  2619. // //创建出库单
  2620. // errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  2621. // if errOne != nil {
  2622. // return errOne
  2623. // }
  2624. // } else {
  2625. // warehouseOutInfo.Count = info.Count + stock_number
  2626. //
  2627. // UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  2628. // }
  2629. //
  2630. // stockFlow := models.VmStockFlow{
  2631. // WarehousingId: warehouse.ID,
  2632. // GoodId: goods.GoodId,
  2633. // Number: warehouse.Number,
  2634. // LicenseNumber: goods.LicenseNumber,
  2635. // Count: stock_number,
  2636. // UserOrgId: goods.OrgId,
  2637. // PatientId: 0,
  2638. // SystemTime: record_time,
  2639. // ConsumableType: 2,
  2640. // IsSys: 0,
  2641. // WarehousingOrder: "",
  2642. // WarehouseOutId: warehouseOut.ID,
  2643. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  2644. // IsEdit: 1,
  2645. // CancelStockId: 0,
  2646. // CancelOrderNumber: "",
  2647. // Manufacturer: goods.Manufacturer,
  2648. // Dealer: goods.Dealer,
  2649. // Creator: creator,
  2650. // UpdateCreator: 0,
  2651. // Status: 1,
  2652. // Ctime: time.Now().Unix(),
  2653. // Mtime: 0,
  2654. // Price: warehouse.PackingPrice,
  2655. // WarehousingDetailId: 0,
  2656. // WarehouseOutDetailId: warehouseOutInfo.ID,
  2657. // CancelOutDetailId: 0,
  2658. // ProductDate: goods.ProductDate,
  2659. // ExpireDate: goods.ExpiryDate,
  2660. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2661. // StorehouseId: goods.StorehouseId,
  2662. // AdminUserId: goods.AdminUserId,
  2663. // BuyPrice: warehouse.Price,
  2664. // StockCount: goods.StockCount,
  2665. // }
  2666. //
  2667. // //创建出库明细
  2668. // CreateStockFlowOne(stockFlow)
  2669. // //查询该机构默认仓库
  2670. // storeConfig, _ := GetAllStoreHouseConfig(goods.OrgId)
  2671. // //查询剩余库存
  2672. // goodList, _ := GetGoodSumCountByStoreId(storeConfig.StorehouseOutInfo, goods.GoodId, goods.OrgId)
  2673. // var sum_count int64
  2674. // var sum_in_count int64
  2675. // for _, item := range goodList {
  2676. // sum_count += item.StockCount
  2677. // sum_in_count += item.WarehousingCount
  2678. // }
  2679. // UpdateGoodByGoodId(goods.GoodId, sum_count, sum_in_count, goods.OrgId)
  2680. // // 出库完成后,要将该批次库存清零
  2681. // warehouse.StockCount = 0
  2682. // warehouse.Mtime = time.Now().Unix()
  2683. //
  2684. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  2685. // fmt.Println(errThree)
  2686. // //if errThree != nil {
  2687. // // utils.ErrorLog("事务失败,原因为: %v", errThree.Error())
  2688. // // tx.Rollback()
  2689. // //} else {
  2690. // // tx.Commit()
  2691. // //}
  2692. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2693. // goods.Count = deliver_number - stock_number
  2694. // ConsumablesDeliveryOne(orgID, record_time, goods, warehouseOut, count, creator)
  2695. //
  2696. // }
  2697. // return nil
  2698. //}
  2699. //
  2700. ////退库逻辑
  2701. //func ConsumablesDeliveryTwo(orgID int64, goods *models.WarehouseOutInfo, count int64) (err error) {
  2702. //
  2703. // ////开事务
  2704. // //tx := XTWriteDB().Begin()
  2705. // var deliver_number int64 = 0
  2706. // var stock_number int64 = 0
  2707. // var maxNumber int64 = 0
  2708. //
  2709. // deliver_number = goods.Count
  2710. //
  2711. // // 根据先进先出原则,查询最先入库的批次,进行出库
  2712. // warehouse, err := FindFirstWarehousingInfoByGoodId(goods.GoodId, goods.ID, goods.StorehouseId)
  2713. //
  2714. // if err != nil {
  2715. // return err
  2716. // }
  2717. //
  2718. // stock_number = warehouse.StockCount
  2719. //
  2720. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  2721. // if stock_number >= deliver_number {
  2722. // warehouseOutInfo := &models.WarehouseOutInfo{
  2723. // WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  2724. // WarehouseOutId: goods.ID,
  2725. // WarehouseInfotId: warehouse.ID,
  2726. // Status: 1,
  2727. // Ctime: time.Now().Unix(),
  2728. // Remark: goods.Remark,
  2729. // OrgId: orgID,
  2730. // Type: 1,
  2731. // Manufacturer: goods.Manufacturer,
  2732. // Dealer: goods.Dealer,
  2733. // IsSys: 0,
  2734. // GoodTypeId: goods.GoodTypeId,
  2735. // GoodId: goods.GoodId,
  2736. // ExpiryDate: goods.ExpiryDate,
  2737. // ProductDate: goods.ProductDate,
  2738. // Number: goods.Number,
  2739. // Price: warehouse.PackingPrice,
  2740. // LicenseNumber: goods.LicenseNumber,
  2741. // ConsumableType: 2,
  2742. // }
  2743. // warehouseOutInfo.Count = goods.Count
  2744. //
  2745. // errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  2746. // if errOne != nil {
  2747. // return errOne
  2748. // }
  2749. //
  2750. // maxNumber = goods.Count
  2751. //
  2752. // if warehouse.StockCount < maxNumber { //这段逻辑根本不会执行,因为当库存数量大于或等于出库数量才会进入这个分支
  2753. // return errors.New("库存数量不足")
  2754. // }
  2755. //
  2756. // warehouse.StockCount = warehouse.StockCount - maxNumber
  2757. // warehouse.Mtime = time.Now().Unix()
  2758. //
  2759. // if warehouse.StockCount <= 0 {
  2760. //
  2761. // return errors.New("库存数量不足")
  2762. // }
  2763. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  2764. // fmt.Println(errThree)
  2765. // //if errThree != nil {
  2766. // // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  2767. // // tx.Rollback()
  2768. // //} else {
  2769. // // tx.Commit()
  2770. // //}
  2771. //
  2772. // return
  2773. // } else {
  2774. //
  2775. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2776. // warehouseOutInfo := &models.WarehouseOutInfo{
  2777. // WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  2778. // WarehouseOutId: goods.ID,
  2779. // WarehouseInfotId: warehouse.ID,
  2780. // Status: 1,
  2781. // Ctime: time.Now().Unix(),
  2782. // Remark: goods.Remark,
  2783. // OrgId: orgID,
  2784. // Type: 1,
  2785. // Manufacturer: goods.Manufacturer,
  2786. // Dealer: goods.Dealer,
  2787. // IsSys: 0,
  2788. // GoodTypeId: goods.GoodTypeId,
  2789. // GoodId: goods.GoodId,
  2790. // ExpiryDate: goods.ExpiryDate,
  2791. // ProductDate: goods.ProductDate,
  2792. // Number: goods.Number,
  2793. // Price: warehouse.PackingPrice,
  2794. // LicenseNumber: goods.LicenseNumber,
  2795. // ConsumableType: 2,
  2796. // IsCheck: 1,
  2797. // StorehouseId: goods.StorehouseId,
  2798. // }
  2799. // warehouseOutInfo.Count = stock_number
  2800. //
  2801. // errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  2802. // if errOne != nil {
  2803. // return errOne
  2804. // }
  2805. // // 出库完成后,要将该批次库存清零
  2806. // warehouse.StockCount = 0
  2807. // warehouse.Mtime = time.Now().Unix()
  2808. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  2809. // //if errThree != nil {
  2810. // // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  2811. // // tx.Rollback()
  2812. // //} else {
  2813. // // tx.Commit()
  2814. // //}
  2815. //
  2816. // fmt.Println(errThree)
  2817. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2818. // goods.Count = deliver_number - stock_number
  2819. //
  2820. // ConsumablesDeliveryTwo(orgID, goods, count)
  2821. //
  2822. // }
  2823. // return nil
  2824. //}
  2825. //
  2826. //func DeleteWarehouseInfo(id int64) error {
  2827. // err := XTWriteDB().Model(models.WarehousingInfo{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": 0}).Error
  2828. // return err
  2829. //}
  2830. //
  2831. //// 药品出库 递归方式
  2832. //func NewDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DoctorAdvice) (err error) {
  2833. //
  2834. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  2835. // var deliver_number int64 = 0
  2836. // var stock_number int64 = 0
  2837. //
  2838. // prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  2839. // count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  2840. //
  2841. // var drug_price float64
  2842. //
  2843. // //按最小单位数据计算
  2844. // deliver_number = count
  2845. //
  2846. // if advice.PrescribingNumberUnit == drup.MaxUnit {
  2847. // drug_price = drup.RetailPrice
  2848. // }
  2849. //
  2850. // if advice.PrescribingNumberUnit == drup.MinUnit {
  2851. // drug_price = drup.MinPrice
  2852. // }
  2853. //
  2854. // if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  2855. // drug_price = drup.RetailPrice
  2856. // }
  2857. //
  2858. // // 根据先进先出原则,查询最先入库的批次,进行出库
  2859. // // 如果没有对应的库存,则报错
  2860. // //查询默认仓库
  2861. // storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  2862. // lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2863. // if lastWarehouse.StockMinNumber >= drup.MinNumber {
  2864. // var stockMax int64
  2865. // var stockMin int64
  2866. // stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  2867. // stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  2868. // ChangeMaxNumber(lastWarehouse.ID, stockMax)
  2869. // UpdateMinNumber(lastWarehouse.ID, stockMin)
  2870. // }
  2871. //
  2872. // warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2873. //
  2874. // if err != nil {
  2875. //
  2876. // return err
  2877. // }
  2878. //
  2879. // // 将该批次的剩余库存数量转换为拆零数量
  2880. //
  2881. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  2882. //
  2883. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  2884. // if stock_number >= deliver_number {
  2885. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2886. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2887. // WarehouseOutId: warehouseout.ID,
  2888. // Status: 1,
  2889. // Ctime: time.Now().Unix(),
  2890. // Remark: warehouse.Remark,
  2891. // OrgId: orgID,
  2892. // Type: 1,
  2893. // Manufacturer: warehouse.Manufacturer,
  2894. // Dealer: warehouse.Dealer,
  2895. // IsSys: 1,
  2896. // SysRecordTime: advice.RecordDate,
  2897. // DrugId: advice.DrugId,
  2898. // Number: warehouse.Number,
  2899. // BatchNumber: warehouse.BatchNumber,
  2900. // Price: warehouse.RetailPrice,
  2901. // CountUnit: drup.MinUnit,
  2902. // RetailPrice: drug_price,
  2903. // ProductDate: warehouse.ProductDate,
  2904. // ExpiryDate: warehouse.ExpiryDate,
  2905. // PatientId: advice.PatientId,
  2906. // Count: deliver_number, //出最小单位
  2907. // WarehouseInfoId: warehouse.ID,
  2908. // AdviceId: advice.ID,
  2909. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2910. // StorehouseId: storeConfig.DrugStorehouseOut,
  2911. // IsCheck: 1,
  2912. // }
  2913. //
  2914. // drugflow := models.DrugFlow{
  2915. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2916. // WarehouseOutId: warehouseout.ID,
  2917. // DrugId: advice.DrugId,
  2918. // Number: warehouse.Number,
  2919. // ProductDate: warehouse.ProductDate,
  2920. // ExpireDate: warehouse.ExpiryDate,
  2921. // Count: deliver_number, //按最小单位计算,
  2922. // Price: warehouse.RetailPrice,
  2923. // Status: 1,
  2924. // Ctime: time.Now().Unix(),
  2925. // UserOrgId: orgID,
  2926. // Manufacturer: warehouse.Manufacturer,
  2927. // Dealer: warehouse.Dealer,
  2928. // BatchNumber: warehouse.BatchNumber,
  2929. // MaxUnit: drup.MinUnit,
  2930. // ConsumableType: 3,
  2931. // IsEdit: 1,
  2932. // Creator: advice.ExecutionStaff,
  2933. // IsSys: 1,
  2934. // PatientId: advice.PatientId,
  2935. // WarehousingDetailId: warehouse.ID,
  2936. // AdviceId: advice.ID,
  2937. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2938. // StorehouseId: storeConfig.DrugStorehouseOut,
  2939. // }
  2940. //
  2941. // CreateDrugFlowOne(drugflow)
  2942. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2943. // if errOne != nil {
  2944. // return errOne
  2945. // }
  2946. // //查询是否存在数据
  2947. //
  2948. // details := &models.DrugAutomaticReduceDetail{
  2949. // WarehouseOutId: warehouseout.ID,
  2950. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2951. // PatientId: advice.PatientId,
  2952. // Ctime: time.Now().Unix(),
  2953. // Mtime: time.Now().Unix(),
  2954. // Status: 1,
  2955. // RecordTime: advice.RecordDate,
  2956. // OrgId: orgID,
  2957. // DrugId: advice.DrugId,
  2958. // Count: deliver_number,
  2959. // CountUnit: drup.MinUnit,
  2960. // WarehouseInfoId: warehouse.ID,
  2961. // StorehouseId: storeConfig.DrugStorehouseOut,
  2962. // }
  2963. // errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2964. // if errTwo != nil {
  2965. // return errTwo
  2966. // }
  2967. //
  2968. // // 出库完成后,要减去对应批次的库存数量
  2969. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  2970. // var maxNumber int64 = 0
  2971. // var minNumber int64 = 0
  2972. // //var stock_max_number int64 = 0
  2973. // //stock_max_number = warehouse.StockMaxNumber
  2974. //
  2975. // maxNumber = deliver_number / drup.MinNumber
  2976. // minNumber = deliver_number % drup.MinNumber
  2977. //
  2978. // if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  2979. // minNumber = maxNumber
  2980. // }
  2981. //
  2982. // if drup.MaxUnit != drup.MinUnit {
  2983. // if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  2984. // return errors.New("库存数量不足")
  2985. // }
  2986. // }
  2987. //
  2988. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  2989. //
  2990. // if warehouse.StockMaxNumber < 0 {
  2991. // warehouse.StockMaxNumber = 0
  2992. // }
  2993. // if warehouse.StockMinNumber < 0 {
  2994. // warehouse.StockMinNumber = 0
  2995. // }
  2996. //
  2997. // warehouse.Mtime = time.Now().Unix()
  2998. //
  2999. // if warehouse.StockMinNumber < minNumber {
  3000. //
  3001. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  3002. // warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  3003. // } else {
  3004. //
  3005. // if minNumber > 0 {
  3006. // warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  3007. // }
  3008. //
  3009. // if minNumber == 0 && maxNumber != 1 {
  3010. // if warehouse.StockMinNumber > 0 {
  3011. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  3012. // }
  3013. //
  3014. // }
  3015. // }
  3016. //
  3017. // if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  3018. // if (warehouse.StockMinNumber - deliver_number) > 0 {
  3019. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  3020. //
  3021. // //if warehouse.StockMinNumber == 0 {
  3022. // // warehouse.StockMaxNumber = stock_max_number
  3023. // //}
  3024. // }
  3025. //
  3026. // }
  3027. //
  3028. // if drup.MaxUnit != drup.MinUnit {
  3029. // if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  3030. // return errors.New("库存数量不足")
  3031. // }
  3032. // }
  3033. // if warehouse.StockMinNumber <= 0 {
  3034. // warehouse.StockMinNumber = 0
  3035. // }
  3036. //
  3037. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  3038. //
  3039. // if errThree != nil {
  3040. // return errThree
  3041. // }
  3042. //
  3043. // return nil
  3044. // } else {
  3045. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  3046. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  3047. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3048. // WarehouseOutId: warehouseout.ID,
  3049. // Status: 1,
  3050. // Ctime: time.Now().Unix(),
  3051. // Remark: warehouse.Remark,
  3052. // OrgId: orgID,
  3053. // Type: 1,
  3054. // Manufacturer: warehouse.Manufacturer,
  3055. // Dealer: warehouse.Dealer,
  3056. // IsSys: 1,
  3057. // SysRecordTime: advice.RecordDate,
  3058. // DrugId: advice.DrugId,
  3059. // Number: warehouse.Number,
  3060. // BatchNumber: warehouse.BatchNumber,
  3061. // Price: warehouse.RetailPrice,
  3062. // CountUnit: drup.MinUnit,
  3063. // ProductDate: warehouse.ProductDate,
  3064. // ExpiryDate: warehouse.ExpiryDate,
  3065. // PatientId: advice.PatientId,
  3066. // Count: deliver_number,
  3067. // WarehouseInfoId: warehouse.ID,
  3068. // AdviceId: advice.ID,
  3069. // StorehouseId: storeConfig.DrugStorehouseOut,
  3070. // IsCheck: 1,
  3071. // }
  3072. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  3073. // if errOne != nil {
  3074. // return errOne
  3075. // }
  3076. //
  3077. // details := &models.DrugAutomaticReduceDetail{
  3078. // WarehouseOutId: warehouseout.ID,
  3079. // WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  3080. // PatientId: advice.PatientId,
  3081. // Ctime: time.Now().Unix(),
  3082. // Mtime: time.Now().Unix(),
  3083. // Status: 1,
  3084. // RecordTime: advice.RecordDate,
  3085. // OrgId: orgID,
  3086. // DrugId: advice.DrugId,
  3087. // Count: deliver_number,
  3088. // CountUnit: drup.MinUnit,
  3089. // WarehouseInfoId: warehouse.ID,
  3090. // StorehouseId: storeConfig.DrugStorehouseOut,
  3091. // }
  3092. // errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  3093. // if errTwo != nil {
  3094. // return errTwo
  3095. // }
  3096. //
  3097. // // 出库完成后,要将该批次库存清零
  3098. // warehouse.StockMaxNumber = 0
  3099. // warehouse.StockMinNumber = 0
  3100. // warehouse.Mtime = time.Now().Unix()
  3101. //
  3102. // info := models.XtDrugWarehouseInfo{
  3103. // ID: warehouse.ID,
  3104. // WarehousingId: warehouse.WarehousingId,
  3105. // DrugId: warehouse.DrugId,
  3106. // Number: warehouse.Number,
  3107. // ProductDate: warehouse.ProductDate,
  3108. // ExpiryDate: warehouse.ExpiryDate,
  3109. // WarehousingCount: warehouse.WarehousingCount,
  3110. // Price: warehouse.Price,
  3111. // TotalPrice: warehouse.TotalPrice,
  3112. // Dealer: warehouse.Dealer,
  3113. // Manufacturer: warehouse.Manufacturer,
  3114. // Remark: warehouse.Remark,
  3115. // Ctime: warehouse.Ctime,
  3116. // Mtime: warehouse.Mtime,
  3117. // Status: 1,
  3118. // OrgId: warehouse.OrgId,
  3119. // IsReturn: warehouse.IsReturn,
  3120. // WarehousingOrder: warehouse.WarehousingOrder,
  3121. // Type: warehouse.Type,
  3122. // RetailPrice: warehouse.RetailPrice,
  3123. // RetailTotalPrice: warehouse.RetailPrice,
  3124. // StockMaxNumber: 0,
  3125. // StockMinNumber: 0,
  3126. // BatchNumber: warehouse.BatchNumber,
  3127. // MaxUnit: warehouse.MaxUnit,
  3128. // WarehousingInfoId: warehouse.WarehousingInfoId,
  3129. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3130. // StorehouseId: storeConfig.DrugStorehouseOut,
  3131. // IsCheck: 1,
  3132. // }
  3133. // //扣减库存
  3134. // errThree := UpDateDrugWarehouseInfoByStock(&info)
  3135. //
  3136. // if errThree != nil {
  3137. // return errThree
  3138. // }
  3139. //
  3140. // drugflow := models.DrugFlow{
  3141. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3142. // WarehouseOutId: warehouseout.ID,
  3143. // DrugId: advice.DrugId,
  3144. // Number: warehouse.Number,
  3145. // ProductDate: warehouse.ProductDate,
  3146. // ExpireDate: warehouse.ExpiryDate,
  3147. // Count: deliver_number,
  3148. // Price: warehouse.RetailPrice,
  3149. // Status: 1,
  3150. // Ctime: time.Now().Unix(),
  3151. // UserOrgId: orgID,
  3152. // Manufacturer: warehouse.Manufacturer,
  3153. // Dealer: warehouse.Dealer,
  3154. // BatchNumber: warehouse.BatchNumber,
  3155. // MaxUnit: drup.MinUnit,
  3156. // ConsumableType: 3,
  3157. // IsEdit: 1,
  3158. // Creator: advice.ExecutionStaff,
  3159. // IsSys: 1,
  3160. // PatientId: advice.PatientId,
  3161. // WarehousingDetailId: warehouse.ID,
  3162. // AdviceId: advice.ID,
  3163. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3164. // StorehouseId: storeConfig.DrugStorehouseOut,
  3165. // }
  3166. //
  3167. // CreateDrugFlowOne(drugflow)
  3168. //
  3169. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3170. // prescribingNumber_two_temp := deliver_number - stock_number
  3171. //
  3172. // overPlusNumber := float64(prescribingNumber_two_temp)
  3173. //
  3174. // advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  3175. // advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  3176. //
  3177. // NewDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  3178. // }
  3179. //
  3180. // return
  3181. //}
  3182. //
  3183. //// 药品出库 递归方式
  3184. //func NewHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  3185. //
  3186. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  3187. // var deliver_number int64 = 0
  3188. // var stock_number int64 = 0
  3189. //
  3190. // prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  3191. // count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  3192. //
  3193. // deliver_number = count
  3194. //
  3195. // var min_price float64
  3196. //
  3197. // if advice.PrescribingNumberUnit == drup.MaxUnit {
  3198. // min_price = drup.RetailPrice
  3199. // }
  3200. // if advice.PrescribingNumberUnit == drup.MinUnit {
  3201. // min_price = drup.MinPrice
  3202. // }
  3203. //
  3204. // if advice.PrescribingNumberUnit != drup.MaxUnit && advice.PrescribingNumberUnit != drup.MinUnit {
  3205. // min_price = drup.RetailPrice
  3206. // }
  3207. //
  3208. // fmt.Println(min_price)
  3209. // // 根据先进先出原则,查询最先入库的批次,进行出库
  3210. // // 如果没有对应的库存,则报错
  3211. // //开启事物
  3212. // storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  3213. // lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  3214. // if lastWarehouse.StockMinNumber >= drup.MinNumber {
  3215. // var stockMax int64
  3216. // var stockMin int64
  3217. // stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  3218. // stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  3219. // ChangeMaxNumber(lastWarehouse.ID, stockMax)
  3220. // UpdateMinNumber(lastWarehouse.ID, stockMin)
  3221. // }
  3222. // warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  3223. // //查找药品信息
  3224. //
  3225. // if err != nil {
  3226. //
  3227. // return err
  3228. // }
  3229. //
  3230. // // 将该批次的剩余库存数量转换为拆零数量
  3231. //
  3232. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  3233. //
  3234. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3235. // if stock_number >= deliver_number {
  3236. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  3237. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3238. // WarehouseOutId: warehouseout.ID,
  3239. // Status: 1,
  3240. // Ctime: time.Now().Unix(),
  3241. // Remark: warehouse.Remark,
  3242. // OrgId: orgID,
  3243. // Type: 1,
  3244. // Manufacturer: warehouse.Manufacturer,
  3245. // Dealer: warehouse.Dealer,
  3246. // IsSys: 1,
  3247. // SysRecordTime: advice.RecordDate,
  3248. // DrugId: advice.DrugId,
  3249. // Number: warehouse.Number,
  3250. // BatchNumber: warehouse.BatchNumber,
  3251. // Price: warehouse.RetailPrice,
  3252. // CountUnit: drup.MinUnit,
  3253. // RetailPrice: warehouse.RetailPrice,
  3254. // ProductDate: warehouse.ProductDate,
  3255. // ExpiryDate: warehouse.ExpiryDate,
  3256. // PatientId: advice.PatientId,
  3257. // WarehouseInfoId: warehouse.ID,
  3258. // Count: deliver_number,
  3259. // AdviceId: advice.ID,
  3260. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3261. // StorehouseId: storeConfig.DrugStorehouseOut,
  3262. // IsCheck: 1,
  3263. // }
  3264. //
  3265. // drugflow := models.DrugFlow{
  3266. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3267. // WarehouseOutId: warehouseout.ID,
  3268. // DrugId: advice.DrugId,
  3269. // Number: warehouse.Number,
  3270. // ProductDate: warehouse.ProductDate,
  3271. // ExpireDate: warehouse.ExpiryDate,
  3272. // Count: deliver_number,
  3273. // Price: warehouse.RetailPrice,
  3274. // Status: 1,
  3275. // Ctime: time.Now().Unix(),
  3276. // UserOrgId: orgID,
  3277. // Manufacturer: warehouse.Manufacturer,
  3278. // Dealer: warehouse.Dealer,
  3279. // BatchNumber: warehouse.BatchNumber,
  3280. // MaxUnit: drup.MinUnit,
  3281. // ConsumableType: 3,
  3282. // IsEdit: 1,
  3283. // Creator: advice.ExecutionStaff,
  3284. // IsSys: 1,
  3285. // PatientId: advice.PatientId,
  3286. // SystemTime: advice.RecordDate,
  3287. // WarehousingDetailId: warehouse.ID,
  3288. // AdviceId: advice.ID,
  3289. // StorehouseId: storeConfig.DrugStorehouseOut,
  3290. // }
  3291. //
  3292. // CreateDrugFlowOne(drugflow)
  3293. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  3294. // if errOne != nil {
  3295. // return errOne
  3296. // }
  3297. //
  3298. // details := &models.DrugAutomaticReduceDetail{
  3299. // WarehouseOutId: warehouseout.ID,
  3300. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3301. // PatientId: advice.PatientId,
  3302. // Ctime: time.Now().Unix(),
  3303. // Mtime: time.Now().Unix(),
  3304. // Status: 1,
  3305. // RecordTime: advice.RecordDate,
  3306. // OrgId: orgID,
  3307. // DrugId: advice.DrugId,
  3308. // Count: deliver_number,
  3309. // CountUnit: drup.MinUnit,
  3310. // WarehouseInfoId: warehouse.ID,
  3311. // AdviceId: advice.ID,
  3312. // StorehouseId: storeConfig.DrugStorehouseOut,
  3313. // }
  3314. // errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  3315. // if errTwo != nil {
  3316. // return errTwo
  3317. // }
  3318. //
  3319. // // 出库完成后,要减去对应批次的库存数量
  3320. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  3321. //
  3322. // var maxNumber int64 = 0
  3323. // var minNumber int64 = 0
  3324. // //var stock_max_number int64 = 0
  3325. // //stock_max_number = warehouse.StockMaxNumber
  3326. //
  3327. // maxNumber = deliver_number / drup.MinNumber
  3328. // minNumber = deliver_number % drup.MinNumber
  3329. //
  3330. // if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  3331. // minNumber = maxNumber
  3332. // }
  3333. //
  3334. // if drup.MaxUnit != drup.MinUnit {
  3335. // if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  3336. // return errors.New("库存数量不足")
  3337. // }
  3338. // }
  3339. //
  3340. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  3341. //
  3342. // if warehouse.StockMaxNumber < 0 {
  3343. // warehouse.StockMaxNumber = 0
  3344. // }
  3345. // if warehouse.StockMinNumber < 0 {
  3346. // warehouse.StockMinNumber = 0
  3347. // }
  3348. //
  3349. // warehouse.Mtime = time.Now().Unix()
  3350. //
  3351. // if warehouse.StockMinNumber < minNumber {
  3352. //
  3353. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  3354. // warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  3355. // } else {
  3356. //
  3357. // if minNumber > 0 {
  3358. // warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  3359. // }
  3360. //
  3361. // if minNumber == 0 && maxNumber != 1 {
  3362. // if warehouse.StockMinNumber > 0 {
  3363. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  3364. // }
  3365. //
  3366. // }
  3367. // }
  3368. //
  3369. // if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  3370. // if (warehouse.StockMinNumber - deliver_number) > 0 {
  3371. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  3372. //
  3373. // //if warehouse.StockMinNumber == 0 {
  3374. // // warehouse.StockMaxNumber = stock_max_number
  3375. // //}
  3376. // }
  3377. //
  3378. // }
  3379. //
  3380. // if drup.MaxUnit != drup.MinUnit {
  3381. // if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  3382. // return errors.New("库存数量不足")
  3383. // }
  3384. // }
  3385. // if warehouse.StockMinNumber <= 0 {
  3386. // warehouse.StockMinNumber = 0
  3387. // }
  3388. //
  3389. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  3390. //
  3391. // if errThree != nil {
  3392. // return errThree
  3393. // }
  3394. //
  3395. // return nil
  3396. // } else {
  3397. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  3398. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  3399. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3400. // WarehouseOutId: warehouseout.ID,
  3401. // Status: 1,
  3402. // Ctime: time.Now().Unix(),
  3403. // Remark: warehouse.Remark,
  3404. // OrgId: orgID,
  3405. // Type: 1,
  3406. // Manufacturer: warehouse.Manufacturer,
  3407. // Dealer: warehouse.Dealer,
  3408. // IsSys: 1,
  3409. // SysRecordTime: advice.RecordDate,
  3410. // DrugId: advice.DrugId,
  3411. // Number: warehouse.Number,
  3412. // BatchNumber: warehouse.BatchNumber,
  3413. // Price: warehouse.RetailPrice,
  3414. // RetailPrice: warehouse.RetailPrice,
  3415. // ProductDate: warehouse.ProductDate,
  3416. // ExpiryDate: warehouse.ExpiryDate,
  3417. // PatientId: advice.PatientId,
  3418. // WarehouseInfoId: warehouse.ID,
  3419. // Count: stock_number,
  3420. // CountUnit: drup.MinUnit,
  3421. // AdviceId: advice.ID,
  3422. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3423. // StorehouseId: storeConfig.DrugStorehouseOut,
  3424. // IsCheck: 1,
  3425. // }
  3426. //
  3427. // drugflow := models.DrugFlow{
  3428. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3429. // WarehouseOutId: warehouseout.ID,
  3430. // DrugId: advice.DrugId,
  3431. // Number: warehouse.Number,
  3432. // ProductDate: warehouse.ProductDate,
  3433. // ExpireDate: warehouse.ExpiryDate,
  3434. // Count: stock_number,
  3435. // Price: warehouse.RetailPrice,
  3436. // Status: 1,
  3437. // Ctime: time.Now().Unix(),
  3438. // UserOrgId: orgID,
  3439. // Manufacturer: warehouse.Manufacturer,
  3440. // Dealer: warehouse.Dealer,
  3441. // BatchNumber: warehouse.BatchNumber,
  3442. // MaxUnit: drup.MinUnit,
  3443. // ConsumableType: 3,
  3444. // IsEdit: 1,
  3445. // Creator: advice.ExecutionStaff,
  3446. // IsSys: 1,
  3447. // PatientId: advice.PatientId,
  3448. // SystemTime: advice.RecordDate,
  3449. // WarehousingDetailId: warehouse.ID,
  3450. // AdviceId: advice.ID,
  3451. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3452. // StorehouseId: storeConfig.DrugStorehouseOut,
  3453. // }
  3454. //
  3455. // CreateDrugFlowOne(drugflow)
  3456. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  3457. // if errOne != nil {
  3458. // return errOne
  3459. // }
  3460. //
  3461. // details := &models.DrugAutomaticReduceDetail{
  3462. // WarehouseOutId: warehouseout.ID,
  3463. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3464. // PatientId: advice.PatientId,
  3465. // Ctime: time.Now().Unix(),
  3466. // Mtime: time.Now().Unix(),
  3467. // Status: 1,
  3468. // RecordTime: advice.RecordDate,
  3469. // OrgId: orgID,
  3470. // DrugId: advice.DrugId,
  3471. // Count: stock_number,
  3472. // CountUnit: drup.MinUnit,
  3473. // WarehouseInfoId: warehouse.ID,
  3474. // AdviceId: advice.ID,
  3475. // StorehouseId: storeConfig.DrugStorehouseOut,
  3476. // }
  3477. // errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  3478. // if errTwo != nil {
  3479. // return errTwo
  3480. // }
  3481. //
  3482. // info := models.XtDrugWarehouseInfo{
  3483. // ID: warehouse.ID,
  3484. // WarehousingId: warehouse.WarehousingId,
  3485. // DrugId: warehouse.DrugId,
  3486. // Number: warehouse.Number,
  3487. // ProductDate: warehouse.ProductDate,
  3488. // ExpiryDate: warehouse.ExpiryDate,
  3489. // WarehousingCount: warehouse.WarehousingCount,
  3490. // Price: warehouse.RetailPrice,
  3491. // TotalPrice: warehouse.TotalPrice,
  3492. // Dealer: warehouse.Dealer,
  3493. // Manufacturer: warehouse.Manufacturer,
  3494. // Remark: warehouse.Remark,
  3495. // Ctime: warehouse.Ctime,
  3496. // Mtime: warehouse.Mtime,
  3497. // Status: 1,
  3498. // OrgId: warehouse.OrgId,
  3499. // IsReturn: warehouse.IsReturn,
  3500. // WarehousingOrder: warehouse.WarehousingOrder,
  3501. // Type: warehouse.Type,
  3502. // RetailPrice: warehouse.RetailPrice,
  3503. // RetailTotalPrice: warehouse.RetailPrice,
  3504. // StockMaxNumber: 0,
  3505. // StockMinNumber: 0,
  3506. // BatchNumber: warehouse.BatchNumber,
  3507. // MaxUnit: warehouse.MaxUnit,
  3508. // WarehousingInfoId: warehouse.WarehousingInfoId,
  3509. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3510. // StorehouseId: storeConfig.DrugStorehouseOut,
  3511. // IsCheck: 1,
  3512. // }
  3513. // errThree := UpDateDrugWarehouseInfoByStock(&info)
  3514. // if errThree != nil {
  3515. // return errThree
  3516. // }
  3517. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3518. // prescribingNumber_two_temp := deliver_number - stock_number
  3519. // overPlusNumber := float64(prescribingNumber_two_temp)
  3520. // advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  3521. // advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  3522. //
  3523. // NewHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  3524. // }
  3525. //
  3526. // return
  3527. //}
  3528. //
  3529. //// 药品出库 递归方式
  3530. //func HisAuToReduce(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  3531. //
  3532. // //查询最后一次出库数据
  3533. // drugInfo, _ := GetDrugWarehouseOutInfoIsExit(advice.DrugId, advice.PatientId, advice.RecordDate)
  3534. //
  3535. // prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  3536. // count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  3537. // var count_number int64
  3538. // var device_number int64
  3539. // if advice.PrescribingNumberUnit == drup.MaxUnit {
  3540. // device_number = count * drup.MinNumber
  3541. // }
  3542. // if advice.PrescribingNumberUnit == drup.MinUnit {
  3543. // device_number = count
  3544. // }
  3545. //
  3546. // if drugInfo.CountUnit == drup.MaxUnit {
  3547. // count_number = drugInfo.Count * drup.MinNumber
  3548. // }
  3549. // if drugInfo.CountUnit == drup.MinUnit {
  3550. // count_number = drugInfo.Count
  3551. // }
  3552. //
  3553. // // 比较数据大小
  3554. // //如果当期医嘱数量小于最后一次出库则退库
  3555. // if device_number < count_number {
  3556. // var cha_number int64
  3557. // var maxNumber int64
  3558. // var minNumber int64
  3559. // cha_number = count_number - device_number
  3560. // maxNumber = cha_number / drup.MinNumber
  3561. // minNumber = cha_number % drup.MinNumber
  3562. // //更改库存
  3563. // ModifyDrugWarehouse(drugInfo.WarehouseInfoId, maxNumber, minNumber)
  3564. //
  3565. // //修改出库表
  3566. // info := models.DrugWarehouseOutInfo{
  3567. // Count: device_number,
  3568. // CountUnit: advice.PrescribingNumberUnit,
  3569. // }
  3570. //
  3571. // ModifyDrugWarehouseOutInfo(info, advice.DrugId, advice.PatientId, advice.RecordDate)
  3572. //
  3573. // //修改自动出库表
  3574. // detail := models.DrugAutomaticReduceDetail{
  3575. // Count: device_number,
  3576. // CountUnit: advice.PrescribingNumberUnit,
  3577. // }
  3578. // ModifyAutoMaticReduce(detail, advice.PatientId, advice.RecordDate, advice.DrugId)
  3579. // //修改流水
  3580. // flow := models.DrugFlow{
  3581. // Count: device_number,
  3582. // MaxUnit: drup.MinUnit,
  3583. // }
  3584. // ModifyDrugFlow(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId)
  3585. //
  3586. // }
  3587. //
  3588. // //如果当期医嘱数量大于最后一次出库则需要出库
  3589. // if device_number > count_number {
  3590. // var cha_number int64
  3591. // cha_number = device_number - count_number
  3592. //
  3593. // // 根据先进先出原则,查询最先入库的批次,进行出库
  3594. //
  3595. // storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  3596. // lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  3597. // if lastWarehouse.StockMinNumber >= drup.MinNumber {
  3598. // var stockMax int64
  3599. // var stockMin int64
  3600. // stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  3601. // stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  3602. // ChangeMaxNumber(lastWarehouse.ID, stockMax)
  3603. // UpdateMinNumber(lastWarehouse.ID, stockMin)
  3604. // }
  3605. // warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  3606. // //查找药品信息
  3607. // //baseInfo, _ := FindBaseDrugLibRecord(advice.UserOrgId, advice.DrugId)
  3608. // if err != nil {
  3609. // return err
  3610. // }
  3611. //
  3612. // // 将该批次的剩余库存数量转换为拆零数量
  3613. // var stock_number int64
  3614. // var maxNumber int64
  3615. // var minNumber int64
  3616. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  3617. //
  3618. // //如果库存数量大于出库数据,正常出库
  3619. // if stock_number >= cha_number {
  3620. // //扣减库存
  3621. // maxNumber = cha_number / drup.MinNumber
  3622. // minNumber = cha_number % drup.MinNumber
  3623. //
  3624. // if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  3625. // minNumber = maxNumber
  3626. // }
  3627. //
  3628. // if drup.MaxUnit != drup.MinUnit {
  3629. // if warehouse.StockMaxNumber < maxNumber {
  3630. // return errors.New("库存数量不足")
  3631. // }
  3632. // }
  3633. //
  3634. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  3635. // if warehouse.StockMaxNumber < 0 {
  3636. // warehouse.StockMaxNumber = 0
  3637. // }
  3638. //
  3639. // warehouse.Mtime = time.Now().Unix()
  3640. //
  3641. // if warehouse.StockMinNumber < minNumber {
  3642. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  3643. // warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  3644. // } else {
  3645. //
  3646. // warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  3647. //
  3648. // }
  3649. //
  3650. // if drup.MaxUnit != drup.MinUnit {
  3651. // if warehouse.StockMaxNumber < 0 {
  3652. // return errors.New("库存数量不足")
  3653. // }
  3654. // }
  3655. //
  3656. // //扣减库存232332332332
  3657. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  3658. //
  3659. // if errThree != nil {
  3660. // return errThree
  3661. // }
  3662. //
  3663. // //修改出库表
  3664. // info := models.DrugWarehouseOutInfo{
  3665. // Count: device_number,
  3666. // CountUnit: advice.PrescribingNumberUnit,
  3667. // }
  3668. // ModifyDrugWarehouseOutInfoOne(info, advice.DrugId, advice.PatientId, advice.RecordDate, warehouse.ID)
  3669. // //修改自动出库表
  3670. // detail := models.DrugAutomaticReduceDetail{
  3671. // Count: device_number,
  3672. // CountUnit: advice.PrescribingNumberUnit,
  3673. // }
  3674. // ModifyAutoMaticReduceOne(detail, advice.PatientId, advice.RecordDate, advice.DrugId, warehouse.ID)
  3675. // //修改流水
  3676. // flow := models.DrugFlow{
  3677. // Count: device_number,
  3678. // MaxUnit: advice.PrescribingNumberUnit,
  3679. // }
  3680. // ModifyDrugFlowOne(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId, warehouse.ID)
  3681. // }
  3682. // //如果库存数量小于出库数据,则需要出库到下一个批次
  3683. // if stock_number < cha_number {
  3684. //
  3685. // //修改出库表
  3686. // info := models.DrugWarehouseOutInfo{
  3687. // Count: cha_number,
  3688. // CountUnit: advice.PrescribingNumberUnit,
  3689. // }
  3690. // ModifyDrugWarehouseOutInfoOne(info, advice.DrugId, advice.PatientId, advice.RecordDate, warehouse.ID)
  3691. // //修改自动出库表
  3692. // detail := models.DrugAutomaticReduceDetail{
  3693. // Count: cha_number,
  3694. // CountUnit: advice.PrescribingNumberUnit,
  3695. // }
  3696. // ModifyAutoMaticReduceOne(detail, advice.PatientId, advice.RecordDate, advice.DrugId, warehouse.ID)
  3697. // //修改流水
  3698. // flow := models.DrugFlow{
  3699. // Count: cha_number,
  3700. // MaxUnit: advice.PrescribingNumberUnit,
  3701. // }
  3702. // ModifyDrugFlowOne(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId, warehouse.ID)
  3703. //
  3704. // druginfo := &models.XtDrugWarehouseInfo{
  3705. // ID: warehouse.ID,
  3706. // WarehousingId: warehouse.WarehousingId,
  3707. // DrugId: warehouse.DrugId,
  3708. // Number: warehouse.Number,
  3709. // ProductDate: warehouse.ProductDate,
  3710. // ExpiryDate: warehouse.ExpiryDate,
  3711. // WarehousingCount: warehouse.WarehousingCount,
  3712. // Price: warehouse.Price,
  3713. // TotalPrice: warehouse.TotalPrice,
  3714. // Dealer: warehouse.Dealer,
  3715. // Manufacturer: warehouse.Manufacturer,
  3716. // Remark: warehouse.Remark,
  3717. // Ctime: warehouse.Ctime,
  3718. // Mtime: warehouse.Mtime,
  3719. // Status: 1,
  3720. // OrgId: warehouse.OrgId,
  3721. // IsReturn: warehouse.IsReturn,
  3722. // WarehousingOrder: warehouse.WarehousingOrder,
  3723. // Type: warehouse.Type,
  3724. // RetailPrice: warehouse.RetailPrice,
  3725. // RetailTotalPrice: warehouse.RetailPrice,
  3726. // StockMaxNumber: 0,
  3727. // StockMinNumber: 0,
  3728. // BatchNumber: warehouse.BatchNumber,
  3729. // MaxUnit: warehouse.MaxUnit,
  3730. // WarehousingInfoId: warehouse.WarehousingInfoId,
  3731. // StorehouseId: storeConfig.DrugStorehouseOut,
  3732. // IsCheck: 1,
  3733. // }
  3734. // errThree := UpDateDrugWarehouseInfoByStock(druginfo)
  3735. // if errThree != nil {
  3736. // return errThree
  3737. // }
  3738. //
  3739. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3740. // prescribingNumber_two_temp := cha_number - stock_number
  3741. //
  3742. // overPlusNumber := float64(prescribingNumber_two_temp)
  3743. //
  3744. // advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  3745. //
  3746. // advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  3747. //
  3748. // NewHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  3749. //
  3750. // }
  3751. // }
  3752. //
  3753. // return
  3754. //}
  3755. //
  3756. ////采购管理耗材出库逻辑
  3757. //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) {
  3758. //
  3759. // var deliver_number int64 = 0
  3760. // var stock_number int64 = 0
  3761. // var maxNumber int64 = 0
  3762. //
  3763. // deliver_number = goods.Count
  3764. //
  3765. // // 根据先进先出原则,查询最先入库的批次,进行出库
  3766. // houseConfig, _ := GetAllStoreHouseConfig(orgID)
  3767. // // 如果没有对应的库存,则报错
  3768. // warehouse, err := FindFirstWarehousingInfoByStockNight(goods.GoodId, goods.GoodTypeId, warehouse_out_id, goods.IsSource, houseConfig.StorehouseOutInfo)
  3769. // if err != nil {
  3770. //
  3771. // return err
  3772. // }
  3773. //
  3774. // stock_number = warehouse.StockCount
  3775. //
  3776. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3777. // if stock_number >= deliver_number {
  3778. //
  3779. // warehouseOutInfo := &models.WarehouseOutInfo{
  3780. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3781. // WarehouseOutId: goods.WarehouseOutId,
  3782. // WarehouseInfotId: warehouse.ID,
  3783. // Status: 1,
  3784. // Ctime: time.Now().Unix(),
  3785. // Remark: goods.Remark,
  3786. // OrgId: orgID,
  3787. // Type: 1,
  3788. // Manufacturer: goods.Manufacturer,
  3789. // Dealer: goods.Dealer,
  3790. // IsSys: 0,
  3791. // SysRecordTime: record_time,
  3792. // GoodTypeId: goods.GoodTypeId,
  3793. // GoodId: goods.GoodId,
  3794. // ExpiryDate: goods.ExpiryDate,
  3795. // ProductDate: goods.ProductDate,
  3796. // Number: warehouse.Number,
  3797. // Price: goods.Price,
  3798. // LicenseNumber: goods.LicenseNumber,
  3799. // SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  3800. // StorehouseId: warehouse.StorehouseId,
  3801. // IsCheck: 1,
  3802. // }
  3803. // warehouseOutInfo.Count = goods.Count
  3804. // //添加出库单详情
  3805. // errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  3806. // if errOne != nil {
  3807. // return errOne
  3808. // }
  3809. //
  3810. // stockFlow := models.VmStockFlow{
  3811. // WarehousingId: warehouse.ID,
  3812. // GoodId: goods.GoodId,
  3813. // Number: warehouse.Number,
  3814. // LicenseNumber: goods.LicenseNumber,
  3815. // Count: goods.Count,
  3816. // UserOrgId: goods.OrgId,
  3817. // PatientId: goods.PatientId,
  3818. // SystemTime: record_time,
  3819. // ConsumableType: 2,
  3820. // IsSys: goods.IsSys,
  3821. // WarehousingOrder: "",
  3822. // WarehouseOutId: goods.WarehouseOutId,
  3823. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3824. // IsEdit: 1,
  3825. // CancelStockId: 0,
  3826. // CancelOrderNumber: "",
  3827. // Manufacturer: goods.Manufacturer,
  3828. // Dealer: goods.Dealer,
  3829. // Creator: creator,
  3830. // UpdateCreator: 0,
  3831. // Status: 1,
  3832. // Ctime: time.Now().Unix(),
  3833. // Mtime: 0,
  3834. // Price: warehouse.PackingPrice,
  3835. // WarehousingDetailId: 0,
  3836. // WarehouseOutDetailId: warehouseOutInfo.ID,
  3837. // CancelOutDetailId: 0,
  3838. // ProductDate: goods.ProductDate,
  3839. // ExpireDate: goods.ExpiryDate,
  3840. // SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  3841. // SupplyWarehouseId: warehouseOut.SupplyWarehouseId,
  3842. // StorehouseId: warehouse.StorehouseId,
  3843. // }
  3844. //
  3845. // //创建出库流水
  3846. // CreateStockFlowOne(stockFlow)
  3847. //
  3848. // maxNumber = goods.Count
  3849. //
  3850. // if warehouse.StockCount < maxNumber {
  3851. // return errors.New("库存数量不足")
  3852. // }
  3853. //
  3854. // warehouse.StockCount = warehouse.StockCount - maxNumber
  3855. // warehouse.Mtime = time.Now().Unix()
  3856. //
  3857. // //扣减库存
  3858. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  3859. // //出库数量增加
  3860. // AddGoodSumOutCount(houseConfig.StorehouseOutInfo, warehouse.GoodId, warehouse.OrgId, goods.Count)
  3861. // ModifyGoodReduceInformationSix(warehouse.GoodId, maxNumber, warehouse.OrgId, warehouse.PackingPrice)
  3862. //
  3863. // if errThree != nil {
  3864. // return errThree
  3865. // }
  3866. //
  3867. // } else {
  3868. //
  3869. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  3870. // warehouseOutInfo := &models.WarehouseOutInfo{
  3871. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3872. // WarehouseOutId: goods.WarehouseOutId,
  3873. // WarehouseInfotId: warehouse.ID,
  3874. // Status: 1,
  3875. // Ctime: time.Now().Unix(),
  3876. // Remark: goods.Remark,
  3877. // OrgId: orgID,
  3878. // Type: 1,
  3879. // Manufacturer: goods.Manufacturer,
  3880. // Dealer: goods.Dealer,
  3881. // IsSys: 0,
  3882. // SysRecordTime: record_time,
  3883. // GoodTypeId: goods.GoodTypeId,
  3884. // GoodId: goods.GoodId,
  3885. // ExpiryDate: goods.ExpiryDate,
  3886. // ProductDate: goods.ProductDate,
  3887. // Number: warehouse.Number,
  3888. // Price: warehouse.PackingPrice,
  3889. // LicenseNumber: goods.LicenseNumber,
  3890. // SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  3891. // StorehouseId: warehouse.StorehouseId,
  3892. // IsCheck: 1,
  3893. // }
  3894. // warehouseOutInfo.Count = stock_number
  3895. //
  3896. // //查询是否已经生成出库单
  3897. // info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  3898. // if errcodes == gorm.ErrRecordNotFound {
  3899. // //创建出库单
  3900. // errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  3901. // if errOne != nil {
  3902. // return errOne
  3903. // }
  3904. // } else {
  3905. // warehouseOutInfo.Count = info.Count + stock_number
  3906. //
  3907. // UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  3908. // }
  3909. //
  3910. // stockFlow := models.VmStockFlow{
  3911. // WarehousingId: warehouse.ID,
  3912. // GoodId: goods.GoodId,
  3913. // Number: warehouse.Number,
  3914. // LicenseNumber: goods.LicenseNumber,
  3915. // Count: stock_number,
  3916. // UserOrgId: goods.OrgId,
  3917. // PatientId: 0,
  3918. // SystemTime: record_time,
  3919. // ConsumableType: 2,
  3920. // IsSys: 0,
  3921. // WarehousingOrder: "",
  3922. // WarehouseOutId: goods.WarehouseOutId,
  3923. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3924. // IsEdit: 1,
  3925. // CancelStockId: 0,
  3926. // CancelOrderNumber: "",
  3927. // Manufacturer: goods.Manufacturer,
  3928. // Dealer: goods.Dealer,
  3929. // Creator: creator,
  3930. // UpdateCreator: 0,
  3931. // Status: 1,
  3932. // Ctime: time.Now().Unix(),
  3933. // Mtime: 0,
  3934. // Price: warehouse.PackingPrice,
  3935. // WarehousingDetailId: 0,
  3936. // WarehouseOutDetailId: warehouseOutInfo.ID,
  3937. // CancelOutDetailId: 0,
  3938. // ProductDate: goods.ProductDate,
  3939. // ExpireDate: goods.ExpiryDate,
  3940. // SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  3941. // SupplyWarehouseId: warehouseOut.SupplyWarehouseId,
  3942. // StorehouseId: warehouse.StorehouseId,
  3943. // }
  3944. //
  3945. // //创建出库明细
  3946. // CreateStockFlowOne(stockFlow)
  3947. // // 出库完成后,要将该批次库存清零
  3948. // ModifyGoodReduceInformationSix(warehouse.GoodId, warehouse.StockCount, warehouse.OrgId, warehouse.PackingPrice)
  3949. // warehouse.StockCount = 0
  3950. // warehouse.Mtime = time.Now().Unix()
  3951. //
  3952. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  3953. //
  3954. // if errThree != nil {
  3955. // return errThree
  3956. // }
  3957. // //出库数量增加
  3958. // AddGoodSumOutCount(houseConfig.StorehouseOutInfo, warehouse.GoodId, warehouse.OrgId, warehouse.StockCount)
  3959. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3960. // goods.Count = deliver_number - stock_number
  3961. //
  3962. // ConsumablesDeliveryNight(orgID, record_time, goods, warehouseOut, count, creator, warehouse_out_id, cancel_out_id)
  3963. //
  3964. // }
  3965. // return nil
  3966. //}
  3967. //
  3968. //func FindFirstWarehousingInfoByStockNight(good_id int64, good_type_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  3969. //
  3970. // if is_source == 1 {
  3971. // 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
  3972. //
  3973. // }
  3974. // if is_source == 2 || is_source == 0 {
  3975. // 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
  3976. // }
  3977. // return info, err
  3978. //}
  3979. //
  3980. //// 药品出库 递归方式
  3981. //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) {
  3982. //
  3983. // houseConfig, _ := GetAllStoreHouseConfig(orgID)
  3984. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  3985. // var deliver_number int64 = 0
  3986. // var stock_number int64 = 0
  3987. // var retail_price float64
  3988. // if advice.CountUnit == drup.MaxUnit {
  3989. // deliver_number = prescribingNumber * drup.MinNumber
  3990. // formatInt := strconv.FormatInt(prescribingNumber, 10)
  3991. // floatcount, _ := strconv.ParseFloat(formatInt, 64)
  3992. // retail_price = floatcount * advice.Price
  3993. // } else {
  3994. // deliver_number = prescribingNumber
  3995. // formatInt := strconv.FormatInt(prescribingNumber, 10)
  3996. // floatcount, _ := strconv.ParseFloat(formatInt, 64)
  3997. // retail_price = floatcount * advice.Price
  3998. // }
  3999. //
  4000. // // 根据先进先出原则,查询最先入库的批次,进行出库
  4001. // // 如果没有对应的库存,则报错
  4002. // //开启事物
  4003. // lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, houseConfig.DrugStorehouseOut)
  4004. // if lastWarehouse.StockMinNumber >= drup.MinNumber {
  4005. // var stockMax int64
  4006. // var stockMin int64
  4007. // stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  4008. // stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  4009. // ChangeMaxNumber(lastWarehouse.ID, stockMax)
  4010. // UpdateMinNumber(lastWarehouse.ID, stockMin)
  4011. // }
  4012. //
  4013. // warehouse, err := FindLastDrugWarehousingInfoByIDSix(advice.DrugId, warehouse_out_id, advice.IsSource, houseConfig.DrugStorehouseOut)
  4014. //
  4015. // if err != nil {
  4016. //
  4017. // return err
  4018. // }
  4019. //
  4020. // // 将该批次的剩余库存数量转换为拆零数量
  4021. //
  4022. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  4023. //
  4024. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  4025. // if stock_number >= deliver_number {
  4026. //
  4027. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4028. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4029. // WarehouseOutId: advice.WarehouseOutId,
  4030. // Status: 1,
  4031. // Ctime: time.Now().Unix(),
  4032. // Remark: advice.Remark,
  4033. // OrgId: orgID,
  4034. // Type: 1,
  4035. // Manufacturer: advice.Manufacturer,
  4036. // Dealer: warehouse.Dealer,
  4037. // IsSys: 0,
  4038. // SysRecordTime: advice.SysRecordTime,
  4039. // DrugId: advice.DrugId,
  4040. // ExpiryDate: advice.ExpiryDate,
  4041. // ProductDate: advice.ProductDate,
  4042. // Number: advice.Number,
  4043. // BatchNumber: warehouse.BatchNumber,
  4044. // Count: deliver_number,
  4045. // RetailPrice: advice.RetailPrice,
  4046. // Price: advice.Price,
  4047. // WarehouseInfoId: warehouse.ID,
  4048. // CountUnit: drup.MinUnit,
  4049. // AdviceId: advice.ID,
  4050. // SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  4051. // SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  4052. // StorehouseId: warehouse.StorehouseId,
  4053. // IsCheck: 1,
  4054. // }
  4055. //
  4056. // warehouseOutInfo.Count = prescribingNumber
  4057. // warehouseOutInfo.CountUnit = advice.CountUnit
  4058. //
  4059. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  4060. // if errOne != nil {
  4061. // return errOne
  4062. // }
  4063. //
  4064. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4065. // drugflow := models.DrugFlow{
  4066. // WarehouseOutId: advice.WarehouseOutId,
  4067. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4068. // DrugId: advice.DrugId,
  4069. // Number: warehouse.Number,
  4070. // ProductDate: advice.ProductDate,
  4071. // ExpireDate: advice.ExpiryDate,
  4072. // Count: deliver_number,
  4073. // Price: warehouse.RetailPrice,
  4074. // Status: 1,
  4075. // Ctime: time.Now().Unix(),
  4076. // UserOrgId: advice.OrgId,
  4077. // Manufacturer: advice.Manufacturer,
  4078. // Dealer: advice.Dealer,
  4079. // BatchNumber: warehouse.BatchNumber,
  4080. // MaxUnit: drup.MinUnit,
  4081. // ConsumableType: 2,
  4082. // IsEdit: 1,
  4083. // Creator: 0,
  4084. // IsSys: 0,
  4085. // WarehouseOutDetailId: drugWareInfo.ID,
  4086. // AdviceId: advice.ID,
  4087. // SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  4088. // SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  4089. // SystemTime: advice.SysRecordTime,
  4090. // StorehouseId: warehouse.StorehouseId,
  4091. // WarehousingDetailId: warehouse.ID,
  4092. // WarehousingId: warehouse.ID,
  4093. // RetailPrice: retail_price,
  4094. // LastPrice: warehouse.Price,
  4095. // }
  4096. //
  4097. // CreateDrugFlowOne(drugflow)
  4098. // // 出库完成后,要减去对应批次的库存数量
  4099. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  4100. // var maxNumber int64 = 0
  4101. // var minNumber int64 = 0
  4102. // if advice.CountUnit == drup.MinUnit {
  4103. // maxNumber = prescribingNumber / drup.MinNumber
  4104. // minNumber = prescribingNumber % drup.MinNumber
  4105. // if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  4106. // minNumber = maxNumber
  4107. // }
  4108. // } else {
  4109. // maxNumber = prescribingNumber
  4110. // }
  4111. //
  4112. // if warehouse.StockMaxNumber < maxNumber {
  4113. //
  4114. // return errors.New("库存数量不足")
  4115. // }
  4116. //
  4117. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  4118. // if warehouse.StockMaxNumber < 0 {
  4119. // warehouse.StockMaxNumber = 0
  4120. // }
  4121. //
  4122. // warehouse.Mtime = time.Now().Unix()
  4123. //
  4124. // if warehouse.StockMinNumber < minNumber {
  4125. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  4126. // warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  4127. // } else {
  4128. // warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  4129. // }
  4130. //
  4131. // if warehouse.StockMaxNumber < 0 {
  4132. // return errors.New("库存数量不足")
  4133. // }
  4134. // if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
  4135. // warehouse.StockMinNumber = 0
  4136. // }
  4137. //
  4138. // if warehouse.StockMaxNumber > 0 {
  4139. // var sum_count int64
  4140. // sum_count = maxNumber * drup.MinNumber
  4141. // ModifyDrugReduceInformationSix(warehouse.DrugId, sum_count, warehouse.OrgId, warehouse.RetailPrice)
  4142. // //出库数量增加
  4143. // AddDrugSumOutCount(houseConfig.DrugStorehouseOut, sum_count, warehouse.OrgId, warehouse.DrugId)
  4144. // }
  4145. //
  4146. // if warehouse.StockMinNumber > 0 {
  4147. // ModifyDrugReduceInformationSix(warehouse.DrugId, minNumber, warehouse.OrgId, warehouse.RetailPrice)
  4148. // //出库数量增加
  4149. // AddDrugSumOutCount(houseConfig.DrugStorehouseOut, minNumber, warehouse.OrgId, warehouse.DrugId)
  4150. // }
  4151. //
  4152. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4153. // if errThree != nil {
  4154. // return errThree
  4155. // }
  4156. //
  4157. // return nil
  4158. // } else {
  4159. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  4160. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4161. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4162. // WarehouseOutId: advice.WarehouseOutId,
  4163. // Status: 1,
  4164. // Ctime: time.Now().Unix(),
  4165. // Remark: advice.Remark,
  4166. // OrgId: orgID,
  4167. // Type: 1,
  4168. // Manufacturer: advice.Manufacturer,
  4169. // Dealer: warehouse.Dealer,
  4170. // IsSys: 0,
  4171. // SysRecordTime: advice.SysRecordTime,
  4172. // DrugId: advice.DrugId,
  4173. // ExpiryDate: warehouse.ExpiryDate,
  4174. // ProductDate: warehouse.ProductDate,
  4175. // Number: warehouse.Number,
  4176. // BatchNumber: warehouse.BatchNumber,
  4177. // Count: stock_number,
  4178. // RetailPrice: advice.RetailPrice,
  4179. // Price: advice.Price,
  4180. // WarehouseInfoId: warehouse.ID,
  4181. // CountUnit: drup.MinUnit,
  4182. // AdviceId: advice.ID,
  4183. // SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  4184. // SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  4185. // StorehouseId: warehouse.StorehouseId,
  4186. // IsCheck: 1,
  4187. // }
  4188. // warehouseOutInfo.Count = stock_number
  4189. //
  4190. // //warehouseOutInfo.CountUnit = advice.CountUnit
  4191. //
  4192. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  4193. // if errOne != nil {
  4194. // return errOne
  4195. // }
  4196. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4197. // drugflow := models.DrugFlow{
  4198. // WarehouseOutId: advice.WarehouseOutId,
  4199. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4200. // DrugId: advice.DrugId,
  4201. // Number: warehouse.Number,
  4202. // ProductDate: advice.ProductDate,
  4203. // ExpireDate: advice.ExpiryDate,
  4204. // Count: stock_number,
  4205. // Price: warehouse.RetailPrice,
  4206. // Status: 1,
  4207. // Ctime: time.Now().Unix(),
  4208. // UserOrgId: advice.OrgId,
  4209. // Manufacturer: advice.Manufacturer,
  4210. // Dealer: advice.Dealer,
  4211. // BatchNumber: warehouse.BatchNumber,
  4212. // MaxUnit: drup.MinUnit,
  4213. // ConsumableType: 2,
  4214. // IsEdit: 1,
  4215. // Creator: 0,
  4216. // IsSys: 0,
  4217. // WarehouseOutDetailId: drugWareInfo.ID,
  4218. // AdviceId: advice.ID,
  4219. // SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  4220. // SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  4221. // SystemTime: advice.SysRecordTime,
  4222. // StorehouseId: warehouse.StorehouseId,
  4223. // WarehousingDetailId: warehouse.ID,
  4224. // WarehousingId: warehouse.ID,
  4225. // RetailPrice: retail_price,
  4226. // LastPrice: warehouse.Price,
  4227. // }
  4228. // CreateDrugFlowOne(drugflow)
  4229. // if warehouse.StockMaxNumber > 0 {
  4230. // var sum_count int64
  4231. // sum_count = warehouse.StockMaxNumber * drup.MinNumber
  4232. // ModifyDrugReduceInformationSix(warehouse.DrugId, sum_count, warehouse.OrgId, warehouse.RetailPrice)
  4233. // AddDrugSumOutCount(houseConfig.DrugStorehouseOut, sum_count, warehouse.OrgId, warehouse.DrugId)
  4234. // }
  4235. //
  4236. // if warehouse.StockMinNumber > 0 {
  4237. // ModifyDrugReduceInformationSix(warehouse.DrugId, warehouse.StockMinNumber, warehouse.OrgId, warehouse.RetailPrice)
  4238. // AddDrugSumOutCount(houseConfig.DrugStorehouseOut, warehouse.StockMinNumber, warehouse.OrgId, warehouse.DrugId)
  4239. // }
  4240. //
  4241. // // 出库完成后,要将该批次库存清零
  4242. // warehouse.StockMaxNumber = 0
  4243. // warehouse.StockMinNumber = 0
  4244. // warehouse.Mtime = time.Now().Unix()
  4245. //
  4246. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4247. // if errThree != nil {
  4248. // return errThree
  4249. // }
  4250. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  4251. // prescribingNumber_two_temp := deliver_number - stock_number
  4252. // advice.CountUnit = drup.MinUnit
  4253. //
  4254. // AutoDrugDeliverInfoNight(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, warehouse_out_id, cancel_out_id)
  4255. // }
  4256. //
  4257. // return
  4258. //}
  4259. //
  4260. ////调拨出库逻辑
  4261. //func ConsumablesDeliveryTwelve(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64, warehouseInfo *models.WarehousingInfo) (err error) {
  4262. //
  4263. // var deliver_number int64 = 0
  4264. // var stock_number int64 = 0
  4265. // var maxNumber int64 = 0
  4266. //
  4267. // deliver_number = goods.Count
  4268. //
  4269. // // 根据先进先出原则,查询最先入库的批次,进行出库
  4270. //
  4271. // // 如果没有对应的库存,则报错
  4272. // warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  4273. // if err != nil {
  4274. //
  4275. // return err
  4276. // }
  4277. //
  4278. // stock_number = warehouse.StockCount
  4279. //
  4280. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  4281. // if stock_number >= deliver_number {
  4282. //
  4283. // warehouseOutInfo := &models.WarehouseOutInfo{
  4284. // WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  4285. // WarehouseOutId: goods.WarehouseOutId,
  4286. // WarehouseInfotId: warehouse.ID,
  4287. // Status: 1,
  4288. // Ctime: time.Now().Unix(),
  4289. // Remark: goods.Remark,
  4290. // OrgId: orgID,
  4291. // Type: 1,
  4292. // Manufacturer: warehouse.Manufacturer,
  4293. // Dealer: warehouse.Dealer,
  4294. // IsSys: 0,
  4295. // SysRecordTime: record_time,
  4296. // GoodTypeId: warehouse.GoodTypeId,
  4297. // GoodId: warehouse.GoodId,
  4298. // ExpiryDate: warehouse.ExpiryDate,
  4299. // ProductDate: warehouse.ProductDate,
  4300. // Number: warehouse.Number,
  4301. // Price: warehouse.PackingPrice,
  4302. // LicenseNumber: warehouse.LicenseNumber,
  4303. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4304. // StorehouseId: goods.StorehouseId,
  4305. // SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  4306. // IsCheck: 1,
  4307. // }
  4308. //
  4309. // warehouseOutInfo.Count = goods.Count
  4310. // //添加出库单详情
  4311. // errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  4312. // if errOne != nil {
  4313. // return errOne
  4314. // }
  4315. //
  4316. // stockFlow := models.VmStockFlow{
  4317. // WarehousingId: warehouse.ID,
  4318. // GoodId: goods.GoodId,
  4319. // Number: warehouse.Number,
  4320. // LicenseNumber: goods.LicenseNumber,
  4321. // Count: goods.Count,
  4322. // UserOrgId: goods.OrgId,
  4323. // PatientId: goods.PatientId,
  4324. // SystemTime: record_time,
  4325. // ConsumableType: 12,
  4326. // IsSys: goods.IsSys,
  4327. // WarehousingOrder: "",
  4328. // WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  4329. // WarehouseOutId: goods.WarehouseOutId,
  4330. // IsEdit: 1,
  4331. // CancelStockId: 0,
  4332. // CancelOrderNumber: "",
  4333. // Manufacturer: warehouse.Manufacturer,
  4334. // Dealer: warehouse.Dealer,
  4335. // Creator: creator,
  4336. // UpdateCreator: 0,
  4337. // Status: 1,
  4338. // Ctime: time.Now().Unix(),
  4339. // Mtime: 0,
  4340. // Price: warehouse.PackingPrice,
  4341. // WarehousingDetailId: 0,
  4342. // WarehouseOutDetailId: warehouseOutInfo.ID,
  4343. // CancelOutDetailId: 0,
  4344. // ProductDate: warehouse.ProductDate,
  4345. // ExpireDate: warehouse.ExpiryDate,
  4346. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4347. // StorehouseId: warehouse.StorehouseId,
  4348. // SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  4349. // }
  4350. //
  4351. // //创建出库流水
  4352. // CreateStockFlowOne(stockFlow)
  4353. //
  4354. // maxNumber = goods.Count
  4355. //
  4356. // if warehouse.StockCount < maxNumber {
  4357. // return errors.New("库存数量不足")
  4358. // }
  4359. //
  4360. // warehouse.StockCount = warehouse.StockCount - maxNumber
  4361. // warehouse.Mtime = time.Now().Unix()
  4362. //
  4363. // //扣减库存
  4364. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  4365. //
  4366. // //入库
  4367. // warehouseInfo := &models.WarehousingInfo{
  4368. // WarehousingOrder: warehouseInfo.WarehousingOrder,
  4369. // WarehousingId: warehouseInfo.WarehousingId,
  4370. // GoodId: warehouse.GoodId,
  4371. // Number: warehouse.Number,
  4372. // GoodTypeId: warehouse.GoodTypeId,
  4373. // ProductDate: warehouse.ProductDate,
  4374. // ExpiryDate: warehouse.ExpiryDate,
  4375. // WarehousingCount: goods.Count,
  4376. // Price: warehouse.Price,
  4377. // TotalPrice: 0,
  4378. // Status: 1,
  4379. // Ctime: warehouse.Ctime,
  4380. // Remark: goods.Remark,
  4381. // OrgId: warehouse.OrgId,
  4382. // Type: 1,
  4383. // Manufacturer: warehouse.Manufacturer,
  4384. // StockCount: goods.Count,
  4385. // Dealer: warehouse.Dealer,
  4386. // LicenseNumber: "",
  4387. // PackingPrice: warehouse.PackingPrice,
  4388. // StorehouseId: warehouseInfo.StorehouseId,
  4389. // SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  4390. // IsCheck: 1,
  4391. // }
  4392. //
  4393. // AddWarehosingInfo(warehouseInfo)
  4394. //
  4395. // stockInfoFlow := models.VmStockFlow{
  4396. // WarehousingId: warehouseInfo.WarehousingId,
  4397. // GoodId: goods.GoodId,
  4398. // Number: warehouse.Number,
  4399. // LicenseNumber: goods.LicenseNumber,
  4400. // Count: goods.Count,
  4401. // UserOrgId: goods.OrgId,
  4402. // PatientId: goods.PatientId,
  4403. // SystemTime: record_time,
  4404. // ConsumableType: 13,
  4405. // IsSys: goods.IsSys,
  4406. // WarehousingOrder: warehouseInfo.WarehousingOrder,
  4407. // WarehouseOutId: 0,
  4408. // WarehouseOutOrderNumber: "",
  4409. // IsEdit: 1,
  4410. // CancelStockId: 0,
  4411. // CancelOrderNumber: "",
  4412. // Manufacturer: warehouse.Manufacturer,
  4413. // Dealer: warehouse.Dealer,
  4414. // Creator: creator,
  4415. // UpdateCreator: 0,
  4416. // Status: 1,
  4417. // Ctime: time.Now().Unix(),
  4418. // Mtime: 0,
  4419. // Price: warehouse.Price,
  4420. // WarehousingDetailId: 0,
  4421. // WarehouseOutDetailId: warehouseOutInfo.ID,
  4422. // CancelOutDetailId: 0,
  4423. // ProductDate: warehouse.ProductDate,
  4424. // ExpireDate: warehouse.ExpiryDate,
  4425. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4426. // StorehouseId: warehouseInfo.StorehouseId,
  4427. // SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  4428. // }
  4429. //
  4430. // //创建入库流水
  4431. // CreateStockFlowOne(stockInfoFlow)
  4432. //
  4433. // if errThree != nil {
  4434. // return errThree
  4435. // }
  4436. //
  4437. // } else {
  4438. //
  4439. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  4440. // warehouseOutInfo := &models.WarehouseOutInfo{
  4441. // WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  4442. // WarehouseOutId: goods.WarehouseOutId,
  4443. // WarehouseInfotId: warehouse.ID,
  4444. // Status: 1,
  4445. // Ctime: time.Now().Unix(),
  4446. // Remark: goods.Remark,
  4447. // OrgId: orgID,
  4448. // Type: 1,
  4449. // Manufacturer: warehouse.Manufacturer,
  4450. // Dealer: warehouse.Dealer,
  4451. // IsSys: 0,
  4452. // SysRecordTime: record_time,
  4453. // GoodTypeId: warehouse.GoodTypeId,
  4454. // GoodId: warehouse.GoodId,
  4455. // ExpiryDate: warehouse.ExpiryDate,
  4456. // ProductDate: warehouse.ProductDate,
  4457. // Number: warehouse.Number,
  4458. // Price: warehouse.PackingPrice,
  4459. // LicenseNumber: warehouse.LicenseNumber,
  4460. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4461. // StorehouseId: warehouse.StorehouseId,
  4462. // SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  4463. // IsCheck: 1,
  4464. // }
  4465. // warehouseOutInfo.Count = warehouse.StockCount
  4466. //
  4467. // //查询是否已经生成出库单
  4468. // info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  4469. // if errcodes == gorm.ErrRecordNotFound {
  4470. // //创建出库单
  4471. // errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  4472. // if errOne != nil {
  4473. // return errOne
  4474. // }
  4475. // } else {
  4476. // warehouseOutInfo.Count = info.Count + stock_number
  4477. //
  4478. // UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  4479. // }
  4480. //
  4481. // stockFlow := models.VmStockFlow{
  4482. // WarehousingId: warehouse.ID,
  4483. // GoodId: goods.GoodId,
  4484. // Number: warehouse.Number,
  4485. // LicenseNumber: goods.LicenseNumber,
  4486. // Count: warehouse.StockCount,
  4487. // UserOrgId: goods.OrgId,
  4488. // PatientId: 0,
  4489. // SystemTime: record_time,
  4490. // ConsumableType: 12,
  4491. // IsSys: 0,
  4492. // WarehousingOrder: "",
  4493. // WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  4494. // WarehouseOutId: goods.WarehouseOutId,
  4495. // IsEdit: 1,
  4496. // CancelStockId: 0,
  4497. // CancelOrderNumber: "",
  4498. // Manufacturer: warehouse.Manufacturer,
  4499. // Dealer: warehouse.Dealer,
  4500. // Creator: creator,
  4501. // UpdateCreator: 0,
  4502. // Status: 1,
  4503. // Ctime: time.Now().Unix(),
  4504. // Mtime: 0,
  4505. // Price: warehouse.PackingPrice,
  4506. // WarehousingDetailId: 0,
  4507. // WarehouseOutDetailId: warehouseOutInfo.ID,
  4508. // CancelOutDetailId: 0,
  4509. // ProductDate: warehouse.ProductDate,
  4510. // ExpireDate: warehouse.ExpiryDate,
  4511. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4512. // StorehouseId: goods.StorehouseId,
  4513. // SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  4514. // }
  4515. //
  4516. // //创建出库明细
  4517. // CreateStockFlowOne(stockFlow)
  4518. //
  4519. // //入库
  4520. // warehouseInfo := &models.WarehousingInfo{
  4521. // WarehousingOrder: warehouseInfo.WarehousingOrder,
  4522. // WarehousingId: warehouseInfo.WarehousingId,
  4523. // GoodId: warehouse.GoodId,
  4524. // Number: warehouse.Number,
  4525. // GoodTypeId: warehouse.GoodTypeId,
  4526. // ProductDate: warehouse.ProductDate,
  4527. // ExpiryDate: warehouse.ExpiryDate,
  4528. // WarehousingCount: warehouse.StockCount,
  4529. // Price: warehouse.Price,
  4530. // TotalPrice: 0,
  4531. // Status: 1,
  4532. // Ctime: warehouse.Ctime,
  4533. // Remark: goods.Remark,
  4534. // OrgId: warehouse.OrgId,
  4535. // Type: 1,
  4536. // Manufacturer: warehouse.Manufacturer,
  4537. // StockCount: warehouse.StockCount,
  4538. // Dealer: warehouse.Dealer,
  4539. // LicenseNumber: "",
  4540. // PackingPrice: warehouse.PackingPrice,
  4541. // StorehouseId: warehouseInfo.StorehouseId,
  4542. // SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  4543. // IsCheck: 1,
  4544. // }
  4545. //
  4546. // AddWarehosingInfo(warehouseInfo)
  4547. //
  4548. // stockInfoFlow := models.VmStockFlow{
  4549. // WarehousingId: warehouseInfo.WarehousingId,
  4550. // GoodId: goods.GoodId,
  4551. // Number: warehouse.Number,
  4552. // LicenseNumber: goods.LicenseNumber,
  4553. // Count: warehouse.StockCount,
  4554. // UserOrgId: goods.OrgId,
  4555. // PatientId: goods.PatientId,
  4556. // SystemTime: record_time,
  4557. // ConsumableType: 13,
  4558. // IsSys: goods.IsSys,
  4559. // WarehousingOrder: warehouseInfo.WarehousingOrder,
  4560. // WarehouseOutId: warehouseOut.ID,
  4561. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  4562. // IsEdit: 1,
  4563. // CancelStockId: 0,
  4564. // CancelOrderNumber: "",
  4565. // Manufacturer: warehouse.Manufacturer,
  4566. // Dealer: warehouse.Dealer,
  4567. // Creator: creator,
  4568. // UpdateCreator: 0,
  4569. // Status: 1,
  4570. // Ctime: time.Now().Unix(),
  4571. // Mtime: 0,
  4572. // Price: warehouse.Price,
  4573. // WarehousingDetailId: 0,
  4574. // WarehouseOutDetailId: warehouseOutInfo.ID,
  4575. // CancelOutDetailId: 0,
  4576. // ProductDate: warehouse.ProductDate,
  4577. // ExpireDate: warehouse.ExpiryDate,
  4578. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4579. // StorehouseId: warehouseInfo.StorehouseId,
  4580. // SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  4581. // }
  4582. //
  4583. // //创建出库流水
  4584. // CreateStockFlowOne(stockInfoFlow)
  4585. // // 出库完成后,要将该批次库存清零
  4586. // warehouse.StockCount = 0
  4587. // warehouse.Mtime = time.Now().Unix()
  4588. //
  4589. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  4590. //
  4591. // if errThree != nil {
  4592. // return errThree
  4593. // }
  4594. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  4595. // goods.Count = deliver_number - stock_number
  4596. //
  4597. // ConsumablesDeliveryTwelve(orgID, record_time, goods, warehouseOut, count, creator, warehouseInfo)
  4598. //
  4599. // }
  4600. // return nil
  4601. //}
  4602. //
  4603. //func AddWarehosingInfo(info *models.WarehousingInfo) error {
  4604. //
  4605. // err = XTWriteDB().Create(&info).Error
  4606. // return err
  4607. //}
  4608. //
  4609. //// 药品出库 递归方式
  4610. //func AutoDrugDeliverInfoTwelve(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo, drugWarehouse *models.DrugWarehouseInfo) (err error) {
  4611. //
  4612. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  4613. // var deliver_number int64 = 0
  4614. // var stock_number int64 = 0
  4615. //
  4616. // if advice.CountUnit == drup.MaxUnit {
  4617. // deliver_number = prescribingNumber * drup.MinNumber
  4618. // } else {
  4619. // deliver_number = prescribingNumber
  4620. // }
  4621. //
  4622. // // 根据先进先出原则,查询最先入库的批次,进行出库
  4623. // // 如果没有对应的库存,则报错
  4624. // //开启事物
  4625. // storeConfig, _ := GetAllStoreHouseConfig(orgID)
  4626. // lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  4627. // if lastWarehouse.StockMinNumber >= drup.MinNumber {
  4628. // var stockMax int64
  4629. // var stockMin int64
  4630. // stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  4631. // stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  4632. // ChangeMaxNumber(lastWarehouse.ID, stockMax)
  4633. // UpdateMinNumber(lastWarehouse.ID, stockMin)
  4634. // }
  4635. //
  4636. // warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  4637. //
  4638. // if err != nil {
  4639. //
  4640. // return err
  4641. // }
  4642. //
  4643. // // 将该批次的剩余库存数量转换为拆零数量
  4644. //
  4645. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  4646. //
  4647. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  4648. // if stock_number >= deliver_number {
  4649. //
  4650. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4651. // WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  4652. // WarehouseOutId: advice.WarehouseOutId,
  4653. // Status: 1,
  4654. // Ctime: time.Now().Unix(),
  4655. // Remark: advice.Remark,
  4656. // OrgId: orgID,
  4657. // Type: 1,
  4658. // Manufacturer: warehouse.Manufacturer,
  4659. // Dealer: warehouse.Dealer,
  4660. // IsSys: 12,
  4661. // SysRecordTime: advice.Ctime,
  4662. // DrugId: advice.DrugId,
  4663. // ExpiryDate: warehouse.ExpiryDate,
  4664. // ProductDate: warehouse.ProductDate,
  4665. // Number: warehouse.Number,
  4666. // BatchNumber: warehouse.BatchNumber,
  4667. // Count: deliver_number,
  4668. // RetailPrice: warehouse.RetailPrice,
  4669. // Price: warehouse.RetailPrice,
  4670. // WarehouseInfoId: warehouse.ID,
  4671. // CountUnit: drup.MinUnit,
  4672. // AdviceId: advice.ID,
  4673. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4674. // StorehouseId: advice.StorehouseId,
  4675. // SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  4676. // IsCheck: 1,
  4677. // }
  4678. //
  4679. // warehouseOutInfo.Count = prescribingNumber
  4680. // warehouseOutInfo.CountUnit = advice.CountUnit
  4681. //
  4682. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  4683. // if errOne != nil {
  4684. // return errOne
  4685. // }
  4686. //
  4687. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4688. // drugflow := models.DrugFlow{
  4689. // WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  4690. // WarehouseOutId: advice.WarehouseOutId,
  4691. // DrugId: advice.DrugId,
  4692. // Number: warehouse.Number,
  4693. // ProductDate: warehouse.ProductDate,
  4694. // ExpireDate: warehouse.ExpiryDate,
  4695. // Count: deliver_number,
  4696. // Price: warehouse.RetailPrice,
  4697. // Status: 1,
  4698. // Ctime: time.Now().Unix(),
  4699. // UserOrgId: advice.OrgId,
  4700. // Manufacturer: warehouse.Manufacturer,
  4701. // Dealer: warehouse.Dealer,
  4702. // BatchNumber: warehouse.BatchNumber,
  4703. // MaxUnit: drup.MinUnit,
  4704. // ConsumableType: 12,
  4705. // IsEdit: 1,
  4706. // Creator: 0,
  4707. // IsSys: 12,
  4708. // WarehouseOutDetailId: drugWareInfo.ID,
  4709. // AdviceId: advice.ID,
  4710. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4711. // StorehouseId: advice.StorehouseId,
  4712. // SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  4713. // }
  4714. //
  4715. // CreateDrugFlowOne(drugflow)
  4716. // // 出库完成后,要减去对应批次的库存数量
  4717. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  4718. // var maxNumber int64 = 0
  4719. // var minNumber int64 = 0
  4720. // if advice.CountUnit == drup.MinUnit {
  4721. // maxNumber = prescribingNumber / drup.MinNumber
  4722. // minNumber = prescribingNumber % drup.MinNumber
  4723. // if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  4724. // minNumber = maxNumber
  4725. // }
  4726. // } else {
  4727. // maxNumber = prescribingNumber
  4728. // }
  4729. //
  4730. // if warehouse.StockMaxNumber < maxNumber {
  4731. //
  4732. // return errors.New("库存数量不足")
  4733. // }
  4734. //
  4735. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  4736. // warehouse.Mtime = time.Now().Unix()
  4737. //
  4738. // if warehouse.StockMinNumber < minNumber {
  4739. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  4740. // warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  4741. // } else {
  4742. // warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  4743. // }
  4744. //
  4745. // if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
  4746. // warehouse.StockMinNumber = 0
  4747. // }
  4748. //
  4749. // if warehouse.StockMaxNumber < 0 {
  4750. // return errors.New("库存数量不足")
  4751. // }
  4752. //
  4753. // ////扣减库存
  4754. // //if(warehouse.StockMaxNumber > 0){
  4755. // // var sum_count int64
  4756. // // sum_count = maxNumber * drup.MinNumber
  4757. // // ModifyDrugReduceInformationSix(warehouse.DrugId,sum_count,warehouse.OrgId,warehouse.RetailPrice)
  4758. // //}
  4759. // //if(warehouse.StockMinNumber > 0){
  4760. // // var sum_count int64
  4761. // // sum_count = minNumber
  4762. // // ModifyDrugReduceInformationSix(warehouse.DrugId,sum_count,warehouse.OrgId,warehouse.RetailPrice)
  4763. // //}
  4764. //
  4765. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4766. //
  4767. // if errThree != nil {
  4768. // return errThree
  4769. // }
  4770. //
  4771. // //入库
  4772. // drugInfo := models.DrugWarehouseInfo{
  4773. // WarehousingOrder: drugWarehouse.WarehousingOrder,
  4774. // WarehousingId: drugWarehouse.WarehousingId,
  4775. // DrugId: warehouse.DrugId,
  4776. // Number: warehouse.Number,
  4777. // ProductDate: warehouse.ProductDate,
  4778. // ExpiryDate: warehouse.ExpiryDate,
  4779. // WarehousingCount: deliver_number,
  4780. // Price: warehouse.RetailPrice,
  4781. // TotalPrice: warehouse.TotalPrice,
  4782. // Status: 1,
  4783. // Ctime: warehouse.Ctime,
  4784. // Remark: "",
  4785. // OrgId: warehouse.OrgId,
  4786. // Type: 13,
  4787. // Manufacturer: warehouse.Manufacturer,
  4788. // Dealer: warehouse.Dealer,
  4789. // StockMaxNumber: 0,
  4790. // StockMinNumber: deliver_number,
  4791. // BatchNumber: warehouse.BatchNumber,
  4792. // MaxUnit: drup.MinUnit,
  4793. // MinUnit: drup.MinUnit,
  4794. // RetailPrice: warehouse.RetailPrice,
  4795. // StorehouseId: drugWarehouse.StorehouseId,
  4796. // SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  4797. // IsCheck: 1,
  4798. // }
  4799. //
  4800. // CreateDrugWarehouseInfo(drugInfo)
  4801. //
  4802. // flow := models.DrugFlow{
  4803. // WarehousingId: drugWarehouse.WarehousingId,
  4804. // DrugId: warehouse.DrugId,
  4805. // Number: warehouse.Number,
  4806. // BatchNumber: warehouse.BatchNumber,
  4807. // Count: deliver_number,
  4808. // UserOrgId: warehouse.OrgId,
  4809. // PatientId: 0,
  4810. // SystemTime: 0,
  4811. // ConsumableType: 13,
  4812. // IsSys: 13,
  4813. // WarehousingOrder: drugWarehouse.WarehousingOrder,
  4814. // WarehouseOutId: 0,
  4815. // WarehouseOutOrderNumber: "",
  4816. // IsEdit: 0,
  4817. // CancelStockId: 0,
  4818. // CancelOrderNumber: "",
  4819. // Manufacturer: warehouse.Manufacturer,
  4820. // Dealer: warehouse.Dealer,
  4821. // Creator: 0,
  4822. // UpdateCreator: 0,
  4823. // Status: 1,
  4824. // Ctime: time.Now().Unix(),
  4825. // Mtime: 0,
  4826. // Price: warehouse.RetailPrice,
  4827. // WarehousingDetailId: warehouse.ID,
  4828. // WarehouseOutDetailId: 0,
  4829. // CancelOutDetailId: 0,
  4830. // ExpireDate: warehouse.ExpiryDate,
  4831. // ProductDate: warehouse.ProductDate,
  4832. // MaxUnit: drup.MinUnit,
  4833. // MinUnit: drup.MinUnit,
  4834. // StockMaxNumber: 0,
  4835. // StockMinNumber: deliver_number,
  4836. // StorehouseId: drugWarehouse.StorehouseId,
  4837. // SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  4838. // }
  4839. //
  4840. // CreateDrugFlowOne(flow)
  4841. //
  4842. // return nil
  4843. // } else {
  4844. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  4845. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4846. // WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  4847. // WarehouseOutId: advice.WarehouseOutId,
  4848. // Status: 1,
  4849. // Ctime: time.Now().Unix(),
  4850. // Remark: advice.Remark,
  4851. // OrgId: orgID,
  4852. // Type: 1,
  4853. // Manufacturer: advice.Manufacturer,
  4854. // Dealer: warehouse.Dealer,
  4855. // IsSys: 12,
  4856. // SysRecordTime: advice.Ctime,
  4857. // DrugId: advice.DrugId,
  4858. // ExpiryDate: warehouse.ExpiryDate,
  4859. // ProductDate: warehouse.ProductDate,
  4860. // Number: warehouse.Number,
  4861. // BatchNumber: warehouse.BatchNumber,
  4862. // Count: stock_number,
  4863. // RetailPrice: advice.RetailPrice,
  4864. // Price: warehouse.RetailPrice,
  4865. // WarehouseInfoId: warehouse.ID,
  4866. // CountUnit: drup.MinUnit,
  4867. // AdviceId: advice.ID,
  4868. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4869. // StorehouseId: advice.StorehouseId,
  4870. // SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  4871. // IsCheck: 1,
  4872. // }
  4873. // warehouseOutInfo.Count = stock_number
  4874. //
  4875. // //warehouseOutInfo.CountUnit = advice.CountUnit
  4876. //
  4877. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  4878. // if errOne != nil {
  4879. // return errOne
  4880. // }
  4881. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  4882. // drugflow := models.DrugFlow{
  4883. // WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  4884. // WarehouseOutId: advice.WarehouseOutId,
  4885. // DrugId: advice.DrugId,
  4886. // Number: warehouse.Number,
  4887. // ProductDate: warehouse.ProductDate,
  4888. // ExpireDate: warehouse.ExpiryDate,
  4889. // Count: stock_number,
  4890. // Price: warehouse.RetailPrice,
  4891. // Status: 1,
  4892. // Ctime: time.Now().Unix(),
  4893. // UserOrgId: advice.OrgId,
  4894. // Manufacturer: warehouse.Manufacturer,
  4895. // Dealer: warehouse.Dealer,
  4896. // BatchNumber: warehouse.BatchNumber,
  4897. // MaxUnit: drup.MinUnit,
  4898. // ConsumableType: 12,
  4899. // IsEdit: 1,
  4900. // Creator: 0,
  4901. // IsSys: 12,
  4902. // WarehouseOutDetailId: drugWareInfo.ID,
  4903. // AdviceId: advice.ID,
  4904. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4905. // StorehouseId: advice.StorehouseId,
  4906. // SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  4907. // }
  4908. // CreateDrugFlowOne(drugflow)
  4909. // // 出库完成后,要将该批次库存清零
  4910. //
  4911. // //入库
  4912. // drugInfo := models.DrugWarehouseInfo{
  4913. // WarehousingOrder: drugWarehouse.WarehousingOrder,
  4914. // WarehousingId: drugWarehouse.WarehousingId,
  4915. // DrugId: warehouse.DrugId,
  4916. // Number: warehouse.Number,
  4917. // ProductDate: warehouse.ProductDate,
  4918. // ExpiryDate: warehouse.ExpiryDate,
  4919. // WarehousingCount: stock_number,
  4920. // Price: warehouse.RetailPrice,
  4921. // TotalPrice: warehouse.TotalPrice,
  4922. // Status: 1,
  4923. // Ctime: warehouse.Ctime,
  4924. // Remark: "",
  4925. // OrgId: warehouse.OrgId,
  4926. // Type: 13,
  4927. // Manufacturer: warehouse.Manufacturer,
  4928. // Dealer: warehouse.Dealer,
  4929. // StockMaxNumber: 0,
  4930. // StockMinNumber: stock_number,
  4931. // RetailTotalPrice: 0,
  4932. // BatchNumber: warehouse.BatchNumber,
  4933. // MaxUnit: drup.MinUnit,
  4934. // MinUnit: drup.MinUnit,
  4935. // RetailPrice: warehouse.RetailPrice,
  4936. // StorehouseId: drugWarehouse.StorehouseId,
  4937. // SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  4938. // }
  4939. //
  4940. // CreateDrugWarehouseInfo(drugInfo)
  4941. //
  4942. // flow := models.DrugFlow{
  4943. // WarehousingId: drugWarehouse.WarehousingId,
  4944. // DrugId: warehouse.DrugId,
  4945. // Number: warehouse.Number,
  4946. // BatchNumber: warehouse.BatchNumber,
  4947. // Count: stock_number,
  4948. // UserOrgId: warehouse.OrgId,
  4949. // PatientId: 0,
  4950. // SystemTime: 0,
  4951. // ConsumableType: 13,
  4952. // IsSys: 13,
  4953. // WarehousingOrder: drugWarehouse.WarehousingOrder,
  4954. // WarehouseOutId: 0,
  4955. // WarehouseOutOrderNumber: "",
  4956. // IsEdit: 0,
  4957. // CancelStockId: 0,
  4958. // CancelOrderNumber: "",
  4959. // Manufacturer: warehouse.Manufacturer,
  4960. // Dealer: warehouse.Dealer,
  4961. // Creator: 0,
  4962. // UpdateCreator: 0,
  4963. // Status: 1,
  4964. // Ctime: time.Now().Unix(),
  4965. // Mtime: 0,
  4966. // Price: warehouse.RetailPrice,
  4967. // WarehousingDetailId: warehouse.ID,
  4968. // WarehouseOutDetailId: 0,
  4969. // CancelOutDetailId: 0,
  4970. // ExpireDate: warehouse.ExpiryDate,
  4971. // ProductDate: warehouse.ProductDate,
  4972. // MaxUnit: drup.MinUnit,
  4973. // MinUnit: drup.MinUnit,
  4974. // StockMaxNumber: 0,
  4975. // StockMinNumber: stock_number,
  4976. // StorehouseId: drugWarehouse.StorehouseId,
  4977. // SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  4978. // }
  4979. //
  4980. // CreateDrugFlowOne(flow)
  4981. //
  4982. // warehouse.StockMaxNumber = 0
  4983. // warehouse.StockMinNumber = 0
  4984. // warehouse.Mtime = time.Now().Unix()
  4985. //
  4986. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  4987. // if errThree != nil {
  4988. // return errThree
  4989. // }
  4990. //
  4991. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  4992. // prescribingNumber_two_temp := deliver_number - stock_number
  4993. //
  4994. // advice.CountUnit = drup.MinUnit
  4995. //
  4996. // AutoDrugDeliverInfoTwelve(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, drugWarehouse)
  4997. // }
  4998. //
  4999. // return
  5000. //}
  5001. //
  5002. //// 药品手动出库 递归方式
  5003. //func AutoDrugDeliverInfoTwenty(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  5004. //
  5005. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  5006. // var deliver_number int64 = 0
  5007. // var stock_number int64 = 0
  5008. //
  5009. // if advice.CountUnit == drup.MaxUnit {
  5010. // deliver_number = prescribingNumber * drup.MinNumber
  5011. // } else {
  5012. // deliver_number = prescribingNumber
  5013. // }
  5014. //
  5015. // // 根据先进先出原则,查询最先入库的批次,进行出库
  5016. // // 如果没有对应的库存,则报错
  5017. // //开启事物
  5018. // lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  5019. // if lastWarehouse.StockMinNumber >= drup.MinNumber {
  5020. // var stockMax int64
  5021. // var stockMin int64
  5022. // stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  5023. // stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  5024. // ChangeMaxNumber(lastWarehouse.ID, stockMax)
  5025. // UpdateMinNumber(lastWarehouse.ID, stockMin)
  5026. // }
  5027. //
  5028. // warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  5029. //
  5030. // if err != nil {
  5031. //
  5032. // return err
  5033. // }
  5034. //
  5035. // // 将该批次的剩余库存数量转换为拆零数量
  5036. //
  5037. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  5038. //
  5039. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  5040. // if stock_number >= deliver_number {
  5041. //
  5042. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  5043. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  5044. // WarehouseOutId: warehouseout.ID,
  5045. // Status: 1,
  5046. // Ctime: time.Now().Unix(),
  5047. // Remark: advice.Remark,
  5048. // OrgId: orgID,
  5049. // Type: 1,
  5050. // Manufacturer: advice.Manufacturer,
  5051. // Dealer: warehouse.Dealer,
  5052. // IsSys: 0,
  5053. // SysRecordTime: advice.Ctime,
  5054. // DrugId: advice.DrugId,
  5055. // ExpiryDate: advice.ExpiryDate,
  5056. // ProductDate: advice.ProductDate,
  5057. // Number: advice.Number,
  5058. // BatchNumber: warehouse.BatchNumber,
  5059. // Count: deliver_number,
  5060. // RetailPrice: advice.RetailPrice,
  5061. // Price: warehouse.RetailPrice,
  5062. // WarehouseInfoId: warehouse.ID,
  5063. // CountUnit: drup.MinUnit,
  5064. // AdviceId: advice.ID,
  5065. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5066. // StorehouseId: advice.StorehouseId,
  5067. // AdminUserId: advice.AdminUserId,
  5068. // IsCheck: 1,
  5069. // }
  5070. //
  5071. // if warehouse.RetailPrice == 0 {
  5072. // warehouseOutInfo.Price = advice.Price
  5073. // }
  5074. //
  5075. // warehouseOutInfo.Count = prescribingNumber
  5076. // warehouseOutInfo.CountUnit = advice.CountUnit
  5077. //
  5078. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  5079. // if errOne != nil {
  5080. // return errOne
  5081. // }
  5082. //
  5083. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  5084. // drugflow := models.DrugFlow{
  5085. // WarehouseOutId: warehouseout.ID,
  5086. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  5087. // DrugId: advice.DrugId,
  5088. // Number: warehouse.Number,
  5089. // ProductDate: advice.ProductDate,
  5090. // ExpireDate: advice.ExpiryDate,
  5091. // Count: deliver_number,
  5092. // Price: warehouse.RetailPrice,
  5093. // Status: 1,
  5094. // Ctime: time.Now().Unix(),
  5095. // UserOrgId: advice.OrgId,
  5096. // Manufacturer: advice.Manufacturer,
  5097. // Dealer: advice.Dealer,
  5098. // BatchNumber: warehouse.BatchNumber,
  5099. // MaxUnit: drup.MinUnit,
  5100. // ConsumableType: 2,
  5101. // IsEdit: 1,
  5102. // Creator: 0,
  5103. // IsSys: 0,
  5104. // WarehouseOutDetailId: drugWareInfo.ID,
  5105. // AdviceId: advice.ID,
  5106. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5107. // StorehouseId: advice.StorehouseId,
  5108. // AdminUserId: advice.AdminUserId,
  5109. // }
  5110. // if warehouse.RetailPrice == 0 {
  5111. // drugflow.Price = advice.Price
  5112. // }
  5113. //
  5114. // CreateDrugFlowOne(drugflow)
  5115. // // 出库完成后,要减去对应批次的库存数量
  5116. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  5117. // var maxNumber int64 = 0
  5118. // var minNumber int64 = 0
  5119. // //var stock_max_number int64 = 0
  5120. // //stock_max_number = warehouse.StockMaxNumber
  5121. //
  5122. // maxNumber = deliver_number / drup.MinNumber
  5123. // minNumber = deliver_number % drup.MinNumber
  5124. //
  5125. // if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  5126. // minNumber = maxNumber
  5127. // }
  5128. //
  5129. // if drup.MaxUnit != drup.MinUnit {
  5130. // if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  5131. // return errors.New("库存数量不足")
  5132. // }
  5133. // }
  5134. //
  5135. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  5136. //
  5137. // if warehouse.StockMaxNumber < 0 {
  5138. // warehouse.StockMaxNumber = 0
  5139. // }
  5140. // if warehouse.StockMinNumber < 0 {
  5141. // warehouse.StockMinNumber = 0
  5142. // }
  5143. //
  5144. // warehouse.Mtime = time.Now().Unix()
  5145. //
  5146. // if warehouse.StockMinNumber < minNumber {
  5147. //
  5148. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  5149. // warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  5150. // } else {
  5151. // if minNumber > 0 {
  5152. // if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  5153. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  5154. // } else {
  5155. // warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  5156. // }
  5157. // }
  5158. //
  5159. // if minNumber == 0 && maxNumber != 1 {
  5160. // if warehouse.StockMinNumber > 0 {
  5161. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  5162. // }
  5163. //
  5164. // }
  5165. //
  5166. // }
  5167. //
  5168. // if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  5169. // if (warehouse.StockMinNumber - deliver_number) > 0 {
  5170. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  5171. //
  5172. // //if warehouse.StockMinNumber == 0 {
  5173. // // warehouse.StockMaxNumber = stock_max_number
  5174. // //}
  5175. // }
  5176. //
  5177. // }
  5178. //
  5179. // if drup.MaxUnit != drup.MinUnit {
  5180. // if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  5181. // return errors.New("库存数量不足")
  5182. // }
  5183. // }
  5184. // if warehouse.StockMinNumber <= 0 {
  5185. // warehouse.StockMinNumber = 0
  5186. // }
  5187. //
  5188. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  5189. //
  5190. // if errThree != nil {
  5191. // return errThree
  5192. // }
  5193. //
  5194. // //查询默认仓库
  5195. // houseConfig, _ := GetAllStoreHouseConfig(orgID)
  5196. // //查询默认仓库剩余多少库存
  5197. // list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  5198. // var sum_count int64
  5199. // var sum_in_count int64
  5200. // for _, it := range list {
  5201. // baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  5202. // if it.MaxUnit == baseDrug.MaxUnit {
  5203. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  5204. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  5205. // sum_count += it.StockMaxNumber + it.StockMinNumber
  5206. // sum_in_count += it.WarehousingCount
  5207. // }
  5208. // }
  5209. // UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  5210. //
  5211. // return nil
  5212. //
  5213. // } else {
  5214. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  5215. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  5216. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  5217. // WarehouseOutId: warehouseout.ID,
  5218. // Status: 1,
  5219. // Ctime: time.Now().Unix(),
  5220. // Remark: advice.Remark,
  5221. // OrgId: orgID,
  5222. // Type: 1,
  5223. // Manufacturer: advice.Manufacturer,
  5224. // Dealer: warehouse.Dealer,
  5225. // IsSys: 0,
  5226. // SysRecordTime: advice.Ctime,
  5227. // DrugId: advice.DrugId,
  5228. // ExpiryDate: warehouse.ExpiryDate,
  5229. // ProductDate: warehouse.ProductDate,
  5230. // Number: warehouse.Number,
  5231. // BatchNumber: warehouse.BatchNumber,
  5232. // Count: stock_number,
  5233. // RetailPrice: advice.RetailPrice,
  5234. // Price: warehouse.RetailPrice,
  5235. // WarehouseInfoId: warehouse.ID,
  5236. // CountUnit: drup.MinUnit,
  5237. // AdviceId: advice.ID,
  5238. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5239. // StorehouseId: advice.StorehouseId,
  5240. // AdminUserId: advice.AdminUserId,
  5241. // IsCheck: 1,
  5242. // }
  5243. // if warehouse.RetailPrice == 0 {
  5244. // warehouseOutInfo.Price = advice.Price
  5245. // }
  5246. // warehouseOutInfo.Count = stock_number
  5247. //
  5248. // //warehouseOutInfo.CountUnit = advice.CountUnit
  5249. //
  5250. // errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  5251. // if errOne != nil {
  5252. // return errOne
  5253. // }
  5254. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  5255. // drugflow := models.DrugFlow{
  5256. // WarehouseOutId: warehouseout.ID,
  5257. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  5258. // DrugId: advice.DrugId,
  5259. // Number: warehouse.Number,
  5260. // ProductDate: advice.ProductDate,
  5261. // ExpireDate: advice.ExpiryDate,
  5262. // Count: stock_number,
  5263. // Price: warehouse.RetailPrice,
  5264. // Status: 1,
  5265. // Ctime: time.Now().Unix(),
  5266. // UserOrgId: advice.OrgId,
  5267. // Manufacturer: advice.Manufacturer,
  5268. // Dealer: advice.Dealer,
  5269. // BatchNumber: warehouse.BatchNumber,
  5270. // MaxUnit: drup.MinUnit,
  5271. // ConsumableType: 2,
  5272. // IsEdit: 1,
  5273. // Creator: 0,
  5274. // IsSys: 0,
  5275. // WarehouseOutDetailId: drugWareInfo.ID,
  5276. // AdviceId: advice.ID,
  5277. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5278. // StorehouseId: advice.StorehouseId,
  5279. // AdminUserId: advice.AdminUserId,
  5280. // }
  5281. // if warehouse.RetailPrice == 0 {
  5282. // drugflow.Price = advice.Price
  5283. // }
  5284. // CreateDrugFlowOne(drugflow)
  5285. // // 出库完成后,要将该批次库存清零
  5286. // //扣减库存
  5287. //
  5288. // warehouse.StockMaxNumber = 0
  5289. // warehouse.StockMinNumber = 0
  5290. // warehouse.Mtime = time.Now().Unix()
  5291. //
  5292. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  5293. //
  5294. // //查询默认仓库
  5295. // houseConfig, _ := GetAllStoreHouseConfig(orgID)
  5296. // //查询默认仓库剩余多少库存
  5297. // list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  5298. // var sum_count int64
  5299. // var sum_in_count int64
  5300. // for _, it := range list {
  5301. // baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  5302. // if it.MaxUnit == baseDrug.MaxUnit {
  5303. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  5304. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  5305. // sum_count += it.StockMaxNumber + it.StockMinNumber
  5306. // sum_in_count += it.WarehousingCount
  5307. // }
  5308. // }
  5309. // UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  5310. // if errThree != nil {
  5311. // return errThree
  5312. // }
  5313. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  5314. // prescribingNumber_two_temp := deliver_number - stock_number
  5315. //
  5316. // advice.CountUnit = drup.MinUnit
  5317. //
  5318. // AutoDrugDeliverInfoTwenty(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  5319. // }
  5320. //
  5321. // return
  5322. //}
  5323. //
  5324. //// 药品手动出库 递归方式
  5325. //func AutoDrugDeliverInfoTwentyOne(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  5326. // //开事务
  5327. // //tx := XTWriteDB().Begin()
  5328. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  5329. // var deliver_number int64 = 0
  5330. // var stock_number int64 = 0
  5331. //
  5332. // if advice.CountUnit == drup.MaxUnit {
  5333. // deliver_number = prescribingNumber * drup.MinNumber
  5334. // } else {
  5335. // deliver_number = prescribingNumber
  5336. // }
  5337. //
  5338. // // 根据先进先出原则,查询最先入库的批次,进行出库
  5339. // // 如果没有对应的库存,则报错
  5340. // //开启事物
  5341. // storeConfig, _ := GetAllStoreHouseConfig(orgID)
  5342. // lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  5343. // if lastWarehouse.StockMinNumber >= drup.MinNumber {
  5344. // var stockMax int64
  5345. // var stockMin int64
  5346. // stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  5347. // stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  5348. // ChangeMaxNumber(lastWarehouse.ID, stockMax)
  5349. // UpdateMinNumber(lastWarehouse.ID, stockMin)
  5350. // }
  5351. // warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  5352. //
  5353. // if err != nil {
  5354. //
  5355. // return err
  5356. // }
  5357. //
  5358. // // 将该批次的剩余库存数量转换为拆零数量
  5359. //
  5360. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  5361. //
  5362. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  5363. // if stock_number >= deliver_number {
  5364. //
  5365. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  5366. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  5367. // WarehouseOutId: warehouseout.ID,
  5368. // Status: 1,
  5369. // Ctime: time.Now().Unix(),
  5370. // Remark: advice.Remark,
  5371. // OrgId: orgID,
  5372. // Type: 1,
  5373. // Manufacturer: advice.Manufacturer,
  5374. // Dealer: warehouse.Dealer,
  5375. // IsSys: 0,
  5376. // SysRecordTime: advice.Ctime,
  5377. // DrugId: advice.DrugId,
  5378. // ExpiryDate: advice.ExpiryDate,
  5379. // ProductDate: advice.ProductDate,
  5380. // Number: advice.Number,
  5381. // BatchNumber: warehouse.BatchNumber,
  5382. // Count: deliver_number,
  5383. // RetailPrice: advice.RetailPrice,
  5384. // Price: advice.Price,
  5385. // WarehouseInfoId: warehouse.ID,
  5386. // CountUnit: drup.MinUnit,
  5387. // AdviceId: advice.ID,
  5388. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5389. // StorehouseId: advice.StorehouseId,
  5390. // AdminUserId: advice.AdminUserId,
  5391. // LastPrice: warehouse.Price,
  5392. // StockCount: advice.StockCount,
  5393. // IsCheck: 1,
  5394. // }
  5395. //
  5396. // warehouseOutInfo.Count = prescribingNumber
  5397. // warehouseOutInfo.CountUnit = advice.CountUnit
  5398. //
  5399. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  5400. // drugflow := models.DrugFlow{
  5401. // WarehouseOutId: warehouseout.ID,
  5402. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  5403. // DrugId: advice.DrugId,
  5404. // Number: warehouse.Number,
  5405. // ProductDate: advice.ProductDate,
  5406. // ExpireDate: advice.ExpiryDate,
  5407. // Count: deliver_number,
  5408. // Price: advice.Price,
  5409. // Status: 1,
  5410. // Ctime: time.Now().Unix(),
  5411. // UserOrgId: advice.OrgId,
  5412. // Manufacturer: advice.Manufacturer,
  5413. // Dealer: advice.Dealer,
  5414. // BatchNumber: warehouse.BatchNumber,
  5415. // MaxUnit: drup.MinUnit,
  5416. // ConsumableType: 2,
  5417. // IsEdit: 1,
  5418. // Creator: 0,
  5419. // IsSys: 0,
  5420. // WarehouseOutDetailId: drugWareInfo.ID,
  5421. // AdviceId: advice.ID,
  5422. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5423. // StorehouseId: advice.StorehouseId,
  5424. // AdminUserId: advice.AdminUserId,
  5425. // LastPrice: warehouse.Price,
  5426. // StockCount: advice.StockCount,
  5427. // }
  5428. // if warehouse.RetailPrice == 0 {
  5429. // drugflow.Price = advice.Price
  5430. // }
  5431. //
  5432. // CreateDrugFlowOne(drugflow)
  5433. // // 出库完成后,要减去对应批次的库存数量
  5434. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  5435. // var maxNumber int64 = 0
  5436. // var minNumber int64 = 0
  5437. // //var stock_max_number int64 = 0
  5438. // //stock_max_number = warehouse.StockMaxNumber
  5439. //
  5440. // maxNumber = deliver_number / drup.MinNumber
  5441. // minNumber = deliver_number % drup.MinNumber
  5442. //
  5443. // if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  5444. // minNumber = maxNumber
  5445. // }
  5446. //
  5447. // if drup.MaxUnit != drup.MinUnit {
  5448. // if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  5449. // return errors.New("库存数量不足")
  5450. // }
  5451. // }
  5452. //
  5453. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  5454. //
  5455. // if warehouse.StockMaxNumber < 0 {
  5456. // warehouse.StockMaxNumber = 0
  5457. // }
  5458. // if warehouse.StockMinNumber < 0 {
  5459. // warehouse.StockMinNumber = 0
  5460. // }
  5461. //
  5462. // warehouse.Mtime = time.Now().Unix()
  5463. //
  5464. // if warehouse.StockMinNumber < minNumber {
  5465. //
  5466. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  5467. // warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  5468. // } else {
  5469. // if minNumber > 0 {
  5470. // if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  5471. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  5472. // } else {
  5473. // warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  5474. // }
  5475. // }
  5476. //
  5477. // if minNumber == 0 && maxNumber != 1 {
  5478. // if warehouse.StockMinNumber > 0 {
  5479. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  5480. // }
  5481. //
  5482. // }
  5483. //
  5484. // }
  5485. //
  5486. // if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  5487. // if (warehouse.StockMinNumber - deliver_number) > 0 {
  5488. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  5489. //
  5490. // //if warehouse.StockMinNumber == 0 {
  5491. // // warehouse.StockMaxNumber = stock_max_number
  5492. // //}
  5493. // }
  5494. //
  5495. // }
  5496. //
  5497. // if drup.MaxUnit != drup.MinUnit {
  5498. // if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  5499. // return errors.New("库存数量不足")
  5500. // }
  5501. // }
  5502. // if warehouse.StockMinNumber <= 0 {
  5503. // warehouse.StockMinNumber = 0
  5504. // }
  5505. //
  5506. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  5507. // fmt.Println(errThree)
  5508. // //if errThree != nil {
  5509. // // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  5510. // // tx.Rollback()
  5511. // //} else {
  5512. // // tx.Commit()
  5513. // //}
  5514. //
  5515. // //查询默认仓库
  5516. // houseConfig, _ := GetAllStoreHouseConfig(orgID)
  5517. // //查询默认仓库剩余多少库存
  5518. // list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  5519. // var sum_count int64
  5520. // var sum_in_count int64
  5521. // for _, it := range list {
  5522. // baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  5523. // if it.MaxUnit == baseDrug.MaxUnit {
  5524. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  5525. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  5526. // sum_count += it.StockMaxNumber + it.StockMinNumber
  5527. // sum_in_count += it.WarehousingCount
  5528. // }
  5529. // }
  5530. // UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  5531. //
  5532. // return nil
  5533. //
  5534. // } else {
  5535. // // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  5536. // warehouseOutInfo := &models.DrugWarehouseOutInfo{
  5537. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  5538. // WarehouseOutId: warehouseout.ID,
  5539. // Status: 1,
  5540. // Ctime: time.Now().Unix(),
  5541. // Remark: advice.Remark,
  5542. // OrgId: orgID,
  5543. // Type: 1,
  5544. // Manufacturer: advice.Manufacturer,
  5545. // Dealer: warehouse.Dealer,
  5546. // IsSys: 0,
  5547. // SysRecordTime: advice.Ctime,
  5548. // DrugId: advice.DrugId,
  5549. // ExpiryDate: warehouse.ExpiryDate,
  5550. // ProductDate: warehouse.ProductDate,
  5551. // Number: warehouse.Number,
  5552. // BatchNumber: warehouse.BatchNumber,
  5553. // Count: stock_number,
  5554. // RetailPrice: advice.RetailPrice,
  5555. // Price: advice.Price,
  5556. // WarehouseInfoId: warehouse.ID,
  5557. // CountUnit: drup.MinUnit,
  5558. // AdviceId: advice.ID,
  5559. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5560. // StorehouseId: advice.StorehouseId,
  5561. // AdminUserId: advice.AdminUserId,
  5562. // LastPrice: warehouse.Price,
  5563. // StockCount: advice.StockCount,
  5564. // IsCheck: 1,
  5565. // }
  5566. //
  5567. // warehouseOutInfo.Count = stock_number
  5568. //
  5569. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  5570. //
  5571. // drugflow := models.DrugFlow{
  5572. // WarehouseOutId: warehouseout.ID,
  5573. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  5574. // DrugId: advice.DrugId,
  5575. // Number: warehouse.Number,
  5576. // ProductDate: advice.ProductDate,
  5577. // ExpireDate: advice.ExpiryDate,
  5578. // Count: stock_number,
  5579. // Price: advice.Price,
  5580. // Status: 1,
  5581. // Ctime: time.Now().Unix(),
  5582. // UserOrgId: advice.OrgId,
  5583. // Manufacturer: advice.Manufacturer,
  5584. // Dealer: advice.Dealer,
  5585. // BatchNumber: warehouse.BatchNumber,
  5586. // MaxUnit: drup.MinUnit,
  5587. // ConsumableType: 2,
  5588. // IsEdit: 1,
  5589. // Creator: 0,
  5590. // IsSys: 0,
  5591. // WarehouseOutDetailId: drugWareInfo.ID,
  5592. // AdviceId: advice.ID,
  5593. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5594. // StorehouseId: advice.StorehouseId,
  5595. // AdminUserId: advice.AdminUserId,
  5596. // LastPrice: warehouse.Price,
  5597. // StockCount: advice.StockCount,
  5598. // }
  5599. //
  5600. // CreateDrugFlowOne(drugflow)
  5601. // // 出库完成后,要将该批次库存清零
  5602. // //扣减库存
  5603. //
  5604. // warehouse.StockMaxNumber = 0
  5605. // warehouse.StockMinNumber = 0
  5606. // warehouse.Mtime = time.Now().Unix()
  5607. //
  5608. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  5609. // fmt.Println(errThree)
  5610. // //if errThree != nil {
  5611. // // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  5612. // // tx.Rollback()
  5613. // //} else {
  5614. // // tx.Commit()
  5615. // //}
  5616. //
  5617. // //查询默认仓库
  5618. // houseConfig, _ := GetAllStoreHouseConfig(orgID)
  5619. // //查询默认仓库剩余多少库存
  5620. // list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  5621. // var sum_count int64
  5622. // var sum_in_count int64
  5623. // for _, it := range list {
  5624. // baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  5625. // if it.MaxUnit == baseDrug.MaxUnit {
  5626. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  5627. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  5628. // sum_count += it.StockMaxNumber + it.StockMinNumber
  5629. // sum_in_count += it.WarehousingCount
  5630. // }
  5631. // }
  5632. // UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  5633. // if errThree != nil {
  5634. // return errThree
  5635. // }
  5636. // // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  5637. // prescribingNumber_two_temp := deliver_number - stock_number
  5638. //
  5639. // advice.CountUnit = drup.MinUnit
  5640. //
  5641. // AutoDrugDeliverInfoTwentyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  5642. // }
  5643. //
  5644. // return
  5645. //}
  5646. //
  5647. ////耗材手动出库
  5648. //func ConsumablesDeliveryThirty(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64) (err error) {
  5649. //
  5650. // var deliver_number int64 = 0
  5651. // var stock_number int64 = 0
  5652. // var maxNumber int64 = 0
  5653. //
  5654. // deliver_number = goods.Count
  5655. //
  5656. // // 根据先进先出原则,查询最先入库的批次,进行出库
  5657. //
  5658. // // 如果没有对应的库存,则报错
  5659. // warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  5660. // if err != nil {
  5661. // goodErrcode := models.XtGoodErrcode{
  5662. // UserOrgId: goods.OrgId,
  5663. // Errcode: "查询库存信息报错",
  5664. // GoodId: goods.GoodId,
  5665. // Status: 1,
  5666. // Ctime: time.Now().Unix(),
  5667. // Mtime: 0,
  5668. // Count: 0,
  5669. // StockCount: 0,
  5670. // Creater: creator,
  5671. // BatchNumberId: warehouse.ID,
  5672. // WarehouseOutId: 0,
  5673. // }
  5674. // CreateGoodErrcode(goodErrcode)
  5675. // return err
  5676. // }
  5677. //
  5678. // stock_number = warehouse.StockCount
  5679. //
  5680. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  5681. // if stock_number >= deliver_number {
  5682. // maxNumber = goods.Count
  5683. // warehouse.StockCount = warehouse.StockCount - maxNumber
  5684. // warehouse.Mtime = time.Now().Unix()
  5685. //
  5686. // //扣减库存
  5687. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  5688. // if errThree != nil {
  5689. // goodErrcode := models.XtGoodErrcode{
  5690. // UserOrgId: goods.OrgId,
  5691. // Errcode: "扣减库存失败",
  5692. // GoodId: goods.GoodId,
  5693. // Status: 1,
  5694. // Ctime: time.Now().Unix(),
  5695. // Mtime: 0,
  5696. // Count: 0,
  5697. // StockCount: 0,
  5698. // Creater: creator,
  5699. // BatchNumberId: warehouse.ID,
  5700. // WarehouseOutId: 0,
  5701. // }
  5702. // CreateGoodErrcode(goodErrcode)
  5703. // return errThree
  5704. // }
  5705. //
  5706. // //查询剩余库存
  5707. // goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID)
  5708. // var sum_count int64
  5709. // for _, item := range goodList {
  5710. // sum_count += item.StockCount
  5711. // }
  5712. //
  5713. // warehouseOutInfo := &models.WarehouseOutInfo{
  5714. // WarehouseInfotId: warehouse.ID,
  5715. // }
  5716. // //添加入库单详情ID
  5717. // errOne := UpdatedSigleWarehouseOutInfo(goods.ID, warehouseOutInfo)
  5718. // if errOne != nil {
  5719. // goodErrcode := models.XtGoodErrcode{
  5720. // UserOrgId: goods.OrgId,
  5721. // Errcode: "添加入库单详情ID",
  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 errOne
  5734. // }
  5735. //
  5736. // stockFlow := models.VmStockFlow{
  5737. // WarehousingId: warehouse.ID,
  5738. // GoodId: goods.GoodId,
  5739. // Number: warehouse.Number,
  5740. // LicenseNumber: goods.LicenseNumber,
  5741. // Count: goods.Count,
  5742. // UserOrgId: goods.OrgId,
  5743. // PatientId: goods.PatientId,
  5744. // SystemTime: record_time,
  5745. // ConsumableType: 2,
  5746. // IsSys: goods.IsSys,
  5747. // WarehousingOrder: warehouse.WarehousingOrder,
  5748. // WarehouseOutId: warehouseOut.ID,
  5749. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  5750. // IsEdit: 1,
  5751. // CancelStockId: 0,
  5752. // CancelOrderNumber: "",
  5753. // Manufacturer: goods.Manufacturer,
  5754. // Dealer: goods.Dealer,
  5755. // Creator: creator,
  5756. // UpdateCreator: 0,
  5757. // Status: 1,
  5758. // Ctime: time.Now().Unix(),
  5759. // Mtime: 0,
  5760. // Price: warehouse.PackingPrice, //零售价
  5761. // WarehousingDetailId: warehouse.ID,
  5762. // WarehouseOutDetailId: goods.ID,
  5763. // CancelOutDetailId: 0,
  5764. // ProductDate: goods.ProductDate,
  5765. // ExpireDate: goods.ExpiryDate,
  5766. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5767. // StorehouseId: warehouse.StorehouseId,
  5768. // AdminUserId: goods.AdminUserId,
  5769. // BuyPrice: warehouse.Price, //进货价
  5770. // StockCount: goods.StockCount,
  5771. // BatchNumberCount: warehouse.StockCount - goods.Count,
  5772. // IsCheck: 1,
  5773. // OverCount: sum_count,
  5774. // RegisterNumber: goods.RegisterNumber,
  5775. // }
  5776. // //创建出库流水
  5777. // errflow := CreateStockFlowOne(stockFlow)
  5778. // if errflow != nil {
  5779. // goodErrcode := models.XtGoodErrcode{
  5780. // UserOrgId: goods.OrgId,
  5781. // Errcode: "创建流水报错",
  5782. // GoodId: goods.GoodId,
  5783. // Status: 1,
  5784. // Ctime: time.Now().Unix(),
  5785. // Mtime: 0,
  5786. // Count: 0,
  5787. // StockCount: 0,
  5788. // Creater: creator,
  5789. // BatchNumberId: warehouse.ID,
  5790. // WarehouseOutId: 0,
  5791. // }
  5792. // CreateGoodErrcode(goodErrcode)
  5793. // }
  5794. //
  5795. // //添加入库ID
  5796. // errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, goods.OrgId, sum_count)
  5797. // if errWarehouse != nil {
  5798. // goodErrcode := models.XtGoodErrcode{
  5799. // UserOrgId: goods.OrgId,
  5800. // Errcode: "添加入库ID报错",
  5801. // GoodId: goods.GoodId,
  5802. // Status: 1,
  5803. // Ctime: time.Now().Unix(),
  5804. // Mtime: 0,
  5805. // Count: 0,
  5806. // StockCount: 0,
  5807. // Creater: creator,
  5808. // BatchNumberId: warehouse.ID,
  5809. // WarehouseOutId: 0,
  5810. // }
  5811. // CreateGoodErrcode(goodErrcode)
  5812. // }
  5813. //
  5814. // //更新出库数量
  5815. // errsumcode := ModifyGoodSumCount(goods.StorehouseId, goods.Count, goods.OrgId, goods.GoodId)
  5816. // if errsumcode != nil {
  5817. // goodErrcode := models.XtGoodErrcode{
  5818. // UserOrgId: goods.OrgId,
  5819. // Errcode: "更新出库数量报错",
  5820. // GoodId: goods.GoodId,
  5821. // Status: 1,
  5822. // Ctime: time.Now().Unix(),
  5823. // Mtime: 0,
  5824. // Count: 0,
  5825. // StockCount: 0,
  5826. // Creater: creator,
  5827. // BatchNumberId: warehouse.ID,
  5828. // WarehouseOutId: 0,
  5829. // }
  5830. // CreateGoodErrcode(goodErrcode)
  5831. // }
  5832. //
  5833. // if warehouse.StockCount < maxNumber {
  5834. // goodErrcode := models.XtGoodErrcode{
  5835. // UserOrgId: goods.OrgId,
  5836. // Errcode: "出库接口库存数量不足",
  5837. // GoodId: goods.GoodId,
  5838. // Status: 1,
  5839. // Ctime: time.Now().Unix(),
  5840. // Mtime: 0,
  5841. // Count: 0,
  5842. // StockCount: 0,
  5843. // Creater: creator,
  5844. // BatchNumberId: warehouse.ID,
  5845. // WarehouseOutId: 0,
  5846. // }
  5847. // CreateGoodErrcode(goodErrcode)
  5848. // return errors.New("库存数量不足")
  5849. //
  5850. // }
  5851. //
  5852. // return nil
  5853. // } else {
  5854. // // 出库完成后,要将该批次库存清零
  5855. // warehouse.StockCount = 0
  5856. // warehouse.Mtime = time.Now().Unix()
  5857. //
  5858. // errThree := UpDateWarehouseInfoByStock(&warehouse)
  5859. // if errThree != nil {
  5860. // goodErrcode := models.XtGoodErrcode{
  5861. // UserOrgId: goods.OrgId,
  5862. // Errcode: "扣减库存报错",
  5863. // GoodId: goods.GoodId,
  5864. // Status: 1,
  5865. // Ctime: time.Now().Unix(),
  5866. // Mtime: 0,
  5867. // Count: 0,
  5868. // StockCount: 0,
  5869. // Creater: creator,
  5870. // BatchNumberId: warehouse.ID,
  5871. // WarehouseOutId: 0,
  5872. // }
  5873. // CreateGoodErrcode(goodErrcode)
  5874. // return errThree
  5875. // }
  5876. //
  5877. // //查询剩余库存
  5878. // goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID)
  5879. // var sum_count int64
  5880. // for _, item := range goodList {
  5881. // sum_count += item.StockCount
  5882. // }
  5883. //
  5884. // stockFlow := models.VmStockFlow{
  5885. // WarehousingId: warehouse.ID,
  5886. // GoodId: goods.GoodId,
  5887. // Number: warehouse.Number,
  5888. // LicenseNumber: goods.LicenseNumber,
  5889. // Count: stock_number,
  5890. // UserOrgId: goods.OrgId,
  5891. // PatientId: goods.PatientId,
  5892. // SystemTime: record_time,
  5893. // ConsumableType: 2,
  5894. // IsSys: goods.IsSys,
  5895. // WarehousingOrder: warehouse.WarehousingOrder,
  5896. // WarehouseOutId: warehouseOut.ID,
  5897. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  5898. // IsEdit: 1,
  5899. // CancelStockId: 0,
  5900. // CancelOrderNumber: "",
  5901. // Manufacturer: goods.Manufacturer,
  5902. // Dealer: goods.Dealer,
  5903. // Creator: creator,
  5904. // UpdateCreator: 0,
  5905. // Status: 1,
  5906. // Ctime: time.Now().Unix(),
  5907. // Mtime: 0,
  5908. // Price: warehouse.PackingPrice, //零售价
  5909. // WarehousingDetailId: warehouse.ID,
  5910. // WarehouseOutDetailId: goods.ID,
  5911. // CancelOutDetailId: 0,
  5912. // ProductDate: goods.ProductDate,
  5913. // ExpireDate: goods.ExpiryDate,
  5914. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  5915. // StorehouseId: warehouse.StorehouseId,
  5916. // AdminUserId: goods.AdminUserId,
  5917. // BuyPrice: warehouse.Price, //进货价
  5918. // StockCount: goods.StockCount,
  5919. // BatchNumberCount: 0,
  5920. // IsCheck: 1,
  5921. // OverCount: sum_count,
  5922. // RegisterNumber: goods.RegisterNumber,
  5923. // }
  5924. // //创建出库流水
  5925. // errflow := CreateStockFlowOne(stockFlow)
  5926. // if errflow != nil {
  5927. // goodErrcode := models.XtGoodErrcode{
  5928. // UserOrgId: goods.OrgId,
  5929. // Errcode: "创建出库流水报错",
  5930. // GoodId: goods.GoodId,
  5931. // Status: 1,
  5932. // Ctime: time.Now().Unix(),
  5933. // Mtime: 0,
  5934. // Count: 0,
  5935. // StockCount: 0,
  5936. // Creater: creator,
  5937. // BatchNumberId: warehouse.ID,
  5938. // WarehouseOutId: 0,
  5939. // }
  5940. // CreateGoodErrcode(goodErrcode)
  5941. // }
  5942. //
  5943. // errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, goods.OrgId, sum_count)
  5944. // if errWarehouse != nil {
  5945. // goodErrcode := models.XtGoodErrcode{
  5946. // UserOrgId: goods.OrgId,
  5947. // Errcode: "添加入库ID报错",
  5948. // GoodId: goods.GoodId,
  5949. // Status: 1,
  5950. // Ctime: time.Now().Unix(),
  5951. // Mtime: 0,
  5952. // Count: 0,
  5953. // StockCount: 0,
  5954. // Creater: creator,
  5955. // BatchNumberId: warehouse.ID,
  5956. // WarehouseOutId: 0,
  5957. // }
  5958. // CreateGoodErrcode(goodErrcode)
  5959. // }
  5960. //
  5961. // //更新出库数量
  5962. // errcodecout := ModifyGoodSumCount(goods.StorehouseId, stock_number, goods.OrgId, goods.GoodId)
  5963. //
  5964. // if errcodecout != nil {
  5965. // goodErrcode := models.XtGoodErrcode{
  5966. // UserOrgId: goods.OrgId,
  5967. // Errcode: "更新出库数量报错",
  5968. // GoodId: goods.GoodId,
  5969. // Status: 1,
  5970. // Ctime: time.Now().Unix(),
  5971. // Mtime: 0,
  5972. // Count: 0,
  5973. // StockCount: 0,
  5974. // Creater: creator,
  5975. // BatchNumberId: warehouse.ID,
  5976. // WarehouseOutId: 0,
  5977. // }
  5978. // CreateGoodErrcode(goodErrcode)
  5979. // }
  5980. // }
  5981. //
  5982. // goods.Count = deliver_number - stock_number
  5983. // ConsumablesDeliveryThirty(orgID, record_time, goods, warehouseOut, count, creator)
  5984. //
  5985. // return nil
  5986. //}
  5987. //
  5988. //// 药品手动出库 递归方式
  5989. //func AutoDrugDeliverInfoFourtyOne(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  5990. //
  5991. // fmt.Println("出库数量", prescribingNumber)
  5992. //
  5993. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  5994. // var deliver_number int64 = 0
  5995. // var stock_number int64 = 0
  5996. // var retail_price float64
  5997. // if advice.CountUnit == drup.MaxUnit {
  5998. // deliver_number = prescribingNumber * drup.MinNumber
  5999. // formatInt := strconv.FormatInt(prescribingNumber, 10)
  6000. // floatcount, _ := strconv.ParseFloat(formatInt, 64)
  6001. // retail_price = floatcount * advice.Price
  6002. // } else {
  6003. // deliver_number = prescribingNumber
  6004. // formatInt := strconv.FormatInt(prescribingNumber, 10)
  6005. // floatcount, _ := strconv.ParseFloat(formatInt, 64)
  6006. // retail_price = floatcount * advice.Price
  6007. // }
  6008. //
  6009. // lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  6010. // if lastWarehouse.StockMinNumber >= drup.MinNumber {
  6011. // var stockMax int64
  6012. // var stockMin int64
  6013. // stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  6014. // stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  6015. // fmt.Println("stockmax", stockMax)
  6016. // fmt.Println("stockMin", stockMin)
  6017. // ChangeMaxNumber(lastWarehouse.ID, stockMax)
  6018. // UpdateMinNumber(lastWarehouse.ID, stockMin)
  6019. // }
  6020. //
  6021. // warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  6022. //
  6023. // if err != nil {
  6024. //
  6025. // return err
  6026. // }
  6027. //
  6028. // // 将该批次的剩余库存数量转换为拆零数量
  6029. // if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  6030. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  6031. // }
  6032. // if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  6033. // stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  6034. // }
  6035. // if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  6036. // stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  6037. // }
  6038. //
  6039. // fmt.Println("stock_number", stock_number)
  6040. // fmt.Println("deliver_number", deliver_number)
  6041. // // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6042. // if stock_number >= deliver_number {
  6043. //
  6044. // // 出库完成后,要减去对应批次的库存数量
  6045. // // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  6046. // var maxNumber int64 = 0
  6047. // var minNumber int64 = 0
  6048. //
  6049. // maxNumber = deliver_number / drup.MinNumber
  6050. // minNumber = deliver_number % drup.MinNumber
  6051. //
  6052. // if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  6053. // minNumber = maxNumber
  6054. // }
  6055. //
  6056. // if drup.MaxUnit != drup.MinUnit {
  6057. // if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  6058. // return errors.New("库存数量不足")
  6059. // }
  6060. // }
  6061. //
  6062. // if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  6063. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  6064. // }
  6065. // if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  6066. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  6067. // }
  6068. // if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  6069. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  6070. // }
  6071. //
  6072. // if warehouse.StockMaxNumber < 0 {
  6073. // warehouse.StockMaxNumber = 0
  6074. // }
  6075. // if warehouse.StockMinNumber < 0 {
  6076. // warehouse.StockMinNumber = 0
  6077. // }
  6078. //
  6079. // warehouse.Mtime = time.Now().Unix()
  6080. //
  6081. // if warehouse.StockMinNumber < minNumber {
  6082. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  6083. // warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  6084. // } else {
  6085. // fmt.Println("maxNumber23323323332322323w9", maxNumber)
  6086. // fmt.Println("maxNumber23323323332322323w9", minNumber)
  6087. // if minNumber > 0 {
  6088. // if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  6089. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6090. // } else {
  6091. // if (warehouse.StockMinNumber - minNumber) >= 0 {
  6092. // warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  6093. // }
  6094. //
  6095. // }
  6096. // }
  6097. //
  6098. // if minNumber == 0 && maxNumber != 1 {
  6099. // if warehouse.StockMinNumber > 0 {
  6100. // if (warehouse.StockMinNumber - deliver_number) >= 0 {
  6101. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6102. // }
  6103. // }
  6104. // }
  6105. // }
  6106. //
  6107. // if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  6108. // if (warehouse.StockMinNumber - deliver_number) >= 0 {
  6109. // warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6110. // }
  6111. //
  6112. // }
  6113. //
  6114. // if drup.MaxUnit != drup.MinUnit {
  6115. // if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  6116. // return errors.New("库存数量不足")
  6117. // }
  6118. // }
  6119. // if warehouse.StockMinNumber <= 0 {
  6120. // warehouse.StockMinNumber = 0
  6121. // }
  6122. //
  6123. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6124. //
  6125. // if errThree != nil {
  6126. // return errors.New("扣减库存失败")
  6127. // }
  6128. //
  6129. // //查询该药品剩余库存
  6130. // infolist, _ := FindDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId)
  6131. // var over_count int64
  6132. // for _, its := range infolist {
  6133. // if its.MaxUnit == drup.MaxUnit {
  6134. // its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  6135. // }
  6136. // over_count += its.StockMaxNumber + its.StockMinNumber
  6137. // }
  6138. //
  6139. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  6140. // drugflow := models.DrugFlow{
  6141. // WarehouseOutId: warehouseout.ID,
  6142. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  6143. // DrugId: advice.DrugId,
  6144. // Number: warehouse.Number,
  6145. // ProductDate: advice.ProductDate,
  6146. // ExpireDate: advice.ExpiryDate,
  6147. // Count: deliver_number,
  6148. // Price: warehouse.RetailPrice,
  6149. // Status: 1,
  6150. // Ctime: time.Now().Unix(),
  6151. // UserOrgId: advice.OrgId,
  6152. // Manufacturer: advice.Manufacturer,
  6153. // Dealer: advice.Dealer,
  6154. // BatchNumber: warehouse.BatchNumber,
  6155. // MaxUnit: drup.MinUnit,
  6156. // ConsumableType: 2,
  6157. // IsEdit: 1,
  6158. // Creator: 0,
  6159. // IsSys: 0,
  6160. // WarehouseOutDetailId: drugWareInfo.ID,
  6161. // AdviceId: advice.ID,
  6162. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6163. // StorehouseId: advice.StorehouseId,
  6164. // AdminUserId: advice.AdminUserId,
  6165. // LastPrice: warehouse.Price,
  6166. // StockCount: advice.StockCount,
  6167. // WarehousingDetailId: warehouse.ID,
  6168. // OverCount: over_count,
  6169. // RetailPrice: retail_price, //手动出库出库价格
  6170. // WarehousingId: warehouse.ID,
  6171. // }
  6172. // if warehouse.RetailPrice == 0 {
  6173. // drugflow.Price = advice.Price
  6174. // }
  6175. //
  6176. // CreateDrugFlowOne(drugflow)
  6177. //
  6178. // //更新出库id
  6179. // UpdateDrugWarehouseOutInfo(advice.ID, warehouse.ID, advice.OrgId, over_count)
  6180. //
  6181. // return nil
  6182. //
  6183. // } else {
  6184. // stock_number_int := strconv.FormatInt(stock_number, 10)
  6185. // stock_number_int_count, _ := strconv.ParseFloat(stock_number_int, 64)
  6186. // retail_price = stock_number_int_count * advice.Price
  6187. // //扣减库存
  6188. // warehouse.StockMaxNumber = 0
  6189. // warehouse.StockMinNumber = 0
  6190. // warehouse.Mtime = time.Now().Unix()
  6191. //
  6192. // errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6193. // if errThree != nil {
  6194. // return errThree
  6195. // }
  6196. //
  6197. // //查询该药品剩余库存
  6198. // infolist, _ := FindDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId)
  6199. // var over_count int64
  6200. // for _, its := range infolist {
  6201. // if its.MaxUnit == drup.MaxUnit {
  6202. // its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  6203. // }
  6204. // over_count += its.StockMaxNumber + its.StockMinNumber
  6205. // }
  6206. // drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  6207. // drugflow := models.DrugFlow{
  6208. // WarehouseOutId: warehouseout.ID,
  6209. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  6210. // DrugId: advice.DrugId,
  6211. // Number: warehouse.Number,
  6212. // ProductDate: advice.ProductDate,
  6213. // ExpireDate: advice.ExpiryDate,
  6214. // Count: stock_number,
  6215. // Price: warehouse.RetailPrice,
  6216. // Status: 1,
  6217. // Ctime: time.Now().Unix(),
  6218. // UserOrgId: advice.OrgId,
  6219. // Manufacturer: advice.Manufacturer,
  6220. // Dealer: advice.Dealer,
  6221. // BatchNumber: warehouse.BatchNumber,
  6222. // MaxUnit: drup.MinUnit,
  6223. // ConsumableType: 2,
  6224. // IsEdit: 1,
  6225. // Creator: 0,
  6226. // IsSys: 0,
  6227. // WarehouseOutDetailId: drugWareInfo.ID,
  6228. // AdviceId: advice.ID,
  6229. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6230. // StorehouseId: advice.StorehouseId,
  6231. // AdminUserId: advice.AdminUserId,
  6232. // LastPrice: warehouse.Price,
  6233. // StockCount: advice.StockCount,
  6234. // OverCount: over_count,
  6235. // RetailPrice: retail_price, //手动出库出库价格
  6236. // WarehousingId: warehouse.ID,
  6237. // WarehousingDetailId: warehouse.ID,
  6238. // }
  6239. //
  6240. // CreateDrugFlowOne(drugflow)
  6241. // //更新出库id
  6242. // UpdateDrugWarehouseOutInfo(advice.ID, warehouse.ID, advice.OrgId, over_count)
  6243. // // 出库完成后,要将该批次库存清零
  6244. //
  6245. // prescribingNumber_two_temp := deliver_number - stock_number
  6246. //
  6247. // advice.CountUnit = drup.MinUnit
  6248. //
  6249. // AutoDrugDeliverInfoFourtyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  6250. // }
  6251. //
  6252. // return
  6253. //}
  6254. //
  6255. //func UpdateDrugWarehouseOutInfo(id int64, warehouse_info_id int64, org_id int64, over_count int64) error {
  6256. // ut := XTWriteDB().Begin()
  6257. // 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
  6258. // if err != nil {
  6259. // ut.Rollback()
  6260. // return err
  6261. // }
  6262. // ut.Commit()
  6263. // return err
  6264. //}
  6265. //
  6266. //func UpdateGoodWarehouseOutById(id int64, warehouse_info_id int64, org_id int64, over_count int64) error {
  6267. // utx := XTWriteDB().Begin()
  6268. // 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
  6269. // if err != nil {
  6270. // utx.Rollback()
  6271. // return err
  6272. // }
  6273. // utx.Commit()
  6274. // return err
  6275. //}
  6276. //
  6277. //func ChangeMaxNumber(id int64, count int64) error {
  6278. //
  6279. // 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
  6280. // return err
  6281. //
  6282. //}
  6283. //
  6284. //func UpdateMinNumber(id int64, count int64) error {
  6285. //
  6286. // err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1 and is_check = 1", id).Update(map[string]interface{}{"stock_min_number": count}).Error
  6287. // return err
  6288. //}
  6289. //
  6290. //func FindLastDrugWarehouseOutInfo(orgid int64) (models.DrugWarehouseOutInfo, error) {
  6291. // info := models.DrugWarehouseOutInfo{}
  6292. // err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&info).Error
  6293. // return info, err
  6294. //}
  6295. //
  6296. //func GetAllGoodSumCount(goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  6297. //
  6298. // err = XTReadDB().Where("good_id = ? and org_id = ? and is_check =1 and status= 1 and stock_count > 0", goodid, orgid).Find(&info).Error
  6299. // return info, err
  6300. //}
  6301. //
  6302. //func CreatedFlushInfo(info models.XtWarehouseFlushInfo) error {
  6303. // ut := writeDb.Begin()
  6304. // err := ut.Create(&info).Error
  6305. // if err != nil {
  6306. // ut.Rollback()
  6307. // return err
  6308. // }
  6309. // ut.Commit()
  6310. // return err
  6311. //
  6312. //}
  6313. //
  6314. //func FindDrugWarehouseInfoList(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6315. //
  6316. // 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
  6317. // return info, err
  6318. //}
  6319. //
  6320. //func CreatedDrugFlushInfo(info models.XtDrugWarehouseFlushInfo) error {
  6321. // ut := writeDb.Begin()
  6322. // err := ut.Create(&info).Error
  6323. // if err != nil {
  6324. // ut.Rollback()
  6325. // return err
  6326. // }
  6327. // ut.Commit()
  6328. // return err
  6329. //}
  6330. //
  6331. //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 {
  6332. //
  6333. // 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
  6334. // return err
  6335. //}
  6336. //
  6337. //func FindLastWarehouseOutInfo(orgId int64) (models.WarehouseOutInfo, error) {
  6338. //
  6339. // info := models.WarehouseOutInfo{}
  6340. // err := XTReadDB().Model(&info).Where("org_id = ? and status = 1", orgId).Find(&info).Error
  6341. // return info, err
  6342. //}
  6343. //
  6344. //func ModifyAddGoodInformation(count int64, id int64) error {
  6345. //
  6346. // ut := XTWriteDB().Begin()
  6347. // err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", count)).Error
  6348. // if err != nil {
  6349. // ut.Rollback()
  6350. // return err
  6351. // }
  6352. // ut.Commit()
  6353. // return err
  6354. //}
  6355. //
  6356. //func ModifyReduceGoodInformation(count int64, id int64) error {
  6357. //
  6358. // ut := XTWriteDB().Begin()
  6359. // err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", count)).Error
  6360. // if err != nil {
  6361. // ut.Rollback()
  6362. // return err
  6363. // }
  6364. // ut.Commit()
  6365. // return err
  6366. //}
  6367. //
  6368. //func ModifyAddCancelCount(count int64, id int64) error {
  6369. //
  6370. // ut := XTWriteDB().Begin()
  6371. // err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", count)).Error
  6372. // if err != nil {
  6373. // ut.Rollback()
  6374. // return err
  6375. // }
  6376. // ut.Commit()
  6377. // return err
  6378. //}
  6379. //
  6380. //func ModifyReduceCancelCount(count int64, id int64) error {
  6381. //
  6382. // ut := XTWriteDB().Begin()
  6383. // err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", count)).Error
  6384. // if err != nil {
  6385. // ut.Rollback()
  6386. // return err
  6387. // }
  6388. // ut.Commit()
  6389. // return err
  6390. //}
  6391. //
  6392. //func ModifyAddDrugInformation(id int64, count int64, orgid int64) error {
  6393. //
  6394. // ut := XTWriteDB().Begin()
  6395. // 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
  6396. // if err != nil {
  6397. // ut.Rollback()
  6398. // return err
  6399. // }
  6400. // ut.Commit()
  6401. // return err
  6402. //}
  6403. //
  6404. //func ModifyReduceDrugInformation(id int64, count int64, orgid int64) error {
  6405. //
  6406. // ut := XTWriteDB().Begin()
  6407. // 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
  6408. // if err != nil {
  6409. // ut.Rollback()
  6410. // return err
  6411. // }
  6412. // ut.Commit()
  6413. // return err
  6414. //}
  6415. //
  6416. //func ModifyAddCancelAddInfomation(id int64, count int64, orgid int64, storehouse_id int64) error {
  6417. // ut := XTWriteDB().Begin()
  6418. // 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
  6419. // if err != nil {
  6420. // ut.Rollback()
  6421. // return err
  6422. // }
  6423. // ut.Commit()
  6424. // return err
  6425. //}
  6426. //
  6427. //func ModifyAddDrugOutReduceInfomation(id int64, count int64, orgid int64, storehouse_id int64) error {
  6428. // ut := XTWriteDB().Begin()
  6429. // 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
  6430. // if err != nil {
  6431. // ut.Rollback()
  6432. // return err
  6433. // }
  6434. // ut.Commit()
  6435. // return err
  6436. //}
  6437. //
  6438. //func ModifyAddDrugOutAddInfomation(id int64, count int64, orgid int64, storehouse_id int64) error {
  6439. // ut := XTWriteDB().Begin()
  6440. // 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
  6441. // if err != nil {
  6442. // ut.Rollback()
  6443. // return err
  6444. // }
  6445. // ut.Commit()
  6446. // return err
  6447. //}
  6448. //
  6449. //func ModifyDrugCancelWarehouseInfo(id int64, count int64, orgid int64, storehouse_id int64) error {
  6450. // ut := XTWriteDB().Begin()
  6451. // 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
  6452. // if err != nil {
  6453. // ut.Rollback()
  6454. // return err
  6455. // }
  6456. // ut.Commit()
  6457. // return err
  6458. //}
  6459. //
  6460. //func ModifyGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
  6461. //
  6462. // ut := XTWriteDB().Begin()
  6463. //
  6464. // 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
  6465. // if err != nil {
  6466. // ut.Rollback()
  6467. // return err
  6468. // }
  6469. //
  6470. // 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
  6471. // if err != nil {
  6472. // ut.Rollback()
  6473. // return err
  6474. // }
  6475. //
  6476. // ut.Commit()
  6477. // return err
  6478. //}
  6479. //
  6480. //func ModifyReduceGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
  6481. //
  6482. // ut := XTWriteDB().Begin()
  6483. //
  6484. // 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
  6485. // if err != nil {
  6486. // ut.Rollback()
  6487. // return err
  6488. // }
  6489. //
  6490. // 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
  6491. // if err != nil {
  6492. // ut.Rollback()
  6493. // return err
  6494. // }
  6495. //
  6496. // ut.Commit()
  6497. // return err
  6498. //}
  6499. //
  6500. //func ModifyAddGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
  6501. // ut := XTWriteDB().Begin()
  6502. //
  6503. // 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
  6504. // if err != nil {
  6505. // ut.Rollback()
  6506. // return err
  6507. // }
  6508. //
  6509. // ut.Commit()
  6510. // return err
  6511. //}
  6512. //
  6513. //func UpdateSumAddCancelCount(user_org_id int64, good_id int64, storehouse_id int64, count int64) error {
  6514. //
  6515. // ut := XTWriteDB().Begin()
  6516. // 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
  6517. // if err != nil {
  6518. // ut.Rollback()
  6519. // return err
  6520. // }
  6521. // ut.Commit()
  6522. // return err
  6523. //}
  6524. //
  6525. //func UpdateSumReduceCancelCount(user_org_id int64, good_id int64, storehouse_id int64, count int64) error {
  6526. //
  6527. // ut := XTWriteDB().Begin()
  6528. // 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
  6529. // if err != nil {
  6530. // ut.Rollback()
  6531. // return err
  6532. // }
  6533. // ut.Commit()
  6534. // return err
  6535. //}
  6536. //
  6537. //func UpdateSumAddOutCount(user_org_id int64, good_id int64, storehouse_id int64, count int64) error {
  6538. //
  6539. // ut := XTWriteDB().Begin()
  6540. // 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
  6541. // if err != nil {
  6542. // ut.Rollback()
  6543. // return err
  6544. // }
  6545. // ut.Commit()
  6546. // return err
  6547. //}
  6548. //
  6549. //func FindDrugWarehouseInfoFlushCount(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6550. //
  6551. // err = XTReadDB().Where("drug_id = ? and org_id = ? and status = 1 and is_check = 1", drug_id, orgid).Find(&info).Error
  6552. // return info, err
  6553. //}
  6554. //
  6555. //func FindDrugWarehouseInfoFlushCountByStorehouse(drug_id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
  6556. //
  6557. // 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
  6558. // return info, err
  6559. //}
  6560. // if(item.PrescribingNumberUnit == drup.MaxUnit && drup.MaxUnit ==drup.MinUnit ){
  6561. // prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  6562. // count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  6563. // total_count += count
  6564. // }
  6565. // info := models.DrugWarehouseOutInfo{
  6566. // Count: total_count,
  6567. // CountUnit: drup.MinUnit,
  6568. // }
  6569. //
  6570. // fmt.Println("total_coutn 233232322323232323233222322wo",total_count)
  6571. // //更新出库单数据
  6572. // UpdateDrugWarehouseOutByPatientId(advice.PatientId,advice.AdviceDate,advice.UserOrgId,info)
  6573. //}
  6574. } else {
  6575. return errors.New("药品信息不存在")
  6576. }
  6577. }
  6578. return
  6579. }
  6580. // 药品出库 递归方式
  6581. func HisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  6582. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  6583. var deliver_number int64 = 0
  6584. var stock_number int64 = 0
  6585. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  6586. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  6587. if advice.PrescribingNumberUnit == drup.MaxUnit {
  6588. deliver_number = count * drup.MinNumber
  6589. } else {
  6590. deliver_number = count
  6591. }
  6592. var min_price float64
  6593. if advice.PrescribingNumberUnit == drup.MaxUnit {
  6594. min_price = drup.RetailPrice
  6595. }
  6596. if advice.PrescribingNumberUnit == drup.MinUnit {
  6597. min_price = drup.MinPrice
  6598. }
  6599. if advice.PrescribingNumberUnit != drup.MaxUnit && advice.PrescribingNumberUnit != drup.MinUnit {
  6600. min_price = drup.RetailPrice
  6601. }
  6602. fmt.Println(min_price)
  6603. // 根据先进先出原则,查询最先入库的批次,进行出库
  6604. // 如果没有对应的库存,则报错
  6605. //查询默认仓库
  6606. storeConfig, _ := GetAllStoreHouseConfig(orgID)
  6607. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  6608. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  6609. var stockMax int64
  6610. var stockMin int64
  6611. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  6612. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  6613. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  6614. UpdateMinNumber(lastWarehouse.ID, stockMin)
  6615. }
  6616. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  6617. if err != nil {
  6618. return err
  6619. }
  6620. // 将该批次的剩余库存数量转换为拆零数量
  6621. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  6622. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6623. if stock_number >= deliver_number {
  6624. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  6625. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  6626. WarehouseOutId: warehouseout.ID,
  6627. Status: 1,
  6628. Ctime: time.Now().Unix(),
  6629. Remark: warehouse.Remark,
  6630. OrgId: orgID,
  6631. Type: 1,
  6632. Manufacturer: warehouse.Manufacturer,
  6633. Dealer: warehouse.Dealer,
  6634. IsSys: 1,
  6635. SysRecordTime: advice.RecordDate,
  6636. DrugId: advice.DrugId,
  6637. Number: warehouse.Number,
  6638. BatchNumber: warehouse.BatchNumber,
  6639. Price: warehouse.RetailPrice,
  6640. CountUnit: drup.MinUnit,
  6641. RetailPrice: warehouse.RetailPrice,
  6642. ProductDate: warehouse.ProductDate,
  6643. ExpiryDate: warehouse.ExpiryDate,
  6644. PatientId: advice.PatientId,
  6645. WarehouseInfoId: warehouse.ID,
  6646. Count: deliver_number,
  6647. AdviceId: advice.ID,
  6648. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6649. StorehouseId: storeConfig.DrugStorehouseOut,
  6650. IsCheck: 1,
  6651. LastPrice: warehouse.Price,
  6652. }
  6653. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  6654. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  6655. if errOne != nil {
  6656. return errOne
  6657. }
  6658. drugflow := models.DrugFlow{
  6659. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  6660. WarehouseOutId: warehouseout.ID,
  6661. DrugId: advice.DrugId,
  6662. Number: warehouse.Number,
  6663. ProductDate: warehouse.ProductDate,
  6664. ExpireDate: warehouse.ExpiryDate,
  6665. Count: deliver_number,
  6666. Price: warehouse.RetailPrice,
  6667. Status: 1,
  6668. Ctime: time.Now().Unix(),
  6669. UserOrgId: orgID,
  6670. Manufacturer: warehouse.Manufacturer,
  6671. Dealer: warehouse.Dealer,
  6672. BatchNumber: warehouse.BatchNumber,
  6673. MaxUnit: drup.MinUnit,
  6674. ConsumableType: 3,
  6675. IsEdit: 1,
  6676. Creator: advice.ExecutionStaff,
  6677. IsSys: 1,
  6678. PatientId: advice.PatientId,
  6679. SystemTime: advice.AdviceDate,
  6680. WarehousingDetailId: warehouse.ID,
  6681. AdviceId: advice.ID,
  6682. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6683. StorehouseId: storeConfig.DrugStorehouseOut,
  6684. WarehouseOutDetailId: lastDrugOutInfo.ID,
  6685. LastPrice: warehouse.Price,
  6686. }
  6687. CreateDrugFlowOne(drugflow)
  6688. details := &models.DrugAutomaticReduceDetail{
  6689. WarehouseOutId: warehouseout.ID,
  6690. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  6691. PatientId: advice.PatientId,
  6692. Ctime: time.Now().Unix(),
  6693. Mtime: time.Now().Unix(),
  6694. Status: 1,
  6695. RecordTime: advice.RecordDate,
  6696. OrgId: orgID,
  6697. DrugId: advice.DrugId,
  6698. Count: deliver_number,
  6699. CountUnit: drup.MinUnit,
  6700. WarehouseInfoId: warehouse.ID,
  6701. AdviceId: advice.ID,
  6702. StorehouseId: storeConfig.DrugStorehouseOut,
  6703. }
  6704. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  6705. if errTwo != nil {
  6706. return errTwo
  6707. }
  6708. // 出库完成后,要减去对应批次的库存数量
  6709. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  6710. var maxNumber int64 = 0
  6711. var minNumber int64 = 0
  6712. maxNumber = deliver_number / drup.MinNumber
  6713. minNumber = deliver_number % drup.MinNumber
  6714. fmt.Println("maxNumber2323233232323232323232", maxNumber)
  6715. fmt.Println("minNumber2323233232323232323232", minNumber)
  6716. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  6717. minNumber = maxNumber
  6718. }
  6719. if drup.MaxUnit != drup.MinUnit {
  6720. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  6721. return errors.New("库存数量不足")
  6722. }
  6723. }
  6724. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  6725. if warehouse.StockMaxNumber < 0 {
  6726. warehouse.StockMaxNumber = 0
  6727. }
  6728. if warehouse.StockMinNumber < 0 {
  6729. warehouse.StockMinNumber = 0
  6730. }
  6731. warehouse.Mtime = time.Now().Unix()
  6732. fmt.Println("warehouse.StockMinNumber", warehouse.StockMinNumber)
  6733. fmt.Println("minNumber", minNumber)
  6734. fmt.Println("maxNumber", maxNumber)
  6735. if warehouse.StockMinNumber < minNumber {
  6736. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  6737. if warehouse.MaxUnit != warehouse.MinUnit {
  6738. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  6739. }
  6740. } else {
  6741. if minNumber > 0 {
  6742. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  6743. }
  6744. }
  6745. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  6746. if (warehouse.StockMinNumber - deliver_number) > 0 {
  6747. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6748. }
  6749. }
  6750. if drup.MaxUnit != drup.MinUnit {
  6751. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  6752. return errors.New("库存数量不足")
  6753. }
  6754. }
  6755. if warehouse.StockMinNumber <= 0 {
  6756. warehouse.StockMinNumber = 0
  6757. }
  6758. //扣减库存
  6759. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6760. if errThree != nil {
  6761. return errThree
  6762. }
  6763. drugInfoList, _ := FindDrugWarehouseInfoList(advice.DrugId, orgID)
  6764. var drug_max_number int64
  6765. var drug_min_number int64
  6766. for _, item := range drugInfoList {
  6767. drug_max_number += item.StockMaxNumber
  6768. drug_min_number += item.StockMinNumber
  6769. }
  6770. drugFlushInfo := models.XtDrugWarehouseFlushInfo{
  6771. DrugMaxNumber: drug_max_number,
  6772. UserOrgId: orgID,
  6773. WarehouseOutId: warehouseout.ID,
  6774. Type: 1,
  6775. SystemTime: advice.RecordDate,
  6776. PatientId: advice.PatientId,
  6777. Ctime: time.Now().Unix(),
  6778. Mtime: 0,
  6779. BatchNumberId: warehouse.ID,
  6780. DrugId: advice.DrugId,
  6781. Count: deliver_number,
  6782. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  6783. Creater: warehouseout.Creater,
  6784. DrugMinNumber: drug_min_number,
  6785. Unit: drup.MinUnit,
  6786. AdviceId: advice.ID,
  6787. Status: 1,
  6788. WarehouseOutDetailId: lastDrugOutInfo.ID,
  6789. }
  6790. CreatedDrugFlushInfo(drugFlushInfo)
  6791. return nil
  6792. } else {
  6793. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  6794. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  6795. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  6796. WarehouseOutId: warehouseout.ID,
  6797. Status: 1,
  6798. Ctime: time.Now().Unix(),
  6799. Remark: warehouse.Remark,
  6800. OrgId: orgID,
  6801. Type: 1,
  6802. Manufacturer: warehouse.Manufacturer,
  6803. Dealer: warehouse.Dealer,
  6804. IsSys: 1,
  6805. SysRecordTime: advice.RecordDate,
  6806. DrugId: advice.DrugId,
  6807. Number: warehouse.Number,
  6808. BatchNumber: warehouse.BatchNumber,
  6809. Price: warehouse.RetailPrice,
  6810. RetailPrice: warehouse.RetailPrice,
  6811. ProductDate: warehouse.ProductDate,
  6812. ExpiryDate: warehouse.ExpiryDate,
  6813. PatientId: advice.PatientId,
  6814. WarehouseInfoId: warehouse.ID,
  6815. Count: stock_number,
  6816. CountUnit: drup.MinUnit,
  6817. AdviceId: advice.ID,
  6818. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6819. StorehouseId: storeConfig.DrugStorehouseOut,
  6820. IsCheck: 1,
  6821. }
  6822. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  6823. if errOne != nil {
  6824. return errOne
  6825. }
  6826. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  6827. drugflow := models.DrugFlow{
  6828. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  6829. WarehouseOutId: warehouseout.ID,
  6830. DrugId: advice.DrugId,
  6831. Number: warehouse.Number,
  6832. ProductDate: warehouse.ProductDate,
  6833. ExpireDate: warehouse.ExpiryDate,
  6834. Count: stock_number,
  6835. Price: warehouse.RetailPrice,
  6836. Status: 1,
  6837. Ctime: time.Now().Unix(),
  6838. UserOrgId: orgID,
  6839. Manufacturer: warehouse.Manufacturer,
  6840. Dealer: warehouse.Dealer,
  6841. BatchNumber: warehouse.BatchNumber,
  6842. MaxUnit: drup.MinUnit,
  6843. ConsumableType: 3,
  6844. IsEdit: 1,
  6845. Creator: advice.ExecutionStaff,
  6846. IsSys: 1,
  6847. PatientId: advice.PatientId,
  6848. SystemTime: advice.AdviceDate,
  6849. WarehousingDetailId: warehouse.ID,
  6850. AdviceId: advice.ID,
  6851. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6852. StorehouseId: storeConfig.DrugStorehouseOut,
  6853. WarehouseOutDetailId: lastDrugOutInfo.ID,
  6854. LastPrice: warehouse.Price,
  6855. }
  6856. CreateDrugFlowOne(drugflow)
  6857. //查询是否存在数据
  6858. details := &models.DrugAutomaticReduceDetail{
  6859. WarehouseOutId: warehouseout.ID,
  6860. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  6861. PatientId: advice.PatientId,
  6862. Ctime: time.Now().Unix(),
  6863. Mtime: time.Now().Unix(),
  6864. Status: 1,
  6865. RecordTime: advice.RecordDate,
  6866. OrgId: orgID,
  6867. DrugId: advice.DrugId,
  6868. Count: stock_number,
  6869. CountUnit: drup.MinUnit,
  6870. WarehouseInfoId: warehouse.ID,
  6871. AdviceId: advice.ID,
  6872. StorehouseId: storeConfig.DrugStorehouseOut,
  6873. }
  6874. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  6875. if errTwo != nil {
  6876. return errTwo
  6877. }
  6878. info := models.XtDrugWarehouseInfo{
  6879. ID: warehouse.ID,
  6880. WarehousingId: warehouse.WarehousingId,
  6881. DrugId: warehouse.DrugId,
  6882. Number: warehouse.Number,
  6883. ProductDate: warehouse.ProductDate,
  6884. ExpiryDate: warehouse.ExpiryDate,
  6885. WarehousingCount: warehouse.WarehousingCount,
  6886. Price: warehouse.Price,
  6887. TotalPrice: warehouse.TotalPrice,
  6888. Dealer: warehouse.Dealer,
  6889. Manufacturer: warehouse.Manufacturer,
  6890. Remark: warehouse.Remark,
  6891. Ctime: warehouse.Ctime,
  6892. Mtime: warehouse.Mtime,
  6893. Status: 1,
  6894. OrgId: warehouse.OrgId,
  6895. IsReturn: warehouse.IsReturn,
  6896. WarehousingOrder: warehouse.WarehousingOrder,
  6897. Type: warehouse.Type,
  6898. RetailPrice: warehouse.RetailPrice,
  6899. RetailTotalPrice: warehouse.RetailPrice,
  6900. StockMaxNumber: 0,
  6901. StockMinNumber: 0,
  6902. BatchNumber: warehouse.BatchNumber,
  6903. MaxUnit: warehouse.MaxUnit,
  6904. MinUnit: warehouse.MinUnit,
  6905. WarehousingInfoId: warehouse.WarehousingInfoId,
  6906. StorehouseId: storeConfig.DrugStorehouseOut,
  6907. IsCheck: 1,
  6908. }
  6909. errThree := UpDateDrugWarehouseInfoByStock(&info)
  6910. if errThree != nil {
  6911. return errThree
  6912. }
  6913. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  6914. prescribingNumber_two_temp := deliver_number - stock_number
  6915. overPlusNumber := float64(prescribingNumber_two_temp)
  6916. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  6917. advice.PrescribingNumberUnit = drup.MinUnit
  6918. HisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  6919. }
  6920. return
  6921. }
  6922. // 药品出库 递归方式
  6923. func AutoDrugDeliverInfo(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  6924. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  6925. var deliver_number int64 = 0
  6926. var stock_number int64 = 0
  6927. if advice.CountUnit == drup.MaxUnit {
  6928. deliver_number = prescribingNumber * drup.MinNumber
  6929. } else {
  6930. deliver_number = prescribingNumber
  6931. }
  6932. // 根据先进先出原则,查询最先入库的批次,进行出库
  6933. // 如果没有对应的库存,则报错
  6934. //开启事物
  6935. storeConfig, _ := GetAllStoreHouseConfig(orgID)
  6936. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  6937. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  6938. var stockMax int64
  6939. var stockMin int64
  6940. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  6941. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  6942. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  6943. UpdateMinNumber(lastWarehouse.ID, stockMin)
  6944. }
  6945. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  6946. if err != nil {
  6947. return err
  6948. }
  6949. // 将该批次的剩余库存数量转换为拆零数量
  6950. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  6951. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6952. if stock_number >= deliver_number {
  6953. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  6954. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  6955. WarehouseOutId: warehouseout.ID,
  6956. Status: 1,
  6957. Ctime: time.Now().Unix(),
  6958. Remark: advice.Remark,
  6959. OrgId: orgID,
  6960. Type: 1,
  6961. Manufacturer: advice.Manufacturer,
  6962. Dealer: warehouse.Dealer,
  6963. IsSys: 0,
  6964. SysRecordTime: advice.Ctime,
  6965. DrugId: advice.DrugId,
  6966. ExpiryDate: advice.ExpiryDate,
  6967. ProductDate: advice.ProductDate,
  6968. Number: advice.Number,
  6969. BatchNumber: warehouse.BatchNumber,
  6970. Count: deliver_number,
  6971. RetailPrice: advice.RetailPrice,
  6972. Price: warehouse.RetailPrice,
  6973. WarehouseInfoId: warehouse.ID,
  6974. CountUnit: drup.MinUnit,
  6975. AdviceId: advice.ID,
  6976. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  6977. StorehouseId: advice.StorehouseId,
  6978. AdminUserId: advice.AdminUserId,
  6979. IsCheck: 1,
  6980. }
  6981. warehouseOutInfo.Count = prescribingNumber
  6982. warehouseOutInfo.CountUnit = advice.CountUnit
  6983. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  6984. if errOne != nil {
  6985. return errOne
  6986. }
  6987. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  6988. drugflow := models.DrugFlow{
  6989. WarehouseOutId: warehouseout.ID,
  6990. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  6991. DrugId: advice.DrugId,
  6992. Number: warehouse.Number,
  6993. ProductDate: advice.ProductDate,
  6994. ExpireDate: advice.ExpiryDate,
  6995. Count: deliver_number,
  6996. Price: warehouse.RetailPrice,
  6997. Status: 1,
  6998. Ctime: time.Now().Unix(),
  6999. UserOrgId: advice.OrgId,
  7000. Manufacturer: advice.Manufacturer,
  7001. Dealer: advice.Dealer,
  7002. BatchNumber: warehouse.BatchNumber,
  7003. MaxUnit: drup.MinUnit,
  7004. ConsumableType: 2,
  7005. IsEdit: 1,
  7006. Creator: 0,
  7007. IsSys: 0,
  7008. WarehouseOutDetailId: drugWareInfo.ID,
  7009. AdviceId: advice.ID,
  7010. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7011. StorehouseId: advice.StorehouseId,
  7012. AdminUserId: advice.AdminUserId,
  7013. }
  7014. CreateDrugFlowOne(drugflow)
  7015. // 出库完成后,要减去对应批次的库存数量
  7016. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  7017. var maxNumber int64 = 0
  7018. var minNumber int64 = 0
  7019. if advice.CountUnit == drup.MinUnit {
  7020. maxNumber = prescribingNumber / drup.MinNumber
  7021. minNumber = prescribingNumber % drup.MinNumber
  7022. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  7023. minNumber = maxNumber
  7024. }
  7025. } else {
  7026. maxNumber = prescribingNumber
  7027. }
  7028. if warehouse.StockMaxNumber < maxNumber {
  7029. return errors.New("库存数量不足")
  7030. }
  7031. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  7032. warehouse.Mtime = time.Now().Unix()
  7033. if warehouse.StockMinNumber < minNumber {
  7034. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  7035. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  7036. } else {
  7037. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  7038. }
  7039. if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
  7040. warehouse.StockMinNumber = 0
  7041. }
  7042. if warehouse.StockMaxNumber < 0 {
  7043. return errors.New("库存数量不足")
  7044. }
  7045. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  7046. if errThree != nil {
  7047. return errThree
  7048. }
  7049. //查询默认仓库
  7050. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  7051. //查询默认仓库剩余多少库存
  7052. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  7053. var sum_count int64
  7054. var sum_in_count int64
  7055. for _, it := range list {
  7056. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  7057. if it.MaxUnit == baseDrug.MaxUnit {
  7058. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  7059. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  7060. }
  7061. sum_count += it.StockMaxNumber + it.StockMinNumber
  7062. sum_in_count += it.WarehousingCount
  7063. }
  7064. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  7065. return nil
  7066. } else {
  7067. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  7068. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  7069. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  7070. WarehouseOutId: warehouseout.ID,
  7071. Status: 1,
  7072. Ctime: time.Now().Unix(),
  7073. Remark: advice.Remark,
  7074. OrgId: orgID,
  7075. Type: 1,
  7076. Manufacturer: advice.Manufacturer,
  7077. Dealer: warehouse.Dealer,
  7078. IsSys: 0,
  7079. SysRecordTime: advice.Ctime,
  7080. DrugId: advice.DrugId,
  7081. ExpiryDate: warehouse.ExpiryDate,
  7082. ProductDate: warehouse.ProductDate,
  7083. Number: warehouse.Number,
  7084. BatchNumber: warehouse.BatchNumber,
  7085. Count: stock_number,
  7086. RetailPrice: advice.RetailPrice,
  7087. Price: warehouse.RetailPrice,
  7088. WarehouseInfoId: warehouse.ID,
  7089. CountUnit: drup.MinUnit,
  7090. AdviceId: advice.ID,
  7091. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7092. StorehouseId: advice.StorehouseId,
  7093. AdminUserId: advice.AdminUserId,
  7094. IsCheck: 1,
  7095. }
  7096. warehouseOutInfo.Count = stock_number
  7097. //warehouseOutInfo.CountUnit = advice.CountUnit
  7098. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  7099. if errOne != nil {
  7100. return errOne
  7101. }
  7102. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  7103. drugflow := models.DrugFlow{
  7104. WarehouseOutId: warehouseout.ID,
  7105. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  7106. DrugId: advice.DrugId,
  7107. Number: warehouse.Number,
  7108. ProductDate: advice.ProductDate,
  7109. ExpireDate: advice.ExpiryDate,
  7110. Count: stock_number,
  7111. Price: warehouse.RetailPrice,
  7112. Status: 1,
  7113. Ctime: time.Now().Unix(),
  7114. UserOrgId: advice.OrgId,
  7115. Manufacturer: advice.Manufacturer,
  7116. Dealer: advice.Dealer,
  7117. BatchNumber: warehouse.BatchNumber,
  7118. MaxUnit: drup.MinUnit,
  7119. ConsumableType: 2,
  7120. IsEdit: 1,
  7121. Creator: 0,
  7122. IsSys: 0,
  7123. WarehouseOutDetailId: drugWareInfo.ID,
  7124. AdviceId: advice.ID,
  7125. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7126. StorehouseId: advice.StorehouseId,
  7127. AdminUserId: advice.AdminUserId,
  7128. }
  7129. CreateDrugFlowOne(drugflow)
  7130. // 出库完成后,要将该批次库存清零
  7131. //扣减库存
  7132. warehouse.StockMaxNumber = 0
  7133. warehouse.StockMinNumber = 0
  7134. warehouse.Mtime = time.Now().Unix()
  7135. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  7136. //查询默认仓库
  7137. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  7138. //查询默认仓库剩余多少库存
  7139. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  7140. var sum_count int64
  7141. var sum_in_count int64
  7142. for _, it := range list {
  7143. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  7144. if it.MaxUnit == baseDrug.MaxUnit {
  7145. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  7146. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  7147. sum_count += it.StockMaxNumber + it.StockMinNumber
  7148. sum_in_count += it.WarehousingCount
  7149. }
  7150. }
  7151. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  7152. if errThree != nil {
  7153. return errThree
  7154. }
  7155. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  7156. prescribingNumber_two_temp := deliver_number - stock_number
  7157. advice.CountUnit = drup.MinUnit
  7158. AutoDrugDeliverInfo(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  7159. }
  7160. return
  7161. }
  7162. // 耗材出库
  7163. func ConsumablesDeliveryTotal(orgID int64, patient_id int64, record_time int64, goods []*models.DialysisBeforePrepareGoods, goodOne []*models.NewDialysisBeforePrepareGoods, creator int64) (err error) {
  7164. //查询该患者当天已经出库的耗材信息
  7165. goods_yc, _ := FindConsumablesByDateTwo(orgID, patient_id, record_time)
  7166. // 和新请求的出库数据进行对比,分出那些是继续出库的,那些是需要删除出库的
  7167. for i := len(goods_yc) - 1; i >= 0; i-- {
  7168. goods_yc_temp := goods_yc[i]
  7169. for j := len(goods) - 1; j >= 0; j-- {
  7170. goods_temp := goods[j]
  7171. // 已经出库和新请求出库都存在该耗材,则判断出库数量,分成是继续出库,还是删除出库
  7172. if goods_yc_temp.GoodTypeId == goods_temp.GoodTypeId && goods_yc_temp.GoodId == goods_temp.GoodId {
  7173. // 已经出库和新请求出库的出库数量一致,则清除两个结构体里的数据(既不出库,也不删除出库)
  7174. if goods_yc_temp.Count == goods_temp.Count {
  7175. goods_yc = append(goods_yc[:i], goods_yc[i+1:]...)
  7176. goods = append(goods[:j], goods[j+1:]...)
  7177. break
  7178. }
  7179. // 如果已经出库的数量 大于 新请求出库的数量,则代表需要删除出库
  7180. if goods_yc_temp.Count > goods_temp.Count {
  7181. temp_count := goods_yc_temp.Count - goods_temp.Count
  7182. goods_yc[i].Count = temp_count
  7183. goods = append(goods[:j], goods[j+1:]...)
  7184. break
  7185. }
  7186. // 如果已经出库的数量 小于 新请求出库的梳理,则代表需要增加出库
  7187. if goods_yc_temp.Count < goods_temp.Count {
  7188. temp_count := goods_temp.Count - goods_yc_temp.Count
  7189. goods[j].Count = temp_count
  7190. goods_yc = append(goods_yc[:i], goods_yc[i+1:]...)
  7191. break
  7192. }
  7193. }
  7194. }
  7195. }
  7196. // goods_yc 这个数据就是需要已经出库了,但是现在需要删除出库的耗材数据
  7197. // goods 这个数据就是需要出库的耗材的数据(新增的数据)
  7198. if len(goods) > 0 {
  7199. out, err := FindStockOutByIsSys(orgID, 1, record_time)
  7200. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  7201. if err == gorm.ErrRecordNotFound {
  7202. //没有记录,则创建出库单
  7203. timeStr := time.Now().Format("2006-01-02")
  7204. timeArr := strings.Split(timeStr, "-")
  7205. total, _ := FindAllWarehouseOut(orgID)
  7206. total = total + 1
  7207. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  7208. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  7209. number = number + total
  7210. warehousing_out_order = "CKD" + strconv.FormatInt(number, 10)
  7211. warehouseOut := models.WarehouseOut{
  7212. WarehouseOutOrderNumber: warehousing_out_order,
  7213. OperationTime: time.Now().Unix(),
  7214. OrgId: orgID,
  7215. Creater: creator,
  7216. Ctime: time.Now().Unix(),
  7217. Status: 1,
  7218. WarehouseOutTime: record_time,
  7219. Dealer: 0,
  7220. Manufacturer: 0,
  7221. Type: 1,
  7222. IsSys: 1,
  7223. StorehouseId: houseConfig.StorehouseOutInfo,
  7224. IsCheck: 1,
  7225. }
  7226. err := AddSigleWarehouseOut(&warehouseOut)
  7227. if err != nil {
  7228. utils.TraceLog("创建出库单失败 err = %v", err)
  7229. return err
  7230. } else {
  7231. out = warehouseOut
  7232. }
  7233. }
  7234. for _, item := range goods {
  7235. var newCount int64 = 0
  7236. for _, it := range goodOne {
  7237. if item.GoodTypeId == it.GoodTypeId && item.GoodId == it.GoodId {
  7238. newCount = it.Count
  7239. }
  7240. }
  7241. var cha_count int64
  7242. var cha_count_two int64
  7243. flowGood, _ := GetStockFlowIsBatchNumberTwo(patient_id, record_time, item.GoodId)
  7244. var out_count int64
  7245. for _, item := range flowGood {
  7246. out_count += item.Count
  7247. }
  7248. cha_count = item.Count - out_count
  7249. //fmt.Println("item.Count", item.Count)
  7250. //fmt.Println("item.Count", out_count)
  7251. //fmt.Println("cha_count2323223232323223", cha_count)
  7252. cha_count_two = out_count - item.Count
  7253. prepare := models.DialysisBeforePrepare{
  7254. GoodTypeId: item.GoodTypeId,
  7255. GoodId: item.GoodId,
  7256. Count: cha_count,
  7257. ProjectId: item.ProjectId,
  7258. StorehouseId: houseConfig.StorehouseOutInfo,
  7259. }
  7260. ConsumablesGoodDelivery(orgID, patient_id, record_time, &prepare, &out, newCount)
  7261. //数量增加
  7262. if cha_count > 0 {
  7263. //出库数量累加
  7264. ModifyGoodSumCount(houseConfig.StorehouseOutInfo, cha_count, orgID, item.GoodId)
  7265. }
  7266. //数量减少
  7267. if cha_count_two > 0 {
  7268. ModifyAddGoodSumCount(houseConfig.StorehouseOutInfo, cha_count_two, orgID, item.GoodId)
  7269. //退库数量增加
  7270. UpdateSumAddCancelCount(orgID, item.GoodId, houseConfig.StorehouseOutInfo, cha_count_two)
  7271. }
  7272. //更新剩余库存
  7273. goodListSix, _ := GetSumGoodList(orgID, houseConfig.StorehouseOutInfo, item.GoodId)
  7274. var flush_count int64
  7275. for _, it := range goodListSix {
  7276. flush_count += it.StockCount
  7277. }
  7278. UpdateSumGood(orgID, houseConfig.StorehouseOutInfo, item.GoodId, flush_count)
  7279. }
  7280. }
  7281. fmt.Println("goods_yc2323323223323223232323232332wode", goods_yc)
  7282. if len(goods_yc) > 0 {
  7283. for _, good_yc := range goods_yc {
  7284. out, _ := FindStockOutByIsSys(orgID, 1, record_time)
  7285. ConsumablesDeliveryDelete(orgID, patient_id, record_time, good_yc, &out)
  7286. }
  7287. }
  7288. return nil
  7289. }
  7290. // 耗材出库
  7291. func ConsumablesDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, count int64) (err error) {
  7292. //开事务
  7293. var deliver_number int64 = 0
  7294. var stock_number int64 = 0
  7295. var maxNumber int64 = 0
  7296. deliver_number = goods.Count
  7297. // 根据先进先出原则,查询最先入库的批次,进行出库
  7298. // 如果没有对应的库存,则报错
  7299. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  7300. if err != nil {
  7301. return errors.New("库存数量不足")
  7302. }
  7303. stock_number = warehouse.StockCount
  7304. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  7305. if stock_number >= deliver_number {
  7306. maxNumber = goods.Count
  7307. //出库
  7308. warehouse.StockCount = warehouse.StockCount - maxNumber
  7309. warehouse.Mtime = time.Now().Unix()
  7310. if warehouse.StockCount < 0 {
  7311. return errors.New("库存数量不足")
  7312. }
  7313. errThree := UpDateWarehouseInfoByStock(&warehouse)
  7314. if errThree != nil {
  7315. return errThree
  7316. }
  7317. //查询剩余库存
  7318. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  7319. var sum_count int64
  7320. for _, item := range goodList {
  7321. sum_count += item.StockCount
  7322. }
  7323. warehouseOutInfo := &models.WarehouseOutInfo{
  7324. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7325. WarehouseOutId: warehouseOut.ID,
  7326. WarehouseInfotId: warehouse.ID,
  7327. Status: 1,
  7328. Ctime: time.Now().Unix(),
  7329. Remark: warehouse.Remark,
  7330. OrgId: orgID,
  7331. Type: 1,
  7332. Manufacturer: warehouse.Manufacturer,
  7333. Dealer: warehouse.Dealer,
  7334. IsSys: 1,
  7335. SysRecordTime: record_time,
  7336. GoodTypeId: goods.GoodTypeId,
  7337. GoodId: goods.GoodId,
  7338. PatientId: patient_id,
  7339. Number: warehouse.Number,
  7340. LicenseNumber: warehouse.LicenseNumber,
  7341. Price: warehouse.PackingPrice,
  7342. ExpiryDate: warehouse.ExpiryDate,
  7343. ProductDate: warehouse.ProductDate,
  7344. ProjectId: goods.ProjectId,
  7345. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7346. StorehouseId: goods.StorehouseId,
  7347. IsCheck: 1,
  7348. }
  7349. warehouseOutInfo.Count = count
  7350. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
  7351. if errcodes == gorm.ErrRecordNotFound {
  7352. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  7353. if errOne != nil {
  7354. return errOne
  7355. }
  7356. } else if errcodes == nil {
  7357. UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId)
  7358. }
  7359. details := models.BloodAutomaticReduceDetail{
  7360. WarehouseOutId: warehouseOutInfo.ID,
  7361. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  7362. PatientId: patient_id,
  7363. Ctime: time.Now().Unix(),
  7364. Mtime: time.Now().Unix(),
  7365. Status: 1,
  7366. RecordTime: record_time,
  7367. OrgId: orgID,
  7368. GoodId: goods.GoodId,
  7369. GoodTypeId: goods.GoodTypeId,
  7370. Count: count,
  7371. ProjectId: goods.ProjectId,
  7372. StorehouseId: goods.StorehouseId,
  7373. }
  7374. //查询当天耗材是否已经存在数据
  7375. _, errcode := GetAutoMaticReduceDetail(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  7376. if errcode == gorm.ErrRecordNotFound {
  7377. errTwo := CreateAutoReduceRecord(&details)
  7378. if errTwo != nil {
  7379. return errTwo
  7380. }
  7381. //插入库存流水表
  7382. flow := models.VmStockFlow{
  7383. WarehouseOutId: warehouseOut.ID,
  7384. WarehousingId: warehouse.ID,
  7385. GoodId: goods.GoodId,
  7386. Number: warehouse.Number,
  7387. ProductDate: warehouse.ProductDate,
  7388. ExpireDate: warehouse.ExpiryDate,
  7389. Count: deliver_number,
  7390. Price: warehouse.PackingPrice,
  7391. Status: 1,
  7392. Ctime: time.Now().Unix(),
  7393. UserOrgId: orgID,
  7394. Manufacturer: warehouse.Manufacturer,
  7395. Dealer: warehouse.Dealer,
  7396. LicenseNumber: warehouse.LicenseNumber,
  7397. IsEdit: 2,
  7398. Creator: warehouseOut.Creater,
  7399. SystemTime: record_time,
  7400. ConsumableType: 3,
  7401. WarehouseOutDetailId: warehouseOutInfo.ID,
  7402. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7403. IsSys: 1,
  7404. PatientId: patient_id,
  7405. ProjectId: goods.ProjectId,
  7406. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7407. StorehouseId: goods.StorehouseId,
  7408. OverCount: sum_count,
  7409. }
  7410. //查询流水是否存在
  7411. exsit, errflow := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  7412. if errflow == gorm.ErrRecordNotFound {
  7413. //创建数据
  7414. errThre := CreateStockFlowOne(flow)
  7415. if errThre != nil {
  7416. return errThre
  7417. }
  7418. } else if errflow == nil {
  7419. flow := models.VmStockFlow{
  7420. ID: exsit.ID,
  7421. WarehouseOutId: warehouseOut.ID,
  7422. WarehousingId: warehouse.ID,
  7423. GoodId: goods.GoodId,
  7424. Number: warehouse.Number,
  7425. ProductDate: warehouse.ProductDate,
  7426. ExpireDate: warehouse.ExpiryDate,
  7427. Count: exsit.Count + goods.Count,
  7428. Price: warehouse.PackingPrice,
  7429. Status: 1,
  7430. Ctime: time.Now().Unix(),
  7431. UserOrgId: orgID,
  7432. Manufacturer: warehouse.Manufacturer,
  7433. Dealer: warehouse.Dealer,
  7434. LicenseNumber: warehouse.LicenseNumber,
  7435. IsEdit: 2,
  7436. Creator: warehouseOut.Creater,
  7437. SystemTime: record_time,
  7438. ConsumableType: 3,
  7439. WarehouseOutDetailId: warehouseOutInfo.ID,
  7440. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7441. IsSys: 1,
  7442. PatientId: patient_id,
  7443. ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  7444. ProjectId: goods.ProjectId,
  7445. StorehouseId: goods.StorehouseId,
  7446. OverCount: sum_count,
  7447. }
  7448. errFour := UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  7449. if errFour != nil {
  7450. return errFour
  7451. }
  7452. }
  7453. } else if errcode == nil {
  7454. flow := models.VmStockFlow{
  7455. WarehouseOutId: warehouseOut.ID,
  7456. WarehousingId: warehouse.ID,
  7457. GoodId: goods.GoodId,
  7458. Number: warehouse.Number,
  7459. ProductDate: warehouse.ProductDate,
  7460. ExpireDate: warehouse.ExpiryDate,
  7461. Count: deliver_number,
  7462. Price: warehouse.PackingPrice,
  7463. Status: 1,
  7464. Ctime: time.Now().Unix(),
  7465. UserOrgId: orgID,
  7466. Manufacturer: warehouse.Manufacturer,
  7467. Dealer: warehouse.Dealer,
  7468. LicenseNumber: warehouse.LicenseNumber,
  7469. IsEdit: 2,
  7470. Creator: warehouseOut.Creater,
  7471. SystemTime: record_time,
  7472. ConsumableType: 3,
  7473. WarehouseOutDetailId: warehouseOutInfo.ID,
  7474. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7475. IsSys: 1,
  7476. PatientId: patient_id,
  7477. ProjectId: goods.ProjectId,
  7478. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7479. StorehouseId: goods.StorehouseId,
  7480. OverCount: sum_count,
  7481. }
  7482. //查询流水是否存在
  7483. exsit, errflow := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  7484. if errflow == gorm.ErrRecordNotFound {
  7485. CreateStockFlowOne(flow)
  7486. DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  7487. CreateAutoReduceRecord(&details)
  7488. } else if errflow == nil {
  7489. flow := models.VmStockFlow{
  7490. ID: exsit.ID,
  7491. WarehouseOutId: warehouseOut.ID,
  7492. WarehousingId: warehouse.ID,
  7493. GoodId: goods.GoodId,
  7494. Number: warehouse.Number,
  7495. ProductDate: warehouse.ProductDate,
  7496. ExpireDate: warehouse.ExpiryDate,
  7497. Count: exsit.Count + goods.Count,
  7498. Price: warehouse.PackingPrice,
  7499. Status: 1,
  7500. Ctime: time.Now().Unix(),
  7501. UserOrgId: orgID,
  7502. Manufacturer: warehouse.Manufacturer,
  7503. Dealer: warehouse.Dealer,
  7504. LicenseNumber: warehouse.LicenseNumber,
  7505. IsEdit: 2,
  7506. Creator: warehouseOut.Creater,
  7507. SystemTime: record_time,
  7508. ConsumableType: 3,
  7509. WarehouseOutDetailId: warehouseOutInfo.ID,
  7510. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7511. IsSys: 1,
  7512. PatientId: patient_id,
  7513. ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  7514. ProjectId: goods.ProjectId,
  7515. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7516. StorehouseId: goods.StorehouseId,
  7517. OverCount: sum_count,
  7518. }
  7519. UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  7520. DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  7521. CreateAutoReduceRecord(&details)
  7522. }
  7523. }
  7524. return nil
  7525. } else {
  7526. // 当刚批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  7527. warehouse.StockCount = 0
  7528. warehouse.Mtime = time.Now().Unix()
  7529. errThree := UpDateWarehouseInfoByStock(&warehouse)
  7530. if errThree != nil {
  7531. return errThree
  7532. }
  7533. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  7534. var sum_count int64
  7535. for _, item := range goodList {
  7536. sum_count += item.StockCount
  7537. }
  7538. warehouseOutInfo := &models.WarehouseOutInfo{
  7539. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7540. WarehouseOutId: warehouseOut.ID,
  7541. WarehouseInfotId: warehouse.ID,
  7542. Status: 1,
  7543. Ctime: time.Now().Unix(),
  7544. Remark: warehouse.Remark,
  7545. OrgId: orgID,
  7546. Type: 1,
  7547. Manufacturer: warehouse.Manufacturer,
  7548. Dealer: warehouse.Dealer,
  7549. IsSys: 1,
  7550. SysRecordTime: record_time,
  7551. GoodTypeId: goods.GoodTypeId,
  7552. GoodId: goods.GoodId,
  7553. PatientId: patient_id,
  7554. Number: warehouse.Number,
  7555. LicenseNumber: warehouse.LicenseNumber,
  7556. Price: warehouse.PackingPrice,
  7557. ExpiryDate: warehouse.ExpiryDate,
  7558. ProductDate: warehouse.ProductDate,
  7559. ProjectId: goods.ProjectId,
  7560. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7561. StorehouseId: goods.StorehouseId,
  7562. IsCheck: 1,
  7563. }
  7564. warehouseOutInfo.Count = stock_number
  7565. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, goods.PatientId, record_time, goods.ProjectId)
  7566. if errcodes == gorm.ErrRecordNotFound {
  7567. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  7568. if errOne != nil {
  7569. return errOne
  7570. }
  7571. //插入库存流水表
  7572. flow := models.VmStockFlow{
  7573. WarehouseOutId: warehouseOut.ID,
  7574. WarehousingId: warehouse.ID,
  7575. GoodId: goods.GoodId,
  7576. Number: warehouse.Number,
  7577. ProductDate: warehouse.ProductDate,
  7578. ExpireDate: warehouse.ExpiryDate,
  7579. Count: stock_number,
  7580. Price: warehouse.PackingPrice,
  7581. Status: 1,
  7582. Ctime: time.Now().Unix(),
  7583. UserOrgId: orgID,
  7584. Manufacturer: warehouse.Manufacturer,
  7585. Dealer: warehouse.Dealer,
  7586. LicenseNumber: warehouse.LicenseNumber,
  7587. IsEdit: 2,
  7588. Creator: warehouseOut.Creater,
  7589. SystemTime: record_time,
  7590. ConsumableType: 3,
  7591. WarehouseOutDetailId: warehouseOutInfo.ID,
  7592. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7593. IsSys: 1,
  7594. PatientId: patient_id,
  7595. ProjectId: goods.ProjectId,
  7596. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7597. StorehouseId: goods.StorehouseId,
  7598. OverCount: sum_count,
  7599. }
  7600. //查询流水是否存在
  7601. exsit, errflows := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  7602. if errflows == gorm.ErrRecordNotFound {
  7603. CreateStockFlowOne(flow)
  7604. } else if errflows == nil {
  7605. flow := models.VmStockFlow{
  7606. ID: exsit.ID,
  7607. WarehouseOutId: warehouseOut.ID,
  7608. WarehousingId: warehouse.ID,
  7609. GoodId: goods.GoodId,
  7610. Number: warehouse.Number,
  7611. ProductDate: warehouse.ProductDate,
  7612. ExpireDate: warehouse.ExpiryDate,
  7613. Count: exsit.Count + goods.Count,
  7614. Price: warehouse.PackingPrice,
  7615. Status: 1,
  7616. Ctime: time.Now().Unix(),
  7617. UserOrgId: orgID,
  7618. Manufacturer: warehouse.Manufacturer,
  7619. Dealer: warehouse.Dealer,
  7620. LicenseNumber: warehouse.LicenseNumber,
  7621. IsEdit: 2,
  7622. Creator: warehouseOut.Creater,
  7623. SystemTime: record_time,
  7624. ConsumableType: 3,
  7625. WarehouseOutDetailId: warehouseOutInfo.ID,
  7626. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7627. IsSys: 1,
  7628. PatientId: patient_id,
  7629. ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  7630. ProjectId: goods.ProjectId,
  7631. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7632. StorehouseId: goods.StorehouseId,
  7633. OverCount: sum_count,
  7634. }
  7635. errOne := UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  7636. if errOne == nil {
  7637. }
  7638. }
  7639. } else if errcodes == nil {
  7640. goods.Count = deliver_number - stock_number
  7641. //更新数量为 该批次剩余数量 + 还有未出的数量
  7642. warehouseOutInfo.Count = stock_number
  7643. UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, goods.PatientId, record_time, goods.ProjectId)
  7644. //插入库存流水表
  7645. flow := models.VmStockFlow{
  7646. WarehouseOutId: warehouseOut.ID,
  7647. WarehousingId: warehouse.ID,
  7648. GoodId: goods.GoodId,
  7649. Number: warehouse.Number,
  7650. ProductDate: warehouse.ProductDate,
  7651. ExpireDate: warehouse.ExpiryDate,
  7652. Count: stock_number,
  7653. Price: warehouse.PackingPrice,
  7654. Status: 1,
  7655. Ctime: time.Now().Unix(),
  7656. UserOrgId: orgID,
  7657. Manufacturer: warehouse.Manufacturer,
  7658. Dealer: warehouse.Dealer,
  7659. LicenseNumber: warehouse.LicenseNumber,
  7660. IsEdit: 2,
  7661. Creator: warehouseOut.Creater,
  7662. SystemTime: record_time,
  7663. ConsumableType: 3,
  7664. WarehouseOutDetailId: warehouseOutInfo.ID,
  7665. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7666. IsSys: 1,
  7667. PatientId: patient_id,
  7668. ProjectId: goods.ProjectId,
  7669. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7670. StorehouseId: goods.StorehouseId,
  7671. OverCount: sum_count,
  7672. }
  7673. //查询流水是否存在
  7674. exsit, errflows := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  7675. if errflows == gorm.ErrRecordNotFound {
  7676. CreateStockFlowOne(flow)
  7677. } else if errflows == nil {
  7678. flow := models.VmStockFlow{
  7679. ID: exsit.ID,
  7680. WarehouseOutId: warehouseOut.ID,
  7681. WarehousingId: warehouse.ID,
  7682. GoodId: goods.GoodId,
  7683. Number: warehouse.Number,
  7684. ProductDate: warehouse.ProductDate,
  7685. ExpireDate: warehouse.ExpiryDate,
  7686. Count: exsit.Count + goods.Count,
  7687. Price: warehouse.PackingPrice,
  7688. Status: 1,
  7689. Ctime: time.Now().Unix(),
  7690. UserOrgId: orgID,
  7691. Manufacturer: warehouse.Manufacturer,
  7692. Dealer: warehouse.Dealer,
  7693. LicenseNumber: warehouse.LicenseNumber,
  7694. IsEdit: 2,
  7695. Creator: warehouseOut.Creater,
  7696. SystemTime: record_time,
  7697. ConsumableType: 3,
  7698. WarehouseOutDetailId: warehouseOutInfo.ID,
  7699. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7700. IsSys: 1,
  7701. PatientId: patient_id,
  7702. ProjectId: goods.ProjectId,
  7703. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7704. StorehouseId: goods.StorehouseId,
  7705. OverCount: sum_count,
  7706. }
  7707. UpdatedStockFlowOne(flow, warehouse.ID, patient_id, record_time, goods.GoodId)
  7708. }
  7709. }
  7710. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  7711. goods.Count = deliver_number - stock_number
  7712. ConsumablesDelivery(orgID, patient_id, record_time, goods, warehouseOut, count)
  7713. }
  7714. return nil
  7715. }
  7716. // 耗材出库删除
  7717. func ConsumablesDeliveryDelete(orgID int64, patient_id int64, record_time int64, good_yc *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut) (err error) {
  7718. // 先根据相关信息查询当天该耗材的出库信息
  7719. warehouseOutInfos, err := FindStockOutInfoByStock(orgID, good_yc.GoodTypeId, good_yc.GoodId, record_time, patient_id)
  7720. if err != nil {
  7721. return err
  7722. }
  7723. var delete_count int64 = 0
  7724. for _, ware := range warehouseOutInfos {
  7725. // 删除出库完成后,要增加对应批次的库存数量
  7726. errThree := UpDateWarehouseInfoByStockDelete(ware.WarehouseInfotId, delete_count, patient_id, record_time, good_yc.GoodId)
  7727. if errThree != nil {
  7728. return errThree
  7729. }
  7730. //查询剩余库存
  7731. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  7732. goodList, _ := GetAllGoodSumCount(good_yc.GoodId, orgID, houseConfig.StorehouseOutInfo)
  7733. var sum_count int64
  7734. for _, item := range goodList {
  7735. sum_count += item.StockCount
  7736. }
  7737. // 判断当前出库的数据和删除出库数量
  7738. if good_yc.Count <= ware.Count {
  7739. delete_count = good_yc.Count
  7740. } else {
  7741. delete_count = ware.Count
  7742. }
  7743. // 在出库记录表里记录退库详情
  7744. warehouseOutInfo := &models.WarehouseOutInfo{
  7745. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7746. WarehouseOutId: warehouseOut.ID,
  7747. //WarehouseInfoId: warehouse.ID,
  7748. Status: 1,
  7749. Ctime: time.Now().Unix(),
  7750. Remark: "",
  7751. OrgId: orgID,
  7752. Type: 1,
  7753. Manufacturer: 0,
  7754. Dealer: 0,
  7755. IsSys: 2,
  7756. SysRecordTime: record_time,
  7757. GoodTypeId: good_yc.GoodTypeId,
  7758. GoodId: good_yc.GoodId,
  7759. PatientId: patient_id,
  7760. StorehouseId: houseConfig.StorehouseOutInfo,
  7761. OverCount: sum_count,
  7762. }
  7763. warehouseOutInfo.Count = ware.Count - delete_count
  7764. stockInInfo, _ := FindLastStockInInfoRecord(good_yc.GoodId, orgID)
  7765. warehouseOutInfo.Price = stockInInfo.Price
  7766. errOne := UpdateAutoMaticReduceDetail(good_yc.GoodId, good_yc.GoodTypeId, record_time, patient_id, orgID, warehouseOutInfo)
  7767. if errOne != nil {
  7768. return errOne
  7769. } else {
  7770. details := &models.AutomaticReduceDetail{
  7771. WarehouseOutId: warehouseOutInfo.ID,
  7772. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  7773. PatientId: patient_id,
  7774. Ctime: time.Now().Unix(),
  7775. Mtime: time.Now().Unix(),
  7776. Status: 1,
  7777. RecordTime: record_time,
  7778. OrgId: orgID,
  7779. GoodId: good_yc.GoodId,
  7780. GoodTypeId: good_yc.GoodTypeId,
  7781. Count: warehouseOutInfo.Count,
  7782. Type: 2,
  7783. StorehouseId: houseConfig.StorehouseOutInfo,
  7784. }
  7785. //查询是否当天已经存在数据
  7786. _, errcode := GetAutoMaticReduceDetail(orgID, patient_id, record_time, good_yc.GoodId, good_yc.GoodTypeId)
  7787. if errcode == gorm.ErrRecordNotFound {
  7788. errTwo := AddSigleAutoReduceRecordInfo(details)
  7789. if errTwo != nil {
  7790. return errTwo
  7791. }
  7792. } else if errcode == nil {
  7793. DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, good_yc.GoodId, good_yc.GoodTypeId)
  7794. AddSigleAutoReduceRecordInfo(details)
  7795. }
  7796. }
  7797. //扣减数量相加
  7798. ModifyAddGoodSumCount(houseConfig.StorehouseOutInfo, delete_count, good_yc.UserOrgId, good_yc.GoodId)
  7799. //退库数量相加
  7800. UpdateSumAddCancelCount(good_yc.UserOrgId, good_yc.GoodId, houseConfig.StorehouseOutInfo, delete_count)
  7801. // 增加了对应的库存后,看看还有多少需要退库的
  7802. good_yc.Count = good_yc.Count - delete_count
  7803. if good_yc.Count == 0 {
  7804. return nil
  7805. }
  7806. }
  7807. if good_yc.Count == 0 {
  7808. return nil
  7809. } else {
  7810. return errors.New("退库和出库数据不匹配")
  7811. }
  7812. }
  7813. func GetAutoReduceRecordInfoByPatientId(orgid int64, patient_id int64, recordTime int64) (autoReduce []*models.AutomaticReduceDetail, err error) {
  7814. err = XTReadDB().Model(&autoReduce).Where("org_id = ? and patient_id = ? and record_time = ? and status = 1", orgid, patient_id, recordTime).Find(&autoReduce).Error
  7815. return autoReduce, err
  7816. }
  7817. func DeleteDialysisBefor(orgid int64, patient_id int64, record_date int64, goodid int64, goodtypeid int64) error {
  7818. prepare := models.DialysisBeforePrepare{}
  7819. 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
  7820. return err
  7821. }
  7822. func DeleteDialysisBeforThree(orgid int64, patient_id int64, record_date int64) error {
  7823. prepare := models.DialysisBeforePrepare{}
  7824. 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
  7825. return err
  7826. }
  7827. func GetWarehouseById(id int64) (models.WarehouseOut, error) {
  7828. out := models.WarehouseOut{}
  7829. err := XTReadDB().Where("id=? and status = 1", id).Find(&out).Error
  7830. return out, err
  7831. }
  7832. // 耗材出库
  7833. func ConsumablesDeliveryOne(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64) (err error) {
  7834. ////开事务
  7835. //tx := XTWriteDB().Begin()
  7836. var deliver_number int64 = 0
  7837. var stock_number int64 = 0
  7838. var maxNumber int64 = 0
  7839. deliver_number = goods.Count
  7840. // 根据先进先出原则,查询最先入库的批次,进行出库
  7841. // 如果没有对应的库存,则报错
  7842. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  7843. if err != nil {
  7844. return err
  7845. }
  7846. stock_number = warehouse.StockCount
  7847. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  7848. if stock_number >= deliver_number {
  7849. warehouseOutInfo := &models.WarehouseOutInfo{
  7850. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7851. WarehouseOutId: warehouseOut.ID,
  7852. WarehouseInfotId: warehouse.ID,
  7853. Status: 1,
  7854. Ctime: time.Now().Unix(),
  7855. Remark: goods.Remark,
  7856. OrgId: orgID,
  7857. Type: 1,
  7858. Manufacturer: goods.Manufacturer,
  7859. Dealer: goods.Dealer,
  7860. IsSys: 0,
  7861. SysRecordTime: record_time,
  7862. GoodTypeId: goods.GoodTypeId,
  7863. GoodId: goods.GoodId,
  7864. ExpiryDate: goods.ExpiryDate,
  7865. ProductDate: goods.ProductDate,
  7866. Number: warehouse.Number,
  7867. Price: goods.Price,
  7868. LicenseNumber: goods.LicenseNumber,
  7869. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7870. StorehouseId: goods.StorehouseId,
  7871. AdminUserId: goods.AdminUserId,
  7872. BuyPrice: warehouse.Price,
  7873. StockCount: goods.StockCount,
  7874. }
  7875. warehouseOutInfo.Count = goods.Count
  7876. //添加出库单详情
  7877. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  7878. if errOne != nil {
  7879. return errOne
  7880. }
  7881. stockFlow := models.VmStockFlow{
  7882. WarehousingId: warehouse.ID,
  7883. GoodId: goods.GoodId,
  7884. Number: warehouse.Number,
  7885. LicenseNumber: goods.LicenseNumber,
  7886. Count: goods.Count,
  7887. UserOrgId: goods.OrgId,
  7888. PatientId: goods.PatientId,
  7889. SystemTime: record_time,
  7890. ConsumableType: 2,
  7891. IsSys: goods.IsSys,
  7892. WarehousingOrder: "",
  7893. WarehouseOutId: warehouseOut.ID,
  7894. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7895. IsEdit: 1,
  7896. CancelStockId: 0,
  7897. CancelOrderNumber: "",
  7898. Manufacturer: goods.Manufacturer,
  7899. Dealer: goods.Dealer,
  7900. Creator: creator,
  7901. UpdateCreator: 0,
  7902. Status: 1,
  7903. Ctime: time.Now().Unix(),
  7904. Mtime: 0,
  7905. Price: warehouse.PackingPrice,
  7906. WarehousingDetailId: 0,
  7907. WarehouseOutDetailId: warehouseOutInfo.ID,
  7908. CancelOutDetailId: 0,
  7909. ProductDate: goods.ProductDate,
  7910. ExpireDate: goods.ExpiryDate,
  7911. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7912. StorehouseId: warehouse.StorehouseId,
  7913. AdminUserId: goods.AdminUserId,
  7914. BuyPrice: warehouse.Price,
  7915. StockCount: goods.StockCount,
  7916. }
  7917. //创建出库流水
  7918. CreateStockFlowOne(stockFlow)
  7919. maxNumber = goods.Count
  7920. if warehouse.StockCount < maxNumber {
  7921. return errors.New("库存数量不足")
  7922. }
  7923. warehouse.StockCount = warehouse.StockCount - maxNumber
  7924. warehouse.Mtime = time.Now().Unix()
  7925. //扣减库存
  7926. errThree := UpDateWarehouseInfoByStock(&warehouse)
  7927. fmt.Println(errThree)
  7928. //if errThree != nil {
  7929. // utils.ErrorLog("事务失败,原因为: %v", errThree.Error())
  7930. // tx.Rollback()
  7931. //} else {
  7932. // tx.Commit()
  7933. //}
  7934. //查询该机构默认仓库
  7935. storeConfig, _ := GetAllStoreHouseConfig(goods.OrgId)
  7936. //查询剩余库存
  7937. goodList, _ := GetGoodSumCountByStoreId(storeConfig.StorehouseOutInfo, goods.GoodId, goods.OrgId)
  7938. var sum_count int64
  7939. var sum_in_count int64
  7940. for _, item := range goodList {
  7941. sum_count += item.StockCount
  7942. sum_in_count += item.WarehousingCount
  7943. }
  7944. UpdateGoodByGoodId(goods.GoodId, sum_count, sum_in_count, goods.OrgId)
  7945. if errThree != nil {
  7946. return errThree
  7947. }
  7948. } else {
  7949. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  7950. warehouseOutInfo := &models.WarehouseOutInfo{
  7951. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  7952. WarehouseOutId: warehouseOut.ID,
  7953. WarehouseInfotId: warehouse.ID,
  7954. Status: 1,
  7955. Ctime: time.Now().Unix(),
  7956. Remark: goods.Remark,
  7957. OrgId: orgID,
  7958. Type: 1,
  7959. Manufacturer: goods.Manufacturer,
  7960. Dealer: goods.Dealer,
  7961. IsSys: 0,
  7962. SysRecordTime: record_time,
  7963. GoodTypeId: goods.GoodTypeId,
  7964. GoodId: goods.GoodId,
  7965. ExpiryDate: goods.ExpiryDate,
  7966. ProductDate: goods.ProductDate,
  7967. Number: warehouse.Number,
  7968. Price: warehouse.PackingPrice,
  7969. LicenseNumber: goods.LicenseNumber,
  7970. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  7971. StorehouseId: warehouse.StorehouseId,
  7972. AdminUserId: goods.AdminUserId,
  7973. BuyPrice: warehouse.Price,
  7974. StockCount: goods.StockCount,
  7975. IsCheck: 1,
  7976. }
  7977. warehouseOutInfo.Count = stock_number
  7978. //查询是否已经生成出库单
  7979. info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  7980. if errcodes == gorm.ErrRecordNotFound {
  7981. //创建出库单
  7982. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  7983. if errOne != nil {
  7984. return errOne
  7985. }
  7986. } else {
  7987. warehouseOutInfo.Count = info.Count + stock_number
  7988. UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  7989. }
  7990. stockFlow := models.VmStockFlow{
  7991. WarehousingId: warehouse.ID,
  7992. GoodId: goods.GoodId,
  7993. Number: warehouse.Number,
  7994. LicenseNumber: goods.LicenseNumber,
  7995. Count: stock_number,
  7996. UserOrgId: goods.OrgId,
  7997. PatientId: 0,
  7998. SystemTime: record_time,
  7999. ConsumableType: 2,
  8000. IsSys: 0,
  8001. WarehousingOrder: "",
  8002. WarehouseOutId: warehouseOut.ID,
  8003. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  8004. IsEdit: 1,
  8005. CancelStockId: 0,
  8006. CancelOrderNumber: "",
  8007. Manufacturer: goods.Manufacturer,
  8008. Dealer: goods.Dealer,
  8009. Creator: creator,
  8010. UpdateCreator: 0,
  8011. Status: 1,
  8012. Ctime: time.Now().Unix(),
  8013. Mtime: 0,
  8014. Price: warehouse.PackingPrice,
  8015. WarehousingDetailId: 0,
  8016. WarehouseOutDetailId: warehouseOutInfo.ID,
  8017. CancelOutDetailId: 0,
  8018. ProductDate: goods.ProductDate,
  8019. ExpireDate: goods.ExpiryDate,
  8020. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  8021. StorehouseId: goods.StorehouseId,
  8022. AdminUserId: goods.AdminUserId,
  8023. BuyPrice: warehouse.Price,
  8024. StockCount: goods.StockCount,
  8025. }
  8026. //创建出库明细
  8027. CreateStockFlowOne(stockFlow)
  8028. //查询该机构默认仓库
  8029. storeConfig, _ := GetAllStoreHouseConfig(goods.OrgId)
  8030. //查询剩余库存
  8031. goodList, _ := GetGoodSumCountByStoreId(storeConfig.StorehouseOutInfo, goods.GoodId, goods.OrgId)
  8032. var sum_count int64
  8033. var sum_in_count int64
  8034. for _, item := range goodList {
  8035. sum_count += item.StockCount
  8036. sum_in_count += item.WarehousingCount
  8037. }
  8038. UpdateGoodByGoodId(goods.GoodId, sum_count, sum_in_count, goods.OrgId)
  8039. // 出库完成后,要将该批次库存清零
  8040. warehouse.StockCount = 0
  8041. warehouse.Mtime = time.Now().Unix()
  8042. errThree := UpDateWarehouseInfoByStock(&warehouse)
  8043. fmt.Println(errThree)
  8044. //if errThree != nil {
  8045. // utils.ErrorLog("事务失败,原因为: %v", errThree.Error())
  8046. // tx.Rollback()
  8047. //} else {
  8048. // tx.Commit()
  8049. //}
  8050. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  8051. goods.Count = deliver_number - stock_number
  8052. ConsumablesDeliveryOne(orgID, record_time, goods, warehouseOut, count, creator)
  8053. }
  8054. return nil
  8055. }
  8056. // 退库逻辑
  8057. func ConsumablesDeliveryTwo(orgID int64, goods *models.WarehouseOutInfo, count int64) (err error) {
  8058. ////开事务
  8059. //tx := XTWriteDB().Begin()
  8060. var deliver_number int64 = 0
  8061. var stock_number int64 = 0
  8062. var maxNumber int64 = 0
  8063. deliver_number = goods.Count
  8064. // 根据先进先出原则,查询最先入库的批次,进行出库
  8065. warehouse, err := FindFirstWarehousingInfoByGoodId(goods.GoodId, goods.ID, goods.StorehouseId)
  8066. if err != nil {
  8067. return err
  8068. }
  8069. stock_number = warehouse.StockCount
  8070. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  8071. if stock_number >= deliver_number {
  8072. warehouseOutInfo := &models.WarehouseOutInfo{
  8073. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  8074. WarehouseOutId: goods.ID,
  8075. WarehouseInfotId: warehouse.ID,
  8076. Status: 1,
  8077. Ctime: time.Now().Unix(),
  8078. Remark: goods.Remark,
  8079. OrgId: orgID,
  8080. Type: 1,
  8081. Manufacturer: goods.Manufacturer,
  8082. Dealer: goods.Dealer,
  8083. IsSys: 0,
  8084. GoodTypeId: goods.GoodTypeId,
  8085. GoodId: goods.GoodId,
  8086. ExpiryDate: goods.ExpiryDate,
  8087. ProductDate: goods.ProductDate,
  8088. Number: goods.Number,
  8089. Price: warehouse.PackingPrice,
  8090. LicenseNumber: goods.LicenseNumber,
  8091. ConsumableType: 2,
  8092. }
  8093. warehouseOutInfo.Count = goods.Count
  8094. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  8095. if errOne != nil {
  8096. return errOne
  8097. }
  8098. maxNumber = goods.Count
  8099. if warehouse.StockCount < maxNumber { //这段逻辑根本不会执行,因为当库存数量大于或等于出库数量才会进入这个分支
  8100. return errors.New("库存数量不足")
  8101. }
  8102. warehouse.StockCount = warehouse.StockCount - maxNumber
  8103. warehouse.Mtime = time.Now().Unix()
  8104. if warehouse.StockCount <= 0 {
  8105. return errors.New("库存数量不足")
  8106. }
  8107. errThree := UpDateWarehouseInfoByStock(&warehouse)
  8108. fmt.Println(errThree)
  8109. //if errThree != nil {
  8110. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  8111. // tx.Rollback()
  8112. //} else {
  8113. // tx.Commit()
  8114. //}
  8115. return
  8116. } else {
  8117. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  8118. warehouseOutInfo := &models.WarehouseOutInfo{
  8119. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  8120. WarehouseOutId: goods.ID,
  8121. WarehouseInfotId: warehouse.ID,
  8122. Status: 1,
  8123. Ctime: time.Now().Unix(),
  8124. Remark: goods.Remark,
  8125. OrgId: orgID,
  8126. Type: 1,
  8127. Manufacturer: goods.Manufacturer,
  8128. Dealer: goods.Dealer,
  8129. IsSys: 0,
  8130. GoodTypeId: goods.GoodTypeId,
  8131. GoodId: goods.GoodId,
  8132. ExpiryDate: goods.ExpiryDate,
  8133. ProductDate: goods.ProductDate,
  8134. Number: goods.Number,
  8135. Price: warehouse.PackingPrice,
  8136. LicenseNumber: goods.LicenseNumber,
  8137. ConsumableType: 2,
  8138. IsCheck: 1,
  8139. StorehouseId: goods.StorehouseId,
  8140. }
  8141. warehouseOutInfo.Count = stock_number
  8142. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  8143. if errOne != nil {
  8144. return errOne
  8145. }
  8146. // 出库完成后,要将该批次库存清零
  8147. warehouse.StockCount = 0
  8148. warehouse.Mtime = time.Now().Unix()
  8149. errThree := UpDateWarehouseInfoByStock(&warehouse)
  8150. //if errThree != nil {
  8151. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  8152. // tx.Rollback()
  8153. //} else {
  8154. // tx.Commit()
  8155. //}
  8156. fmt.Println(errThree)
  8157. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  8158. goods.Count = deliver_number - stock_number
  8159. ConsumablesDeliveryTwo(orgID, goods, count)
  8160. }
  8161. return nil
  8162. }
  8163. func DeleteWarehouseInfo(id int64) error {
  8164. err := XTWriteDB().Model(models.WarehousingInfo{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": 0}).Error
  8165. return err
  8166. }
  8167. // 药品出库 递归方式
  8168. func NewDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DoctorAdvice) (err error) {
  8169. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  8170. var deliver_number int64 = 0
  8171. var stock_number int64 = 0
  8172. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  8173. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  8174. var drug_price float64
  8175. //按最小单位数据计算
  8176. deliver_number = count
  8177. if advice.PrescribingNumberUnit == drup.MaxUnit {
  8178. drug_price = drup.RetailPrice
  8179. }
  8180. if advice.PrescribingNumberUnit == drup.MinUnit {
  8181. drug_price = drup.MinPrice
  8182. }
  8183. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  8184. drug_price = drup.RetailPrice
  8185. }
  8186. // 根据先进先出原则,查询最先入库的批次,进行出库
  8187. // 如果没有对应的库存,则报错
  8188. //查询默认仓库
  8189. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  8190. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  8191. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  8192. var stockMax int64
  8193. var stockMin int64
  8194. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  8195. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  8196. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  8197. UpdateMinNumber(lastWarehouse.ID, stockMin)
  8198. }
  8199. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  8200. if err != nil {
  8201. return err
  8202. }
  8203. // 将该批次的剩余库存数量转换为拆零数量
  8204. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  8205. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  8206. if stock_number >= deliver_number {
  8207. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  8208. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8209. WarehouseOutId: warehouseout.ID,
  8210. Status: 1,
  8211. Ctime: time.Now().Unix(),
  8212. Remark: warehouse.Remark,
  8213. OrgId: orgID,
  8214. Type: 1,
  8215. Manufacturer: warehouse.Manufacturer,
  8216. Dealer: warehouse.Dealer,
  8217. IsSys: 1,
  8218. SysRecordTime: advice.RecordDate,
  8219. DrugId: advice.DrugId,
  8220. Number: warehouse.Number,
  8221. BatchNumber: warehouse.BatchNumber,
  8222. Price: warehouse.RetailPrice,
  8223. CountUnit: drup.MinUnit,
  8224. RetailPrice: drug_price,
  8225. ProductDate: warehouse.ProductDate,
  8226. ExpiryDate: warehouse.ExpiryDate,
  8227. PatientId: advice.PatientId,
  8228. Count: deliver_number, //出最小单位
  8229. WarehouseInfoId: warehouse.ID,
  8230. AdviceId: advice.ID,
  8231. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  8232. StorehouseId: storeConfig.DrugStorehouseOut,
  8233. IsCheck: 1,
  8234. }
  8235. drugflow := models.DrugFlow{
  8236. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8237. WarehouseOutId: warehouseout.ID,
  8238. DrugId: advice.DrugId,
  8239. Number: warehouse.Number,
  8240. ProductDate: warehouse.ProductDate,
  8241. ExpireDate: warehouse.ExpiryDate,
  8242. Count: deliver_number, //按最小单位计算,
  8243. Price: warehouse.RetailPrice,
  8244. Status: 1,
  8245. Ctime: time.Now().Unix(),
  8246. UserOrgId: orgID,
  8247. Manufacturer: warehouse.Manufacturer,
  8248. Dealer: warehouse.Dealer,
  8249. BatchNumber: warehouse.BatchNumber,
  8250. MaxUnit: drup.MinUnit,
  8251. ConsumableType: 3,
  8252. IsEdit: 1,
  8253. Creator: advice.ExecutionStaff,
  8254. IsSys: 1,
  8255. PatientId: advice.PatientId,
  8256. WarehousingDetailId: warehouse.ID,
  8257. AdviceId: advice.ID,
  8258. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  8259. StorehouseId: storeConfig.DrugStorehouseOut,
  8260. }
  8261. CreateDrugFlowOne(drugflow)
  8262. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  8263. if errOne != nil {
  8264. return errOne
  8265. }
  8266. //查询是否存在数据
  8267. details := &models.DrugAutomaticReduceDetail{
  8268. WarehouseOutId: warehouseout.ID,
  8269. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8270. PatientId: advice.PatientId,
  8271. Ctime: time.Now().Unix(),
  8272. Mtime: time.Now().Unix(),
  8273. Status: 1,
  8274. RecordTime: advice.RecordDate,
  8275. OrgId: orgID,
  8276. DrugId: advice.DrugId,
  8277. Count: deliver_number,
  8278. CountUnit: drup.MinUnit,
  8279. WarehouseInfoId: warehouse.ID,
  8280. StorehouseId: storeConfig.DrugStorehouseOut,
  8281. }
  8282. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  8283. if errTwo != nil {
  8284. return errTwo
  8285. }
  8286. // 出库完成后,要减去对应批次的库存数量
  8287. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  8288. var maxNumber int64 = 0
  8289. var minNumber int64 = 0
  8290. //var stock_max_number int64 = 0
  8291. //stock_max_number = warehouse.StockMaxNumber
  8292. maxNumber = deliver_number / drup.MinNumber
  8293. minNumber = deliver_number % drup.MinNumber
  8294. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  8295. minNumber = maxNumber
  8296. }
  8297. if drup.MaxUnit != drup.MinUnit {
  8298. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  8299. return errors.New("库存数量不足")
  8300. }
  8301. }
  8302. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  8303. if warehouse.StockMaxNumber < 0 {
  8304. warehouse.StockMaxNumber = 0
  8305. }
  8306. if warehouse.StockMinNumber < 0 {
  8307. warehouse.StockMinNumber = 0
  8308. }
  8309. warehouse.Mtime = time.Now().Unix()
  8310. if warehouse.StockMinNumber < minNumber {
  8311. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  8312. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  8313. } else {
  8314. if minNumber > 0 {
  8315. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  8316. }
  8317. if minNumber == 0 && maxNumber != 1 {
  8318. if warehouse.StockMinNumber > 0 {
  8319. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  8320. }
  8321. }
  8322. }
  8323. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  8324. if (warehouse.StockMinNumber - deliver_number) > 0 {
  8325. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  8326. //if warehouse.StockMinNumber == 0 {
  8327. // warehouse.StockMaxNumber = stock_max_number
  8328. //}
  8329. }
  8330. }
  8331. if drup.MaxUnit != drup.MinUnit {
  8332. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  8333. return errors.New("库存数量不足")
  8334. }
  8335. }
  8336. if warehouse.StockMinNumber <= 0 {
  8337. warehouse.StockMinNumber = 0
  8338. }
  8339. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  8340. if errThree != nil {
  8341. return errThree
  8342. }
  8343. return nil
  8344. } else {
  8345. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  8346. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  8347. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8348. WarehouseOutId: warehouseout.ID,
  8349. Status: 1,
  8350. Ctime: time.Now().Unix(),
  8351. Remark: warehouse.Remark,
  8352. OrgId: orgID,
  8353. Type: 1,
  8354. Manufacturer: warehouse.Manufacturer,
  8355. Dealer: warehouse.Dealer,
  8356. IsSys: 1,
  8357. SysRecordTime: advice.RecordDate,
  8358. DrugId: advice.DrugId,
  8359. Number: warehouse.Number,
  8360. BatchNumber: warehouse.BatchNumber,
  8361. Price: warehouse.RetailPrice,
  8362. CountUnit: drup.MinUnit,
  8363. ProductDate: warehouse.ProductDate,
  8364. ExpiryDate: warehouse.ExpiryDate,
  8365. PatientId: advice.PatientId,
  8366. Count: deliver_number,
  8367. WarehouseInfoId: warehouse.ID,
  8368. AdviceId: advice.ID,
  8369. StorehouseId: storeConfig.DrugStorehouseOut,
  8370. IsCheck: 1,
  8371. }
  8372. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  8373. if errOne != nil {
  8374. return errOne
  8375. }
  8376. details := &models.DrugAutomaticReduceDetail{
  8377. WarehouseOutId: warehouseout.ID,
  8378. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  8379. PatientId: advice.PatientId,
  8380. Ctime: time.Now().Unix(),
  8381. Mtime: time.Now().Unix(),
  8382. Status: 1,
  8383. RecordTime: advice.RecordDate,
  8384. OrgId: orgID,
  8385. DrugId: advice.DrugId,
  8386. Count: deliver_number,
  8387. CountUnit: drup.MinUnit,
  8388. WarehouseInfoId: warehouse.ID,
  8389. StorehouseId: storeConfig.DrugStorehouseOut,
  8390. }
  8391. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  8392. if errTwo != nil {
  8393. return errTwo
  8394. }
  8395. // 出库完成后,要将该批次库存清零
  8396. warehouse.StockMaxNumber = 0
  8397. warehouse.StockMinNumber = 0
  8398. warehouse.Mtime = time.Now().Unix()
  8399. info := models.XtDrugWarehouseInfo{
  8400. ID: warehouse.ID,
  8401. WarehousingId: warehouse.WarehousingId,
  8402. DrugId: warehouse.DrugId,
  8403. Number: warehouse.Number,
  8404. ProductDate: warehouse.ProductDate,
  8405. ExpiryDate: warehouse.ExpiryDate,
  8406. WarehousingCount: warehouse.WarehousingCount,
  8407. Price: warehouse.Price,
  8408. TotalPrice: warehouse.TotalPrice,
  8409. Dealer: warehouse.Dealer,
  8410. Manufacturer: warehouse.Manufacturer,
  8411. Remark: warehouse.Remark,
  8412. Ctime: warehouse.Ctime,
  8413. Mtime: warehouse.Mtime,
  8414. Status: 1,
  8415. OrgId: warehouse.OrgId,
  8416. IsReturn: warehouse.IsReturn,
  8417. WarehousingOrder: warehouse.WarehousingOrder,
  8418. Type: warehouse.Type,
  8419. RetailPrice: warehouse.RetailPrice,
  8420. RetailTotalPrice: warehouse.RetailPrice,
  8421. StockMaxNumber: 0,
  8422. StockMinNumber: 0,
  8423. BatchNumber: warehouse.BatchNumber,
  8424. MaxUnit: warehouse.MaxUnit,
  8425. WarehousingInfoId: warehouse.WarehousingInfoId,
  8426. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  8427. StorehouseId: storeConfig.DrugStorehouseOut,
  8428. IsCheck: 1,
  8429. }
  8430. //扣减库存
  8431. errThree := UpDateDrugWarehouseInfoByStock(&info)
  8432. if errThree != nil {
  8433. return errThree
  8434. }
  8435. drugflow := models.DrugFlow{
  8436. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8437. WarehouseOutId: warehouseout.ID,
  8438. DrugId: advice.DrugId,
  8439. Number: warehouse.Number,
  8440. ProductDate: warehouse.ProductDate,
  8441. ExpireDate: warehouse.ExpiryDate,
  8442. Count: deliver_number,
  8443. Price: warehouse.RetailPrice,
  8444. Status: 1,
  8445. Ctime: time.Now().Unix(),
  8446. UserOrgId: orgID,
  8447. Manufacturer: warehouse.Manufacturer,
  8448. Dealer: warehouse.Dealer,
  8449. BatchNumber: warehouse.BatchNumber,
  8450. MaxUnit: drup.MinUnit,
  8451. ConsumableType: 3,
  8452. IsEdit: 1,
  8453. Creator: advice.ExecutionStaff,
  8454. IsSys: 1,
  8455. PatientId: advice.PatientId,
  8456. WarehousingDetailId: warehouse.ID,
  8457. AdviceId: advice.ID,
  8458. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  8459. StorehouseId: storeConfig.DrugStorehouseOut,
  8460. }
  8461. CreateDrugFlowOne(drugflow)
  8462. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  8463. prescribingNumber_two_temp := deliver_number - stock_number
  8464. overPlusNumber := float64(prescribingNumber_two_temp)
  8465. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  8466. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  8467. NewDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  8468. }
  8469. return
  8470. }
  8471. // 药品出库 递归方式
  8472. func NewHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  8473. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  8474. var deliver_number int64 = 0
  8475. var stock_number int64 = 0
  8476. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  8477. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  8478. deliver_number = count
  8479. var min_price float64
  8480. if advice.PrescribingNumberUnit == drup.MaxUnit {
  8481. min_price = drup.RetailPrice
  8482. }
  8483. if advice.PrescribingNumberUnit == drup.MinUnit {
  8484. min_price = drup.MinPrice
  8485. }
  8486. if advice.PrescribingNumberUnit != drup.MaxUnit && advice.PrescribingNumberUnit != drup.MinUnit {
  8487. min_price = drup.RetailPrice
  8488. }
  8489. fmt.Println(min_price)
  8490. // 根据先进先出原则,查询最先入库的批次,进行出库
  8491. // 如果没有对应的库存,则报错
  8492. //开启事物
  8493. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  8494. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  8495. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  8496. var stockMax int64
  8497. var stockMin int64
  8498. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  8499. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  8500. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  8501. UpdateMinNumber(lastWarehouse.ID, stockMin)
  8502. }
  8503. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  8504. //查找药品信息
  8505. if err != nil {
  8506. return err
  8507. }
  8508. // 将该批次的剩余库存数量转换为拆零数量
  8509. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  8510. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  8511. if stock_number >= deliver_number {
  8512. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  8513. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8514. WarehouseOutId: warehouseout.ID,
  8515. Status: 1,
  8516. Ctime: time.Now().Unix(),
  8517. Remark: warehouse.Remark,
  8518. OrgId: orgID,
  8519. Type: 1,
  8520. Manufacturer: warehouse.Manufacturer,
  8521. Dealer: warehouse.Dealer,
  8522. IsSys: 1,
  8523. SysRecordTime: advice.RecordDate,
  8524. DrugId: advice.DrugId,
  8525. Number: warehouse.Number,
  8526. BatchNumber: warehouse.BatchNumber,
  8527. Price: warehouse.RetailPrice,
  8528. CountUnit: drup.MinUnit,
  8529. RetailPrice: warehouse.RetailPrice,
  8530. ProductDate: warehouse.ProductDate,
  8531. ExpiryDate: warehouse.ExpiryDate,
  8532. PatientId: advice.PatientId,
  8533. WarehouseInfoId: warehouse.ID,
  8534. Count: deliver_number,
  8535. AdviceId: advice.ID,
  8536. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  8537. StorehouseId: storeConfig.DrugStorehouseOut,
  8538. IsCheck: 1,
  8539. }
  8540. drugflow := models.DrugFlow{
  8541. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8542. WarehouseOutId: warehouseout.ID,
  8543. DrugId: advice.DrugId,
  8544. Number: warehouse.Number,
  8545. ProductDate: warehouse.ProductDate,
  8546. ExpireDate: warehouse.ExpiryDate,
  8547. Count: deliver_number,
  8548. Price: warehouse.RetailPrice,
  8549. Status: 1,
  8550. Ctime: time.Now().Unix(),
  8551. UserOrgId: orgID,
  8552. Manufacturer: warehouse.Manufacturer,
  8553. Dealer: warehouse.Dealer,
  8554. BatchNumber: warehouse.BatchNumber,
  8555. MaxUnit: drup.MinUnit,
  8556. ConsumableType: 3,
  8557. IsEdit: 1,
  8558. Creator: advice.ExecutionStaff,
  8559. IsSys: 1,
  8560. PatientId: advice.PatientId,
  8561. SystemTime: advice.RecordDate,
  8562. WarehousingDetailId: warehouse.ID,
  8563. AdviceId: advice.ID,
  8564. StorehouseId: storeConfig.DrugStorehouseOut,
  8565. }
  8566. CreateDrugFlowOne(drugflow)
  8567. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  8568. if errOne != nil {
  8569. return errOne
  8570. }
  8571. details := &models.DrugAutomaticReduceDetail{
  8572. WarehouseOutId: warehouseout.ID,
  8573. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8574. PatientId: advice.PatientId,
  8575. Ctime: time.Now().Unix(),
  8576. Mtime: time.Now().Unix(),
  8577. Status: 1,
  8578. RecordTime: advice.RecordDate,
  8579. OrgId: orgID,
  8580. DrugId: advice.DrugId,
  8581. Count: deliver_number,
  8582. CountUnit: drup.MinUnit,
  8583. WarehouseInfoId: warehouse.ID,
  8584. AdviceId: advice.ID,
  8585. StorehouseId: storeConfig.DrugStorehouseOut,
  8586. }
  8587. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  8588. if errTwo != nil {
  8589. return errTwo
  8590. }
  8591. // 出库完成后,要减去对应批次的库存数量
  8592. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  8593. var maxNumber int64 = 0
  8594. var minNumber int64 = 0
  8595. //var stock_max_number int64 = 0
  8596. //stock_max_number = warehouse.StockMaxNumber
  8597. maxNumber = deliver_number / drup.MinNumber
  8598. minNumber = deliver_number % drup.MinNumber
  8599. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  8600. minNumber = maxNumber
  8601. }
  8602. if drup.MaxUnit != drup.MinUnit {
  8603. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  8604. return errors.New("库存数量不足")
  8605. }
  8606. }
  8607. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  8608. if warehouse.StockMaxNumber < 0 {
  8609. warehouse.StockMaxNumber = 0
  8610. }
  8611. if warehouse.StockMinNumber < 0 {
  8612. warehouse.StockMinNumber = 0
  8613. }
  8614. warehouse.Mtime = time.Now().Unix()
  8615. if warehouse.StockMinNumber < minNumber {
  8616. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  8617. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  8618. } else {
  8619. if minNumber > 0 {
  8620. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  8621. }
  8622. if minNumber == 0 && maxNumber != 1 {
  8623. if warehouse.StockMinNumber > 0 {
  8624. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  8625. }
  8626. }
  8627. }
  8628. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  8629. if (warehouse.StockMinNumber - deliver_number) > 0 {
  8630. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  8631. //if warehouse.StockMinNumber == 0 {
  8632. // warehouse.StockMaxNumber = stock_max_number
  8633. //}
  8634. }
  8635. }
  8636. if drup.MaxUnit != drup.MinUnit {
  8637. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  8638. return errors.New("库存数量不足")
  8639. }
  8640. }
  8641. if warehouse.StockMinNumber <= 0 {
  8642. warehouse.StockMinNumber = 0
  8643. }
  8644. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  8645. if errThree != nil {
  8646. return errThree
  8647. }
  8648. return nil
  8649. } else {
  8650. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  8651. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  8652. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8653. WarehouseOutId: warehouseout.ID,
  8654. Status: 1,
  8655. Ctime: time.Now().Unix(),
  8656. Remark: warehouse.Remark,
  8657. OrgId: orgID,
  8658. Type: 1,
  8659. Manufacturer: warehouse.Manufacturer,
  8660. Dealer: warehouse.Dealer,
  8661. IsSys: 1,
  8662. SysRecordTime: advice.RecordDate,
  8663. DrugId: advice.DrugId,
  8664. Number: warehouse.Number,
  8665. BatchNumber: warehouse.BatchNumber,
  8666. Price: warehouse.RetailPrice,
  8667. RetailPrice: warehouse.RetailPrice,
  8668. ProductDate: warehouse.ProductDate,
  8669. ExpiryDate: warehouse.ExpiryDate,
  8670. PatientId: advice.PatientId,
  8671. WarehouseInfoId: warehouse.ID,
  8672. Count: stock_number,
  8673. CountUnit: drup.MinUnit,
  8674. AdviceId: advice.ID,
  8675. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  8676. StorehouseId: storeConfig.DrugStorehouseOut,
  8677. IsCheck: 1,
  8678. }
  8679. drugflow := models.DrugFlow{
  8680. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8681. WarehouseOutId: warehouseout.ID,
  8682. DrugId: advice.DrugId,
  8683. Number: warehouse.Number,
  8684. ProductDate: warehouse.ProductDate,
  8685. ExpireDate: warehouse.ExpiryDate,
  8686. Count: stock_number,
  8687. Price: warehouse.RetailPrice,
  8688. Status: 1,
  8689. Ctime: time.Now().Unix(),
  8690. UserOrgId: orgID,
  8691. Manufacturer: warehouse.Manufacturer,
  8692. Dealer: warehouse.Dealer,
  8693. BatchNumber: warehouse.BatchNumber,
  8694. MaxUnit: drup.MinUnit,
  8695. ConsumableType: 3,
  8696. IsEdit: 1,
  8697. Creator: advice.ExecutionStaff,
  8698. IsSys: 1,
  8699. PatientId: advice.PatientId,
  8700. SystemTime: advice.RecordDate,
  8701. WarehousingDetailId: warehouse.ID,
  8702. AdviceId: advice.ID,
  8703. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  8704. StorehouseId: storeConfig.DrugStorehouseOut,
  8705. }
  8706. CreateDrugFlowOne(drugflow)
  8707. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  8708. if errOne != nil {
  8709. return errOne
  8710. }
  8711. details := &models.DrugAutomaticReduceDetail{
  8712. WarehouseOutId: warehouseout.ID,
  8713. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  8714. PatientId: advice.PatientId,
  8715. Ctime: time.Now().Unix(),
  8716. Mtime: time.Now().Unix(),
  8717. Status: 1,
  8718. RecordTime: advice.RecordDate,
  8719. OrgId: orgID,
  8720. DrugId: advice.DrugId,
  8721. Count: stock_number,
  8722. CountUnit: drup.MinUnit,
  8723. WarehouseInfoId: warehouse.ID,
  8724. AdviceId: advice.ID,
  8725. StorehouseId: storeConfig.DrugStorehouseOut,
  8726. }
  8727. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  8728. if errTwo != nil {
  8729. return errTwo
  8730. }
  8731. info := models.XtDrugWarehouseInfo{
  8732. ID: warehouse.ID,
  8733. WarehousingId: warehouse.WarehousingId,
  8734. DrugId: warehouse.DrugId,
  8735. Number: warehouse.Number,
  8736. ProductDate: warehouse.ProductDate,
  8737. ExpiryDate: warehouse.ExpiryDate,
  8738. WarehousingCount: warehouse.WarehousingCount,
  8739. Price: warehouse.RetailPrice,
  8740. TotalPrice: warehouse.TotalPrice,
  8741. Dealer: warehouse.Dealer,
  8742. Manufacturer: warehouse.Manufacturer,
  8743. Remark: warehouse.Remark,
  8744. Ctime: warehouse.Ctime,
  8745. Mtime: warehouse.Mtime,
  8746. Status: 1,
  8747. OrgId: warehouse.OrgId,
  8748. IsReturn: warehouse.IsReturn,
  8749. WarehousingOrder: warehouse.WarehousingOrder,
  8750. Type: warehouse.Type,
  8751. RetailPrice: warehouse.RetailPrice,
  8752. RetailTotalPrice: warehouse.RetailPrice,
  8753. StockMaxNumber: 0,
  8754. StockMinNumber: 0,
  8755. BatchNumber: warehouse.BatchNumber,
  8756. MaxUnit: warehouse.MaxUnit,
  8757. WarehousingInfoId: warehouse.WarehousingInfoId,
  8758. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  8759. StorehouseId: storeConfig.DrugStorehouseOut,
  8760. IsCheck: 1,
  8761. }
  8762. errThree := UpDateDrugWarehouseInfoByStock(&info)
  8763. if errThree != nil {
  8764. return errThree
  8765. }
  8766. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  8767. prescribingNumber_two_temp := deliver_number - stock_number
  8768. overPlusNumber := float64(prescribingNumber_two_temp)
  8769. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  8770. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  8771. NewHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  8772. }
  8773. return
  8774. }
  8775. // 药品出库 递归方式
  8776. func HisAuToReduce(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  8777. //查询最后一次出库数据
  8778. drugInfo, _ := GetDrugWarehouseOutInfoIsExit(advice.DrugId, advice.PatientId, advice.RecordDate)
  8779. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  8780. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  8781. var count_number int64
  8782. var device_number int64
  8783. if advice.PrescribingNumberUnit == drup.MaxUnit {
  8784. device_number = count * drup.MinNumber
  8785. }
  8786. if advice.PrescribingNumberUnit == drup.MinUnit {
  8787. device_number = count
  8788. }
  8789. if drugInfo.CountUnit == drup.MaxUnit {
  8790. count_number = drugInfo.Count * drup.MinNumber
  8791. }
  8792. if drugInfo.CountUnit == drup.MinUnit {
  8793. count_number = drugInfo.Count
  8794. }
  8795. // 比较数据大小
  8796. //如果当期医嘱数量小于最后一次出库则退库
  8797. if device_number < count_number {
  8798. var cha_number int64
  8799. var maxNumber int64
  8800. var minNumber int64
  8801. cha_number = count_number - device_number
  8802. maxNumber = cha_number / drup.MinNumber
  8803. minNumber = cha_number % drup.MinNumber
  8804. //更改库存
  8805. ModifyDrugWarehouse(drugInfo.WarehouseInfoId, maxNumber, minNumber)
  8806. //修改出库表
  8807. info := models.DrugWarehouseOutInfo{
  8808. Count: device_number,
  8809. CountUnit: advice.PrescribingNumberUnit,
  8810. }
  8811. ModifyDrugWarehouseOutInfo(info, advice.DrugId, advice.PatientId, advice.RecordDate)
  8812. //修改自动出库表
  8813. detail := models.DrugAutomaticReduceDetail{
  8814. Count: device_number,
  8815. CountUnit: advice.PrescribingNumberUnit,
  8816. }
  8817. ModifyAutoMaticReduce(detail, advice.PatientId, advice.RecordDate, advice.DrugId)
  8818. //修改流水
  8819. flow := models.DrugFlow{
  8820. Count: device_number,
  8821. MaxUnit: drup.MinUnit,
  8822. }
  8823. ModifyDrugFlow(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId)
  8824. }
  8825. //如果当期医嘱数量大于最后一次出库则需要出库
  8826. if device_number > count_number {
  8827. var cha_number int64
  8828. cha_number = device_number - count_number
  8829. // 根据先进先出原则,查询最先入库的批次,进行出库
  8830. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  8831. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  8832. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  8833. var stockMax int64
  8834. var stockMin int64
  8835. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  8836. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  8837. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  8838. UpdateMinNumber(lastWarehouse.ID, stockMin)
  8839. }
  8840. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  8841. //查找药品信息
  8842. //baseInfo, _ := FindBaseDrugLibRecord(advice.UserOrgId, advice.DrugId)
  8843. if err != nil {
  8844. return err
  8845. }
  8846. // 将该批次的剩余库存数量转换为拆零数量
  8847. var stock_number int64
  8848. var maxNumber int64
  8849. var minNumber int64
  8850. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  8851. //如果库存数量大于出库数据,正常出库
  8852. if stock_number >= cha_number {
  8853. //扣减库存
  8854. maxNumber = cha_number / drup.MinNumber
  8855. minNumber = cha_number % drup.MinNumber
  8856. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  8857. minNumber = maxNumber
  8858. }
  8859. if drup.MaxUnit != drup.MinUnit {
  8860. if warehouse.StockMaxNumber < maxNumber {
  8861. return errors.New("库存数量不足")
  8862. }
  8863. }
  8864. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  8865. if warehouse.StockMaxNumber < 0 {
  8866. warehouse.StockMaxNumber = 0
  8867. }
  8868. warehouse.Mtime = time.Now().Unix()
  8869. if warehouse.StockMinNumber < minNumber {
  8870. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  8871. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  8872. } else {
  8873. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  8874. }
  8875. if drup.MaxUnit != drup.MinUnit {
  8876. if warehouse.StockMaxNumber < 0 {
  8877. return errors.New("库存数量不足")
  8878. }
  8879. }
  8880. //扣减库存232332332332
  8881. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  8882. if errThree != nil {
  8883. return errThree
  8884. }
  8885. //修改出库表
  8886. info := models.DrugWarehouseOutInfo{
  8887. Count: device_number,
  8888. CountUnit: advice.PrescribingNumberUnit,
  8889. }
  8890. ModifyDrugWarehouseOutInfoOne(info, advice.DrugId, advice.PatientId, advice.RecordDate, warehouse.ID)
  8891. //修改自动出库表
  8892. detail := models.DrugAutomaticReduceDetail{
  8893. Count: device_number,
  8894. CountUnit: advice.PrescribingNumberUnit,
  8895. }
  8896. ModifyAutoMaticReduceOne(detail, advice.PatientId, advice.RecordDate, advice.DrugId, warehouse.ID)
  8897. //修改流水
  8898. flow := models.DrugFlow{
  8899. Count: device_number,
  8900. MaxUnit: advice.PrescribingNumberUnit,
  8901. }
  8902. ModifyDrugFlowOne(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId, warehouse.ID)
  8903. }
  8904. //如果库存数量小于出库数据,则需要出库到下一个批次
  8905. if stock_number < cha_number {
  8906. //修改出库表
  8907. info := models.DrugWarehouseOutInfo{
  8908. Count: cha_number,
  8909. CountUnit: advice.PrescribingNumberUnit,
  8910. }
  8911. ModifyDrugWarehouseOutInfoOne(info, advice.DrugId, advice.PatientId, advice.RecordDate, warehouse.ID)
  8912. //修改自动出库表
  8913. detail := models.DrugAutomaticReduceDetail{
  8914. Count: cha_number,
  8915. CountUnit: advice.PrescribingNumberUnit,
  8916. }
  8917. ModifyAutoMaticReduceOne(detail, advice.PatientId, advice.RecordDate, advice.DrugId, warehouse.ID)
  8918. //修改流水
  8919. flow := models.DrugFlow{
  8920. Count: cha_number,
  8921. MaxUnit: advice.PrescribingNumberUnit,
  8922. }
  8923. ModifyDrugFlowOne(flow, drugInfo.WarehouseOutOrderNumber, advice.DrugId, advice.PatientId, warehouse.ID)
  8924. druginfo := &models.XtDrugWarehouseInfo{
  8925. ID: warehouse.ID,
  8926. WarehousingId: warehouse.WarehousingId,
  8927. DrugId: warehouse.DrugId,
  8928. Number: warehouse.Number,
  8929. ProductDate: warehouse.ProductDate,
  8930. ExpiryDate: warehouse.ExpiryDate,
  8931. WarehousingCount: warehouse.WarehousingCount,
  8932. Price: warehouse.Price,
  8933. TotalPrice: warehouse.TotalPrice,
  8934. Dealer: warehouse.Dealer,
  8935. Manufacturer: warehouse.Manufacturer,
  8936. Remark: warehouse.Remark,
  8937. Ctime: warehouse.Ctime,
  8938. Mtime: warehouse.Mtime,
  8939. Status: 1,
  8940. OrgId: warehouse.OrgId,
  8941. IsReturn: warehouse.IsReturn,
  8942. WarehousingOrder: warehouse.WarehousingOrder,
  8943. Type: warehouse.Type,
  8944. RetailPrice: warehouse.RetailPrice,
  8945. RetailTotalPrice: warehouse.RetailPrice,
  8946. StockMaxNumber: 0,
  8947. StockMinNumber: 0,
  8948. BatchNumber: warehouse.BatchNumber,
  8949. MaxUnit: warehouse.MaxUnit,
  8950. WarehousingInfoId: warehouse.WarehousingInfoId,
  8951. StorehouseId: storeConfig.DrugStorehouseOut,
  8952. IsCheck: 1,
  8953. }
  8954. errThree := UpDateDrugWarehouseInfoByStock(druginfo)
  8955. if errThree != nil {
  8956. return errThree
  8957. }
  8958. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  8959. prescribingNumber_two_temp := cha_number - stock_number
  8960. overPlusNumber := float64(prescribingNumber_two_temp)
  8961. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  8962. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  8963. NewHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  8964. }
  8965. }
  8966. return
  8967. }
  8968. // 采购管理耗材出库逻辑
  8969. 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) {
  8970. var deliver_number int64 = 0
  8971. var stock_number int64 = 0
  8972. var maxNumber int64 = 0
  8973. deliver_number = goods.Count
  8974. // 根据先进先出原则,查询最先入库的批次,进行出库
  8975. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  8976. // 如果没有对应的库存,则报错
  8977. warehouse, err := FindFirstWarehousingInfoByStockNight(goods.GoodId, goods.GoodTypeId, warehouse_out_id, goods.IsSource, houseConfig.StorehouseOutInfo)
  8978. if err != nil {
  8979. return err
  8980. }
  8981. stock_number = warehouse.StockCount
  8982. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  8983. if stock_number >= deliver_number {
  8984. warehouseOutInfo := &models.WarehouseOutInfo{
  8985. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  8986. WarehouseOutId: goods.WarehouseOutId,
  8987. WarehouseInfotId: warehouse.ID,
  8988. Status: 1,
  8989. Ctime: time.Now().Unix(),
  8990. Remark: goods.Remark,
  8991. OrgId: orgID,
  8992. Type: 1,
  8993. Manufacturer: goods.Manufacturer,
  8994. Dealer: goods.Dealer,
  8995. IsSys: 0,
  8996. SysRecordTime: record_time,
  8997. GoodTypeId: goods.GoodTypeId,
  8998. GoodId: goods.GoodId,
  8999. ExpiryDate: goods.ExpiryDate,
  9000. ProductDate: goods.ProductDate,
  9001. Number: warehouse.Number,
  9002. Price: goods.Price,
  9003. LicenseNumber: goods.LicenseNumber,
  9004. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  9005. StorehouseId: warehouse.StorehouseId,
  9006. IsCheck: 1,
  9007. }
  9008. warehouseOutInfo.Count = goods.Count
  9009. //添加出库单详情
  9010. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  9011. if errOne != nil {
  9012. return errOne
  9013. }
  9014. stockFlow := models.VmStockFlow{
  9015. WarehousingId: warehouse.ID,
  9016. GoodId: goods.GoodId,
  9017. Number: warehouse.Number,
  9018. LicenseNumber: goods.LicenseNumber,
  9019. Count: goods.Count,
  9020. UserOrgId: goods.OrgId,
  9021. PatientId: goods.PatientId,
  9022. SystemTime: record_time,
  9023. ConsumableType: 2,
  9024. IsSys: goods.IsSys,
  9025. WarehousingOrder: "",
  9026. WarehouseOutId: goods.WarehouseOutId,
  9027. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  9028. IsEdit: 1,
  9029. CancelStockId: 0,
  9030. CancelOrderNumber: "",
  9031. Manufacturer: goods.Manufacturer,
  9032. Dealer: goods.Dealer,
  9033. Creator: creator,
  9034. UpdateCreator: 0,
  9035. Status: 1,
  9036. Ctime: time.Now().Unix(),
  9037. Mtime: 0,
  9038. Price: warehouse.PackingPrice,
  9039. WarehousingDetailId: 0,
  9040. WarehouseOutDetailId: warehouseOutInfo.ID,
  9041. CancelOutDetailId: 0,
  9042. ProductDate: goods.ProductDate,
  9043. ExpireDate: goods.ExpiryDate,
  9044. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  9045. SupplyWarehouseId: warehouseOut.SupplyWarehouseId,
  9046. StorehouseId: warehouse.StorehouseId,
  9047. }
  9048. //创建出库流水
  9049. CreateStockFlowOne(stockFlow)
  9050. maxNumber = goods.Count
  9051. if warehouse.StockCount < maxNumber {
  9052. return errors.New("库存数量不足")
  9053. }
  9054. warehouse.StockCount = warehouse.StockCount - maxNumber
  9055. warehouse.Mtime = time.Now().Unix()
  9056. //扣减库存
  9057. errThree := UpDateWarehouseInfoByStock(&warehouse)
  9058. //出库数量增加
  9059. AddGoodSumOutCount(houseConfig.StorehouseOutInfo, warehouse.GoodId, warehouse.OrgId, goods.Count)
  9060. ModifyGoodReduceInformationSix(warehouse.GoodId, maxNumber, warehouse.OrgId, warehouse.PackingPrice)
  9061. if errThree != nil {
  9062. return errThree
  9063. }
  9064. } else {
  9065. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  9066. warehouseOutInfo := &models.WarehouseOutInfo{
  9067. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  9068. WarehouseOutId: goods.WarehouseOutId,
  9069. WarehouseInfotId: warehouse.ID,
  9070. Status: 1,
  9071. Ctime: time.Now().Unix(),
  9072. Remark: goods.Remark,
  9073. OrgId: orgID,
  9074. Type: 1,
  9075. Manufacturer: goods.Manufacturer,
  9076. Dealer: goods.Dealer,
  9077. IsSys: 0,
  9078. SysRecordTime: record_time,
  9079. GoodTypeId: goods.GoodTypeId,
  9080. GoodId: goods.GoodId,
  9081. ExpiryDate: goods.ExpiryDate,
  9082. ProductDate: goods.ProductDate,
  9083. Number: warehouse.Number,
  9084. Price: warehouse.PackingPrice,
  9085. LicenseNumber: goods.LicenseNumber,
  9086. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  9087. StorehouseId: warehouse.StorehouseId,
  9088. IsCheck: 1,
  9089. }
  9090. warehouseOutInfo.Count = stock_number
  9091. //查询是否已经生成出库单
  9092. info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  9093. if errcodes == gorm.ErrRecordNotFound {
  9094. //创建出库单
  9095. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  9096. if errOne != nil {
  9097. return errOne
  9098. }
  9099. } else {
  9100. warehouseOutInfo.Count = info.Count + stock_number
  9101. UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  9102. }
  9103. stockFlow := models.VmStockFlow{
  9104. WarehousingId: warehouse.ID,
  9105. GoodId: goods.GoodId,
  9106. Number: warehouse.Number,
  9107. LicenseNumber: goods.LicenseNumber,
  9108. Count: stock_number,
  9109. UserOrgId: goods.OrgId,
  9110. PatientId: 0,
  9111. SystemTime: record_time,
  9112. ConsumableType: 2,
  9113. IsSys: 0,
  9114. WarehousingOrder: "",
  9115. WarehouseOutId: goods.WarehouseOutId,
  9116. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  9117. IsEdit: 1,
  9118. CancelStockId: 0,
  9119. CancelOrderNumber: "",
  9120. Manufacturer: goods.Manufacturer,
  9121. Dealer: goods.Dealer,
  9122. Creator: creator,
  9123. UpdateCreator: 0,
  9124. Status: 1,
  9125. Ctime: time.Now().Unix(),
  9126. Mtime: 0,
  9127. Price: warehouse.PackingPrice,
  9128. WarehousingDetailId: 0,
  9129. WarehouseOutDetailId: warehouseOutInfo.ID,
  9130. CancelOutDetailId: 0,
  9131. ProductDate: goods.ProductDate,
  9132. ExpireDate: goods.ExpiryDate,
  9133. SupplyCancelOutId: warehouseOut.SupplyCancelOutId,
  9134. SupplyWarehouseId: warehouseOut.SupplyWarehouseId,
  9135. StorehouseId: warehouse.StorehouseId,
  9136. }
  9137. //创建出库明细
  9138. CreateStockFlowOne(stockFlow)
  9139. // 出库完成后,要将该批次库存清零
  9140. ModifyGoodReduceInformationSix(warehouse.GoodId, warehouse.StockCount, warehouse.OrgId, warehouse.PackingPrice)
  9141. warehouse.StockCount = 0
  9142. warehouse.Mtime = time.Now().Unix()
  9143. errThree := UpDateWarehouseInfoByStock(&warehouse)
  9144. if errThree != nil {
  9145. return errThree
  9146. }
  9147. //出库数量增加
  9148. AddGoodSumOutCount(houseConfig.StorehouseOutInfo, warehouse.GoodId, warehouse.OrgId, warehouse.StockCount)
  9149. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  9150. goods.Count = deliver_number - stock_number
  9151. ConsumablesDeliveryNight(orgID, record_time, goods, warehouseOut, count, creator, warehouse_out_id, cancel_out_id)
  9152. }
  9153. return nil
  9154. }
  9155. func FindFirstWarehousingInfoByStockNight(good_id int64, good_type_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  9156. if is_source == 1 {
  9157. 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
  9158. }
  9159. if is_source == 2 || is_source == 0 {
  9160. 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
  9161. }
  9162. return info, err
  9163. }
  9164. // 药品出库 递归方式
  9165. 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) {
  9166. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  9167. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  9168. var deliver_number int64 = 0
  9169. var stock_number int64 = 0
  9170. var retail_price float64
  9171. if advice.CountUnit == drup.MaxUnit {
  9172. deliver_number = prescribingNumber * drup.MinNumber
  9173. formatInt := strconv.FormatInt(prescribingNumber, 10)
  9174. floatcount, _ := strconv.ParseFloat(formatInt, 64)
  9175. retail_price = floatcount * advice.Price
  9176. } else {
  9177. deliver_number = prescribingNumber
  9178. formatInt := strconv.FormatInt(prescribingNumber, 10)
  9179. floatcount, _ := strconv.ParseFloat(formatInt, 64)
  9180. retail_price = floatcount * advice.Price
  9181. }
  9182. // 根据先进先出原则,查询最先入库的批次,进行出库
  9183. // 如果没有对应的库存,则报错
  9184. //开启事物
  9185. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, houseConfig.DrugStorehouseOut)
  9186. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  9187. var stockMax int64
  9188. var stockMin int64
  9189. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  9190. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  9191. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  9192. UpdateMinNumber(lastWarehouse.ID, stockMin)
  9193. }
  9194. warehouse, err := FindLastDrugWarehousingInfoByIDSix(advice.DrugId, warehouse_out_id, advice.IsSource, houseConfig.DrugStorehouseOut)
  9195. if err != nil {
  9196. return err
  9197. }
  9198. // 将该批次的剩余库存数量转换为拆零数量
  9199. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  9200. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  9201. if stock_number >= deliver_number {
  9202. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  9203. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  9204. WarehouseOutId: advice.WarehouseOutId,
  9205. Status: 1,
  9206. Ctime: time.Now().Unix(),
  9207. Remark: advice.Remark,
  9208. OrgId: orgID,
  9209. Type: 1,
  9210. Manufacturer: advice.Manufacturer,
  9211. Dealer: warehouse.Dealer,
  9212. IsSys: 0,
  9213. SysRecordTime: advice.SysRecordTime,
  9214. DrugId: advice.DrugId,
  9215. ExpiryDate: advice.ExpiryDate,
  9216. ProductDate: advice.ProductDate,
  9217. Number: advice.Number,
  9218. BatchNumber: warehouse.BatchNumber,
  9219. Count: deliver_number,
  9220. RetailPrice: advice.RetailPrice,
  9221. Price: advice.Price,
  9222. WarehouseInfoId: warehouse.ID,
  9223. CountUnit: drup.MinUnit,
  9224. AdviceId: advice.ID,
  9225. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  9226. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  9227. StorehouseId: warehouse.StorehouseId,
  9228. IsCheck: 1,
  9229. }
  9230. warehouseOutInfo.Count = prescribingNumber
  9231. warehouseOutInfo.CountUnit = advice.CountUnit
  9232. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  9233. if errOne != nil {
  9234. return errOne
  9235. }
  9236. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  9237. drugflow := models.DrugFlow{
  9238. WarehouseOutId: advice.WarehouseOutId,
  9239. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  9240. DrugId: advice.DrugId,
  9241. Number: warehouse.Number,
  9242. ProductDate: advice.ProductDate,
  9243. ExpireDate: advice.ExpiryDate,
  9244. Count: deliver_number,
  9245. Price: warehouse.RetailPrice,
  9246. Status: 1,
  9247. Ctime: time.Now().Unix(),
  9248. UserOrgId: advice.OrgId,
  9249. Manufacturer: advice.Manufacturer,
  9250. Dealer: advice.Dealer,
  9251. BatchNumber: warehouse.BatchNumber,
  9252. MaxUnit: drup.MinUnit,
  9253. ConsumableType: 2,
  9254. IsEdit: 1,
  9255. Creator: 0,
  9256. IsSys: 0,
  9257. WarehouseOutDetailId: drugWareInfo.ID,
  9258. AdviceId: advice.ID,
  9259. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  9260. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  9261. SystemTime: advice.SysRecordTime,
  9262. StorehouseId: warehouse.StorehouseId,
  9263. WarehousingDetailId: warehouse.ID,
  9264. WarehousingId: warehouse.ID,
  9265. RetailPrice: retail_price,
  9266. LastPrice: warehouse.Price,
  9267. }
  9268. CreateDrugFlowOne(drugflow)
  9269. // 出库完成后,要减去对应批次的库存数量
  9270. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  9271. var maxNumber int64 = 0
  9272. var minNumber int64 = 0
  9273. if advice.CountUnit == drup.MinUnit {
  9274. maxNumber = prescribingNumber / drup.MinNumber
  9275. minNumber = prescribingNumber % drup.MinNumber
  9276. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  9277. minNumber = maxNumber
  9278. }
  9279. } else {
  9280. maxNumber = prescribingNumber
  9281. }
  9282. if warehouse.StockMaxNumber < maxNumber {
  9283. return errors.New("库存数量不足")
  9284. }
  9285. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  9286. if warehouse.StockMaxNumber < 0 {
  9287. warehouse.StockMaxNumber = 0
  9288. }
  9289. warehouse.Mtime = time.Now().Unix()
  9290. if warehouse.StockMinNumber < minNumber {
  9291. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  9292. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  9293. } else {
  9294. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  9295. }
  9296. if warehouse.StockMaxNumber < 0 {
  9297. return errors.New("库存数量不足")
  9298. }
  9299. if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
  9300. warehouse.StockMinNumber = 0
  9301. }
  9302. if warehouse.StockMaxNumber > 0 {
  9303. var sum_count int64
  9304. sum_count = maxNumber * drup.MinNumber
  9305. ModifyDrugReduceInformationSix(warehouse.DrugId, sum_count, warehouse.OrgId, warehouse.RetailPrice)
  9306. //出库数量增加
  9307. AddDrugSumOutCount(houseConfig.DrugStorehouseOut, sum_count, warehouse.OrgId, warehouse.DrugId)
  9308. }
  9309. if warehouse.StockMinNumber > 0 {
  9310. ModifyDrugReduceInformationSix(warehouse.DrugId, minNumber, warehouse.OrgId, warehouse.RetailPrice)
  9311. //出库数量增加
  9312. AddDrugSumOutCount(houseConfig.DrugStorehouseOut, minNumber, warehouse.OrgId, warehouse.DrugId)
  9313. }
  9314. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  9315. if errThree != nil {
  9316. return errThree
  9317. }
  9318. return nil
  9319. } else {
  9320. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  9321. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  9322. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  9323. WarehouseOutId: advice.WarehouseOutId,
  9324. Status: 1,
  9325. Ctime: time.Now().Unix(),
  9326. Remark: advice.Remark,
  9327. OrgId: orgID,
  9328. Type: 1,
  9329. Manufacturer: advice.Manufacturer,
  9330. Dealer: warehouse.Dealer,
  9331. IsSys: 0,
  9332. SysRecordTime: advice.SysRecordTime,
  9333. DrugId: advice.DrugId,
  9334. ExpiryDate: warehouse.ExpiryDate,
  9335. ProductDate: warehouse.ProductDate,
  9336. Number: warehouse.Number,
  9337. BatchNumber: warehouse.BatchNumber,
  9338. Count: stock_number,
  9339. RetailPrice: advice.RetailPrice,
  9340. Price: advice.Price,
  9341. WarehouseInfoId: warehouse.ID,
  9342. CountUnit: drup.MinUnit,
  9343. AdviceId: advice.ID,
  9344. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  9345. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  9346. StorehouseId: warehouse.StorehouseId,
  9347. IsCheck: 1,
  9348. }
  9349. warehouseOutInfo.Count = stock_number
  9350. //warehouseOutInfo.CountUnit = advice.CountUnit
  9351. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  9352. if errOne != nil {
  9353. return errOne
  9354. }
  9355. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  9356. drugflow := models.DrugFlow{
  9357. WarehouseOutId: advice.WarehouseOutId,
  9358. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  9359. DrugId: advice.DrugId,
  9360. Number: warehouse.Number,
  9361. ProductDate: advice.ProductDate,
  9362. ExpireDate: advice.ExpiryDate,
  9363. Count: stock_number,
  9364. Price: warehouse.RetailPrice,
  9365. Status: 1,
  9366. Ctime: time.Now().Unix(),
  9367. UserOrgId: advice.OrgId,
  9368. Manufacturer: advice.Manufacturer,
  9369. Dealer: advice.Dealer,
  9370. BatchNumber: warehouse.BatchNumber,
  9371. MaxUnit: drup.MinUnit,
  9372. ConsumableType: 2,
  9373. IsEdit: 1,
  9374. Creator: 0,
  9375. IsSys: 0,
  9376. WarehouseOutDetailId: drugWareInfo.ID,
  9377. AdviceId: advice.ID,
  9378. SupplyCancelOutId: warehouseout.SupplyCancelOutId,
  9379. SupplyWarehouseId: warehouseout.SupplyWarehouseId,
  9380. SystemTime: advice.SysRecordTime,
  9381. StorehouseId: warehouse.StorehouseId,
  9382. WarehousingDetailId: warehouse.ID,
  9383. WarehousingId: warehouse.ID,
  9384. RetailPrice: retail_price,
  9385. LastPrice: warehouse.Price,
  9386. }
  9387. CreateDrugFlowOne(drugflow)
  9388. if warehouse.StockMaxNumber > 0 {
  9389. var sum_count int64
  9390. sum_count = warehouse.StockMaxNumber * drup.MinNumber
  9391. ModifyDrugReduceInformationSix(warehouse.DrugId, sum_count, warehouse.OrgId, warehouse.RetailPrice)
  9392. AddDrugSumOutCount(houseConfig.DrugStorehouseOut, sum_count, warehouse.OrgId, warehouse.DrugId)
  9393. }
  9394. if warehouse.StockMinNumber > 0 {
  9395. ModifyDrugReduceInformationSix(warehouse.DrugId, warehouse.StockMinNumber, warehouse.OrgId, warehouse.RetailPrice)
  9396. AddDrugSumOutCount(houseConfig.DrugStorehouseOut, warehouse.StockMinNumber, warehouse.OrgId, warehouse.DrugId)
  9397. }
  9398. // 出库完成后,要将该批次库存清零
  9399. warehouse.StockMaxNumber = 0
  9400. warehouse.StockMinNumber = 0
  9401. warehouse.Mtime = time.Now().Unix()
  9402. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  9403. if errThree != nil {
  9404. return errThree
  9405. }
  9406. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  9407. prescribingNumber_two_temp := deliver_number - stock_number
  9408. advice.CountUnit = drup.MinUnit
  9409. AutoDrugDeliverInfoNight(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, warehouse_out_id, cancel_out_id)
  9410. }
  9411. return
  9412. }
  9413. // 调拨出库逻辑
  9414. func ConsumablesDeliveryTwelve(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64, warehouseInfo *models.WarehousingInfo) (err error) {
  9415. var deliver_number int64 = 0
  9416. var stock_number int64 = 0
  9417. var maxNumber int64 = 0
  9418. deliver_number = goods.Count
  9419. // 根据先进先出原则,查询最先入库的批次,进行出库
  9420. // 如果没有对应的库存,则报错
  9421. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  9422. if err != nil {
  9423. return err
  9424. }
  9425. stock_number = warehouse.StockCount
  9426. maxNumber = goods.Count
  9427. //if warehouse.StockCount < maxNumber {
  9428. // return errors.New("库存数量不足")
  9429. //}
  9430. if warehouse.StockCount-maxNumber >= 0 {
  9431. warehouse.StockCount = warehouse.StockCount - maxNumber
  9432. } else {
  9433. warehouse.StockCount = warehouse.StockCount
  9434. }
  9435. warehouse.Mtime = time.Now().Unix()
  9436. //扣减库存
  9437. errThree := UpDateWarehouseInfoByStock(&warehouse)
  9438. //查询剩余库存
  9439. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  9440. var sum_count int64
  9441. for _, item := range goodList {
  9442. sum_count += item.StockCount
  9443. }
  9444. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  9445. if stock_number >= deliver_number {
  9446. warehouseOutInfo := &models.WarehouseOutInfo{
  9447. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  9448. WarehouseOutId: goods.WarehouseOutId,
  9449. WarehouseInfotId: warehouse.ID,
  9450. Status: 1,
  9451. Ctime: time.Now().Unix(),
  9452. Remark: goods.Remark,
  9453. OrgId: orgID,
  9454. Type: 1,
  9455. Manufacturer: warehouse.Manufacturer,
  9456. Dealer: warehouse.Dealer,
  9457. IsSys: 0,
  9458. SysRecordTime: record_time,
  9459. GoodTypeId: warehouse.GoodTypeId,
  9460. GoodId: warehouse.GoodId,
  9461. ExpiryDate: warehouse.ExpiryDate,
  9462. ProductDate: warehouse.ProductDate,
  9463. Number: warehouse.Number,
  9464. Price: warehouse.PackingPrice,
  9465. LicenseNumber: warehouse.LicenseNumber,
  9466. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9467. StorehouseId: goods.StorehouseId,
  9468. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  9469. IsCheck: 1,
  9470. OverCount: sum_count,
  9471. }
  9472. warehouseOutInfo.Count = goods.Count
  9473. //添加出库单详情
  9474. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  9475. if errOne != nil {
  9476. return errOne
  9477. }
  9478. stockFlow := models.VmStockFlow{
  9479. WarehousingId: warehouse.ID,
  9480. GoodId: goods.GoodId,
  9481. Number: warehouse.Number,
  9482. LicenseNumber: goods.LicenseNumber,
  9483. Count: goods.Count,
  9484. UserOrgId: goods.OrgId,
  9485. PatientId: goods.PatientId,
  9486. SystemTime: record_time,
  9487. ConsumableType: 12,
  9488. IsSys: goods.IsSys,
  9489. WarehousingOrder: "",
  9490. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  9491. WarehouseOutId: goods.WarehouseOutId,
  9492. IsEdit: 1,
  9493. CancelStockId: 0,
  9494. CancelOrderNumber: "",
  9495. Manufacturer: warehouse.Manufacturer,
  9496. Dealer: warehouse.Dealer,
  9497. Creator: creator,
  9498. UpdateCreator: 0,
  9499. Status: 1,
  9500. Ctime: time.Now().Unix(),
  9501. Mtime: 0,
  9502. Price: warehouse.PackingPrice,
  9503. WarehousingDetailId: 0,
  9504. WarehouseOutDetailId: warehouseOutInfo.ID,
  9505. CancelOutDetailId: 0,
  9506. ProductDate: warehouse.ProductDate,
  9507. ExpireDate: warehouse.ExpiryDate,
  9508. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9509. StorehouseId: warehouse.StorehouseId,
  9510. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  9511. OverCount: sum_count,
  9512. }
  9513. //创建出库流水
  9514. CreateStockFlowOne(stockFlow)
  9515. //入库
  9516. warehouseInfo := &models.WarehousingInfo{
  9517. WarehousingOrder: warehouseInfo.WarehousingOrder,
  9518. WarehousingId: warehouseInfo.WarehousingId,
  9519. GoodId: warehouse.GoodId,
  9520. Number: warehouse.Number,
  9521. GoodTypeId: warehouse.GoodTypeId,
  9522. ProductDate: warehouse.ProductDate,
  9523. ExpiryDate: warehouse.ExpiryDate,
  9524. WarehousingCount: goods.Count,
  9525. Price: warehouse.Price,
  9526. TotalPrice: 0,
  9527. Status: 1,
  9528. Ctime: warehouse.Ctime,
  9529. Remark: goods.Remark,
  9530. OrgId: warehouse.OrgId,
  9531. Type: 1,
  9532. Manufacturer: warehouse.Manufacturer,
  9533. StockCount: goods.Count,
  9534. Dealer: warehouse.Dealer,
  9535. LicenseNumber: "",
  9536. PackingPrice: warehouse.PackingPrice,
  9537. StorehouseId: warehouseInfo.StorehouseId,
  9538. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  9539. IsCheck: 1,
  9540. }
  9541. AddWarehosingInfo(warehouseInfo)
  9542. stockInfoFlow := models.VmStockFlow{
  9543. WarehousingId: warehouseInfo.WarehousingId,
  9544. GoodId: goods.GoodId,
  9545. Number: warehouse.Number,
  9546. LicenseNumber: goods.LicenseNumber,
  9547. Count: goods.Count,
  9548. UserOrgId: goods.OrgId,
  9549. PatientId: goods.PatientId,
  9550. SystemTime: record_time,
  9551. ConsumableType: 13,
  9552. IsSys: goods.IsSys,
  9553. WarehousingOrder: warehouseInfo.WarehousingOrder,
  9554. WarehouseOutId: 0,
  9555. WarehouseOutOrderNumber: "",
  9556. IsEdit: 1,
  9557. CancelStockId: 0,
  9558. CancelOrderNumber: "",
  9559. Manufacturer: warehouse.Manufacturer,
  9560. Dealer: warehouse.Dealer,
  9561. Creator: creator,
  9562. UpdateCreator: 0,
  9563. Status: 1,
  9564. Ctime: time.Now().Unix(),
  9565. Mtime: 0,
  9566. Price: warehouse.Price,
  9567. WarehousingDetailId: 0,
  9568. WarehouseOutDetailId: warehouseOutInfo.ID,
  9569. CancelOutDetailId: 0,
  9570. ProductDate: warehouse.ProductDate,
  9571. ExpireDate: warehouse.ExpiryDate,
  9572. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9573. StorehouseId: warehouseInfo.StorehouseId,
  9574. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  9575. }
  9576. //创建入库流水
  9577. CreateStockFlowOne(stockInfoFlow)
  9578. if errThree != nil {
  9579. return errThree
  9580. }
  9581. } else {
  9582. // 出库完成后,要将该批次库存清零
  9583. warehouse.StockCount = 0
  9584. warehouse.Mtime = time.Now().Unix()
  9585. errThree := UpDateWarehouseInfoByStock(&warehouse)
  9586. if errThree != nil {
  9587. return errThree
  9588. }
  9589. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  9590. goods.Count = deliver_number - stock_number
  9591. //查询剩余库存
  9592. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  9593. var sum_count int64
  9594. for _, item := range goodList {
  9595. sum_count += item.StockCount
  9596. }
  9597. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  9598. warehouseOutInfo := &models.WarehouseOutInfo{
  9599. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  9600. WarehouseOutId: goods.WarehouseOutId,
  9601. WarehouseInfotId: warehouse.ID,
  9602. Status: 1,
  9603. Ctime: time.Now().Unix(),
  9604. Remark: goods.Remark,
  9605. OrgId: orgID,
  9606. Type: 1,
  9607. Manufacturer: warehouse.Manufacturer,
  9608. Dealer: warehouse.Dealer,
  9609. IsSys: 0,
  9610. SysRecordTime: record_time,
  9611. GoodTypeId: warehouse.GoodTypeId,
  9612. GoodId: warehouse.GoodId,
  9613. ExpiryDate: warehouse.ExpiryDate,
  9614. ProductDate: warehouse.ProductDate,
  9615. Number: warehouse.Number,
  9616. Price: warehouse.PackingPrice,
  9617. LicenseNumber: warehouse.LicenseNumber,
  9618. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9619. StorehouseId: warehouse.StorehouseId,
  9620. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  9621. IsCheck: 1,
  9622. OverCount: sum_count,
  9623. }
  9624. warehouseOutInfo.Count = stock_number
  9625. //查询是否已经生成出库单
  9626. info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  9627. if errcodes == gorm.ErrRecordNotFound {
  9628. //创建出库单
  9629. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  9630. if errOne != nil {
  9631. return errOne
  9632. }
  9633. } else {
  9634. warehouseOutInfo.Count = info.Count + stock_number
  9635. UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  9636. }
  9637. stockFlow := models.VmStockFlow{
  9638. WarehousingId: warehouse.ID,
  9639. GoodId: goods.GoodId,
  9640. Number: warehouse.Number,
  9641. LicenseNumber: goods.LicenseNumber,
  9642. Count: stock_number,
  9643. UserOrgId: goods.OrgId,
  9644. PatientId: 0,
  9645. SystemTime: record_time,
  9646. ConsumableType: 12,
  9647. IsSys: 0,
  9648. WarehousingOrder: "",
  9649. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  9650. WarehouseOutId: goods.WarehouseOutId,
  9651. IsEdit: 1,
  9652. CancelStockId: 0,
  9653. CancelOrderNumber: "",
  9654. Manufacturer: warehouse.Manufacturer,
  9655. Dealer: warehouse.Dealer,
  9656. Creator: creator,
  9657. UpdateCreator: 0,
  9658. Status: 1,
  9659. Ctime: time.Now().Unix(),
  9660. Mtime: 0,
  9661. Price: warehouse.PackingPrice,
  9662. WarehousingDetailId: 0,
  9663. WarehouseOutDetailId: warehouseOutInfo.ID,
  9664. CancelOutDetailId: 0,
  9665. ProductDate: warehouse.ProductDate,
  9666. ExpireDate: warehouse.ExpiryDate,
  9667. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9668. StorehouseId: goods.StorehouseId,
  9669. SecondWarehouseInfoId: goods.SecondWarehouseInfoId,
  9670. OverCount: sum_count,
  9671. }
  9672. //创建出库明细
  9673. CreateStockFlowOne(stockFlow)
  9674. //入库
  9675. warehouseInfo := &models.WarehousingInfo{
  9676. WarehousingOrder: warehouseInfo.WarehousingOrder,
  9677. WarehousingId: warehouseInfo.WarehousingId,
  9678. GoodId: warehouse.GoodId,
  9679. Number: warehouse.Number,
  9680. GoodTypeId: warehouse.GoodTypeId,
  9681. ProductDate: warehouse.ProductDate,
  9682. ExpiryDate: warehouse.ExpiryDate,
  9683. WarehousingCount: stock_number,
  9684. Price: warehouse.Price,
  9685. TotalPrice: 0,
  9686. Status: 1,
  9687. Ctime: warehouse.Ctime,
  9688. Remark: goods.Remark,
  9689. OrgId: warehouse.OrgId,
  9690. Type: 1,
  9691. Manufacturer: warehouse.Manufacturer,
  9692. StockCount: stock_number,
  9693. Dealer: warehouse.Dealer,
  9694. LicenseNumber: "",
  9695. PackingPrice: warehouse.PackingPrice,
  9696. StorehouseId: warehouseInfo.StorehouseId,
  9697. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  9698. IsCheck: 1,
  9699. }
  9700. AddWarehosingInfo(warehouseInfo)
  9701. stockInfoFlow := models.VmStockFlow{
  9702. WarehousingId: warehouseInfo.WarehousingId,
  9703. GoodId: goods.GoodId,
  9704. Number: warehouse.Number,
  9705. LicenseNumber: goods.LicenseNumber,
  9706. Count: stock_number,
  9707. UserOrgId: goods.OrgId,
  9708. PatientId: goods.PatientId,
  9709. SystemTime: record_time,
  9710. ConsumableType: 13,
  9711. IsSys: goods.IsSys,
  9712. WarehousingOrder: warehouseInfo.WarehousingOrder,
  9713. WarehouseOutId: warehouseOut.ID,
  9714. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  9715. IsEdit: 1,
  9716. CancelStockId: 0,
  9717. CancelOrderNumber: "",
  9718. Manufacturer: warehouse.Manufacturer,
  9719. Dealer: warehouse.Dealer,
  9720. Creator: creator,
  9721. UpdateCreator: 0,
  9722. Status: 1,
  9723. Ctime: time.Now().Unix(),
  9724. Mtime: 0,
  9725. Price: warehouse.Price,
  9726. WarehousingDetailId: 0,
  9727. WarehouseOutDetailId: warehouseOutInfo.ID,
  9728. CancelOutDetailId: 0,
  9729. ProductDate: warehouse.ProductDate,
  9730. ExpireDate: warehouse.ExpiryDate,
  9731. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9732. StorehouseId: warehouseInfo.StorehouseId,
  9733. SecondWarehouseInfoId: warehouseInfo.SecondWarehouseInfoId,
  9734. }
  9735. //创建出库流水
  9736. CreateStockFlowOne(stockInfoFlow)
  9737. ConsumablesDeliveryTwelve(orgID, record_time, goods, warehouseOut, count, creator, warehouseInfo)
  9738. }
  9739. return nil
  9740. }
  9741. func AddWarehosingInfo(info *models.WarehousingInfo) error {
  9742. err = XTWriteDB().Create(&info).Error
  9743. return err
  9744. }
  9745. // 药品出库 递归方式
  9746. func AutoDrugDeliverInfoTwelve(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo, drugWarehouse *models.DrugWarehouseInfo) (err error) {
  9747. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  9748. var deliver_number int64 = 0
  9749. var stock_number int64 = 0
  9750. if advice.CountUnit == drup.MaxUnit {
  9751. deliver_number = prescribingNumber * drup.MinNumber
  9752. } else {
  9753. deliver_number = prescribingNumber
  9754. }
  9755. // 根据先进先出原则,查询最先入库的批次,进行出库
  9756. // 如果没有对应的库存,则报错
  9757. //开启事物
  9758. //storeConfig, _ := GetAllStoreHouseConfig(orgID)
  9759. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  9760. fmt.Println("lastWarehouse.StockMinNumber", lastWarehouse.StockMinNumber)
  9761. fmt.Println("drup.MinNumber", drup.MinNumber)
  9762. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  9763. var stockMax int64
  9764. var stockMin int64
  9765. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  9766. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  9767. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  9768. UpdateMinNumber(lastWarehouse.ID, stockMin)
  9769. }
  9770. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  9771. if err != nil {
  9772. return err
  9773. }
  9774. // 将该批次的剩余库存数量转换为拆零数量
  9775. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  9776. fmt.Println("stock23233233232", stock_number)
  9777. fmt.Println("deliver_number", deliver_number)
  9778. // 出库完成后,要减去对应批次的库存数量
  9779. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  9780. var maxNumber int64 = 0
  9781. var minNumber int64 = 0
  9782. if advice.CountUnit == drup.MinUnit {
  9783. maxNumber = prescribingNumber / drup.MinNumber
  9784. minNumber = prescribingNumber % drup.MinNumber
  9785. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  9786. minNumber = maxNumber
  9787. }
  9788. } else {
  9789. maxNumber = prescribingNumber
  9790. }
  9791. //fmt.Println("warehouse.StockMaxNumber",warehouse.StockMaxNumber)
  9792. //fmt.Println("maxNumber9899923323232wo",maxNumber)
  9793. //if warehouse.StockMaxNumber < maxNumber {
  9794. //
  9795. // return errors.New("库存数量不足")
  9796. //}
  9797. if warehouse.StockMaxNumber-maxNumber > 0 {
  9798. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  9799. }
  9800. warehouse.Mtime = time.Now().Unix()
  9801. if warehouse.StockMinNumber < minNumber {
  9802. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  9803. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  9804. } else {
  9805. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  9806. }
  9807. if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
  9808. warehouse.StockMinNumber = 0
  9809. }
  9810. if warehouse.StockMaxNumber < 0 {
  9811. return errors.New("库存数量不足")
  9812. }
  9813. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  9814. if errThree != nil {
  9815. return errThree
  9816. }
  9817. //查询剩余库存
  9818. stockInfo, _ := GetDrugAllStockInfo(advice.StorehouseId, orgID, advice.DrugId)
  9819. var sum_count int64
  9820. for _, its := range stockInfo {
  9821. if its.MaxUnit == drup.MaxUnit {
  9822. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  9823. }
  9824. sum_count += its.StockMaxNumber + its.StockMinNumber
  9825. }
  9826. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  9827. if stock_number >= deliver_number {
  9828. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  9829. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  9830. WarehouseOutId: advice.WarehouseOutId,
  9831. Status: 1,
  9832. Ctime: time.Now().Unix(),
  9833. Remark: advice.Remark,
  9834. OrgId: orgID,
  9835. Type: 1,
  9836. Manufacturer: warehouse.Manufacturer,
  9837. Dealer: warehouse.Dealer,
  9838. IsSys: 12,
  9839. SysRecordTime: advice.Ctime,
  9840. DrugId: advice.DrugId,
  9841. ExpiryDate: warehouse.ExpiryDate,
  9842. ProductDate: warehouse.ProductDate,
  9843. Number: warehouse.Number,
  9844. BatchNumber: warehouse.BatchNumber,
  9845. Count: deliver_number,
  9846. RetailPrice: warehouse.RetailPrice,
  9847. Price: warehouse.RetailPrice,
  9848. WarehouseInfoId: warehouse.ID,
  9849. CountUnit: drup.MinUnit,
  9850. AdviceId: advice.ID,
  9851. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9852. StorehouseId: advice.StorehouseId,
  9853. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  9854. IsCheck: 1,
  9855. OverCount: sum_count,
  9856. }
  9857. warehouseOutInfo.Count = prescribingNumber
  9858. warehouseOutInfo.CountUnit = advice.CountUnit
  9859. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  9860. if errOne != nil {
  9861. return errOne
  9862. }
  9863. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  9864. drugflow := models.DrugFlow{
  9865. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  9866. WarehouseOutId: advice.WarehouseOutId,
  9867. DrugId: advice.DrugId,
  9868. Number: warehouse.Number,
  9869. ProductDate: warehouse.ProductDate,
  9870. ExpireDate: warehouse.ExpiryDate,
  9871. Count: deliver_number,
  9872. Price: warehouse.RetailPrice,
  9873. Status: 1,
  9874. Ctime: time.Now().Unix(),
  9875. UserOrgId: advice.OrgId,
  9876. Manufacturer: warehouse.Manufacturer,
  9877. Dealer: warehouse.Dealer,
  9878. BatchNumber: warehouse.BatchNumber,
  9879. MaxUnit: drup.MinUnit,
  9880. ConsumableType: 12,
  9881. IsEdit: 1,
  9882. Creator: 0,
  9883. IsSys: 12,
  9884. WarehouseOutDetailId: drugWareInfo.ID,
  9885. AdviceId: advice.ID,
  9886. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  9887. StorehouseId: advice.StorehouseId,
  9888. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  9889. WarehousingDetailId: warehouse.ID,
  9890. OverCount: sum_count,
  9891. }
  9892. CreateDrugFlowOne(drugflow)
  9893. //入库
  9894. drugInfo := models.DrugWarehouseInfo{
  9895. WarehousingOrder: drugWarehouse.WarehousingOrder,
  9896. WarehousingId: drugWarehouse.WarehousingId,
  9897. DrugId: warehouse.DrugId,
  9898. Number: warehouse.Number,
  9899. ProductDate: warehouse.ProductDate,
  9900. ExpiryDate: warehouse.ExpiryDate,
  9901. WarehousingCount: deliver_number,
  9902. Price: warehouse.RetailPrice,
  9903. TotalPrice: warehouse.TotalPrice,
  9904. Status: 1,
  9905. Ctime: warehouse.Ctime,
  9906. Remark: "",
  9907. OrgId: warehouse.OrgId,
  9908. Type: 13,
  9909. Manufacturer: warehouse.Manufacturer,
  9910. Dealer: warehouse.Dealer,
  9911. StockMaxNumber: 0,
  9912. StockMinNumber: deliver_number,
  9913. BatchNumber: warehouse.BatchNumber,
  9914. MaxUnit: drup.MinUnit,
  9915. MinUnit: drup.MinUnit,
  9916. RetailPrice: warehouse.RetailPrice,
  9917. StorehouseId: drugWarehouse.StorehouseId,
  9918. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  9919. IsCheck: 1,
  9920. }
  9921. CreateDrugWarehouseInfo(drugInfo)
  9922. flow := models.DrugFlow{
  9923. WarehousingId: drugWarehouse.WarehousingId,
  9924. DrugId: warehouse.DrugId,
  9925. Number: warehouse.Number,
  9926. BatchNumber: warehouse.BatchNumber,
  9927. Count: deliver_number,
  9928. UserOrgId: warehouse.OrgId,
  9929. PatientId: 0,
  9930. SystemTime: 0,
  9931. ConsumableType: 13,
  9932. IsSys: 13,
  9933. WarehousingOrder: drugWarehouse.WarehousingOrder,
  9934. WarehouseOutId: 0,
  9935. WarehouseOutOrderNumber: "",
  9936. IsEdit: 0,
  9937. CancelStockId: 0,
  9938. CancelOrderNumber: "",
  9939. Manufacturer: warehouse.Manufacturer,
  9940. Dealer: warehouse.Dealer,
  9941. Creator: 0,
  9942. UpdateCreator: 0,
  9943. Status: 1,
  9944. Ctime: time.Now().Unix(),
  9945. Mtime: 0,
  9946. Price: warehouse.RetailPrice,
  9947. WarehousingDetailId: warehouse.ID,
  9948. WarehouseOutDetailId: 0,
  9949. CancelOutDetailId: 0,
  9950. ExpireDate: warehouse.ExpiryDate,
  9951. ProductDate: warehouse.ProductDate,
  9952. MaxUnit: drup.MinUnit,
  9953. MinUnit: drup.MinUnit,
  9954. StockMaxNumber: 0,
  9955. StockMinNumber: deliver_number,
  9956. StorehouseId: drugWarehouse.StorehouseId,
  9957. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  9958. }
  9959. CreateDrugFlowOne(flow)
  9960. return nil
  9961. } else {
  9962. warehouse.StockMaxNumber = 0
  9963. warehouse.StockMinNumber = 0
  9964. warehouse.Mtime = time.Now().Unix()
  9965. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  9966. if errThree != nil {
  9967. return errThree
  9968. }
  9969. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  9970. prescribingNumber_two_temp := deliver_number - stock_number
  9971. advice.CountUnit = drup.MinUnit
  9972. //查询剩余库存
  9973. stockInfo, _ := GetDrugAllStockInfo(advice.StorehouseId, orgID, advice.DrugId)
  9974. var sum_count int64
  9975. for _, its := range stockInfo {
  9976. if its.MaxUnit == drup.MaxUnit {
  9977. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  9978. }
  9979. sum_count += its.StockMaxNumber + its.StockMinNumber
  9980. }
  9981. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  9982. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  9983. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  9984. WarehouseOutId: advice.WarehouseOutId,
  9985. Status: 1,
  9986. Ctime: time.Now().Unix(),
  9987. Remark: advice.Remark,
  9988. OrgId: orgID,
  9989. Type: 1,
  9990. Manufacturer: advice.Manufacturer,
  9991. Dealer: warehouse.Dealer,
  9992. IsSys: 12,
  9993. SysRecordTime: advice.Ctime,
  9994. DrugId: advice.DrugId,
  9995. ExpiryDate: warehouse.ExpiryDate,
  9996. ProductDate: warehouse.ProductDate,
  9997. Number: warehouse.Number,
  9998. BatchNumber: warehouse.BatchNumber,
  9999. Count: stock_number,
  10000. RetailPrice: advice.RetailPrice,
  10001. Price: warehouse.RetailPrice,
  10002. WarehouseInfoId: warehouse.ID,
  10003. CountUnit: drup.MinUnit,
  10004. AdviceId: advice.ID,
  10005. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  10006. StorehouseId: advice.StorehouseId,
  10007. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  10008. IsCheck: 1,
  10009. OverCount: sum_count,
  10010. }
  10011. warehouseOutInfo.Count = stock_number
  10012. //warehouseOutInfo.CountUnit = advice.CountUnit
  10013. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  10014. if errOne != nil {
  10015. return errOne
  10016. }
  10017. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  10018. drugflow := models.DrugFlow{
  10019. WarehouseOutOrderNumber: advice.WarehouseOutOrderNumber,
  10020. WarehouseOutId: advice.WarehouseOutId,
  10021. DrugId: advice.DrugId,
  10022. Number: warehouse.Number,
  10023. ProductDate: warehouse.ProductDate,
  10024. ExpireDate: warehouse.ExpiryDate,
  10025. Count: stock_number,
  10026. Price: warehouse.RetailPrice,
  10027. Status: 1,
  10028. Ctime: time.Now().Unix(),
  10029. UserOrgId: advice.OrgId,
  10030. Manufacturer: warehouse.Manufacturer,
  10031. Dealer: warehouse.Dealer,
  10032. BatchNumber: warehouse.BatchNumber,
  10033. MaxUnit: drup.MinUnit,
  10034. ConsumableType: 12,
  10035. IsEdit: 1,
  10036. Creator: 0,
  10037. IsSys: 12,
  10038. WarehouseOutDetailId: drugWareInfo.ID,
  10039. AdviceId: advice.ID,
  10040. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  10041. StorehouseId: advice.StorehouseId,
  10042. SecondWarehouseInfoId: advice.SecondWarehouseInfoId,
  10043. WarehousingDetailId: warehouse.ID,
  10044. OverCount: sum_count,
  10045. }
  10046. CreateDrugFlowOne(drugflow)
  10047. // 出库完成后,要将该批次库存清零
  10048. //入库
  10049. drugInfo := models.DrugWarehouseInfo{
  10050. WarehousingOrder: drugWarehouse.WarehousingOrder,
  10051. WarehousingId: drugWarehouse.WarehousingId,
  10052. DrugId: warehouse.DrugId,
  10053. Number: warehouse.Number,
  10054. ProductDate: warehouse.ProductDate,
  10055. ExpiryDate: warehouse.ExpiryDate,
  10056. WarehousingCount: stock_number,
  10057. Price: warehouse.RetailPrice,
  10058. TotalPrice: warehouse.TotalPrice,
  10059. Status: 1,
  10060. Ctime: warehouse.Ctime,
  10061. Remark: "",
  10062. OrgId: warehouse.OrgId,
  10063. Type: 13,
  10064. Manufacturer: warehouse.Manufacturer,
  10065. Dealer: warehouse.Dealer,
  10066. StockMaxNumber: 0,
  10067. StockMinNumber: stock_number,
  10068. RetailTotalPrice: 0,
  10069. BatchNumber: warehouse.BatchNumber,
  10070. MaxUnit: drup.MinUnit,
  10071. MinUnit: drup.MinUnit,
  10072. RetailPrice: warehouse.RetailPrice,
  10073. StorehouseId: drugWarehouse.StorehouseId,
  10074. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  10075. }
  10076. CreateDrugWarehouseInfo(drugInfo)
  10077. flow := models.DrugFlow{
  10078. WarehousingId: drugWarehouse.WarehousingId,
  10079. DrugId: warehouse.DrugId,
  10080. Number: warehouse.Number,
  10081. BatchNumber: warehouse.BatchNumber,
  10082. Count: stock_number,
  10083. UserOrgId: warehouse.OrgId,
  10084. PatientId: 0,
  10085. SystemTime: 0,
  10086. ConsumableType: 13,
  10087. IsSys: 13,
  10088. WarehousingOrder: drugWarehouse.WarehousingOrder,
  10089. WarehouseOutId: 0,
  10090. WarehouseOutOrderNumber: "",
  10091. IsEdit: 0,
  10092. CancelStockId: 0,
  10093. CancelOrderNumber: "",
  10094. Manufacturer: warehouse.Manufacturer,
  10095. Dealer: warehouse.Dealer,
  10096. Creator: 0,
  10097. UpdateCreator: 0,
  10098. Status: 1,
  10099. Ctime: time.Now().Unix(),
  10100. Mtime: 0,
  10101. Price: warehouse.RetailPrice,
  10102. WarehousingDetailId: warehouse.ID,
  10103. WarehouseOutDetailId: 0,
  10104. CancelOutDetailId: 0,
  10105. ExpireDate: warehouse.ExpiryDate,
  10106. ProductDate: warehouse.ProductDate,
  10107. MaxUnit: drup.MinUnit,
  10108. MinUnit: drup.MinUnit,
  10109. StockMaxNumber: 0,
  10110. StockMinNumber: stock_number,
  10111. StorehouseId: drugWarehouse.StorehouseId,
  10112. SecondWarehouseInfoId: drugWarehouse.SecondWarehouseInfoId,
  10113. OverCount: sum_count,
  10114. }
  10115. CreateDrugFlowOne(flow)
  10116. AutoDrugDeliverInfoTwelve(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, drugWarehouse)
  10117. }
  10118. return
  10119. }
  10120. // 药品手动出库 递归方式
  10121. func AutoDrugDeliverInfoTwenty(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  10122. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  10123. var deliver_number int64 = 0
  10124. var stock_number int64 = 0
  10125. if advice.CountUnit == drup.MaxUnit {
  10126. deliver_number = prescribingNumber * drup.MinNumber
  10127. } else {
  10128. deliver_number = prescribingNumber
  10129. }
  10130. // 根据先进先出原则,查询最先入库的批次,进行出库
  10131. // 如果没有对应的库存,则报错
  10132. //开启事物
  10133. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  10134. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  10135. var stockMax int64
  10136. var stockMin int64
  10137. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  10138. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  10139. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  10140. UpdateMinNumber(lastWarehouse.ID, stockMin)
  10141. }
  10142. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  10143. if err != nil {
  10144. return err
  10145. }
  10146. // 将该批次的剩余库存数量转换为拆零数量
  10147. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  10148. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  10149. if stock_number >= deliver_number {
  10150. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  10151. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  10152. WarehouseOutId: warehouseout.ID,
  10153. Status: 1,
  10154. Ctime: time.Now().Unix(),
  10155. Remark: advice.Remark,
  10156. OrgId: orgID,
  10157. Type: 1,
  10158. Manufacturer: advice.Manufacturer,
  10159. Dealer: warehouse.Dealer,
  10160. IsSys: 0,
  10161. SysRecordTime: advice.Ctime,
  10162. DrugId: advice.DrugId,
  10163. ExpiryDate: advice.ExpiryDate,
  10164. ProductDate: advice.ProductDate,
  10165. Number: advice.Number,
  10166. BatchNumber: warehouse.BatchNumber,
  10167. Count: deliver_number,
  10168. RetailPrice: advice.RetailPrice,
  10169. Price: warehouse.RetailPrice,
  10170. WarehouseInfoId: warehouse.ID,
  10171. CountUnit: drup.MinUnit,
  10172. AdviceId: advice.ID,
  10173. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  10174. StorehouseId: advice.StorehouseId,
  10175. AdminUserId: advice.AdminUserId,
  10176. IsCheck: 1,
  10177. }
  10178. if warehouse.RetailPrice == 0 {
  10179. warehouseOutInfo.Price = advice.Price
  10180. }
  10181. warehouseOutInfo.Count = prescribingNumber
  10182. warehouseOutInfo.CountUnit = advice.CountUnit
  10183. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  10184. if errOne != nil {
  10185. return errOne
  10186. }
  10187. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  10188. drugflow := models.DrugFlow{
  10189. WarehouseOutId: warehouseout.ID,
  10190. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  10191. DrugId: advice.DrugId,
  10192. Number: warehouse.Number,
  10193. ProductDate: advice.ProductDate,
  10194. ExpireDate: advice.ExpiryDate,
  10195. Count: deliver_number,
  10196. Price: warehouse.RetailPrice,
  10197. Status: 1,
  10198. Ctime: time.Now().Unix(),
  10199. UserOrgId: advice.OrgId,
  10200. Manufacturer: advice.Manufacturer,
  10201. Dealer: advice.Dealer,
  10202. BatchNumber: warehouse.BatchNumber,
  10203. MaxUnit: drup.MinUnit,
  10204. ConsumableType: 2,
  10205. IsEdit: 1,
  10206. Creator: 0,
  10207. IsSys: 0,
  10208. WarehouseOutDetailId: drugWareInfo.ID,
  10209. AdviceId: advice.ID,
  10210. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  10211. StorehouseId: advice.StorehouseId,
  10212. AdminUserId: advice.AdminUserId,
  10213. }
  10214. if warehouse.RetailPrice == 0 {
  10215. drugflow.Price = advice.Price
  10216. }
  10217. CreateDrugFlowOne(drugflow)
  10218. // 出库完成后,要减去对应批次的库存数量
  10219. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  10220. var maxNumber int64 = 0
  10221. var minNumber int64 = 0
  10222. //var stock_max_number int64 = 0
  10223. //stock_max_number = warehouse.StockMaxNumber
  10224. maxNumber = deliver_number / drup.MinNumber
  10225. minNumber = deliver_number % drup.MinNumber
  10226. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  10227. minNumber = maxNumber
  10228. }
  10229. if drup.MaxUnit != drup.MinUnit {
  10230. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  10231. return errors.New("库存数量不足")
  10232. }
  10233. }
  10234. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  10235. if warehouse.StockMaxNumber < 0 {
  10236. warehouse.StockMaxNumber = 0
  10237. }
  10238. if warehouse.StockMinNumber < 0 {
  10239. warehouse.StockMinNumber = 0
  10240. }
  10241. warehouse.Mtime = time.Now().Unix()
  10242. if warehouse.StockMinNumber < minNumber {
  10243. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  10244. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  10245. } else {
  10246. if minNumber > 0 {
  10247. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  10248. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  10249. } else {
  10250. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  10251. }
  10252. }
  10253. if minNumber == 0 && maxNumber != 1 {
  10254. if warehouse.StockMinNumber > 0 {
  10255. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  10256. }
  10257. }
  10258. }
  10259. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  10260. if (warehouse.StockMinNumber - deliver_number) > 0 {
  10261. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  10262. //if warehouse.StockMinNumber == 0 {
  10263. // warehouse.StockMaxNumber = stock_max_number
  10264. //}
  10265. }
  10266. }
  10267. if drup.MaxUnit != drup.MinUnit {
  10268. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  10269. return errors.New("库存数量不足")
  10270. }
  10271. }
  10272. if warehouse.StockMinNumber <= 0 {
  10273. warehouse.StockMinNumber = 0
  10274. }
  10275. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  10276. if errThree != nil {
  10277. return errThree
  10278. }
  10279. //查询默认仓库
  10280. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  10281. //查询默认仓库剩余多少库存
  10282. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  10283. var sum_count int64
  10284. var sum_in_count int64
  10285. for _, it := range list {
  10286. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  10287. if it.MaxUnit == baseDrug.MaxUnit {
  10288. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  10289. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  10290. sum_count += it.StockMaxNumber + it.StockMinNumber
  10291. sum_in_count += it.WarehousingCount
  10292. }
  10293. }
  10294. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  10295. return nil
  10296. } else {
  10297. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  10298. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  10299. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  10300. WarehouseOutId: warehouseout.ID,
  10301. Status: 1,
  10302. Ctime: time.Now().Unix(),
  10303. Remark: advice.Remark,
  10304. OrgId: orgID,
  10305. Type: 1,
  10306. Manufacturer: advice.Manufacturer,
  10307. Dealer: warehouse.Dealer,
  10308. IsSys: 0,
  10309. SysRecordTime: advice.Ctime,
  10310. DrugId: advice.DrugId,
  10311. ExpiryDate: warehouse.ExpiryDate,
  10312. ProductDate: warehouse.ProductDate,
  10313. Number: warehouse.Number,
  10314. BatchNumber: warehouse.BatchNumber,
  10315. Count: stock_number,
  10316. RetailPrice: advice.RetailPrice,
  10317. Price: warehouse.RetailPrice,
  10318. WarehouseInfoId: warehouse.ID,
  10319. CountUnit: drup.MinUnit,
  10320. AdviceId: advice.ID,
  10321. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  10322. StorehouseId: advice.StorehouseId,
  10323. AdminUserId: advice.AdminUserId,
  10324. IsCheck: 1,
  10325. }
  10326. if warehouse.RetailPrice == 0 {
  10327. warehouseOutInfo.Price = advice.Price
  10328. }
  10329. warehouseOutInfo.Count = stock_number
  10330. //warehouseOutInfo.CountUnit = advice.CountUnit
  10331. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  10332. if errOne != nil {
  10333. return errOne
  10334. }
  10335. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  10336. drugflow := models.DrugFlow{
  10337. WarehouseOutId: warehouseout.ID,
  10338. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  10339. DrugId: advice.DrugId,
  10340. Number: warehouse.Number,
  10341. ProductDate: advice.ProductDate,
  10342. ExpireDate: advice.ExpiryDate,
  10343. Count: stock_number,
  10344. Price: warehouse.RetailPrice,
  10345. Status: 1,
  10346. Ctime: time.Now().Unix(),
  10347. UserOrgId: advice.OrgId,
  10348. Manufacturer: advice.Manufacturer,
  10349. Dealer: advice.Dealer,
  10350. BatchNumber: warehouse.BatchNumber,
  10351. MaxUnit: drup.MinUnit,
  10352. ConsumableType: 2,
  10353. IsEdit: 1,
  10354. Creator: 0,
  10355. IsSys: 0,
  10356. WarehouseOutDetailId: drugWareInfo.ID,
  10357. AdviceId: advice.ID,
  10358. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  10359. StorehouseId: advice.StorehouseId,
  10360. AdminUserId: advice.AdminUserId,
  10361. }
  10362. if warehouse.RetailPrice == 0 {
  10363. drugflow.Price = advice.Price
  10364. }
  10365. CreateDrugFlowOne(drugflow)
  10366. // 出库完成后,要将该批次库存清零
  10367. //扣减库存
  10368. warehouse.StockMaxNumber = 0
  10369. warehouse.StockMinNumber = 0
  10370. warehouse.Mtime = time.Now().Unix()
  10371. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  10372. //查询默认仓库
  10373. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  10374. //查询默认仓库剩余多少库存
  10375. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  10376. var sum_count int64
  10377. var sum_in_count int64
  10378. for _, it := range list {
  10379. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  10380. if it.MaxUnit == baseDrug.MaxUnit {
  10381. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  10382. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  10383. sum_count += it.StockMaxNumber + it.StockMinNumber
  10384. sum_in_count += it.WarehousingCount
  10385. }
  10386. }
  10387. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  10388. if errThree != nil {
  10389. return errThree
  10390. }
  10391. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  10392. prescribingNumber_two_temp := deliver_number - stock_number
  10393. advice.CountUnit = drup.MinUnit
  10394. AutoDrugDeliverInfoTwenty(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  10395. }
  10396. return
  10397. }
  10398. // 药品手动出库 递归方式
  10399. func AutoDrugDeliverInfoTwentyOne(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  10400. //开事务
  10401. //tx := XTWriteDB().Begin()
  10402. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  10403. var deliver_number int64 = 0
  10404. var stock_number int64 = 0
  10405. if advice.CountUnit == drup.MaxUnit {
  10406. deliver_number = prescribingNumber * drup.MinNumber
  10407. } else {
  10408. deliver_number = prescribingNumber
  10409. }
  10410. // 根据先进先出原则,查询最先入库的批次,进行出库
  10411. // 如果没有对应的库存,则报错
  10412. //开启事物
  10413. storeConfig, _ := GetAllStoreHouseConfig(orgID)
  10414. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  10415. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  10416. var stockMax int64
  10417. var stockMin int64
  10418. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  10419. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  10420. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  10421. UpdateMinNumber(lastWarehouse.ID, stockMin)
  10422. }
  10423. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  10424. if err != nil {
  10425. return err
  10426. }
  10427. // 将该批次的剩余库存数量转换为拆零数量
  10428. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  10429. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  10430. if stock_number >= deliver_number {
  10431. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  10432. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  10433. WarehouseOutId: warehouseout.ID,
  10434. Status: 1,
  10435. Ctime: time.Now().Unix(),
  10436. Remark: advice.Remark,
  10437. OrgId: orgID,
  10438. Type: 1,
  10439. Manufacturer: advice.Manufacturer,
  10440. Dealer: warehouse.Dealer,
  10441. IsSys: 0,
  10442. SysRecordTime: advice.Ctime,
  10443. DrugId: advice.DrugId,
  10444. ExpiryDate: advice.ExpiryDate,
  10445. ProductDate: advice.ProductDate,
  10446. Number: advice.Number,
  10447. BatchNumber: warehouse.BatchNumber,
  10448. Count: deliver_number,
  10449. RetailPrice: advice.RetailPrice,
  10450. Price: advice.Price,
  10451. WarehouseInfoId: warehouse.ID,
  10452. CountUnit: drup.MinUnit,
  10453. AdviceId: advice.ID,
  10454. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  10455. StorehouseId: advice.StorehouseId,
  10456. AdminUserId: advice.AdminUserId,
  10457. LastPrice: warehouse.Price,
  10458. StockCount: advice.StockCount,
  10459. IsCheck: 1,
  10460. }
  10461. warehouseOutInfo.Count = prescribingNumber
  10462. warehouseOutInfo.CountUnit = advice.CountUnit
  10463. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  10464. drugflow := models.DrugFlow{
  10465. WarehouseOutId: warehouseout.ID,
  10466. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  10467. DrugId: advice.DrugId,
  10468. Number: warehouse.Number,
  10469. ProductDate: advice.ProductDate,
  10470. ExpireDate: advice.ExpiryDate,
  10471. Count: deliver_number,
  10472. Price: advice.Price,
  10473. Status: 1,
  10474. Ctime: time.Now().Unix(),
  10475. UserOrgId: advice.OrgId,
  10476. Manufacturer: advice.Manufacturer,
  10477. Dealer: advice.Dealer,
  10478. BatchNumber: warehouse.BatchNumber,
  10479. MaxUnit: drup.MinUnit,
  10480. ConsumableType: 2,
  10481. IsEdit: 1,
  10482. Creator: 0,
  10483. IsSys: 0,
  10484. WarehouseOutDetailId: drugWareInfo.ID,
  10485. AdviceId: advice.ID,
  10486. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  10487. StorehouseId: advice.StorehouseId,
  10488. AdminUserId: advice.AdminUserId,
  10489. LastPrice: warehouse.Price,
  10490. StockCount: advice.StockCount,
  10491. }
  10492. if warehouse.RetailPrice == 0 {
  10493. drugflow.Price = advice.Price
  10494. }
  10495. CreateDrugFlowOne(drugflow)
  10496. // 出库完成后,要减去对应批次的库存数量
  10497. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  10498. var maxNumber int64 = 0
  10499. var minNumber int64 = 0
  10500. //var stock_max_number int64 = 0
  10501. //stock_max_number = warehouse.StockMaxNumber
  10502. maxNumber = deliver_number / drup.MinNumber
  10503. minNumber = deliver_number % drup.MinNumber
  10504. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  10505. minNumber = maxNumber
  10506. }
  10507. if drup.MaxUnit != drup.MinUnit {
  10508. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  10509. return errors.New("库存数量不足")
  10510. }
  10511. }
  10512. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  10513. if warehouse.StockMaxNumber < 0 {
  10514. warehouse.StockMaxNumber = 0
  10515. }
  10516. if warehouse.StockMinNumber < 0 {
  10517. warehouse.StockMinNumber = 0
  10518. }
  10519. warehouse.Mtime = time.Now().Unix()
  10520. if warehouse.StockMinNumber < minNumber {
  10521. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  10522. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  10523. } else {
  10524. if minNumber > 0 {
  10525. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  10526. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  10527. } else {
  10528. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  10529. }
  10530. }
  10531. if minNumber == 0 && maxNumber != 1 {
  10532. if warehouse.StockMinNumber > 0 {
  10533. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  10534. }
  10535. }
  10536. }
  10537. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  10538. if (warehouse.StockMinNumber - deliver_number) > 0 {
  10539. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  10540. //if warehouse.StockMinNumber == 0 {
  10541. // warehouse.StockMaxNumber = stock_max_number
  10542. //}
  10543. }
  10544. }
  10545. if drup.MaxUnit != drup.MinUnit {
  10546. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  10547. return errors.New("库存数量不足")
  10548. }
  10549. }
  10550. if warehouse.StockMinNumber <= 0 {
  10551. warehouse.StockMinNumber = 0
  10552. }
  10553. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  10554. fmt.Println(errThree)
  10555. //if errThree != nil {
  10556. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  10557. // tx.Rollback()
  10558. //} else {
  10559. // tx.Commit()
  10560. //}
  10561. //查询默认仓库
  10562. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  10563. //查询默认仓库剩余多少库存
  10564. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  10565. var sum_count int64
  10566. var sum_in_count int64
  10567. for _, it := range list {
  10568. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  10569. if it.MaxUnit == baseDrug.MaxUnit {
  10570. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  10571. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  10572. sum_count += it.StockMaxNumber + it.StockMinNumber
  10573. sum_in_count += it.WarehousingCount
  10574. }
  10575. }
  10576. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  10577. return nil
  10578. } else {
  10579. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  10580. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  10581. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  10582. WarehouseOutId: warehouseout.ID,
  10583. Status: 1,
  10584. Ctime: time.Now().Unix(),
  10585. Remark: advice.Remark,
  10586. OrgId: orgID,
  10587. Type: 1,
  10588. Manufacturer: advice.Manufacturer,
  10589. Dealer: warehouse.Dealer,
  10590. IsSys: 0,
  10591. SysRecordTime: advice.Ctime,
  10592. DrugId: advice.DrugId,
  10593. ExpiryDate: warehouse.ExpiryDate,
  10594. ProductDate: warehouse.ProductDate,
  10595. Number: warehouse.Number,
  10596. BatchNumber: warehouse.BatchNumber,
  10597. Count: stock_number,
  10598. RetailPrice: advice.RetailPrice,
  10599. Price: advice.Price,
  10600. WarehouseInfoId: warehouse.ID,
  10601. CountUnit: drup.MinUnit,
  10602. AdviceId: advice.ID,
  10603. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  10604. StorehouseId: advice.StorehouseId,
  10605. AdminUserId: advice.AdminUserId,
  10606. LastPrice: warehouse.Price,
  10607. StockCount: advice.StockCount,
  10608. IsCheck: 1,
  10609. }
  10610. warehouseOutInfo.Count = stock_number
  10611. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  10612. drugflow := models.DrugFlow{
  10613. WarehouseOutId: warehouseout.ID,
  10614. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  10615. DrugId: advice.DrugId,
  10616. Number: warehouse.Number,
  10617. ProductDate: advice.ProductDate,
  10618. ExpireDate: advice.ExpiryDate,
  10619. Count: stock_number,
  10620. Price: advice.Price,
  10621. Status: 1,
  10622. Ctime: time.Now().Unix(),
  10623. UserOrgId: advice.OrgId,
  10624. Manufacturer: advice.Manufacturer,
  10625. Dealer: advice.Dealer,
  10626. BatchNumber: warehouse.BatchNumber,
  10627. MaxUnit: drup.MinUnit,
  10628. ConsumableType: 2,
  10629. IsEdit: 1,
  10630. Creator: 0,
  10631. IsSys: 0,
  10632. WarehouseOutDetailId: drugWareInfo.ID,
  10633. AdviceId: advice.ID,
  10634. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  10635. StorehouseId: advice.StorehouseId,
  10636. AdminUserId: advice.AdminUserId,
  10637. LastPrice: warehouse.Price,
  10638. StockCount: advice.StockCount,
  10639. }
  10640. CreateDrugFlowOne(drugflow)
  10641. // 出库完成后,要将该批次库存清零
  10642. //扣减库存
  10643. warehouse.StockMaxNumber = 0
  10644. warehouse.StockMinNumber = 0
  10645. warehouse.Mtime = time.Now().Unix()
  10646. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  10647. fmt.Println(errThree)
  10648. //if errThree != nil {
  10649. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  10650. // tx.Rollback()
  10651. //} else {
  10652. // tx.Commit()
  10653. //}
  10654. //查询默认仓库
  10655. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  10656. //查询默认仓库剩余多少库存
  10657. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  10658. var sum_count int64
  10659. var sum_in_count int64
  10660. for _, it := range list {
  10661. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  10662. if it.MaxUnit == baseDrug.MaxUnit {
  10663. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  10664. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  10665. sum_count += it.StockMaxNumber + it.StockMinNumber
  10666. sum_in_count += it.WarehousingCount
  10667. }
  10668. }
  10669. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  10670. if errThree != nil {
  10671. return errThree
  10672. }
  10673. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  10674. prescribingNumber_two_temp := deliver_number - stock_number
  10675. advice.CountUnit = drup.MinUnit
  10676. AutoDrugDeliverInfoTwentyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  10677. }
  10678. return
  10679. }
  10680. // 耗材手动出库
  10681. func ConsumablesDeliveryThirty(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64) (err error) {
  10682. var deliver_number int64 = 0
  10683. var stock_number int64 = 0
  10684. var maxNumber int64 = 0
  10685. deliver_number = goods.Count
  10686. // 根据先进先出原则,查询最先入库的批次,进行出库
  10687. // 如果没有对应的库存,则报错
  10688. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  10689. if err != nil {
  10690. goodErrcode := models.XtGoodErrcode{
  10691. UserOrgId: goods.OrgId,
  10692. Errcode: "查询库存信息报错",
  10693. GoodId: goods.GoodId,
  10694. Status: 1,
  10695. Ctime: time.Now().Unix(),
  10696. Mtime: 0,
  10697. Count: 0,
  10698. StockCount: 0,
  10699. Creater: creator,
  10700. BatchNumberId: warehouse.ID,
  10701. WarehouseOutId: 0,
  10702. }
  10703. CreateGoodErrcode(goodErrcode)
  10704. return err
  10705. }
  10706. stock_number = warehouse.StockCount
  10707. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  10708. if stock_number >= deliver_number {
  10709. maxNumber = goods.Count
  10710. warehouse.StockCount = warehouse.StockCount - maxNumber
  10711. warehouse.Mtime = time.Now().Unix()
  10712. //扣减库存
  10713. errThree := UpDateWarehouseInfoByStock(&warehouse)
  10714. if errThree != nil {
  10715. goodErrcode := models.XtGoodErrcode{
  10716. UserOrgId: goods.OrgId,
  10717. Errcode: "扣减库存失败",
  10718. GoodId: goods.GoodId,
  10719. Status: 1,
  10720. Ctime: time.Now().Unix(),
  10721. Mtime: 0,
  10722. Count: 0,
  10723. StockCount: 0,
  10724. Creater: creator,
  10725. BatchNumberId: warehouse.ID,
  10726. WarehouseOutId: 0,
  10727. }
  10728. CreateGoodErrcode(goodErrcode)
  10729. return errThree
  10730. }
  10731. //查询剩余库存
  10732. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  10733. var sum_count int64
  10734. for _, item := range goodList {
  10735. sum_count += item.StockCount
  10736. }
  10737. warehouseOutInfo := &models.WarehouseOutInfo{
  10738. WarehouseInfotId: warehouse.ID,
  10739. }
  10740. //添加入库单详情ID
  10741. errOne := UpdatedSigleWarehouseOutInfo(goods.ID, warehouseOutInfo)
  10742. if errOne != nil {
  10743. goodErrcode := models.XtGoodErrcode{
  10744. UserOrgId: goods.OrgId,
  10745. Errcode: "添加入库单详情ID",
  10746. GoodId: goods.GoodId,
  10747. Status: 1,
  10748. Ctime: time.Now().Unix(),
  10749. Mtime: 0,
  10750. Count: 0,
  10751. StockCount: 0,
  10752. Creater: creator,
  10753. BatchNumberId: warehouse.ID,
  10754. WarehouseOutId: 0,
  10755. }
  10756. CreateGoodErrcode(goodErrcode)
  10757. return errOne
  10758. }
  10759. stockFlow := models.VmStockFlow{
  10760. WarehousingId: warehouse.ID,
  10761. GoodId: goods.GoodId,
  10762. Number: warehouse.Number,
  10763. LicenseNumber: goods.LicenseNumber,
  10764. Count: goods.Count,
  10765. UserOrgId: goods.OrgId,
  10766. PatientId: goods.PatientId,
  10767. SystemTime: record_time,
  10768. ConsumableType: 2,
  10769. IsSys: goods.IsSys,
  10770. WarehousingOrder: warehouse.WarehousingOrder,
  10771. WarehouseOutId: warehouseOut.ID,
  10772. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  10773. IsEdit: 1,
  10774. CancelStockId: 0,
  10775. CancelOrderNumber: "",
  10776. Manufacturer: goods.Manufacturer,
  10777. Dealer: goods.Dealer,
  10778. Creator: creator,
  10779. UpdateCreator: 0,
  10780. Status: 1,
  10781. Ctime: time.Now().Unix(),
  10782. Mtime: 0,
  10783. Price: warehouse.PackingPrice, //零售价
  10784. WarehousingDetailId: warehouse.ID,
  10785. WarehouseOutDetailId: goods.ID,
  10786. CancelOutDetailId: 0,
  10787. ProductDate: goods.ProductDate,
  10788. ExpireDate: goods.ExpiryDate,
  10789. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  10790. StorehouseId: warehouse.StorehouseId,
  10791. AdminUserId: goods.AdminUserId,
  10792. BuyPrice: warehouse.Price, //进货价
  10793. StockCount: goods.StockCount,
  10794. BatchNumberCount: warehouse.StockCount - goods.Count,
  10795. IsCheck: 1,
  10796. OverCount: sum_count,
  10797. RegisterNumber: goods.RegisterNumber,
  10798. }
  10799. //创建出库流水
  10800. errflow := CreateStockFlowOne(stockFlow)
  10801. if errflow != nil {
  10802. goodErrcode := models.XtGoodErrcode{
  10803. UserOrgId: goods.OrgId,
  10804. Errcode: "创建流水报错",
  10805. GoodId: goods.GoodId,
  10806. Status: 1,
  10807. Ctime: time.Now().Unix(),
  10808. Mtime: 0,
  10809. Count: 0,
  10810. StockCount: 0,
  10811. Creater: creator,
  10812. BatchNumberId: warehouse.ID,
  10813. WarehouseOutId: 0,
  10814. }
  10815. CreateGoodErrcode(goodErrcode)
  10816. }
  10817. //添加入库ID
  10818. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, goods.OrgId, sum_count)
  10819. if errWarehouse != nil {
  10820. goodErrcode := models.XtGoodErrcode{
  10821. UserOrgId: goods.OrgId,
  10822. Errcode: "添加入库ID报错",
  10823. GoodId: goods.GoodId,
  10824. Status: 1,
  10825. Ctime: time.Now().Unix(),
  10826. Mtime: 0,
  10827. Count: 0,
  10828. StockCount: 0,
  10829. Creater: creator,
  10830. BatchNumberId: warehouse.ID,
  10831. WarehouseOutId: 0,
  10832. }
  10833. CreateGoodErrcode(goodErrcode)
  10834. }
  10835. //更新出库数量
  10836. errsumcode := ModifyGoodSumCount(goods.StorehouseId, goods.Count, goods.OrgId, goods.GoodId)
  10837. if errsumcode != nil {
  10838. goodErrcode := models.XtGoodErrcode{
  10839. UserOrgId: goods.OrgId,
  10840. Errcode: "更新出库数量报错",
  10841. GoodId: goods.GoodId,
  10842. Status: 1,
  10843. Ctime: time.Now().Unix(),
  10844. Mtime: 0,
  10845. Count: 0,
  10846. StockCount: 0,
  10847. Creater: creator,
  10848. BatchNumberId: warehouse.ID,
  10849. WarehouseOutId: 0,
  10850. }
  10851. CreateGoodErrcode(goodErrcode)
  10852. }
  10853. if warehouse.StockCount < maxNumber {
  10854. goodErrcode := models.XtGoodErrcode{
  10855. UserOrgId: goods.OrgId,
  10856. Errcode: "出库接口库存数量不足",
  10857. GoodId: goods.GoodId,
  10858. Status: 1,
  10859. Ctime: time.Now().Unix(),
  10860. Mtime: 0,
  10861. Count: 0,
  10862. StockCount: 0,
  10863. Creater: creator,
  10864. BatchNumberId: warehouse.ID,
  10865. WarehouseOutId: 0,
  10866. }
  10867. CreateGoodErrcode(goodErrcode)
  10868. return errors.New("库存数量不足")
  10869. }
  10870. return nil
  10871. } else {
  10872. // 出库完成后,要将该批次库存清零
  10873. warehouse.StockCount = 0
  10874. warehouse.Mtime = time.Now().Unix()
  10875. errThree := UpDateWarehouseInfoByStock(&warehouse)
  10876. if errThree != nil {
  10877. goodErrcode := models.XtGoodErrcode{
  10878. UserOrgId: goods.OrgId,
  10879. Errcode: "扣减库存报错",
  10880. GoodId: goods.GoodId,
  10881. Status: 1,
  10882. Ctime: time.Now().Unix(),
  10883. Mtime: 0,
  10884. Count: 0,
  10885. StockCount: 0,
  10886. Creater: creator,
  10887. BatchNumberId: warehouse.ID,
  10888. WarehouseOutId: 0,
  10889. }
  10890. CreateGoodErrcode(goodErrcode)
  10891. return errThree
  10892. }
  10893. //查询剩余库存
  10894. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  10895. var sum_count int64
  10896. for _, item := range goodList {
  10897. sum_count += item.StockCount
  10898. }
  10899. stockFlow := models.VmStockFlow{
  10900. WarehousingId: warehouse.ID,
  10901. GoodId: goods.GoodId,
  10902. Number: warehouse.Number,
  10903. LicenseNumber: goods.LicenseNumber,
  10904. Count: stock_number,
  10905. UserOrgId: goods.OrgId,
  10906. PatientId: goods.PatientId,
  10907. SystemTime: record_time,
  10908. ConsumableType: 2,
  10909. IsSys: goods.IsSys,
  10910. WarehousingOrder: warehouse.WarehousingOrder,
  10911. WarehouseOutId: warehouseOut.ID,
  10912. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  10913. IsEdit: 1,
  10914. CancelStockId: 0,
  10915. CancelOrderNumber: "",
  10916. Manufacturer: goods.Manufacturer,
  10917. Dealer: goods.Dealer,
  10918. Creator: creator,
  10919. UpdateCreator: 0,
  10920. Status: 1,
  10921. Ctime: time.Now().Unix(),
  10922. Mtime: 0,
  10923. Price: warehouse.PackingPrice, //零售价
  10924. WarehousingDetailId: warehouse.ID,
  10925. WarehouseOutDetailId: goods.ID,
  10926. CancelOutDetailId: 0,
  10927. ProductDate: goods.ProductDate,
  10928. ExpireDate: goods.ExpiryDate,
  10929. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  10930. StorehouseId: warehouse.StorehouseId,
  10931. AdminUserId: goods.AdminUserId,
  10932. BuyPrice: warehouse.Price, //进货价
  10933. StockCount: goods.StockCount,
  10934. BatchNumberCount: 0,
  10935. IsCheck: 1,
  10936. OverCount: sum_count,
  10937. RegisterNumber: goods.RegisterNumber,
  10938. }
  10939. //创建出库流水
  10940. errflow := CreateStockFlowOne(stockFlow)
  10941. if errflow != nil {
  10942. goodErrcode := models.XtGoodErrcode{
  10943. UserOrgId: goods.OrgId,
  10944. Errcode: "创建出库流水报错",
  10945. GoodId: goods.GoodId,
  10946. Status: 1,
  10947. Ctime: time.Now().Unix(),
  10948. Mtime: 0,
  10949. Count: 0,
  10950. StockCount: 0,
  10951. Creater: creator,
  10952. BatchNumberId: warehouse.ID,
  10953. WarehouseOutId: 0,
  10954. }
  10955. CreateGoodErrcode(goodErrcode)
  10956. }
  10957. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, goods.OrgId, sum_count)
  10958. if errWarehouse != nil {
  10959. goodErrcode := models.XtGoodErrcode{
  10960. UserOrgId: goods.OrgId,
  10961. Errcode: "添加入库ID报错",
  10962. GoodId: goods.GoodId,
  10963. Status: 1,
  10964. Ctime: time.Now().Unix(),
  10965. Mtime: 0,
  10966. Count: 0,
  10967. StockCount: 0,
  10968. Creater: creator,
  10969. BatchNumberId: warehouse.ID,
  10970. WarehouseOutId: 0,
  10971. }
  10972. CreateGoodErrcode(goodErrcode)
  10973. }
  10974. //更新出库数量
  10975. errcodecout := ModifyGoodSumCount(goods.StorehouseId, stock_number, goods.OrgId, goods.GoodId)
  10976. if errcodecout != nil {
  10977. goodErrcode := models.XtGoodErrcode{
  10978. UserOrgId: goods.OrgId,
  10979. Errcode: "更新出库数量报错",
  10980. GoodId: goods.GoodId,
  10981. Status: 1,
  10982. Ctime: time.Now().Unix(),
  10983. Mtime: 0,
  10984. Count: 0,
  10985. StockCount: 0,
  10986. Creater: creator,
  10987. BatchNumberId: warehouse.ID,
  10988. WarehouseOutId: 0,
  10989. }
  10990. CreateGoodErrcode(goodErrcode)
  10991. }
  10992. }
  10993. goods.Count = deliver_number - stock_number
  10994. ConsumablesDeliveryThirty(orgID, record_time, goods, warehouseOut, count, creator)
  10995. return nil
  10996. }
  10997. // 药品手动出库 递归方式
  10998. func AutoDrugDeliverInfoFourtyOne(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  10999. fmt.Println("出库数量", prescribingNumber)
  11000. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  11001. var deliver_number int64 = 0
  11002. var stock_number int64 = 0
  11003. var retail_price float64
  11004. if advice.CountUnit == drup.MaxUnit {
  11005. deliver_number = prescribingNumber * drup.MinNumber
  11006. formatInt := strconv.FormatInt(prescribingNumber, 10)
  11007. floatcount, _ := strconv.ParseFloat(formatInt, 64)
  11008. retail_price = floatcount * advice.Price
  11009. } else {
  11010. deliver_number = prescribingNumber
  11011. formatInt := strconv.FormatInt(prescribingNumber, 10)
  11012. floatcount, _ := strconv.ParseFloat(formatInt, 64)
  11013. retail_price = floatcount * advice.Price
  11014. }
  11015. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  11016. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  11017. var stockMax int64
  11018. var stockMin int64
  11019. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  11020. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  11021. //fmt.Println("stockmax", stockMax)
  11022. //fmt.Println("stockMin", stockMin)
  11023. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  11024. UpdateMinNumber(lastWarehouse.ID, stockMin)
  11025. }
  11026. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, warehouseout.StorehouseId)
  11027. if err != nil {
  11028. return err
  11029. }
  11030. // 将该批次的剩余库存数量转换为拆零数量
  11031. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  11032. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  11033. }
  11034. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  11035. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  11036. }
  11037. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  11038. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  11039. }
  11040. fmt.Println("stock_number", stock_number)
  11041. fmt.Println("deliver_number", deliver_number)
  11042. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  11043. if stock_number >= deliver_number {
  11044. // 出库完成后,要减去对应批次的库存数量
  11045. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  11046. var maxNumber int64 = 0
  11047. var minNumber int64 = 0
  11048. maxNumber = deliver_number / drup.MinNumber
  11049. minNumber = deliver_number % drup.MinNumber
  11050. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  11051. minNumber = maxNumber
  11052. }
  11053. if drup.MaxUnit != drup.MinUnit {
  11054. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  11055. return errors.New("库存数量不足")
  11056. }
  11057. }
  11058. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  11059. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  11060. }
  11061. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  11062. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  11063. }
  11064. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  11065. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  11066. }
  11067. if warehouse.StockMaxNumber < 0 {
  11068. warehouse.StockMaxNumber = 0
  11069. }
  11070. if warehouse.StockMinNumber < 0 {
  11071. warehouse.StockMinNumber = 0
  11072. }
  11073. warehouse.Mtime = time.Now().Unix()
  11074. if warehouse.StockMinNumber < minNumber {
  11075. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  11076. if warehouse.MaxUnit != warehouse.MinUnit {
  11077. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  11078. }
  11079. } else {
  11080. //fmt.Println("maxNumber23323323332322323w9", maxNumber)
  11081. //fmt.Println("maxNumber23323323332322323w9", minNumber)
  11082. if minNumber > 0 {
  11083. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  11084. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  11085. } else {
  11086. if (warehouse.StockMinNumber - minNumber) >= 0 {
  11087. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  11088. }
  11089. }
  11090. }
  11091. if minNumber == 0 && maxNumber != 1 {
  11092. if warehouse.StockMinNumber > 0 {
  11093. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  11094. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  11095. }
  11096. }
  11097. }
  11098. }
  11099. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  11100. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  11101. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  11102. }
  11103. }
  11104. if drup.MaxUnit != drup.MinUnit {
  11105. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  11106. return errors.New("库存数量不足")
  11107. }
  11108. }
  11109. if warehouse.StockMinNumber <= 0 {
  11110. warehouse.StockMinNumber = 0
  11111. }
  11112. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  11113. if errThree != nil {
  11114. return errors.New("扣减库存失败")
  11115. }
  11116. //查询该药品剩余库存
  11117. infolist, _ := FindDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId)
  11118. var over_count int64
  11119. for _, its := range infolist {
  11120. if its.MaxUnit == drup.MaxUnit {
  11121. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  11122. }
  11123. over_count += its.StockMaxNumber + its.StockMinNumber
  11124. }
  11125. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  11126. drugflow := models.DrugFlow{
  11127. WarehouseOutId: warehouseout.ID,
  11128. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  11129. DrugId: advice.DrugId,
  11130. Number: warehouse.Number,
  11131. ProductDate: advice.ProductDate,
  11132. ExpireDate: advice.ExpiryDate,
  11133. Count: deliver_number,
  11134. Price: warehouse.RetailPrice,
  11135. Status: 1,
  11136. Ctime: time.Now().Unix(),
  11137. UserOrgId: advice.OrgId,
  11138. Manufacturer: advice.Manufacturer,
  11139. Dealer: advice.Dealer,
  11140. BatchNumber: warehouse.BatchNumber,
  11141. MaxUnit: drup.MinUnit,
  11142. ConsumableType: 2,
  11143. IsEdit: 1,
  11144. Creator: 0,
  11145. IsSys: 0,
  11146. WarehouseOutDetailId: drugWareInfo.ID,
  11147. AdviceId: advice.ID,
  11148. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  11149. StorehouseId: advice.StorehouseId,
  11150. AdminUserId: advice.AdminUserId,
  11151. LastPrice: warehouse.Price,
  11152. StockCount: advice.StockCount,
  11153. WarehousingDetailId: warehouse.ID,
  11154. OverCount: over_count,
  11155. RetailPrice: retail_price, //手动出库出库价格
  11156. WarehousingId: warehouse.ID,
  11157. SystemTime: advice.SysRecordTime,
  11158. }
  11159. if warehouse.RetailPrice == 0 {
  11160. drugflow.Price = advice.Price
  11161. }
  11162. CreateDrugFlowOne(drugflow)
  11163. //更新出库id
  11164. UpdateDrugWarehouseOutInfo(advice.ID, warehouse.ID, advice.OrgId, over_count)
  11165. return nil
  11166. } else {
  11167. stock_number_int := strconv.FormatInt(stock_number, 10)
  11168. stock_number_int_count, _ := strconv.ParseFloat(stock_number_int, 64)
  11169. retail_price = stock_number_int_count * advice.Price
  11170. //扣减库存
  11171. warehouse.StockMaxNumber = 0
  11172. warehouse.StockMinNumber = 0
  11173. warehouse.Mtime = time.Now().Unix()
  11174. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  11175. if errThree != nil {
  11176. return errThree
  11177. }
  11178. //查询该药品剩余库存
  11179. infolist, _ := FindDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId)
  11180. var over_count int64
  11181. for _, its := range infolist {
  11182. if its.MaxUnit == drup.MaxUnit {
  11183. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  11184. }
  11185. over_count += its.StockMaxNumber + its.StockMinNumber
  11186. }
  11187. drugWareInfo, _ := GetLastWarehouseOutInfoById(advice.DrugId)
  11188. drugflow := models.DrugFlow{
  11189. WarehouseOutId: warehouseout.ID,
  11190. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  11191. DrugId: advice.DrugId,
  11192. Number: warehouse.Number,
  11193. ProductDate: advice.ProductDate,
  11194. ExpireDate: advice.ExpiryDate,
  11195. Count: stock_number,
  11196. Price: warehouse.RetailPrice,
  11197. Status: 1,
  11198. Ctime: time.Now().Unix(),
  11199. UserOrgId: advice.OrgId,
  11200. Manufacturer: advice.Manufacturer,
  11201. Dealer: advice.Dealer,
  11202. BatchNumber: warehouse.BatchNumber,
  11203. MaxUnit: drup.MinUnit,
  11204. ConsumableType: 2,
  11205. IsEdit: 1,
  11206. Creator: 0,
  11207. IsSys: 0,
  11208. WarehouseOutDetailId: drugWareInfo.ID,
  11209. AdviceId: advice.ID,
  11210. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  11211. StorehouseId: advice.StorehouseId,
  11212. AdminUserId: advice.AdminUserId,
  11213. LastPrice: warehouse.Price,
  11214. StockCount: advice.StockCount,
  11215. OverCount: over_count,
  11216. RetailPrice: retail_price, //手动出库出库价格
  11217. WarehousingId: warehouse.ID,
  11218. WarehousingDetailId: warehouse.ID,
  11219. SystemTime: advice.SysRecordTime,
  11220. }
  11221. CreateDrugFlowOne(drugflow)
  11222. //更新出库id
  11223. UpdateDrugWarehouseOutInfo(advice.ID, warehouse.ID, advice.OrgId, over_count)
  11224. // 出库完成后,要将该批次库存清零
  11225. prescribingNumber_two_temp := deliver_number - stock_number
  11226. advice.CountUnit = drup.MinUnit
  11227. AutoDrugDeliverInfoFourtyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  11228. }
  11229. return
  11230. }
  11231. func UpdateDrugWarehouseOutInfo(id int64, warehouse_info_id int64, org_id int64, over_count int64) error {
  11232. ut := XTWriteDB().Begin()
  11233. 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
  11234. if err != nil {
  11235. ut.Rollback()
  11236. return err
  11237. }
  11238. ut.Commit()
  11239. return err
  11240. }
  11241. func UpdateGoodWarehouseOutById(id int64, warehouse_info_id int64, org_id int64, over_count int64) error {
  11242. utx := XTWriteDB().Begin()
  11243. 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
  11244. if err != nil {
  11245. utx.Rollback()
  11246. return err
  11247. }
  11248. utx.Commit()
  11249. return err
  11250. }
  11251. func ChangeMaxNumber(id int64, count int64) error {
  11252. 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
  11253. return err
  11254. }
  11255. func UpdateMinNumber(id int64, count int64) error {
  11256. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1 and is_check = 1", id).Update(map[string]interface{}{"stock_min_number": count}).Error
  11257. return err
  11258. }
  11259. func FindLastDrugWarehouseOutInfo(orgid int64) (models.DrugWarehouseOutInfo, error) {
  11260. info := models.DrugWarehouseOutInfo{}
  11261. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&info).Error
  11262. return info, err
  11263. }
  11264. func GetAllGoodSumCount(goodid int64, orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  11265. 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
  11266. return info, err
  11267. }
  11268. func CreatedFlushInfo(info models.XtWarehouseFlushInfo) error {
  11269. ut := writeDb.Begin()
  11270. err := ut.Create(&info).Error
  11271. if err != nil {
  11272. ut.Rollback()
  11273. return err
  11274. }
  11275. ut.Commit()
  11276. return err
  11277. }
  11278. func FindDrugWarehouseInfoList(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  11279. 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
  11280. return info, err
  11281. }
  11282. func CreatedDrugFlushInfo(info models.XtDrugWarehouseFlushInfo) error {
  11283. ut := writeDb.Begin()
  11284. err := ut.Create(&info).Error
  11285. if err != nil {
  11286. ut.Rollback()
  11287. return err
  11288. }
  11289. ut.Commit()
  11290. return err
  11291. }
  11292. 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 {
  11293. 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
  11294. return err
  11295. }
  11296. func FindLastWarehouseOutInfo(orgId int64) (models.WarehouseOutInfo, error) {
  11297. info := models.WarehouseOutInfo{}
  11298. err := XTReadDB().Model(&info).Where("org_id = ? and status = 1", orgId).Find(&info).Error
  11299. return info, err
  11300. }
  11301. func ModifyAddGoodInformation(count int64, id int64) error {
  11302. ut := XTWriteDB().Begin()
  11303. err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", count)).Error
  11304. if err != nil {
  11305. ut.Rollback()
  11306. return err
  11307. }
  11308. ut.Commit()
  11309. return err
  11310. }
  11311. func ModifyReduceGoodInformation(count int64, id int64) error {
  11312. ut := XTWriteDB().Begin()
  11313. err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", count)).Error
  11314. if err != nil {
  11315. ut.Rollback()
  11316. return err
  11317. }
  11318. ut.Commit()
  11319. return err
  11320. }
  11321. func ModifyAddCancelCount(count int64, id int64) error {
  11322. ut := XTWriteDB().Begin()
  11323. err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", count)).Error
  11324. if err != nil {
  11325. ut.Rollback()
  11326. return err
  11327. }
  11328. ut.Commit()
  11329. return err
  11330. }
  11331. func ModifyReduceCancelCount(count int64, id int64) error {
  11332. ut := XTWriteDB().Begin()
  11333. err = ut.Model(&models.GoodInfo{}).Where("id = ? and status =1", id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", count)).Error
  11334. if err != nil {
  11335. ut.Rollback()
  11336. return err
  11337. }
  11338. ut.Commit()
  11339. return err
  11340. }
  11341. func ModifyAddDrugInformation(id int64, count int64, orgid int64) error {
  11342. ut := XTWriteDB().Begin()
  11343. 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
  11344. if err != nil {
  11345. ut.Rollback()
  11346. return err
  11347. }
  11348. ut.Commit()
  11349. return err
  11350. }
  11351. func ModifyReduceDrugInformation(id int64, count int64, orgid int64) error {
  11352. ut := XTWriteDB().Begin()
  11353. 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
  11354. if err != nil {
  11355. ut.Rollback()
  11356. return err
  11357. }
  11358. ut.Commit()
  11359. return err
  11360. }
  11361. func ModifyAddCancelAddInfomation(id int64, count int64, orgid int64, storehouse_id int64) error {
  11362. ut := XTWriteDB().Begin()
  11363. 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
  11364. if err != nil {
  11365. ut.Rollback()
  11366. return err
  11367. }
  11368. ut.Commit()
  11369. return err
  11370. }
  11371. func ModifyAddDrugOutReduceInfomation(id int64, count int64, orgid int64, storehouse_id int64) error {
  11372. ut := XTWriteDB().Begin()
  11373. 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
  11374. if err != nil {
  11375. ut.Rollback()
  11376. return err
  11377. }
  11378. ut.Commit()
  11379. return err
  11380. }
  11381. func ModifyAddDrugOutAddInfomation(id int64, count int64, orgid int64, storehouse_id int64) error {
  11382. ut := XTWriteDB().Begin()
  11383. 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
  11384. if err != nil {
  11385. ut.Rollback()
  11386. return err
  11387. }
  11388. ut.Commit()
  11389. return err
  11390. }
  11391. func ModifyDrugCancelWarehouseInfo(id int64, count int64, orgid int64, storehouse_id int64) error {
  11392. ut := XTWriteDB().Begin()
  11393. 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
  11394. if err != nil {
  11395. ut.Rollback()
  11396. return err
  11397. }
  11398. ut.Commit()
  11399. return err
  11400. }
  11401. func ModifyGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
  11402. ut := XTWriteDB().Begin()
  11403. 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
  11404. if err != nil {
  11405. ut.Rollback()
  11406. return err
  11407. }
  11408. 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
  11409. if err != nil {
  11410. ut.Rollback()
  11411. return err
  11412. }
  11413. ut.Commit()
  11414. return err
  11415. }
  11416. func ModifyReduceGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
  11417. ut := XTWriteDB().Begin()
  11418. 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
  11419. if err != nil {
  11420. ut.Rollback()
  11421. return err
  11422. }
  11423. 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
  11424. if err != nil {
  11425. ut.Rollback()
  11426. return err
  11427. }
  11428. ut.Commit()
  11429. return err
  11430. }
  11431. func ModifyAddGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
  11432. ut := XTWriteDB().Begin()
  11433. 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
  11434. if err != nil {
  11435. ut.Rollback()
  11436. return err
  11437. }
  11438. ut.Commit()
  11439. return err
  11440. }
  11441. func UpdateSumAddCancelCount(user_org_id int64, good_id int64, storehouse_id int64, count int64) error {
  11442. ut := XTWriteDB().Begin()
  11443. 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
  11444. if err != nil {
  11445. ut.Rollback()
  11446. return err
  11447. }
  11448. ut.Commit()
  11449. return err
  11450. }
  11451. func UpdateSumReduceCancelCount(user_org_id int64, good_id int64, storehouse_id int64, count int64) error {
  11452. ut := XTWriteDB().Begin()
  11453. 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
  11454. if err != nil {
  11455. ut.Rollback()
  11456. return err
  11457. }
  11458. ut.Commit()
  11459. return err
  11460. }
  11461. func UpdateSumAddOutCount(user_org_id int64, good_id int64, storehouse_id int64, count int64) error {
  11462. ut := XTWriteDB().Begin()
  11463. 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
  11464. if err != nil {
  11465. ut.Rollback()
  11466. return err
  11467. }
  11468. ut.Commit()
  11469. return err
  11470. }
  11471. func FindDrugWarehouseInfoFlushCount(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  11472. err = XTReadDB().Where("drug_id = ? and org_id = ? and status = 1 and is_check = 1", drug_id, orgid).Find(&info).Error
  11473. return info, err
  11474. }
  11475. func FindDrugWarehouseInfoFlushCountByStorehouse(drug_id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
  11476. 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
  11477. return info, err
  11478. }
  11479. // 结算出库
  11480. func HisSettleDrugsDelivery(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo, order_id int64) (err error) {
  11481. // 1.判断药品是否来自专用字典的药品库
  11482. // 2.判断当天当前机构有没有创建出库单,没有则创建
  11483. // 3.创建出库流程
  11484. // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  11485. isHasWay := false
  11486. record_time := int64(0)
  11487. isHasWay = true
  11488. record_time = advice.RecordDate
  11489. if isHasWay {
  11490. //判断当天当前机构有没有创建结算出库单,没有则创建
  11491. out, err := FindDrugSettleStockOutByIsSys(orgID, 5, record_time)
  11492. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  11493. if err == gorm.ErrRecordNotFound {
  11494. timeStr := time.Now().Format("2006-01-02")
  11495. timeArr := strings.Split(timeStr, "-")
  11496. total, _ := FindAllDrugWarehouseOut(orgID)
  11497. total = total + 1
  11498. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  11499. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  11500. number = number + total
  11501. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  11502. warehouseOut := models.DrugWarehouseOut{
  11503. WarehouseOutOrderNumber: warehousing_out_order,
  11504. OperationTime: time.Now().Unix(),
  11505. OrgId: orgID,
  11506. Creater: creater,
  11507. Ctime: time.Now().Unix(),
  11508. Status: 1,
  11509. WarehouseOutTime: record_time,
  11510. Dealer: 0,
  11511. Manufacturer: 0,
  11512. Type: 1, //结算出库
  11513. IsSys: 5,
  11514. StorehouseId: houseConfig.DrugStorehouseOut,
  11515. IsCheck: 1,
  11516. OrderId: order_id,
  11517. }
  11518. err := AddSigleDrugWarehouseOut(&warehouseOut)
  11519. if err != nil {
  11520. utils.TraceLog("创建出库单失败 err = %v", err)
  11521. return err
  11522. } else {
  11523. out = warehouseOut
  11524. }
  11525. }
  11526. // 出库流程
  11527. // 1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  11528. drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
  11529. if drup.ID > 0 {
  11530. prescribingNumber := advice.PrescribingNumber
  11531. var prescribingNumberOne int64
  11532. prescribingCount_temp := strconv.FormatFloat(math.Abs(advice.PrescribingNumber), 'f', 0, 64)
  11533. prescribe_count, _ := strconv.ParseInt(prescribingCount_temp, 10, 64)
  11534. if advice.PrescribingNumberUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  11535. prescribingNumberOne = prescribe_count * drup.MinNumber
  11536. }
  11537. if advice.PrescribingNumberUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  11538. prescribingNumberOne = prescribe_count
  11539. }
  11540. if advice.PrescribingNumberUnit == drup.MinUnit && drup.MaxUnit == drup.MinUnit {
  11541. prescribingNumberOne = prescribe_count
  11542. }
  11543. HisSettleDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice, prescribingNumberOne)
  11544. } else {
  11545. return errors.New("药品信息不存在")
  11546. }
  11547. }
  11548. return
  11549. }
  11550. // 耗材结算出库
  11551. func ConsumableSettleDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, creator int64, order_id int64) (err error) {
  11552. var deliver_number int64 = 0
  11553. var stock_number int64 = 0
  11554. var maxNumber int64 = 0
  11555. fmt.Println("223323233233223233223233223woo", goods.Count)
  11556. deliver_number = goods.Count
  11557. // 根据先进先出原则,查询最先入库的批次,进行出库
  11558. // 如果没有对应的库存,则报错
  11559. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  11560. if err != nil {
  11561. goodErrcode := models.XtGoodErrcode{
  11562. UserOrgId: goods.UserOrgId,
  11563. Errcode: "查询库存信息报错",
  11564. GoodId: goods.GoodId,
  11565. Status: 1,
  11566. Ctime: time.Now().Unix(),
  11567. Mtime: 0,
  11568. Count: 0,
  11569. StockCount: 0,
  11570. Creater: creator,
  11571. BatchNumberId: warehouse.ID,
  11572. WarehouseOutId: 0,
  11573. }
  11574. CreateGoodErrcode(goodErrcode)
  11575. return err
  11576. }
  11577. stock_number = warehouse.StockCount
  11578. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  11579. fmt.Println("stock_numnber233232232323", stock_number)
  11580. fmt.Println("deliver_nmuberwooeooo2oo23", deliver_number)
  11581. if stock_number >= deliver_number {
  11582. maxNumber = goods.Count
  11583. warehouse.StockCount = warehouse.StockCount - maxNumber
  11584. warehouse.Mtime = time.Now().Unix()
  11585. //扣减库存
  11586. errThree := UpDateWarehouseInfoByStock(&warehouse)
  11587. if errThree != nil {
  11588. goodErrcode := models.XtGoodErrcode{
  11589. UserOrgId: goods.UserOrgId,
  11590. Errcode: "扣减库存失败",
  11591. GoodId: goods.GoodId,
  11592. Status: 1,
  11593. Ctime: time.Now().Unix(),
  11594. Mtime: 0,
  11595. Count: 0,
  11596. StockCount: 0,
  11597. Creater: creator,
  11598. BatchNumberId: warehouse.ID,
  11599. WarehouseOutId: 0,
  11600. }
  11601. CreateGoodErrcode(goodErrcode)
  11602. return errThree
  11603. }
  11604. //查询剩余库存
  11605. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  11606. var sum_count int64
  11607. for _, item := range goodList {
  11608. sum_count += item.StockCount
  11609. }
  11610. warehouseOutInfo := &models.WarehouseOutInfo{
  11611. WarehouseInfotId: warehouse.ID,
  11612. }
  11613. //添加入库单详情ID
  11614. errOne := UpdatedSigleWarehouseOutInfo(goods.ID, warehouseOutInfo)
  11615. if errOne != nil {
  11616. goodErrcode := models.XtGoodErrcode{
  11617. UserOrgId: goods.UserOrgId,
  11618. Errcode: "添加入库单详情ID",
  11619. GoodId: goods.GoodId,
  11620. Status: 1,
  11621. Ctime: time.Now().Unix(),
  11622. Mtime: 0,
  11623. Count: 0,
  11624. StockCount: 0,
  11625. Creater: creator,
  11626. BatchNumberId: warehouse.ID,
  11627. WarehouseOutId: 0,
  11628. }
  11629. CreateGoodErrcode(goodErrcode)
  11630. return errOne
  11631. }
  11632. OutInfo := &models.WarehouseOutInfo{
  11633. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  11634. WarehouseOutId: warehouseOut.ID,
  11635. WarehouseInfotId: warehouse.ID,
  11636. Status: 1,
  11637. Ctime: time.Now().Unix(),
  11638. Remark: warehouse.Remark,
  11639. OrgId: orgID,
  11640. Type: 1,
  11641. Manufacturer: warehouse.Manufacturer,
  11642. Dealer: warehouse.Dealer,
  11643. IsSys: 5,
  11644. SysRecordTime: record_time,
  11645. GoodTypeId: goods.GoodTypeId,
  11646. GoodId: goods.GoodId,
  11647. PatientId: patient_id,
  11648. Number: warehouse.Number,
  11649. LicenseNumber: warehouse.LicenseNumber,
  11650. Price: warehouse.PackingPrice,
  11651. ExpiryDate: warehouse.ExpiryDate,
  11652. ProductDate: warehouse.ProductDate,
  11653. ProjectId: goods.ProjectId,
  11654. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  11655. StorehouseId: goods.StorehouseId,
  11656. IsCheck: 1,
  11657. OverCount: sum_count,
  11658. RegisterNumber: warehouse.RegisterNumber,
  11659. OrderId: order_id,
  11660. }
  11661. fmt.Println("goods.count2323232233232233232322332322332", goods.Count)
  11662. OutInfo.Count = goods.Count
  11663. AddSigleWarehouseOutInfo(OutInfo)
  11664. stockFlow := models.VmStockFlow{
  11665. WarehousingId: warehouse.ID,
  11666. GoodId: goods.GoodId,
  11667. Number: warehouse.Number,
  11668. LicenseNumber: warehouse.LicenseNumber,
  11669. Count: goods.Count,
  11670. UserOrgId: orgID,
  11671. PatientId: patient_id,
  11672. SystemTime: record_time,
  11673. ConsumableType: 15,
  11674. IsSys: 5,
  11675. WarehousingOrder: warehouse.WarehousingOrder,
  11676. WarehouseOutId: warehouseOut.ID,
  11677. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  11678. IsEdit: 1,
  11679. CancelStockId: 0,
  11680. CancelOrderNumber: "",
  11681. Manufacturer: warehouse.Manufacturer,
  11682. Dealer: warehouse.Dealer,
  11683. Creator: creator,
  11684. UpdateCreator: 0,
  11685. Status: 1,
  11686. Ctime: time.Now().Unix(),
  11687. Mtime: 0,
  11688. Price: warehouse.PackingPrice, //零售价
  11689. WarehousingDetailId: warehouse.ID,
  11690. WarehouseOutDetailId: goods.ID,
  11691. CancelOutDetailId: 0,
  11692. ProductDate: warehouse.ProductDate,
  11693. ExpireDate: warehouse.ExpiryDate,
  11694. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  11695. StorehouseId: warehouse.StorehouseId,
  11696. AdminUserId: creator,
  11697. BuyPrice: warehouse.Price, //进货价
  11698. StockCount: "",
  11699. BatchNumberCount: warehouse.StockCount - goods.Count,
  11700. IsCheck: 1,
  11701. OverCount: sum_count,
  11702. RegisterNumber: warehouse.RegisterNumber,
  11703. OrderId: order_id,
  11704. ProjectId: goods.ProjectId,
  11705. }
  11706. //创建出库流水
  11707. errflow := CreateStockFlowOne(stockFlow)
  11708. if errflow != nil {
  11709. goodErrcode := models.XtGoodErrcode{
  11710. UserOrgId: orgID,
  11711. Errcode: "创建流水报错",
  11712. GoodId: goods.GoodId,
  11713. Status: 1,
  11714. Ctime: time.Now().Unix(),
  11715. Mtime: 0,
  11716. Count: 0,
  11717. StockCount: 0,
  11718. Creater: creator,
  11719. BatchNumberId: warehouse.ID,
  11720. WarehouseOutId: 0,
  11721. }
  11722. CreateGoodErrcode(goodErrcode)
  11723. }
  11724. //添加入库ID
  11725. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count)
  11726. if errWarehouse != nil {
  11727. goodErrcode := models.XtGoodErrcode{
  11728. UserOrgId: orgID,
  11729. Errcode: "添加入库ID报错",
  11730. GoodId: goods.GoodId,
  11731. Status: 1,
  11732. Ctime: time.Now().Unix(),
  11733. Mtime: 0,
  11734. Count: 0,
  11735. StockCount: 0,
  11736. Creater: creator,
  11737. BatchNumberId: warehouse.ID,
  11738. WarehouseOutId: 0,
  11739. }
  11740. CreateGoodErrcode(goodErrcode)
  11741. }
  11742. //更新出库数量
  11743. errsumcode := ModifyGoodSumCount(goods.StorehouseId, goods.Count, orgID, goods.GoodId)
  11744. if errsumcode != nil {
  11745. goodErrcode := models.XtGoodErrcode{
  11746. UserOrgId: orgID,
  11747. Errcode: "更新出库数量报错",
  11748. GoodId: goods.GoodId,
  11749. Status: 1,
  11750. Ctime: time.Now().Unix(),
  11751. Mtime: 0,
  11752. Count: 0,
  11753. StockCount: 0,
  11754. Creater: creator,
  11755. BatchNumberId: warehouse.ID,
  11756. WarehouseOutId: 0,
  11757. }
  11758. CreateGoodErrcode(goodErrcode)
  11759. }
  11760. if warehouse.StockCount < maxNumber {
  11761. goodErrcode := models.XtGoodErrcode{
  11762. UserOrgId: orgID,
  11763. Errcode: "出库接口库存数量不足",
  11764. GoodId: goods.GoodId,
  11765. Status: 1,
  11766. Ctime: time.Now().Unix(),
  11767. Mtime: 0,
  11768. Count: 0,
  11769. StockCount: 0,
  11770. Creater: creator,
  11771. BatchNumberId: warehouse.ID,
  11772. WarehouseOutId: 0,
  11773. }
  11774. CreateGoodErrcode(goodErrcode)
  11775. return errors.New("库存数量不足")
  11776. }
  11777. return nil
  11778. } else {
  11779. // 出库完成后,要将该批次库存清零
  11780. warehouse.StockCount = 0
  11781. warehouse.Mtime = time.Now().Unix()
  11782. errThree := UpDateWarehouseInfoByStock(&warehouse)
  11783. if errThree != nil {
  11784. goodErrcode := models.XtGoodErrcode{
  11785. UserOrgId: orgID,
  11786. Errcode: "扣减库存报错",
  11787. GoodId: goods.GoodId,
  11788. Status: 1,
  11789. Ctime: time.Now().Unix(),
  11790. Mtime: 0,
  11791. Count: 0,
  11792. StockCount: 0,
  11793. Creater: creator,
  11794. BatchNumberId: warehouse.ID,
  11795. WarehouseOutId: 0,
  11796. }
  11797. CreateGoodErrcode(goodErrcode)
  11798. return errThree
  11799. }
  11800. //查询剩余库存
  11801. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  11802. var sum_count int64
  11803. for _, item := range goodList {
  11804. sum_count += item.StockCount
  11805. }
  11806. OutInfo := &models.WarehouseOutInfo{
  11807. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  11808. WarehouseOutId: warehouseOut.ID,
  11809. WarehouseInfotId: warehouse.ID,
  11810. Status: 1,
  11811. Ctime: time.Now().Unix(),
  11812. Remark: warehouse.Remark,
  11813. OrgId: orgID,
  11814. Type: 1,
  11815. Manufacturer: warehouse.Manufacturer,
  11816. Dealer: warehouse.Dealer,
  11817. IsSys: 5,
  11818. SysRecordTime: record_time,
  11819. GoodTypeId: goods.GoodTypeId,
  11820. GoodId: goods.GoodId,
  11821. PatientId: patient_id,
  11822. Number: warehouse.Number,
  11823. LicenseNumber: warehouse.LicenseNumber,
  11824. Price: warehouse.PackingPrice,
  11825. ExpiryDate: warehouse.ExpiryDate,
  11826. ProductDate: warehouse.ProductDate,
  11827. ProjectId: goods.ProjectId,
  11828. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  11829. StorehouseId: goods.StorehouseId,
  11830. IsCheck: 1,
  11831. OverCount: sum_count,
  11832. RegisterNumber: warehouse.RegisterNumber,
  11833. OrderId: order_id,
  11834. }
  11835. OutInfo.Count = stock_number
  11836. AddSigleWarehouseOutInfo(OutInfo)
  11837. stockFlow := models.VmStockFlow{
  11838. WarehousingId: warehouse.ID,
  11839. GoodId: goods.GoodId,
  11840. Number: warehouse.Number,
  11841. LicenseNumber: warehouse.LicenseNumber,
  11842. Count: stock_number,
  11843. UserOrgId: orgID,
  11844. PatientId: patient_id,
  11845. SystemTime: record_time,
  11846. ConsumableType: 15,
  11847. IsSys: 5,
  11848. WarehousingOrder: warehouse.WarehousingOrder,
  11849. WarehouseOutId: warehouseOut.ID,
  11850. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  11851. IsEdit: 1,
  11852. CancelStockId: 0,
  11853. CancelOrderNumber: "",
  11854. Manufacturer: warehouse.Manufacturer,
  11855. Dealer: warehouse.Dealer,
  11856. Creator: creator,
  11857. UpdateCreator: 0,
  11858. Status: 1,
  11859. Ctime: time.Now().Unix(),
  11860. Mtime: 0,
  11861. Price: warehouse.PackingPrice, //零售价
  11862. WarehousingDetailId: warehouse.ID,
  11863. WarehouseOutDetailId: goods.ID,
  11864. CancelOutDetailId: 0,
  11865. ProductDate: warehouse.ProductDate,
  11866. ExpireDate: warehouse.ExpiryDate,
  11867. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  11868. StorehouseId: warehouse.StorehouseId,
  11869. AdminUserId: creator,
  11870. BuyPrice: warehouse.Price, //进货价
  11871. StockCount: "",
  11872. BatchNumberCount: 0,
  11873. IsCheck: 1,
  11874. OverCount: sum_count,
  11875. RegisterNumber: warehouse.RegisterNumber,
  11876. OrderId: order_id,
  11877. ProjectId: goods.ProjectId,
  11878. }
  11879. //创建出库流水
  11880. errflow := CreateStockFlowOne(stockFlow)
  11881. if errflow != nil {
  11882. goodErrcode := models.XtGoodErrcode{
  11883. UserOrgId: orgID,
  11884. Errcode: "创建出库流水报错",
  11885. GoodId: goods.GoodId,
  11886. Status: 1,
  11887. Ctime: time.Now().Unix(),
  11888. Mtime: 0,
  11889. Count: 0,
  11890. StockCount: 0,
  11891. Creater: creator,
  11892. BatchNumberId: warehouse.ID,
  11893. WarehouseOutId: 0,
  11894. }
  11895. CreateGoodErrcode(goodErrcode)
  11896. }
  11897. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count)
  11898. if errWarehouse != nil {
  11899. goodErrcode := models.XtGoodErrcode{
  11900. UserOrgId: orgID,
  11901. Errcode: "添加入库ID报错",
  11902. GoodId: goods.GoodId,
  11903. Status: 1,
  11904. Ctime: time.Now().Unix(),
  11905. Mtime: 0,
  11906. Count: 0,
  11907. StockCount: 0,
  11908. Creater: creator,
  11909. BatchNumberId: warehouse.ID,
  11910. WarehouseOutId: 0,
  11911. }
  11912. CreateGoodErrcode(goodErrcode)
  11913. }
  11914. //更新出库数量
  11915. errcodecout := ModifyGoodSumCount(goods.StorehouseId, stock_number, orgID, goods.GoodId)
  11916. if errcodecout != nil {
  11917. goodErrcode := models.XtGoodErrcode{
  11918. UserOrgId: orgID,
  11919. Errcode: "更新出库数量报错",
  11920. GoodId: goods.GoodId,
  11921. Status: 1,
  11922. Ctime: time.Now().Unix(),
  11923. Mtime: 0,
  11924. Count: 0,
  11925. StockCount: 0,
  11926. Creater: creator,
  11927. BatchNumberId: warehouse.ID,
  11928. WarehouseOutId: 0,
  11929. }
  11930. CreateGoodErrcode(goodErrcode)
  11931. }
  11932. }
  11933. goods.Count = deliver_number - stock_number
  11934. prepare := &models.DialysisBeforePrepare{
  11935. Count: deliver_number - stock_number,
  11936. GoodTypeId: goods.GoodTypeId,
  11937. GoodId: goods.GoodId,
  11938. PatientId: goods.PatientId,
  11939. RecordDate: goods.RecordDate,
  11940. UserOrgId: orgID,
  11941. Ctime: time.Now().Unix(),
  11942. Creater: goods.Creater,
  11943. CommdityCode: goods.CommdityCode,
  11944. Status: 1,
  11945. StorehouseId: goods.StorehouseId,
  11946. }
  11947. ConsumableSettleDelivery(orgID, patient_id, record_time, prepare, warehouseOut, creator, order_id)
  11948. return nil
  11949. }
  11950. // 保存处方出库
  11951. func ConsumablePrescriptionDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, creator int64, count int64) (err error) {
  11952. var deliver_number int64 = 0
  11953. var stock_number int64 = 0
  11954. var maxNumber int64 = 0
  11955. deliver_number = goods.Count
  11956. // 根据先进先出原则,查询最先入库的批次,进行出库
  11957. // 如果没有对应的库存,则报错
  11958. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  11959. if err != nil {
  11960. goodErrcode := models.XtGoodErrcode{
  11961. UserOrgId: goods.UserOrgId,
  11962. Errcode: "查询库存信息报错",
  11963. GoodId: goods.GoodId,
  11964. Status: 1,
  11965. Ctime: time.Now().Unix(),
  11966. Mtime: 0,
  11967. Count: 0,
  11968. StockCount: 0,
  11969. Creater: creator,
  11970. BatchNumberId: warehouse.ID,
  11971. WarehouseOutId: 0,
  11972. }
  11973. CreateGoodErrcode(goodErrcode)
  11974. return err
  11975. }
  11976. stock_number = warehouse.StockCount
  11977. fmt.Println("sotck_number233223322o", stock_number)
  11978. fmt.Println("deliver_number", deliver_number)
  11979. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  11980. if stock_number >= deliver_number {
  11981. maxNumber = goods.Count
  11982. warehouse.StockCount = warehouse.StockCount - maxNumber
  11983. warehouse.Mtime = time.Now().Unix()
  11984. //扣减库存
  11985. errThree := UpDateWarehouseInfoByStock(&warehouse)
  11986. if errThree != nil {
  11987. goodErrcode := models.XtGoodErrcode{
  11988. UserOrgId: goods.UserOrgId,
  11989. Errcode: "扣减库存失败",
  11990. GoodId: goods.GoodId,
  11991. Status: 1,
  11992. Ctime: time.Now().Unix(),
  11993. Mtime: 0,
  11994. Count: 0,
  11995. StockCount: 0,
  11996. Creater: creator,
  11997. BatchNumberId: warehouse.ID,
  11998. WarehouseOutId: 0,
  11999. }
  12000. CreateGoodErrcode(goodErrcode)
  12001. return errThree
  12002. }
  12003. //查询剩余库存
  12004. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  12005. var sum_count int64
  12006. for _, item := range goodList {
  12007. sum_count += item.StockCount
  12008. }
  12009. //查询已经出库的数据
  12010. flowGood, _ := GetStockFlowIsBatchNumberSix(patient_id, record_time, goods.GoodId, goods.ProjectId)
  12011. var out_count int64
  12012. var out_count_one int64
  12013. for _, item := range flowGood {
  12014. out_count += item.Count
  12015. }
  12016. //查询退库数据
  12017. flowGoodTwo, _ := GetStockFlowIsBatchNumberFour(patient_id, record_time, goods.GoodId, goods.ProjectId)
  12018. for _, item := range flowGoodTwo {
  12019. out_count_one += item.Count
  12020. }
  12021. //flowGood, _ := GetStockFlowIsBatchNumberSeventy(patient_id, record_time, goods.GoodId, goods.ProjectId)
  12022. //
  12023. //var out_count int64
  12024. //var out_count_one int64
  12025. var out_count_two int64
  12026. //
  12027. //for _, item := range flowGood {
  12028. // out_count += item.Count
  12029. //}
  12030. //查询退库数据
  12031. //flowGoodTwo, _ := GetStockFlowIsBatchNumberFourty(patient_id, record_time, goods.GoodId, goods.ProjectId)
  12032. //for _, item := range flowGoodTwo {
  12033. // out_count_one += item.Count
  12034. //}
  12035. wareOutList, _ := GetStockFlowIsBatchNumberSixty(patient_id, record_time, goods.GoodId, goods.ProjectId)
  12036. for _, item := range wareOutList {
  12037. out_count_two += item.Count
  12038. }
  12039. OutInfo := &models.WarehouseOutInfo{
  12040. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  12041. WarehouseOutId: warehouseOut.ID,
  12042. WarehouseInfotId: warehouse.ID,
  12043. Status: 1,
  12044. Ctime: time.Now().Unix(),
  12045. Remark: warehouse.Remark,
  12046. OrgId: orgID,
  12047. Type: 1,
  12048. Manufacturer: warehouse.Manufacturer,
  12049. Dealer: warehouse.Dealer,
  12050. IsSys: 1,
  12051. SysRecordTime: record_time,
  12052. GoodTypeId: goods.GoodTypeId,
  12053. GoodId: goods.GoodId,
  12054. PatientId: patient_id,
  12055. Number: warehouse.Number,
  12056. LicenseNumber: warehouse.LicenseNumber,
  12057. Price: warehouse.PackingPrice,
  12058. ExpiryDate: warehouse.ExpiryDate,
  12059. ProductDate: warehouse.ProductDate,
  12060. ProjectId: goods.ProjectId,
  12061. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  12062. StorehouseId: goods.StorehouseId,
  12063. IsCheck: 1,
  12064. OverCount: sum_count,
  12065. RegisterNumber: warehouse.RegisterNumber,
  12066. }
  12067. OutInfo.Count = count - out_count_two + out_count_one
  12068. _, errcodes := GetNewWarehouseOutInfoIsExistSix(goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID)
  12069. if errcodes == gorm.ErrRecordNotFound {
  12070. errOne := AddSigleWarehouseOutInfo(OutInfo)
  12071. if errOne != nil {
  12072. return errOne
  12073. }
  12074. } else if errcodes == nil {
  12075. outInfoOne, _ := GetWarehouseOutInfoIsExistThree(goods.GoodId, patient_id, record_time, goods.ProjectId)
  12076. if count != outInfoOne.Count {
  12077. UpdatedWarehouseOutInfo(OutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId)
  12078. }
  12079. }
  12080. lastOut, _ := FindWarehouseOutInfoByPatientIdTwo(patient_id, record_time, goods.GoodId, orgID, goods.ProjectId)
  12081. //如果本次出库数据大于历史出库数据 新增1条流水
  12082. fmt.Println("本次出库数据", count)
  12083. fmt.Println("历史出库数据", out_count)
  12084. fmt.Println("退库数据", out_count_one)
  12085. if count > (out_count - out_count_one) {
  12086. stockFlow := models.VmStockFlow{
  12087. WarehousingId: warehouse.ID,
  12088. GoodId: goods.GoodId,
  12089. Number: warehouse.Number,
  12090. LicenseNumber: warehouse.LicenseNumber,
  12091. Count: count - out_count + out_count_one,
  12092. UserOrgId: orgID,
  12093. PatientId: goods.PatientId,
  12094. SystemTime: record_time,
  12095. ConsumableType: 3,
  12096. IsSys: 1,
  12097. WarehousingOrder: warehouse.WarehousingOrder,
  12098. WarehouseOutId: warehouseOut.ID,
  12099. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  12100. IsEdit: 1,
  12101. CancelStockId: 0,
  12102. CancelOrderNumber: "",
  12103. Manufacturer: warehouse.Manufacturer,
  12104. Dealer: warehouse.Dealer,
  12105. Creator: creator,
  12106. UpdateCreator: 0,
  12107. Status: 1,
  12108. Ctime: time.Now().Unix(),
  12109. Mtime: 0,
  12110. Price: warehouse.PackingPrice, //零售价
  12111. WarehousingDetailId: warehouse.ID,
  12112. WarehouseOutDetailId: goods.ID,
  12113. CancelOutDetailId: 0,
  12114. ProductDate: warehouse.ProductDate,
  12115. ExpireDate: warehouse.ExpiryDate,
  12116. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  12117. StorehouseId: warehouse.StorehouseId,
  12118. AdminUserId: creator,
  12119. BuyPrice: warehouse.Price, //进货价
  12120. StockCount: "",
  12121. BatchNumberCount: warehouse.StockCount - goods.Count,
  12122. IsCheck: 1,
  12123. OverCount: sum_count,
  12124. RegisterNumber: warehouse.RegisterNumber,
  12125. ProjectId: goods.ProjectId,
  12126. }
  12127. //创建出库流水
  12128. errflow := CreateStockFlowOne(stockFlow)
  12129. if errflow != nil {
  12130. goodErrcode := models.XtGoodErrcode{
  12131. UserOrgId: orgID,
  12132. Errcode: "创建流水报错",
  12133. GoodId: goods.GoodId,
  12134. Status: 1,
  12135. Ctime: time.Now().Unix(),
  12136. Mtime: 0,
  12137. Count: 0,
  12138. StockCount: 0,
  12139. Creater: creator,
  12140. BatchNumberId: warehouse.ID,
  12141. WarehouseOutId: 0,
  12142. }
  12143. CreateGoodErrcode(goodErrcode)
  12144. }
  12145. fmt.Println("stockFlow.Count233232323232322323232332wo0-----------------------", stockFlow.Count)
  12146. //更新出库数量
  12147. errsumcode := ModifyGoodSumCount(goods.StorehouseId, stockFlow.Count, orgID, goods.GoodId)
  12148. if errsumcode != nil {
  12149. goodErrcode := models.XtGoodErrcode{
  12150. UserOrgId: orgID,
  12151. Errcode: "更新出库数量报错",
  12152. GoodId: goods.GoodId,
  12153. Status: 1,
  12154. Ctime: time.Now().Unix(),
  12155. Mtime: 0,
  12156. Count: 0,
  12157. StockCount: 0,
  12158. Creater: creator,
  12159. BatchNumberId: warehouse.ID,
  12160. WarehouseOutId: 0,
  12161. }
  12162. CreateGoodErrcode(goodErrcode)
  12163. }
  12164. }
  12165. //如果本次出库数据小于历史出库数据 新增1条退库流水
  12166. if count < (out_count - out_count_one) {
  12167. operation_time := time.Now().Unix()
  12168. //创建退库单
  12169. timeStr := time.Now().Format("2006-01-02")
  12170. timeArr := strings.Split(timeStr, "-")
  12171. total, _ := FindAllCancelStockTotal(orgID)
  12172. total = total + 1
  12173. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  12174. cancelStock := models.CancelStock{
  12175. OrderNumber: orderNumber,
  12176. OperaTime: operation_time,
  12177. OrgId: orgID,
  12178. Creater: warehouseOut.Creater,
  12179. Ctime: time.Now().Unix(),
  12180. Status: 1,
  12181. ReturnTime: record_time,
  12182. Type: 1,
  12183. StorehouseId: goods.StorehouseId,
  12184. IsCheck: 1,
  12185. }
  12186. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, orgID)
  12187. if msgerrkonde == gorm.ErrRecordNotFound {
  12188. AddSigleCancelStock(&cancelStock)
  12189. }
  12190. cancel, _ := GetLastCancelStockById(orgID)
  12191. manufacturer, _ := GetManufactureById(warehouse.Manufacturer)
  12192. deaerler, _ := GetDealerById(warehouse.Dealer)
  12193. cancelStockInfo := models.CancelStockInfo{
  12194. GoodId: goods.GoodId,
  12195. CancelStockId: cancel.ID,
  12196. GoodTypeId: goods.GoodTypeId,
  12197. Count: out_count - out_count_one - count,
  12198. Price: warehouse.PackingPrice,
  12199. Total: 0,
  12200. ProductDate: warehouse.ProductDate,
  12201. ExpiryDate: warehouse.ExpiryDate,
  12202. Ctime: time.Now().Unix(),
  12203. Status: 1,
  12204. OrgId: orgID,
  12205. OrderNumber: cancel.OrderNumber,
  12206. Type: 0,
  12207. Dealer: deaerler.DealerName,
  12208. Manufacturer: manufacturer.ManufacturerName,
  12209. Number: warehouse.Number,
  12210. RegisterAccount: "",
  12211. Remark: "",
  12212. WarehouseInfoId: warehouse.ID,
  12213. PatientId: patient_id,
  12214. RecordDate: record_time,
  12215. StorehouseId: goods.StorehouseId,
  12216. IsCheck: 1,
  12217. }
  12218. if orgID == 9671 || orgID == 10265 {
  12219. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  12220. cancelStockInfo.Price = goodsInfo.PackingPrice
  12221. }
  12222. CreateCancelStockInfoOne(&cancelStockInfo)
  12223. cancelInfo, _ := GetLastCancelStockInfoByGoodId(goods.GoodId)
  12224. flow := models.VmStockFlow{
  12225. WarehousingId: warehouse.ID,
  12226. GoodId: goods.GoodId,
  12227. Number: warehouse.Number,
  12228. LicenseNumber: warehouse.LicenseNumber,
  12229. Count: out_count - out_count_one - count,
  12230. UserOrgId: orgID,
  12231. PatientId: patient_id,
  12232. SystemTime: record_time,
  12233. ConsumableType: 7,
  12234. IsSys: 1,
  12235. WarehousingOrder: "",
  12236. WarehouseOutId: lastOut.WarehouseOutId,
  12237. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  12238. IsEdit: 0,
  12239. CancelStockId: cancel.ID,
  12240. CancelOrderNumber: cancel.OrderNumber,
  12241. Manufacturer: manufacturer.ID,
  12242. Dealer: 0,
  12243. Creator: warehouseOut.Creater,
  12244. UpdateCreator: 0,
  12245. Status: 1,
  12246. Ctime: time.Now().Unix(),
  12247. Mtime: 0,
  12248. Price: warehouse.PackingPrice,
  12249. WarehousingDetailId: warehouse.ID,
  12250. WarehouseOutDetailId: lastOut.ID,
  12251. CancelOutDetailId: cancelInfo.ID,
  12252. ProductDate: warehouse.ProductDate,
  12253. ExpireDate: warehouse.ExpiryDate,
  12254. StorehouseId: goods.StorehouseId,
  12255. BuyPrice: warehouse.Price,
  12256. ProjectId: goods.ProjectId,
  12257. OverCount: sum_count,
  12258. RegisterNumber: warehouse.RegisterNumber,
  12259. }
  12260. if orgID == 9671 || orgID == 10265 {
  12261. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  12262. flow.Price = goodsInfo.PackingPrice
  12263. flow.BuyPrice = goodsInfo.BuyPrice
  12264. }
  12265. CreateStockFlowOne(flow)
  12266. //退库数量增加
  12267. UpdateSumAddCancelCount(orgID, goods.GoodId, goods.StorehouseId, flow.Count)
  12268. ModifyAddGoodSumCount(goods.StorehouseId, flow.Count, orgID, goods.GoodId)
  12269. }
  12270. //添加入库ID
  12271. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count)
  12272. if errWarehouse != nil {
  12273. goodErrcode := models.XtGoodErrcode{
  12274. UserOrgId: orgID,
  12275. Errcode: "添加入库ID报错",
  12276. GoodId: goods.GoodId,
  12277. Status: 1,
  12278. Ctime: time.Now().Unix(),
  12279. Mtime: 0,
  12280. Count: 0,
  12281. StockCount: 0,
  12282. Creater: creator,
  12283. BatchNumberId: warehouse.ID,
  12284. WarehouseOutId: 0,
  12285. }
  12286. CreateGoodErrcode(goodErrcode)
  12287. }
  12288. if warehouse.StockCount < maxNumber {
  12289. goodErrcode := models.XtGoodErrcode{
  12290. UserOrgId: orgID,
  12291. Errcode: "出库接口库存数量不足",
  12292. GoodId: goods.GoodId,
  12293. Status: 1,
  12294. Ctime: time.Now().Unix(),
  12295. Mtime: 0,
  12296. Count: 0,
  12297. StockCount: 0,
  12298. Creater: creator,
  12299. BatchNumberId: warehouse.ID,
  12300. WarehouseOutId: 0,
  12301. }
  12302. CreateGoodErrcode(goodErrcode)
  12303. return errors.New("库存数量不足")
  12304. }
  12305. return nil
  12306. } else {
  12307. // 出库完成后,要将该批次库存清零
  12308. warehouse.StockCount = 0
  12309. warehouse.Mtime = time.Now().Unix()
  12310. errThree := UpDateWarehouseInfoByStock(&warehouse)
  12311. if errThree != nil {
  12312. goodErrcode := models.XtGoodErrcode{
  12313. UserOrgId: orgID,
  12314. Errcode: "扣减库存报错",
  12315. GoodId: goods.GoodId,
  12316. Status: 1,
  12317. Ctime: time.Now().Unix(),
  12318. Mtime: 0,
  12319. Count: 0,
  12320. StockCount: 0,
  12321. Creater: creator,
  12322. BatchNumberId: warehouse.ID,
  12323. WarehouseOutId: 0,
  12324. }
  12325. CreateGoodErrcode(goodErrcode)
  12326. return errThree
  12327. }
  12328. //查询剩余库存
  12329. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  12330. var sum_count int64
  12331. for _, item := range goodList {
  12332. sum_count += item.StockCount
  12333. }
  12334. fmt.Println("剩余库存", sum_count)
  12335. OutInfo := &models.WarehouseOutInfo{
  12336. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  12337. WarehouseOutId: warehouseOut.ID,
  12338. WarehouseInfotId: warehouse.ID,
  12339. Status: 1,
  12340. Ctime: time.Now().Unix(),
  12341. Remark: warehouse.Remark,
  12342. OrgId: orgID,
  12343. Type: 1,
  12344. Manufacturer: warehouse.Manufacturer,
  12345. Dealer: warehouse.Dealer,
  12346. IsSys: 1,
  12347. SysRecordTime: record_time,
  12348. GoodTypeId: goods.GoodTypeId,
  12349. GoodId: goods.GoodId,
  12350. PatientId: patient_id,
  12351. Number: warehouse.Number,
  12352. LicenseNumber: warehouse.LicenseNumber,
  12353. Price: warehouse.PackingPrice,
  12354. ExpiryDate: warehouse.ExpiryDate,
  12355. ProductDate: warehouse.ProductDate,
  12356. ProjectId: goods.ProjectId,
  12357. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  12358. StorehouseId: goods.StorehouseId,
  12359. IsCheck: 1,
  12360. OverCount: sum_count,
  12361. RegisterNumber: warehouse.RegisterNumber,
  12362. }
  12363. OutInfo.Count = stock_number
  12364. _, errcodes := GetNewWarehouseOutInfoIsExistSix(goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID)
  12365. if errcodes == gorm.ErrRecordNotFound {
  12366. errOne := AddSigleWarehouseOutInfo(OutInfo)
  12367. if errOne != nil {
  12368. return errOne
  12369. }
  12370. } else if errcodes == nil {
  12371. outInfoOne, _ := GetWarehouseOutInfoIsExistTen(goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID)
  12372. if count != outInfoOne.Count {
  12373. UpdatedWarehouseOutInfoSix(OutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID)
  12374. }
  12375. }
  12376. lastOut, _ := FindWarehouseOutInfoByPatientIdTwo(patient_id, record_time, goods.GoodId, orgID, goods.ProjectId)
  12377. //查询该该批次已经出库的数据
  12378. //flowGood, _ := GetStockFlowIsBatchNumber(warehouse.ID, patient_id, record_time, goods.GoodId)
  12379. //查询已经出库的数据
  12380. //flowGood, _ := GetStockFlowIsBatchNumberSeventy(patient_id, record_time, goods.GoodId, goods.ProjectId)
  12381. //var out_count int64
  12382. //var out_count_one int64
  12383. //for _, item := range flowGood {
  12384. // out_count += item.Count
  12385. //}
  12386. //flowGoodTwo, _ := GetStockFlowIsBatchNumberSix(patient_id, record_time, goods.GoodId, goods.ProjectId)
  12387. //for _, item := range flowGoodTwo {
  12388. // out_count_one += item.Count
  12389. //}
  12390. //查询该该批次已经出库的数据
  12391. flowGood, _ := GetStockFlowIsBatchNumberSix(patient_id, record_time, goods.GoodId, goods.ProjectId)
  12392. var out_count int64
  12393. var out_count_one int64
  12394. for _, item := range flowGood {
  12395. out_count += item.Count
  12396. }
  12397. flowGoodTwo, _ := GetStockFlowIsBatchNumberFour(patient_id, record_time, goods.GoodId, goods.ProjectId)
  12398. for _, item := range flowGoodTwo {
  12399. out_count_one += item.Count
  12400. }
  12401. fmt.Println("count----------------", count)
  12402. fmt.Println("out_count----------", out_count)
  12403. fmt.Println("out_count_one--------", out_count_one)
  12404. //如果出库数量 大于 历史出库数据 新增1条流水
  12405. if count > out_count-out_count_one {
  12406. stockFlow := models.VmStockFlow{
  12407. WarehousingId: warehouse.ID,
  12408. GoodId: goods.GoodId,
  12409. Number: warehouse.Number,
  12410. LicenseNumber: warehouse.LicenseNumber,
  12411. Count: stock_number,
  12412. UserOrgId: orgID,
  12413. PatientId: goods.PatientId,
  12414. SystemTime: record_time,
  12415. ConsumableType: 3,
  12416. IsSys: 1,
  12417. WarehousingOrder: warehouse.WarehousingOrder,
  12418. WarehouseOutId: warehouseOut.ID,
  12419. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  12420. IsEdit: 1,
  12421. CancelStockId: 0,
  12422. CancelOrderNumber: "",
  12423. Manufacturer: warehouse.Manufacturer,
  12424. Dealer: warehouse.Dealer,
  12425. Creator: creator,
  12426. UpdateCreator: 0,
  12427. Status: 1,
  12428. Ctime: time.Now().Unix(),
  12429. Mtime: 0,
  12430. Price: warehouse.PackingPrice, //零售价
  12431. WarehousingDetailId: warehouse.ID,
  12432. WarehouseOutDetailId: goods.ID,
  12433. CancelOutDetailId: 0,
  12434. ProductDate: warehouse.ProductDate,
  12435. ExpireDate: warehouse.ExpiryDate,
  12436. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  12437. StorehouseId: warehouse.StorehouseId,
  12438. AdminUserId: creator,
  12439. BuyPrice: warehouse.Price, //进货价
  12440. StockCount: "",
  12441. BatchNumberCount: 0,
  12442. IsCheck: 1,
  12443. OverCount: sum_count,
  12444. RegisterNumber: warehouse.RegisterNumber,
  12445. ProjectId: goods.ProjectId,
  12446. }
  12447. //创建出库流水
  12448. errflow := CreateStockFlowOne(stockFlow)
  12449. if errflow != nil {
  12450. goodErrcode := models.XtGoodErrcode{
  12451. UserOrgId: orgID,
  12452. Errcode: "创建出库流水报错",
  12453. GoodId: goods.GoodId,
  12454. Status: 1,
  12455. Ctime: time.Now().Unix(),
  12456. Mtime: 0,
  12457. Count: 0,
  12458. StockCount: 0,
  12459. Creater: creator,
  12460. BatchNumberId: warehouse.ID,
  12461. WarehouseOutId: 0,
  12462. }
  12463. CreateGoodErrcode(goodErrcode)
  12464. }
  12465. //更新出库数量
  12466. errcodecout := ModifyGoodSumCount(goods.StorehouseId, stock_number, orgID, goods.GoodId)
  12467. if errcodecout != nil {
  12468. goodErrcode := models.XtGoodErrcode{
  12469. UserOrgId: orgID,
  12470. Errcode: "更新出库数量报错",
  12471. GoodId: goods.GoodId,
  12472. Status: 1,
  12473. Ctime: time.Now().Unix(),
  12474. Mtime: 0,
  12475. Count: 0,
  12476. StockCount: 0,
  12477. Creater: creator,
  12478. BatchNumberId: warehouse.ID,
  12479. WarehouseOutId: 0,
  12480. }
  12481. CreateGoodErrcode(goodErrcode)
  12482. }
  12483. }
  12484. //退库
  12485. if count < out_count-out_count_one {
  12486. operation_time := time.Now().Unix()
  12487. //创建退库单
  12488. timeStr := time.Now().Format("2006-01-02")
  12489. timeArr := strings.Split(timeStr, "-")
  12490. total, _ := FindAllCancelStockTotal(orgID)
  12491. total = total + 1
  12492. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  12493. cancelStock := models.CancelStock{
  12494. OrderNumber: orderNumber,
  12495. OperaTime: operation_time,
  12496. OrgId: orgID,
  12497. Creater: warehouseOut.Creater,
  12498. Ctime: time.Now().Unix(),
  12499. Status: 1,
  12500. ReturnTime: record_time,
  12501. Type: 1,
  12502. StorehouseId: goods.StorehouseId,
  12503. IsCheck: 1,
  12504. }
  12505. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, orgID)
  12506. if msgerrkonde == gorm.ErrRecordNotFound {
  12507. AddSigleCancelStock(&cancelStock)
  12508. }
  12509. cancel, _ := GetLastCancelStockById(orgID)
  12510. manufacturer, _ := GetManufactureById(warehouse.Manufacturer)
  12511. deaerler, _ := GetDealerById(warehouse.Dealer)
  12512. cancelStockInfo := models.CancelStockInfo{
  12513. GoodId: goods.GoodId,
  12514. CancelStockId: cancel.ID,
  12515. GoodTypeId: goods.GoodTypeId,
  12516. Count: out_count - out_count_one - count,
  12517. Price: warehouse.PackingPrice,
  12518. Total: 0,
  12519. ProductDate: warehouse.ProductDate,
  12520. ExpiryDate: warehouse.ExpiryDate,
  12521. Ctime: time.Now().Unix(),
  12522. Status: 1,
  12523. OrgId: orgID,
  12524. OrderNumber: cancel.OrderNumber,
  12525. Type: 0,
  12526. Dealer: deaerler.DealerName,
  12527. Manufacturer: manufacturer.ManufacturerName,
  12528. Number: warehouse.Number,
  12529. RegisterAccount: "",
  12530. Remark: "",
  12531. WarehouseInfoId: warehouse.ID,
  12532. PatientId: patient_id,
  12533. RecordDate: record_time,
  12534. StorehouseId: goods.StorehouseId,
  12535. IsCheck: 1,
  12536. }
  12537. if orgID == 9671 || orgID == 10265 {
  12538. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  12539. cancelStockInfo.Price = goodsInfo.PackingPrice
  12540. }
  12541. CreateCancelStockInfoOne(&cancelStockInfo)
  12542. cancelInfo, _ := GetLastCancelStockInfoByGoodId(goods.GoodId)
  12543. flow := models.VmStockFlow{
  12544. WarehousingId: warehouse.ID,
  12545. GoodId: goods.GoodId,
  12546. Number: warehouse.Number,
  12547. LicenseNumber: warehouse.LicenseNumber,
  12548. Count: out_count - count,
  12549. UserOrgId: orgID,
  12550. PatientId: patient_id,
  12551. SystemTime: record_time,
  12552. ConsumableType: 7,
  12553. IsSys: 1,
  12554. WarehousingOrder: "",
  12555. WarehouseOutId: lastOut.WarehouseOutId,
  12556. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  12557. IsEdit: 0,
  12558. CancelStockId: cancel.ID,
  12559. CancelOrderNumber: cancel.OrderNumber,
  12560. Manufacturer: manufacturer.ID,
  12561. Dealer: 0,
  12562. Creator: warehouseOut.Creater,
  12563. UpdateCreator: 0,
  12564. Status: 1,
  12565. Ctime: time.Now().Unix(),
  12566. Mtime: 0,
  12567. Price: warehouse.Price,
  12568. WarehousingDetailId: warehouse.ID,
  12569. WarehouseOutDetailId: lastOut.ID,
  12570. CancelOutDetailId: cancelInfo.ID,
  12571. ProductDate: warehouse.ProductDate,
  12572. ExpireDate: warehouse.ExpiryDate,
  12573. StorehouseId: goods.StorehouseId,
  12574. BuyPrice: warehouse.Price,
  12575. ProjectId: goods.ProjectId,
  12576. OverCount: sum_count,
  12577. RegisterNumber: warehouse.RegisterNumber,
  12578. }
  12579. if orgID == 9671 || orgID == 10265 {
  12580. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  12581. flow.Price = goodsInfo.PackingPrice
  12582. flow.BuyPrice = goodsInfo.BuyPrice
  12583. }
  12584. CreateStockFlowOne(flow)
  12585. //退库数量增加
  12586. UpdateSumAddCancelCount(orgID, goods.GoodId, goods.StorehouseId, flow.Count)
  12587. //实际出库减少
  12588. ModifyAddGoodSumCount(goods.StorehouseId, flow.Count, orgID, goods.GoodId)
  12589. }
  12590. errWarehouse := UpdateGoodWarehouseOutById(goods.ID, warehouse.ID, orgID, sum_count)
  12591. if errWarehouse != nil {
  12592. goodErrcode := models.XtGoodErrcode{
  12593. UserOrgId: orgID,
  12594. Errcode: "添加入库ID报错",
  12595. GoodId: goods.GoodId,
  12596. Status: 1,
  12597. Ctime: time.Now().Unix(),
  12598. Mtime: 0,
  12599. Count: 0,
  12600. StockCount: 0,
  12601. Creater: creator,
  12602. BatchNumberId: warehouse.ID,
  12603. WarehouseOutId: 0,
  12604. }
  12605. CreateGoodErrcode(goodErrcode)
  12606. }
  12607. }
  12608. goods.Count = deliver_number - stock_number
  12609. prepare := &models.DialysisBeforePrepare{
  12610. Count: deliver_number - stock_number,
  12611. GoodTypeId: goods.GoodTypeId,
  12612. GoodId: goods.GoodId,
  12613. PatientId: goods.PatientId,
  12614. RecordDate: goods.RecordDate,
  12615. UserOrgId: orgID,
  12616. Ctime: time.Now().Unix(),
  12617. Creater: creator,
  12618. Status: 1,
  12619. StorehouseId: goods.StorehouseId,
  12620. ProjectId: goods.ProjectId,
  12621. }
  12622. fmt.Println("剩余出库数量", prepare.Count)
  12623. ConsumablePrescriptionDelivery(orgID, patient_id, record_time, prepare, warehouseOut, creator, count)
  12624. return nil
  12625. }
  12626. func CreateAutoDetail(detail models.AutomaticReduceDetail) error {
  12627. err := XTWriteDB().Create(&detail).Error
  12628. return err
  12629. }
  12630. func UpdateAutomaticReduce(patient_id int64, record_date int64, good_id int64, project_id int64) error {
  12631. 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
  12632. return err
  12633. }