drug_stock_api_contorller.go 122KB

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