drug_stock_api_contorller.go 143KB

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