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(); $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(); } // 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) { 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 .= "