@@ -0,0 +1,46 @@
|
||||
package order
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"cake_crm/proto"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Service struct{}
|
||||
|
||||
func NewService() *Service {
|
||||
return &Service{}
|
||||
}
|
||||
|
||||
func (s *Service) CreateOrderText(req *proto.OrderReq, items []*proto.CardItem) (string, error) {
|
||||
buffer := bytes.Buffer{}
|
||||
var orderAmount int64
|
||||
buffer.WriteString(fmt.Sprintf("Заказ от:\n%s\n%s\n", req.Order.Name, req.Order.Phone))
|
||||
buffer.WriteString("\n")
|
||||
for _, item := range items {
|
||||
buffer.WriteString(item.Name)
|
||||
buffer.WriteString("\n")
|
||||
unit, err := unitToText(item.Unit)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
buffer.WriteString(fmt.Sprintf("Количество: %d%s\n", item.Count, unit))
|
||||
orderAmount += item.Amount
|
||||
buffer.WriteString(fmt.Sprintf("Сумма: %.00fр\n", float64(item.Amount)/100))
|
||||
buffer.WriteString("\n")
|
||||
}
|
||||
buffer.WriteString("\n")
|
||||
buffer.WriteString(fmt.Sprintf("ИТОГО: %.00fр\n", float64(orderAmount)/100))
|
||||
return buffer.String(), nil
|
||||
}
|
||||
|
||||
func unitToText(unit string) (string, error) {
|
||||
switch unit {
|
||||
case "kg":
|
||||
return "кг", nil
|
||||
case "piece":
|
||||
return "шт", nil
|
||||
}
|
||||
return "", errors.New("unit not found")
|
||||
}
|
||||
Reference in New Issue
Block a user