drug_stock_api_contorller.go 144KB

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