bebeboutik/modules/philea_magistor/script/recept_preparation.php
2016-11-28 17:07:43 +01:00

187 lines
10 KiB
PHP

<?php
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
include('../../../config/settings.inc.php');
include('../../../config/config.inc.php');
@ini_set('display_errors', 'on');
include( dirname(__FILE__) . '/../philea_magistor.php');
global $regex_file_in;
$regex_file_in = '@^CRP(.*)\.(BAL|DAT)@';
$magistorModule = new philea_magistor();
$CRP = array(
'OP_CODE' => array(1,10),
'CODE_SOC' => array(11,20),
'N_CDE' => array(31,50),
'NO_CLIENT' => array(81,8), // NO_VENTE
'DATE_PREP' => array(89,8),
'NO_LIGNE' => array(97,4),
'NO_COLIS' => array(101,50), // N_CDE
'CODE_ART' => array(151,50),
'QTE' => array(201,10)
);
$format = $CRP;
if($magistorModule->active) {
require_once('connection_ftp.php');
@set_time_limit(0);
// Checking for ".bal" files with a "CRP" prefix that has an equivalent ".dat" file and process them...
$inFolder = dirname(__FILE__) . '/IN/';
$iterator = new DirectoryIterator($inFolder);
foreach ($iterator as $fileinfo) {
if ($fileinfo->isFile()) {
if( preg_match( '@^CRP(.*)\.BAL@', $fileinfo->getFilename() ) ) {
$datFile = $fileinfo->getPath() . '/' . preg_replace( '@BAL$@', 'DAT', $fileinfo->getFilename() );
if( file_exists( $datFile ) ) {
$content = file_get_contents( $datFile );
$lines = preg_split( '@\n@', $content );
// ANTADIS
if( !empty($lines) ) {
$order_details = array();
foreach( $lines as $line ) {
$data = array();
foreach($format as $field => $value) {
$data[] = substr($line, ($value[0]-1), $value[1]);
}
if( !isset($data[2]) || (isset($data[2]) && !$data[2]) )
continue;
// GET ORDER
$order = (string) trim($data[2]);
if(substr($order, 0, 2) == 'OP') {
$order = explode('-', $order);
if(count($order) < 2)
continue;
$id_sale = (int) str_replace('OP', '', $order[0]);
$id_order = (int) $order[1];
} else {
$id_order = (int) $order;
}
// GET PRODUCT
$product = trim($data[7]);
$product = explode('_', $product);
$id_product = (int) $product[0];
$id_product_attribute = (isset($product[1]) ? (int) $product[1] : 0);
$is_pack = false;
if (isset($order_details[$id_sale][$id_order][$id_product.'|'.$id_product_attribute])) {
if (Pack::isPacked((int) $id_product)){
$is_pack = true;
}
}
if(!$is_pack && $detail = Db::getInstance()->getRow('
SELECT `id_order_detail`, `product_quantity` - GREATEST(`product_quantity_return`, `product_quantity_refunded`) AS `quantity`
FROM `'._DB_PREFIX_.'order_detail`
WHERE `id_order` = '.(int) $id_order.'
AND `product_id` = '.(int) $id_product.'
AND `product_attribute_id` = '.(int) $id_product_attribute.'
')) {
if ($id_sale && !isset($order_details[$id_sale])) {
$order_details[$id_sale] = array();
}
if ($id_sale && !isset($order_details[$id_sale][$id_order])) {
$order_details[$id_sale][$id_order] = array();
}
// GET QTY
$qty = (int) trim($data[8]);
$order_details[$id_sale][$id_order][$id_product.'|'.$id_product_attribute] = (int) $detail['quantity'] - $qty;
} else {
if (Pack::isPacked((int) $id_product)) {
$packs = Pack::getPacks((int) $id_product);
foreach($packs as $pack) {
if (is_array($pack)) {
if($detail = Db::getInstance()->getRow('
SELECT `id_order_detail`, `product_quantity` - GREATEST(`product_quantity_return`, `product_quantity_refunded`) AS `quantity`
FROM `'._DB_PREFIX_.'order_detail`
WHERE `id_order` = '.(int) $id_order.'
AND `product_id` = '.(int) $pack['id_product_pack'].'
AND `product_attribute_id` = 0
')) {
if ($id_sale && !isset($order_details[$id_sale])) {
$order_details[$id_sale] = array();
}
if ($id_sale && !isset($order_details[$id_sale][$id_order])) {
$order_details[$id_sale][$id_order] = array();
}
// GET QTY
$qty = (int) trim($data[8]);
$order_details[$id_sale][$id_order][$pack['id_product_pack'].'|'.$id_product.'|1'] = ((int) $detail['quantity'] * (int) $pack['quantity']) - $qty;
}
}
}
}
}
}
foreach ($order_details as $sale => $orders) {
$comments = "";
foreach ($orders as $order => $products) {
$errors = "";
$count = 0;
foreach ($products as $key => $qty) {
$count += $qty;
$product = explode('|', $key);
$is_pack = (isset($product[2]) ? (int) $product[2] : 0);
if ($qty>0) {
if($is_pack) {
$id_pack = (int) $product[0];
$id_product = (isset($product[1]) ? (int) $product[1] : 0);
$errors .= "<li>".$qty." Produit #".$id_product." du Pack ".$id_pack." manquant</li>";
} else {
$id_product = (int) $product[0];
$id_product_attribute = (isset($product[1]) ? (int) $product[1] : 0);
$errors .= "<li>".$qty." Produit #".$id_product.($id_product_attribute?" - attribute #".$id_product_attribute:"")." manquant</li>";
}
}
}
$comments .= "<strong>Commande [".$order."] : ".($count>0? "<span style='color:red;'>Produits manquants</span>": "<span style='color:green;'>Commande préparée</span>")."</strong><br />";
if(!empty($errors)) {
$comments .= "<ul style='margin:0; padding:0 25px; list-style:none;'>".$errors."</ul>";
}
}
if ($sale) {
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'philea_syncreport`
VALUE (
'.(int) $sale.',
"'.pSQL(preg_replace('@BAL$@', 'DAT', $fileinfo->getFilename())).'",
NOW(),
NOW(),
"'.pSQL($comments, TRUE).'"
)
');
}
}
}
$repo_archive = './archives/IN/PREPARATION/';
$repo_paths = array(date('Y'), date('m'));
foreach ($repo_paths as $repo_path) {
$repo_archive .= $repo_path . '/';
if (!file_exists($repo_archive))
mkdir($repo_archive);
}
copy($datFile, $repo_archive . preg_replace( '@BAL$@', 'DAT', $fileinfo->getFilename() ));
unlink($inFolder.$fileinfo->getFilename());
unlink($datFile);
}
}
}
}
}