Stock.go 101 KB

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