drug_stock_api_contorller.go 95KB

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