drug_stock_api_contorller.go 78KB

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