generated from MokoConsulting/Template-Joomla
feat: build out core helpers — 1 files added
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
namespace Moko\Plugin\System\MokoSuiteAuto\Helper;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\Database\DatabaseInterface;
|
||||
|
||||
/**
|
||||
* Trade-in appraisal — valuation, condition assessment, equity calculation.
|
||||
*/
|
||||
class TradeInHelper
|
||||
{
|
||||
/**
|
||||
* Submit a trade-in appraisal.
|
||||
*/
|
||||
public static function submitAppraisal(string $vin, int $ownerContactId, int $mileage, string $condition, float $appraisalValue): object
|
||||
{
|
||||
$allowedConditions = ['excellent', 'good', 'fair', 'poor'];
|
||||
if (!in_array($condition, $allowedConditions, true)) {
|
||||
$condition = 'fair';
|
||||
}
|
||||
|
||||
if ($appraisalValue < 0) {
|
||||
throw new \InvalidArgumentException('Appraisal value cannot be negative.');
|
||||
}
|
||||
|
||||
$db = Factory::getContainer()->get(DatabaseInterface::class);
|
||||
|
||||
$tradeIn = (object) [
|
||||
'vin' => strtoupper($vin),
|
||||
'owner_contact_id' => $ownerContactId,
|
||||
'mileage' => max(0, $mileage),
|
||||
'vehicle_condition'=> $condition,
|
||||
'appraisal_value' => round($appraisalValue, 2),
|
||||
'status' => 'pending_review',
|
||||
'appraised_by' => (int) Factory::getApplication()->getIdentity()->id,
|
||||
'appraised_at' => Factory::getDate()->toSql(),
|
||||
];
|
||||
|
||||
$db->insertObject('#__mokosuiteauto_trade_ins', $tradeIn, 'id');
|
||||
|
||||
return (object) ['success' => true, 'trade_in_id' => (int) $tradeIn->id];
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate trade equity (trade value minus payoff).
|
||||
*/
|
||||
public static function calculateEquity(int $tradeInId): object
|
||||
{
|
||||
$db = Factory::getContainer()->get(DatabaseInterface::class);
|
||||
|
||||
$db->setQuery($db->getQuery(true)
|
||||
->select('ti.appraisal_value, ti.payoff_amount')
|
||||
->from($db->quoteName('#__mokosuiteauto_trade_ins', 'ti'))
|
||||
->where('ti.id = ' . (int) $tradeInId));
|
||||
$ti = $db->loadObject();
|
||||
|
||||
if (!$ti) {
|
||||
return (object) ['success' => false, 'error' => 'Trade-in not found'];
|
||||
}
|
||||
|
||||
$equity = (float) $ti->appraisal_value - (float) ($ti->payoff_amount ?? 0);
|
||||
|
||||
return (object) [
|
||||
'trade_in_id' => $tradeInId,
|
||||
'appraisal_value' => (float) $ti->appraisal_value,
|
||||
'payoff_amount' => (float) ($ti->payoff_amount ?? 0),
|
||||
'equity' => round($equity, 2),
|
||||
'is_negative' => $equity < 0,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get pending trade-in appraisals needing manager review.
|
||||
*/
|
||||
public static function getPendingReview(): array
|
||||
{
|
||||
$db = Factory::getContainer()->get(DatabaseInterface::class);
|
||||
|
||||
$db->setQuery($db->getQuery(true)
|
||||
->select('ti.*, cd.name AS owner_name, cd.telephone')
|
||||
->from($db->quoteName('#__mokosuiteauto_trade_ins', 'ti'))
|
||||
->join('LEFT', $db->quoteName('#__contact_details', 'cd') . ' ON cd.id = ti.owner_contact_id')
|
||||
->where($db->quoteName('ti.status') . ' = ' . $db->quote('pending_review'))
|
||||
->order('ti.appraised_at ASC'));
|
||||
|
||||
return $db->loadObjectList() ?: [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user