drug_stock_api_contorller.go 95KB

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