drug_stock_api_contorller.go 143KB

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