drug_stock_api_contorller.go 143KB

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