bebeboutik/modules/philea_magistor/AdminPhileaMagistor.php

797 lines
38 KiB
PHP
Raw Normal View History

<?php
require_once(PS_ADMIN_DIR . '/helpers/HelperForm.php');
require_once(PS_ADMIN_DIR . '/helpers/HelperList.php');
class AdminPhileaMagistor extends AdminTab {
2016-10-21 16:06:36 +02:00
const TIME_DISPLAY = 0; // list displaying time in days. 0 to disable
static $_crrlist_cache = NULL;
public function postProcess() {
if(Tools::isSubmit('submitProductSaleCache')) {
$hour = (int) date('H');
$min = (int) date('i');
if ($hour%3 == 0 && $min <= 20 && $min >= 10 ){
echo '<p class="error">'.$this->l('Association automatique en cours, réessayez plus tard').'</p><br />';
} else {
$min_id_product = Db::getInstance()->getValue('
SELECT MIN(`id_product`) FROM `'._DB_PREFIX_.'product` WHERE `date_add` > DATE_SUB(NOW(), INTERVAL 10 DAY)
');
if ($min_id_product) {
Db::getInstance()->ExecuteS('
DELETE FROM `'._DB_PREFIX_.'product_ps_cache`
WHERE `id_product` >= '.$min_id_product.'
');
Db::getInstance()->ExecuteS('
INSERT IGNORE INTO `'._DB_PREFIX_.'product_ps_cache` (
SELECT p.id_product, IFNULL(
(
SELECT s.id_sale
FROM `'._DB_PREFIX_.'privatesale_category` s
WHERE s.`id_category` = p.`id_category_default`
LIMIT 1)
, 0
)
FROM `'._DB_PREFIX_.'product` p
WHERE p.`id_product` >= '.$min_id_product.'
)
');
echo '<p class="conf">'.$this->l('Produits associés aux ventes depuis le produit #').$min_id_product.'</p><br />';
} else {
echo '<p class="error">'.$this->l('Pas de produit ajouté depuis 10 jours').'</p><br />';
}
}
} elseif(Tools::isSubmit('submitPhilea')) {
2016-10-21 16:06:36 +02:00
$id_sale = (int) Tools::getValue('id_sale');
if($id_sale) {
if(!Db::getInstance()->getRow('
SELECT *
FROM `'._DB_PREFIX_.'philea_sync`
WHERE `id_sale` = '.(int) $id_sale.'
AND `status` = 0
')) {
Db::getInstance()->execute('
INSERT INTO `'._DB_PREFIX_.'philea_sync`
VALUES (
DEFAULT,
'.(int) $id_sale.',
0,
NOW()
)
');
// no REC on BBB
// if (Tools::getValue('force_rec_file')){
// // get last sync order form for this sale
// $last_order_sync = Db::getInstance()->getRow(
// 'SELECT * FROM `' . _DB_PREFIX_ . 'philea_supplier_order_sync`
// WHERE `id_sale` = ' . (int) $id_sale . '
// ORDER BY `date_add` DESC');
// if ($last_order_sync && isset($last_order_sync['id_order_form'])){
// Db::getInstance()->execute('
// DELETE FROM `' . _DB_PREFIX_ . 'philea_supplier_order_sync`
// WHERE `id_sale` = ' . (int) $id_sale . '
// AND `id_order_form` = ' . (int) $last_order_sync['id_order_form'] . '
// ORDER BY `date_add` DESC
// LIMIT 1');
// }
// }
echo '<p class="conf">'.$this->l('Sale added to queue successfully').'</p><br />';
} else {
echo '<p class="error">'.$this->l('Sync already in progress for this sale').'</p><br />';
}
} else {
echo '<p class="error">'.$this->l('Invalid sale id').'</p><br />';
}
}
elseif (Tools::isSubmit('auto_sync') && Tools::isSubmit('active')){
$id_sale = (int) Tools::getValue('id_sale');
if($id_sale) {
$active = (int) Tools::getValue('active');
$sql = '
INSERT INTO `'._DB_PREFIX_.'philea_auto_sync`
VALUES (
'.(int) $id_sale.',
'.(int) $active.',
"0000-00-00 00:00:00"
)
ON DUPLICATE KEY
UPDATE `active` = '.(int) $active;
if(Db::getInstance()->execute($sql)){
echo '<p class="conf">'.$this->l('Export auto activé').'</p><br />';
} else {
echo '<p class="error">'.$this->l('Export auto désactivé').'</p><br />';
}
} else {
echo '<p class="error">'.$this->l('Invalid sale id').'</p><br />';
}
}
elseif (Tools::isSubmit('addCDC') && Tools::getValue('id_sale')){
$id_sale = (int) Tools::getValue('id_sale');
if($id_sale) {
if(Db::getInstance()->getRow('
SELECT *
FROM `'._DB_PREFIX_.'philea_sync`
WHERE `id_sale` = '.(int) $id_sale.'
AND (`status` = 2
OR `status` = 4)
')) {
Db::getInstance()->ExecuteS('
UPDATE `'._DB_PREFIX_.'philea_sync`
SET `status` = 3
WHERE id_sale = '.(int) $id_sale.'
LIMIT 1
');
2016-10-28 11:37:41 +02:00
echo '<p class="conf">'.$this->l('CDC for this Sale added to queue successfully').'</p><br />';
2016-10-21 16:06:36 +02:00
} else {
2016-10-28 11:37:41 +02:00
echo '<p class="error">'.$this->l('CDC Sync already in progress for this sale').'</p><br />';
2016-10-21 16:06:36 +02:00
}
} else {
echo '<p class="error">'.$this->l('Invalid sale id').'</p><br />';
}
}
elseif(Tools::isSubmit('getCsv') && Tools::getValue('id_sale') && Tools::getValue('filename')){
$id_sale = (int) Tools::getValue('id_sale');
$filename = Tools::getValue('filename');
$this->exportCsv((int) $id_sale, $filename);
}
elseif(Tools::isSubmit('philea_archive_submit')){
$id_sales = Tools::getValue('id_sales');
if (count($id_sales)){
$sql_values = array();
$sql_insert = 'INSERT INTO `'._DB_PREFIX_.'philea_archive` (`id_sale`, `date_add`) VALUES';
foreach ($id_sales as $id_sale)
$sql_values[] = '(' . (int) $id_sale . ', NOW())';
$sql_insert .= implode(', ', $sql_values) . ' ON DUPLICATE KEY UPDATE `date_add` = NOW()';
if (Db::getInstance()->execute($sql_insert)){
echo '<p class="conf">'.$this->l('Sales successfully archived').'</p><br />';
}
else{
echo '<p class="conf">'.$this->l('Sales cannot be archived').'</p><br />';
}
}
else{
echo '<p class="error">'.$this->l('No sale selected.').'</p><br />';
}
}
elseif(Tools::isSubmit('unarchiveSale') && Tools::getValue('id_sale_unarchive')){
if (Db::getInstance()->executeS('
DELETE FROM `' . _DB_PREFIX_ . 'philea_archive`
WHERE `id_sale` = ' . (int) Tools::getValue('id_sale_unarchive') . '
LIMIT 1
')){
echo '<p class="conf">'.$this->l('Vente désarchivée').'</p><br />';
}
else{
echo '<p class="error">'.$this->l('La vente n\'a pas pu être désarchivée.').'</p><br />';
}
}
}
public function exportCsv($id_sale, $filename){
$sql = '
SELECT *
FROM `'._DB_PREFIX_.'philea_reception`
WHERE `id_sale` = ' . (int) $id_sale . '
AND `filename` LIKE "' . pSQL($filename) . '"';
$recept = Db::getInstance()->getRow($sql);
if (!$recept)
return;
$Y = date('Y', strtotime($recept['date_add']));
$m = date('m', strtotime($recept['date_add']));
$filepath = dirname(__FILE__).'/script/archives/IN/RECEPTION/';
$filepath .= $Y . '/' . $m . '/' . $recept['filename'];
$content = file_get_contents( $filepath );
$lines = preg_split( '@\n@', $content );
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Description: File Transfer');
header('Content-Type: application/csv; charset=utf-8');
header('Content-Disposition: attachment; filename='.$filename.'_'.(int)$id_sale.'.csv;');
header('Content-Transfer-Encoding: binary');
$format = array(
'OP_CODE' => array(1,10),
'CODE_SOC' => array(11,20),
'N_CDE' => array(31,20),
'DATE_RECEP' => array(51,8),
'CODE_ART' => array(59,50),
'QTE' => array(109,10),
'OTHER' => array(119, 28),
);
ob_clean();
//open file pointer to standard output
$fp = fopen('php://output', 'w');
$delim = ';';
// first row
$data=array();
foreach ($format as $col_name => $array_def)
$data[]=Tools::htmlentitiesDecodeUTF8($col_name);
fputcsv ($fp,$data,$delim);
foreach( $lines as $line ) {
$data = array();
foreach($format as $field => $value) {
$data[] = substr($line, ($value[0]-1), $value[1]);
}
fputcsv ($fp,array_map('utf8_decode',array_values($data)),$delim);
}
fclose($fp);
exit;
}
public function display() {
global $cookie, $currentIndex;
$form = '<style type="text/css">
.path_bar {
background-color: #F1F1F1;
border: 1px solid #565485;
}
h2 {
color: #565485;
}
fieldset {
background: #F1F1F1;
border: 1px solid #565485;
}
legend {
background: #565485;
background: rgba(86,84,133,0.9);
border: 1px solid #565485;
color: #fff;
}
.table {
border: 1px solid #565485;
}
.table tr th {
background: #565485;
background: rgba(86,84,133,0.9);
text-align: center;
color: #fff;
}
.table tr{
background:#fff;
}
.table tr:nth-child(even){
background:#F1F1F1;
}
.table tr:nth-child(even) td {
border-left:1px solid #FFF;
border-right:1px solid #FFF;
}
.table tr td {
border-bottom: 1px solid #DEDEDE;
color: #000;
height: auto;
padding: 5px 4px 10px 4px;
}
.table tr td[id*=ART] p,
.table tr td[id*=CDC] p{
font-size:11px;
}
.table tr td label {
font-size:13px;
}
input[type="text"], input[type="password"],
input[type="file"], textarea, select {
border: 1px solid #565485;
}
.button {
background-color: rgba(86,84,133,0.7);
border: 1px solid #565485;
border-left: 1px solid rgba(86,84,133,0.6);
border-top: 1px solid rgba(86,84,133,0.6);
color: rgba(255,255,255,0.9);
padding: 3px;
}
.button:hover {
background-color: #565485;
border: 1px solid rgba(86,84,133,0.6);
border-left: 1px solid #565485;
border-top: 1px solid #565485;
color: #fff;
padding: 3px;
}
.button:focus{
background-color: rgba(86,84,133,0.6);
}
.table td div{
margin: 5px 0;
2016-10-28 13:09:19 +02:00
margin-bottom: 12px;
2016-10-21 16:06:36 +02:00
}
.table td .button{
2016-11-02 10:26:40 +01:00
padding: 5px 2px;
2016-10-21 16:06:36 +02:00
}
.div_report_overlay{
background: rgb(181,189,200); /* Old browsers */
background: -moz-linear-gradient(top, rgba(181,189,200,0.4) 0%, rgba(130,140,149,0.4) 36%, rgba(40,52,59,0.4) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(181,189,200,0.4)), color-stop(36%,rgba(130,140,149,0.4)), color-stop(100%,rgba(40,52,59,0.4))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, rgba(181,189,200,0.4) 0%,rgba(130,140,149,0.4) 36%,rgba(40,52,59,0.4) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, rgba(181,189,200,0.4) 0%,rgba(130,140,149,0.4) 36%,rgba(40,52,59,0.4) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, rgba(181,189,200,0.4) 0%,rgba(130,140,149,0.4) 36%,rgba(40,52,59,0.4) 100%); /* IE10+ */
background: linear-gradient(to bottom, rgba(181,189,200,0.4) 0%,rgba(130,140,149,0.4) 36%,rgba(40,52,59,0.4) 100%); /* W3C */
position: fixed;
top: 0;
left: 0;
width: 100%;
z-index: 998;
height: 100%;
cursor: pointer;
display: none;
}
2016-09-19 11:58:14 +02:00
.div_report {
2016-10-21 16:06:36 +02:00
display:none;
position: fixed;
z-index: 999;
left: 50%;
margin-left: -287.5px;
position: fixed;
top: 15%;
padding: 15px;
padding-top: 20px;
background: #fff;
border: 1px solid;
line-height: 20px;
text-align: left;
max-height: 300px;
width: 575px;
overflow: auto;
color:#000;
}
.div_report h3 {text-align:center;}
2016-09-16 16:36:16 +02:00
</style>';
2016-10-21 16:06:36 +02:00
$form .= '<h2>'.$this->l('Philea').'</h2>';
$base_link = $currentIndex . '&token='.Tools::getAdminTokenLite('AdminPhileaMagistor');
$id_sale_options = array();
foreach(Db::getInstance()->ExecuteS('
SELECT p.`id_sale`, c.`name`, c.`id_category`
FROM `'._DB_PREFIX_.'privatesale` p
LEFT JOIN `'._DB_PREFIX_.'category_lang` c ON (c.`id_category` = p.`id_category`)
WHERE c.`id_lang` = '.$cookie->id_lang.'
AND p.`date_start` > "2015-01-01 00:00:00"
ORDER BY p.`id_sale` DESC
') as $row) {
$extrafields = Category::getSalesInfos(array((int) $row['id_category']));
$id_sale_options[] = array(
'label' => (int) $row['id_sale'].' - '.$row['name'].(empty($extrafields[(int) $row['id_category']]['sales'][1])?'':' - '.$extrafields[(int) $row['id_category']]['sales'][1]) ,
'value' => (int) $row['id_sale']
);
}
$helperForm = new HelperForm();
$helperForm->_forms = array(
array(
'action' => $base_link,
'legend' => $this->l('Association Vente - Produits'),
'actions' => array(
array(
'type' => 'submit',
'name' => 'submitProductSaleCache',
'label' => $this->l('Associer les produits aux ventes')
)
)
)
);
$form .= $helperForm->renderForm(false, NULL, NULL, true);
2016-10-21 16:06:36 +02:00
$helperForm = new HelperForm();
$helperForm->_forms = array(
array(
'action' => $base_link,
'legend' => $this->l('Philea'),
'inputs' => array(
array(
'type' => 'select-styled',
2016-10-21 16:16:12 +02:00
'class-select' => 'bg-purple-light big-select',
2016-10-21 16:06:36 +02:00
'name' => 'id_sale',
'label' => $this->l('Select a sale'),
'options' => $id_sale_options,
'filter' => true,
'filter_text' => $this->l('Filter par vente')
),
// array(
// 'type' => 'checkbox',
// 'name' => 'force_rec_file',
// 'label' => $this->l('Forcer l\'envoi'),
// 'text' => $this->l('Force un nouvel envoi du fichier REC.')
// )
),
'actions' => array(
array(
'type' => 'submit',
'name' => 'submitPhilea',
'label' => $this->l('Envoyer à Philea')
)
)
)
);
$form .= $helperForm->renderForm(false, NULL, NULL, true);
$CRR_list = $this->getCRRList();
$form .= '
<form id="upload_form" action="' . $base_link . '" method="post" class="form"><div class="clear">&nbsp;</div>
<fieldset><legend>'.$this->l('Orders CRR').'</legend>
<p>'.$this->l('Historique des ventes envoyées à Philea'). (self::TIME_DISPLAY > 0 ? ' (' . self::TIME_DISPLAY . ' ' . $this->l('derniers jours') . ')' : '').'</p>
<input type="hidden" id="id_sale_hidden" name="id_sale" value="" />
<input type="hidden" name="addCDC"/>
<p><input type="submit" class="btn button" name="philea_archive_submit" value="' . $this->l('Archiver la sélection') . '"/></p>
<table class="table">
<thead>
<tr>
<th colspan="2">' . $this->l('ID sale') . '</th>
<th width="325">' . $this->l('Name') . '</th>
<th>' . $this->l('Date envoi (ART)') . '</th>
<th>' . $this->l('Envoyer la commande') . '</th>
<th>' . $this->l('Date envoi (CMD)') . '</th>
<th>' . $this->l('Préparation (CRP)') . '</th>
<th>' . $this->l('Envoi auto') . '</th>
</tr>
</thead>
<tbody>';
$dowload_link = _MODULE_DIR_.'philea_magistor/script/archives/IN/RECEPTION/';
$upload_link = $base_link;
$now = date('Y-m-d H:i:s', mktime(date('H') -1));
foreach ($CRR_list as $CRR) {
$_recep = FALSE;
if (isset($CRR['recep_date']) && $CRR['recep_date'] && isset($CRR['filename']) && $CRR['filename']){
$_recep = TRUE;
$Y = date('Y', strtotime($CRR['recep_date']));
$m = date('m', strtotime($CRR['recep_date']));
$dl_link = $dowload_link . $Y . '/' . $m . '/' . $CRR['filename'];
$dl_csv = $base_link . '&getCsv&id_sale='.(int) $CRR['id_sale'].'&filename='.$CRR['filename'];
$dl_link = '<a class="button" target="_blank" href="' . $dl_link . '" title="'.$this->l('Download CRR file').'"><img src="../img/admin/import.gif"/>'.$this->l('.DAT').'</a>&nbsp;&nbsp;&nbsp;';
//$dl_link .= '<a class="button" target="_blank" href="' . $dl_csv . '" title="'.$this->l('Download CRR file').'"><img src="../img/admin/import.gif"/>'.$this->l('.CSV').'</a>';
}
else{
$CRR['recep_date'] = '--';
$dl_link = '';
}
$auto_sync = (isset($CRR['auto_sync_active']) && $CRR['auto_sync_active']) ? true : false;
$title = ($auto_sync ? $this->l('Désactiver l\'envoi automatique pour la vente') : $this->l('Activer l\'envoi automatique pour la vente') );
if (isset($CRR['date_end']) && $CRR['date_end'] >= $now)
$auto_sync_btn = '<a title="' . $title . ' '. (int)$CRR['id_sale'] .'" data-id_sale = "' . (int) $CRR['id_sale'] . '" class="auto_sync_btn' . ($auto_sync ? ' active' : '') . '" href="' . $currentIndex . '&token=' . $this->token . '&auto_sync&id_sale=' . (int)$CRR['id_sale'] . '&active=' . (int) !$auto_sync . '"><img src="../img/admin/' . ($auto_sync ? 'module_install.png' : 'module_disabled.png') . '"/></a>';
else
$auto_sync_btn = '<span style="opacity: 0.4;" title="' . $this->l('La vente est terminée') .'"><img src="../img/admin/' . ($auto_sync ? 'module_install.png' : 'module_notinstall.png') . '"/></span>';
$submit_link = $upload_link;// . '&id_sale=' . (int) $CRR['id_sale'] . '&addCDC=1';
$art_reports = Db::getInstance()->executeS('
SELECT `report_data`, `report_date`, `id_sale`
FROM `'._DB_PREFIX_.'philea_syncreport`
WHERE `id_sale` = '.(int) $CRR['id_sale'] .'
AND `filename` LIKE "ART%"
AND `report_data` IS NOT NULL
');
$cdc_reports = Db::getInstance()->executeS('
SELECT `report_data`, `report_date`, `id_sale`
FROM `'._DB_PREFIX_.'philea_syncreport`
WHERE `id_sale` = '.(int) $CRR['id_sale'] .'
AND `filename` LIKE "CDC%"
AND `report_data` IS NOT NULL
');
$crp_reports = Db::getInstance()->executeS('
SELECT `report_data`, `report_date`, `id_sale`
FROM `'._DB_PREFIX_.'philea_syncreport`
WHERE `id_sale` = '.(int) $CRR['id_sale'] .'
AND `filename` LIKE "CRP%"
AND `report_data` IS NOT NULL
');
$form .= '<tr>
<td><input type="checkbox" id="check_sale_'.(int) $CRR['id_sale'].'" name="id_sales[]" value="'.(int) $CRR['id_sale'].'" /></td>
<td><label style="float:none; width:auto; padding: 0; text-align: left; font-weight: normal;" for="check_sale_'.(int) $CRR['id_sale'].'">'.(int) $CRR['id_sale'].'</label></td>
<td><label style="float:none; width:auto; padding: 0; text-align: left; font-weight: normal;" for="check_sale_'.(int) $CRR['id_sale'].'"><strong>'.$CRR['name'].'</strong> - '.$CRR['subtitle'].'</label></td>
<td style="text-align: center;" id="ART_'. (int) $CRR['id_sale'] .'">
<p style="color: #565485;">'.(!empty($CRR['sync_date'])?'<span style="font-size: 12px;" class="anticon anticon-clock"></span> '.date('d/m/Y H:i',strtotime($CRR['sync_date'])):'--').'</p>';
if(!empty($art_reports)){
foreach($art_reports as $key => $report) {
$form .= '<div>
<a class="button see_report" data-id="'. (int) $CRR['id_sale'] .'" data-key="'.(int)$key.'" data-type="ART" href="#" title="'.$this->l('See ART report').'"><span class="anticon anticon-file-text2"></span> '.date('d/m/Y H:i',strtotime($report['report_date'])).'</a>
<div id="'.(int) $CRR['id_sale'].'_'.$key.'_ART" style="display:none">'.(!empty($report['report_data'])?$report['report_data']:'Pas encore de Rapport').'</div>
</div>';
}
}
$form .= '</td>
2016-10-28 11:51:40 +02:00
<td style="text-align: center;"><a title="'.$this->l('Envoi du fichier CDC').'" class="button upload_link" data-id="' . (int) $CRR['id_sale'] . '" href="'.$submit_link.'" title="'.$this->l('Upload CDC file').'"><span class="anticon anticon-upload2"></span> '.$this->l('Envoyer à Philea').'</a></td>
2016-10-21 16:06:36 +02:00
<td style="text-align: center;" id="CDC_'. (int) $CRR['id_sale'] .'">
<p style="color: #565485;">'.(!empty($CRR['sent_date'])?'<span style="font-size: 12px;" class="anticon anticon-clock"></span> '.date('d/m/Y H:i',strtotime($CRR['sent_date'])):'--').'</p>';
if(!empty($cdc_reports)){
2016-11-02 17:41:06 +01:00
$form .= '<span class="see_more anticon anticon-zoom-in" style="color:#565485;cursor:pointer"></span><div class="reports" style="display:none">';
2016-10-21 16:06:36 +02:00
foreach($cdc_reports as $key => $report) {
$form .= '<div>
<a class="button see_report" data-id="'. (int) $CRR['id_sale'] .'" data-key="'.(int)$key.'" data-type="CDC" href="#" title="'.$this->l('See CDC report').'"><span class="anticon anticon-file-text2"></span> '.date('d/m/Y H:i',strtotime($report['report_date'])).'</a>
<div id="'.(int) $CRR['id_sale'].'_'.$key.'_CDC" style="display:none">'.(!empty($report['report_data'])?$report['report_data']:'Pas encore de Rapport').'</div>
</div>';
}
2016-11-02 17:41:06 +01:00
$form .= '</div>';
2016-10-21 16:06:36 +02:00
}
$form .= '</td>
<td style="text-align: center;" id="CRP_'. (int) $CRR['id_sale'] .'">';
if(!empty($crp_reports)){
2016-11-02 17:41:06 +01:00
$form .= '<p style="color: #565485;"><span style="font-size: 12px;" class="anticon anticon-clock"></span> '.date('d/m/Y H:i',strtotime($crp_reports[0]['report_date'])).'</p>
<span class="see_more anticon anticon-zoom-in" style="color:#565485;cursor:pointer"></span><div class="reports" style="display:none">';
2016-10-21 16:06:36 +02:00
foreach($crp_reports as $key => $report) {
$form .= '<div>
<a class="button see_report" data-id="'. (int) $CRR['id_sale'] .'" data-key="'.(int)$key.'" data-type="CRP" href="#" title="'.$this->l('See CDC report').'"><span class="anticon anticon-file-text2"></span> '.date('d/m/Y H:i',strtotime($report['report_date'])).'</a>
<div id="'.(int) $CRR['id_sale'].'_'.$key.'_CRP" style="display:none">'.(!empty($report['report_data'])?$report['report_data']:'Pas encore de Rapport').'</div>
</div>';
}
2016-11-02 17:41:06 +01:00
$form .= '</div>';
2016-10-21 16:06:36 +02:00
}
$form .= '</td>
<td style="text-align: center;">'.$auto_sync_btn.'</td>
</tr>';
}
$form .= '
</tbody>
</table>
<div class="report_error div_report"></div>
<p><input type="submit" class="btn button" name="philea_archive_submit" value="' . $this->l('Archiver la sélection') . '"/></p>
</fieldset>
</form>
<div class="div_report_overlay"></div>
<script type="text/javascript">
$("document").ready(function(){
2016-11-02 17:41:06 +01:00
$(".see_more").click(function(){
$(this).toggleClass("anticon-zoom-in");
$(this).toggleClass("anticon-zoom-out");
$(this).next("div.reports").toggle();
});
2016-10-21 16:06:36 +02:00
$(".upload_link").click(function(e){
e.preventDefault();
$("#id_sale_hidden").val($(this).data("id"));
$("#upload_form").submit();
});
$(".see_report").each(function(){
$(this).click(function(e){
e.preventDefault();
$(".report_error").empty();
$(".report_error").hide();
id_sale = $(this).data("id");
type = $(this).data("type");
key = $(this).data("key");
report_data = $("div#"+id_sale+"_"+key+"_"+type).html();
console.log(report_data);
$(".div_report").append("<h3>Rapport "+type+" vente "+id_sale+"</h3>"+report_data+"");
if(type == "ART") {
$(".div_report").css("text-align","left");
} else {
$(".div_report").css("text-align","center");
}
$(".div_report").show();
$(".div_report_overlay").show();
})
});
$(".div_report_overlay").click(function(e){
$(".div_report").empty();
$(".div_report").hide();
$(".div_report_overlay").hide();
});
});
</script>
';
$form .= $this->displayArchives();
echo $form;
}
/**
* Get CRR files for not sent sales
*/
public function getCRRList(){
if (isset(self::$_crrlist_cache) && self::$_crrlist_cache)
return self::$_crrlist_cache;
// Get CRR if CDC has not been sent OR sync_date < TIME_DISPLAY DAYs
$id_sales = array();
$pm_sync = array();
$sql = '
SELECT
pm_sync.`id_sale`,
MAX(pm_sync.`date_add`) as `sync_date`
FROM `'._DB_PREFIX_.'philea_sync` pm_sync
LEFT JOIN `'._DB_PREFIX_.'philea_archive` pm_arch
ON pm_arch.`id_sale` = pm_sync.`id_sale`
WHERE 1
AND `pm_arch`.`id_sale` IS NULL
GROUP BY pm_sync.`id_sale`
' . ((int) self::TIME_DISPLAY > 0 ? 'HAVING `sync_date` > DATE_SUB(NOW(), INTERVAL ' . self::TIME_DISPLAY . ' DAY)' : '') . '
ORDER BY `sync_date` DESC';
foreach (Db::getInstance()->executeS($sql) as $row) {
$id_sales[] = (int) $row['id_sale'];
$pm_sync[(int) $row['id_sale']] = $row['sync_date'];
}
$receptions = array();
$sql = '
SELECT
pm_r.`id_sale`,
pm_r.`filename`,
MAX(pm_r.`date_add`) as `recep_date`
FROM `'._DB_PREFIX_.'philea_reception` pm_r
WHERE `id_sale` IN (' . implode(', ', $id_sales) . ')
GROUP BY `id_sale`';
foreach (Db::getInstance()->executeS($sql) as $row)
$receptions[$row['id_sale']] = $row;
$sales = array();
$sql = '
SELECT
p.`id_sale`,
c.`name`,
c.`id_category`,
p.`date_start`,
p.`date_end`
FROM `'._DB_PREFIX_.'privatesale` p
LEFT JOIN `'._DB_PREFIX_.'category_lang` c
ON c.`id_category` = p.`id_category`
AND c.`id_lang` = 2
WHERE p.`id_sale` IN (' .implode(', ', $id_sales) . ')';
foreach (Db::getInstance()->executeS($sql) as $row)
$sales[(int) $row['id_sale']] = $row;
$pm_autosales = array();
$sql = '
SELECT
pm_as.`id_sale`,
pm_as.`active` as `auto_sync_active`
FROM `'._DB_PREFIX_.'philea_auto_sync` pm_as
WHERE `id_sale` IN (' . implode(', ', $id_sales) . ')
AND pm_as.`active` = 1';
foreach (Db::getInstance()->executeS($sql) as $row)
2016-11-03 17:20:09 +01:00
$pm_active[(int) $row['id_sale']] = (int) $row['auto_sync_active'];
2016-10-21 16:06:36 +02:00
$pm_sent = array();
$sql = '
SELECT
pm_s.`id_sale`,
MAX(pm_s.`date_add`) as `sent_date`
FROM `'._DB_PREFIX_.'philea_sent` pm_s
WHERE pm_s.`id_sale` IN (' . implode(', ', $id_sales) . ')
GROUP BY pm_s.`id_sale`';
foreach (Db::getInstance()->executeS($sql) as $row)
$pm_sent[(int) $row['id_sale']] = $row['sent_date'];
$CRR_list = array();
foreach ($id_sales as $id_sale) {
$sync_date = (isset($pm_sync[(int) $id_sale]) ? $pm_sync[(int) $id_sale] : '');
$recep = (isset($receptions[(int) $id_sale]) ? $receptions[(int) $id_sale] : array('filename' => '', 'recep_date' => ''));
$sale = (isset($sales[(int) $id_sale]) ? $sales[(int) $id_sale] : array('name' => '', 'date_start' => '', 'date_end' => ''));
$sent_date = (isset($pm_sent[(int) $id_sale]) ? $pm_sent[(int) $id_sale] : '');
$as_active = (isset($pm_active[(int) $id_sale])) ? 1 : 0;
$extrafields = Category::getSalesInfos(array((int) $sales[(int) $id_sale]['id_category']));
$CRR_list[$id_sale] = array(
'id_sale' => $id_sale,
'sync_date' => $sync_date,
'filename' => $recep['filename'],
'recep_date' => $recep['recep_date'],
'name' => $sale['name'],
'sent_date' => $sent_date,
'auto_sync_active' => $as_active,
'date_start' => $sale['date_start'],
'date_end' => $sale['date_end'],
'subtitle' => $extrafields[(int) $sales[(int) $id_sale]['id_category']]['sales'][1]
);
}
self::$_crrlist_cache = $CRR_list;
return self::$_crrlist_cache;
// $CRR_files = Db::getInstance()->executeS('
// SELECT
// pmsync.id_sale,
// MAX(pmsync.`date_add`) as `sync_date`,
// pmr.`filename`,
// MAX(pmr.`date_add`) as `recep_date`,
// c.`name`,
// MAX(pms.date_add) as `sent_date`,
// pmas.`active` as `auto_sync_active`,
// p.date_start,
// p.date_end
// FROM `'._DB_PREFIX_.'philea_sync` pmsync
// LEFT JOIN `'._DB_PREFIX_.'philea_sent` pms
// ON pms.`id_sale` = pmsync.`id_sale`
// LEFT JOIN `'._DB_PREFIX_.'privatesale` p
// ON p.`id_sale` = pmsync.`id_sale`
// LEFT JOIN `'._DB_PREFIX_.'category_lang` c
// ON c.`id_category` = p.`id_category`
// LEFT JOIN `'._DB_PREFIX_.'philea_reception` pmr
// ON pmsync.`id_sale` = pmr.`id_sale`
// LEFT JOIN `'._DB_PREFIX_.'philea_auto_sync` pmas
// ON pmas.`id_sale` = pmsync.`id_sale`
// LEFT JOIN `'._DB_PREFIX_.'philea_archive` pm_arch
// ON pm_arch.`id_sale` = pmsync.`id_sale`
// WHERE 1
// AND c.id_lang = 2
// AND `pm_arch`.id_sale IS NULL
// ' . ((int) self::TIME_DISPLAY > 0 ? 'AND pmsync.`date_add` > DATE_SUB(NOW(), INTERVAL ' . self::TIME_DISPLAY . ' DAY)' : '') . '
// GROUP BY pmsync.`id_sale`
// ' . ((int) self::TIME_DISPLAY > 0 ? '
// HAVING (
// MAX(pms.date_add) IS NULL
// OR MAX(pmsync.`date_add`) > DATE_SUB(NOW(), INTERVAL ' . self::TIME_DISPLAY . ' DAY)
// OR pmsync.id_sale = 6304
// )' : '') . '
// ORDER BY `sync_date` DESC, `recep_date` DESC');
// self::$_crrlist_cache = $CRR_files;
// return self::$_crrlist_cache;
}
public function displayArchives(){
global $cookie, $currentIndex;
$base_link = $currentIndex . '&token='.Tools::getAdminTokenLite('AdminPhileaMagistor');
$archives_options = array();
foreach(Db::getInstance()->executeS('
SELECT a.`id_sale`, cl.`name`
FROM `' . _DB_PREFIX_ . 'philea_archive` a
LEFT JOIN `' . _DB_PREFIX_ . 'privatesale` p
ON p.`id_sale` = a.`id_sale`
LEFT JOIN `' . _DB_PREFIX_ . 'category_lang` cl
ON cl.`id_category` = p.`id_category`
AND cl.`id_lang` = ' . Configuration::get('PS_LANG_DEFAULT') . '
') as $row){
$archives_options[] = array(
'label' => (int) $row['id_sale'].' - '.$row['name'],
'value' => (int) $row['id_sale']
);
}
$helperForm = new HelperForm();
$helperForm->_forms = array(
array(
'action' => $base_link,
'legend' => $this->l('Archives'),
'inputs' => array(
array(
'type' => 'select-styled',
2016-10-21 16:16:12 +02:00
'class-select' => 'bg-purple-light big-select',
2016-10-21 16:06:36 +02:00
'name' => 'id_sale_unarchive',
'label' => $this->l('Désarchiver une vente'),
'options' => $archives_options,
'filter' => true,
'filter_text' => $this->l('Filter par vente')
)
),
'actions' => array(
array(
'type' => 'submit',
'name' => 'unarchiveSale',
'label' => $this->l('Désarchiver la vente')
)
)
)
);
return $helperForm->renderForm(false, NULL, NULL, true);
}
}