drug_stock_api_contorller.go 96KB

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