123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- package Stock
- import (
- "ERP_storage/models/Account"
- "ERP_storage/models/Basic"
- "fmt"
- "gogs.baozhida.cn/zoie/ERP_libs/lib"
- orm2 "github.com/beego/beego/v2/client/orm"
- "strconv"
- "strings"
- "time"
- "ERP_storage/logs"
- _ "github.com/astaxie/beego/cache/redis"
- "github.com/beego/beego/v2/adapter/orm"
- _ "github.com/go-sql-driver/mysql"
- )
- // 入库
- type StockOut struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_number string `orm:"size(256);null"` // 入库单号
- T_contract_number string `orm:"size(256);null"` // 合同编号
- T_depot_id int `orm:"size(20);null"` // 仓库id
- T_type int `orm:"size(2);null"` // 出库类型 1-领料出库 2-销售出库
- T_date string `orm:"size(256);null"` // 业务日期
- T_receive string `orm:"size(256);null"` // 领取人
- T_submit string `orm:"size(256);null"` // 经办人
- T_remark string `orm:"type(text);null"` // 备注
- T_project string `orm:"type(text);null"` // 出库项目
- // ----销售出库------
- T_delivery_type int `orm:"size(2);null"` // 1-自送 2-自提 3-快递
- T_signer_unit string `orm:"size(256);null"` // 签收单位
- T_signer string `orm:"size(256);null"` // 签收人
- T_signer_phone string `orm:"size(256);null"` // 签收人联系电话
- T_signer_date string `orm:"size(256);null"` // 签收日期
- T_courier_number string `orm:"size(256);null"` // 物流单号
- CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
- UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
- }
- func (t *StockOut) TableName() string {
- return "stock_out" // 数据库名称 // ************** 替换 FormulaList **************
- }
- type StockOutDaoImpl struct {
- orm orm.Ormer
- }
- func NewStockOut(orm orm.Ormer) *StockOutDaoImpl {
- return &StockOutDaoImpl{orm: orm}
- }
- func init() {
- //注册模型
- orm.RegisterModel(new(StockOut))
- }
- type StockOut_R struct {
- Id int
- T_number string // 出库单号
- T_contract_number string // 合同编号
- T_depot_id int // 仓库id
- T_depot_name string // 仓库名称
- T_type int // 出库类型 1-领料出库 2-销售出库
- T_date string // 业务日期
- T_receive string // 领取人
- T_receive_name string // 领取人名称
- T_project string // 关联项目
- }
- type StockOut_Detail struct {
- Id int
- T_number string // 入库单号
- T_contract_number string // 合同编号
- T_depot_id int // 仓库id
- T_depot_name string // 仓库名称
- T_type int // 出库类型 1-领料出库 2-销售出库
- T_date string // 业务日期
- T_receive string // 领取人
- T_receive_name string // 领取人名称
- T_submit string // 经办人
- T_submit_name string // 经办人名称
- T_project string // 关联项目
- T_remark string // 备注
- // ----销售出库------
- T_delivery_type int // 1-自送 2-自提 3-快递
- T_signer_unit string // 签收单位
- T_signer string // 签收人
- T_signer_phone string // 签收人联系电话
- T_signer_date string // 签收日期
- T_courier_number string // 物流单号
- T_Product []StockOutProduct_R
- }
- func StockOutToStockOut_R(t StockOut) (r StockOut_R) {
- r.Id = t.Id
- r.T_number = t.T_number
- r.T_contract_number = t.T_contract_number
- r.T_depot_id = t.T_depot_id
- r.T_depot_name = Basic.Read_Depot_Get(t.T_depot_id)
- r.T_type = t.T_type
- r.T_date = t.T_date
- r.T_receive = t.T_receive
- r.T_receive_name = Account.Read_User_T_name_Get(t.T_receive)
- r.T_project = t.T_project
- return r
- }
- func StockOutToStockOut_Detail(t StockOut, productList []StockOutProduct_R) (r StockOut_Detail) {
- r.Id = t.Id
- r.T_number = t.T_number
- r.T_contract_number = t.T_contract_number
- r.T_depot_id = t.T_depot_id
- r.T_depot_name = Basic.Read_Depot_Get(t.T_depot_id)
- r.T_type = t.T_type
- r.T_date = t.T_date
- r.T_receive = t.T_receive
- r.T_receive_name = Account.Read_User_T_name_Get(t.T_receive)
- r.T_project = t.T_project
- r.T_remark = t.T_remark
- r.T_delivery_type = t.T_delivery_type
- r.T_signer_unit = t.T_signer_unit
- r.T_signer = t.T_signer
- r.T_signer_phone = t.T_signer_phone
- r.T_signer_date = t.T_signer_date
- r.T_courier_number = t.T_courier_number
- r.T_Product = productList
- return r
- }
- // 添加
- func (dao *StockOutDaoImpl) Add_StockOut(r StockOut) (id int64, err error) {
- id, err = dao.orm.Insert(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return id, err
- }
- // 获取 ById
- func (dao *StockOutDaoImpl) Read_StockOut_ByT_number(T_number string) (r StockOut, err error) {
- qs := dao.orm.QueryTable(new(StockOut))
- err = qs.Filter("T_number", T_number).One(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return
- }
- // 修改
- func (dao *StockOutDaoImpl) Update_StockOut(m StockOut, cols ...string) error {
- _, err := dao.orm.Update(&m, cols...)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return err
- }
- return nil
- }
- // 删除
- func (dao *StockOutDaoImpl) Delete_StockOut(m StockOut) error {
- _, err := dao.orm.Delete(&m)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return err
- }
- return nil
- }
- // 获取列表
- 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) {
- qs := dao.orm.QueryTable(new(StockOut))
- var offset int64
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- // 过滤
- cond := orm.NewCondition()
- if T_depot_id > 0 {
- cond = cond.And("T_depot_id", T_depot_id)
- }
- if len(T_contract_number) > 0 {
- cond = cond.And("T_contract_number", T_contract_number)
- }
- if len(T_start_date) > 0 {
- cond = cond.And("T_date__gte", T_start_date)
- }
- if len(T_end_date) > 0 {
- cond = cond.And("T_date__lte", T_end_date)
- }
- // 查询
- var r []StockOut
- _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- cnt, err = qs.SetCond((*orm2.Condition)(cond)).Count()
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- for _, v := range r {
- r_ = append(r_, StockOutToStockOut_R(v))
- }
- return r_, cnt
- }
- // 获取出库单号
- func (dao *StockOutDaoImpl) Read_StockOut_T_contract_number(T_depot_id, T_product_id int, T_date string) string {
- 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 + "%')"
- var pl_lists orm2.ParamsList
- _, err := dao.orm.Raw(sql).ValuesFlat(&pl_lists)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return ""
- }
- var r []string
- for _, v := range pl_lists {
- r = append(r, v.(string))
- }
- return strings.Join(r, ",")
- }
- func (dao *StockOutDaoImpl) Read_StockOut_T_project(T_depot_id, T_product_id int, T_date string) []string {
- 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 + "%')"
- var pl_lists orm2.ParamsList
- _, err := dao.orm.Raw(sql).ValuesFlat(&pl_lists)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return []string{}
- }
- var r []string
- var j = 1
- for _, v := range pl_lists {
- if v == nil || len(v.(string)) == 0 {
- continue
- }
- r = append(r, fmt.Sprintf("%d、%s", j, strings.Trim(v.(string), "\r\n")))
- j += 1
- }
- return r
- }
- // 通过入库编号查询入库产品列表
- func (dao *StockOutDaoImpl) Read_StockOutProduct_List(T_name, T_start_date, T_end_date string, T_depot_id int,
- T_product_name, T_product_model string, page int, page_z int) (
- r_ []StockOutProductList, cnt int64) {
- var offset int
- if page <= 1 {
- offset = 0
- } else {
- offset = (page - 1) * page_z
- }
- var maps_z []orm2.ParamsList
- var r []StockOutProductList
- sqlWhere := ""
- if len(T_name) > 0 {
- sqlWhere += " AND (so.t_number like \"%" + T_name + "%\" or so.t_remark like \"%" + T_name + "%\" or so.t_project like \"%" + T_name + "%\")"
- }
- if T_depot_id > 0 {
- sqlWhere += fmt.Sprintf(" AND so.t_depot_id = %d", T_depot_id)
- }
- if len(T_start_date) > 0 {
- sqlWhere += " AND so.t_date >= \"" + T_start_date + "\""
- }
- if len(T_end_date) > 0 {
- sqlWhere += " AND so.t_date <= \"" + T_end_date + "\""
- }
- if len(T_product_name) > 0 {
- sqlWhere += " AND p.t_name like \"%" + T_product_name + "%\""
- }
- if len(T_product_model) > 0 {
- sqlWhere += " AND p.t_model like \"%" + T_product_model + "%\""
- }
- // 获取总条数
- sql := "SELECT COUNT(product.ID) FROM stock_out_product product " +
- "JOIN product p ON product.t_product_id = p.ID " +
- "LEFT JOIN stock_out so ON so.t_number=product.t_number WHERE 1=1 "
- sql = sql + sqlWhere
- fmt.Println(sql)
- _, err := dao.orm.Raw(sql).ValuesList(&maps_z)
- if err != nil {
- return r, 0
- }
- if len(maps_z) == 0 {
- return r, 0
- }
- 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 " +
- "JOIN product p ON product.t_product_id = p.ID " +
- "LEFT JOIN stock_out so ON so.t_number=product.t_number WHERE 1=1"
- sql = sql + sqlWhere
- sql += " ORDER BY so.t_date DESC"
- if page_z != 9999 {
- sql += " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
- }
- fmt.Println(sql)
- _, err = dao.orm.Raw(sql).QueryRows(&r)
- for _, v := range r {
- r_ = append(r_, StockOutProductListToStockOutProductList(v))
- }
- count, _ := strconv.Atoi(maps_z[0][0].(string))
- return r_, int64(count)
- }
|