drug_stock_api_contorller.go 136KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184
  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 = "YPCKD" + 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, operation_time, warehousing_out_order, tx)
  1019. if outIsExit.ID == 0 {
  1020. service.AddNewSigleDrugWarehouseOut(&warehouseOut, tx)
  1021. }
  1022. }
  1023. if warehouse_out_id > 0 {
  1024. service.UpdateNewSingleDrugWarehouseOut(warehouse_out_id, storehouse_id, tx)
  1025. }
  1026. lastWarehouseOut, _ := service.GetNewDrugLastWarehouseOut(adminUserInfo.CurrentOrgId, tx)
  1027. dataBody := make(map[string]interface{}, 0)
  1028. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  1029. if err != nil {
  1030. utils.ErrorLog(err.Error())
  1031. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1032. return
  1033. }
  1034. //出库单详情
  1035. var warehousingOutInfo []*models.DrugWarehouseOutInfo
  1036. //把前端的参数加工为出库单详情
  1037. if dataBody["stockOut"] != nil && reflect.TypeOf(dataBody["stockOut"]).String() == "[]interface {}" {
  1038. thisStockIn, _ := dataBody["stockOut"].([]interface{})
  1039. if len(thisStockIn) > 0 {
  1040. for _, item := range thisStockIn {
  1041. items := item.(map[string]interface{})
  1042. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  1043. utils.ErrorLog("drug_id")
  1044. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1045. return
  1046. }
  1047. drug_id := int64(items["drug_id"].(float64))
  1048. if items["count"] == nil || reflect.TypeOf(items["count"]).String() != "string" {
  1049. utils.ErrorLog("count")
  1050. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1051. return
  1052. }
  1053. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  1054. utils.ErrorLog("price")
  1055. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1056. return
  1057. }
  1058. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  1059. count, _ := strconv.ParseInt(items["count"].(string), 10, 64)
  1060. total := float64(count) * price
  1061. retail_price_total := float64(count) * price
  1062. remark := items["remark"].(string)
  1063. max_unit := items["max_unit"].(string)
  1064. dealer := int64(items["dealer"].(float64))
  1065. manufacturer := int64(items["manufacturer"].(float64))
  1066. number := items["number"].(string)
  1067. batch_number := items["batch_number"].(string)
  1068. warehouse_info_id := int64(items["warehouse_info_id"].(float64))
  1069. var productDates int64
  1070. var expiryDates int64
  1071. if items["expiry_date"] == nil || reflect.TypeOf(items["expiry_date"]).String() != "string" {
  1072. expiryDates = 0
  1073. } else {
  1074. if len(items["expiry_date"].(string)) == 0 {
  1075. expiryDates = 0
  1076. } else {
  1077. expiryDate, _ := items["expiry_date"].(string)
  1078. expiry_date, _ := utils.ParseTimeStringToTime("2006-01-02", expiryDate)
  1079. expiryDates = expiry_date.Unix()
  1080. }
  1081. }
  1082. if items["product_date"] == nil || reflect.TypeOf(items["product_date"]).String() != "string" {
  1083. productDates = 0
  1084. } else {
  1085. if len(items["product_date"].(string)) == 0 {
  1086. productDates = 0
  1087. } else {
  1088. productDate, _ := items["product_date"].(string)
  1089. product_date, _ := utils.ParseTimeStringToTime("2006-01-02", productDate)
  1090. productDates = product_date.Unix()
  1091. }
  1092. }
  1093. admin_user_id := int64(items["admin_user_id"].(float64))
  1094. //if items["last_price"] == nil || reflect.TypeOf(items["last_price"]).String() != "string" {
  1095. // utils.ErrorLog("last_price")
  1096. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1097. // return
  1098. //}
  1099. //last_price, _ := strconv.ParseFloat(items["last_price"].(string), 64)
  1100. stock_count, _ := items["stock_count"].(string)
  1101. patient_id := int64(items["patient_id"].(float64))
  1102. id := int64(items["id"].(float64))
  1103. class_type, _ := items["class_type"].(string)
  1104. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1105. ID: id,
  1106. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1107. WarehouseOutId: lastWarehouseOut.ID,
  1108. DrugId: drug_id,
  1109. Count: count,
  1110. Price: price,
  1111. TotalPrice: total,
  1112. Status: 1,
  1113. Ctime: warehousingOutDate.Unix(),
  1114. Mtime: ctime,
  1115. Remark: remark,
  1116. OrgId: adminUserInfo.CurrentOrgId,
  1117. Type: types,
  1118. Manufacturer: manufacturer,
  1119. Dealer: dealer,
  1120. RetailPrice: price,
  1121. RetailTotalPrice: retail_price_total,
  1122. CountUnit: max_unit,
  1123. ExpiryDate: expiryDates,
  1124. ProductDate: productDates,
  1125. Number: number,
  1126. BatchNumber: batch_number,
  1127. IsSys: 0,
  1128. WarehouseInfoId: warehouse_info_id,
  1129. StorehouseId: storehouse_id,
  1130. AdminUserId: admin_user_id,
  1131. StockCount: stock_count,
  1132. IsCheck: 2,
  1133. SysRecordTime: warehousingOutDate.Unix(),
  1134. PatientId: patient_id,
  1135. ClassType: class_type,
  1136. }
  1137. warehousingOutInfo = append(warehousingOutInfo, warehouseOutInfo)
  1138. }
  1139. }
  1140. }
  1141. //var total_count int64 //总库存
  1142. //var prescribing_number_total int64 //出库数据
  1143. //调用出库逻辑
  1144. for _, item := range warehousingOutInfo {
  1145. if item.ID == 0 {
  1146. //查询改耗材是否出库
  1147. outInfo, _ := service.GetNewDrugsIsExsit(item.DrugId, item.SysRecordTime, item.OrgId, lastWarehouseOut.ID, tx)
  1148. if outInfo.ID == 0 {
  1149. errOne := service.AddNewSigleDrugWarehouseOutInfo(item, tx)
  1150. fmt.Println(errOne)
  1151. }
  1152. }
  1153. if item.ID > 0 {
  1154. service.UpdatedNewDrugWarehouseInfo(item, item.ID, tx)
  1155. }
  1156. }
  1157. list, _ := service.GetAllDealerList(adminUserInfo.CurrentOrgId)
  1158. manufacturerList, _ := service.GetAllManufacturerList(adminUserInfo.CurrentOrgId)
  1159. warehousingOutInfoList, _ := service.GetNewDrugWarehouseOutListById(lastWarehouseOut.ID, adminUserInfo.CurrentOrgId, tx)
  1160. tx.Commit()
  1161. var drugType = "药品类型"
  1162. drugTypeParent, _ := service.GetDrugDataConfig(0, drugType)
  1163. drugTypeList, _ := service.GetParentDataConfig(drugTypeParent.ID, adminUserInfo.CurrentOrgId)
  1164. c.ServeSuccessJSON(map[string]interface{}{
  1165. "msg": "2",
  1166. "drug_name": "",
  1167. "dose": "",
  1168. "dose_unit": "",
  1169. "min_number": "",
  1170. "min_unit": "",
  1171. "max_unit": "",
  1172. "dealerList": list,
  1173. "manufacturerList": manufacturerList,
  1174. "warehousingOutInfo": warehousingOutInfoList,
  1175. "warehouseOut": lastWarehouseOut,
  1176. "drugTypeList": drugTypeList,
  1177. })
  1178. return
  1179. }
  1180. func (c *StockDrugApiController) GetDrugWarehouseOutList() {
  1181. page, _ := c.GetInt64("page", -1)
  1182. limit, _ := c.GetInt64("limit", -1)
  1183. start_time := c.GetString("start_time")
  1184. end_time := c.GetString("end_time")
  1185. types, _ := c.GetInt64("type", 0)
  1186. keywords := c.GetString("keywords")
  1187. storehouse_id, _ := c.GetInt64("storehouse_id")
  1188. way_type, _ := c.GetInt64("way_type")
  1189. check_type, _ := c.GetInt64("check_type")
  1190. timeLayout := "2006-01-02"
  1191. loc, _ := time.LoadLocation("Local")
  1192. var startTime int64
  1193. if len(start_time) > 0 {
  1194. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  1195. if err != nil {
  1196. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1197. return
  1198. }
  1199. startTime = theTime.Unix()
  1200. }
  1201. var endTime int64
  1202. if len(end_time) > 0 {
  1203. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  1204. if err != nil {
  1205. utils.ErrorLog(err.Error())
  1206. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1207. return
  1208. }
  1209. endTime = theTime.Unix()
  1210. }
  1211. adminUserInfo := c.GetAdminUserInfo()
  1212. var ids []int64
  1213. var goodids []int64
  1214. if len(keywords) > 0 {
  1215. //查询商品名称
  1216. list, _ := service.GetDrugNameByKeyword(keywords, adminUserInfo.CurrentOrgId)
  1217. for _, item := range list {
  1218. goodids = append(goodids, item.ID)
  1219. }
  1220. if len(goodids) > 0 {
  1221. //出库详情但里面查询
  1222. info, _ := service.GetDrugWarehouseOrderDetail(goodids, adminUserInfo.CurrentOrgId, startTime, endTime, storehouse_id)
  1223. for _, it := range info {
  1224. ids = append(ids, it.WarehouseOutId)
  1225. }
  1226. }
  1227. }
  1228. warehouseOutList, total, err := service.FindAllDrugWarehouseOutListOne(adminUserInfo.CurrentOrgId, page, limit, startTime, endTime, types, keywords, ids, storehouse_id, way_type, check_type)
  1229. houseList, _ := service.GetAllStoreHouseList(adminUserInfo.CurrentOrgId)
  1230. patients, _ := service.GetAllpatient(adminUserInfo.CurrentOrgId)
  1231. fmt.Println(err)
  1232. if err == nil {
  1233. c.ServeSuccessJSON(map[string]interface{}{
  1234. "list": warehouseOutList,
  1235. "total": total,
  1236. "houseList": houseList,
  1237. "patients": patients,
  1238. })
  1239. } else {
  1240. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  1241. }
  1242. }
  1243. func (c *StockDrugApiController) DeleteDrugWarehouseOut() {
  1244. ids := c.GetString("ids")
  1245. if len(ids) == 0 {
  1246. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1247. return
  1248. }
  1249. idArray := strings.Split(ids, ",")
  1250. err := service.DeleteDrugWarehouseOut(idArray)
  1251. //list, _ := service.GetDrugWarhouseOutByIds(idArray)
  1252. //for _, item := range list {
  1253. // medical, _ := service.GetBaseDrugMedical(item.DrugId)
  1254. // if item.CountUnit == medical.MaxUnit {
  1255. //
  1256. // service.UpdateDrugInfoByIds(item.WarehouseInfoId, item.Count)
  1257. //
  1258. // }
  1259. // if medical.MaxUnit != medical.MinUnit && item.CountUnit == medical.MinUnit {
  1260. // service.UpdateDrugInfoByIdsOne(item.WarehouseInfoId, item.Count)
  1261. // }
  1262. //
  1263. // //查询默认仓库
  1264. // houseConfig, _ := service.GetAllStoreHouseConfig(item.OrgId)
  1265. // //查询默认仓库剩余多少库存
  1266. // list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  1267. // var sum_count int64
  1268. // var sum_in_count int64
  1269. // for _, it := range list {
  1270. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  1271. // if it.MaxUnit == baseDrug.MaxUnit {
  1272. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  1273. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  1274. // }
  1275. // sum_count += it.StockMaxNumber + it.StockMinNumber
  1276. // sum_in_count += it.WarehousingCount
  1277. // }
  1278. // service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.OrgId)
  1279. //}
  1280. if err != nil {
  1281. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  1282. } else {
  1283. c.ServeSuccessJSON(map[string]interface{}{
  1284. "msg": "删除成功",
  1285. })
  1286. }
  1287. }
  1288. func (this *StockDrugApiController) DeleteDrugWarehouseOutInfo() {
  1289. id, _ := this.GetInt64("id", 0)
  1290. if id == 0 {
  1291. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1292. return
  1293. }
  1294. ////查询详情
  1295. //info, _ := service.GetDrugWarehosueOutInfo(id)
  1296. //
  1297. ////查询默认仓库
  1298. //houseConfig, _ := service.GetAllStoreHouseConfig(info.OrgId)
  1299. ////查询默认仓库剩余多少库存
  1300. //list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, info.OrgId, info.DrugId)
  1301. //
  1302. //for _, it := range list {
  1303. //
  1304. // //查询默认仓库
  1305. // houseConfig, _ := service.GetAllStoreHouseConfig(it.OrgId)
  1306. // //查询默认仓库剩余多少库存
  1307. // list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, it.OrgId, it.DrugId)
  1308. // var sum_count int64
  1309. // var sum_in_count int64
  1310. // for _, it := range list {
  1311. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  1312. // if it.MaxUnit == baseDrug.MaxUnit {
  1313. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  1314. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  1315. // }
  1316. // sum_count += it.StockMaxNumber + it.StockMinNumber
  1317. // sum_in_count += it.WarehousingCount
  1318. // }
  1319. // service.UpdateMedicalSumCount(it.DrugId, sum_count, sum_in_count, it.OrgId)
  1320. //
  1321. //}
  1322. //删除流水
  1323. //service.DeleteDrugWarehouseOutInfoById(info.ID)
  1324. err := service.UpDateDrugWarehouseOutStatus(id)
  1325. if err != nil {
  1326. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  1327. } else {
  1328. this.ServeSuccessJSON(map[string]interface{}{
  1329. "msg": "删除成功",
  1330. })
  1331. }
  1332. }
  1333. func (c *StockDrugApiController) GetDrugWarehouseOutInfoList() {
  1334. id, _ := c.GetInt64("id", 0)
  1335. adminInfo := c.GetAdminUserInfo()
  1336. warehouseOutInfo, _ := service.FindDrugWarehouseOutInfoById(id, adminInfo.CurrentOrgId)
  1337. warehouseOut, _ := service.FindDrugWareHouseOutById(id, adminInfo.CurrentOrgId)
  1338. manulist, _ := service.GetAllManufacturerList(adminInfo.CurrentOrgId)
  1339. dealerList, _ := service.GetAllDealerList(adminInfo.CurrentOrgId)
  1340. houstList, _ := service.GetAllStoreHouseList(adminInfo.CurrentOrgId)
  1341. appId := c.GetAdminUserInfo().CurrentAppId
  1342. doctorList, _ := service.GetAllDoctorListSix(adminInfo.CurrentOrgId, appId)
  1343. patients, _ := service.GetAllpatientTwenty(adminInfo.CurrentOrgId)
  1344. c.ServeSuccessJSON(map[string]interface{}{
  1345. "list": warehouseOutInfo,
  1346. "info": warehouseOut,
  1347. "manulist": manulist,
  1348. "dealerList": dealerList,
  1349. "houstList": houstList,
  1350. "doctorList": doctorList,
  1351. "patients": patients,
  1352. })
  1353. }
  1354. func (c *StockDrugApiController) EditDrugWarehouseOut() {
  1355. warehouse_out_time := c.GetString("warehouse_out_time")
  1356. id, _ := c.GetInt64("id", 0)
  1357. types, _ := c.GetInt64("type", 0)
  1358. storehouse_id, _ := c.GetInt64("storehouse_id")
  1359. if id == 0 {
  1360. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1361. return
  1362. }
  1363. ctime := time.Now().Unix()
  1364. mtime := time.Now().Unix()
  1365. adminUserInfo := c.GetAdminUserInfo()
  1366. warehouseOutDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", warehouse_out_time)
  1367. if parseDateErr != nil {
  1368. c.ErrorLog("日期(%v)解析错误:%v", warehouseOutDate, parseDateErr)
  1369. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1370. return
  1371. }
  1372. // 开始外部循环的事务
  1373. db := service.XTWriteDB()
  1374. tx := db.Begin()
  1375. // 在函数结束时处理事务回滚
  1376. defer func() {
  1377. if r := recover(); r != nil {
  1378. tx.Rollback()
  1379. }
  1380. }()
  1381. warehouseOut, _ := service.FindNewDrugWareHouseOutById(id, adminUserInfo.CurrentOrgId, tx)
  1382. tempWarehouseOut := models.DrugWarehouseOut{
  1383. ID: warehouseOut.ID,
  1384. Mtime: mtime,
  1385. WarehouseOutTime: warehouseOutDate.Unix(),
  1386. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1387. StorehouseId: storehouse_id,
  1388. }
  1389. service.EditNewDrugWarehouseOut(tempWarehouseOut, tx)
  1390. dataBody := make(map[string]interface{}, 0)
  1391. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  1392. if err != nil {
  1393. utils.ErrorLog(err.Error())
  1394. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1395. return
  1396. }
  1397. var warehousingOutInfo []*models.DrugWarehouseOutInfo
  1398. var upDateWarehouseOutInfos []*models.DrugWarehouseOutInfo
  1399. var drugFlow []*models.DrugFlow
  1400. if dataBody["stockOut"] != nil && reflect.TypeOf(dataBody["stockOut"]).String() == "[]interface {}" {
  1401. thisStockOut, _ := dataBody["stockOut"].([]interface{})
  1402. if len(thisStockOut) > 0 {
  1403. for _, item := range thisStockOut {
  1404. items := item.(map[string]interface{})
  1405. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  1406. utils.ErrorLog("drug_id")
  1407. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1408. return
  1409. }
  1410. drug_id := int64(items["drug_id"].(float64))
  1411. if items["count"] == nil || reflect.TypeOf(items["count"]).String() != "string" {
  1412. utils.ErrorLog("count")
  1413. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1414. return
  1415. }
  1416. count, _ := strconv.ParseInt(items["count"].(string), 10, 64)
  1417. count_unit, _ := items["count_unit"].(string)
  1418. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  1419. utils.ErrorLog("price")
  1420. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1421. return
  1422. }
  1423. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  1424. if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  1425. utils.ErrorLog("retail_price")
  1426. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1427. return
  1428. }
  1429. retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  1430. total := float64(count) * price
  1431. retail_price_total := float64(count) * retail_price
  1432. remark := items["remark"].(string)
  1433. if items["id"] == nil || reflect.TypeOf(items["id"]).String() != "float64" {
  1434. utils.ErrorLog("id")
  1435. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1436. return
  1437. }
  1438. id := int64(items["id"].(float64))
  1439. dealer := int64(items["dealer"].(float64))
  1440. manufacturer := int64(items["manufacturer"].(float64))
  1441. batch_number := items["batch_number"].(string)
  1442. number := items["number"].(string)
  1443. max_unit := items["count_unit"].(string)
  1444. warehouse_info_id := int64(items["warehouse_info_id"].(float64))
  1445. var productDates int64
  1446. var expiryDates int64
  1447. if items["expiry_date"] == nil || reflect.TypeOf(items["expiry_date"]).String() != "string" {
  1448. expiryDates = 0
  1449. } else {
  1450. if len(items["expiry_date"].(string)) == 0 {
  1451. expiryDates = 0
  1452. } else {
  1453. expiryDate, _ := items["expiry_date"].(string)
  1454. expiry_date, _ := utils.ParseTimeStringToTime("2006-01-02", expiryDate)
  1455. expiryDates = expiry_date.Unix()
  1456. }
  1457. }
  1458. if items["product_date"] == nil || reflect.TypeOf(items["product_date"]).String() != "string" {
  1459. productDates = 0
  1460. } else {
  1461. if len(items["product_date"].(string)) == 0 {
  1462. productDates = 0
  1463. } else {
  1464. productDate, _ := items["product_date"].(string)
  1465. product_date, _ := utils.ParseTimeStringToTime("2006-01-02", productDate)
  1466. productDates = product_date.Unix()
  1467. }
  1468. }
  1469. admin_user_id := int64(items["admin_user_id"].(float64))
  1470. patient_id := int64(items["patient_id"].(float64))
  1471. stock_count, _ := items["stock_count"].(string)
  1472. class_type, _ := items["class_type"].(string)
  1473. if id == 0 {
  1474. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1475. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1476. WarehouseOutId: warehouseOut.ID,
  1477. DrugId: drug_id,
  1478. Count: count,
  1479. Price: price,
  1480. TotalPrice: total,
  1481. Status: 1,
  1482. Ctime: warehouseOut.WarehouseOutTime,
  1483. Mtime: ctime,
  1484. Remark: remark,
  1485. OrgId: adminUserInfo.CurrentOrgId,
  1486. Type: types,
  1487. Manufacturer: manufacturer,
  1488. Dealer: dealer,
  1489. IsSys: 0,
  1490. SysRecordTime: warehouseOut.WarehouseOutTime,
  1491. RetailPrice: retail_price,
  1492. RetailTotalPrice: retail_price_total,
  1493. BatchNumber: batch_number,
  1494. Number: number,
  1495. ProductDate: productDates,
  1496. ExpiryDate: expiryDates,
  1497. CountUnit: count_unit,
  1498. WarehouseInfoId: warehouse_info_id,
  1499. StorehouseId: storehouse_id,
  1500. AdminUserId: admin_user_id,
  1501. StockCount: stock_count,
  1502. PatientId: patient_id,
  1503. ClassType: class_type,
  1504. }
  1505. warehousingOutInfo = append(warehousingOutInfo, warehouseOutInfo)
  1506. drugflow := &models.DrugFlow{
  1507. WarehousingOrder: "",
  1508. WarehousingId: 0,
  1509. DrugId: drug_id,
  1510. Number: number,
  1511. ProductDate: productDates,
  1512. ExpireDate: expiryDates,
  1513. Count: count,
  1514. Price: price,
  1515. Status: 1,
  1516. Ctime: warehouseOut.WarehouseOutTime,
  1517. Mtime: ctime,
  1518. UserOrgId: adminUserInfo.CurrentOrgId,
  1519. Manufacturer: manufacturer,
  1520. Dealer: dealer,
  1521. BatchNumber: batch_number,
  1522. MaxUnit: max_unit,
  1523. ConsumableType: 1,
  1524. IsEdit: 1,
  1525. Creator: adminUserInfo.AdminUser.Id,
  1526. IsSys: 0,
  1527. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1528. WarehouseOutId: id,
  1529. StorehouseId: storehouse_id,
  1530. AdminUserId: admin_user_id,
  1531. StockCount: stock_count,
  1532. PatientId: patient_id,
  1533. }
  1534. drugFlow = append(drugFlow, drugflow)
  1535. } else {
  1536. if items["is_sys"] == nil || reflect.TypeOf(items["is_sys"]).String() != "float64" {
  1537. utils.ErrorLog("is_sys")
  1538. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1539. return
  1540. }
  1541. is_sys := int64(items["is_sys"].(float64))
  1542. if items["sys_record_time"] == nil || reflect.TypeOf(items["sys_record_time"]).String() != "float64" {
  1543. utils.ErrorLog("sys_record_time")
  1544. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1545. return
  1546. }
  1547. sys_record_time := int64(items["sys_record_time"].(float64))
  1548. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1549. ID: id,
  1550. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1551. WarehouseOutId: warehouseOut.ID,
  1552. DrugId: drug_id,
  1553. Count: count,
  1554. Price: price,
  1555. TotalPrice: total,
  1556. Status: 1,
  1557. Ctime: warehouseOut.WarehouseOutTime,
  1558. Remark: remark,
  1559. OrgId: adminUserInfo.CurrentOrgId,
  1560. Mtime: time.Now().Unix(),
  1561. Type: types,
  1562. Manufacturer: manufacturer,
  1563. Dealer: dealer,
  1564. IsSys: is_sys,
  1565. SysRecordTime: sys_record_time,
  1566. RetailPrice: retail_price,
  1567. RetailTotalPrice: retail_price_total,
  1568. BatchNumber: batch_number,
  1569. Number: number,
  1570. ProductDate: productDates,
  1571. ExpiryDate: expiryDates,
  1572. CountUnit: count_unit,
  1573. WarehouseInfoId: warehouse_info_id,
  1574. StorehouseId: storehouse_id,
  1575. AdminUserId: admin_user_id,
  1576. StockCount: stock_count,
  1577. PatientId: patient_id,
  1578. ClassType: class_type,
  1579. }
  1580. upDateWarehouseOutInfos = append(upDateWarehouseOutInfos, warehouseOutInfo)
  1581. drugflow := &models.DrugFlow{
  1582. WarehousingOrder: "",
  1583. WarehousingId: 0,
  1584. DrugId: drug_id,
  1585. Number: number,
  1586. ProductDate: productDates,
  1587. ExpireDate: expiryDates,
  1588. Count: count,
  1589. Price: price,
  1590. Status: 1,
  1591. Ctime: warehouseOut.WarehouseOutTime,
  1592. Mtime: time.Now().Unix(),
  1593. UserOrgId: adminUserInfo.CurrentOrgId,
  1594. Manufacturer: manufacturer,
  1595. Dealer: dealer,
  1596. BatchNumber: batch_number,
  1597. MaxUnit: max_unit,
  1598. ConsumableType: 1,
  1599. IsEdit: 1,
  1600. Creator: adminUserInfo.AdminUser.Id,
  1601. IsSys: 0,
  1602. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1603. WarehouseOutId: warehouseOut.ID,
  1604. StorehouseId: storehouse_id,
  1605. AdminUserId: admin_user_id,
  1606. StockCount: stock_count,
  1607. PatientId: patient_id,
  1608. }
  1609. drugFlow = append(drugFlow, drugflow)
  1610. }
  1611. }
  1612. }
  1613. }
  1614. var errs error
  1615. if len(warehousingOutInfo) > 0 {
  1616. //调用出库逻辑
  1617. for _, item := range warehousingOutInfo {
  1618. errOnes := service.AddNewSigleDrugWarehouseOutInfo(item, tx)
  1619. fmt.Println("errone", errOnes)
  1620. }
  1621. }
  1622. if len(upDateWarehouseOutInfos) > 0 {
  1623. for _, item := range upDateWarehouseOutInfos {
  1624. if item.ID > 0 {
  1625. service.UpdatedNewDrugWarehouseInfo(item, item.ID, tx)
  1626. }
  1627. }
  1628. }
  1629. tx.Commit()
  1630. if errs != nil {
  1631. utils.ErrorLog(errs.Error())
  1632. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockOutFail)
  1633. return
  1634. }
  1635. c.ServeSuccessJSON(map[string]interface{}{
  1636. "msg": "2",
  1637. "drug_name": "",
  1638. "dose": "",
  1639. "dose_unit": "",
  1640. "min_number": "",
  1641. "min_unit": "",
  1642. "max_unit": "",
  1643. })
  1644. }
  1645. func (c *StockDrugApiController) CreateDrugCancelStock() {
  1646. dealer_id, _ := c.GetInt64("dealer_id", 0)
  1647. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  1648. cancel_stock_time := c.GetString("time")
  1649. types, _ := c.GetInt64("type", 0)
  1650. storehouse_id, _ := c.GetInt64("storehouse_id")
  1651. cancelstock_id, _ := c.GetInt64("cancelstock_id")
  1652. cancelStockDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", cancel_stock_time)
  1653. if parseDateErr != nil {
  1654. c.ErrorLog("日期(%v)解析错误:%v", cancelStockDate, parseDateErr)
  1655. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1656. return
  1657. }
  1658. adminUserInfo := c.GetAdminUserInfo()
  1659. operation_time := time.Now().Unix()
  1660. creater := adminUserInfo.AdminUser.Id
  1661. ctime := time.Now().Unix()
  1662. timeStr := time.Now().Format("2006-01-02")
  1663. timeArr := strings.Split(timeStr, "-")
  1664. total, _ := service.FindAllDrugCancelStockTotal(adminUserInfo.CurrentOrgId)
  1665. total = total + 1
  1666. orderNumber := "CKTKD" + strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  1667. cancelStock := models.DrugCancelStock{
  1668. OrderNumber: orderNumber,
  1669. OperaTime: operation_time,
  1670. OrgId: adminUserInfo.CurrentOrgId,
  1671. Creater: creater,
  1672. Ctime: ctime,
  1673. Status: 1,
  1674. ReturnTime: cancelStockDate.Unix(),
  1675. Dealer: dealer_id,
  1676. Manufacturer: manufacturer_id,
  1677. Type: types,
  1678. StorehouseId: storehouse_id,
  1679. IsCheck: 2,
  1680. }
  1681. if cancelstock_id == 0 {
  1682. service.AddSigleDrugCancelStock(&cancelStock)
  1683. }
  1684. if cancelstock_id > 0 {
  1685. service.UpdateSingleDrugCancelStock(cancelstock_id, storehouse_id)
  1686. }
  1687. cancelLastStock, _ := service.GetLastDrugCancelStockById(adminUserInfo.CurrentOrgId)
  1688. dataBody := make(map[string]interface{}, 0)
  1689. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  1690. if err != nil {
  1691. utils.ErrorLog(err.Error())
  1692. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1693. return
  1694. }
  1695. var cancelStockInfos []*models.DrugCancelStockInfo
  1696. var drugFlow []*models.DrugFlow
  1697. if dataBody["cancelStock"] != nil && reflect.TypeOf(dataBody["cancelStock"]).String() == "[]interface {}" {
  1698. thisStockIn, _ := dataBody["cancelStock"].([]interface{})
  1699. if len(thisStockIn) > 0 {
  1700. for _, item := range thisStockIn {
  1701. items := item.(map[string]interface{})
  1702. if items["id"] == nil || reflect.TypeOf(items["id"]).String() != "float64" {
  1703. utils.ErrorLog("id")
  1704. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1705. return
  1706. }
  1707. id := int64(items["id"].(float64))
  1708. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  1709. utils.ErrorLog("drug_id")
  1710. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1711. return
  1712. }
  1713. drug_id := int64(items["drug_id"].(float64))
  1714. return_count, _ := items["return_count"].(string)
  1715. if len(return_count) == 0 {
  1716. utils.ErrorLog("len(return_count) == 0")
  1717. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1718. return
  1719. }
  1720. count, _ := strconv.ParseInt(return_count, 10, 64)
  1721. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  1722. utils.ErrorLog("price")
  1723. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1724. return
  1725. }
  1726. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  1727. total := float64(count) * price
  1728. if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  1729. utils.ErrorLog("retail_price")
  1730. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1731. return
  1732. }
  1733. retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  1734. retail_price_total := float64(count) * retail_price
  1735. register_account, _ := items["register_account"].(string)
  1736. remark, _ := items["remark"].(string)
  1737. var productDates int64
  1738. var expiryDates int64
  1739. if items["expiry_date"] == nil || reflect.TypeOf(items["expiry_date"]).String() != "string" {
  1740. expiryDates = 0
  1741. } else {
  1742. if len(items["expiry_date"].(string)) == 0 {
  1743. expiryDates = 0
  1744. } else {
  1745. expiryDate, _ := items["expiry_date"].(string)
  1746. expiry_date, _ := utils.ParseTimeStringToTime("2006-01-02", expiryDate)
  1747. expiryDates = expiry_date.Unix()
  1748. }
  1749. }
  1750. if items["product_date"] == nil || reflect.TypeOf(items["product_date"]).String() != "string" {
  1751. productDates = 0
  1752. } else {
  1753. if len(items["product_date"].(string)) == 0 {
  1754. productDates = 0
  1755. } else {
  1756. productDate, _ := items["product_date"].(string)
  1757. product_date, _ := utils.ParseTimeStringToTime("2006-01-02", productDate)
  1758. productDates = product_date.Unix()
  1759. }
  1760. }
  1761. manufacturer, _ := items["manufacturer"].(string)
  1762. dealer, _ := items["dealer"].(string)
  1763. batch_number := items["batch_number"].(string)
  1764. max_unit := items["max_unit"].(string)
  1765. batch_number_id := int64(items["batch_number_id"].(float64))
  1766. var manufacturer_id int64
  1767. var dealer_id int64
  1768. manufacturerList, _ := service.GetAllManufacturerList(adminUserInfo.CurrentOrgId)
  1769. dealerList, _ := service.GetAllDealerList(adminUserInfo.CurrentOrgId)
  1770. for _, item := range manufacturerList {
  1771. if manufacturer == item.ManufacturerName {
  1772. manufacturer_id = item.ID
  1773. }
  1774. }
  1775. for _, item := range dealerList {
  1776. if dealer == item.DealerName {
  1777. dealer_id = item.ID
  1778. }
  1779. }
  1780. cancelInfo, _ := service.GetLastDrugCancelStockById(adminUserInfo.CurrentOrgId)
  1781. cancelStockInfo := &models.DrugCancelStockInfo{
  1782. ID: id,
  1783. OrderNumber: cancelInfo.OrderNumber,
  1784. CancelStockId: cancelInfo.ID,
  1785. DrugId: drug_id,
  1786. Count: count,
  1787. Status: 1,
  1788. Ctime: ctime,
  1789. OrgId: adminUserInfo.CurrentOrgId,
  1790. Type: types,
  1791. Manufacturer: manufacturer,
  1792. Dealer: dealer,
  1793. Total: total,
  1794. RetailPrice: retail_price,
  1795. RetailTotalPrice: retail_price_total,
  1796. Price: price,
  1797. RegisterAccount: register_account,
  1798. Remark: remark,
  1799. BatchNumber: batch_number,
  1800. MaxUnit: max_unit,
  1801. ProductDate: productDates,
  1802. ExpiryDate: expiryDates,
  1803. BatchNumberId: batch_number_id,
  1804. StorehouseId: storehouse_id,
  1805. IsCheck: 2,
  1806. }
  1807. cancelStockInfos = append(cancelStockInfos, cancelStockInfo)
  1808. flow := &models.DrugFlow{
  1809. WarehousingId: 0,
  1810. DrugId: drug_id,
  1811. Number: "",
  1812. BatchNumber: batch_number,
  1813. Count: count,
  1814. UserOrgId: adminUserInfo.CurrentOrgId,
  1815. PatientId: 0,
  1816. SystemTime: ctime,
  1817. ConsumableType: 4,
  1818. IsSys: 0,
  1819. WarehousingOrder: "",
  1820. WarehouseOutId: 0,
  1821. WarehouseOutOrderNumber: "",
  1822. IsEdit: 0,
  1823. CancelStockId: cancelStock.ID,
  1824. CancelOrderNumber: cancelStock.OrderNumber,
  1825. Manufacturer: manufacturer_id,
  1826. Dealer: dealer_id,
  1827. Creator: adminUserInfo.AdminUser.Id,
  1828. UpdateCreator: 0,
  1829. Status: 1,
  1830. Ctime: time.Now().Unix(),
  1831. Mtime: 0,
  1832. Price: price,
  1833. WarehousingDetailId: 0,
  1834. WarehouseOutDetailId: 0,
  1835. CancelOutDetailId: 0,
  1836. ExpireDate: expiryDates,
  1837. ProductDate: productDates,
  1838. MaxUnit: max_unit,
  1839. MinUnit: "",
  1840. StorehouseId: storehouse_id,
  1841. }
  1842. drugFlow = append(drugFlow, flow)
  1843. }
  1844. }
  1845. }
  1846. for _, item := range cancelStockInfos {
  1847. if item.ID == 0 {
  1848. service.CreateCancelStockInfoTwo(item)
  1849. }
  1850. if item.ID > 0 {
  1851. service.ModifyDrugCancelStockInfo(item.ID, item)
  1852. }
  1853. }
  1854. list, _ := service.GetLastCancelStockByList(cancelLastStock.ID, adminUserInfo.CurrentOrgId)
  1855. manufacturerList, _ := service.GetAllManufacturerList(adminUserInfo.CurrentOrgId)
  1856. dealerList, _ := service.GetAllDealerList(adminUserInfo.CurrentOrgId)
  1857. var drugType = "药品类型"
  1858. drugTypeParent, _ := service.GetDrugDataConfig(0, drugType)
  1859. drugTypeList, _ := service.GetParentDataConfig(drugTypeParent.ID, adminUserInfo.CurrentOrgId)
  1860. c.ServeSuccessJSON(map[string]interface{}{
  1861. "cancelStock": cancelLastStock,
  1862. "cancelLastStock": cancelLastStock,
  1863. "list": list,
  1864. "manufacturerList": manufacturerList,
  1865. "dealerList": dealerList,
  1866. "drugTypeList": drugTypeList,
  1867. "msg": "1",
  1868. })
  1869. //扣减库存逻辑
  1870. //for _, item := range cancelStockInfos {
  1871. //
  1872. // var total_number int64
  1873. // var out_number int64
  1874. // var can_number int64
  1875. // medical, _ := service.GetBaseDrugMedical(item.DrugId)
  1876. // //将当前退库数转为最小单位
  1877. // if item.MaxUnit == medical.MaxUnit {
  1878. // total_number = item.Count * medical.MinNumber
  1879. // }
  1880. // if item.MaxUnit == medical.MinUnit {
  1881. // total_number = item.Count
  1882. // }
  1883. // //查询当前药品退库的批次号的总入库数
  1884. // infoWareInfo, _ := service.GetDrugWarehouseInfo(item.BatchNumberId)
  1885. // var total_count int64
  1886. // total_count = infoWareInfo.WarehousingCount * medical.MinNumber
  1887. //
  1888. // //查询该批次的出库数量
  1889. // outInfo, _ := service.GetDrugWarehouseOutInfo(item.BatchNumberId, item.DrugId)
  1890. //
  1891. // //查询该批次总的退库数量
  1892. // canInfo, _ := service.GetCancelDrugStockOutInfo(item.BatchNumberId, item.DrugId)
  1893. //
  1894. // for _, it := range canInfo {
  1895. // if it.MaxUnit == medical.MaxUnit {
  1896. // it.Count = it.Count * medical.MinNumber
  1897. // }
  1898. // if it.MaxUnit == medical.MinUnit {
  1899. // it.Count = it.Count
  1900. // }
  1901. // }
  1902. //
  1903. // for _, it := range canInfo {
  1904. // can_number += it.Count
  1905. // }
  1906. //
  1907. // for _, it := range outInfo {
  1908. // if it.CountUnit == medical.MaxUnit {
  1909. // it.Count = it.Count * medical.MinNumber
  1910. // }
  1911. // if it.CountUnit == medical.MinUnit {
  1912. // it.Count = it.Count
  1913. // }
  1914. // }
  1915. // for _, it := range outInfo {
  1916. // out_number += it.Count
  1917. // }
  1918. //
  1919. // if out_number == 0 {
  1920. // service.UpdateDrugCancel(item.CancelStockId)
  1921. // c.ServeSuccessJSON(map[string]interface{}{
  1922. // "msg": "2",
  1923. // })
  1924. // return
  1925. // }
  1926. // fmt.Println("当前退库数", total_count)
  1927. // fmt.Println("当前批次总库存", total_count)
  1928. // fmt.Println("总出库数", out_number)
  1929. // //判断退库数量是否大于总入库数量
  1930. // if total_number > total_count {
  1931. // service.UpdateDrugCancel(item.CancelStockId)
  1932. // c.ServeSuccessJSON(map[string]interface{}{
  1933. // "msg": "2",
  1934. // })
  1935. // return
  1936. // }
  1937. //
  1938. // //判断总退库数量是否大于出库数量
  1939. //
  1940. // if can_number+total_number > out_number {
  1941. // //清空退库单
  1942. // service.UpdateDrugCancel(item.CancelStockId)
  1943. // c.ServeSuccessJSON(map[string]interface{}{
  1944. // "msg": "2",
  1945. // })
  1946. // return
  1947. // }
  1948. //
  1949. // //正常退库
  1950. // if total_number <= out_number {
  1951. // //创建退库详情
  1952. // errs := service.CreateCancelStockInfoTwo(item)
  1953. // lastInfo, _ := service.GetLastStockInfoThree(item.DrugId)
  1954. // //创建库存明细
  1955. // flows := models.DrugFlow{
  1956. // WarehousingId: 0,
  1957. // DrugId: item.DrugId,
  1958. // Number: "",
  1959. // BatchNumber: item.BatchNumber,
  1960. // Count: item.Count,
  1961. // UserOrgId: adminUserInfo.CurrentOrgId,
  1962. // PatientId: 0,
  1963. // SystemTime: ctime,
  1964. // ConsumableType: 4,
  1965. // IsSys: 0,
  1966. // WarehousingOrder: infoWareInfo.WarehousingOrder,
  1967. // WarehouseOutId: 0,
  1968. // WarehouseOutOrderNumber: "",
  1969. // IsEdit: 0,
  1970. // CancelStockId: cancelStock.ID,
  1971. // CancelOrderNumber: cancelStock.OrderNumber,
  1972. // Manufacturer: manufacturer_id,
  1973. // Dealer: dealer_id,
  1974. // Creator: adminUserInfo.AdminUser.Id,
  1975. // UpdateCreator: 0,
  1976. // Status: 1,
  1977. // Ctime: time.Now().Unix(),
  1978. // Mtime: 0,
  1979. // Price: item.Price,
  1980. // WarehousingDetailId: 0,
  1981. // WarehouseOutDetailId: 0,
  1982. // CancelOutDetailId: lastInfo.ID,
  1983. // ExpireDate: item.ExpiryDate,
  1984. // ProductDate: item.ProductDate,
  1985. // MaxUnit: item.MaxUnit,
  1986. // MinUnit: "",
  1987. // StorehouseId: storehouse_id,
  1988. // }
  1989. // errs = service.CreateDrugFlowOne(flows)
  1990. //
  1991. // var total int64
  1992. //
  1993. // if medical.MaxUnit == item.MaxUnit {
  1994. // total = item.Count
  1995. //
  1996. // warehouseInfo := models.XtDrugWarehouseInfo{
  1997. // StockMaxNumber: total,
  1998. // }
  1999. //
  2000. // errs = service.UpdateDrugWarehouseInfo(&warehouseInfo, item.BatchNumberId)
  2001. // //查询默认仓库
  2002. // houseConfig, _ := service.GetAllStoreHouseConfig(item.OrgId)
  2003. // //查询默认仓库剩余多少库存
  2004. // list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  2005. // var sum_count int64
  2006. // var sum_in_count int64
  2007. // for _, it := range list {
  2008. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  2009. // if it.MaxUnit == baseDrug.MaxUnit {
  2010. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  2011. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  2012. // }
  2013. // sum_count += it.StockMaxNumber + it.StockMinNumber
  2014. // sum_in_count += it.WarehousingCount
  2015. // }
  2016. // service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.OrgId)
  2017. // if errs != nil {
  2018. // utils.ErrorLog(errs.Error())
  2019. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCancelStockFail)
  2020. // return
  2021. // }
  2022. //
  2023. // c.ServeSuccessJSON(map[string]interface{}{
  2024. // "msg": "1",
  2025. // })
  2026. // }
  2027. //
  2028. // if medical.MinUnit == item.MaxUnit && medical.MaxUnit != medical.MinUnit {
  2029. // total = item.Count
  2030. // warehouseInfo := models.XtDrugWarehouseInfo{
  2031. // StockMinNumber: total,
  2032. // }
  2033. //
  2034. // errs = service.UpdateDrugWarehouseInfoOne(&warehouseInfo, item.BatchNumberId)
  2035. // //查询默认仓库
  2036. // houseConfig, _ := service.GetAllStoreHouseConfig(item.OrgId)
  2037. // //查询默认仓库剩余多少库存
  2038. // list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  2039. // var sum_count int64
  2040. // var sum_in_count int64
  2041. // for _, it := range list {
  2042. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  2043. // if it.MaxUnit == baseDrug.MaxUnit {
  2044. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  2045. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  2046. // }
  2047. // sum_count += it.StockMaxNumber + it.StockMinNumber
  2048. // sum_in_count += it.WarehousingCount
  2049. // }
  2050. // service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.OrgId)
  2051. // if errs != nil {
  2052. // utils.ErrorLog(errs.Error())
  2053. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCancelStockFail)
  2054. // return
  2055. // }
  2056. //
  2057. // c.ServeSuccessJSON(map[string]interface{}{
  2058. // "msg": "1",
  2059. // })
  2060. // }
  2061. // }
  2062. //
  2063. //}
  2064. }
  2065. func (c *StockDrugApiController) GetDrugCancelStockInfoList() {
  2066. id, _ := c.GetInt64("id", 0)
  2067. adminUserInfo := c.GetAdminUserInfo()
  2068. cancelStockInfoList, _ := service.FindDrugCancelStockInfoById(id, adminUserInfo.CurrentOrgId)
  2069. info, _ := service.FindCancelDrugById(id, adminUserInfo.CurrentOrgId)
  2070. list, _ := service.GetAllStoreHouseList(adminUserInfo.CurrentOrgId)
  2071. c.ServeSuccessJSON(map[string]interface{}{
  2072. "list": cancelStockInfoList,
  2073. "info": info,
  2074. "houselist": list,
  2075. })
  2076. }
  2077. func (c *StockDrugApiController) GetDrugCancelStockList() {
  2078. page, _ := c.GetInt64("page", -1)
  2079. limit, _ := c.GetInt64("limit", -1)
  2080. start_time := c.GetString("start_time")
  2081. end_time := c.GetString("end_time")
  2082. types, _ := c.GetInt64("type", 0)
  2083. keywords := c.GetString("keywords")
  2084. storehouse_id, _ := c.GetInt64("storehouse_id")
  2085. check_type, _ := c.GetInt64("check_type")
  2086. timeLayout := "2006-01-02"
  2087. loc, _ := time.LoadLocation("Local")
  2088. var startTime int64
  2089. if len(start_time) > 0 {
  2090. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  2091. if err != nil {
  2092. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2093. return
  2094. }
  2095. startTime = theTime.Unix()
  2096. }
  2097. var endTime int64
  2098. if len(end_time) > 0 {
  2099. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  2100. if err != nil {
  2101. utils.ErrorLog(err.Error())
  2102. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2103. return
  2104. }
  2105. endTime = theTime.Unix()
  2106. }
  2107. adminUserInfo := c.GetAdminUserInfo()
  2108. returnList, total, err := service.FindAllDrugCancelList(adminUserInfo.CurrentOrgId, page, limit, startTime, endTime, types, keywords, storehouse_id, check_type)
  2109. houseList, _ := service.GetAllStoreHouseList(adminUserInfo.CurrentOrgId)
  2110. if err == nil {
  2111. c.ServeSuccessJSON(map[string]interface{}{
  2112. "list": returnList,
  2113. "total": total,
  2114. "houseList": houseList,
  2115. })
  2116. } else {
  2117. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  2118. }
  2119. }
  2120. func (this *StockDrugApiController) DeleteDrugCancelStock() {
  2121. ids := this.GetString("ids")
  2122. if len(ids) == 0 {
  2123. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2124. return
  2125. }
  2126. idArray := strings.Split(ids, ",")
  2127. ////获取入库数据
  2128. //list, _ := service.GetDrugWareInfoByCancelId(idArray)
  2129. //for _, item := range list {
  2130. // medical, _ := service.GetBaseDrugMedical(item.DrugId)
  2131. // if item.MaxUnit == medical.MaxUnit {
  2132. // //回退库存
  2133. // service.ModifyDrugWarehouseByInfoId(item.BatchNumberId, item.Count)
  2134. //
  2135. // }
  2136. // if item.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  2137. // service.ModifyDrugWarehouseByStockMin(item.BatchNumberId, item.Count)
  2138. // }
  2139. //
  2140. // //查询默认仓库
  2141. // houseConfig, _ := service.GetAllStoreHouseConfig(item.OrgId)
  2142. // //查询默认仓库剩余多少库存
  2143. // list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  2144. // var sum_count int64
  2145. // var sum_in_count int64
  2146. // for _, it := range list {
  2147. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  2148. // if it.MaxUnit == baseDrug.MaxUnit {
  2149. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  2150. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  2151. // sum_count += it.StockMaxNumber + it.StockMinNumber
  2152. // sum_in_count += it.WarehousingCount
  2153. // }
  2154. // }
  2155. // service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.OrgId)
  2156. //}
  2157. err := service.DeleteDrugCancelStock(idArray)
  2158. if err != nil {
  2159. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  2160. } else {
  2161. this.ServeSuccessJSON(map[string]interface{}{
  2162. "msg": "删除成功",
  2163. })
  2164. }
  2165. }
  2166. func (this *StockDrugApiController) DeleteDrugCancelStockInfo() {
  2167. id, _ := this.GetInt64("id", 0)
  2168. if id == 0 {
  2169. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2170. return
  2171. }
  2172. ////查询
  2173. //cancelinfo, _ := service.GetDrugCancelById(id)
  2174. //
  2175. //drug, _ := service.GetBaseDrugMedical(cancelinfo.DrugId)
  2176. //if drug.MaxUnit == cancelinfo.MaxUnit {
  2177. // service.ModeFyDrugWareseInfo(cancelinfo.BatchNumberId, cancelinfo.Count)
  2178. //
  2179. //}
  2180. //if drug.MinUnit == cancelinfo.MaxUnit && drug.MaxUnit != drug.MinUnit {
  2181. // service.ModeFyDrugWarehouseMin(cancelinfo.BatchNumberId, cancelinfo.Count)
  2182. //}
  2183. //
  2184. err := service.UpDateDrugCancleStockStatus(id)
  2185. //删除流水
  2186. // service.UpDateDrugCancelStockById(cancelinfo.ID)
  2187. //查询默认仓库
  2188. //houseConfig, _ := service.GetAllStoreHouseConfig(cancelinfo.OrgId)
  2189. ////查询默认仓库剩余多少库存
  2190. //list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, cancelinfo.OrgId, cancelinfo.DrugId)
  2191. //var sum_count int64
  2192. //var sum_in_count int64
  2193. //for _, it := range list {
  2194. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  2195. // if it.MaxUnit == baseDrug.MaxUnit {
  2196. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  2197. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  2198. // }
  2199. // sum_count += it.StockMaxNumber + it.StockMinNumber
  2200. // sum_in_count += it.WarehousingCount
  2201. //}
  2202. //service.UpdateMedicalSumCount(cancelinfo.DrugId, sum_count, sum_in_count, cancelinfo.OrgId)
  2203. if err != nil {
  2204. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  2205. } else {
  2206. this.ServeSuccessJSON(map[string]interface{}{
  2207. "msg": "删除成功",
  2208. })
  2209. }
  2210. }
  2211. func (c *StockDrugApiController) EditDrugCancelStock() {
  2212. cancel_time := c.GetString("cancel_time")
  2213. id, _ := c.GetInt64("id", 0)
  2214. types, _ := c.GetInt64("type", 0)
  2215. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  2216. dealer_id, _ := c.GetInt64("dealer_id", 0)
  2217. storehouse_id, _ := c.GetInt64("storehouse_id")
  2218. if id == 0 {
  2219. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2220. return
  2221. }
  2222. ctime := time.Now().Unix()
  2223. mtime := time.Now().Unix()
  2224. adminUserInfo := c.GetAdminUserInfo()
  2225. cancelDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", cancel_time)
  2226. if parseDateErr != nil {
  2227. c.ErrorLog("日期(%v)解析错误:%v", cancelDate, parseDateErr)
  2228. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2229. return
  2230. }
  2231. cancelStock, _ := service.FindDrugCancelStockById(id)
  2232. tempCancelStock := models.DrugCancelStock{
  2233. ID: cancelStock.ID,
  2234. Mtime: mtime,
  2235. ReturnTime: cancelDate.Unix(),
  2236. Manufacturer: manufacturer_id,
  2237. Dealer: dealer_id,
  2238. StorehouseId: storehouse_id,
  2239. IsCheck: 1,
  2240. }
  2241. service.EditDrugCancelStock(tempCancelStock)
  2242. dataBody := make(map[string]interface{}, 0)
  2243. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  2244. if err != nil {
  2245. utils.ErrorLog(err.Error())
  2246. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2247. return
  2248. }
  2249. var cancelStockInfos []*models.DrugCancelStockInfo
  2250. var upDateCancelStockInfos []*models.DrugCancelStockInfo
  2251. var drugFlow []*models.DrugFlow
  2252. if dataBody["cancelStock"] != nil && reflect.TypeOf(dataBody["cancelStock"]).String() == "[]interface {}" {
  2253. thisCancelStock, _ := dataBody["cancelStock"].([]interface{})
  2254. if len(thisCancelStock) > 0 {
  2255. for _, item := range thisCancelStock {
  2256. items := item.(map[string]interface{})
  2257. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  2258. utils.ErrorLog("drug_id")
  2259. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2260. return
  2261. }
  2262. drug_id := int64(items["drug_id"].(float64))
  2263. if items["count"] == nil || reflect.TypeOf(items["count"]).String() != "string" {
  2264. utils.ErrorLog("count")
  2265. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2266. return
  2267. }
  2268. count, _ := strconv.ParseInt(items["count"].(string), 10, 64)
  2269. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  2270. utils.ErrorLog("price")
  2271. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2272. return
  2273. }
  2274. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  2275. total := float64(count) * price
  2276. if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  2277. utils.ErrorLog("retail_price")
  2278. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2279. return
  2280. }
  2281. retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  2282. retail_price_total := float64(count) * retail_price
  2283. if items["id"] == nil || reflect.TypeOf(items["id"]).String() != "float64" {
  2284. utils.ErrorLog("id")
  2285. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2286. return
  2287. }
  2288. id := int64(items["id"].(float64))
  2289. max_unit := items["max_unit"].(string)
  2290. batch_number := items["batch_number"].(string)
  2291. register_account := items["register_account"].(string)
  2292. remark := items["remark"].(string)
  2293. manufacturer := items["manufacturer"].(string)
  2294. dealer := items["dealer"].(string)
  2295. var productDates int64
  2296. var expiryDates int64
  2297. if items["expiry_date"] == nil || reflect.TypeOf(items["expiry_date"]).String() != "string" {
  2298. expiryDates = 0
  2299. } else {
  2300. if len(items["expiry_date"].(string)) == 0 {
  2301. expiryDates = 0
  2302. } else {
  2303. expiryDate, _ := items["expiry_date"].(string)
  2304. expiry_date, _ := utils.ParseTimeStringToTime("2006-01-02", expiryDate)
  2305. expiryDates = expiry_date.Unix()
  2306. }
  2307. }
  2308. if items["product_date"] == nil || reflect.TypeOf(items["product_date"]).String() != "string" {
  2309. productDates = 0
  2310. } else {
  2311. if len(items["product_date"].(string)) == 0 {
  2312. productDates = 0
  2313. } else {
  2314. productDate, _ := items["product_date"].(string)
  2315. product_date, _ := utils.ParseTimeStringToTime("2006-01-02", productDate)
  2316. productDates = product_date.Unix()
  2317. }
  2318. }
  2319. batch_number_id := int64(items["batch_number_id"].(float64))
  2320. if id == 0 {
  2321. cancelStockInfo := &models.DrugCancelStockInfo{
  2322. DrugId: drug_id,
  2323. Count: count,
  2324. Status: 1,
  2325. Ctime: ctime,
  2326. OrgId: adminUserInfo.CurrentOrgId,
  2327. OrderNumber: cancelStock.OrderNumber,
  2328. CancelStockId: cancelStock.ID,
  2329. Mtime: time.Now().Unix(),
  2330. Type: types,
  2331. RetailPrice: retail_price,
  2332. RetailTotalPrice: retail_price_total,
  2333. Price: price,
  2334. Total: total,
  2335. Manufacturer: manufacturer,
  2336. Dealer: dealer,
  2337. RegisterAccount: register_account,
  2338. MaxUnit: max_unit,
  2339. BatchNumber: batch_number,
  2340. Remark: remark,
  2341. ProductDate: productDates,
  2342. ExpiryDate: expiryDates,
  2343. BatchNumberId: batch_number_id,
  2344. StorehouseId: storehouse_id,
  2345. }
  2346. cancelStockInfos = append(cancelStockInfos, cancelStockInfo)
  2347. } else {
  2348. cancelStockInfo := &models.DrugCancelStockInfo{
  2349. ID: id,
  2350. DrugId: drug_id,
  2351. Count: count,
  2352. Status: 1,
  2353. Ctime: ctime,
  2354. OrgId: adminUserInfo.CurrentOrgId,
  2355. OrderNumber: cancelStock.OrderNumber,
  2356. CancelStockId: cancelStock.ID,
  2357. Mtime: time.Now().Unix(),
  2358. Type: types,
  2359. RetailPrice: retail_price,
  2360. RetailTotalPrice: retail_price_total,
  2361. Price: price,
  2362. Total: total,
  2363. Manufacturer: manufacturer,
  2364. Dealer: dealer,
  2365. RegisterAccount: register_account,
  2366. MaxUnit: max_unit,
  2367. BatchNumber: batch_number,
  2368. Remark: remark,
  2369. ProductDate: productDates,
  2370. ExpiryDate: expiryDates,
  2371. BatchNumberId: batch_number_id,
  2372. StorehouseId: storehouse_id,
  2373. }
  2374. upDateCancelStockInfos = append(upDateCancelStockInfos, cancelStockInfo)
  2375. }
  2376. flow := &models.DrugFlow{
  2377. WarehousingId: 0,
  2378. DrugId: drug_id,
  2379. Number: "",
  2380. BatchNumber: batch_number,
  2381. Count: count,
  2382. UserOrgId: adminUserInfo.CurrentOrgId,
  2383. PatientId: 0,
  2384. SystemTime: time.Now().Unix(),
  2385. ConsumableType: 4,
  2386. IsSys: 0,
  2387. WarehousingOrder: "",
  2388. WarehouseOutId: 0,
  2389. WarehouseOutOrderNumber: "",
  2390. IsEdit: 0,
  2391. CancelStockId: cancelStock.ID,
  2392. CancelOrderNumber: cancelStock.OrderNumber,
  2393. Manufacturer: 0,
  2394. Dealer: 0,
  2395. Creator: adminUserInfo.AdminUser.Id,
  2396. UpdateCreator: adminUserInfo.AdminUser.Id,
  2397. Status: 1,
  2398. Ctime: time.Now().Unix(),
  2399. Mtime: 0,
  2400. Price: price,
  2401. WarehousingDetailId: 0,
  2402. WarehouseOutDetailId: 0,
  2403. CancelOutDetailId: 0,
  2404. ExpireDate: expiryDates,
  2405. ProductDate: productDates,
  2406. MaxUnit: max_unit,
  2407. MinUnit: "",
  2408. StorehouseId: storehouse_id,
  2409. }
  2410. drugFlow = append(drugFlow, flow)
  2411. }
  2412. }
  2413. }
  2414. if len(cancelStockInfos) > 0 {
  2415. service.CreateDrugCancelStockInfo(cancelStockInfos)
  2416. }
  2417. if len(upDateCancelStockInfos) > 0 {
  2418. for _, item := range upDateCancelStockInfos {
  2419. service.UpDateDrugCancelStockInfo(item)
  2420. }
  2421. }
  2422. //var errs error
  2423. //if len(cancelStockInfos) > 0 {
  2424. //
  2425. // //扣减库存逻辑
  2426. // for _, item := range cancelStockInfos {
  2427. // medical, _ := service.GetBaseDrugMedical(item.DrugId)
  2428. // //查询当前药品退库的批次号的总入库数
  2429. // infoWareInfo, _ := service.GetDrugWarehouseInfo(item.BatchNumberId)
  2430. //
  2431. // var total_count int64
  2432. // var cancel_count int64
  2433. // var out_count int64
  2434. // total_count = infoWareInfo.StockMaxNumber*medical.MinNumber + infoWareInfo.StockMinNumber
  2435. // //转化为最小单位
  2436. // var total_number int64
  2437. // if item.MaxUnit == medical.MaxUnit {
  2438. // total_number = item.Count * medical.MinNumber
  2439. // }
  2440. // if item.MaxUnit == medical.MinUnit {
  2441. // total_number = item.Count
  2442. // }
  2443. //
  2444. // //统计该批次的总出库数
  2445. // outInfo, _ := service.GetDrugWarehouseOutInfo(item.BatchNumberId, item.DrugId)
  2446. // for _, item := range outInfo {
  2447. // if item.CountUnit == medical.MaxUnit {
  2448. // item.Count = item.Count * medical.MinNumber
  2449. // }
  2450. // if item.CountUnit == medical.MinUnit {
  2451. // item.Count = item.Count
  2452. // }
  2453. // }
  2454. //
  2455. // for _, item := range outInfo {
  2456. // out_count += item.Count
  2457. // }
  2458. //
  2459. // //判断退库数量是否大于出库数量
  2460. // cancelInfo, _ := service.GetCancelDrugStockOutInfo(item.BatchNumberId, item.DrugId)
  2461. // for _, item := range cancelInfo {
  2462. // if item.MaxUnit == medical.MaxUnit {
  2463. // item.Count = item.Count * medical.MinNumber
  2464. // }
  2465. // if item.MaxUnit == medical.MinUnit {
  2466. // item.Count = item.Count
  2467. // }
  2468. // }
  2469. //
  2470. // for _, item := range cancelInfo {
  2471. // cancel_count += item.Count
  2472. // }
  2473. //
  2474. // if total_number+cancel_count > out_count {
  2475. // c.ServeSuccessJSON(map[string]interface{}{
  2476. // "msg": "4",
  2477. // })
  2478. // return
  2479. // }
  2480. //
  2481. // //判断退库数量是否大于总入库数量
  2482. // if total_number > total_count {
  2483. // c.ServeSuccessJSON(map[string]interface{}{
  2484. // "msg": "2",
  2485. // })
  2486. // return
  2487. // }
  2488. //
  2489. // //正常退库
  2490. // if total_number <= total_count {
  2491. // //创建退库详情
  2492. // errs := service.CreateDrugCancelStockInfo(cancelStockInfos)
  2493. // //创建库存明细
  2494. // //查询这个药品这个订单是否已存在流水
  2495. // for _, items := range drugFlow {
  2496. // drugwarehouseinfo, _ := service.GetLastDrugCancelStockInfoById(items.DrugId)
  2497. // items.CancelOutDetailId = drugwarehouseinfo.ID
  2498. // _, errsId := service.GetCancelStockId(items.CancelStockId, items.DrugId)
  2499. // if errsId == gorm.ErrRecordNotFound {
  2500. //
  2501. // errs = service.CreateDrugFlowTwo(items)
  2502. // }
  2503. // }
  2504. //
  2505. // //获取该批次最后一条退库详情
  2506. // lastCancel, _ := service.GetLastCancelStockInfo(id)
  2507. // var cancel_number int64
  2508. // if lastCancel.MaxUnit == medical.MaxUnit {
  2509. // cancel_number = lastCancel.Count * medical.MinNumber
  2510. // }
  2511. //
  2512. // if lastCancel.MaxUnit == medical.MinUnit {
  2513. // cancel_number = lastCancel.Count
  2514. // }
  2515. //
  2516. // var total int64
  2517. //
  2518. // if medical.MaxUnit == item.MaxUnit {
  2519. // total = item.Count - cancel_number
  2520. //
  2521. // warehouseInfo := models.XtDrugWarehouseInfo{
  2522. // StockMaxNumber: total,
  2523. // }
  2524. // errs = service.UpdateDrugWarehouseInfo(&warehouseInfo, item.BatchNumberId)
  2525. //
  2526. // if errs != nil {
  2527. // utils.ErrorLog(errs.Error())
  2528. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCancelStockFail)
  2529. // return
  2530. // }
  2531. //
  2532. // c.ServeSuccessJSON(map[string]interface{}{
  2533. // "msg": "1",
  2534. // })
  2535. // }
  2536. // if medical.MinUnit == item.MaxUnit && medical.MaxUnit != medical.MinUnit {
  2537. // total = item.Count - cancel_number
  2538. // warehouseInfo := models.XtDrugWarehouseInfo{
  2539. // StockMinNumber: total,
  2540. // }
  2541. //
  2542. // errs = service.UpdateDrugWarehouseInfoOne(&warehouseInfo, item.BatchNumberId)
  2543. //
  2544. // //查询默认仓库
  2545. // houseConfig, _ := service.GetAllStoreHouseConfig(item.OrgId)
  2546. // //查询默认仓库剩余多少库存
  2547. // list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  2548. // var sum_count int64
  2549. // var sum_in_count int64
  2550. // for _, it := range list {
  2551. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  2552. // if it.MaxUnit == baseDrug.MaxUnit {
  2553. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  2554. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  2555. // }
  2556. // sum_count += it.StockMaxNumber + it.StockMinNumber
  2557. // sum_in_count += it.WarehousingCount
  2558. // }
  2559. // service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.OrgId)
  2560. //
  2561. // if errs != nil {
  2562. // utils.ErrorLog(errs.Error())
  2563. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCancelStockFail)
  2564. // return
  2565. // }
  2566. //
  2567. // c.ServeSuccessJSON(map[string]interface{}{
  2568. // "msg": "1",
  2569. // })
  2570. // }
  2571. //
  2572. // }
  2573. //
  2574. // }
  2575. //}
  2576. //if len(drugFlow) > 0 {
  2577. // for _, items := range drugFlow {
  2578. // _, errsId := service.GetCancelStockId(items.CancelStockId, items.DrugId)
  2579. // if errsId == gorm.ErrRecordNotFound {
  2580. // errs = service.CreateDrugFlowTwo(items)
  2581. // }
  2582. // }
  2583. //}
  2584. //
  2585. //if len(upDateCancelStockInfos) > 0 {
  2586. // var total int64
  2587. // var out_count int64
  2588. // var cancel_count int64
  2589. // var now_count int64
  2590. // var all_count int64
  2591. // for _, item := range upDateCancelStockInfos {
  2592. //
  2593. // //查询最后一条退库记录
  2594. // info, _ := service.GetLastCancelStockInfoOne(id, item.DrugId)
  2595. //
  2596. // errs = service.UpDateDrugCancelStockInfo(item)
  2597. //
  2598. // //查询该药品的信息
  2599. // medical, _ := service.GetBaseDrugMedical(item.DrugId)
  2600. // //统计该批次的总出库数
  2601. // outInfo, _ := service.GetDrugWarehouseOutInfo(item.BatchNumberId, item.DrugId)
  2602. // //查询该批次的总退库数量
  2603. // cancelInfo, _ := service.GetTotalDrugCancelInfo(item.BatchNumberId, item.DrugId, item.ID)
  2604. // for _, item := range outInfo {
  2605. // if item.CountUnit == medical.MaxUnit {
  2606. // item.Count = item.Count * medical.MinNumber
  2607. // }
  2608. // if item.CountUnit == medical.MinUnit {
  2609. // item.Count = item.Count
  2610. // }
  2611. // }
  2612. //
  2613. // for _, item := range outInfo {
  2614. // out_count += item.Count
  2615. // }
  2616. //
  2617. // for _, item := range cancelInfo {
  2618. // if item.MaxUnit == medical.MaxUnit {
  2619. // item.Count = item.Count * medical.MinNumber
  2620. // }
  2621. // if item.MaxUnit == medical.MinUnit {
  2622. // item.Count = item.Count
  2623. // }
  2624. // }
  2625. //
  2626. // for _, item := range cancelInfo {
  2627. // cancel_count += item.Count
  2628. // }
  2629. //
  2630. // fmt.Println("out_count232233332232323223", out_count)
  2631. //
  2632. // if item.MaxUnit == medical.MaxUnit {
  2633. // now_count = item.Count * medical.MinNumber
  2634. // }
  2635. // if item.MaxUnit == medical.MinUnit {
  2636. // now_count = item.Count
  2637. // }
  2638. // fmt.Println("hhhhhh2323322323", now_count+cancel_count)
  2639. // //总退库批次
  2640. // all_count = now_count + cancel_count
  2641. //
  2642. // //该批次的总出库数小与退库数报错返回
  2643. // if all_count > out_count {
  2644. // c.ServeSuccessJSON(map[string]interface{}{
  2645. // "msg": "4",
  2646. // })
  2647. // return
  2648. // }
  2649. //
  2650. // if item.MaxUnit == medical.MaxUnit {
  2651. // //更新库存
  2652. // total = item.Count - info.Count
  2653. // parseDateErr := service.UpdatedDrugInfo(item.BatchNumberId, total)
  2654. // fmt.Println("parseDateErr", parseDateErr)
  2655. //
  2656. // flow := models.DrugFlow{
  2657. // Count: item.Count,
  2658. // }
  2659. // service.UpdatedDrugFlow(info.CancelStockId, item.DrugId, flow)
  2660. // }
  2661. //
  2662. // if item.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  2663. // item.Count = item.Count
  2664. // total = item.Count - info.Count
  2665. // service.UpdatedDrugInfoOne(item.BatchNumberId, total)
  2666. //
  2667. // flow := models.DrugFlow{
  2668. // Count: item.Count,
  2669. // }
  2670. // service.UpdatedDrugFlow(info.CancelStockId, item.DrugId, flow)
  2671. // }
  2672. //
  2673. // //查询默认仓库
  2674. // houseConfig, _ := service.GetAllStoreHouseConfig(item.OrgId)
  2675. // //查询默认仓库剩余多少库存
  2676. // list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  2677. // var sum_count int64
  2678. // var sum_in_count int64
  2679. // for _, it := range list {
  2680. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  2681. // if it.MaxUnit == baseDrug.MaxUnit {
  2682. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  2683. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  2684. // sum_count += it.StockMaxNumber + it.StockMinNumber
  2685. // sum_in_count += it.WarehousingCount
  2686. // }
  2687. // }
  2688. // service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.OrgId)
  2689. //
  2690. // }
  2691. //}
  2692. //if errs != nil {
  2693. // utils.ErrorLog(errs.Error())
  2694. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateCancelStockFail)
  2695. // return
  2696. //}
  2697. c.ServeSuccessJSON(map[string]interface{}{
  2698. "msg": "1",
  2699. })
  2700. }
  2701. func (c *StockDrugApiController) GetAllConfig() {
  2702. adminUserInfo := c.GetAdminUserInfo()
  2703. manufacturer, _ := service.FindAllManufacturer(adminUserInfo.CurrentOrgId)
  2704. dealer, _ := service.FindAllDealer(adminUserInfo.CurrentOrgId)
  2705. stockIns, _ := service.FindStockInNumber(adminUserInfo.CurrentOrgId)
  2706. drugs, _ := service.GetDrugs(adminUserInfo.CurrentOrgId)
  2707. list, _ := service.GetAllStoreHouseListThree(adminUserInfo.CurrentOrgId)
  2708. configlist, _ := service.GetAllStoreHouseConfig(adminUserInfo.CurrentOrgId)
  2709. appId := c.GetAdminUserInfo().CurrentAppId
  2710. doctorList, _ := service.GetAllDoctorListSix(adminUserInfo.CurrentOrgId, appId)
  2711. patients, _ := service.GetAllpatientTwenty(adminUserInfo.CurrentOrgId)
  2712. c.ServeSuccessJSON(map[string]interface{}{
  2713. "manufacturer": manufacturer,
  2714. "dealer": dealer,
  2715. "drugs": drugs,
  2716. "numbers": stockIns,
  2717. "list": list,
  2718. "configlist": configlist,
  2719. "doctorList": doctorList,
  2720. "patients": patients,
  2721. })
  2722. }
  2723. func (c *StockDrugApiController) GetAllDrugStock() {
  2724. adminUserInfo := c.GetAdminUserInfo()
  2725. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  2726. dealer_id, _ := c.GetInt64("dealer_id", 0)
  2727. drugs, err := service.FindAllDrugByManufactureId(manufacturer_id, dealer_id, adminUserInfo.CurrentOrgId)
  2728. if err == nil {
  2729. c.ServeSuccessJSON(map[string]interface{}{
  2730. "drugs": drugs,
  2731. })
  2732. } else {
  2733. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  2734. }
  2735. }
  2736. func (c *StockDrugApiController) GetDrugQueryInfo() {
  2737. page, _ := c.GetInt64("page", -1)
  2738. limit, _ := c.GetInt64("limit", -1)
  2739. keyword := c.GetString("keyword")
  2740. drug_category, _ := c.GetInt64("drug_category")
  2741. start_time := c.GetString("start_time")
  2742. end_time := c.GetString("end_time")
  2743. timeLayout := "2006-01-02"
  2744. loc, _ := time.LoadLocation("Local")
  2745. var startTime int64
  2746. if len(start_time) > 0 {
  2747. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  2748. if err != nil {
  2749. fmt.Println(err)
  2750. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2751. return
  2752. }
  2753. startTime = theTime.Unix()
  2754. }
  2755. var endTime int64
  2756. if len(end_time) > 0 {
  2757. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  2758. if err != nil {
  2759. utils.ErrorLog(err.Error())
  2760. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2761. return
  2762. }
  2763. endTime = theTime.Unix()
  2764. }
  2765. adminUserInfo := c.GetAdminUserInfo()
  2766. list, total, err := service.FindAllDrugStockInfo(adminUserInfo.CurrentOrgId, page, limit, keyword, drug_category, startTime, endTime)
  2767. if err == nil {
  2768. c.ServeSuccessJSON(map[string]interface{}{
  2769. "list": list,
  2770. "total": total,
  2771. })
  2772. } else {
  2773. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  2774. }
  2775. }
  2776. func (this *StockDrugApiController) GetUserDetailInfo() {
  2777. order_id, _ := this.GetInt64("id")
  2778. if order_id <= 0 {
  2779. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2780. return
  2781. }
  2782. adminUserInfo := this.GetAdminUserInfo()
  2783. userDetails, err, total := service.FindDrugStockUserDetailById(adminUserInfo.CurrentOrgId, order_id)
  2784. if err == nil {
  2785. this.ServeSuccessJSON(map[string]interface{}{
  2786. "list": userDetails,
  2787. "total": total,
  2788. })
  2789. } else {
  2790. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  2791. return
  2792. }
  2793. }
  2794. func (this *StockDrugApiController) GetCancelDrugOrderPrint() {
  2795. orderId := this.GetString("order_id")
  2796. ids := strings.Split(orderId, ",")
  2797. orgId := this.GetAdminUserInfo().CurrentOrgId
  2798. list, _ := service.GetCancelDrugOrderPrint(ids, orgId)
  2799. this.ServeSuccessJSON(map[string]interface{}{
  2800. "list": list,
  2801. })
  2802. }
  2803. func (this *StockDrugApiController) GetDrugCancelExportList() {
  2804. orderId := this.GetString("order_id")
  2805. ids := strings.Split(orderId, ",")
  2806. orgId := this.GetAdminUserInfo().CurrentOrgId
  2807. list, _ := service.GetDrugCancelExportList(ids, orgId)
  2808. this.ServeSuccessJSON(map[string]interface{}{
  2809. "list": list,
  2810. })
  2811. }
  2812. func (c *StockDrugApiController) GetDrugWarehouseInfoPrint() {
  2813. start_time := c.GetString("start_time")
  2814. end_time := c.GetString("end_time")
  2815. order_type, _ := c.GetInt64("order_type")
  2816. storehouse_id, _ := c.GetInt64("storehouse_id")
  2817. timeLayout := "2006-01-02"
  2818. loc, _ := time.LoadLocation("Local")
  2819. var startTime int64
  2820. if len(start_time) > 0 {
  2821. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  2822. if err != nil {
  2823. fmt.Println(err)
  2824. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2825. return
  2826. }
  2827. startTime = theTime.Unix()
  2828. }
  2829. var endTime int64
  2830. if len(end_time) > 0 {
  2831. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  2832. if err != nil {
  2833. utils.ErrorLog(err.Error())
  2834. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2835. return
  2836. }
  2837. endTime = theTime.Unix()
  2838. }
  2839. adminUserInfo := c.GetAdminUserInfo()
  2840. orgId := adminUserInfo.CurrentOrgId
  2841. if order_type == 1 {
  2842. list, err := service.GetDrugWarehouseInfoPrint(startTime, endTime, orgId, storehouse_id)
  2843. manufacturerList, _ := service.GetAllManufacturerList(orgId)
  2844. dealerList, _ := service.GetAllDealerList(orgId)
  2845. if err == nil {
  2846. c.ServeSuccessJSON(map[string]interface{}{
  2847. "list": list,
  2848. "manufacturerList": manufacturerList,
  2849. "dealerList": dealerList,
  2850. })
  2851. return
  2852. }
  2853. }
  2854. }
  2855. func (c *StockDrugApiController) GetDrugOutOrderPrint() {
  2856. start_time := c.GetString("start_time")
  2857. end_time := c.GetString("end_time")
  2858. order_type, _ := c.GetInt64("order_type")
  2859. storehouse_id, _ := c.GetInt64("storehouse_id")
  2860. timeLayout := "2006-01-02"
  2861. loc, _ := time.LoadLocation("Local")
  2862. var startTime int64
  2863. if len(start_time) > 0 {
  2864. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  2865. if err != nil {
  2866. fmt.Println(err)
  2867. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2868. return
  2869. }
  2870. startTime = theTime.Unix()
  2871. }
  2872. var endTime int64
  2873. if len(end_time) > 0 {
  2874. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  2875. if err != nil {
  2876. utils.ErrorLog(err.Error())
  2877. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2878. return
  2879. }
  2880. endTime = theTime.Unix()
  2881. }
  2882. adminUserInfo := c.GetAdminUserInfo()
  2883. orgId := adminUserInfo.CurrentOrgId
  2884. dealerList, _ := service.GetAllDealerList(orgId)
  2885. manufacturerList, _ := service.GetAllManufacturerList(orgId)
  2886. if order_type == 2 {
  2887. list, err := service.GetDrugOutOrderInfoPrintList(startTime, endTime, orgId, storehouse_id)
  2888. if err == nil {
  2889. c.ServeSuccessJSON(map[string]interface{}{
  2890. "list": list,
  2891. "dealerList": dealerList,
  2892. "manufacturerList": manufacturerList,
  2893. })
  2894. return
  2895. }
  2896. }
  2897. }
  2898. func (c *StockDrugApiController) GetConfigList() {
  2899. adminUserInfo := c.GetAdminUserInfo()
  2900. manufacturer, _ := service.FindAllManufacturer(adminUserInfo.CurrentOrgId)
  2901. dealer, _ := service.FindAllDealer(adminUserInfo.CurrentOrgId)
  2902. stockIns, _ := service.FindStockInNumber(adminUserInfo.CurrentOrgId)
  2903. list, _ := service.GetAllStoreHouseListThree(adminUserInfo.CurrentOrgId)
  2904. configlist, _ := service.GetAllStoreHouseConfig(adminUserInfo.CurrentOrgId)
  2905. appId := c.GetAdminUserInfo().CurrentAppId
  2906. doctorList, _ := service.GetAllDoctorListSix(adminUserInfo.CurrentOrgId, appId)
  2907. c.ServeSuccessJSON(map[string]interface{}{
  2908. "manufacturer": manufacturer,
  2909. "dealer": dealer,
  2910. "numbers": stockIns,
  2911. "list": list,
  2912. "configlist": configlist,
  2913. "doctorList": doctorList,
  2914. })
  2915. }
  2916. func (c *StockDrugApiController) CheckWarehousingInfo() {
  2917. warehousing_id, _ := c.GetInt64("warehousing_id")
  2918. orgId := c.GetAdminUserInfo().CurrentOrgId
  2919. // 开始外部循环的事务
  2920. db := service.XTWriteDB()
  2921. tx := db.Begin()
  2922. // 在函数结束时处理事务回滚
  2923. defer func() {
  2924. if r := recover(); r != nil {
  2925. tx.Rollback()
  2926. }
  2927. }()
  2928. houseConfig, _ := service.GetNewAllStoreHouseConfig(orgId, tx)
  2929. checker := c.GetAdminUserInfo().AdminUser.Id
  2930. //更改核对状态
  2931. service.CheckNewWarehousingInfo(warehousing_id, orgId, checker, tx)
  2932. list, _ := service.GetNewWarehousingInfoByList(warehousing_id, orgId, tx)
  2933. var is_success = 2
  2934. for _, item := range list {
  2935. var his_warehousing_count = item.WarehousingCount
  2936. var his_max_unit = item.MaxUnit
  2937. var in_cout int64
  2938. medical, _ := service.GetNewBaseDrugMedical(item.DrugId, tx)
  2939. if his_max_unit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  2940. in_cout = his_warehousing_count * medical.MinNumber
  2941. }
  2942. if his_max_unit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  2943. in_cout = his_warehousing_count
  2944. }
  2945. if his_max_unit == medical.MinUnit && medical.MaxUnit == medical.MinUnit {
  2946. in_cout = his_warehousing_count
  2947. }
  2948. if item.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  2949. //新增库存
  2950. service.AddNewDrugWarehouseStockMaxNumber(item.WarehousingCount, item.ID, tx)
  2951. }
  2952. if item.MaxUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  2953. //新增库存
  2954. service.AddNewDrugWarehouseStockMaxNumber(item.WarehousingCount, item.ID, tx)
  2955. }
  2956. if item.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  2957. //新增库存
  2958. service.AddNewDrugWarehouseStockMinNumber(item.WarehousingCount, item.ID, tx)
  2959. }
  2960. Creator := c.GetAdminUserInfo().AdminUser.Id
  2961. //查询默认仓库
  2962. //查询默认仓库剩余多少库存
  2963. list, _ := service.GetNewDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.OrgId, item.DrugId, tx)
  2964. var sum_count int64
  2965. var sum_in_count int64
  2966. for _, it := range list {
  2967. baseDrug, _ := service.GetNewBaseDrugMedical(it.DrugId, tx)
  2968. if it.MaxUnit == baseDrug.MaxUnit {
  2969. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  2970. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  2971. }
  2972. sum_count += it.StockMaxNumber + it.StockMinNumber
  2973. sum_in_count += it.WarehousingCount
  2974. }
  2975. flow := &models.DrugFlow{
  2976. WarehousingId: 0,
  2977. DrugId: item.DrugId,
  2978. Number: item.Number,
  2979. BatchNumber: item.BatchNumber,
  2980. Count: item.WarehousingCount,
  2981. UserOrgId: item.OrgId,
  2982. PatientId: 0,
  2983. SystemTime: time.Now().Unix(),
  2984. ConsumableType: 1,
  2985. IsSys: 0,
  2986. WarehousingOrder: item.WarehousingOrder,
  2987. WarehouseOutId: 0,
  2988. WarehouseOutOrderNumber: "",
  2989. IsEdit: 0,
  2990. CancelStockId: 0,
  2991. CancelOrderNumber: "",
  2992. Manufacturer: 0,
  2993. Dealer: 0,
  2994. Creator: Creator,
  2995. UpdateCreator: Creator,
  2996. Status: 1,
  2997. Ctime: item.Ctime,
  2998. Mtime: 0,
  2999. Price: item.Price,
  3000. WarehousingDetailId: item.ID,
  3001. WarehouseOutDetailId: 0,
  3002. CancelOutDetailId: 0,
  3003. ExpireDate: item.ExpiryDate,
  3004. ProductDate: item.ProductDate,
  3005. MaxUnit: item.MaxUnit,
  3006. MinUnit: item.MinUnit,
  3007. StorehouseId: item.StorehouseId,
  3008. OverCount: sum_count,
  3009. OperateTime: time.Now().Unix(),
  3010. DrugCode: item.DrugCode,
  3011. }
  3012. service.CreateNewDrugFlowTwo(flow, tx)
  3013. //更新基础库剩余库存
  3014. service.UpdateNewMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.OrgId, tx)
  3015. drugInfolist, _ := service.GetNewDrugSumCountByStorehouseId(item.StorehouseId, item.OrgId, item.DrugId, tx)
  3016. var total_count int64 // 入库总数量
  3017. var over_count int64 //剩余库存
  3018. for _, it := range drugInfolist {
  3019. baseDrug, _ := service.GetNewBaseDrugMedical(it.DrugId, tx)
  3020. if it.MaxUnit == baseDrug.MaxUnit {
  3021. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  3022. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  3023. }
  3024. }
  3025. for _, it := range drugInfolist {
  3026. total_count += it.WarehousingCount
  3027. over_count += it.StockMaxNumber + it.StockMinNumber
  3028. }
  3029. //查询该仓库是否有默认数据
  3030. drugStockCount, _ := service.GetNewDrugStockCount(item.StorehouseId, item.DrugId, item.OrgId, tx)
  3031. if drugStockCount.ID == 0 {
  3032. drugStock := models.XtDrugStockCount{
  3033. UserOrgId: item.OrgId,
  3034. StorehouseId: item.StorehouseId,
  3035. SumInCount: total_count,
  3036. SumOutCount: 0,
  3037. SumCancelCount: 0,
  3038. DrugId: item.DrugId,
  3039. Ctime: time.Now().Unix(),
  3040. Mtime: 0,
  3041. Status: 1,
  3042. FlushCount: over_count,
  3043. SumActOutCount: 0,
  3044. }
  3045. service.CreateNewDrugStockSum(drugStock, tx)
  3046. }
  3047. if drugStockCount.ID > 0 {
  3048. service.UpdateNewDrugStockSum(item.StorehouseId, item.DrugId, item.OrgId, total_count, over_count, tx)
  3049. }
  3050. flowList, _ := service.GetNewDrugFlowList(item.DrugId, item.OrgId, item.ID, tx)
  3051. var his_count int64
  3052. if len(flowList) > 0 {
  3053. for _, item := range flowList {
  3054. baseDrug, _ := service.GetNewBaseDrugMedical(item.DrugId, tx)
  3055. if item.MaxUnit == baseDrug.MaxUnit && baseDrug.MaxUnit != baseDrug.MinUnit {
  3056. his_count = item.Count * baseDrug.MinNumber
  3057. }
  3058. if item.MaxUnit == baseDrug.MinUnit && baseDrug.MaxUnit != baseDrug.MinUnit {
  3059. his_count = item.Count
  3060. }
  3061. if item.MaxUnit == baseDrug.MaxUnit && baseDrug.MaxUnit == baseDrug.MinUnit {
  3062. his_count = item.Count
  3063. }
  3064. }
  3065. }
  3066. if in_cout != his_count {
  3067. is_success = 1
  3068. tx.Rollback()
  3069. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeFlow)
  3070. return
  3071. }
  3072. }
  3073. if is_success != 1 {
  3074. tx.Commit()
  3075. }
  3076. c.ServeSuccessJSON(map[string]interface{}{
  3077. "list": list,
  3078. })
  3079. return
  3080. }
  3081. func (c *StockDrugApiController) ReturnCheckWarehouseingInfo() {
  3082. warehousing_id, _ := c.GetInt64("warehousing_id")
  3083. orgId := c.GetAdminUserInfo().CurrentOrgId
  3084. // 开始外部循环的事务
  3085. db := service.XTWriteDB()
  3086. tx := db.Begin()
  3087. // 在函数结束时处理事务回滚
  3088. defer func() {
  3089. if r := recover(); r != nil {
  3090. tx.Rollback()
  3091. }
  3092. }()
  3093. list, _ := service.GetNewWarehousingInfoByList(warehousing_id, orgId, tx)
  3094. for _, item := range list {
  3095. //查询该药品是否有出库数据
  3096. outInfo, _ := service.GetNewDrugWarehouseOutByWarehouseInfoId(item.ID, item.DrugId, item.OrgId, tx)
  3097. if len(outInfo) > 0 {
  3098. tx.Rollback()
  3099. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCheckGoodParamWrong)
  3100. return
  3101. }
  3102. }
  3103. for _, item := range list {
  3104. //删除流水
  3105. service.UpdateNewWarehousingInfoFlow(item.ID, orgId, tx)
  3106. medical, _ := service.GetNewBaseDrugMedical(item.DrugId, tx)
  3107. if item.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3108. //新增库存
  3109. service.AddNewDrugReturnWarehouseStockMaxNumber(item.WarehousingCount, item.ID, tx)
  3110. }
  3111. if item.MaxUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3112. //新增库存
  3113. service.AddNewDrugReturnWarehouseStockMaxNumber(item.WarehousingCount, item.ID, tx)
  3114. }
  3115. if item.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3116. //新增库存
  3117. service.AddNewDrugReturnWarehouseStockMinNumber(item.WarehousingCount, item.ID, tx)
  3118. }
  3119. storeHouseConfig, _ := service.GetNewAllStoreHouseConfig(orgId, tx)
  3120. stockInfoList, _ := service.GetNewDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgId, item.DrugId, tx)
  3121. var sum_count int64
  3122. var sum_in_count int64
  3123. for _, its := range stockInfoList {
  3124. if its.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3125. sum_count += its.StockMaxNumber * medical.MinNumber
  3126. sum_in_count += its.WarehousingCount * medical.MinNumber
  3127. }
  3128. if its.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3129. sum_count += its.StockMaxNumber
  3130. sum_in_count += its.WarehousingCount
  3131. }
  3132. if its.MaxUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3133. sum_count += its.StockMaxNumber
  3134. sum_in_count += its.WarehousingCount
  3135. }
  3136. }
  3137. service.UpdateNewBaseDrugSumInfo(sum_count, item.DrugId, item.OrgId, sum_in_count, tx)
  3138. var total_count int64
  3139. if item.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3140. total_count = item.WarehousingCount * medical.MinNumber
  3141. }
  3142. if item.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3143. total_count = item.WarehousingCount
  3144. }
  3145. if item.MaxUnit == medical.MinUnit && medical.MaxUnit == medical.MinUnit {
  3146. total_count = item.WarehousingCount * medical.MinNumber
  3147. }
  3148. drugInfolist, _ := service.GetNewDrugSumCountByStorehouseId(item.StorehouseId, item.OrgId, item.DrugId, tx)
  3149. var over_count int64 //剩余库存
  3150. for _, it := range drugInfolist {
  3151. if it.MaxUnit == medical.MaxUnit {
  3152. it.StockMaxNumber = it.StockMaxNumber * medical.MinNumber
  3153. }
  3154. }
  3155. for _, it := range drugInfolist {
  3156. over_count += it.StockMaxNumber*medical.MinNumber + it.StockMinNumber
  3157. }
  3158. //扣减库存
  3159. service.ReduceNewDrugStockCount(item.StorehouseId, item.DrugId, item.OrgId, total_count, over_count, tx)
  3160. }
  3161. //更新审核
  3162. err := service.ReturnNewCheckWarehouseingInfo(warehousing_id, orgId, tx)
  3163. tx.Commit()
  3164. if err == nil {
  3165. c.ServeSuccessJSON(map[string]interface{}{
  3166. "msg": "反审核成功!",
  3167. })
  3168. return
  3169. }
  3170. }
  3171. func (c *StockDrugApiController) CheckDrugOut() {
  3172. warehouse_out_id, _ := c.GetInt64("warehouse_out_id")
  3173. orgId := c.GetAdminUserInfo().CurrentOrgId
  3174. // 开始外部循环的事务
  3175. db := service.XTWriteDB()
  3176. tx := db.Begin()
  3177. // 在函数结束时处理事务回滚
  3178. defer func() {
  3179. if r := recover(); r != nil {
  3180. tx.Rollback()
  3181. }
  3182. }()
  3183. warehousingOutInfoList, _ := service.GetNewDrugWarehouseOutListById(warehouse_out_id, orgId, tx)
  3184. drugWarehouseOut, _ := service.GetNewDrugWarehouseOutByLastId(warehouse_out_id, tx)
  3185. for _, it := range warehousingOutInfoList {
  3186. var total_count int64
  3187. var out_count int64
  3188. //查询剩余库存
  3189. stockInfo, _ := service.GetNewDrugAllStockInfo(it.StorehouseId, it.OrgId, it.DrugId, tx)
  3190. drup, _ := service.FindNewBaseDrugLibRecord(it.OrgId, it.DrugId, tx)
  3191. for _, item := range stockInfo {
  3192. if item.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  3193. item.StockMaxNumber = item.StockMaxNumber * drup.MinNumber
  3194. }
  3195. total_count += item.StockMaxNumber + item.StockMinNumber
  3196. }
  3197. //出库数量累加
  3198. if it.CountUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  3199. out_count = it.Count * drup.MinNumber
  3200. }
  3201. if it.CountUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  3202. out_count = it.Count
  3203. }
  3204. if it.CountUnit == drup.MinUnit && drup.MaxUnit == drup.MinUnit {
  3205. out_count = it.Count
  3206. }
  3207. if out_count > total_count {
  3208. c.ServeSuccessJSON(map[string]interface{}{
  3209. "warehousingOutInfoList": warehousingOutInfoList,
  3210. "drug_name": drup.DrugName,
  3211. "dose": drup.Dose,
  3212. "dose_unit": drup.DoseUnit,
  3213. "min_number": drup.MinNumber,
  3214. "min_unit": drup.MinUnit,
  3215. "max_unit": drup.MaxUnit,
  3216. "msg": 2,
  3217. })
  3218. tx.Rollback()
  3219. return
  3220. }
  3221. }
  3222. warehousingOutInfoListTwo, _ := service.GetNewDrugWarehouseOutListById(warehouse_out_id, orgId, tx)
  3223. for _, it := range warehousingOutInfoListTwo {
  3224. medical, _ := service.GetBaseDrugMedical(it.DrugId)
  3225. ////出库数量累加
  3226. var out_count_one int64
  3227. if it.CountUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3228. out_count_one = it.Count * medical.MinNumber
  3229. }
  3230. if it.CountUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3231. out_count_one = it.Count
  3232. }
  3233. if it.CountUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3234. out_count_one = it.Count
  3235. }
  3236. service.AddNewDrugCount(it.DrugId, it.OrgId, it.StorehouseId, out_count_one, tx)
  3237. }
  3238. warehousingOutInfoListThree, _ := service.GetNewDrugWarehouseOutListById(warehouse_out_id, orgId, tx)
  3239. var is_success = 2
  3240. for _, it := range warehousingOutInfoListThree {
  3241. var his_count = it.Count
  3242. var his_count_unit = it.CountUnit
  3243. medical, _ := service.GetNewBaseDrugMedical(it.DrugId, tx)
  3244. drup, _ := service.FindNewBaseDrugLibRecord(it.OrgId, it.DrugId, tx)
  3245. //出库逻辑
  3246. service.AutoNewDrugDeliverInfoFourtyOne(orgId, it.Count, &drugWarehouseOut, &drup, it, it.CountUnit, tx)
  3247. //查询剩余库存
  3248. stockInfo, _ := service.GetNewDrugAllStockInfo(it.StorehouseId, it.OrgId, it.DrugId, tx)
  3249. var sum_count int64
  3250. for _, its := range stockInfo {
  3251. if its.MaxUnit == medical.MaxUnit {
  3252. its.StockMaxNumber = its.StockMaxNumber * medical.MinNumber
  3253. }
  3254. sum_count += its.StockMaxNumber + its.StockMinNumber
  3255. }
  3256. service.UpdateNewDrugStockCount(it.DrugId, it.OrgId, it.StorehouseId, sum_count, tx)
  3257. storeHouseConfig, _ := service.GetNewAllStoreHouseConfig(orgId, tx)
  3258. stockInfoOne, _ := service.GetNewDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, it.OrgId, it.DrugId, tx)
  3259. var sum_count_one int64
  3260. for _, its := range stockInfoOne {
  3261. if its.MaxUnit == medical.MaxUnit {
  3262. its.StockMaxNumber = its.StockMaxNumber * medical.MinNumber
  3263. }
  3264. sum_count_one += its.StockMaxNumber + its.StockMinNumber
  3265. }
  3266. //更新基础库库存
  3267. service.UpdateNewBaseDrugSumTwo(it.DrugId, sum_count_one, it.OrgId, tx)
  3268. if it.OrgId == 10164 || it.OrgId == 10721 {
  3269. var now_count int64
  3270. if his_count_unit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3271. now_count = his_count * medical.MinNumber
  3272. }
  3273. if his_count_unit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3274. now_count = his_count
  3275. }
  3276. if his_count_unit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3277. now_count = his_count
  3278. }
  3279. var flow_count int64
  3280. //查询该药品的出库数量
  3281. drugflowlist, _ := service.GetDrugOutFlowByWarehouseOut(it.DrugId, it.ID, it.OrgId, tx)
  3282. if len(drugflowlist) > 0 {
  3283. for _, items := range drugflowlist {
  3284. if items.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3285. flow_count = items.Count * medical.MinNumber
  3286. }
  3287. if items.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3288. flow_count = items.Count
  3289. }
  3290. if items.MaxUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3291. flow_count = items.Count
  3292. }
  3293. }
  3294. }
  3295. if now_count != flow_count {
  3296. is_success = 1
  3297. tx.Rollback()
  3298. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeFlow)
  3299. return
  3300. }
  3301. }
  3302. }
  3303. out := models.DrugWarehouseOut{
  3304. IsCheck: 1,
  3305. }
  3306. //更新审核状态
  3307. service.UpdateNewCheckDrugOut(out, warehouse_out_id, tx)
  3308. //查询
  3309. if is_success != 1 {
  3310. tx.Commit()
  3311. c.ServeSuccessJSON(map[string]interface{}{
  3312. "warehousingOutInfoList": warehousingOutInfoList,
  3313. "drug_name": "",
  3314. "dose": "",
  3315. "dose_unit": "",
  3316. "min_number": "",
  3317. "min_unit": "",
  3318. "max_unit": "",
  3319. "msg": 1,
  3320. })
  3321. return
  3322. }
  3323. }
  3324. func (c *StockDrugApiController) ToReturnCheck() {
  3325. warehouse_out_id, _ := c.GetInt64("warehouse_out_id")
  3326. out := models.DrugWarehouseOut{
  3327. IsCheck: 2,
  3328. }
  3329. service.UpdateCheckDrugOut(out, warehouse_out_id)
  3330. outInfo := models.DrugWarehouseOutInfo{
  3331. IsCheck: 2,
  3332. }
  3333. // 开始外部循环的事务
  3334. db := service.XTWriteDB()
  3335. tx := db.Begin()
  3336. // 在函数结束时处理事务回滚
  3337. defer func() {
  3338. if r := recover(); r != nil {
  3339. tx.Rollback()
  3340. }
  3341. }()
  3342. service.UpdateNewCheckDrugOutInfoList(outInfo, warehouse_out_id, tx)
  3343. creater := c.GetAdminUserInfo().AdminUser.Id
  3344. orgId := c.GetAdminUserInfo().CurrentOrgId
  3345. list, _ := service.GetDrugWarehouseOutListById(warehouse_out_id, orgId)
  3346. storeHouseConfig, _ := service.GetAllStoreHouseConfig(orgId)
  3347. //调用出库逻辑
  3348. for _, item := range list {
  3349. medical, _ := service.GetBaseDrugMedical(item.DrugId)
  3350. if item.CountUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3351. var sum_out_count int64
  3352. sum_out_count = item.Count * medical.MinNumber
  3353. service.UpdateNewDrugMaxNumberOne(sum_out_count, item.WarehouseInfoId, tx)
  3354. service.ModifyNewReduceDrugInformation(item.DrugId, sum_out_count, item.OrgId, tx)
  3355. drugInfoList, _ := service.FindNewDrugWarehouseInfoList(item.DrugId, item.OrgId, tx)
  3356. var drug_max_number int64
  3357. var drug_min_number int64
  3358. for _, item := range drugInfoList {
  3359. drug_max_number += item.StockMaxNumber
  3360. drug_min_number += item.StockMinNumber
  3361. }
  3362. drugFlushInfo := models.XtDrugWarehouseFlushInfo{
  3363. DrugMaxNumber: drug_max_number,
  3364. UserOrgId: item.OrgId,
  3365. WarehouseOutId: item.ID,
  3366. Type: 2,
  3367. SystemTime: item.SysRecordTime,
  3368. PatientId: 0,
  3369. Ctime: time.Now().Unix(),
  3370. Mtime: 0,
  3371. BatchNumberId: item.WarehouseInfoId,
  3372. DrugId: item.DrugId,
  3373. Count: item.Count,
  3374. WarehouseOutOrderNumber: item.WarehouseOutOrderNumber,
  3375. Creater: creater,
  3376. DrugMinNumber: drug_min_number,
  3377. Unit: item.CountUnit,
  3378. Status: 1,
  3379. }
  3380. service.CreatedNewDrugFlushInfo(drugFlushInfo, tx)
  3381. service.DeleteNewDrugOutFlow(item.ID, item.OrgId, tx)
  3382. }
  3383. if item.CountUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3384. service.UpdateDrugMaxNumber(item.Count, item.WarehouseInfoId)
  3385. var sum_out_count int64
  3386. sum_out_count = item.Count * medical.MinNumber
  3387. service.ModifyReduceDrugInformation(item.DrugId, sum_out_count, item.OrgId)
  3388. service.DeleteDrugOutFlow(item.ID, item.OrgId)
  3389. }
  3390. if item.CountUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3391. service.UpdateNewDrugMinNumber(item.Count, item.WarehouseInfoId, tx)
  3392. var sum_out_count int64
  3393. sum_out_count = item.Count
  3394. service.ModifyNewReduceDrugInformation(item.DrugId, sum_out_count, item.OrgId, tx)
  3395. service.DeleteNewDrugOutFlow(item.ID, item.OrgId, tx)
  3396. }
  3397. stockInfo, _ := service.GetNewDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, item.OrgId, item.DrugId, tx)
  3398. var sum_count int64
  3399. for _, its := range stockInfo {
  3400. baseDrug, _ := service.GetNewBaseDrugMedical(its.DrugId, tx)
  3401. if its.MaxUnit == baseDrug.MaxUnit {
  3402. its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
  3403. }
  3404. sum_count += its.StockMaxNumber + its.StockMinNumber
  3405. }
  3406. service.UpdateNewBaseDrugSumTwo(item.DrugId, sum_count, item.OrgId, tx)
  3407. //出库数量累加
  3408. var out_count int64
  3409. if item.CountUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3410. out_count = item.Count * medical.MinNumber
  3411. }
  3412. if item.CountUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3413. out_count = item.Count
  3414. }
  3415. if item.CountUnit == medical.MinUnit && medical.MaxUnit == medical.MinUnit {
  3416. out_count = item.Count
  3417. }
  3418. //增加出库数量
  3419. service.ReduceNewDrugCount(item.DrugId, item.OrgId, item.StorehouseId, out_count, tx)
  3420. //查询该药品剩余库存
  3421. infolist, _ := service.FindNewDrugWarehouseInfoFlushCountByStorehouse(item.DrugId, item.OrgId, item.StorehouseId, tx)
  3422. var over_count int64
  3423. for _, its := range infolist {
  3424. baseDrug, _ := service.GetBaseDrugMedical(its.DrugId)
  3425. if its.MaxUnit == baseDrug.MaxUnit {
  3426. its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
  3427. }
  3428. over_count += its.StockMaxNumber + its.StockMinNumber
  3429. }
  3430. service.UpdateNewDrugStockCount(item.DrugId, item.OrgId, item.StorehouseId, over_count, tx)
  3431. }
  3432. tx.Commit()
  3433. c.ServeSuccessJSON(map[string]interface{}{
  3434. "out": out,
  3435. })
  3436. }
  3437. func (c *StockDrugApiController) CheckCancelDrugStock() {
  3438. cancelstock_id, _ := c.GetInt64("cancelstock_id")
  3439. orgId := c.GetAdminUserInfo().CurrentOrgId
  3440. cancelDrugStock, _ := service.GetCancelDrugStock(cancelstock_id, orgId)
  3441. list, _ := service.GetCancelDrugStockList(cancelstock_id, orgId)
  3442. cancelStock := models.DrugCancelStock{
  3443. IsCheck: 1,
  3444. }
  3445. var manufacturer_id int64
  3446. var dealer_id int64
  3447. manufacturerList, _ := service.GetAllManufacturerList(orgId)
  3448. dealerList, _ := service.GetAllDealerList(orgId)
  3449. creater := c.GetAdminUserInfo().AdminUser.Id
  3450. //查询退库单据
  3451. cancelList, _ := service.GetDrugCancelCheckByIdTwenty(cancelstock_id, orgId)
  3452. storeHouseConfig, _ := service.GetAllStoreHouseConfig(orgId)
  3453. var cancel_total_count int64
  3454. var out_count int64
  3455. for _, its := range cancelList {
  3456. medical, _ := service.GetBaseDrugMedical(its.DrugId)
  3457. //查询该批次的出库数据
  3458. outList, _ := service.GetDrugWarehouseOutList(its.BatchNumberId, its.DrugId, its.OrgId)
  3459. for _, item := range outList {
  3460. if item.CountUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3461. out_count += item.Count * medical.MinNumber
  3462. }
  3463. if item.CountUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3464. out_count += item.Count
  3465. }
  3466. if item.CountUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3467. out_count += item.Count
  3468. }
  3469. }
  3470. if its.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3471. cancel_total_count += its.Count * medical.MinNumber
  3472. }
  3473. if its.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3474. cancel_total_count += its.Count
  3475. }
  3476. if its.MaxUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3477. cancel_total_count += its.Count
  3478. }
  3479. if cancel_total_count > out_count {
  3480. c.ServeSuccessJSON(map[string]interface{}{
  3481. "drug_name": medical.DrugName,
  3482. "dose": medical.Dose,
  3483. "dose_unit": medical.DoseUnit,
  3484. "min_number": medical.MinNumber,
  3485. "min_unit": medical.MinUnit,
  3486. "max_unit": medical.MaxUnit,
  3487. "msg": "2",
  3488. })
  3489. return
  3490. }
  3491. }
  3492. for _, its := range list {
  3493. for _, item := range manufacturerList {
  3494. if its.Manufacturer == item.ManufacturerName {
  3495. manufacturer_id = item.ID
  3496. }
  3497. }
  3498. for _, item := range dealerList {
  3499. if its.Dealer == item.DealerName {
  3500. dealer_id = item.ID
  3501. }
  3502. }
  3503. medical, _ := service.GetBaseDrugMedical(its.DrugId)
  3504. if its.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3505. var sum_cancel_count int64
  3506. sum_cancel_count += its.Count * medical.MinNumber
  3507. //出库数量减少
  3508. service.ModifyAddDrugOutReduceInfomation(its.DrugId, sum_cancel_count, its.OrgId, its.StorehouseId)
  3509. }
  3510. if its.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3511. //出库数量减少
  3512. service.ModifyAddDrugOutReduceInfomation(its.DrugId, its.Count, its.OrgId, its.StorehouseId)
  3513. }
  3514. if its.MaxUnit == medical.MinUnit && medical.MaxUnit == medical.MinUnit {
  3515. //出库数量减少
  3516. service.ModifyAddDrugOutReduceInfomation(its.DrugId, its.Count, its.OrgId, its.StorehouseId)
  3517. }
  3518. }
  3519. for _, its := range cancelList {
  3520. medical, _ := service.GetBaseDrugMedical(its.DrugId)
  3521. //扣减库存
  3522. if its.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3523. service.ModifyDrugWarehouseInfoStockMaxNumber(its.Count, its.DrugId, its.OrgId, its.BatchNumberId)
  3524. }
  3525. if its.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3526. service.ModifyDrugWarehouseInfoStockMinNumber(its.Count, its.DrugId, its.OrgId, its.BatchNumberId)
  3527. }
  3528. if its.MaxUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3529. service.ModifyDrugWarehouseInfoStockMaxNumber(its.Count, its.DrugId, its.OrgId, its.BatchNumberId)
  3530. }
  3531. stockInfo, _ := service.GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, its.OrgId, its.DrugId)
  3532. var sum_count int64
  3533. for _, its := range stockInfo {
  3534. baseDrug, _ := service.GetBaseDrugMedical(its.DrugId)
  3535. if its.MaxUnit == baseDrug.MaxUnit {
  3536. its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
  3537. }
  3538. sum_count += its.StockMaxNumber + its.StockMinNumber
  3539. }
  3540. //更新剩余库存
  3541. service.UpdateBaseDrugSumTwo(its.DrugId, sum_count, its.OrgId)
  3542. var cancel_count int64
  3543. if its.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3544. cancel_count = its.Count * medical.MinNumber
  3545. }
  3546. if its.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3547. cancel_count = its.Count
  3548. }
  3549. if its.MaxUnit == medical.MinUnit && medical.MaxUnit == medical.MinUnit {
  3550. cancel_count = its.Count * medical.MinNumber
  3551. }
  3552. cancelInfo, _ := service.GetDrugAllStockInfo(its.StorehouseId, its.OrgId, its.DrugId)
  3553. var over_count int64
  3554. for _, its := range cancelInfo {
  3555. if its.MaxUnit == medical.MaxUnit {
  3556. its.StockMaxNumber = its.StockMaxNumber * medical.MinNumber
  3557. }
  3558. over_count += its.StockMaxNumber + its.StockMinNumber
  3559. }
  3560. //查询该批次的退库数量
  3561. drugFlow := models.DrugFlow{
  3562. ID: 0,
  3563. WarehousingId: its.BatchNumberId,
  3564. DrugId: its.DrugId,
  3565. Number: its.Number,
  3566. BatchNumber: "",
  3567. Count: its.Count,
  3568. UserOrgId: its.OrgId,
  3569. PatientId: 0,
  3570. SystemTime: cancelDrugStock.ReturnTime,
  3571. ConsumableType: 4,
  3572. IsSys: 0,
  3573. WarehousingOrder: "",
  3574. WarehouseOutId: 0,
  3575. WarehouseOutOrderNumber: "",
  3576. IsEdit: 0,
  3577. CancelStockId: 0,
  3578. CancelOrderNumber: its.OrderNumber,
  3579. Manufacturer: manufacturer_id,
  3580. Dealer: dealer_id,
  3581. Creator: creater,
  3582. UpdateCreator: 0,
  3583. Status: 1,
  3584. Ctime: time.Now().Unix(),
  3585. Mtime: 0,
  3586. Price: its.Price,
  3587. WarehousingDetailId: its.BatchNumberId,
  3588. WarehouseOutDetailId: 0,
  3589. CancelOutDetailId: its.ID,
  3590. ExpireDate: its.ExpiryDate,
  3591. ProductDate: its.ProductDate,
  3592. MaxUnit: its.MaxUnit,
  3593. MinUnit: "",
  3594. StockMaxNumber: 0,
  3595. StockMinNumber: 0,
  3596. LastStockMaxNumber: 0,
  3597. LastStockMinNumber: 0,
  3598. AdviceId: 0,
  3599. SupplyWarehouseId: 0,
  3600. SupplyCancelOutId: 0,
  3601. SupplyWarehouseDetailInfo: 0,
  3602. StorehouseId: its.StorehouseId,
  3603. SecondWarehouseInfoId: 0,
  3604. AdminUserId: 0,
  3605. LastPrice: 0,
  3606. StockCount: "",
  3607. PharmacyId: 0,
  3608. OverCount: over_count,
  3609. }
  3610. service.CreateDrugFlowOne(drugFlow)
  3611. //增加退库数量 更新剩余库存
  3612. service.AddCancelSumCount(its.StorehouseId, its.DrugId, its.OrgId, cancel_count, over_count)
  3613. //更改审核状态
  3614. service.ModifyCancelStock(cancelstock_id, cancelStock, orgId)
  3615. }
  3616. //新增库存
  3617. c.ServeSuccessJSON(map[string]interface{}{
  3618. "drug_name": "",
  3619. "dose": "",
  3620. "dose_unit": "",
  3621. "min_number": "",
  3622. "min_unit": "",
  3623. "max_unit": "",
  3624. "msg": "1",
  3625. })
  3626. return
  3627. }
  3628. func (c *StockDrugApiController) ReturnDrugCancelStock() {
  3629. cancelstock_id, _ := c.GetInt64("cancelstock_id")
  3630. orgId := c.GetAdminUserInfo().CurrentOrgId
  3631. drugStock := models.DrugCancelStock{
  3632. IsCheck: 2,
  3633. }
  3634. err := service.ReturnDrugCancelStock(cancelstock_id, drugStock)
  3635. list, err := service.GetDrugCancelStockInfo(cancelstock_id, orgId)
  3636. storeHouseConfig, _ := service.GetAllStoreHouseConfig(orgId)
  3637. for _, item := range list {
  3638. //删除流水
  3639. service.ModifyDrugFlowByCancelId(item.ID, item.DrugId, item.OrgId)
  3640. //扣减库存
  3641. medical, _ := service.GetBaseDrugMedical(item.DrugId)
  3642. if item.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3643. var sum_cancel_count int64
  3644. sum_cancel_count += item.Count * medical.MinNumber
  3645. service.ModifyDrugMaxNumberWarehouseInfo(item.BatchNumberId, item.Count, item.OrgId)
  3646. //退库数量减少
  3647. //service.ModifyDrugCancelWarehouseInfo(item.DrugId,sum_cancel_count,item.OrgId,item.StorehouseId)
  3648. //出库数量增加
  3649. service.ModifyAddDrugOutAddInfomation(item.DrugId, sum_cancel_count, item.OrgId, item.StorehouseId)
  3650. }
  3651. if item.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3652. service.ModifyDrugMinNumberWarehouseInfo(item.BatchNumberId, item.Count, item.OrgId)
  3653. //退库数量减少
  3654. //service.ModifyDrugCancelWarehouseInfo(item.DrugId,item.Count,item.OrgId,item.StorehouseId)
  3655. //出库数量增加
  3656. service.ModifyAddDrugOutAddInfomation(item.DrugId, item.Count, item.OrgId, item.StorehouseId)
  3657. }
  3658. if item.MaxUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3659. service.ModifyDrugMaxNumberWarehouseInfo(item.BatchNumberId, item.Count, item.OrgId)
  3660. //退库数量减少
  3661. //service.ModifyDrugCancelWarehouseInfo(item.DrugId,item.Count,item.OrgId,item.StorehouseId)
  3662. //出库数量增加
  3663. service.ModifyAddDrugOutAddInfomation(item.DrugId, item.Count, item.OrgId, item.StorehouseId)
  3664. }
  3665. stockInfo, _ := service.GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  3666. var sum_count int64
  3667. for _, its := range stockInfo {
  3668. if its.MaxUnit == medical.MaxUnit {
  3669. its.StockMaxNumber = its.StockMaxNumber * medical.MinNumber
  3670. }
  3671. sum_count += its.StockMaxNumber + its.StockMinNumber
  3672. }
  3673. service.UpdateBaseDrugSumTwo(item.DrugId, sum_count, item.OrgId)
  3674. var cancel_count int64
  3675. if item.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3676. cancel_count = item.Count * medical.MinNumber
  3677. }
  3678. if item.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3679. cancel_count = item.Count
  3680. }
  3681. if item.MaxUnit == medical.MinUnit && medical.MaxUnit == medical.MinUnit {
  3682. cancel_count = item.Count * medical.MinNumber
  3683. }
  3684. cancelInfo, _ := service.GetDrugAllStockInfo(item.StorehouseId, item.OrgId, item.DrugId)
  3685. var over_count int64
  3686. for _, its := range cancelInfo {
  3687. if its.MaxUnit == medical.MaxUnit {
  3688. its.StockMaxNumber = its.StockMaxNumber * medical.MinNumber
  3689. }
  3690. over_count += its.StockMaxNumber + its.StockMinNumber
  3691. }
  3692. //退库数量减少,更新剩余库存
  3693. service.ReduceCancelSumCount(item.StorehouseId, item.DrugId, item.OrgId, cancel_count, over_count)
  3694. }
  3695. if err == nil {
  3696. c.ServeSuccessJSON(map[string]interface{}{
  3697. "msg": "反审核成功!",
  3698. })
  3699. return
  3700. }
  3701. }