mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-10 04:36:50 +00:00
101 lines
2.5 KiB
Go
101 lines
2.5 KiB
Go
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)
|
|
}
|