From ef06ecd77497e5e0acfe2b17d186221cbee57716 Mon Sep 17 00:00:00 2001 From: momo <1012651275@qq.com> Date: Tue, 24 Oct 2023 15:22:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E8=AE=A2=E5=8D=95=E5=8F=91=E7=A5=A8?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/repositories/order.go | 54 ++++++++++++++++++++++++++++------- utils/pdf/html_to_pdf.go | 7 +++-- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/service/repositories/order.go b/service/repositories/order.go index bd0ca6c7..799a266b 100644 --- a/service/repositories/order.go +++ b/service/repositories/order.go @@ -9,6 +9,7 @@ import ( "fusenapi/model/gmodel" "fusenapi/utils/basic" "fusenapi/utils/file" + "fusenapi/utils/format" "fusenapi/utils/handlers" "fusenapi/utils/hash" "fusenapi/utils/order" @@ -16,6 +17,7 @@ import ( "fusenapi/utils/pdf" "fusenapi/utils/queue" "math" + "strconv" "time" "github.com/aws/aws-sdk-go/aws/session" @@ -374,19 +376,47 @@ func (d *defaultOrder) Invoice(ctx context.Context, in *InvoiceReq) (res *Invoic state = ress.OrderDetail.DeliveryAddress.State suite = ress.OrderDetail.DeliveryAddress.Suite zipCode = ress.OrderDetail.DeliveryAddress.ZipCode + } else { + // 邮箱 + model1 := d.MysqlConn.Where("is_del = ?", 0).Model(&gmodel.FsUser{}) + if in.UserId != 0 { + model1 = model.Where("user_id = ?", in.UserId) + } + var userInfo = gmodel.FsUser{} + result1 := model1.Take(&userInfo) + if result1.Error != nil { + if errors.Is(result1.Error, gorm.ErrRecordNotFound) { + errorCode = *basic.CodeErrOrderCreatePrePaymentInfoNoFound + } else { + errorCode = *basic.CodeServiceErr + } + logc.Errorf(ctx, "order invoice trade failed, err: %v", err) + return &InvoiceRes{ + ErrorCode: errorCode, + }, result1.Error + } + name = *userInfo.Email } var products string for _, orderProduct := range ress.OrderDetail.OrderProduct { var model00301 = constants.INVOICE_TEMPLATE_0301 - var price = fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(orderProduct.ItemPrice.Current.CurrentCurrency)], orderProduct.ItemPrice.Current.CurrentAmount.(string)) - var priceTotal = fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(orderProduct.TotalPrice.Current.CurrentCurrency)], orderProduct.TotalPrice.Current.CurrentAmount.(string)) - var productsInfo = fmt.Sprintf(model00301, orderProduct.ProductName, price, orderProduct.PurchaseQuantity.Current, priceTotal) + + var priceStr = format.NumToStringWithThousandthPercentile(orderProduct.ItemPrice.Current.CurrentAmount.(string)) + var price = fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(orderProduct.ItemPrice.Current.CurrentCurrency)], priceStr) + + var priceTotalStr = format.NumToStringWithThousandthPercentile(orderProduct.TotalPrice.Current.CurrentAmount.(string)) + var priceTotal = fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(orderProduct.TotalPrice.Current.CurrentCurrency)], priceTotalStr) + + productNum := strconv.FormatFloat(orderProduct.PurchaseQuantity.Current.(float64), 'f', -1, 64) + var productNumStr = format.NumToStringWithThousandthPercentile(productNum) + + var productsInfo = fmt.Sprintf(model00301, orderProduct.ProductName, price, productNumStr, priceTotal) products = products + productsInfo } model003 = fmt.Sprintf(constants.INVOICE_TEMPLATE_03, products) - - var subtotal = fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(ress.OrderDetail.OrderAmount.Subtotal.Current.CurrentCurrency)], ress.OrderDetail.OrderAmount.Subtotal.Current.CurrentAmount.(string)) + var subtotalStr = format.NumToStringWithThousandthPercentile(ress.OrderDetail.OrderAmount.Subtotal.Current.CurrentAmount.(string)) + var subtotal = fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(ress.OrderDetail.OrderAmount.Subtotal.Current.CurrentCurrency)], subtotalStr) var taxStr = "0.00" if ress.OrderDetail.OrderAmount.Tax.Current.CurrentAmount != nil { taxStr = ress.OrderDetail.OrderAmount.Tax.Current.CurrentAmount.(string) @@ -396,14 +426,16 @@ func (d *defaultOrder) Invoice(ctx context.Context, in *InvoiceReq) (res *Invoic taxCurrency = constants.OrderCurrencyMessage[constants.Currency(ress.OrderDetail.OrderAmount.Total.Current.CurrentCurrency)] } var tax = fmt.Sprintf("%s%s", taxCurrency, taxStr) - var total = fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(ress.OrderDetail.OrderAmount.Total.Current.CurrentCurrency)], ress.OrderDetail.OrderAmount.Total.Current.CurrentAmount.(string)) + var totalStr = format.NumToStringWithThousandthPercentile(ress.OrderDetail.OrderAmount.Total.Current.CurrentAmount.(string)) + var total = fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(ress.OrderDetail.OrderAmount.Total.Current.CurrentCurrency)], totalStr) // 生成收据发票--首款 if receiptSnsDeposit != "" { model002 = fmt.Sprintf(constants.INVOICE_TEMPLATE_02, receiptSnsDeposit, name, ctimeDate, street+" "+suite, city, state+zipCode) + var payAmountStr01 = format.NumToStringWithThousandthPercentile(ress.OrderDetail.OrderAmount.Deposit.PayAmount.Current.CurrentAmount.(string)) v7 := "Deposit Requested" - v8 := fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(ress.OrderDetail.OrderAmount.Deposit.PayAmount.Current.CurrentCurrency)], ress.OrderDetail.OrderAmount.Deposit.PayAmount.Current.CurrentAmount.(string)) + v8 := fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(ress.OrderDetail.OrderAmount.Deposit.PayAmount.Current.CurrentCurrency)], payAmountStr01) v9 := "Deposit Due" v10 := v8 model004 = fmt.Sprintf(constants.INVOICE_TEMPLATE_04, subtotal, tax, total, v7, v8, v9, v10) @@ -413,7 +445,7 @@ func (d *defaultOrder) Invoice(ctx context.Context, in *InvoiceReq) (res *Invoic var content = model001 + model002 + model003 + model004 + model005 + model006 - base64, err := pdf.HtmlToPdfBase64(content, "html") + base64, err := pdf.HtmlToPdfBase64(content, "html", "Letter") if err != nil { logc.Errorf(ctx, "order invoice HtmlToPdfBase64 failed, err: %v", err) errorCode = *basic.CodeServiceErr @@ -455,9 +487,9 @@ func (d *defaultOrder) Invoice(ctx context.Context, in *InvoiceReq) (res *Invoic // 生成收据发票--尾款 if receiptSnsFinal != "" { model002 = fmt.Sprintf(constants.INVOICE_TEMPLATE_02, receiptSnsDeposit, name, ctimeDate, street+" "+suite, city, state+zipCode) - + var payAmountStr02 = format.NumToStringWithThousandthPercentile(ress.OrderDetail.OrderAmount.RemainingBalance.PayAmount.Current.CurrentAmount.(string)) v7 := "Balance Requested" - v8 := fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(ress.OrderDetail.OrderAmount.RemainingBalance.PayAmount.Current.CurrentCurrency)], ress.OrderDetail.OrderAmount.RemainingBalance.PayAmount.Current.CurrentAmount.(string)) + v8 := fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(ress.OrderDetail.OrderAmount.RemainingBalance.PayAmount.Current.CurrentCurrency)], payAmountStr02) v9 := "Balance Due" v10 := v8 model004 = fmt.Sprintf(constants.INVOICE_TEMPLATE_04, subtotal, tax, total, v7, v8, v9, v10) @@ -465,7 +497,7 @@ func (d *defaultOrder) Invoice(ctx context.Context, in *InvoiceReq) (res *Invoic cardSn := "****" + *orderTradeFinal.CardSn model005 = fmt.Sprintf(constants.INVOICE_TEMPLATE_05, *orderTradeDeposit.CardBrand, cardSn) var content = model001 + model002 + model003 + model004 + model005 + model006 - base64, err := pdf.HtmlToPdfBase64(content, "html") + base64, err := pdf.HtmlToPdfBase64(content, "html", "Letter") if err != nil { logc.Errorf(ctx, "order invoice HtmlToPdfBase64 failed, err: %v", err) errorCode = *basic.CodeServiceErr diff --git a/utils/pdf/html_to_pdf.go b/utils/pdf/html_to_pdf.go index 2af6de15..ad983dfd 100644 --- a/utils/pdf/html_to_pdf.go +++ b/utils/pdf/html_to_pdf.go @@ -3,15 +3,16 @@ package pdf import ( "encoding/base64" "errors" - "github.com/SebastiaanKlippert/go-wkhtmltopdf" "strings" + + "github.com/SebastiaanKlippert/go-wkhtmltopdf" ) /* html转 Pdf outFile为空则不保存(使用该方法需要安装工具 sudo apt-get install wkhtmltopdf) */ -func HtmlToPdfBase64(content string, dataType string, outFile ...string) (string, error) { +func HtmlToPdfBase64(content string, dataType string, pageSize string, outFile ...string) (string, error) { pdfg, err := wkhtmltopdf.NewPDFGenerator() if err != nil { return "", err @@ -27,7 +28,7 @@ func HtmlToPdfBase64(content string, dataType string, outFile ...string) (string //模式 pdfg.Orientation.Set(wkhtmltopdf.OrientationPortrait) //pdf尺寸默认A4纸 - pdfg.PageSize.Set(wkhtmltopdf.PageSizeA4) + pdfg.PageSize.Set(pageSize) // Create PDF document in memory if err = pdfg.Create(); err != nil { return "", err