179 lines
6.9 KiB
PHP
179 lines
6.9 KiB
PHP
<?php
|
||
|
||
$_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
|
||
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
||
$_SERVER['SERVER_PORT'] = 80;
|
||
|
||
include(dirname(__FILE__) . '/../../../config/config.inc.php');
|
||
include( dirname(__FILE__) . '/../philea_magistor.php');
|
||
|
||
@ini_set('display_errors', 'on');
|
||
|
||
global $regex_file_in;
|
||
$regex_file_in = '@^CRR(.*)\.(BAL|DAT)@';
|
||
|
||
$magistorModule = new philea_magistor();
|
||
|
||
## FILE FORMAT
|
||
/**
|
||
* CHAMPS VALEURS COMMENTAIRES DEBUT LONGUEUR
|
||
* OP_CODE « REC01 » « REC01 » : balise fichier de réception 1 10
|
||
* CODE_SOC ALPHA-NUMERIQUE Code société gestionnaire à définir 11 20
|
||
* N_PIECE ALPHA-NUMERIQUE Numéro de réception ou numéro de commande d’achat 31 20
|
||
* DATE_RECEP NUMERIQUE (YYYYMMDD) Date de réception 51 8
|
||
* CODE_ART ALPHA-NUMERIQUE Code Article 59 50
|
||
* QTE NUMERIQUE Quantité réellement réceptionnée 109 10
|
||
* NO_LOT NUMERIQUE Numéro de lot 119 20
|
||
* DATE_DLV NUMERIQUE (YYYYMMDD) Date limite de vente ou date de fabrication 139 8
|
||
* COMMT ALPHA-NUMERIQUE Commentaires / Litige 147 100
|
||
* NUM_BL ALPHA-NUMERIQUE Numéro du BL fournisseur si connu 247 50
|
||
*/
|
||
$CRR = array(
|
||
'OP_CODE' => array(1,10),
|
||
'CODE_SOC' => array(11,20),
|
||
'N_PIECE' => array(31,20),
|
||
'DATE_RECEP' => array(51,8),
|
||
'CODE_ART' => array(59,50),
|
||
'QTE' => array(109,10),
|
||
'NO_LOT' => array(119, 20),
|
||
'DATE_DLV' => array(139, 8),
|
||
'COMMT' => array(147, 100),
|
||
'NUM_BL' => array(247, 50),
|
||
);
|
||
|
||
$format = $CRR;
|
||
if($magistorModule->active) {
|
||
require_once('connection_ftp.php');
|
||
@set_time_limit(0);
|
||
|
||
$inFolder = dirname(__FILE__) . '/IN/';
|
||
|
||
$iterator = new DirectoryIterator($inFolder);
|
||
if(is_object($iterator) && count($iterator)) {
|
||
|
||
foreach ($iterator as $fileinfo) {
|
||
if (!$fileinfo->isFile() || $fileinfo->getFilename() == '..' || $fileinfo->getFilename() == '.'){
|
||
continue;
|
||
}
|
||
if( !preg_match( '@^CRR(.*)\.BAL@', $fileinfo->getFilename() ) ){
|
||
continue;
|
||
}
|
||
$datFile = $fileinfo->getPath() . '/' . preg_replace( '@BAL$@', 'DAT', $fileinfo->getFilename() );
|
||
if( !file_exists( $datFile ) ){
|
||
continue;
|
||
}
|
||
|
||
$content = file_get_contents( $datFile );
|
||
$lines = preg_split( '@\n@', $content );
|
||
if( empty($lines) ){
|
||
continue;
|
||
}
|
||
// GET DATA
|
||
$sales = array();
|
||
foreach( $lines as $line ) {
|
||
$data = array();
|
||
foreach($format as $field => $value) {
|
||
$data[] = substr($line, ($value[0]-1), $value[1]);
|
||
}
|
||
|
||
// GET SALE ID
|
||
if (!isset($data[2]) || (isset($data[2]) && !$data[2])){
|
||
continue;
|
||
}
|
||
|
||
$id_sale = (int) str_replace('OP', '', (string) trim($data[2]));
|
||
if (!isset($sales[$id_sale])) {
|
||
$query_stock = Db::getInstance()->getRow('
|
||
SELECT `quantity_expected`, `quantity_received`, `status`
|
||
FROM `'._DB_PREFIX_.'philea_stock_sync`
|
||
WHERE `id_sale`='.(int)$id_sale.'
|
||
');
|
||
if(!$query_stock){
|
||
continue;
|
||
}
|
||
$sales[$id_sale] = array(
|
||
'expected' => (Array)json_decode($query_stock['quantity_expected'],true),
|
||
'received' => (Array)json_decode($query_stock['quantity_received'],true),
|
||
'status' => ($query_stock['status'] == 0 ? 1:$query_stock['status'])
|
||
);
|
||
}
|
||
|
||
if (!isset($data[4]) || !$data[4] || !isset($data[5])){
|
||
continue;
|
||
}
|
||
$product = trim($data[4]);
|
||
$product = explode('_', $product);
|
||
$id_product = (int) $product[0];
|
||
$id_product_attribute = (isset($product[1]) ? (int) $product[1] : 0);
|
||
if($id_product_attribute!=0){
|
||
$ref = $id_product.'_'.$id_product_attribute;
|
||
} else {
|
||
$ref = $id_product;
|
||
}
|
||
|
||
// GET PRODUCT/QTY
|
||
if((int)$data[5] == 0){
|
||
$sales[$id_sale]['status'] = 2;
|
||
}
|
||
$sales[$id_sale]['received'][$ref] += (int)$data[5];
|
||
|
||
unset($data);
|
||
}
|
||
|
||
|
||
## LOG RECEPTION FILE
|
||
// $fp = fopen("reception.txt", "w");
|
||
// fputs ($fp, $content);
|
||
// fclose ($fp);
|
||
|
||
// $email = Configuration::get('PS_SHOP_EMAIL');
|
||
$email = 'marion@antadis.com';
|
||
// Mail::Send(
|
||
// Configuration::get('PS_LANG_DEFAULT'),
|
||
// 'philea_reception',
|
||
// 'Fichier de rŽception',
|
||
// array('{date}' => date('d/m/Y H:i:s')),
|
||
// $email,
|
||
// NULL,
|
||
// NULL,
|
||
// NULL,
|
||
// array(
|
||
// 'name' => 'reception.txt',
|
||
// 'mime' => 'text/plain',
|
||
// 'content' => file_get_contents('reception.txt')
|
||
// )
|
||
// );
|
||
|
||
// SAVE INTO ARCHIVE
|
||
$repo_archive = dirname(__FILE__) . '/archives/IN/RECEPTION/';
|
||
$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);
|
||
}
|
||
|
||
$datfile_name = preg_replace( '@BAL$@', 'DAT', $fileinfo->getFilename() );
|
||
|
||
$sales = array_unique($sales);
|
||
|
||
foreach ($sales as $id_sale => $detail) {
|
||
$query = Db::getInstance()->execute('
|
||
UPDATE `'._DB_PREFIX_.'philea_stock_sync`
|
||
SET
|
||
`date_upd` = NOW(),
|
||
`status` = '.(int)$detail['status'].',
|
||
`quantity_received` = "'.pSQL(json_encode($detail['received'])).'"
|
||
WHERE `id_sale` = '.(int)$id_sale.'
|
||
LIMIT 1
|
||
');
|
||
}
|
||
|
||
// CLEAN FILE
|
||
copy($datFile, $repo_archive . $datfile_name);
|
||
unlink($inFolder.$fileinfo->getFilename());
|
||
unlink($datFile);
|
||
}
|
||
}
|
||
}
|