drug_stock_api_contorller.go 105KB

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