mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 02:36:49 +00:00
327 lines
8.9 KiB
Go
327 lines
8.9 KiB
Go
package booking
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
u "github.com/rjNemo/underscore"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
|
|
"github.com/rjNemo/rentease/config"
|
|
)
|
|
|
|
type Service struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewService(db *gorm.DB) *Service {
|
|
return &Service{db: db}
|
|
}
|
|
|
|
func (bs Service) All() []*Line {
|
|
bookings := make([]*Line, 0)
|
|
bs.db.Raw(`
|
|
select bookings.id, customer_name, "from", "to", platform, sum(price * quantity) as total, canceled
|
|
from bookings
|
|
left join items on bookings.id = items.booking_id
|
|
group by bookings.id
|
|
order by id desc;
|
|
`).
|
|
Scan(&bookings)
|
|
return bookings
|
|
}
|
|
|
|
func (bs Service) Create(From time.Time, To time.Time, Name, PhoneNumber, Email, Platform string,
|
|
CustomerNumber int, PlatformFees float64, externalId *string,
|
|
) *Booking {
|
|
b := &Booking{
|
|
Name: Name,
|
|
PhoneNumber: PhoneNumber,
|
|
CustomerNumber: CustomerNumber,
|
|
Email: Email,
|
|
From: From,
|
|
To: To,
|
|
Platform: Platform,
|
|
PlatformFees: PlatformFees,
|
|
ExternalId: externalId,
|
|
}
|
|
err := bs.db.Create(b)
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
return b
|
|
}
|
|
|
|
func (bs Service) One(id int) *Booking {
|
|
b := &Booking{Id: id}
|
|
bs.db.Preload("Items").First(b)
|
|
return b
|
|
}
|
|
|
|
func (bs Service) Update(id int, From time.Time, To time.Time, Name string, PhoneNumber string, Email string, Platform string,
|
|
CustomerNumber int, PlatformFees float64, externalId *string,
|
|
) *Booking {
|
|
b := &Booking{
|
|
Id: id,
|
|
Name: Name,
|
|
PhoneNumber: PhoneNumber,
|
|
CustomerNumber: CustomerNumber,
|
|
Email: Email,
|
|
From: From,
|
|
To: To,
|
|
Platform: Platform,
|
|
PlatformFees: PlatformFees,
|
|
ExternalId: externalId,
|
|
}
|
|
bs.db.Save(b)
|
|
return b
|
|
}
|
|
|
|
func (bs Service) CreateItem(bid int, item string, qty int, price float64, method string) *Item {
|
|
i := &Item{
|
|
BookingId: bid,
|
|
Item: item,
|
|
Quantity: qty,
|
|
Price: price,
|
|
PaymentMethod: method,
|
|
}
|
|
_ = bs.db.Create(i)
|
|
return i
|
|
}
|
|
|
|
func (bs Service) PayItem(id int) *Item {
|
|
i := new(Item)
|
|
bs.db.Model(i).Clauses(clause.Returning{}).Where("id = ?", id).Update("payment_status", "Completed")
|
|
return i
|
|
}
|
|
|
|
func (bs Service) OneItem(id int) *Item {
|
|
i := &Item{Id: id}
|
|
bs.db.First(i)
|
|
return i
|
|
}
|
|
|
|
func (bs Service) UpdateItem(id int, item string, qty int, price float64, paymentMethod, paymentStatus string) *Item {
|
|
i := new(Item)
|
|
bs.db.Model(i).Clauses(clause.Returning{}).Where("id = ?", id).Updates(map[string]any{
|
|
"item": item,
|
|
"payment_method": paymentMethod,
|
|
"payment_status": paymentStatus,
|
|
"quantity": qty,
|
|
"price": price,
|
|
})
|
|
return i
|
|
}
|
|
|
|
type Report struct {
|
|
Lines []*Line
|
|
Total float64
|
|
PlatformFees float64
|
|
Fee float64
|
|
Profit float64
|
|
CardTotal float64
|
|
BookingFees float64
|
|
}
|
|
|
|
type Line struct {
|
|
From time.Time
|
|
To time.Time
|
|
CustomerName string
|
|
Platform string
|
|
Id int
|
|
Total float64
|
|
PlatformFees float64
|
|
Canceled bool
|
|
}
|
|
|
|
func (l Line) InvoiceNumber(hc *config.Host) string {
|
|
return fmt.Sprintf("%s%04s", hc.InvoicePrefix, strconv.Itoa(l.Id+hc.CustomerSeed))
|
|
}
|
|
|
|
func (l Line) Fee() float64 {
|
|
if l.Platform == "Other" {
|
|
return l.Total * 5 / 100
|
|
}
|
|
return l.Total * 10 / 100
|
|
}
|
|
|
|
func (l Line) Profit() float64 {
|
|
return l.Total - l.PlatformFees - l.Fee()
|
|
}
|
|
|
|
func (bs Service) BuildReport(period string, month, year int) *Report {
|
|
lines := make([]*Line, 0)
|
|
var startDate time.Time
|
|
var endDate time.Time
|
|
|
|
if period == "month" {
|
|
startDate = time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC)
|
|
endDate = time.Date(year, time.Month(month)+1, 1, 0, 0, 0, 0, time.UTC).Add(-24 * time.Hour)
|
|
} else {
|
|
startDate = time.Date(year, time.January, 1, 0, 0, 0, 0, time.UTC)
|
|
endDate = time.Date(year, time.December, 31, 0, 0, 0, 0, time.UTC)
|
|
}
|
|
|
|
bs.db.Raw(`
|
|
select bookings.id, customer_name, "from", "to", platform, sum(price * quantity) as total, platform_fees
|
|
from bookings
|
|
join items on bookings.id = items.booking_id
|
|
where "to" between ? and ?
|
|
group by bookings.id
|
|
order by bookings.id;
|
|
`,
|
|
startDate.Format(time.DateOnly), endDate.Format(time.DateOnly)).
|
|
Scan(&lines)
|
|
|
|
cardTotal := 0.0
|
|
bs.db.Raw(`
|
|
select sum(total)
|
|
from (select sum(price * quantity) as total
|
|
from bookings
|
|
join items on bookings.id = items.booking_id
|
|
where "to" between ? and ?
|
|
and payment_method = 'Card'
|
|
group by booking_id) as t;
|
|
`,
|
|
startDate.Format("2006-01-02"), endDate.Format("2006-01-02")).
|
|
Scan(&cardTotal)
|
|
|
|
return &Report{
|
|
Total: u.Reduce(lines, func(l *Line, sum float64) float64 { return sum + l.Total }, 0.0),
|
|
PlatformFees: u.Reduce(lines, func(l *Line, sum float64) float64 { return sum + l.PlatformFees }, 0.0),
|
|
Fee: u.Reduce(lines, func(l *Line, sum float64) float64 { return sum + l.Fee() }, 0.0),
|
|
Profit: u.Reduce(lines, func(l *Line, sum float64) float64 { return sum + l.Profit() }, 0.0),
|
|
Lines: lines,
|
|
CardTotal: cardTotal,
|
|
BookingFees: u.Reduce(lines, func(l *Line, sum float64) float64 {
|
|
if l.Platform == "Booking" {
|
|
return sum + l.PlatformFees
|
|
}
|
|
return sum
|
|
}, 0.0),
|
|
}
|
|
}
|
|
|
|
func (bs Service) CreateRequest(From time.Time, To time.Time, Name string, PhoneNumber string, Email string, Item string, CustomerNumber int) *BookingRequest {
|
|
b := &BookingRequest{
|
|
CustomerName: Name,
|
|
PhoneNumber: &PhoneNumber,
|
|
CustomerNumber: CustomerNumber,
|
|
Email: &Email,
|
|
From: From,
|
|
To: To,
|
|
ItemType: Item,
|
|
}
|
|
_ = bs.db.Create(b)
|
|
return b
|
|
}
|
|
|
|
func (bs Service) Cancel(id int) {
|
|
b := &Booking{Id: id}
|
|
bs.db.Model(&b).Update("canceled", true)
|
|
}
|
|
|
|
func (bs Service) ParseFromApi(rawContent string) (*Booking, error) {
|
|
type BookingInfo struct {
|
|
Content string `json:"content"`
|
|
}
|
|
|
|
var bookingInfo BookingInfo
|
|
err := json.Unmarshal([]byte(rawContent), &bookingInfo)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error unmarshalling JSON: %w", err)
|
|
}
|
|
|
|
content := strings.ReplaceAll(strings.TrimSpace(bookingInfo.Content), "\u00a0", " ")
|
|
|
|
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)
|
|
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")
|
|
|
|
return b, nil
|
|
}
|
|
|
|
func extractDate(pattern, content string) string {
|
|
re := regexp.MustCompile(pattern + `(lun|mar|mer|jeu|ven|sam|dim)\. \d{1,2} (janv|févr|mars|avr|mai|juin|juil|août|sept|oct|nov|déc)\.? \d{4}`)
|
|
dateMatch := re.FindString(content)
|
|
|
|
if dateMatch == "" {
|
|
log.Println("date not found")
|
|
return ""
|
|
}
|
|
|
|
// Regular expression to remove the prefix
|
|
rePrefix := regexp.MustCompile(pattern + `\w+\.\s*`)
|
|
dateString := rePrefix.ReplaceAllString(dateMatch, "")
|
|
return dateString
|
|
}
|
|
|
|
func extractInt(pattern, content string) int {
|
|
re := regexp.MustCompile(pattern)
|
|
match := re.FindStringSubmatch(content)
|
|
if len(match) > 1 {
|
|
val, err := strconv.Atoi(match[1])
|
|
if err == nil {
|
|
return val
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func extractFloat(pattern, content string) float64 {
|
|
re := regexp.MustCompile(pattern)
|
|
match := re.FindStringSubmatch(content)
|
|
if len(match) > 1 {
|
|
val, err := strconv.ParseFloat(strings.ReplaceAll(match[1], ",", "."), 64)
|
|
if err == nil {
|
|
return val
|
|
}
|
|
}
|
|
return 0.0
|
|
}
|
|
|
|
func extractString(pattern, content string) string {
|
|
re := regexp.MustCompile(pattern)
|
|
match := re.FindStringSubmatch(content)
|
|
if len(match) > 1 {
|
|
return strings.TrimSpace(match[1])
|
|
}
|
|
return strings.TrimSpace(match[0])
|
|
}
|
|
|
|
func formatDate(date string) *time.Time {
|
|
months := map[string]string{
|
|
"janv.": "01", "fév": "02", "mar": "03", "avr": "04",
|
|
"mai": "05", "juin": "06", "juil.": "07", "août": "08",
|
|
"sep": "09", "oct": "10", "nov": "11", "déc": "12",
|
|
}
|
|
parts := strings.Split(date, " ")
|
|
dateString := fmt.Sprintf("%s-%02s-%02s", parts[2], months[parts[1]], parts[0])
|
|
|
|
t, err := time.Parse(time.DateOnly, dateString)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return nil
|
|
}
|
|
return &t
|
|
}
|