drug_stock_api_contorller.go 101KB

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