Stock.go 125 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987
  1. package controllers
  2. import (
  3. "ERP_storage/Nats"
  4. "ERP_storage/Nats/NatsServer"
  5. "ERP_storage/conf"
  6. "ERP_storage/dto"
  7. "ERP_storage/logs"
  8. "ERP_storage/models/Account"
  9. "ERP_storage/models/Basic"
  10. "ERP_storage/models/Contract"
  11. "ERP_storage/models/Property"
  12. "ERP_storage/models/Stock"
  13. "errors"
  14. "fmt"
  15. "math"
  16. "os"
  17. "strconv"
  18. "strings"
  19. "time"
  20. "github.com/beego/beego/v2/adapter/orm"
  21. beego "github.com/beego/beego/v2/server/web"
  22. "github.com/robfig/cron/v3"
  23. "github.com/xuri/excelize/v2"
  24. natslibs "gogs.baozhida.cn/zoie/ERP_libs/Nats"
  25. userlibs "gogs.baozhida.cn/zoie/ERP_libs/User"
  26. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  27. )
  28. type StockController struct {
  29. beego.Controller
  30. User userlibs.User
  31. }
  32. func (c *StockController) Prepare() {
  33. c.User = *Account.User_r
  34. }
  35. func (c *StockController) Device_List() {
  36. // 分页参数 初始化
  37. page, _ := c.GetInt("page")
  38. if page < 1 {
  39. page = 1
  40. }
  41. page_z, _ := c.GetInt("page_z")
  42. if page_z < 1 {
  43. page_z = conf.Page_size
  44. }
  45. T_state, _ := c.GetInt("T_state")
  46. T_name := c.GetString("T_name")
  47. T_product_name := c.GetString("T_product_name") // 产品名称
  48. T_product_model := c.GetString("T_product_model") // 产品型号
  49. DeviceDao := Stock.NewDevice(orm.NewOrm())
  50. R_List, R_cnt := DeviceDao.Read_Device_List(T_name, T_product_name, T_product_model, T_state, page, page_z)
  51. var r_jsons lib.R_JSONS
  52. r_jsons.Num = R_cnt
  53. r_jsons.Data = R_List
  54. r_jsons.Page = page
  55. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  56. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  57. c.ServeJSON()
  58. return
  59. }
  60. func (c *StockController) Device_Check() {
  61. T_sn := c.GetString("T_sn")
  62. T_type, _ := c.GetInt("T_type") //1-出库 2-入库
  63. T_product_id, _ := c.GetInt("T_product_id")
  64. DeviceDao := Stock.NewDevice(orm.NewOrm())
  65. device, err := DeviceDao.Read_Device_ByT_sn(T_sn)
  66. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  67. c.Data["json"] = lib.JSONS{Code: 202, Msg: "请稍后重试!"}
  68. c.ServeJSON()
  69. return
  70. }
  71. // T_State 1-已出库 2-未出库/入库
  72. if len(device.T_in_number) > 0 && device.T_State == 2 && T_type == 2 {
  73. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已入库,请勿重复提交!", T_sn)}
  74. c.ServeJSON()
  75. return
  76. }
  77. if T_type == 2 {
  78. mqtt := Stock.Read_MqttUser(T_sn)
  79. if len(mqtt.Username) == 0 {
  80. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】未授权,请先授权!", T_sn)}
  81. c.ServeJSON()
  82. return
  83. }
  84. }
  85. if len(device.T_out_number) > 0 && device.T_State == 1 && T_type == 1 {
  86. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已出库,请勿重复提交!", T_sn)}
  87. c.ServeJSON()
  88. return
  89. }
  90. if T_type == 1 {
  91. if device.Id == 0 {
  92. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】未入库,请先入库!", T_sn)}
  93. c.ServeJSON()
  94. return
  95. }
  96. if device.T_product_id != T_product_id {
  97. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库产品与出库产品不一致,请检查!"}
  98. c.ServeJSON()
  99. return
  100. }
  101. }
  102. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  103. c.ServeJSON()
  104. return
  105. }
  106. func (c *StockController) Stock_List() {
  107. // 分页参数 初始化
  108. page, _ := c.GetInt("page")
  109. if page < 1 {
  110. page = 1
  111. }
  112. page_z, _ := c.GetInt("page_z")
  113. if page_z < 1 {
  114. page_z = conf.Page_size
  115. }
  116. // 查询
  117. T_depot_id, _ := c.GetInt("T_depot_id")
  118. // 查询
  119. T_product_name := c.GetString("T_product_name")
  120. T_product_model := c.GetString("T_product_model")
  121. T_product_class, _ := c.GetInt("T_product_class")
  122. userList, _ := NatsServer.Read_User_List_All()
  123. Account.Read_User_All_Map(userList)
  124. Basic.Read_Depot_All_Map()
  125. StockDao := Stock.NewStock(orm.NewOrm())
  126. R_List, R_cnt := StockDao.Read_Stock_List(T_depot_id, T_product_class, T_product_name, T_product_model, page, page_z)
  127. var r_jsons lib.R_JSONS
  128. r_jsons.Num = R_cnt
  129. r_jsons.Data = R_List
  130. r_jsons.Page = page
  131. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  132. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  133. c.ServeJSON()
  134. return
  135. }
  136. // 修改库存排序
  137. func (c *StockController) Stock_Edit_Sort() {
  138. T_id, _ := c.GetInt("T_id")
  139. T_sort, _ := c.GetInt("T_sort")
  140. StockDao := Stock.NewStock(orm.NewOrm())
  141. Stock_r, err := StockDao.Read_Stock_ById(T_id)
  142. if err != nil {
  143. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  144. c.ServeJSON()
  145. return
  146. }
  147. Stock_r.T_sort = T_sort
  148. if err = StockDao.Update_Stock(Stock_r, "T_sort"); err != nil {
  149. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  150. c.ServeJSON()
  151. return
  152. }
  153. NatsServer.AddUserLogs(c.User.T_uuid, "库存", "修改", Stock_r)
  154. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  155. c.ServeJSON()
  156. return
  157. }
  158. func (c *StockController) Stock_Detail_List() {
  159. // 查询
  160. T_depot_id, _ := c.GetInt("T_depot_id")
  161. // 查询
  162. T_product_id, _ := c.GetInt("T_product_id")
  163. T_start_date := c.GetString("T_start_date")
  164. T_end_date := c.GetString("T_end_date")
  165. now := time.Now()
  166. if len(T_start_date) == 0 {
  167. T_start_date = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local).Format("2006-01")
  168. } else {
  169. T_start_date_t, _ := lib.DateStrToTime(T_start_date)
  170. T_start_date = T_start_date_t.Format("2006-01")
  171. }
  172. if len(T_end_date) == 0 {
  173. T_end_date = now.Format("2006-01")
  174. } else {
  175. T_end_date_t, _ := lib.DateStrToTime(T_end_date)
  176. T_end_date = T_end_date_t.Format("2006-01")
  177. }
  178. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  179. StockMonthDao := Stock.NewStockMonth(orm.NewOrm())
  180. R_List := StockMonthDao.Read_StockMonth_List(T_depot_id, T_product_id, 0, T_start_date, T_end_date)
  181. for i := 0; i < len(R_List); i++ {
  182. R_List[i].T_project = strings.Join(StockOutDao.Read_StockOut_T_project(T_depot_id, R_List[i].T_product_id, R_List[i].T_month), "|")
  183. }
  184. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: R_List}
  185. c.ServeJSON()
  186. return
  187. }
  188. func (c *StockController) Stock_Detail_Excel() {
  189. // 查询
  190. T_depot_id, _ := c.GetInt("T_depot_id")
  191. T_product_id, _ := c.GetInt("T_product_id")
  192. // 查询
  193. T_start_date := c.GetString("T_start_date")
  194. T_end_date := c.GetString("T_end_date")
  195. now := time.Now()
  196. if len(T_start_date) == 0 {
  197. T_start_date = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local).Format("2006-01")
  198. } else {
  199. T_start_date_t, _ := lib.DateStrToTime(T_start_date)
  200. T_start_date = T_start_date_t.Format("2006-01")
  201. }
  202. if len(T_end_date) == 0 {
  203. T_end_date = now.Format("2006-01")
  204. } else {
  205. T_end_date_t, _ := lib.DateStrToTime(T_end_date)
  206. T_end_date = T_end_date_t.Format("2006-01")
  207. }
  208. var class_List []Basic.ProductClass_R
  209. filename := fmt.Sprintf("进销存(%s)", lib.GetRandstring(6, "0123456789", 0))
  210. if T_product_id > 0 {
  211. product, _ := Basic.Read_Product_ById(T_product_id)
  212. filename = fmt.Sprintf("进销存-%s明细(%s)", product.T_name, lib.GetRandstring(6, "0123456789", 0))
  213. class, _ := Basic.Read_ProductClass_ById(product.T_class)
  214. class_List = append(class_List, Basic.ProductClassToProductClass_R(class))
  215. } else {
  216. class_List, _ = Basic.Read_ProductClass_List("", 0, 0, 9999)
  217. }
  218. f := excelize.NewFile() // 设置单元格的值
  219. Style1, _ := f.NewStyle(
  220. &excelize.Style{
  221. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  222. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  223. })
  224. Style2, _ := f.NewStyle(
  225. &excelize.Style{
  226. Font: &excelize.Font{Bold: true, Size: 10, Family: "宋体"},
  227. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  228. Border: []excelize.Border{
  229. {Type: "left", Color: "000000", Style: 1},
  230. {Type: "top", Color: "000000", Style: 1},
  231. {Type: "bottom", Color: "000000", Style: 1},
  232. {Type: "right", Color: "000000", Style: 1},
  233. },
  234. })
  235. Style3, _ := f.NewStyle(
  236. &excelize.Style{
  237. Font: &excelize.Font{Bold: true, Size: 10, Family: "宋体", Color: "FF0000"},
  238. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  239. Border: []excelize.Border{
  240. {Type: "left", Color: "000000", Style: 1},
  241. {Type: "top", Color: "000000", Style: 1},
  242. {Type: "bottom", Color: "000000", Style: 1},
  243. {Type: "right", Color: "000000", Style: 1},
  244. },
  245. })
  246. Style4, _ := f.NewStyle(
  247. &excelize.Style{
  248. Font: &excelize.Font{Size: 10, Family: "宋体"},
  249. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  250. Border: []excelize.Border{
  251. {Type: "left", Color: "000000", Style: 1},
  252. {Type: "top", Color: "000000", Style: 1},
  253. {Type: "bottom", Color: "000000", Style: 1},
  254. {Type: "right", Color: "000000", Style: 1},
  255. },
  256. })
  257. StockMonthDao := Stock.NewStockMonth(orm.NewOrm())
  258. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  259. var j = 0
  260. for _, r := range class_List {
  261. StockList := StockMonthDao.Read_StockMonth_List(T_depot_id, T_product_id, r.Id, T_start_date, T_end_date)
  262. if len(StockList) == 0 {
  263. continue
  264. }
  265. if j == 0 {
  266. f.SetSheetName("Sheet1", r.T_name)
  267. } else {
  268. f.NewSheet(r.T_name)
  269. }
  270. j += 1
  271. f.MergeCell(r.T_name, "A1", "J1")
  272. f.SetRowStyle(r.T_name, 1, 1, Style1)
  273. f.SetCellValue(r.T_name, "A1", fmt.Sprintf("宝智达科技产品进销存统计表"))
  274. f.SetRowHeight(r.T_name, 1, 30)
  275. // 这里设置表头
  276. f.SetCellStyle(r.T_name, "A2", "J2", Style2)
  277. f.SetRowHeight(r.T_name, 2, 20)
  278. f.SetCellValue(r.T_name, "A2", "序号")
  279. f.SetCellValue(r.T_name, "B2", "产品名称")
  280. f.SetCellValue(r.T_name, "C2", "型号")
  281. f.SetCellValue(r.T_name, "D2", "规格")
  282. f.SetCellValue(r.T_name, "E2", "月份")
  283. f.SetCellValue(r.T_name, "F2", "期初库存")
  284. f.SetCellValue(r.T_name, "G2", "当月入库")
  285. f.SetCellValue(r.T_name, "H2", "当月出库")
  286. f.SetCellValue(r.T_name, "I2", "期末库存")
  287. f.SetCellValue(r.T_name, "J2", "出库项目")
  288. //f.SetCellValue(r.T_name, "K2", "备注")
  289. // 设置列宽
  290. f.SetColWidth(r.T_name, "A", "A", 10)
  291. f.SetColWidth(r.T_name, "B", "B", 10)
  292. f.SetColWidth(r.T_name, "C", "D", 10)
  293. f.SetColWidth(r.T_name, "D", "D", 10)
  294. f.SetColWidth(r.T_name, "E", "E", 10)
  295. f.SetColWidth(r.T_name, "F", "F", 10)
  296. f.SetColWidth(r.T_name, "G", "G", 10)
  297. f.SetColWidth(r.T_name, "H", "H", 10)
  298. f.SetColWidth(r.T_name, "I", "I", 10)
  299. f.SetColWidth(r.T_name, "J", "J", 70)
  300. //f.SetColWidth(r.T_name, "K", "K", 10)
  301. line := 2
  302. // 循环写入数据
  303. for _, v := range StockList {
  304. line++
  305. product, _ := Basic.Read_Product_ById(v.T_product_id)
  306. f.SetCellValue(r.T_name, fmt.Sprintf("A%d", line), line-2)
  307. f.SetCellValue(r.T_name, fmt.Sprintf("B%d", line), product.T_name)
  308. f.SetCellValue(r.T_name, fmt.Sprintf("C%d", line), product.T_model)
  309. f.SetCellValue(r.T_name, fmt.Sprintf("D%d", line), product.T_spec)
  310. f.SetCellValue(r.T_name, fmt.Sprintf("E%d", line), v.T_month)
  311. f.SetCellValue(r.T_name, fmt.Sprintf("F%d", line), v.T_beginning)
  312. f.SetCellValue(r.T_name, fmt.Sprintf("G%d", line), v.T_in)
  313. f.SetCellValue(r.T_name, fmt.Sprintf("H%d", line), v.T_out)
  314. f.SetCellValue(r.T_name, fmt.Sprintf("I%d", line), v.T_ending)
  315. if product.T_early_warning > v.T_ending {
  316. f.SetCellStyle(r.T_name, fmt.Sprintf("I%d", line), fmt.Sprintf("I%d", line), Style3)
  317. } else {
  318. f.SetCellStyle(r.T_name, fmt.Sprintf("I%d", line), fmt.Sprintf("I%d", line), Style4)
  319. }
  320. T_project := StockOutDao.Read_StockOut_T_project(T_depot_id, product.Id, v.T_month)
  321. //f.SetCellValue(r.T_name, fmt.Sprintf("J%d", line), v.T_project)
  322. f.SetCellValue(r.T_name, fmt.Sprintf("J%d", line), strings.Join(T_project, "\r\n"))
  323. }
  324. f.SetCellStyle(r.T_name, "A2", fmt.Sprintf("H%d", line), Style4)
  325. Style5, _ := f.NewStyle(
  326. &excelize.Style{
  327. Font: &excelize.Font{Size: 10, Family: "宋体"},
  328. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  329. Border: []excelize.Border{
  330. {Type: "left", Color: "000000", Style: 1},
  331. {Type: "top", Color: "000000", Style: 1},
  332. {Type: "bottom", Color: "000000", Style: 1},
  333. {Type: "right", Color: "000000", Style: 1},
  334. },
  335. })
  336. f.SetCellStyle(r.T_name, "J2", fmt.Sprintf("J%d", line), Style5)
  337. }
  338. // 保存文件
  339. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  340. fmt.Println(err)
  341. }
  342. var url string
  343. //// 上传 OSS
  344. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  345. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  346. if !is {
  347. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  348. c.ServeJSON()
  349. return
  350. }
  351. //删除目录
  352. err := os.Remove("ofile/" + filename + ".xlsx")
  353. if err != nil {
  354. logs.Error(lib.FuncName(), err)
  355. }
  356. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  357. c.ServeJSON()
  358. return
  359. }
  360. func (c *StockController) StockIn_List() {
  361. // 分页参数 初始化
  362. page, _ := c.GetInt("page")
  363. if page < 1 {
  364. page = 1
  365. }
  366. page_z, _ := c.GetInt("page_z")
  367. if page_z < 1 {
  368. page_z = conf.Page_size
  369. }
  370. // 查询
  371. T_depot_id, _ := c.GetInt("T_depot_id")
  372. T_start_date := c.GetString("T_start_date")
  373. T_end_date := c.GetString("T_end_date")
  374. T_name := c.GetString("T_name")
  375. T_batch_number := c.GetString("T_batch_number")
  376. userList, _ := NatsServer.Read_User_List_All()
  377. Account.Read_User_All_Map(userList)
  378. Basic.Read_Depot_All_Map()
  379. StockInDao := Stock.NewStockIn(orm.NewOrm())
  380. R_List, R_cnt := StockInDao.Read_StockIn_List(T_batch_number, T_name, T_depot_id, T_start_date, T_end_date, page, page_z)
  381. var r_jsons lib.R_JSONS
  382. r_jsons.Num = R_cnt
  383. r_jsons.Data = R_List
  384. r_jsons.Page = page
  385. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  386. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  387. c.ServeJSON()
  388. return
  389. }
  390. func (c *StockController) StockIn_List_Product() {
  391. // 分页参数 初始化
  392. page, _ := c.GetInt("page")
  393. if page < 1 {
  394. page = 1
  395. }
  396. page_z, _ := c.GetInt("page_z")
  397. if page_z < 1 {
  398. page_z = conf.Page_size
  399. }
  400. // 查询
  401. T_depot_id, _ := c.GetInt("T_depot_id")
  402. T_name := c.GetString("T_name")
  403. T_start_date := c.GetString("T_start_date")
  404. T_end_date := c.GetString("T_end_date")
  405. T_product_name := c.GetString("T_product_name") // 产品名称
  406. T_product_model := c.GetString("T_product_model") // 产品型号
  407. userList, _ := NatsServer.Read_User_List_All()
  408. Account.Read_User_All_Map(userList)
  409. Basic.Read_Depot_All_Map()
  410. StockInDao := Stock.NewStockIn(orm.NewOrm())
  411. R_List, R_cnt := StockInDao.Read_StockInProduct_List(T_name, T_start_date, T_end_date, T_depot_id, T_product_name, T_product_model, page, page_z)
  412. var r_jsons lib.R_JSONS
  413. r_jsons.Num = R_cnt
  414. r_jsons.Data = R_List
  415. r_jsons.Page = page
  416. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  417. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  418. c.ServeJSON()
  419. return
  420. }
  421. func (c *StockController) StockIn_List_Product_Excel() {
  422. // 查询
  423. T_depot_id, _ := c.GetInt("T_depot_id")
  424. T_name := c.GetString("T_name")
  425. T_start_date := c.GetString("T_start_date")
  426. T_end_date := c.GetString("T_end_date")
  427. T_product_name := c.GetString("T_product_name") // 产品名称
  428. T_product_model := c.GetString("T_product_model") // 产品型号
  429. userList, _ := NatsServer.Read_User_List_All()
  430. Account.Read_User_All_Map(userList)
  431. Basic.Read_Depot_All_Map()
  432. StockInDao := Stock.NewStockIn(orm.NewOrm())
  433. R_List, _ := StockInDao.Read_StockInProduct_List(T_name, T_start_date, T_end_date, T_depot_id, T_product_name, T_product_model, 0, 9999)
  434. filename := fmt.Sprintf("入库明细单(%s)", lib.GetRandstring(6, "0123456789", 0))
  435. f := excelize.NewFile() // 设置单元格的值
  436. Style1, _ := f.NewStyle(
  437. &excelize.Style{
  438. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  439. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  440. })
  441. Style2, _ := f.NewStyle(
  442. &excelize.Style{
  443. Font: &excelize.Font{Bold: true, Size: 11, Family: "宋体"},
  444. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  445. Border: []excelize.Border{
  446. {Type: "left", Color: "000000", Style: 1},
  447. {Type: "top", Color: "000000", Style: 1},
  448. {Type: "bottom", Color: "000000", Style: 1},
  449. {Type: "right", Color: "000000", Style: 1},
  450. },
  451. })
  452. Style3, _ := f.NewStyle(
  453. &excelize.Style{
  454. Font: &excelize.Font{Size: 11, Family: "宋体"},
  455. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  456. Border: []excelize.Border{
  457. {Type: "left", Color: "000000", Style: 1},
  458. {Type: "top", Color: "000000", Style: 1},
  459. {Type: "bottom", Color: "000000", Style: 1},
  460. {Type: "right", Color: "000000", Style: 1},
  461. },
  462. })
  463. f.MergeCell("Sheet1", "A1", "L1")
  464. f.SetRowStyle("Sheet1", 1, 1, Style1)
  465. f.SetCellValue("Sheet1", "A1", fmt.Sprintf("入库明细单"))
  466. f.SetRowHeight("Sheet1", 1, 30)
  467. // 这里设置表头
  468. f.SetCellStyle("Sheet1", "A2", "L2", Style2)
  469. f.SetRowHeight("Sheet1", 2, 20)
  470. f.SetCellValue("Sheet1", "A2", "序号")
  471. f.SetCellValue("Sheet1", "B2", "入库单号")
  472. f.SetCellValue("Sheet1", "C2", "经办人")
  473. f.SetCellValue("Sheet1", "D2", "入库日期")
  474. f.SetCellValue("Sheet1", "E2", "入库仓库")
  475. f.SetCellValue("Sheet1", "F2", "入库类型")
  476. f.SetCellValue("Sheet1", "G2", "产品名称")
  477. f.SetCellValue("Sheet1", "H2", "产品型号")
  478. f.SetCellValue("Sheet1", "I2", "数量")
  479. f.SetCellValue("Sheet1", "J2", "是否关联SN")
  480. f.SetCellValue("Sheet1", "K2", "备注")
  481. f.SetCellValue("Sheet1", "L2", "SN")
  482. // 设置列宽
  483. f.SetColWidth("Sheet1", "A", "A", 10)
  484. f.SetColWidth("Sheet1", "B", "B", 15)
  485. f.SetColWidth("Sheet1", "C", "D", 10)
  486. f.SetColWidth("Sheet1", "D", "D", 10)
  487. f.SetColWidth("Sheet1", "E", "E", 10)
  488. f.SetColWidth("Sheet1", "F", "F", 10)
  489. f.SetColWidth("Sheet1", "G", "G", 20)
  490. f.SetColWidth("Sheet1", "H", "H", 15)
  491. f.SetColWidth("Sheet1", "I", "I", 10)
  492. f.SetColWidth("Sheet1", "J", "J", 12)
  493. f.SetColWidth("Sheet1", "K", "K", 30)
  494. f.SetColWidth("Sheet1", "L", "L", 100)
  495. line := 2
  496. for _, product := range R_List {
  497. line += 1
  498. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-2)
  499. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), product.T_number)
  500. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), product.T_submit_name)
  501. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), product.T_date)
  502. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), product.T_depot_name)
  503. var type_str string
  504. if product.T_type == 1 {
  505. type_str = "入库"
  506. } else {
  507. type_str = "退库"
  508. }
  509. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), type_str)
  510. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), product.T_product_name)
  511. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), product.T_product_model)
  512. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), product.T_num)
  513. var relation_sn string
  514. if product.T_product_relation_sn == 1 {
  515. relation_sn = "是"
  516. } else {
  517. relation_sn = "否"
  518. }
  519. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), relation_sn)
  520. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), product.T_remark)
  521. f.SetCellValue("Sheet1", fmt.Sprintf("L%d", line), product.T_relation_sn)
  522. }
  523. f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("L%d", line), Style3)
  524. // 保存文件
  525. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  526. fmt.Println(err)
  527. }
  528. var url string
  529. //// 上传 OSS
  530. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  531. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  532. if !is {
  533. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  534. c.ServeJSON()
  535. return
  536. }
  537. //删除目录
  538. err := os.Remove("ofile/" + filename + ".xlsx")
  539. if err != nil {
  540. logs.Error(lib.FuncName(), err)
  541. }
  542. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  543. c.ServeJSON()
  544. return
  545. }
  546. func (c *StockController) StockIn_Get() {
  547. // 查询
  548. T_number := c.GetString("T_number")
  549. o := orm.NewOrm()
  550. StockInDao := Stock.NewStockIn(o)
  551. StockInProductDao := Stock.NewStockInProduct(o)
  552. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  553. if err != nil {
  554. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  555. c.ServeJSON()
  556. return
  557. }
  558. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  559. var pList []Stock.StockInProduct_R
  560. for _, v := range productList {
  561. pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
  562. }
  563. userList, _ := NatsServer.Read_User_List_All()
  564. Account.Read_User_All_Map(userList)
  565. Basic.Read_Depot_All_Map()
  566. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Stock.StockInToStockIn_Detail(stockIn, pList)}
  567. c.ServeJSON()
  568. return
  569. }
  570. func (c *StockController) StockIn_generate_number() {
  571. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  572. rand_x := 0
  573. T_number := ""
  574. for true {
  575. T_number = "RK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
  576. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  577. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  578. break
  579. }
  580. rand_x += 1
  581. }
  582. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  583. c.ServeJSON()
  584. return
  585. }
  586. func (c *StockController) StockIn_Add() {
  587. o := orm.NewOrm()
  588. o.Begin()
  589. StockInDao := Stock.NewStockIn(o)
  590. T_number := c.GetString("T_number")
  591. // 检查前端传入的T_number是否已提交过
  592. _, err := StockInDao.Read_StockIn_ByT_number(T_number)
  593. if err == nil {
  594. // 入库编号已存在,不需要做任何处理
  595. o.Rollback()
  596. c.Data["json"] = lib.JSONS{Code: 200, Msg: "入库编号已存在,无需重复提交!", Data: T_number}
  597. c.ServeJSON()
  598. return
  599. }
  600. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  601. // 数据库查询错误
  602. o.Rollback()
  603. c.Data["json"] = lib.JSONS{Code: 203, Msg: "查询入库编号失败"}
  604. c.ServeJSON()
  605. return
  606. }
  607. T_depot_id, _ := c.GetInt("T_depot_id")
  608. T_type, _ := c.GetInt("T_type") // 1-入库 2-退库
  609. T_date := c.GetString("T_date")
  610. T_product := c.GetString("T_product")
  611. T_remark := c.GetString("T_remark")
  612. T_project := c.GetString("T_project")
  613. T_return_user := c.GetString("T_return_user")
  614. T_batch_number := c.GetString("T_batch_number")
  615. date, is := lib.DateStrToTime(T_date)
  616. if !is {
  617. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  618. c.ServeJSON()
  619. return
  620. }
  621. if T_type == 2 && len(T_project) == 0 {
  622. c.Data["json"] = lib.JSONS{Code: 202, Msg: "退库时,项目不能为空!"}
  623. c.ServeJSON()
  624. return
  625. }
  626. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "入库", T_product)
  627. var_ := Stock.StockIn{
  628. T_number: T_number,
  629. T_depot_id: T_depot_id,
  630. T_type: T_type,
  631. T_date: T_date,
  632. T_remark: T_remark,
  633. T_submit: c.User.T_uuid,
  634. T_project: T_project,
  635. T_return_user: T_return_user,
  636. T_batch_number: T_batch_number,
  637. }
  638. StockInProductDao := Stock.NewStockInProduct(o)
  639. StockDao := Stock.NewStock(o)
  640. DeviceDao := Stock.NewDevice(o)
  641. IotCardDao := Property.NewIotCard(&o)
  642. _, err = StockInDao.Add_StockIn(var_)
  643. if err != nil {
  644. o.Rollback()
  645. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  646. c.ServeJSON()
  647. return
  648. }
  649. if len(T_product) == 0 {
  650. o.Rollback()
  651. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  652. c.ServeJSON()
  653. return
  654. }
  655. productList := lib.SplitString(T_product, "|")
  656. allProductList := []int{}
  657. for _, v := range productList {
  658. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  659. allProductList = append(allProductList, product_id)
  660. product, _ := Basic.Read_Product_ById(product_id)
  661. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  662. T_relation_sn := strings.Split(v, "-")[2]
  663. if T_relation_sn == "" && product.T_relation_sn == 1 {
  664. o.Rollback()
  665. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  666. c.ServeJSON()
  667. return
  668. }
  669. if len(T_relation_sn) > 0 {
  670. snList := strings.Split(strings.Trim(T_relation_sn, ","), ",")
  671. num = len(snList)
  672. for _, sn := range snList {
  673. mqtt := Stock.Read_MqttUser(sn)
  674. // 添加设备
  675. device := Stock.Device{
  676. T_contract_number: "",
  677. T_out_number: "",
  678. T_product_id: product_id,
  679. T_in_number: T_number,
  680. T_sn: sn,
  681. T_iccid: mqtt.Iccid,
  682. T_imei: mqtt.Imei,
  683. T_State: 2,
  684. CreateTime: date,
  685. }
  686. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  687. if err != nil {
  688. o.Rollback()
  689. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  690. c.ServeJSON()
  691. return
  692. }
  693. // 添加物联网卡
  694. iotCard := Property.IotCard{
  695. T_iccid: mqtt.Iccid,
  696. T_sn: sn,
  697. T_State: 2, //1-未使用 2-已使用 3-已作废
  698. T_type: "4G",
  699. }
  700. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  701. if err != nil {
  702. o.Rollback()
  703. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  704. c.ServeJSON()
  705. return
  706. }
  707. }
  708. }
  709. stockInProduct := Stock.StockInProduct{
  710. T_number: T_number,
  711. T_product_id: product_id,
  712. T_depot_id: T_depot_id,
  713. T_num: num, // 入库数量
  714. T_date: T_date, // 入库日期
  715. T_relation_sn: T_relation_sn,
  716. T_batch_number: T_batch_number,
  717. }
  718. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  719. if err != nil {
  720. o.Rollback()
  721. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  722. c.ServeJSON()
  723. return
  724. }
  725. _, err = StockDao.AddOrUpdate_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
  726. if err != nil {
  727. o.Rollback()
  728. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  729. c.ServeJSON()
  730. return
  731. }
  732. }
  733. o.Commit()
  734. StockIn_Edit_StockMonth(T_date, T_depot_id, allProductList)
  735. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "入库", var_)
  736. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  737. c.ServeJSON()
  738. return
  739. }
  740. func StockIn_Edit_StockMonth(T_date string, T_depot_id int, allProductList []int) error {
  741. if len(T_date) == 0 {
  742. return errors.New("日期不能为空")
  743. }
  744. date, _ := lib.DateStrToTime(T_date)
  745. //if date.Year() == time.Now().Year() && date.Month() == time.Now().Month() {
  746. // // 修改本月数据不同步库存
  747. // return nil
  748. //}
  749. //T_month := date.Format("2006-01")
  750. months := generateMonthList(date, "in")
  751. o := orm.NewOrm()
  752. StockOutDao := Stock.NewStockOut(o)
  753. StockMonthDao := Stock.NewStockMonth(o)
  754. StockOutProductDao := Stock.NewStockOutProduct(o)
  755. StockInProductDao := Stock.NewStockInProduct(o)
  756. for _, T_product_id := range allProductList {
  757. for _, T_month := range months {
  758. // 获取当前产品本月出入库数量
  759. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  760. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  761. if stockMonth.Id > 0 {
  762. // 修改
  763. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  764. if len(lastMonth) > 0 {
  765. stockMonth.T_beginning = lastMonth[0].T_ending
  766. } else {
  767. stockMonth.T_beginning = 0
  768. }
  769. // 期末库存 = 期初库存+入库-出库
  770. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  771. err := StockMonthDao.Update_StockMonth(stockMonth, "T_in", "T_beginning", "T_ending")
  772. if err != nil {
  773. return err
  774. }
  775. } else {
  776. product, _ := Basic.Read_Product_ById(T_product_id)
  777. // 添加库存记录
  778. stockMonth = Stock.StockMonth{
  779. T_depot_id: T_depot_id,
  780. T_product_id: T_product_id,
  781. T_product_class: product.T_class,
  782. T_month: T_month,
  783. }
  784. // 获取当前产品本月出入库数量
  785. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  786. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  787. if len(lastMonth) > 0 {
  788. stockMonth.T_beginning = lastMonth[0].T_ending
  789. } else {
  790. stockMonth.T_beginning = 0
  791. }
  792. // 期末库存 = 期初库存+入库-出库
  793. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  794. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(T_depot_id, T_product_id, T_month)
  795. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  796. if err != nil {
  797. return err
  798. }
  799. }
  800. }
  801. }
  802. return nil
  803. }
  804. func (c *StockController) StockIn_Edit() {
  805. T_number := c.GetString("T_number") // 入库单号
  806. //T_depot_id, _ := c.GetInt("T_depot_id")
  807. T_date := c.GetString("T_date")
  808. T_product := c.GetString("T_product")
  809. T_remark := c.GetString("T_remark")
  810. T_project := c.GetString("T_project")
  811. T_return_user := c.GetString("T_return_user")
  812. T_batch_number := c.GetString("T_batch_number")
  813. date, is := lib.DateStrToTime(T_date)
  814. if !is {
  815. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  816. c.ServeJSON()
  817. return
  818. }
  819. if date.Year() != time.Now().Year() && date.Month() != time.Now().Month() {
  820. T_date = ""
  821. }
  822. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改入库", T_product)
  823. o := orm.NewOrm()
  824. o.Begin()
  825. StockInDao := Stock.NewStockIn(o)
  826. StockInProductDao := Stock.NewStockInProduct(o)
  827. StockDao := Stock.NewStock(o)
  828. DeviceDao := Stock.NewDevice(o)
  829. IotCardDao := Property.NewIotCard(&o)
  830. // 查询入库信息
  831. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  832. if err != nil {
  833. o.Rollback()
  834. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  835. c.ServeJSON()
  836. return
  837. }
  838. T_old_date := stockIn.T_date
  839. T_old_batch_number := stockIn.T_batch_number
  840. // 查询入库产品信息
  841. productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  842. var oldProductList []dto.StockProduct
  843. var newProductList []dto.StockProduct
  844. allProductListMap := make(map[int]struct{})
  845. productOldMap := map[int]dto.StockProduct{}
  846. for _, product := range productOldList {
  847. stockProduct := dto.StockProduct{
  848. T_product_id: product.T_product_id,
  849. T_num: product.T_num,
  850. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  851. }
  852. oldProductList = append(oldProductList, stockProduct)
  853. productOldMap[product.T_product_id] = stockProduct
  854. if _, ok := allProductListMap[product.T_product_id]; !ok {
  855. allProductListMap[product.T_product_id] = struct{}{}
  856. }
  857. }
  858. productNewList := lib.SplitString(T_product, "|")
  859. productNewMap := map[int]dto.StockProduct{}
  860. for _, v := range productNewList {
  861. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  862. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  863. T_relation_sn := strings.Split(v, "-")[2]
  864. snList := lib.SplitString(T_relation_sn, ",")
  865. if len(snList) > 0 {
  866. num = len(snList)
  867. }
  868. stockProduct := dto.StockProduct{
  869. T_product_id: product_id,
  870. T_num: num,
  871. T_relation_sn: snList,
  872. }
  873. newProductList = append(newProductList, stockProduct)
  874. productNewMap[product_id] = stockProduct
  875. if _, ok := allProductListMap[product_id]; !ok {
  876. allProductListMap[product_id] = struct{}{}
  877. }
  878. }
  879. // 判断产品列表信息是否相同
  880. StockProductListIsEqual, needDelete, needAdd, needEdit, snDiff := dto.StockProductListsEqual(oldProductList, newProductList)
  881. // 两次提交的数据相同,则不用修改
  882. if !StockProductListIsEqual {
  883. if len(needDelete) > 0 {
  884. // 删除入库产品列表
  885. for _, v := range needDelete {
  886. product, _ := Basic.Read_Product_ById(v.T_product_id)
  887. // 删除入库产品列表
  888. err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
  889. if err != nil {
  890. o.Rollback()
  891. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  892. c.ServeJSON()
  893. return
  894. }
  895. if product.T_relation_sn == 1 {
  896. // 删除该入库单关联sn
  897. for _, sn := range v.T_relation_sn {
  898. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  899. if err != nil {
  900. o.Rollback()
  901. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  902. c.ServeJSON()
  903. return
  904. }
  905. }
  906. }
  907. // 减少库存
  908. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  909. if err != nil {
  910. o.Rollback()
  911. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  912. c.ServeJSON()
  913. return
  914. }
  915. }
  916. }
  917. if len(needAdd) > 0 {
  918. // 新增入库产品列表
  919. for _, v := range needAdd {
  920. product, _ := Basic.Read_Product_ById(v.T_product_id)
  921. if product.T_relation_sn == 1 && len(v.T_relation_sn) == 0 {
  922. o.Rollback()
  923. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  924. c.ServeJSON()
  925. return
  926. }
  927. // 添加该入库单关联sn
  928. for _, sn := range v.T_relation_sn {
  929. mqtt := Stock.Read_MqttUser(sn)
  930. // 添加设备
  931. device := Stock.Device{
  932. T_contract_number: "",
  933. T_out_number: "",
  934. T_product_id: v.T_product_id,
  935. T_in_number: stockIn.T_number,
  936. T_sn: sn,
  937. T_iccid: mqtt.Iccid,
  938. T_imei: mqtt.Imei,
  939. T_State: 2,
  940. CreateTime: date,
  941. UpdateTime: stockIn.UpdateTime,
  942. }
  943. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  944. if err != nil {
  945. o.Rollback()
  946. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  947. c.ServeJSON()
  948. return
  949. }
  950. // 添加物联网卡
  951. iotCard := Property.IotCard{
  952. T_iccid: mqtt.Iccid,
  953. T_sn: sn,
  954. T_State: 2, //1-未使用 2-已使用 3-已作废
  955. T_type: "4G",
  956. }
  957. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  958. if err != nil {
  959. o.Rollback()
  960. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  961. c.ServeJSON()
  962. return
  963. }
  964. }
  965. stockInProduct := Stock.StockInProduct{
  966. T_number: T_number,
  967. T_product_id: v.T_product_id,
  968. T_depot_id: stockIn.T_depot_id,
  969. T_num: v.T_num, // 入库数量
  970. T_date: stockIn.T_date, // 入库日期
  971. T_relation_sn: strings.Join(v.T_relation_sn, ","),
  972. }
  973. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  974. if err != nil {
  975. o.Rollback()
  976. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  977. c.ServeJSON()
  978. return
  979. }
  980. // 增加库存
  981. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  982. if err != nil {
  983. o.Rollback()
  984. c.Data["json"] = lib.JSONS{Code: 203, Msg: "增加库存失败"}
  985. c.ServeJSON()
  986. return
  987. }
  988. }
  989. }
  990. if len(needEdit) > 0 {
  991. for _, v := range needEdit {
  992. product, _ := Basic.Read_Product_ById(v.T_product_id)
  993. stockInProduct := Stock.StockInProduct{
  994. T_number: stockIn.T_number,
  995. T_product_id: v.T_product_id,
  996. T_depot_id: stockIn.T_depot_id,
  997. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  998. T_date: stockIn.T_date, // 入库日期
  999. }
  1000. // 入库数量比之前多,增加库存
  1001. var T_type int
  1002. var T_num int
  1003. if productNewMap[v.T_product_id].T_num > productOldMap[v.T_product_id].T_num {
  1004. // 增加库存
  1005. T_type = 2
  1006. T_num = productNewMap[v.T_product_id].T_num - productOldMap[v.T_product_id].T_num
  1007. } else {
  1008. // 减少库存
  1009. T_type = 1
  1010. T_num = productOldMap[v.T_product_id].T_num - productNewMap[v.T_product_id].T_num
  1011. }
  1012. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, T_num, T_type)
  1013. if err != nil {
  1014. o.Rollback()
  1015. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  1016. c.ServeJSON()
  1017. return
  1018. }
  1019. // 更新产品库存表
  1020. err = StockInProductDao.Update_StockInProduct(stockInProduct)
  1021. if err != nil {
  1022. o.Rollback()
  1023. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库产品信息失败"}
  1024. c.ServeJSON()
  1025. return
  1026. }
  1027. }
  1028. }
  1029. for _, diff := range snDiff {
  1030. product, _ := Basic.Read_Product_ById(diff.T_product_id)
  1031. // 删除减少的sn
  1032. if len(diff.T_delete_relation_sn) > 0 {
  1033. for _, sn := range diff.T_delete_relation_sn {
  1034. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  1035. if err != nil {
  1036. o.Rollback()
  1037. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  1038. c.ServeJSON()
  1039. return
  1040. }
  1041. }
  1042. // 减少库存
  1043. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, len(diff.T_delete_relation_sn), 1)
  1044. if err != nil {
  1045. o.Rollback()
  1046. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  1047. c.ServeJSON()
  1048. return
  1049. }
  1050. }
  1051. if len(diff.T_add_relation_sn) > 0 {
  1052. for _, sn := range diff.T_add_relation_sn {
  1053. mqtt := Stock.Read_MqttUser(sn)
  1054. // 添加设备
  1055. device := Stock.Device{
  1056. T_contract_number: "",
  1057. T_out_number: "",
  1058. T_product_id: diff.T_product_id,
  1059. T_in_number: stockIn.T_number,
  1060. T_sn: sn,
  1061. T_iccid: mqtt.Iccid,
  1062. T_imei: mqtt.Imei,
  1063. T_State: 2,
  1064. CreateTime: date,
  1065. UpdateTime: stockIn.UpdateTime,
  1066. }
  1067. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  1068. if err != nil {
  1069. o.Rollback()
  1070. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  1071. c.ServeJSON()
  1072. return
  1073. }
  1074. // 添加物联网卡
  1075. iotCard := Property.IotCard{
  1076. T_iccid: mqtt.Iccid,
  1077. T_sn: sn,
  1078. T_State: 2, //1-未使用 2-已使用 3-已作废
  1079. T_type: "4G",
  1080. }
  1081. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  1082. if err != nil {
  1083. o.Rollback()
  1084. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  1085. c.ServeJSON()
  1086. return
  1087. }
  1088. }
  1089. // 增加库存
  1090. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, len(diff.T_add_relation_sn), 2)
  1091. if err != nil {
  1092. o.Rollback()
  1093. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1094. c.ServeJSON()
  1095. return
  1096. }
  1097. }
  1098. stockInProduct := Stock.StockInProduct{
  1099. T_number: stockIn.T_number,
  1100. T_product_id: diff.T_product_id,
  1101. T_depot_id: stockIn.T_depot_id,
  1102. T_num: productNewMap[diff.T_product_id].T_num, // 入库数量
  1103. T_date: stockIn.T_date, // 入库日期
  1104. T_relation_sn: strings.Join(productNewMap[diff.T_product_id].T_relation_sn, ","),
  1105. }
  1106. // 更新产品库存表
  1107. err = StockInProductDao.Update_StockInProduct(stockInProduct)
  1108. if err != nil {
  1109. o.Rollback()
  1110. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库产品信息失败"}
  1111. c.ServeJSON()
  1112. return
  1113. }
  1114. }
  1115. }
  1116. if len(T_remark) > 0 {
  1117. stockIn.T_remark = T_remark
  1118. }
  1119. if len(T_date) > 0 {
  1120. stockIn.T_date = T_date
  1121. }
  1122. if len(T_project) > 0 {
  1123. stockIn.T_project = T_project
  1124. }
  1125. if len(T_return_user) > 0 {
  1126. stockIn.T_return_user = T_return_user
  1127. }
  1128. if len(T_batch_number) > 0 {
  1129. stockIn.T_batch_number = T_batch_number
  1130. }
  1131. err = StockInDao.Update_StockIn(stockIn, "T_remark", "T_date", "T_project", "T_return_user", "T_batch_number")
  1132. if err != nil {
  1133. o.Rollback()
  1134. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  1135. c.ServeJSON()
  1136. return
  1137. }
  1138. o.Commit()
  1139. if len(T_date) > 0 && T_old_date != T_date {
  1140. // 修改出库产品日期
  1141. StockInProductDao.Update_StockInProduct_T_date(stockIn.T_number, T_date)
  1142. DeviceDao.Update_Device_CreateTimeByT_in_number(stockIn.T_number, T_date)
  1143. }
  1144. if len(T_batch_number) > 0 && T_old_batch_number != T_batch_number {
  1145. // 修改出库产品日期
  1146. StockInProductDao.Update_StockInProduct_T_batch_number(stockIn.T_number, T_date)
  1147. }
  1148. // 更新月份统计表
  1149. allProductList := []int{}
  1150. for i, _ := range allProductListMap {
  1151. allProductList = append(allProductList, i)
  1152. }
  1153. StockIn_Edit_StockMonth(stockIn.T_date, stockIn.T_depot_id, allProductList)
  1154. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "修改", stockIn)
  1155. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1156. c.ServeJSON()
  1157. return
  1158. }
  1159. // 删除入库
  1160. func (c *StockController) StockIn_Del() {
  1161. T_number := c.GetString("T_number") // 入库单号
  1162. o := orm.NewOrm()
  1163. o.Begin()
  1164. StockInDao := Stock.NewStockIn(o)
  1165. StockInProductDao := Stock.NewStockInProduct(o)
  1166. StockDao := Stock.NewStock(o)
  1167. DeviceDao := Stock.NewDevice(o)
  1168. // 查询入库信息
  1169. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1170. if err != nil {
  1171. o.Rollback()
  1172. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1173. c.ServeJSON()
  1174. return
  1175. }
  1176. // 查询入库产品信息
  1177. productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1178. var oldProductList []dto.StockProduct
  1179. allProductList := []int{}
  1180. for _, product := range productOldList {
  1181. oldProductList = append(oldProductList, dto.StockProduct{
  1182. T_product_id: product.T_product_id,
  1183. T_num: product.T_num,
  1184. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  1185. })
  1186. allProductList = append(allProductList, product.T_product_id)
  1187. }
  1188. // 删除入库产品列表
  1189. for _, v := range oldProductList {
  1190. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1191. // 删除入库产品列表
  1192. err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
  1193. if err != nil {
  1194. o.Rollback()
  1195. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  1196. c.ServeJSON()
  1197. return
  1198. }
  1199. if product.T_relation_sn == 1 {
  1200. // 删除该入库单关联sn
  1201. for _, sn := range v.T_relation_sn {
  1202. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  1203. if err != nil {
  1204. o.Rollback()
  1205. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  1206. c.ServeJSON()
  1207. return
  1208. }
  1209. }
  1210. }
  1211. // 减少库存
  1212. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  1213. if err != nil {
  1214. o.Rollback()
  1215. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  1216. c.ServeJSON()
  1217. return
  1218. }
  1219. }
  1220. err = StockInDao.Delete_StockIn(stockIn)
  1221. if err != nil {
  1222. o.Rollback()
  1223. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库失败"}
  1224. c.ServeJSON()
  1225. return
  1226. }
  1227. o.Commit()
  1228. // 更新月份统计表
  1229. StockIn_Edit_StockMonth(stockIn.T_date, stockIn.T_depot_id, allProductList)
  1230. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "删除", stockIn)
  1231. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1232. c.ServeJSON()
  1233. return
  1234. }
  1235. // 导出出库单
  1236. func (c *StockController) StockIn_Excel() {
  1237. // 查询
  1238. T_number := c.GetString("T_number")
  1239. o := orm.NewOrm()
  1240. StockInDao := Stock.NewStockIn(o)
  1241. StockInProductDao := Stock.NewStockInProduct(o)
  1242. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1243. if err != nil {
  1244. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1245. c.ServeJSON()
  1246. return
  1247. }
  1248. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1249. var pList []Stock.StockInProduct_R
  1250. for _, v := range productList {
  1251. pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
  1252. }
  1253. userList, _ := NatsServer.Read_User_List_All()
  1254. Account.Read_User_All_Map(userList)
  1255. stockInDetail := Stock.StockInToStockIn_Detail(stockIn, pList)
  1256. T_date, _ := lib.DateStrToTime(stockIn.T_date)
  1257. filename := fmt.Sprintf("冷链产品入库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  1258. f := excelize.NewFile() // 设置单元格的值
  1259. Style1, _ := f.NewStyle(
  1260. &excelize.Style{
  1261. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  1262. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1263. })
  1264. Style2, _ := f.NewStyle(
  1265. &excelize.Style{
  1266. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1267. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  1268. })
  1269. Style3, _ := f.NewStyle(
  1270. &excelize.Style{
  1271. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  1272. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1273. Border: []excelize.Border{
  1274. {Type: "left", Color: "000000", Style: 1},
  1275. {Type: "top", Color: "000000", Style: 1},
  1276. {Type: "bottom", Color: "000000", Style: 1},
  1277. {Type: "right", Color: "000000", Style: 1},
  1278. },
  1279. })
  1280. Style4, _ := f.NewStyle(
  1281. &excelize.Style{
  1282. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1283. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1284. Border: []excelize.Border{
  1285. {Type: "left", Color: "000000", Style: 1},
  1286. {Type: "top", Color: "000000", Style: 1},
  1287. {Type: "bottom", Color: "000000", Style: 1},
  1288. {Type: "right", Color: "000000", Style: 1},
  1289. },
  1290. })
  1291. productList2 := lib.ChunkBy(productList, 10)
  1292. for i, products := range productList2 {
  1293. if len(products) < 10 {
  1294. products = append(products, make([]Stock.StockInProduct, 10-len(products))...)
  1295. }
  1296. row := i*17 + 1
  1297. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1298. f.SetRowStyle("Sheet1", row, row, Style1)
  1299. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  1300. f.SetRowHeight("Sheet1", row, 20)
  1301. row += 1
  1302. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1303. f.SetRowStyle("Sheet1", row, row, Style1)
  1304. title := "硬件入库单"
  1305. if len(productList2) > 1 {
  1306. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  1307. }
  1308. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  1309. f.SetRowHeight("Sheet1", row, 20)
  1310. row += 1
  1311. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1312. f.SetRowStyle("Sheet1", row, row, Style2)
  1313. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库单号:%s", stockIn.T_number))
  1314. f.SetRowHeight("Sheet1", row, 20)
  1315. row += 1
  1316. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1317. f.SetRowStyle("Sheet1", row, row, Style2)
  1318. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockIn.T_remark))
  1319. f.SetRowHeight("Sheet1", row, 20)
  1320. row += 1
  1321. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1322. f.SetRowStyle("Sheet1", row, row, Style2)
  1323. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库时间:%s", T_date.Format("2006年01月02日")))
  1324. f.SetRowHeight("Sheet1", row, 20)
  1325. // 这里设置表头
  1326. row += 1
  1327. f.SetRowHeight("Sheet1", row, 20)
  1328. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  1329. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  1330. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  1331. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  1332. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  1333. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  1334. // 设置列宽
  1335. f.SetColWidth("Sheet1", "A", "A", 5)
  1336. f.SetColWidth("Sheet1", "B", "B", 20)
  1337. f.SetColWidth("Sheet1", "C", "C", 12)
  1338. f.SetColWidth("Sheet1", "D", "D", 10)
  1339. f.SetColWidth("Sheet1", "E", "E", 10)
  1340. f.SetColWidth("Sheet1", "F", "F", 15)
  1341. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  1342. sRow := row
  1343. // 循环写入数据
  1344. for j, v := range products {
  1345. row += 1
  1346. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1347. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  1348. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  1349. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  1350. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  1351. if v.T_num > 0 {
  1352. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  1353. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockIn.T_remark)
  1354. }
  1355. }
  1356. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  1357. row += 1
  1358. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  1359. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  1360. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  1361. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("经办人:%s", stockInDetail.T_submit_name))
  1362. f.SetRowStyle("Sheet1", row, row, Style2)
  1363. f.SetRowHeight("Sheet1", row, 20)
  1364. }
  1365. // 保存文件
  1366. if err = f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  1367. fmt.Println(err)
  1368. }
  1369. var url string
  1370. //// 上传 OSS
  1371. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  1372. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  1373. if !is {
  1374. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1375. c.ServeJSON()
  1376. return
  1377. }
  1378. //删除目录
  1379. err = os.Remove("ofile/" + filename + ".xlsx")
  1380. if err != nil {
  1381. logs.Error(lib.FuncName(), err)
  1382. }
  1383. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1384. c.ServeJSON()
  1385. return
  1386. }
  1387. // 批量导出出库单
  1388. func (c *StockController) StockIn_Excel_Batch() {
  1389. // 查询
  1390. T_number_list := c.GetString("T_number_list")
  1391. o := orm.NewOrm()
  1392. StockInDao := Stock.NewStockIn(o)
  1393. StockInProductDao := Stock.NewStockInProduct(o)
  1394. list := strings.Split(strings.Trim(T_number_list, "|"), "|")
  1395. userList, _ := NatsServer.Read_User_List_All()
  1396. Account.Read_User_All_Map(userList)
  1397. filename := fmt.Sprintf("冷链产品入库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  1398. f := excelize.NewFile() // 设置单元格的值
  1399. Style1, _ := f.NewStyle(
  1400. &excelize.Style{
  1401. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  1402. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1403. })
  1404. Style2, _ := f.NewStyle(
  1405. &excelize.Style{
  1406. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1407. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  1408. })
  1409. Style3, _ := f.NewStyle(
  1410. &excelize.Style{
  1411. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  1412. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1413. Border: []excelize.Border{
  1414. {Type: "left", Color: "000000", Style: 1},
  1415. {Type: "top", Color: "000000", Style: 1},
  1416. {Type: "bottom", Color: "000000", Style: 1},
  1417. {Type: "right", Color: "000000", Style: 1},
  1418. },
  1419. })
  1420. Style4, _ := f.NewStyle(
  1421. &excelize.Style{
  1422. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1423. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1424. Border: []excelize.Border{
  1425. {Type: "left", Color: "000000", Style: 1},
  1426. {Type: "top", Color: "000000", Style: 1},
  1427. {Type: "bottom", Color: "000000", Style: 1},
  1428. {Type: "right", Color: "000000", Style: 1},
  1429. },
  1430. })
  1431. var num int
  1432. for _, T_number := range list {
  1433. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1434. if err != nil {
  1435. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1436. c.ServeJSON()
  1437. return
  1438. }
  1439. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1440. var pList []Stock.StockInProduct_R
  1441. for _, v := range productList {
  1442. pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
  1443. }
  1444. stockInDetail := Stock.StockInToStockIn_Detail(stockIn, pList)
  1445. T_date, _ := lib.DateStrToTime(stockIn.T_date)
  1446. productList2 := lib.ChunkBy(productList, 10)
  1447. for i, products := range productList2 {
  1448. if len(products) < 10 {
  1449. products = append(products, make([]Stock.StockInProduct, 10-len(products))...)
  1450. }
  1451. row := num*18 + 1
  1452. num += 1
  1453. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1454. f.SetRowStyle("Sheet1", row, row, Style1)
  1455. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  1456. f.SetRowHeight("Sheet1", row, 20)
  1457. row += 1
  1458. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1459. f.SetRowStyle("Sheet1", row, row, Style1)
  1460. title := "硬件入库单"
  1461. if len(productList2) > 1 {
  1462. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  1463. }
  1464. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  1465. f.SetRowHeight("Sheet1", row, 20)
  1466. row += 1
  1467. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1468. f.SetRowStyle("Sheet1", row, row, Style2)
  1469. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库单号:%s", stockIn.T_number))
  1470. f.SetRowHeight("Sheet1", row, 20)
  1471. row += 1
  1472. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1473. f.SetRowStyle("Sheet1", row, row, Style2)
  1474. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockIn.T_remark))
  1475. f.SetRowHeight("Sheet1", row, 20)
  1476. row += 1
  1477. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1478. f.SetRowStyle("Sheet1", row, row, Style2)
  1479. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库时间:%s", T_date.Format("2006年01月02日")))
  1480. f.SetRowHeight("Sheet1", row, 20)
  1481. // 这里设置表头
  1482. row += 1
  1483. f.SetRowHeight("Sheet1", row, 20)
  1484. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  1485. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  1486. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  1487. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  1488. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  1489. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  1490. // 设置列宽
  1491. f.SetColWidth("Sheet1", "A", "A", 5)
  1492. f.SetColWidth("Sheet1", "B", "B", 20)
  1493. f.SetColWidth("Sheet1", "C", "C", 12)
  1494. f.SetColWidth("Sheet1", "D", "D", 10)
  1495. f.SetColWidth("Sheet1", "E", "E", 10)
  1496. f.SetColWidth("Sheet1", "F", "F", 15)
  1497. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  1498. sRow := row
  1499. // 循环写入数据
  1500. for j, v := range products {
  1501. row += 1
  1502. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1503. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  1504. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  1505. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  1506. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  1507. if v.T_num > 0 {
  1508. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  1509. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockIn.T_remark)
  1510. }
  1511. }
  1512. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  1513. row += 1
  1514. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  1515. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  1516. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  1517. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("经办人:%s", stockInDetail.T_submit_name))
  1518. f.SetRowStyle("Sheet1", row, row, Style2)
  1519. f.SetRowHeight("Sheet1", row, 20)
  1520. // 空一行
  1521. row += 1
  1522. //f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1523. //f.SetRowStyle("Sheet1", row, row, Style1)
  1524. //f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "")
  1525. // 插入下一页分页符
  1526. f.InsertPageBreak("Sheet1", fmt.Sprintf("A%d", row))
  1527. }
  1528. }
  1529. // 保存文件
  1530. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  1531. fmt.Println(err)
  1532. }
  1533. var url string
  1534. //// 上传 OSS
  1535. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  1536. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  1537. if !is {
  1538. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1539. c.ServeJSON()
  1540. return
  1541. }
  1542. //删除目录
  1543. err := os.Remove("ofile/" + filename + ".xlsx")
  1544. if err != nil {
  1545. logs.Error(lib.FuncName(), err)
  1546. }
  1547. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1548. c.ServeJSON()
  1549. return
  1550. }
  1551. func (c *StockController) StockOut_List() {
  1552. // 分页参数 初始化
  1553. page, _ := c.GetInt("page")
  1554. if page < 1 {
  1555. page = 1
  1556. }
  1557. page_z, _ := c.GetInt("page_z")
  1558. if page_z < 1 {
  1559. page_z = conf.Page_size
  1560. }
  1561. // 查询
  1562. T_depot_id, _ := c.GetInt("T_depot_id")
  1563. T_contract_number := c.GetString("T_contract_number")
  1564. T_start_date := c.GetString("T_start_date")
  1565. T_end_date := c.GetString("T_end_date")
  1566. T_name := c.GetString("T_name")
  1567. userList, _ := NatsServer.Read_User_List_All()
  1568. Account.Read_User_All_Map(userList)
  1569. Basic.Read_Depot_All_Map()
  1570. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  1571. R_List, R_cnt := StockOutDao.Read_StockOut_List(T_name, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  1572. var r_jsons lib.R_JSONS
  1573. r_jsons.Num = R_cnt
  1574. r_jsons.Data = R_List
  1575. r_jsons.Page = page
  1576. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  1577. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1578. c.ServeJSON()
  1579. return
  1580. }
  1581. func (c *StockController) StockOut_List_Product() {
  1582. // 分页参数 初始化
  1583. page, _ := c.GetInt("page")
  1584. if page < 1 {
  1585. page = 1
  1586. }
  1587. page_z, _ := c.GetInt("page_z")
  1588. if page_z < 1 {
  1589. page_z = conf.Page_size
  1590. }
  1591. // 查询
  1592. T_depot_id, _ := c.GetInt("T_depot_id")
  1593. T_name := c.GetString("T_name")
  1594. T_start_date := c.GetString("T_start_date")
  1595. T_end_date := c.GetString("T_end_date")
  1596. T_product_name := c.GetString("T_product_name") // 产品名称
  1597. T_product_model := c.GetString("T_product_model") // 产品型号
  1598. userList, _ := NatsServer.Read_User_List_All()
  1599. Account.Read_User_All_Map(userList)
  1600. Basic.Read_Depot_All_Map()
  1601. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  1602. R_List, R_cnt := StockOutDao.Read_StockOutProduct_List(T_name, T_start_date, T_end_date, T_depot_id, T_product_name, T_product_model, page, page_z)
  1603. var r_jsons lib.R_JSONS
  1604. r_jsons.Num = R_cnt
  1605. r_jsons.Data = R_List
  1606. r_jsons.Page = page
  1607. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  1608. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1609. c.ServeJSON()
  1610. return
  1611. }
  1612. func (c *StockController) StockOut_List_Product_Excel() {
  1613. // 查询
  1614. T_depot_id, _ := c.GetInt("T_depot_id")
  1615. T_name := c.GetString("T_name")
  1616. T_start_date := c.GetString("T_start_date")
  1617. T_end_date := c.GetString("T_end_date")
  1618. T_product_name := c.GetString("T_product_name") // 产品名称
  1619. T_product_model := c.GetString("T_product_model") // 产品型号
  1620. userList, _ := NatsServer.Read_User_List_All()
  1621. Account.Read_User_All_Map(userList)
  1622. Basic.Read_Depot_All_Map()
  1623. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  1624. R_List, _ := StockOutDao.Read_StockOutProduct_List(T_name, T_start_date, T_end_date, T_depot_id, T_product_name, T_product_model, 0, 9999)
  1625. filename := fmt.Sprintf("出库明细单(%s)", lib.GetRandstring(6, "0123456789", 0))
  1626. f := excelize.NewFile() // 设置单元格的值
  1627. Style1, _ := f.NewStyle(
  1628. &excelize.Style{
  1629. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  1630. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1631. })
  1632. Style2, _ := f.NewStyle(
  1633. &excelize.Style{
  1634. Font: &excelize.Font{Bold: true, Size: 11, Family: "宋体"},
  1635. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1636. Border: []excelize.Border{
  1637. {Type: "left", Color: "000000", Style: 1},
  1638. {Type: "top", Color: "000000", Style: 1},
  1639. {Type: "bottom", Color: "000000", Style: 1},
  1640. {Type: "right", Color: "000000", Style: 1},
  1641. },
  1642. })
  1643. Style3, _ := f.NewStyle(
  1644. &excelize.Style{
  1645. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1646. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  1647. Border: []excelize.Border{
  1648. {Type: "left", Color: "000000", Style: 1},
  1649. {Type: "top", Color: "000000", Style: 1},
  1650. {Type: "bottom", Color: "000000", Style: 1},
  1651. {Type: "right", Color: "000000", Style: 1},
  1652. },
  1653. })
  1654. f.MergeCell("Sheet1", "A1", "L1")
  1655. f.SetRowStyle("Sheet1", 1, 1, Style1)
  1656. f.SetCellValue("Sheet1", "A1", fmt.Sprintf("出库明细单"))
  1657. f.SetRowHeight("Sheet1", 1, 30)
  1658. // 这里设置表头
  1659. f.SetCellStyle("Sheet1", "A2", "L2", Style2)
  1660. f.SetRowHeight("Sheet1", 2, 20)
  1661. f.SetCellValue("Sheet1", "A2", "序号")
  1662. f.SetCellValue("Sheet1", "B2", "出库单号")
  1663. f.SetCellValue("Sheet1", "C2", "领取人")
  1664. f.SetCellValue("Sheet1", "D2", "出库日期")
  1665. f.SetCellValue("Sheet1", "E2", "出库仓库")
  1666. f.SetCellValue("Sheet1", "F2", "关联项目")
  1667. f.SetCellValue("Sheet1", "G2", "产品名称")
  1668. f.SetCellValue("Sheet1", "H2", "产品型号")
  1669. f.SetCellValue("Sheet1", "I2", "数量")
  1670. f.SetCellValue("Sheet1", "J2", "是否关联SN")
  1671. f.SetCellValue("Sheet1", "K2", "备注")
  1672. f.SetCellValue("Sheet1", "L2", "SN")
  1673. // 设置列宽
  1674. f.SetColWidth("Sheet1", "A", "A", 10)
  1675. f.SetColWidth("Sheet1", "B", "B", 15)
  1676. f.SetColWidth("Sheet1", "C", "D", 10)
  1677. f.SetColWidth("Sheet1", "D", "D", 10)
  1678. f.SetColWidth("Sheet1", "E", "E", 10)
  1679. f.SetColWidth("Sheet1", "F", "F", 20)
  1680. f.SetColWidth("Sheet1", "G", "G", 20)
  1681. f.SetColWidth("Sheet1", "H", "H", 15)
  1682. f.SetColWidth("Sheet1", "I", "I", 10)
  1683. f.SetColWidth("Sheet1", "J", "J", 12)
  1684. f.SetColWidth("Sheet1", "K", "K", 30)
  1685. f.SetColWidth("Sheet1", "L", "L", 100)
  1686. line := 2
  1687. for _, product := range R_List {
  1688. line += 1
  1689. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-2)
  1690. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), product.T_number)
  1691. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), product.T_receive_name)
  1692. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), product.T_date)
  1693. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), product.T_depot_name)
  1694. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), product.T_project)
  1695. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), product.T_product_name)
  1696. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), product.T_product_model)
  1697. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), product.T_num)
  1698. var relation_sn string
  1699. if product.T_product_relation_sn == 1 {
  1700. relation_sn = "是"
  1701. } else {
  1702. relation_sn = "否"
  1703. }
  1704. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), relation_sn)
  1705. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), product.T_remark)
  1706. f.SetCellValue("Sheet1", fmt.Sprintf("L%d", line), product.T_relation_sn)
  1707. }
  1708. f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("L%d", line), Style3)
  1709. // 保存文件
  1710. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  1711. fmt.Println(err)
  1712. }
  1713. var url string
  1714. //// 上传 OSS
  1715. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  1716. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  1717. if !is {
  1718. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1719. c.ServeJSON()
  1720. return
  1721. }
  1722. //删除目录
  1723. err := os.Remove("ofile/" + filename + ".xlsx")
  1724. if err != nil {
  1725. logs.Error(lib.FuncName(), err)
  1726. }
  1727. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1728. c.ServeJSON()
  1729. return
  1730. }
  1731. func (c *StockController) StockOut_Get() {
  1732. // 查询
  1733. T_number := c.GetString("T_number")
  1734. o := orm.NewOrm()
  1735. StockOutDao := Stock.NewStockOut(o)
  1736. StockOutProductDao := Stock.NewStockOutProduct(o)
  1737. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  1738. if err != nil {
  1739. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1740. c.ServeJSON()
  1741. return
  1742. }
  1743. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  1744. var pList []Stock.StockOutProduct_R
  1745. for _, v := range productList {
  1746. pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
  1747. }
  1748. userList, _ := NatsServer.Read_User_List_All()
  1749. Account.Read_User_All_Map(userList)
  1750. Basic.Read_Depot_All_Map()
  1751. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Stock.StockOutToStockOut_Detail(stockOut, pList)}
  1752. c.ServeJSON()
  1753. return
  1754. }
  1755. func (c *StockController) StockOut_generate_number() {
  1756. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  1757. rand_x := 0
  1758. T_number := ""
  1759. for true {
  1760. T_number = "CK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
  1761. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  1762. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  1763. break
  1764. }
  1765. rand_x += 1
  1766. }
  1767. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1768. c.ServeJSON()
  1769. return
  1770. }
  1771. func (c *StockController) StockOut_Add() {
  1772. o := orm.NewOrm()
  1773. o.Begin()
  1774. StockOutDao := Stock.NewStockOut(o)
  1775. // 检查前端传入的T_number是否已提交过
  1776. T_number := c.GetString("T_number")
  1777. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  1778. if err == nil {
  1779. // 入库编号已存在,不需要做任何处理
  1780. o.Rollback()
  1781. c.Data["json"] = lib.JSONS{Code: 200, Msg: "出库编号已存在,无需重复提交!", Data: T_number}
  1782. c.ServeJSON()
  1783. return
  1784. }
  1785. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  1786. // 数据库查询错误
  1787. o.Rollback()
  1788. c.Data["json"] = lib.JSONS{Code: 203, Msg: "查询入库编号失败"}
  1789. c.ServeJSON()
  1790. return
  1791. }
  1792. T_contract_number := c.GetString("T_contract_number")
  1793. T_depot_id, _ := c.GetInt("T_depot_id")
  1794. T_type, _ := c.GetInt("T_type") // 出库类型 1-领料出库 2-销售出库
  1795. T_date := c.GetString("T_date")
  1796. T_receive := c.GetString("T_receive")
  1797. T_product := c.GetString("T_product")
  1798. T_remark := c.GetString("T_remark")
  1799. T_project := c.GetString("T_project")
  1800. T_delivery_type, _ := c.GetInt("T_delivery_type")
  1801. T_signer_unit := c.GetString("T_signer_unit")
  1802. T_signer := c.GetString("T_signer")
  1803. T_signer_phone := c.GetString("T_signer_phone")
  1804. T_signer_date := c.GetString("T_signer_date")
  1805. T_courier_number := c.GetString("T_courier_number")
  1806. date, is := lib.DateStrToTime(T_date)
  1807. if !is {
  1808. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  1809. c.ServeJSON()
  1810. return
  1811. }
  1812. var_ := Stock.StockOut{
  1813. T_number: T_number,
  1814. T_contract_number: T_contract_number,
  1815. T_depot_id: T_depot_id,
  1816. T_type: T_type,
  1817. T_date: T_date,
  1818. T_receive: T_receive,
  1819. T_remark: T_remark,
  1820. T_project: T_project,
  1821. T_submit: c.User.T_uuid,
  1822. // 销售出库
  1823. T_delivery_type: T_delivery_type, // 1-自送 2-自提 3-快递
  1824. T_signer_unit: T_signer_unit,
  1825. T_signer: T_signer,
  1826. T_signer_phone: T_signer_phone,
  1827. T_signer_date: T_signer_date,
  1828. T_courier_number: T_courier_number,
  1829. T_state: Stock.StockOutAlreadyOut,
  1830. }
  1831. StockOutProductDao := Stock.NewStockOutProduct(o)
  1832. StockDao := Stock.NewStock(o)
  1833. DeviceDao := Stock.NewDevice(o)
  1834. ContractDao := Contract.NewContract(o)
  1835. ContractProductDao := Contract.NewContractProduct(o)
  1836. _, err = StockOutDao.Add_StockOut(var_)
  1837. if err != nil {
  1838. o.Rollback()
  1839. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  1840. c.ServeJSON()
  1841. return
  1842. }
  1843. // 1、添加出库单
  1844. contract, _ := ContractDao.Read_Contract_ByT_number(T_contract_number)
  1845. if contract.Id == 0 && T_type == 2 {
  1846. o.Rollback()
  1847. c.Data["json"] = lib.JSONS{Code: 203, Msg: "合同编号错误!"}
  1848. c.ServeJSON()
  1849. return
  1850. }
  1851. if len(T_product) == 0 {
  1852. o.Rollback()
  1853. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  1854. c.ServeJSON()
  1855. return
  1856. }
  1857. productList := lib.SplitString(T_product, "|")
  1858. allProductList := []int{}
  1859. for _, v := range productList {
  1860. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  1861. allProductList = append(allProductList, product_id)
  1862. product, _ := Basic.Read_Product_ById(product_id)
  1863. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  1864. T_relation_sn := strings.Split(v, "-")[2]
  1865. if T_relation_sn == "" && product.T_relation_sn == 1 {
  1866. o.Rollback()
  1867. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  1868. c.ServeJSON()
  1869. return
  1870. }
  1871. // 2、更新设备状态为已出库
  1872. if len(T_relation_sn) > 0 {
  1873. snList := lib.SplitString(T_relation_sn, ",")
  1874. num = len(snList)
  1875. for _, sn := range snList {
  1876. mqtt := Stock.Read_MqttUser(sn)
  1877. device := Stock.Device{
  1878. T_contract_number: T_contract_number,
  1879. T_product_id: product_id,
  1880. T_out_number: T_number,
  1881. T_sn: sn,
  1882. T_iccid: mqtt.Iccid,
  1883. T_imei: mqtt.Imei,
  1884. T_State: 1,
  1885. T_project: T_project,
  1886. CreateTime: date,
  1887. }
  1888. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  1889. if err != nil {
  1890. o.Rollback()
  1891. c.Data["json"] = lib.JSONS{Code: 202, Msg: "出库失败"}
  1892. c.ServeJSON()
  1893. return
  1894. }
  1895. }
  1896. }
  1897. // 3、添加出库产品清单
  1898. stockOutProduct := Stock.StockOutProduct{
  1899. T_number: T_number,
  1900. T_product_id: product_id,
  1901. T_depot_id: T_depot_id,
  1902. T_num: num, // 出库数量
  1903. T_date: T_date, // 出库数量
  1904. T_relation_sn: T_relation_sn,
  1905. T_state: 1,
  1906. }
  1907. _, err = StockOutProductDao.Add_StockOutProduct(stockOutProduct)
  1908. if err != nil {
  1909. o.Rollback()
  1910. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  1911. c.ServeJSON()
  1912. return
  1913. }
  1914. // 4、更改合同产品列表清单
  1915. contractProduct, _ := ContractProductDao.Read_ContractProduct_ByT_number_T_product_id(T_contract_number, product_id)
  1916. contractProduct.T_product_out += num
  1917. if contractProduct.T_product_out > contractProduct.T_product_total && T_type == 2 {
  1918. o.Rollback()
  1919. p, _ := Basic.Read_Product_ById(product_id)
  1920. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】出库数量超过预计出库数量!", p.T_name)}
  1921. c.ServeJSON()
  1922. return
  1923. }
  1924. contractProduct.T_State = 2
  1925. if contractProduct.T_product_out == 0 {
  1926. contractProduct.T_State = 1
  1927. }
  1928. if contractProduct.T_product_out == contractProduct.T_product_total {
  1929. contractProduct.T_State = 3
  1930. }
  1931. err = ContractProductDao.Update_ContractProduct(contractProduct, "T_product_out", "T_State")
  1932. if err != nil {
  1933. o.Rollback()
  1934. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同产品清单失败"}
  1935. c.ServeJSON()
  1936. return
  1937. }
  1938. // 5、更新产品库存列表
  1939. _, err = StockDao.AddOrUpdate_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 1)
  1940. if err != nil {
  1941. o.Rollback()
  1942. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存失败" + err.Error()}
  1943. c.ServeJSON()
  1944. return
  1945. }
  1946. }
  1947. o.Commit()
  1948. o2 := orm.NewOrm()
  1949. o2.Begin()
  1950. ContractDao2 := Contract.NewContract(o2)
  1951. ContractProductDao2 := Contract.NewContractProduct(o2)
  1952. // 5、更新合同出库状态
  1953. var T_out int
  1954. if contract.T_State == 1 {
  1955. T_out = 2
  1956. // 查询合同产品清单是否全部都为已出库
  1957. state := ContractProductDao2.Read_ContractProduct_T_State_List(contract.T_number)
  1958. if state == 1 {
  1959. T_out = 1
  1960. }
  1961. if state == 3 {
  1962. T_out = 3
  1963. }
  1964. }
  1965. if T_out != contract.T_out {
  1966. contract.T_out = T_out
  1967. err = ContractDao2.Update_Contract(contract, "T_out")
  1968. if err != nil {
  1969. o2.Rollback()
  1970. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同出库状态失败"}
  1971. c.ServeJSON()
  1972. return
  1973. }
  1974. }
  1975. o2.Commit()
  1976. StockOut_Edit_StockMonth(T_date, T_depot_id, allProductList)
  1977. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "出库", var_)
  1978. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1979. c.ServeJSON()
  1980. return
  1981. }
  1982. func (c *StockController) StockOut_Edit_Delivery() {
  1983. T_number := c.GetString("T_number")
  1984. T_remark := c.GetString("T_remark")
  1985. T_project := c.GetString("T_project")
  1986. T_delivery_type, _ := c.GetInt("T_delivery_type")
  1987. T_signer_unit := c.GetString("T_signer_unit")
  1988. T_signer := c.GetString("T_signer")
  1989. T_signer_phone := c.GetString("T_signer_phone")
  1990. T_signer_date := c.GetString("T_signer_date")
  1991. T_courier_number := c.GetString("T_courier_number")
  1992. o := orm.NewOrm()
  1993. StockOutDao := Stock.NewStockOut(o)
  1994. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  1995. if err != nil {
  1996. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库编号错误!"}
  1997. c.ServeJSON()
  1998. return
  1999. }
  2000. if len(T_remark) > 0 {
  2001. stockOut.T_remark = T_remark
  2002. }
  2003. if len(T_project) > 0 {
  2004. stockOut.T_project = T_project
  2005. }
  2006. if T_delivery_type > 0 {
  2007. stockOut.T_delivery_type = T_delivery_type
  2008. }
  2009. if len(T_signer_unit) > 0 {
  2010. stockOut.T_signer_unit = T_signer_unit
  2011. }
  2012. if len(T_signer) > 0 {
  2013. stockOut.T_signer = T_signer
  2014. }
  2015. if len(T_signer_phone) > 0 {
  2016. stockOut.T_signer_phone = T_signer_phone
  2017. }
  2018. if len(T_signer_date) > 0 {
  2019. stockOut.T_signer_date = T_signer_date
  2020. }
  2021. if len(T_courier_number) > 0 {
  2022. stockOut.T_courier_number = T_courier_number
  2023. }
  2024. err = StockOutDao.Update_StockOut(stockOut, "T_remark", "T_delivery_type", "T_signer_unit", "T_signer", "T_signer_phone", "T_signer_date", "T_courier_number", "T_project")
  2025. if err != nil {
  2026. o.Rollback()
  2027. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  2028. c.ServeJSON()
  2029. return
  2030. }
  2031. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "修改发货单", stockOut)
  2032. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2033. c.ServeJSON()
  2034. return
  2035. }
  2036. func (c *StockController) StockOut_Edit() {
  2037. T_number := c.GetString("T_number") // 出库单号
  2038. //T_depot_id, _ := c.GetInt("T_depot_id")
  2039. T_date := c.GetString("T_date")
  2040. T_receive := c.GetString("T_receive")
  2041. T_project := c.GetString("T_project")
  2042. T_product := c.GetString("T_product")
  2043. T_remark := c.GetString("T_remark")
  2044. date, is := lib.DateStrToTime(T_date)
  2045. if !is {
  2046. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  2047. c.ServeJSON()
  2048. return
  2049. }
  2050. if date.Year() != time.Now().Year() && date.Month() != time.Now().Month() {
  2051. T_date = ""
  2052. }
  2053. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改出库", T_product)
  2054. o := orm.NewOrm()
  2055. o.Begin()
  2056. StockOutDao := Stock.NewStockOut(o)
  2057. StockOutProductDao := Stock.NewStockOutProduct(o)
  2058. StockDao := Stock.NewStock(o)
  2059. DeviceDao := Stock.NewDevice(o)
  2060. IotCardDao := Property.NewIotCard(&o)
  2061. // 查询出库信息
  2062. StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2063. if err != nil {
  2064. o.Rollback()
  2065. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2066. c.ServeJSON()
  2067. return
  2068. }
  2069. T_old_date := StockOut.T_date
  2070. // 查询出库产品信息
  2071. productOldList := StockOutProductDao.Read_StockOutProduct_List(StockOut.T_number)
  2072. var oldProductList []dto.StockProduct
  2073. var newProductList []dto.StockProduct
  2074. productOldMap := map[int]dto.StockProduct{}
  2075. allProductListMap := make(map[int]struct{})
  2076. for _, product := range productOldList {
  2077. stockProduct := dto.StockProduct{
  2078. T_product_id: product.T_product_id,
  2079. T_num: product.T_num,
  2080. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  2081. }
  2082. oldProductList = append(oldProductList, stockProduct)
  2083. productOldMap[product.T_product_id] = stockProduct
  2084. if _, ok := allProductListMap[product.T_product_id]; !ok {
  2085. allProductListMap[product.T_product_id] = struct{}{}
  2086. }
  2087. }
  2088. productNewList := lib.SplitString(T_product, "|")
  2089. productNewMap := map[int]dto.StockProduct{}
  2090. for _, v := range productNewList {
  2091. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  2092. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  2093. T_relation_sn := strings.Split(v, "-")[2]
  2094. snList := lib.SplitString(T_relation_sn, ",")
  2095. if len(snList) > 0 {
  2096. num = len(snList)
  2097. }
  2098. stockProduct := dto.StockProduct{
  2099. T_product_id: product_id,
  2100. T_num: num,
  2101. T_relation_sn: snList,
  2102. }
  2103. newProductList = append(newProductList, stockProduct)
  2104. productNewMap[product_id] = stockProduct
  2105. if _, ok := allProductListMap[product_id]; !ok {
  2106. allProductListMap[product_id] = struct{}{}
  2107. }
  2108. }
  2109. // 判断产品列表信息是否相同
  2110. StockProductListIsEqual, needDelete, needAdd, needEdit, snDiff := dto.StockProductListsEqual(oldProductList, newProductList)
  2111. // 两次提交的数据相同,则不用修改
  2112. if !StockProductListIsEqual {
  2113. if len(needDelete) > 0 {
  2114. // 删除入库产品列表
  2115. for _, v := range needDelete {
  2116. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2117. // 删除入库产品列表
  2118. err = StockOutProductDao.Delete_StockOutProduct(StockOut.T_number, StockOut.T_depot_id, v.T_product_id)
  2119. if err != nil {
  2120. o.Rollback()
  2121. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  2122. c.ServeJSON()
  2123. return
  2124. }
  2125. if product.T_relation_sn == 1 {
  2126. // 删除该入库单关联sn
  2127. for _, sn := range v.T_relation_sn {
  2128. err = DeviceDao.Delete_Device_ByT_out_number(StockOut.T_number, sn)
  2129. if err != nil {
  2130. o.Rollback()
  2131. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  2132. c.ServeJSON()
  2133. return
  2134. }
  2135. }
  2136. }
  2137. // 增加库存
  2138. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  2139. if err != nil {
  2140. o.Rollback()
  2141. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  2142. c.ServeJSON()
  2143. return
  2144. }
  2145. }
  2146. }
  2147. if len(needAdd) > 0 {
  2148. // 新增入库产品列表
  2149. for _, v := range needAdd {
  2150. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2151. if product.T_relation_sn == 1 && len(v.T_relation_sn) == 0 {
  2152. o.Rollback()
  2153. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  2154. c.ServeJSON()
  2155. return
  2156. }
  2157. // 添加该入库单关联sn
  2158. for _, sn := range v.T_relation_sn {
  2159. mqtt := Stock.Read_MqttUser(sn)
  2160. // 添加设备
  2161. device := Stock.Device{
  2162. T_contract_number: "",
  2163. T_out_number: StockOut.T_number,
  2164. T_product_id: v.T_product_id,
  2165. T_in_number: "",
  2166. T_sn: sn,
  2167. T_iccid: mqtt.Iccid,
  2168. T_imei: mqtt.Imei,
  2169. T_State: 1,
  2170. CreateTime: date,
  2171. UpdateTime: StockOut.UpdateTime,
  2172. }
  2173. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  2174. if err != nil {
  2175. o.Rollback()
  2176. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  2177. c.ServeJSON()
  2178. return
  2179. }
  2180. // 添加物联网卡
  2181. iotCard := Property.IotCard{
  2182. T_iccid: mqtt.Iccid,
  2183. T_sn: sn,
  2184. T_State: 2, //1-未使用 2-已使用 3-已作废
  2185. T_type: "4G",
  2186. }
  2187. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  2188. if err != nil {
  2189. o.Rollback()
  2190. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  2191. c.ServeJSON()
  2192. return
  2193. }
  2194. }
  2195. StockOutProduct := Stock.StockOutProduct{
  2196. T_number: T_number,
  2197. T_product_id: v.T_product_id,
  2198. T_depot_id: StockOut.T_depot_id,
  2199. T_num: v.T_num, // 入库数量
  2200. T_date: StockOut.T_date, // 入库日期
  2201. T_relation_sn: strings.Join(v.T_relation_sn, ","),
  2202. T_state: 1,
  2203. }
  2204. _, err = StockOutProductDao.Add_StockOutProduct(StockOutProduct)
  2205. if err != nil {
  2206. o.Rollback()
  2207. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  2208. c.ServeJSON()
  2209. return
  2210. }
  2211. // 减少库存
  2212. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  2213. if err != nil {
  2214. o.Rollback()
  2215. c.Data["json"] = lib.JSONS{Code: 203, Msg: "增加库存失败"}
  2216. c.ServeJSON()
  2217. return
  2218. }
  2219. }
  2220. }
  2221. if len(needEdit) > 0 {
  2222. for _, v := range needEdit {
  2223. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2224. StockOutProduct := Stock.StockOutProduct{
  2225. T_number: StockOut.T_number,
  2226. T_product_id: v.T_product_id,
  2227. T_depot_id: StockOut.T_depot_id,
  2228. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  2229. T_date: StockOut.T_date, // 入库日期
  2230. T_state: 1,
  2231. }
  2232. // 出库数量比之前多,减少库存
  2233. var T_type int
  2234. var T_num int
  2235. if productNewMap[v.T_product_id].T_num > productOldMap[v.T_product_id].T_num {
  2236. // 减少库存
  2237. T_type = 1
  2238. T_num = productNewMap[v.T_product_id].T_num - productOldMap[v.T_product_id].T_num
  2239. } else {
  2240. // 增加库存
  2241. T_type = 2
  2242. T_num = productOldMap[v.T_product_id].T_num - productNewMap[v.T_product_id].T_num
  2243. }
  2244. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, T_num, T_type)
  2245. if err != nil {
  2246. o.Rollback()
  2247. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  2248. c.ServeJSON()
  2249. return
  2250. }
  2251. // 更新产品库存表
  2252. err = StockOutProductDao.Update_StockOutProduct(StockOutProduct)
  2253. if err != nil {
  2254. o.Rollback()
  2255. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  2256. c.ServeJSON()
  2257. return
  2258. }
  2259. }
  2260. }
  2261. for _, diff := range snDiff {
  2262. product, _ := Basic.Read_Product_ById(diff.T_product_id)
  2263. // 删除减少的sn
  2264. if len(diff.T_delete_relation_sn) > 0 {
  2265. for _, sn := range diff.T_delete_relation_sn {
  2266. err = DeviceDao.Delete_Device_ByT_out_number(StockOut.T_number, sn)
  2267. if err != nil {
  2268. o.Rollback()
  2269. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  2270. c.ServeJSON()
  2271. return
  2272. }
  2273. }
  2274. // 增加库存
  2275. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, len(diff.T_delete_relation_sn), 2)
  2276. if err != nil {
  2277. o.Rollback()
  2278. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  2279. c.ServeJSON()
  2280. return
  2281. }
  2282. }
  2283. if len(diff.T_add_relation_sn) > 0 {
  2284. for _, sn := range diff.T_add_relation_sn {
  2285. mqtt := Stock.Read_MqttUser(sn)
  2286. // 添加设备
  2287. device := Stock.Device{
  2288. T_contract_number: "",
  2289. T_out_number: StockOut.T_number,
  2290. T_product_id: diff.T_product_id,
  2291. T_in_number: "",
  2292. T_sn: sn,
  2293. T_iccid: mqtt.Iccid,
  2294. T_imei: mqtt.Imei,
  2295. T_State: 1,
  2296. CreateTime: date,
  2297. UpdateTime: StockOut.UpdateTime,
  2298. }
  2299. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  2300. if err != nil {
  2301. o.Rollback()
  2302. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  2303. c.ServeJSON()
  2304. return
  2305. }
  2306. // 添加物联网卡
  2307. iotCard := Property.IotCard{
  2308. T_iccid: mqtt.Iccid,
  2309. T_sn: sn,
  2310. T_State: 2, //1-未使用 2-已使用 3-已作废
  2311. T_type: "4G",
  2312. }
  2313. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  2314. if err != nil {
  2315. o.Rollback()
  2316. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  2317. c.ServeJSON()
  2318. return
  2319. }
  2320. }
  2321. // 减少库存
  2322. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, len(diff.T_add_relation_sn), 1)
  2323. if err != nil {
  2324. o.Rollback()
  2325. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  2326. c.ServeJSON()
  2327. return
  2328. }
  2329. }
  2330. StockOutProduct := Stock.StockOutProduct{
  2331. T_number: StockOut.T_number,
  2332. T_product_id: diff.T_product_id,
  2333. T_depot_id: StockOut.T_depot_id,
  2334. T_num: productNewMap[diff.T_product_id].T_num, // 入库数量
  2335. T_date: StockOut.T_date, // 入库日期
  2336. T_relation_sn: strings.Join(productNewMap[diff.T_product_id].T_relation_sn, ","),
  2337. T_state: 1,
  2338. }
  2339. // 更新产品库存表
  2340. err = StockOutProductDao.Update_StockOutProduct(StockOutProduct)
  2341. if err != nil {
  2342. o.Rollback()
  2343. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  2344. c.ServeJSON()
  2345. return
  2346. }
  2347. }
  2348. }
  2349. if len(T_remark) > 0 {
  2350. StockOut.T_remark = T_remark
  2351. }
  2352. if len(T_receive) > 0 {
  2353. StockOut.T_receive = T_receive
  2354. }
  2355. if len(T_project) > 0 {
  2356. StockOut.T_project = T_project
  2357. }
  2358. if len(T_date) > 0 {
  2359. StockOut.T_date = T_date
  2360. }
  2361. err = StockOutDao.Update_StockOut(StockOut, "T_remark", "T_receive", "T_project", "T_date")
  2362. if err != nil {
  2363. o.Rollback()
  2364. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  2365. c.ServeJSON()
  2366. return
  2367. }
  2368. o.Commit()
  2369. if len(T_date) > 0 && T_old_date != T_date {
  2370. // 修改出库产品日期
  2371. StockOutProductDao.Update_StockOutProduct_T_date(StockOut.T_number, T_date)
  2372. DeviceDao.Update_Device_CreateTimeByT_out_number(StockOut.T_number, T_date)
  2373. }
  2374. if len(T_project) > 0 {
  2375. DeviceDao.Update_Device_ByT_out_number_T_project(StockOut.T_number, T_project)
  2376. }
  2377. // 更新月份统计表
  2378. allProductList := []int{}
  2379. for i, _ := range allProductListMap {
  2380. allProductList = append(allProductList, i)
  2381. }
  2382. StockOut_Edit_StockMonth(StockOut.T_date, StockOut.T_depot_id, allProductList)
  2383. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "修改", StockOut)
  2384. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2385. c.ServeJSON()
  2386. return
  2387. }
  2388. // 删除出库
  2389. func (c *StockController) StockOut_Del() {
  2390. T_number := c.GetString("T_number") // 入库单号
  2391. o := orm.NewOrm()
  2392. o.Begin()
  2393. StockOutDao := Stock.NewStockOut(o)
  2394. StockOutProductDao := Stock.NewStockOutProduct(o)
  2395. StockDao := Stock.NewStock(o)
  2396. DeviceDao := Stock.NewDevice(o)
  2397. // 查询出库信息
  2398. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2399. if err != nil {
  2400. o.Rollback()
  2401. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2402. c.ServeJSON()
  2403. return
  2404. }
  2405. // 查询出库产品信息
  2406. productOldList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  2407. var oldProductList []dto.StockProduct
  2408. allProductList := []int{}
  2409. for _, product := range productOldList {
  2410. oldProductList = append(oldProductList, dto.StockProduct{
  2411. T_product_id: product.T_product_id,
  2412. T_num: product.T_num,
  2413. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  2414. })
  2415. allProductList = append(allProductList, product.T_product_id)
  2416. }
  2417. // 删除入库产品列表
  2418. for _, v := range oldProductList {
  2419. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2420. // 删除入库产品列表
  2421. err = StockOutProductDao.Delete_StockOutProduct(stockOut.T_number, stockOut.T_depot_id, v.T_product_id)
  2422. if err != nil {
  2423. o.Rollback()
  2424. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品出库信息失败!"}
  2425. c.ServeJSON()
  2426. return
  2427. }
  2428. if product.T_relation_sn == 1 {
  2429. // 删除该入库单关联sn
  2430. for _, sn := range v.T_relation_sn {
  2431. err = DeviceDao.Delete_Device_ByT_out_number(stockOut.T_number, sn)
  2432. if err != nil {
  2433. o.Rollback()
  2434. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除出库设备失败"}
  2435. c.ServeJSON()
  2436. return
  2437. }
  2438. }
  2439. }
  2440. // 增加库存
  2441. _, err = StockDao.AddOrUpdate_Stock(stockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  2442. if err != nil {
  2443. o.Rollback()
  2444. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  2445. c.ServeJSON()
  2446. return
  2447. }
  2448. }
  2449. err = StockOutDao.Delete_StockOut(stockOut)
  2450. if err != nil {
  2451. o.Rollback()
  2452. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除出库失败"}
  2453. c.ServeJSON()
  2454. return
  2455. }
  2456. o.Commit()
  2457. // 更新月份统计表
  2458. StockOut_Edit_StockMonth(stockOut.T_date, stockOut.T_depot_id, allProductList)
  2459. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "删除", stockOut)
  2460. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2461. c.ServeJSON()
  2462. return
  2463. }
  2464. // 导出出库单
  2465. func (c *StockController) StockOut_Excel() {
  2466. // 查询
  2467. T_number := c.GetString("T_number")
  2468. o := orm.NewOrm()
  2469. StockOutDao := Stock.NewStockOut(o)
  2470. StockOutProductDao := Stock.NewStockOutProduct(o)
  2471. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2472. if err != nil {
  2473. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2474. c.ServeJSON()
  2475. return
  2476. }
  2477. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  2478. //productList = append(productList, productList...)
  2479. //productList = append(productList, productList...)
  2480. //productList = append(productList, productList...)
  2481. var pList []Stock.StockOutProduct_R
  2482. for _, v := range productList {
  2483. pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
  2484. }
  2485. userList, _ := NatsServer.Read_User_List_All()
  2486. Account.Read_User_All_Map(userList)
  2487. stockOutDetail := Stock.StockOutToStockOut_Detail(stockOut, pList)
  2488. T_date, _ := lib.DateStrToTime(stockOut.T_date)
  2489. filename := fmt.Sprintf("冷链产品出库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  2490. f := excelize.NewFile() // 设置单元格的值
  2491. Style1, _ := f.NewStyle(
  2492. &excelize.Style{
  2493. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  2494. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  2495. })
  2496. Style2, _ := f.NewStyle(
  2497. &excelize.Style{
  2498. Font: &excelize.Font{Size: 11, Family: "宋体"},
  2499. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  2500. })
  2501. Style3, _ := f.NewStyle(
  2502. &excelize.Style{
  2503. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  2504. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  2505. Border: []excelize.Border{
  2506. {Type: "left", Color: "000000", Style: 1},
  2507. {Type: "top", Color: "000000", Style: 1},
  2508. {Type: "bottom", Color: "000000", Style: 1},
  2509. {Type: "right", Color: "000000", Style: 1},
  2510. },
  2511. })
  2512. Style4, _ := f.NewStyle(
  2513. &excelize.Style{
  2514. Font: &excelize.Font{Size: 11, Family: "宋体"},
  2515. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  2516. Border: []excelize.Border{
  2517. {Type: "left", Color: "000000", Style: 1},
  2518. {Type: "top", Color: "000000", Style: 1},
  2519. {Type: "bottom", Color: "000000", Style: 1},
  2520. {Type: "right", Color: "000000", Style: 1},
  2521. },
  2522. })
  2523. productList2 := lib.ChunkBy(productList, 10)
  2524. for i, products := range productList2 {
  2525. if len(products) < 10 {
  2526. products = append(products, make([]Stock.StockOutProduct, 10-len(products))...)
  2527. }
  2528. row := i*17 + 1
  2529. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2530. f.SetRowStyle("Sheet1", row, row, Style1)
  2531. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  2532. f.SetRowHeight("Sheet1", row, 20)
  2533. row += 1
  2534. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2535. f.SetRowStyle("Sheet1", row, row, Style1)
  2536. title := "硬件出库单"
  2537. if len(productList2) > 1 {
  2538. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  2539. }
  2540. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  2541. f.SetRowHeight("Sheet1", row, 20)
  2542. row += 1
  2543. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2544. f.SetRowStyle("Sheet1", row, row, Style2)
  2545. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("出库单号:%s", stockOut.T_number))
  2546. f.SetRowHeight("Sheet1", row, 20)
  2547. row += 1
  2548. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2549. f.SetRowStyle("Sheet1", row, row, Style2)
  2550. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockOut.T_project))
  2551. f.SetRowHeight("Sheet1", row, 20)
  2552. row += 1
  2553. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2554. f.SetRowStyle("Sheet1", row, row, Style2)
  2555. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("领用时间:%s", T_date.Format("2006年01月02日")))
  2556. f.SetRowHeight("Sheet1", row, 20)
  2557. // 这里设置表头
  2558. row += 1
  2559. f.SetRowHeight("Sheet1", row, 20)
  2560. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  2561. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  2562. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  2563. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  2564. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  2565. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  2566. // 设置列宽
  2567. f.SetColWidth("Sheet1", "A", "A", 5)
  2568. f.SetColWidth("Sheet1", "B", "B", 20)
  2569. f.SetColWidth("Sheet1", "C", "C", 12)
  2570. f.SetColWidth("Sheet1", "D", "D", 10)
  2571. f.SetColWidth("Sheet1", "E", "E", 10)
  2572. f.SetColWidth("Sheet1", "F", "F", 15)
  2573. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  2574. sRow := row
  2575. // 循环写入数据
  2576. for j, v := range products {
  2577. row += 1
  2578. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2579. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  2580. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  2581. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  2582. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  2583. if v.T_num > 0 {
  2584. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  2585. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockOut.T_remark)
  2586. }
  2587. }
  2588. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  2589. row += 1
  2590. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  2591. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  2592. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  2593. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("领用人:%s", stockOutDetail.T_receive_name))
  2594. f.SetRowStyle("Sheet1", row, row, Style2)
  2595. f.SetRowHeight("Sheet1", row, 20)
  2596. }
  2597. // 保存文件
  2598. if err = f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  2599. fmt.Println(err)
  2600. }
  2601. var url string
  2602. //// 上传 OSS
  2603. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  2604. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  2605. if !is {
  2606. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  2607. c.ServeJSON()
  2608. return
  2609. }
  2610. //删除目录
  2611. err = os.Remove("ofile/" + filename + ".xlsx")
  2612. if err != nil {
  2613. logs.Error(lib.FuncName(), err)
  2614. }
  2615. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  2616. c.ServeJSON()
  2617. return
  2618. }
  2619. func (c *StockController) StockOut_Excel_Batch() {
  2620. // 查询
  2621. T_number_list := c.GetString("T_number_list")
  2622. o := orm.NewOrm()
  2623. StockOutDao := Stock.NewStockOut(o)
  2624. StockOutProductDao := Stock.NewStockOutProduct(o)
  2625. list := strings.Split(strings.Trim(T_number_list, "|"), "|")
  2626. userList, _ := NatsServer.Read_User_List_All()
  2627. Account.Read_User_All_Map(userList)
  2628. filename := fmt.Sprintf("冷链产品出库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  2629. f := excelize.NewFile() // 设置单元格的值
  2630. Style1, _ := f.NewStyle(
  2631. &excelize.Style{
  2632. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  2633. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  2634. })
  2635. Style2, _ := f.NewStyle(
  2636. &excelize.Style{
  2637. Font: &excelize.Font{Size: 11, Family: "宋体"},
  2638. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  2639. })
  2640. Style3, _ := f.NewStyle(
  2641. &excelize.Style{
  2642. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  2643. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  2644. Border: []excelize.Border{
  2645. {Type: "left", Color: "000000", Style: 1},
  2646. {Type: "top", Color: "000000", Style: 1},
  2647. {Type: "bottom", Color: "000000", Style: 1},
  2648. {Type: "right", Color: "000000", Style: 1},
  2649. },
  2650. })
  2651. Style4, _ := f.NewStyle(
  2652. &excelize.Style{
  2653. Font: &excelize.Font{Size: 11, Family: "宋体"},
  2654. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  2655. Border: []excelize.Border{
  2656. {Type: "left", Color: "000000", Style: 1},
  2657. {Type: "top", Color: "000000", Style: 1},
  2658. {Type: "bottom", Color: "000000", Style: 1},
  2659. {Type: "right", Color: "000000", Style: 1},
  2660. },
  2661. })
  2662. var num int
  2663. for _, T_number := range list {
  2664. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2665. if err != nil {
  2666. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2667. c.ServeJSON()
  2668. return
  2669. }
  2670. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  2671. var pList []Stock.StockOutProduct_R
  2672. for _, v := range productList {
  2673. pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
  2674. }
  2675. stockOutDetail := Stock.StockOutToStockOut_Detail(stockOut, pList)
  2676. T_date, _ := lib.DateStrToTime(stockOut.T_date)
  2677. productList2 := lib.ChunkBy(productList, 10)
  2678. for i, products := range productList2 {
  2679. if len(products) < 10 {
  2680. products = append(products, make([]Stock.StockOutProduct, 10-len(products))...)
  2681. }
  2682. row := num*18 + 1
  2683. num += 1
  2684. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2685. f.SetRowStyle("Sheet1", row, row, Style1)
  2686. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  2687. f.SetRowHeight("Sheet1", row, 20)
  2688. row += 1
  2689. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2690. f.SetRowStyle("Sheet1", row, row, Style1)
  2691. title := "硬件出库单"
  2692. if len(productList2) > 1 {
  2693. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  2694. }
  2695. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  2696. f.SetRowHeight("Sheet1", row, 20)
  2697. row += 1
  2698. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2699. f.SetRowStyle("Sheet1", row, row, Style2)
  2700. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("出库单号:%s", stockOut.T_number))
  2701. f.SetRowHeight("Sheet1", row, 20)
  2702. row += 1
  2703. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2704. f.SetRowStyle("Sheet1", row, row, Style2)
  2705. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockOut.T_project))
  2706. f.SetRowHeight("Sheet1", row, 20)
  2707. row += 1
  2708. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2709. f.SetRowStyle("Sheet1", row, row, Style2)
  2710. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("领用时间:%s", T_date.Format("2006年01月02日")))
  2711. f.SetRowHeight("Sheet1", row, 20)
  2712. // 这里设置表头
  2713. row += 1
  2714. f.SetRowHeight("Sheet1", row, 20)
  2715. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  2716. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  2717. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  2718. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  2719. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  2720. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  2721. // 设置列宽
  2722. f.SetColWidth("Sheet1", "A", "A", 5)
  2723. f.SetColWidth("Sheet1", "B", "B", 20)
  2724. f.SetColWidth("Sheet1", "C", "C", 12)
  2725. f.SetColWidth("Sheet1", "D", "D", 10)
  2726. f.SetColWidth("Sheet1", "E", "E", 10)
  2727. f.SetColWidth("Sheet1", "F", "F", 15)
  2728. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  2729. sRow := row
  2730. // 循环写入数据
  2731. for j, v := range products {
  2732. row += 1
  2733. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2734. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  2735. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  2736. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  2737. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  2738. if v.T_num > 0 {
  2739. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  2740. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockOut.T_remark)
  2741. }
  2742. }
  2743. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  2744. row += 1
  2745. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  2746. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  2747. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  2748. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("领用人:%s", stockOutDetail.T_receive_name))
  2749. f.SetRowStyle("Sheet1", row, row, Style2)
  2750. f.SetRowHeight("Sheet1", row, 20)
  2751. // 空一行
  2752. row += 1
  2753. //f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2754. //f.SetRowStyle("Sheet1", row, row, Style1)
  2755. //f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "")
  2756. // 插入下一页分页符
  2757. f.InsertPageBreak("Sheet1", fmt.Sprintf("A%d", row))
  2758. }
  2759. }
  2760. // 保存文件
  2761. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  2762. fmt.Println(err)
  2763. }
  2764. var url string
  2765. //// 上传 OSS
  2766. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  2767. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  2768. if !is {
  2769. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  2770. c.ServeJSON()
  2771. return
  2772. }
  2773. //删除目录
  2774. err := os.Remove("ofile/" + filename + ".xlsx")
  2775. if err != nil {
  2776. logs.Error(lib.FuncName(), err)
  2777. }
  2778. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  2779. c.ServeJSON()
  2780. return
  2781. }
  2782. func StockOut_Edit_StockMonth(T_date string, T_depot_id int, allProductList []int) error {
  2783. date, _ := lib.DateStrToTime(T_date)
  2784. //if date.Year() == time.Now().Year() && date.Month() == time.Now().Month() {
  2785. // // 修改本月数据不同步库存
  2786. // return nil
  2787. //}
  2788. //T_month := date.Format("2006-01")
  2789. months := generateMonthList(date, "out")
  2790. o := orm.NewOrm()
  2791. StockOutDao := Stock.NewStockOut(o)
  2792. StockMonthDao := Stock.NewStockMonth(o)
  2793. StockOutProductDao := Stock.NewStockOutProduct(o)
  2794. StockInProductDao := Stock.NewStockInProduct(o)
  2795. for _, T_product_id := range allProductList {
  2796. for _, T_month := range months {
  2797. // 获取当前产品本月出入库数量
  2798. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  2799. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  2800. if stockMonth.Id > 0 {
  2801. // 修改
  2802. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  2803. if len(lastMonth) > 0 {
  2804. stockMonth.T_beginning = lastMonth[0].T_ending
  2805. } else {
  2806. stockMonth.T_beginning = 0
  2807. }
  2808. // 期末库存 = 期初库存+入库-出库
  2809. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  2810. err := StockMonthDao.Update_StockMonth(stockMonth, "T_out", "T_beginning", "T_ending")
  2811. if err != nil {
  2812. return err
  2813. }
  2814. } else {
  2815. product, _ := Basic.Read_Product_ById(T_product_id)
  2816. // 添加库存记录
  2817. stockMonth = Stock.StockMonth{
  2818. T_depot_id: T_depot_id,
  2819. T_product_id: T_product_id,
  2820. T_product_class: product.T_class,
  2821. T_month: T_month,
  2822. }
  2823. // 获取当前产品本月出入库数量
  2824. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  2825. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  2826. if len(lastMonth) > 0 {
  2827. stockMonth.T_beginning = lastMonth[0].T_ending
  2828. } else {
  2829. stockMonth.T_beginning = 0
  2830. }
  2831. // 期末库存 = 期初库存+入库-出库
  2832. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  2833. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(T_depot_id, T_product_id, T_month)
  2834. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  2835. if err != nil {
  2836. return err
  2837. }
  2838. }
  2839. }
  2840. }
  2841. return nil
  2842. }
  2843. func Cron_StockMonth() {
  2844. //创建一个定时任务对象
  2845. c := cron.New(cron.WithSeconds())
  2846. //给对象增加定时任务
  2847. // @monthly 每月运行一次,每月第一天午夜 0 0 0 1 * *
  2848. //c.AddFunc("0 */1 * * * ?", Cron_StockMonth_Add)
  2849. c.AddFunc("@monthly", Cron_StockMonth_Add)
  2850. //启动定时任务
  2851. c.Start()
  2852. defer c.Stop()
  2853. //查询语句,阻塞,让main函数不退出,保持程序运行
  2854. select {}
  2855. }
  2856. // 保存每月入库出库明细
  2857. func Cron_StockMonth_Add() {
  2858. T_month := time.Now().Format("2006-01")
  2859. //T_month := time.Now().AddDate(0, -1, 0).Format("2006-01")
  2860. logs.Info("开始统计" + T_month + "库存明细数据")
  2861. o := orm.NewOrm()
  2862. StockDao := Stock.NewStock(o)
  2863. StockOutDao := Stock.NewStockOut(o)
  2864. StockMonthDao := Stock.NewStockMonth(o)
  2865. StockOutProductDao := Stock.NewStockOutProduct(o)
  2866. StockInProductDao := Stock.NewStockInProduct(o)
  2867. stockList, _ := StockDao.Read_Stock_List(0, 0, "", "", 0, 9999)
  2868. for _, stock := range stockList {
  2869. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(stock.T_depot_id, stock.T_product_id, T_month)
  2870. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(stock.T_depot_id, stock.T_product_id, T_month)
  2871. if stockMonth.Id > 0 {
  2872. // 修改
  2873. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(stock.T_depot_id, stock.T_product_id, T_month)
  2874. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(stock.T_depot_id, stock.T_product_id, T_month)
  2875. if len(lastMonth) > 0 {
  2876. stockMonth.T_beginning = lastMonth[0].T_ending
  2877. } else {
  2878. stockMonth.T_beginning = 0
  2879. }
  2880. // 期末库存 = 期初库存+入库-出库
  2881. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  2882. err := StockMonthDao.Update_StockMonth(stockMonth, "T_in", "T_out", "T_beginning", "T_ending")
  2883. if err != nil {
  2884. NatsServer.AddSysLogs("库存明细统计", fmt.Sprintf("库存明细统计失败%s:%d:%d", T_month, stock.T_depot_id, stock.T_product_id), stockMonth)
  2885. }
  2886. } else {
  2887. stockMonth = Stock.StockMonth{
  2888. T_depot_id: stock.T_depot_id,
  2889. T_product_id: stock.T_product_id,
  2890. T_product_class: stock.T_product_class,
  2891. T_month: T_month,
  2892. }
  2893. // 获取当前产品本月出入库数量
  2894. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(stock.T_depot_id, stock.T_product_id, T_month)
  2895. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(stock.T_depot_id, stock.T_product_id, T_month)
  2896. if len(lastMonth) > 0 {
  2897. stockMonth.T_beginning = lastMonth[0].T_ending
  2898. } else {
  2899. stockMonth.T_beginning = 0
  2900. }
  2901. // 期末库存 = 期初库存+入库-出库
  2902. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  2903. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(stock.T_depot_id, stock.T_product_id, T_month)
  2904. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  2905. if err != nil {
  2906. NatsServer.AddSysLogs("库存明细统计", fmt.Sprintf("库存明细统计失败%s:%d:%d", T_month, stock.T_depot_id, stock.T_product_id), stockMonth)
  2907. }
  2908. }
  2909. }
  2910. }
  2911. func generateMonthList(startMonth time.Time, t_type string) []string {
  2912. var months []string
  2913. endMonth := time.Now()
  2914. if t_type == "in" {
  2915. endMonth = endMonth.AddDate(0, 1, 0)
  2916. }
  2917. for month := startMonth; month.Before(endMonth); month = month.AddDate(0, 1, 0) {
  2918. months = append(months, month.Format("2006-01"))
  2919. }
  2920. return months
  2921. }
  2922. // 我的申请列表
  2923. func (c *StockController) StockOut_Apply_List() {
  2924. // 分页参数 初始化
  2925. page, _ := c.GetInt("page")
  2926. if page < 1 {
  2927. page = 1
  2928. }
  2929. page_z, _ := c.GetInt("page_z")
  2930. if page_z < 1 {
  2931. page_z = conf.Page_size
  2932. }
  2933. // 查询
  2934. T_depot_id, _ := c.GetInt("T_depot_id")
  2935. T_state, _ := c.GetInt("T_state")
  2936. T_contract_number := c.GetString("T_contract_number")
  2937. T_start_date := c.GetString("T_start_date")
  2938. T_end_date := c.GetString("T_end_date")
  2939. T_name := c.GetString("T_name")
  2940. userList, _ := NatsServer.Read_User_List_All()
  2941. Account.Read_User_All_Map(userList)
  2942. Basic.Read_Depot_All_Map()
  2943. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  2944. R_List, R_cnt := StockOutDao.Read_StockOut_Audit_List(c.User.T_uuid, T_name, Stock.StockOutTypeApply, T_state, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  2945. var r_jsons lib.R_JSONS
  2946. r_jsons.Num = R_cnt
  2947. r_jsons.Data = R_List
  2948. r_jsons.Page = page
  2949. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  2950. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  2951. c.ServeJSON()
  2952. return
  2953. }
  2954. // 出库申请
  2955. func (c *StockController) StockOut_Apply() {
  2956. rand_x := 0
  2957. T_number := ""
  2958. o := orm.NewOrm()
  2959. StockOutDao := Stock.NewStockOut(o)
  2960. for true {
  2961. T_number = "CK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
  2962. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2963. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  2964. break
  2965. }
  2966. rand_x += 1
  2967. }
  2968. T_contract_number := c.GetString("T_contract_number")
  2969. T_depot_id, _ := c.GetInt("T_depot_id")
  2970. T_product := c.GetString("T_product")
  2971. T_remark := c.GetString("T_remark")
  2972. T_project := c.GetString("T_project")
  2973. T_company_name := c.GetString("T_company_name")
  2974. T_payment_method := c.GetString("T_payment_method")
  2975. if len(T_company_name) == 0 {
  2976. c.Data["json"] = lib.JSONS{Code: 202, Msg: "公司名称不能为空"}
  2977. c.ServeJSON()
  2978. return
  2979. }
  2980. if len(T_payment_method) == 0 {
  2981. c.Data["json"] = lib.JSONS{Code: 202, Msg: "付款方式不能为空"}
  2982. c.ServeJSON()
  2983. return
  2984. }
  2985. var_ := Stock.StockOut{
  2986. T_number: T_number,
  2987. T_contract_number: T_contract_number,
  2988. T_depot_id: T_depot_id,
  2989. T_type: 1,
  2990. T_application_date: time.Now().Format("2006-01-02"),
  2991. T_receive: c.User.T_uuid,
  2992. T_remark: T_remark,
  2993. T_project: T_project,
  2994. T_submit: c.User.T_uuid,
  2995. T_state: Stock.StockOutWaitAudit,
  2996. T_company_name: T_company_name,
  2997. T_payment_method: T_payment_method,
  2998. }
  2999. o.Begin()
  3000. StockOutProductDao := Stock.NewStockOutProduct(o)
  3001. StockDao := Stock.NewStock(o)
  3002. _, err := StockOutDao.Add_StockOut(var_)
  3003. if err != nil {
  3004. o.Rollback()
  3005. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  3006. c.ServeJSON()
  3007. return
  3008. }
  3009. // 1、添加出库单
  3010. if len(T_product) == 0 {
  3011. o.Rollback()
  3012. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  3013. c.ServeJSON()
  3014. return
  3015. }
  3016. productList := lib.SplitString(T_product, "|")
  3017. allProductList := []int{}
  3018. for _, v := range productList {
  3019. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  3020. allProductList = append(allProductList, product_id)
  3021. product, _ := Basic.Read_Product_ById(product_id)
  3022. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  3023. if num == 0 {
  3024. o.Rollback()
  3025. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s出库数量不能为0!", product.T_name)}
  3026. c.ServeJSON()
  3027. return
  3028. }
  3029. // 2、添加出库产品清单
  3030. stockOutProduct := Stock.StockOutProduct{
  3031. T_number: T_number,
  3032. T_product_id: product_id,
  3033. T_depot_id: T_depot_id,
  3034. T_num: num, // 出库数量
  3035. T_state: 2,
  3036. }
  3037. _, err = StockOutProductDao.Add_StockOutProduct(stockOutProduct)
  3038. if err != nil {
  3039. o.Rollback()
  3040. c.Data["json"] = lib.JSONS{Code: 203, Msg: "申请出库失败"}
  3041. c.ServeJSON()
  3042. return
  3043. }
  3044. // 3、更新产品库存列表
  3045. _, err = StockDao.AddOrUpdate_Occupy_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
  3046. if err != nil {
  3047. o.Rollback()
  3048. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新占用库存失败" + err.Error()}
  3049. c.ServeJSON()
  3050. return
  3051. }
  3052. }
  3053. o.Commit()
  3054. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "出库申请", var_)
  3055. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  3056. c.ServeJSON()
  3057. return
  3058. }
  3059. // 修改出库申请
  3060. func (c *StockController) StockOut_Apply_Edit() {
  3061. T_number := c.GetString("T_number") // 出库单号
  3062. //T_depot_id, _ := c.GetInt("T_depot_id")
  3063. T_project := c.GetString("T_project")
  3064. T_product := c.GetString("T_product")
  3065. T_remark := c.GetString("T_remark")
  3066. T_company_name := c.GetString("T_company_name")
  3067. T_payment_method := c.GetString("T_payment_method")
  3068. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改出库", T_product)
  3069. o := orm.NewOrm()
  3070. o.Begin()
  3071. StockOutDao := Stock.NewStockOut(o)
  3072. StockOutProductDao := Stock.NewStockOutProduct(o)
  3073. StockDao := Stock.NewStock(o)
  3074. // 查询入库信息
  3075. StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3076. if err != nil {
  3077. o.Rollback()
  3078. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  3079. c.ServeJSON()
  3080. return
  3081. }
  3082. // 查询入库产品信息
  3083. productOldList := StockOutProductDao.Read_StockOutProduct_List(StockOut.T_number)
  3084. var oldProductList []dto.StockProduct
  3085. var newProductList []dto.StockProduct
  3086. productOldMap := map[int]dto.StockProduct{}
  3087. allProductListMap := make(map[int]struct{})
  3088. for _, product := range productOldList {
  3089. stockProduct := dto.StockProduct{
  3090. T_product_id: product.T_product_id,
  3091. T_num: product.T_num,
  3092. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  3093. }
  3094. oldProductList = append(oldProductList, stockProduct)
  3095. productOldMap[product.T_product_id] = stockProduct
  3096. if _, ok := allProductListMap[product.T_product_id]; !ok {
  3097. allProductListMap[product.T_product_id] = struct{}{}
  3098. }
  3099. if StockOut.T_state != Stock.StockOutAuditManagerUnPass && StockOut.T_state != Stock.StockOutAuditFinanceUnPass {
  3100. // 减少占用库存
  3101. product2, _ := Basic.Read_Product_ById(product.T_product_id)
  3102. _, err = StockDao.AddOrUpdate_Occupy_Stock(StockOut.T_depot_id, product2.Id, product2.T_class, product2.T_name, product2.T_model, product.T_num, 1)
  3103. if err != nil {
  3104. o.Rollback()
  3105. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新占用库存失败"}
  3106. c.ServeJSON()
  3107. return
  3108. }
  3109. }
  3110. }
  3111. productNewList := lib.SplitString(T_product, "|")
  3112. productNewMap := map[int]dto.StockProduct{}
  3113. for _, v := range productNewList {
  3114. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  3115. product, _ := Basic.Read_Product_ById(product_id)
  3116. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  3117. if num == 0 {
  3118. o.Rollback()
  3119. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s出库数量不能为0!", product.T_name)}
  3120. c.ServeJSON()
  3121. return
  3122. }
  3123. T_relation_sn := strings.Split(v, "-")[2]
  3124. stockProduct := dto.StockProduct{
  3125. T_product_id: product_id,
  3126. T_num: num,
  3127. T_relation_sn: lib.SplitString(T_relation_sn, ","),
  3128. }
  3129. newProductList = append(newProductList, stockProduct)
  3130. productNewMap[product_id] = stockProduct
  3131. if _, ok := allProductListMap[product_id]; !ok {
  3132. allProductListMap[product_id] = struct{}{}
  3133. }
  3134. // 增加占用库存
  3135. _, err = StockDao.AddOrUpdate_Occupy_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
  3136. if err != nil {
  3137. o.Rollback()
  3138. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新占用库存失败"}
  3139. c.ServeJSON()
  3140. return
  3141. }
  3142. }
  3143. // 判断产品列表信息是否相同
  3144. StockProductListIsEqual, needDelete, needAdd, needEdit, _ := dto.StockProductListsEqual(oldProductList, newProductList)
  3145. // 两次提交的数据相同,则不用修改
  3146. if !StockProductListIsEqual {
  3147. if len(needDelete) > 0 {
  3148. // 删除入库产品列表
  3149. for _, v := range needDelete {
  3150. // 删除入库产品列表
  3151. err = StockOutProductDao.Delete_StockOutProduct(StockOut.T_number, StockOut.T_depot_id, v.T_product_id)
  3152. if err != nil {
  3153. o.Rollback()
  3154. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  3155. c.ServeJSON()
  3156. return
  3157. }
  3158. }
  3159. }
  3160. if len(needAdd) > 0 {
  3161. // 新增入库产品列表
  3162. for _, v := range needAdd {
  3163. StockOutProduct := Stock.StockOutProduct{
  3164. T_number: T_number,
  3165. T_product_id: v.T_product_id,
  3166. T_depot_id: StockOut.T_depot_id,
  3167. T_num: v.T_num, // 出库数量
  3168. T_state: 1,
  3169. }
  3170. _, err = StockOutProductDao.Add_StockOutProduct(StockOutProduct)
  3171. if err != nil {
  3172. o.Rollback()
  3173. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  3174. c.ServeJSON()
  3175. return
  3176. }
  3177. }
  3178. }
  3179. if len(needEdit) > 0 {
  3180. for _, v := range needEdit {
  3181. StockOutProduct := Stock.StockOutProduct{
  3182. T_number: StockOut.T_number,
  3183. T_product_id: v.T_product_id,
  3184. T_depot_id: StockOut.T_depot_id,
  3185. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  3186. T_state: 1,
  3187. }
  3188. // 更新产品表
  3189. err = StockOutProductDao.Update_StockOutProduct(StockOutProduct)
  3190. if err != nil {
  3191. o.Rollback()
  3192. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  3193. c.ServeJSON()
  3194. return
  3195. }
  3196. }
  3197. }
  3198. }
  3199. if len(T_remark) > 0 {
  3200. StockOut.T_remark = T_remark
  3201. }
  3202. if len(T_project) > 0 {
  3203. StockOut.T_project = T_project
  3204. }
  3205. if len(T_company_name) > 0 {
  3206. StockOut.T_company_name = T_company_name
  3207. }
  3208. if len(T_payment_method) > 0 {
  3209. StockOut.T_payment_method = T_payment_method
  3210. }
  3211. StockOut.T_state = Stock.StockOutWaitAudit
  3212. err = StockOutDao.Update_StockOut(StockOut, "T_remark", "T_receive", "T_project", "T_company_name", "T_payment_method", "T_state")
  3213. if err != nil {
  3214. o.Rollback()
  3215. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  3216. c.ServeJSON()
  3217. return
  3218. }
  3219. o.Commit()
  3220. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "修改", StockOut)
  3221. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  3222. c.ServeJSON()
  3223. return
  3224. }
  3225. // 删除出库申请
  3226. func (c *StockController) StockOut_Apply_Del() {
  3227. T_number := c.GetString("T_number") // 入库单号
  3228. o := orm.NewOrm()
  3229. o.Begin()
  3230. StockOutDao := Stock.NewStockOut(o)
  3231. StockOutProductDao := Stock.NewStockOutProduct(o)
  3232. StockDao := Stock.NewStock(o)
  3233. // 查询出库信息
  3234. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3235. if err != nil {
  3236. o.Rollback()
  3237. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  3238. c.ServeJSON()
  3239. return
  3240. }
  3241. // 查询出库产品信息
  3242. productOldList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  3243. var oldProductList []dto.StockProduct
  3244. allProductList := []int{}
  3245. for _, product := range productOldList {
  3246. oldProductList = append(oldProductList, dto.StockProduct{
  3247. T_product_id: product.T_product_id,
  3248. T_num: product.T_num,
  3249. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  3250. })
  3251. allProductList = append(allProductList, product.T_product_id)
  3252. }
  3253. // 删除入库产品列表
  3254. for _, v := range oldProductList {
  3255. product, _ := Basic.Read_Product_ById(v.T_product_id)
  3256. // 删除入库产品列表
  3257. err = StockOutProductDao.Delete_StockOutProduct(stockOut.T_number, stockOut.T_depot_id, v.T_product_id)
  3258. if err != nil {
  3259. o.Rollback()
  3260. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品出库信息失败!"}
  3261. c.ServeJSON()
  3262. return
  3263. }
  3264. // 减少占用库存
  3265. _, err = StockDao.AddOrUpdate_Occupy_Stock(stockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  3266. if err != nil {
  3267. o.Rollback()
  3268. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新占用库存失败"}
  3269. c.ServeJSON()
  3270. return
  3271. }
  3272. }
  3273. err = StockOutDao.Delete_StockOut(stockOut)
  3274. if err != nil {
  3275. o.Rollback()
  3276. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除出库失败"}
  3277. c.ServeJSON()
  3278. return
  3279. }
  3280. o.Commit()
  3281. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "删除", stockOut)
  3282. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  3283. c.ServeJSON()
  3284. return
  3285. }
  3286. // 财务
  3287. func (c *StockController) StockOut_Finance_List() {
  3288. // 分页参数 初始化
  3289. page, _ := c.GetInt("page")
  3290. if page < 1 {
  3291. page = 1
  3292. }
  3293. page_z, _ := c.GetInt("page_z")
  3294. if page_z < 1 {
  3295. page_z = conf.Page_size
  3296. }
  3297. // 查询
  3298. T_depot_id, _ := c.GetInt("T_depot_id")
  3299. T_state, _ := c.GetInt("T_state")
  3300. T_contract_number := c.GetString("T_contract_number")
  3301. T_start_date := c.GetString("T_start_date")
  3302. T_end_date := c.GetString("T_end_date")
  3303. T_name := c.GetString("T_name")
  3304. userList, _ := NatsServer.Read_User_List_All()
  3305. Account.Read_User_All_Map(userList)
  3306. Basic.Read_Depot_All_Map()
  3307. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  3308. R_List, R_cnt := StockOutDao.Read_StockOut_Audit_List("", T_name, Stock.StockOutTypeFinance, T_state, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  3309. var r_jsons lib.R_JSONS
  3310. r_jsons.Num = R_cnt
  3311. r_jsons.Data = R_List
  3312. r_jsons.Page = page
  3313. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  3314. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  3315. c.ServeJSON()
  3316. return
  3317. }
  3318. // 管理员
  3319. func (c *StockController) StockOut_Manager_List() {
  3320. // 分页参数 初始化
  3321. page, _ := c.GetInt("page")
  3322. if page < 1 {
  3323. page = 1
  3324. }
  3325. page_z, _ := c.GetInt("page_z")
  3326. if page_z < 1 {
  3327. page_z = conf.Page_size
  3328. }
  3329. // 查询
  3330. T_depot_id, _ := c.GetInt("T_depot_id")
  3331. T_state, _ := c.GetInt("T_state")
  3332. T_contract_number := c.GetString("T_contract_number")
  3333. T_start_date := c.GetString("T_start_date")
  3334. T_end_date := c.GetString("T_end_date")
  3335. T_name := c.GetString("T_name")
  3336. userList, _ := NatsServer.Read_User_List_All()
  3337. Account.Read_User_All_Map(userList)
  3338. Basic.Read_Depot_All_Map()
  3339. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  3340. R_List, R_cnt := StockOutDao.Read_StockOut_Audit_List("", T_name, Stock.StockOutTypeManager, T_state, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  3341. var r_jsons lib.R_JSONS
  3342. r_jsons.Num = R_cnt
  3343. r_jsons.Data = R_List
  3344. r_jsons.Page = page
  3345. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  3346. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  3347. c.ServeJSON()
  3348. return
  3349. }
  3350. // 仓管
  3351. func (c *StockController) StockOut_Warehouse_List() {
  3352. // 分页参数 初始化
  3353. page, _ := c.GetInt("page")
  3354. if page < 1 {
  3355. page = 1
  3356. }
  3357. page_z, _ := c.GetInt("page_z")
  3358. if page_z < 1 {
  3359. page_z = conf.Page_size
  3360. }
  3361. // 查询
  3362. T_depot_id, _ := c.GetInt("T_depot_id")
  3363. T_state, _ := c.GetInt("T_state")
  3364. T_contract_number := c.GetString("T_contract_number")
  3365. T_start_date := c.GetString("T_start_date")
  3366. T_end_date := c.GetString("T_end_date")
  3367. T_name := c.GetString("T_name")
  3368. userList, _ := NatsServer.Read_User_List_All()
  3369. Account.Read_User_All_Map(userList)
  3370. Basic.Read_Depot_All_Map()
  3371. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  3372. R_List, R_cnt := StockOutDao.Read_StockOut_Audit_List(c.User.T_uuid, T_name, Stock.StockOutTypeWarehouse, T_state, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  3373. var r_jsons lib.R_JSONS
  3374. r_jsons.Num = R_cnt
  3375. r_jsons.Data = R_List
  3376. r_jsons.Page = page
  3377. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  3378. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  3379. c.ServeJSON()
  3380. return
  3381. }
  3382. // 财务/总经理审核
  3383. func (c *StockController) StockOut_Audit() {
  3384. T_number := c.GetString("T_number") // 出库单号
  3385. //T_depot_id, _ := c.GetInt("T_depot_id")
  3386. T_audit, _ := c.GetInt("T_audit") // 审核 2财务通过 3财务不通过 4总经理通过 5总经理不通过 6已出库
  3387. T_approval_opinion := c.GetString("T_approval_opinion") // 审批意见
  3388. T_approval_img := c.GetString("T_approval_img") // 审批意见
  3389. T_type := c.GetString("T_type") // 类型 Finance Manager
  3390. o := orm.NewOrm()
  3391. o.Begin()
  3392. StockOutDao := Stock.NewStockOut(o)
  3393. StockDao := Stock.NewStock(o)
  3394. StockOutProductDao := Stock.NewStockOutProduct(o)
  3395. StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3396. if err != nil {
  3397. o.Rollback()
  3398. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  3399. c.ServeJSON()
  3400. return
  3401. }
  3402. StockOut.T_state = T_audit
  3403. if T_type == Stock.StockOutTypeFinance {
  3404. StockOut.T_finance_approval_opinion = T_approval_opinion
  3405. StockOut.T_finance_approval_img = T_approval_img
  3406. } else {
  3407. StockOut.T_manager_approval_opinion = T_approval_opinion
  3408. StockOut.T_manager_approval_img = T_approval_img
  3409. }
  3410. if T_audit == Stock.StockOutAuditFinanceUnPass || T_audit == Stock.StockOutAuditManagerUnPass {
  3411. productList := StockOutProductDao.Read_StockOutProduct_List(StockOut.T_number)
  3412. for _, v := range productList {
  3413. product, _ := Basic.Read_Product_ById(v.T_product_id)
  3414. _, err = StockDao.AddOrUpdate_Occupy_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  3415. if err != nil {
  3416. o.Rollback()
  3417. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  3418. c.ServeJSON()
  3419. return
  3420. }
  3421. }
  3422. }
  3423. err = StockOutDao.Update_StockOut(StockOut, "T_state", "T_finance_approval_opinion", "T_manager_approval_opinion", "T_finance_approval_img", "T_manager_approval_img")
  3424. if err != nil {
  3425. o.Rollback()
  3426. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库审批失败"}
  3427. c.ServeJSON()
  3428. return
  3429. }
  3430. o.Commit()
  3431. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "审批", StockOut)
  3432. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  3433. c.ServeJSON()
  3434. return
  3435. }
  3436. // 仓管出库
  3437. func (c *StockController) StockOut_Warehouse() {
  3438. T_number := c.GetString("T_number") // 出库单号
  3439. T_date := c.GetString("T_date")
  3440. date, is := lib.DateStrToTime(T_date)
  3441. if !is {
  3442. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  3443. c.ServeJSON()
  3444. return
  3445. }
  3446. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "确认出库", T_number)
  3447. o := orm.NewOrm()
  3448. o.Begin()
  3449. StockOutDao := Stock.NewStockOut(o)
  3450. StockOutProductDao := Stock.NewStockOutProduct(o)
  3451. DeviceDao := Stock.NewDevice(o)
  3452. StockDao := Stock.NewStock(o)
  3453. // 查询入库信息
  3454. StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3455. if err != nil {
  3456. o.Rollback()
  3457. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  3458. c.ServeJSON()
  3459. return
  3460. }
  3461. T_product := c.GetString("T_product")
  3462. productList := lib.SplitString(T_product, "|")
  3463. allProductList := []int{}
  3464. for _, v := range productList {
  3465. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  3466. allProductList = append(allProductList, product_id)
  3467. product, _ := Basic.Read_Product_ById(product_id)
  3468. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  3469. if num == 0 {
  3470. o.Rollback()
  3471. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s出库数量不能为0!", product.T_name)}
  3472. c.ServeJSON()
  3473. return
  3474. }
  3475. T_relation_sn := strings.Split(v, "-")[2]
  3476. if T_relation_sn == "" && product.T_relation_sn == 1 {
  3477. o.Rollback()
  3478. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  3479. c.ServeJSON()
  3480. return
  3481. }
  3482. snList := strings.Split(T_relation_sn, ",")
  3483. if product.T_relation_sn == 1 && num != len(snList) {
  3484. o.Rollback()
  3485. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s SN数量与出库数量不一致,请核查!", product.T_name)}
  3486. c.ServeJSON()
  3487. return
  3488. }
  3489. // 2、更新设备状态为已出库
  3490. if len(snList) > 0 {
  3491. for _, sn := range snList {
  3492. mqtt := Stock.Read_MqttUser(sn)
  3493. device := Stock.Device{
  3494. T_contract_number: StockOut.T_contract_number,
  3495. T_product_id: product_id,
  3496. T_out_number: T_number,
  3497. T_sn: sn,
  3498. T_iccid: mqtt.Iccid,
  3499. T_imei: mqtt.Imei,
  3500. T_State: 1,
  3501. T_project: StockOut.T_project,
  3502. CreateTime: date,
  3503. }
  3504. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  3505. if err != nil {
  3506. o.Rollback()
  3507. c.Data["json"] = lib.JSONS{Code: 202, Msg: "出库失败"}
  3508. c.ServeJSON()
  3509. return
  3510. }
  3511. }
  3512. }
  3513. // 3、添加出库产品清单
  3514. stockOutProduct := Stock.StockOutProduct{
  3515. T_number: T_number,
  3516. T_product_id: product_id,
  3517. T_depot_id: StockOut.T_depot_id,
  3518. T_num: num, // 出库数量
  3519. T_date: T_date, // 出库数量
  3520. T_relation_sn: T_relation_sn,
  3521. T_state: 1,
  3522. }
  3523. // 更新产品表
  3524. err = StockOutProductDao.Update_StockOutProduct(stockOutProduct)
  3525. if err != nil {
  3526. o.Rollback()
  3527. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  3528. c.ServeJSON()
  3529. return
  3530. }
  3531. // 5、更新产品库存列表、占用库存
  3532. _, err = StockDao.Stock_Warehouse(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num)
  3533. if err != nil {
  3534. o.Rollback()
  3535. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  3536. c.ServeJSON()
  3537. return
  3538. }
  3539. }
  3540. StockOut.T_warehouse = c.User.T_uuid
  3541. StockOut.T_state = Stock.StockOutAlreadyOut
  3542. StockOut.T_date = T_date
  3543. err = StockOutDao.Update_StockOut(StockOut, "T_state", "T_date", "T_warehouse")
  3544. if err != nil {
  3545. o.Rollback()
  3546. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库审批失败"}
  3547. c.ServeJSON()
  3548. return
  3549. }
  3550. o.Commit()
  3551. StockOut_Edit_StockMonth(StockOut.T_date, StockOut.T_depot_id, allProductList)
  3552. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "出库", StockOut)
  3553. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  3554. c.ServeJSON()
  3555. return
  3556. }