577 lines
21 KiB
PHP
577 lines
21 KiB
PHP
<?php
|
|
/**
|
|
* NOTICE OF LICENSE
|
|
*
|
|
* This source file is subject to a commercial license from SARL Ether Création
|
|
* Use, copy, modification or distribution of this source file without written
|
|
* license agreement from the SARL Ether Création is strictly forbidden.
|
|
* In order to obtain a license, please contact us: contact@ethercreation.com
|
|
* ...........................................................................
|
|
* INFORMATION SUR LA LICENCE D'UTILISATION
|
|
*
|
|
* L'utilisation de ce fichier source est soumise a une licence commerciale
|
|
* concedee par la societe Ether Création
|
|
* Toute utilisation, reproduction, modification ou distribution du present
|
|
* fichier source sans contrat de licence ecrit de la part de la SARL Ether Création est
|
|
* expressement interdite.
|
|
* Pour obtenir une licence, veuillez contacter la SARL Ether Création a l'adresse: contact@ethercreation.com
|
|
* ...........................................................................
|
|
* @package ecstock
|
|
* @author Nicolas Bedrane
|
|
* @copyright Copyright (c) 2010-2017 S.A.R.L Ether Création (http://www.ethercreation.com)
|
|
* @license Commercial license
|
|
*/
|
|
|
|
require_once dirname(__FILE__) . '/../../config/config.inc.php';
|
|
require_once dirname(__FILE__) .'/ecstock.php';
|
|
if (Tools::getValue('token') != Configuration::get('ECSTOCK_TOKEN')) {
|
|
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
|
|
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
|
header('Cache-Control: no-store, no-cache, must-revalidate');
|
|
header('Cache-Control: post-check=0, pre-check=0', false);
|
|
header('Pragma: no-cache');
|
|
Tools::redirect('Location: ../');
|
|
exit();
|
|
}
|
|
$id_shop = Tools::getValue('id_shop');
|
|
$token = Tools::getValue('token');
|
|
ignore_user_abort(true);
|
|
|
|
$paramHelp = Tools::getValue('help', null);
|
|
if (!is_null($paramHelp)) {
|
|
$help = array(
|
|
'kill' => array(
|
|
'fr' => 'Arrête l\'opération en cours. Facultatif.',
|
|
'en' => 'Stops the current update. Optional.'
|
|
),
|
|
);
|
|
echo Tools::jsonEncode($help);
|
|
exit();
|
|
}
|
|
|
|
//$token = Tools::getValue('ec_token');
|
|
/*
|
|
if ($token != Catalog::getInfoEco('ECO_TOKEN')) {
|
|
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
|
|
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
|
header('Cache-Control: no-store, no-cache, must-revalidate');
|
|
header('Cache-Control: post-check=0, pre-check=0', false);
|
|
header('Pragma: no-cache');
|
|
header('Location: ../');
|
|
exit();
|
|
}
|
|
*/
|
|
|
|
$logging = true;
|
|
if ($logging) {
|
|
$logger = new FileLogger();
|
|
$logger->setFilename(_PS_ROOT_DIR_ . '/modules/ecstock/log/ecexportstock.log');
|
|
} else {
|
|
$logger = false;
|
|
}
|
|
|
|
$stopTime = time() + 15;
|
|
$listStages = array(
|
|
'1' => 'STARTED',
|
|
'2' => 'EXPSTOCK',
|
|
);
|
|
$id_supplier = 0; // 0 => all suppliers, if else => only one supplier
|
|
|
|
// paramètres
|
|
$paramNbCron = Tools::getValue('nbC', null);
|
|
$nbCron = is_null($paramNbCron) ? 0 : (int) $paramNbCron;
|
|
|
|
$paramAct = Tools::getValue('act', null);
|
|
$action = (Configuration::getGlobalValue('ECI_USTK_ACT') === 'die') ? 'die' : (is_null($paramAct) ? 'go' : $paramAct);
|
|
|
|
$paramSpy = Tools::getValue('spy', null);
|
|
$spy = is_null($paramSpy) ? false : true;
|
|
|
|
$paramSpy2 = Tools::getValue('spytwo', null);
|
|
$spy2 = (empty($paramSpy2)) ? false : true;
|
|
$who = $spy ? ($spy2 ? 'spy2' : 'spy') : 'normal';
|
|
|
|
$paramKill = Tools::getValue('kill', null);
|
|
$kill = is_null($paramKill) ? false : true;
|
|
|
|
//link
|
|
$base_uri = (((Configuration::get('PS_SSL_ENABLED') == 1) && (Configuration::get('PS_SSL_ENABLED_EVERYWHERE') == 1)) ? 'https://' : 'http://' )
|
|
. Tools::getShopDomain()
|
|
. __PS_BASE_URI__
|
|
. str_replace(_PS_ROOT_DIR_ . '/', '', __FILE__)
|
|
. '?ts=' . microtime(true);
|
|
|
|
|
|
if ($logging) {
|
|
$logger->logInfo(
|
|
$who . ' entered, parameters '
|
|
. $nbCron . ','
|
|
. $action . ','
|
|
. $spy . ','
|
|
. $spy2 . ','
|
|
. $kill . ','
|
|
);
|
|
}
|
|
|
|
|
|
// kill
|
|
if ($kill) {
|
|
if (Configuration::getGlobalValue('ECI_USTK_STATE') != 'done') {
|
|
Configuration::updateGlobalValue('ECI_USTK_ACT', 'die');
|
|
}
|
|
exit('willdiesoon');
|
|
}
|
|
|
|
|
|
// espion
|
|
if ($spy) {
|
|
sleep(14);
|
|
$state = Configuration::getGlobalValue('ECI_USTK_STATE');
|
|
$progress = Configuration::getGlobalValue('ECI_USTK_PROGRESS');
|
|
if ($nbCron == $progress) {
|
|
if ($spy2) {
|
|
if ($state != 'done') {
|
|
Configuration::updateGlobalValue('ECI_USTK_STATE', 'still');
|
|
}
|
|
} else {
|
|
followLink($base_uri . '&spy=1&spytwo=1&nbC=' . $progress);
|
|
}
|
|
} else {
|
|
followLink($base_uri . '&spy=1&nbC=' . $progress);
|
|
}
|
|
exit('spywillreturn');
|
|
}
|
|
|
|
|
|
// abandon ou initialisation
|
|
$etat = Configuration::getGlobalValue('ECI_USTK_STATE');
|
|
$starting = is_null($paramSpy) & is_null($paramNbCron) & is_null($paramKill) & is_null($paramAct);
|
|
if (!$starting && ($action === 'die')) {
|
|
// abandon demandé par un kill
|
|
Configuration::updateGlobalValue('ECI_USTK_STATE', 'done');
|
|
Configuration::updateGlobalValue('ECI_USTK_END_TIME', date('Y-m-d H:i:s'));
|
|
Configuration::updateGlobalValue('ECI_USTK_ACT', 'go');
|
|
exit('dead');
|
|
}
|
|
if ($starting && ($etat === 'running')) {
|
|
// tentative de double lancement à éviter
|
|
$progress = Configuration::getGlobalValue('ECI_USTK_PROGRESS');
|
|
// envoi d'espion pour déjouer un plantage de serveur pendant une mise à jour
|
|
followLink($base_uri . '&spy=1&nbC=' . (int) $progress);
|
|
exit('nodoublestartplease');
|
|
}
|
|
if (!$starting && ($etat === 'still')) {
|
|
// un espion a pensé à tort qu'on était planté mais on est là !
|
|
Configuration::updateGlobalValue('ECI_USTK_STATE', 'running');
|
|
followLink($base_uri . '&spy=1&nbC=' . $nbCron.'');
|
|
}
|
|
if ($starting) {
|
|
// initialisation du process
|
|
$aShops = Shop::getShops(true, null, true);
|
|
asort($aShops);
|
|
$listShops = array_values($aShops);
|
|
Configuration::updateGlobalValue('ECI_USTK_START_TIME', date('Y-m-d H:i:s'));
|
|
Configuration::updateGlobalValue('ECI_USTK_END_TIME', '');
|
|
Configuration::updateGlobalValue('ECI_USTK_SHOPS_TODO', implode(',', $listShops));
|
|
Configuration::updateGlobalValue('ECI_USTK_SHOP', reset($listShops));
|
|
Configuration::updateGlobalValue('ECI_USTK_STAGE', $listStages['1']);
|
|
Configuration::updateGlobalValue('ECI_USTK_PROGRESS', 0);
|
|
Configuration::updateGlobalValue('ECI_USTK_LOOPS', 0);
|
|
Configuration::updateGlobalValue('ECI_USTK_STATE', 'running');
|
|
Configuration::updateGlobalValue('ECI_USTK_ACT', 'go');
|
|
// lancement de l'espion
|
|
followLink($base_uri . '&spy=1&nbC=0');
|
|
} else {
|
|
Configuration::updateGlobalValue('ECI_USTK_PROGRESS', $nbCron);
|
|
}
|
|
$listShopsTodo = explode(',', Configuration::getGlobalValue('ECI_USTK_SHOPS_TODO'));
|
|
//$id_shop = (int) Configuration::getGlobalValue('ECI_USTK_SHOP');
|
|
$stage = Configuration::getGlobalValue('ECI_USTK_STAGE');
|
|
|
|
|
|
// gestion des reprises, ruptures, fin
|
|
if ($action === 'next') {
|
|
$action = 'go';
|
|
Configuration::updateGlobalValue('ECI_USTK_ACT', 'go');
|
|
|
|
$numStage = array_search($stage, $listStages, true);
|
|
$keys = array_keys($listStages);
|
|
$next = $nextKey = false;
|
|
foreach ($keys as $key) {
|
|
if ($next) {
|
|
$nextKey = $key;
|
|
break;
|
|
}
|
|
if ($numStage == $key) {
|
|
$next = true;
|
|
}
|
|
}
|
|
|
|
if ($nextKey) {
|
|
$stage = $listStages[$nextKey];
|
|
$nbCron = 0;
|
|
} else {
|
|
//$ShopJustDone = array_shift($listShopsTodo);
|
|
// if (count($listShopsTodo) > 0) {
|
|
// $id_shop = reset($listShopsTodo);
|
|
// $stage = $listStages['1'];
|
|
// $nbCron = 0;
|
|
// } else {
|
|
Configuration::updateGlobalValue('ECI_USTK_SHOPS_TODO', '');
|
|
Configuration::updateGlobalValue('ECI_USTK_STATE', 'done');
|
|
Configuration::updateGlobalValue('ECI_USTK_END_TIME', date('Y-m-d H:i:s'));
|
|
//chaining with other task
|
|
|
|
exit('alldone');
|
|
//}
|
|
}
|
|
Configuration::updateGlobalValue('ECI_USTK_SHOPS_TODO', implode(',', $listShopsTodo));
|
|
Configuration::updateGlobalValue('ECI_USTK_SHOP', $id_shop);
|
|
Configuration::updateGlobalValue('ECI_USTK_STAGE', $stage);
|
|
Configuration::updateGlobalValue('ECI_USTK_LOOPS', 0);
|
|
Configuration::updateGlobalValue('ECI_USTK_PROGRESS', 0);
|
|
}
|
|
|
|
|
|
if ($logging) {
|
|
$logger->logInfo($who . ' do ' . $stage . ',' . $action);
|
|
}
|
|
|
|
|
|
// aiguillage
|
|
switch ($stage) {
|
|
case $listStages['1']:
|
|
$reps = stage1($stage);
|
|
if ($reps === true) {
|
|
Configuration::updateGlobalValue('ECI_USTK_ACT', 'next');
|
|
followLink($base_uri . '&nbC=0&act=next');
|
|
} elseif (is_numeric($reps)) {
|
|
Configuration::updateGlobalValue('ECI_USTK_LOOPS', Configuration::getGlobalValue('ECI_USTK_LOOPS') + 1);
|
|
followLink($base_uri . '&nbC=' . $reps);
|
|
}
|
|
break;
|
|
|
|
case $listStages['2']:
|
|
$reps = stage2($nbCron, $id_supplier, $id_shop, $stopTime, $logger);
|
|
$logger->logInfo('valeur stage2 :' . $reps);
|
|
if ($reps === true) {
|
|
Configuration::updateGlobalValue('ECI_USTK_ACT', 'next');
|
|
followLink($base_uri . '&nbC=0&act=next');
|
|
} elseif (is_numeric($reps)) {
|
|
Configuration::updateGlobalValue('ECI_USTK_LOOPS', Configuration::getGlobalValue('ECI_USTK_LOOPS') + 1);
|
|
followLink($base_uri . '&nbC=' . $reps);
|
|
}
|
|
break;
|
|
|
|
default:
|
|
exit('dontunderstand');
|
|
}
|
|
|
|
if ($reps !== true && !is_numeric($reps)) {
|
|
if ($logging) {
|
|
$logger->logInfo($who . ', stage ' . $stage . ', ' . $reps);
|
|
}
|
|
}
|
|
|
|
|
|
exit('Bye');
|
|
|
|
|
|
function stage1($stage)
|
|
{
|
|
//tell the calling process that we begin working thus avoiding error 500
|
|
echo 'Task successfully started. ';
|
|
|
|
return true;
|
|
}
|
|
|
|
function stage2($nbCron, $id_supplier, $id_shop, $stopTime, $logger)
|
|
{
|
|
$mod = new Ecstock();
|
|
$id_lang = (int)$mod->getIdLang();
|
|
//$id_shop = (int)$mod->getIdShop();
|
|
Shop::setContext(Shop::CONTEXT_SHOP, (int) $id_shop);
|
|
Context::getContext()->employee = 1;
|
|
Context::getContext()->shop->id = (int) $id_shop;
|
|
$logger->logInfo('ID SHOP :' . $id_shop);
|
|
// if (!Shop::isFeatureActive()) {
|
|
// $id_shop = 0;
|
|
// }
|
|
if ($logger) {
|
|
$logger->logInfo('Nbcon ' . $nbCron);
|
|
}
|
|
if ($nbCron == 0) {
|
|
$logger->logInfo('test : ');
|
|
$stockmin = (Configuration::get('ECSTOCK_STOCKMIN')!=''?Configuration::get('ECSTOCK_STOCKMIN'):0);
|
|
$stockmax = (Configuration::get('ECSTOCK_STOCKMAX')!=''?Configuration::get('ECSTOCK_STOCKMAX'):0);
|
|
$reference = (Configuration::get('ECSTOCK_REFERENCE')!=''?Configuration::get('ECSTOCK_REFERENCE'):0);
|
|
$product = (Configuration::get('ECSTOCK_PRODUCT')!=''?Configuration::get('ECSTOCK_PRODUCT'):0);
|
|
$req = 'SELECT pa.reference as productref, p.id_product, st.quantity, st.id_product_attribute, pl.name, p.reference as refprod, p.location as locprod, pa.location, p.price
|
|
FROM `'._DB_PREFIX_.'product` p, `'._DB_PREFIX_.'stock_available` st, `'._DB_PREFIX_.'product_lang` pl, `'._DB_PREFIX_.'product_attribute` pa
|
|
WHERE 1=1 AND p.id_product = st.id_product AND p.id_product = pl.id_product
|
|
AND pa.id_product_attribute = st.id_product_attribute';
|
|
if ($stockmin != 0) {
|
|
$req.=' AND st.quantity >= '.(int)$stockmin;
|
|
}
|
|
|
|
if ($stockmax != 0) {
|
|
$req.=' AND st.quantity <= '.(int)$stockmax;
|
|
}
|
|
|
|
if ($reference != '') {
|
|
$req.=' AND pa.reference like "'.pSQL(Configuration::get('ECSTOCK_REFERENCE')).'%"';
|
|
}
|
|
|
|
if ($product != 0 && $product != 1) {
|
|
if ($product == 2) {
|
|
$req.=' AND p.active = 1';
|
|
} else {
|
|
$req.=' AND p.active = 0';
|
|
}
|
|
}
|
|
$req .= ' AND id_lang = '.(int)$id_lang.' and st.id_shop = '.(int)$id_shop.' and pl.id_shop = '.(int)$id_shop.' ORDER BY p.id_product';
|
|
$logger->logInfo('requete attribute : '.$req);
|
|
$resreq = Db::getInstance()->ExecuteS($req);
|
|
|
|
|
|
|
|
$req = 'SELECT distinct p.id_product, st.quantity, st.id_product_attribute, pl.name, p.reference as reffff, p.location, p.price
|
|
FROM `'._DB_PREFIX_.'product` p, `'._DB_PREFIX_.'stock_available` st, `'._DB_PREFIX_.'product_lang` pl
|
|
WHERE 1=1 AND p.id_product = st.id_product AND p.id_product = pl.id_product AND st.id_product_attribute = 0';
|
|
if ($stockmin != 0) {
|
|
$req.=' AND st.quantity >= '.(int)$stockmin;
|
|
}
|
|
|
|
if ($stockmax != 0) {
|
|
$req.=' AND st.quantity <= '.(int)$stockmax;
|
|
}
|
|
|
|
if ($reference != '') {
|
|
$req.=' AND p.reference like "'.pSQL(Configuration::get('ECSTOCK_REFERENCE')).'%"';
|
|
}
|
|
|
|
if ($product != 0 && $product != 1) {
|
|
if ($product == 2) {
|
|
$req.=' AND p.active = 1';
|
|
} else {
|
|
$req.=' AND p.active = 0';
|
|
}
|
|
}
|
|
$req .= ' AND id_lang = '.(int)$id_lang.' and st.id_shop = '.(int)$id_shop.' and pl.id_shop = '.(int)$id_shop.' ORDER BY p.id_product';
|
|
$resreq2 = Db::getInstance()->ExecuteS($req);
|
|
foreach ($resreq2 as $key => $value) {
|
|
$resreq[] = $value;
|
|
}
|
|
|
|
$logger->logInfo('requete id_product_attribute = 0 : '.$req);
|
|
|
|
$resreq = Tools::jsonEncode($resreq);
|
|
$file = dirname(__FILE__).'/file/data.json';
|
|
$fichier = fopen($file, 'w+');
|
|
fwrite($fichier, $resreq);
|
|
fclose($fichier);
|
|
|
|
$configfile = dirname(__FILE__).'/file/config.json';
|
|
$dataconfig = array(
|
|
'ECSTOCK_SALEDATEMIN' => (Configuration::get('ECSTOCK_SALEDATEMIN')!=''?Configuration::get('ECSTOCK_SALEDATEMIN'):null),
|
|
'ECSTOCK_SALEDATEMAX' => (Configuration::get('ECSTOCK_SALEDATEMAX')!=''?Configuration::get('ECSTOCK_SALEDATEMAX'):null),
|
|
'ECSTOCK_EXPORTFTP' => Configuration::get('ECSTOCK_EXPORTFTP'),
|
|
'ECSTOCK_EXPORT_MAILOK' => Configuration::get('ECSTOCK_EXPORT_MAILOK'),
|
|
'ECSTOCK_EXPORTMAIL' => Configuration::get('ECSTOCK_EXPORTMAIL'),
|
|
'ECSTOCK_EXPORTHOTE' => Configuration::get('ECSTOCK_EXPORTHOTE'),
|
|
'ECSTOCK_EXPORTLOGIN' => Configuration::get('ECSTOCK_EXPORTLOGIN'),
|
|
'ECSTOCK_EXPORTPASSWORD' => Configuration::get('ECSTOCK_EXPORTPASSWORD'),
|
|
'ECSTOCK_EXPORTCHEM' => Configuration::get('ECSTOCK_EXPORTCHEM'),
|
|
);
|
|
$dataconfig = Tools::jsonEncode($dataconfig);
|
|
$fichierconfig = fopen($configfile, 'w+');
|
|
fwrite($fichierconfig, $dataconfig);
|
|
fclose($fichierconfig);
|
|
}
|
|
$fStock = dirname(__FILE__) . '/file/1.csv';
|
|
$json_source = Tools::file_get_contents(dirname(__FILE__).'/file/data.json');
|
|
$exreq = Tools::jsonDecode($json_source, true);
|
|
$config = Tools::jsonDecode(Tools::file_get_contents(dirname(__FILE__).'/file/config.json'), true);
|
|
$jobLine = 0;
|
|
$csv = '';
|
|
while ($jobLine<count($exreq)) {
|
|
if ($nbCron == 0 && $jobLine == 0) {
|
|
$csv.="id_product;id_product_attribute;reference;product name;stock;sales with filter; sales of the year; all sales;\n";
|
|
}
|
|
if (($stopTime < time()) && ($jobLine > $nbCron)) {
|
|
//fclose($handle);
|
|
$fp = fopen(dirname(__FILE__) . '/file/exportstock.csv', 'w');
|
|
fputs($fp, $csv);
|
|
fclose($fp);
|
|
return $jobLine;
|
|
}
|
|
$jobLine++;
|
|
if (($jobLine < $nbCron)) {
|
|
continue;
|
|
}
|
|
$csv.=utf8_decode($exreq[$jobLine-1]['id_product']).';';
|
|
$csv.=utf8_decode($exreq[$jobLine-1]['id_product_attribute']).';';
|
|
if ($exreq[$jobLine-1]['id_product_attribute'] == 0) {
|
|
$csv.= utf8_decode($exreq[$jobLine-1]['reffff']).';';
|
|
} else {
|
|
$csv.= utf8_decode($exreq[$jobLine-1]['productref']).';';
|
|
}
|
|
|
|
//$csv.=($exreq[$jobLine-1]['paref'] == null ?utf8_decode($exreq[$jobLine-1]['reference']):utf8_decode($exreq[$jobLine-1]['paref'])).';';
|
|
$csv.=utf8_decode($exreq[$jobLine-1]['name']).';';
|
|
$csv.=utf8_decode($exreq[$jobLine-1]['quantity']).';';
|
|
$csv.=($config['ECSTOCK_SALEDATEMIN'] != null || $config['ECSTOCK_SALEDATEMAX'] != null?getSalesByDate($exreq[$jobLine-1]['id_product'], $exreq[$jobLine-1]['id_product_attribute'], (int)$id_shop, $config['ECSTOCK_SALEDATEMIN'], $config['ECSTOCK_SALEDATEMAX']):'').';';
|
|
$csv.=getYearSales($exreq[$jobLine-1]['id_product'], $exreq[$jobLine-1]['id_product_attribute'], (int)$id_shop).';';
|
|
$csv.=getSales($exreq[$jobLine-1]['id_product'], $exreq[$jobLine-1]['id_product_attribute'], (int)$id_shop, $logger).';';
|
|
$csv.="\n";
|
|
if (0 === ($jobLine % 50)) {
|
|
Configuration::updateGlobalValue('ECI_USTK_PROGRESS', $jobLine);
|
|
}
|
|
}
|
|
$dos = dirname(__FILE__) . '/file/exportstock.csv';
|
|
$fp = fopen($dos, 'w');
|
|
fputs($fp, $csv);
|
|
fclose($fp);
|
|
if ($config['ECSTOCK_EXPORT_MAILOK']) {
|
|
$fic = dirname(__FILE__) . '/file/exportstock.csv';
|
|
$fic = Tools::file_get_contents($fic);
|
|
$file_attachement = array();
|
|
$file_attachement['content'] = $fic;
|
|
$file_attachement['name'] = 'exportstock.csv';
|
|
$file_attachement['mime'] = 'application/csv';
|
|
$lstMa = explode(';', $config['ECSTOCK_EXPORTMAIL']);
|
|
foreach ($lstMa as $mma) {
|
|
Mail::Send(
|
|
(int)$id_lang,
|
|
'exportstock',
|
|
Mail::l('New stock export CSV', (int)$id_lang),
|
|
null,
|
|
$mma,
|
|
null,
|
|
Configuration::get('PS_SHOP_EMAIL'),
|
|
Configuration::get('PS_SHOP_NAME'),
|
|
$file_attachement,
|
|
null,
|
|
dirname(__FILE__).'/mails/',
|
|
null,
|
|
null
|
|
);
|
|
}
|
|
}
|
|
if ($config['ECSTOCK_EXPORTFTP']) {
|
|
$logger->logInfo('export FTP ');
|
|
$conn_id = ftp_connect(Configuration::get('ECSTOCK_EXPORTHOTE'));
|
|
$login_result = ftp_login($conn_id, Configuration::get('ECSTOCK_EXPORTLOGIN'), Configuration::get('ECSTOCK_EXPORTPASSWORD'));
|
|
$file = dirname(__FILE__) . '/file/exportstock.csv';
|
|
ftp_pasv($conn_id, true);
|
|
ftp_put($conn_id, Configuration::get('ECSTOCK_EXPORTCHEM'), $file, FTP_BINARY);
|
|
ftp_close($conn_id);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function getPIdsByReference($reference, $id_fournisseur = false)
|
|
{
|
|
$res = Db::getInstance()->getRow('
|
|
SELECT pa.id_product, pa.id_product_attribute
|
|
FROM ' . _DB_PREFIX_ . 'product_attribute pa ' .
|
|
(!$id_fournisseur ? '' : 'LEFT JOIN ' . _DB_PREFIX_ . 'product p ON pa.id_product = p.id_product') . '
|
|
WHERE pa.reference = "' . pSQL($reference) . '"' .
|
|
(!$id_fournisseur ? '' : ' AND p.id_supplier = ' . (int) $id_fournisseur));
|
|
if (isset($res['id_product_attribute'])) {
|
|
return $res;
|
|
}
|
|
|
|
$id = Db::getInstance()->getValue('
|
|
SELECT `id_product`
|
|
FROM `' . _DB_PREFIX_ . 'product`
|
|
WHERE `reference` = "' . pSQL($reference) . '"' .
|
|
(!$id_fournisseur ? '' : ' AND `id_supplier` = ' . (int) $id_fournisseur));
|
|
if ($id) {
|
|
return array('id_product' => $id, 'id_product_attribute' => 0);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
function followLink($link, $timeout = 4)
|
|
{
|
|
$link.='&token='.Tools::getValue('token').'&id_shop='.(int)Tools::getValue('id_shop');
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $link);
|
|
curl_setopt($ch, CURLOPT_HEADER, false);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
|
$result = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
return $result;
|
|
}
|
|
|
|
function getSales($id_product, $id_product_attribute, $id_shop, $logger)
|
|
{
|
|
$req = Db::getInstance()->executeS(
|
|
'SELECT product_quantity
|
|
FROM ' . _DB_PREFIX_ . 'orders o, '. _DB_PREFIX_ . 'order_detail od
|
|
WHERE o.id_order = od.id_order
|
|
AND product_id ='.(int)$id_product.'
|
|
AND product_attribute_id ='.(int)$id_product_attribute.'
|
|
AND o.id_shop = '.(int)$id_shop.'
|
|
'
|
|
);
|
|
$res = 0;
|
|
foreach ($req as $key => $value) {
|
|
$res+=$value['product_quantity'];
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
function getYearSales($id_product, $id_product_attribute, $id_shop)
|
|
{
|
|
|
|
$req = Db::getInstance()->executeS(
|
|
'SELECT product_quantity
|
|
FROM ' . _DB_PREFIX_ . 'orders o, '. _DB_PREFIX_ . 'order_detail od
|
|
WHERE o.id_order = od.id_order
|
|
AND date_add >="'.date('Y').'"
|
|
AND product_id ='.(int)$id_product.'
|
|
AND product_attribute_id ='.(int)$id_product_attribute.'
|
|
AND o.id_shop = '.(int)$id_shop.'
|
|
'
|
|
);
|
|
$res = 0;
|
|
foreach ($req as $key => $value) {
|
|
$res+=$value['product_quantity'];
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
function getSalesByDate($id_product, $id_product_attribute, $id_shop, $datedebut = null, $datefin = null)
|
|
{
|
|
|
|
$req =
|
|
'
|
|
SELECT product_quantity
|
|
FROM ' . _DB_PREFIX_ . 'orders o, '. _DB_PREFIX_ . 'order_detail od
|
|
WHERE o.id_order = od.id_order
|
|
AND product_id ='.(int)$id_product.'
|
|
AND product_attribute_id ='.(int)$id_product_attribute.'
|
|
AND o.id_shop = '.(int)$id_shop.'
|
|
';
|
|
|
|
if ($datedebut != null) {
|
|
$req.=' AND date_add >="'.$datedebut.'"';
|
|
}
|
|
|
|
if ($datefin != null) {
|
|
$req.=' AND date_add <="'.$datefin.'"';
|
|
}
|
|
|
|
$req = Db::getInstance()->executeS($req);
|
|
$res = 0;
|
|
foreach ($req as $key => $value) {
|
|
$res+=$value['product_quantity'];
|
|
}
|
|
return $res;
|
|
}
|