new_warehouse_service.go 214KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "math"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "XT_New/models"
  10. "XT_New/utils"
  11. "github.com/jinzhu/gorm"
  12. )
  13. // 耗材出库
  14. func ConsumablesGoodDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, count int64) (err error) {
  15. //开事务
  16. var deliver_number int64 = 0
  17. var stock_number int64 = 0
  18. var maxNumber int64 = 0
  19. deliver_number = goods.Count
  20. // 根据先进先出原则,查询最先入库的批次,进行出库
  21. // 如果没有对应的库存,则报错
  22. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  23. if goods.Count > 0 {
  24. if err != nil {
  25. return errors.New("库存数量不足")
  26. }
  27. }
  28. stock_number = warehouse.StockCount
  29. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  30. if stock_number >= deliver_number {
  31. maxNumber = goods.Count
  32. //出库
  33. warehouse.StockCount = warehouse.StockCount - maxNumber
  34. warehouse.Mtime = time.Now().Unix()
  35. if warehouse.StockCount < 0 {
  36. return errors.New("库存数量不足")
  37. }
  38. errThree := UpDateWarehouseInfoByStock(&warehouse)
  39. if errThree != nil {
  40. return errThree
  41. }
  42. //if orgID == 10265 {
  43. // UpdateGoodInfoByPrice(warehouse.GoodId, orgID, warehouse.Price, warehouse.PackingPrice)
  44. //}
  45. //更新his_prescripton_project出库状态值
  46. UpdateHisPrescriptionProjectStatus(goods.GoodId, record_time, orgID, patient_id)
  47. //查询剩余库存
  48. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  49. var sum_count int64
  50. for _, item := range goodList {
  51. sum_count += item.StockCount
  52. }
  53. warehouseOutInfo := &models.WarehouseOutInfo{
  54. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  55. WarehouseOutId: warehouseOut.ID,
  56. WarehouseInfotId: warehouse.ID,
  57. Status: 1,
  58. Ctime: record_time,
  59. Remark: warehouse.Remark,
  60. OrgId: orgID,
  61. Type: 1,
  62. Manufacturer: warehouse.Manufacturer,
  63. Dealer: warehouse.Dealer,
  64. IsSys: 1,
  65. SysRecordTime: record_time,
  66. GoodTypeId: goods.GoodTypeId,
  67. GoodId: goods.GoodId,
  68. PatientId: patient_id,
  69. Number: warehouse.Number,
  70. LicenseNumber: warehouse.LicenseNumber,
  71. Price: warehouse.PackingPrice,
  72. ExpiryDate: warehouse.ExpiryDate,
  73. ProductDate: warehouse.ProductDate,
  74. ProjectId: goods.ProjectId,
  75. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  76. StorehouseId: goods.StorehouseId,
  77. IsCheck: 1,
  78. OverCount: sum_count,
  79. RegisterNumber: warehouse.RegisterNumber,
  80. BuyPrice: warehouse.PackingPrice,
  81. }
  82. warehouseOutInfo.Count = count
  83. if orgID == 9671 || orgID == 10265 {
  84. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  85. warehouseOutInfo.Price = goodsInfo.PackingPrice
  86. }
  87. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
  88. if errcodes == gorm.ErrRecordNotFound {
  89. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  90. if errOne != nil {
  91. return errOne
  92. }
  93. } else if errcodes == nil {
  94. outInfoOne, _ := GetWarehouseOutInfoIsExistTwo(goods.GoodId, patient_id, record_time, goods.ProjectId)
  95. if count != outInfoOne.Count {
  96. UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId)
  97. }
  98. }
  99. lastOut, _ := FindWarehouseOutInfoByPatientId(patient_id, record_time, goods.GoodId, orgID)
  100. //查询已经出库的数据
  101. flowGood, _ := GetStockFlowIsBatchNumber(warehouse.ID, patient_id, record_time, goods.GoodId)
  102. var out_count int64
  103. var out_count_one int64
  104. for _, item := range flowGood {
  105. out_count += item.Count
  106. }
  107. //查询退库数据
  108. flowGoodTwo, _ := GetStockFlowIsBatchNumberThree(patient_id, record_time, goods.GoodId)
  109. for _, item := range flowGoodTwo {
  110. out_count_one += item.Count
  111. }
  112. //如果本次出库数据大于历史出库数据 新增1条流水
  113. if count > (out_count - out_count_one) {
  114. flow := models.VmStockFlow{
  115. WarehouseOutId: warehouseOut.ID,
  116. WarehousingId: warehouse.ID,
  117. GoodId: goods.GoodId,
  118. Number: warehouse.Number,
  119. ProductDate: warehouse.ProductDate,
  120. ExpireDate: warehouse.ExpiryDate,
  121. Count: count - out_count + out_count_one,
  122. Price: warehouse.PackingPrice,
  123. Status: 1,
  124. Ctime: record_time,
  125. UserOrgId: orgID,
  126. Manufacturer: warehouse.Manufacturer,
  127. Dealer: warehouse.Dealer,
  128. LicenseNumber: warehouse.LicenseNumber,
  129. IsEdit: 2,
  130. Creator: warehouseOut.Creater,
  131. SystemTime: record_time,
  132. ConsumableType: 3,
  133. WarehouseOutDetailId: lastOut.ID,
  134. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  135. IsSys: 1,
  136. PatientId: patient_id,
  137. ProjectId: goods.ProjectId,
  138. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  139. StorehouseId: goods.StorehouseId,
  140. OverCount: sum_count,
  141. BuyPrice: warehouse.Price,
  142. RegisterNumber: warehouse.RegisterNumber,
  143. OperateTime: time.Now().Unix(),
  144. }
  145. if orgID == 9671 || orgID == 10265 {
  146. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  147. flow.Price = goodsInfo.PackingPrice
  148. flow.BuyPrice = goodsInfo.BuyPrice
  149. }
  150. errThre := CreateStockFlowOne(flow)
  151. if errThre != nil {
  152. return errThre
  153. }
  154. }
  155. //如果本次出库数据小于历史出库数据 新增1条退库流水
  156. if count < (out_count - out_count_one) {
  157. operation_time := time.Now().Unix()
  158. //创建退库单
  159. timeStr := time.Now().Format("2006-01-02")
  160. timeArr := strings.Split(timeStr, "-")
  161. total, _ := FindAllCancelStockTotal(orgID)
  162. total = total + 1
  163. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  164. cancelStock := models.CancelStock{
  165. OrderNumber: orderNumber,
  166. OperaTime: operation_time,
  167. OrgId: orgID,
  168. Creater: warehouseOut.Creater,
  169. Ctime: time.Now().Unix(),
  170. Status: 1,
  171. ReturnTime: record_time,
  172. Type: 1,
  173. StorehouseId: goods.StorehouseId,
  174. IsCheck: 1,
  175. }
  176. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, orgID)
  177. if msgerrkonde == gorm.ErrRecordNotFound {
  178. AddSigleCancelStock(&cancelStock)
  179. }
  180. cancel, _ := GetLastCancelStockById(orgID)
  181. manufacturer, _ := GetManufactureById(warehouse.Manufacturer)
  182. deaerler, _ := GetDealerById(warehouse.Dealer)
  183. cancelStockInfo := models.CancelStockInfo{
  184. GoodId: goods.GoodId,
  185. CancelStockId: cancel.ID,
  186. GoodTypeId: goods.GoodTypeId,
  187. Count: out_count - out_count_one - count,
  188. Price: warehouse.PackingPrice,
  189. Total: 0,
  190. ProductDate: warehouse.ProductDate,
  191. ExpiryDate: warehouse.ExpiryDate,
  192. Ctime: record_time,
  193. Status: 1,
  194. OrgId: orgID,
  195. OrderNumber: cancel.OrderNumber,
  196. Type: 0,
  197. Dealer: deaerler.DealerName,
  198. Manufacturer: manufacturer.ManufacturerName,
  199. Number: warehouse.Number,
  200. RegisterAccount: "",
  201. Remark: "",
  202. WarehouseInfoId: warehouse.ID,
  203. PatientId: patient_id,
  204. RecordDate: record_time,
  205. StorehouseId: goods.StorehouseId,
  206. IsCheck: 1,
  207. }
  208. if orgID == 9671 || orgID == 10265 {
  209. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  210. cancelStockInfo.Price = goodsInfo.PackingPrice
  211. }
  212. CreateCancelStockInfoOne(&cancelStockInfo)
  213. cancelInfo, _ := GetLastCancelStockInfoByGoodId(goods.GoodId)
  214. flow := models.VmStockFlow{
  215. WarehousingId: warehouse.ID,
  216. GoodId: goods.GoodId,
  217. Number: warehouse.Number,
  218. LicenseNumber: warehouse.LicenseNumber,
  219. Count: out_count - out_count_one - count,
  220. UserOrgId: orgID,
  221. PatientId: patient_id,
  222. SystemTime: record_time,
  223. ConsumableType: 7,
  224. IsSys: 1,
  225. WarehousingOrder: "",
  226. WarehouseOutId: lastOut.WarehouseOutId,
  227. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  228. IsEdit: 0,
  229. CancelStockId: cancel.ID,
  230. CancelOrderNumber: cancel.OrderNumber,
  231. Manufacturer: manufacturer.ID,
  232. Dealer: 0,
  233. Creator: warehouseOut.Creater,
  234. UpdateCreator: 0,
  235. Status: 1,
  236. Ctime: record_time,
  237. Mtime: 0,
  238. Price: warehouse.PackingPrice,
  239. WarehousingDetailId: warehouse.ID,
  240. WarehouseOutDetailId: lastOut.ID,
  241. CancelOutDetailId: cancelInfo.ID,
  242. ProductDate: warehouse.ProductDate,
  243. ExpireDate: warehouse.ExpiryDate,
  244. StorehouseId: goods.StorehouseId,
  245. BuyPrice: warehouse.Price,
  246. ProjectId: goods.ProjectId,
  247. OverCount: sum_count,
  248. RegisterNumber: warehouse.RegisterNumber,
  249. OperateTime: time.Now().Unix(),
  250. }
  251. if orgID == 9671 || orgID == 10265 {
  252. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  253. flow.Price = goodsInfo.PackingPrice
  254. flow.BuyPrice = goodsInfo.BuyPrice
  255. }
  256. CreateStockFlowOne(flow)
  257. }
  258. details := models.BloodAutomaticReduceDetail{
  259. WarehouseOutId: lastOut.ID,
  260. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  261. PatientId: patient_id,
  262. Ctime: time.Now().Unix(),
  263. Mtime: time.Now().Unix(),
  264. Status: 1,
  265. RecordTime: record_time,
  266. OrgId: orgID,
  267. GoodId: goods.GoodId,
  268. GoodTypeId: goods.GoodTypeId,
  269. Count: count,
  270. ProjectId: goods.ProjectId,
  271. StorehouseId: goods.StorehouseId,
  272. }
  273. ////查询该耗材已经出库的数量
  274. _, errcode := GetAutoMaticReduceDetailTwenty(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  275. if errcode == gorm.ErrRecordNotFound {
  276. errTwo := CreateAutoReduceRecord(&details)
  277. if errTwo != nil {
  278. return errTwo
  279. }
  280. } else if errcode == nil {
  281. DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  282. CreateAutoReduceRecord(&details)
  283. }
  284. return nil
  285. } else {
  286. // 当刚批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  287. warehouse.StockCount = 0
  288. warehouse.Mtime = time.Now().Unix()
  289. errThree := UpDateWarehouseInfoByStock(&warehouse)
  290. if errThree != nil {
  291. return errThree
  292. }
  293. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  294. var sum_count int64
  295. for _, item := range goodList {
  296. sum_count += item.StockCount
  297. }
  298. warehouseOutInfo := &models.WarehouseOutInfo{
  299. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  300. WarehouseOutId: warehouseOut.ID,
  301. WarehouseInfotId: warehouse.ID,
  302. Status: 1,
  303. Ctime: record_time,
  304. Remark: warehouse.Remark,
  305. OrgId: orgID,
  306. Type: 1,
  307. Manufacturer: warehouse.Manufacturer,
  308. Dealer: warehouse.Dealer,
  309. IsSys: 1,
  310. SysRecordTime: record_time,
  311. GoodTypeId: goods.GoodTypeId,
  312. GoodId: goods.GoodId,
  313. PatientId: patient_id,
  314. Number: warehouse.Number,
  315. LicenseNumber: warehouse.LicenseNumber,
  316. Price: warehouse.PackingPrice,
  317. ExpiryDate: warehouse.ExpiryDate,
  318. ProductDate: warehouse.ProductDate,
  319. ProjectId: goods.ProjectId,
  320. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  321. StorehouseId: goods.StorehouseId,
  322. IsCheck: 1,
  323. OverCount: sum_count,
  324. RegisterNumber: warehouse.RegisterNumber,
  325. }
  326. warehouseOutInfo.Count = stock_number
  327. if orgID == 9671 || orgID == 10265 {
  328. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  329. warehouseOutInfo.Price = goodsInfo.PackingPrice
  330. }
  331. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
  332. if errcodes == gorm.ErrRecordNotFound {
  333. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  334. if errOne != nil {
  335. return errOne
  336. }
  337. } else if errcodes == nil {
  338. goods.Count = deliver_number - stock_number
  339. //更新数量为 该批次剩余数量 + 还有未出的数量
  340. warehouseOutInfo.Count = stock_number
  341. outInfoOne, _ := GetWarehouseOutInfoIsExistTwo(goods.GoodId, patient_id, record_time, goods.ProjectId)
  342. if count != outInfoOne.Count {
  343. UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId)
  344. }
  345. }
  346. lastOut, _ := FindWarehouseOutInfoByPatientId(patient_id, record_time, goods.GoodId, orgID)
  347. //查询该该批次已经出库的数据
  348. flowGood, _ := GetStockFlowIsBatchNumber(warehouse.ID, patient_id, record_time, goods.GoodId)
  349. var out_count int64
  350. var out_count_one int64
  351. for _, item := range flowGood {
  352. out_count += item.Count
  353. }
  354. flowGoodTwo, _ := GetStockFlowIsBatchNumberThree(patient_id, record_time, goods.GoodId)
  355. for _, item := range flowGoodTwo {
  356. out_count_one += item.Count
  357. }
  358. //如果出库数量 大于 历史出库数据 新增1条流水
  359. if count > out_count-out_count_one {
  360. flow := models.VmStockFlow{
  361. WarehouseOutId: warehouseOut.ID,
  362. WarehousingId: warehouse.ID,
  363. GoodId: goods.GoodId,
  364. Number: warehouse.Number,
  365. ProductDate: warehouse.ProductDate,
  366. ExpireDate: warehouse.ExpiryDate,
  367. Count: stock_number,
  368. Price: warehouse.PackingPrice,
  369. Status: 1,
  370. Ctime: record_time,
  371. UserOrgId: orgID,
  372. Manufacturer: warehouse.Manufacturer,
  373. Dealer: warehouse.Dealer,
  374. LicenseNumber: warehouse.LicenseNumber,
  375. IsEdit: 2,
  376. Creator: warehouseOut.Creater,
  377. SystemTime: record_time,
  378. ConsumableType: 3,
  379. WarehouseOutDetailId: lastOut.ID,
  380. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  381. IsSys: 1,
  382. PatientId: patient_id,
  383. ProjectId: goods.ProjectId,
  384. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  385. StorehouseId: goods.StorehouseId,
  386. OverCount: sum_count,
  387. BuyPrice: warehouse.Price,
  388. RegisterNumber: warehouse.RegisterNumber,
  389. OperateTime: time.Now().Unix(),
  390. }
  391. if orgID == 9671 || orgID == 10265 {
  392. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  393. flow.Price = goodsInfo.PackingPrice
  394. flow.BuyPrice = goodsInfo.BuyPrice
  395. }
  396. errThre := CreateStockFlowOne(flow)
  397. if errThre != nil {
  398. return errThre
  399. }
  400. }
  401. //退库
  402. if count < out_count-out_count_one {
  403. operation_time := time.Now().Unix()
  404. //创建退库单
  405. timeStr := time.Now().Format("2006-01-02")
  406. timeArr := strings.Split(timeStr, "-")
  407. total, _ := FindAllCancelStockTotal(orgID)
  408. total = total + 1
  409. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  410. cancelStock := models.CancelStock{
  411. OrderNumber: orderNumber,
  412. OperaTime: operation_time,
  413. OrgId: orgID,
  414. Creater: warehouseOut.Creater,
  415. Ctime: time.Now().Unix(),
  416. Status: 1,
  417. ReturnTime: record_time,
  418. Type: 1,
  419. StorehouseId: goods.StorehouseId,
  420. IsCheck: 1,
  421. }
  422. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, orgID)
  423. if msgerrkonde == gorm.ErrRecordNotFound {
  424. AddSigleCancelStock(&cancelStock)
  425. }
  426. cancel, _ := GetLastCancelStockById(orgID)
  427. manufacturer, _ := GetManufactureById(warehouse.Manufacturer)
  428. deaerler, _ := GetDealerById(warehouse.Dealer)
  429. cancelStockInfo := models.CancelStockInfo{
  430. GoodId: goods.GoodId,
  431. CancelStockId: cancel.ID,
  432. GoodTypeId: goods.GoodTypeId,
  433. Count: out_count - out_count_one - count,
  434. Price: warehouse.PackingPrice,
  435. Total: 0,
  436. ProductDate: warehouse.ProductDate,
  437. ExpiryDate: warehouse.ExpiryDate,
  438. Ctime: record_time,
  439. Status: 1,
  440. OrgId: orgID,
  441. OrderNumber: cancel.OrderNumber,
  442. Type: 0,
  443. Dealer: deaerler.DealerName,
  444. Manufacturer: manufacturer.ManufacturerName,
  445. Number: warehouse.Number,
  446. RegisterAccount: "",
  447. Remark: "",
  448. WarehouseInfoId: warehouse.ID,
  449. PatientId: patient_id,
  450. RecordDate: record_time,
  451. StorehouseId: goods.StorehouseId,
  452. IsCheck: 1,
  453. }
  454. if orgID == 9671 || orgID == 10265 {
  455. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  456. cancelStockInfo.Price = goodsInfo.PackingPrice
  457. }
  458. CreateCancelStockInfoOne(&cancelStockInfo)
  459. cancelInfo, _ := GetLastCancelStockInfoByGoodId(goods.GoodId)
  460. flow := models.VmStockFlow{
  461. WarehousingId: warehouse.ID,
  462. GoodId: goods.GoodId,
  463. Number: warehouse.Number,
  464. LicenseNumber: warehouse.LicenseNumber,
  465. Count: out_count - count,
  466. UserOrgId: orgID,
  467. PatientId: patient_id,
  468. SystemTime: record_time,
  469. ConsumableType: 7,
  470. IsSys: 1,
  471. WarehousingOrder: "",
  472. WarehouseOutId: lastOut.WarehouseOutId,
  473. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  474. IsEdit: 0,
  475. CancelStockId: cancel.ID,
  476. CancelOrderNumber: cancel.OrderNumber,
  477. Manufacturer: manufacturer.ID,
  478. Dealer: 0,
  479. Creator: warehouseOut.Creater,
  480. UpdateCreator: 0,
  481. Status: 1,
  482. Ctime: record_time,
  483. Mtime: 0,
  484. Price: warehouse.PackingPrice,
  485. WarehousingDetailId: warehouse.ID,
  486. WarehouseOutDetailId: lastOut.ID,
  487. CancelOutDetailId: cancelInfo.ID,
  488. ProductDate: warehouse.ProductDate,
  489. ExpireDate: warehouse.ExpiryDate,
  490. StorehouseId: goods.StorehouseId,
  491. BuyPrice: warehouse.Price,
  492. ProjectId: goods.ProjectId,
  493. OverCount: sum_count,
  494. RegisterNumber: warehouse.RegisterNumber,
  495. OperateTime: time.Now().Unix(),
  496. }
  497. if orgID == 9671 || orgID == 10265 {
  498. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  499. flow.Price = goodsInfo.PackingPrice
  500. flow.BuyPrice = goodsInfo.BuyPrice
  501. }
  502. CreateStockFlowOne(flow)
  503. }
  504. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  505. goods.Count = deliver_number - stock_number
  506. ConsumablesGoodDelivery(orgID, patient_id, record_time, goods, warehouseOut, count)
  507. }
  508. return nil
  509. }
  510. func FindWarehouseOutInfoByPatientId(patient_id int64, record_time int64, good_id int64, org_id int64) (models.WarehouseOutInfo, error) {
  511. outInfo := models.WarehouseOutInfo{}
  512. err := XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and org_id = ? and status = 1", patient_id, record_time, good_id, org_id).Last(&outInfo).Error
  513. return outInfo, err
  514. }
  515. func FindWarehouseOutInfoByPatientIdTwo(patient_id int64, record_time int64, good_id int64, org_id int64, project_id int64) (models.WarehouseOutInfo, error) {
  516. outInfo := models.WarehouseOutInfo{}
  517. err := XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and org_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, org_id, project_id).Last(&outInfo).Error
  518. return outInfo, err
  519. }
  520. func FindNewWarehouseOutInfoByPatientIdTwo(patient_id int64, record_time int64, good_id int64, org_id int64, project_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  521. outInfo := models.WarehouseOutInfo{}
  522. err := tx.Where("patient_id = ? and sys_record_time = ? and good_id = ? and org_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, org_id, project_id).Last(&outInfo).Error
  523. if err != gorm.ErrRecordNotFound {
  524. if err != nil {
  525. tx.Rollback()
  526. return outInfo, err
  527. }
  528. }
  529. return outInfo, err
  530. }
  531. func UpdateDrugStockCount(drug_id int64, org_id int64, storehouse_id int64, flush_count int64) error {
  532. drug := models.XtDrugStockCount{}
  533. err := XTWriteDB().Model(&drug).Where("drug_id = ? and user_org_id = ? and storehouse_id = ? and status = 1", drug_id, org_id, storehouse_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  534. return err
  535. }
  536. func UpdateNewDrugStockCount(drug_id int64, org_id int64, storehouse_id int64, flush_count int64, tx *gorm.DB) error {
  537. drug := models.XtDrugStockCount{}
  538. err := tx.Model(&drug).Where("drug_id = ? and user_org_id = ? and storehouse_id = ? and status = 1", drug_id, org_id, storehouse_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  539. if err != nil {
  540. tx.Rollback()
  541. return err
  542. }
  543. return err
  544. }
  545. func AddDrugCount(drug_id int64, org_id int64, storehouse_id int64, out_count int64) error {
  546. ut := XTWriteDB().Begin()
  547. err := ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id = ?", storehouse_id, org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", out_count)).Error
  548. if err != nil {
  549. ut.Rollback()
  550. return err
  551. }
  552. err = ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id = ?", storehouse_id, org_id, drug_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count + ?", out_count)).Error
  553. if err != nil {
  554. ut.Rollback()
  555. return err
  556. }
  557. ut.Commit()
  558. return err
  559. }
  560. func AddNewDrugCount(drug_id int64, org_id int64, storehouse_id int64, out_count int64, tx *gorm.DB) error {
  561. err := tx.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id = ?", storehouse_id, org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", out_count)).Error
  562. if err != nil {
  563. tx.Rollback()
  564. return err
  565. }
  566. err = tx.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id = ?", storehouse_id, org_id, drug_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count + ?", out_count)).Error
  567. if err != nil {
  568. tx.Rollback()
  569. return err
  570. }
  571. return err
  572. }
  573. func FindOverCount(drug_id int64, org_id int64, storehouse_id int64) (models.XtDrugStockCount, error) {
  574. stockCount := models.XtDrugStockCount{}
  575. err := XTReadDB().Where("drug_id = ? and user_org_id = ? and storehouse_id=?", drug_id, org_id, storehouse_id).Find(&stockCount).Error
  576. return stockCount, err
  577. }
  578. func FindNewOverCount(drug_id int64, org_id int64, storehouse_id int64, tx *gorm.DB) (models.XtDrugStockCount, error) {
  579. stockCount := models.XtDrugStockCount{}
  580. err := tx.Where("drug_id = ? and user_org_id = ? and storehouse_id=?", drug_id, org_id, storehouse_id).Find(&stockCount).Error
  581. if err != gorm.ErrRecordNotFound {
  582. if err != nil {
  583. tx.Rollback()
  584. return stockCount, err
  585. }
  586. }
  587. return stockCount, err
  588. }
  589. func UpdateActOut(id int64, sum_in_count int64, flush_count int64, sum_cancel_count int64) error {
  590. var sum_out_count int64
  591. sum_out_count = sum_in_count - flush_count
  592. var sum_act_out_count int64
  593. sum_act_out_count = sum_out_count + sum_cancel_count
  594. err := XTWriteDB().Model(&models.XtDrugStockCount{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"sum_out_count": sum_out_count, "sum_act_out_count": sum_act_out_count}).Error
  595. return err
  596. }
  597. func UpdateNewActOut(id int64, sum_in_count int64, flush_count int64, sum_cancel_count int64, tx *gorm.DB) error {
  598. var sum_out_count int64
  599. sum_out_count = sum_in_count - flush_count
  600. var sum_act_out_count int64
  601. sum_act_out_count = sum_out_count + sum_cancel_count
  602. err := tx.Model(&models.XtDrugStockCount{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"sum_out_count": sum_out_count, "sum_act_out_count": sum_act_out_count}).Error
  603. if err != nil {
  604. tx.Rollback()
  605. return err
  606. }
  607. return err
  608. }
  609. func ReduceDrugCount(drug_id int64, org_id int64, storehouse_id int64, out_count int64) error {
  610. ut := XTWriteDB().Begin()
  611. err := ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id = ?", storehouse_id, org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", out_count)).Error
  612. if err != nil {
  613. ut.Rollback()
  614. return err
  615. }
  616. err = ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id = ?", storehouse_id, org_id, drug_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count - ?", out_count)).Error
  617. if err != nil {
  618. ut.Rollback()
  619. return err
  620. }
  621. ut.Commit()
  622. return err
  623. }
  624. func ReduceDrugCountTwo(drug_id int64, org_id int64, storehouse_id int64, out_count int64) error {
  625. ut := XTWriteDB().Begin()
  626. err := ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id = ?", storehouse_id, org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", out_count)).Error
  627. if err != nil {
  628. ut.Rollback()
  629. return err
  630. }
  631. ut.Commit()
  632. return err
  633. }
  634. func ReduceNewDrugCountTwo(drug_id int64, org_id int64, storehouse_id int64, out_count int64, tx *gorm.DB) error {
  635. err := tx.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id = ?", storehouse_id, org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", out_count)).Error
  636. if err != nil {
  637. tx.Rollback()
  638. return err
  639. }
  640. return err
  641. }
  642. // 药品自动出库 递归方式
  643. func BloodDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DoctorAdvice) (err error) {
  644. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  645. var deliver_number int64 = 0
  646. var stock_number int64 = 0
  647. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  648. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  649. var drug_price float64
  650. if advice.PrescribingNumberUnit == drup.MaxUnit {
  651. deliver_number = count * drup.MinNumber
  652. } else {
  653. deliver_number = count
  654. }
  655. if advice.PrescribingNumberUnit == drup.MaxUnit {
  656. drug_price = drup.RetailPrice
  657. }
  658. fmt.Println(drug_price)
  659. if advice.PrescribingNumberUnit == drup.MinUnit {
  660. drug_price = drup.MinPrice
  661. }
  662. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  663. drug_price = drup.RetailPrice
  664. }
  665. // 根据先进先出原则,查询最先入库的批次,进行出库
  666. // 如果没有对应的库存,则报错
  667. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  668. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  669. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  670. var stockMax int64
  671. var stockMin int64
  672. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  673. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  674. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  675. UpdateMinNumber(lastWarehouse.ID, stockMin)
  676. }
  677. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  678. if err != nil {
  679. return err
  680. }
  681. // 将该批次的剩余库存数量转换为拆零数量
  682. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  683. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  684. if stock_number >= deliver_number {
  685. var maxNumber int64 = 0
  686. var minNumber int64 = 0
  687. maxNumber = deliver_number / drup.MinNumber
  688. minNumber = deliver_number % drup.MinNumber
  689. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  690. minNumber = maxNumber
  691. }
  692. if drup.MaxUnit != drup.MinUnit {
  693. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  694. return errors.New("库存数量不足")
  695. }
  696. }
  697. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  698. if warehouse.StockMaxNumber < 0 {
  699. warehouse.StockMaxNumber = 0
  700. }
  701. if warehouse.StockMinNumber < 0 {
  702. warehouse.StockMinNumber = 0
  703. }
  704. warehouse.Mtime = time.Now().Unix()
  705. if warehouse.StockMinNumber < minNumber {
  706. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  707. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  708. } else {
  709. if minNumber > 0 {
  710. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  711. }
  712. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  713. if warehouse.StockMinNumber > 0 {
  714. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  715. }
  716. }
  717. }
  718. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  719. if (warehouse.StockMinNumber - deliver_number) > 0 {
  720. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  721. }
  722. }
  723. if drup.MaxUnit != drup.MinUnit {
  724. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  725. return errors.New("库存数量不足")
  726. }
  727. }
  728. if warehouse.StockMinNumber <= 0 {
  729. warehouse.StockMinNumber = 0
  730. }
  731. //扣减库存数据
  732. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  733. if errThree != nil {
  734. return errThree
  735. }
  736. //查询剩余库存
  737. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  738. var sum_count int64
  739. for _, its := range stockInfo {
  740. if its.MaxUnit == drup.MaxUnit {
  741. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  742. }
  743. sum_count += its.StockMaxNumber + its.StockMinNumber
  744. }
  745. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  746. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  747. WarehouseOutId: warehouseout.ID,
  748. Status: 1,
  749. Ctime: time.Now().Unix(),
  750. Remark: warehouse.Remark,
  751. OrgId: orgID,
  752. Type: 1,
  753. Manufacturer: warehouse.Manufacturer,
  754. Dealer: warehouse.Dealer,
  755. IsSys: 1,
  756. SysRecordTime: advice.RecordDate,
  757. DrugId: advice.DrugId,
  758. Number: warehouse.Number,
  759. BatchNumber: warehouse.BatchNumber,
  760. Price: warehouse.RetailPrice,
  761. CountUnit: drup.MinUnit,
  762. RetailPrice: warehouse.RetailPrice,
  763. ProductDate: warehouse.ProductDate,
  764. ExpiryDate: warehouse.ExpiryDate,
  765. PatientId: advice.PatientId,
  766. Count: deliver_number, //出最小单位
  767. WarehouseInfoId: warehouse.ID,
  768. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  769. StorehouseId: storeConfig.DrugStorehouseOut,
  770. IsCheck: 1,
  771. OverCount: sum_count,
  772. AdviceId: advice.ID,
  773. }
  774. //查询今日该药品该患者是否有出库数据
  775. lastDrugOutInfo, errThreeCode := GetNewDrugWarehouseOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId)
  776. if errThreeCode == gorm.ErrRecordNotFound {
  777. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  778. if errOne != nil {
  779. return errOne
  780. }
  781. }
  782. if errThreeCode == nil {
  783. errThree := UpdateSigleDrugWarehouseOutInfo(lastDrugOutInfo.ID, warehouseOutInfo)
  784. if errThree != nil {
  785. return errThree
  786. }
  787. }
  788. var out_count int64
  789. var cancel_count int64
  790. //查询出库数据
  791. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  792. //查询退库数据
  793. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  794. for _, item := range infoCountList {
  795. out_count += item.Count
  796. }
  797. for _, item := range cancelInfoList {
  798. cancel_count += item.Count
  799. }
  800. //如果本次出库数据大于历史出库数据 新增1条流水
  801. if deliver_number > (out_count - cancel_count) {
  802. drugflow := models.DrugFlow{
  803. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  804. WarehouseOutId: warehouseout.ID,
  805. DrugId: advice.DrugId,
  806. Number: warehouse.Number,
  807. ProductDate: warehouse.ProductDate,
  808. ExpireDate: warehouse.ExpiryDate,
  809. Count: deliver_number - (out_count - cancel_count), //按最小单位计算,
  810. Price: warehouse.RetailPrice,
  811. Status: 1,
  812. Ctime: time.Now().Unix(),
  813. UserOrgId: orgID,
  814. Manufacturer: warehouse.Manufacturer,
  815. Dealer: warehouse.Dealer,
  816. BatchNumber: warehouse.BatchNumber,
  817. MaxUnit: drup.MinUnit,
  818. ConsumableType: 3,
  819. IsEdit: 1,
  820. Creator: advice.ExecutionStaff,
  821. IsSys: 1,
  822. PatientId: advice.PatientId,
  823. SystemTime: advice.AdviceDate,
  824. WarehousingDetailId: warehouse.ID,
  825. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  826. StorehouseId: storeConfig.DrugStorehouseOut,
  827. WarehouseOutDetailId: lastDrugOutInfo.ID,
  828. OverCount: sum_count,
  829. AdviceId: advice.ID,
  830. LastPrice: warehouse.Price,
  831. OperateTime: time.Now().Unix(),
  832. }
  833. CreateDrugFlowOne(drugflow)
  834. //出库数量相加
  835. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  836. }
  837. //如果本次出库数据小于历史出库数据 新增1条退库流水
  838. if deliver_number < (out_count - cancel_count) {
  839. operation_time := time.Now().Unix()
  840. ctime := time.Now().Unix()
  841. timeStr := time.Now().Format("2006-01-02")
  842. timeArr := strings.Split(timeStr, "-")
  843. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  844. total = total + 1
  845. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  846. cancelStock := models.DrugCancelStock{
  847. OrderNumber: orderNumber,
  848. OperaTime: operation_time,
  849. OrgId: advice.UserOrgId,
  850. Creater: advice.ExecutionStaff,
  851. Ctime: ctime,
  852. Status: 1,
  853. ReturnTime: advice.AdviceDate,
  854. Dealer: 0,
  855. Manufacturer: 0,
  856. Type: 1,
  857. StorehouseId: storeConfig.DrugStorehouseOut,
  858. IsCheck: 1,
  859. }
  860. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  861. if errCode == gorm.ErrRecordNotFound {
  862. AddSigleDrugCancelStock(&cancelStock)
  863. }
  864. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  865. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  866. dealer, _ := GetDealerById(warehouse.Dealer)
  867. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  868. cancelStockInfo := models.DrugCancelStockInfo{
  869. OrderNumber: cancelInfo.OrderNumber,
  870. CancelStockId: lastDrugCancelStock.ID,
  871. DrugId: advice.DrugId,
  872. Count: (out_count - cancel_count) - deliver_number,
  873. Status: 1,
  874. Ctime: ctime,
  875. OrgId: advice.UserOrgId,
  876. Type: 1,
  877. Manufacturer: manufactureName.ManufacturerName,
  878. Dealer: dealer.DealerName,
  879. Total: 0,
  880. RetailPrice: warehouse.RetailPrice,
  881. RetailTotalPrice: warehouse.RetailTotalPrice,
  882. Price: warehouse.Price,
  883. RegisterAccount: "",
  884. Remark: warehouse.Remark,
  885. BatchNumber: warehouse.BatchNumber,
  886. MaxUnit: drup.MinUnit,
  887. ProductDate: warehouse.ProductDate,
  888. ExpiryDate: warehouse.ExpiryDate,
  889. BatchNumberId: warehouse.ID,
  890. StorehouseId: storeConfig.DrugStorehouseOut,
  891. IsCheck: 1,
  892. }
  893. CreatedCancelStock(cancelStockInfo)
  894. flow := models.DrugFlow{
  895. WarehousingId: warehouse.ID,
  896. DrugId: warehouse.DrugId,
  897. Number: "",
  898. BatchNumber: warehouse.BatchNumber,
  899. Count: (out_count - cancel_count) - deliver_number,
  900. UserOrgId: advice.UserOrgId,
  901. PatientId: advice.PatientId,
  902. SystemTime: advice.AdviceDate,
  903. ConsumableType: 7,
  904. IsSys: 1,
  905. WarehousingOrder: "",
  906. WarehouseOutId: 0,
  907. WarehouseOutOrderNumber: "",
  908. IsEdit: 0,
  909. CancelStockId: cancelStock.ID,
  910. CancelOrderNumber: cancelStock.OrderNumber,
  911. Manufacturer: warehouse.Manufacturer,
  912. Dealer: warehouse.Dealer,
  913. Creator: advice.ExecutionStaff,
  914. UpdateCreator: 0,
  915. Status: 1,
  916. Ctime: time.Now().Unix(),
  917. Mtime: 0,
  918. Price: warehouse.RetailPrice,
  919. WarehousingDetailId: warehouse.ID,
  920. WarehouseOutDetailId: 0,
  921. CancelOutDetailId: lastDrugCancelStock.ID,
  922. ExpireDate: warehouse.ExpiryDate,
  923. ProductDate: warehouse.ProductDate,
  924. MaxUnit: drup.MinUnit,
  925. MinUnit: "",
  926. AdviceId: advice.ID,
  927. StorehouseId: storeConfig.DrugStorehouseOut,
  928. LastPrice: warehouse.Price,
  929. OperateTime: time.Now().Unix(),
  930. }
  931. CreateDrugFlowOne(flow)
  932. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  933. //出库数量减
  934. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  935. }
  936. //查询是否存在数据
  937. details := &models.DrugAutomaticReduceDetail{
  938. WarehouseOutId: warehouseout.ID,
  939. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  940. PatientId: advice.PatientId,
  941. Ctime: time.Now().Unix(),
  942. Mtime: time.Now().Unix(),
  943. Status: 1,
  944. RecordTime: advice.RecordDate,
  945. OrgId: orgID,
  946. DrugId: advice.DrugId,
  947. Count: deliver_number,
  948. CountUnit: drup.MinUnit,
  949. WarehouseInfoId: warehouse.ID,
  950. StorehouseId: storeConfig.DrugStorehouseOut,
  951. AdviceId: advice.ID,
  952. }
  953. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  954. if errTwo != nil {
  955. return errTwo
  956. }
  957. return nil
  958. } else {
  959. info := models.XtDrugWarehouseInfo{
  960. ID: warehouse.ID,
  961. WarehousingId: warehouse.WarehousingId,
  962. DrugId: warehouse.DrugId,
  963. Number: warehouse.Number,
  964. ProductDate: warehouse.ProductDate,
  965. ExpiryDate: warehouse.ExpiryDate,
  966. WarehousingCount: warehouse.WarehousingCount,
  967. Price: warehouse.RetailPrice,
  968. TotalPrice: warehouse.TotalPrice,
  969. Dealer: warehouse.Dealer,
  970. Manufacturer: warehouse.Manufacturer,
  971. Remark: warehouse.Remark,
  972. Ctime: warehouse.Ctime,
  973. Mtime: warehouse.Mtime,
  974. Status: 1,
  975. OrgId: warehouse.OrgId,
  976. IsReturn: warehouse.IsReturn,
  977. WarehousingOrder: warehouse.WarehousingOrder,
  978. Type: warehouse.Type,
  979. RetailPrice: warehouse.RetailPrice,
  980. RetailTotalPrice: warehouse.RetailPrice,
  981. StockMaxNumber: 0,
  982. StockMinNumber: 0,
  983. BatchNumber: warehouse.BatchNumber,
  984. MaxUnit: warehouse.MaxUnit,
  985. WarehousingInfoId: warehouse.WarehousingInfoId,
  986. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  987. StorehouseId: storeConfig.DrugStorehouseOut,
  988. IsCheck: 1,
  989. }
  990. //扣减库存
  991. errThree := UpDateDrugWarehouseInfoByStock(&info)
  992. if errThree != nil {
  993. return errThree
  994. }
  995. //查询剩余库存
  996. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  997. var sum_count int64
  998. for _, its := range stockInfo {
  999. if its.MaxUnit == drup.MaxUnit {
  1000. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  1001. }
  1002. sum_count += its.StockMaxNumber + its.StockMinNumber
  1003. }
  1004. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  1005. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1006. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1007. WarehouseOutId: warehouseout.ID,
  1008. Status: 1,
  1009. Ctime: time.Now().Unix(),
  1010. Remark: warehouse.Remark,
  1011. OrgId: orgID,
  1012. Type: 1,
  1013. Manufacturer: warehouse.Manufacturer,
  1014. Dealer: warehouse.Dealer,
  1015. IsSys: 1,
  1016. SysRecordTime: advice.RecordDate,
  1017. DrugId: advice.DrugId,
  1018. Number: warehouse.Number,
  1019. BatchNumber: warehouse.BatchNumber,
  1020. Price: warehouse.RetailPrice,
  1021. CountUnit: drup.MinUnit,
  1022. ProductDate: warehouse.ProductDate,
  1023. ExpiryDate: warehouse.ExpiryDate,
  1024. PatientId: advice.PatientId,
  1025. Count: stock_number,
  1026. WarehouseInfoId: warehouse.ID,
  1027. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1028. StorehouseId: storeConfig.DrugStorehouseOut,
  1029. IsCheck: 1,
  1030. OverCount: sum_count,
  1031. AdviceId: advice.ID,
  1032. }
  1033. //查询是否存在出库数据
  1034. _, errCode := GetSigleDrugWarehouseOutInfoSix(advice.PatientId, advice.AdviceDate, orgID, advice.ID)
  1035. if errCode == gorm.ErrRecordNotFound {
  1036. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1037. if errOne != nil {
  1038. return errOne
  1039. }
  1040. }
  1041. if errCode == nil {
  1042. UpdateSingleDrugWarehouseOutInfo(advice.PatientId, advice.AdviceDate, orgID, warehouseOutInfo)
  1043. }
  1044. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  1045. details := &models.DrugAutomaticReduceDetail{
  1046. WarehouseOutId: warehouseout.ID,
  1047. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  1048. PatientId: advice.PatientId,
  1049. Ctime: time.Now().Unix(),
  1050. Mtime: time.Now().Unix(),
  1051. Status: 1,
  1052. RecordTime: advice.RecordDate,
  1053. OrgId: orgID,
  1054. DrugId: advice.DrugId,
  1055. Count: stock_number,
  1056. CountUnit: drup.MinUnit,
  1057. WarehouseInfoId: warehouse.ID,
  1058. StorehouseId: storeConfig.DrugStorehouseOut,
  1059. AdviceId: advice.ID,
  1060. }
  1061. _, errcodeThree := FindDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId)
  1062. if errcodeThree == gorm.ErrRecordNotFound {
  1063. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  1064. if errTwo != nil {
  1065. return errTwo
  1066. }
  1067. }
  1068. if errcodeThree == nil {
  1069. UpdateDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId, details)
  1070. }
  1071. var out_count int64
  1072. var cancel_count int64
  1073. //查询出库数据
  1074. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  1075. //查询退库数据
  1076. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  1077. for _, item := range infoCountList {
  1078. out_count += item.Count
  1079. }
  1080. for _, item := range cancelInfoList {
  1081. cancel_count += item.Count
  1082. }
  1083. //如果本次出库数据大于历史出库数据 新增1条流水
  1084. if deliver_number > (out_count - cancel_count) {
  1085. drugflow := models.DrugFlow{
  1086. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1087. WarehouseOutId: warehouseout.ID,
  1088. DrugId: advice.DrugId,
  1089. Number: warehouse.Number,
  1090. ProductDate: warehouse.ProductDate,
  1091. ExpireDate: warehouse.ExpiryDate,
  1092. Count: stock_number,
  1093. Price: warehouse.RetailPrice,
  1094. Status: 1,
  1095. Ctime: time.Now().Unix(),
  1096. UserOrgId: orgID,
  1097. Manufacturer: warehouse.Manufacturer,
  1098. Dealer: warehouse.Dealer,
  1099. BatchNumber: warehouse.BatchNumber,
  1100. MaxUnit: drup.MinUnit,
  1101. ConsumableType: 3,
  1102. IsEdit: 1,
  1103. Creator: advice.ExecutionStaff,
  1104. IsSys: 1,
  1105. PatientId: advice.PatientId,
  1106. WarehousingDetailId: warehouse.ID,
  1107. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1108. StorehouseId: storeConfig.DrugStorehouseOut,
  1109. WarehouseOutDetailId: lastDrugOutInfo.ID,
  1110. OverCount: sum_count,
  1111. AdviceId: advice.ID,
  1112. OperateTime: time.Now().Unix(),
  1113. }
  1114. CreateDrugFlowOne(drugflow)
  1115. //出库数量相加
  1116. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  1117. }
  1118. if deliver_number < (out_count - cancel_count) {
  1119. operation_time := time.Now().Unix()
  1120. ctime := time.Now().Unix()
  1121. timeStr := time.Now().Format("2006-01-02")
  1122. timeArr := strings.Split(timeStr, "-")
  1123. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  1124. total = total + 1
  1125. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  1126. cancelStock := models.DrugCancelStock{
  1127. OrderNumber: orderNumber,
  1128. OperaTime: operation_time,
  1129. OrgId: advice.UserOrgId,
  1130. Creater: advice.ExecutionStaff,
  1131. Ctime: ctime,
  1132. Status: 1,
  1133. ReturnTime: advice.AdviceDate,
  1134. Dealer: 0,
  1135. Manufacturer: 0,
  1136. Type: 1,
  1137. StorehouseId: storeConfig.DrugStorehouseOut,
  1138. IsCheck: 1,
  1139. }
  1140. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  1141. if errCode == gorm.ErrRecordNotFound {
  1142. AddSigleDrugCancelStock(&cancelStock)
  1143. }
  1144. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  1145. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  1146. dealer, _ := GetDealerById(warehouse.Dealer)
  1147. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  1148. cancelStockInfo := models.DrugCancelStockInfo{
  1149. OrderNumber: cancelInfo.OrderNumber,
  1150. CancelStockId: lastDrugCancelStock.ID,
  1151. DrugId: advice.DrugId,
  1152. Count: (out_count - cancel_count) - deliver_number,
  1153. Status: 1,
  1154. Ctime: ctime,
  1155. OrgId: advice.UserOrgId,
  1156. Type: 1,
  1157. Manufacturer: manufactureName.ManufacturerName,
  1158. Dealer: dealer.DealerName,
  1159. Total: 0,
  1160. RetailPrice: warehouse.RetailPrice,
  1161. RetailTotalPrice: warehouse.RetailTotalPrice,
  1162. Price: warehouse.Price,
  1163. RegisterAccount: "",
  1164. Remark: warehouse.Remark,
  1165. BatchNumber: warehouse.BatchNumber,
  1166. MaxUnit: drup.MinUnit,
  1167. ProductDate: warehouse.ProductDate,
  1168. ExpiryDate: warehouse.ExpiryDate,
  1169. BatchNumberId: warehouse.ID,
  1170. StorehouseId: storeConfig.DrugStorehouseOut,
  1171. IsCheck: 1,
  1172. }
  1173. CreatedCancelStock(cancelStockInfo)
  1174. flow := models.DrugFlow{
  1175. WarehousingId: warehouse.ID,
  1176. DrugId: warehouse.DrugId,
  1177. Number: "",
  1178. BatchNumber: warehouse.BatchNumber,
  1179. Count: (out_count - cancel_count) - deliver_number,
  1180. UserOrgId: advice.UserOrgId,
  1181. PatientId: advice.PatientId,
  1182. SystemTime: advice.AdviceDate,
  1183. ConsumableType: 7,
  1184. IsSys: 1,
  1185. WarehousingOrder: "",
  1186. WarehouseOutId: 0,
  1187. WarehouseOutOrderNumber: "",
  1188. IsEdit: 0,
  1189. CancelStockId: cancelStock.ID,
  1190. CancelOrderNumber: cancelStock.OrderNumber,
  1191. Manufacturer: warehouse.Manufacturer,
  1192. Dealer: warehouse.Dealer,
  1193. Creator: advice.ExecutionStaff,
  1194. UpdateCreator: 0,
  1195. Status: 1,
  1196. Ctime: time.Now().Unix(),
  1197. Mtime: 0,
  1198. Price: warehouse.Price,
  1199. WarehousingDetailId: warehouse.ID,
  1200. WarehouseOutDetailId: 0,
  1201. CancelOutDetailId: lastDrugCancelStock.ID,
  1202. ExpireDate: warehouse.ExpiryDate,
  1203. ProductDate: warehouse.ProductDate,
  1204. MaxUnit: drup.MinUnit,
  1205. MinUnit: "",
  1206. AdviceId: advice.ID,
  1207. StorehouseId: storeConfig.DrugStorehouseOut,
  1208. OperateTime: time.Now().Unix(),
  1209. }
  1210. CreateDrugFlowOne(flow)
  1211. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  1212. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  1213. }
  1214. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1215. prescribingNumber_two_temp := deliver_number - stock_number
  1216. overPlusNumber := float64(prescribingNumber_two_temp)
  1217. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  1218. advice.PrescribingNumberUnit = drup.MinUnit
  1219. BloodDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  1220. }
  1221. return
  1222. }
  1223. // 药品自动出库 递归方式
  1224. func BloodHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo, hisprescribingNumber float64, unit string) (err error) {
  1225. //开事务
  1226. tx := XTWriteDB().Begin()
  1227. defer func() {
  1228. if err != nil {
  1229. utils.ErrorLog("事务失败,原因为: %v", err)
  1230. tx.Rollback()
  1231. } else {
  1232. tx.Commit()
  1233. }
  1234. }()
  1235. drugError := models.XtDrugError{
  1236. UserOrgId: orgID,
  1237. DrugId: advice.DrugId,
  1238. RecordDate: advice.AdviceDate,
  1239. PatientId: advice.PatientId,
  1240. Remark: "进入出库页面",
  1241. Status: 1,
  1242. Ctime: time.Now().Unix(),
  1243. Mtime: 0,
  1244. SumCount: 0,
  1245. Prescribingnumber: advice.PrescribingNumber,
  1246. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1247. }
  1248. CreateDrugError(drugError)
  1249. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  1250. var deliver_number int64 = 0
  1251. var deliver_number_one int64 = 0
  1252. var stock_number int64 = 0
  1253. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  1254. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1255. prescribingNumber_temp_one := strconv.FormatFloat(math.Abs(hisprescribingNumber), 'f', 0, 64)
  1256. countone, _ := strconv.ParseInt(prescribingNumber_temp_one, 10, 64)
  1257. var drug_price float64
  1258. if advice.PrescribingNumberUnit == drup.MaxUnit {
  1259. deliver_number = count * drup.MinNumber
  1260. } else {
  1261. deliver_number = count
  1262. }
  1263. if unit == drup.MaxUnit {
  1264. deliver_number_one = countone * drup.MinNumber
  1265. } else {
  1266. deliver_number_one = countone
  1267. }
  1268. if advice.PrescribingNumberUnit == drup.MaxUnit {
  1269. drug_price = drup.RetailPrice
  1270. }
  1271. fmt.Println(drug_price)
  1272. if advice.PrescribingNumberUnit == drup.MinUnit {
  1273. drug_price = drup.MinPrice
  1274. }
  1275. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  1276. drug_price = drup.RetailPrice
  1277. }
  1278. // 根据先进先出原则,查询最先入库的批次,进行出库
  1279. // 如果没有对应的库存,则报错
  1280. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  1281. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1282. if lastWarehouse.MaxUnit != lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  1283. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  1284. var stockMax int64
  1285. var stockMin int64
  1286. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  1287. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  1288. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  1289. UpdateMinNumber(lastWarehouse.ID, stockMin)
  1290. }
  1291. }
  1292. if lastWarehouse.MaxUnit == lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  1293. lastWarehouse.StockMaxNumber = lastWarehouse.StockMaxNumber + lastWarehouse.StockMinNumber
  1294. ChangeMaxNumberOne(lastWarehouse.ID, lastWarehouse.StockMaxNumber)
  1295. }
  1296. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1297. if err != nil {
  1298. drugError := models.XtDrugError{
  1299. UserOrgId: orgID,
  1300. DrugId: advice.DrugId,
  1301. RecordDate: advice.AdviceDate,
  1302. PatientId: advice.PatientId,
  1303. Remark: "查询库存报错",
  1304. Status: 1,
  1305. Ctime: time.Now().Unix(),
  1306. Mtime: 0,
  1307. SumCount: 0,
  1308. Prescribingnumber: advice.PrescribingNumber,
  1309. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1310. }
  1311. CreateDrugError(drugError)
  1312. return err
  1313. }
  1314. // 将该批次的剩余库存数量转换为拆零数量
  1315. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1316. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  1317. }
  1318. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  1319. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  1320. }
  1321. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  1322. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  1323. }
  1324. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1325. if stock_number >= deliver_number {
  1326. var maxNumber int64 = 0
  1327. var minNumber int64 = 0
  1328. maxNumber = deliver_number / drup.MinNumber
  1329. minNumber = deliver_number % drup.MinNumber
  1330. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  1331. minNumber = maxNumber
  1332. }
  1333. if drup.MaxUnit != drup.MinUnit {
  1334. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  1335. return errors.New("库存数量不足")
  1336. }
  1337. }
  1338. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1339. //fmt.Println("金1")
  1340. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  1341. }
  1342. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1343. //fmt.Println("金2")
  1344. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  1345. }
  1346. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  1347. //fmt.Println("金3")
  1348. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  1349. }
  1350. if warehouse.StockMaxNumber < 0 {
  1351. warehouse.StockMaxNumber = 0
  1352. }
  1353. if warehouse.StockMinNumber < 0 {
  1354. warehouse.StockMinNumber = 0
  1355. }
  1356. warehouse.Mtime = time.Now().Unix()
  1357. if warehouse.StockMinNumber < minNumber {
  1358. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  1359. if warehouse.MaxUnit != warehouse.MinUnit {
  1360. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  1361. }
  1362. if minNumber == 2 && warehouse.MaxUnit == warehouse.MinUnit && warehouse.StockMinNumber == 0 {
  1363. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  1364. }
  1365. } else {
  1366. if minNumber > 0 {
  1367. if (warehouse.StockMinNumber - minNumber) >= 0 {
  1368. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  1369. }
  1370. }
  1371. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  1372. if warehouse.StockMinNumber > 0 {
  1373. if warehouse.StockMinNumber-deliver_number >= 0 {
  1374. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  1375. }
  1376. }
  1377. }
  1378. }
  1379. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit && warehouse.MaxUnit != warehouse.MinUnit {
  1380. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  1381. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  1382. }
  1383. }
  1384. if drup.MaxUnit != drup.MinUnit {
  1385. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  1386. return errors.New("库存数量不足")
  1387. }
  1388. }
  1389. if warehouse.StockMinNumber <= 0 {
  1390. warehouse.StockMinNumber = 0
  1391. }
  1392. //扣减库存数据
  1393. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  1394. if errThree != nil {
  1395. drugError := models.XtDrugError{
  1396. UserOrgId: orgID,
  1397. DrugId: advice.DrugId,
  1398. RecordDate: advice.AdviceDate,
  1399. PatientId: advice.PatientId,
  1400. Remark: "扣减库存失败",
  1401. Status: 1,
  1402. Ctime: time.Now().Unix(),
  1403. Mtime: 0,
  1404. SumCount: 0,
  1405. Prescribingnumber: advice.PrescribingNumber,
  1406. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1407. }
  1408. CreateDrugError(drugError)
  1409. return errThree
  1410. }
  1411. //更改字典里面的价格
  1412. if advice.UserOrgId == 10265 || advice.UserOrgId == 10215 {
  1413. if lastWarehouse.RetailPrice > 0 {
  1414. UpdateBasePrice(lastWarehouse.DrugId, lastWarehouse.RetailPrice)
  1415. }
  1416. }
  1417. //查询剩余库存
  1418. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  1419. var sum_count int64
  1420. for _, its := range stockInfo {
  1421. if its.MaxUnit == drup.MaxUnit {
  1422. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  1423. }
  1424. sum_count += its.StockMaxNumber + its.StockMinNumber
  1425. }
  1426. if orgID != 10188 && orgID != 10217 && orgID != 3877 {
  1427. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1428. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1429. WarehouseOutId: warehouseout.ID,
  1430. Status: 1,
  1431. Ctime: advice.RecordDate,
  1432. Mtime: time.Now().Unix(),
  1433. Remark: warehouse.Remark,
  1434. OrgId: orgID,
  1435. Type: 1,
  1436. Manufacturer: warehouse.Manufacturer,
  1437. Dealer: warehouse.Dealer,
  1438. IsSys: 1,
  1439. SysRecordTime: advice.RecordDate,
  1440. DrugId: advice.DrugId,
  1441. Number: warehouse.Number,
  1442. BatchNumber: warehouse.BatchNumber,
  1443. Price: warehouse.RetailPrice,
  1444. CountUnit: drup.MinUnit,
  1445. RetailPrice: warehouse.RetailPrice,
  1446. ProductDate: warehouse.ProductDate,
  1447. ExpiryDate: warehouse.ExpiryDate,
  1448. PatientId: advice.PatientId,
  1449. Count: deliver_number, //出最小单位
  1450. WarehouseInfoId: warehouse.ID,
  1451. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1452. StorehouseId: storeConfig.DrugStorehouseOut,
  1453. IsCheck: 1,
  1454. OverCount: sum_count,
  1455. AdviceId: advice.ID,
  1456. }
  1457. //查询今日该药品该患者是否有出库数据
  1458. lastDrugOutInfo, _ := GetNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID)
  1459. if lastDrugOutInfo.ID == 0 {
  1460. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1461. if errOne != nil {
  1462. drugError := models.XtDrugError{
  1463. UserOrgId: orgID,
  1464. DrugId: advice.DrugId,
  1465. RecordDate: advice.AdviceDate,
  1466. PatientId: advice.PatientId,
  1467. Remark: "创建出库单失败",
  1468. Status: 1,
  1469. Ctime: time.Now().Unix(),
  1470. Mtime: 0,
  1471. SumCount: 0,
  1472. Prescribingnumber: advice.PrescribingNumber,
  1473. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1474. }
  1475. CreateDrugError(drugError)
  1476. }
  1477. }
  1478. if lastDrugOutInfo.ID > 0 {
  1479. //删除在新增
  1480. UpdateDrugWarehouseOutInfoTen(lastDrugOutInfo.PatientId, lastDrugOutInfo.SysRecordTime, advice.ID)
  1481. AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1482. }
  1483. }
  1484. var out_count int64
  1485. var cancel_count int64
  1486. //查询出库数据
  1487. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  1488. //查询退库数据
  1489. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  1490. for _, item := range infoCountList {
  1491. out_count += item.Count
  1492. }
  1493. for _, item := range cancelInfoList {
  1494. cancel_count += item.Count
  1495. }
  1496. if deliver_number_one-(out_count-cancel_count) >= 0 {
  1497. if (deliver_number_one - out_count - cancel_count) != 0 {
  1498. if orgID == 10217 || orgID == 10188 || orgID == 3877 {
  1499. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1500. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1501. WarehouseOutId: warehouseout.ID,
  1502. Status: 1,
  1503. Ctime: advice.RecordDate,
  1504. Mtime: time.Now().Unix(),
  1505. Remark: warehouse.Remark,
  1506. OrgId: orgID,
  1507. Type: 1,
  1508. Manufacturer: warehouse.Manufacturer,
  1509. Dealer: warehouse.Dealer,
  1510. IsSys: 1,
  1511. SysRecordTime: advice.RecordDate,
  1512. DrugId: advice.DrugId,
  1513. Number: warehouse.Number,
  1514. BatchNumber: warehouse.BatchNumber,
  1515. Price: warehouse.RetailPrice,
  1516. CountUnit: drup.MinUnit,
  1517. RetailPrice: warehouse.RetailPrice,
  1518. ProductDate: warehouse.ProductDate,
  1519. ExpiryDate: warehouse.ExpiryDate,
  1520. PatientId: advice.PatientId,
  1521. Count: deliver_number_one - (out_count - cancel_count), //出最小单位
  1522. WarehouseInfoId: warehouse.ID,
  1523. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1524. StorehouseId: storeConfig.DrugStorehouseOut,
  1525. IsCheck: 1,
  1526. OverCount: sum_count,
  1527. AdviceId: advice.ID,
  1528. }
  1529. errThreeThrtiy := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1530. if errThreeThrtiy != nil {
  1531. drugError := models.XtDrugError{
  1532. UserOrgId: orgID,
  1533. DrugId: advice.DrugId,
  1534. RecordDate: advice.AdviceDate,
  1535. PatientId: advice.PatientId,
  1536. Remark: "插入数据报错",
  1537. Status: 1,
  1538. Ctime: time.Now().Unix(),
  1539. Mtime: 0,
  1540. SumCount: 0,
  1541. Prescribingnumber: advice.PrescribingNumber,
  1542. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1543. }
  1544. CreateDrugError(drugError)
  1545. }
  1546. }
  1547. lastDrugOutInfo, _ := GetNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID)
  1548. drugflow := models.DrugFlow{
  1549. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1550. WarehouseOutId: warehouseout.ID,
  1551. DrugId: advice.DrugId,
  1552. Number: warehouse.Number,
  1553. ProductDate: warehouse.ProductDate,
  1554. ExpireDate: warehouse.ExpiryDate,
  1555. Count: deliver_number_one - (out_count - cancel_count), //按最小单位计算,
  1556. Price: warehouse.RetailPrice,
  1557. Status: 1,
  1558. Ctime: advice.AdviceDate,
  1559. Mtime: time.Now().Unix(),
  1560. UserOrgId: orgID,
  1561. Manufacturer: warehouse.Manufacturer,
  1562. Dealer: warehouse.Dealer,
  1563. BatchNumber: warehouse.BatchNumber,
  1564. MaxUnit: drup.MinUnit,
  1565. ConsumableType: 3,
  1566. IsEdit: 1,
  1567. Creator: advice.ExecutionStaff,
  1568. IsSys: 1,
  1569. PatientId: advice.PatientId,
  1570. SystemTime: advice.AdviceDate,
  1571. WarehousingDetailId: warehouse.ID,
  1572. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1573. StorehouseId: storeConfig.DrugStorehouseOut,
  1574. WarehouseOutDetailId: lastDrugOutInfo.ID,
  1575. OverCount: sum_count,
  1576. AdviceId: advice.ID,
  1577. LastPrice: warehouse.Price,
  1578. OperateTime: time.Now().Unix(),
  1579. DrugCode: advice.DrugCode,
  1580. }
  1581. errThreeTy := CreateDrugFlowOne(drugflow)
  1582. if errThreeTy != nil {
  1583. drugError := models.XtDrugError{
  1584. UserOrgId: orgID,
  1585. DrugId: advice.DrugId,
  1586. RecordDate: advice.AdviceDate,
  1587. PatientId: advice.PatientId,
  1588. Remark: "创建流水失败",
  1589. Status: 1,
  1590. Ctime: time.Now().Unix(),
  1591. Mtime: 0,
  1592. SumCount: 0,
  1593. Prescribingnumber: advice.PrescribingNumber,
  1594. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1595. }
  1596. CreateDrugError(drugError)
  1597. }
  1598. //出库数量相加
  1599. errThreeTys := AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  1600. if errThreeTys != nil {
  1601. drugError := models.XtDrugError{
  1602. UserOrgId: orgID,
  1603. DrugId: advice.DrugId,
  1604. RecordDate: advice.AdviceDate,
  1605. PatientId: advice.PatientId,
  1606. Remark: "出库数量相加失败",
  1607. Status: 1,
  1608. Ctime: time.Now().Unix(),
  1609. Mtime: 0,
  1610. SumCount: 0,
  1611. Prescribingnumber: advice.PrescribingNumber,
  1612. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1613. }
  1614. CreateDrugError(drugError)
  1615. }
  1616. }
  1617. }
  1618. //如果本次出库数据小于历史出库数据 新增1条退库流水
  1619. if deliver_number_one-(out_count-cancel_count) < 0 {
  1620. operation_time := time.Now().Unix()
  1621. ctime := time.Now().Unix()
  1622. timeStr := time.Now().Format("2006-01-02")
  1623. timeArr := strings.Split(timeStr, "-")
  1624. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  1625. total = total + 1
  1626. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  1627. cancelStock := models.DrugCancelStock{
  1628. OrderNumber: orderNumber,
  1629. OperaTime: operation_time,
  1630. OrgId: advice.UserOrgId,
  1631. Creater: advice.ExecutionStaff,
  1632. Ctime: ctime,
  1633. Status: 1,
  1634. ReturnTime: advice.AdviceDate,
  1635. Dealer: 0,
  1636. Manufacturer: 0,
  1637. Type: 1,
  1638. StorehouseId: storeConfig.DrugStorehouseOut,
  1639. IsCheck: 1,
  1640. }
  1641. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  1642. if errCode == gorm.ErrRecordNotFound {
  1643. errCode := AddSigleDrugCancelStock(&cancelStock)
  1644. if errCode != nil {
  1645. drugError := models.XtDrugError{
  1646. UserOrgId: orgID,
  1647. DrugId: advice.DrugId,
  1648. RecordDate: advice.AdviceDate,
  1649. PatientId: advice.PatientId,
  1650. Remark: "创建退库单失败",
  1651. Status: 1,
  1652. Ctime: time.Now().Unix(),
  1653. Mtime: 0,
  1654. SumCount: 0,
  1655. Prescribingnumber: advice.PrescribingNumber,
  1656. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1657. }
  1658. CreateDrugError(drugError)
  1659. }
  1660. }
  1661. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  1662. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  1663. dealer, _ := GetDealerById(warehouse.Dealer)
  1664. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  1665. cancelStockInfo := models.DrugCancelStockInfo{
  1666. OrderNumber: cancelInfo.OrderNumber,
  1667. CancelStockId: lastDrugCancelStock.ID,
  1668. DrugId: advice.DrugId,
  1669. Count: (out_count - cancel_count) - deliver_number,
  1670. Status: 1,
  1671. Ctime: advice.AdviceDate,
  1672. Mtime: time.Now().Unix(),
  1673. OrgId: advice.UserOrgId,
  1674. Type: 1,
  1675. Manufacturer: manufactureName.ManufacturerName,
  1676. Dealer: dealer.DealerName,
  1677. Total: 0,
  1678. RetailPrice: warehouse.RetailPrice,
  1679. RetailTotalPrice: warehouse.RetailTotalPrice,
  1680. Price: warehouse.Price,
  1681. RegisterAccount: "",
  1682. Remark: warehouse.Remark,
  1683. BatchNumber: warehouse.BatchNumber,
  1684. MaxUnit: drup.MinUnit,
  1685. ProductDate: warehouse.ProductDate,
  1686. ExpiryDate: warehouse.ExpiryDate,
  1687. BatchNumberId: warehouse.ID,
  1688. StorehouseId: storeConfig.DrugStorehouseOut,
  1689. IsCheck: 1,
  1690. }
  1691. errCodes := CreatedCancelStock(cancelStockInfo)
  1692. if errCodes != nil {
  1693. drugError := models.XtDrugError{
  1694. UserOrgId: orgID,
  1695. DrugId: advice.DrugId,
  1696. RecordDate: advice.AdviceDate,
  1697. PatientId: advice.PatientId,
  1698. Remark: "创建退库单失败",
  1699. Status: 1,
  1700. Ctime: time.Now().Unix(),
  1701. Mtime: 0,
  1702. SumCount: 0,
  1703. Prescribingnumber: advice.PrescribingNumber,
  1704. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1705. }
  1706. CreateDrugError(drugError)
  1707. }
  1708. flow := models.DrugFlow{
  1709. WarehousingId: warehouse.ID,
  1710. DrugId: warehouse.DrugId,
  1711. Number: "",
  1712. BatchNumber: warehouse.BatchNumber,
  1713. Count: out_count - cancel_count - deliver_number,
  1714. UserOrgId: advice.UserOrgId,
  1715. PatientId: advice.PatientId,
  1716. SystemTime: advice.AdviceDate,
  1717. ConsumableType: 7,
  1718. IsSys: 0,
  1719. WarehousingOrder: "",
  1720. WarehouseOutId: 0,
  1721. WarehouseOutOrderNumber: "",
  1722. IsEdit: 0,
  1723. CancelStockId: cancelStock.ID,
  1724. CancelOrderNumber: cancelStock.OrderNumber,
  1725. Manufacturer: warehouse.Manufacturer,
  1726. Dealer: warehouse.Dealer,
  1727. Creator: advice.ExecutionStaff,
  1728. UpdateCreator: 0,
  1729. Status: 1,
  1730. Ctime: advice.AdviceDate,
  1731. Mtime: time.Now().Unix(),
  1732. Price: warehouse.RetailPrice,
  1733. WarehousingDetailId: warehouse.ID,
  1734. WarehouseOutDetailId: 0,
  1735. CancelOutDetailId: lastDrugCancelStock.ID,
  1736. ExpireDate: warehouse.ExpiryDate,
  1737. ProductDate: warehouse.ProductDate,
  1738. MaxUnit: drup.MinUnit,
  1739. MinUnit: "",
  1740. AdviceId: advice.ID,
  1741. StorehouseId: storeConfig.DrugStorehouseOut,
  1742. LastPrice: warehouse.Price,
  1743. OverCount: sum_count,
  1744. OperateTime: time.Now().Unix(),
  1745. DrugCode: advice.DrugCode,
  1746. }
  1747. errCodeflow := CreateDrugFlowOne(flow)
  1748. if errCodeflow != nil {
  1749. drugError := models.XtDrugError{
  1750. UserOrgId: orgID,
  1751. DrugId: advice.DrugId,
  1752. RecordDate: advice.AdviceDate,
  1753. PatientId: advice.PatientId,
  1754. Remark: "退库流水创建失败",
  1755. Status: 1,
  1756. Ctime: time.Now().Unix(),
  1757. Mtime: 0,
  1758. SumCount: 0,
  1759. Prescribingnumber: advice.PrescribingNumber,
  1760. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1761. }
  1762. CreateDrugError(drugError)
  1763. }
  1764. errCodesum := AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  1765. if errCodesum != nil {
  1766. drugError := models.XtDrugError{
  1767. UserOrgId: orgID,
  1768. DrugId: advice.DrugId,
  1769. RecordDate: advice.AdviceDate,
  1770. PatientId: advice.PatientId,
  1771. Remark: "增加退库数量失败",
  1772. Status: 1,
  1773. Ctime: time.Now().Unix(),
  1774. Mtime: 0,
  1775. SumCount: 0,
  1776. Prescribingnumber: advice.PrescribingNumber,
  1777. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1778. }
  1779. CreateDrugError(drugError)
  1780. }
  1781. errCodereduce := ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  1782. if errCodereduce != nil {
  1783. drugError := models.XtDrugError{
  1784. UserOrgId: orgID,
  1785. DrugId: advice.DrugId,
  1786. RecordDate: advice.AdviceDate,
  1787. PatientId: advice.PatientId,
  1788. Remark: "减少出库数量失败",
  1789. Status: 1,
  1790. Ctime: time.Now().Unix(),
  1791. Mtime: 0,
  1792. SumCount: 0,
  1793. Prescribingnumber: advice.PrescribingNumber,
  1794. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1795. }
  1796. CreateDrugError(drugError)
  1797. }
  1798. }
  1799. //查询是否存在数据
  1800. details := &models.DrugAutomaticReduceDetail{
  1801. WarehouseOutId: warehouseout.ID,
  1802. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1803. PatientId: advice.PatientId,
  1804. Ctime: advice.RecordDate,
  1805. Mtime: time.Now().Unix(),
  1806. Status: 1,
  1807. RecordTime: advice.RecordDate,
  1808. OrgId: orgID,
  1809. DrugId: advice.DrugId,
  1810. Count: deliver_number,
  1811. CountUnit: drup.MinUnit,
  1812. WarehouseInfoId: warehouse.ID,
  1813. StorehouseId: storeConfig.DrugStorehouseOut,
  1814. AdviceId: advice.ID,
  1815. }
  1816. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  1817. if errTwo != nil {
  1818. drugError := models.XtDrugError{
  1819. UserOrgId: orgID,
  1820. DrugId: advice.DrugId,
  1821. RecordDate: advice.AdviceDate,
  1822. PatientId: advice.PatientId,
  1823. Remark: "新增自动出库表失败",
  1824. Status: 1,
  1825. Ctime: time.Now().Unix(),
  1826. Mtime: 0,
  1827. SumCount: 0,
  1828. Prescribingnumber: advice.PrescribingNumber,
  1829. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1830. }
  1831. CreateDrugError(drugError)
  1832. return errTwo
  1833. }
  1834. return nil
  1835. } else {
  1836. info := models.XtDrugWarehouseInfo{
  1837. ID: warehouse.ID,
  1838. WarehousingId: warehouse.WarehousingId,
  1839. DrugId: warehouse.DrugId,
  1840. Number: warehouse.Number,
  1841. ProductDate: warehouse.ProductDate,
  1842. ExpiryDate: warehouse.ExpiryDate,
  1843. WarehousingCount: warehouse.WarehousingCount,
  1844. Price: warehouse.RetailPrice,
  1845. TotalPrice: warehouse.TotalPrice,
  1846. Dealer: warehouse.Dealer,
  1847. Manufacturer: warehouse.Manufacturer,
  1848. Remark: warehouse.Remark,
  1849. Ctime: warehouse.Ctime,
  1850. Mtime: warehouse.Mtime,
  1851. Status: 1,
  1852. OrgId: warehouse.OrgId,
  1853. IsReturn: warehouse.IsReturn,
  1854. WarehousingOrder: warehouse.WarehousingOrder,
  1855. Type: warehouse.Type,
  1856. RetailPrice: warehouse.RetailPrice,
  1857. RetailTotalPrice: warehouse.RetailPrice,
  1858. StockMaxNumber: 0,
  1859. StockMinNumber: 0,
  1860. BatchNumber: warehouse.BatchNumber,
  1861. MaxUnit: warehouse.MaxUnit,
  1862. MinUnit: drup.MinUnit,
  1863. WarehousingInfoId: warehouse.WarehousingInfoId,
  1864. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1865. StorehouseId: storeConfig.DrugStorehouseOut,
  1866. IsCheck: 1,
  1867. }
  1868. //扣减库存
  1869. errThree := UpDateDrugWarehouseInfoByStock(&info)
  1870. if errThree != nil {
  1871. drugError := models.XtDrugError{
  1872. UserOrgId: orgID,
  1873. DrugId: advice.DrugId,
  1874. RecordDate: advice.AdviceDate,
  1875. PatientId: advice.PatientId,
  1876. Remark: "第二批次扣减库存失败",
  1877. Status: 1,
  1878. Ctime: time.Now().Unix(),
  1879. Mtime: 0,
  1880. SumCount: 0,
  1881. Prescribingnumber: advice.PrescribingNumber,
  1882. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1883. }
  1884. CreateDrugError(drugError)
  1885. return errThree
  1886. }
  1887. //查询剩余库存
  1888. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  1889. var sum_count int64
  1890. for _, its := range stockInfo {
  1891. if its.MaxUnit == drup.MaxUnit {
  1892. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  1893. }
  1894. sum_count += its.StockMaxNumber + its.StockMinNumber
  1895. }
  1896. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  1897. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1898. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1899. WarehouseOutId: warehouseout.ID,
  1900. Status: 1,
  1901. Ctime: advice.RecordDate,
  1902. Mtime: time.Now().Unix(),
  1903. Remark: warehouse.Remark,
  1904. OrgId: orgID,
  1905. Type: 1,
  1906. Manufacturer: warehouse.Manufacturer,
  1907. Dealer: warehouse.Dealer,
  1908. IsSys: 1,
  1909. SysRecordTime: advice.RecordDate,
  1910. DrugId: advice.DrugId,
  1911. Number: warehouse.Number,
  1912. BatchNumber: warehouse.BatchNumber,
  1913. Price: warehouse.RetailPrice,
  1914. CountUnit: drup.MinUnit,
  1915. ProductDate: warehouse.ProductDate,
  1916. ExpiryDate: warehouse.ExpiryDate,
  1917. PatientId: advice.PatientId,
  1918. Count: stock_number,
  1919. WarehouseInfoId: warehouse.ID,
  1920. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1921. StorehouseId: storeConfig.DrugStorehouseOut,
  1922. IsCheck: 1,
  1923. OverCount: sum_count,
  1924. AdviceId: advice.ID,
  1925. }
  1926. //查询该药品是否存在出库数据
  1927. _, errCode := GetSigleDrugWarehouseOutInfoTwo(advice.PatientId, advice.AdviceDate, orgID, advice.DrugId)
  1928. if errCode == gorm.ErrRecordNotFound {
  1929. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1930. if errOne != nil {
  1931. drugError := models.XtDrugError{
  1932. UserOrgId: orgID,
  1933. DrugId: advice.DrugId,
  1934. RecordDate: advice.AdviceDate,
  1935. PatientId: advice.PatientId,
  1936. Remark: "创建出库表失败",
  1937. Status: 1,
  1938. Ctime: time.Now().Unix(),
  1939. Mtime: 0,
  1940. SumCount: 0,
  1941. Prescribingnumber: advice.PrescribingNumber,
  1942. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1943. }
  1944. CreateDrugError(drugError)
  1945. return errOne
  1946. }
  1947. }
  1948. if errCode == nil {
  1949. lastDrugOutInfo, _ := GetSigleDrugWarehouseOutInfoTwo(advice.PatientId, advice.AdviceDate, orgID, advice.DrugId)
  1950. var his_count int64
  1951. if lastDrugOutInfo.CountUnit == drup.MaxUnit {
  1952. his_count = lastDrugOutInfo.Count * drup.MinNumber
  1953. }
  1954. if lastDrugOutInfo.CountUnit == drup.MinUnit {
  1955. his_count = lastDrugOutInfo.Count
  1956. }
  1957. if deliver_number != his_count {
  1958. errThree := UpdateSingleDrugWarehouseOutInfoSix(advice.PatientId, advice.AdviceDate, orgID, warehouseOutInfo, advice.ID)
  1959. if errThree != nil {
  1960. drugError := models.XtDrugError{
  1961. UserOrgId: orgID,
  1962. DrugId: advice.DrugId,
  1963. RecordDate: advice.AdviceDate,
  1964. PatientId: advice.PatientId,
  1965. Remark: "更新出库表失败",
  1966. Status: 1,
  1967. Ctime: time.Now().Unix(),
  1968. Mtime: 0,
  1969. SumCount: 0,
  1970. Prescribingnumber: advice.PrescribingNumber,
  1971. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1972. }
  1973. CreateDrugError(drugError)
  1974. return errThree
  1975. }
  1976. }
  1977. }
  1978. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  1979. details := &models.DrugAutomaticReduceDetail{
  1980. WarehouseOutId: warehouseout.ID,
  1981. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  1982. PatientId: advice.PatientId,
  1983. Ctime: advice.AdviceDate,
  1984. Mtime: time.Now().Unix(),
  1985. Status: 1,
  1986. RecordTime: advice.RecordDate,
  1987. OrgId: orgID,
  1988. DrugId: advice.DrugId,
  1989. Count: stock_number,
  1990. CountUnit: drup.MinUnit,
  1991. WarehouseInfoId: warehouse.ID,
  1992. StorehouseId: storeConfig.DrugStorehouseOut,
  1993. AdviceId: advice.ID,
  1994. }
  1995. _, errcodeThree := FindDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId)
  1996. if errcodeThree == gorm.ErrRecordNotFound {
  1997. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  1998. if errTwo != nil {
  1999. drugError := models.XtDrugError{
  2000. UserOrgId: orgID,
  2001. DrugId: advice.DrugId,
  2002. RecordDate: advice.AdviceDate,
  2003. PatientId: advice.PatientId,
  2004. Remark: "第二批次新增自动出库表失败",
  2005. Status: 1,
  2006. Ctime: time.Now().Unix(),
  2007. Mtime: 0,
  2008. SumCount: 0,
  2009. Prescribingnumber: advice.PrescribingNumber,
  2010. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  2011. }
  2012. CreateDrugError(drugError)
  2013. return errTwo
  2014. }
  2015. }
  2016. if errcodeThree == nil {
  2017. errThreeauto := UpdateDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId, details)
  2018. if errThreeauto != nil {
  2019. drugError := models.XtDrugError{
  2020. UserOrgId: orgID,
  2021. DrugId: advice.DrugId,
  2022. RecordDate: advice.AdviceDate,
  2023. PatientId: advice.PatientId,
  2024. Remark: "第二批次更新自动出库表失败",
  2025. Status: 1,
  2026. Ctime: time.Now().Unix(),
  2027. Mtime: 0,
  2028. SumCount: 0,
  2029. Prescribingnumber: advice.PrescribingNumber,
  2030. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  2031. }
  2032. CreateDrugError(drugError)
  2033. }
  2034. }
  2035. var out_count int64
  2036. var cancel_count int64
  2037. //查询出库数据
  2038. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2039. //查询退库数据
  2040. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2041. for _, item := range infoCountList {
  2042. out_count += item.Count
  2043. }
  2044. for _, item := range cancelInfoList {
  2045. cancel_count += item.Count
  2046. }
  2047. //如果本次出库数据大于历史出库数据 新增1条流水
  2048. if deliver_number > (out_count - cancel_count) {
  2049. drugflow := models.DrugFlow{
  2050. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2051. WarehouseOutId: warehouseout.ID,
  2052. DrugId: advice.DrugId,
  2053. Number: warehouse.Number,
  2054. ProductDate: warehouse.ProductDate,
  2055. ExpireDate: warehouse.ExpiryDate,
  2056. Count: stock_number,
  2057. Price: warehouse.RetailPrice,
  2058. Status: 1,
  2059. Ctime: advice.AdviceDate,
  2060. Mtime: time.Now().Unix(),
  2061. UserOrgId: orgID,
  2062. Manufacturer: warehouse.Manufacturer,
  2063. Dealer: warehouse.Dealer,
  2064. BatchNumber: warehouse.BatchNumber,
  2065. MaxUnit: drup.MinUnit,
  2066. ConsumableType: 3,
  2067. IsEdit: 1,
  2068. Creator: advice.ExecutionStaff,
  2069. IsSys: 1,
  2070. PatientId: advice.PatientId,
  2071. WarehousingDetailId: warehouse.ID,
  2072. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2073. StorehouseId: storeConfig.DrugStorehouseOut,
  2074. WarehouseOutDetailId: lastDrugOutInfo.ID,
  2075. OverCount: sum_count,
  2076. AdviceId: advice.ID,
  2077. LastPrice: warehouse.Price,
  2078. SystemTime: advice.AdviceDate,
  2079. OperateTime: time.Now().Unix(),
  2080. DrugCode: advice.DrugCode,
  2081. }
  2082. CreateDrugFlowOne(drugflow)
  2083. //出库数量相加
  2084. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  2085. }
  2086. if deliver_number < (out_count - cancel_count) {
  2087. operation_time := time.Now().Unix()
  2088. ctime := time.Now().Unix()
  2089. timeStr := time.Now().Format("2006-01-02")
  2090. timeArr := strings.Split(timeStr, "-")
  2091. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  2092. total = total + 1
  2093. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  2094. cancelStock := models.DrugCancelStock{
  2095. OrderNumber: orderNumber,
  2096. OperaTime: operation_time,
  2097. OrgId: advice.UserOrgId,
  2098. Creater: advice.ExecutionStaff,
  2099. Ctime: ctime,
  2100. Status: 1,
  2101. ReturnTime: advice.AdviceDate,
  2102. Dealer: 0,
  2103. Manufacturer: 0,
  2104. Type: 1,
  2105. StorehouseId: storeConfig.DrugStorehouseOut,
  2106. IsCheck: 1,
  2107. }
  2108. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2109. if errCode == gorm.ErrRecordNotFound {
  2110. errCodeCancel := AddSigleDrugCancelStock(&cancelStock)
  2111. if errCodeCancel != nil {
  2112. drugError := models.XtDrugError{
  2113. UserOrgId: orgID,
  2114. DrugId: advice.DrugId,
  2115. RecordDate: advice.AdviceDate,
  2116. PatientId: advice.PatientId,
  2117. Remark: "第二批次新增流水表失败",
  2118. Status: 1,
  2119. Ctime: time.Now().Unix(),
  2120. Mtime: 0,
  2121. SumCount: 0,
  2122. Prescribingnumber: advice.PrescribingNumber,
  2123. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  2124. }
  2125. CreateDrugError(drugError)
  2126. }
  2127. }
  2128. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2129. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  2130. dealer, _ := GetDealerById(warehouse.Dealer)
  2131. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  2132. cancelStockInfo := models.DrugCancelStockInfo{
  2133. OrderNumber: cancelInfo.OrderNumber,
  2134. CancelStockId: lastDrugCancelStock.ID,
  2135. DrugId: advice.DrugId,
  2136. Count: (out_count - cancel_count) - deliver_number,
  2137. Status: 1,
  2138. Ctime: advice.AdviceDate,
  2139. Mtime: time.Now().Unix(),
  2140. OrgId: advice.UserOrgId,
  2141. Type: 1,
  2142. Manufacturer: manufactureName.ManufacturerName,
  2143. Dealer: dealer.DealerName,
  2144. Total: 0,
  2145. RetailPrice: warehouse.RetailPrice,
  2146. RetailTotalPrice: warehouse.RetailTotalPrice,
  2147. Price: warehouse.Price,
  2148. RegisterAccount: "",
  2149. Remark: warehouse.Remark,
  2150. BatchNumber: warehouse.BatchNumber,
  2151. MaxUnit: drup.MinUnit,
  2152. ProductDate: warehouse.ProductDate,
  2153. ExpiryDate: warehouse.ExpiryDate,
  2154. BatchNumberId: warehouse.ID,
  2155. StorehouseId: storeConfig.DrugStorehouseOut,
  2156. IsCheck: 1,
  2157. }
  2158. errCodeCancel := CreatedCancelStock(cancelStockInfo)
  2159. if errCodeCancel != nil {
  2160. drugError := models.XtDrugError{
  2161. UserOrgId: orgID,
  2162. DrugId: advice.DrugId,
  2163. RecordDate: advice.AdviceDate,
  2164. PatientId: advice.PatientId,
  2165. Remark: "第二批次创建自动退库表失败",
  2166. Status: 1,
  2167. Ctime: time.Now().Unix(),
  2168. Mtime: 0,
  2169. SumCount: 0,
  2170. Prescribingnumber: advice.PrescribingNumber,
  2171. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  2172. }
  2173. CreateDrugError(drugError)
  2174. }
  2175. flow := models.DrugFlow{
  2176. WarehousingId: warehouse.ID,
  2177. DrugId: warehouse.DrugId,
  2178. Number: "",
  2179. BatchNumber: warehouse.BatchNumber,
  2180. Count: out_count - cancel_count - deliver_number,
  2181. UserOrgId: advice.UserOrgId,
  2182. PatientId: advice.PatientId,
  2183. SystemTime: advice.AdviceDate,
  2184. ConsumableType: 7,
  2185. IsSys: 0,
  2186. WarehousingOrder: "",
  2187. WarehouseOutId: 0,
  2188. WarehouseOutOrderNumber: "",
  2189. IsEdit: 0,
  2190. CancelStockId: cancelStock.ID,
  2191. CancelOrderNumber: cancelStock.OrderNumber,
  2192. Manufacturer: warehouse.Manufacturer,
  2193. Dealer: warehouse.Dealer,
  2194. Creator: advice.ExecutionStaff,
  2195. UpdateCreator: 0,
  2196. Status: 1,
  2197. Ctime: advice.AdviceDate,
  2198. Mtime: time.Now().Unix(),
  2199. Price: warehouse.RetailPrice,
  2200. WarehousingDetailId: warehouse.ID,
  2201. WarehouseOutDetailId: 0,
  2202. CancelOutDetailId: lastDrugCancelStock.ID,
  2203. ExpireDate: warehouse.ExpiryDate,
  2204. ProductDate: warehouse.ProductDate,
  2205. MaxUnit: drup.MinUnit,
  2206. MinUnit: "",
  2207. AdviceId: advice.ID,
  2208. StorehouseId: storeConfig.DrugStorehouseOut,
  2209. LastPrice: warehouse.Price,
  2210. OverCount: sum_count,
  2211. OperateTime: time.Now().Unix(),
  2212. DrugCode: advice.DrugCode,
  2213. }
  2214. errCodeFlows := CreateDrugFlowOne(flow)
  2215. if errCodeFlows != nil {
  2216. drugError := models.XtDrugError{
  2217. UserOrgId: orgID,
  2218. DrugId: advice.DrugId,
  2219. RecordDate: advice.AdviceDate,
  2220. PatientId: advice.PatientId,
  2221. Remark: "第二批次创建自动退库流水表失败",
  2222. Status: 1,
  2223. Ctime: time.Now().Unix(),
  2224. Mtime: 0,
  2225. SumCount: 0,
  2226. Prescribingnumber: advice.PrescribingNumber,
  2227. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  2228. }
  2229. CreateDrugError(drugError)
  2230. }
  2231. //退库数量增加
  2232. errCodeFlows = AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  2233. if errCodeFlows != nil {
  2234. drugError := models.XtDrugError{
  2235. UserOrgId: orgID,
  2236. DrugId: advice.DrugId,
  2237. RecordDate: advice.AdviceDate,
  2238. PatientId: advice.PatientId,
  2239. Remark: "第二批次创建自动退库流水表失败",
  2240. Status: 1,
  2241. Ctime: time.Now().Unix(),
  2242. Mtime: 0,
  2243. SumCount: 0,
  2244. Prescribingnumber: advice.PrescribingNumber,
  2245. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  2246. }
  2247. CreateDrugError(drugError)
  2248. }
  2249. errCodereduce := ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  2250. if errCodereduce != nil {
  2251. drugError := models.XtDrugError{
  2252. UserOrgId: orgID,
  2253. DrugId: advice.DrugId,
  2254. RecordDate: advice.AdviceDate,
  2255. PatientId: advice.PatientId,
  2256. Remark: "第二批次减少库存表失败",
  2257. Status: 1,
  2258. Ctime: time.Now().Unix(),
  2259. Mtime: 0,
  2260. SumCount: 0,
  2261. Prescribingnumber: advice.PrescribingNumber,
  2262. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  2263. }
  2264. CreateDrugError(drugError)
  2265. }
  2266. }
  2267. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2268. prescribingNumber_two_temp := deliver_number - stock_number
  2269. overPlusNumber := float64(prescribingNumber_two_temp)
  2270. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  2271. advice.PrescribingNumberUnit = drup.MinUnit
  2272. BloodHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice, hisprescribingNumber, unit)
  2273. }
  2274. return
  2275. }
  2276. func GetSigleDrugWarehouseOutInfoSix(patient_id int64, advice_date int64, orgid int64, advice_id int64) (*models.XtDrugWarehouseOutInfo, error) {
  2277. info := models.XtDrugWarehouseOutInfo{}
  2278. var err error
  2279. err = XTReadDB().Where("patient_id = ? and advice_date = ? and org_id = ? and status = 1 and advice_id =?", patient_id, advice_date, orgid, advice_id).Find(&info).Error
  2280. if err == gorm.ErrRecordNotFound {
  2281. return nil, err
  2282. }
  2283. if err != nil {
  2284. return nil, err
  2285. }
  2286. return &info, nil
  2287. }
  2288. func GetSigleDrugWarehouseOutInfo(patient_id int64, advice_date int64, orgid int64) (*models.XtDrugWarehouseOutInfo, error) {
  2289. info := models.XtDrugWarehouseOutInfo{}
  2290. var err error
  2291. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and org_id = ? and status = 1", patient_id, advice_date, orgid).Find(&info).Error
  2292. if err == gorm.ErrRecordNotFound {
  2293. return nil, err
  2294. }
  2295. if err != nil {
  2296. return nil, err
  2297. }
  2298. return &info, nil
  2299. }
  2300. func GetSigleDrugWarehouseOutInfoTwo(patient_id int64, advice_date int64, orgid int64, drug_id int64) (*models.XtDrugWarehouseOutInfo, error) {
  2301. info := models.XtDrugWarehouseOutInfo{}
  2302. var err error
  2303. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and org_id = ? and status = 1 and drug_id = ? and is_sys=1", patient_id, advice_date, orgid, drug_id).Find(&info).Error
  2304. if err == gorm.ErrRecordNotFound {
  2305. return nil, err
  2306. }
  2307. if err != nil {
  2308. return nil, err
  2309. }
  2310. return &info, nil
  2311. }
  2312. func GetNewSigleDrugWarehouseOutInfoTwo(patient_id int64, advice_date int64, orgid int64, drug_id int64, tx *gorm.DB) (*models.XtDrugWarehouseOutInfo, error) {
  2313. info := models.XtDrugWarehouseOutInfo{}
  2314. var err error
  2315. err = tx.Where("patient_id = ? and sys_record_time = ? and org_id = ? and status = 1 and drug_id = ? and is_sys=1", patient_id, advice_date, orgid, drug_id).Find(&info).Error
  2316. if err != gorm.ErrRecordNotFound {
  2317. if err != nil {
  2318. tx.Rollback()
  2319. return &info, nil
  2320. }
  2321. }
  2322. return &info, nil
  2323. }
  2324. func GetSigleDrugWarehouseOutInfoOne(patient_id int64, advice_date int64, orgid int64) (models.XtDrugWarehouseOutInfo, error) {
  2325. info := models.XtDrugWarehouseOutInfo{}
  2326. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and org_id = ? and status = 1", patient_id, advice_date, orgid).Find(&info).Error
  2327. return info, err
  2328. }
  2329. func UpdateSingleDrugWarehouseOutInfoSix(patient_id int64, advice_date int64, orgid int64, info *models.DrugWarehouseOutInfo, advice_id int64) error {
  2330. err := XTWriteDB().Model(&models.XtDrugWarehouseOutInfo{}).Where("patient_id = ? and sys_record_time = ? and org_id = ? and advice_id = ? and status= 1", patient_id, advice_date, orgid, advice_id).Update(map[string]interface{}{"count": info.Count, "price": info.Price, "total_price": info.TotalPrice, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "number": info.Number, "batch_number": info.BatchNumber, "count_unit": info.CountUnit, "warehouse_info_id": info.WarehouseInfoId, "storehouse_id": info.StorehouseId}).Error
  2331. return err
  2332. }
  2333. func UpdateNewSingleDrugWarehouseOutInfoSix(patient_id int64, advice_date int64, orgid int64, info *models.DrugWarehouseOutInfo, advice_id int64, tx *gorm.DB) error {
  2334. err := tx.Model(&models.XtDrugWarehouseOutInfo{}).Where("patient_id = ? and sys_record_time = ? and org_id = ? and advice_id = ? and status= 1", patient_id, advice_date, orgid, advice_id).Update(map[string]interface{}{"count": info.Count, "price": info.Price, "total_price": info.TotalPrice, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "number": info.Number, "batch_number": info.BatchNumber, "count_unit": info.CountUnit, "warehouse_info_id": info.WarehouseInfoId, "storehouse_id": info.StorehouseId}).Error
  2335. if err != gorm.ErrRecordNotFound {
  2336. if err != nil {
  2337. tx.Rollback()
  2338. return err
  2339. }
  2340. }
  2341. return err
  2342. }
  2343. func UpdateSingleDrugWarehouseOutInfo(patient_id int64, advice_date int64, orgid int64, info *models.DrugWarehouseOutInfo) error {
  2344. err := XTWriteDB().Model(&models.XtDrugWarehouseOutInfo{}).Where("patient_id = ? and advice_date = ? and org_id = ?", patient_id, advice_date, orgid).Update(map[string]interface{}{"count": info.Count, "price": info.Price, "total_price": info.TotalPrice, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "number": info.Number, "batch_number": info.BatchNumber, "count_unit": info.CountUnit, "warehouse_info_id": info.WarehouseInfoId, "storehouse_id": info.StorehouseId}).Error
  2345. return err
  2346. }
  2347. func FindDrugAutoReduceRecordInfo(patient_id int64, advice_date int64, orgid int64) (*models.DrugAutomaticReduceDetail, error) {
  2348. detail := models.DrugAutomaticReduceDetail{}
  2349. var err error
  2350. err = XTReadDB().Where("patient_id = ? and record_time = ? and org_id = ?", patient_id, advice_date, orgid).Find(&detail).Error
  2351. if err == gorm.ErrRecordNotFound {
  2352. return nil, err
  2353. }
  2354. if err != nil {
  2355. return nil, err
  2356. }
  2357. return &detail, nil
  2358. }
  2359. func FindNewDrugAutoReduceRecordInfo(patient_id int64, advice_date int64, orgid int64, tx *gorm.DB) (*models.DrugAutomaticReduceDetail, error) {
  2360. detail := models.DrugAutomaticReduceDetail{}
  2361. var err error
  2362. err = tx.Where("patient_id = ? and record_time = ? and org_id = ?", patient_id, advice_date, orgid).Find(&detail).Error
  2363. if err != gorm.ErrRecordNotFound {
  2364. if err != nil {
  2365. tx.Rollback()
  2366. return &detail, nil
  2367. }
  2368. }
  2369. return &detail, nil
  2370. }
  2371. func UpdateDrugAutoReduceRecordInfo(patient_id int64, record_date int64, drug_id int64, detail *models.DrugAutomaticReduceDetail) error {
  2372. err := XTWriteDB().Model(&models.DrugAutomaticReduceDetail{}).Where("patient_id = ? and record_time = ? and drug_id = ?", patient_id, record_date, drug_id).Update(map[string]interface{}{"patient_id": detail.PatientId, "record_time": detail.RecordTime, "drug_id": detail.DrugId, "count": detail.Count, "count_unit": detail.CountUnit, "warehouse_info_id": detail.WarehouseInfoId, "advice_id": detail.AdviceId, "storehouse_id": detail.StorehouseId}).Error
  2373. return err
  2374. }
  2375. func UpdateNewDrugAutoReduceRecordInfo(patient_id int64, record_date int64, drug_id int64, detail *models.DrugAutomaticReduceDetail, tx *gorm.DB) error {
  2376. err := tx.Model(&models.DrugAutomaticReduceDetail{}).Where("patient_id = ? and record_time = ? and drug_id = ?", patient_id, record_date, drug_id).Update(map[string]interface{}{"patient_id": detail.PatientId, "record_time": detail.RecordTime, "drug_id": detail.DrugId, "count": detail.Count, "count_unit": detail.CountUnit, "warehouse_info_id": detail.WarehouseInfoId, "advice_id": detail.AdviceId, "storehouse_id": detail.StorehouseId}).Error
  2377. if err != nil {
  2378. tx.Rollback()
  2379. return err
  2380. }
  2381. return err
  2382. }
  2383. func GetNewDrugWarehouseOutInfoSix(drugid int64, patient_id int64, record_date int64, org_id int64, advice_id int64, warehouse_info_id int64) (models.DrugWarehouseOutInfo, error) {
  2384. info := models.DrugWarehouseOutInfo{}
  2385. var err error
  2386. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time = ? and org_id = ? and status = 1 and advice_id = ? and warehouse_info_id = ?", drugid, patient_id, record_date, org_id, advice_id, warehouse_info_id).Find(&info).Error
  2387. return info, err
  2388. }
  2389. func GetNewNewDrugWarehouseOutInfoSix(drugid int64, patient_id int64, record_date int64, org_id int64, advice_id int64, warehouse_info_id int64, tx *gorm.DB) (models.DrugWarehouseOutInfo, error) {
  2390. info := models.DrugWarehouseOutInfo{}
  2391. err = tx.Where("drug_id = ? and patient_id = ? and sys_record_time = ? and org_id = ? and status = 1 and advice_id = ? and warehouse_info_id = ?", drugid, patient_id, record_date, org_id, advice_id, warehouse_info_id).Find(&info).Error
  2392. if err != gorm.ErrRecordNotFound {
  2393. if err != nil {
  2394. tx.Rollback()
  2395. return info, err
  2396. }
  2397. }
  2398. return info, err
  2399. }
  2400. func GetNewDrugWarehouseOutInfo(drugid int64, patient_id int64, record_date int64, org_id int64) (models.DrugWarehouseOutInfo, error) {
  2401. info := models.DrugWarehouseOutInfo{}
  2402. var err error
  2403. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time = ? and org_id = ? and status = 1 ", drugid, patient_id, record_date, org_id).Find(&info).Error
  2404. return info, err
  2405. }
  2406. func UpdateSigleDrugWarehouseOutInfo(id int64, info *models.DrugWarehouseOutInfo) error {
  2407. err := XTWriteDB().Model(models.DrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "total_price": info.TotalPrice, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "mtime": time.Now().Unix(), "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "warehouse_info_id": info.WarehouseInfoId, "number": info.Number, "batch_number": info.BatchNumber, "advice_id": info.AdviceId, "storehouse_id": info.StorehouseId, "last_price": info.LastPrice, "over_count": info.OverCount}).Error
  2408. return err
  2409. }
  2410. func UpdateSigleDrugWarehouseOutInfoTwo(id int64, info *models.DrugWarehouseOutInfo) error {
  2411. err := XTWriteDB().Model(models.DrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "total_price": info.TotalPrice, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "mtime": time.Now().Unix(), "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "warehouse_info_id": info.WarehouseInfoId, "number": info.Number, "batch_number": info.BatchNumber, "advice_id": info.AdviceId, "storehouse_id": info.StorehouseId, "last_price": info.LastPrice}).Error
  2412. return err
  2413. }
  2414. func FindDrugWarehouseOutInfoCount(drugid int64, patient_id int64, record_date int64, org_id int64, advice_id int64) (flow []*models.DrugFlow, err error) {
  2415. err = XTReadDB().Where("drug_id = ? and patient_id = ? and system_time = ? and user_org_id = ? and consumable_type = 3 and status = 1 and advice_id = ?", drugid, patient_id, record_date, org_id, advice_id).Find(&flow).Error
  2416. return flow, err
  2417. }
  2418. func FindNewDrugWarehouseOutInfoCount(drugid int64, patient_id int64, record_date int64, org_id int64, advice_id int64, tx *gorm.DB) (flow []*models.DrugFlow, err error) {
  2419. err = tx.Where("drug_id = ? and patient_id = ? and system_time = ? and user_org_id = ? and consumable_type = 3 and status = 1 and advice_id = ?", drugid, patient_id, record_date, org_id, advice_id).Find(&flow).Error
  2420. if err != gorm.ErrRecordNotFound {
  2421. if err != nil {
  2422. tx.Rollback()
  2423. return flow, err
  2424. }
  2425. }
  2426. return flow, err
  2427. }
  2428. func FindDrugSettleWarehouseOutInfoCount(drugid int64, patient_id int64, record_date int64, org_id int64, advice_id int64) (flow []*models.DrugFlow, err error) {
  2429. err = XTReadDB().Where("drug_id = ? and patient_id = ? and system_time = ? and user_org_id = ? and consumable_type = 15 and status = 1 and advice_id = ?", drugid, patient_id, record_date, org_id, advice_id).Find(&flow).Error
  2430. return flow, err
  2431. }
  2432. func FindDrugCancelOutInfo(drugid int64, patient_id int64, record_date int64, org_id int64, advice_id int64) (flow []*models.DrugFlow, err error) {
  2433. err = XTReadDB().Where("drug_id = ? and patient_id = ? and system_time = ? and user_org_id = ? and consumable_type = 7 and status = 1 and advice_id = ?", drugid, patient_id, record_date, org_id, advice_id).Find(&flow).Error
  2434. return flow, err
  2435. }
  2436. func FindNewDrugCancelOutInfo(drugid int64, patient_id int64, record_date int64, org_id int64, advice_id int64, tx *gorm.DB) (flow []*models.DrugFlow, err error) {
  2437. err = tx.Where("drug_id = ? and patient_id = ? and system_time = ? and user_org_id = ? and consumable_type = 7 and status = 1 and advice_id = ?", drugid, patient_id, record_date, org_id, advice_id).Find(&flow).Error
  2438. if err != gorm.ErrRecordNotFound {
  2439. if err != nil {
  2440. tx.Rollback()
  2441. return flow, err
  2442. }
  2443. }
  2444. return flow, err
  2445. }
  2446. func FindDrugCancelStock(return_date int64, org_id int64) (*models.DrugCancelStock, error) {
  2447. stock := models.DrugCancelStock{}
  2448. var err error
  2449. err = XTReadDB().Where("return_time = ? and org_id = ? and status = 1", return_date, org_id).Find(&stock).Error
  2450. if err == gorm.ErrRecordNotFound {
  2451. return nil, err
  2452. }
  2453. if err != nil {
  2454. return nil, err
  2455. }
  2456. return &stock, nil
  2457. }
  2458. func FindNewDrugCancelStock(return_date int64, org_id int64, tx *gorm.DB) (*models.DrugCancelStock, error) {
  2459. stock := models.DrugCancelStock{}
  2460. var err error
  2461. err = tx.Where("return_time = ? and org_id = ? and status = 1", return_date, org_id).Find(&stock).Error
  2462. if err != gorm.ErrRecordNotFound {
  2463. if err != nil {
  2464. tx.Rollback()
  2465. return &stock, nil
  2466. }
  2467. }
  2468. return &stock, nil
  2469. }
  2470. func FindLastDrugCancelStock(advice_date int64, org_id int64) (models.DrugCancelStock, error) {
  2471. cancelStock := models.DrugCancelStock{}
  2472. err := XTReadDB().Where("return_time = ? and org_id = ? and status = 1", advice_date, org_id).Find(&cancelStock).Error
  2473. return cancelStock, err
  2474. }
  2475. func FindNewLastDrugCancelStock(advice_date int64, org_id int64, tx *gorm.DB) (models.DrugCancelStock, error) {
  2476. cancelStock := models.DrugCancelStock{}
  2477. err := tx.Where("return_time = ? and org_id = ? and status = 1", advice_date, org_id).Find(&cancelStock).Error
  2478. if err != gorm.ErrRecordNotFound {
  2479. if err != nil {
  2480. tx.Rollback()
  2481. return cancelStock, err
  2482. }
  2483. }
  2484. return cancelStock, err
  2485. }
  2486. func GetAllSumDrugWarehouseOut(patient_id int64, user_org_id int64, advice_date int64) (advice []*models.HisDoctorAdvice, err error) {
  2487. err = XTReadDB().Where("patient_id = ? and user_org_id = ? and advice_date = ? and status = 1", patient_id, user_org_id, advice_date).Find(&advice).Error
  2488. return advice, err
  2489. }
  2490. func UpdateDrugWarehouseOutByPatientId(patient_id int64, advice_date int64, orgid int64, info models.DrugWarehouseOutInfo) error {
  2491. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("patient_id = ? and sys_record_time = ? and org_id = ? and status = 1", patient_id, advice_date, orgid).Update(map[string]interface{}{"count": info.Count, "count_unit": info.CountUnit}).Error
  2492. return err
  2493. }
  2494. func FindeGoodInfo(orgid int64, id int64) (models.GoodInformation, error) {
  2495. information := models.GoodInformation{}
  2496. err := XTReadDB().Where("org_id = ? and id = ? and status = 1", orgid, id).Find(&information).Error
  2497. return information, err
  2498. }
  2499. func FindeNewGoodInfo(orgid int64, id int64, tx *gorm.DB) (models.GoodInformation, error) {
  2500. information := models.GoodInformation{}
  2501. err := tx.Where("org_id = ? and id = ? and status = 1", orgid, id).Find(&information).Error
  2502. if err != gorm.ErrRecordNotFound {
  2503. if err != nil {
  2504. tx.Rollback()
  2505. return information, err
  2506. }
  2507. }
  2508. return information, err
  2509. }
  2510. func UpdateHisPrescriptionProjectStatus(project_id int64, record_time int64, user_org_id int64, patient_id int64) (models.HisPrescriptionProject, error) {
  2511. project := models.HisPrescriptionProject{}
  2512. err := XTWriteDB().Model(&project).Where("project_id = ? and record_date = ? and user_org_id = ? and patient_id = ? and status = 1", project_id, record_time, user_org_id, patient_id).Update(map[string]interface{}{"is_out": 1}).Error
  2513. return project, err
  2514. }
  2515. // 药品自动出库 递归方式 deliver_number_one已经转成最小数量
  2516. func HisSettleDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo, deliver_number_one int64) (err error) {
  2517. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  2518. var deliver_number int64 = 0
  2519. //var deliver_number_one int64 = 0
  2520. var stock_number int64 = 0
  2521. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  2522. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  2523. //prescribingCount_temp := strconv.FormatFloat(math.Abs(prescribingCount), 'f', 0, 64)
  2524. //prescribe_count, _ := strconv.ParseInt(prescribingCount_temp, 10, 64)
  2525. var drug_price float64
  2526. if advice.PrescribingNumberUnit == drup.MaxUnit {
  2527. deliver_number = count * drup.MinNumber
  2528. } else {
  2529. deliver_number = count
  2530. }
  2531. //if advice.PrescribingNumberUnit == drup.MaxUnit {
  2532. // deliver_number_one = prescribe_count * drup.MinNumber
  2533. //
  2534. //} else {
  2535. // deliver_number_one = prescribe_count
  2536. //
  2537. //}
  2538. if advice.PrescribingNumberUnit == drup.MaxUnit {
  2539. drug_price = drup.RetailPrice
  2540. }
  2541. fmt.Println(drug_price)
  2542. if advice.PrescribingNumberUnit == drup.MinUnit {
  2543. drug_price = drup.MinPrice
  2544. }
  2545. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  2546. drug_price = drup.RetailPrice
  2547. }
  2548. // 根据先进先出原则,查询最先入库的批次,进行出库
  2549. // 如果没有对应的库存,则报错
  2550. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  2551. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2552. if lastWarehouse.StockMinNumber >= drup.MinNumber && lastWarehouse.MaxUnit != lastWarehouse.MinUnit {
  2553. var stockMax int64
  2554. var stockMin int64
  2555. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  2556. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  2557. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  2558. UpdateMinNumber(lastWarehouse.ID, stockMin)
  2559. }
  2560. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2561. if err != nil {
  2562. return err
  2563. }
  2564. // 将该批次的剩余库存数量转换为拆零数量
  2565. // 将该批次的剩余库存数量转换为拆零数量
  2566. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  2567. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  2568. }
  2569. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  2570. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  2571. }
  2572. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  2573. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  2574. }
  2575. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  2576. if stock_number >= deliver_number {
  2577. var maxNumber int64 = 0
  2578. var minNumber int64 = 0
  2579. maxNumber = deliver_number / drup.MinNumber
  2580. minNumber = deliver_number % drup.MinNumber
  2581. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  2582. minNumber = maxNumber
  2583. }
  2584. if drup.MaxUnit != drup.MinUnit {
  2585. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  2586. return errors.New("库存数量不足")
  2587. }
  2588. }
  2589. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  2590. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  2591. }
  2592. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  2593. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  2594. }
  2595. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  2596. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  2597. }
  2598. if warehouse.StockMaxNumber < 0 {
  2599. warehouse.StockMaxNumber = 0
  2600. }
  2601. if warehouse.StockMinNumber < 0 {
  2602. warehouse.StockMinNumber = 0
  2603. }
  2604. warehouse.Mtime = time.Now().Unix()
  2605. if warehouse.MaxUnit != warehouse.MinUnit {
  2606. if warehouse.StockMinNumber < minNumber {
  2607. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  2608. if warehouse.MaxUnit != warehouse.MinUnit {
  2609. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  2610. }
  2611. } else {
  2612. if minNumber > 0 {
  2613. if (warehouse.StockMinNumber - minNumber) >= 0 {
  2614. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2615. }
  2616. }
  2617. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  2618. if warehouse.StockMinNumber > 0 {
  2619. if warehouse.StockMinNumber-deliver_number >= 0 {
  2620. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2621. }
  2622. }
  2623. }
  2624. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  2625. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  2626. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2627. }
  2628. }
  2629. }
  2630. }
  2631. if warehouse.MinUnit == warehouse.MaxUnit {
  2632. if warehouse.StockMaxNumber > 0 && warehouse.StockMinNumber == 0 && warehouse.StockMaxNumber >= minNumber {
  2633. warehouse.StockMaxNumber = warehouse.StockMaxNumber - minNumber
  2634. }
  2635. if warehouse.StockMaxNumber == 0 && warehouse.StockMinNumber > 0 && warehouse.StockMinNumber >= minNumber {
  2636. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2637. }
  2638. if warehouse.StockMaxNumber > 0 && warehouse.StockMinNumber > 0 && (warehouse.StockMaxNumber+warehouse.StockMinNumber) > minNumber {
  2639. var out_false int64 = 0
  2640. if warehouse.StockMinNumber >= minNumber {
  2641. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2642. out_false = 1
  2643. }
  2644. if warehouse.StockMinNumber < minNumber && out_false == 0 {
  2645. var over_total int64
  2646. over_total = warehouse.StockMinNumber
  2647. warehouse.StockMinNumber = 0
  2648. warehouse.StockMaxNumber = warehouse.StockMaxNumber - (minNumber - over_total)
  2649. }
  2650. }
  2651. }
  2652. if drup.MaxUnit != drup.MinUnit {
  2653. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  2654. return errors.New("库存数量不足")
  2655. }
  2656. }
  2657. if warehouse.StockMinNumber <= 0 {
  2658. warehouse.StockMinNumber = 0
  2659. }
  2660. //扣减库存数据
  2661. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  2662. if errThree != nil {
  2663. return errThree
  2664. }
  2665. //查询剩余库存
  2666. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  2667. var sum_count int64
  2668. for _, its := range stockInfo {
  2669. if its.MaxUnit == drup.MaxUnit {
  2670. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  2671. }
  2672. sum_count += its.StockMaxNumber + its.StockMinNumber
  2673. }
  2674. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2675. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2676. WarehouseOutId: warehouseout.ID,
  2677. Status: 1,
  2678. Ctime: time.Now().Unix(),
  2679. Remark: warehouse.Remark,
  2680. OrgId: orgID,
  2681. Type: 5,
  2682. Manufacturer: warehouse.Manufacturer,
  2683. Dealer: warehouse.Dealer,
  2684. IsSys: 1,
  2685. SysRecordTime: advice.RecordDate,
  2686. DrugId: advice.DrugId,
  2687. Number: warehouse.Number,
  2688. BatchNumber: warehouse.BatchNumber,
  2689. Price: warehouse.RetailPrice,
  2690. CountUnit: drup.MinUnit,
  2691. RetailPrice: warehouse.RetailPrice,
  2692. ProductDate: warehouse.ProductDate,
  2693. ExpiryDate: warehouse.ExpiryDate,
  2694. PatientId: advice.PatientId,
  2695. Count: deliver_number, //出最小单位
  2696. WarehouseInfoId: warehouse.ID,
  2697. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2698. StorehouseId: storeConfig.DrugStorehouseOut,
  2699. IsCheck: 1,
  2700. OverCount: sum_count,
  2701. AdviceId: advice.ID,
  2702. OrderId: warehouseout.OrderId,
  2703. }
  2704. //查询今日该药品该患者是否有出库数据
  2705. lastDrugOutInfo, _ := GetNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID)
  2706. if lastDrugOutInfo.ID == 0 {
  2707. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2708. if errOne != nil {
  2709. return errOne
  2710. }
  2711. }
  2712. if lastDrugOutInfo.ID > 0 {
  2713. var his_count int64
  2714. if lastDrugOutInfo.CountUnit == drup.MaxUnit {
  2715. his_count = lastDrugOutInfo.Count * drup.MinNumber
  2716. }
  2717. if lastDrugOutInfo.CountUnit == drup.MinUnit {
  2718. his_count = lastDrugOutInfo.Count
  2719. }
  2720. if deliver_number != his_count {
  2721. //warehouseOutInfo.Count = deliver_number_one
  2722. errThree := UpdateSigleDrugWarehouseOutInfo(lastDrugOutInfo.ID, warehouseOutInfo)
  2723. if errThree != nil {
  2724. return errThree
  2725. }
  2726. }
  2727. }
  2728. var out_count int64
  2729. var cancel_count int64
  2730. //查询出库数据
  2731. infoCountList, _ := FindDrugSettleWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2732. //查询退库数据
  2733. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2734. for _, item := range infoCountList {
  2735. out_count += item.Count
  2736. }
  2737. for _, item := range cancelInfoList {
  2738. cancel_count += item.Count
  2739. }
  2740. fmt.Println("deliver_number_one", deliver_number_one)
  2741. fmt.Println("out_count", out_count)
  2742. fmt.Println("cancel_count", cancel_count)
  2743. //如果本次出库数据大于历史出库数据 新增1条流水
  2744. //if deliver_number_one > (out_count - cancel_count) {
  2745. // drugflow := models.DrugFlow{
  2746. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2747. // WarehouseOutId: warehouseout.ID,
  2748. // DrugId: advice.DrugId,
  2749. // Number: warehouse.Number,
  2750. // ProductDate: warehouse.ProductDate,
  2751. // ExpireDate: warehouse.ExpiryDate,
  2752. // Count: deliver_number_one - (out_count - cancel_count), //按最小单位计算,
  2753. // Price: warehouse.RetailPrice,
  2754. // Status: 1,
  2755. // Ctime: time.Now().Unix(),
  2756. // UserOrgId: orgID,
  2757. // Manufacturer: warehouse.Manufacturer,
  2758. // Dealer: warehouse.Dealer,
  2759. // BatchNumber: warehouse.BatchNumber,
  2760. // MaxUnit: drup.MinUnit,
  2761. // ConsumableType: 15,
  2762. // IsEdit: 1,
  2763. // Creator: advice.ExecutionStaff,
  2764. // IsSys: 1,
  2765. // PatientId: advice.PatientId,
  2766. // SystemTime: advice.AdviceDate,
  2767. // WarehousingDetailId: warehouse.ID,
  2768. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2769. // StorehouseId: storeConfig.DrugStorehouseOut,
  2770. // WarehouseOutDetailId: lastDrugOutInfo.ID,
  2771. // OverCount: sum_count,
  2772. // AdviceId: advice.ID,
  2773. // LastPrice: warehouse.Price,
  2774. // OrderId: warehouseout.OrderId,
  2775. // }
  2776. //
  2777. // CreateDrugFlowOne(drugflow)
  2778. // //出库数量相加
  2779. // AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  2780. //
  2781. //}
  2782. //
  2783. //if deliver_number == (out_count - cancel_count) {
  2784. // drugflow := models.DrugFlow{
  2785. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2786. // WarehouseOutId: warehouseout.ID,
  2787. // DrugId: advice.DrugId,
  2788. // Number: warehouse.Number,
  2789. // ProductDate: warehouse.ProductDate,
  2790. // ExpireDate: warehouse.ExpiryDate,
  2791. // Count: deliver_number, //按最小单位计算,
  2792. // Price: warehouse.RetailPrice,
  2793. // Status: 1,
  2794. // Ctime: time.Now().Unix(),
  2795. // UserOrgId: orgID,
  2796. // Manufacturer: warehouse.Manufacturer,
  2797. // Dealer: warehouse.Dealer,
  2798. // BatchNumber: warehouse.BatchNumber,
  2799. // MaxUnit: drup.MinUnit,
  2800. // ConsumableType: 15,
  2801. // IsEdit: 1,
  2802. // Creator: advice.ExecutionStaff,
  2803. // IsSys: 1,
  2804. // PatientId: advice.PatientId,
  2805. // SystemTime: advice.AdviceDate,
  2806. // WarehousingDetailId: warehouse.ID,
  2807. // SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2808. // StorehouseId: storeConfig.DrugStorehouseOut,
  2809. // WarehouseOutDetailId: lastDrugOutInfo.ID,
  2810. // OverCount: sum_count,
  2811. // AdviceId: advice.ID,
  2812. // LastPrice: warehouse.Price,
  2813. // OrderId: warehouseout.OrderId,
  2814. // }
  2815. //
  2816. // CreateDrugFlowOne(drugflow)
  2817. // //出库数量相加
  2818. // AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  2819. //
  2820. //}
  2821. //
  2822. //fmt.Println("出库数据", deliver_number_one)
  2823. //fmt.Println("马文强out_count233232232ooqw", out_count)
  2824. //fmt.Println("马cancel_count33232232ooqw", cancel_count)
  2825. ////如果本次出库数据小于历史出库数据 新增1条退库流水
  2826. //if deliver_number_one < (out_count - cancel_count) {
  2827. //
  2828. // operation_time := time.Now().Unix()
  2829. //
  2830. // ctime := time.Now().Unix()
  2831. // timeStr := time.Now().Format("2006-01-02")
  2832. // timeArr := strings.Split(timeStr, "-")
  2833. // total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  2834. // total = total + 1
  2835. // orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  2836. // cancelStock := models.DrugCancelStock{
  2837. // OrderNumber: orderNumber,
  2838. // OperaTime: operation_time,
  2839. // OrgId: advice.UserOrgId,
  2840. // Creater: advice.ExecutionStaff,
  2841. // Ctime: ctime,
  2842. // Status: 1,
  2843. // ReturnTime: advice.AdviceDate,
  2844. // Dealer: 0,
  2845. // Manufacturer: 0,
  2846. // Type: 5,
  2847. // StorehouseId: storeConfig.DrugStorehouseOut,
  2848. // IsCheck: 1,
  2849. // OrderId: warehouseout.OrderId,
  2850. // }
  2851. //
  2852. // _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2853. // if errCode == gorm.ErrRecordNotFound {
  2854. // AddSigleDrugCancelStock(&cancelStock)
  2855. // }
  2856. // lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2857. //
  2858. // manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  2859. // dealer, _ := GetDealerById(warehouse.Dealer)
  2860. // //cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  2861. // cancelStockInfo := models.DrugCancelStockInfo{
  2862. // OrderNumber: lastDrugCancelStock.OrderNumber,
  2863. // CancelStockId: lastDrugCancelStock.ID,
  2864. // DrugId: advice.DrugId,
  2865. // Count: (out_count - cancel_count) - deliver_number,
  2866. // Status: 1,
  2867. // Ctime: ctime,
  2868. // OrgId: advice.UserOrgId,
  2869. // Type: 5,
  2870. // Manufacturer: manufactureName.ManufacturerName,
  2871. // Dealer: dealer.DealerName,
  2872. // Total: 0,
  2873. // RetailPrice: warehouse.RetailPrice,
  2874. // RetailTotalPrice: warehouse.RetailTotalPrice,
  2875. // Price: warehouse.Price,
  2876. // RegisterAccount: "",
  2877. // Remark: warehouse.Remark,
  2878. // BatchNumber: warehouse.BatchNumber,
  2879. // MaxUnit: drup.MinUnit,
  2880. // ProductDate: warehouse.ProductDate,
  2881. // ExpiryDate: warehouse.ExpiryDate,
  2882. // BatchNumberId: warehouse.ID,
  2883. // StorehouseId: storeConfig.DrugStorehouseOut,
  2884. // IsCheck: 1,
  2885. // OrderId: warehouseout.OrderId,
  2886. // }
  2887. // CreatedCancelStock(cancelStockInfo)
  2888. // flow := models.DrugFlow{
  2889. // WarehousingId: warehouse.ID,
  2890. // DrugId: warehouse.DrugId,
  2891. // Number: "",
  2892. // BatchNumber: warehouse.BatchNumber,
  2893. // Count: out_count - cancel_count - deliver_number,
  2894. // UserOrgId: advice.UserOrgId,
  2895. // PatientId: advice.PatientId,
  2896. // SystemTime: advice.AdviceDate,
  2897. // ConsumableType: 7,
  2898. // IsSys: 0,
  2899. // WarehousingOrder: "",
  2900. // WarehouseOutId: 0,
  2901. // WarehouseOutOrderNumber: "",
  2902. // IsEdit: 0,
  2903. // CancelStockId: cancelStock.ID,
  2904. // CancelOrderNumber: lastDrugCancelStock.OrderNumber,
  2905. // Manufacturer: warehouse.Manufacturer,
  2906. // Dealer: warehouse.Dealer,
  2907. // Creator: advice.ExecutionStaff,
  2908. // UpdateCreator: 0,
  2909. // Status: 1,
  2910. // Ctime: time.Now().Unix(),
  2911. // Mtime: 0,
  2912. // Price: warehouse.RetailPrice,
  2913. // WarehousingDetailId: warehouse.ID,
  2914. // WarehouseOutDetailId: 0,
  2915. // CancelOutDetailId: lastDrugCancelStock.ID,
  2916. // ExpireDate: warehouse.ExpiryDate,
  2917. // ProductDate: warehouse.ProductDate,
  2918. // MaxUnit: drup.MinUnit,
  2919. // MinUnit: "",
  2920. // AdviceId: advice.ID,
  2921. // StorehouseId: storeConfig.DrugStorehouseOut,
  2922. // LastPrice: warehouse.Price,
  2923. // OverCount: sum_count,
  2924. // OrderId: warehouseout.OrderId,
  2925. // }
  2926. // CreateDrugFlowOne(flow)
  2927. // AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  2928. // ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  2929. //}
  2930. //
  2931. ////查询是否存在数据
  2932. //details := &models.DrugAutomaticReduceDetail{
  2933. // WarehouseOutId: warehouseout.ID,
  2934. // WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2935. // PatientId: advice.PatientId,
  2936. // Ctime: time.Now().Unix(),
  2937. // Mtime: time.Now().Unix(),
  2938. // Status: 1,
  2939. // RecordTime: advice.RecordDate,
  2940. // OrgId: orgID,
  2941. // DrugId: advice.DrugId,
  2942. // Count: deliver_number,
  2943. // CountUnit: drup.MinUnit,
  2944. // WarehouseInfoId: warehouse.ID,
  2945. // StorehouseId: storeConfig.DrugStorehouseOut,
  2946. // AdviceId: advice.ID,
  2947. //}
  2948. //errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2949. //if errTwo != nil {
  2950. // return errTwo
  2951. //}
  2952. return nil
  2953. } else {
  2954. info := models.XtDrugWarehouseInfo{
  2955. ID: warehouse.ID,
  2956. WarehousingId: warehouse.WarehousingId,
  2957. DrugId: warehouse.DrugId,
  2958. Number: warehouse.Number,
  2959. ProductDate: warehouse.ProductDate,
  2960. ExpiryDate: warehouse.ExpiryDate,
  2961. WarehousingCount: warehouse.WarehousingCount,
  2962. Price: warehouse.RetailPrice,
  2963. TotalPrice: warehouse.TotalPrice,
  2964. Dealer: warehouse.Dealer,
  2965. Manufacturer: warehouse.Manufacturer,
  2966. Remark: warehouse.Remark,
  2967. Ctime: warehouse.Ctime,
  2968. Mtime: warehouse.Mtime,
  2969. Status: 1,
  2970. OrgId: warehouse.OrgId,
  2971. IsReturn: warehouse.IsReturn,
  2972. WarehousingOrder: warehouse.WarehousingOrder,
  2973. Type: warehouse.Type,
  2974. RetailPrice: warehouse.RetailPrice,
  2975. RetailTotalPrice: warehouse.RetailPrice,
  2976. StockMaxNumber: 0,
  2977. StockMinNumber: 0,
  2978. BatchNumber: warehouse.BatchNumber,
  2979. MaxUnit: warehouse.MaxUnit,
  2980. WarehousingInfoId: warehouse.WarehousingInfoId,
  2981. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2982. StorehouseId: storeConfig.DrugStorehouseOut,
  2983. IsCheck: 1,
  2984. }
  2985. //扣减库存
  2986. errThree := UpDateDrugWarehouseInfoByStock(&info)
  2987. if errThree != nil {
  2988. return errThree
  2989. }
  2990. //查询剩余库存
  2991. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  2992. var sum_count int64
  2993. for _, its := range stockInfo {
  2994. if its.MaxUnit == drup.MaxUnit {
  2995. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  2996. }
  2997. sum_count += its.StockMaxNumber + its.StockMinNumber
  2998. }
  2999. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  3000. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  3001. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3002. WarehouseOutId: warehouseout.ID,
  3003. Status: 1,
  3004. Ctime: time.Now().Unix(),
  3005. Remark: warehouse.Remark,
  3006. OrgId: orgID,
  3007. Type: 5,
  3008. Manufacturer: warehouse.Manufacturer,
  3009. Dealer: warehouse.Dealer,
  3010. IsSys: 1,
  3011. SysRecordTime: advice.RecordDate,
  3012. DrugId: advice.DrugId,
  3013. Number: warehouse.Number,
  3014. BatchNumber: warehouse.BatchNumber,
  3015. Price: warehouse.RetailPrice,
  3016. CountUnit: drup.MinUnit,
  3017. ProductDate: warehouse.ProductDate,
  3018. ExpiryDate: warehouse.ExpiryDate,
  3019. PatientId: advice.PatientId,
  3020. Count: stock_number,
  3021. WarehouseInfoId: warehouse.ID,
  3022. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3023. StorehouseId: storeConfig.DrugStorehouseOut,
  3024. IsCheck: 1,
  3025. OverCount: sum_count,
  3026. AdviceId: advice.ID,
  3027. OrderId: warehouseout.OrderId,
  3028. }
  3029. //查询是否存在出库数据
  3030. lastDrugOutInfoTwo, _ := GetNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID)
  3031. if lastDrugOutInfoTwo.ID == 0 {
  3032. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  3033. if errOne != nil {
  3034. return errOne
  3035. }
  3036. }
  3037. if lastDrugOutInfoTwo.ID > 0 {
  3038. lastDrugOutInfo, _ := GetSigleDrugWarehouseOutInfoOne(advice.PatientId, advice.AdviceDate, orgID)
  3039. var his_count int64
  3040. if lastDrugOutInfo.CountUnit == drup.MaxUnit {
  3041. his_count = lastDrugOutInfo.Count * drup.MinNumber
  3042. }
  3043. if lastDrugOutInfo.CountUnit == drup.MinUnit {
  3044. his_count = lastDrugOutInfo.Count
  3045. }
  3046. if deliver_number != his_count {
  3047. errThree := UpdateSingleDrugWarehouseOutInfoSix(advice.PatientId, advice.AdviceDate, orgID, warehouseOutInfo, advice.ID)
  3048. if errThree != nil {
  3049. return errThree
  3050. }
  3051. }
  3052. }
  3053. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  3054. details := &models.DrugAutomaticReduceDetail{
  3055. WarehouseOutId: warehouseout.ID,
  3056. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  3057. PatientId: advice.PatientId,
  3058. Ctime: time.Now().Unix(),
  3059. Mtime: time.Now().Unix(),
  3060. Status: 1,
  3061. RecordTime: advice.RecordDate,
  3062. OrgId: orgID,
  3063. DrugId: advice.DrugId,
  3064. Count: stock_number,
  3065. CountUnit: drup.MinUnit,
  3066. WarehouseInfoId: warehouse.ID,
  3067. StorehouseId: storeConfig.DrugStorehouseOut,
  3068. AdviceId: advice.ID,
  3069. }
  3070. _, errcodeThree := FindDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId)
  3071. if errcodeThree == gorm.ErrRecordNotFound {
  3072. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  3073. if errTwo != nil {
  3074. return errTwo
  3075. }
  3076. }
  3077. if errcodeThree == nil {
  3078. UpdateDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId, details)
  3079. }
  3080. var out_count int64
  3081. var cancel_count int64
  3082. //查询出库数据
  3083. infoCountList, _ := FindDrugSettleWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  3084. //查询退库数据
  3085. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  3086. for _, item := range infoCountList {
  3087. out_count += item.Count
  3088. }
  3089. for _, item := range cancelInfoList {
  3090. cancel_count += item.Count
  3091. }
  3092. fmt.Println("出库数量", deliver_number)
  3093. fmt.Println("out_coutn233232322323wo", cancel_count)
  3094. fmt.Println("out_count99988233232", out_count)
  3095. //如果本次出库数据大于历史出库数据 新增1条流水
  3096. if deliver_number > (out_count - cancel_count) {
  3097. drugflow := models.DrugFlow{
  3098. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3099. WarehouseOutId: warehouseout.ID,
  3100. DrugId: advice.DrugId,
  3101. Number: warehouse.Number,
  3102. ProductDate: warehouse.ProductDate,
  3103. ExpireDate: warehouse.ExpiryDate,
  3104. Count: stock_number,
  3105. Price: warehouse.RetailPrice,
  3106. Status: 1,
  3107. Ctime: time.Now().Unix(),
  3108. UserOrgId: orgID,
  3109. Manufacturer: warehouse.Manufacturer,
  3110. Dealer: warehouse.Dealer,
  3111. BatchNumber: warehouse.BatchNumber,
  3112. MaxUnit: drup.MinUnit,
  3113. ConsumableType: 15,
  3114. IsEdit: 1,
  3115. Creator: advice.ExecutionStaff,
  3116. IsSys: 1,
  3117. PatientId: advice.PatientId,
  3118. WarehousingDetailId: warehouse.ID,
  3119. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3120. StorehouseId: storeConfig.DrugStorehouseOut,
  3121. WarehouseOutDetailId: lastDrugOutInfo.ID,
  3122. OverCount: sum_count,
  3123. AdviceId: advice.ID,
  3124. LastPrice: warehouse.Price,
  3125. SystemTime: advice.AdviceDate,
  3126. OrderId: warehouseout.OrderId,
  3127. }
  3128. CreateDrugFlowOne(drugflow)
  3129. //出库数量相加
  3130. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  3131. }
  3132. if deliver_number < (out_count - cancel_count) {
  3133. fmt.Println("即那里3232232323")
  3134. operation_time := time.Now().Unix()
  3135. ctime := time.Now().Unix()
  3136. timeStr := time.Now().Format("2006-01-02")
  3137. timeArr := strings.Split(timeStr, "-")
  3138. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  3139. total = total + 1
  3140. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  3141. cancelStock := models.DrugCancelStock{
  3142. OrderNumber: orderNumber,
  3143. OperaTime: operation_time,
  3144. OrgId: advice.UserOrgId,
  3145. Creater: advice.ExecutionStaff,
  3146. Ctime: ctime,
  3147. Status: 1,
  3148. ReturnTime: advice.AdviceDate,
  3149. Dealer: 0,
  3150. Manufacturer: 0,
  3151. Type: 5,
  3152. StorehouseId: storeConfig.DrugStorehouseOut,
  3153. IsCheck: 1,
  3154. OrderId: warehouseout.OrderId,
  3155. }
  3156. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  3157. if errCode == gorm.ErrRecordNotFound {
  3158. AddSigleDrugCancelStock(&cancelStock)
  3159. }
  3160. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  3161. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  3162. dealer, _ := GetDealerById(warehouse.Dealer)
  3163. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  3164. cancelStockInfo := models.DrugCancelStockInfo{
  3165. OrderNumber: cancelInfo.OrderNumber,
  3166. CancelStockId: lastDrugCancelStock.ID,
  3167. DrugId: advice.DrugId,
  3168. Count: (out_count - cancel_count) - deliver_number,
  3169. Status: 1,
  3170. Ctime: ctime,
  3171. OrgId: advice.UserOrgId,
  3172. Type: 5,
  3173. Manufacturer: manufactureName.ManufacturerName,
  3174. Dealer: dealer.DealerName,
  3175. Total: 0,
  3176. RetailPrice: warehouse.RetailPrice,
  3177. RetailTotalPrice: warehouse.RetailTotalPrice,
  3178. Price: warehouse.Price,
  3179. RegisterAccount: "",
  3180. Remark: warehouse.Remark,
  3181. BatchNumber: warehouse.BatchNumber,
  3182. MaxUnit: drup.MinUnit,
  3183. ProductDate: warehouse.ProductDate,
  3184. ExpiryDate: warehouse.ExpiryDate,
  3185. BatchNumberId: warehouse.ID,
  3186. StorehouseId: storeConfig.DrugStorehouseOut,
  3187. IsCheck: 1,
  3188. OrderId: warehouseout.OrderId,
  3189. }
  3190. CreatedCancelStock(cancelStockInfo)
  3191. flow := models.DrugFlow{
  3192. WarehousingId: warehouse.ID,
  3193. DrugId: warehouse.DrugId,
  3194. Number: "",
  3195. BatchNumber: warehouse.BatchNumber,
  3196. Count: out_count - cancel_count - deliver_number,
  3197. UserOrgId: advice.UserOrgId,
  3198. PatientId: advice.PatientId,
  3199. SystemTime: advice.AdviceDate,
  3200. ConsumableType: 7,
  3201. IsSys: 0,
  3202. WarehousingOrder: "",
  3203. WarehouseOutId: 0,
  3204. WarehouseOutOrderNumber: "",
  3205. IsEdit: 0,
  3206. CancelStockId: cancelStock.ID,
  3207. CancelOrderNumber: lastDrugCancelStock.OrderNumber,
  3208. Manufacturer: warehouse.Manufacturer,
  3209. Dealer: warehouse.Dealer,
  3210. Creator: advice.ExecutionStaff,
  3211. UpdateCreator: 0,
  3212. Status: 1,
  3213. Ctime: time.Now().Unix(),
  3214. Mtime: 0,
  3215. Price: warehouse.RetailPrice,
  3216. WarehousingDetailId: warehouse.ID,
  3217. WarehouseOutDetailId: 0,
  3218. CancelOutDetailId: lastDrugCancelStock.ID,
  3219. ExpireDate: warehouse.ExpiryDate,
  3220. ProductDate: warehouse.ProductDate,
  3221. MaxUnit: drup.MinUnit,
  3222. MinUnit: "",
  3223. AdviceId: advice.ID,
  3224. StorehouseId: storeConfig.DrugStorehouseOut,
  3225. LastPrice: warehouse.Price,
  3226. OverCount: sum_count,
  3227. OrderId: warehouseout.OrderId,
  3228. }
  3229. CreateDrugFlowOne(flow)
  3230. //退库数量增加
  3231. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  3232. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  3233. }
  3234. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3235. prescribingNumber_two_temp := deliver_number - stock_number
  3236. overPlusNumber := float64(prescribingNumber_two_temp)
  3237. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  3238. advice.PrescribingNumberUnit = drup.MinUnit
  3239. fmt.Println("剩余库存9999999233232", overPlusNumber)
  3240. HisSettleDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice, deliver_number_one)
  3241. }
  3242. return
  3243. }
  3244. func UpdateDrugWarehouseOutInfoTen(patient_id int64, sys_record_time int64, advice_id int64) error {
  3245. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("patient_id = ? and sys_record_time = ? and status=1 and advice_id = ?", patient_id, sys_record_time, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  3246. return err
  3247. }
  3248. func UpdateNewDrugWarehouseOutInfoTen(patient_id int64, sys_record_time int64, advice_id int64, tx *gorm.DB) error {
  3249. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("patient_id = ? and sys_record_time = ? and status=1 and advice_id = ?", patient_id, sys_record_time, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  3250. if err != nil {
  3251. tx.Rollback()
  3252. return err
  3253. }
  3254. return err
  3255. }
  3256. func UpdateBasePrice(id int64, min_price float64) error {
  3257. err := XTWriteDB().Model(models.XtBaseDrug{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"min_price": min_price}).Error
  3258. return err
  3259. }
  3260. func UpdateNewBasePrice(id int64, min_price float64, tx *gorm.DB) error {
  3261. err := tx.Model(models.XtBaseDrug{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"min_price": min_price}).Error
  3262. if err != nil {
  3263. tx.Rollback()
  3264. return err
  3265. }
  3266. return err
  3267. }
  3268. func UpdateGoodInfoByPrice(id int64, org_id int64, price float64, packingprice float64) error {
  3269. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id=? and org_id = ? and status =1", id, org_id).Updates(map[string]interface{}{"buy_price": price, "packing_price": packingprice}).Error
  3270. return err
  3271. }
  3272. func NewBloodHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo, hisprescribingNumber float64, unit string, tx *gorm.DB) (err error) {
  3273. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  3274. var deliver_number int64 = 0
  3275. var deliver_number_one int64 = 0
  3276. var stock_number int64 = 0
  3277. //fmt.Println(deliver_number)
  3278. //fmt.Println(deliver_number_one)
  3279. //fmt.Println(stock_number)
  3280. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  3281. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  3282. prescribingNumber_temp_one := strconv.FormatFloat(math.Abs(hisprescribingNumber), 'f', 0, 64)
  3283. countone, _ := strconv.ParseInt(prescribingNumber_temp_one, 10, 64)
  3284. var drug_price float64
  3285. if advice.PrescribingNumberUnit == drup.MaxUnit {
  3286. deliver_number = count * drup.MinNumber
  3287. } else {
  3288. deliver_number = count
  3289. }
  3290. if unit == drup.MaxUnit {
  3291. deliver_number_one = countone * drup.MinNumber
  3292. } else {
  3293. deliver_number_one = countone
  3294. }
  3295. if advice.PrescribingNumberUnit == drup.MaxUnit {
  3296. drug_price = drup.RetailPrice
  3297. }
  3298. fmt.Println(drug_price)
  3299. if advice.PrescribingNumberUnit == drup.MinUnit {
  3300. drug_price = drup.MinPrice
  3301. }
  3302. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  3303. drug_price = drup.RetailPrice
  3304. }
  3305. // 根据先进先出原则,查询最先入库的批次,进行出库
  3306. // 如果没有对应的库存,则报错
  3307. storeConfig, _ := GetNewAllStoreHouseConfig(advice.UserOrgId, tx)
  3308. lastWarehouse, _ := FindNewLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut, tx)
  3309. if lastWarehouse.MaxUnit != lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  3310. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  3311. var stockMax int64
  3312. var stockMin int64
  3313. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  3314. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  3315. ChangeNewMaxNumber(lastWarehouse.ID, stockMax, tx)
  3316. UpdateNewMinNumber(lastWarehouse.ID, stockMin, tx)
  3317. }
  3318. }
  3319. if lastWarehouse.MaxUnit == lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  3320. lastWarehouse.StockMaxNumber = lastWarehouse.StockMaxNumber + lastWarehouse.StockMinNumber
  3321. ChangeNewMaxNumberOne(lastWarehouse.ID, lastWarehouse.StockMaxNumber, tx)
  3322. }
  3323. warehouse, _ := FindNewLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut, tx)
  3324. // 将该批次的剩余库存数量转换为拆零数量
  3325. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  3326. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  3327. }
  3328. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  3329. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  3330. }
  3331. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  3332. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  3333. }
  3334. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3335. if stock_number >= deliver_number {
  3336. var maxNumber int64 = 0
  3337. var minNumber int64 = 0
  3338. maxNumber = deliver_number / drup.MinNumber
  3339. minNumber = deliver_number % drup.MinNumber
  3340. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  3341. minNumber = maxNumber
  3342. }
  3343. if drup.MaxUnit != drup.MinUnit {
  3344. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  3345. return errors.New("库存数量不足")
  3346. }
  3347. }
  3348. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  3349. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  3350. }
  3351. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  3352. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  3353. }
  3354. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  3355. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  3356. }
  3357. if warehouse.StockMaxNumber < 0 {
  3358. warehouse.StockMaxNumber = 0
  3359. }
  3360. if warehouse.StockMinNumber < 0 {
  3361. warehouse.StockMinNumber = 0
  3362. }
  3363. warehouse.Mtime = time.Now().Unix()
  3364. if warehouse.StockMinNumber < minNumber {
  3365. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  3366. if warehouse.MaxUnit != warehouse.MinUnit {
  3367. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  3368. }
  3369. if minNumber == 2 && warehouse.MaxUnit == warehouse.MinUnit && warehouse.StockMinNumber == 0 {
  3370. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  3371. }
  3372. } else {
  3373. if minNumber > 0 {
  3374. if (warehouse.StockMinNumber - minNumber) >= 0 {
  3375. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  3376. }
  3377. }
  3378. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  3379. if warehouse.StockMinNumber > 0 {
  3380. if warehouse.StockMinNumber-deliver_number >= 0 {
  3381. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  3382. }
  3383. }
  3384. }
  3385. }
  3386. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit && warehouse.MaxUnit != warehouse.MinUnit {
  3387. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  3388. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  3389. }
  3390. }
  3391. if drup.MaxUnit != drup.MinUnit {
  3392. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  3393. return errors.New("库存数量不足")
  3394. }
  3395. }
  3396. if warehouse.StockMinNumber <= 0 {
  3397. warehouse.StockMinNumber = 0
  3398. }
  3399. //扣减库存数据
  3400. UpDateNewDrugWarehouseInfoByStock(&warehouse, tx)
  3401. ////查询剩余库存
  3402. stockInfo, _ := GetNewDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId, tx)
  3403. var sum_count int64
  3404. for _, its := range stockInfo {
  3405. if its.MaxUnit == drup.MaxUnit {
  3406. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  3407. }
  3408. sum_count += its.StockMaxNumber + its.StockMinNumber
  3409. }
  3410. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  3411. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3412. WarehouseOutId: warehouseout.ID,
  3413. Status: 1,
  3414. Ctime: advice.RecordDate,
  3415. Mtime: time.Now().Unix(),
  3416. Remark: warehouse.Remark,
  3417. OrgId: orgID,
  3418. Type: 1,
  3419. Manufacturer: warehouse.Manufacturer,
  3420. Dealer: warehouse.Dealer,
  3421. IsSys: 1,
  3422. SysRecordTime: advice.RecordDate,
  3423. DrugId: advice.DrugId,
  3424. Number: warehouse.Number,
  3425. BatchNumber: warehouse.BatchNumber,
  3426. Price: warehouse.RetailPrice,
  3427. CountUnit: drup.MinUnit,
  3428. RetailPrice: warehouse.RetailPrice,
  3429. ProductDate: warehouse.ProductDate,
  3430. ExpiryDate: warehouse.ExpiryDate,
  3431. PatientId: advice.PatientId,
  3432. Count: deliver_number, //出最小单位
  3433. WarehouseInfoId: warehouse.ID,
  3434. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3435. StorehouseId: storeConfig.DrugStorehouseOut,
  3436. IsCheck: 1,
  3437. OverCount: sum_count,
  3438. AdviceId: advice.ID,
  3439. }
  3440. //查询今日该药品该患者是否有出库数据
  3441. lastDrugOutInfo, _ := GetNewNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID, tx)
  3442. if lastDrugOutInfo.ID == 0 {
  3443. //删除在新增
  3444. UpdateNewDrugWarehouseOutInfoTen(lastDrugOutInfo.PatientId, lastDrugOutInfo.SysRecordTime, advice.ID, tx)
  3445. AddNewSigleDrugWarehouseOutInfo(warehouseOutInfo, tx)
  3446. }
  3447. if lastDrugOutInfo.ID > 0 {
  3448. //删除在新增
  3449. UpdateNewDrugWarehouseOutInfoTen(lastDrugOutInfo.PatientId, lastDrugOutInfo.SysRecordTime, advice.ID, tx)
  3450. AddNewSigleDrugWarehouseOutInfo(warehouseOutInfo, tx)
  3451. }
  3452. //
  3453. var out_count int64
  3454. var cancel_count int64
  3455. //查询出库数据
  3456. infoCountList, _ := FindNewDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, tx)
  3457. //查询退库数据
  3458. cancelInfoList, _ := FindNewDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, tx)
  3459. for _, item := range infoCountList {
  3460. out_count += item.Count
  3461. }
  3462. for _, item := range cancelInfoList {
  3463. cancel_count += item.Count
  3464. }
  3465. fmt.Println("已经出库数据", out_count)
  3466. fmt.Println("已经退库数据", cancel_count)
  3467. fmt.Println("当前正在出库的数据", deliver_number_one)
  3468. fmt.Println("差", deliver_number_one-out_count-cancel_count)
  3469. //如果本次出库数据大于历史出库数据 新增1条退库流水
  3470. if deliver_number_one-(out_count-cancel_count) >= 0 {
  3471. if (deliver_number_one - out_count - cancel_count) != 0 {
  3472. lastDrugOutInfo, _ := GetNewNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID, tx)
  3473. drugflow := models.DrugFlow{
  3474. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3475. WarehouseOutId: warehouseout.ID,
  3476. DrugId: advice.DrugId,
  3477. Number: warehouse.Number,
  3478. ProductDate: warehouse.ProductDate,
  3479. ExpireDate: warehouse.ExpiryDate,
  3480. Count: deliver_number_one - (out_count - cancel_count), //按最小单位计算,
  3481. Price: warehouse.RetailPrice,
  3482. Status: 1,
  3483. Ctime: advice.AdviceDate,
  3484. Mtime: time.Now().Unix(),
  3485. UserOrgId: orgID,
  3486. Manufacturer: warehouse.Manufacturer,
  3487. Dealer: warehouse.Dealer,
  3488. BatchNumber: warehouse.BatchNumber,
  3489. MaxUnit: drup.MinUnit,
  3490. ConsumableType: 3,
  3491. IsEdit: 1,
  3492. Creator: advice.ExecutionStaff,
  3493. IsSys: 1,
  3494. PatientId: advice.PatientId,
  3495. SystemTime: advice.AdviceDate,
  3496. WarehousingDetailId: warehouse.ID,
  3497. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3498. StorehouseId: storeConfig.DrugStorehouseOut,
  3499. WarehouseOutDetailId: lastDrugOutInfo.ID,
  3500. OverCount: sum_count,
  3501. AdviceId: advice.ID,
  3502. LastPrice: warehouse.Price,
  3503. }
  3504. CreateNewDrugFlowOne(drugflow, tx)
  3505. AddNewDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count, tx)
  3506. }
  3507. }
  3508. //如果本次出库数据小于历史出库数据 新增1条退库流水
  3509. if deliver_number_one-(out_count-cancel_count) < 0 {
  3510. operation_time := time.Now().Unix()
  3511. ctime := time.Now().Unix()
  3512. timeStr := time.Now().Format("2006-01-02")
  3513. timeArr := strings.Split(timeStr, "-")
  3514. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  3515. total = total + 1
  3516. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  3517. cancelStock := models.DrugCancelStock{
  3518. OrderNumber: orderNumber,
  3519. OperaTime: operation_time,
  3520. OrgId: advice.UserOrgId,
  3521. Creater: advice.ExecutionStaff,
  3522. Ctime: ctime,
  3523. Status: 1,
  3524. ReturnTime: advice.AdviceDate,
  3525. Dealer: 0,
  3526. Manufacturer: 0,
  3527. Type: 1,
  3528. StorehouseId: storeConfig.DrugStorehouseOut,
  3529. IsCheck: 1,
  3530. }
  3531. _, errCode := FindNewDrugCancelStock(advice.AdviceDate, advice.UserOrgId, tx)
  3532. if errCode == gorm.ErrRecordNotFound {
  3533. AddNewSigleDrugCancelStock(&cancelStock, tx)
  3534. }
  3535. lastDrugCancelStock, _ := FindNewLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId, tx)
  3536. manufactureName, _ := GetNewManufactureById(warehouse.Manufacturer, tx)
  3537. dealer, _ := GetNewDealerById(warehouse.Dealer, tx)
  3538. cancelInfo, _ := GetNewLastDrugCancelStockById(advice.UserOrgId, tx)
  3539. cancelStockInfo := models.DrugCancelStockInfo{
  3540. OrderNumber: cancelInfo.OrderNumber,
  3541. CancelStockId: lastDrugCancelStock.ID,
  3542. DrugId: advice.DrugId,
  3543. Count: (out_count - cancel_count) - deliver_number,
  3544. Status: 1,
  3545. Ctime: advice.AdviceDate,
  3546. Mtime: time.Now().Unix(),
  3547. OrgId: advice.UserOrgId,
  3548. Type: 1,
  3549. Manufacturer: manufactureName.ManufacturerName,
  3550. Dealer: dealer.DealerName,
  3551. Total: 0,
  3552. RetailPrice: warehouse.RetailPrice,
  3553. RetailTotalPrice: warehouse.RetailTotalPrice,
  3554. Price: warehouse.Price,
  3555. RegisterAccount: "",
  3556. Remark: warehouse.Remark,
  3557. BatchNumber: warehouse.BatchNumber,
  3558. MaxUnit: drup.MinUnit,
  3559. ProductDate: warehouse.ProductDate,
  3560. ExpiryDate: warehouse.ExpiryDate,
  3561. BatchNumberId: warehouse.ID,
  3562. StorehouseId: storeConfig.DrugStorehouseOut,
  3563. IsCheck: 1,
  3564. }
  3565. CreatedNewCancelStock(cancelStockInfo, tx)
  3566. flow := models.DrugFlow{
  3567. WarehousingId: warehouse.ID,
  3568. DrugId: warehouse.DrugId,
  3569. Number: "",
  3570. BatchNumber: warehouse.BatchNumber,
  3571. Count: out_count - cancel_count - deliver_number,
  3572. UserOrgId: advice.UserOrgId,
  3573. PatientId: advice.PatientId,
  3574. SystemTime: advice.AdviceDate,
  3575. ConsumableType: 7,
  3576. IsSys: 0,
  3577. WarehousingOrder: "",
  3578. WarehouseOutId: 0,
  3579. WarehouseOutOrderNumber: "",
  3580. IsEdit: 0,
  3581. CancelStockId: cancelStock.ID,
  3582. CancelOrderNumber: cancelStock.OrderNumber,
  3583. Manufacturer: warehouse.Manufacturer,
  3584. Dealer: warehouse.Dealer,
  3585. Creator: advice.ExecutionStaff,
  3586. UpdateCreator: 0,
  3587. Status: 1,
  3588. Ctime: advice.AdviceDate,
  3589. Mtime: time.Now().Unix(),
  3590. Price: warehouse.RetailPrice,
  3591. WarehousingDetailId: warehouse.ID,
  3592. WarehouseOutDetailId: 0,
  3593. CancelOutDetailId: lastDrugCancelStock.ID,
  3594. ExpireDate: warehouse.ExpiryDate,
  3595. ProductDate: warehouse.ProductDate,
  3596. MaxUnit: drup.MinUnit,
  3597. MinUnit: "",
  3598. AdviceId: advice.ID,
  3599. StorehouseId: storeConfig.DrugStorehouseOut,
  3600. LastPrice: warehouse.Price,
  3601. OverCount: sum_count,
  3602. }
  3603. CreateNewDrugFlowOne(flow, tx)
  3604. AddNewCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count, tx)
  3605. ReduceNewDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count, tx)
  3606. }
  3607. //查询是否存在数据
  3608. details := &models.DrugAutomaticReduceDetail{
  3609. WarehouseOutId: warehouseout.ID,
  3610. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3611. PatientId: advice.PatientId,
  3612. Ctime: advice.RecordDate,
  3613. Mtime: time.Now().Unix(),
  3614. Status: 1,
  3615. RecordTime: advice.RecordDate,
  3616. OrgId: orgID,
  3617. DrugId: advice.DrugId,
  3618. Count: deliver_number,
  3619. CountUnit: drup.MinUnit,
  3620. WarehouseInfoId: warehouse.ID,
  3621. StorehouseId: storeConfig.DrugStorehouseOut,
  3622. AdviceId: advice.ID,
  3623. }
  3624. AddNewSigleDrugAutoReduceRecordInfo(details, tx)
  3625. return nil
  3626. } else {
  3627. info := models.XtDrugWarehouseInfo{
  3628. ID: warehouse.ID,
  3629. WarehousingId: warehouse.WarehousingId,
  3630. DrugId: warehouse.DrugId,
  3631. Number: warehouse.Number,
  3632. ProductDate: warehouse.ProductDate,
  3633. ExpiryDate: warehouse.ExpiryDate,
  3634. WarehousingCount: warehouse.WarehousingCount,
  3635. Price: warehouse.RetailPrice,
  3636. TotalPrice: warehouse.TotalPrice,
  3637. Dealer: warehouse.Dealer,
  3638. Manufacturer: warehouse.Manufacturer,
  3639. Remark: warehouse.Remark,
  3640. Ctime: warehouse.Ctime,
  3641. Mtime: warehouse.Mtime,
  3642. Status: 1,
  3643. OrgId: warehouse.OrgId,
  3644. IsReturn: warehouse.IsReturn,
  3645. WarehousingOrder: warehouse.WarehousingOrder,
  3646. Type: warehouse.Type,
  3647. RetailPrice: warehouse.RetailPrice,
  3648. RetailTotalPrice: warehouse.RetailPrice,
  3649. StockMaxNumber: 0,
  3650. StockMinNumber: 0,
  3651. BatchNumber: warehouse.BatchNumber,
  3652. MaxUnit: warehouse.MaxUnit,
  3653. MinUnit: drup.MinUnit,
  3654. WarehousingInfoId: warehouse.WarehousingInfoId,
  3655. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3656. StorehouseId: storeConfig.DrugStorehouseOut,
  3657. IsCheck: 1,
  3658. }
  3659. //扣减库存
  3660. UpDateNewDrugWarehouseInfoByStock(&info, tx)
  3661. //查询剩余库存
  3662. stockInfo, _ := GetNewDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId, tx)
  3663. var sum_count int64
  3664. for _, its := range stockInfo {
  3665. if its.MaxUnit == drup.MaxUnit {
  3666. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  3667. }
  3668. sum_count += its.StockMaxNumber + its.StockMinNumber
  3669. }
  3670. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  3671. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  3672. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3673. WarehouseOutId: warehouseout.ID,
  3674. Status: 1,
  3675. Ctime: advice.RecordDate,
  3676. Mtime: time.Now().Unix(),
  3677. Remark: warehouse.Remark,
  3678. OrgId: orgID,
  3679. Type: 1,
  3680. Manufacturer: warehouse.Manufacturer,
  3681. Dealer: warehouse.Dealer,
  3682. IsSys: 1,
  3683. SysRecordTime: advice.RecordDate,
  3684. DrugId: advice.DrugId,
  3685. Number: warehouse.Number,
  3686. BatchNumber: warehouse.BatchNumber,
  3687. Price: warehouse.RetailPrice,
  3688. CountUnit: drup.MinUnit,
  3689. ProductDate: warehouse.ProductDate,
  3690. ExpiryDate: warehouse.ExpiryDate,
  3691. PatientId: advice.PatientId,
  3692. Count: stock_number,
  3693. WarehouseInfoId: warehouse.ID,
  3694. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3695. StorehouseId: storeConfig.DrugStorehouseOut,
  3696. IsCheck: 1,
  3697. OverCount: sum_count,
  3698. AdviceId: advice.ID,
  3699. }
  3700. //查询该药品是否存在出库数据
  3701. _, errCode := GetNewSigleDrugWarehouseOutInfoTwo(advice.PatientId, advice.AdviceDate, orgID, advice.DrugId, tx)
  3702. if errCode == nil {
  3703. lastDrugOutInfo, _ := GetNewSigleDrugWarehouseOutInfoTwo(advice.PatientId, advice.AdviceDate, orgID, advice.DrugId, tx)
  3704. var his_count int64
  3705. if lastDrugOutInfo.CountUnit == drup.MaxUnit {
  3706. his_count = lastDrugOutInfo.Count * drup.MinNumber
  3707. }
  3708. if lastDrugOutInfo.CountUnit == drup.MinUnit {
  3709. his_count = lastDrugOutInfo.Count
  3710. }
  3711. if deliver_number != his_count {
  3712. UpdateNewSingleDrugWarehouseOutInfoSix(advice.PatientId, advice.AdviceDate, orgID, warehouseOutInfo, advice.ID, tx)
  3713. }
  3714. }
  3715. lastDrugOutInfo, _ := FindNewLastDrugWarehouseOutInfo(orgID, tx)
  3716. details := &models.DrugAutomaticReduceDetail{
  3717. WarehouseOutId: warehouseout.ID,
  3718. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  3719. PatientId: advice.PatientId,
  3720. Ctime: advice.AdviceDate,
  3721. Mtime: time.Now().Unix(),
  3722. Status: 1,
  3723. RecordTime: advice.RecordDate,
  3724. OrgId: orgID,
  3725. DrugId: advice.DrugId,
  3726. Count: stock_number,
  3727. CountUnit: drup.MinUnit,
  3728. WarehouseInfoId: warehouse.ID,
  3729. StorehouseId: storeConfig.DrugStorehouseOut,
  3730. AdviceId: advice.ID,
  3731. }
  3732. _, errcodeThree := FindNewDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId, tx)
  3733. if errcodeThree == nil {
  3734. UpdateNewDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId, details, tx)
  3735. }
  3736. var out_count int64
  3737. var cancel_count int64
  3738. //查询出库数据
  3739. infoCountList, _ := FindNewDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, tx)
  3740. //查询退库数据
  3741. cancelInfoList, _ := FindNewDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, tx)
  3742. for _, item := range infoCountList {
  3743. out_count += item.Count
  3744. }
  3745. for _, item := range cancelInfoList {
  3746. cancel_count += item.Count
  3747. }
  3748. //如果本次出库数据大于历史出库数据 新增1条流水
  3749. if deliver_number > (out_count - cancel_count) {
  3750. drugflow := models.DrugFlow{
  3751. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  3752. WarehouseOutId: warehouseout.ID,
  3753. DrugId: advice.DrugId,
  3754. Number: warehouse.Number,
  3755. ProductDate: warehouse.ProductDate,
  3756. ExpireDate: warehouse.ExpiryDate,
  3757. Count: stock_number,
  3758. Price: warehouse.RetailPrice,
  3759. Status: 1,
  3760. Ctime: advice.AdviceDate,
  3761. Mtime: time.Now().Unix(),
  3762. UserOrgId: orgID,
  3763. Manufacturer: warehouse.Manufacturer,
  3764. Dealer: warehouse.Dealer,
  3765. BatchNumber: warehouse.BatchNumber,
  3766. MaxUnit: drup.MinUnit,
  3767. ConsumableType: 3,
  3768. IsEdit: 1,
  3769. Creator: advice.ExecutionStaff,
  3770. IsSys: 1,
  3771. PatientId: advice.PatientId,
  3772. WarehousingDetailId: warehouse.ID,
  3773. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3774. StorehouseId: storeConfig.DrugStorehouseOut,
  3775. WarehouseOutDetailId: lastDrugOutInfo.ID,
  3776. OverCount: sum_count,
  3777. AdviceId: advice.ID,
  3778. LastPrice: warehouse.Price,
  3779. SystemTime: advice.AdviceDate,
  3780. }
  3781. CreateNewDrugFlowOne(drugflow, tx)
  3782. //出库数量相加
  3783. AddNewDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count, tx)
  3784. }
  3785. if deliver_number < (out_count - cancel_count) {
  3786. operation_time := time.Now().Unix()
  3787. ctime := time.Now().Unix()
  3788. timeStr := time.Now().Format("2006-01-02")
  3789. timeArr := strings.Split(timeStr, "-")
  3790. total, _ := FindNewAllDrugCancelStockTotal(advice.UserOrgId, tx)
  3791. total = total + 1
  3792. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  3793. cancelStock := models.DrugCancelStock{
  3794. OrderNumber: orderNumber,
  3795. OperaTime: operation_time,
  3796. OrgId: advice.UserOrgId,
  3797. Creater: advice.ExecutionStaff,
  3798. Ctime: ctime,
  3799. Status: 1,
  3800. ReturnTime: advice.AdviceDate,
  3801. Dealer: 0,
  3802. Manufacturer: 0,
  3803. Type: 1,
  3804. StorehouseId: storeConfig.DrugStorehouseOut,
  3805. IsCheck: 1,
  3806. }
  3807. _, errCode := FindNewDrugCancelStock(advice.AdviceDate, advice.UserOrgId, tx)
  3808. if errCode == gorm.ErrRecordNotFound {
  3809. AddNewSigleDrugCancelStock(&cancelStock, tx)
  3810. }
  3811. lastDrugCancelStock, _ := FindNewLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId, tx)
  3812. manufactureName, _ := GetNewManufactureById(warehouse.Manufacturer, tx)
  3813. dealer, _ := GetNewDealerById(warehouse.Dealer, tx)
  3814. cancelInfo, _ := GetNewLastDrugCancelStockById(advice.UserOrgId, tx)
  3815. cancelStockInfo := models.DrugCancelStockInfo{
  3816. OrderNumber: cancelInfo.OrderNumber,
  3817. CancelStockId: lastDrugCancelStock.ID,
  3818. DrugId: advice.DrugId,
  3819. Count: (out_count - cancel_count) - deliver_number,
  3820. Status: 1,
  3821. Ctime: advice.AdviceDate,
  3822. Mtime: time.Now().Unix(),
  3823. OrgId: advice.UserOrgId,
  3824. Type: 1,
  3825. Manufacturer: manufactureName.ManufacturerName,
  3826. Dealer: dealer.DealerName,
  3827. Total: 0,
  3828. RetailPrice: warehouse.RetailPrice,
  3829. RetailTotalPrice: warehouse.RetailTotalPrice,
  3830. Price: warehouse.Price,
  3831. RegisterAccount: "",
  3832. Remark: warehouse.Remark,
  3833. BatchNumber: warehouse.BatchNumber,
  3834. MaxUnit: drup.MinUnit,
  3835. ProductDate: warehouse.ProductDate,
  3836. ExpiryDate: warehouse.ExpiryDate,
  3837. BatchNumberId: warehouse.ID,
  3838. StorehouseId: storeConfig.DrugStorehouseOut,
  3839. IsCheck: 1,
  3840. }
  3841. CreatedNewCancelStock(cancelStockInfo, tx)
  3842. flow := models.DrugFlow{
  3843. WarehousingId: warehouse.ID,
  3844. DrugId: warehouse.DrugId,
  3845. Number: "",
  3846. BatchNumber: warehouse.BatchNumber,
  3847. Count: out_count - cancel_count - deliver_number,
  3848. UserOrgId: advice.UserOrgId,
  3849. PatientId: advice.PatientId,
  3850. SystemTime: advice.AdviceDate,
  3851. ConsumableType: 7,
  3852. IsSys: 0,
  3853. WarehousingOrder: "",
  3854. WarehouseOutId: 0,
  3855. WarehouseOutOrderNumber: "",
  3856. IsEdit: 0,
  3857. CancelStockId: cancelStock.ID,
  3858. CancelOrderNumber: cancelStock.OrderNumber,
  3859. Manufacturer: warehouse.Manufacturer,
  3860. Dealer: warehouse.Dealer,
  3861. Creator: advice.ExecutionStaff,
  3862. UpdateCreator: 0,
  3863. Status: 1,
  3864. Ctime: advice.AdviceDate,
  3865. Mtime: time.Now().Unix(),
  3866. Price: warehouse.RetailPrice,
  3867. WarehousingDetailId: warehouse.ID,
  3868. WarehouseOutDetailId: 0,
  3869. CancelOutDetailId: lastDrugCancelStock.ID,
  3870. ExpireDate: warehouse.ExpiryDate,
  3871. ProductDate: warehouse.ProductDate,
  3872. MaxUnit: drup.MinUnit,
  3873. MinUnit: "",
  3874. AdviceId: advice.ID,
  3875. StorehouseId: storeConfig.DrugStorehouseOut,
  3876. LastPrice: warehouse.Price,
  3877. OverCount: sum_count,
  3878. }
  3879. errCodeFlows := CreateNewDrugFlowOne(flow, tx)
  3880. //退库数量增加
  3881. errCodeFlows = AddNewCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count, tx)
  3882. if errCodeFlows != nil {
  3883. drugError := models.XtDrugError{
  3884. UserOrgId: orgID,
  3885. DrugId: advice.DrugId,
  3886. RecordDate: advice.AdviceDate,
  3887. PatientId: advice.PatientId,
  3888. Remark: "第二批次创建自动退库流水表失败",
  3889. Status: 1,
  3890. Ctime: time.Now().Unix(),
  3891. Mtime: 0,
  3892. SumCount: 0,
  3893. Prescribingnumber: advice.PrescribingNumber,
  3894. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  3895. }
  3896. CreateDrugError(drugError)
  3897. }
  3898. errCodereduce := ReduceNewDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count, tx)
  3899. if errCodereduce != nil {
  3900. drugError := models.XtDrugError{
  3901. UserOrgId: orgID,
  3902. DrugId: advice.DrugId,
  3903. RecordDate: advice.AdviceDate,
  3904. PatientId: advice.PatientId,
  3905. Remark: "第二批次减少库存表失败",
  3906. Status: 1,
  3907. Ctime: time.Now().Unix(),
  3908. Mtime: 0,
  3909. SumCount: 0,
  3910. Prescribingnumber: advice.PrescribingNumber,
  3911. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  3912. }
  3913. CreateDrugError(drugError)
  3914. }
  3915. }
  3916. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3917. prescribingNumber_two_temp := deliver_number - stock_number
  3918. overPlusNumber := float64(prescribingNumber_two_temp)
  3919. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  3920. advice.PrescribingNumberUnit = drup.MinUnit
  3921. NewBloodHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice, hisprescribingNumber, unit, tx)
  3922. }
  3923. return
  3924. }
  3925. func HisStoryBloodHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo, hisprescribingNumber float64, unit string, tx *gorm.DB) (err error) {
  3926. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  3927. var deliver_number int64 = 0
  3928. var stock_number int64 = 0
  3929. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  3930. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  3931. if advice.PrescribingNumberUnit == drup.MaxUnit {
  3932. deliver_number = count * drup.MinNumber
  3933. } else {
  3934. deliver_number = count
  3935. }
  3936. // 根据先进先出原则,查询最先入库的批次,进行出库
  3937. // 如果没有对应的库存,则报错
  3938. storeConfig, _ := GetNewAllStoreHouseConfig(advice.UserOrgId, tx)
  3939. lastWarehouse, _ := FindNewLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut, tx)
  3940. if lastWarehouse.MaxUnit != lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  3941. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  3942. var stockMax int64
  3943. var stockMin int64
  3944. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  3945. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  3946. ChangeNewMaxNumber(lastWarehouse.ID, stockMax, tx)
  3947. UpdateNewMinNumber(lastWarehouse.ID, stockMin, tx)
  3948. }
  3949. }
  3950. if lastWarehouse.MaxUnit == lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  3951. lastWarehouse.StockMaxNumber = lastWarehouse.StockMaxNumber + lastWarehouse.StockMinNumber
  3952. ChangeNewMaxNumberOne(lastWarehouse.ID, lastWarehouse.StockMaxNumber, tx)
  3953. }
  3954. warehouse, _ := FindNewLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut, tx)
  3955. // 将该批次的剩余库存数量转换为拆零数量
  3956. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  3957. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  3958. }
  3959. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  3960. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  3961. }
  3962. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  3963. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  3964. }
  3965. //fmt.Println("stock_number", stock_number)
  3966. //fmt.Println("deliver_number=============", deliver_number)
  3967. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3968. if stock_number >= deliver_number {
  3969. var maxNumber int64 = 0
  3970. var minNumber int64 = 0
  3971. maxNumber = deliver_number / drup.MinNumber
  3972. minNumber = deliver_number % drup.MinNumber
  3973. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  3974. minNumber = maxNumber
  3975. }
  3976. if drup.MaxUnit != drup.MinUnit {
  3977. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  3978. return errors.New("库存数量不足")
  3979. }
  3980. }
  3981. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  3982. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  3983. }
  3984. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  3985. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  3986. }
  3987. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  3988. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  3989. }
  3990. if warehouse.StockMaxNumber < 0 {
  3991. warehouse.StockMaxNumber = 0
  3992. }
  3993. if warehouse.StockMinNumber < 0 {
  3994. warehouse.StockMinNumber = 0
  3995. }
  3996. warehouse.Mtime = time.Now().Unix()
  3997. //fmt.Println("hahhahahah", warehouse.StockMinNumber)
  3998. //fmt.Println("howowowoowow", minNumber)
  3999. if warehouse.StockMinNumber < minNumber {
  4000. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  4001. if warehouse.MaxUnit != warehouse.MinUnit {
  4002. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  4003. }
  4004. if minNumber == 2 && warehouse.MaxUnit == warehouse.MinUnit && warehouse.StockMinNumber == 0 {
  4005. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  4006. }
  4007. } else {
  4008. if minNumber > 0 {
  4009. if (warehouse.StockMinNumber - minNumber) >= 0 {
  4010. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  4011. }
  4012. }
  4013. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  4014. if warehouse.StockMinNumber > 0 {
  4015. if warehouse.StockMinNumber-deliver_number >= 0 {
  4016. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4017. }
  4018. }
  4019. }
  4020. }
  4021. //fmt.Println("maxNumber", maxNumber)
  4022. //fmt.Println("minNumber", maxNumber)
  4023. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit && warehouse.MaxUnit != warehouse.MinUnit {
  4024. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  4025. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4026. }
  4027. }
  4028. if drup.MaxUnit != drup.MinUnit {
  4029. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  4030. tx.Rollback()
  4031. return errors.New("库存数量不足")
  4032. }
  4033. }
  4034. if warehouse.StockMinNumber <= 0 {
  4035. warehouse.StockMinNumber = 0
  4036. }
  4037. //扣减库存数据
  4038. UpDateNewDrugWarehouseInfoByStock(&warehouse, tx)
  4039. ////查询剩余库存
  4040. stockInfo, _ := GetNewDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId, tx)
  4041. var sum_count int64
  4042. for _, its := range stockInfo {
  4043. if its.MaxUnit == drup.MaxUnit {
  4044. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  4045. }
  4046. sum_count += its.StockMaxNumber + its.StockMinNumber
  4047. }
  4048. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4049. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4050. WarehouseOutId: warehouseout.ID,
  4051. Status: 1,
  4052. Ctime: advice.RecordDate,
  4053. Mtime: time.Now().Unix(),
  4054. Remark: warehouse.Remark,
  4055. OrgId: orgID,
  4056. Type: 1,
  4057. Manufacturer: warehouse.Manufacturer,
  4058. Dealer: warehouse.Dealer,
  4059. IsSys: 1,
  4060. SysRecordTime: advice.RecordDate,
  4061. DrugId: advice.DrugId,
  4062. Number: warehouse.Number,
  4063. BatchNumber: warehouse.BatchNumber,
  4064. Price: warehouse.RetailPrice,
  4065. CountUnit: drup.MinUnit,
  4066. RetailPrice: warehouse.RetailPrice,
  4067. ProductDate: warehouse.ProductDate,
  4068. ExpiryDate: warehouse.ExpiryDate,
  4069. PatientId: advice.PatientId,
  4070. Count: deliver_number, //出最小单位
  4071. WarehouseInfoId: warehouse.ID,
  4072. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4073. StorehouseId: storeConfig.DrugStorehouseOut,
  4074. IsCheck: 1,
  4075. OverCount: sum_count,
  4076. AdviceId: advice.ID,
  4077. }
  4078. //查询今日该药品该患者是否有出库数据
  4079. lastDrugOutInfo, _ := GetNewNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID, tx)
  4080. if lastDrugOutInfo.ID == 0 {
  4081. AddNewSigleDrugWarehouseOutInfo(warehouseOutInfo, tx)
  4082. }
  4083. //如果有数量则新增数量
  4084. if lastDrugOutInfo.ID > 0 {
  4085. UpdateSigleDrugWarehouseOutInfoDetail(lastDrugOutInfo.ID, warehouseOutInfo.Count, tx)
  4086. }
  4087. drugflow := models.DrugFlow{
  4088. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4089. WarehouseOutId: warehouseout.ID,
  4090. DrugId: advice.DrugId,
  4091. Number: warehouse.Number,
  4092. ProductDate: warehouse.ProductDate,
  4093. ExpireDate: warehouse.ExpiryDate,
  4094. Count: deliver_number, //按最小单位计算,
  4095. Price: warehouse.RetailPrice,
  4096. Status: 1,
  4097. Ctime: advice.AdviceDate,
  4098. Mtime: time.Now().Unix(),
  4099. UserOrgId: orgID,
  4100. Manufacturer: warehouse.Manufacturer,
  4101. Dealer: warehouse.Dealer,
  4102. BatchNumber: warehouse.BatchNumber,
  4103. MaxUnit: drup.MinUnit,
  4104. ConsumableType: 3,
  4105. IsEdit: 1,
  4106. Creator: advice.ExecutionStaff,
  4107. IsSys: 1,
  4108. PatientId: advice.PatientId,
  4109. SystemTime: advice.AdviceDate,
  4110. WarehousingDetailId: warehouse.ID,
  4111. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4112. StorehouseId: storeConfig.DrugStorehouseOut,
  4113. WarehouseOutDetailId: lastDrugOutInfo.ID,
  4114. OverCount: sum_count,
  4115. AdviceId: advice.ID,
  4116. LastPrice: warehouse.Price,
  4117. OperateTime: time.Now().Unix(),
  4118. DrugCode: advice.DrugCode,
  4119. }
  4120. CreateNewDrugFlowOne(drugflow, tx)
  4121. AddNewDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count, tx)
  4122. //查询是否存在数据
  4123. details := &models.DrugAutomaticReduceDetail{
  4124. WarehouseOutId: warehouseout.ID,
  4125. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4126. PatientId: advice.PatientId,
  4127. Ctime: advice.RecordDate,
  4128. Mtime: time.Now().Unix(),
  4129. Status: 1,
  4130. RecordTime: advice.RecordDate,
  4131. OrgId: orgID,
  4132. DrugId: advice.DrugId,
  4133. Count: deliver_number,
  4134. CountUnit: drup.MinUnit,
  4135. WarehouseInfoId: warehouse.ID,
  4136. StorehouseId: storeConfig.DrugStorehouseOut,
  4137. AdviceId: advice.ID,
  4138. }
  4139. //查找是否有数据
  4140. autoMaticReduceDetail, _ := GetDrugAutoMaticReduceDetail(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID, tx)
  4141. if autoMaticReduceDetail.ID == 0 {
  4142. AddNewSigleDrugAutoReduceRecordInfo(details, tx)
  4143. }
  4144. if autoMaticReduceDetail.ID > 0 {
  4145. UpdateNewSingleDrugAutoReduceRecordInfo(autoMaticReduceDetail.ID, deliver_number, tx)
  4146. }
  4147. return nil
  4148. } else {
  4149. info := models.XtDrugWarehouseInfo{
  4150. ID: warehouse.ID,
  4151. WarehousingId: warehouse.WarehousingId,
  4152. DrugId: warehouse.DrugId,
  4153. Number: warehouse.Number,
  4154. ProductDate: warehouse.ProductDate,
  4155. ExpiryDate: warehouse.ExpiryDate,
  4156. WarehousingCount: warehouse.WarehousingCount,
  4157. Price: warehouse.RetailPrice,
  4158. TotalPrice: warehouse.TotalPrice,
  4159. Dealer: warehouse.Dealer,
  4160. Manufacturer: warehouse.Manufacturer,
  4161. Remark: warehouse.Remark,
  4162. Ctime: warehouse.Ctime,
  4163. Mtime: warehouse.Mtime,
  4164. Status: 1,
  4165. OrgId: warehouse.OrgId,
  4166. IsReturn: warehouse.IsReturn,
  4167. WarehousingOrder: warehouse.WarehousingOrder,
  4168. Type: warehouse.Type,
  4169. RetailPrice: warehouse.RetailPrice,
  4170. RetailTotalPrice: warehouse.RetailPrice,
  4171. StockMaxNumber: 0,
  4172. StockMinNumber: 0,
  4173. BatchNumber: warehouse.BatchNumber,
  4174. MaxUnit: warehouse.MaxUnit,
  4175. MinUnit: drup.MinUnit,
  4176. WarehousingInfoId: warehouse.WarehousingInfoId,
  4177. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4178. StorehouseId: storeConfig.DrugStorehouseOut,
  4179. IsCheck: 1,
  4180. }
  4181. //扣减库存
  4182. UpDateNewDrugWarehouseInfoByStock(&info, tx)
  4183. //查询剩余库存
  4184. stockInfo, _ := GetNewDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId, tx)
  4185. var sum_count int64
  4186. for _, its := range stockInfo {
  4187. if its.MaxUnit == drup.MaxUnit {
  4188. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  4189. }
  4190. sum_count += its.StockMaxNumber + its.StockMinNumber
  4191. }
  4192. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  4193. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4194. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4195. WarehouseOutId: warehouseout.ID,
  4196. Status: 1,
  4197. Ctime: advice.RecordDate,
  4198. Mtime: time.Now().Unix(),
  4199. Remark: warehouse.Remark,
  4200. OrgId: orgID,
  4201. Type: 1,
  4202. Manufacturer: warehouse.Manufacturer,
  4203. Dealer: warehouse.Dealer,
  4204. IsSys: 1,
  4205. SysRecordTime: advice.RecordDate,
  4206. DrugId: advice.DrugId,
  4207. Number: warehouse.Number,
  4208. BatchNumber: warehouse.BatchNumber,
  4209. Price: warehouse.RetailPrice,
  4210. CountUnit: drup.MinUnit,
  4211. ProductDate: warehouse.ProductDate,
  4212. ExpiryDate: warehouse.ExpiryDate,
  4213. PatientId: advice.PatientId,
  4214. Count: stock_number,
  4215. WarehouseInfoId: warehouse.ID,
  4216. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4217. StorehouseId: storeConfig.DrugStorehouseOut,
  4218. IsCheck: 1,
  4219. OverCount: sum_count,
  4220. AdviceId: advice.ID,
  4221. }
  4222. //查询今日该药品该患者是否有出库数据
  4223. lastDrugOutInfo, _ := GetNewNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID, tx)
  4224. if lastDrugOutInfo.ID == 0 {
  4225. AddNewSigleDrugWarehouseOutInfo(warehouseOutInfo, tx)
  4226. }
  4227. //如果有数量则新增数量
  4228. if lastDrugOutInfo.ID > 0 {
  4229. UpdateSigleDrugWarehouseOutInfoDetail(lastDrugOutInfo.ID, warehouseOutInfo.Count, tx)
  4230. }
  4231. //查询是否存在数据
  4232. details := &models.DrugAutomaticReduceDetail{
  4233. WarehouseOutId: warehouseout.ID,
  4234. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4235. PatientId: advice.PatientId,
  4236. Ctime: advice.RecordDate,
  4237. Mtime: time.Now().Unix(),
  4238. Status: 1,
  4239. RecordTime: advice.RecordDate,
  4240. OrgId: orgID,
  4241. DrugId: advice.DrugId,
  4242. Count: stock_number,
  4243. CountUnit: drup.MinUnit,
  4244. WarehouseInfoId: warehouse.ID,
  4245. StorehouseId: storeConfig.DrugStorehouseOut,
  4246. AdviceId: advice.ID,
  4247. }
  4248. //查找是否有数据
  4249. autoMaticReduceDetail, _ := GetDrugAutoMaticReduceDetail(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID, tx)
  4250. if autoMaticReduceDetail.ID == 0 {
  4251. AddNewSigleDrugAutoReduceRecordInfo(details, tx)
  4252. }
  4253. if autoMaticReduceDetail.ID > 0 {
  4254. UpdateNewSingleDrugAutoReduceRecordInfo(autoMaticReduceDetail.ID, deliver_number, tx)
  4255. }
  4256. drugflow := models.DrugFlow{
  4257. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4258. WarehouseOutId: warehouseout.ID,
  4259. DrugId: advice.DrugId,
  4260. Number: warehouse.Number,
  4261. ProductDate: warehouse.ProductDate,
  4262. ExpireDate: warehouse.ExpiryDate,
  4263. Count: stock_number,
  4264. Price: warehouse.RetailPrice,
  4265. Status: 1,
  4266. Ctime: advice.AdviceDate,
  4267. Mtime: time.Now().Unix(),
  4268. UserOrgId: orgID,
  4269. Manufacturer: warehouse.Manufacturer,
  4270. Dealer: warehouse.Dealer,
  4271. BatchNumber: warehouse.BatchNumber,
  4272. MaxUnit: drup.MinUnit,
  4273. ConsumableType: 3,
  4274. IsEdit: 1,
  4275. Creator: advice.ExecutionStaff,
  4276. IsSys: 1,
  4277. PatientId: advice.PatientId,
  4278. WarehousingDetailId: warehouse.ID,
  4279. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4280. StorehouseId: storeConfig.DrugStorehouseOut,
  4281. WarehouseOutDetailId: lastDrugOutInfo.ID,
  4282. OverCount: sum_count,
  4283. AdviceId: advice.ID,
  4284. LastPrice: warehouse.Price,
  4285. SystemTime: advice.AdviceDate,
  4286. OperateTime: time.Now().Unix(),
  4287. DrugCode: advice.DrugCode,
  4288. }
  4289. CreateNewDrugFlowOne(drugflow, tx)
  4290. //出库数量相加
  4291. AddNewDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count, tx)
  4292. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  4293. prescribingNumber_two_temp := deliver_number - stock_number
  4294. overPlusNumber := float64(prescribingNumber_two_temp)
  4295. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  4296. advice.PrescribingNumberUnit = drup.MinUnit
  4297. HisStoryBloodHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice, hisprescribingNumber, unit, tx)
  4298. }
  4299. return
  4300. }
  4301. func NewHisDrugCancelInfo(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo, cha_count int64, drug models.BaseDrugLibSeven, tx *gorm.DB) (err error) {
  4302. storeConfig, _ := GetNewAllStoreHouseConfig(advice.UserOrgId, tx)
  4303. //查找患者最后一条的退库数据是否满足要求
  4304. var out_count int64
  4305. drugOutInfo, _ := GetDrugWarehouseOutInfoByAdviceIdTwo(advice.PatientId, advice.UserOrgId, advice.AdviceDate, advice.DrugId, advice.ID, tx)
  4306. if drugOutInfo.CountUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  4307. out_count = drugOutInfo.Count * drug.MinNumber
  4308. }
  4309. if drugOutInfo.CountUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  4310. out_count = drugOutInfo.Count
  4311. }
  4312. if drugOutInfo.CountUnit == drug.MinUnit && drug.MaxUnit == drug.MinUnit {
  4313. out_count = drugOutInfo.Count
  4314. }
  4315. //如果最后1条出库数量大于退库数量,则直接退库,满足退库要求
  4316. if out_count >= cha_count {
  4317. //退库 退回 cha_count 的数量
  4318. ModifyNewDrugWarehouseInfoStockMinNumber(cha_count, drugOutInfo.DrugId, drugOutInfo.OrgId, drugOutInfo.WarehouseInfoId, tx)
  4319. if out_count == cha_count {
  4320. //删除该出库单
  4321. DeleteDrugWarehouseOutInfoByNumber(drugOutInfo.ID, tx)
  4322. }
  4323. //减去出库单里面的数量
  4324. ModifyDrugWarehouseOutInfoByNumber(drugOutInfo.ID, cha_count, tx)
  4325. //创建退库单
  4326. //查询剩余库存
  4327. stockInfo, _ := GetNewDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId, tx)
  4328. var sum_count int64
  4329. for _, its := range stockInfo {
  4330. if its.MaxUnit == drug.MaxUnit {
  4331. its.StockMaxNumber = its.StockMaxNumber * drug.MinNumber
  4332. }
  4333. sum_count += its.StockMaxNumber + its.StockMinNumber
  4334. }
  4335. //创建退库单
  4336. operation_time := time.Now().Unix()
  4337. ctime := time.Now().Unix()
  4338. timeStr := time.Now().Format("2006-01-02")
  4339. timeArr := strings.Split(timeStr, "-")
  4340. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  4341. total = total + 1
  4342. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  4343. cancelStock := models.DrugCancelStock{
  4344. OrderNumber: orderNumber,
  4345. OperaTime: operation_time,
  4346. OrgId: advice.UserOrgId,
  4347. Creater: advice.ExecutionStaff,
  4348. Ctime: ctime,
  4349. Status: 1,
  4350. ReturnTime: advice.AdviceDate,
  4351. Dealer: 0,
  4352. Manufacturer: 0,
  4353. Type: 1,
  4354. StorehouseId: storeConfig.DrugStorehouseOut,
  4355. IsCheck: 1,
  4356. }
  4357. _, errCode := FindNewDrugCancelStock(advice.AdviceDate, advice.UserOrgId, tx)
  4358. if errCode == gorm.ErrRecordNotFound {
  4359. AddNewSigleDrugCancelStock(&cancelStock, tx)
  4360. }
  4361. lastDrugCancelStock, _ := FindNewLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId, tx)
  4362. manufactureName, _ := GetNewManufactureById(drugOutInfo.Manufacturer, tx)
  4363. dealer, _ := GetNewDealerById(drugOutInfo.Dealer, tx)
  4364. cancelInfo, _ := GetNewLastDrugCancelStockById(advice.UserOrgId, tx)
  4365. cancelStockInfo := models.DrugCancelStockInfo{
  4366. OrderNumber: cancelInfo.OrderNumber,
  4367. CancelStockId: lastDrugCancelStock.ID,
  4368. DrugId: advice.DrugId,
  4369. Count: cha_count,
  4370. Status: 1,
  4371. Ctime: advice.AdviceDate,
  4372. Mtime: time.Now().Unix(),
  4373. OrgId: advice.UserOrgId,
  4374. Type: 1,
  4375. Manufacturer: manufactureName.ManufacturerName,
  4376. Dealer: dealer.DealerName,
  4377. Total: 0,
  4378. RetailPrice: drugOutInfo.RetailPrice,
  4379. RetailTotalPrice: drugOutInfo.RetailTotalPrice,
  4380. Price: drugOutInfo.Price,
  4381. RegisterAccount: "",
  4382. Remark: drugOutInfo.Remark,
  4383. BatchNumber: drugOutInfo.BatchNumber,
  4384. MaxUnit: drugOutInfo.CountUnit,
  4385. ProductDate: drugOutInfo.ProductDate,
  4386. ExpiryDate: drugOutInfo.ExpiryDate,
  4387. BatchNumberId: drugOutInfo.WarehouseInfoId,
  4388. StorehouseId: storeConfig.DrugStorehouseOut,
  4389. IsCheck: 1,
  4390. }
  4391. CreatedNewCancelStock(cancelStockInfo, tx)
  4392. flow := models.DrugFlow{
  4393. WarehousingId: drugOutInfo.WarehouseInfoId,
  4394. DrugId: drugOutInfo.DrugId,
  4395. Number: "",
  4396. BatchNumber: drugOutInfo.BatchNumber,
  4397. Count: cha_count,
  4398. UserOrgId: advice.UserOrgId,
  4399. PatientId: advice.PatientId,
  4400. SystemTime: advice.AdviceDate,
  4401. ConsumableType: 7,
  4402. IsSys: 0,
  4403. WarehousingOrder: "",
  4404. WarehouseOutId: 0,
  4405. WarehouseOutOrderNumber: "",
  4406. IsEdit: 0,
  4407. CancelStockId: cancelStock.ID,
  4408. CancelOrderNumber: cancelStock.OrderNumber,
  4409. Manufacturer: drugOutInfo.Manufacturer,
  4410. Dealer: drugOutInfo.Dealer,
  4411. Creator: advice.ExecutionStaff,
  4412. UpdateCreator: 0,
  4413. Status: 1,
  4414. Ctime: advice.AdviceDate,
  4415. Mtime: time.Now().Unix(),
  4416. Price: drugOutInfo.RetailPrice,
  4417. WarehousingDetailId: drugOutInfo.ID,
  4418. WarehouseOutDetailId: 0,
  4419. CancelOutDetailId: lastDrugCancelStock.ID,
  4420. ExpireDate: drugOutInfo.ExpiryDate,
  4421. ProductDate: drugOutInfo.ProductDate,
  4422. MaxUnit: drugOutInfo.CountUnit,
  4423. MinUnit: "",
  4424. AdviceId: advice.ID,
  4425. StorehouseId: storeConfig.DrugStorehouseOut,
  4426. LastPrice: drugOutInfo.Price,
  4427. OverCount: sum_count,
  4428. OperateTime: time.Now().Unix(),
  4429. }
  4430. CreateNewDrugFlowOne(flow, tx)
  4431. AddNewCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count, tx)
  4432. ReduceNewDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count, tx)
  4433. }
  4434. //如果出库的数据小于这个退库的数量差,则先该批次出库数量出完
  4435. if out_count < cha_count {
  4436. //把该出库单的数量退完
  4437. ModifyNewDrugWarehouseInfoStockMinNumber(out_count, drugOutInfo.DrugId, drugOutInfo.OrgId, drugOutInfo.WarehouseInfoId, tx)
  4438. //删除该出库单
  4439. DeleteDrugWarehouseOutInfoByNumber(drugOutInfo.ID, tx)
  4440. //创建退库单
  4441. //查询剩余库存
  4442. stockInfo, _ := GetNewDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId, tx)
  4443. var sum_count int64
  4444. for _, its := range stockInfo {
  4445. if its.MaxUnit == drug.MaxUnit {
  4446. its.StockMaxNumber = its.StockMaxNumber * drug.MinNumber
  4447. }
  4448. sum_count += its.StockMaxNumber + its.StockMinNumber
  4449. }
  4450. //创建退库单
  4451. operation_time := time.Now().Unix()
  4452. ctime := time.Now().Unix()
  4453. timeStr := time.Now().Format("2006-01-02")
  4454. timeArr := strings.Split(timeStr, "-")
  4455. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  4456. total = total + 1
  4457. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  4458. cancelStock := models.DrugCancelStock{
  4459. OrderNumber: orderNumber,
  4460. OperaTime: operation_time,
  4461. OrgId: advice.UserOrgId,
  4462. Creater: advice.ExecutionStaff,
  4463. Ctime: ctime,
  4464. Status: 1,
  4465. ReturnTime: advice.AdviceDate,
  4466. Dealer: 0,
  4467. Manufacturer: 0,
  4468. Type: 1,
  4469. StorehouseId: storeConfig.DrugStorehouseOut,
  4470. IsCheck: 1,
  4471. }
  4472. _, errCode := FindNewDrugCancelStock(advice.AdviceDate, advice.UserOrgId, tx)
  4473. if errCode == gorm.ErrRecordNotFound {
  4474. AddNewSigleDrugCancelStock(&cancelStock, tx)
  4475. }
  4476. lastDrugCancelStock, _ := FindNewLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId, tx)
  4477. manufactureName, _ := GetNewManufactureById(drugOutInfo.Manufacturer, tx)
  4478. dealer, _ := GetNewDealerById(drugOutInfo.Dealer, tx)
  4479. cancelInfo, _ := GetNewLastDrugCancelStockById(advice.UserOrgId, tx)
  4480. cancelStockInfo := models.DrugCancelStockInfo{
  4481. OrderNumber: cancelInfo.OrderNumber,
  4482. CancelStockId: lastDrugCancelStock.ID,
  4483. DrugId: advice.DrugId,
  4484. Count: out_count,
  4485. Status: 1,
  4486. Ctime: advice.AdviceDate,
  4487. Mtime: time.Now().Unix(),
  4488. OrgId: advice.UserOrgId,
  4489. Type: 1,
  4490. Manufacturer: manufactureName.ManufacturerName,
  4491. Dealer: dealer.DealerName,
  4492. Total: 0,
  4493. RetailPrice: drugOutInfo.RetailPrice,
  4494. RetailTotalPrice: drugOutInfo.RetailTotalPrice,
  4495. Price: drugOutInfo.Price,
  4496. RegisterAccount: "",
  4497. Remark: drugOutInfo.Remark,
  4498. BatchNumber: drugOutInfo.BatchNumber,
  4499. MaxUnit: drugOutInfo.CountUnit,
  4500. ProductDate: drugOutInfo.ProductDate,
  4501. ExpiryDate: drugOutInfo.ExpiryDate,
  4502. BatchNumberId: drugOutInfo.WarehouseInfoId,
  4503. StorehouseId: storeConfig.DrugStorehouseOut,
  4504. IsCheck: 1,
  4505. }
  4506. CreatedNewCancelStock(cancelStockInfo, tx)
  4507. flow := models.DrugFlow{
  4508. WarehousingId: drugOutInfo.WarehouseInfoId,
  4509. DrugId: drugOutInfo.DrugId,
  4510. Number: "",
  4511. BatchNumber: drugOutInfo.BatchNumber,
  4512. Count: out_count,
  4513. UserOrgId: advice.UserOrgId,
  4514. PatientId: advice.PatientId,
  4515. SystemTime: advice.AdviceDate,
  4516. ConsumableType: 7,
  4517. IsSys: 0,
  4518. WarehousingOrder: "",
  4519. WarehouseOutId: 0,
  4520. WarehouseOutOrderNumber: "",
  4521. IsEdit: 0,
  4522. CancelStockId: cancelStock.ID,
  4523. CancelOrderNumber: cancelStock.OrderNumber,
  4524. Manufacturer: drugOutInfo.Manufacturer,
  4525. Dealer: drugOutInfo.Dealer,
  4526. Creator: advice.ExecutionStaff,
  4527. UpdateCreator: 0,
  4528. Status: 1,
  4529. Ctime: advice.AdviceDate,
  4530. Mtime: time.Now().Unix(),
  4531. Price: drugOutInfo.RetailPrice,
  4532. WarehousingDetailId: drugOutInfo.ID,
  4533. WarehouseOutDetailId: 0,
  4534. CancelOutDetailId: lastDrugCancelStock.ID,
  4535. ExpireDate: drugOutInfo.ExpiryDate,
  4536. ProductDate: drugOutInfo.ProductDate,
  4537. MaxUnit: drugOutInfo.CountUnit,
  4538. MinUnit: "",
  4539. AdviceId: advice.ID,
  4540. StorehouseId: storeConfig.DrugStorehouseOut,
  4541. LastPrice: drugOutInfo.Price,
  4542. OverCount: sum_count,
  4543. OperateTime: time.Now().Unix(),
  4544. }
  4545. CreateNewDrugFlowOne(flow, tx)
  4546. AddNewCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count, tx)
  4547. ReduceNewDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count, tx)
  4548. //计算还有多少没有退库
  4549. var over_count = cha_count - out_count
  4550. NewHisDrugCancelInfo(orgID, creater, advice, over_count, drug, tx)
  4551. }
  4552. return err
  4553. }
  4554. func NewHisDrugAutoCancelInfo(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo, cha_count int64, drug models.BaseDrugLibSeven, tx *gorm.DB) error {
  4555. //查找患者最后一条的退库数据是否满足要求
  4556. var out_count int64
  4557. drugOutInfo, _ := GetDrugAutoOutInfoByAdviceIdTwo(advice.PatientId, advice.UserOrgId, advice.AdviceDate, advice.DrugId, advice.ID, tx)
  4558. if drugOutInfo.CountUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  4559. out_count = drugOutInfo.Count * drug.MinNumber
  4560. }
  4561. if drugOutInfo.CountUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  4562. out_count = drugOutInfo.Count
  4563. }
  4564. if drugOutInfo.CountUnit == drug.MinUnit && drug.MaxUnit == drug.MinUnit {
  4565. out_count = drugOutInfo.Count
  4566. }
  4567. //fmt.Println("out_count++++==++", out_count)
  4568. //fmt.Println("cha_count-------", cha_count)
  4569. //如果最后1条出库数量大于退库数量,则直接退库,满足退库要求
  4570. if out_count >= cha_count {
  4571. //减去出库单里面的数量
  4572. ModifyDrugAuToWarehouseOut(drugOutInfo.ID, cha_count, tx)
  4573. if out_count == cha_count {
  4574. //删除该出库单
  4575. DeleteDrugWarehouseAutoByNumber(drugOutInfo.ID, tx)
  4576. }
  4577. }
  4578. //如果出库的数据小于这个退库的数量差,则先该批次出库数量出完
  4579. if out_count < cha_count {
  4580. //删除该出库单
  4581. DeleteDrugWarehouseAutoByNumber(drugOutInfo.ID, tx)
  4582. //计算还有多少没有退库
  4583. var over_count = cha_count - out_count
  4584. NewHisDrugAutoCancelInfo(orgID, creater, advice, over_count, drug, tx)
  4585. }
  4586. return err
  4587. }
  4588. func ReduceNewGoodCountTwo(good_id int64, org_id int64, storehouse_id int64, out_count int64, tx *gorm.DB) error {
  4589. err := tx.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id = ?", storehouse_id, org_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count - ?", out_count)).Error
  4590. if err != nil {
  4591. tx.Rollback()
  4592. return err
  4593. }
  4594. return err
  4595. }
  4596. func AddDrugWarehouseOut(drug_id int64, total_count int64, storehouse_id int64, user_org_id int64) error {
  4597. ut := XTWriteDB().Begin()
  4598. err = ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", total_count)).Error
  4599. err = ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count + ?", total_count)).Error
  4600. if err != nil {
  4601. ut.Rollback()
  4602. return err
  4603. }
  4604. ut.Commit()
  4605. return err
  4606. }
  4607. func ReduceDrugWarehouseOut(drug_id int64, total_count int64, storehouse_id int64, user_org_id int64) error {
  4608. ut := XTWriteDB().Begin()
  4609. err = ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", total_count)).Error
  4610. err = ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count - ?", total_count)).Error
  4611. if err != nil {
  4612. ut.Rollback()
  4613. return err
  4614. }
  4615. ut.Commit()
  4616. return err
  4617. }
  4618. func HisStoryBloodHisDrugDeliverInfoOne(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo, hisprescribingNumber float64, unit string, tx *gorm.DB) (err error) {
  4619. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  4620. var deliver_number int64 = 0
  4621. var stock_number int64 = 0
  4622. var stock_number_one int64 = 0
  4623. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  4624. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  4625. prescribingNumber_temp_one := strconv.FormatFloat(math.Abs(hisprescribingNumber), 'f', 0, 64)
  4626. count_one, _ := strconv.ParseInt(prescribingNumber_temp_one, 10, 64)
  4627. if advice.PrescribingNumberUnit == drup.MaxUnit {
  4628. deliver_number = count * drup.MinNumber
  4629. } else {
  4630. deliver_number = count
  4631. }
  4632. if advice.PrescribingNumberUnit == drup.MaxUnit {
  4633. stock_number_one = count_one * drup.MinNumber
  4634. } else {
  4635. stock_number_one = count_one
  4636. }
  4637. fmt.Println(stock_number_one)
  4638. // 根据先进先出原则,查询最先入库的批次,进行出库
  4639. // 如果没有对应的库存,则报错
  4640. storeConfig, _ := GetNewAllStoreHouseConfig(advice.UserOrgId, tx)
  4641. lastWarehouse, _ := FindNewLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut, tx)
  4642. if lastWarehouse.MaxUnit != lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  4643. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  4644. var stockMax int64
  4645. var stockMin int64
  4646. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  4647. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  4648. ChangeNewMaxNumber(lastWarehouse.ID, stockMax, tx)
  4649. UpdateNewMinNumber(lastWarehouse.ID, stockMin, tx)
  4650. }
  4651. }
  4652. if lastWarehouse.MaxUnit == lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  4653. lastWarehouse.StockMaxNumber = lastWarehouse.StockMaxNumber + lastWarehouse.StockMinNumber
  4654. ChangeNewMaxNumberOne(lastWarehouse.ID, lastWarehouse.StockMaxNumber, tx)
  4655. }
  4656. warehouse, _ := FindNewLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut, tx)
  4657. // 将该批次的剩余库存数量转换为拆零数量
  4658. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  4659. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  4660. }
  4661. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  4662. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  4663. }
  4664. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  4665. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  4666. }
  4667. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  4668. if stock_number >= deliver_number {
  4669. var maxNumber int64 = 0
  4670. var minNumber int64 = 0
  4671. maxNumber = deliver_number / drup.MinNumber
  4672. minNumber = deliver_number % drup.MinNumber
  4673. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  4674. minNumber = maxNumber
  4675. }
  4676. if drup.MaxUnit != drup.MinUnit {
  4677. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  4678. return errors.New("库存数量不足")
  4679. }
  4680. }
  4681. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  4682. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  4683. }
  4684. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  4685. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  4686. }
  4687. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  4688. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  4689. }
  4690. if warehouse.StockMaxNumber < 0 {
  4691. warehouse.StockMaxNumber = 0
  4692. }
  4693. if warehouse.StockMinNumber < 0 {
  4694. warehouse.StockMinNumber = 0
  4695. }
  4696. warehouse.Mtime = time.Now().Unix()
  4697. //fmt.Println("hahhahahah", warehouse.StockMinNumber)
  4698. //fmt.Println("howowowoowow", minNumber)
  4699. if warehouse.StockMinNumber < minNumber {
  4700. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  4701. if warehouse.MaxUnit != warehouse.MinUnit {
  4702. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  4703. }
  4704. if minNumber == 2 && warehouse.MaxUnit == warehouse.MinUnit && warehouse.StockMinNumber == 0 {
  4705. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  4706. }
  4707. } else {
  4708. if minNumber > 0 {
  4709. if (warehouse.StockMinNumber - minNumber) >= 0 {
  4710. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  4711. }
  4712. }
  4713. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  4714. if warehouse.StockMinNumber > 0 {
  4715. if warehouse.StockMinNumber-deliver_number >= 0 {
  4716. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4717. }
  4718. }
  4719. }
  4720. }
  4721. //fmt.Println("maxNumber", maxNumber)
  4722. //fmt.Println("minNumber", maxNumber)
  4723. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit && warehouse.MaxUnit != warehouse.MinUnit {
  4724. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  4725. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  4726. }
  4727. }
  4728. if drup.MaxUnit != drup.MinUnit {
  4729. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  4730. return errors.New("库存数量不足")
  4731. }
  4732. }
  4733. if warehouse.StockMinNumber <= 0 {
  4734. warehouse.StockMinNumber = 0
  4735. }
  4736. //扣减库存数据
  4737. UpDateNewDrugWarehouseInfoByStock(&warehouse, tx)
  4738. ////查询剩余库存
  4739. stockInfo, _ := GetNewDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId, tx)
  4740. var sum_count int64
  4741. for _, its := range stockInfo {
  4742. if its.MaxUnit == drup.MaxUnit {
  4743. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  4744. }
  4745. sum_count += its.StockMaxNumber + its.StockMinNumber
  4746. }
  4747. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4748. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4749. WarehouseOutId: warehouseout.ID,
  4750. Status: 1,
  4751. Ctime: advice.RecordDate,
  4752. Mtime: time.Now().Unix(),
  4753. Remark: warehouse.Remark,
  4754. OrgId: orgID,
  4755. Type: 1,
  4756. Manufacturer: warehouse.Manufacturer,
  4757. Dealer: warehouse.Dealer,
  4758. IsSys: 1,
  4759. SysRecordTime: advice.RecordDate,
  4760. DrugId: advice.DrugId,
  4761. Number: warehouse.Number,
  4762. BatchNumber: warehouse.BatchNumber,
  4763. Price: warehouse.RetailPrice,
  4764. CountUnit: drup.MinUnit,
  4765. RetailPrice: warehouse.RetailPrice,
  4766. ProductDate: warehouse.ProductDate,
  4767. ExpiryDate: warehouse.ExpiryDate,
  4768. PatientId: advice.PatientId,
  4769. Count: deliver_number, //出最小单位
  4770. WarehouseInfoId: warehouse.ID,
  4771. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4772. StorehouseId: storeConfig.DrugStorehouseOut,
  4773. IsCheck: 1,
  4774. OverCount: sum_count,
  4775. AdviceId: advice.ID,
  4776. }
  4777. //查询今日该药品该患者是否有出库数据
  4778. lastDrugOutInfo, _ := GetNewNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID, tx)
  4779. if lastDrugOutInfo.ID == 0 {
  4780. AddNewSigleDrugWarehouseOutInfo(warehouseOutInfo, tx)
  4781. }
  4782. //如果有数量则新增数量
  4783. if lastDrugOutInfo.ID > 0 {
  4784. UpdateSigleDrugWarehouseOutInfoDetail(lastDrugOutInfo.ID, warehouseOutInfo.Count, tx)
  4785. }
  4786. drugflow := models.DrugFlow{
  4787. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4788. WarehouseOutId: warehouseout.ID,
  4789. DrugId: advice.DrugId,
  4790. Number: warehouse.Number,
  4791. ProductDate: warehouse.ProductDate,
  4792. ExpireDate: warehouse.ExpiryDate,
  4793. Count: deliver_number, //按最小单位计算,
  4794. Price: warehouse.RetailPrice,
  4795. Status: 1,
  4796. Ctime: advice.AdviceDate,
  4797. Mtime: time.Now().Unix(),
  4798. UserOrgId: orgID,
  4799. Manufacturer: warehouse.Manufacturer,
  4800. Dealer: warehouse.Dealer,
  4801. BatchNumber: warehouse.BatchNumber,
  4802. MaxUnit: drup.MinUnit,
  4803. ConsumableType: 3,
  4804. IsEdit: 1,
  4805. Creator: advice.ExecutionStaff,
  4806. IsSys: 1,
  4807. PatientId: advice.PatientId,
  4808. SystemTime: advice.AdviceDate,
  4809. WarehousingDetailId: warehouse.ID,
  4810. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4811. StorehouseId: storeConfig.DrugStorehouseOut,
  4812. WarehouseOutDetailId: lastDrugOutInfo.ID,
  4813. OverCount: sum_count,
  4814. AdviceId: advice.ID,
  4815. LastPrice: warehouse.Price,
  4816. OperateTime: time.Now().Unix(),
  4817. }
  4818. CreateNewDrugFlowOne(drugflow, tx)
  4819. AddNewDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count, tx)
  4820. //查询是否存在数据
  4821. details := &models.DrugAutomaticReduceDetail{
  4822. WarehouseOutId: warehouseout.ID,
  4823. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4824. PatientId: advice.PatientId,
  4825. Ctime: advice.RecordDate,
  4826. Mtime: time.Now().Unix(),
  4827. Status: 1,
  4828. RecordTime: advice.RecordDate,
  4829. OrgId: orgID,
  4830. DrugId: advice.DrugId,
  4831. Count: deliver_number,
  4832. CountUnit: drup.MinUnit,
  4833. WarehouseInfoId: warehouse.ID,
  4834. StorehouseId: storeConfig.DrugStorehouseOut,
  4835. AdviceId: advice.ID,
  4836. }
  4837. //查找是否有数据
  4838. autoMaticReduceDetail, _ := GetDrugAutoMaticReduceDetail(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID, tx)
  4839. if autoMaticReduceDetail.ID == 0 {
  4840. AddNewSigleDrugAutoReduceRecordInfo(details, tx)
  4841. }
  4842. if autoMaticReduceDetail.ID > 0 {
  4843. UpdateNewSingleDrugAutoReduceRecordInfo(autoMaticReduceDetail.ID, deliver_number, tx)
  4844. }
  4845. return nil
  4846. } else {
  4847. info := models.XtDrugWarehouseInfo{
  4848. ID: warehouse.ID,
  4849. WarehousingId: warehouse.WarehousingId,
  4850. DrugId: warehouse.DrugId,
  4851. Number: warehouse.Number,
  4852. ProductDate: warehouse.ProductDate,
  4853. ExpiryDate: warehouse.ExpiryDate,
  4854. WarehousingCount: warehouse.WarehousingCount,
  4855. Price: warehouse.RetailPrice,
  4856. TotalPrice: warehouse.TotalPrice,
  4857. Dealer: warehouse.Dealer,
  4858. Manufacturer: warehouse.Manufacturer,
  4859. Remark: warehouse.Remark,
  4860. Ctime: warehouse.Ctime,
  4861. Mtime: warehouse.Mtime,
  4862. Status: 1,
  4863. OrgId: warehouse.OrgId,
  4864. IsReturn: warehouse.IsReturn,
  4865. WarehousingOrder: warehouse.WarehousingOrder,
  4866. Type: warehouse.Type,
  4867. RetailPrice: warehouse.RetailPrice,
  4868. RetailTotalPrice: warehouse.RetailPrice,
  4869. StockMaxNumber: 0,
  4870. StockMinNumber: 0,
  4871. BatchNumber: warehouse.BatchNumber,
  4872. MaxUnit: warehouse.MaxUnit,
  4873. MinUnit: drup.MinUnit,
  4874. WarehousingInfoId: warehouse.WarehousingInfoId,
  4875. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4876. StorehouseId: storeConfig.DrugStorehouseOut,
  4877. IsCheck: 1,
  4878. }
  4879. //扣减库存
  4880. UpDateNewDrugWarehouseInfoByStock(&info, tx)
  4881. //查询剩余库存
  4882. stockInfo, _ := GetNewDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId, tx)
  4883. var sum_count int64
  4884. for _, its := range stockInfo {
  4885. if its.MaxUnit == drup.MaxUnit {
  4886. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  4887. }
  4888. sum_count += its.StockMaxNumber + its.StockMinNumber
  4889. }
  4890. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  4891. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  4892. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4893. WarehouseOutId: warehouseout.ID,
  4894. Status: 1,
  4895. Ctime: advice.RecordDate,
  4896. Mtime: time.Now().Unix(),
  4897. Remark: warehouse.Remark,
  4898. OrgId: orgID,
  4899. Type: 1,
  4900. Manufacturer: warehouse.Manufacturer,
  4901. Dealer: warehouse.Dealer,
  4902. IsSys: 1,
  4903. SysRecordTime: advice.RecordDate,
  4904. DrugId: advice.DrugId,
  4905. Number: warehouse.Number,
  4906. BatchNumber: warehouse.BatchNumber,
  4907. Price: warehouse.RetailPrice,
  4908. CountUnit: drup.MinUnit,
  4909. ProductDate: warehouse.ProductDate,
  4910. ExpiryDate: warehouse.ExpiryDate,
  4911. PatientId: advice.PatientId,
  4912. Count: stock_number,
  4913. WarehouseInfoId: warehouse.ID,
  4914. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4915. StorehouseId: storeConfig.DrugStorehouseOut,
  4916. IsCheck: 1,
  4917. OverCount: sum_count,
  4918. AdviceId: advice.ID,
  4919. }
  4920. //查询今日该药品该患者是否有出库数据
  4921. lastDrugOutInfo, _ := GetNewNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID, tx)
  4922. if lastDrugOutInfo.ID == 0 {
  4923. AddNewSigleDrugWarehouseOutInfo(warehouseOutInfo, tx)
  4924. }
  4925. //如果有数量则新增数量
  4926. if lastDrugOutInfo.ID > 0 {
  4927. UpdateSigleDrugWarehouseOutInfoDetail(lastDrugOutInfo.ID, warehouseOutInfo.Count, tx)
  4928. }
  4929. //查询是否存在数据
  4930. details := &models.DrugAutomaticReduceDetail{
  4931. WarehouseOutId: warehouseout.ID,
  4932. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4933. PatientId: advice.PatientId,
  4934. Ctime: advice.RecordDate,
  4935. Mtime: time.Now().Unix(),
  4936. Status: 1,
  4937. RecordTime: advice.RecordDate,
  4938. OrgId: orgID,
  4939. DrugId: advice.DrugId,
  4940. Count: stock_number,
  4941. CountUnit: drup.MinUnit,
  4942. WarehouseInfoId: warehouse.ID,
  4943. StorehouseId: storeConfig.DrugStorehouseOut,
  4944. AdviceId: advice.ID,
  4945. }
  4946. //查找是否有数据
  4947. autoMaticReduceDetail, _ := GetDrugAutoMaticReduceDetail(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID, tx)
  4948. if autoMaticReduceDetail.ID == 0 {
  4949. AddNewSigleDrugAutoReduceRecordInfo(details, tx)
  4950. }
  4951. if autoMaticReduceDetail.ID > 0 {
  4952. UpdateNewSingleDrugAutoReduceRecordInfo(autoMaticReduceDetail.ID, deliver_number, tx)
  4953. }
  4954. drugflow := models.DrugFlow{
  4955. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  4956. WarehouseOutId: warehouseout.ID,
  4957. DrugId: advice.DrugId,
  4958. Number: warehouse.Number,
  4959. ProductDate: warehouse.ProductDate,
  4960. ExpireDate: warehouse.ExpiryDate,
  4961. Count: stock_number,
  4962. Price: warehouse.RetailPrice,
  4963. Status: 1,
  4964. Ctime: advice.AdviceDate,
  4965. Mtime: time.Now().Unix(),
  4966. UserOrgId: orgID,
  4967. Manufacturer: warehouse.Manufacturer,
  4968. Dealer: warehouse.Dealer,
  4969. BatchNumber: warehouse.BatchNumber,
  4970. MaxUnit: drup.MinUnit,
  4971. ConsumableType: 3,
  4972. IsEdit: 1,
  4973. Creator: advice.ExecutionStaff,
  4974. IsSys: 1,
  4975. PatientId: advice.PatientId,
  4976. WarehousingDetailId: warehouse.ID,
  4977. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4978. StorehouseId: storeConfig.DrugStorehouseOut,
  4979. WarehouseOutDetailId: lastDrugOutInfo.ID,
  4980. OverCount: sum_count,
  4981. AdviceId: advice.ID,
  4982. LastPrice: warehouse.Price,
  4983. SystemTime: advice.AdviceDate,
  4984. OperateTime: time.Now().Unix(),
  4985. }
  4986. CreateNewDrugFlowOne(drugflow, tx)
  4987. //出库数量相加
  4988. AddNewDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count, tx)
  4989. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  4990. prescribingNumber_two_temp := deliver_number - stock_number
  4991. overPlusNumber := float64(prescribingNumber_two_temp)
  4992. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  4993. advice.PrescribingNumberUnit = drup.MinUnit
  4994. HisStoryBloodHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice, hisprescribingNumber, unit, tx)
  4995. }
  4996. return
  4997. }