Stock.go 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. package controllers
  2. import (
  3. "ERP_storage/Nats"
  4. "ERP_storage/Nats/NatsServer"
  5. "ERP_storage/conf"
  6. "ERP_storage/logs"
  7. "ERP_storage/models/Account"
  8. "ERP_storage/models/Basic"
  9. "ERP_storage/models/Contract"
  10. "ERP_storage/models/Property"
  11. "ERP_storage/models/Stock"
  12. "fmt"
  13. natslibs "git.baozhida.cn/ERP_libs/Nats"
  14. userlibs "git.baozhida.cn/ERP_libs/User"
  15. "git.baozhida.cn/ERP_libs/lib"
  16. "github.com/beego/beego/v2/adapter/orm"
  17. beego "github.com/beego/beego/v2/server/web"
  18. "github.com/robfig/cron/v3"
  19. "github.com/xuri/excelize/v2"
  20. "math"
  21. "os"
  22. "strconv"
  23. "strings"
  24. "time"
  25. )
  26. type StockController struct {
  27. beego.Controller
  28. User userlibs.User
  29. }
  30. func (c *StockController) Prepare() {
  31. c.User = *Account.User_r
  32. }
  33. func (c *StockController) Device_List() {
  34. // 分页参数 初始化
  35. page, _ := c.GetInt("page")
  36. if page < 1 {
  37. page = 1
  38. }
  39. page_z, _ := c.GetInt("page_z")
  40. if page_z < 1 {
  41. page_z = conf.Page_size
  42. }
  43. T_state, _ := c.GetInt("T_state")
  44. T_name := c.GetString("T_name")
  45. DeviceDao := Stock.NewDevice(orm.NewOrm())
  46. R_List, R_cnt := DeviceDao.Read_Device_List(T_name, T_state, page, page_z)
  47. var r_jsons lib.R_JSONS
  48. r_jsons.Num = R_cnt
  49. r_jsons.Data = R_List
  50. r_jsons.Page = page
  51. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  52. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  53. c.ServeJSON()
  54. return
  55. }
  56. func (c *StockController) Device_Check() {
  57. T_sn := c.GetString("T_sn")
  58. T_type, _ := c.GetInt("T_type") //1-出库 2-入库
  59. DeviceDao := Stock.NewDevice(orm.NewOrm())
  60. device, err := DeviceDao.Read_Device_ByT_sn(T_sn)
  61. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  62. c.Data["json"] = lib.JSONS{Code: 202, Msg: "请稍后重试!"}
  63. c.ServeJSON()
  64. return
  65. }
  66. // T_State 1-已出库 2-未出库/入库
  67. if len(device.T_in_number) > 0 && device.T_State == 2 && T_type == 2 {
  68. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已入库,请勿重复提交!", T_sn)}
  69. c.ServeJSON()
  70. return
  71. }
  72. if T_type == 2 {
  73. mqtt := Stock.Read_MqttUser(T_sn)
  74. if len(mqtt.Username) == 0 {
  75. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】未授权,请先授权!", T_sn)}
  76. c.ServeJSON()
  77. return
  78. }
  79. }
  80. if len(device.T_out_number) > 0 && device.T_State == 1 && T_type == 1 {
  81. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已出库,请勿重复提交!", T_sn)}
  82. c.ServeJSON()
  83. return
  84. }
  85. if T_type == 1 {
  86. if device.Id == 0 {
  87. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】未入库,请先入库!", T_sn)}
  88. c.ServeJSON()
  89. return
  90. }
  91. }
  92. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  93. c.ServeJSON()
  94. return
  95. }
  96. func (c *StockController) Stock_List() {
  97. // 分页参数 初始化
  98. page, _ := c.GetInt("page")
  99. if page < 1 {
  100. page = 1
  101. }
  102. page_z, _ := c.GetInt("page_z")
  103. if page_z < 1 {
  104. page_z = conf.Page_size
  105. }
  106. // 查询
  107. T_depot_id, _ := c.GetInt("T_depot_id")
  108. // 查询
  109. T_product_name := c.GetString("T_product_name")
  110. T_product_model := c.GetString("T_product_model")
  111. T_product_class, _ := c.GetInt("T_product_class")
  112. userList, _ := NatsServer.Read_User_List_All()
  113. Account.Read_User_All_Map(userList)
  114. Basic.Read_Depot_All_Map()
  115. StockDao := Stock.NewStock(orm.NewOrm())
  116. R_List, R_cnt := StockDao.Read_Stock_List(T_depot_id, T_product_class, T_product_name, T_product_model, page, page_z)
  117. var r_jsons lib.R_JSONS
  118. r_jsons.Num = R_cnt
  119. r_jsons.Data = R_List
  120. r_jsons.Page = page
  121. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  122. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  123. c.ServeJSON()
  124. return
  125. }
  126. func (c *StockController) Stock_Detail_List() {
  127. // 查询
  128. T_depot_id, _ := c.GetInt("T_depot_id")
  129. // 查询
  130. T_product_id, _ := c.GetInt("T_product_id")
  131. T_start_date := c.GetString("T_start_date")
  132. T_end_date := c.GetString("T_end_date")
  133. now := time.Now()
  134. if len(T_start_date) == 0 {
  135. T_start_date = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local).Format("2006-01")
  136. }
  137. if len(T_end_date) == 0 {
  138. T_start_date = now.Format("2006-01")
  139. }
  140. StockMonthDao := Stock.NewStockMonth(orm.NewOrm())
  141. R_List := StockMonthDao.Read_StockMonth_List(T_depot_id, T_product_id, 0, T_start_date, T_end_date)
  142. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: R_List}
  143. c.ServeJSON()
  144. return
  145. }
  146. func (c *StockController) Stock_Detail_Excel() {
  147. // 查询
  148. T_depot_id, _ := c.GetInt("T_depot_id")
  149. T_product_id, _ := c.GetInt("T_product_id")
  150. // 查询
  151. T_start_date := c.GetString("T_start_date")
  152. T_end_date := c.GetString("T_end_date")
  153. now := time.Now()
  154. if len(T_start_date) == 0 {
  155. T_start_date = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local).Format("2006-01")
  156. }
  157. if len(T_end_date) == 0 {
  158. T_start_date = now.Format("2006-01")
  159. }
  160. var class_List []Basic.ProductClass_R
  161. filename := fmt.Sprintf("进销存(%s)", lib.GetRandstring(6, "0123456789", 0))
  162. if T_product_id > 0 {
  163. product, _ := Basic.Read_Product_ById(T_product_id)
  164. filename = fmt.Sprintf("进销存-%s明细(%s)", product.T_name, lib.GetRandstring(6, "0123456789", 0))
  165. class, _ := Basic.Read_ProductClass_ById(product.T_class)
  166. class_List = append(class_List, Basic.ProductClassToProductClass_R(class))
  167. } else {
  168. class_List, _ = Basic.Read_ProductClass_List("", 0, 9999)
  169. }
  170. f := excelize.NewFile() // 设置单元格的值
  171. Style1, _ := f.NewStyle(
  172. &excelize.Style{
  173. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  174. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  175. })
  176. Style2, _ := f.NewStyle(
  177. &excelize.Style{
  178. Font: &excelize.Font{Bold: true, Size: 10, Family: "宋体"},
  179. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  180. Border: []excelize.Border{
  181. {Type: "left", Color: "000000", Style: 1},
  182. {Type: "top", Color: "000000", Style: 1},
  183. {Type: "bottom", Color: "000000", Style: 1},
  184. {Type: "right", Color: "000000", Style: 1},
  185. },
  186. })
  187. StockMonthDao := Stock.NewStockMonth(orm.NewOrm())
  188. for i, r := range class_List {
  189. StockList := StockMonthDao.Read_StockMonth_List(T_depot_id, T_product_id, r.Id, T_start_date, T_end_date)
  190. if len(StockList) == 0 {
  191. continue
  192. }
  193. if i == 0 {
  194. f.SetSheetName("Sheet1", r.T_name)
  195. } else {
  196. f.NewSheet(r.T_name)
  197. }
  198. f.MergeCell(r.T_name, "A1", "K1")
  199. f.SetRowStyle(r.T_name, 1, 1, Style1)
  200. f.SetCellValue(r.T_name, "A1", fmt.Sprintf("宝智达科技产品进销存统计表"))
  201. f.SetRowHeight(r.T_name, 1, 30)
  202. // 这里设置表头
  203. f.SetCellStyle(r.T_name, "A2", "K2", Style2)
  204. f.SetRowHeight(r.T_name, 2, 20)
  205. f.SetCellValue(r.T_name, "A2", "序号")
  206. f.SetCellValue(r.T_name, "B2", "产品名称")
  207. f.SetCellValue(r.T_name, "C2", "型号")
  208. f.SetCellValue(r.T_name, "D2", "规格")
  209. f.SetCellValue(r.T_name, "E2", "月份")
  210. f.SetCellValue(r.T_name, "F2", "期初库存")
  211. f.SetCellValue(r.T_name, "G2", "当月入库")
  212. f.SetCellValue(r.T_name, "H2", "当月出库")
  213. f.SetCellValue(r.T_name, "I2", "期末库存")
  214. f.SetCellValue(r.T_name, "J2", "出库项目")
  215. f.SetCellValue(r.T_name, "K2", "备注")
  216. // 设置列宽
  217. f.SetColWidth(r.T_name, "A", "A", 10)
  218. f.SetColWidth(r.T_name, "B", "B", 10)
  219. f.SetColWidth(r.T_name, "C", "D", 10)
  220. f.SetColWidth(r.T_name, "D", "D", 10)
  221. f.SetColWidth(r.T_name, "E", "E", 10)
  222. f.SetColWidth(r.T_name, "F", "F", 10)
  223. f.SetColWidth(r.T_name, "G", "G", 10)
  224. f.SetColWidth(r.T_name, "H", "H", 10)
  225. f.SetColWidth(r.T_name, "I", "I", 10)
  226. f.SetColWidth(r.T_name, "J", "J", 10)
  227. f.SetColWidth(r.T_name, "K", "K", 10)
  228. line := 2
  229. // 循环写入数据
  230. for _, v := range StockList {
  231. line++
  232. product, _ := Basic.Read_Product_ById(v.T_product_id)
  233. f.SetCellValue(r.T_name, fmt.Sprintf("A%d", line), line-2)
  234. f.SetCellValue(r.T_name, fmt.Sprintf("B%d", line), product.T_name)
  235. f.SetCellValue(r.T_name, fmt.Sprintf("C%d", line), r.T_name)
  236. f.SetCellValue(r.T_name, fmt.Sprintf("D%d", line), product.T_spec)
  237. f.SetCellValue(r.T_name, fmt.Sprintf("E%d", line), v.T_month)
  238. f.SetCellValue(r.T_name, fmt.Sprintf("F%d", line), v.T_beginning)
  239. f.SetCellValue(r.T_name, fmt.Sprintf("G%d", line), v.T_in)
  240. f.SetCellValue(r.T_name, fmt.Sprintf("H%d", line), v.T_out)
  241. f.SetCellValue(r.T_name, fmt.Sprintf("I%d", line), v.T_ending)
  242. f.SetCellValue(r.T_name, fmt.Sprintf("J%d", line), v.T_project)
  243. }
  244. Style4, _ := f.NewStyle(
  245. &excelize.Style{
  246. Font: &excelize.Font{Size: 10, Family: "宋体"},
  247. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  248. Border: []excelize.Border{
  249. {Type: "left", Color: "000000", Style: 1},
  250. {Type: "top", Color: "000000", Style: 1},
  251. {Type: "bottom", Color: "000000", Style: 1},
  252. {Type: "right", Color: "000000", Style: 1},
  253. },
  254. })
  255. f.SetCellStyle(r.T_name, "A2", fmt.Sprintf("K%d", line), Style4)
  256. }
  257. // 保存文件
  258. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  259. fmt.Println(err)
  260. }
  261. var url string
  262. //// 上传 OSS
  263. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  264. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  265. if !is {
  266. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  267. c.ServeJSON()
  268. return
  269. }
  270. //删除目录
  271. err := os.Remove("ofile/" + filename + ".xlsx")
  272. if err != nil {
  273. logs.Error(lib.FuncName(), err)
  274. }
  275. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  276. c.ServeJSON()
  277. return
  278. }
  279. func (c *StockController) StockIn_List() {
  280. // 分页参数 初始化
  281. page, _ := c.GetInt("page")
  282. if page < 1 {
  283. page = 1
  284. }
  285. page_z, _ := c.GetInt("page_z")
  286. if page_z < 1 {
  287. page_z = conf.Page_size
  288. }
  289. // 查询
  290. T_depot_id, _ := c.GetInt("T_depot_id")
  291. T_start_date := c.GetString("T_start_date")
  292. T_end_date := c.GetString("T_end_date")
  293. userList, _ := NatsServer.Read_User_List_All()
  294. Account.Read_User_All_Map(userList)
  295. Basic.Read_Depot_All_Map()
  296. StockInDao := Stock.NewStockIn(orm.NewOrm())
  297. R_List, R_cnt := StockInDao.Read_StockIn_List(T_depot_id, T_start_date, T_end_date, page, page_z)
  298. var r_jsons lib.R_JSONS
  299. r_jsons.Num = R_cnt
  300. r_jsons.Data = R_List
  301. r_jsons.Page = page
  302. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  303. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  304. c.ServeJSON()
  305. return
  306. }
  307. func (c *StockController) StockIn_List_Product() {
  308. // 分页参数 初始化
  309. page, _ := c.GetInt("page")
  310. if page < 1 {
  311. page = 1
  312. }
  313. page_z, _ := c.GetInt("page_z")
  314. if page_z < 1 {
  315. page_z = conf.Page_size
  316. }
  317. // 查询
  318. T_depot_id, _ := c.GetInt("T_depot_id")
  319. T_name := c.GetString("T_name")
  320. T_start_date := c.GetString("T_start_date")
  321. T_end_date := c.GetString("T_end_date")
  322. userList, _ := NatsServer.Read_User_List_All()
  323. Account.Read_User_All_Map(userList)
  324. Basic.Read_Depot_All_Map()
  325. StockInDao := Stock.NewStockIn(orm.NewOrm())
  326. R_List, R_cnt := StockInDao.Read_StockInProduct_List(T_name, T_start_date, T_end_date, T_depot_id, page, page_z)
  327. var r_jsons lib.R_JSONS
  328. r_jsons.Num = R_cnt
  329. r_jsons.Data = R_List
  330. r_jsons.Page = page
  331. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  332. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  333. c.ServeJSON()
  334. return
  335. }
  336. func (c *StockController) StockIn_Get() {
  337. // 查询
  338. T_number := c.GetString("T_number")
  339. o := orm.NewOrm()
  340. StockInDao := Stock.NewStockIn(o)
  341. StockInProductDao := Stock.NewStockInProduct(o)
  342. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  343. if err != nil {
  344. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  345. c.ServeJSON()
  346. return
  347. }
  348. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  349. var pList []Stock.StockInProduct_R
  350. for _, v := range productList {
  351. pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
  352. }
  353. userList, _ := NatsServer.Read_User_List_All()
  354. Account.Read_User_All_Map(userList)
  355. Basic.Read_Depot_All_Map()
  356. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Stock.StockInToStockIn_Detail(stockIn, pList)}
  357. c.ServeJSON()
  358. return
  359. }
  360. func (c *StockController) StockIn_Add() {
  361. rand_x := 0
  362. T_number := ""
  363. o := orm.NewOrm()
  364. o.Begin()
  365. StockInDao := Stock.NewStockIn(o)
  366. for true {
  367. T_number = "RK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
  368. _, err := StockInDao.Read_StockIn_ByT_number(T_number)
  369. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  370. break
  371. }
  372. rand_x += 1
  373. }
  374. T_depot_id, _ := c.GetInt("T_depot_id")
  375. T_date := c.GetString("T_date")
  376. T_product := c.GetString("T_product")
  377. T_remark := c.GetString("T_remark")
  378. if _, is := lib.DateStrToTime(T_date); !is {
  379. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  380. c.ServeJSON()
  381. return
  382. }
  383. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "入库", T_product)
  384. var_ := Stock.StockIn{
  385. T_number: T_number,
  386. T_depot_id: T_depot_id,
  387. T_date: T_date,
  388. T_remark: T_remark,
  389. T_submit: c.User.T_uuid,
  390. }
  391. StockInProductDao := Stock.NewStockInProduct(o)
  392. StockDao := Stock.NewStock(o)
  393. DeviceDao := Stock.NewDevice(o)
  394. IotCardDao := Property.NewIotCard(&o)
  395. _, err := StockInDao.Add_StockIn(var_)
  396. if err != nil {
  397. o.Rollback()
  398. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  399. c.ServeJSON()
  400. return
  401. }
  402. if len(T_product) == 0 {
  403. o.Rollback()
  404. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  405. c.ServeJSON()
  406. return
  407. }
  408. productList := lib.SplitString(T_product, "|")
  409. for _, v := range productList {
  410. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  411. product, _ := Basic.Read_Product_ById(product_id)
  412. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  413. T_relation_sn := strings.Split(v, "-")[2]
  414. if T_relation_sn == "" && product.T_relation_sn == 1 {
  415. o.Rollback()
  416. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", T_relation_sn)}
  417. c.ServeJSON()
  418. return
  419. }
  420. if len(T_relation_sn) > 0 {
  421. snList := strings.Split(strings.Trim(T_relation_sn, ","), ",")
  422. for _, sn := range snList {
  423. mqtt := Stock.Read_MqttUser(sn)
  424. // 添加设备
  425. device := Stock.Device{
  426. T_contract_number: "",
  427. T_out_number: "",
  428. T_product_id: product_id,
  429. T_in_number: T_number,
  430. T_sn: sn,
  431. T_iccid: mqtt.Iccid,
  432. T_imei: mqtt.Imei,
  433. T_State: 2,
  434. }
  435. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  436. if err != nil {
  437. o.Rollback()
  438. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  439. c.ServeJSON()
  440. return
  441. }
  442. // 添加物联网卡
  443. iotCard := Property.IotCard{
  444. T_iccid: mqtt.Iccid,
  445. T_sn: sn,
  446. T_State: 2, //1-未使用 2-已使用 3-已作废
  447. T_type: "4G",
  448. }
  449. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  450. if err != nil {
  451. o.Rollback()
  452. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  453. c.ServeJSON()
  454. return
  455. }
  456. }
  457. }
  458. stockInProduct := Stock.StockInProduct{
  459. T_number: T_number,
  460. T_product_id: product_id,
  461. T_depot_id: T_depot_id,
  462. T_num: num, // 入库数量
  463. T_date: T_date, // 入库日期
  464. T_relation_sn: T_relation_sn,
  465. }
  466. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  467. if err != nil {
  468. o.Rollback()
  469. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  470. c.ServeJSON()
  471. return
  472. }
  473. _, err = StockDao.AddOrUpdate_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
  474. if err != nil {
  475. o.Rollback()
  476. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  477. c.ServeJSON()
  478. return
  479. }
  480. }
  481. o.Commit()
  482. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "入库", var_)
  483. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  484. c.ServeJSON()
  485. return
  486. }
  487. func (c *StockController) StockIn_Edit() {
  488. o := orm.NewOrm()
  489. o.Begin()
  490. StockInDao := Stock.NewStockIn(o)
  491. T_number := c.GetString("T_number") // 入库单号
  492. T_depot_id, _ := c.GetInt("T_depot_id")
  493. T_date := c.GetString("T_date")
  494. T_product := c.GetString("T_product")
  495. T_remark := c.GetString("T_remark")
  496. if _, is := lib.DateStrToTime(T_date); !is {
  497. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  498. c.ServeJSON()
  499. return
  500. }
  501. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "入库", T_product)
  502. var_ := Stock.StockIn{
  503. T_number: T_number,
  504. T_depot_id: T_depot_id,
  505. T_date: T_date,
  506. T_remark: T_remark,
  507. T_submit: c.User.T_uuid,
  508. }
  509. StockInProductDao := Stock.NewStockInProduct(o)
  510. StockDao := Stock.NewStock(o)
  511. DeviceDao := Stock.NewDevice(o)
  512. IotCardDao := Property.NewIotCard(&o)
  513. _, err := StockInDao.Add_StockIn(var_)
  514. if err != nil {
  515. o.Rollback()
  516. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  517. c.ServeJSON()
  518. return
  519. }
  520. if len(T_product) == 0 {
  521. o.Rollback()
  522. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  523. c.ServeJSON()
  524. return
  525. }
  526. productList := lib.SplitString(T_product, "|")
  527. for _, v := range productList {
  528. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  529. product, _ := Basic.Read_Product_ById(product_id)
  530. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  531. T_relation_sn := strings.Split(v, "-")[2]
  532. if T_relation_sn == "" && product.T_relation_sn == 1 {
  533. o.Rollback()
  534. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", T_relation_sn)}
  535. c.ServeJSON()
  536. return
  537. }
  538. if len(T_relation_sn) > 0 {
  539. snList := strings.Split(strings.Trim(T_relation_sn, ","), ",")
  540. for _, sn := range snList {
  541. mqtt := Stock.Read_MqttUser(sn)
  542. // 添加设备
  543. device := Stock.Device{
  544. T_contract_number: "",
  545. T_out_number: "",
  546. T_product_id: product_id,
  547. T_in_number: T_number,
  548. T_sn: sn,
  549. T_iccid: mqtt.Iccid,
  550. T_imei: mqtt.Imei,
  551. T_State: 2,
  552. }
  553. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  554. if err != nil {
  555. o.Rollback()
  556. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  557. c.ServeJSON()
  558. return
  559. }
  560. // 添加物联网卡
  561. iotCard := Property.IotCard{
  562. T_iccid: mqtt.Iccid,
  563. T_sn: sn,
  564. T_State: 2, //1-未使用 2-已使用 3-已作废
  565. T_type: "4G",
  566. }
  567. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  568. if err != nil {
  569. o.Rollback()
  570. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  571. c.ServeJSON()
  572. return
  573. }
  574. }
  575. }
  576. stockInProduct := Stock.StockInProduct{
  577. T_number: T_number,
  578. T_product_id: product_id,
  579. T_depot_id: T_depot_id,
  580. T_num: num, // 入库数量
  581. T_date: T_date, // 入库日期
  582. T_relation_sn: T_relation_sn,
  583. }
  584. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  585. if err != nil {
  586. o.Rollback()
  587. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  588. c.ServeJSON()
  589. return
  590. }
  591. _, err = StockDao.AddOrUpdate_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
  592. if err != nil {
  593. o.Rollback()
  594. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  595. c.ServeJSON()
  596. return
  597. }
  598. }
  599. o.Commit()
  600. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "入库", var_)
  601. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  602. c.ServeJSON()
  603. return
  604. }
  605. func (c *StockController) StockOut_List() {
  606. // 分页参数 初始化
  607. page, _ := c.GetInt("page")
  608. if page < 1 {
  609. page = 1
  610. }
  611. page_z, _ := c.GetInt("page_z")
  612. if page_z < 1 {
  613. page_z = conf.Page_size
  614. }
  615. // 查询
  616. T_depot_id, _ := c.GetInt("T_depot_id")
  617. T_contract_number := c.GetString("T_contract_number")
  618. T_start_date := c.GetString("T_start_date")
  619. T_end_date := c.GetString("T_end_date")
  620. userList, _ := NatsServer.Read_User_List_All()
  621. Account.Read_User_All_Map(userList)
  622. Basic.Read_Depot_All_Map()
  623. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  624. R_List, R_cnt := StockOutDao.Read_StockOut_List(T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  625. var r_jsons lib.R_JSONS
  626. r_jsons.Num = R_cnt
  627. r_jsons.Data = R_List
  628. r_jsons.Page = page
  629. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  630. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  631. c.ServeJSON()
  632. return
  633. }
  634. func (c *StockController) StockOut_List_Product() {
  635. // 分页参数 初始化
  636. page, _ := c.GetInt("page")
  637. if page < 1 {
  638. page = 1
  639. }
  640. page_z, _ := c.GetInt("page_z")
  641. if page_z < 1 {
  642. page_z = conf.Page_size
  643. }
  644. // 查询
  645. T_depot_id, _ := c.GetInt("T_depot_id")
  646. T_name := c.GetString("T_name")
  647. T_start_date := c.GetString("T_start_date")
  648. T_end_date := c.GetString("T_end_date")
  649. userList, _ := NatsServer.Read_User_List_All()
  650. Account.Read_User_All_Map(userList)
  651. Basic.Read_Depot_All_Map()
  652. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  653. R_List, R_cnt := StockOutDao.Read_StockOutProduct_List(T_name, T_start_date, T_end_date, T_depot_id, page, page_z)
  654. var r_jsons lib.R_JSONS
  655. r_jsons.Num = R_cnt
  656. r_jsons.Data = R_List
  657. r_jsons.Page = page
  658. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  659. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  660. c.ServeJSON()
  661. return
  662. }
  663. func (c *StockController) StockOut_Get() {
  664. // 查询
  665. T_number := c.GetString("T_number")
  666. o := orm.NewOrm()
  667. StockOutDao := Stock.NewStockOut(o)
  668. StockOutProductDao := Stock.NewStockOutProduct(o)
  669. stockIn, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  670. if err != nil {
  671. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  672. c.ServeJSON()
  673. return
  674. }
  675. productList := StockOutProductDao.Read_StockOutProduct_List(stockIn.T_number)
  676. var pList []Stock.StockOutProduct_R
  677. for _, v := range productList {
  678. pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
  679. }
  680. userList, _ := NatsServer.Read_User_List_All()
  681. Account.Read_User_All_Map(userList)
  682. Basic.Read_Depot_All_Map()
  683. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Stock.StockOutToStockOut_Detail(stockIn, pList)}
  684. c.ServeJSON()
  685. return
  686. }
  687. func (c *StockController) StockOut_Add() {
  688. rand_x := 0
  689. T_number := ""
  690. o := orm.NewOrm()
  691. o.Begin()
  692. StockOutDao := Stock.NewStockOut(o)
  693. for true {
  694. T_number = "CK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
  695. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  696. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  697. break
  698. }
  699. rand_x += 1
  700. }
  701. T_contract_number := c.GetString("T_contract_number")
  702. T_depot_id, _ := c.GetInt("T_depot_id")
  703. T_type, _ := c.GetInt("T_type") // 出库类型 1-领料出库 2-销售出库
  704. T_date := c.GetString("T_date")
  705. T_receive := c.GetString("T_receive")
  706. T_product := c.GetString("T_product")
  707. T_remark := c.GetString("T_remark")
  708. T_project := c.GetString("T_project")
  709. T_delivery_type, _ := c.GetInt("T_delivery_type")
  710. T_signer_unit := c.GetString("T_signer_unit")
  711. T_signer := c.GetString("T_signer")
  712. T_signer_phone := c.GetString("T_signer_phone")
  713. T_signer_date := c.GetString("T_signer_date")
  714. T_courier_number := c.GetString("T_courier_number")
  715. if _, is := lib.DateStrToTime(T_date); !is {
  716. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  717. c.ServeJSON()
  718. return
  719. }
  720. var_ := Stock.StockOut{
  721. T_number: T_number,
  722. T_contract_number: T_contract_number,
  723. T_depot_id: T_depot_id,
  724. T_type: T_type,
  725. T_date: T_date,
  726. T_receive: T_receive,
  727. T_remark: T_remark,
  728. T_project: T_project,
  729. T_submit: c.User.T_uuid,
  730. // 销售出库
  731. T_delivery_type: T_delivery_type, // 1-自送 2-自提 3-快递
  732. T_signer_unit: T_signer_unit,
  733. T_signer: T_signer,
  734. T_signer_phone: T_signer_phone,
  735. T_signer_date: T_signer_date,
  736. T_courier_number: T_courier_number,
  737. }
  738. StockOutProductDao := Stock.NewStockOutProduct(o)
  739. StockDao := Stock.NewStock(o)
  740. DeviceDao := Stock.NewDevice(o)
  741. ContractDao := Contract.NewContract(o)
  742. ContractProductDao := Contract.NewContractProduct(o)
  743. _, err := StockOutDao.Add_StockOut(var_)
  744. if err != nil {
  745. o.Rollback()
  746. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  747. c.ServeJSON()
  748. return
  749. }
  750. // 1、添加出库单
  751. contract, _ := ContractDao.Read_Contract_ByT_number(T_contract_number)
  752. if contract.Id == 0 && T_type == 2 {
  753. o.Rollback()
  754. c.Data["json"] = lib.JSONS{Code: 203, Msg: "合同编号错误!"}
  755. c.ServeJSON()
  756. return
  757. }
  758. if len(T_product) == 0 {
  759. o.Rollback()
  760. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  761. c.ServeJSON()
  762. return
  763. }
  764. productList := lib.SplitString(T_product, "|")
  765. for _, v := range productList {
  766. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  767. product, _ := Basic.Read_Product_ById(product_id)
  768. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  769. T_relation_sn := strings.Split(v, "-")[2]
  770. if T_relation_sn == "" && product.T_relation_sn == 1 {
  771. o.Rollback()
  772. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  773. c.ServeJSON()
  774. return
  775. }
  776. // 2、更新设备状态为已出库
  777. if len(T_relation_sn) > 0 {
  778. snList := strings.Split(T_relation_sn, ",")
  779. for _, sn := range snList {
  780. mqtt := Stock.Read_MqttUser(sn)
  781. device := Stock.Device{
  782. T_contract_number: T_contract_number,
  783. T_product_id: product_id,
  784. T_out_number: T_number,
  785. T_sn: sn,
  786. T_iccid: mqtt.Iccid,
  787. T_imei: mqtt.Imei,
  788. T_State: 1,
  789. T_project: T_project,
  790. }
  791. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  792. if err != nil {
  793. o.Rollback()
  794. c.Data["json"] = lib.JSONS{Code: 202, Msg: "出库失败"}
  795. c.ServeJSON()
  796. return
  797. }
  798. }
  799. }
  800. // 3、添加出库产品清单
  801. stockOutProduct := Stock.StockOutProduct{
  802. T_number: T_number,
  803. T_product_id: product_id,
  804. T_depot_id: T_depot_id,
  805. T_num: num, // 出库数量
  806. T_date: T_date, // 出库数量
  807. T_relation_sn: T_relation_sn,
  808. }
  809. _, err = StockOutProductDao.Add_StockOutProduct(stockOutProduct)
  810. if err != nil {
  811. o.Rollback()
  812. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  813. c.ServeJSON()
  814. return
  815. }
  816. // 4、更改合同产品列表清单
  817. contractProduct, _ := ContractProductDao.Read_ContractProduct_ByT_number_T_product_id(T_contract_number, product_id)
  818. contractProduct.T_product_out += num
  819. if contractProduct.T_product_out > contractProduct.T_product_total && T_type == 2 {
  820. o.Rollback()
  821. p, _ := Basic.Read_Product_ById(product_id)
  822. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】出库数量超过预计出库数量!", p.T_name)}
  823. c.ServeJSON()
  824. return
  825. }
  826. contractProduct.T_State = 2
  827. if contractProduct.T_product_out == 0 {
  828. contractProduct.T_State = 1
  829. }
  830. if contractProduct.T_product_out == contractProduct.T_product_total {
  831. contractProduct.T_State = 3
  832. }
  833. err = ContractProductDao.Update_ContractProduct(contractProduct, "T_product_out", "T_State")
  834. if err != nil {
  835. o.Rollback()
  836. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同产品清单失败"}
  837. c.ServeJSON()
  838. return
  839. }
  840. // 5、更新产品库存列表
  841. _, err = StockDao.AddOrUpdate_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 1)
  842. if err != nil {
  843. o.Rollback()
  844. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存失败"}
  845. c.ServeJSON()
  846. return
  847. }
  848. }
  849. o.Commit()
  850. o2 := orm.NewOrm()
  851. o2.Begin()
  852. ContractDao2 := Contract.NewContract(o2)
  853. ContractProductDao2 := Contract.NewContractProduct(o2)
  854. // 5、更新合同出库状态
  855. var T_out int
  856. if contract.T_State == 1 {
  857. T_out = 2
  858. // 查询合同产品清单是否全部都为已出库
  859. state := ContractProductDao2.Read_ContractProduct_T_State_List(contract.T_number)
  860. if state == 1 {
  861. T_out = 1
  862. }
  863. if state == 3 {
  864. T_out = 3
  865. }
  866. }
  867. if T_out != contract.T_out {
  868. contract.T_out = T_out
  869. err = ContractDao2.Update_Contract(contract, "T_out")
  870. if err != nil {
  871. o2.Rollback()
  872. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同出库状态失败"}
  873. c.ServeJSON()
  874. return
  875. }
  876. }
  877. o2.Commit()
  878. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "出库", var_)
  879. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  880. c.ServeJSON()
  881. return
  882. }
  883. func (c *StockController) StockOut_Edit() {
  884. T_number := c.GetString("T_number")
  885. T_remark := c.GetString("T_remark")
  886. T_project := c.GetString("T_project")
  887. T_delivery_type, _ := c.GetInt("T_delivery_type")
  888. T_signer_unit := c.GetString("T_signer_unit")
  889. T_signer := c.GetString("T_signer")
  890. T_signer_phone := c.GetString("T_signer_phone")
  891. T_signer_date := c.GetString("T_signer_date")
  892. T_courier_number := c.GetString("T_courier_number")
  893. o := orm.NewOrm()
  894. StockOutDao := Stock.NewStockOut(o)
  895. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  896. if err != nil {
  897. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库编号错误!"}
  898. c.ServeJSON()
  899. return
  900. }
  901. if len(T_remark) > 0 {
  902. stockOut.T_remark = T_remark
  903. }
  904. if len(T_project) > 0 {
  905. stockOut.T_project = T_project
  906. }
  907. if T_delivery_type > 0 {
  908. stockOut.T_delivery_type = T_delivery_type
  909. }
  910. if len(T_signer_unit) > 0 {
  911. stockOut.T_signer_unit = T_signer_unit
  912. }
  913. if len(T_signer) > 0 {
  914. stockOut.T_signer = T_signer
  915. }
  916. if len(T_signer_phone) > 0 {
  917. stockOut.T_signer_phone = T_signer_phone
  918. }
  919. if len(T_signer_date) > 0 {
  920. stockOut.T_signer_date = T_signer_date
  921. }
  922. if len(T_courier_number) > 0 {
  923. stockOut.T_courier_number = T_courier_number
  924. }
  925. 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")
  926. if err != nil {
  927. o.Rollback()
  928. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  929. c.ServeJSON()
  930. return
  931. }
  932. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "修改发货单", stockOut)
  933. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  934. c.ServeJSON()
  935. return
  936. }
  937. func Cron_StockMonth() {
  938. //创建一个定时任务对象
  939. c := cron.New(cron.WithSeconds())
  940. //给对象增加定时任务
  941. // @monthly 每月运行一次,每月第一天午夜 0 0 0 1 * *
  942. //c.AddFunc("0 */1 * * * ?", Cron_StockMonth_Add)
  943. c.AddFunc("@monthly", Cron_StockMonth_Add)
  944. //启动定时任务
  945. c.Start()
  946. defer c.Stop()
  947. //查询语句,阻塞,让main函数不退出,保持程序运行
  948. select {}
  949. }
  950. // 保存每月入库出库明细
  951. func Cron_StockMonth_Add() {
  952. T_month := time.Now().AddDate(0, -1, 0).Format("2006-01")
  953. logs.Info("开始统计" + T_month + "库存明细数据")
  954. o := orm.NewOrm()
  955. StockDao := Stock.NewStock(o)
  956. StockOutDao := Stock.NewStockOut(o)
  957. StockMonthDao := Stock.NewStockMonth(o)
  958. StockOutProductDao := Stock.NewStockOutProduct(o)
  959. StockInProductDao := Stock.NewStockInProduct(o)
  960. stockList, _ := StockDao.Read_Stock_List(0, 0, "", "", 0, 9999)
  961. for _, stock := range stockList {
  962. stockMonth := Stock.StockMonth{
  963. T_depot_id: stock.T_depot_id,
  964. T_product_id: stock.T_product_id,
  965. T_product_class: stock.T_product_class,
  966. T_month: T_month,
  967. }
  968. // 获取当前产品本月出库数量
  969. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(stock.T_depot_id, stock.T_product_id, T_month)
  970. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(stock.T_depot_id, stock.T_product_id, T_month)
  971. s := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(stock.T_depot_id, stock.T_product_id, T_month)
  972. if len(s) > 0 {
  973. stockMonth.T_beginning = s[0].T_ending
  974. } else {
  975. stockMonth.T_beginning = 0
  976. }
  977. // 期末库存 = 期初库存+入库-出库
  978. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  979. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(stock.T_depot_id, stock.T_product_id, T_month)
  980. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  981. if err != nil {
  982. NatsServer.AddSysLogs("库存明细统计", fmt.Sprintf("库存明细统计失败%s:%d:%d", T_month, stock.T_depot_id, stock.T_product_id), stockMonth)
  983. }
  984. }
  985. }