* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 9146 $
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
require_once(PS_ADMIN_DIR . '/helpers/HelperFormBootstrap.php');
require_once(PS_ADMIN_DIR . '/helpers/HelperList.php');
class AdminInvoices extends AdminTab
{
public $print_sales;
public function __construct()
{
global $cookie;
$this->table = 'invoice';
$this->optionTitle = $this->l('Invoice options');
$this->_fieldsOptions = array(
'PS_INVOICE' => array('title' => $this->l('Enable invoices:'), 'desc' => $this->l('Select whether or not to activate invoices for your shop'), 'cast' => 'intval', 'type' => 'bool'),
'PS_INVOICE_PREFIX' => array('title' => $this->l('Invoice prefix:'), 'desc' => $this->l('Prefix used for invoices'), 'size' => 6, 'type' => 'textLang'),
'PS_INVOICE_START_NUMBER' => array('title' => $this->l('Invoice number:'), 'desc' => $this->l('The next invoice will begin with this number, and then increase with each additional invoice. Set to 0 if you want to keep the current number (#').(Order::getLastInvoiceNumber() + 1).').', 'size' => 6, 'type' => 'text', 'cast' => 'intval'),
'PS_INVOICE_FREE_TEXT' => array('title' => $this->l('Free Text:'), 'desc' => $this->l('This text will appear at the bottom of the invoice'), 'size' => 6, 'type' => 'textareaLang',
'cols' => 40, 'rows' => 8)
);
parent::__construct();
}
public function displayForm($isMainTab = true)
{
global $currentIndex, $cookie;
$statuses = OrderState::getOrderStates($cookie->id_lang);
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
SELECT COUNT(*) as nbOrders, (
SELECT oh.id_order_state
FROM '._DB_PREFIX_.'order_history oh
WHERE oh.id_order = o.id_order
ORDER BY oh.date_add DESC, oh.id_order_history DESC
LIMIT 1
) id_order_state
FROM '._DB_PREFIX_.'orders o
GROUP BY id_order_state');
$statusStats = array();
foreach ($result as $row)
$statusStats[$row['id_order_state']] = $row['nbOrders'];
$helperForm = new HelperFormBootstrap();
$helperForm->_inputMask = true;
$helperForm->_forms = array(
array(
'action' => $currentIndex.'&token='.$this->token,
'title' => $this->l('Print PDF ').' - '.$this->l('By date and Sale'),
'icon' => ' ',
'class' => 'form-horizontal',
'class_div' => 'col-md-6',
'sections' => array(
array(
'inputs' => array(
array(
'type' => 'simpleDate',
'period' => true,
'class-from' => 'col-md-6',
'class-to' => 'col-md-6',
'id' => 'date_from',
'id-to' => 'date_to',
'name' => 'date_from',
'name-to' => 'date_to',
'label' => $this->l('De :'),
'label-to' => $this->l('A :'),
'label-class' => 'text-left',
'before' => '',
'before-to' => '',
'value' => (!empty(Tools::getValue('date_from')) ? Tools::getValue('date_from') : date('Y-m-d')),
'value-to' => (!empty(Tools::getValue('date_to')) ? Tools::getValue('date_to') : date('Y-m-d')),
),
),
'actions' => array(
array(
'type' => 'submit',
'class' => 'btn-primary',
'name' => 'submitGetSale',
'value' => $this->l('Get Sales')
)
),
'actions-class' => 'text-right',
),
),
),
array(
'action' => $currentIndex.'&token='.$this->token,
'title' => $this->l('Print PDF invoices').' - '.$this->l('By date'),
'icon' => ' ',
'class' => 'form-horizontal',
'class_div' => 'col-md-6',
'sections' => array(
array(
'inputs' => array(
array(
'type' => 'simpleDate',
'period' => true,
'class-from' => 'col-md-6',
'class-to' => 'col-md-6',
'id' => 'date_from2',
'id-to' => 'date_to2',
'name' => 'date_from',
'name-to' => 'date_to',
'label' => $this->l('De :'),
'label-to' => $this->l('A :'),
'label-class' => 'text-left',
'before' => '',
'before-to' => '',
'value' => date('Y-m-d'),
'value-to' => date('Y-m-d'),
),
),
'actions' => array(
array(
'type' => 'submit',
'class' => 'btn-primary',
'name' => 'submitPrint',
'value' => $this->l('Generate PDF file')
)
),
'actions-class' => 'text-right',
),
),
)
);
$helperForm->_css .= '
#invoices_form .table tr th {
text-align: center;
font-size: 12px;
}
#invoices_form .table tr td {
border-bottom: 1px solid #DEDEDE;
color: #000;
height: auto;
padding: 5px 4px;
vertical-align: middle;
}
#invoices_form .table tr td label {
font-size: 13px;
}
#invoices_form input[type=checkbox],
#invoices_form input[type=radio] {
margin: 0;
}
';
$helperForm->_js .= '
';
$form .= $helperForm->renderStyle();
$form .= '
'.$helperForm->renderForm(false, NULL, NULL, true).'
';
if($this->print_sales) {
$form .= '';
}
$form .= '';
if(Module::isInstalled('privatesales')) {
include_once(dirname(__FILE__).'/../../modules/privatesales/Sale.php');
$form .= '
'.$this->l('By private sale').'
';
if(Tools::getValue('check_print')) {
$check = '';
$token = Tools::getAdminTokenLite('AdminOrders');
foreach(Db::getInstance()->ExecuteS('
SELECT *
FROM `'._DB_PREFIX_.'orders` o
WHERE o.`invoice_number` != 0
AND o.`id_order` NOT IN (
SELECT p.`id_order`
FROM `'._DB_PREFIX_.'privatesale_printedinvoices` p
)
') as $not_printed) {
$check .= '
'.$not_printed['id_order'].'';
}
$form .= '
'.$this->l('Unprinted orders:').'';
} else {
$form .= '
'.$this->l('Display unprinted orders').'';
}
$form .= '
';
}
$form .= '
'.$this->l('By statuses').'
';
$form .= '
';
$form .= $helperForm->renderScript();
echo $form;
echo '';
return parent::displayForm();
}
/**
* @Override Antadis Options lists
*/
public function displayOptionsList()
{
global $currentIndex, $cookie, $tab;
if (!isset($this->_fieldsOptions) OR !sizeof($this->_fieldsOptions))
return false;
$defaultLanguage = (int)Configuration::get('PS_LANG_DEFAULT');
$this->_languages = Language::getLanguages(false);
$tab = Tab::getTab((int)$cookie->id_lang, Tab::getIdFromClassName($tab));
echo '
';
echo '
'.(isset($this->optionTitle) ? ''.$this->optionTitle.'
' : '').'
';
}
public function ajaxProcessResetPrintM2Plus()
{
Configuration::updateValue("ANT_CHECKED_SALES", '');
}
public function display()
{
$this->displayForm();
$this->displayOptionsList();
}
public function postProcess()
{
global $currentIndex;
if (Tools::isSubmit('submitPrint'))
{
if (!Validate::isDate(Tools::getValue('date_from')))
$this->_errors[] = $this->l('Invalid from date');
if (!Validate::isDate(Tools::getValue('date_to')))
$this->_errors[] = $this->l('Invalid end date');
if (!sizeof($this->_errors))
{
$orders = Order::getOrdersIdInvoiceByDate(Tools::getValue('date_from'), Tools::getValue('date_to'), NULL, 'invoice');
if (sizeof($orders))
Tools::redirectAdmin('pdf.php?invoices&date_from='.urlencode(Tools::getValue('date_from')).'&date_to='.urlencode(Tools::getValue('date_to')).'&token='.$this->token);
$this->_errors[] = $this->l('No invoice found for this period');
}
}
elseif (Tools::isSubmit('submitPrint2'))
{
if (!is_array($statusArray = Tools::getValue('id_order_state')) OR !count($statusArray))
$this->_errors[] = $this->l('Invalid order statuses');
else
{
foreach ($statusArray as $id_order_state)
if (count($orders = Order::getOrderIdsByStatus((int)$id_order_state)))
Tools::redirectAdmin('pdf.php?invoices2&id_order_state='.implode('-',$statusArray).'&token='.$this->token);
$this->_errors[] = $this->l('No invoice found for this status');
}
}
elseif (Tools::isSubmit('submitGetSale'))
{
include_once(dirname(__FILE__).'/../../modules/privatesales/Sale.php');
if (!Validate::isDate(Tools::getValue('date_from')))
$this->_errors[] = $this->l('Invalid from date');
if (!Validate::isDate(Tools::getValue('date_to')))
$this->_errors[] = $this->l('Invalid end date');
$statuts = array(17, Configuration::get('PS_OS_PAYMENT'));
$sales = Sale::getSalesByDateBis($statuts, Tools::getValue('date_from'), Tools::getValue('date_to'));
if($sales) {
$this->print_sales = $sales;
}
}
// print 48h
elseif (Tools::getValue('print48h') == 1)
{
include_once(dirname(__FILE__).'/../../modules/privatesales/Sale.php');
if($sale = new Sale((int) Tools::getValue('id_sale'))) {
if(!in_array($sale->delivery_delay, array(2 ,6) )) {
$this->_errors[] = $this->l('This sale is not delivery 48h');
} else {
$statuts = array(17, Configuration::get('PS_OS_PAYMENT'));
$orders = $sale->getOrdersFromSale($statuts, FALSE, TRUE);
if(sizeof($orders)) {
Tools::redirectAdmin('pdf.php?privatesales48h&id_sale='.(int) $sale->id.'&token='.$this->token);
}
}
}
$this->_errors[] = $this->l('No invoice found for this sale');
}
// print 48h bis (distinct multi)
elseif (Tools::getValue('print48hbis') == 1)
{
include_once(dirname(__FILE__).'/../../modules/privatesales/Sale.php');
if($sale = new Sale((int) Tools::getValue('id_sale'))) {
if(!in_array($sale->delivery_delay, array(2 ,6) )) {
$this->_errors[] = $this->l('This sale is not delivery 48h');
} else {
$statuts = array(17, Configuration::get('PS_OS_PAYMENT'));
$orders = $sale->getOrdersFromSale($statuts, FALSE, TRUE);
if(sizeof($orders)) {
Tools::redirectAdmin('pdf.php?privatesales48hbis&id_sale='.(int) $sale->id.'&token='.$this->token);
}
}
}
$this->_errors[] = $this->l('No invoice found for this sale');
}
// print printAll
elseif (Tools::getValue('printAll') == 1)
{
include_once(dirname(__FILE__).'/../../modules/privatesales/Sale.php');
if($sale = new Sale((int) Tools::getValue('id_sale'))) {
$statuts = array(17, Configuration::get('PS_OS_PAYMENT'));
$orders = $sale->getOrdersFromSale($statuts, FALSE, TRUE);
if(sizeof($orders)) {
if(Tools::getValue('gift') == 1) {
Tools::redirectAdmin('pdf.php?privatesalesAll&gift=1&id_sale='.(int) $sale->id.'&token='.$this->token);
} else {
Tools::redirectAdmin('pdf.php?privatesalesAll&id_sale='.(int) $sale->id.'&token='.$this->token);
}
}
}
$this->_errors[] = $this->l('No invoice found for this sale');
}
// print M1
elseif (Tools::getValue('printM1') == 1)
{
include_once(dirname(__FILE__).'/../../modules/privatesales/Sale.php');
if($sale = new Sale((int) Tools::getValue('id_sale'))) {
$statuts = array(17, Configuration::get('PS_OS_PAYMENT'));
$orders = $sale->getOrdersFromSale($statuts);
if(sizeof($orders)) {
if(Tools::getValue('gift') == 1) {
Tools::redirectAdmin('pdf.php?privatesales&gift=1&id_sale='.(int) $sale->id.'&token='.$this->token);
} else {
Tools::redirectAdmin('pdf.php?privatesales&id_sale='.(int) $sale->id.'&token='.$this->token);
}
}
}
$this->_errors[] = $this->l('No invoice found for this sale');
}
// print M2
elseif (Tools::getValue('printM2') == 1)
{
include_once(dirname(__FILE__).'/../../modules/privatesales/Sale.php');
if($sale = new Sale((int) Tools::getValue('id_sale'))) {
$statuts = array(17, Configuration::get('PS_OS_PAYMENT'));
$orders = $sale->getOrdersFromSaleMulti($statuts, 2);
if(sizeof($orders)) {
Tools::redirectAdmin('pdf.php?privatesalesM2&id_sale='.(int) $sale->id.'&token='.$this->token);
}
}
$this->_errors[] = $this->l('No invoice found for this sale');
}
// print M2'
elseif (Tools::isSubmit('submitPrintM2Bis')) {
include_once(dirname(__FILE__).'/../../modules/privatesales/Sale.php');
$id_sales = Tools::getValue('m2');
if (empty($id_sales) || (count($id_sales) != 2) ) {
$this->_errors[] = $this->l('You must select 2 sales for print M2\'');
} else {
$statuts = array(17, Configuration::get('PS_OS_PAYMENT'));
$orders = Sale::getOrdersM3($statuts, $id_sales, Tools::getValue('date_from'), Tools::getValue('date_to'), true);
if(sizeof($orders)) {
Tools::redirectAdmin('pdf.php?privatesalesM2Bis&id_sale='.implode(',', $id_sales) .'&date_from='.urlencode(Tools::getValue('date_from')).'&date_to='.urlencode(Tools::getValue('date_to')).'&token='.$this->token);
}
$this->_errors[] = $this->l('No invoice found for these sales');
}
}
// print multi (M2+)
elseif (Tools::isSubmit('submitPrintM2Plus')) {
include_once(dirname(__FILE__).'/../../modules/privatesales/Sale.php');
$id_sales = Tools::getValue('multi2');
Configuration::updateValue("ANT_CHECKED_SALES", serialize($id_sales));
if (empty($id_sales) || (count($id_sales) < 2) ) {
$this->_errors[] = $this->l('You must select 2 sales minimum for print M2+');
} else {
$statuts = array(17, (int)Configuration::get('PS_OS_PAYMENT'));
$orders = Sale::getOrdersM2($statuts, $id_sales, Tools::getValue('date_from'), Tools::getValue('date_to'));
if(sizeof($orders)) {
if(Tools::getValue('gift') == 1) {
Tools::redirectAdmin('pdf.php?privatesalesM2Plus&gift=1&id_sale='.implode(',', $id_sales) .'&date_from='.urlencode(Tools::getValue('date_from')).'&date_to='.urlencode(Tools::getValue('date_to')).'&token='.$this->token);
} else {
Tools::redirectAdmin('pdf.php?privatesalesM2Plus&id_sale='.implode(',', $id_sales) .'&date_from='.urlencode(Tools::getValue('date_from')).'&date_to='.urlencode(Tools::getValue('date_to')).'&token='.$this->token);
}
}
$this->_errors[] = $this->l('No invoice found for these sales');
}
}
// print multi with same brand
elseif (Tools::isSubmit('submitPrintMPlus')) {
include_once(dirname(__FILE__).'/../../modules/privatesales/Sale.php');
$id_sales = Tools::getValue('multiPlus');
if (empty($id_sales) || (count($id_sales) < 2) ) {
$this->_errors[] = $this->l('You must select 2 sales minimum for print M2+');
} else {
$statuts = array(17, Configuration::get('PS_OS_PAYMENT'));
$orders = Sale::getOrdersM2($statuts, $id_sales, Tools::getValue('date_from'), Tools::getValue('date_to'));
if(sizeof($orders)) {
if(Tools::getValue('gift') == 1) {
Tools::redirectAdmin('pdf.php?privatesalesM2Plus&gift=1&id_sale='.implode(',', $id_sales) .'&date_from='.urlencode(Tools::getValue('date_from')).'&date_to='.urlencode(Tools::getValue('date_to')).'&token='.$this->token);
} else {
Tools::redirectAdmin('pdf.php?privatesalesM2Plus&id_sale='.implode(',', $id_sales) .'&date_from='.urlencode(Tools::getValue('date_from')).'&date_to='.urlencode(Tools::getValue('date_to')).'&token='.$this->token);
}
}
$this->_errors[] = $this->l('No invoice found for these sales');
}
}
// print M3
elseif (Tools::isSubmit('submitPrintM3')) {
include_once(dirname(__FILE__).'/../../modules/privatesales/Sale.php');
$id_sales = Tools::getValue('multi3');
if (empty($id_sales) || (count($id_sales) < 3) ) {
$this->_errors[] = $this->l('You must select 3 sales minimum for print M3+');
} else {
$statuts = array(17, Configuration::get('PS_OS_PAYMENT'));
$orders = Sale::getOrdersM3($statuts, $id_sales, Tools::getValue('date_from'), Tools::getValue('date_to'));
if(sizeof($orders)) {
Tools::redirectAdmin('pdf.php?privatesalesM3&id_sale='.implode(',', $id_sales) .'&date_from='.urlencode(Tools::getValue('date_from')).'&date_to='.urlencode(Tools::getValue('date_to')).'&token='.$this->token);
}
$this->_errors[] = $this->l('No invoice found for these sales');
}
}
elseif (Tools::isSubmit('submitOptionsinvoice'))
{
if ((int)(Tools::getValue('PS_INVOICE_START_NUMBER')) != 0 AND (int)(Tools::getValue('PS_INVOICE_START_NUMBER')) <= Order::getLastInvoiceNumber())
$this->_errors[] = $this->l('Invalid invoice number (must be > ').Order::getLastInvoiceNumber() .')';
else
parent::postProcess();
} elseif(Module::isInstalled('privatesales')) {
include_once(dirname(__FILE__).'/../../modules/privatesales/Sale.php');
if(Tools::isSubmit('submitPrintMultipleSales')) {
if (!Validate::isDate(Tools::getValue('date_from')))
$this->_errors[] = $this->l('Invalid from date');
if (!Validate::isDate(Tools::getValue('date_to')))
$this->_errors[] = $this->l('Invalid end date');
if (!sizeof($this->_errors))
{
$orders = Sale::getOrdersByDate(TRUE, Tools::getValue('date_from'), Tools::getValue('date_to'), Configuration::get('PS_OS_PAYMENT'));
if (sizeof($orders)) {
Tools::redirectAdmin('pdf.php?privatesales2&date_from='.urlencode(Tools::getValue('date_from')).'&date_to='.urlencode(Tools::getValue('date_to')).'&token='.$this->token);
}
$this->_errors[] = $this->l('No invoice found for this period');
}
} elseif(Tools::isSubmit('submitPrintOneSale')) {
if($sale = new Sale((int) Tools::getValue('id_sale'))) {
$orders = $sale->getOrdersFromSale(Configuration::get('PS_OS_PAYMENT'));
if(sizeof($orders)) {
Tools::redirectAdmin('pdf.php?privatesales&id_sale='.(int) $sale->id.'&token='.$this->token);
}
}
$this->_errors[] = $this->l('No invoice found for this sale');
}
}
else
parent::postProcess();
}
}