drug_stock_api_contorller.go 107KB

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