drug_stock_api_contorller.go 142KB

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