StockOut.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package Stock
  2. import (
  3. "ERP_storage/models/Account"
  4. "ERP_storage/models/Basic"
  5. "fmt"
  6. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  7. orm2 "github.com/beego/beego/v2/client/orm"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "ERP_storage/logs"
  12. _ "github.com/astaxie/beego/cache/redis"
  13. "github.com/beego/beego/v2/adapter/orm"
  14. _ "github.com/go-sql-driver/mysql"
  15. )
  16. // 入库
  17. type StockOut struct {
  18. Id int `orm:"column(ID);size(11);auto;pk"`
  19. T_number string `orm:"size(256);null"` // 入库单号
  20. T_contract_number string `orm:"size(256);null"` // 合同编号
  21. T_depot_id int `orm:"size(20);null"` // 仓库id
  22. T_type int `orm:"size(2);null"` // 出库类型 1-领料出库 2-销售出库
  23. T_date string `orm:"size(256);null"` // 业务日期
  24. T_receive string `orm:"size(256);null"` // 领取人
  25. T_submit string `orm:"size(256);null"` // 经办人
  26. T_remark string `orm:"type(text);null"` // 备注
  27. T_project string `orm:"type(text);null"` // 出库项目
  28. // ----销售出库------
  29. T_delivery_type int `orm:"size(2);null"` // 1-自送 2-自提 3-快递
  30. T_signer_unit string `orm:"size(256);null"` // 签收单位
  31. T_signer string `orm:"size(256);null"` // 签收人
  32. T_signer_phone string `orm:"size(256);null"` // 签收人联系电话
  33. T_signer_date string `orm:"size(256);null"` // 签收日期
  34. T_courier_number string `orm:"size(256);null"` // 物流单号
  35. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  36. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  37. }
  38. func (t *StockOut) TableName() string {
  39. return "stock_out" // 数据库名称 // ************** 替换 FormulaList **************
  40. }
  41. type StockOutDaoImpl struct {
  42. orm orm.Ormer
  43. }
  44. func NewStockOut(orm orm.Ormer) *StockOutDaoImpl {
  45. return &StockOutDaoImpl{orm: orm}
  46. }
  47. func init() {
  48. //注册模型
  49. orm.RegisterModel(new(StockOut))
  50. }
  51. type StockOut_R struct {
  52. Id int
  53. T_number string // 出库单号
  54. T_contract_number string // 合同编号
  55. T_depot_id int // 仓库id
  56. T_depot_name string // 仓库名称
  57. T_type int // 出库类型 1-领料出库 2-销售出库
  58. T_date string // 业务日期
  59. T_receive string // 领取人
  60. T_receive_name string // 领取人名称
  61. T_project string // 关联项目
  62. }
  63. type StockOut_Detail struct {
  64. Id int
  65. T_number string // 入库单号
  66. T_contract_number string // 合同编号
  67. T_depot_id int // 仓库id
  68. T_depot_name string // 仓库名称
  69. T_type int // 出库类型 1-领料出库 2-销售出库
  70. T_date string // 业务日期
  71. T_receive string // 领取人
  72. T_receive_name string // 领取人名称
  73. T_submit string // 经办人
  74. T_submit_name string // 经办人名称
  75. T_project string // 关联项目
  76. T_remark string // 备注
  77. // ----销售出库------
  78. T_delivery_type int // 1-自送 2-自提 3-快递
  79. T_signer_unit string // 签收单位
  80. T_signer string // 签收人
  81. T_signer_phone string // 签收人联系电话
  82. T_signer_date string // 签收日期
  83. T_courier_number string // 物流单号
  84. T_Product []StockOutProduct_R
  85. }
  86. func StockOutToStockOut_R(t StockOut) (r StockOut_R) {
  87. r.Id = t.Id
  88. r.T_number = t.T_number
  89. r.T_contract_number = t.T_contract_number
  90. r.T_depot_id = t.T_depot_id
  91. r.T_depot_name = Basic.Read_Depot_Get(t.T_depot_id)
  92. r.T_type = t.T_type
  93. r.T_date = t.T_date
  94. r.T_receive = t.T_receive
  95. r.T_receive_name = Account.Read_User_T_name_Get(t.T_receive)
  96. r.T_project = t.T_project
  97. return r
  98. }
  99. func StockOutToStockOut_Detail(t StockOut, productList []StockOutProduct_R) (r StockOut_Detail) {
  100. r.Id = t.Id
  101. r.T_number = t.T_number
  102. r.T_contract_number = t.T_contract_number
  103. r.T_depot_id = t.T_depot_id
  104. r.T_depot_name = Basic.Read_Depot_Get(t.T_depot_id)
  105. r.T_type = t.T_type
  106. r.T_date = t.T_date
  107. r.T_receive = t.T_receive
  108. r.T_receive_name = Account.Read_User_T_name_Get(t.T_receive)
  109. r.T_project = t.T_project
  110. r.T_remark = t.T_remark
  111. r.T_delivery_type = t.T_delivery_type
  112. r.T_signer_unit = t.T_signer_unit
  113. r.T_signer = t.T_signer
  114. r.T_signer_phone = t.T_signer_phone
  115. r.T_signer_date = t.T_signer_date
  116. r.T_courier_number = t.T_courier_number
  117. r.T_Product = productList
  118. return r
  119. }
  120. // 添加
  121. func (dao *StockOutDaoImpl) Add_StockOut(r StockOut) (id int64, err error) {
  122. id, err = dao.orm.Insert(&r)
  123. if err != nil {
  124. logs.Error(lib.FuncName(), err)
  125. }
  126. return id, err
  127. }
  128. // 获取 ById
  129. func (dao *StockOutDaoImpl) Read_StockOut_ByT_number(T_number string) (r StockOut, err error) {
  130. qs := dao.orm.QueryTable(new(StockOut))
  131. err = qs.Filter("T_number", T_number).One(&r)
  132. if err != nil {
  133. logs.Error(lib.FuncName(), err)
  134. }
  135. return
  136. }
  137. // 修改
  138. func (dao *StockOutDaoImpl) Update_StockOut(m StockOut, cols ...string) error {
  139. _, err := dao.orm.Update(&m, cols...)
  140. if err != nil {
  141. logs.Error(lib.FuncName(), err)
  142. return err
  143. }
  144. return nil
  145. }
  146. // 删除
  147. func (dao *StockOutDaoImpl) Delete_StockOut(m StockOut) error {
  148. _, err := dao.orm.Delete(&m)
  149. if err != nil {
  150. logs.Error(lib.FuncName(), err)
  151. return err
  152. }
  153. return nil
  154. }
  155. // 获取列表
  156. func (dao *StockOutDaoImpl) Read_StockOut_List(T_depot_id int, T_contract_number, T_start_date, T_end_date string, page, page_z int) (r_ []StockOut_R, cnt int64) {
  157. qs := dao.orm.QueryTable(new(StockOut))
  158. var offset int64
  159. if page <= 1 {
  160. offset = 0
  161. } else {
  162. offset = int64((page - 1) * page_z)
  163. }
  164. // 过滤
  165. cond := orm.NewCondition()
  166. if T_depot_id > 0 {
  167. cond = cond.And("T_depot_id", T_depot_id)
  168. }
  169. if len(T_contract_number) > 0 {
  170. cond = cond.And("T_contract_number", T_contract_number)
  171. }
  172. if len(T_start_date) > 0 {
  173. cond = cond.And("T_date__gte", T_start_date)
  174. }
  175. if len(T_end_date) > 0 {
  176. cond = cond.And("T_date__lte", T_end_date)
  177. }
  178. // 查询
  179. var r []StockOut
  180. _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&r)
  181. if err != nil {
  182. logs.Error(lib.FuncName(), err)
  183. return
  184. }
  185. cnt, err = qs.SetCond((*orm2.Condition)(cond)).Count()
  186. if err != nil {
  187. logs.Error(lib.FuncName(), err)
  188. return
  189. }
  190. for _, v := range r {
  191. r_ = append(r_, StockOutToStockOut_R(v))
  192. }
  193. return r_, cnt
  194. }
  195. // 获取出库单号
  196. func (dao *StockOutDaoImpl) Read_StockOut_T_contract_number(T_depot_id, T_product_id int, T_date string) string {
  197. sql := "SELECT DISTINCT(t_contract_number) FROM stock_out WHERE t_number in (SELECT t_number FROM stock_out_product WHERE t_depot_id = " + strconv.Itoa(T_depot_id) + " AND t_product_id = " + strconv.Itoa(T_product_id) + " AND t_date like '" + T_date + "%')"
  198. var pl_lists orm2.ParamsList
  199. _, err := dao.orm.Raw(sql).ValuesFlat(&pl_lists)
  200. if err != nil {
  201. logs.Error(lib.FuncName(), err)
  202. return ""
  203. }
  204. var r []string
  205. for _, v := range pl_lists {
  206. r = append(r, v.(string))
  207. }
  208. return strings.Join(r, ",")
  209. }
  210. func (dao *StockOutDaoImpl) Read_StockOut_T_project(T_depot_id, T_product_id int, T_date string) []string {
  211. sql := "SELECT DISTINCT(t_project) FROM stock_out WHERE t_number in (SELECT t_number FROM stock_out_product WHERE t_depot_id = " + strconv.Itoa(T_depot_id) + " AND t_product_id = " + strconv.Itoa(T_product_id) + " AND t_date like '" + T_date + "%')"
  212. var pl_lists orm2.ParamsList
  213. _, err := dao.orm.Raw(sql).ValuesFlat(&pl_lists)
  214. if err != nil {
  215. logs.Error(lib.FuncName(), err)
  216. return []string{}
  217. }
  218. var r []string
  219. var j = 1
  220. for _, v := range pl_lists {
  221. if v == nil || len(v.(string)) == 0 {
  222. continue
  223. }
  224. r = append(r, fmt.Sprintf("%d、%s", j, strings.Trim(v.(string), "\r\n")))
  225. j += 1
  226. }
  227. return r
  228. }
  229. // 通过入库编号查询入库产品列表
  230. func (dao *StockOutDaoImpl) Read_StockOutProduct_List(T_name, T_start_date, T_end_date string, T_depot_id int,
  231. T_product_name, T_product_model string, page int, page_z int) (
  232. r_ []StockOutProductList, cnt int64) {
  233. var offset int
  234. if page <= 1 {
  235. offset = 0
  236. } else {
  237. offset = (page - 1) * page_z
  238. }
  239. var maps_z []orm2.ParamsList
  240. var r []StockOutProductList
  241. sqlWhere := ""
  242. if len(T_name) > 0 {
  243. sqlWhere += " AND (so.t_number like \"%" + T_name + "%\" or so.t_remark like \"%" + T_name + "%\" or so.t_project like \"%" + T_name + "%\")"
  244. }
  245. if T_depot_id > 0 {
  246. sqlWhere += fmt.Sprintf(" AND so.t_depot_id = %d", T_depot_id)
  247. }
  248. if len(T_start_date) > 0 {
  249. sqlWhere += " AND so.t_date >= \"" + T_start_date + "\""
  250. }
  251. if len(T_end_date) > 0 {
  252. sqlWhere += " AND so.t_date <= \"" + T_end_date + "\""
  253. }
  254. if len(T_product_name) > 0 {
  255. sqlWhere += " AND p.t_name like \"%" + T_product_name + "%\""
  256. }
  257. if len(T_product_model) > 0 {
  258. sqlWhere += " AND p.t_model like \"%" + T_product_model + "%\""
  259. }
  260. // 获取总条数
  261. sql := "SELECT COUNT(product.ID) FROM stock_out_product product " +
  262. "JOIN product p ON product.t_product_id = p.ID " +
  263. "LEFT JOIN stock_out so ON so.t_number=product.t_number WHERE 1=1 "
  264. sql = sql + sqlWhere
  265. fmt.Println(sql)
  266. _, err := dao.orm.Raw(sql).ValuesList(&maps_z)
  267. if err != nil {
  268. return r, 0
  269. }
  270. if len(maps_z) == 0 {
  271. return r, 0
  272. }
  273. sql = "SELECT so.t_number as t_number,so.t_depot_id as t_depot_id,so.t_date as t_date,product.*,so.* FROM stock_out_product product " +
  274. "JOIN product p ON product.t_product_id = p.ID " +
  275. "LEFT JOIN stock_out so ON so.t_number=product.t_number WHERE 1=1"
  276. sql = sql + sqlWhere
  277. sql += " ORDER BY so.t_date DESC"
  278. if page_z != 9999 {
  279. sql += " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  280. }
  281. fmt.Println(sql)
  282. _, err = dao.orm.Raw(sql).QueryRows(&r)
  283. for _, v := range r {
  284. r_ = append(r_, StockOutProductListToStockOutProductList(v))
  285. }
  286. count, _ := strconv.Atoi(maps_z[0][0].(string))
  287. return r_, int64(count)
  288. }