drug_stock_api_contorller.go 98KB

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