drug_stock_api_contorller.go 139KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "reflect"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "XT_New/enums"
  10. "XT_New/models"
  11. "XT_New/service"
  12. "XT_New/utils"
  13. "github.com/astaxie/beego"
  14. )
  15. type StockDrugApiController struct {
  16. BaseAuthAPIController
  17. }
  18. func DrugStockManagerApiRegistRouters() {
  19. //入库管理和其他入库管理相关接口
  20. beego.Router("/api/drugwarehouse/create", &StockDrugApiController{}, "post:CreateDrugWarehouse")
  21. beego.Router("/api/drugwarehouse/list", &StockDrugApiController{}, "get:GetDrugWarehouseList")
  22. beego.Router("/api/drugwarehouse/delete", &StockDrugApiController{}, "post:DeleteDrugWarehouse")
  23. beego.Router("/api/drugwarehouseinfo/delete", &StockDrugApiController{}, "post:DeleteDrugWarehouseInfo")
  24. beego.Router("/api/drugwarehouseinfo/list", &StockDrugApiController{}, "get:GetDrugWarehouseInfoList")
  25. beego.Router("/api/drugwarehouse/edit", &StockDrugApiController{}, "post:EditDrugWarehouse")
  26. //退货管理和其他退货管理相关接口
  27. beego.Router("/api/drugsalesreturn/create", &StockDrugApiController{}, "post:CreateDrugSalesReturn")
  28. beego.Router("/api/drugsalesreturn/delete", &StockDrugApiController{}, "post:DeleteDrugSalesReturn")
  29. beego.Router("/api/drugsalesreturninfo/delete", &StockDrugApiController{}, "post:DeleteDrugSalesReturnInfo")
  30. beego.Router("/api/drugsalesreturninfo/list", &StockDrugApiController{}, "get:GetDrugSalesReturnInfoList")
  31. beego.Router("/api/drugsalesreturn/list", &StockDrugApiController{}, "get:GetDrugSalesReturnList")
  32. beego.Router("/api/drugsalesreturn/edit", &StockDrugApiController{}, "post:EditDrugReturnInfo")
  33. //出库管理和其他出库管理相关接口
  34. beego.Router("/api/drugwarehouseout/create", &StockDrugApiController{}, "post:CreateDrugWarehouseOut")
  35. beego.Router("/api/drugwarehouseout/delete", &StockDrugApiController{}, "post:DeleteDrugWarehouseOut")
  36. beego.Router("/api/drugwarehouseoutinfo/delete", &StockDrugApiController{}, "post:DeleteDrugWarehouseOutInfo")
  37. beego.Router("/api/drugwarehouseout/list", &StockDrugApiController{}, "get:GetDrugWarehouseOutList")
  38. beego.Router("/api/drugwarehouseout/edit", &StockDrugApiController{}, "post:EditDrugWarehouseOut")
  39. beego.Router("/api/drugwarehouseout/info", &StockDrugApiController{}, "get:GetDrugWarehouseOutInfoList")
  40. //出库退库管理和其他出库退库管理相关接口
  41. beego.Router("/api/drugcancelstock/create", &StockDrugApiController{}, "post:CreateDrugCancelStock")
  42. beego.Router("/api/drugcancelstock/list", &StockDrugApiController{}, "get:GetDrugCancelStockList")
  43. beego.Router("/api/drugcancelstock/info", &StockDrugApiController{}, "get:GetDrugCancelStockInfoList")
  44. beego.Router("/api/drugcancelstock/delete", &StockDrugApiController{}, "post:DeleteDrugCancelStock")
  45. beego.Router("/api/drugcancelstockinfo/delete", &StockDrugApiController{}, "post:DeleteDrugCancelStockInfo")
  46. beego.Router("/api/drugcancelstockinfo/edit", &StockDrugApiController{}, "post:EditDrugCancelStock")
  47. ////库存相关全局配置
  48. beego.Router("/api/drugstock/config", &StockDrugApiController{}, "get:GetAllConfig")
  49. //beego.Router("/api/drugreturn/config", &StockDrugApiController{}, "get:GetAllSalesReturnConfig")
  50. //
  51. ////库存查询
  52. beego.Router("/api/drugstock/query", &StockDrugApiController{}, "get:GetDrugQueryInfo")
  53. //
  54. //beego.Router("/api/drugstock/detail", &StockDrugApiController{}, "get:GetDetailInfo")
  55. //
  56. beego.Router("/api/drugwarehouseout/user", &StockDrugApiController{}, "get:GetUserDetailInfo")
  57. beego.Router("/api/drugstock/get", &StockDrugApiController{}, "get:GetAllDrugStock")
  58. beego.Router("/api/drug/getcanceldrugorderprint", &StockDrugApiController{}, "get:GetCancelDrugOrderPrint")
  59. beego.Router("/api/drug/drugcancelexportlist", &StockDrugApiController{}, "get:GetDrugCancelExportList")
  60. beego.Router("/api/drug/getdrugwarehouseinfoprint", &StockDrugApiController{}, "get:GetDrugWarehouseInfoPrint")
  61. beego.Router("/api/drug/getdrugoutorderprint", &StockDrugApiController{}, "get:GetDrugOutOrderPrint")
  62. beego.Router("/api/drugstock/config/list", &StockDrugApiController{}, "get:GetConfigList")
  63. beego.Router("/api/drug/checkwarehouseinginfo", &StockDrugApiController{}, "get:CheckWarehousingInfo")
  64. beego.Router("/api/drug/returncheckwarehouseinginfo", &StockDrugApiController{}, "get:ReturnCheckWarehouseingInfo")
  65. beego.Router("/api/drug/tocheckdrugout", &StockDrugApiController{}, "get:CheckDrugOut")
  66. beego.Router("/api/drug/toreturncheck", &StockDrugApiController{}, "get:ToReturnCheck")
  67. beego.Router("/api/drug/checkcanceldrugstock", &StockDrugApiController{}, "get:CheckCancelDrugStock")
  68. beego.Router("/api/drug/returndrugcancelstock", &StockDrugApiController{}, "get:ReturnDrugCancelStock")
  69. }
  70. func (c *StockDrugApiController) CreateDrugWarehouse() {
  71. warehousing_time := c.GetString("warehousing_time")
  72. types, _ := c.GetInt64("type", 0)
  73. storehouse_id, _ := c.GetInt64("storehouse_id")
  74. warehousing_id, _ := c.GetInt64("warehousing_id")
  75. ctime := time.Now().Unix()
  76. adminUserInfo := c.GetAdminUserInfo()
  77. // 开始外部循环的事务
  78. db := service.XTWriteDB()
  79. tx := db.Begin()
  80. // 在函数结束时处理事务回滚
  81. defer func() {
  82. if r := recover(); r != nil {
  83. tx.Rollback()
  84. }
  85. }()
  86. warehousingDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", warehousing_time)
  87. if parseDateErr != nil {
  88. c.ErrorLog("日期(%v)解析错误:%v", warehousingDate, parseDateErr)
  89. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  90. return
  91. }
  92. timeStr := time.Now().Format("2006-01-02")
  93. timeArr := strings.Split(timeStr, "-")
  94. total, _ := service.FindNewAllWarehouseTotalOne(adminUserInfo.CurrentOrgId, tx)
  95. total = total + 1
  96. warehousing_order := "YPRKD" + strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  97. operation_time := time.Now().Unix()
  98. creater := adminUserInfo.AdminUser.Id
  99. warehousing := models.DrugWarehouse{
  100. WarehousingOrder: warehousing_order,
  101. OperationTime: operation_time,
  102. OrgId: adminUserInfo.CurrentOrgId,
  103. Creater: creater,
  104. Ctime: ctime,
  105. Status: 1,
  106. WarehousingTime: warehousingDate.Unix(),
  107. Type: types,
  108. StorehouseId: storehouse_id,
  109. IsCheck: 2,
  110. }
  111. if warehousing_id == 0 {
  112. service.AddNewSigleDrugWarehouse(&warehousing, tx)
  113. }
  114. if warehousing_id > 0 {
  115. service.UpdateNewSigleDrugWarehouse(warehousing_id, storehouse_id, tx)
  116. }
  117. info, _ := service.FindNewLastWarehousingSeven(adminUserInfo.CurrentOrgId, tx)
  118. dataBody := make(map[string]interface{}, 0)
  119. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  120. if err != nil {
  121. utils.ErrorLog(err.Error())
  122. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  123. return
  124. }
  125. var warehousingInfo []*models.DrugWarehouseInfo
  126. if dataBody["stockIn"] != nil && reflect.TypeOf(dataBody["stockIn"]).String() == "[]interface {}" {
  127. thisStockIn, _ := dataBody["stockIn"].([]interface{})
  128. if len(thisStockIn) > 0 {
  129. for _, item := range thisStockIn {
  130. items := item.(map[string]interface{})
  131. if items["id"] == nil || reflect.TypeOf(items["id"]).String() != "float64" {
  132. utils.ErrorLog("id")
  133. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  134. return
  135. }
  136. id := int64(items["id"].(float64))
  137. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  138. utils.ErrorLog("drug_id")
  139. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  140. return
  141. }
  142. drug_id := int64(items["drug_id"].(float64))
  143. if items["warehousing_count"] == nil || reflect.TypeOf(items["warehousing_count"]).String() != "string" {
  144. utils.ErrorLog("warehousing_count")
  145. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  146. return
  147. }
  148. warehousing_count, _ := strconv.ParseInt(items["warehousing_count"].(string), 10, 64)
  149. if items["last_price"] == nil || reflect.TypeOf(items["last_price"]).String() != "string" {
  150. utils.ErrorLog("last_price")
  151. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  152. return
  153. }
  154. last_price, _ := strconv.ParseFloat(items["last_price"].(string), 64)
  155. total := float64(warehousing_count) * last_price
  156. retail_price_total := float64(warehousing_count) * last_price
  157. var productDates int64
  158. var expiryDates int64
  159. if items["expiry_date"] == nil || reflect.TypeOf(items["expiry_date"]).String() != "string" {
  160. expiryDates = 0
  161. } else {
  162. if len(items["expiry_date"].(string)) == 0 {
  163. expiryDates = 0
  164. } else {
  165. expiryDate, _ := items["expiry_date"].(string)
  166. expiry_date, _ := utils.ParseTimeStringToTime("2006-01-02", expiryDate)
  167. expiryDates = expiry_date.Unix()
  168. }
  169. }
  170. if items["product_date"] == nil || reflect.TypeOf(items["product_date"]).String() != "string" {
  171. productDates = 0
  172. } else {
  173. if len(items["product_date"].(string)) == 0 {
  174. productDates = 0
  175. } else {
  176. productDate, _ := items["product_date"].(string)
  177. product_date, _ := utils.ParseTimeStringToTime("2006-01-02", productDate)
  178. productDates = product_date.Unix()
  179. }
  180. }
  181. number, _ := items["number"].(string)
  182. batch_number, _ := items["batch_number"].(string)
  183. if items["max_unit"] == nil || reflect.TypeOf(items["max_unit"]).String() != "string" {
  184. utils.ErrorLog("max_unit")
  185. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  186. return
  187. }
  188. max_unit, _ := items["max_unit"].(string)
  189. if items["min_unit"] == nil || reflect.TypeOf(items["min_unit"]).String() != "string" {
  190. utils.ErrorLog("min_unit")
  191. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  192. return
  193. }
  194. min_unit, _ := items["min_unit"].(string)
  195. var remark string
  196. if items["remark"] == nil || reflect.TypeOf(items["remark"]).String() != "string" {
  197. remark = ""
  198. } else {
  199. remark = items["remark"].(string)
  200. }
  201. manufacturer := int64(items["manufacturer"].(float64))
  202. dealer := int64(items["dealer"].(float64))
  203. if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  204. utils.ErrorLog("retail_price")
  205. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  206. return
  207. }
  208. retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  209. drug_code := items["drug_code"].(string)
  210. warehouseInfo := &models.DrugWarehouseInfo{
  211. ID: id,
  212. WarehousingOrder: info.WarehousingOrder,
  213. WarehousingId: info.ID,
  214. DrugId: drug_id,
  215. Number: number,
  216. ProductDate: productDates,
  217. ExpiryDate: expiryDates,
  218. WarehousingCount: warehousing_count,
  219. Price: last_price,
  220. TotalPrice: total,
  221. Status: 1,
  222. Ctime: warehousingDate.Unix(),
  223. Remark: remark,
  224. OrgId: adminUserInfo.CurrentOrgId,
  225. Type: types,
  226. Manufacturer: manufacturer,
  227. Dealer: dealer,
  228. StockMaxNumber: 0,
  229. RetailTotalPrice: retail_price_total,
  230. BatchNumber: batch_number,
  231. MaxUnit: max_unit,
  232. MinUnit: min_unit,
  233. RetailPrice: retail_price,
  234. StorehouseId: storehouse_id,
  235. IsCheck: 2,
  236. StockMinNumber: 0,
  237. DrugCode: drug_code,
  238. }
  239. warehousingInfo = append(warehousingInfo, warehouseInfo)
  240. }
  241. }
  242. }
  243. for _, item := range warehousingInfo {
  244. if item.ID == 0 {
  245. service.CreatedNewWarehouseing(item, tx)
  246. }
  247. if item.ID > 0 {
  248. service.UpdateNewWarehouseing(item, tx)
  249. }
  250. }
  251. list, _ := service.FindNewDrugWarehouseInfoListById(info.ID, adminUserInfo.CurrentOrgId, tx)
  252. tx.Commit()
  253. manufacturerList, _ := service.GetAllManufacturerList(adminUserInfo.CurrentOrgId)
  254. dealerList, _ := service.GetAllDealerList(adminUserInfo.CurrentOrgId)
  255. var drugType = "药品类型"
  256. drugTypeParent, _ := service.GetDrugDataConfig(0, drugType)
  257. drugTypeList, _ := service.GetParentDataConfig(drugTypeParent.ID, adminUserInfo.CurrentOrgId)
  258. c.ServeSuccessJSON(map[string]interface{}{
  259. "msg": "入库成功",
  260. "warehousing_order": warehousing_order,
  261. "info": info,
  262. "list": list,
  263. "manufacturerList": manufacturerList,
  264. "dealerList": dealerList,
  265. "drugTypeList": drugTypeList,
  266. })
  267. }
  268. func (c *StockDrugApiController) GetDrugWarehouseList() {
  269. page, _ := c.GetInt64("page", -1)
  270. limit, _ := c.GetInt64("limit", -1)
  271. start_time := c.GetString("start_time")
  272. end_time := c.GetString("end_time")
  273. types, _ := c.GetInt64("type", 0)
  274. keywords := c.GetString("keywords")
  275. storehouse_id, _ := c.GetInt64("storehouse_id")
  276. check_type, _ := c.GetInt64("check_type")
  277. timeLayout := "2006-01-02"
  278. loc, _ := time.LoadLocation("Local")
  279. var startTime int64
  280. if len(start_time) > 0 {
  281. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  282. if err != nil {
  283. fmt.Println(err)
  284. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  285. return
  286. }
  287. startTime = theTime.Unix()
  288. }
  289. var endTime int64
  290. if len(end_time) > 0 {
  291. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  292. if err != nil {
  293. utils.ErrorLog(err.Error())
  294. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  295. return
  296. }
  297. endTime = theTime.Unix()
  298. }
  299. adminUserInfo := c.GetAdminUserInfo()
  300. //按药品名称搜索
  301. var ids []int64
  302. var idArray []int64
  303. list, _ := service.GetDrugNameByKeyword(keywords, adminUserInfo.CurrentOrgId)
  304. for _, item := range list {
  305. ids = append(ids, item.ID)
  306. }
  307. //查找入库单详情
  308. if len(ids) > 0 {
  309. info, _ := service.GetDrugWarehouseOrderDetailTwo(ids, adminUserInfo.CurrentOrgId, startTime, endTime, storehouse_id)
  310. for _, it := range info {
  311. idArray = append(idArray, it.WarehousingId)
  312. }
  313. }
  314. warehouseList, total, err := service.FindAllDrugWarehousingListOne(adminUserInfo.CurrentOrgId, page, limit, startTime, endTime, types, keywords, idArray, storehouse_id, check_type)
  315. houseList, _ := service.GetAllStoreHouseList(adminUserInfo.CurrentOrgId)
  316. if err == nil {
  317. c.ServeSuccessJSON(map[string]interface{}{
  318. "list": warehouseList,
  319. "total": total,
  320. "houseList": houseList,
  321. })
  322. } else {
  323. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  324. }
  325. }
  326. func (c *StockDrugApiController) GetDrugWarehouseInfoList() {
  327. id, _ := c.GetInt64("id", 0)
  328. if id <= 0 {
  329. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  330. return
  331. }
  332. admin := c.GetAdminUserInfo()
  333. warehousing, err := service.FindDrugWarehousingById(id, admin.CurrentOrgId)
  334. manufacturerList, _ := service.GetAllManufacturerList(admin.CurrentOrgId)
  335. dealerList, _ := service.GetAllDealerList(admin.CurrentOrgId)
  336. if err != nil {
  337. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  338. return
  339. }
  340. if warehousing.ID <= 0 {
  341. return
  342. }
  343. warehousingInfo, err := service.FindDrugWarehousingInfoByIdThree(id, admin.CurrentOrgId)
  344. list, _ := service.GetAllStoreHouseListThree(admin.CurrentOrgId)
  345. if err == nil {
  346. c.ServeSuccessJSON(map[string]interface{}{
  347. "info": warehousingInfo,
  348. "warehousing": warehousing,
  349. "manufacturerList": manufacturerList,
  350. "dealerList": dealerList,
  351. "list": list,
  352. })
  353. } else {
  354. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  355. }
  356. }
  357. func (c *StockDrugApiController) EditDrugWarehouse() {
  358. warehousing_time := c.GetString("warehousing_time")
  359. id, _ := c.GetInt64("id", 0)
  360. types, _ := c.GetInt64("type", 0)
  361. storehouse_id, _ := c.GetInt64("storehouse_id")
  362. if id == 0 {
  363. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  364. return
  365. }
  366. // 开始外部循环的事务
  367. db := service.XTWriteDB()
  368. tx := db.Begin()
  369. // 在函数结束时处理事务回滚
  370. defer func() {
  371. if r := recover(); r != nil {
  372. tx.Rollback()
  373. }
  374. }()
  375. ctime := time.Now().Unix()
  376. mtime := time.Now().Unix()
  377. adminUserInfo := c.GetAdminUserInfo()
  378. warehousingDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", warehousing_time)
  379. if parseDateErr != nil {
  380. c.ErrorLog("日期(%v)解析错误:%v", warehousingDate, parseDateErr)
  381. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  382. return
  383. }
  384. warehouse, _ := service.FindNewDrugWarehousingById(id, adminUserInfo.CurrentOrgId, tx)
  385. warehousing := models.DrugWarehouse{
  386. WarehousingOrder: warehouse.WarehousingOrder,
  387. OrgId: adminUserInfo.CurrentOrgId,
  388. Modifier: adminUserInfo.AdminUser.Id,
  389. Mtime: mtime,
  390. Status: 1,
  391. WarehousingTime: warehousingDate.Unix(),
  392. Type: warehouse.Type,
  393. StorehouseId: storehouse_id,
  394. }
  395. service.EditNewDrugWarehousingOne(warehousing, id, tx)
  396. dataBody := make(map[string]interface{}, 0)
  397. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  398. if err != nil {
  399. utils.ErrorLog(err.Error())
  400. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  401. return
  402. }
  403. var warehousingInfo []*models.DrugWarehouseInfo
  404. var upDateWarehousingInfo []*models.DrugWarehouseInfo
  405. if dataBody["stockIn"] != nil && reflect.TypeOf(dataBody["stockIn"]).String() == "[]interface {}" {
  406. thisStockIn, _ := dataBody["stockIn"].([]interface{})
  407. if len(thisStockIn) > 0 {
  408. for _, item := range thisStockIn {
  409. items := item.(map[string]interface{})
  410. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  411. utils.ErrorLog("drug_id")
  412. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  413. return
  414. }
  415. drug_id := int64(items["drug_id"].(float64))
  416. if items["warehousing_count"] == nil || reflect.TypeOf(items["warehousing_count"]).String() != "string" {
  417. utils.ErrorLog("warehousing_count")
  418. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  419. return
  420. }
  421. warehousing_count, _ := strconv.ParseInt(items["warehousing_count"].(string), 10, 64)
  422. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  423. utils.ErrorLog("price")
  424. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  425. return
  426. }
  427. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  428. total := float64(warehousing_count) * price
  429. if items["last_price"] == nil || reflect.TypeOf(items["last_price"]).String() != "string" {
  430. utils.ErrorLog("last_price")
  431. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  432. return
  433. }
  434. last_price, _ := strconv.ParseFloat(items["last_price"].(string), 64)
  435. retail_price_total := float64(warehousing_count) * last_price
  436. retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  437. var productDates int64
  438. var expiryDates int64
  439. if items["expiry_date"] == nil || reflect.TypeOf(items["expiry_date"]).String() != "string" {
  440. expiryDates = 0
  441. } else {
  442. if len(items["expiry_date"].(string)) == 0 {
  443. expiryDates = 0
  444. } else {
  445. expiryDate, _ := items["expiry_date"].(string)
  446. expiry_date, _ := utils.ParseTimeStringToTime("2006-01-02", expiryDate)
  447. expiryDates = expiry_date.Unix()
  448. }
  449. }
  450. if items["product_date"] == nil || reflect.TypeOf(items["product_date"]).String() != "string" {
  451. productDates = 0
  452. } else {
  453. if len(items["product_date"].(string)) == 0 {
  454. productDates = 0
  455. } else {
  456. productDate, _ := items["product_date"].(string)
  457. product_date, _ := utils.ParseTimeStringToTime("2006-01-02", productDate)
  458. productDates = product_date.Unix()
  459. }
  460. }
  461. number, _ := items["number"].(string)
  462. batch_number, _ := items["batch_number"].(string)
  463. max_unit, _ := items["max_unit"].(string)
  464. min_unit, _ := items["min_unit"].(string)
  465. var remark string
  466. if items["remark"] == nil || reflect.TypeOf(items["remark"]).String() != "string" {
  467. remark = ""
  468. } else {
  469. remark = items["remark"].(string)
  470. }
  471. if items["id"] == nil || reflect.TypeOf(items["id"]).String() != "float64" {
  472. utils.ErrorLog("id")
  473. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  474. return
  475. }
  476. id := int64(items["id"].(float64))
  477. manufacturer := int64(items["manufacturer"].(float64))
  478. dealer := int64(items["dealer"].(float64))
  479. drug_code, _ := items["drug_code"].(string)
  480. medical, _ := service.GetBaseDrugMedical(drug_id)
  481. if id == 0 {
  482. warehouseInfo := &models.DrugWarehouseInfo{
  483. WarehousingOrder: warehouse.WarehousingOrder,
  484. WarehousingId: warehouse.ID,
  485. DrugId: drug_id,
  486. Number: number,
  487. ProductDate: productDates,
  488. ExpiryDate: expiryDates,
  489. WarehousingCount: warehousing_count,
  490. Price: price,
  491. TotalPrice: total,
  492. Status: 1,
  493. Ctime: ctime,
  494. Remark: remark,
  495. OrgId: adminUserInfo.CurrentOrgId,
  496. Type: types,
  497. Manufacturer: manufacturer,
  498. Dealer: dealer,
  499. RetailPrice: retail_price,
  500. RetailTotalPrice: retail_price_total,
  501. BatchNumber: batch_number,
  502. MaxUnit: max_unit,
  503. MinUnit: min_unit,
  504. StockMaxNumber: 0,
  505. StorehouseId: storehouse_id,
  506. StockMinNumber: 0,
  507. DrugCode: drug_code,
  508. }
  509. warehousingInfo = append(warehousingInfo, warehouseInfo)
  510. } else {
  511. warehouseInfo := &models.DrugWarehouseInfo{
  512. ID: id,
  513. WarehousingOrder: warehouse.WarehousingOrder,
  514. WarehousingId: warehouse.ID,
  515. DrugId: drug_id,
  516. Number: number,
  517. ProductDate: productDates,
  518. ExpiryDate: expiryDates,
  519. WarehousingCount: warehousing_count,
  520. Price: price,
  521. TotalPrice: total,
  522. Status: 1,
  523. Ctime: ctime,
  524. Remark: remark,
  525. OrgId: adminUserInfo.CurrentOrgId,
  526. Type: types,
  527. Manufacturer: manufacturer,
  528. Dealer: dealer,
  529. RetailPrice: retail_price,
  530. RetailTotalPrice: retail_price_total,
  531. MaxUnit: max_unit,
  532. MinUnit: min_unit,
  533. BatchNumber: batch_number,
  534. StorehouseId: storehouse_id,
  535. DrugCode: drug_code,
  536. }
  537. if medical.MaxUnit == medical.MinUnit {
  538. warehouseInfo.StockMaxNumber = warehousing_count
  539. warehouseInfo.StockMinNumber = 0
  540. warehouseInfo.MaxUnit = min_unit
  541. }
  542. if medical.MinUnit != medical.MaxUnit {
  543. if max_unit == medical.MaxUnit {
  544. warehouseInfo.StockMaxNumber = warehousing_count
  545. warehouseInfo.StockMinNumber = 0
  546. warehouseInfo.MaxUnit = max_unit
  547. warehouseInfo.MinUnit = medical.MinUnit
  548. }
  549. if max_unit == medical.MinUnit {
  550. warehouseInfo.StockMaxNumber = 0
  551. warehouseInfo.StockMinNumber = warehousing_count
  552. warehouseInfo.MaxUnit = medical.MinUnit
  553. warehouseInfo.MinUnit = medical.MinUnit
  554. }
  555. }
  556. upDateWarehousingInfo = append(upDateWarehousingInfo, warehouseInfo)
  557. }
  558. }
  559. }
  560. }
  561. var errs error
  562. if len(warehousingInfo) > 0 {
  563. errs = service.CreateNewDrugWarehousingInfo(warehousingInfo, tx)
  564. }
  565. if len(upDateWarehousingInfo) > 0 {
  566. for _, item := range upDateWarehousingInfo {
  567. if item.ID > 0 {
  568. service.UpdateNewDrugWarehouseInfoTwenty(item.ID, item, tx)
  569. }
  570. }
  571. }
  572. if errs != nil {
  573. utils.ErrorLog(errs.Error())
  574. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockInFail)
  575. return
  576. }
  577. list, _ := service.GetNewDrugWarehouseInfoList(id, adminUserInfo.CurrentOrgId, tx)
  578. tx.Commit()
  579. manufacturerList, _ := service.GetAllManufacturerList(adminUserInfo.CurrentOrgId)
  580. dealerList, _ := service.GetAllDealerList(adminUserInfo.CurrentOrgId)
  581. var drugType = "药品类型"
  582. drugTypeParent, _ := service.GetDrugDataConfig(0, drugType)
  583. drugTypeList, _ := service.GetParentDataConfig(drugTypeParent.ID, adminUserInfo.CurrentOrgId)
  584. c.ServeSuccessJSON(map[string]interface{}{
  585. "msg": "编辑成功",
  586. "list": list,
  587. "manufacturerList": manufacturerList,
  588. "dealerList": dealerList,
  589. "drugTypeList": drugTypeList,
  590. })
  591. }
  592. func (c *StockDrugApiController) DeleteDrugWarehouse() {
  593. ids := c.GetString("ids")
  594. if len(ids) == 0 {
  595. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  596. return
  597. }
  598. idArray := strings.Split(ids, ",")
  599. orgId := c.GetAdminUserInfo().CurrentOrgId
  600. err := service.DeleteDrugWarehouse(idArray)
  601. list, _ := service.GetLastDrugwarehouseInfoByIdTwenty(idArray, orgId)
  602. for _, it := range list {
  603. //查询默认仓库
  604. houseConfig, _ := service.GetAllStoreHouseConfig(it.OrgId)
  605. //查询默认仓库剩余多少库存
  606. list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, it.OrgId, it.DrugId)
  607. var sum_count int64
  608. var sum_in_count int64
  609. for _, it := range list {
  610. baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  611. if it.MaxUnit == baseDrug.MaxUnit {
  612. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  613. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  614. }
  615. sum_count += it.StockMaxNumber + it.StockMinNumber
  616. sum_in_count += it.WarehousingCount
  617. }
  618. service.UpdateMedicalSumCount(it.DrugId, sum_count, sum_in_count, it.OrgId)
  619. }
  620. if err != nil {
  621. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  622. } else {
  623. c.ServeSuccessJSON(map[string]interface{}{
  624. "msg": "删除成功",
  625. })
  626. }
  627. }
  628. func (c *StockDrugApiController) DeleteDrugWarehouseInfo() {
  629. id, _ := c.GetInt64("id", 0)
  630. if id == 0 {
  631. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  632. return
  633. }
  634. err := service.UpDateDrugWarehouseStatus(id)
  635. service.UpdateDrugFlowById(id)
  636. var sum_total int64
  637. list, _ := service.GetDrugWarehouseInfoByIdTwenty(id)
  638. medical, _ := service.GetBaseDrugMedical(list.DrugId)
  639. sum_total += list.StockMaxNumber*medical.MinNumber + list.StockMinNumber
  640. storeConfig, _ := service.GetAllStoreHouseConfig(list.OrgId)
  641. if storeConfig.DrugStorehouseInfo == list.StorehouseId {
  642. service.ModifyDrugReduceInformationTwo(list.DrugId, sum_total, list.OrgId)
  643. }
  644. if err != nil {
  645. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  646. } else {
  647. c.ServeSuccessJSON(map[string]interface{}{
  648. "msg": "删除成功",
  649. })
  650. }
  651. }
  652. func (c *StockDrugApiController) CreateDrugSalesReturn() {
  653. dealer_id, _ := c.GetInt64("dealer_id", 0)
  654. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  655. sales_return_time := c.GetString("time")
  656. types, _ := c.GetInt64("type", 0)
  657. salesReturnDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", sales_return_time)
  658. if parseDateErr != nil {
  659. c.ErrorLog("日期(%v)解析错误:%v", salesReturnDate, parseDateErr)
  660. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  661. return
  662. }
  663. adminUserInfo := c.GetAdminUserInfo()
  664. operation_time := time.Now().Unix()
  665. creater := adminUserInfo.AdminUser.Id
  666. ctime := time.Now().Unix()
  667. timeStr := time.Now().Format("2006-01-02")
  668. timeArr := strings.Split(timeStr, "-")
  669. total, _ := service.FindAllSalesReturnTotal(adminUserInfo.CurrentOrgId)
  670. total = total + 1
  671. orderNumber := "YPTHD" + strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  672. salesReturn := models.DrugSalesReturn{
  673. OrderNumber: orderNumber,
  674. OperaTime: operation_time,
  675. OrgId: adminUserInfo.CurrentOrgId,
  676. Creater: creater,
  677. Ctime: ctime,
  678. Status: 1,
  679. ReturnTime: salesReturnDate.Unix(),
  680. Dealer: dealer_id,
  681. Manufacturer: manufacturer_id,
  682. Type: types,
  683. }
  684. service.AddSigleDrugSalesReturn(&salesReturn)
  685. dataBody := make(map[string]interface{}, 0)
  686. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  687. if err != nil {
  688. utils.ErrorLog(err.Error())
  689. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  690. return
  691. }
  692. var salesReturnInfos []*models.DrugSalesReturnInfo
  693. if dataBody["salesReturn"] != nil && reflect.TypeOf(dataBody["salesReturn"]).String() == "[]interface {}" {
  694. thisStockIn, _ := dataBody["salesReturn"].([]interface{})
  695. if len(thisStockIn) > 0 {
  696. for _, item := range thisStockIn {
  697. items := item.(map[string]interface{})
  698. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  699. utils.ErrorLog("drug_id")
  700. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  701. return
  702. }
  703. drug_id := int64(items["drug_id"].(float64))
  704. return_count, _ := items["return_count"].(string)
  705. if len(return_count) == 0 {
  706. utils.ErrorLog("len(return_count) == 0")
  707. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  708. return
  709. }
  710. count, _ := strconv.ParseInt(return_count, 10, 64)
  711. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  712. utils.ErrorLog("price")
  713. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  714. return
  715. }
  716. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  717. total := float64(count) * price
  718. if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  719. utils.ErrorLog("retail_price")
  720. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  721. return
  722. }
  723. retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  724. retail_price_total := float64(count) * retail_price
  725. salesReturnInfo := &models.DrugSalesReturnInfo{
  726. OrderNumber: salesReturn.OrderNumber,
  727. SalesReturnId: salesReturn.ID,
  728. DrugId: drug_id,
  729. Count: count,
  730. Price: price,
  731. Total: total,
  732. Status: 1,
  733. Ctime: ctime,
  734. OrgId: adminUserInfo.CurrentOrgId,
  735. Type: types,
  736. Manufacturer: manufacturer_id,
  737. Dealer: dealer_id,
  738. RetailPrice: retail_price,
  739. RetailTotalPrice: retail_price_total,
  740. }
  741. salesReturnInfos = append(salesReturnInfos, salesReturnInfo)
  742. }
  743. }
  744. }
  745. errs := service.CreateDrugSalesReturnInfo(salesReturnInfos)
  746. if errs != nil {
  747. utils.ErrorLog(errs.Error())
  748. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateReturnFail)
  749. return
  750. }
  751. c.ServeSuccessJSON(map[string]interface{}{
  752. "msg": "退货成功",
  753. })
  754. }
  755. func (c *StockDrugApiController) GetDrugSalesReturnList() {
  756. page, _ := c.GetInt64("page", -1)
  757. limit, _ := c.GetInt64("limit", -1)
  758. start_time := c.GetString("start_time")
  759. end_time := c.GetString("end_time")
  760. types, _ := c.GetInt64("type", 0)
  761. keywords := c.GetString("keywords")
  762. timeLayout := "2006-01-02"
  763. loc, _ := time.LoadLocation("Local")
  764. var startTime int64
  765. if len(start_time) > 0 {
  766. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  767. if err != nil {
  768. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  769. return
  770. }
  771. startTime = theTime.Unix()
  772. }
  773. var endTime int64
  774. if len(end_time) > 0 {
  775. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  776. if err != nil {
  777. utils.ErrorLog(err.Error())
  778. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  779. return
  780. }
  781. endTime = theTime.Unix()
  782. }
  783. adminUserInfo := c.GetAdminUserInfo()
  784. returnList, total, err := service.FindAllDrugReturnList(adminUserInfo.CurrentOrgId, page, limit, startTime, endTime, types, keywords)
  785. if err == nil {
  786. c.ServeSuccessJSON(map[string]interface{}{
  787. "list": returnList,
  788. "total": total,
  789. })
  790. } else {
  791. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  792. }
  793. }
  794. func (this *StockDrugApiController) DeleteDrugSalesReturn() {
  795. ids := this.GetString("ids")
  796. if len(ids) == 0 {
  797. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  798. return
  799. }
  800. idArray := strings.Split(ids, ",")
  801. err := service.DeleteDrugSalesReturn(idArray)
  802. if err != nil {
  803. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  804. } else {
  805. this.ServeSuccessJSON(map[string]interface{}{
  806. "msg": "删除成功",
  807. })
  808. }
  809. }
  810. func (this *StockDrugApiController) DeleteDrugSalesReturnInfo() {
  811. id, _ := this.GetInt64("id", 0)
  812. fmt.Println(id)
  813. if id == 0 {
  814. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  815. return
  816. }
  817. err := service.UpDateDrugSaleReturnStatus(id)
  818. if err != nil {
  819. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  820. } else {
  821. this.ServeSuccessJSON(map[string]interface{}{
  822. "msg": "删除成功",
  823. })
  824. }
  825. }
  826. func (this *StockDrugApiController) GetDrugSalesReturnInfoList() {
  827. id, _ := this.GetInt64("id", 0)
  828. adminUserInfo := this.GetAdminUserInfo()
  829. list, _ := service.FindAllDrugSalesReturnInfoById(id, adminUserInfo.CurrentOrgId)
  830. salesReturn, _ := service.FindAllDrugSalesReturnById(id, adminUserInfo.CurrentOrgId)
  831. this.ServeSuccessJSON(map[string]interface{}{
  832. "list": list,
  833. "salesReturn": salesReturn,
  834. })
  835. }
  836. func (c *StockDrugApiController) EditDrugReturnInfo() {
  837. return_time := c.GetString("return_time")
  838. id, _ := c.GetInt64("id", 0)
  839. types, _ := c.GetInt64("type", 0)
  840. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  841. dealer_id, _ := c.GetInt64("dealer_id", 0)
  842. if id == 0 {
  843. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  844. return
  845. }
  846. ctime := time.Now().Unix()
  847. mtime := time.Now().Unix()
  848. adminUserInfo := c.GetAdminUserInfo()
  849. returnDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", return_time)
  850. if parseDateErr != nil {
  851. c.ErrorLog("日期(%v)解析错误:%v", returnDate, parseDateErr)
  852. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  853. return
  854. }
  855. salesReturns, _ := service.FindDrugSalesReturnById(id)
  856. sales := models.DrugSalesReturn{
  857. ID: salesReturns.ID,
  858. Mtime: mtime,
  859. ReturnTime: returnDate.Unix(),
  860. Manufacturer: manufacturer_id,
  861. Dealer: dealer_id,
  862. }
  863. service.EditDrugSaleReturn(sales)
  864. dataBody := make(map[string]interface{}, 0)
  865. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  866. if err != nil {
  867. utils.ErrorLog(err.Error())
  868. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  869. return
  870. }
  871. var returnInfos []*models.DrugSalesReturnInfo
  872. var upDateReturnInfos []*models.DrugSalesReturnInfo
  873. if dataBody["salesReturn"] != nil && reflect.TypeOf(dataBody["salesReturn"]).String() == "[]interface {}" {
  874. thisStockIn, _ := dataBody["salesReturn"].([]interface{})
  875. if len(thisStockIn) > 0 {
  876. for _, item := range thisStockIn {
  877. items := item.(map[string]interface{})
  878. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  879. utils.ErrorLog("drug_id")
  880. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  881. return
  882. }
  883. drug_id := int64(items["drug_id"].(float64))
  884. if items["count"] == nil || reflect.TypeOf(items["count"]).String() != "string" {
  885. utils.ErrorLog("count")
  886. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  887. return
  888. }
  889. count, _ := strconv.ParseInt(items["count"].(string), 10, 64)
  890. if items["id"] == nil || reflect.TypeOf(items["id"]).String() != "float64" {
  891. utils.ErrorLog("id")
  892. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  893. return
  894. }
  895. id := int64(items["id"].(float64))
  896. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  897. utils.ErrorLog("price")
  898. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  899. return
  900. }
  901. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  902. total := float64(count) * price
  903. if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  904. utils.ErrorLog("retail_price")
  905. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  906. return
  907. }
  908. retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  909. retail_price_total := float64(count) * retail_price
  910. if id == 0 {
  911. returnInfo := &models.DrugSalesReturnInfo{
  912. DrugId: drug_id,
  913. Count: count,
  914. Status: 1,
  915. Ctime: ctime,
  916. OrgId: adminUserInfo.CurrentOrgId,
  917. OrderNumber: sales.OrderNumber,
  918. SalesReturnId: sales.ID,
  919. Mtime: time.Now().Unix(),
  920. Type: types,
  921. Manufacturer: manufacturer_id,
  922. Dealer: dealer_id,
  923. RetailPrice: retail_price,
  924. RetailTotalPrice: retail_price_total,
  925. Total: total,
  926. Price: price,
  927. }
  928. returnInfos = append(returnInfos, returnInfo)
  929. } else {
  930. returnInfo := &models.DrugSalesReturnInfo{
  931. ID: id,
  932. DrugId: drug_id,
  933. Count: count,
  934. Status: 1,
  935. Ctime: ctime,
  936. OrgId: adminUserInfo.CurrentOrgId,
  937. OrderNumber: sales.OrderNumber,
  938. SalesReturnId: sales.ID,
  939. Mtime: time.Now().Unix(),
  940. Type: types,
  941. Manufacturer: manufacturer_id,
  942. Dealer: dealer_id,
  943. RetailPrice: retail_price,
  944. RetailTotalPrice: retail_price_total,
  945. Total: total,
  946. Price: price,
  947. }
  948. upDateReturnInfos = append(upDateReturnInfos, returnInfo)
  949. }
  950. }
  951. }
  952. }
  953. var errs error
  954. if len(returnInfos) > 0 {
  955. errs = service.CreateDrugSalesReturnInfo(returnInfos)
  956. }
  957. if len(upDateReturnInfos) > 0 {
  958. for _, item := range upDateReturnInfos {
  959. errs = service.UpDateDrugSalesReturnInfo(item)
  960. }
  961. }
  962. if errs != nil {
  963. utils.ErrorLog(errs.Error())
  964. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateReturnFail)
  965. return
  966. }
  967. c.ServeSuccessJSON(map[string]interface{}{
  968. "msg": "编辑成功",
  969. })
  970. }
  971. func (c *StockDrugApiController) CreateDrugWarehouseOut() {
  972. types, _ := c.GetInt64("type", 0)
  973. storehouse_id, _ := c.GetInt64("storehouse_id")
  974. warehouse_out_id, _ := c.GetInt64("warehouse_out_id")
  975. ctime := time.Now().Unix()
  976. adminUserInfo := c.GetAdminUserInfo()
  977. warehousing_out_time := c.GetString("warehousing_out_time")
  978. warehousingOutDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", warehousing_out_time)
  979. if parseDateErr != nil {
  980. c.ErrorLog("日期(%v)解析错误:%v", warehousingOutDate, parseDateErr)
  981. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  982. return
  983. }
  984. timeStr := time.Now().Format("2006-01-02")
  985. timeArr := strings.Split(timeStr, "-")
  986. // 开始外部循环的事务
  987. db := service.XTWriteDB()
  988. tx := db.Begin()
  989. // 在函数结束时处理事务回滚
  990. defer func() {
  991. if r := recover(); r != nil {
  992. tx.Rollback()
  993. }
  994. }()
  995. total, _ := service.FindAllSelfDrugWarehouseOut(adminUserInfo.CurrentOrgId, tx)
  996. total = total + 1
  997. warehousing_out_order := strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  998. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  999. number = number + total
  1000. warehousing_out_order = "YPCKM" + strconv.FormatInt(number, 10)
  1001. operation_time := time.Now().Unix()
  1002. creater := adminUserInfo.AdminUser.Id
  1003. //创建一个出库单
  1004. warehouseOut := models.DrugWarehouseOut{
  1005. WarehouseOutOrderNumber: warehousing_out_order,
  1006. OperationTime: operation_time,
  1007. OrgId: adminUserInfo.CurrentOrgId,
  1008. Creater: creater,
  1009. Ctime: ctime,
  1010. Status: 1,
  1011. WarehouseOutTime: warehousingOutDate.Unix(),
  1012. Type: types,
  1013. StorehouseId: storehouse_id,
  1014. IsCheck: 2,
  1015. }
  1016. if warehouse_out_id == 0 {
  1017. //查询是否生成出库单
  1018. outIsExit, _ := service.GetNewDrugWarehouseOutIsExit(adminUserInfo.CurrentOrgId, 0, warehousingOutDate.Unix(), warehousing_out_order, tx)
  1019. fmt.Println("outIsExit---------------------", outIsExit)
  1020. if outIsExit.ID == 0 {
  1021. service.AddNewSigleDrugWarehouseOut(&warehouseOut, tx)
  1022. }
  1023. }
  1024. if warehouse_out_id > 0 {
  1025. service.UpdateNewSingleDrugWarehouseOut(warehouse_out_id, storehouse_id, tx)
  1026. }
  1027. lastWarehouseOut, _ := service.GetNewDrugLastWarehouseOut(adminUserInfo.CurrentOrgId, tx)
  1028. dataBody := make(map[string]interface{}, 0)
  1029. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  1030. if err != nil {
  1031. utils.ErrorLog(err.Error())
  1032. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1033. return
  1034. }
  1035. //出库单详情
  1036. var warehousingOutInfo []*models.DrugWarehouseOutInfo
  1037. //把前端的参数加工为出库单详情
  1038. if dataBody["stockOut"] != nil && reflect.TypeOf(dataBody["stockOut"]).String() == "[]interface {}" {
  1039. thisStockIn, _ := dataBody["stockOut"].([]interface{})
  1040. if len(thisStockIn) > 0 {
  1041. for _, item := range thisStockIn {
  1042. items := item.(map[string]interface{})
  1043. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  1044. utils.ErrorLog("drug_id")
  1045. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1046. return
  1047. }
  1048. drug_id := int64(items["drug_id"].(float64))
  1049. if items["count"] == nil || reflect.TypeOf(items["count"]).String() != "string" {
  1050. utils.ErrorLog("count")
  1051. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1052. return
  1053. }
  1054. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  1055. utils.ErrorLog("price")
  1056. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1057. return
  1058. }
  1059. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  1060. count, _ := strconv.ParseInt(items["count"].(string), 10, 64)
  1061. total := float64(count) * price
  1062. retail_price_total := float64(count) * price
  1063. remark := items["remark"].(string)
  1064. max_unit := items["max_unit"].(string)
  1065. dealer := int64(items["dealer"].(float64))
  1066. manufacturer := int64(items["manufacturer"].(float64))
  1067. number := items["number"].(string)
  1068. batch_number := items["batch_number"].(string)
  1069. warehouse_info_id := int64(items["warehouse_info_id"].(float64))
  1070. var productDates int64
  1071. var expiryDates int64
  1072. if items["expiry_date"] == nil || reflect.TypeOf(items["expiry_date"]).String() != "string" {
  1073. expiryDates = 0
  1074. } else {
  1075. if len(items["expiry_date"].(string)) == 0 {
  1076. expiryDates = 0
  1077. } else {
  1078. expiryDate, _ := items["expiry_date"].(string)
  1079. expiry_date, _ := utils.ParseTimeStringToTime("2006-01-02", expiryDate)
  1080. expiryDates = expiry_date.Unix()
  1081. }
  1082. }
  1083. if items["product_date"] == nil || reflect.TypeOf(items["product_date"]).String() != "string" {
  1084. productDates = 0
  1085. } else {
  1086. if len(items["product_date"].(string)) == 0 {
  1087. productDates = 0
  1088. } else {
  1089. productDate, _ := items["product_date"].(string)
  1090. product_date, _ := utils.ParseTimeStringToTime("2006-01-02", productDate)
  1091. productDates = product_date.Unix()
  1092. }
  1093. }
  1094. admin_user_id := int64(items["admin_user_id"].(float64))
  1095. //if items["last_price"] == nil || reflect.TypeOf(items["last_price"]).String() != "string" {
  1096. // utils.ErrorLog("last_price")
  1097. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1098. // return
  1099. //}
  1100. //last_price, _ := strconv.ParseFloat(items["last_price"].(string), 64)
  1101. stock_count, _ := items["stock_count"].(string)
  1102. patient_id := int64(items["patient_id"].(float64))
  1103. id := int64(items["id"].(float64))
  1104. class_type, _ := items["class_type"].(string)
  1105. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1106. ID: id,
  1107. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1108. WarehouseOutId: lastWarehouseOut.ID,
  1109. DrugId: drug_id,
  1110. Count: count,
  1111. Price: price,
  1112. TotalPrice: total,
  1113. Status: 1,
  1114. Ctime: warehousingOutDate.Unix(),
  1115. Mtime: ctime,
  1116. Remark: remark,
  1117. OrgId: adminUserInfo.CurrentOrgId,
  1118. Type: types,
  1119. Manufacturer: manufacturer,
  1120. Dealer: dealer,
  1121. RetailPrice: price,
  1122. RetailTotalPrice: retail_price_total,
  1123. CountUnit: max_unit,
  1124. ExpiryDate: expiryDates,
  1125. ProductDate: productDates,
  1126. Number: number,
  1127. BatchNumber: batch_number,
  1128. IsSys: 0,
  1129. WarehouseInfoId: warehouse_info_id,
  1130. StorehouseId: storehouse_id,
  1131. AdminUserId: admin_user_id,
  1132. StockCount: stock_count,
  1133. IsCheck: 2,
  1134. SysRecordTime: warehousingOutDate.Unix(),
  1135. PatientId: patient_id,
  1136. ClassType: class_type,
  1137. }
  1138. warehousingOutInfo = append(warehousingOutInfo, warehouseOutInfo)
  1139. }
  1140. }
  1141. }
  1142. //var total_count int64 //总库存
  1143. //var prescribing_number_total int64 //出库数据
  1144. //调用出库逻辑
  1145. for _, item := range warehousingOutInfo {
  1146. if item.ID == 0 {
  1147. //查询改耗材是否出库
  1148. outInfo, _ := service.GetNewDrugsIsExsit(item.DrugId, item.SysRecordTime, item.OrgId, lastWarehouseOut.ID, tx)
  1149. if outInfo.ID == 0 {
  1150. errOne := service.AddNewSigleDrugWarehouseOutInfo(item, tx)
  1151. fmt.Println(errOne)
  1152. }
  1153. }
  1154. if item.ID > 0 {
  1155. service.UpdatedNewDrugWarehouseInfo(item, item.ID, tx)
  1156. }
  1157. }
  1158. list, _ := service.GetAllDealerList(adminUserInfo.CurrentOrgId)
  1159. manufacturerList, _ := service.GetAllManufacturerList(adminUserInfo.CurrentOrgId)
  1160. warehousingOutInfoList, _ := service.GetNewDrugWarehouseOutListById(lastWarehouseOut.ID, adminUserInfo.CurrentOrgId, tx)
  1161. tx.Commit()
  1162. var drugType = "药品类型"
  1163. drugTypeParent, _ := service.GetDrugDataConfig(0, drugType)
  1164. drugTypeList, _ := service.GetParentDataConfig(drugTypeParent.ID, adminUserInfo.CurrentOrgId)
  1165. c.ServeSuccessJSON(map[string]interface{}{
  1166. "msg": "2",
  1167. "drug_name": "",
  1168. "dose": "",
  1169. "dose_unit": "",
  1170. "min_number": "",
  1171. "min_unit": "",
  1172. "max_unit": "",
  1173. "dealerList": list,
  1174. "manufacturerList": manufacturerList,
  1175. "warehousingOutInfo": warehousingOutInfoList,
  1176. "warehouseOut": lastWarehouseOut,
  1177. "drugTypeList": drugTypeList,
  1178. })
  1179. return
  1180. }
  1181. func (c *StockDrugApiController) GetDrugWarehouseOutList() {
  1182. page, _ := c.GetInt64("page", -1)
  1183. limit, _ := c.GetInt64("limit", -1)
  1184. start_time := c.GetString("start_time")
  1185. end_time := c.GetString("end_time")
  1186. types, _ := c.GetInt64("type", 0)
  1187. keywords := c.GetString("keywords")
  1188. storehouse_id, _ := c.GetInt64("storehouse_id")
  1189. way_type, _ := c.GetInt64("way_type")
  1190. check_type, _ := c.GetInt64("check_type")
  1191. timeLayout := "2006-01-02"
  1192. loc, _ := time.LoadLocation("Local")
  1193. var startTime int64
  1194. if len(start_time) > 0 {
  1195. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  1196. if err != nil {
  1197. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1198. return
  1199. }
  1200. startTime = theTime.Unix()
  1201. }
  1202. var endTime int64
  1203. if len(end_time) > 0 {
  1204. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  1205. if err != nil {
  1206. utils.ErrorLog(err.Error())
  1207. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1208. return
  1209. }
  1210. endTime = theTime.Unix()
  1211. }
  1212. adminUserInfo := c.GetAdminUserInfo()
  1213. var ids []int64
  1214. var goodids []int64
  1215. if len(keywords) > 0 {
  1216. //查询商品名称
  1217. list, _ := service.GetDrugNameByKeyword(keywords, adminUserInfo.CurrentOrgId)
  1218. for _, item := range list {
  1219. goodids = append(goodids, item.ID)
  1220. }
  1221. if len(goodids) > 0 {
  1222. //出库详情但里面查询
  1223. info, _ := service.GetDrugWarehouseOrderDetail(goodids, adminUserInfo.CurrentOrgId, startTime, endTime, storehouse_id)
  1224. for _, it := range info {
  1225. ids = append(ids, it.WarehouseOutId)
  1226. }
  1227. }
  1228. }
  1229. warehouseOutList, total, err := service.FindAllDrugWarehouseOutListOne(adminUserInfo.CurrentOrgId, page, limit, startTime, endTime, types, keywords, ids, storehouse_id, way_type, check_type)
  1230. houseList, _ := service.GetAllStoreHouseList(adminUserInfo.CurrentOrgId)
  1231. patients, _ := service.GetAllpatient(adminUserInfo.CurrentOrgId)
  1232. fmt.Println(err)
  1233. if err == nil {
  1234. c.ServeSuccessJSON(map[string]interface{}{
  1235. "list": warehouseOutList,
  1236. "total": total,
  1237. "houseList": houseList,
  1238. "patients": patients,
  1239. })
  1240. } else {
  1241. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  1242. }
  1243. }
  1244. func (c *StockDrugApiController) DeleteDrugWarehouseOut() {
  1245. ids := c.GetString("ids")
  1246. if len(ids) == 0 {
  1247. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1248. return
  1249. }
  1250. idArray := strings.Split(ids, ",")
  1251. err := service.DeleteDrugWarehouseOut(idArray)
  1252. //list, _ := service.GetDrugWarhouseOutByIds(idArray)
  1253. //for _, item := range list {
  1254. // medical, _ := service.GetBaseDrugMedical(item.DrugId)
  1255. // if item.CountUnit == medical.MaxUnit {
  1256. //
  1257. // service.UpdateDrugInfoByIds(item.WarehouseInfoId, item.Count)
  1258. //
  1259. // }
  1260. // if medical.MaxUnit != medical.MinUnit && item.CountUnit == medical.MinUnit {
  1261. // service.UpdateDrugInfoByIdsOne(item.WarehouseInfoId, item.Count)
  1262. // }
  1263. //
  1264. // //查询默认仓库
  1265. // houseConfig, _ := service.GetAllStoreHouseConfig(item.OrgId)
  1266. // //查询默认仓库剩余多少库存
  1267. // list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  1268. // var sum_count int64
  1269. // var sum_in_count int64
  1270. // for _, it := range list {
  1271. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  1272. // if it.MaxUnit == baseDrug.MaxUnit {
  1273. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  1274. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  1275. // }
  1276. // sum_count += it.StockMaxNumber + it.StockMinNumber
  1277. // sum_in_count += it.WarehousingCount
  1278. // }
  1279. // service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.OrgId)
  1280. //}
  1281. if err != nil {
  1282. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  1283. } else {
  1284. c.ServeSuccessJSON(map[string]interface{}{
  1285. "msg": "删除成功",
  1286. })
  1287. }
  1288. }
  1289. func (this *StockDrugApiController) DeleteDrugWarehouseOutInfo() {
  1290. id, _ := this.GetInt64("id", 0)
  1291. if id == 0 {
  1292. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1293. return
  1294. }
  1295. ////查询详情
  1296. //info, _ := service.GetDrugWarehosueOutInfo(id)
  1297. //
  1298. ////查询默认仓库
  1299. //houseConfig, _ := service.GetAllStoreHouseConfig(info.OrgId)
  1300. ////查询默认仓库剩余多少库存
  1301. //list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, info.OrgId, info.DrugId)
  1302. //
  1303. //for _, it := range list {
  1304. //
  1305. // //查询默认仓库
  1306. // houseConfig, _ := service.GetAllStoreHouseConfig(it.OrgId)
  1307. // //查询默认仓库剩余多少库存
  1308. // list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, it.OrgId, it.DrugId)
  1309. // var sum_count int64
  1310. // var sum_in_count int64
  1311. // for _, it := range list {
  1312. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  1313. // if it.MaxUnit == baseDrug.MaxUnit {
  1314. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  1315. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  1316. // }
  1317. // sum_count += it.StockMaxNumber + it.StockMinNumber
  1318. // sum_in_count += it.WarehousingCount
  1319. // }
  1320. // service.UpdateMedicalSumCount(it.DrugId, sum_count, sum_in_count, it.OrgId)
  1321. //
  1322. //}
  1323. //删除流水
  1324. //service.DeleteDrugWarehouseOutInfoById(info.ID)
  1325. err := service.UpDateDrugWarehouseOutStatus(id)
  1326. if err != nil {
  1327. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  1328. } else {
  1329. this.ServeSuccessJSON(map[string]interface{}{
  1330. "msg": "删除成功",
  1331. })
  1332. }
  1333. }
  1334. func (c *StockDrugApiController) GetDrugWarehouseOutInfoList() {
  1335. id, _ := c.GetInt64("id", 0)
  1336. adminInfo := c.GetAdminUserInfo()
  1337. warehouseOutInfo, _ := service.FindDrugWarehouseOutInfoById(id, adminInfo.CurrentOrgId)
  1338. warehouseOut, _ := service.FindDrugWareHouseOutById(id, adminInfo.CurrentOrgId)
  1339. manulist, _ := service.GetAllManufacturerList(adminInfo.CurrentOrgId)
  1340. dealerList, _ := service.GetAllDealerList(adminInfo.CurrentOrgId)
  1341. houstList, _ := service.GetAllStoreHouseList(adminInfo.CurrentOrgId)
  1342. appId := c.GetAdminUserInfo().CurrentAppId
  1343. doctorList, _ := service.GetAllDoctorListSix(adminInfo.CurrentOrgId, appId)
  1344. patients, _ := service.GetAllpatientTwenty(adminInfo.CurrentOrgId)
  1345. c.ServeSuccessJSON(map[string]interface{}{
  1346. "list": warehouseOutInfo,
  1347. "info": warehouseOut,
  1348. "manulist": manulist,
  1349. "dealerList": dealerList,
  1350. "houstList": houstList,
  1351. "doctorList": doctorList,
  1352. "patients": patients,
  1353. })
  1354. }
  1355. func (c *StockDrugApiController) EditDrugWarehouseOut() {
  1356. warehouse_out_time := c.GetString("warehouse_out_time")
  1357. id, _ := c.GetInt64("id", 0)
  1358. types, _ := c.GetInt64("type", 0)
  1359. storehouse_id, _ := c.GetInt64("storehouse_id")
  1360. if id == 0 {
  1361. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1362. return
  1363. }
  1364. ctime := time.Now().Unix()
  1365. mtime := time.Now().Unix()
  1366. adminUserInfo := c.GetAdminUserInfo()
  1367. warehouseOutDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", warehouse_out_time)
  1368. if parseDateErr != nil {
  1369. c.ErrorLog("日期(%v)解析错误:%v", warehouseOutDate, parseDateErr)
  1370. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1371. return
  1372. }
  1373. // 开始外部循环的事务
  1374. db := service.XTWriteDB()
  1375. tx := db.Begin()
  1376. // 在函数结束时处理事务回滚
  1377. defer func() {
  1378. if r := recover(); r != nil {
  1379. tx.Rollback()
  1380. }
  1381. }()
  1382. warehouseOut, _ := service.FindNewDrugWareHouseOutById(id, adminUserInfo.CurrentOrgId, tx)
  1383. tempWarehouseOut := models.DrugWarehouseOut{
  1384. ID: warehouseOut.ID,
  1385. Mtime: mtime,
  1386. WarehouseOutTime: warehouseOutDate.Unix(),
  1387. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1388. StorehouseId: storehouse_id,
  1389. }
  1390. service.EditNewDrugWarehouseOut(tempWarehouseOut, tx)
  1391. dataBody := make(map[string]interface{}, 0)
  1392. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  1393. if err != nil {
  1394. utils.ErrorLog(err.Error())
  1395. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1396. return
  1397. }
  1398. var warehousingOutInfo []*models.DrugWarehouseOutInfo
  1399. var upDateWarehouseOutInfos []*models.DrugWarehouseOutInfo
  1400. var drugFlow []*models.DrugFlow
  1401. if dataBody["stockOut"] != nil && reflect.TypeOf(dataBody["stockOut"]).String() == "[]interface {}" {
  1402. thisStockOut, _ := dataBody["stockOut"].([]interface{})
  1403. if len(thisStockOut) > 0 {
  1404. for _, item := range thisStockOut {
  1405. items := item.(map[string]interface{})
  1406. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  1407. utils.ErrorLog("drug_id")
  1408. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1409. return
  1410. }
  1411. drug_id := int64(items["drug_id"].(float64))
  1412. if items["count"] == nil || reflect.TypeOf(items["count"]).String() != "string" {
  1413. utils.ErrorLog("count")
  1414. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1415. return
  1416. }
  1417. count, _ := strconv.ParseInt(items["count"].(string), 10, 64)
  1418. count_unit, _ := items["count_unit"].(string)
  1419. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  1420. utils.ErrorLog("price")
  1421. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1422. return
  1423. }
  1424. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  1425. if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  1426. utils.ErrorLog("retail_price")
  1427. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1428. return
  1429. }
  1430. retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  1431. total := float64(count) * price
  1432. retail_price_total := float64(count) * retail_price
  1433. remark := items["remark"].(string)
  1434. if items["id"] == nil || reflect.TypeOf(items["id"]).String() != "float64" {
  1435. utils.ErrorLog("id")
  1436. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1437. return
  1438. }
  1439. id := int64(items["id"].(float64))
  1440. dealer := int64(items["dealer"].(float64))
  1441. manufacturer := int64(items["manufacturer"].(float64))
  1442. batch_number := items["batch_number"].(string)
  1443. number := items["number"].(string)
  1444. max_unit := items["count_unit"].(string)
  1445. warehouse_info_id := int64(items["warehouse_info_id"].(float64))
  1446. var productDates int64
  1447. var expiryDates int64
  1448. if items["expiry_date"] == nil || reflect.TypeOf(items["expiry_date"]).String() != "string" {
  1449. expiryDates = 0
  1450. } else {
  1451. if len(items["expiry_date"].(string)) == 0 {
  1452. expiryDates = 0
  1453. } else {
  1454. expiryDate, _ := items["expiry_date"].(string)
  1455. expiry_date, _ := utils.ParseTimeStringToTime("2006-01-02", expiryDate)
  1456. expiryDates = expiry_date.Unix()
  1457. }
  1458. }
  1459. if items["product_date"] == nil || reflect.TypeOf(items["product_date"]).String() != "string" {
  1460. productDates = 0
  1461. } else {
  1462. if len(items["product_date"].(string)) == 0 {
  1463. productDates = 0
  1464. } else {
  1465. productDate, _ := items["product_date"].(string)
  1466. product_date, _ := utils.ParseTimeStringToTime("2006-01-02", productDate)
  1467. productDates = product_date.Unix()
  1468. }
  1469. }
  1470. admin_user_id := int64(items["admin_user_id"].(float64))
  1471. patient_id := int64(items["patient_id"].(float64))
  1472. stock_count, _ := items["stock_count"].(string)
  1473. class_type, _ := items["class_type"].(string)
  1474. if id == 0 {
  1475. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1476. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1477. WarehouseOutId: warehouseOut.ID,
  1478. DrugId: drug_id,
  1479. Count: count,
  1480. Price: price,
  1481. TotalPrice: total,
  1482. Status: 1,
  1483. Ctime: warehouseOut.WarehouseOutTime,
  1484. Mtime: ctime,
  1485. Remark: remark,
  1486. OrgId: adminUserInfo.CurrentOrgId,
  1487. Type: types,
  1488. Manufacturer: manufacturer,
  1489. Dealer: dealer,
  1490. IsSys: 0,
  1491. SysRecordTime: warehouseOut.WarehouseOutTime,
  1492. RetailPrice: retail_price,
  1493. RetailTotalPrice: retail_price_total,
  1494. BatchNumber: batch_number,
  1495. Number: number,
  1496. ProductDate: productDates,
  1497. ExpiryDate: expiryDates,
  1498. CountUnit: count_unit,
  1499. WarehouseInfoId: warehouse_info_id,
  1500. StorehouseId: storehouse_id,
  1501. AdminUserId: admin_user_id,
  1502. StockCount: stock_count,
  1503. PatientId: patient_id,
  1504. ClassType: class_type,
  1505. }
  1506. warehousingOutInfo = append(warehousingOutInfo, warehouseOutInfo)
  1507. drugflow := &models.DrugFlow{
  1508. WarehousingOrder: "",
  1509. WarehousingId: 0,
  1510. DrugId: drug_id,
  1511. Number: number,
  1512. ProductDate: productDates,
  1513. ExpireDate: expiryDates,
  1514. Count: count,
  1515. Price: price,
  1516. Status: 1,
  1517. Ctime: warehouseOut.WarehouseOutTime,
  1518. Mtime: ctime,
  1519. UserOrgId: adminUserInfo.CurrentOrgId,
  1520. Manufacturer: manufacturer,
  1521. Dealer: dealer,
  1522. BatchNumber: batch_number,
  1523. MaxUnit: max_unit,
  1524. ConsumableType: 1,
  1525. IsEdit: 1,
  1526. Creator: adminUserInfo.AdminUser.Id,
  1527. IsSys: 0,
  1528. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1529. WarehouseOutId: id,
  1530. StorehouseId: storehouse_id,
  1531. AdminUserId: admin_user_id,
  1532. StockCount: stock_count,
  1533. PatientId: patient_id,
  1534. }
  1535. drugFlow = append(drugFlow, drugflow)
  1536. } else {
  1537. if items["is_sys"] == nil || reflect.TypeOf(items["is_sys"]).String() != "float64" {
  1538. utils.ErrorLog("is_sys")
  1539. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1540. return
  1541. }
  1542. is_sys := int64(items["is_sys"].(float64))
  1543. if items["sys_record_time"] == nil || reflect.TypeOf(items["sys_record_time"]).String() != "float64" {
  1544. utils.ErrorLog("sys_record_time")
  1545. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1546. return
  1547. }
  1548. sys_record_time := int64(items["sys_record_time"].(float64))
  1549. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1550. ID: id,
  1551. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1552. WarehouseOutId: warehouseOut.ID,
  1553. DrugId: drug_id,
  1554. Count: count,
  1555. Price: price,
  1556. TotalPrice: total,
  1557. Status: 1,
  1558. Ctime: warehouseOut.WarehouseOutTime,
  1559. Remark: remark,
  1560. OrgId: adminUserInfo.CurrentOrgId,
  1561. Mtime: time.Now().Unix(),
  1562. Type: types,
  1563. Manufacturer: manufacturer,
  1564. Dealer: dealer,
  1565. IsSys: is_sys,
  1566. SysRecordTime: sys_record_time,
  1567. RetailPrice: retail_price,
  1568. RetailTotalPrice: retail_price_total,
  1569. BatchNumber: batch_number,
  1570. Number: number,
  1571. ProductDate: productDates,
  1572. ExpiryDate: expiryDates,
  1573. CountUnit: count_unit,
  1574. WarehouseInfoId: warehouse_info_id,
  1575. StorehouseId: storehouse_id,
  1576. AdminUserId: admin_user_id,
  1577. StockCount: stock_count,
  1578. PatientId: patient_id,
  1579. ClassType: class_type,
  1580. }
  1581. upDateWarehouseOutInfos = append(upDateWarehouseOutInfos, warehouseOutInfo)
  1582. drugflow := &models.DrugFlow{
  1583. WarehousingOrder: "",
  1584. WarehousingId: 0,
  1585. DrugId: drug_id,
  1586. Number: number,
  1587. ProductDate: productDates,
  1588. ExpireDate: expiryDates,
  1589. Count: count,
  1590. Price: price,
  1591. Status: 1,
  1592. Ctime: warehouseOut.WarehouseOutTime,
  1593. Mtime: time.Now().Unix(),
  1594. UserOrgId: adminUserInfo.CurrentOrgId,
  1595. Manufacturer: manufacturer,
  1596. Dealer: dealer,
  1597. BatchNumber: batch_number,
  1598. MaxUnit: max_unit,
  1599. ConsumableType: 1,
  1600. IsEdit: 1,
  1601. Creator: adminUserInfo.AdminUser.Id,
  1602. IsSys: 0,
  1603. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1604. WarehouseOutId: warehouseOut.ID,
  1605. StorehouseId: storehouse_id,
  1606. AdminUserId: admin_user_id,
  1607. StockCount: stock_count,
  1608. PatientId: patient_id,
  1609. }
  1610. drugFlow = append(drugFlow, drugflow)
  1611. }
  1612. }
  1613. }
  1614. }
  1615. var errs error
  1616. if len(warehousingOutInfo) > 0 {
  1617. //调用出库逻辑
  1618. for _, item := range warehousingOutInfo {
  1619. errOnes := service.AddNewSigleDrugWarehouseOutInfo(item, tx)
  1620. fmt.Println("errone", errOnes)
  1621. }
  1622. }
  1623. if len(upDateWarehouseOutInfos) > 0 {
  1624. for _, item := range upDateWarehouseOutInfos {
  1625. if item.ID > 0 {
  1626. service.UpdatedNewDrugWarehouseInfo(item, item.ID, tx)
  1627. }
  1628. }
  1629. }
  1630. tx.Commit()
  1631. if errs != nil {
  1632. utils.ErrorLog(errs.Error())
  1633. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockOutFail)
  1634. return
  1635. }
  1636. c.ServeSuccessJSON(map[string]interface{}{
  1637. "msg": "2",
  1638. "drug_name": "",
  1639. "dose": "",
  1640. "dose_unit": "",
  1641. "min_number": "",
  1642. "min_unit": "",
  1643. "max_unit": "",
  1644. })
  1645. }
  1646. func (c *StockDrugApiController) CreateDrugCancelStock() {
  1647. dealer_id, _ := c.GetInt64("dealer_id", 0)
  1648. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  1649. cancel_stock_time := c.GetString("time")
  1650. types, _ := c.GetInt64("type", 0)
  1651. storehouse_id, _ := c.GetInt64("storehouse_id")
  1652. cancelstock_id, _ := c.GetInt64("cancelstock_id")
  1653. cancelStockDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", cancel_stock_time)
  1654. if parseDateErr != nil {
  1655. c.ErrorLog("日期(%v)解析错误:%v", cancelStockDate, parseDateErr)
  1656. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1657. return
  1658. }
  1659. adminUserInfo := c.GetAdminUserInfo()
  1660. operation_time := time.Now().Unix()
  1661. creater := adminUserInfo.AdminUser.Id
  1662. ctime := time.Now().Unix()
  1663. timeStr := time.Now().Format("2006-01-02")
  1664. timeArr := strings.Split(timeStr, "-")
  1665. total, _ := service.FindAllDrugCancelStockTotal(adminUserInfo.CurrentOrgId)
  1666. total = total + 1
  1667. orderNumber := "CKTKD" + strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  1668. cancelStock := models.DrugCancelStock{
  1669. OrderNumber: orderNumber,
  1670. OperaTime: operation_time,
  1671. OrgId: adminUserInfo.CurrentOrgId,
  1672. Creater: creater,
  1673. Ctime: ctime,
  1674. Status: 1,
  1675. ReturnTime: cancelStockDate.Unix(),
  1676. Dealer: dealer_id,
  1677. Manufacturer: manufacturer_id,
  1678. Type: types,
  1679. StorehouseId: storehouse_id,
  1680. IsCheck: 2,
  1681. }
  1682. if cancelstock_id == 0 {
  1683. service.AddSigleDrugCancelStock(&cancelStock)
  1684. }
  1685. if cancelstock_id > 0 {
  1686. service.UpdateSingleDrugCancelStock(cancelstock_id, storehouse_id)
  1687. }
  1688. cancelLastStock, _ := service.GetLastDrugCancelStockById(adminUserInfo.CurrentOrgId)
  1689. dataBody := make(map[string]interface{}, 0)
  1690. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  1691. if err != nil {
  1692. utils.ErrorLog(err.Error())
  1693. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1694. return
  1695. }
  1696. var cancelStockInfos []*models.DrugCancelStockInfo
  1697. var drugFlow []*models.DrugFlow
  1698. if dataBody["cancelStock"] != nil && reflect.TypeOf(dataBody["cancelStock"]).String() == "[]interface {}" {
  1699. thisStockIn, _ := dataBody["cancelStock"].([]interface{})
  1700. if len(thisStockIn) > 0 {
  1701. for _, item := range thisStockIn {
  1702. items := item.(map[string]interface{})
  1703. if items["id"] == nil || reflect.TypeOf(items["id"]).String() != "float64" {
  1704. utils.ErrorLog("id")
  1705. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1706. return
  1707. }
  1708. id := int64(items["id"].(float64))
  1709. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  1710. utils.ErrorLog("drug_id")
  1711. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1712. return
  1713. }
  1714. drug_id := int64(items["drug_id"].(float64))
  1715. return_count, _ := items["return_count"].(string)
  1716. if len(return_count) == 0 {
  1717. utils.ErrorLog("len(return_count) == 0")
  1718. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1719. return
  1720. }
  1721. count, _ := strconv.ParseInt(return_count, 10, 64)
  1722. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  1723. utils.ErrorLog("price")
  1724. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1725. return
  1726. }
  1727. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  1728. total := float64(count) * price
  1729. if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  1730. utils.ErrorLog("retail_price")
  1731. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1732. return
  1733. }
  1734. retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  1735. retail_price_total := float64(count) * retail_price
  1736. register_account, _ := items["register_account"].(string)
  1737. remark, _ := items["remark"].(string)
  1738. var productDates int64
  1739. var expiryDates int64
  1740. if items["expiry_date"] == nil || reflect.TypeOf(items["expiry_date"]).String() != "string" {
  1741. expiryDates = 0
  1742. } else {
  1743. if len(items["expiry_date"].(string)) == 0 {
  1744. expiryDates = 0
  1745. } else {
  1746. expiryDate, _ := items["expiry_date"].(string)
  1747. expiry_date, _ := utils.ParseTimeStringToTime("2006-01-02", expiryDate)
  1748. expiryDates = expiry_date.Unix()
  1749. }
  1750. }
  1751. if items["product_date"] == nil || reflect.TypeOf(items["product_date"]).String() != "string" {
  1752. productDates = 0
  1753. } else {
  1754. if len(items["product_date"].(string)) == 0 {
  1755. productDates = 0
  1756. } else {
  1757. productDate, _ := items["product_date"].(string)
  1758. product_date, _ := utils.ParseTimeStringToTime("2006-01-02", productDate)
  1759. productDates = product_date.Unix()
  1760. }
  1761. }
  1762. manufacturer, _ := items["manufacturer"].(string)
  1763. dealer, _ := items["dealer"].(string)
  1764. batch_number := items["batch_number"].(string)
  1765. max_unit := items["max_unit"].(string)
  1766. batch_number_id := int64(items["batch_number_id"].(float64))
  1767. var manufacturer_id int64
  1768. var dealer_id int64
  1769. manufacturerList, _ := service.GetAllManufacturerList(adminUserInfo.CurrentOrgId)
  1770. dealerList, _ := service.GetAllDealerList(adminUserInfo.CurrentOrgId)
  1771. for _, item := range manufacturerList {
  1772. if manufacturer == item.ManufacturerName {
  1773. manufacturer_id = item.ID
  1774. }
  1775. }
  1776. for _, item := range dealerList {
  1777. if dealer == item.DealerName {
  1778. dealer_id = item.ID
  1779. }
  1780. }
  1781. cancelInfo, _ := service.GetLastDrugCancelStockById(adminUserInfo.CurrentOrgId)
  1782. cancelStockInfo := &models.DrugCancelStockInfo{
  1783. ID: id,
  1784. OrderNumber: cancelInfo.OrderNumber,
  1785. CancelStockId: cancelInfo.ID,
  1786. DrugId: drug_id,
  1787. Count: count,
  1788. Status: 1,
  1789. Ctime: ctime,
  1790. OrgId: adminUserInfo.CurrentOrgId,
  1791. Type: types,
  1792. Manufacturer: manufacturer,
  1793. Dealer: dealer,
  1794. Total: total,
  1795. RetailPrice: retail_price,
  1796. RetailTotalPrice: retail_price_total,
  1797. Price: price,
  1798. RegisterAccount: register_account,
  1799. Remark: remark,
  1800. BatchNumber: batch_number,
  1801. MaxUnit: max_unit,
  1802. ProductDate: productDates,
  1803. ExpiryDate: expiryDates,
  1804. BatchNumberId: batch_number_id,
  1805. StorehouseId: storehouse_id,
  1806. IsCheck: 2,
  1807. }
  1808. cancelStockInfos = append(cancelStockInfos, cancelStockInfo)
  1809. flow := &models.DrugFlow{
  1810. WarehousingId: 0,
  1811. DrugId: drug_id,
  1812. Number: "",
  1813. BatchNumber: batch_number,
  1814. Count: count,
  1815. UserOrgId: adminUserInfo.CurrentOrgId,
  1816. PatientId: 0,
  1817. SystemTime: ctime,
  1818. ConsumableType: 4,
  1819. IsSys: 0,
  1820. WarehousingOrder: "",
  1821. WarehouseOutId: 0,
  1822. WarehouseOutOrderNumber: "",
  1823. IsEdit: 0,
  1824. CancelStockId: cancelStock.ID,
  1825. CancelOrderNumber: cancelStock.OrderNumber,
  1826. Manufacturer: manufacturer_id,
  1827. Dealer: dealer_id,
  1828. Creator: adminUserInfo.AdminUser.Id,
  1829. UpdateCreator: 0,
  1830. Status: 1,
  1831. Ctime: time.Now().Unix(),
  1832. Mtime: 0,
  1833. Price: price,
  1834. WarehousingDetailId: 0,
  1835. WarehouseOutDetailId: 0,
  1836. CancelOutDetailId: 0,
  1837. ExpireDate: expiryDates,
  1838. ProductDate: productDates,
  1839. MaxUnit: max_unit,
  1840. MinUnit: "",
  1841. StorehouseId: storehouse_id,
  1842. }
  1843. drugFlow = append(drugFlow, flow)
  1844. }
  1845. }
  1846. }
  1847. for _, item := range cancelStockInfos {
  1848. if item.ID == 0 {
  1849. service.CreateCancelStockInfoTwo(item)
  1850. }
  1851. if item.ID > 0 {
  1852. service.ModifyDrugCancelStockInfo(item.ID, item)
  1853. }
  1854. }
  1855. list, _ := service.GetLastCancelStockByList(cancelLastStock.ID, adminUserInfo.CurrentOrgId)
  1856. manufacturerList, _ := service.GetAllManufacturerList(adminUserInfo.CurrentOrgId)
  1857. dealerList, _ := service.GetAllDealerList(adminUserInfo.CurrentOrgId)
  1858. var drugType = "药品类型"
  1859. drugTypeParent, _ := service.GetDrugDataConfig(0, drugType)
  1860. drugTypeList, _ := service.GetParentDataConfig(drugTypeParent.ID, adminUserInfo.CurrentOrgId)
  1861. c.ServeSuccessJSON(map[string]interface{}{
  1862. "cancelStock": cancelLastStock,
  1863. "cancelLastStock": cancelLastStock,
  1864. "list": list,
  1865. "manufacturerList": manufacturerList,
  1866. "dealerList": dealerList,
  1867. "drugTypeList": drugTypeList,
  1868. "msg": "1",
  1869. })
  1870. //扣减库存逻辑
  1871. //for _, item := range cancelStockInfos {
  1872. //
  1873. // var total_number int64
  1874. // var out_number int64
  1875. // var can_number int64
  1876. // medical, _ := service.GetBaseDrugMedical(item.DrugId)
  1877. // //将当前退库数转为最小单位
  1878. // if item.MaxUnit == medical.MaxUnit {
  1879. // total_number = item.Count * medical.MinNumber
  1880. // }
  1881. // if item.MaxUnit == medical.MinUnit {
  1882. // total_number = item.Count
  1883. // }
  1884. // //查询当前药品退库的批次号的总入库数
  1885. // infoWareInfo, _ := service.GetDrugWarehouseInfo(item.BatchNumberId)
  1886. // var total_count int64
  1887. // total_count = infoWareInfo.WarehousingCount * medical.MinNumber
  1888. //
  1889. // //查询该批次的出库数量
  1890. // outInfo, _ := service.GetDrugWarehouseOutInfo(item.BatchNumberId, item.DrugId)
  1891. //
  1892. // //查询该批次总的退库数量
  1893. // canInfo, _ := service.GetCancelDrugStockOutInfo(item.BatchNumberId, item.DrugId)
  1894. //
  1895. // for _, it := range canInfo {
  1896. // if it.MaxUnit == medical.MaxUnit {
  1897. // it.Count = it.Count * medical.MinNumber
  1898. // }
  1899. // if it.MaxUnit == medical.MinUnit {
  1900. // it.Count = it.Count
  1901. // }
  1902. // }
  1903. //
  1904. // for _, it := range canInfo {
  1905. // can_number += it.Count
  1906. // }
  1907. //
  1908. // for _, it := range outInfo {
  1909. // if it.CountUnit == medical.MaxUnit {
  1910. // it.Count = it.Count * medical.MinNumber
  1911. // }
  1912. // if it.CountUnit == medical.MinUnit {
  1913. // it.Count = it.Count
  1914. // }
  1915. // }
  1916. // for _, it := range outInfo {
  1917. // out_number += it.Count
  1918. // }
  1919. //
  1920. // if out_number == 0 {
  1921. // service.UpdateDrugCancel(item.CancelStockId)
  1922. // c.ServeSuccessJSON(map[string]interface{}{
  1923. // "msg": "2",
  1924. // })
  1925. // return
  1926. // }
  1927. // fmt.Println("当前退库数", total_count)
  1928. // fmt.Println("当前批次总库存", total_count)
  1929. // fmt.Println("总出库数", out_number)
  1930. // //判断退库数量是否大于总入库数量
  1931. // if total_number > total_count {
  1932. // service.UpdateDrugCancel(item.CancelStockId)
  1933. // c.ServeSuccessJSON(map[string]interface{}{
  1934. // "msg": "2",
  1935. // })
  1936. // return
  1937. // }
  1938. //
  1939. // //判断总退库数量是否大于出库数量
  1940. //
  1941. // if can_number+total_number > out_number {
  1942. // //清空退库单
  1943. // service.UpdateDrugCancel(item.CancelStockId)
  1944. // c.ServeSuccessJSON(map[string]interface{}{
  1945. // "msg": "2",
  1946. // })
  1947. // return
  1948. // }
  1949. //
  1950. // //正常退库
  1951. // if total_number <= out_number {
  1952. // //创建退库详情
  1953. // errs := service.CreateCancelStockInfoTwo(item)
  1954. // lastInfo, _ := service.GetLastStockInfoThree(item.DrugId)
  1955. // //创建库存明细
  1956. // flows := models.DrugFlow{
  1957. // WarehousingId: 0,
  1958. // DrugId: item.DrugId,
  1959. // Number: "",
  1960. // BatchNumber: item.BatchNumber,
  1961. // Count: item.Count,
  1962. // UserOrgId: adminUserInfo.CurrentOrgId,
  1963. // PatientId: 0,
  1964. // SystemTime: ctime,
  1965. // ConsumableType: 4,
  1966. // IsSys: 0,
  1967. // WarehousingOrder: infoWareInfo.WarehousingOrder,
  1968. // WarehouseOutId: 0,
  1969. // WarehouseOutOrderNumber: "",
  1970. // IsEdit: 0,
  1971. // CancelStockId: cancelStock.ID,
  1972. // CancelOrderNumber: cancelStock.OrderNumber,
  1973. // Manufacturer: manufacturer_id,
  1974. // Dealer: dealer_id,
  1975. // Creator: adminUserInfo.AdminUser.Id,
  1976. // UpdateCreator: 0,
  1977. // Status: 1,
  1978. // Ctime: time.Now().Unix(),
  1979. // Mtime: 0,
  1980. // Price: item.Price,
  1981. // WarehousingDetailId: 0,
  1982. // WarehouseOutDetailId: 0,
  1983. // CancelOutDetailId: lastInfo.ID,
  1984. // ExpireDate: item.ExpiryDate,
  1985. // ProductDate: item.ProductDate,
  1986. // MaxUnit: item.MaxUnit,
  1987. // MinUnit: "",
  1988. // StorehouseId: storehouse_id,
  1989. // }
  1990. // errs = service.CreateDrugFlowOne(flows)
  1991. //
  1992. // var total int64
  1993. //
  1994. // if medical.MaxUnit == item.MaxUnit {
  1995. // total = item.Count
  1996. //
  1997. // warehouseInfo := models.XtDrugWarehouseInfo{
  1998. // StockMaxNumber: total,
  1999. // }
  2000. //
  2001. // errs = service.UpdateDrugWarehouseInfo(&warehouseInfo, item.BatchNumberId)
  2002. // //查询默认仓库
  2003. // houseConfig, _ := service.GetAllStoreHouseConfig(item.OrgId)
  2004. // //查询默认仓库剩余多少库存
  2005. // list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  2006. // var sum_count int64
  2007. // var sum_in_count int64
  2008. // for _, it := range list {
  2009. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  2010. // if it.MaxUnit == baseDrug.MaxUnit {
  2011. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  2012. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  2013. // }
  2014. // sum_count += it.StockMaxNumber + it.StockMinNumber
  2015. // sum_in_count += it.WarehousingCount
  2016. // }
  2017. // service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.OrgId)
  2018. // if errs != nil {
  2019. // utils.ErrorLog(errs.Error())
  2020. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCancelStockFail)
  2021. // return
  2022. // }
  2023. //
  2024. // c.ServeSuccessJSON(map[string]interface{}{
  2025. // "msg": "1",
  2026. // })
  2027. // }
  2028. //
  2029. // if medical.MinUnit == item.MaxUnit && medical.MaxUnit != medical.MinUnit {
  2030. // total = item.Count
  2031. // warehouseInfo := models.XtDrugWarehouseInfo{
  2032. // StockMinNumber: total,
  2033. // }
  2034. //
  2035. // errs = service.UpdateDrugWarehouseInfoOne(&warehouseInfo, item.BatchNumberId)
  2036. // //查询默认仓库
  2037. // houseConfig, _ := service.GetAllStoreHouseConfig(item.OrgId)
  2038. // //查询默认仓库剩余多少库存
  2039. // list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  2040. // var sum_count int64
  2041. // var sum_in_count int64
  2042. // for _, it := range list {
  2043. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  2044. // if it.MaxUnit == baseDrug.MaxUnit {
  2045. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  2046. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  2047. // }
  2048. // sum_count += it.StockMaxNumber + it.StockMinNumber
  2049. // sum_in_count += it.WarehousingCount
  2050. // }
  2051. // service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.OrgId)
  2052. // if errs != nil {
  2053. // utils.ErrorLog(errs.Error())
  2054. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCancelStockFail)
  2055. // return
  2056. // }
  2057. //
  2058. // c.ServeSuccessJSON(map[string]interface{}{
  2059. // "msg": "1",
  2060. // })
  2061. // }
  2062. // }
  2063. //
  2064. //}
  2065. }
  2066. func (c *StockDrugApiController) GetDrugCancelStockInfoList() {
  2067. id, _ := c.GetInt64("id", 0)
  2068. adminUserInfo := c.GetAdminUserInfo()
  2069. cancelStockInfoList, _ := service.FindDrugCancelStockInfoById(id, adminUserInfo.CurrentOrgId)
  2070. info, _ := service.FindCancelDrugById(id, adminUserInfo.CurrentOrgId)
  2071. list, _ := service.GetAllStoreHouseList(adminUserInfo.CurrentOrgId)
  2072. c.ServeSuccessJSON(map[string]interface{}{
  2073. "list": cancelStockInfoList,
  2074. "info": info,
  2075. "houselist": list,
  2076. })
  2077. }
  2078. func (c *StockDrugApiController) GetDrugCancelStockList() {
  2079. page, _ := c.GetInt64("page", -1)
  2080. limit, _ := c.GetInt64("limit", -1)
  2081. start_time := c.GetString("start_time")
  2082. end_time := c.GetString("end_time")
  2083. types, _ := c.GetInt64("type", 0)
  2084. keywords := c.GetString("keywords")
  2085. storehouse_id, _ := c.GetInt64("storehouse_id")
  2086. check_type, _ := c.GetInt64("check_type")
  2087. timeLayout := "2006-01-02"
  2088. loc, _ := time.LoadLocation("Local")
  2089. var startTime int64
  2090. if len(start_time) > 0 {
  2091. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  2092. if err != nil {
  2093. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2094. return
  2095. }
  2096. startTime = theTime.Unix()
  2097. }
  2098. var endTime int64
  2099. if len(end_time) > 0 {
  2100. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  2101. if err != nil {
  2102. utils.ErrorLog(err.Error())
  2103. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2104. return
  2105. }
  2106. endTime = theTime.Unix()
  2107. }
  2108. adminUserInfo := c.GetAdminUserInfo()
  2109. returnList, total, err := service.FindAllDrugCancelList(adminUserInfo.CurrentOrgId, page, limit, startTime, endTime, types, keywords, storehouse_id, check_type)
  2110. houseList, _ := service.GetAllStoreHouseList(adminUserInfo.CurrentOrgId)
  2111. if err == nil {
  2112. c.ServeSuccessJSON(map[string]interface{}{
  2113. "list": returnList,
  2114. "total": total,
  2115. "houseList": houseList,
  2116. })
  2117. } else {
  2118. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  2119. }
  2120. }
  2121. func (this *StockDrugApiController) DeleteDrugCancelStock() {
  2122. ids := this.GetString("ids")
  2123. if len(ids) == 0 {
  2124. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2125. return
  2126. }
  2127. idArray := strings.Split(ids, ",")
  2128. ////获取入库数据
  2129. //list, _ := service.GetDrugWareInfoByCancelId(idArray)
  2130. //for _, item := range list {
  2131. // medical, _ := service.GetBaseDrugMedical(item.DrugId)
  2132. // if item.MaxUnit == medical.MaxUnit {
  2133. // //回退库存
  2134. // service.ModifyDrugWarehouseByInfoId(item.BatchNumberId, item.Count)
  2135. //
  2136. // }
  2137. // if item.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  2138. // service.ModifyDrugWarehouseByStockMin(item.BatchNumberId, item.Count)
  2139. // }
  2140. //
  2141. // //查询默认仓库
  2142. // houseConfig, _ := service.GetAllStoreHouseConfig(item.OrgId)
  2143. // //查询默认仓库剩余多少库存
  2144. // list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  2145. // var sum_count int64
  2146. // var sum_in_count int64
  2147. // for _, it := range list {
  2148. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  2149. // if it.MaxUnit == baseDrug.MaxUnit {
  2150. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  2151. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  2152. // sum_count += it.StockMaxNumber + it.StockMinNumber
  2153. // sum_in_count += it.WarehousingCount
  2154. // }
  2155. // }
  2156. // service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.OrgId)
  2157. //}
  2158. err := service.DeleteDrugCancelStock(idArray)
  2159. if err != nil {
  2160. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  2161. } else {
  2162. this.ServeSuccessJSON(map[string]interface{}{
  2163. "msg": "删除成功",
  2164. })
  2165. }
  2166. }
  2167. func (this *StockDrugApiController) DeleteDrugCancelStockInfo() {
  2168. id, _ := this.GetInt64("id", 0)
  2169. if id == 0 {
  2170. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2171. return
  2172. }
  2173. ////查询
  2174. //cancelinfo, _ := service.GetDrugCancelById(id)
  2175. //
  2176. //drug, _ := service.GetBaseDrugMedical(cancelinfo.DrugId)
  2177. //if drug.MaxUnit == cancelinfo.MaxUnit {
  2178. // service.ModeFyDrugWareseInfo(cancelinfo.BatchNumberId, cancelinfo.Count)
  2179. //
  2180. //}
  2181. //if drug.MinUnit == cancelinfo.MaxUnit && drug.MaxUnit != drug.MinUnit {
  2182. // service.ModeFyDrugWarehouseMin(cancelinfo.BatchNumberId, cancelinfo.Count)
  2183. //}
  2184. //
  2185. err := service.UpDateDrugCancleStockStatus(id)
  2186. //删除流水
  2187. // service.UpDateDrugCancelStockById(cancelinfo.ID)
  2188. //查询默认仓库
  2189. //houseConfig, _ := service.GetAllStoreHouseConfig(cancelinfo.OrgId)
  2190. ////查询默认仓库剩余多少库存
  2191. //list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, cancelinfo.OrgId, cancelinfo.DrugId)
  2192. //var sum_count int64
  2193. //var sum_in_count int64
  2194. //for _, it := range list {
  2195. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  2196. // if it.MaxUnit == baseDrug.MaxUnit {
  2197. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  2198. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  2199. // }
  2200. // sum_count += it.StockMaxNumber + it.StockMinNumber
  2201. // sum_in_count += it.WarehousingCount
  2202. //}
  2203. //service.UpdateMedicalSumCount(cancelinfo.DrugId, sum_count, sum_in_count, cancelinfo.OrgId)
  2204. if err != nil {
  2205. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  2206. } else {
  2207. this.ServeSuccessJSON(map[string]interface{}{
  2208. "msg": "删除成功",
  2209. })
  2210. }
  2211. }
  2212. func (c *StockDrugApiController) EditDrugCancelStock() {
  2213. cancel_time := c.GetString("cancel_time")
  2214. id, _ := c.GetInt64("id", 0)
  2215. types, _ := c.GetInt64("type", 0)
  2216. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  2217. dealer_id, _ := c.GetInt64("dealer_id", 0)
  2218. storehouse_id, _ := c.GetInt64("storehouse_id")
  2219. if id == 0 {
  2220. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2221. return
  2222. }
  2223. ctime := time.Now().Unix()
  2224. mtime := time.Now().Unix()
  2225. adminUserInfo := c.GetAdminUserInfo()
  2226. cancelDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", cancel_time)
  2227. if parseDateErr != nil {
  2228. c.ErrorLog("日期(%v)解析错误:%v", cancelDate, parseDateErr)
  2229. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2230. return
  2231. }
  2232. cancelStock, _ := service.FindDrugCancelStockById(id)
  2233. tempCancelStock := models.DrugCancelStock{
  2234. ID: cancelStock.ID,
  2235. Mtime: mtime,
  2236. ReturnTime: cancelDate.Unix(),
  2237. Manufacturer: manufacturer_id,
  2238. Dealer: dealer_id,
  2239. StorehouseId: storehouse_id,
  2240. IsCheck: 1,
  2241. }
  2242. service.EditDrugCancelStock(tempCancelStock)
  2243. dataBody := make(map[string]interface{}, 0)
  2244. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  2245. if err != nil {
  2246. utils.ErrorLog(err.Error())
  2247. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2248. return
  2249. }
  2250. var cancelStockInfos []*models.DrugCancelStockInfo
  2251. var upDateCancelStockInfos []*models.DrugCancelStockInfo
  2252. var drugFlow []*models.DrugFlow
  2253. if dataBody["cancelStock"] != nil && reflect.TypeOf(dataBody["cancelStock"]).String() == "[]interface {}" {
  2254. thisCancelStock, _ := dataBody["cancelStock"].([]interface{})
  2255. if len(thisCancelStock) > 0 {
  2256. for _, item := range thisCancelStock {
  2257. items := item.(map[string]interface{})
  2258. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  2259. utils.ErrorLog("drug_id")
  2260. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2261. return
  2262. }
  2263. drug_id := int64(items["drug_id"].(float64))
  2264. if items["count"] == nil || reflect.TypeOf(items["count"]).String() != "string" {
  2265. utils.ErrorLog("count")
  2266. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2267. return
  2268. }
  2269. count, _ := strconv.ParseInt(items["count"].(string), 10, 64)
  2270. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  2271. utils.ErrorLog("price")
  2272. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2273. return
  2274. }
  2275. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  2276. total := float64(count) * price
  2277. if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  2278. utils.ErrorLog("retail_price")
  2279. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2280. return
  2281. }
  2282. retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  2283. retail_price_total := float64(count) * retail_price
  2284. if items["id"] == nil || reflect.TypeOf(items["id"]).String() != "float64" {
  2285. utils.ErrorLog("id")
  2286. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2287. return
  2288. }
  2289. id := int64(items["id"].(float64))
  2290. max_unit := items["max_unit"].(string)
  2291. batch_number := items["batch_number"].(string)
  2292. register_account := items["register_account"].(string)
  2293. remark := items["remark"].(string)
  2294. manufacturer := items["manufacturer"].(string)
  2295. dealer := items["dealer"].(string)
  2296. var productDates int64
  2297. var expiryDates int64
  2298. if items["expiry_date"] == nil || reflect.TypeOf(items["expiry_date"]).String() != "string" {
  2299. expiryDates = 0
  2300. } else {
  2301. if len(items["expiry_date"].(string)) == 0 {
  2302. expiryDates = 0
  2303. } else {
  2304. expiryDate, _ := items["expiry_date"].(string)
  2305. expiry_date, _ := utils.ParseTimeStringToTime("2006-01-02", expiryDate)
  2306. expiryDates = expiry_date.Unix()
  2307. }
  2308. }
  2309. if items["product_date"] == nil || reflect.TypeOf(items["product_date"]).String() != "string" {
  2310. productDates = 0
  2311. } else {
  2312. if len(items["product_date"].(string)) == 0 {
  2313. productDates = 0
  2314. } else {
  2315. productDate, _ := items["product_date"].(string)
  2316. product_date, _ := utils.ParseTimeStringToTime("2006-01-02", productDate)
  2317. productDates = product_date.Unix()
  2318. }
  2319. }
  2320. batch_number_id := int64(items["batch_number_id"].(float64))
  2321. if id == 0 {
  2322. cancelStockInfo := &models.DrugCancelStockInfo{
  2323. DrugId: drug_id,
  2324. Count: count,
  2325. Status: 1,
  2326. Ctime: ctime,
  2327. OrgId: adminUserInfo.CurrentOrgId,
  2328. OrderNumber: cancelStock.OrderNumber,
  2329. CancelStockId: cancelStock.ID,
  2330. Mtime: time.Now().Unix(),
  2331. Type: types,
  2332. RetailPrice: retail_price,
  2333. RetailTotalPrice: retail_price_total,
  2334. Price: price,
  2335. Total: total,
  2336. Manufacturer: manufacturer,
  2337. Dealer: dealer,
  2338. RegisterAccount: register_account,
  2339. MaxUnit: max_unit,
  2340. BatchNumber: batch_number,
  2341. Remark: remark,
  2342. ProductDate: productDates,
  2343. ExpiryDate: expiryDates,
  2344. BatchNumberId: batch_number_id,
  2345. StorehouseId: storehouse_id,
  2346. }
  2347. cancelStockInfos = append(cancelStockInfos, cancelStockInfo)
  2348. } else {
  2349. cancelStockInfo := &models.DrugCancelStockInfo{
  2350. ID: id,
  2351. DrugId: drug_id,
  2352. Count: count,
  2353. Status: 1,
  2354. Ctime: ctime,
  2355. OrgId: adminUserInfo.CurrentOrgId,
  2356. OrderNumber: cancelStock.OrderNumber,
  2357. CancelStockId: cancelStock.ID,
  2358. Mtime: time.Now().Unix(),
  2359. Type: types,
  2360. RetailPrice: retail_price,
  2361. RetailTotalPrice: retail_price_total,
  2362. Price: price,
  2363. Total: total,
  2364. Manufacturer: manufacturer,
  2365. Dealer: dealer,
  2366. RegisterAccount: register_account,
  2367. MaxUnit: max_unit,
  2368. BatchNumber: batch_number,
  2369. Remark: remark,
  2370. ProductDate: productDates,
  2371. ExpiryDate: expiryDates,
  2372. BatchNumberId: batch_number_id,
  2373. StorehouseId: storehouse_id,
  2374. }
  2375. upDateCancelStockInfos = append(upDateCancelStockInfos, cancelStockInfo)
  2376. }
  2377. flow := &models.DrugFlow{
  2378. WarehousingId: 0,
  2379. DrugId: drug_id,
  2380. Number: "",
  2381. BatchNumber: batch_number,
  2382. Count: count,
  2383. UserOrgId: adminUserInfo.CurrentOrgId,
  2384. PatientId: 0,
  2385. SystemTime: time.Now().Unix(),
  2386. ConsumableType: 4,
  2387. IsSys: 0,
  2388. WarehousingOrder: "",
  2389. WarehouseOutId: 0,
  2390. WarehouseOutOrderNumber: "",
  2391. IsEdit: 0,
  2392. CancelStockId: cancelStock.ID,
  2393. CancelOrderNumber: cancelStock.OrderNumber,
  2394. Manufacturer: 0,
  2395. Dealer: 0,
  2396. Creator: adminUserInfo.AdminUser.Id,
  2397. UpdateCreator: adminUserInfo.AdminUser.Id,
  2398. Status: 1,
  2399. Ctime: time.Now().Unix(),
  2400. Mtime: 0,
  2401. Price: price,
  2402. WarehousingDetailId: 0,
  2403. WarehouseOutDetailId: 0,
  2404. CancelOutDetailId: 0,
  2405. ExpireDate: expiryDates,
  2406. ProductDate: productDates,
  2407. MaxUnit: max_unit,
  2408. MinUnit: "",
  2409. StorehouseId: storehouse_id,
  2410. }
  2411. drugFlow = append(drugFlow, flow)
  2412. }
  2413. }
  2414. }
  2415. if len(cancelStockInfos) > 0 {
  2416. service.CreateDrugCancelStockInfo(cancelStockInfos)
  2417. }
  2418. if len(upDateCancelStockInfos) > 0 {
  2419. for _, item := range upDateCancelStockInfos {
  2420. service.UpDateDrugCancelStockInfo(item)
  2421. }
  2422. }
  2423. //var errs error
  2424. //if len(cancelStockInfos) > 0 {
  2425. //
  2426. // //扣减库存逻辑
  2427. // for _, item := range cancelStockInfos {
  2428. // medical, _ := service.GetBaseDrugMedical(item.DrugId)
  2429. // //查询当前药品退库的批次号的总入库数
  2430. // infoWareInfo, _ := service.GetDrugWarehouseInfo(item.BatchNumberId)
  2431. //
  2432. // var total_count int64
  2433. // var cancel_count int64
  2434. // var out_count int64
  2435. // total_count = infoWareInfo.StockMaxNumber*medical.MinNumber + infoWareInfo.StockMinNumber
  2436. // //转化为最小单位
  2437. // var total_number int64
  2438. // if item.MaxUnit == medical.MaxUnit {
  2439. // total_number = item.Count * medical.MinNumber
  2440. // }
  2441. // if item.MaxUnit == medical.MinUnit {
  2442. // total_number = item.Count
  2443. // }
  2444. //
  2445. // //统计该批次的总出库数
  2446. // outInfo, _ := service.GetDrugWarehouseOutInfo(item.BatchNumberId, item.DrugId)
  2447. // for _, item := range outInfo {
  2448. // if item.CountUnit == medical.MaxUnit {
  2449. // item.Count = item.Count * medical.MinNumber
  2450. // }
  2451. // if item.CountUnit == medical.MinUnit {
  2452. // item.Count = item.Count
  2453. // }
  2454. // }
  2455. //
  2456. // for _, item := range outInfo {
  2457. // out_count += item.Count
  2458. // }
  2459. //
  2460. // //判断退库数量是否大于出库数量
  2461. // cancelInfo, _ := service.GetCancelDrugStockOutInfo(item.BatchNumberId, item.DrugId)
  2462. // for _, item := range cancelInfo {
  2463. // if item.MaxUnit == medical.MaxUnit {
  2464. // item.Count = item.Count * medical.MinNumber
  2465. // }
  2466. // if item.MaxUnit == medical.MinUnit {
  2467. // item.Count = item.Count
  2468. // }
  2469. // }
  2470. //
  2471. // for _, item := range cancelInfo {
  2472. // cancel_count += item.Count
  2473. // }
  2474. //
  2475. // if total_number+cancel_count > out_count {
  2476. // c.ServeSuccessJSON(map[string]interface{}{
  2477. // "msg": "4",
  2478. // })
  2479. // return
  2480. // }
  2481. //
  2482. // //判断退库数量是否大于总入库数量
  2483. // if total_number > total_count {
  2484. // c.ServeSuccessJSON(map[string]interface{}{
  2485. // "msg": "2",
  2486. // })
  2487. // return
  2488. // }
  2489. //
  2490. // //正常退库
  2491. // if total_number <= total_count {
  2492. // //创建退库详情
  2493. // errs := service.CreateDrugCancelStockInfo(cancelStockInfos)
  2494. // //创建库存明细
  2495. // //查询这个药品这个订单是否已存在流水
  2496. // for _, items := range drugFlow {
  2497. // drugwarehouseinfo, _ := service.GetLastDrugCancelStockInfoById(items.DrugId)
  2498. // items.CancelOutDetailId = drugwarehouseinfo.ID
  2499. // _, errsId := service.GetCancelStockId(items.CancelStockId, items.DrugId)
  2500. // if errsId == gorm.ErrRecordNotFound {
  2501. //
  2502. // errs = service.CreateDrugFlowTwo(items)
  2503. // }
  2504. // }
  2505. //
  2506. // //获取该批次最后一条退库详情
  2507. // lastCancel, _ := service.GetLastCancelStockInfo(id)
  2508. // var cancel_number int64
  2509. // if lastCancel.MaxUnit == medical.MaxUnit {
  2510. // cancel_number = lastCancel.Count * medical.MinNumber
  2511. // }
  2512. //
  2513. // if lastCancel.MaxUnit == medical.MinUnit {
  2514. // cancel_number = lastCancel.Count
  2515. // }
  2516. //
  2517. // var total int64
  2518. //
  2519. // if medical.MaxUnit == item.MaxUnit {
  2520. // total = item.Count - cancel_number
  2521. //
  2522. // warehouseInfo := models.XtDrugWarehouseInfo{
  2523. // StockMaxNumber: total,
  2524. // }
  2525. // errs = service.UpdateDrugWarehouseInfo(&warehouseInfo, item.BatchNumberId)
  2526. //
  2527. // if errs != nil {
  2528. // utils.ErrorLog(errs.Error())
  2529. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCancelStockFail)
  2530. // return
  2531. // }
  2532. //
  2533. // c.ServeSuccessJSON(map[string]interface{}{
  2534. // "msg": "1",
  2535. // })
  2536. // }
  2537. // if medical.MinUnit == item.MaxUnit && medical.MaxUnit != medical.MinUnit {
  2538. // total = item.Count - cancel_number
  2539. // warehouseInfo := models.XtDrugWarehouseInfo{
  2540. // StockMinNumber: total,
  2541. // }
  2542. //
  2543. // errs = service.UpdateDrugWarehouseInfoOne(&warehouseInfo, item.BatchNumberId)
  2544. //
  2545. // //查询默认仓库
  2546. // houseConfig, _ := service.GetAllStoreHouseConfig(item.OrgId)
  2547. // //查询默认仓库剩余多少库存
  2548. // list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  2549. // var sum_count int64
  2550. // var sum_in_count int64
  2551. // for _, it := range list {
  2552. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  2553. // if it.MaxUnit == baseDrug.MaxUnit {
  2554. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  2555. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  2556. // }
  2557. // sum_count += it.StockMaxNumber + it.StockMinNumber
  2558. // sum_in_count += it.WarehousingCount
  2559. // }
  2560. // service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.OrgId)
  2561. //
  2562. // if errs != nil {
  2563. // utils.ErrorLog(errs.Error())
  2564. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCancelStockFail)
  2565. // return
  2566. // }
  2567. //
  2568. // c.ServeSuccessJSON(map[string]interface{}{
  2569. // "msg": "1",
  2570. // })
  2571. // }
  2572. //
  2573. // }
  2574. //
  2575. // }
  2576. //}
  2577. //if len(drugFlow) > 0 {
  2578. // for _, items := range drugFlow {
  2579. // _, errsId := service.GetCancelStockId(items.CancelStockId, items.DrugId)
  2580. // if errsId == gorm.ErrRecordNotFound {
  2581. // errs = service.CreateDrugFlowTwo(items)
  2582. // }
  2583. // }
  2584. //}
  2585. //
  2586. //if len(upDateCancelStockInfos) > 0 {
  2587. // var total int64
  2588. // var out_count int64
  2589. // var cancel_count int64
  2590. // var now_count int64
  2591. // var all_count int64
  2592. // for _, item := range upDateCancelStockInfos {
  2593. //
  2594. // //查询最后一条退库记录
  2595. // info, _ := service.GetLastCancelStockInfoOne(id, item.DrugId)
  2596. //
  2597. // errs = service.UpDateDrugCancelStockInfo(item)
  2598. //
  2599. // //查询该药品的信息
  2600. // medical, _ := service.GetBaseDrugMedical(item.DrugId)
  2601. // //统计该批次的总出库数
  2602. // outInfo, _ := service.GetDrugWarehouseOutInfo(item.BatchNumberId, item.DrugId)
  2603. // //查询该批次的总退库数量
  2604. // cancelInfo, _ := service.GetTotalDrugCancelInfo(item.BatchNumberId, item.DrugId, item.ID)
  2605. // for _, item := range outInfo {
  2606. // if item.CountUnit == medical.MaxUnit {
  2607. // item.Count = item.Count * medical.MinNumber
  2608. // }
  2609. // if item.CountUnit == medical.MinUnit {
  2610. // item.Count = item.Count
  2611. // }
  2612. // }
  2613. //
  2614. // for _, item := range outInfo {
  2615. // out_count += item.Count
  2616. // }
  2617. //
  2618. // for _, item := range cancelInfo {
  2619. // if item.MaxUnit == medical.MaxUnit {
  2620. // item.Count = item.Count * medical.MinNumber
  2621. // }
  2622. // if item.MaxUnit == medical.MinUnit {
  2623. // item.Count = item.Count
  2624. // }
  2625. // }
  2626. //
  2627. // for _, item := range cancelInfo {
  2628. // cancel_count += item.Count
  2629. // }
  2630. //
  2631. // fmt.Println("out_count232233332232323223", out_count)
  2632. //
  2633. // if item.MaxUnit == medical.MaxUnit {
  2634. // now_count = item.Count * medical.MinNumber
  2635. // }
  2636. // if item.MaxUnit == medical.MinUnit {
  2637. // now_count = item.Count
  2638. // }
  2639. // fmt.Println("hhhhhh2323322323", now_count+cancel_count)
  2640. // //总退库批次
  2641. // all_count = now_count + cancel_count
  2642. //
  2643. // //该批次的总出库数小与退库数报错返回
  2644. // if all_count > out_count {
  2645. // c.ServeSuccessJSON(map[string]interface{}{
  2646. // "msg": "4",
  2647. // })
  2648. // return
  2649. // }
  2650. //
  2651. // if item.MaxUnit == medical.MaxUnit {
  2652. // //更新库存
  2653. // total = item.Count - info.Count
  2654. // parseDateErr := service.UpdatedDrugInfo(item.BatchNumberId, total)
  2655. // fmt.Println("parseDateErr", parseDateErr)
  2656. //
  2657. // flow := models.DrugFlow{
  2658. // Count: item.Count,
  2659. // }
  2660. // service.UpdatedDrugFlow(info.CancelStockId, item.DrugId, flow)
  2661. // }
  2662. //
  2663. // if item.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  2664. // item.Count = item.Count
  2665. // total = item.Count - info.Count
  2666. // service.UpdatedDrugInfoOne(item.BatchNumberId, total)
  2667. //
  2668. // flow := models.DrugFlow{
  2669. // Count: item.Count,
  2670. // }
  2671. // service.UpdatedDrugFlow(info.CancelStockId, item.DrugId, flow)
  2672. // }
  2673. //
  2674. // //查询默认仓库
  2675. // houseConfig, _ := service.GetAllStoreHouseConfig(item.OrgId)
  2676. // //查询默认仓库剩余多少库存
  2677. // list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  2678. // var sum_count int64
  2679. // var sum_in_count int64
  2680. // for _, it := range list {
  2681. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  2682. // if it.MaxUnit == baseDrug.MaxUnit {
  2683. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  2684. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  2685. // sum_count += it.StockMaxNumber + it.StockMinNumber
  2686. // sum_in_count += it.WarehousingCount
  2687. // }
  2688. // }
  2689. // service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.OrgId)
  2690. //
  2691. // }
  2692. //}
  2693. //if errs != nil {
  2694. // utils.ErrorLog(errs.Error())
  2695. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateCancelStockFail)
  2696. // return
  2697. //}
  2698. c.ServeSuccessJSON(map[string]interface{}{
  2699. "msg": "1",
  2700. })
  2701. }
  2702. func (c *StockDrugApiController) GetAllConfig() {
  2703. adminUserInfo := c.GetAdminUserInfo()
  2704. manufacturer, _ := service.FindAllManufacturer(adminUserInfo.CurrentOrgId)
  2705. dealer, _ := service.FindAllDealer(adminUserInfo.CurrentOrgId)
  2706. stockIns, _ := service.FindStockInNumber(adminUserInfo.CurrentOrgId)
  2707. drugs, _ := service.GetDrugs(adminUserInfo.CurrentOrgId)
  2708. list, _ := service.GetAllStoreHouseListThree(adminUserInfo.CurrentOrgId)
  2709. configlist, _ := service.GetAllStoreHouseConfig(adminUserInfo.CurrentOrgId)
  2710. appId := c.GetAdminUserInfo().CurrentAppId
  2711. doctorList, _ := service.GetAllDoctorListSix(adminUserInfo.CurrentOrgId, appId)
  2712. patients, _ := service.GetAllpatientTwenty(adminUserInfo.CurrentOrgId)
  2713. c.ServeSuccessJSON(map[string]interface{}{
  2714. "manufacturer": manufacturer,
  2715. "dealer": dealer,
  2716. "drugs": drugs,
  2717. "numbers": stockIns,
  2718. "list": list,
  2719. "configlist": configlist,
  2720. "doctorList": doctorList,
  2721. "patients": patients,
  2722. })
  2723. }
  2724. func (c *StockDrugApiController) GetAllDrugStock() {
  2725. adminUserInfo := c.GetAdminUserInfo()
  2726. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  2727. dealer_id, _ := c.GetInt64("dealer_id", 0)
  2728. drugs, err := service.FindAllDrugByManufactureId(manufacturer_id, dealer_id, adminUserInfo.CurrentOrgId)
  2729. if err == nil {
  2730. c.ServeSuccessJSON(map[string]interface{}{
  2731. "drugs": drugs,
  2732. })
  2733. } else {
  2734. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  2735. }
  2736. }
  2737. func (c *StockDrugApiController) GetDrugQueryInfo() {
  2738. page, _ := c.GetInt64("page", -1)
  2739. limit, _ := c.GetInt64("limit", -1)
  2740. keyword := c.GetString("keyword")
  2741. drug_category, _ := c.GetInt64("drug_category")
  2742. start_time := c.GetString("start_time")
  2743. end_time := c.GetString("end_time")
  2744. timeLayout := "2006-01-02"
  2745. loc, _ := time.LoadLocation("Local")
  2746. var startTime int64
  2747. if len(start_time) > 0 {
  2748. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  2749. if err != nil {
  2750. fmt.Println(err)
  2751. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2752. return
  2753. }
  2754. startTime = theTime.Unix()
  2755. }
  2756. var endTime int64
  2757. if len(end_time) > 0 {
  2758. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  2759. if err != nil {
  2760. utils.ErrorLog(err.Error())
  2761. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2762. return
  2763. }
  2764. endTime = theTime.Unix()
  2765. }
  2766. adminUserInfo := c.GetAdminUserInfo()
  2767. list, total, err := service.FindAllDrugStockInfo(adminUserInfo.CurrentOrgId, page, limit, keyword, drug_category, startTime, endTime)
  2768. if err == nil {
  2769. c.ServeSuccessJSON(map[string]interface{}{
  2770. "list": list,
  2771. "total": total,
  2772. })
  2773. } else {
  2774. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  2775. }
  2776. }
  2777. func (this *StockDrugApiController) GetUserDetailInfo() {
  2778. order_id, _ := this.GetInt64("id")
  2779. if order_id <= 0 {
  2780. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2781. return
  2782. }
  2783. adminUserInfo := this.GetAdminUserInfo()
  2784. userDetails, err, total := service.FindDrugStockUserDetailById(adminUserInfo.CurrentOrgId, order_id)
  2785. if err == nil {
  2786. this.ServeSuccessJSON(map[string]interface{}{
  2787. "list": userDetails,
  2788. "total": total,
  2789. })
  2790. } else {
  2791. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  2792. return
  2793. }
  2794. }
  2795. func (this *StockDrugApiController) GetCancelDrugOrderPrint() {
  2796. orderId := this.GetString("order_id")
  2797. ids := strings.Split(orderId, ",")
  2798. orgId := this.GetAdminUserInfo().CurrentOrgId
  2799. list, _ := service.GetCancelDrugOrderPrint(ids, orgId)
  2800. this.ServeSuccessJSON(map[string]interface{}{
  2801. "list": list,
  2802. })
  2803. }
  2804. func (this *StockDrugApiController) GetDrugCancelExportList() {
  2805. orderId := this.GetString("order_id")
  2806. ids := strings.Split(orderId, ",")
  2807. orgId := this.GetAdminUserInfo().CurrentOrgId
  2808. list, _ := service.GetDrugCancelExportList(ids, orgId)
  2809. this.ServeSuccessJSON(map[string]interface{}{
  2810. "list": list,
  2811. })
  2812. }
  2813. func (c *StockDrugApiController) GetDrugWarehouseInfoPrint() {
  2814. start_time := c.GetString("start_time")
  2815. end_time := c.GetString("end_time")
  2816. order_type, _ := c.GetInt64("order_type")
  2817. storehouse_id, _ := c.GetInt64("storehouse_id")
  2818. drug_id, _ := c.GetInt64("drug_id")
  2819. timeLayout := "2006-01-02"
  2820. loc, _ := time.LoadLocation("Local")
  2821. var startTime int64
  2822. if len(start_time) > 0 {
  2823. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  2824. if err != nil {
  2825. fmt.Println(err)
  2826. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2827. return
  2828. }
  2829. startTime = theTime.Unix()
  2830. }
  2831. var endTime int64
  2832. if len(end_time) > 0 {
  2833. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  2834. if err != nil {
  2835. utils.ErrorLog(err.Error())
  2836. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2837. return
  2838. }
  2839. endTime = theTime.Unix()
  2840. }
  2841. adminUserInfo := c.GetAdminUserInfo()
  2842. orgId := adminUserInfo.CurrentOrgId
  2843. if order_type == 1 {
  2844. list, err := service.GetDrugWarehouseInfoPrint(startTime, endTime, orgId, storehouse_id, drug_id)
  2845. manufacturerList, _ := service.GetAllManufacturerList(orgId)
  2846. dealerList, _ := service.GetAllDealerList(orgId)
  2847. if err == nil {
  2848. c.ServeSuccessJSON(map[string]interface{}{
  2849. "list": list,
  2850. "manufacturerList": manufacturerList,
  2851. "dealerList": dealerList,
  2852. })
  2853. return
  2854. }
  2855. }
  2856. }
  2857. func (c *StockDrugApiController) GetDrugOutOrderPrint() {
  2858. start_time := c.GetString("start_time")
  2859. end_time := c.GetString("end_time")
  2860. order_type, _ := c.GetInt64("order_type")
  2861. storehouse_id, _ := c.GetInt64("storehouse_id")
  2862. drug_id, _ := c.GetInt64("drug_id")
  2863. order_way, _ := c.GetInt64("order_way")
  2864. timeLayout := "2006-01-02"
  2865. loc, _ := time.LoadLocation("Local")
  2866. var startTime int64
  2867. if len(start_time) > 0 {
  2868. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  2869. if err != nil {
  2870. fmt.Println(err)
  2871. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2872. return
  2873. }
  2874. startTime = theTime.Unix()
  2875. }
  2876. var endTime int64
  2877. if len(end_time) > 0 {
  2878. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  2879. if err != nil {
  2880. utils.ErrorLog(err.Error())
  2881. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2882. return
  2883. }
  2884. endTime = theTime.Unix()
  2885. }
  2886. adminUserInfo := c.GetAdminUserInfo()
  2887. orgId := adminUserInfo.CurrentOrgId
  2888. dealerList, _ := service.GetAllDealerList(orgId)
  2889. manufacturerList, _ := service.GetAllManufacturerList(orgId)
  2890. if order_type == 2 {
  2891. list, _ := service.GetDrugOutOrderInfoPrintList(startTime, endTime, orgId, storehouse_id, drug_id, order_way)
  2892. if len(list) > 0 {
  2893. if orgId == 10721 {
  2894. if len(list) > 0 {
  2895. for _, item := range list {
  2896. if item.IsSys == 1 {
  2897. var is_number = item.Count % item.XtBaseDrug.MinNumber
  2898. if is_number == 0 {
  2899. item.Count = item.Count / item.XtBaseDrug.MinNumber
  2900. item.TotalPrice = float64(item.Count) * item.Price
  2901. item.CountUnit = item.XtBaseDrug.MaxUnit
  2902. }
  2903. if is_number != 0 {
  2904. item.TotalPrice = float64(item.Count) * item.Price
  2905. item.CountUnit = item.XtBaseDrug.MinUnit
  2906. }
  2907. }
  2908. }
  2909. }
  2910. }
  2911. }
  2912. c.ServeSuccessJSON(map[string]interface{}{
  2913. "list": list,
  2914. "dealerList": dealerList,
  2915. "manufacturerList": manufacturerList,
  2916. })
  2917. return
  2918. }
  2919. }
  2920. func (c *StockDrugApiController) GetConfigList() {
  2921. adminUserInfo := c.GetAdminUserInfo()
  2922. manufacturer, _ := service.FindAllManufacturer(adminUserInfo.CurrentOrgId)
  2923. dealer, _ := service.FindAllDealer(adminUserInfo.CurrentOrgId)
  2924. stockIns, _ := service.FindStockInNumber(adminUserInfo.CurrentOrgId)
  2925. list, _ := service.GetAllStoreHouseListThree(adminUserInfo.CurrentOrgId)
  2926. configlist, _ := service.GetAllStoreHouseConfig(adminUserInfo.CurrentOrgId)
  2927. appId := c.GetAdminUserInfo().CurrentAppId
  2928. doctorList, _ := service.GetAllDoctorListSix(adminUserInfo.CurrentOrgId, appId)
  2929. c.ServeSuccessJSON(map[string]interface{}{
  2930. "manufacturer": manufacturer,
  2931. "dealer": dealer,
  2932. "numbers": stockIns,
  2933. "list": list,
  2934. "configlist": configlist,
  2935. "doctorList": doctorList,
  2936. })
  2937. }
  2938. func (c *StockDrugApiController) CheckWarehousingInfo() {
  2939. warehousing_id, _ := c.GetInt64("warehousing_id")
  2940. orgId := c.GetAdminUserInfo().CurrentOrgId
  2941. // 开始外部循环的事务
  2942. db := service.XTWriteDB()
  2943. tx := db.Begin()
  2944. // 在函数结束时处理事务回滚
  2945. defer func() {
  2946. if r := recover(); r != nil {
  2947. tx.Rollback()
  2948. }
  2949. }()
  2950. houseConfig, _ := service.GetNewAllStoreHouseConfig(orgId, tx)
  2951. checker := c.GetAdminUserInfo().AdminUser.Id
  2952. //更改核对状态
  2953. service.CheckNewWarehousingInfo(warehousing_id, orgId, checker, tx)
  2954. list, _ := service.GetNewWarehousingInfoByList(warehousing_id, orgId, tx)
  2955. var is_success = 2
  2956. for _, item := range list {
  2957. var his_warehousing_count = item.WarehousingCount
  2958. var his_max_unit = item.MaxUnit
  2959. var in_cout int64
  2960. medical, _ := service.GetNewBaseDrugMedical(item.DrugId, tx)
  2961. if his_max_unit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  2962. in_cout = his_warehousing_count * medical.MinNumber
  2963. }
  2964. if his_max_unit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  2965. in_cout = his_warehousing_count
  2966. }
  2967. if his_max_unit == medical.MinUnit && medical.MaxUnit == medical.MinUnit {
  2968. in_cout = his_warehousing_count
  2969. }
  2970. if item.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  2971. //新增库存
  2972. service.AddNewDrugWarehouseStockMaxNumber(item.WarehousingCount, item.ID, tx)
  2973. }
  2974. if item.MaxUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  2975. //新增库存
  2976. service.AddNewDrugWarehouseStockMaxNumber(item.WarehousingCount, item.ID, tx)
  2977. }
  2978. if item.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  2979. //新增库存
  2980. service.AddNewDrugWarehouseStockMinNumber(item.WarehousingCount, item.ID, tx)
  2981. }
  2982. Creator := c.GetAdminUserInfo().AdminUser.Id
  2983. //查询默认仓库
  2984. //查询默认仓库剩余多少库存
  2985. list, _ := service.GetNewDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.OrgId, item.DrugId, tx)
  2986. var sum_count int64
  2987. var sum_in_count int64
  2988. for _, it := range list {
  2989. baseDrug, _ := service.GetNewBaseDrugMedical(it.DrugId, tx)
  2990. if it.MaxUnit == baseDrug.MaxUnit {
  2991. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  2992. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  2993. }
  2994. sum_count += it.StockMaxNumber + it.StockMinNumber
  2995. sum_in_count += it.WarehousingCount
  2996. }
  2997. flow := &models.DrugFlow{
  2998. WarehousingId: 0,
  2999. DrugId: item.DrugId,
  3000. Number: item.Number,
  3001. BatchNumber: item.BatchNumber,
  3002. Count: item.WarehousingCount,
  3003. UserOrgId: item.OrgId,
  3004. PatientId: 0,
  3005. SystemTime: time.Now().Unix(),
  3006. ConsumableType: 1,
  3007. IsSys: 0,
  3008. WarehousingOrder: item.WarehousingOrder,
  3009. WarehouseOutId: 0,
  3010. WarehouseOutOrderNumber: "",
  3011. IsEdit: 0,
  3012. CancelStockId: 0,
  3013. CancelOrderNumber: "",
  3014. Manufacturer: 0,
  3015. Dealer: 0,
  3016. Creator: Creator,
  3017. UpdateCreator: Creator,
  3018. Status: 1,
  3019. Ctime: item.Ctime,
  3020. Mtime: 0,
  3021. Price: item.Price,
  3022. WarehousingDetailId: item.ID,
  3023. WarehouseOutDetailId: 0,
  3024. CancelOutDetailId: 0,
  3025. ExpireDate: item.ExpiryDate,
  3026. ProductDate: item.ProductDate,
  3027. MaxUnit: item.MaxUnit,
  3028. MinUnit: item.MinUnit,
  3029. StorehouseId: item.StorehouseId,
  3030. OverCount: sum_count,
  3031. OperateTime: time.Now().Unix(),
  3032. DrugCode: item.DrugCode,
  3033. }
  3034. service.CreateNewDrugFlowTwo(flow, tx)
  3035. infoLog := models.XtDrugWarehouseInfoLog{
  3036. DrugId: item.DrugId,
  3037. WarehouseInfoId: item.ID,
  3038. RecordDate: time.Now().Unix(),
  3039. UserOrgId: item.OrgId,
  3040. Status: 1,
  3041. Ctime: time.Now().Unix(),
  3042. Mtime: time.Now().Unix(),
  3043. }
  3044. service.CreateInfoLog(infoLog, tx)
  3045. //更新基础库剩余库存
  3046. service.UpdateNewMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.OrgId, tx)
  3047. drugInfolist, _ := service.GetNewDrugSumCountByStorehouseId(item.StorehouseId, item.OrgId, item.DrugId, tx)
  3048. var total_count int64 // 入库总数量
  3049. var over_count int64 //剩余库存
  3050. for _, it := range drugInfolist {
  3051. baseDrug, _ := service.GetNewBaseDrugMedical(it.DrugId, tx)
  3052. if it.MaxUnit == baseDrug.MaxUnit {
  3053. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  3054. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  3055. }
  3056. }
  3057. for _, it := range drugInfolist {
  3058. total_count += it.WarehousingCount
  3059. over_count += it.StockMaxNumber + it.StockMinNumber
  3060. }
  3061. //查询该仓库是否有默认数据
  3062. drugStockCount, _ := service.GetNewDrugStockCount(item.StorehouseId, item.DrugId, item.OrgId, tx)
  3063. if drugStockCount.ID == 0 {
  3064. drugStock := models.XtDrugStockCount{
  3065. UserOrgId: item.OrgId,
  3066. StorehouseId: item.StorehouseId,
  3067. SumInCount: total_count,
  3068. SumOutCount: 0,
  3069. SumCancelCount: 0,
  3070. DrugId: item.DrugId,
  3071. Ctime: time.Now().Unix(),
  3072. Mtime: 0,
  3073. Status: 1,
  3074. FlushCount: over_count,
  3075. SumActOutCount: 0,
  3076. }
  3077. service.CreateNewDrugStockSum(drugStock, tx)
  3078. }
  3079. if drugStockCount.ID > 0 {
  3080. service.UpdateNewDrugStockSum(item.StorehouseId, item.DrugId, item.OrgId, total_count, over_count, tx)
  3081. }
  3082. flowList, _ := service.GetNewDrugFlowList(item.DrugId, item.OrgId, item.ID, tx)
  3083. var his_count int64
  3084. if len(flowList) > 0 {
  3085. for _, item := range flowList {
  3086. baseDrug, _ := service.GetNewBaseDrugMedical(item.DrugId, tx)
  3087. if item.MaxUnit == baseDrug.MaxUnit && baseDrug.MaxUnit != baseDrug.MinUnit {
  3088. his_count = item.Count * baseDrug.MinNumber
  3089. }
  3090. if item.MaxUnit == baseDrug.MinUnit && baseDrug.MaxUnit != baseDrug.MinUnit {
  3091. his_count = item.Count
  3092. }
  3093. if item.MaxUnit == baseDrug.MaxUnit && baseDrug.MaxUnit == baseDrug.MinUnit {
  3094. his_count = item.Count
  3095. }
  3096. }
  3097. }
  3098. if in_cout != his_count {
  3099. is_success = 1
  3100. tx.Rollback()
  3101. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeFlow)
  3102. return
  3103. }
  3104. goodcodelist := strings.Split(item.DrugCode, ",")
  3105. if len(goodcodelist) > 0 {
  3106. goodInfo, _ := service.GetNewDrugByIdIsStop(item.DrugId, item.OrgId, tx)
  3107. for _, it := range goodcodelist {
  3108. if it != "" {
  3109. codeInfo := models.XtDrugCodeInfo{
  3110. DrugId: item.DrugId,
  3111. BatchNumber: item.Number,
  3112. DrugCode: it,
  3113. WarehouseInfoId: item.ID,
  3114. IsUserCount: 0,
  3115. IsOutStatus: 2,
  3116. Status: 1,
  3117. UserOrgId: item.OrgId,
  3118. Ctime: time.Now().Unix(),
  3119. Mtime: time.Now().Unix(),
  3120. SplitOfNumber: goodInfo.SplitOfNumber,
  3121. }
  3122. service.CreateDrugCodeInfo(codeInfo, tx)
  3123. }
  3124. }
  3125. }
  3126. }
  3127. if is_success != 1 {
  3128. tx.Commit()
  3129. }
  3130. c.ServeSuccessJSON(map[string]interface{}{
  3131. "list": list,
  3132. })
  3133. return
  3134. }
  3135. func (c *StockDrugApiController) ReturnCheckWarehouseingInfo() {
  3136. warehousing_id, _ := c.GetInt64("warehousing_id")
  3137. orgId := c.GetAdminUserInfo().CurrentOrgId
  3138. // 开始外部循环的事务
  3139. db := service.XTWriteDB()
  3140. tx := db.Begin()
  3141. // 在函数结束时处理事务回滚
  3142. defer func() {
  3143. if r := recover(); r != nil {
  3144. tx.Rollback()
  3145. }
  3146. }()
  3147. list, _ := service.GetNewWarehousingInfoByList(warehousing_id, orgId, tx)
  3148. for _, item := range list {
  3149. //查询该药品是否有出库数据
  3150. outInfo, _ := service.GetNewDrugWarehouseOutByWarehouseInfoId(item.ID, item.DrugId, item.OrgId, tx)
  3151. if len(outInfo) > 0 {
  3152. tx.Rollback()
  3153. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCheckGoodParamWrong)
  3154. return
  3155. }
  3156. }
  3157. for _, item := range list {
  3158. //删除流水
  3159. service.UpdateNewWarehousingInfoFlow(item.ID, orgId, tx)
  3160. medical, _ := service.GetNewBaseDrugMedical(item.DrugId, tx)
  3161. if item.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3162. //新增库存
  3163. service.AddNewDrugReturnWarehouseStockMaxNumber(item.WarehousingCount, item.ID, tx)
  3164. }
  3165. if item.MaxUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3166. //新增库存
  3167. service.AddNewDrugReturnWarehouseStockMaxNumber(item.WarehousingCount, item.ID, tx)
  3168. }
  3169. if item.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3170. //新增库存
  3171. service.AddNewDrugReturnWarehouseStockMinNumber(item.WarehousingCount, item.ID, tx)
  3172. }
  3173. storeHouseConfig, _ := service.GetNewAllStoreHouseConfig(orgId, tx)
  3174. stockInfoList, _ := service.GetNewDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgId, item.DrugId, tx)
  3175. var sum_count int64
  3176. var sum_in_count int64
  3177. for _, its := range stockInfoList {
  3178. if its.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3179. sum_count += its.StockMaxNumber * medical.MinNumber
  3180. sum_in_count += its.WarehousingCount * medical.MinNumber
  3181. }
  3182. if its.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3183. sum_count += its.StockMaxNumber
  3184. sum_in_count += its.WarehousingCount
  3185. }
  3186. if its.MaxUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3187. sum_count += its.StockMaxNumber
  3188. sum_in_count += its.WarehousingCount
  3189. }
  3190. }
  3191. service.UpdateNewBaseDrugSumInfo(sum_count, item.DrugId, item.OrgId, sum_in_count, tx)
  3192. var total_count int64
  3193. if item.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3194. total_count = item.WarehousingCount * medical.MinNumber
  3195. }
  3196. if item.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3197. total_count = item.WarehousingCount
  3198. }
  3199. if item.MaxUnit == medical.MinUnit && medical.MaxUnit == medical.MinUnit {
  3200. total_count = item.WarehousingCount * medical.MinNumber
  3201. }
  3202. drugInfolist, _ := service.GetNewDrugSumCountByStorehouseId(item.StorehouseId, item.OrgId, item.DrugId, tx)
  3203. var over_count int64 //剩余库存
  3204. for _, it := range drugInfolist {
  3205. if it.MaxUnit == medical.MaxUnit {
  3206. it.StockMaxNumber = it.StockMaxNumber * medical.MinNumber
  3207. }
  3208. }
  3209. for _, it := range drugInfolist {
  3210. over_count += it.StockMaxNumber*medical.MinNumber + it.StockMinNumber
  3211. }
  3212. //扣减库存
  3213. service.ReduceNewDrugStockCount(item.StorehouseId, item.DrugId, item.OrgId, total_count, over_count, tx)
  3214. //改变扫码库里面的状态
  3215. goodcodelist := strings.Split(item.DrugCode, ",")
  3216. if len(goodcodelist) > 0 {
  3217. goodInfo, _ := service.GetNewGoodByIdIsStop(item.DrugId, item.OrgId, tx)
  3218. for _, it := range goodcodelist {
  3219. if it != "" {
  3220. codeInfo := models.XtDrugCodeInfo{
  3221. DrugId: item.DrugId,
  3222. BatchNumber: item.Number,
  3223. DrugCode: it,
  3224. WarehouseInfoId: item.ID,
  3225. IsUserCount: 0,
  3226. IsOutStatus: 2,
  3227. Status: 0,
  3228. UserOrgId: item.OrgId,
  3229. Ctime: time.Now().Unix(),
  3230. Mtime: time.Now().Unix(),
  3231. SplitOfNumber: goodInfo.SplitOfNumber,
  3232. }
  3233. service.UpdateDrugCodeInfo(codeInfo, tx)
  3234. }
  3235. }
  3236. }
  3237. }
  3238. //更新审核
  3239. err := service.ReturnNewCheckWarehouseingInfo(warehousing_id, orgId, tx)
  3240. tx.Commit()
  3241. if err == nil {
  3242. c.ServeSuccessJSON(map[string]interface{}{
  3243. "msg": "反审核成功!",
  3244. })
  3245. return
  3246. }
  3247. }
  3248. func (c *StockDrugApiController) CheckDrugOut() {
  3249. warehouseOutId, _ := c.GetInt64("warehouse_out_id")
  3250. orgId := c.GetAdminUserInfo().CurrentOrgId
  3251. // 开始外部循环的事务
  3252. db := service.XTWriteDB()
  3253. tx := db.Begin()
  3254. // 在函数结束时处理事务回滚
  3255. defer func() {
  3256. if r := recover(); r != nil {
  3257. tx.Rollback()
  3258. }
  3259. }()
  3260. warehousingOutInfoList, _ := service.GetNewDrugWarehouseOutListById(warehouseOutId, orgId, tx)
  3261. drugWarehouseOut, _ := service.GetNewDrugWarehouseOutByLastId(warehouseOutId, tx)
  3262. for _, it := range warehousingOutInfoList {
  3263. var total_count int64
  3264. var out_count int64
  3265. //查询剩余库存
  3266. stockInfo, _ := service.GetNewDrugAllStockInfo(it.StorehouseId, it.OrgId, it.DrugId, tx)
  3267. drup, _ := service.FindNewBaseDrugLibRecord(it.OrgId, it.DrugId, tx)
  3268. for _, item := range stockInfo {
  3269. if item.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  3270. item.StockMaxNumber = item.StockMaxNumber * drup.MinNumber
  3271. }
  3272. total_count += item.StockMaxNumber + item.StockMinNumber
  3273. }
  3274. //出库数量累加
  3275. if it.CountUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  3276. out_count = it.Count * drup.MinNumber
  3277. }
  3278. if it.CountUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  3279. out_count = it.Count
  3280. }
  3281. if it.CountUnit == drup.MinUnit && drup.MaxUnit == drup.MinUnit {
  3282. out_count = it.Count
  3283. }
  3284. if out_count > total_count {
  3285. c.ServeSuccessJSON(map[string]interface{}{
  3286. "warehousingOutInfoList": warehousingOutInfoList,
  3287. "drug_name": drup.DrugName,
  3288. "dose": drup.Dose,
  3289. "dose_unit": drup.DoseUnit,
  3290. "min_number": drup.MinNumber,
  3291. "min_unit": drup.MinUnit,
  3292. "max_unit": drup.MaxUnit,
  3293. "msg": 2,
  3294. })
  3295. tx.Rollback()
  3296. return
  3297. }
  3298. }
  3299. warehousingOutInfoListTwo, _ := service.GetNewDrugWarehouseOutListById(warehouseOutId, orgId, tx)
  3300. for _, it := range warehousingOutInfoListTwo {
  3301. medical, _ := service.GetBaseDrugMedical(it.DrugId)
  3302. ////出库数量累加
  3303. var out_count_one int64
  3304. if it.CountUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3305. out_count_one = it.Count * medical.MinNumber
  3306. }
  3307. if it.CountUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3308. out_count_one = it.Count
  3309. }
  3310. if it.CountUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3311. out_count_one = it.Count
  3312. }
  3313. service.AddNewDrugCount(it.DrugId, it.OrgId, it.StorehouseId, out_count_one, tx)
  3314. }
  3315. warehousingOutInfoListThree, _ := service.GetNewDrugWarehouseOutListById(warehouseOutId, orgId, tx)
  3316. var is_success = 2
  3317. for _, it := range warehousingOutInfoListThree {
  3318. var his_count = it.Count
  3319. var his_count_unit = it.CountUnit
  3320. medical, _ := service.GetNewBaseDrugMedical(it.DrugId, tx)
  3321. drup, _ := service.FindNewBaseDrugLibRecord(it.OrgId, it.DrugId, tx)
  3322. //出库逻辑
  3323. service.AutoNewDrugDeliverInfoFourtyOne(orgId, it.Count, &drugWarehouseOut, &drup, it, it.CountUnit, tx)
  3324. //查询剩余库存
  3325. stockInfo, _ := service.GetNewDrugAllStockInfo(it.StorehouseId, it.OrgId, it.DrugId, tx)
  3326. var sum_count int64
  3327. for _, its := range stockInfo {
  3328. if its.MaxUnit == medical.MaxUnit {
  3329. its.StockMaxNumber = its.StockMaxNumber * medical.MinNumber
  3330. }
  3331. sum_count += its.StockMaxNumber + its.StockMinNumber
  3332. }
  3333. service.UpdateNewDrugStockCount(it.DrugId, it.OrgId, it.StorehouseId, sum_count, tx)
  3334. storeHouseConfig, _ := service.GetNewAllStoreHouseConfig(orgId, tx)
  3335. stockInfoOne, _ := service.GetNewDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, it.OrgId, it.DrugId, tx)
  3336. var sum_count_one int64
  3337. for _, its := range stockInfoOne {
  3338. if its.MaxUnit == medical.MaxUnit {
  3339. its.StockMaxNumber = its.StockMaxNumber * medical.MinNumber
  3340. }
  3341. sum_count_one += its.StockMaxNumber + its.StockMinNumber
  3342. }
  3343. //更新基础库库存
  3344. service.UpdateNewBaseDrugSumTwo(it.DrugId, sum_count_one, it.OrgId, tx)
  3345. if it.OrgId == 10164 || it.OrgId == 10721 {
  3346. var now_count int64
  3347. if his_count_unit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3348. now_count = his_count * medical.MinNumber
  3349. }
  3350. if his_count_unit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3351. now_count = his_count
  3352. }
  3353. if his_count_unit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3354. now_count = his_count
  3355. }
  3356. var flow_count int64
  3357. var flow_count_one int64
  3358. //查询该药品的出库数量
  3359. drugflowlist, _ := service.GetDrugOutFlowByWarehouseOut(it.DrugId, it.ID, it.OrgId, tx)
  3360. if len(drugflowlist) > 0 {
  3361. for _, items := range drugflowlist {
  3362. if items.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3363. flow_count = items.Count * medical.MinNumber
  3364. flow_count_one += flow_count
  3365. }
  3366. if items.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3367. flow_count = items.Count
  3368. flow_count_one += flow_count
  3369. }
  3370. if items.MaxUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3371. flow_count = items.Count
  3372. flow_count_one += flow_count
  3373. }
  3374. }
  3375. }
  3376. fmt.Println("now_count=-------------------", now_count)
  3377. fmt.Println("flow_count_one=-------------------", flow_count_one)
  3378. if now_count != flow_count_one {
  3379. is_success = 1
  3380. tx.Rollback()
  3381. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeFlow)
  3382. return
  3383. }
  3384. }
  3385. }
  3386. out := models.DrugWarehouseOut{
  3387. IsCheck: 1,
  3388. }
  3389. //更新审核状态
  3390. service.UpdateNewCheckDrugOut(out, warehouseOutId, tx)
  3391. //查询
  3392. if is_success != 1 {
  3393. tx.Commit()
  3394. c.ServeSuccessJSON(map[string]interface{}{
  3395. "warehousingOutInfoList": warehousingOutInfoList,
  3396. "drug_name": "",
  3397. "dose": "",
  3398. "dose_unit": "",
  3399. "min_number": "",
  3400. "min_unit": "",
  3401. "max_unit": "",
  3402. "msg": 1,
  3403. })
  3404. return
  3405. }
  3406. }
  3407. func (c *StockDrugApiController) ToReturnCheck() {
  3408. warehouse_out_id, _ := c.GetInt64("warehouse_out_id")
  3409. out := models.DrugWarehouseOut{
  3410. IsCheck: 2,
  3411. }
  3412. service.UpdateCheckDrugOut(out, warehouse_out_id)
  3413. outInfo := models.DrugWarehouseOutInfo{
  3414. IsCheck: 2,
  3415. }
  3416. // 开始外部循环的事务
  3417. db := service.XTWriteDB()
  3418. tx := db.Begin()
  3419. // 在函数结束时处理事务回滚
  3420. defer func() {
  3421. if r := recover(); r != nil {
  3422. tx.Rollback()
  3423. }
  3424. }()
  3425. service.UpdateNewCheckDrugOutInfoList(outInfo, warehouse_out_id, tx)
  3426. creater := c.GetAdminUserInfo().AdminUser.Id
  3427. orgId := c.GetAdminUserInfo().CurrentOrgId
  3428. list, _ := service.GetDrugWarehouseOutListById(warehouse_out_id, orgId)
  3429. storeHouseConfig, _ := service.GetAllStoreHouseConfig(orgId)
  3430. //调用出库逻辑
  3431. for _, item := range list {
  3432. medical, _ := service.GetBaseDrugMedical(item.DrugId)
  3433. if item.CountUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3434. var sum_out_count int64
  3435. sum_out_count = item.Count * medical.MinNumber
  3436. service.UpdateNewDrugMaxNumberOne(sum_out_count, item.WarehouseInfoId, tx)
  3437. service.ModifyNewReduceDrugInformation(item.DrugId, sum_out_count, item.OrgId, tx)
  3438. drugInfoList, _ := service.FindNewDrugWarehouseInfoList(item.DrugId, item.OrgId, tx)
  3439. var drug_max_number int64
  3440. var drug_min_number int64
  3441. for _, item := range drugInfoList {
  3442. drug_max_number += item.StockMaxNumber
  3443. drug_min_number += item.StockMinNumber
  3444. }
  3445. drugFlushInfo := models.XtDrugWarehouseFlushInfo{
  3446. DrugMaxNumber: drug_max_number,
  3447. UserOrgId: item.OrgId,
  3448. WarehouseOutId: item.ID,
  3449. Type: 2,
  3450. SystemTime: item.SysRecordTime,
  3451. PatientId: 0,
  3452. Ctime: time.Now().Unix(),
  3453. Mtime: 0,
  3454. BatchNumberId: item.WarehouseInfoId,
  3455. DrugId: item.DrugId,
  3456. Count: item.Count,
  3457. WarehouseOutOrderNumber: item.WarehouseOutOrderNumber,
  3458. Creater: creater,
  3459. DrugMinNumber: drug_min_number,
  3460. Unit: item.CountUnit,
  3461. Status: 1,
  3462. }
  3463. service.CreatedNewDrugFlushInfo(drugFlushInfo, tx)
  3464. service.DeleteNewDrugOutFlow(item.ID, item.OrgId, tx)
  3465. }
  3466. if item.CountUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3467. service.UpdateDrugMaxNumber(item.Count, item.WarehouseInfoId)
  3468. var sum_out_count int64
  3469. sum_out_count = item.Count * medical.MinNumber
  3470. service.ModifyReduceDrugInformation(item.DrugId, sum_out_count, item.OrgId)
  3471. service.DeleteDrugOutFlow(item.ID, item.OrgId)
  3472. }
  3473. if item.CountUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3474. service.UpdateNewDrugMinNumber(item.Count, item.WarehouseInfoId, tx)
  3475. var sum_out_count int64
  3476. sum_out_count = item.Count
  3477. service.ModifyNewReduceDrugInformation(item.DrugId, sum_out_count, item.OrgId, tx)
  3478. service.DeleteNewDrugOutFlow(item.ID, item.OrgId, tx)
  3479. }
  3480. stockInfo, _ := service.GetNewDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, item.OrgId, item.DrugId, tx)
  3481. var sum_count int64
  3482. for _, its := range stockInfo {
  3483. baseDrug, _ := service.GetNewBaseDrugMedical(its.DrugId, tx)
  3484. if its.MaxUnit == baseDrug.MaxUnit {
  3485. its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
  3486. }
  3487. sum_count += its.StockMaxNumber + its.StockMinNumber
  3488. }
  3489. service.UpdateNewBaseDrugSumTwo(item.DrugId, sum_count, item.OrgId, tx)
  3490. //出库数量累加
  3491. var out_count int64
  3492. if item.CountUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3493. out_count = item.Count * medical.MinNumber
  3494. }
  3495. if item.CountUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3496. out_count = item.Count
  3497. }
  3498. if item.CountUnit == medical.MinUnit && medical.MaxUnit == medical.MinUnit {
  3499. out_count = item.Count
  3500. }
  3501. //增加出库数量
  3502. service.ReduceNewDrugCount(item.DrugId, item.OrgId, item.StorehouseId, out_count, tx)
  3503. //查询该药品剩余库存
  3504. infolist, _ := service.FindNewDrugWarehouseInfoFlushCountByStorehouse(item.DrugId, item.OrgId, item.StorehouseId, tx)
  3505. var over_count int64
  3506. for _, its := range infolist {
  3507. baseDrug, _ := service.GetBaseDrugMedical(its.DrugId)
  3508. if its.MaxUnit == baseDrug.MaxUnit {
  3509. its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
  3510. }
  3511. over_count += its.StockMaxNumber + its.StockMinNumber
  3512. }
  3513. service.UpdateNewDrugStockCount(item.DrugId, item.OrgId, item.StorehouseId, over_count, tx)
  3514. service.UpdateDrugWarehouseOutInfoLog(item.ID, item.DrugId, tx)
  3515. }
  3516. tx.Commit()
  3517. c.ServeSuccessJSON(map[string]interface{}{
  3518. "out": out,
  3519. })
  3520. }
  3521. func (c *StockDrugApiController) CheckCancelDrugStock() {
  3522. cancelstock_id, _ := c.GetInt64("cancelstock_id")
  3523. orgId := c.GetAdminUserInfo().CurrentOrgId
  3524. cancelDrugStock, _ := service.GetCancelDrugStock(cancelstock_id, orgId)
  3525. list, _ := service.GetCancelDrugStockList(cancelstock_id, orgId)
  3526. cancelStock := models.DrugCancelStock{
  3527. IsCheck: 1,
  3528. }
  3529. var manufacturer_id int64
  3530. var dealer_id int64
  3531. manufacturerList, _ := service.GetAllManufacturerList(orgId)
  3532. dealerList, _ := service.GetAllDealerList(orgId)
  3533. creater := c.GetAdminUserInfo().AdminUser.Id
  3534. //查询退库单据
  3535. cancelList, _ := service.GetDrugCancelCheckByIdTwenty(cancelstock_id, orgId)
  3536. storeHouseConfig, _ := service.GetAllStoreHouseConfig(orgId)
  3537. var cancel_total_count int64
  3538. var out_count int64
  3539. for _, its := range cancelList {
  3540. medical, _ := service.GetBaseDrugMedical(its.DrugId)
  3541. //查询该批次的出库数据
  3542. outList, _ := service.GetDrugWarehouseOutList(its.BatchNumberId, its.DrugId, its.OrgId)
  3543. for _, item := range outList {
  3544. if item.CountUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3545. out_count += item.Count * medical.MinNumber
  3546. }
  3547. if item.CountUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3548. out_count += item.Count
  3549. }
  3550. if item.CountUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3551. out_count += item.Count
  3552. }
  3553. }
  3554. if its.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3555. cancel_total_count += its.Count * medical.MinNumber
  3556. }
  3557. if its.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3558. cancel_total_count += its.Count
  3559. }
  3560. if its.MaxUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3561. cancel_total_count += its.Count
  3562. }
  3563. if cancel_total_count > out_count {
  3564. c.ServeSuccessJSON(map[string]interface{}{
  3565. "drug_name": medical.DrugName,
  3566. "dose": medical.Dose,
  3567. "dose_unit": medical.DoseUnit,
  3568. "min_number": medical.MinNumber,
  3569. "min_unit": medical.MinUnit,
  3570. "max_unit": medical.MaxUnit,
  3571. "msg": "2",
  3572. })
  3573. return
  3574. }
  3575. }
  3576. for _, its := range list {
  3577. for _, item := range manufacturerList {
  3578. if its.Manufacturer == item.ManufacturerName {
  3579. manufacturer_id = item.ID
  3580. }
  3581. }
  3582. for _, item := range dealerList {
  3583. if its.Dealer == item.DealerName {
  3584. dealer_id = item.ID
  3585. }
  3586. }
  3587. medical, _ := service.GetBaseDrugMedical(its.DrugId)
  3588. if its.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3589. var sum_cancel_count int64
  3590. sum_cancel_count += its.Count * medical.MinNumber
  3591. //出库数量减少
  3592. service.ModifyAddDrugOutReduceInfomation(its.DrugId, sum_cancel_count, its.OrgId, its.StorehouseId)
  3593. }
  3594. if its.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3595. //出库数量减少
  3596. service.ModifyAddDrugOutReduceInfomation(its.DrugId, its.Count, its.OrgId, its.StorehouseId)
  3597. }
  3598. if its.MaxUnit == medical.MinUnit && medical.MaxUnit == medical.MinUnit {
  3599. //出库数量减少
  3600. service.ModifyAddDrugOutReduceInfomation(its.DrugId, its.Count, its.OrgId, its.StorehouseId)
  3601. }
  3602. }
  3603. for _, its := range cancelList {
  3604. medical, _ := service.GetBaseDrugMedical(its.DrugId)
  3605. //扣减库存
  3606. if its.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3607. service.ModifyDrugWarehouseInfoStockMaxNumber(its.Count, its.DrugId, its.OrgId, its.BatchNumberId)
  3608. }
  3609. if its.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3610. service.ModifyDrugWarehouseInfoStockMinNumber(its.Count, its.DrugId, its.OrgId, its.BatchNumberId)
  3611. }
  3612. if its.MaxUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3613. service.ModifyDrugWarehouseInfoStockMaxNumber(its.Count, its.DrugId, its.OrgId, its.BatchNumberId)
  3614. }
  3615. stockInfo, _ := service.GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, its.OrgId, its.DrugId)
  3616. var sum_count int64
  3617. for _, its := range stockInfo {
  3618. baseDrug, _ := service.GetBaseDrugMedical(its.DrugId)
  3619. if its.MaxUnit == baseDrug.MaxUnit {
  3620. its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
  3621. }
  3622. sum_count += its.StockMaxNumber + its.StockMinNumber
  3623. }
  3624. //更新剩余库存
  3625. service.UpdateBaseDrugSumTwo(its.DrugId, sum_count, its.OrgId)
  3626. var cancel_count int64
  3627. if its.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3628. cancel_count = its.Count * medical.MinNumber
  3629. }
  3630. if its.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3631. cancel_count = its.Count
  3632. }
  3633. if its.MaxUnit == medical.MinUnit && medical.MaxUnit == medical.MinUnit {
  3634. cancel_count = its.Count * medical.MinNumber
  3635. }
  3636. cancelInfo, _ := service.GetDrugAllStockInfo(its.StorehouseId, its.OrgId, its.DrugId)
  3637. var over_count int64
  3638. for _, its := range cancelInfo {
  3639. if its.MaxUnit == medical.MaxUnit {
  3640. its.StockMaxNumber = its.StockMaxNumber * medical.MinNumber
  3641. }
  3642. over_count += its.StockMaxNumber + its.StockMinNumber
  3643. }
  3644. //查询该批次的退库数量
  3645. drugFlow := models.DrugFlow{
  3646. ID: 0,
  3647. WarehousingId: its.BatchNumberId,
  3648. DrugId: its.DrugId,
  3649. Number: its.Number,
  3650. BatchNumber: "",
  3651. Count: its.Count,
  3652. UserOrgId: its.OrgId,
  3653. PatientId: 0,
  3654. SystemTime: cancelDrugStock.ReturnTime,
  3655. ConsumableType: 4,
  3656. IsSys: 0,
  3657. WarehousingOrder: "",
  3658. WarehouseOutId: 0,
  3659. WarehouseOutOrderNumber: "",
  3660. IsEdit: 0,
  3661. CancelStockId: 0,
  3662. CancelOrderNumber: its.OrderNumber,
  3663. Manufacturer: manufacturer_id,
  3664. Dealer: dealer_id,
  3665. Creator: creater,
  3666. UpdateCreator: 0,
  3667. Status: 1,
  3668. Ctime: time.Now().Unix(),
  3669. Mtime: 0,
  3670. Price: its.Price,
  3671. WarehousingDetailId: its.BatchNumberId,
  3672. WarehouseOutDetailId: 0,
  3673. CancelOutDetailId: its.ID,
  3674. ExpireDate: its.ExpiryDate,
  3675. ProductDate: its.ProductDate,
  3676. MaxUnit: its.MaxUnit,
  3677. MinUnit: "",
  3678. StockMaxNumber: 0,
  3679. StockMinNumber: 0,
  3680. LastStockMaxNumber: 0,
  3681. LastStockMinNumber: 0,
  3682. AdviceId: 0,
  3683. SupplyWarehouseId: 0,
  3684. SupplyCancelOutId: 0,
  3685. SupplyWarehouseDetailInfo: 0,
  3686. StorehouseId: its.StorehouseId,
  3687. SecondWarehouseInfoId: 0,
  3688. AdminUserId: 0,
  3689. LastPrice: 0,
  3690. StockCount: "",
  3691. PharmacyId: 0,
  3692. OverCount: over_count,
  3693. }
  3694. service.CreateDrugFlowOne(drugFlow)
  3695. //增加退库数量 更新剩余库存
  3696. service.AddCancelSumCount(its.StorehouseId, its.DrugId, its.OrgId, cancel_count, over_count)
  3697. //更改审核状态
  3698. service.ModifyCancelStock(cancelstock_id, cancelStock, orgId)
  3699. }
  3700. //新增库存
  3701. c.ServeSuccessJSON(map[string]interface{}{
  3702. "drug_name": "",
  3703. "dose": "",
  3704. "dose_unit": "",
  3705. "min_number": "",
  3706. "min_unit": "",
  3707. "max_unit": "",
  3708. "msg": "1",
  3709. })
  3710. return
  3711. }
  3712. func (c *StockDrugApiController) ReturnDrugCancelStock() {
  3713. cancelstock_id, _ := c.GetInt64("cancelstock_id")
  3714. orgId := c.GetAdminUserInfo().CurrentOrgId
  3715. drugStock := models.DrugCancelStock{
  3716. IsCheck: 2,
  3717. }
  3718. err := service.ReturnDrugCancelStock(cancelstock_id, drugStock)
  3719. list, err := service.GetDrugCancelStockInfo(cancelstock_id, orgId)
  3720. storeHouseConfig, _ := service.GetAllStoreHouseConfig(orgId)
  3721. for _, item := range list {
  3722. //删除流水
  3723. service.ModifyDrugFlowByCancelId(item.ID, item.DrugId, item.OrgId)
  3724. //扣减库存
  3725. medical, _ := service.GetBaseDrugMedical(item.DrugId)
  3726. if item.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3727. var sum_cancel_count int64
  3728. sum_cancel_count += item.Count * medical.MinNumber
  3729. service.ModifyDrugMaxNumberWarehouseInfo(item.BatchNumberId, item.Count, item.OrgId)
  3730. //退库数量减少
  3731. //service.ModifyDrugCancelWarehouseInfo(item.DrugId,sum_cancel_count,item.OrgId,item.StorehouseId)
  3732. //出库数量增加
  3733. service.ModifyAddDrugOutAddInfomation(item.DrugId, sum_cancel_count, item.OrgId, item.StorehouseId)
  3734. }
  3735. if item.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3736. service.ModifyDrugMinNumberWarehouseInfo(item.BatchNumberId, item.Count, item.OrgId)
  3737. //退库数量减少
  3738. //service.ModifyDrugCancelWarehouseInfo(item.DrugId,item.Count,item.OrgId,item.StorehouseId)
  3739. //出库数量增加
  3740. service.ModifyAddDrugOutAddInfomation(item.DrugId, item.Count, item.OrgId, item.StorehouseId)
  3741. }
  3742. if item.MaxUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3743. service.ModifyDrugMaxNumberWarehouseInfo(item.BatchNumberId, item.Count, item.OrgId)
  3744. //退库数量减少
  3745. //service.ModifyDrugCancelWarehouseInfo(item.DrugId,item.Count,item.OrgId,item.StorehouseId)
  3746. //出库数量增加
  3747. service.ModifyAddDrugOutAddInfomation(item.DrugId, item.Count, item.OrgId, item.StorehouseId)
  3748. }
  3749. stockInfo, _ := service.GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  3750. var sum_count int64
  3751. for _, its := range stockInfo {
  3752. if its.MaxUnit == medical.MaxUnit {
  3753. its.StockMaxNumber = its.StockMaxNumber * medical.MinNumber
  3754. }
  3755. sum_count += its.StockMaxNumber + its.StockMinNumber
  3756. }
  3757. service.UpdateBaseDrugSumTwo(item.DrugId, sum_count, item.OrgId)
  3758. var cancel_count int64
  3759. if item.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3760. cancel_count = item.Count * medical.MinNumber
  3761. }
  3762. if item.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3763. cancel_count = item.Count
  3764. }
  3765. if item.MaxUnit == medical.MinUnit && medical.MaxUnit == medical.MinUnit {
  3766. cancel_count = item.Count * medical.MinNumber
  3767. }
  3768. cancelInfo, _ := service.GetDrugAllStockInfo(item.StorehouseId, item.OrgId, item.DrugId)
  3769. var over_count int64
  3770. for _, its := range cancelInfo {
  3771. if its.MaxUnit == medical.MaxUnit {
  3772. its.StockMaxNumber = its.StockMaxNumber * medical.MinNumber
  3773. }
  3774. over_count += its.StockMaxNumber + its.StockMinNumber
  3775. }
  3776. //退库数量减少,更新剩余库存
  3777. service.ReduceCancelSumCount(item.StorehouseId, item.DrugId, item.OrgId, cancel_count, over_count)
  3778. }
  3779. if err == nil {
  3780. c.ServeSuccessJSON(map[string]interface{}{
  3781. "msg": "反审核成功!",
  3782. })
  3783. return
  3784. }
  3785. }