2017-09-28 10:39:08 +02:00

223 lines
6.9 KiB
PHP

<?php
class SupplierDemand extends ObjectModel
{
const RENEW_STATE = 2;
public static $states = array(
1 => array(
'name' => "Fournisseur contacté",
'icon' => 'anticon anticon-phone text-orange',
),
2 => array(
'name' => "Fournisseur relancé",
'icon' => 'anticon anticon-spinner11 text-orange',
),
3 => array(
'name' => "En cours",
'icon' => 'anticon anticon-hour-glass text-orange',
),
4 => array(
'name' => "Pas de réponse",
'icon' => 'anticon anticon-blocked text-rose',
),
5 => array(
'name' => "SAV réglé",
'icon' => 'anticon anticon-checkmark text-green-light',
),
);
public static $solutions = array(
1 => 'Remboursement/Avoir',
2 => 'Remplacement du produit',
3 => 'Autre'
);
public $id_order;
public $id_order_detail;
public $qty;
public $id_state;
public $comment;
public $solution;
public $date_add;
public $date_upd;
protected $fieldsRequired = array('id_order', 'id_order_detail','qty');
protected $fieldsValidate = array(
'id_order' => 'isUnsignedId',
'id_order_detail' => 'isUnsignedId',
'qty' => 'isUnsignedId',
'id_state' => 'isUnsignedId',
'comment' => 'isString',
'solution' => 'isInt',
'date_add' => 'isDate',
'date_upd' => 'isDate',
);
protected $table = 'supplier_demand';
protected $identifier = 'id_supplier_demand';
public function getFields()
{
parent::validateFields();
$fields['id_order'] = (int)$this->id_order;
$fields['id_order_detail'] = (int)$this->id_order_detail;
$fields['qty'] = (int)$this->qty;
$fields['id_state'] = (int)$this->id_state;
$fields['comment'] = pSQL($this->comment);
$fields['solution'] = (int)$this->solution;
$fields['date_add'] = pSQL($this->date_add);
$fields['date_upd'] = pSQL($this->date_upd);
return $fields;
}
public function delete()
{
return true;
}
/**
* Get demands
* @param $states array get only demands in specific states
* @return Array Groups
*/
public static function getDemands($states = array(), $where = false)
{
if($where){
return Db::getInstance()->executeS('
SELECT *
FROM `'._DB_PREFIX_.'supplier_demand` sd
WHERE '.$where.'
');
} else {
return Db::getInstance()->executeS('
SELECT *
FROM `'._DB_PREFIX_.'supplier_demand` sd
WHERE 1
'.(!empty($states) ? 'AND sd.`id_state` IN ('.implode(',',$states).')' : '').'
ORDER BY sd.date_add DESC
');
}
}
public static function getCountDemands($states = array(), $where = false)
{
if($where){
return Db::getInstance()->getValue('
SELECT COUNT(`id_supplier_demand`)
FROM `'._DB_PREFIX_.'supplier_demand` sd
WHERE '.$where.'
');
} else {
return Db::getInstance()->getValue('
SELECT COUNT(`id_supplier_demand`)
FROM `'._DB_PREFIX_.'supplier_demand` sd
WHERE 1
'.(!empty($states) ? 'AND sd.`id_state` IN ('.implode(',',$states).')' : '').'
ORDER BY sd.date_add DESC
');
}
}
public function save($nullValues = false, $autodate = true)
{
if (parent::save()) {
return true;
}
}
public static function getCountRenews($id_supplier_demand)
{
return Db::getInstance()->getValue("
SELECT COUNT(id_supplier_demand_history)
FROM `" . _DB_PREFIX_ . "supplier_demand_history`
WHERE `id_supplier_demand` = " . (int)$id_supplier_demand."
AND `id_state` = ". self::RENEW_STATE
);
}
public static function getCustomer($id_supplier_demand)
{
return Db::getInstance()->getRow("
SELECT c.*
FROM `" . _DB_PREFIX_ . "customer` c
LEFT JOIN `" . _DB_PREFIX_ . "orders` o ON (o.id_customer = c.id_customer)
LEFT JOIN `" . _DB_PREFIX_ . "supplier_demand` sd ON (sd.id_order = o.id_order)
WHERE sd.`id_supplier_demand` = " . (int)$id_supplier_demand
);
}
public function getHistory()
{
return self::getHistoryStatic($this->id);
}
public static function getHistoryStatic($id_supplier_demand)
{
return Db::getInstance()->ExecuteS("
SELECT h.*, CONCAT(e.firstname,' ',e.lastname) as employee
FROM `" . _DB_PREFIX_ . "supplier_demand_history` h
LEFT JOIN `" . _DB_PREFIX_ . "employee` e ON (e.id_employee = h.id_employee)
WHERE h.`id_supplier_demand` = " . (int)$id_supplier_demand."
ORDER BY h.date_add DESC"
);
}
public function addHistory()
{
return self::addHistoryStatic($this->id, $this->id_state);
}
public static function addHistoryStatic($id_supplier_demand, $id_state)
{
global $cookie;
return Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'supplier_demand_history`
VALUES (DEFAULT,'.(int)$id_supplier_demand.','.(int)$cookie->id_employee.','.(int)$id_state.',NOW())'
);
}
public function getProduct()
{
return self::getProductStatic($this->id);
}
public static function getProductStatic($id_supplier_demand)
{
return Db::getInstance()->getRow("
SELECT od.*
FROM `" . _DB_PREFIX_ . "supplier_demand` sd
LEFT JOIN `" . _DB_PREFIX_ . "order_detail` od ON (od.id_order_detail = sd.id_order_detail)
WHERE sd.`id_supplier_demand` = " . (int)$id_supplier_demand
);
}
public function getSale($with_product = false)
{
return self::getSaleStatic($this->id, $with_product);
}
public static function getSaleStatic($id_supplier_demand, $with_product = false)
{
$product = self::getProductStatic($id_supplier_demand);
$sale = Db::getInstance()->getRow("
SELECT ps.`id_sale`, cl.`name` as `category_name`
FROM `" . _DB_PREFIX_ . "product_ps_cache` ps
LEFT JOIN `" . _DB_PREFIX_ . "privatesale` s ON (s.`id_sale` = ps.`id_sale`)
LEFT JOIN `" . _DB_PREFIX_ . "category_lang` cl ON (cl.`id_category` = s.`id_category`)
WHERE ps.`id_product` = " . (int)$product['product_id']."
AND cl.`id_lang` = 2"
);
if($with_product){
return array_merge($sale, $product);
}
return $sale;
}
}