Stock.go 79 KB

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