drug_stock_api_contorller.go 144KB

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