Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(E_ALL^E_NOTICE);
- define('SELF_CHECK',1);
- //ini_set('display_errors',1);
- //define(DOC_ROOT,"z:/home/v-ticket.dev/www/");
- //define(DOC_ROOT,'/usr/home/ekvitokcom/vhosts/v-ticket-test.com.ua/html/'); // test server
- define(DOC_ROOT,'/mnt/node_05_01/projects/vtick/vhosts/v-ticket.com.ua/trunk/'); // production
- require_once DOC_ROOT.'inc/_common.inc.php';
- $GLOBALS['nav'] = new navigation();
- $GLOBALS['lng']['id'] = 1;
- set_include_path(DOC_ROOT."classes/tools/");
- require_once DOC_ROOT."classes/tools/Mail/MailParser.php";
- $mailParser = new MailParser();
- $user = $mailParser->user;
- $pass = $mailParser->pass;
- $mbox = imap_open("{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX", "$user", "$pass");
- $msgCnt = imap_num_msg($mbox);
- if ($msgCnt <= 10 )
- $start = $msgCnt - 1;
- else
- $start = 10;
- for($j = $msgCnt - $start; $j <= $msgCnt; $j++){
- $struct['header'] = imap_fetchheader ($mbox,$j);
- $struct['body'] = imap_body($mbox, $j);
- $headers = $mailParser->decode_header($struct['header']);
- $type = $ctype = $headers['content-type'];
- $ctype = explode(";",$ctype);
- $types = explode("/",$ctype[0]);
- $maintype = trim(strtolower($types[0]));
- $subtype = trim(strtolower($types[1]));
- if ($maintype == "multipart" && $subtype == "mixed"){
- $boundary = $mailParser->get_boundary($ctype[1]);
- $part = $mailParser->split_parts($boundary, $struct['body']);
- for($i = 0; $i < count($part); $i++) {
- $email = $mailParser->fetch_structure($part[$i]);
- $header = $email["header"];
- $body = trim($email["body"]);
- $msgHeaders = $mailParser->decode_header($header);
- $msgType = explode(";",$msgHeaders['content-type']);
- $msgTypes = explode("/",$msgType[0]);
- $msgMaintype = trim(strtolower($msgTypes[0]));
- $msgSubtype = trim(strtolower($msgTypes[1]));
- if ($msgMaintype == "application" && $msgSubtype == "zip" || $msgSubtype == "octet-stream"){
- $is_download = preg_match("/name=/",$msgHeaders["content-disposition"].$msgHeaders["content-type"]);
- if ($is_download){
- $msgDisp = $msgHeaders["content-disposition"];
- $aDisp = explode(";",$msgDisp);
- $fname = $aDisp[1];
- if(preg_match("/filename=(.*)/",$fname,$regs))
- $filename = $regs[1];
- if($filename == "" && preg_match("/name=(.*)/",$msgType[1],$regs))
- $filename = $regs[1];
- $filename = preg_replace('/\"/', "", $filename);
- $filename = trim($mailParser->decode_mime_string($filename));
- $body = $mailParser->compile_body($body, $msgHeaders["content-transfer-encoding"], $msgHeaders['content-type']);
- if (file_exists(DOC_ROOT."dynamic/zip/".$filename)){
- unlink(DOC_ROOT."dynamic/zip/".$filename);
- }
- $ft = fopen(DOC_ROOT."dynamic/zip/".$filename,"wb");
- $bites = fwrite($ft,$body);
- if ($bites){
- echo "Archive is saved<br/>\r\n";
- $date = substr($filename, 0, 8);
- $zip = new ZipArchive;
- $zipName = DOC_ROOT.'dynamic/zip/'.$filename;
- if (file_exists($zipName)){
- if ($zip->open($zipName) === true){
- if (file_exists(DOC_ROOT.'dynamic/zip/reports/'.$date.'_CODreport.txt')){
- unlink(DOC_ROOT.'dynamic/zip/reports/'.$date.'_CODreport.txt');
- }
- if (file_exists(DOC_ROOT.'dynamic/zip/reports/'.$date.'_statuses.txt')){
- unlink(DOC_ROOT.'dynamic/zip/reports/'.$date.'_statuses.txt');
- }
- $zip->extractTo(DOC_ROOT.'dynamic/zip/reports');
- $zip->close();
- }else{
- echo "Failed to open archive\n";
- }
- }else{
- echo "Archive does not exist\n";
- }
- // Parsing txt file with reports
- $fileReport = DOC_ROOT.'dynamic/zip/reports/'.$date.'_CODreport.txt';
- $reportData = array();
- if (file_exists($fileReport)){
- $rfile = fopen($fileReport, 'r');
- if ($rfile)
- {
- while (!feof($rfile))
- {
- $arr = explode("|",fgets($rfile));
- $reportData[] = array_slice($arr, 0, 5);
- }
- fclose($rfile);
- }else{
- echo "Failed to open file\n";
- }
- }else{
- echo "File does not exist\n";
- }
- if (!empty($reportData) && (!end($reportData[count($reportData)-1]) || !end($reportData[count($reportData)-1][0]))){
- array_pop($reportData);
- }
- $reportKeys = array(
- 0 => 'shipments_ref',
- 1 => 'barcode',
- 2 => 'delivery_date',
- 3 => 'cod',
- 4 => 'cod_date',
- );
- // Remove special characters and adding to the database
- if (!empty($reportData[0])){
- foreach($reportData as $key => $repost){
- $reportData[$key] = array_combine($reportKeys,$repost);
- }
- foreach ($reportData as $key => &$report){
- static $counterRep = 1;
- $cod = '';
- for ($i = 0; $i < strlen($report['cod']); $i++){
- if (ord($report['cod']{$i}) != 194 && ord($report['cod']{$i}) != 160){
- $cod .= $report['cod']{$i};
- }
- }
- $report['cod'] = (int)$cod;
- $shipment = '';
- for ($i = 0; $i < strlen($report['shipments_ref']); $i++){
- if (ord($report['shipments_ref']{$i}) != 239 && ord($report['shipments_ref']{$i}) != 187 && ord($report['shipments_ref']{$i}) != 191){
- $shipment .= $report['shipments_ref']{$i};
- }
- }
- $report['shipments_ref'] = (int)$shipment;
- $report['delivery_date'] = date("Y-m-d H:i:s",strtotime($report['delivery_date']));
- $report['cod_date'] = date("Y-m-d H:i:s",strtotime($report['cod_date']));
- $report_id = $GLOBALS['dbh']->getOne("
- SELECT mer.id
- FROM t_most_express_report AS mer
- WHERE mer.shipments_ref = '".$report['shipments_ref']."'
- AND mer.barcode = '".$report['barcode']."'
- AND mer.delivery_date = '".$report['delivery_date']."'
- AND mer.cod = '".$report['cod']."'
- AND mer.cod_date = '".$report['cod_date']."'"
- );
- $res = false;
- if (empty($report_id)){
- $res = $GLOBALS['dbh']->autoExecute('t_most_express_report', array(
- 'shipments_ref' => $report['shipments_ref'],
- 'barcode' => $report['barcode'],
- 'delivery_date' => $report['delivery_date'],
- 'cod' => $report['cod'],
- 'cod_date' => $report['cod_date']
- ));
- if ($res){
- echo "In the table t_most_express_report added ".$counterRep." row<br/>\n";
- }else{
- echo "The row not was added to the table<br/>\n";
- }
- }else{
- echo "The table already exists a record<br/>\n";
- }
- $counterRep++;
- }
- }else{
- echo "File is empty<br/>\n";
- }
- // Parsing txt file with stutuses
- $fileStatus = DOC_ROOT.'dynamic/zip/reports/'.$date.'_statuses.txt';
- $stutusData = array();
- if (file_exists($fileStatus)){
- $sfile = fopen($fileStatus, 'r');
- if ($sfile)
- {
- while (!feof($sfile))
- {
- $arr = explode("|",fgets($sfile));
- $stutusData[] = array_slice($arr, 0, 5);
- }
- fclose($sfile);
- }else{
- echo "Failed to open file\n";
- }
- }else{
- echo "File does not exist\n";
- }
- if (!empty($stutusData) && (!end($stutusData[count($stutusData)-1]) || !end($stutusData[count($stutusData)-1][0]))){
- array_pop($stutusData);
- }
- $statusKeys = array(
- 0 => 'shipments_ref',
- 1 => 'barcode',
- 2 => 'statuscode',
- 3 => 'status_date_time',
- 4 => 'station_name',
- );
- if (!empty($stutusData[0])){
- foreach($stutusData as $key => $status){
- $stutusData[$key] = array_combine($statusKeys,$status);
- }
- foreach ($stutusData as &$status){
- static $counterStat = 1;
- $shipment = '';
- for ($i = 0; $i < strlen($status['shipments_ref']); $i++){
- if (ord($status['shipments_ref']{$i}) != 239 && ord($status['shipments_ref']{$i}) != 187 && ord($status['shipments_ref']{$i}) != 191){
- $shipment .= $status['shipments_ref']{$i};
- }
- }
- $status['shipments_ref'] = (int)$shipment;
- $status['status_date_time'] = date("Y-m-d H:i:s",strtotime($status['status_date_time']));
- $status_id = $GLOBALS['dbh']->getOne("
- SELECT mes.id
- FROM t_most_express_status AS mes
- WHERE mes.shipments_ref = '".checkData($status['shipments_ref'],'number')."'
- AND mes.barcode = '".checkData($status['barcode'],'text')."'
- AND mes.statuscode = '".checkData($status['statuscode'],'text')."'
- AND mes.status_date_time = '".checkData($status['status_date_time'],'text')."'
- AND mes.station_name = '".checkData($status['station_name'],'text')."'"
- );
- $res = false;
- if (empty($status_id)){
- $res = $GLOBALS['dbh']->autoExecute('t_most_express_status', array(
- 'shipments_ref' => $status['shipments_ref'],
- 'barcode' => $status['barcode'],
- 'statuscode' => $status['statuscode'],
- 'status_date_time' => $status['status_date_time'],
- 'station_name' => $status['station_name']
- ));
- if ($res){
- echo "In the table t_most_express_status added ".$counterStat." row<br/>\n";
- }else{
- echo "The row not was added to the table<br/>\n";
- }
- }else{
- echo "The table already exists a record<br/>\n";
- }
- $counterStat++;
- }
- }else{
- echo "File is empty<br/>\n";
- }
- }else{
- echo "Archive is not saved<br/>\r\n";
- }
- fclose($ft);
- }
- }
- }
- }else{
- echo "No message with an attachment<br>\n";
- }
- }
- imap_close($mbox);
- $paidStatuses = ginst('Order')->paidStatuses;
- $orderIds = $GLOBALS['dbh']->getCol(
- "SELECT DISTINCT t_order.id
- FROM t_order
- LEFT JOIN t_event_content ON (t_event_content.id = t_order.event_id AND t_event_content.language_id = 1)
- LEFT JOIN t_ticket ON (t_ticket.order_id = t_order.id)
- LEFT JOIN t_user ON (t_user.id = t_ticket.user_id)
- WHERE t_order.delivery_type = 2 AND paid_status = 8"
- );
- if(!empty($orderIds)){
- $deliveryInfo = array();
- foreach ($orderIds as $id){
- $deliveryInfo = $GLOBALS['dbh']->getRow("
- SELECT
- d.order_id,
- mes.statuscode,
- mec.name_ru
- FROM
- t_delivery AS d
- LEFT JOIN
- t_most_express_status AS mes ON mes.shipments_ref = d.order_id
- JOIN
- t_most_express_content AS mec ON mec.code = mes.statuscode
- WHERE
- d.order_id = '".$id."' AND statuscode IN ('000000027', '000000010', '000000015', '000000028')
- ORDER BY
- mes.status_date_time DESC
- LIMIT 1
- ");
- if (!empty($deliveryInfo)){
- if ($deliveryInfo['statuscode'] == '000000027'){
- $orderInfo = ginst('Order')->get($deliveryInfo['order_id']);
- if ($orderInfo['paid_status'] != 6){
- ginst('Order')->apply($deliveryInfo['order_id']);
- if (!ginst('Order')->checkLog($deliveryInfo['order_id'])){
- ginst('Order')->addLog(array(
- 'order_id' => $deliveryInfo['order_id'],
- 'action' => 'confirm-order',
- 'info' => array(
- 'status' => $paidStatuses[$orderInfo['paid_status']]['title']
- ),
- 'user_id' => 41518
- ));
- }
- }
- }
- if ($deliveryInfo['statuscode'] == '000000010' || $deliveryInfo['statuscode'] == '000000015' || $deliveryInfo['statuscode'] == '000000028'){
- $orderInfo = ginst('Order')->get($deliveryInfo['order_id']);
- ginst('Order')->cancel($orderInfo, $deliveryInfo['name_ru']);
- }
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement