| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502 |
- package controllers
- import (
- "ERP_storage/Nats"
- "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"
- "github.com/beego/beego/v2/adapter/orm"
- beego "github.com/beego/beego/v2/server/web"
- "github.com/robfig/cron/v3"
- "github.com/xuri/excelize/v2"
- natslibs "gogs.baozhida.cn/zoie/ERP_libs/Nats"
- userlibs "gogs.baozhida.cn/zoie/ERP_libs/User"
- "gogs.baozhida.cn/zoie/ERP_libs/lib"
- "math"
- "os"
- "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() {
- Id, _ := c.GetInt("Id")
- 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_ById(Id)
- if err != nil || contract.T_type == 1 {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
- c.ServeJSON()
- return
- }
- oldT_number := contract.T_number
- if T_number != oldT_number {
- _, err1 := ContractDao.Read_Contract_ByT_number(T_number)
- if err1 == nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("合同编号%s已存在!", T_number)}
- c.ServeJSON()
- return
- }
- contract.T_number = T_number
- }
- 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_number", "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(oldT_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 && T_number == oldT_number {
- continue
- }
- cp := Contract.ContractProduct{
- Id: contractProductId,
- T_contract_number: T_number,
- T_product_total: num,
- T_price: float32(price), // 价格
- }
- err = ContractProductDao.Update_ContractProduct(cp, "T_product_total", "T_contract_number")
- 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_submit := c.GetString("T_submit")
- T_name := c.GetString("T_name")
- T_start_date := c.GetString("T_start_date") // 回款开始日期
- T_end_date := c.GetString("T_end_date") // 回款结束日期
- T_recoveries_state, _ := c.GetInt("T_recoveries_state") // 回款状态 1 未回款 2 部分回款 3 全部回款
- T_invoice_state, _ := c.GetInt("T_invoice_state") // 开票状态 1 未开票 2 部分开票 3 全部开票
- T_percentage_state, _ := c.GetInt("T_percentage_state") // 提成状态 1-未打款 2-已打款
- T_mine, _ := c.GetInt("T_mine") // 我的 用于合同提成显示
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- Contract.Read_VerifyContract_All_Map()
- ContractDao := Contract.NewContract(orm.NewOrm())
- R_List, R_cnt := ContractDao.Read_Contract_List_For_RecoveriesAndInvoice(c.User.T_uuid, T_submit, T_mine, T_name, T_start_date, T_end_date, T_recoveries_state, T_invoice_state, T_percentage_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_Export() {
- // 查询
- T_submit := c.GetString("T_submit")
- T_name := c.GetString("T_name")
- T_start_date := c.GetString("T_start_date") // 回款开始日期
- T_end_date := c.GetString("T_end_date") // 回款结束日期
- T_recoveries_state, _ := c.GetInt("T_recoveries_state") // 回款状态 1 未回款 2 部分回款 3 全部回款
- T_invoice_state, _ := c.GetInt("T_invoice_state") // 开票状态 1 未开票 2 部分开票 3 全部开票
- T_percentage_state, _ := c.GetInt("T_percentage_state") // 提成状态 1-未打款 2-已打款
- T_mine, _ := c.GetInt("T_mine") // 我的 用于合同提成显示
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- Contract.Read_VerifyContract_All_Map()
- ContractDao := Contract.NewContract(orm.NewOrm())
- R_List, _ := ContractDao.Read_Contract_List_For_RecoveriesAndInvoice(c.User.T_uuid, T_submit, T_mine, T_name, T_start_date, T_end_date, T_recoveries_state, T_invoice_state, T_percentage_state, 0, 9999)
- title := "销售提成明细表"
- if T_mine == 1 {
- title = fmt.Sprintf("%s-销售提成明细表", c.User.T_name)
- }
- filename := fmt.Sprintf("%s(%s)", title, lib.GetRandstring(6, "0123456789", 0))
- f := excelize.NewFile() // 设置单元格的值
- Style1, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
- })
- Style2, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Bold: true, Size: 11, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
- Border: []excelize.Border{
- {Type: "left", Color: "000000", Style: 1},
- {Type: "top", Color: "000000", Style: 1},
- {Type: "bottom", Color: "000000", Style: 1},
- {Type: "right", Color: "000000", Style: 1},
- },
- })
- Style3, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Size: 11, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
- Border: []excelize.Border{
- {Type: "left", Color: "000000", Style: 1},
- {Type: "top", Color: "000000", Style: 1},
- {Type: "bottom", Color: "000000", Style: 1},
- {Type: "right", Color: "000000", Style: 1},
- },
- })
- f.MergeCell("Sheet1", "A1", "J1")
- f.SetRowStyle("Sheet1", 1, 1, Style1)
- f.SetCellValue("Sheet1", "A1", title)
- f.SetRowHeight("Sheet1", 1, 30)
- // 这里设置表头
- f.SetCellStyle("Sheet1", "A2", "J2", Style2)
- f.SetRowHeight("Sheet1", 2, 20)
- f.SetCellValue("Sheet1", "A2", "公司名称")
- f.SetCellValue("Sheet1", "B2", "合同编号")
- f.SetCellValue("Sheet1", "C2", "签约时间")
- f.SetCellValue("Sheet1", "D2", "合同金额")
- f.SetCellValue("Sheet1", "E2", "回款金额")
- f.SetCellValue("Sheet1", "F2", "回款比例")
- f.SetCellValue("Sheet1", "G2", "销售类型")
- f.SetCellValue("Sheet1", "H2", "销售提成5%")
- f.SetCellValue("Sheet1", "I2", "提成金额")
- f.SetCellValue("Sheet1", "J2", "提成状态")
- // 设置列宽
- f.SetColWidth("Sheet1", "A", "A", 25)
- f.SetColWidth("Sheet1", "B", "I", 15)
- line := 2
- for _, contract := range R_List {
- line += 1
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-2)
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), contract.T_customer)
- f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), contract.T_number)
- f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), contract.T_date)
- f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), contract.T_discount)
- f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), contract.T_recoveries_money)
- f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), fmt.Sprintf("%.02f%%", contract.T_recoveries_money/contract.T_discount*100))
- T_type_str := "验证合同"
- if contract.T_type == 1 {
- T_type_str = "销售合同"
- }
- f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), T_type_str)
- f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), fmt.Sprintf("%.02f", contract.T_discount*0.05))
- f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), fmt.Sprintf("%.02f", contract.T_discount*0.05))
- T_percentage_str := "未打款"
- if contract.T_percentage_state == 2 {
- T_percentage_str = "已打款"
- }
- f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), T_percentage_str)
- }
- f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("J%d", line), Style3)
- // 保存文件
- if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
- fmt.Println(err)
- }
- var url string
- //// 上传 OSS
- nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
- url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
- c.ServeJSON()
- return
- }
- //删除目录
- err := os.Remove("ofile/" + filename + ".xlsx")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
- 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")
- }
- }
- // 修改回款时间
- func UpdateVerifyContract_RecoveriesInvoice_Time() {
- ContractDao := Contract.NewContract(orm.NewOrm())
- R_List, _ := ContractDao.Read_VerifyContract_List("", "", 0, 9999)
- for _, verify := range R_List {
- R := Contract.Contract{Id: verify.Id}
- if verify.T_discount == verify.T_recoveries_money {
- recoveriesList, _ := Contract.ContractToContractFinance_R(verify.T_recoveries)
- if len(recoveriesList) > 0 {
- R.T_recoveries_time = recoveriesList[len(recoveriesList)-1].T_date
- ContractDao.Update_Contract(R, "T_recoveries_time")
- }
- }
- }
- }
- // 合同提成打款
- func (c *ContractController) Contract_Percentage_Remit() {
- T_number := c.GetString("T_number")
- o := orm.NewOrm()
- ContractDao := Contract.NewContract(o)
- contract, err := ContractDao.Read_Contract_ByT_number(T_number)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
- c.ServeJSON()
- return
- }
- if contract.T_recoveries_money != contract.T_discount {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同未回款,不可打款!"}
- c.ServeJSON()
- return
- }
- contract.T_percentage_state = 2 // 已打款
- err = ContractDao.Update_Contract(contract, "T_percentage_state")
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 203, 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) User_List() {
- var r_jsons lib.R_JSONS
- // 分页参数 初始化
- 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")
- ContractDao := Contract.NewContract(orm.NewOrm())
- uuidList := ContractDao.Read_T_submit_List()
- if len(uuidList) == 0 {
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- R_List, R_cnt, err := NatsServer.Read_User_List_T_uuid(T_name, uuidList, page, page_z)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败"}
- c.ServeJSON()
- return
- }
- var U_List []userlibs.User
- for _, user := range R_List {
- U_List = append(U_List, user)
- }
- r_jsons.Num = R_cnt
- r_jsons.Data = U_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
- }
|