153 lines
6.8 KiB
Go
153 lines
6.8 KiB
Go
package model
|
|
|
|
import (
|
|
"errors"
|
|
m2 "github.com/fonchain_enterprise/fonchain-approval-fork/pkg/m"
|
|
"github.com/jinzhu/copier"
|
|
"gorm.io/plugin/soft_delete"
|
|
"time"
|
|
)
|
|
|
|
// financial_form
|
|
type FinancialForm struct {
|
|
ID uint64 `gorm:"primaryKey;column:id" json:"id"` // ID
|
|
DeletedAt soft_delete.DeletedAt `gorm:"column:deleted_at" json:"deletedAt"` // 删除时间
|
|
CreatedAt time.Time `gorm:"column:created_at" json:"createdAt"` // 创建时间
|
|
UpdatedAt time.Time `gorm:"column:updated_at" json:"updatedAt"`
|
|
ApprovalID uint64 `gorm:"primaryKey;column:approval_id" json:"approvalId"` // 关联申请的ID
|
|
PaymentCompany string `json:"paymentCompany" gorm:"column:payment_company;type:varchar(2048);comment:付款公司别"`
|
|
TransactionDepartment string `json:"transactionDepartment" gorm:"column:transaction_department;type:varchar(255);comment:经办部门"`
|
|
Payee string `json:"payee" gorm:"column:payee;type:varchar(2048);comment:收款人全称"`
|
|
BeneficiaryBank string `json:"beneficiaryBank" gorm:"column:beneficiary_bank;type:varchar(1024);comment:收款人开户行"`
|
|
BankNo string `json:"bankNo" gorm:"column:bank_no;type:varchar(1024);comment:收款人账号"`
|
|
CostInfo []*CostInfo `json:"costInfo" gorm:"column:cost_info;type:json;serializer:json;comment:费用说明"`
|
|
PaymentMethod string `json:"paymentMethod" gorm:"column:payment_method;type:varchar(255);comment:付款方式"`
|
|
InvoiceInfo []*InvoiceInfo `json:"invoiceInfo" gorm:"column:invoice_info;type:json;serializer:json;comment:发票信息"`
|
|
}
|
|
|
|
type CostInfo struct {
|
|
Description string `json:"description" gorm:"column:description;type:text;comment:说明"`
|
|
UnitPrice string `json:"unitPrice" gorm:"column:unit_price;type:varchar(255);comment:费用单价"`
|
|
CostUnit string `json:"costUnit" gorm:"column:cost_unit;type:varchar(255);comment:费用单位"`
|
|
CostNum int64 `json:"costNum" gorm:"column:cost_num;type:int;comment:费用数量"`
|
|
TotalPrice string `json:"totalPrice" gorm:"column:total_price;type:varchar(255);comment:费用总额"`
|
|
UsedDepartment string `json:"usedDepartment" gorm:"column:used_department;type:varchar(2048);comment:使用部门"`
|
|
}
|
|
|
|
type InvoiceInfo struct {
|
|
InvoiceDate string `json:"invoiceDate" gorm:"column:invoice_date;type:varchar(255);comment:发票日期"`
|
|
InvoiceNo string `json:"invoiceNo" gorm:"column:invoice_no;type:varchar(255);comment:发票号码"`
|
|
InvoiceProvider string `json:"invoiceProvider" gorm:"column:invoice_provider;type:varchar(1024);comment:发票提供者"`
|
|
Amount string `json:"amount" gorm:"column:amount;type:varchar(255);comment:发票或付款金额"`
|
|
UseTo string `json:"useTo" gorm:"column:use_to;type:varchar(1024);comment:用途"`
|
|
Applicant string `json:"applicant" gorm:"column:applicant;varchar(255);comment:申请人"`
|
|
PaymentMethod string `json:"paymentMethod" gorm:"column:payment_method;type:varchar(255);comment:付款方式"`
|
|
PaymentObj string `json:"paymentObj" gorm:"column:payment_obj;type:varchar(255);comment:付款或报销对象"`
|
|
InvoiceType string `json:"invoiceType" gorm:"column:invoice_type;type:varchar(255);comment:发票类型"`
|
|
Notes string `json:"notes" gorm:"column:notes;type:text;comment:备注"`
|
|
}
|
|
|
|
// TableName get sql table name.获取数据库表名
|
|
func (m *FinancialForm) TableName() string {
|
|
return "financial_form"
|
|
}
|
|
|
|
func (m *FinancialForm) GetApproval(id uint64) (*Approval, error) {
|
|
|
|
var entity *Approval
|
|
if err := DB.
|
|
Preload("ApprovalWorkFlows").
|
|
Preload("ApprovalType").
|
|
Preload("FinancialForm").
|
|
First(&entity, id).Error; err != nil {
|
|
return entity, err
|
|
}
|
|
|
|
return entity, nil
|
|
}
|
|
|
|
func (m *FinancialForm) SaveApprovalContent(in *approval.CreateRequest, a *Approval) error {
|
|
|
|
copier.CopyWithOption(&m, in.FinancialForm, copier.Option{DeepCopy: true})
|
|
m.ApprovalID = a.ID
|
|
costInfos := make([]*CostInfo, 0)
|
|
for i := 0; i < len(in.FinancialForm.CostInfo); i++ {
|
|
costInfo := new(CostInfo)
|
|
copier.CopyWithOption(&costInfo, in.FinancialForm.CostInfo[i], copier.Option{DeepCopy: true})
|
|
costInfos = append(costInfos, costInfo)
|
|
}
|
|
m.CostInfo = costInfos
|
|
|
|
invoiceInfos := make([]*InvoiceInfo, 0)
|
|
for i := 0; i < len(in.FinancialForm.InvoiceInfo); i++ {
|
|
invoiceInfo := new(InvoiceInfo)
|
|
copier.CopyWithOption(&invoiceInfo, in.FinancialForm.InvoiceInfo[i], copier.Option{DeepCopy: true})
|
|
invoiceInfos = append(invoiceInfos, invoiceInfo)
|
|
}
|
|
m.InvoiceInfo = invoiceInfos
|
|
|
|
return DB.Create(&m).Error
|
|
}
|
|
|
|
func (m *FinancialForm) UpdateApprovalContent(in *approval.CreateRequest, a *Approval) error {
|
|
|
|
var entity *FinancialForm
|
|
|
|
if err := DB.Where(&FinancialForm{ApprovalID: a.ID}).First(&entity).Error; err != nil {
|
|
return errors.New(m2.ErrorNotFound)
|
|
}
|
|
|
|
copier.CopyWithOption(&m, in.FinancialForm, copier.Option{DeepCopy: true})
|
|
m.ID = entity.ID
|
|
|
|
costInfos := make([]*CostInfo, 0)
|
|
for i := 0; i < len(in.FinancialForm.CostInfo); i++ {
|
|
costInfo := new(CostInfo)
|
|
copier.CopyWithOption(&costInfo, in.FinancialForm.CostInfo[i], copier.Option{DeepCopy: true})
|
|
costInfos = append(costInfos, costInfo)
|
|
}
|
|
m.CostInfo = costInfos
|
|
|
|
invoiceInfos := make([]*InvoiceInfo, 0)
|
|
for i := 0; i < len(in.FinancialForm.InvoiceInfo); i++ {
|
|
invoiceInfo := new(InvoiceInfo)
|
|
copier.CopyWithOption(&invoiceInfo, in.FinancialForm.InvoiceInfo[i], copier.Option{DeepCopy: true})
|
|
invoiceInfos = append(invoiceInfos, invoiceInfo)
|
|
}
|
|
m.InvoiceInfo = invoiceInfos
|
|
|
|
return DB.Model(&m).Updates(m).Error
|
|
}
|
|
|
|
func (m *FinancialForm) BuildResContent(a *Approval, request *approval.CreateRequest) {
|
|
|
|
if a.FinancialForm != nil {
|
|
financialForm := new(approval.FinancialForm)
|
|
copier.CopyWithOption(&financialForm, a.FinancialForm, copier.Option{DeepCopy: true})
|
|
|
|
costInfos := make([]*approval.CostInfo, 0)
|
|
for i := 0; i < len(a.FinancialForm.CostInfo); i++ {
|
|
costInfo := new(approval.CostInfo)
|
|
copier.CopyWithOption(&costInfo, a.FinancialForm.CostInfo[i], copier.Option{DeepCopy: true})
|
|
costInfos = append(costInfos, costInfo)
|
|
}
|
|
|
|
financialForm.CostInfo = costInfos
|
|
|
|
invoiceInfos := make([]*approval.InvoiceInfo, 0)
|
|
for i := 0; i < len(a.FinancialForm.InvoiceInfo); i++ {
|
|
invoiceInfo := new(approval.InvoiceInfo)
|
|
copier.CopyWithOption(&invoiceInfo, a.FinancialForm.InvoiceInfo[i], copier.Option{DeepCopy: true})
|
|
invoiceInfos = append(invoiceInfos, invoiceInfo)
|
|
}
|
|
|
|
financialForm.InvoiceInfo = invoiceInfos
|
|
|
|
request.FinancialForm = financialForm
|
|
}
|
|
}
|
|
|
|
func (m *FinancialForm) DeleteApproval(p *Approval) error {
|
|
return DB.Where(&FinancialForm{ApprovalID: p.ID}).Delete(&FinancialForm{}).Error
|
|
}
|