Rework the fuck of everything

This commit is contained in:
2025-10-19 15:17:36 +02:00
parent 123b2961a2
commit e016dd7851
19 changed files with 762 additions and 155 deletions

View File

@@ -134,3 +134,117 @@ WHERE Payment.paymentDate IS NULL
return s.GetPaymentForBillAndDate(billid, monthFor)
}
func (s *BillService) MovePayment(billid int64, fromMonth time.Time, toMonth time.Time) (Payment, error) {
log.Printf("MovePayment for %d from %v to %v", billid, fromMonth, toMonth)
res := Payment{}
if s == nil {
return res, fmt.Errorf("calling MovePayment on nil BillService")
}
if s.db == nil || !s.db.Ready {
return res, fmt.Errorf("cannot move payment, db is nil or not ready - %v", s.db)
}
// First get the existing payment
existingPayment, err := s.GetPaymentForBillAndDate(billid, fromMonth)
if err != nil {
return res, fmt.Errorf("failed to get existing payment: %w", err)
}
// Delete the old payment
_, err = s.db.writeConn.Exec(`
DELETE FROM Payment WHERE billid = ? AND monthFor = date(strftime('%Y-%m-01', ?))
`, billid, fromMonth)
if err != nil {
return res, fmt.Errorf("failed to delete old payment: %w", err)
}
// Create new payment in the target month
payment, err := s.MarkPaid(billid, toMonth, existingPayment.PaymentDate)
if err != nil {
return res, fmt.Errorf("failed to create new payment: %w", err)
}
return payment, nil
}
func (s *BillService) AddBill(name string) (Bill, error) {
log.Printf("AddBill with name %s", name)
res := Bill{}
if s == nil {
return res, fmt.Errorf("calling AddBill on nil BillService")
}
if s.db == nil || !s.db.Ready {
return res, fmt.Errorf("cannot add bill, db is nil or not ready - %v", s.db)
}
qres, err := s.db.writeConn.Exec(`
INSERT INTO Bill (name) VALUES (?)
`, name)
if err != nil {
return res, fmt.Errorf("failed to insert bill: %w", err)
}
id, err := qres.LastInsertId()
if err != nil {
return res, fmt.Errorf("failed to get last insert id: %w", err)
}
res.Id = id
res.Name = name
// Refresh the bills cache
_, _ = s.GetAllBills()
return res, nil
}
func (s *BillService) RemoveBill(billid int64) error {
log.Printf("RemoveBill with id %d", billid)
if s == nil {
return fmt.Errorf("calling RemoveBill on nil BillService")
}
if s.db == nil || !s.db.Ready {
return fmt.Errorf("cannot remove bill, db is nil or not ready - %v", s.db)
}
// Delete all payments for this bill first
_, err := s.db.writeConn.Exec(`
DELETE FROM Payment WHERE billid = ?
`, billid)
if err != nil {
return fmt.Errorf("failed to delete payments for bill: %w", err)
}
// Delete the bill
_, err = s.db.writeConn.Exec(`
DELETE FROM Bill WHERE id = ?
`, billid)
if err != nil {
return fmt.Errorf("failed to delete bill: %w", err)
}
// Refresh the bills cache
_, _ = s.GetAllBills()
return nil
}
func (s *BillService) UnmarkPaid(billid int64, month time.Time) error {
log.Printf("UnmarkPaid for %d and %v", billid, month)
if s == nil {
return fmt.Errorf("calling UnmarkPaid on nil BillService")
}
if s.db == nil || !s.db.Ready {
return fmt.Errorf("cannot unmark paid, db is nil or not ready - %v", s.db)
}
_, err := s.db.writeConn.Exec(`
DELETE FROM Payment WHERE billid = ? AND monthFor = date(strftime('%Y-%m-01', ?))
`, billid, month)
if err != nil {
return fmt.Errorf("failed to delete payment: %w", err)
}
return nil
}