drug_stock_api_contorller.go 98KB

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