'erp_order_detail',
'primary' => 'id_erp_order_detail',
'fields' => array(
'id_erp_order' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'id_product' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'id_product_attribute' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'reference' => array('type' => self::TYPE_STRING, 'validate' => 'isReference'),
'supplier_reference' => array('type' => self::TYPE_STRING, 'validate' => 'isReference'),
'name' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true),
'ean13' => array('type' => self::TYPE_STRING, 'validate' => 'isEan13'),
'upc' => array('type' => self::TYPE_STRING, 'validate' => 'isUpc'),
'id_currency' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'exchange_rate' => array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat', 'required' => true),
'unit_price_te' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice', 'required' => true),
'quantity_ordered' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true),
'quantity_received' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
'price_te' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice', 'required' => true),
'discount_rate' => array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat', 'required' => true),
'discount_value_te' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice', 'required' => true),
'price_with_discount_te' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice', 'required' => true),
'tax_rate' => array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat', 'required' => true),
'tax_value' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice', 'required' => true),
'price_ti' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice', 'required' => true),
'tax_value_with_order_discount' => array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat', 'required' => true),
'price_with_order_discount_te' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice', 'required' => true),
),
);
/**
* @see ObjectModel::update()
*/
public function update($null_values = false)
{
$this->calculatePrices();
parent::update($null_values);
}
/**
* @see ObjectModel::add()
*/
public function add($autodate = true, $null_values = false)
{
$this->calculatePrices();
parent::add($autodate, $null_values);
}
/**
* Calculates all prices for this product based on its quantity and unit price
* Applies discount if necessary
* Calculates tax value, function of tax rate
*/
protected function calculatePrices()
{
// calculates entry price
$this->price_te = Tools::ps_round((float)$this->unit_price_te * (int)$this->quantity_ordered, 6);
// calculates entry discount value
if ($this->discount_rate != null && (is_float($this->discount_rate) || is_numeric($this->discount_rate)) && $this->discount_rate > 0) {
$this->discount_value_te = Tools::ps_round((float)$this->price_te * ($this->discount_rate / 100), 6);
}
// calculates entry price with discount
$this->price_with_discount_te = Tools::ps_round($this->price_te - $this->discount_value_te, 6);
// calculates tax value
$this->tax_value = Tools::ps_round($this->price_with_discount_te * ((float)$this->tax_rate / 100), 6);
$this->price_ti = Tools::ps_round($this->price_with_discount_te + $this->tax_value, 6);
// defines default values for order discount fields
$this->tax_value_with_order_discount = Tools::ps_round($this->tax_value, 6);
$this->price_with_order_discount_te = Tools::ps_round($this->price_with_discount_te, 6);
}
/**
* Applies a global order discount rate, for the current product (i.e detail)
* Calls ObjectModel::update()
*
* @param $discount_rate The discount rate in percent (Ex. 5 for 5 percents)
*/
public function applyGlobalDiscount($discount_rate)
{
if ($discount_rate != null && is_numeric($discount_rate) && (float)$discount_rate > 0) {
// calculates new price, with global order discount, tax ecluded
$discount_value = $this->price_with_discount_te - (($this->price_with_discount_te * (float)$discount_rate) / 100);
$this->price_with_order_discount_te = Tools::ps_round($discount_value, 6);
// calculates new tax value, with global order discount
$this->tax_value_with_order_discount = Tools::ps_round($this->price_with_order_discount_te * ((float)$this->tax_rate / 100), 6);
parent::update();
}
}
/**
* @see ObjectModel::validateController()
*
* @param $htmlentities Optional
* @return $errors If any..
*/
public function validateController($htmlentities = true)
{
$errors = array();
/* required fields */
$fields_required = $this->fieldsRequired;
if (isset(self::$fieldsRequiredDatabase[get_class($this)])) {
$fields_required = array_merge(
$this->fieldsRequired,
self::$fieldsRequiredDatabase[get_class($this)]
);
}
foreach ($fields_required as $field) {
if (($value = $this->{$field}) == false && (string)$value != '0') {
if (!$this->id || $field != 'passwd') {
$errors[] = ''.ErpOrderDetail::displayFieldName($field, get_class($this), $htmlentities)
.' '.Tools::displayError('is required.');
}
}
}
/* Checks maximum fields sizes */
foreach ($this->fieldsSize as $field => $max_length) {
if ($value = $this->{$field} && Tools::strlen($value) > $max_length) {
$errors[] = sprintf(
Tools::displayError('%1$s is too long. Maximum length: %2$d'),
ErpOrderDetail::displayFieldName($field, get_class($this), $htmlentities),
$max_length
);
}
}
/* Checks fields validity */
foreach ($this->fieldsValidate as $field => $function) {
if ($value = $this->{$field}) {
if (!Validate::$function($value) && (!empty($value) || in_array($field, $this->fieldsRequired))) {
$errors[] = ''.ErpOrderDetail::displayFieldName($field, get_class($this), $htmlentities).' '.Tools::displayError('is invalid.');
} elseif ($field == 'passwd') {
if ($value = Tools::getValue($field)) {
$this->{$field} = Tools::encrypt($value);
} else {
$this->{$field} = $value;
}
}
}
}
if ($this->quantity_ordered <= 0) {
$errors[] = ''.ErpOrderDetail::displayFieldName('quantity_ordered', get_class($this)).' '.Tools::displayError('is invalid.');
}
if ($this->tax_rate < 0 || $this->tax_rate > 100) {
$errors[] = ''.ErpOrderDetail::displayFieldName('tax_rate', get_class($this)).' '.Tools::displayError('is invalid.');
}
if ($this->discount_rate < 0 || $this->discount_rate > 100) {
$errors[] = ''.ErpOrderDetail::displayFieldName('discount_rate', get_class($this)).' '.Tools::displayError('is invalid.');
}
return $errors;
}
/**
* @see ObjectModel::hydrate()
*/
public function hydrate(array $data, $id_lang = null)
{
$this->id_lang = $id_lang;
if (isset($data[$this->def['primary']])) {
$this->id = $data[$this->def['primary']];
}
foreach ($data as $key => $value) {
if (array_key_exists($key, $this)) {
// formats prices and floats
if ($this->def['fields'][$key]['validate'] == 'isFloat' ||
$this->def['fields'][$key]['validate'] == 'isPrice') {
$value = Tools::ps_round($value, 6);
}
$this->$key = $value;
}
}
}
public static function getProductRealQuantities($id_product, $id_attribute)
{
return Db::getInstance()->getValue('SELECT SUM(od.`product_quantity`-od.`product_quantity_refunded`)
-SUM(od.`product_quantity_in_stock`) as `stock_to_order`
FROM
`'._DB_PREFIX_.'order_detail` od
LEFT JOIN
`'._DB_PREFIX_.'orders` o ON (o.`id_order` = od.`id_order`)
WHERE
od.`product_id` = '.(int)$id_product.'
AND
od.`product_attribute_id` = '.(int)$id_attribute.'
AND
o.`current_state` IN ('.pSQL(Configuration::get('WIC_ERP_NOT_COMPLETE')).')
');
}
}