name = 'bulkstatus';
$this->tab = 'administration';
$this->version = '1.0';
parent::__construct();
$this->displayName = $this->l('Bulk status edition');
$this->description = $this->l('Mass status changes');
$this->_html = '';
}
public function install()
{
if (!parent::install())
return false;
return true;
}
function uninstall()
{
if (parent::uninstall() == false)
return false;
return true;
}
function getContent()
{
$expeditor = False;
if(is_dir(dirname(__FILE__).'/../expeditor')) {
$m = Module::getInstanceByName('expeditor');
if($m->active) {
$expeditor = True;
}
}
global $cookie;
$this->_html = '
'.$this->displayName.'
';
$this->_displayForm();
if((Tools::getValue('submitFilter') || Tools::getValue('submitExport')) and (Tools::getValue('filterState') || Tools::getValue('date_from') || Tools::getValue('date_to'))) {
$states = array();
foreach(Tools::getValue('filterState') as $s) {
$states[] = (int) $s;
}
$orders = array();
if(count($states) > 0) {
foreach($states as $state) {
foreach(Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
SELECT id_order
FROM '._DB_PREFIX_.'orders o
WHERE
'.$state.' = (
SELECT id_order_state
FROM '._DB_PREFIX_.'order_history oh
WHERE oh.id_order = o .id_order
ORDER BY date_add DESC, id_order_history DESC
LIMIT 1
)
'.(Tools::getValue('date_from')?' AND date_add >= "'.pSQL(Tools::getValue('date_from')).'"':'').(Tools::getValue('date_to')? ' AND date_add < "'.pSQL(Tools::getValue('date_to')).'"':'').'
ORDER BY invoice_date ASC
') as $o) {
$orders[] = $o['id_order'];
}
}
} else {
foreach(Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
SELECT id_order
FROM '._DB_PREFIX_.'orders o
WHERE '.(Tools::getValue('date_from')?' date_add >= "'.pSQL(Tools::getValue('date_from')).'"':'').(Tools::getValue('date_to')? (Tools::getValue('date_from')?' AND ':'').' date_add < "'.pSQL(Tools::getValue('date_to')).'"':'').'
ORDER BY invoice_date ASC
') as $o) {
$orders[] = $o['id_order'];
}
}
if(Tools::getValue('submitFilter')) {
$this->_html .= '
';
} else {
foreach(glob(dirname(__FILE__).'/*.csv') as $filename) {
unlink($filename);
}
$fname = Tools::passwdGen(10).'.csv';
$f = fopen(dirname(__FILE__).'/'.$fname, 'w');
fwrite($f, 'id_order;id_customer;customer;carrier;date;total'."\n");
foreach($orders as $order_id) {
$order = new Order($order_id);
$customer = new Customer($order->id_customer);
$carrier = new Carrier($order->id_carrier, $cookie->id_lang);
fputcsv($f, array(
$order_id,
$order->id_customer,
$customer->lastname.' '.$customer->firstname,
$carrier->name,
$order->date_add,
$order->total_paid_real,
), ';');
}
fclose($f);
$this->_html .= '
';
}
} elseif (isset($_POST['submitStatus']) and Tools::getValue('orderState') and Tools::getValue('order')) {
$orders = Tools::getValue('order');
foreach($orders as $id_order) {
if(Tools::getValue('noMail')) {
Db::getInstance()->ExecuteS('
INSERT INTO `'._DB_PREFIX_.'order_history`
VALUES (
DEFAULT,
'.(int) $cookie->id_employee.',
'.(int) $id_order.',
'.(int) Tools::getValue('orderState').',
NOW()
)
');
} else {
$history = new OrderHistory();
$history->id_order = (int) $id_order;
$history->changeIdOrderState((int) Tools::getValue('orderState'), (int) $id_order);
$history->addWithemail();
}
if($expeditor) {
$expeditor_state = Configuration::getInt('EXPEDITOR_STATE_EXP');
if(is_array($expeditor_state)) {
$expeditor_state = array_values($expeditor_state);
$expeditor_state = $expeditor_state[0];
}
if($expeditor_state == Tools::getValue('orderState') && isset($_POST['weight_'.$id_order])) {
Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'expeditor` VALUES (DEFAULT, '.$id_order.', '.intval(Tools::getValue('weight_'.$id_order)).', 0, 0, NOW(), NOW())');
}
}
}
$this->_html .= '
'.$this->displayConfirmation($this->l('Status updated successfully'));
}
return $this->_html;
}
private function _displayForm()
{
global $cookie;
$this->_html .= '';
}
}