drug_stock_api_contorller.go 101KB

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