bebeboutik/modules/bulkstatus/bulkstatus.php
Srv Bebeboutik 6c0978166c add modules
2016-01-04 12:49:26 +01:00

359 lines
12 KiB
PHP

<?php
class BulkStatus extends Module
{
public function __construct()
{
$this->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 = '
<style type="text/css">
@media print {
#menu, #submenu, .path_bar, legend, #content > .table, #header_search, #header_quick, #header_infos, #footer {
display: none;
}
@page {
margin: 0;
padding: 0;
}
body, html {
margin: 0;
padding: 0;
}
form, fieldset, table, tr, tbody {
page-break-inside: auto !important;
page-break-after: auto !important;
height: auto;
}
thead {
display: table-header-group !important;
}
fieldset, form, #content, #main {
padding: 0px !important;
margin: 0px !important;
border: 0px !important;
}
form > label, form > input, form > br, #content > br, h2, .flatclear {
display: none !important;
}
table.table tr td:first-child, table.table tr th:first-child {
display: none;
}
#content > fieldset:first-of-type {
display: none;
}
}
</style>
<script type="text/javascript">
/*
* Copyright (c) 2008 Greg Weber greg at gregweber.info
* Dual licensed under the MIT and GPLv2 licenses just as jQuery is:
* http://jquery.org/license
*
* documentation at http://gregweber.info/projects/uitablefilter
*
* allows table rows to be filtered (made invisible)
* <code>
* t = $(\'table\')
* $.uiTableFilter( t, phrase )
* </code>
* arguments:
* jQuery object containing table rows
* phrase to search for
* optional arguments:
* column to limit search too (the column title in the table header)
* ifHidden - callback to execute if one or more elements was hidden
*/
(function($) {
$.uiTableFilter = function(jq, phrase, column, ifHidden){
var new_hidden = false;
if( this.last_phrase === phrase ) return false;
var phrase_length = phrase.length;
var words = phrase.toLowerCase().split(" ");
// these function pointers may change
var matches = function(elem) { elem.show() }
var noMatch = function(elem) { elem.hide(); new_hidden = true }
var getText = function(elem) { return elem.text() }
if( column ) {
var index = null;
jq.find("thead > tr:last > th").each( function(i){
if( $.trim($(this).text()) == column ){
index = i; return false;
}
});
if( index == null ) throw("given column: " + column + " not found")
getText = function(elem){ return $(elem.find(
("td:eq(" + index + ")") )).text()
}
}
// if added one letter to last time,
// just check newest word and only need to hide
if( (words.size > 1) && (phrase.substr(0, phrase_length - 1) ===
this.last_phrase) ) {
if( phrase[-1] === " " )
{ this.last_phrase = phrase; return false; }
var words = words[-1]; // just search for the newest word
// only hide visible rows
matches = function(elem) {;}
var elems = jq.find("tbody:first > tr:visible")
}
else {
new_hidden = true;
var elems = jq.find("tbody:first > tr")
}
elems.each(function(){
var elem = $(this);
$.uiTableFilter.has_words( getText(elem), words, false ) ?
matches(elem) : noMatch(elem);
});
last_phrase = phrase;
if( ifHidden && new_hidden ) ifHidden();
return jq;
};
// caching for speedup
$.uiTableFilter.last_phrase = ""
// not jQuery dependent
// "" [""] -> Boolean
// "" [""] Boolean -> Boolean
$.uiTableFilter.has_words = function( str, words, caseSensitive )
{
var text = caseSensitive ? str : str.toLowerCase();
for (var i=0; i < words.length; i++) {
if (text.indexOf(words[i]) === -1) return false;
}
return true;
}
}) (jQuery);
</script>
<h2>'.$this->displayName.'</h2>';
$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 .= '<br class="clear" /><fieldset><legend>'.$this->l('Orders').'</legend><form method="post" action="'.$_SERVER['REQUEST_URI'].'" enctype="multipart/form-data">
<label>'.$this->l('Filter by ID:').'</label><input type="text" onkeyup="$.uiTableFilter($(this).parent().children(\'table\'), $(this).attr(\'value\'), \''.$this->l('ID').'\');" />
<br class="clear" /><br class="clear" />
<table class="table">
<thead>
<tr>
<th><input type="checkbox" onclick="if($(this).attr(\'checked\')) {$(this).parent().parent().parent().parent().children(\'tbody\').children(\'tr\').children(\'td\').children(\'input\').attr(\'checked\', \'checked\');} else {$(this).parent().parent().parent().parent().children(\'tbody\').children(\'tr\').children(\'td\').children(\'input\').attr(\'checked\', \'\');}" /></th>
<th>'.$this->l('ID').'</th>
<th>'.$this->l('ID Customer').'</th>
<th>'.$this->l('Customer').'</th>
<th>'.$this->l('Carrier').'</th>
<th>'.$this->l('Date').'</th>
<th>'.$this->l('Total').'</th>';
if($expeditor) {
$this->_html .= '<th>'.$this->l('Package weight (Expeditor INET)').'</th>';
}
$this->_html .= '
</tr></thead><tbody>';
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);
$weight = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
SELECT SUM(product_weight * product_quantity) AS `weight`
FROM `'._DB_PREFIX_.'order_detail`
WHERE `id_order` = '.(int) $order_id.'
');
$this->_html .= '<tr>
<td><input type="checkbox" value="'.$order_id.'" name="order[]" /></td>
<td>'.$order_id.'</td>
<td>'.$order->id_customer.'</td>
<td>'.$customer->lastname.' '.$customer->firstname.'</td>
<td>'.$carrier->name.'</td>
<td>'.$order->date_add.'</td>
<td>'.$order->total_paid_real.'</td>';
if($expeditor) {
$this->_html .= '<td><input type="text" name="weight_'.$order_id.'" value="'.(Tools::ps_round((float)($weight['weight']), 2) * 1000).'" /></td>';
}
$this->_html .= '</tr>';
}
$this->_html .= '</tbody></table><br class="clear" />';
$this->_html .= '<label>'.$this->l('Set a new status:').'</label>
<select name="orderState" style="float: left;">';
$order_states = OrderState::getOrderStates(intval($cookie->id_lang));
foreach($order_states as $state) {
$this->_html .= '<option value="'.$state['id_order_state'].'"'.($state['id_order_state'] == 3?' selected="selected"':'').'>'.$state['name'].'</option>';
}
$this->_html .= '</select> <input type="checkbox" style="margin-left: 20px; margin-right: 10px; float: left;" id="noMail" name="noMail" value="1" /> <label style="width: auto; margin: 0px 20px 0px 0px;" for="noMail">'.$this->l('Don\'t send any email').'</label> <input class="button" type="submit" name="submitStatus" value="'.$this->l('Submit').'" />';
$this->_html .= '</form></fieldset><br class="clear" />';
} 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 .= '<br class="clear" /><fieldset><legend>'.$this->l('Export to CSV').'</legend>
<label>'.$this->l('CSV File:').'</label> <a href="/modules/bulkstatus/'.$fname.'" onclick="window.open(this.href); return false;" style="padding-top: 3px; float: left;">'.$this->l('Click here to download the file').'</a></fieldset>
<br class="clear" /><br class="clear" />';
}
} 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 .= '<br class="clear" />'.$this->displayConfirmation($this->l('Status updated successfully'));
}
return $this->_html;
}
private function _displayForm()
{
global $cookie;
$this->_html .= '<fieldset>
<legend>'.$this->l('Filter orders').'</legend>
<form method="post" action="'.$_SERVER['REQUEST_URI'].'" enctype="multipart/form-data">
<p>
<label>'.$this->l('From:').'</label>
<input type="text" name="date_from" value="" />
</p>
<p>
<label>'.$this->l('To:').'</label>
<input type="text" name="date_to" value="" />
</p>
<p>
<label>'.$this->l('Filter by status:').'</label>
<select name="filterState[]" multiple="multiple" style="height: 200px;">';
$order_states = OrderState::getOrderStates(intval($cookie->id_lang));
foreach($order_states as $state) {
$this->_html .= '<option value="'.$state['id_order_state'].'"'.($state['id_order_state'] == 2?' selected="selected"':'').'>'.$state['name'].'</option>';
}
$this->_html .= '</select>
</p>
<p class="center">
<input class="button" type="submit" name="submitFilter" value="'.$this->l('Display orders').'" />
<input class="button" type="submit" name="submitExport" value="'.$this->l('Export to CSV').'" />
</p>';
$this->_html .='</form></fieldset>';
}
}