package erp

import (
	"context"
	"git.kumo.work/shama/service/client"
	accounting2 "git.kumo.work/shama/service/erp/accounting"
	"git.kumo.work/shama/service/lib/bean"
	"github.com/shopspring/decimal"
	"time"
)

type accounting struct {
	accounting2.Accounting
}
type ArgsAccountingList struct {
	Page   bean.Page
	Search AccountingSearch
}
type AccountingSearch struct {
	AccountingSerial string     // 做账单号
	CustomId         int64      // 客户id
	CustomName       string     // 客户名称
	CustomShortName  string     // 客户简称
	WorkflowStatus   []int64    // 审批状态
	CreatedAtStart   *time.Time // 创建开始时间
	CreatedAtEnd     *time.Time // 创建结束时间
	CreatedStaffIds  []int64    // 创建人
	StaffIds         []int64    // 业务员
	AccountingIds    []int64    // 做账id
	BanFlag          int64      // 是否有效
}

type ReplyAccountingList struct {
	List  []AccountingItem `json:"list"`
	Total int64            `json:"total"`
}
type AccountingItem struct {
	Id               int64           `json:"id"`
	AccountingSerial string          `json:"accountingSerial"`
	InvoiceDate      *time.Time      `json:"invoiceDate"`
	CreatedAt        *time.Time      `json:"createdAt"`
	CustomShortName  string          `json:"customShortName"`
	CustomName       string          `json:"customName"`
	CreatedStaffId   int64           `json:"createdStaffId"`
	WorkflowId       int64           `json:"workflowId"`
	WorkflowStatus   int64           `json:"workflowStatus"`
	WorkflowReason   string          `json:"workflowReason"`
	TotalBoxCount    int64           `json:"totalBoxCount"`
	TotalAmount      decimal.Decimal `json:"totalAmount"`
	TotalVolume      decimal.Decimal `json:"totalVolume"`
	TotalGrossWeight decimal.Decimal `json:"totalGrossWeight"`
	TotalNetWeight   decimal.Decimal `json:"totalNetWeight"`
}

// List @TITLE 列表
func (a *accounting) List(ctx context.Context, args ArgsAccountingList) (reply ReplyAccountingList, err error) {
	xClient, err := client.GetClient(a)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "List", args, &reply)
	return
}

type ArgsAccountingAdd struct {
	StaffId       int64
	AccountingAdd AccountingAdd
}

type AccountingAdd struct {
	CustomId         int64                   // 客户id
	CustomShortName  string                  // 客户简称
	CustomName       string                  // 客户名称
	InvoiceDate      *time.Time              // 发票日期
	InvoiceDateEnd   *time.Time              // 开票期限
	AccountingSerial string                  // 做账合同号
	Remark           string                  // 备注
	Products         []AccountingProductItem // 销售产品
}

type AccountingProductItem struct {
	PurchaseProductId  int64           // 采购产品id
	AccountingCount    int64           // 做账数量
	CustomsSerial      string          // hs编码
	CustomsName        string          // 报关名称
	CustomsInvoiceUnit string          // 开票单位
	InvoiceType        int64           // 开票方式 1=按净重 2=按数量
	InvoiceCount       decimal.Decimal // 开票数量
}

// Add @TITLE 添加
func (a *accounting) Add(ctx context.Context, args ArgsAccountingAdd) (accountId int64, err error) {
	xClient, err := client.GetClient(a)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "Add", args, &accountId)
	return
}

type ReplyAccountingInfo struct {
	Id               int64           `json:"id"`
	CustomId         int64           `json:"customId"`
	CustomShortName  string          `json:"customShortName"`
	CustomName       string          `json:"customName"`
	InvoiceDate      *time.Time      `json:"invoiceDate"`
	InvoiceDateEnd   *time.Time      `json:"invoiceDateEnd"`
	AccountingSerial string          `json:"accountingSerial"`
	Remark           string          `json:"remark"`
	WorkflowId       int64           `json:"workflowId"`
	WorkflowStatus   int64           `json:"workflowStatus"`
	WorkflowReason   string          `json:"workflowReason"`
	BanFlag          int64           `json:"banFlag"`
	CreatedStaffId   int64           `json:"createdStaffId"`
	CreatedAt        *time.Time      `json:"createdAt"`
	TotalBoxCount    int64           `json:"totalBoxCount"`
	TotalAmount      decimal.Decimal `json:"totalAmount"`
	TotalVolume      decimal.Decimal `json:"totalVolume"`
	TotalGrossWeight decimal.Decimal `json:"totalGrossWeight"`
	TotalNetWeight   decimal.Decimal `json:"totalNetWeight"`
}

// Info @TITLE 详情
func (a *accounting) Info(ctx context.Context, accountingId int64) (reply ReplyAccountingInfo, err error) {
	xClient, err := client.GetClient(a)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "Info", accountingId, &reply)
	return
}

type ArgsAccountingEdit struct {
	AccountingId   int64 // 做账合同id
	AccountingEdit AccountingEdit
}

type AccountingEdit struct {
	InvoiceDate      *time.Time // 发票日期
	InvoiceDateEnd   *time.Time // 开票期限
	AccountingSerial string     // 做账合同号
	Remark           string     // 备注
}

// Edit @TITLE 编辑
func (a *accounting) Edit(ctx context.Context, args ArgsAccountingEdit) (err error) {
	xClient, err := client.GetClient(a)
	if err != nil {
		return
	}
	reply := 0
	err = xClient.Call(ctx, "Edit", args, &reply)
	return
}

type InvoiceProductItem struct {
	FactoryId          int64           `json:"factoryId" label:"工厂id"`          // 工厂id
	FactoryName        string          `json:"factoryName" label:"工厂名称"`        // 工厂名称
	CustomsSerial      string          `json:"customsSerial" label:"hs编码"`      // hs编码
	CustomsName        string          `json:"customsName" label:"报关名称"`        // 报关名称
	CustomsInvoiceUnit string          `json:"customsInvoiceUnit" label:"开票单位"` // 开票单位
	InvoiceCount       decimal.Decimal `json:"invoiceCount" label:"开票数量"`       // 开票数量
	Amount             decimal.Decimal `json:"amount" label:"金额"`               // 金额
}

// InvoiceProducts @TITLE 开票产品资料
func (a *accounting) InvoiceProducts(ctx context.Context, accountingId int64) (reply []InvoiceProductItem, err error) {
	xClient, err := client.GetClient(a)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "InvoiceProducts", accountingId, &reply)
	return

}

type ReplyAccountFactoryItem struct {
	FactoryId   int64  // 工厂id
	FactoryName string // 工厂名称
}

// Factories @TITLE 做账工厂
func (a *accounting) Factories(ctx context.Context, accountingId int64) (reply []ReplyAccountFactoryItem, err error) {
	xClient, err := client.GetClient(a)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "Factories", accountingId, &reply)
	return

}

// Cancel @TITLE 作废
func (a *accounting) Cancel(ctx context.Context, accountingId int64) (err error) {
	xClient, err := client.GetClient(a)
	if err != nil {
		return
	}
	reply := 0
	err = xClient.Call(ctx, "Cancel", accountingId, &reply)
	return
}