drug_stock_api_contorller.go 107KB

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