2017-08-11 16:10:27 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Antadis\API\Front\Models\Sale as BaseSale;
|
2017-08-14 12:39:19 +02:00
|
|
|
use App\Classes\ImageTrait;
|
2017-08-11 16:10:27 +02:00
|
|
|
|
|
|
|
class Sale extends BaseSale {
|
2017-08-14 12:39:19 +02:00
|
|
|
use ImageTrait;
|
2017-08-16 12:35:31 +02:00
|
|
|
|
2017-08-11 16:10:27 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function toArray() {
|
2017-08-14 17:56:42 +02:00
|
|
|
global $cookie;
|
2017-08-30 10:59:48 +02:00
|
|
|
|
2017-08-11 16:10:27 +02:00
|
|
|
$sale = parent::toArray();
|
2017-08-14 12:39:19 +02:00
|
|
|
$is_active = strtotime($this->date_start) < strtotime(date('Y-m-d 07:00:00'));
|
|
|
|
$sale['img'] = $this->getSaleImage($this->id, $is_active);
|
2017-08-14 17:56:42 +02:00
|
|
|
$sale['reduction'] = $this->extraFields['reduction'] ? $this->extraFields['reduction'] : '0';
|
2017-08-16 12:35:31 +02:00
|
|
|
$sale['tags'] = $this->extraFields['tags'] ? $this->extraFields['tags'] : [];
|
2017-08-30 10:59:48 +02:00
|
|
|
|
|
|
|
if (!class_exists('SaleDelay')) {
|
|
|
|
require_once _PS_ROOT_DIR_.'/modules/privatesales_delay/saledelay.php';
|
|
|
|
}
|
|
|
|
$delay = \SaleDelay::getDayDelay($this->extraFields['delivery_delay'], $cookie->id_lang);
|
|
|
|
$delay_from = \SaleDelay::getDateWithDelay($delay['value'], new \DateTime());
|
|
|
|
$delay_to = \SaleDelay::getDateWithDelay($delay['value_max'], new \DateTime());
|
|
|
|
$sale['delay_from'] = $delay_from->format('d/m');
|
|
|
|
$sale['delay_to'] = $delay_to->format('d/m');
|
2017-08-11 16:10:27 +02:00
|
|
|
return $sale;
|
|
|
|
}
|
2017-08-14 17:56:42 +02:00
|
|
|
|
2017-08-16 12:35:31 +02:00
|
|
|
/**
|
|
|
|
* Returns the associtated tags for $ids_sales
|
|
|
|
*
|
|
|
|
* @param array $ids_sales List of sale's id
|
|
|
|
*
|
|
|
|
* @return array array(
|
|
|
|
* id_sale => array(
|
|
|
|
* id_tag,
|
|
|
|
* id_tag,
|
|
|
|
* ...
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
static public function getTagsBySaleIds(array $ids_sales) {
|
|
|
|
$sales_tags = array();
|
2017-08-21 13:36:07 +02:00
|
|
|
|
|
|
|
if (!empty($ids_sales)) {
|
|
|
|
$tags = \Db::getInstance()->executeS('
|
|
|
|
SELECT p.`id_sale`, pcf.`id_parent` as `id_tag`
|
|
|
|
FROM ps_privatesale p
|
|
|
|
LEFT JOIN ps_privatesale_category pc ON (pc.`id_sale` = p.`id_sale`)
|
|
|
|
LEFT JOIN ps_category_family_association pcfa ON (pc.`id_category` = pcfa.`id_category`)
|
|
|
|
LEFT JOIN ps_category_family pcf ON (pcfa.`id_category_family` = pcf.`id_category_family`)
|
|
|
|
WHERE p.`id_sale` IN (' . implode(',', $ids_sales) . ')
|
|
|
|
AND pcf.`id_parent` IS NOT NULL
|
|
|
|
GROUP BY p.`id_sale`, pcf.`id_parent`
|
|
|
|
');
|
|
|
|
|
|
|
|
foreach ($tags as $tag) {
|
|
|
|
$sales_tags[(int)$tag['id_sale']][] = (int)$tag['id_tag'];
|
|
|
|
}
|
2017-08-16 12:35:31 +02:00
|
|
|
}
|
|
|
|
return $sales_tags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the associtated reduction for $ids_sales
|
|
|
|
*
|
|
|
|
* @param array $ids_sales List of sale's id
|
|
|
|
*
|
|
|
|
* @return array array(
|
|
|
|
* id_sale => reduction
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
static public function getReductionBySaleIds(array $ids_sales) {
|
2017-08-14 17:56:42 +02:00
|
|
|
global $cookie;
|
|
|
|
|
2017-08-16 12:35:31 +02:00
|
|
|
$sales_reduction = array();
|
2017-08-21 13:36:07 +02:00
|
|
|
if (!empty($id_sales)) {
|
|
|
|
$reductions = \Db::getInstance()->executeS('
|
|
|
|
SELECT id_sale, value FROM ' . _DB_PREFIX_ . 'privatesale_extrafield_sale
|
|
|
|
WHERE `id_field` = 2
|
|
|
|
AND `id_sale` IN (' . implode(',', $ids_sales) . ')
|
|
|
|
AND `id_lang` = ' . (int)$cookie->id_lang . '
|
|
|
|
');
|
|
|
|
$sales_reduction = array();
|
|
|
|
foreach ($reductions as $reduction) {
|
|
|
|
$sales_reduction[(int)$reduction['id_sale']] = $reduction['value'];
|
|
|
|
}
|
|
|
|
|
2017-08-16 12:35:31 +02:00
|
|
|
}
|
|
|
|
return $sales_reduction;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*
|
|
|
|
* Adding the reduction extraField
|
|
|
|
* Adding the tags extraField
|
|
|
|
*/
|
|
|
|
static public function apiGetSales($future = false) {
|
|
|
|
$sales = parent::apiGetSales($future);
|
|
|
|
$ids_sales = array();
|
|
|
|
foreach ($sales as $sale) {
|
|
|
|
$ids_sales[] = $sale->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$reductions = static::getReductionBySaleIds($ids_sales);
|
|
|
|
foreach ($sales as $sale) {
|
|
|
|
$value = isset($reductions[(int)$sale->id]) ? $reductions[(int)$sale->id] : null;
|
|
|
|
$sale = $sale->populateRow('reduction', $value, '0');
|
|
|
|
}
|
|
|
|
|
|
|
|
$sales_tags = static::getTagsBySaleIds($ids_sales);
|
|
|
|
foreach ($sales as $sale) {
|
|
|
|
$value = isset($sales_tags[(int)$sale->id]) ? $sales_tags[(int)$sale->id] : null;
|
|
|
|
$sale = $sale->populateRow('tags', $value, array());
|
2017-08-14 17:56:42 +02:00
|
|
|
}
|
|
|
|
return $sales;
|
|
|
|
}
|
2017-08-16 12:35:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate the current Sale model field ($name) with $value
|
|
|
|
* If $value is null it will populate the field with the given $defaultValue
|
|
|
|
*
|
|
|
|
* @param string $name The field name to populate
|
|
|
|
* @param any $value The value to populate with
|
|
|
|
* @param any $defaultValue The defaultValue to apply
|
|
|
|
*
|
|
|
|
* @return Sale The current sale model with the populated field.
|
|
|
|
*/
|
|
|
|
protected function populateRow($name, $value, $defaultValue = null) {
|
|
|
|
$value = $value !== null ? $value : $defaultValue;
|
|
|
|
return $this->populate(array(
|
|
|
|
$name => $value
|
|
|
|
), $this->id);
|
|
|
|
}
|
2017-08-11 16:10:27 +02:00
|
|
|
}
|