package controllers import ( "ERP_salary/Nats/NatsServer" "ERP_salary/conf" "ERP_salary/logs" "ERP_salary/models/Account" "ERP_salary/models/Reimburse" "encoding/json" "fmt" powerlibs "git.baozhida.cn/ERP_libs/Power" userlibs "git.baozhida.cn/ERP_libs/User" "git.baozhida.cn/ERP_libs/lib" "github.com/beego/beego/v2/adapter/orm" "strconv" "strings" //"github.com/beego/beego/v2/client/orm" beego "github.com/beego/beego/v2/server/web" "math" "time" ) type ReimburseController struct { beego.Controller User userlibs.User } func (c *ReimburseController) Prepare() { c.User = *Account.User_r } func (c *ReimburseController) CheckRBAC(T_power string) { o := orm.NewOrm() powerDao := powerlibs.NewPower(o) power, err := powerDao.Read_Power_ByT_id(T_power) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权访问!"} c.ServeJSON() return } if power.T_menu == "*" { return } api := Account.Read_API_List_ByPower(power.T_id, power.T_menu) flag := false for _, v := range api { if v.T_uri == c.Ctx.Request.URL.Path { flag = true break } } if !flag { c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权访问!"} c.ServeJSON() return } } func (c *ReimburseController) 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") ReimburseDao := Reimburse.NewReimburse(orm.NewOrm()) uuidList := ReimburseDao.Read_T_uuid_List() if len(uuidList) == 0 { c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } WaitAuditList := ReimburseDao.Read_T_uuid_List_by_State(Reimburse.WaitAudit) WaitAuditListMap := make(map[string]struct{}) for _, v := range WaitAuditList { WaitAuditListMap[v] = struct{}{} } type User struct { userlibs.User T_wait_audit bool `json:"T_wait_audit"` } 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 []User for _, user := range R_List { var T_wait_audit bool if _, ok := WaitAuditListMap[user.T_uuid]; ok { T_wait_audit = true } U_List = append(U_List, User{user, T_wait_audit}) } 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 } // 消费类型 func (c *ReimburseController) FeeType_List() { type Reimburse_R struct { Id int T_name string T_mid int T_State int FeeDetails []Reimburse.FeeType } var res []Reimburse_R list := Reimburse.Read_FeeType_List() for _, v := range list { res = append(res, Reimburse_R{ Id: v.Id, T_name: v.T_name, T_mid: v.T_mid, T_State: v.T_State, FeeDetails: Reimburse.Read_FeeDetails_List(v.Id), }) } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: res} c.ServeJSON() return } //func (c *ReimburseController) FeeType_List() { // c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Reimburse.Read_FeeType_List()} // c.ServeJSON() // return //} // 消费明细 func (c *ReimburseController) FeeDetails_List() { T_feeType, _ := c.GetInt("T_feeType") if T_feeType == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_feeType Err!"} c.ServeJSON() return } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Reimburse.Read_FeeDetails_List(T_feeType)} c.ServeJSON() return } // 管理元报销列表 func (c *ReimburseController) Reimburse_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_uuid := c.GetString("T_uuid") userList, _ := NatsServer.Read_User_List_All() Account.Read_User_All_Map(userList) ReimburseDao := Reimburse.NewReimburse(orm.NewOrm()) reimburse_List, num := ReimburseDao.Read_Reimburse_Finance_List(T_uuid, page, page_z) var r_jsons lib.R_JSONS r_jsons.Data = reimburse_List r_jsons.Num = num r_jsons.Page = page r_jsons.Page_size = int(math.Ceil(float64(num) / float64(page_z))) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } // 员工报销列表 func (c *ReimburseController) Reimburse_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 } userList, _ := NatsServer.Read_User_List_All() Account.Read_User_All_Map(userList) ReimburseDao := Reimburse.NewReimburse(orm.NewOrm()) reimburse_List, num := ReimburseDao.Read_Reimburse_List(c.User.T_uuid, page, page_z) var r_jsons lib.R_JSONS r_jsons.Data = reimburse_List r_jsons.Num = num r_jsons.Page = page r_jsons.Page_size = int(math.Ceil(float64(num) / float64(page_z))) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *ReimburseController) Reimburse_Add() { type RequestBody struct { User_tokey string ReimburseDetails []Reimburse.ReimburseDetails T_money float32 } var body RequestBody data := c.Ctx.Input.RequestBody err := json.Unmarshal(data, &body) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "json 反序列化错误:" + err.Error()} c.ServeJSON() return } if len(body.ReimburseDetails) == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "报销明细不能为空"} c.ServeJSON() return } user_r, err := NatsServer.Verification(body.User_tokey, "") if err != nil { c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登陆!"} c.ServeJSON() return } // 权限验证 c.CheckRBAC(user_r.T_power) var_ := Reimburse.Reimburse{ T_uid: user_r.T_uuid, T_date: time.Now().Format("2006-01-02"), T_money: body.T_money, T_State: Reimburse.WaitSubmit, } o := orm.NewOrm() o.Begin() ReimburseDao := Reimburse.NewReimburse(o) ReimburseDetailsDao := Reimburse.NewReimburseDetails(o) id, err := ReimburseDao.Add_Reimburse(var_) if err != nil { o.Rollback() c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"} c.ServeJSON() return } for _, v := range body.ReimburseDetails { reimburseDetails := Reimburse.ReimburseDetails{ T_reimburse_id: int(id), T_trip_start_time: v.T_trip_start_time, T_trip_end_time: v.T_trip_end_time, T_trip_origin: v.T_trip_origin, T_trip_terminus: v.T_trip_terminus, T_fee_type: v.T_fee_type, T_fee_details: v.T_fee_details, T_money: v.T_money, T_reasons: v.T_reasons, T_project: v.T_project, T_img: v.T_img, T_State: 1, } _, err = ReimburseDetailsDao.Add_ReimburseDetails(reimburseDetails) 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: id} c.ServeJSON() return } func (c *ReimburseController) Reimburse_Edit() { type RequestBody struct { User_tokey string ReimburseDetails []Reimburse.ReimburseDetails T_money float32 T_id int } var body RequestBody data := c.Ctx.Input.RequestBody err := json.Unmarshal(data, &body) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "json.Unmarshal is err:" + err.Error()} c.ServeJSON() return } user_r, err := NatsServer.Verification(body.User_tokey, "") if err != nil { c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登陆!"} c.ServeJSON() return } // 权限验证 c.CheckRBAC(user_r.T_power) o := orm.NewOrm() o.Begin() ReimburseDao := Reimburse.NewReimburse(o) ReimburseDetailsDao := Reimburse.NewReimburseDetails(o) reimburse, err := ReimburseDao.Read_Reimburse_ById(body.T_id) if err != nil { o.Rollback() c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err"} c.ServeJSON() return } if len(body.ReimburseDetails) == 0 { o.Rollback() c.Data["json"] = lib.JSONS{Code: 202, Msg: "报销明细不能为空"} c.ServeJSON() return } reimburse.T_money = body.T_money _, err = ReimburseDao.Update_Reimburse(reimburse, "T_money") if err != nil { o.Rollback() c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err"} c.ServeJSON() return } // 删除之前的报销明细 err = ReimburseDetailsDao.Delete_ReimburseDetails(body.T_id) if err != nil { o.Rollback() c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败"} c.ServeJSON() return } for _, v := range body.ReimburseDetails { reimburseDetails := Reimburse.ReimburseDetails{ T_reimburse_id: reimburse.Id, T_trip_start_time: v.T_trip_start_time, T_trip_end_time: v.T_trip_end_time, T_trip_origin: v.T_trip_origin, T_trip_terminus: v.T_trip_terminus, T_fee_type: v.T_fee_type, T_fee_details: v.T_fee_details, T_money: v.T_money, T_reasons: v.T_reasons, T_project: v.T_project, T_img: v.T_img, T_State: 1, } _, err = ReimburseDetailsDao.Add_ReimburseDetails(reimburseDetails) if err != nil { o.Rollback() c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败"} c.ServeJSON() return } } o.Commit() NatsServer.AddUserLogs(c.User.T_uuid, "报销", "修改", reimburse) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: reimburse.Id} c.ServeJSON() return } func (c *ReimburseController) Reimburse_Del() { T_id, _ := c.GetInt("T_id") o := orm.NewOrm() o.Begin() ReimburseDao := Reimburse.NewReimburse(o) ReimburseDetailsDao := Reimburse.NewReimburseDetails(o) reimburse, err := ReimburseDao.Read_Reimburse_ById(T_id) if err != nil { o.Rollback() c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"} c.ServeJSON() return } if reimburse.T_State == Reimburse.AuditPass || reimburse.T_State == Reimburse.RemitPart || reimburse.T_State == Reimburse.RemitAll { o.Rollback() c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同已审核通过或已打款,禁止删除!"} c.ServeJSON() return } _, err = ReimburseDao.Delete_Reimburse(reimburse) if err != nil { o.Rollback() c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"} c.ServeJSON() return } //删除报销明细 err = ReimburseDetailsDao.Delete_ReimburseDetails(reimburse.Id) 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_id) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: reimburse.Id} c.ServeJSON() return } func (c *ReimburseController) Reimburse_Get() { logs.Info("header=======", c.Ctx.Request.Header.Get("User_tokey")) T_id, _ := c.GetInt("T_id") o := orm.NewOrm() ReimburseDao := Reimburse.NewReimburse(o) ReimburseDetailsDao := Reimburse.NewReimburseDetails(o) Reimburse.Read_FeeType_All_Map() reimburse, err := ReimburseDao.Read_Reimburse_ById(T_id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"} c.ServeJSON() return } //删除报销明细 reimburseDetailsList, err := ReimburseDetailsDao.Read_ReimburseDetailsList_ByT_reimburse_id(reimburse.Id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"} c.ServeJSON() return } var pList []Reimburse.ReimburseDetails_R for _, v := range reimburseDetailsList { p := Reimburse.ReimburseDetailsToReimburseDetails_R(v) pList = append(pList, p) } NatsServer.AddUserLogs(c.User.T_uuid, "报销", "删除", T_id) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Reimburse.ReimburseToReimburse_Detail(reimburse, pList)} c.ServeJSON() return } // 提交审核 func (c *ReimburseController) Reimburse_Submit_Audit() { T_id, _ := c.GetInt("T_id") o := orm.NewOrm() ReimburseDao := Reimburse.NewReimburse(o) reimburse, err := ReimburseDao.Read_Reimburse_ById(T_id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"} c.ServeJSON() return } if c.User.T_uuid != reimburse.T_uid { c.Data["json"] = lib.JSONS{Code: 202, Msg: "没有权限!"} c.ServeJSON() return } if reimburse.T_State != Reimburse.WaitSubmit && reimburse.T_State != Reimburse.AuditUnPass { c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("状态为%s,禁止提交审核!", Reimburse.Read_Audit_Get(reimburse.T_State))} c.ServeJSON() return } reimburse.T_State = Reimburse.WaitAudit _, err = ReimburseDao.Update_Reimburse(reimburse, "T_State") if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "提交审核失败!"} c.ServeJSON() return } NatsServer.AddNews(conf.FinanceUuid, fmt.Sprintf("【报销审核】您有一条%s的报销审核待处理", c.User.T_name), conf.ReimburseApprovalUrl) NatsServer.AddUserLogs(c.User.T_uuid, "报销", "提交审核", reimburse) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_id} c.ServeJSON() return } // 修改审核状态 func (c *ReimburseController) Reimburse_Edit_Audit() { T_id, _ := c.GetInt("T_id") T_State, _ := c.GetInt("T_State") o := orm.NewOrm() ReimburseDao := Reimburse.NewReimburse(o) reimburse, err := ReimburseDao.Read_Reimburse_ById(T_id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"} c.ServeJSON() return } if reimburse.T_State == Reimburse.WaitSubmit { c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("状态为%s,禁止修改!", Reimburse.Read_Audit_Get(reimburse.T_State))} c.ServeJSON() return } if T_State == Reimburse.RemitPart || T_State == Reimburse.RemitAll { if reimburse.T_State != Reimburse.AuditPass && reimburse.T_State != Reimburse.RemitPart { c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("状态为%s,禁止修改!", Reimburse.Read_Audit_Get(reimburse.T_State))} c.ServeJSON() return } } if T_State == Reimburse.AuditPass || T_State == Reimburse.AuditUnPass { if reimburse.T_State != Reimburse.WaitAudit && T_State != Reimburse.AuditUnPass { c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("状态为%s,禁止修改!", Reimburse.Read_Audit_Get(reimburse.T_State))} c.ServeJSON() return } } reimburse.T_State = T_State _, err = ReimburseDao.Update_Reimburse(reimburse, "T_State") if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改审核状态失败!"} c.ServeJSON() return } NatsServer.AddNews(reimburse.T_uid, fmt.Sprintf("【报销申请】您的报销申请%s", Reimburse.Read_Audit_Get(T_State)), conf.MyReimburseUrl) NatsServer.AddUserLogs(c.User.T_uuid, "报销", "修改审核状态", reimburse) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_id} c.ServeJSON() return } // 修改(财务) func (c *ReimburseController) Reimburse_Edit_Finance() { T_id, _ := c.GetInt("T_id") T_approval_money, _ := c.GetFloat("T_approval_money") T_approval_details := c.GetString("T_approval_details") T_remit_details := c.GetString("T_remit_detials") o := orm.NewOrm() o.Begin() ReimburseDao := Reimburse.NewReimburse(o) ReimburseDetailsDao := Reimburse.NewReimburseDetails(o) reimburse, err := ReimburseDao.Read_Reimburse_ById(T_id) if err != nil { o.Rollback() c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"} c.ServeJSON() return } // 1 未提交审核 2 待审核 3 审核通过 4 审核不通过 5 已部分打款 6 已全部打款 if reimburse.T_State == Reimburse.WaitSubmit { o.Rollback() c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("状态为%s,禁止修改!", Reimburse.Read_Audit_Get(reimburse.T_State))} c.ServeJSON() return } approval_details_list := strings.Split(strings.Trim(T_approval_details, "|"), "|") for _, detail := range approval_details_list { id, _ := strconv.Atoi(strings.Split(detail, ",")[0]) money := lib.StringToFloat64TwoDecimal(strings.Split(detail, ",")[1]) // 审批金额 opinion := strings.Split(detail, ",")[2] // 审批意见 err = ReimburseDetailsDao.Update_ReimburseDetails(Reimburse.ReimburseDetails{ Id: id, T_approval_money: float32(money), T_approval_opinion: opinion, }, "T_approval_money", "T_approval_opinion") if err != nil { o.Rollback() c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改报销详情失败!"} c.ServeJSON() return } } var T_remit float32 remitList := Reimburse.ReimburseToRemitDetail(T_remit_details) for _, r := range remitList { T_remit += r.T_money } if T_remit == reimburse.T_approval_money && T_remit > 0 { reimburse.T_State = Reimburse.RemitAll } else if T_remit < reimburse.T_approval_money && T_remit > 0 { reimburse.T_State = Reimburse.RemitPart } reimburse.T_remit = T_remit reimburse.T_approval_money = float32(T_approval_money) reimburse.T_remit_detials = T_remit_details _, err = ReimburseDao.Update_Reimburse(reimburse, "T_State", "T_remit", "T_remit_detials", "T_approval_money") if err != nil { o.Rollback() c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改审核状态失败!"} c.ServeJSON() return } o.Commit() if T_remit > 0 { NatsServer.AddNews(reimburse.T_uid, fmt.Sprintf("【报销打款】您的报销申请已%s", Reimburse.Read_Audit_Get(reimburse.T_State)), conf.MyReimburseUrl) } NatsServer.AddUserLogs(c.User.T_uuid, "报销", "修改(财务)", reimburse) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_id} c.ServeJSON() return }