Files
ibiza_sistema/models/ElectricityBill.php

93 lines
2.5 KiB
PHP
Executable File

<?php
class ElectricityBill
{
/**
* Obtener configuraciones de todos los periodos de un año
*/
public static function getYear($year)
{
$db = Database::getInstance();
$bills = $db->fetchAll(
"SELECT * FROM electricity_bills WHERE year = ? ORDER BY FIELD(period, 'Ene-Feb', 'Mar-Abr', 'May-Jun', 'Jul-Ago', 'Sep-Oct', 'Nov-Dic')",
[$year]
);
// Organizar por periodo
$result = [];
foreach ($bills as $bill) {
$result[$bill['period']] = $bill;
}
return $result;
}
/**
* Guardar o actualizar configuración de un periodo
*/
public static function save($data, $userId)
{
$db = Database::getInstance();
$id = $data['id'] ?? null;
$year = $data['year'];
$period = $data['period'];
$totalAmount = $data['total_amount'] ?? 0;
$amountPerHouse = $data['amount_per_house'] ?? 0;
$notes = $data['notes'] ?? '';
if ($id) {
// Actualizar existente
$db->execute(
"UPDATE electricity_bills
SET total_amount = ?, amount_per_house = ?, notes = ?, updated_at = NOW()
WHERE id = ?",
[$totalAmount, $amountPerHouse, $notes, $id]
);
return $id;
}
else {
// Insertar nuevo o actualizar si existe
$db->execute(
"INSERT INTO electricity_bills (year, period, total_amount, amount_per_house, notes)
VALUES (?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
total_amount = VALUES(total_amount),
amount_per_house = VALUES(amount_per_house),
notes = VALUES(notes),
updated_at = NOW()",
[$year, $period, $totalAmount, $amountPerHouse, $notes]
);
return $db->lastInsertId();
}
}
/**
* Obtener periodos bimestrales
*/
public static function getPeriods()
{
return [
'Ene-Feb',
'Mar-Abr',
'May-Jun',
'Jul-Ago',
'Sep-Oct',
'Nov-Dic'
];
}
/**
* Obtener configuración de un periodo específico
*/
public static function getByYearPeriod($year, $period)
{
$db = Database::getInstance();
return $db->fetchOne(
"SELECT * FROM electricity_bills WHERE year = ? AND period = ?",
[$year, $period]
);
}
}