diff --git a/internal/booking/service.go b/internal/booking/service.go index 6cc6d3c..95ba3c6 100644 --- a/internal/booking/service.go +++ b/internal/booking/service.go @@ -97,15 +97,27 @@ func (bs Service) Update(id int, From time.Time, To time.Time, Name string, Phon return b } -func (bs Service) CreateItem(bid int, item string, qty int, price float64, method string) *Item { +func (bs Service) CreateItem(bookingId int, item config.HostItem, quantity int, paymentMethod string, customerNumber int) *Item { i := &Item{ - BookingId: bid, - Item: item, - Quantity: qty, - Price: price, - PaymentMethod: method, + BookingId: bookingId, + Item: item.Name, + Quantity: quantity, + Price: item.Price, + PaymentMethod: paymentMethod, } _ = bs.db.Create(i) + + if item.Taxes != 0.0 { + ti := &Item{ + BookingId: bookingId, + Item: "Taxes", + Quantity: quantity * customerNumber, + Price: item.Taxes, + PaymentMethod: "Cash", + } + _ = bs.db.Create(ti) + } + return i } @@ -257,20 +269,18 @@ func (bs Service) ParseFromApi(rawContent string) (*Booking, error) { arrivalDate := extractDate(`Date d'arrivée `, content) departureDate := extractDate(`Date de départ `, content) stayLength := extractInt(`Durée de séjour : (\d+) nuits`, content) - totalAmount := extractFloat(`Montant total € (\d+)`, content) customerName := extractString(`Nom du client : \n\s+([\w\s]+)`, content) customerName = strings.SplitN(customerName, "\n", 2)[0] customerEmail := extractString(`[\w\.\-]+@[\w\.\-]+\.\w+`, content) customerNumber := extractInt(`Nombre de personnes : \s*\n\s*(\d+)`, content) commissionAmount := extractFloat(`Commission : € (\d+,\d+)`, content) - item := extractString(`Maison 1 Chambre \((T2|T3) -`, content) + itemName := extractString(`Maison 1 Chambre \((T2|T3) -`, content) externalId := extractString(`Numéro de réservation : \n\s+(\d+)`, content) - standardRate := extractFloat(`Standard Rate\n\s+€ (\d+)`, content) - taxQty := (totalAmount - standardRate*float64(stayLength)) / 1.5 b := bs.Create(*formatDate(arrivalDate), *formatDate(departureDate), customerName, "", customerEmail, "Booking", customerNumber, commissionAmount, &externalId) - bs.CreateItem(b.Id, item, stayLength, standardRate, "Card") - bs.CreateItem(b.Id, "Taxes", int(taxQty), 1.5, "Cash") + if item, ok := config.NewHost().Items[itemName]; ok { + bs.CreateItem(b.Id, item, stayLength, "Card", customerNumber) + } return b, nil } diff --git a/internal/server/handle_bookings.go b/internal/server/handle_bookings.go index 65dd907..9d49ad2 100644 --- a/internal/server/handle_bookings.go +++ b/internal/server/handle_bookings.go @@ -257,7 +257,7 @@ func handleCreateItem(bs *booking.Service, cs *calendar.Service, hc *config.Host return fmt.Errorf("invalid item name %q", ni.Item) } - i := bs.CreateItem(b.Id, ni.Item, ni.Quantity, ni.Price, ni.PaymentMethod) + i := bs.CreateItem(b.Id, itm, ni.Quantity, ni.PaymentMethod, b.CustomerNumber) if err = cs.Create( itm.CalendarId,