drug_stock_api_contorller.go 95KB

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