* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 9565 $
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/**
* @since 1.5.0
*/
class AdminSupplyOrdersControllerCore extends AdminController
{
public function __construct()
{
$this->context = Context::getContext();
$this->table = 'supply_order_state';
$this->className = 'SupplyOrderState';
$this->lang = true;
$this->fieldsDisplay = array(
'name' => array(
'title' => $this->l('Name'),
'color' => 'color',
),
'editable' => array(
'title' => $this->l('Editable?'),
'align' => 'center',
'icon' => array(
'1' => 'enabled.gif',
'0' => 'disabled.gif'
),
'type' => 'bool',
'width' => 170,
'orderby' => false
),
'delivery_note' => array(
'title' => $this->l('Delivery note available?'),
'align' => 'center',
'icon' => array(
'1' => 'enabled.gif',
'0' => 'disabled.gif'
),
'type' => 'bool',
'width' => 170,
'orderby' => false
),
'pending_receipt' => array(
'title' => $this->l('Is a pending receipt state?'),
'align' => 'center',
'icon' => array(
'1' => 'enabled.gif',
'0' => 'disabled.gif'
),
'type' => 'bool',
'width' => 170,
'orderby' => false
),
'receipt_state' => array(
'title' => $this->l('Is a delivery state?'),
'align' => 'center',
'icon' => array(
'1' => 'enabled.gif',
'0' => 'disabled.gif'
),
'type' => 'bool',
'width' => 170,
'orderby' => false
),
'enclosed' => array(
'title' => $this->l('Is an enclosed order state?'),
'align' => 'center',
'icon' => array(
'1' => 'enabled.gif',
'0' => 'disabled.gif'
),
'type' => 'bool',
'width' => 170,
'orderby' => false
),
);
parent::__construct();
}
/**
* AdminController::init() override
* @see AdminController::init()
*/
public function init()
{
parent::init();
if (Tools::isSubmit('addsupply_order') ||
Tools::isSubmit('submitAddsupply_order') ||
(Tools::isSubmit('updatesupply_order') && Tools::isSubmit('id_supply_order')))
{
// override table, lang, className and identifier for the current controller
$this->table = 'supply_order';
$this->className = 'SupplyOrder';
$this->identifier = 'id_supply_order';
$this->lang = false;
$this->action = 'new';
$this->display = 'add';
if (Tools::isSubmit('updatesupply_order'))
if ($this->tabAccess['edit'] === '1')
$this->display = 'edit';
else
$this->_errors[] = Tools::displayError($this->l('You do not have permission to edit here.'));
}
if (Tools::isSubmit('update_receipt') && Tools::isSubmit('id_supply_order'))
{
// change the display type in order to add specific actions to
$this->display = 'update_receipt';
// display correct toolBar
$this->initToolbar();
}
}
/**
* AdminController::initForm() override
* @see AdminController::initForm()
*/
public function initForm()
{
if (Tools::isSubmit('addsupply_order_state') ||
Tools::isSubmit('updatesupply_order_state') ||
Tools::isSubmit('submitAddsupply_order_state') ||
Tools::isSubmit('submitUpdatesupply_order_state'))
{
$this->fields_form = array(
'legend' => array(
'title' => $this->l('Supply Order State'),
'image' => '../img/admin/edit.gif'
),
'input' => array(
array(
'type' => 'text',
'lang' => true,
'label' => $this->l('Name:'),
'name' => 'name',
'size' => 50,
'required' => true
),
array(
'type' => 'color',
'label' => $this->l('Back office color:'),
'name' => 'color',
'size' => 20,
'desc' => $this->l('Back office background will be displayed in this color. HTML colors only (e.g.,').' "lightblue", "#CC6600")'
),
array(
'type' => 'radio',
'label' => $this->l('Is the order editable in this state?:'),
'name' => 'editable',
'required' => true,
'class' => 't',
'is_bool' => true,
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Yes')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('No')
)
),
'desc' => $this->l('You have to define if it is possible to edit the order in this state.
An editable order is an order not valid to send to the supplier.')
),
array(
'type' => 'radio',
'label' => $this->l('Is the delivery note function is available in this state?:'),
'name' => 'delivery_note',
'required' => true,
'class' => 't',
'is_bool' => true,
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Yes')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('No')
)
),
'desc' => $this->l('You have to define if it is possible to generate the delivery note of the order in this state.
The order has to be valid to use this function.')
),
array(
'type' => 'radio',
'label' => $this->l('This state corresponds to a delivery state ?:'),
'name' => 'receipt_state',
'required' => true,
'class' => 't',
'is_bool' => true,
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Yes')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('No')
)
),
'desc' => $this->l('You have to define if this state correspond to a product receipt on this order (partial or complete).
This permit to know if the concerned products have to be added in stock.')
),
array(
'type' => 'radio',
'label' => $this->l('This state corresponds to a product pending receipt ?:'),
'name' => 'pending_receipt',
'required' => true,
'class' => 't',
'is_bool' => true,
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Yes')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('No')
)
),
'desc' => $this->l('You have to define if some products are pending receipt in this state.')
),
),
'submit' => array(
'title' => $this->l(' Save '),
'class' => 'button'
)
);
if (Tools::isSubmit('addsupply_order_state'))
$this->toolbar_title = $this->l('Stock : Add Supply order state');
else
{
$this->toolbar_title = $this->l('Stock : Update Supply order state');
$id_supply_order_state = Tools::getValue('id_supply_order_state', 0);
// only some fields are editable for initial states
if (in_array($id_supply_order_state, array(1, 2, 3, 4, 5, 6)))
{
$this->fields_form = array(
'legend' => array(
'title' => $this->l('Supply Order State'),
'image' => '../img/admin/edit.gif'
),
'input' => array(
array(
'type' => 'text',
'lang' => true,
'label' => $this->l('Name:'),
'name' => 'name',
'size' => 50,
'required' => true
),
array(
'type' => 'color',
'label' => $this->l('Back office color:'),
'name' => 'color',
'size' => 20,
'desc' => $this->l('Back office background will be displayed in this color. HTML colors only (e.g.,').' "lightblue", "#CC6600")'
),
),
'submit' => array(
'title' => $this->l(' Save '),
'class' => 'button'
)
);
}
}
return parent::initForm();
}
if (Tools::isSubmit('addsupply_order') ||
Tools::isSubmit('updatesupply_order') ||
Tools::isSubmit('submitAddsupply_order') ||
Tools::isSubmit('submitUpdatesupply_order'))
{
if (Tools::isSubmit('addsupply_order') || Tools::isSubmit('submitAddsupply_order'))
$this->toolbar_title = $this->l('Stock : Create new supply order');
if (Tools::isSubmit('updatesupply_order') || Tools::isSubmit('submitUpdatesupply_order'))
$this->toolbar_title = $this->l('Stock : Manage supply order');
$this->addJqueryUI('ui.datepicker');
//get warehouses list
$warehouses = Warehouse::getWarehouses(true);
//get currencies list
$currencies = Currency::getCurrencies();
//get suppliers list
$suppliers = Supplier::getSuppliers();
//get languages list
$languages = Language::getLanguages(true);
$this->fields_form = array(
'legend' => array(
'title' => $this->l('Order informations'),
'image' => '../img/admin/edit.gif'
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Reference:'),
'name' => 'reference',
'size' => 50,
'required' => true,
'desc' => $this->l('This is the reference of your order.'),
),
array(
'type' => 'select',
'label' => $this->l('Supplier:'),
'name' => 'id_supplier',
'required' => true,
'options' => array(
'query' => $suppliers,
'id' => 'id_supplier',
'name' => 'name'
),
'desc' => $this->l('Select the supplier you are buying product from'),
'hint' => $this->l('Be careful ! When changing this field, all products already added to the order will be removed.')
),
array(
'type' => 'select',
'label' => $this->l('Warehouse:'),
'name' => 'id_warehouse',
'required' => true,
'options' => array(
'query' => $warehouses,
'id' => 'id_warehouse',
'name' => 'name'
),
'desc' => $this->l('Select the warehouse where you want the order to be sent to.'),
),
array(
'type' => 'select',
'label' => $this->l('Currency:'),
'name' => 'id_currency',
'required' => true,
'options' => array(
'query' => $currencies,
'id' => 'id_currency',
'name' => 'name'
),
'desc' => $this->l('The currency of the order'),
'hint' => $this->l('Be careful ! When changing this field, all products already added to the order will be removed.')
),
array(
'type' => 'select',
'label' => $this->l('Order Language:'),
'name' => 'id_lang',
'required' => true,
'options' => array(
'query' => $languages,
'id' => 'id_lang',
'name' => 'name'
),
'desc' => $this->l('The language of the order')
),
array(
'type' => 'date',
'label' => $this->l('Delivery date:'),
'name' => 'date_delivery_expected',
'size' => 20,
'required' => true,
'desc' => $this->l('This is the expected delivery date for this order'),
),
array(
'type' => 'text',
'label' => $this->l('Global discount rate (%):'),
'name' => 'discount_rate',
'size' => 7,
'required' => true,
'desc' => $this->l('This is the global discount rate in percents for the order.'),
),
),
'submit' => array(
'title' => $this->l(' Save order '),
)
);
//specific discount display
$this->object->discount_rate = Tools::ps_round($this->object->discount_rate, 4);
return parent::initForm();
}
}
/**
* AdminController::getList() override
* @see AdminController::getList()
*/
public function getList($id_lang, $order_by = null, $order_way = null, $start = 0, $limit = null, $id_lang_shop = false)
{
parent::getList($id_lang, $order_by, $order_way, $start, $limit, $id_lang_shop);
// actions filters on supply orders list
if ($this->table == 'supply_order')
{
$nb_items = count($this->_list);
for ($i = 0; $i < $nb_items; $i++)
{
// if the current state doesn't allow order edit, skip the edit action
if ($this->_list[$i]['editable'] == 0)
$this->addRowActionSkipList('edit', $this->_list[$i]['id_supply_order']);
if ($this->_list[$i]['enclosed'] == 1)
$this->addRowActionSkipList('changestate', $this->_list[$i]['id_supply_order']);
if (1 != $this->_list[$i]['pending_receipt'])
$this->addRowActionSkipList('updatereceipt', $this->_list[$i]['id_supply_order']);
}
}
}
/**
* AdminController::initList() override
* @see AdminController::initList()
*/
public function initList()
{
$this->displayInformation($this->l('This interface allows you to manage supply orders.').'
');
$this->displayInformation($this->l('Also, it allows you to add and edit your own supply order states.'));
// access
if (!($this->tabAccess['add'] === '1'))
unset($this->toolbar_btn['new']);
//no link on list rows
$this->list_no_link = true;
/*
* Manage default list
*/
$this->addRowAction('edit');
$this->addRowAction('delete');
$this->addRowActionSkipList('delete', array(1, 2, 3, 4, 5, 6));
$this->toolbar_title = $this->l('Stock : Suppliers Orders States');
$first_list = parent::initList();
/*
* Manage second list
*/
// reset actions, toolbar and query vars
$this->actions = array();
$this->list_skip_actions = array();
$this->toolbar_btn = array();
$this->toolbar_title = '';
unset($this->_select, $this->_join, $this->_group, $this->_filterHaving, $this->_filter);
// override table, land, className and identifier for the current controller
$this->table = 'supply_order';
$this->className = 'SupplyOrder';
$this->identifier = 'id_supply_order';
$this->lang = false;
$this->addRowAction('updatereceipt');
$this->addRowAction('changestate');
$this->addRowAction('edit');
$this->addRowAction('view');
$this->addRowAction('details');
// test if a filter is applied for this list
if (Tools::isSubmit('submitFilter'.$this->table) || $this->context->cookie->{'submitFilter'.$this->table} !== false)
$this->filter = true;
// test if a filter reset request is required for this list
if (isset($_POST['submitReset'.$this->table]))
$this->action = 'reset_filters';
else
$this->action = '';
// redifine fields display
$this->fieldsDisplay = array(
'reference' => array(
'title' => $this->l('Order Reference'),
'width' => 100,
'havingFilter' => true
),
'supplier' => array(
'title' => $this->l('Supplier'),
'width' => 100,
'filter_key' => 's!name'
),
'warehouse' => array(
'title' => $this->l('Warehouse'),
'width' => 100,
'filter_key' => 'w!name'
),
'state' => array(
'title' => $this->l('State'),
'width' => 200,
'filter_key' => 'stl!name',
'color' => 'color',
),
'date_add' => array(
'title' => $this->l('Creation date'),
'width' => 150,
'align' => 'right',
'type' => 'datetime',
'havingFilter' => true,
'filter_key' => 'a!date_add'
),
'date_upd' => array(
'title' => $this->l('Last modification date'),
'width' => 150,
'align' => 'right',
'type' => 'datetime',
'havingFilter' => true,
'filter_key' => 'a!date_upd'
),
'date_delivery_expected' => array(
'title' => $this->l('Delivery date'),
'width' => 150,
'align' => 'right',
'type' => 'datetime',
'havingFilter' => true,
'filter_key' => 'a!date_delivery_expected'
),
'id_pdf' => array(
'title' => $this->l('PDF'),
'width' => 50,
'callback' => 'printPDFIcons',
'orderby' => false,
'search' => false
),
);
// make new query
$this->_select = '
s.name AS supplier,
w.name AS warehouse,
stl.name AS state,
st.delivery_note,
st.editable,
st.enclosed,
st.receipt_state,
st.pending_receipt,
st.color AS color,
a.id_supply_order as id_pdf';
$this->_join = 'LEFT JOIN `'._DB_PREFIX_.'supply_order_state_lang` stl ON
(
a.id_supply_order_state = stl.id_supply_order_state
AND stl.id_lang = '.(int)$this->context->language->id.'
)
LEFT JOIN `'._DB_PREFIX_.'supply_order_state` st ON a.id_supply_order_state = st.id_supply_order_state
LEFT JOIN `'._DB_PREFIX_.'supplier` s ON a.id_supplier = s.id_supplier
LEFT JOIN `'._DB_PREFIX_.'warehouse` w ON (w.id_warehouse = a.id_warehouse)';
// init the toolbar according to the current list
$this->initToolbar();
// generate the second list
$second_list = parent::initList();
// reset all query vars
unset($this->_select, $this->_join, $this->_group, $this->_filterHaving, $this->_filter);
// return the two lists
return $second_list.$first_list;
}
/**
* Init the content of change state action
*/
public function initChangeStateContent()
{
$id_supply_order = (int)Tools::getValue('id_supply_order', 0);
if ($id_supply_order <= 0)
{
$this->_errors[] = Tools::displayError($this->l('The specified supply order is not valid'));
return parent::initContent();
}
$supply_order = new SupplyOrder($id_supply_order);
if (!Validate::isLoadedObject($supply_order))
{
$this->_errors[] = Tools::displayError($this->l('The specified supply order is not valid'));
return parent::initContent();
}
// change the display type in order to add specific actions to
$this->display = 'update_order_state';
// overrides parent::initContent();
$this->initToolbar();
// given the current state, loads available states
$states = SupplyOrderState::getSupplyOrderStates($supply_order->id_supply_order_state);
// loads languages
$this->getlanguages();
// defines the fields of the form to display
$this->fields_form[]['form'] = array(
'legend' => array(
'title' => $this->l('Supply Order State'),
'image' => '../img/admin/cms.gif'
),
'input' => array(
array(
'type' => 'hidden',
'name' => 'id_supply_order',
),
array(
'type' => 'select',
'label' => $this->l('New state of the order:'),
'name' => 'id_supply_order_state',
'required' => true,
'options' => array(
'query' => $states,
'id' => 'id_supply_order_state',
'name' => 'name'
),
'desc' => $this->l('Choose the new state of your order')
),
),
'submit' => array(
'title' => $this->l(' Save '),
'class' => 'button'
)
);
// sets up the helper
$helper = new HelperForm();
$helper->submit_action = 'submitChangestate';
$helper->currentIndex = self::$currentIndex;
$helper->toolbar_btn = $this->toolbar_btn;
$helper->toolbar_fix = false;
$helper->token = $this->token;
$helper->id = null; // no display standard hidden field in the form
$helper->languages = $this->_languages;
$helper->default_form_language = $this->default_form_language;
$helper->allow_employee_form_lang = $this->allow_employee_form_lang;
$helper->fields_value = array(
'id_supply_order_state' => Tools::getValue('id_supplier', ''),
'id_supply_order' => $id_supply_order,
);
// generates the form to display
$this->content = $helper->generateForm($this->fields_form);
// assigns our content
$this->tpl_form_vars['show_change_state_form'] = true;
$this->tpl_form_vars['state_content'] = $this->content;
$this->context->smarty->assign(array(
'content' => $this->content,
'url_post' => self::$currentIndex.'&token='.$this->token,
));
}
/**
* Init the content of change state action
*/
public function initUpdateSupplyOrderContent()
{
$this->addJqueryPlugin('autocomplete');
// load supply order
$id_supply_order = (int)Tools::getValue('id_supply_order', null);
if ($id_supply_order != null)
{
$supply_order = new SupplyOrder($id_supply_order);
$currency = new Currency($supply_order->id_currency);
if (Validate::isLoadedObject($supply_order))
{
// load products of this order
$products = $supply_order->getEntries();
$product_ids = array();
if (isset($this->order_products_errors) && is_array($this->order_products_errors))
{
//for each product in error array, check if it is in products array, and remove it to conserve last user values
foreach ($this->order_products_errors as $pe)
foreach ($products as $index_p => $p)
if (($p['id_product'] == $pe['id_product']) && ($p['id_product_attribute'] == $pe['id_product_attribute']))
unset($products[$index_p]);
// then merge arrays
$products = array_merge($this->order_products_errors, $products);
}
foreach ($products as &$item)
{
// calculate md5 checksum on each product for use in tpl
$item['checksum'] = md5(_COOKIE_KEY_.$item['id_product'].'_'.$item['id_product_attribute']);
$item['unit_price_te'] = Tools::ps_round($item['unit_price_te'], 2);
// add id to ids list
$product_ids[] = $item['id_product'].'_'.$item['id_product_attribute'];
}
$this->tpl_form_vars['products_list'] = $products;
$this->tpl_form_vars['product_ids'] = implode($product_ids, '|');
$this->tpl_form_vars['product_ids_to_delete'] = '';
$this->tpl_form_vars['supplier_id'] = $supply_order->id_supplier;
$this->tpl_form_vars['currency'] = $currency;
}
}
$this->tpl_form_vars['content'] = $this->content;
$this->tpl_form_vars['token'] = $this->token;
$this->tpl_form_vars['show_product_management_form'] = true;
// call parent initcontent to render standard form content
parent::initContent();
}
/**
* Inits the content of 'update_receipt' action
* Called in initContent()
* @see AdminSuppliersOrders::initContent()
*/
public function initUpdateReceiptContent()
{
$id_supply_order = (int)Tools::getValue('id_supply_order', null);
// if there is no order to fetch
if (null == $id_supply_order)
return parent::initContent();
$supply_order = new SupplyOrder($id_supply_order);
// if it's not a valid order
if (!Validate::isLoadedObject($supply_order))
return parent::initContent();
// re-defines fieldsDisplay
$this->fieldsDisplay = array(
'reference' => array(
'title' => $this->l('Reference'),
'align' => 'center',
'width' => 30,
'orderby' => false,
'filter' => false,
'search' => false,
),
'ean13' => array(
'title' => $this->l('EAN13'),
'align' => 'center',
'width' => 30,
'orderby' => false,
'filter' => false,
'search' => false,
),
'upc' => array(
'title' => $this->l('UPC'),
'align' => 'center',
'width' => 30,
'orderby' => false,
'filter' => false,
'search' => false,
),
'name' => array(
'title' => $this->l('Name'),
'align' => 'center',
'width' => 350,
'orderby' => false,
'filter' => false,
'search' => false,
),
'quantity_received_today' => array(
'title' => $this->l('Quantity received today'),
'align' => 'center',
'width' => 20,
'type' => 'editable',
'orderby' => false,
'filter' => false,
'search' => false,
),
'quantity_received' => array(
'title' => $this->l('Quantity received'),
'align' => 'center',
'width' => 20,
'orderby' => false,
'filter' => false,
'search' => false,
),
'quantity_expected' => array(
'title' => $this->l('Quantity expected'),
'align' => 'center',
'width' => 20,
'orderby' => false,
'filter' => false,
'search' => false,
),
'quantity_left' => array(
'title' => $this->l('Quantity left to receive'),
'align' => 'center',
'width' => 20,
'orderby' => false,
'filter' => false,
'search' => false,
)
);
// attributes override
unset($this->_select, $this->_join, $this->_where, $this->_orderBy, $this->_orderWay, $this->_group, $this->_filterHaving, $this->_filter);
$this->table = 'supply_order_detail';
$this->identifier = 'id_supply_order_detail';
$this->className = 'SupplyOrderDetail';
$this->list_simple_header = true;
$this->list_no_link = true;
$this->bulk_actions = array('Update' => array('text' => $this->l('Update selected'), 'confirm' => $this->l('Update selected items?')));
$this->addRowAction('details');
// sets toolbar title with order reference
$this->toolbar_title = sprintf($this->l('Reception of products for supply order #%s'), $supply_order->reference);
$this->lang = false;
$lang_id = (int)$this->context->language->id; //employee lang
// gets values corresponding to fieldsDisplay
$this->_select = '
a.id_supply_order_detail as id,
a.quantity_received as quantity_received,
a.quantity_expected as quantity_expected,
IF (a.quantity_expected < a.quantity_received, 0, a.quantity_expected - a.quantity_received) as quantity_left,
IF (a.quantity_expected < a.quantity_received, 0, a.quantity_expected - a.quantity_received) as quantity_received_today';
$this->_where = 'AND a.`id_supply_order` = '.(int)$id_supply_order;
$this->_group = 'GROUP BY a.id_supply_order_detail';
// gets the list ordered by price desc, without limit
$this->getList($lang_id, 'quantity_expected', 'DESC', 0, false, false);
// defines action for POST
$action = '&id_supply_order='.$id_supply_order;
// renders list
$helper = new HelperList();
$this->setHelperDisplay($helper);
$helper->override_folder = 'supply_orders_receipt_history/';
$helper->currentIndex = self::$currentIndex.$action;
// display these global order informations
$this->displayInformation($this->l('This interface allows you to update the quantities of this on-going order.').'
');
$this->displayInformation($this->l('Be careful : once you update, you cannot go back unless you add new negative stock movements.').'
');
// generates content
$content = $helper->generateList($this->_list, $this->fieldsDisplay);
// assigns var
$this->context->smarty->assign(array(
'content' => $content,
));
}
/**
* AdminController::initContent() override
* @see AdminController::initContent()
*/
public function initContent()
{
// Manage the add stock form
if (Tools::isSubmit('changestate'))
$this->initChangeStateContent();
else if (Tools::isSubmit('update_receipt') && Tools::isSubmit('id_supply_order'))
$this->initUpdateReceiptContent();
else if (Tools::isSubmit('viewsupply_order') && Tools::isSubmit('id_supply_order'))
{
$this->action = 'view';
$this->display = 'view';
parent::initContent();
}
else if (Tools::isSubmit('updatesupply_order'))
$this->initUpdateSupplyOrderContent();
else
parent::initContent();
}
/**
* Ths method manage associated products to the order when updating it
*/
public function manageOrderProducts()
{
// load supply order
$id_supply_order = (int)Tools::getValue('id_supply_order', null);
$products_already_in_order = array();
if ($id_supply_order != null)
{
$supply_order = new SupplyOrder($id_supply_order);
if (Validate::isLoadedObject($supply_order))
{
// tests if the supplier or currency have changed in the supply order
$new_supplier_id = (int)Tools::getValue('id_supplier');
$new_currency_id = (int)Tools::getValue('id_currency');
if (($new_supplier_id != $supply_order->id_supplier) ||
($new_currency_id != $supply_order->id_currency))
{
// resets all products in this order
$supply_order->resetProducts();
}
else
{
$products_already_in_order = $supply_order->getEntries();
$currency = new Currency($supply_order->id_ref_currency);
// gets all product ids to manage
$product_ids_str = Tools::getValue('product_ids', null);
$product_ids = explode('|', $product_ids_str);
$product_ids_to_delete_str = Tools::getValue('product_ids_to_delete', null);
$product_ids_to_delete = array_unique(explode('|', $product_ids_to_delete_str));
//delete products that are not managed anymore
foreach ($products_already_in_order as $paio)
{
$product_ok = false;
foreach ($product_ids_to_delete as $id)
{
$id_check = $paio['id_product'].'_'.$paio['id_product_attribute'];
if ($id_check == $id)
$product_ok = true;
}
if ($product_ok === true)
{
$entry = new SupplyOrderDetail($paio['id_supply_order_detail']);
$entry->delete();
}
}
// manage each product
foreach ($product_ids as $id)
{
$errors = array();
// check if a checksum is available for this product and test it
$check = Tools::getValue('input_check_'.$id, '');
$check_valid = md5(_COOKIE_KEY_.$id);
if ($check_valid != $check)
continue;
$pos = strpos($id, '_');
if ($pos === false)
continue;
// Load / Create supply order detail
$entry = new SupplyOrderDetail();
$id_supply_order_detail = (int)Tools::getValue('input_id_'.$id, 0);
if ($id_supply_order_detail > 0)
{
$existing_entry = new SupplyOrderDetail($id_supply_order_detail);
if (Validate::isLoadedObject($supply_order))
$entry = &$existing_entry;
}
// get product informations
$entry->id_product = substr($id, 0, $pos);
$entry->id_product_attribute = substr($id, $pos + 1);
$entry->unit_price_te = (float)str_replace(array(' ', ','), array('', '.'), Tools::getValue('input_unit_price_te_'.$id, 0));
$entry->quantity_expected = (int)str_replace(array(' ', ','), array('', '.'), Tools::getValue('input_quantity_expected_'.$id, 0));
$entry->discount_rate = (float)str_replace(array(' ', ','), array('', '.'), Tools::getValue('input_discount_rate_'.$id, 0));
$entry->tax_rate = (float)str_replace(array(' ', ','), array('', '.'), Tools::getValue('input_tax_rate_'.$id, 0));
$entry->reference = Tools::getValue('input_reference_'.$id, '');
$entry->ean13 = Tools::getValue('input_ean13_'.$id, '');
$entry->upc = Tools::getValue('input_upc_'.$id, '');
//get the product name in the order language
$entry->name = Product::getProductName($entry->id_product, $entry->id_product_attribute, $supply_order->id_lang);
if (empty($entry->name))
$entry->name = '';
//get the product supplier reference
$entry->supplier_reference = ProductSupplier::getProductSupplierReference($entry->id_product, $entry->id_product_attribute, $supply_order->id_supplier);
if ($entry->supplier_reference == null)
$entry->supplier_reference = '';
$entry->exchange_rate = $currency->conversion_rate;
$entry->id_currency = $currency->id;
$entry->id_supply_order = $supply_order->id;
$errors = $entry->validateController();
//get the product name displayed in the backoffice according to the employee language
$entry->name_displayed = Tools::getValue('input_name_'.$id, '');
// if there is a problem, handle error for the current product
if (count($errors) > 0)
{
// add the product to error array => display again product line
$this->order_products_errors[] = array(
'id_product' => $entry->id_product,
'id_product_attribute' => $entry->id_product_attribute,
'unit_price_te' => $entry->unit_price_te,
'quantity_expected' => $entry->quantity_expected,
'discount_rate' => $entry->discount_rate,
'tax_rate' => $entry->tax_rate,
'name' => $entry->name,
'name_displayed' => $entry->name_displayed,
'reference' => $entry->reference,
'ean13' => $entry->ean13,
'upc' => $entry->upc,
);
$error_str = '
';
else
$content .= '-';
$content .= '';
return $content;
}
/**
* Assigns default actions in toolbar_btn smarty var, if they are not set.
* uses override to specifically add, modify or remove items
* @see AdminSupplier::initToolbar()
*/
public function initToolbar()
{
switch ($this->display)
{
case 'update_order_state':
$this->toolbar_btn['save'] = array(
'href' => '#',
'desc' => $this->l('Save')
);
case 'update_receipt':
// Default cancel button - like old back link
if (!isset($this->no_back) || $this->no_back == false)
{
$back = Tools::safeOutput(Tools::getValue('back', ''));
if (empty($back))
$back = self::$currentIndex.'&token='.$this->token;
$this->toolbar_btn['cancel'] = array(
'href' => $back,
'desc' => $this->l('Cancel')
);
}
break;
case 'add':
case 'edit':
$this->toolbar_btn['save-and-stay'] = array(
'href' => '#',
'desc' => $this->l('Save and stay')
);
default:
parent::initToolbar();
}
}
}