drug_stock_api_contorller.go 92KB

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