new_warehouse_service.go 215KB

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