auto create tax items if the item is taxable

This commit is contained in:
Ruidy 2024-08-25 13:21:54 +02:00
parent 2d8fce7ad2
commit 778254bc61
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
2 changed files with 23 additions and 13 deletions

View file

@ -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
}

View file

@ -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,