drug_stock_api_contorller.go 98KB

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