1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249 |
- package controllers
- import (
- "ERP_storage/Nats/NatsServer"
- "ERP_storage/conf"
- "ERP_storage/logs"
- "ERP_storage/models/Account"
- "ERP_storage/models/Basic"
- "ERP_storage/models/Contract"
- "ERP_storage/models/Stock"
- "fmt"
- userlibs "git.baozhida.cn/ERP_libs/User"
- "git.baozhida.cn/ERP_libs/lib"
- "github.com/beego/beego/v2/adapter/orm"
- beego "github.com/beego/beego/v2/server/web"
- "github.com/robfig/cron/v3"
- "math"
- "strconv"
- "strings"
- "time"
- )
- type ContractController struct {
- beego.Controller
- User userlibs.User
- }
- func (c *ContractController) Prepare() {
- c.User = *Account.User_r
- }
- func (c *ContractController) Contract_GenT_number() {
- o := orm.NewOrm()
- ContractDao := Contract.NewContract(o)
- T_number := ""
- rand_x := 1
- for true {
- T_number = fmt.Sprintf("GZBZD-%s%03d", time.Now().Format("20060102"), rand_x)
- _, err := ContractDao.Read_Contract_ByT_number(T_number)
- if err != nil && err.Error() == orm.ErrNoRows.Error() {
- break
- }
- rand_x += 1
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
- c.ServeJSON()
- return
- }
- func (c *ContractController) Contract_List() {
- // 分页参数 初始化
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- // 查询
- T_name := c.GetString("T_name")
- T_state, _ := c.GetInt("T_state")
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- ContractDao := Contract.NewContract(orm.NewOrm())
- R_List, R_cnt := ContractDao.Read_Contract_List("", T_name, T_state, 0, page, page_z)
- var r_jsons lib.R_JSONS
- r_jsons.Num = R_cnt
- r_jsons.Data = R_List
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *ContractController) Contract_User_List() {
- // 分页参数 初始化
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- // 查询
- T_name := c.GetString("T_name")
- T_state, _ := c.GetInt("T_state")
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- ContractDao := Contract.NewContract(orm.NewOrm())
- R_List, R_cnt := ContractDao.Read_Contract_List(c.User.T_uuid, T_name, T_state, 0, page, page_z)
- var r_jsons lib.R_JSONS
- r_jsons.Num = R_cnt
- r_jsons.Data = R_List
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *ContractController) Contract_Get() {
- // 查询
- T_number := c.GetString("T_number")
- o := orm.NewOrm()
- ContractDao := Contract.NewContract(o)
- DeviceDao := Stock.NewDevice(o)
- ContractProductDao := Contract.NewContractProduct(o)
- contract, err := ContractDao.Read_Contract_ByT_number(T_number)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- productList := ContractProductDao.Read_ContractProduct_List(contract.T_number)
- var pList []Contract.ContractProduct_R
- for _, v := range productList {
- p := Contract.ContractProductToContractProduct_R(v)
- p.T_device_list, _ = DeviceDao.Read_DeviceSn_List(T_number, v.T_product_id, "", "")
- pList = append(pList, p)
- }
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Contract.ContractToContract_Detail(contract, pList)}
- c.ServeJSON()
- return
- }
- func (c *ContractController) Contract_Add() {
- T_number := c.GetString("T_number")
- T_customer := c.GetString("T_customer")
- T_money, _ := c.GetFloat("T_money") // 总价
- T_discount, _ := c.GetFloat("T_discount") // 优惠价
- T_date := c.GetString("T_date")
- T_product := c.GetString("T_product")
- T_remark := c.GetString("T_remark")
- T_pdf := c.GetString("T_pdf")
- T_project := c.GetString("T_project")
- T_recoveries := c.GetString("T_recoveries")
- T_invoice := c.GetString("T_invoice")
- T_submit := c.GetString("T_submit")
- if len(T_submit) == 0 {
- T_submit = c.User.T_uuid
- }
- var_ := Contract.Contract{
- T_number: T_number,
- T_customer: T_customer,
- T_money: float32(T_money),
- T_discount: float32(T_discount),
- T_project: T_project,
- T_type: 1, //合同类型 1-销售合同 2-验证合同
- T_date: T_date,
- T_out: 1,
- T_State: 3,
- T_remark: T_remark,
- T_pdf: T_pdf,
- T_submit: T_submit,
- T_recoveries: T_recoveries,
- T_invoice: T_invoice,
- }
- _, var_.T_recoveries_money = Contract.ContractToContractFinance_R(T_recoveries)
- _, var_.T_invoice_money = Contract.ContractToContractFinance_R(T_invoice)
- o := orm.NewOrm()
- o.Begin()
- ContractDao := Contract.NewContract(o)
- ContractProductDao := Contract.NewContractProduct(o)
- _, err := ContractDao.Read_Contract_ByT_number(T_number)
- if err == nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同编号重复!"}
- c.ServeJSON()
- return
- }
- _, err = ContractDao.Add_Contract(var_)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
- c.ServeJSON()
- return
- }
- if len(T_product) == 0 {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同产品明细不能为空"}
- c.ServeJSON()
- return
- }
- productList := lib.SplitString(T_product, "|")
- for _, v := range productList {
- product_id, _ := strconv.Atoi(strings.Split(v, ",")[0])
- num, _ := strconv.Atoi(strings.Split(v, ",")[1])
- contractProduct := Contract.ContractProduct{
- T_contract_number: T_number,
- T_product_id: product_id,
- T_product_total: num,
- T_product_out: 0, // 已出库数量
- T_State: 1, // 1-未出库 2-已部分出库 3-已全部出库
- }
- _, err = ContractProductDao.Add_ContractProduct(contractProduct)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
- c.ServeJSON()
- return
- }
- }
- o.Commit()
- NatsServer.AddUserLogs(c.User.T_uuid, "合同", "添加", var_)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
- c.ServeJSON()
- return
- }
- func (c *ContractController) Contract_Approval() {
- T_number := c.GetString("T_number")
- T_state, _ := c.GetInt("T_state")
- o := orm.NewOrm()
- ContractDao := Contract.NewContract(o)
- contract, err := ContractDao.Read_Contract_ByT_number(T_number)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
- c.ServeJSON()
- return
- }
- // 1-已通过 2-未通过
- contract.T_State = T_state
- contract.T_approver = c.User.T_uuid
- err = ContractDao.Update_Contract(contract, "T_State", "T_approver")
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
- c.ServeJSON()
- return
- }
- if T_state == 1 {
- NatsServer.AddNews(contract.T_submit, fmt.Sprintf("【合同审核】您提交的合同(%s)审核已通过", T_number), conf.ContractApprovalUrl)
- }
- if T_state == 2 {
- NatsServer.AddNews(contract.T_submit, fmt.Sprintf("【合同审核】您提交的合同(%s)审核未通过", T_number), conf.ContractApprovalUrl)
- }
- NatsServer.AddUserLogs(c.User.T_uuid, "合同", "审核", contract)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
- c.ServeJSON()
- return
- }
- func (c *ContractController) Contract_Edit() {
- T_number := c.GetString("T_number")
- T_customer := c.GetString("T_customer")
- T_money, _ := c.GetFloat("T_money")
- T_discount, _ := c.GetFloat("T_discount") // 优惠价
- T_date := c.GetString("T_date")
- T_product := c.GetString("T_product")
- T_remark := c.GetString("T_remark")
- T_pdf := c.GetString("T_pdf")
- T_project := c.GetString("T_project")
- T_recoveries := c.GetString("T_recoveries")
- T_invoice := c.GetString("T_invoice")
- T_submit := c.GetString("T_submit")
- o := orm.NewOrm()
- o.Begin()
- ContractDao := Contract.NewContract(o)
- ContractProductDao := Contract.NewContractProduct(o)
- contract, err := ContractDao.Read_Contract_ByT_number(T_number)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
- c.ServeJSON()
- return
- }
- if len(T_customer) > 0 {
- contract.T_customer = T_customer
- }
- if T_money > 0 {
- contract.T_money = float32(T_money)
- }
- if T_discount > 0 {
- contract.T_discount = float32(T_discount)
- }
- if len(T_date) > 0 {
- contract.T_date = T_date
- }
- if len(T_remark) > 0 {
- contract.T_remark = T_remark
- }
- if len(T_pdf) > 0 {
- contract.T_pdf = T_pdf
- }
- if len(T_project) > 0 {
- contract.T_project = T_project
- }
- if len(T_recoveries) > 0 {
- contract.T_recoveries = T_recoveries
- _, contract.T_recoveries_money = Contract.ContractToContractFinance_R(T_recoveries)
- }
- if len(T_invoice) > 0 {
- contract.T_invoice = T_invoice
- _, contract.T_invoice_money = Contract.ContractToContractFinance_R(T_invoice)
- }
- if len(T_submit) > 0 {
- contract.T_submit = T_submit
- }
- //1-已通过 2-未通过 3-待审核 合同状态为未通过,修改之后将状态更改为待审核
- if contract.T_State == 2 {
- contract.T_State = 3
- }
- err = ContractDao.Update_Contract(contract, "T_customer", "T_money", "T_discount", "T_date", "T_remark", "T_pdf", "T_State",
- "T_project", "T_recoveries", "T_recoveries_money", "T_invoice", "T_invoice_money", "T_submit")
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
- c.ServeJSON()
- return
- }
- if len(T_product) == 0 {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同产品明细不能为空"}
- c.ServeJSON()
- return
- }
- productList := lib.SplitString(T_product, "|")
- cpl, _ := ContractProductDao.Read_ContractProductList_ByT_number(T_number)
- needToDeleteList, allList := Check_NeedToDelete_Product_List(productList, cpl)
- // 检查编辑时需要修改的产品
- for k, v := range needToDeleteList {
- id, _ := strconv.Atoi(strings.Split(k, "-")[0])
- productId, _ := strconv.Atoi(strings.Split(k, "-")[1])
- if v > 0 {
- o.Rollback()
- product, _ := Basic.Read_Product_ById(productId)
- c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s已(部分)出库,禁止删除!", product.T_name)}
- c.ServeJSON()
- return
- }
- cp := Contract.ContractProduct{Id: id}
- err = ContractProductDao.Delete_ContractProduct(cp)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
- c.ServeJSON()
- return
- }
- }
- for _, v := range productList {
- product_id, _ := strconv.Atoi(strings.Split(v, ",")[0])
- num, _ := strconv.Atoi(strings.Split(v, ",")[1])
- oldContractProduct, ok := allList[product_id]
- if !ok {
- contractProduct := Contract.ContractProduct{
- T_contract_number: T_number,
- T_product_id: product_id,
- T_product_total: num,
- T_product_out: 0, // 已出库数量
- T_State: 1, // 1-未出库 2-已部分出库 3-已全部出库
- }
- _, err = ContractProductDao.Add_ContractProduct(contractProduct)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
- c.ServeJSON()
- return
- } else {
- continue
- }
- }
- // 修改产品总数量
- contractProductId, _ := strconv.Atoi(strings.Split(oldContractProduct, "-")[0])
- oldNum, _ := strconv.Atoi(strings.Split(oldContractProduct, "-")[1])
- if oldNum == num {
- continue
- }
- cp := Contract.ContractProduct{Id: contractProductId, T_product_total: num}
- err = ContractProductDao.Update_ContractProduct(cp, "T_product_total")
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
- c.ServeJSON()
- return
- }
- }
- o.Commit()
- NatsServer.AddUserLogs(c.User.T_uuid, "合同", "修改", contract)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: contract.Id}
- c.ServeJSON()
- return
- }
- func (c *ContractController) Contract_Del() {
- T_number := c.GetString("T_number")
- o := orm.NewOrm()
- o.Begin()
- ContractDao := Contract.NewContract(o)
- ContractProductDao := Contract.NewContractProduct(o)
- contract, err := ContractDao.Read_Contract_ByT_number(T_number)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
- c.ServeJSON()
- return
- }
- //1-已通过 2-未通过 3-待审核 审核状态
- if contract.T_State == 1 {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同已审核通过,禁止删除!"}
- c.ServeJSON()
- return
- }
- err = ContractDao.Delete_Contract(contract)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
- c.ServeJSON()
- return
- }
- //合同类型 1-销售合同 2-验证合同
- cpList := ContractProductDao.Read_ContractProduct_List(T_number)
- for _, v := range cpList {
- err = ContractProductDao.Delete_ContractProduct(v)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
- c.ServeJSON()
- return
- }
- }
- o.Commit()
- NatsServer.AddUserLogs(c.User.T_uuid, "合同", "删除", T_number)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
- c.ServeJSON()
- return
- }
- func (c *ContractController) Contract_List_For_Out() {
- // 分页参数 初始化
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- // 查询
- T_name := c.GetString("T_name")
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- ContractDao := Contract.NewContract(orm.NewOrm())
- R_List, R_cnt := ContractDao.Read_Contract_List("", T_name, 1, 1, page, page_z)
- var r_jsons lib.R_JSONS
- r_jsons.Num = R_cnt
- r_jsons.Data = R_List
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 合同下的产品列表 出库时使用
- func (c *ContractController) Contract_Product_List() {
- // 查询
- T_number := c.GetString("T_number")
- T_depot_id, _ := c.GetInt("T_depot_id")
- o := orm.NewOrm()
- ContractDao := Contract.NewContract(o)
- ContractProductDao := Contract.NewContractProduct(o)
- contract, err := ContractDao.Read_Contract_ByT_number(T_number)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- productList := ContractProductDao.Read_ContractProduct_OutList(contract.T_number)
- Stock.Read_Stock_All_Map(T_depot_id)
- var pList []Contract.ContractProduct_Out
- for _, v := range productList {
- p := Contract.ContractProductToContractProduct_Out(v)
- p.T_stock_total = Stock.Read_Stock_Get(T_depot_id, p.T_product_id)
- pList = append(pList, p)
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: pList}
- c.ServeJSON()
- return
- }
- // 检查需要删除的合同产品列表
- func Check_NeedToDelete_Product_List(list []string, newList []Contract.ContractProduct) (needToDelete map[string]int, all map[int]string) {
- var productMap = map[int]int{}
- needToDelete = make(map[string]int)
- all = make(map[int]string)
- for _, v := range list {
- product_id, _ := strconv.Atoi(strings.Split(v, ",")[0])
- num, _ := strconv.Atoi(strings.Split(v, ",")[1])
- productMap[product_id] = num
- }
- for _, product := range newList {
- all[product.T_product_id] = fmt.Sprintf("%d-%d", product.Id, product.T_product_total)
- _, ok := productMap[product.T_product_id]
- if !ok {
- id := fmt.Sprintf("%d-%d", product.Id, product.T_product_id)
- needToDelete[id] = product.T_product_out
- }
- }
- return
- }
- // 验证合同
- func (c *ContractController) VerifyContract_List() {
- // 分页参数 初始化
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- T_customer_id := c.GetString("T_customer_id")
- // 查询
- T_name := c.GetString("T_name")
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- ContractDao := Contract.NewContract(orm.NewOrm())
- R_List, R_cnt := ContractDao.Read_VerifyContract_List(T_name, T_customer_id, page, page_z)
- var r_jsons lib.R_JSONS
- r_jsons.Num = R_cnt
- r_jsons.Data = R_List
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 验证合同呢添加
- func (c *ContractController) VerifyContract_Add() {
- T_number := c.GetString("T_number")
- T_customer_id := c.GetString("T_customer_id")
- T_money, _ := c.GetFloat("T_money")
- T_discount, _ := c.GetFloat("T_discount")
- T_date := c.GetString("T_date")
- T_product := c.GetString("T_product")
- T_submit := c.GetString("T_submit")
- T_remark := c.GetString("T_remark")
- T_pdf := c.GetString("T_pdf")
- T_project := c.GetString("T_project")
- T_recoveries := c.GetString("T_recoveries")
- T_invoice := c.GetString("T_invoice")
- T_start_date := c.GetString("T_start_date")
- T_end_date := c.GetString("T_end_date")
- var_ := Contract.Contract{
- T_number: T_number,
- T_customer_id: T_customer_id,
- T_money: float32(T_money),
- T_discount: float32(T_discount),
- T_project: T_project,
- T_type: 2, //合同类型 1-销售合同 2-验证合同
- T_date: T_date,
- T_State: 1,
- T_remark: T_remark,
- T_pdf: T_pdf,
- T_submit: T_submit,
- T_recoveries: T_recoveries,
- T_invoice: T_invoice,
- T_start_date: T_start_date,
- T_end_date: T_end_date,
- }
- _, var_.T_recoveries_money = Contract.ContractToContractFinance_R(T_recoveries)
- _, var_.T_invoice_money = Contract.ContractToContractFinance_R(T_invoice)
- o := orm.NewOrm()
- o.Begin()
- ContractDao := Contract.NewContract(o)
- ContractProductDao := Contract.NewContractProduct(o)
- _, err := ContractDao.Read_Contract_ByT_date(T_customer_id, T_start_date, T_end_date)
- if err == nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同签约时间重复!"}
- c.ServeJSON()
- return
- }
- _, err = ContractDao.Read_Contract_ByT_number(T_number)
- if err == nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同编号重复!"}
- c.ServeJSON()
- return
- }
- ContractID, err := ContractDao.Add_Contract(var_)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
- c.ServeJSON()
- return
- }
- if len(T_product) == 0 {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "验证合同明细不能为空"}
- c.ServeJSON()
- return
- }
- productList := lib.SplitString(T_product, "|")
- ContractOut := 0
- for _, v := range productList {
- product_id, _ := strconv.Atoi(strings.Split(v, ",")[0])
- num, _ := strconv.Atoi(strings.Split(v, ",")[1])
- price := lib.StringToFloat64TwoDecimal(strings.Split(v, ",")[2])
- product, _ := Basic.Read_Product_ById(product_id)
- contractProduct := Contract.ContractProduct{
- T_contract_number: T_number,
- T_product_id: product_id,
- T_product_total: num,
- T_product_out: 0, // 已出库数量
- T_price: float32(price), // 价格
- T_State: 1, // 1-未出库 2-已部分出库 3-已全部出库
- }
- if product.T_class < 0 {
- contractProduct.T_State = 3
- }
- if product.T_class == 0 {
- ContractOut = 1
- }
- _, err = ContractProductDao.Add_ContractProduct(contractProduct)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
- c.ServeJSON()
- return
- }
- }
- o.Commit()
- o2 := orm.NewOrm()
- ContractDao2 := Contract.NewContract(o2)
- if ContractOut == 1 {
- err = ContractDao2.Update_Contract(Contract.Contract{Id: int(ContractID), T_out: ContractOut}, "T_out")
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同出库状态失败"}
- c.ServeJSON()
- return
- }
- }
- err = Update_VerifyContract_State(T_customer_id, "ADD")
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新状态失败"}
- c.ServeJSON()
- return
- }
- NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "添加", var_)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
- c.ServeJSON()
- return
- }
- // 验证合同呢修改
- func (c *ContractController) VerifyContract_Edit() {
- T_number := c.GetString("T_number")
- T_money, _ := c.GetFloat("T_money")
- T_discount, _ := c.GetFloat("T_discount")
- T_date := c.GetString("T_date")
- T_product := c.GetString("T_product")
- T_remark := c.GetString("T_remark")
- T_pdf := c.GetString("T_pdf")
- T_project := c.GetString("T_project")
- T_recoveries := c.GetString("T_recoveries")
- T_invoice := c.GetString("T_invoice")
- T_start_date := c.GetString("T_start_date")
- T_end_date := c.GetString("T_end_date")
- T_submit := c.GetString("T_submit")
- o := orm.NewOrm()
- o.Begin()
- ContractDao := Contract.NewContract(o)
- ContractProductDao := Contract.NewContractProduct(o)
- contract, err := ContractDao.Read_Contract_ByT_number(T_number)
- if err != nil || contract.T_type == 1 {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
- c.ServeJSON()
- return
- }
- if T_money > 0 {
- contract.T_money = float32(T_money)
- }
- if T_discount > 0 {
- contract.T_discount = float32(T_discount)
- }
- if len(T_date) > 0 {
- contract.T_date = T_date
- }
- if len(T_remark) > 0 {
- contract.T_remark = T_remark
- }
- if len(T_pdf) > 0 {
- contract.T_pdf = T_pdf
- }
- if len(T_project) > 0 {
- contract.T_project = T_project
- }
- if len(T_recoveries) > 0 {
- contract.T_recoveries = T_recoveries
- _, contract.T_recoveries_money = Contract.ContractToContractFinance_R(T_recoveries)
- }
- if len(T_invoice) > 0 {
- contract.T_invoice = T_invoice
- _, contract.T_invoice_money = Contract.ContractToContractFinance_R(T_invoice)
- }
- if len(T_start_date) > 0 {
- contract.T_start_date = T_start_date
- }
- if len(T_end_date) > 0 {
- contract.T_end_date = T_end_date
- }
- if len(T_submit) > 0 {
- contract.T_submit = T_submit
- }
- err = ContractDao.Update_Contract(contract, "T_money", "T_discount", "T_date", "T_remark", "T_pdf", "T_State", "T_project",
- "T_recoveries", "T_recoveries_money", "T_invoice", "T_invoice_money", "T_start_date", "T_end_date", "T_submit")
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
- c.ServeJSON()
- return
- }
- if len(T_product) == 0 {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
- c.ServeJSON()
- return
- }
- productList := lib.SplitString(T_product, "|")
- cpl, _ := ContractProductDao.Read_ContractProductList_ByT_number(T_number)
- needToDeleteList, allList := Check_NeedToDelete_Product_List(productList, cpl)
- // 检查编辑时需要修改的产品
- for k, v := range needToDeleteList {
- id, _ := strconv.Atoi(strings.Split(k, "-")[0])
- productId, _ := strconv.Atoi(strings.Split(k, "-")[1])
- if v > 0 {
- o.Rollback()
- product, _ := Basic.Read_Product_ById(productId)
- c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s已(部分)出库,禁止删除!", product.T_name)}
- c.ServeJSON()
- return
- }
- cp := Contract.ContractProduct{Id: id}
- err = ContractProductDao.Delete_ContractProduct(cp)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
- c.ServeJSON()
- return
- }
- }
- for _, v := range productList {
- product_id, _ := strconv.Atoi(strings.Split(v, ",")[0])
- num, _ := strconv.Atoi(strings.Split(v, ",")[1])
- price := lib.StringToFloat64TwoDecimal(strings.Split(v, ",")[2])
- oldContractProduct, ok := allList[product_id]
- if !ok {
- contractProduct := Contract.ContractProduct{
- T_contract_number: T_number,
- T_product_id: product_id,
- T_product_total: num,
- T_product_out: 0, // 已出库数量
- T_State: 1, // 1-未出库 2-已部分出库 3-已全部出库
- }
- _, err = ContractProductDao.Add_ContractProduct(contractProduct)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
- c.ServeJSON()
- return
- } else {
- continue
- }
- }
- // 修改产品总数量
- contractProductId, _ := strconv.Atoi(strings.Split(oldContractProduct, "-")[0])
- oldNum, _ := strconv.Atoi(strings.Split(oldContractProduct, "-")[1])
- if oldNum == num {
- continue
- }
- cp := Contract.ContractProduct{
- Id: contractProductId,
- T_product_total: num,
- T_price: float32(price), // 价格
- }
- err = ContractProductDao.Update_ContractProduct(cp, "T_product_total")
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
- c.ServeJSON()
- return
- }
- }
- o.Commit()
- err = Update_VerifyContract_State(contract.T_customer_id, "UPDATE")
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新状态失败"}
- c.ServeJSON()
- return
- }
- NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "修改", contract)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: contract.Id}
- c.ServeJSON()
- return
- }
- func (c *ContractController) VerifyContract_Del() {
- T_number := c.GetString("T_number")
- o := orm.NewOrm()
- o.Begin()
- ContractDao := Contract.NewContract(o)
- ContractProductDao := Contract.NewContractProduct(o)
- contract, err := ContractDao.Read_Contract_ByT_number(T_number)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
- c.ServeJSON()
- return
- }
- err = ContractDao.Delete_Contract(contract)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
- c.ServeJSON()
- return
- }
- cpList := ContractProductDao.Read_ContractProduct_List(T_number)
- for _, v := range cpList {
- err = ContractProductDao.Delete_ContractProduct(v)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
- c.ServeJSON()
- return
- }
- }
- o.Commit()
- err = Update_VerifyContract_State(contract.T_customer_id, "DELETE")
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改状态失败"}
- c.ServeJSON()
- return
- }
- NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "删除", T_number)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
- c.ServeJSON()
- return
- }
- func Update_VerifyContract_State(T_customer_id, flag string) error {
- o := orm.NewOrm()
- ContractDao := Contract.NewContract(o)
- VerifyContractDao := Contract.NewVerifyContract(o)
- vcn := ContractDao.Read_VerifyContract_Newest(T_customer_id)
- vc, err := VerifyContractDao.Read_VerifyContract_ByT_customer_id(T_customer_id)
- if err != nil {
- return err
- }
- if vcn.Id == 0 {
- vc.T_sign_times = 0
- vc.T_start_date = ""
- vc.T_end_date = ""
- vc.T_State = 1
- err = VerifyContractDao.Update_VerifyContract(vc, "T_start_date", "T_end_date", "T_sign_times", "T_State")
- return err
- }
- if flag == "ADD" {
- vc.T_sign_times += 1
- } else if flag == "DELETE" {
- vc.T_sign_times -= 1
- }
- if len(vc.T_start_date) == 0 {
- vc.T_start_date = vcn.T_start_date
- }
- if vc.T_end_date != vcn.T_end_date {
- vc.T_end_date = vcn.T_end_date
- }
- nowTime := time.Now()
- now := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, time.Local)
- end, _ := lib.DateStrToTime(vcn.T_end_date)
- now2Month := now.AddDate(0, 2, 0)
- // 1-未签约 2-已作废 3-已签约 4-即将到期
- state := 1
- // 结束时间<今天 已过期
- if end.Before(now) {
- state = 2
- } else {
- // 结束时间<今天+2月 即将到期 && 结束时间>今天
- if end.Before(now2Month) || end.Equal(now) {
- state = 4
- NatsServer.AddNews(vcn.T_approver, fmt.Sprintf("【合同即将过期提醒】%s的验证合同将于%s过期!", vc.T_customer, vc.T_end_date), conf.VerifyContractUrl)
- }
- // 结束时间>今天+2月 已签约
- if end.After(now2Month) || end.Equal(now2Month) {
- state = 3
- }
- }
- vc.T_State = state
- err = VerifyContractDao.Update_VerifyContract(vc, "T_start_date", "T_end_date", "T_sign_times", "T_State")
- return err
- }
- // 验证合同-添加客户
- func (c *ContractController) VerifyContract_Add_Customer() {
- T_customer := c.GetString("T_customer")
- o := orm.NewOrm()
- VerifyContractDao := Contract.NewVerifyContract(o)
- T_number := ""
- for true {
- rand_x := 1
- T_number = fmt.Sprintf("YZKH-%s", lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x)))
- _, err := VerifyContractDao.Read_VerifyContract_ByT_customer_id(T_number)
- if err != nil && err.Error() == orm.ErrNoRows.Error() {
- break
- }
- rand_x += 1
- }
- var_ := Contract.VerifyContract{
- T_customer: T_customer,
- T_customer_id: T_number,
- T_State: 1,
- }
- _, err := VerifyContractDao.Add_VerifyContract(var_)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
- c.ServeJSON()
- return
- }
- NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "添加客户", var_)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
- c.ServeJSON()
- return
- }
- // 验证合同-编辑客户
- func (c *ContractController) VerifyContract_Update_Customer() {
- T_customer_id := c.GetString("T_customer_id")
- T_customer := c.GetString("T_customer")
- o := orm.NewOrm()
- VerifyContractDao := Contract.NewVerifyContract(o)
- vc, err := VerifyContractDao.Read_VerifyContract_ByT_customer_id(T_customer_id)
- vc.T_customer = T_customer
- err = VerifyContractDao.Update_VerifyContract(vc)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
- c.ServeJSON()
- return
- }
- NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "编辑客户", vc)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_customer_id}
- c.ServeJSON()
- return
- }
- // 验证合同-客户列表
- func (c *ContractController) VerifyContract_Customer_List() {
- // 分页参数 初始化
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- // 查询
- T_name := c.GetString("T_name")
- T_state, _ := c.GetInt("T_state")
- VerifyContractDao := Contract.NewVerifyContract(orm.NewOrm())
- R_List, R_cnt := VerifyContractDao.Read_VerifyContract_List(T_name, T_state, page, page_z)
- var r_jsons lib.R_JSONS
- r_jsons.Num = R_cnt
- r_jsons.Data = R_List
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 合同回款明细列表
- func (c *ContractController) Contract_List_RecoveriesMoney() {
- // 分页参数 初始化
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- // 查询
- T_name := c.GetString("T_name")
- T_recoveries_state, _ := c.GetInt("T_recoveries_state") // 回款状态 1 未回款 2 部分回款 3 全部回款
- T_invoice_state, _ := c.GetInt("T_invoice_state") // 开票状态 1 未开票 2 部分开票 3 全部开票
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- ContractDao := Contract.NewContract(orm.NewOrm())
- R_List, R_cnt := ContractDao.Read_Contract_List_For_RecoveriesAndInvoice(T_name, T_recoveries_state, T_invoice_state, page, page_z)
- var r_jsons lib.R_JSONS
- r_jsons.Num = R_cnt
- r_jsons.Data = R_List
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *ContractController) Contract_Stat() {
- T_type, _ := c.GetInt("T_type")
- T_year, _ := c.GetInt("T_year")
- type R_JSONS struct {
- ContractMoneyByYear interface{}
- TotalMoneyByYear float32
- RecoveriesMoneyByYear float32
- ContractNumByYear interface{}
- TotalMumByYear int64
- ContractMoneyByMonth interface{}
- TotalMoneyByMonth float32
- RecoveriesMoneyByMonth float32
- ContractNumByMonth interface{}
- TotalMumByMonth int64
- TotalMoneyByThisYear float32 // 今年合同总金额
- RecoveriesMoneyByThisYear float32 // 今年回款总金额
- TotalMumByThisYear int64 // 今年合同总数
- TotalMoneyByLastYear float32 // 去年合同总金额
- RecoveriesMoneyByLastYear float32 // 去年回款总金额
- TotalMumByLastYear int64 // 去年合同总数
- }
- var r_jsons R_JSONS
- thisYear := time.Now().Year()
- if T_year == 0 {
- T_year = thisYear
- }
- ContractDao := Contract.NewContract(orm.NewOrm())
- monryStat, moneyTotal, moneyRecoveries := ContractDao.Stat_ContractMoney_ByYear(T_type)
- numStat, numTotal := ContractDao.Stat_ContractNum_ByYear(T_type)
- monryMonthStat, moneyMonthTotal, moneyMonthRecoveries := ContractDao.Stat_ContractMoney_ByMonth(T_type, T_year)
- numMonthStat, numMonthTotal := ContractDao.Stat_ContractNum_ByMonth(T_type, T_year)
- // 去年
- _, r_jsons.TotalMoneyByLastYear, r_jsons.RecoveriesMoneyByLastYear = ContractDao.Stat_ContractMoney_ByMonth(T_type, T_year-1)
- _, r_jsons.TotalMumByLastYear = ContractDao.Stat_ContractNum_ByMonth(T_type, T_year-1)
- r_jsons.ContractMoneyByYear = monryStat
- r_jsons.TotalMoneyByYear = moneyTotal
- r_jsons.RecoveriesMoneyByYear = moneyRecoveries
- r_jsons.ContractNumByYear = numStat
- r_jsons.TotalMumByYear = numTotal
- r_jsons.ContractMoneyByMonth = monryMonthStat
- r_jsons.TotalMoneyByMonth = moneyMonthTotal
- r_jsons.RecoveriesMoneyByMonth = moneyMonthRecoveries
- r_jsons.ContractNumByMonth = numMonthStat
- r_jsons.TotalMumByMonth = numMonthTotal
- r_jsons.TotalMoneyByThisYear = moneyMonthTotal
- r_jsons.RecoveriesMoneyByThisYear = moneyMonthRecoveries
- r_jsons.TotalMumByThisYear = numMonthTotal
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func Cron_VerifyContract() {
- //创建一个定时任务对象
- c := cron.New(cron.WithSeconds())
- //给对象增加定时任务
- //c.AddFunc("0 */1 * * * ?", Cron_VerifyContract_ChangeState)
- c.AddFunc("@daily", Cron_VerifyContract_ChangeState)
- //启动定时任务
- c.Start()
- defer c.Stop()
- //查询语句,阻塞,让main函数不退出,保持程序运行
- select {}
- }
- // 修改合同状态
- func Cron_VerifyContract_ChangeState() {
- T_date := time.Now().Format("2006-01-02")
- logs.Info("开始处理" + T_date + "验证合同状态")
- VerifyContractDao := Contract.NewVerifyContract(orm.NewOrm())
- R_List, _ := VerifyContractDao.Read_VerifyContract_List("", 0, 0, 999)
- for _, v := range R_List {
- err := Update_VerifyContract_State(v.T_customer_id, "UPDATE")
- if err != nil {
- logs.Error("修改合同状态失败")
- }
- }
- }
- // 修改回款金额及开票金额
- func UpdateVerifyContract_RecoveriesInvoice_Money() {
- ContractDao := Contract.NewContract(orm.NewOrm())
- R_List, _ := ContractDao.Read_VerifyContract_List("", "", 0, 9999)
- for _, verify := range R_List {
- R := Contract.Contract{Id: verify.Id}
- _, R.T_recoveries_money = Contract.ContractToContractFinance_R(verify.T_recoveries)
- _, R.T_invoice_money = Contract.ContractToContractFinance_R(verify.T_invoice)
- ContractDao.Update_Contract(R, "T_recoveries_money", "T_invoice_money")
- }
- }
|