<?php

class CustomerPackages{
    const PACKAGE_TYPES = [
        'ID_STANDARD_TYPE' => 1,
        'ID_OPTION_TYPE' => 2,
        'ID_ADDITONAL_TYPE' => 3
    ];

    public function getShopPackages($idCommercialLead, $idCustomer, $idPackage = 0, $search = ''){
        global $database;

        $whereSql = "WHERE p.status='available' AND p.idPackageType=".self::PACKAGE_TYPES['ID_STANDARD_TYPE']." ";
        $search = $database->escapeValue($search);

        if($idPackage !== 0){
            $whereSql .= " AND p.id=$idPackage";
        }

        if($search !== ''){
            $searchValues = explode(' ', $search);
            $packageCondition = '';
            $descriptionCondition = '';
            $referenceCondition = '';
            $countryCondition = '';
            foreach ($searchValues as $valueToSearch) {
                if($valueToSearch !== ''){
                    $packageCondition .= "p.name like '%".$valueToSearch."%'"." OR ";
                    $descriptionCondition .= " p.description like '%".$valueToSearch."%'"." OR ";
                    $referenceCondition .= "p.reference='".$valueToSearch."' OR ";
                    $countryCondition .= "c.name='".$valueToSearch."' OR ";
                }
            }
            $countryCondition = rtrim($countryCondition, ' OR ');
            $whereSql .= " AND ( $packageCondition
                                $descriptionCondition
                                $referenceCondition
                                $countryCondition)";
        }

        $sql = "SELECT
                   p.id AS idPackage,
                   p.name AS packageName
               FROM
                   ".TABLES['packages']." p
                       INNER JOIN
                   ".TABLES['countries']." c ON c.id = p.idCountry
                   INNER JOIN
                   (SELECT
                       idPackage,
                           plcl.idPaymentType,
                           plcl.idCommercialLead,
                           MAX(IFNULL(idCustomer, 0)) AS idCustomer
                   FROM
                       ".TABLES['price_list_commercial_lead']." plcl
                   LEFT OUTER JOIN ".TABLES['customers']." cust
                       ON cust.id = plcl.idCustomer
                   WHERE
                       (cust.id = $idCustomer OR cust.idUser IS NULL)
                       AND plcl.idCommercialLead = $idCommercialLead
                   GROUP BY plcl.idPackage , plcl.idPaymentType
                   ) AS price_selection
                       ON price_selection.idPackage = p.id
                   INNER JOIN ".TABLES['price_list_commercial_lead']." plcl
                       ON plcl.idPackage = price_selection.idPackage
                       AND plcl.idPaymentType = price_selection.idPaymentType
                       AND plcl.idCommercialLead = price_selection.idCommercialLead
                       AND IFNULL(plcl.idCustomer, 0) = price_selection.idCustomer
                       AND plcl.visibleToCustomer = 1
                   INNER JOIN
                   (SELECT
                       proc.idCountry
                   FROM ".TABLES['processes']." proc
                       GROUP BY proc.idCountry
                   ) proc_countries
                       ON proc_countries.idCountry=p.idCountry
                   $whereSql
               GROUP BY p.id
               ORDER BY p.id DESC";

        return $database->fetchResultArray($sql);
    }
}
