You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
7.5 KiB

package source
import (
"errors"
"fmt"
"git.oa00.com/go/mysql"
"github.com/shopspring/decimal"
"gorm.io/gorm"
"recook/internal/libs/bean"
finance2 "recook/internal/v2/logic/finance"
"recook/internal/v2/model/finance"
"recook/internal/v2/model/recook/goods"
manage "recook/internal/v2/model/recook/order"
"time"
)
var BillLogic = &billLogic{}
type billLogic struct {
}
// Lists @Title 订单列表
func (b *billLogic) Lists(sourceId uint, filter finance2.BillFilter, page bean.Page) ([]finance2.BillItem, int64) {
return finance2.BillLogic.Lists(sourceId, filter, page, nil)
}
// Confirm @Title 确认完结
func (b *billLogic) Confirm(sourceId uint, ids []uint) error {
billOrders := []finance.RecookFinanceBillOrder{}
if mysql.Db.Preload("OrderGoods").Preload("After").Preload("Source").Where("id in ? and status = ? and source_id = ?", ids, finance.RecookFinanceBillOrderStatusFinish, sourceId).Find(&billOrders).Error != nil {
return errors.New("选择数据错误")
}
totalAmount := decimal.Zero
orderSources := []finance.RecookFinanceBillOrderSource{}
for _, order := range billOrders {
amount := order.PurchasePrice.Mul(decimal.NewFromInt(int64(order.OrderGoods.Quantity))).Add(order.OrderGoods.ExpressFee)
refundAmount := decimal.Zero
compensateAmount := order.After.SupplierPrice
if order.OrderGoods.AssType == manage.RecookOrderGoodsDetailAssTypeRejected {
refundAmount = amount
compensateAmount = order.After.ExpressFree
}
billAmount := amount.Sub(refundAmount).Sub(compensateAmount)
totalAmount = totalAmount.Add(billAmount)
orderSources = append(orderSources, finance.RecookFinanceBillOrderSource{
SourceId: sourceId,
Receipt: finance.RecookFinanceBillOrderSourceReceiptFalse,
BillOrderId: order.Id,
BillQuantity: order.OrderGoods.Quantity - order.After.Quantity,
BillAmount: billAmount,
})
}
if len(orderSources) == 0 {
return errors.New("选择错误")
}
return mysql.Db.Transaction(func(tx *gorm.DB) error {
if tx.Create(&orderSources).Error != nil {
return errors.New("确认失败")
}
if err := finance2.SourceLogic.BillAdd(tx, sourceId, totalAmount); err != nil {
return err
}
return nil
})
}
type billApplyDataItem struct {
SkuId uint `json:"sku_id"`
SkuCode string `json:"sku_code"`
SkuGoodsNum string `json:"sku_goods_num"`
InvoiceTaxSn string `json:"invoice_tax_sn"`
InvoiceTaxName string `json:"invoice_tax_name"`
InvoiceGoodsName string `json:"invoice_goods_name"`
SkuName string `json:"sku_name"`
InvoiceUnit string `json:"invoice_unit"`
InvoiceTaxRate decimal.Decimal `json:"invoice_tax_rate"`
BillQuantity uint `json:"bill_quantity"`
BillAmount decimal.Decimal `json:"bill_amount"`
UnTaxAmount decimal.Decimal `json:"un_tax_amount"`
TaxAmount decimal.Decimal `json:"tax_amount"`
}
func init() {
//dbc.SetupMysql()
}
// ApplyData @Title 申请结算数据
func (b *billLogic) ApplyData(sourceId uint) (lists []billApplyDataItem) {
lists = []billApplyDataItem{}
orderSources := []finance.RecookFinanceBillOrderSource{}
mysql.Db.Joins("BillOrder").Where(fmt.Sprintf("%s.source_id = ? and tax_id = 0", mysql.Db.NamingStrategy.TableName("RecookFinanceBillOrderSource")), sourceId).
Group("BillOrder.sku_id").
Select("BillOrder.sku_id,sum(bill_quantity) bill_quantity,sum(bill_amount) bill_amount").
Find(&orderSources)
if len(orderSources) > 0 {
skuIds := []uint{}
for _, source := range orderSources {
skuIds = append(skuIds, source.BillOrder.SkuId)
}
skuModels := []goods.RecookGoodsSkuModel{}
mysql.Db.Preload("Invoice").Where("id in ?", skuIds).Find(&skuModels)
skuMap := map[uint]goods.RecookGoodsSkuModel{}
for _, skuModel := range skuModels {
skuMap[skuModel.Id] = skuModel
}
for _, orderSource := range orderSources {
unTaxAmount := orderSource.BillAmount.Div(skuMap[orderSource.BillOrder.SkuId].Invoice.TaxRate.Add(decimal.NewFromInt(1))).Round(2)
taxAmount := orderSource.BillAmount.Sub(unTaxAmount)
lists = append(lists, billApplyDataItem{
SkuId: orderSource.BillOrder.SkuId,
SkuCode: skuMap[orderSource.BillOrder.SkuId].Code,
SkuGoodsNum: skuMap[orderSource.BillOrder.SkuId].GoodsNum,
InvoiceTaxName: skuMap[orderSource.BillOrder.SkuId].Invoice.TaxName,
InvoiceTaxSn: skuMap[orderSource.BillOrder.SkuId].Invoice.TaxSn,
InvoiceGoodsName: skuMap[orderSource.BillOrder.SkuId].Invoice.GoodsName,
SkuName: skuMap[orderSource.BillOrder.SkuId].Name,
InvoiceUnit: skuMap[orderSource.BillOrder.SkuId].Invoice.Unit,
InvoiceTaxRate: skuMap[orderSource.BillOrder.SkuId].Invoice.TaxRate,
BillQuantity: orderSource.BillQuantity,
BillAmount: orderSource.BillAmount,
UnTaxAmount: unTaxAmount,
TaxAmount: taxAmount,
})
}
}
return
}
// SubmitApply @Title 提交申请
func (b *billLogic) SubmitApply(sourceId uint, skuIds []uint) error {
var billOrderSources []finance.RecookFinanceBillOrderSource
if sourceId > 0 {
mysql.Db.Joins("BillOrder").
Where(fmt.Sprintf("%s.source_id = ? and BillOrder.sku_id in ? and tax_id = 0", mysql.Db.NamingStrategy.TableName("RecookFinanceBillOrderSource")), sourceId, skuIds).
Find(&billOrderSources)
} else {
mysql.Db.Joins("BillOrder").Where("BillOrder.sku_id in (?) and tax_id = 0", skuIds).Find(&billOrderSources)
}
if len(billOrderSources) == 0 {
return errors.New("商品错误")
}
var billOrderSourceIds []uint
amount := decimal.Zero
for _, billOrderSource := range billOrderSources {
billOrderSourceIds = append(billOrderSourceIds, billOrderSource.Id)
amount = amount.Add(billOrderSource.BillAmount)
}
return mysql.Db.Transaction(func(tx *gorm.DB) error {
sourceTax := finance.RecookFinanceBillOrderSourceTax{
SourceId: billOrderSources[0].SourceId,
Status: finance.RecookFinanceBillOrderSourceTaxStatusUnUpload,
Amount: amount,
SubmitTime: time.Now(),
}
//tax表创建开票tax
if err := tx.Create(&sourceTax).Error; err != nil {
fmt.Println(err.Error())
return errors.New("提交失败1")
}
//bill_order变成已开票
if tx.Model(finance.RecookFinanceBillOrderSource{}).Where("id in ?", billOrderSourceIds).UpdateColumns(map[string]interface{}{
"tax_id": sourceTax.Id,
"receipt": finance.RecookFinanceBillOrderSourceReceiptTrue,
}).RowsAffected != int64(len(billOrderSourceIds)) {
return errors.New("提交失败2")
}
if err := AddFinanceSource(tx, amount, billOrderSources[0].SourceId, 0); err != nil {
return errors.New("提交失败3")
}
return nil
})
}
//AddFinanceSource 更新运营方累计可开票金额数据
func AddFinanceSource(tx *gorm.DB, amount decimal.Decimal, sourceId uint, status uint) error {
var source finance.RecookFinanceSource
err := tx.Model(&source).First(&source, "source_id=?", sourceId).Error
if err != nil {
return err
}
if status > 0 {
//删除发票,未开票金额加上开票的数字,已开票金额减去该数字
err = tx.Model(&source).Updates(&finance.RecookFinanceSource{
UnReceipt: source.UnReceipt.Add(amount),
Receipt: source.Receipt.Sub(amount),
}).Error
} else {
//确认打款后,未开票金额减去开票的数字,已开票金额加上该数字
err = tx.Model(&source).Updates(&finance.RecookFinanceSource{
UnReceipt: source.UnReceipt.Sub(amount),
Receipt: source.Receipt.Add(amount),
}).Error
}
if err != nil {
return err
}
return nil
}