|
|
|
@@ -0,0 +1,102 @@
|
|
|
|
|
<?php
|
|
|
|
|
namespace Moko\Plugin\System\MokoSuiteField\Helper;
|
|
|
|
|
|
|
|
|
|
defined('_JEXEC') or die;
|
|
|
|
|
|
|
|
|
|
use Joomla\CMS\Factory;
|
|
|
|
|
use Joomla\Database\DatabaseInterface;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Equipment warranty tracking — warranty status, claim processing, expiry alerts.
|
|
|
|
|
*/
|
|
|
|
|
class WarrantyHelper
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Check warranty status for a piece of equipment.
|
|
|
|
|
*/
|
|
|
|
|
public static function checkWarranty(int $equipmentId): object
|
|
|
|
|
{
|
|
|
|
|
$db = Factory::getContainer()->get(DatabaseInterface::class);
|
|
|
|
|
|
|
|
|
|
$db->setQuery($db->getQuery(true)
|
|
|
|
|
->select('e.id, e.serial_number, e.model, e.install_date')
|
|
|
|
|
->select('e.warranty_start, e.warranty_end, e.warranty_provider, e.warranty_type')
|
|
|
|
|
->from($db->quoteName('#__mokosuitefield_equipment', 'e'))
|
|
|
|
|
->where('e.id = ' . (int) $equipmentId));
|
|
|
|
|
$equipment = $db->loadObject();
|
|
|
|
|
|
|
|
|
|
if (!$equipment) return (object) ['found' => false];
|
|
|
|
|
|
|
|
|
|
$now = new \DateTime('today');
|
|
|
|
|
$warrantyEnd = $equipment->warranty_end ? new \DateTime($equipment->warranty_end) : null;
|
|
|
|
|
|
|
|
|
|
$equipment->under_warranty = $warrantyEnd && $now <= $warrantyEnd;
|
|
|
|
|
$equipment->days_remaining = $warrantyEnd ? max(0, (int) $now->diff($warrantyEnd)->format('%r%a')) : null;
|
|
|
|
|
$equipment->warranty_expired = $warrantyEnd && $now > $warrantyEnd;
|
|
|
|
|
|
|
|
|
|
// Get claim history
|
|
|
|
|
$db->setQuery($db->getQuery(true)
|
|
|
|
|
->select('COUNT(*) AS total_claims, COALESCE(SUM(claim_amount), 0) AS total_claimed')
|
|
|
|
|
->from('#__mokosuitefield_warranty_claims')
|
|
|
|
|
->where('equipment_id = ' . (int) $equipmentId));
|
|
|
|
|
$claims = $db->loadObject();
|
|
|
|
|
|
|
|
|
|
$equipment->total_claims = (int) ($claims->total_claims ?? 0);
|
|
|
|
|
$equipment->total_claimed = (float) ($claims->total_claimed ?? 0);
|
|
|
|
|
|
|
|
|
|
return $equipment;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Submit a warranty claim.
|
|
|
|
|
*/
|
|
|
|
|
public static function submitClaim(int $equipmentId, int $woId, string $description, float $claimAmount): object
|
|
|
|
|
{
|
|
|
|
|
$warranty = self::checkWarranty($equipmentId);
|
|
|
|
|
|
|
|
|
|
if (empty($warranty->id)) return (object) ['success' => false, 'error' => 'Equipment not found'];
|
|
|
|
|
if (!$warranty->under_warranty) return (object) ['success' => false, 'error' => 'Warranty expired'];
|
|
|
|
|
|
|
|
|
|
// Verify work order exists and is linked to this equipment
|
|
|
|
|
$db->setQuery($db->getQuery(true)->select('id')->from('#__mokosuitefield_work_orders')
|
|
|
|
|
->where('id = ' . (int) $woId));
|
|
|
|
|
if (!(int) $db->loadResult()) {
|
|
|
|
|
return (object) ['success' => false, 'error' => 'Invalid work order'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$db = Factory::getContainer()->get(DatabaseInterface::class);
|
|
|
|
|
$now = Factory::getDate()->toSql();
|
|
|
|
|
|
|
|
|
|
$claim = (object) [
|
|
|
|
|
'equipment_id' => $equipmentId,
|
|
|
|
|
'wo_id' => $woId,
|
|
|
|
|
'description' => $description,
|
|
|
|
|
'claim_amount' => $claimAmount,
|
|
|
|
|
'status' => 'submitted',
|
|
|
|
|
'submitted_at' => $now,
|
|
|
|
|
'submitted_by' => Factory::getApplication()->getIdentity()->id,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$db->insertObject('#__mokosuitefield_warranty_claims', $claim, 'id');
|
|
|
|
|
return (object) ['success' => true, 'claim_id' => (int) $claim->id];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get equipment with warranties expiring within N days.
|
|
|
|
|
*/
|
|
|
|
|
public static function getExpiringSoon(int $days = 90): array
|
|
|
|
|
{
|
|
|
|
|
$db = Factory::getContainer()->get(DatabaseInterface::class);
|
|
|
|
|
|
|
|
|
|
$db->setQuery($db->getQuery(true)
|
|
|
|
|
->select('e.*, l.name AS location_name, cd.name AS customer_name')
|
|
|
|
|
->from($db->quoteName('#__mokosuitefield_equipment', 'e'))
|
|
|
|
|
->join('LEFT', $db->quoteName('#__mokosuitefield_locations', 'l') . ' ON l.id = e.location_id')
|
|
|
|
|
->join('LEFT', $db->quoteName('#__contact_details', 'cd') . ' ON cd.id = e.contact_id')
|
|
|
|
|
->where($db->quoteName('e.warranty_end') . ' IS NOT NULL')
|
|
|
|
|
->where($db->quoteName('e.warranty_end') . ' BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL ' . (int) $days . ' DAY)')
|
|
|
|
|
->order('e.warranty_end ASC'));
|
|
|
|
|
|
|
|
|
|
return $db->loadObjectList() ?: [];
|
|
|
|
|
}
|
|
|
|
|
}
|