continuing packs

This commit is contained in:
Marion Muszynski 2016-12-02 15:34:57 +01:00
parent 0dc8db8d58
commit 192cd79a6b
3 changed files with 122 additions and 150 deletions

View File

@ -1,7 +1,7 @@
<?php
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
// $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
// $_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
include('../../../config/settings.inc.php');
include('../../../config/config.inc.php');
@ -27,7 +27,7 @@
$format = $CRP;
if($magistorModule->active) {
require_once('connection_ftp.php');
//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...
@ -43,12 +43,13 @@
$datFile = $fileinfo->getPath() . '/' . preg_replace( '@BAL$@', 'DAT', $fileinfo->getFilename() );
if( file_exists( $datFile ) ) {
$order_details = array();
$details = array();
$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) {
@ -69,60 +70,53 @@
} else {
$id_order = (int) $order;
}
if(!isset($details[(int)$id_order])) {
$details[(int)$id_order] = array();
}
// GET PRODUCT
// 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);
$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(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]);
}
if(!$is_pack && $detail = Db::getInstance()->getRow('
SELECT `id_order_detail`, `product_quantity` - GREATEST(`product_quantity_return`, `product_quantity_refunded`) AS `quantity`
}
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.'
AND `product_id` = '.(int) $id_product.'
AND `product_attribute_id` = '.(int) $id_product_attribute.'
')) {
') as $key => $row) {
if ($id_sale && !isset($order_details[$id_sale])) {
$order_details[$id_sale] = array();
$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;
}
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;
}
}
}
}
@ -177,8 +171,8 @@
copy($datFile, $repo_archive . preg_replace( '@BAL$@', 'DAT', $fileinfo->getFilename() ));
unlink($inFolder.$fileinfo->getFilename());
unlink($datFile);
// unlink($inFolder.$fileinfo->getFilename());
// unlink($datFile);
}
}
}

View File

@ -1,6 +1,6 @@
<?php
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
//$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
//$_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
//include(dirname(__FILE__) . '/../../../config/settings.inc.php');
include(dirname(__FILE__) . '/../../../config/config.inc.php');
@ -8,8 +8,9 @@ include( dirname(__FILE__) . '/../philea_magistor.php');
ini_set('memory_limit', '4G');
$id_sale = (int) $argv[1];
//$id_sale = (int) $argv[1];
$id_sale = 3966;
echo $id_sale."\n";
$_id_shipping = 1; // philea
if($id_sale == 0) {
@ -73,13 +74,14 @@ if($magistorModule->active) {
LEFT JOIN `'._DB_PREFIX_.'order_detail` d ON o.`id_order` = d.`id_order`
LEFT JOIN `'._DB_PREFIX_.'product_ps_cache` c ON d.`product_id` = c.`id_product`
LEFT JOIN `'._DB_PREFIX_.'philea_sent` pms ON (pms.`id_sale` = c.`id_sale` AND pms.`id_order` = o.`id_order`)
WHERE o.`id_order_state` IN (2, 3, 4, 13, 17, 9, 18, 19)
WHERE o.`id_order_state` IN (2, 3, 12, 13, 17)
AND c.`id_sale` = '.(int) $id_sale.'
AND pms.`id_order` IS NULL
AND d.`product_quantity` - d.`product_quantity_refunded` > 0
') as $row) {
$include_orders[] = (int) $row['id_order'];
}
echo '<pre>';var_dump($include_orders);echo "</pre>\n";
if(count($include_orders) == 0) {
exit;
@ -122,6 +124,7 @@ if($magistorModule->active) {
$fileName = dirname(__FILE__) . '/OUT/CDC02' . $dateNow;
$repo_archive = dirname(__FILE__) . '/archives/OUT/CMD/';
echo $repo_archive."\n";
$repo_paths = array(date('Y'), date('m'));
foreach ($repo_paths as $repo_path) {
$repo_archive .= $repo_path . '/';
@ -130,37 +133,26 @@ if($magistorModule->active) {
}
$fileArchive = $repo_archive . 'CDC02' . date('ymdHis');
/**
* @Override get pack items
*/
$pack_items = array();
foreach(Db::getInstance()->executeS('
SELECT pp.`id_product_pack`, pp.`quantity` AS `pack_quantity`, p.*, pl.*, s.*
FROM `'._DB_PREFIX_.'pack` pp
LEFT JOIN `'._DB_PREFIX_.'product` p ON (pp.`id_product_item` = p.`id_product`)
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
ON (
p.`id_product` = pl.`id_product`
AND pl.`id_lang` = '.(int)(Configuration::get('PS_LANG_DEFAULT')).'
)
LEFT JOIN `'._DB_PREFIX_.'product_ps_cache` c ON (c.`id_product` = pp.`id_product_pack`)
LEFT JOIN `'._DB_PREFIX_.'privatesale_shipping_sale` s (ON s.`id_sale` = c.`id_sale`)
WHERE s.`id_shipping` = '.(int) $_id_shipping.'
AND s.`id_sale` = '.(int) $id_sale.'
ORDER BY p.`id_product` ASC
') as $row){
if (!isset($pack_items[$row['id_product_pack']])) {
$pack_items[$row['id_product_pack']] = array();
}
$pack_items[$row['id_product_pack']][] = $row;
}
echo '<pre>';var_dump($orders);echo "</pre>\n";
$data = '';
foreach($orders as $o) {
$partialOrder = false;
$order = new Order($o['id_order']);
$sales = array();
$sales[(int)$id_sale] = (int) $id_sale;
// ANTADIS - check if multi and order already sent
$other_sales = Db::getInstance()->getValue('
SELECT COUNT(`id_sale`)
FROM `'._DB_PREFIX_.'philea_sent`
WHERE `id_order` = '.(int) $o['id_order'].'
');
if($other_sales > 0){
// order already sent to philea
continue;
}
$products = $order->getProducts();
// ANTADIS
$products_ids = array();
@ -317,6 +309,7 @@ if($magistorModule->active) {
$nb_ligne = 0;
$product_packs = array();
$product_rows = array();
echo '<pre>';var_dump($products);echo "</pre>\n";
foreach($products as $product)
{
if(!Db::getInstance()->getRow('
@ -325,81 +318,44 @@ if($magistorModule->active) {
WHERE `id_sale` = '.(int) $id_sale.'
AND `id_product` = '.(int) $product['product_id'].'
')) {
continue;
$other_sale_id = Db::getInstance()->getValue('
SELECT `id_sale`
FROM `'._DB_PREFIX_.'product_ps_cache`
WHERE `id_product` = '.(int) $product['product_id'].'
');
$sales[(int)$other_sale_id] = (int)$other_sale_id;
}
/**
* @Override get pack items
*/
if (isset($pack_items[$product['product_id']])
&& is_array($pack_items[$product['product_id']])
&& !empty($pack_items[$product['product_id']])
){
foreach ($pack_items[$product['product_id']] as $pack_item) {
$productObj = new Product($pack_item['id_product']);
if(Pack::isPack((int)$product['product_id'])) {
$pack_items = Pack::getCompleteSimplePack((int) $product['product_id']);
foreach ($pack_items as $pack_item) {
$ean = !empty($pack_item['ean13'])? $pack_item['ean13']: $pack_item['supplier_reference'];
$ref = $pack_item['id_product'];
// ANTADIS
$product_shipping = (int) Db::getInstance()->getValue('
SELECT s.`id_shipping`
FROM `'._DB_PREFIX_.'product_ps_cache` c
LEFT JOIN `'._DB_PREFIX_.'privatesale_shipping_sale` s
ON s.`id_sale` = c.`id_sale`
WHERE c.`id_product` = '.(int) $productObj->id.'
AND s.`id_sale` = '.(int) $id_sale.'
');
//
if($product_shipping === $_id_shipping)
{
$nb_ligne++;
$ean = !empty($pack_item['ean13'])? $pack_item['ean13']: $pack_item['supplier_reference'];
$ref = $pack_item['id_product'];;
$p_key = (int) $pack_item['id_product'];
if (!isset($product_packs[$p_key])) {
$product_packs[$p_key] = array(
str_replace(array("\r", "\n"), '', $ref),
0,
substr(utf8_decode(cleanChar($pack_item['name'])),0,50),
str_replace(array("\r", "\n"), "", $ean)
);
}
$product_packs[$p_key][1] += max($product['product_quantity'] - max($product['product_quantity_return'], $product['product_quantity_refunded']), 0) * $pack_item['pack_quantity'];
$p_key = (int) $pack_item['id_product'];
if (!isset($product_packs[$p_key])) {
$product_packs[$p_key] = array(
str_replace(array("\r", "\n"), '', $ref),
0,
substr(utf8_decode(cleanChar($pack_item['name'])),0,50),
str_replace(array("\r", "\n"), "", $ean)
);
}
else
$partialOrder = true;
$product_packs[$p_key][1] += max($product['product_quantity'] - max($product['product_quantity_return'], $product['product_quantity_refunded']), 0) * $pack_item['pack_quantity'];
}
} else {
$productObj = new Product($product['product_id']);
$ean = !empty($product['product_ean13'])? $product['product_ean13']: $product['product_supplier_reference'];
$ref = ($product['product_attribute_id']?$product['product_id'].'_'.$product['product_attribute_id']:$product['product_id']);
// ANTADIS
$product_shipping = (int) Db::getInstance()->getValue('
SELECT s.`id_shipping`
FROM `'._DB_PREFIX_.'product_ps_cache` c
LEFT JOIN `'._DB_PREFIX_.'privatesale_shipping_sale` s
ON s.`id_sale` = c.`id_sale`
WHERE c.`id_product` = '.(int) $productObj->id.'
AND s.`id_sale` = '.(int) $id_sale.'
');
//
if($product_shipping === $_id_shipping)
{
$nb_ligne++;
$ean = !empty($product['product_ean13'])? $product['product_ean13']: $product['product_supplier_reference'];
$ref = ($product['product_attribute_id']?$product['product_id'].'_'.$product['product_attribute_id']:$product['product_id']);
$p_key = ($product['product_attribute_id']?$product['product_id'].'_'.$product['product_attribute_id']:$product['product_id']);
$product_rows[(int) $p_key] = array(
str_replace(array("\r", "\n"), '', $ref),
max($product['product_quantity'] - max($product['product_quantity_return'], $product['product_quantity_refunded']), 0),
substr(utf8_decode(cleanChar($product['product_name'])),0,50),
str_replace(array("\r", "\n"), "", $ean),
);
}
else
$partialOrder = true;
$p_key = ($product['product_attribute_id']?$product['product_id'].'_'.$product['product_attribute_id']:$product['product_id']);
$product_rows[(int) $p_key] = array(
str_replace(array("\r", "\n"), '', $ref),
max($product['product_quantity'] - max($product['product_quantity_return'], $product['product_quantity_refunded']), 0),
substr(utf8_decode(cleanChar($product['product_name'])),0,50),
str_replace(array("\r", "\n"), "", $ean),
);
}
}
@ -431,14 +387,16 @@ if($magistorModule->active) {
$data .= PHP_EOL;
}
Db::getInstance()->ExecuteS('
INSERT INTO `'._DB_PREFIX_.'philea_sent`
VALUES (
'.(int) $order->id.',
'.(int) $id_sale.',
NOW()
)
');
foreach ($sales as $key => $sale_id) {
Db::getInstance()->ExecuteS('
INSERT INTO `'._DB_PREFIX_.'philea_sent`
VALUES (
'.(int) $order->id.',
'.(int) $sale_id.',
NOW()
)
');
}
} //End if nbproducts
}//End foreach
@ -459,7 +417,7 @@ if($magistorModule->active) {
file_put_contents($fileArchive . '.DAT', "\xEF\xBB\xBF".utf8_encode($data));
require_once('connection_ftp.php');
//require_once('connection_ftp.php');
}
}

View File

@ -37,6 +37,26 @@ class Pack extends PackCore
');
return $_cache_getSimplePack[(int) $id_product_pack];
}
/**
* Get complete simple pack
*/
public static function getCompleteSimplePack($id_product_pack){
if (isset($_cache_getSimplePack[(int) $id_product_pack])) {
return $_cache_getSimplePack[(int) $id_product_pack];
}
$_cache_getSimplePack[(int) $id_product_pack] = Db::getInstance()->executeS('
SELECT pp.`id_product_pack`, pp.`quantity` AS `pack_quantity`, p.*, pl.*
FROM `'._DB_PREFIX_.'pack` pp
LEFT JOIN `'._DB_PREFIX_.'product` p ON (pp.`id_product_item` = p.`id_product`)
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
ON (
p.`id_product` = pl.`id_product`
AND pl.`id_lang` = '.(int)(Configuration::get('PS_LANG_DEFAULT')).'
)
WHERE pp.`id_product_pack` = ' . (int) $id_product_pack . '
');
return $_cache_getSimplePack[(int) $id_product_pack];
}
/**
* Get item pack