new_warehouse_service.go 214KB

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