Add sales-month planning for crops/products and fix today task label format

This commit is contained in:
Kai
2026-02-16 14:03:19 +01:00
parent 723e9142b2
commit b1c502a919
8 changed files with 216 additions and 15 deletions

View File

@@ -38,12 +38,21 @@ func ensureSchema(db *sql.DB) error {
sow_start_month TINYINT NOT NULL,
sow_end_month TINYINT NOT NULL,
grow_months TINYINT NOT NULL,
best_sale_month TINYINT NOT NULL DEFAULT 0,
regrow_enabled TINYINT(1) NOT NULL DEFAULT 0,
regrow_cycles INT NOT NULL DEFAULT 0
)`,
`ALTER TABLE crops ADD COLUMN IF NOT EXISTS best_sale_month TINYINT NOT NULL DEFAULT 0`,
`ALTER TABLE crops ADD COLUMN IF NOT EXISTS regrow_enabled TINYINT(1) NOT NULL DEFAULT 0`,
`ALTER TABLE crops ADD COLUMN IF NOT EXISTS regrow_cycles INT NOT NULL DEFAULT 0`,
`UPDATE crops SET regrow_cycles=0 WHERE regrow_cycles < 0`,
`UPDATE crops SET best_sale_month=0 WHERE best_sale_month < 0 OR best_sale_month > 12`,
`CREATE TABLE IF NOT EXISTS products(
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(120) NOT NULL UNIQUE,
best_sale_month TINYINT NOT NULL DEFAULT 0,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
)`,
`CREATE TABLE IF NOT EXISTS crop_steps(
id BIGINT AUTO_INCREMENT PRIMARY KEY,
crop_id BIGINT NOT NULL,
@@ -154,7 +163,7 @@ func (a *App) getFieldsByIDs(ids []int64) ([]Field, error) {
}
func (a *App) listCrops() ([]Crop, error) {
rows, err := a.db.Query(`SELECT id,name,sow_start_month,sow_end_month,grow_months,regrow_enabled,regrow_cycles FROM crops ORDER BY name`)
rows, err := a.db.Query(`SELECT id,name,sow_start_month,sow_end_month,grow_months,best_sale_month,regrow_enabled,regrow_cycles FROM crops ORDER BY name`)
if err != nil {
return nil, err
}
@@ -162,7 +171,7 @@ func (a *App) listCrops() ([]Crop, error) {
var out []Crop
for rows.Next() {
var c Crop
if err := rows.Scan(&c.ID, &c.Name, &c.SowStartMonth, &c.SowEndMonth, &c.GrowMonths, &c.RegrowEnabled, &c.RegrowCycles); err != nil {
if err := rows.Scan(&c.ID, &c.Name, &c.SowStartMonth, &c.SowEndMonth, &c.GrowMonths, &c.BestSaleMonth, &c.RegrowEnabled, &c.RegrowCycles); err != nil {
return nil, err
}
out = append(out, c)
@@ -170,6 +179,24 @@ func (a *App) listCrops() ([]Crop, error) {
return out, rows.Err()
}
func (a *App) listProducts() ([]Product, error) {
rows, err := a.db.Query(`SELECT id,name,best_sale_month FROM products ORDER BY name`)
if err != nil {
return nil, err
}
defer rows.Close()
var out []Product
for rows.Next() {
var p Product
if err := rows.Scan(&p.ID, &p.Name, &p.BestSaleMonth); err != nil {
return nil, err
}
out = append(out, p)
}
return out, rows.Err()
}
func (a *App) listPlans() ([]Plan, error) {
rows, err := a.db.Query(`
SELECT p.id,p.field_id,COALESCE(p.target_ref,''),COALESCE(p.target_name,''),p.crop_id,COALESCE(c.name,''),COALESCE(c.grow_months,1),p.start_month,p.start_day,p.harvest_month,p.harvest_day,COALESCE(p.notes,''),COALESCE(c.regrow_enabled,0),COALESCE(c.regrow_cycles,0)
@@ -301,7 +328,7 @@ func seedCrops(db *sql.DB) error {
{Name: "Baumwolle", SowStartMonth: 2, SowEndMonth: 3, GrowMonths: 8},
}
for _, c := range items {
if _, err := db.Exec(`INSERT INTO crops(name,sow_start_month,sow_end_month,grow_months,regrow_enabled,regrow_cycles) VALUES (?,?,?,?,0,0) ON DUPLICATE KEY UPDATE sow_start_month=VALUES(sow_start_month),sow_end_month=VALUES(sow_end_month),grow_months=VALUES(grow_months)`, c.Name, c.SowStartMonth, c.SowEndMonth, c.GrowMonths); err != nil {
if _, err := db.Exec(`INSERT INTO crops(name,sow_start_month,sow_end_month,grow_months,best_sale_month,regrow_enabled,regrow_cycles) VALUES (?,?,?,?,0,0,0) ON DUPLICATE KEY UPDATE sow_start_month=VALUES(sow_start_month),sow_end_month=VALUES(sow_end_month),grow_months=VALUES(grow_months)`, c.Name, c.SowStartMonth, c.SowEndMonth, c.GrowMonths); err != nil {
return err
}
}