bebeboutik/modules/philea_magistor/script/recept_reception.php

170 lines
5.3 KiB
PHP
Raw Normal View History

<?php
$_SERVER['HTTP_HOST'] = 'www.bricoprive.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 = '@^CRR(.*)\.(BAL|DAT)@';
$magistorModule = new philea_magistor();
## FILE FORMAT
/**
* CHAMPS VALEURS COMMENTAIRES DEBUT LONGUEUR
* OP_CODE <EFBFBD> REC01 <EFBFBD> <EFBFBD> REC01 <EFBFBD> : balise fichier de r<EFBFBD>ception 1 10
* CODE_SOC ALPHA-NUMERIQUE Code soci<EFBFBD>t<EFBFBD> gestionnaire <EFBFBD> d<EFBFBD>finir 11 20
* N_PIECE ALPHA-NUMERIQUE Num<EFBFBD>ro de r<EFBFBD>ception ou num<EFBFBD>ro de commande d<EFBFBD>achat 31 20
* DATE_RECEP NUMERIQUE (YYYYMMDD) Date de r<EFBFBD>ception 51 8
* CODE_ART ALPHA-NUMERIQUE Code Article 59 50
* QTE NUMERIQUE Quantit<EFBFBD> r<EFBFBD>ellement r<EFBFBD>ceptionn<EFBFBD>e 109 10
* NO_LOT NUMERIQUE Num<EFBFBD>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<EFBFBD>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]);
if (isset($data[2]) && $data[2])
$sales[] = (int) str_replace('OP', '', $data[2]);
}
## UPDATE SUPPLIER_ORDER
// GET SALE ID
if( !isset($data[2]) || (isset($data[2]) && !$data[2]) )
continue;
$id_sale = (int) str_replace('OP', '', (string) trim($data[2]));
// GET PRODUCT
$product = trim($data[4]);
$product = explode('_', $product);
$id_product = (int) $product[0];
$id_product_attribute = (isset($product[1]) ? (int) $product[1] : 0);
// GET QTY
if (!isset($data[5]) || !$data[5])
continue;
$qty = (int) $data[5];
if (!$qty)
continue;
// GET ORDER FORM ID
// get id in file
$id_order_form = false;
// if (isset($data[9]) && $data[9])
// $id_order_form = (int) trim($data[9]);
// else get id from sale id
if (!$id_order_form){
$id_order_form = Db::getInstance()->getValue('
SELECT `id_order_form`
FROM `' . _DB_PREFIX_ . 'supplier_order`
WHERE `id_sale` = ' . (int) $id_sale);
}
if (!$id_order_form)
continue;
$update_sql = '
UPDATE `' . _DB_PREFIX_ . 'supplier_order_detail`
SET `quantity_accepted` = `quantity_accepted` + ' . (int)$qty . '
WHERE `id_order_form` = ' . (int) $id_order_form . '
AND `id_product` = ' . (int) $id_product . '
AND `id_product_attribute` = ' . (int) $id_product_attribute . '
LIMIT 1';
Db::getInstance()->execute($update_sql);
unset($data);
}
$sales = array_unique($sales);
## LOG RECEPTION FILE
$fp = fopen("reception.txt", "w");
fputs ($fp, $content);
fclose ($fp);
// $email = Configuration::get('PS_SHOP_EMAIL');
$email = 'coppee@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() );
$sql_insert = array();
foreach ($sales as $id_sale)
$sql_insert[] = '(' . (int) $id_sale . ', "' . pSQL($datfile_name) . '", NOW())';
$sql = 'INSERT INTO `'._DB_PREFIX_.'philea_magistor_reception` VALUES ' . implode(',', $sql_insert);
Db::getInstance()->execute($sql);
// CLEAN FILE
copy($datFile, $repo_archive . $datfile_name);
unlink($inFolder.$fileinfo->getFilename());
unlink($datFile);
}
}
}