change the offset

This commit is contained in:
Ruidy 2024-02-24 21:48:38 +01:00
parent 0ccabe71b3
commit 8afd35da8e
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
2 changed files with 1 additions and 102 deletions

View file

@ -14,7 +14,7 @@ type HostItem struct {
func NewHost() *Host {
return &Host{
CustomerSeed: 286,
CustomerSeed: 239,
PaymentMethods: []string{"Card", "Cash", "Cheque", "Transfer"},
Platforms: []string{"Booking", "AirBnb", "TripAdvisor", "Other"},
Items: []HostItem{

View file

@ -1,101 +0,0 @@
package main
import (
"fmt"
"log"
"time"
"github.com/rjNemo/rentease/config"
"github.com/rjNemo/rentease/internal/booking"
u "github.com/rjNemo/underscore"
"gorm.io/gorm"
)
type Invoice struct {
To time.Time
From time.Time
Platform string
Name string
PhoneNumber string `gorm:"column:phoneNumber"`
Room string
PaymentMethod string `gorm:"column:paymentMethod"`
Status string
Id int
Price float64 `gorm:"type:decimal(10,2)"`
PlatformFees float64 `gorm:"type:decimal(10,2);column:platformFees"`
CustomerNumber int `gorm:"column:customersNumber"`
WithTaxes bool `gorm:"column:withTaxes"`
}
func (Invoice) TableName() string {
return "Invoice"
}
var hc *config.Host = config.NewHost()
func MigrateDB(db *gorm.DB) error {
return db.Transaction(func(tx *gorm.DB) error {
invoices := make([]Invoice, 0)
if err := tx.Find(&invoices).Error; err != nil {
return err
}
// log.Println(invoices)
// - for each row in Invoice table,
for _, i := range invoices {
// create a booking strcut based on the invoice value, commit
fmt.Println(" ")
b := &booking.Booking{
From: i.From,
To: i.To,
PhoneNumber: i.PhoneNumber,
CustomerNumber: i.CustomerNumber,
Platform: i.Platform,
PlatformFees: i.PlatformFees,
Name: i.Name,
}
if err := tx.Create(b).Error; err != nil {
return err
}
log.Printf("%+v\n", b)
// take the new booking id and create a item struct based on the Invoice value, commit
item := &booking.Item{
BookingId: b.Id,
Item: i.Room,
Quantity: computeQty(i),
Price: priceFromConfig(i, hc),
PaymentMethod: i.PaymentMethod,
PaymentStatus: i.Status,
}
if err := tx.Create(item).Error; err != nil {
return err
}
log.Printf("%+v\n", item)
if i.WithTaxes {
taxItem := &booking.Item{
BookingId: b.Id,
Item: "Taxes",
Quantity: computeQty(i) * i.CustomerNumber,
Price: 1.5,
PaymentMethod: i.PaymentMethod,
PaymentStatus: i.Status,
}
if err := tx.Create(taxItem).Error; err != nil {
return err
}
log.Printf("%+v\n", taxItem)
}
}
return nil
})
}
func priceFromConfig(i Invoice, hc *config.Host) float64 {
itm, _ := u.Find(hc.Items, func(hi config.HostItem) bool {
return hi.Name == i.Room
})
return itm.Price
}
func computeQty(i Invoice) int {
return int(i.To.Sub(i.From) / 86400000000000)
}