drug_stock_api_contorller.go 125KB

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