bebeboutik/modules/philea_magistor/script/recept_preparation.php
2017-12-11 11:27:44 +01:00

179 lines
9.9 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 ) ) {
$order_details = array();
$details = array();
$OP = array();
$content = file_get_contents( $datFile );
$lines = preg_split( '@\n@', $content );
// ANTADIS
if( !empty($lines) ) {
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;
}
if(!isset($details[(int)$id_order])) {
$details[(int)$id_order] = array();
}
if(!isset($OP[(int)$id_order])) {
$OP[(int)$id_order] = $id_sale;
}
// GET PRODUCT DETAIL FROM FILE
$product = trim($data[7]);
$product = explode('_', $product);
$id_product = (int) $product[0];
$id_product_attribute = (isset($product[1]) ? (int) $product[1] : 0);
if(isset($details[(int)$id_order][$id_product.'_'.$id_product_attribute])) {
$details[(int)$id_order][$id_product.'_'.$id_product_attribute] += (int) trim($data[8]);
} else {
$details[(int)$id_order][$id_product.'_'.$id_product_attribute] = (int) trim($data[8]);
}
}
foreach ($details as $id_order => $detail) {
foreach (Db::getInstance()->ExecuteS('
SELECT `id_order_detail`,`product_id`,`product_attribute_id`, `product_quantity` - GREATEST(`product_quantity_return`, `product_quantity_refunded`) AS `quantity`
FROM `'._DB_PREFIX_.'order_detail`
WHERE `id_order` = '.(int) $id_order.'
') as $key => $row) {
$id_sale = (isset($OP[(int)$id_order]) && $OP[(int)$id_order]!=0)? (int)$OP[(int)$id_order]:$id_sale;
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();
}
if (Pack::isPack((int) $row['product_id'])){
$pack_items = Pack::getSimplePack((int) $row['product_id']);
foreach ($pack_items as $pack_item) {
$qty = (isset($details[(int)$id_order][$pack_item['id_product_item'].'_0']) ? $details[(int)$id_order][$pack_item['id_product_item'].'_0'] : 0);
if (isset($order_details[$id_sale][$id_order][$pack_item['id_product_item'].'|0'])) {
$order_details[$id_sale][$id_order][$pack_item['id_product_item'].'|0'] += ((int) $row['quantity'] * (int) $pack_item['quantity']);
} else {
$order_details[$id_sale][$id_order][$pack_item['id_product_item'].'|0'] = ((int) $row['quantity'] * (int) $pack_item['quantity']) - $qty;
}
}
} else {
$qty = (isset($details[(int)$id_order][$row['product_id'].'_'.$row['product_attribute_id']]) ? $details[(int)$id_order][$row['product_id'].'_'.$row['product_attribute_id']] : 0);
if(isset($order_details[$id_sale][$id_order][$row['product_id'].'|'.$row['product_attribute_id']])) {
$order_details[$id_sale][$id_order][$row['product_id'].'|'.$row['product_attribute_id']] += (int) $row['quantity'];
} else {
$order_details[$id_sale][$id_order][$row['product_id'].'|'.$row['product_attribute_id']] = (int) $row['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);
if ($qty>0) {
$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);
}
}
}
}
}