public static function getTicket($vendorId)
{
$ticket = array();
$app = Factory::getApplication();
$ticketId = $app->input->get('ticketid', 1, 'int');
// if not set, always set it to 1, ticekt 1 is default
$unitId = $app->input->get('unitid', 0, 'int');
// if not set, always set it to 1, ticekt 1 is default
$sectionId = $app->input->get('sectionid', 0, 'int');
// if not set, always set it to 1, ticekt 1 is default
$existsSection = PhocacartSection::existsSection($sectionId);
$existsUnit = PhocacartUnit::existsUnit($unitId, $sectionId);
// SECTION IS DEFINED by administrator
// UNIT IS DEFINED by administrator
// TICKET CAN BE CREATED by vendor
// Check if the section even exists, if not set to first you will find
if (!$existsSection) {
$sections = PhocacartSection::getSections(1);
if (!empty($sections)) {
foreach ($sections as $k => $v) {
$sectionId = (int) $v->id;
}
} else {
$sectionId = 0;
}
}
// Check if the unit even exists, if not set to default 1
if (!$existsUnit) {
$units = PhocacartUnit::getUnits($sectionId, 1);
if (!empty($units)) {
foreach ($units as $k => $v) {
$unitId = (int) $v->id;
}
} else {
$unitId = 0;
}
}
// Check complet ticket
$existsTicket = self::existsTicket($vendorId, $ticketId, $unitId, $sectionId);
if ($existsTicket) {
// Asked ticket exists ... OK
$ticket['ticketid'] = $ticketId;
$ticket['unitid'] = $unitId;
$ticket['sectionid'] = $sectionId;
return $ticket;
} else {
// Asked ticket does not exists ... find another
$firstTicket = self::getFirstVendorTicket($vendorId, $unitId, $sectionId);
if ($firstTicket) {
// Some ticket found ... OK
$ticket['ticketid'] = $firstTicket;
$ticket['unitid'] = $unitId;
$ticket['sectionid'] = $sectionId;
return $ticket;
} else {
// No ticket found ... set the default
// if there is no ticket id for this vendor return the base - ticket = 1
$ticket['ticketid'] = 1;
$ticket['unitid'] = $unitId;
$ticket['sectionid'] = $sectionId;
PhocacartTicket::addNewVendorTicket($vendorId, 1, $unitId, $sectionId);
return $ticket;
}
}
$ticket['ticketid'] = 1;
$ticket['unitid'] = $unitId;
$ticket['sectionid'] = $sectionId;
}