Advertisement
ThisYouRick

Untitled

Apr 28th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.58 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ALL^E_NOTICE);
  3. define('SELF_CHECK',1);
  4. //ini_set('display_errors',1);
  5. //define(DOC_ROOT,"z:/home/v-ticket.dev/www/");
  6. //define(DOC_ROOT,'/usr/home/ekvitokcom/vhosts/v-ticket-test.com.ua/html/'); // test server
  7. define(DOC_ROOT,'/mnt/node_05_01/projects/vtick/vhosts/v-ticket.com.ua/trunk/'); // production
  8. require_once DOC_ROOT.'inc/_common.inc.php';
  9. $GLOBALS['nav'] = new navigation();
  10. $GLOBALS['lng']['id'] = 1;
  11. set_include_path(DOC_ROOT."classes/tools/");
  12. require_once DOC_ROOT."classes/tools/Mail/MailParser.php";
  13.  
  14. $mailParser = new MailParser();
  15. $user = $mailParser->user;
  16. $pass = $mailParser->pass;
  17.  
  18. $mbox = imap_open("{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX", "$user", "$pass");
  19. $msgCnt = imap_num_msg($mbox);
  20. if ($msgCnt <= 10 )
  21.     $start = $msgCnt - 1;
  22. else
  23.     $start = 10;
  24.  
  25. for($j = $msgCnt - $start; $j <= $msgCnt; $j++){
  26.     $struct['header'] = imap_fetchheader ($mbox,$j);
  27.     $struct['body'] = imap_body($mbox, $j);
  28.     $headers = $mailParser->decode_header($struct['header']);
  29.    
  30.     $type = $ctype = $headers['content-type'];
  31.     $ctype = explode(";",$ctype);
  32.     $types = explode("/",$ctype[0]);
  33.     $maintype = trim(strtolower($types[0]));
  34.     $subtype = trim(strtolower($types[1]));
  35.  
  36.     if ($maintype == "multipart" && $subtype == "mixed"){
  37.         $boundary = $mailParser->get_boundary($ctype[1]);
  38.         $part = $mailParser->split_parts($boundary, $struct['body']);
  39.         for($i = 0; $i < count($part); $i++) {
  40.             $email = $mailParser->fetch_structure($part[$i]);
  41.             $header = $email["header"];
  42.             $body = trim($email["body"]);
  43.             $msgHeaders = $mailParser->decode_header($header);
  44.             $msgType = explode(";",$msgHeaders['content-type']);
  45.             $msgTypes = explode("/",$msgType[0]);
  46.             $msgMaintype = trim(strtolower($msgTypes[0]));
  47.             $msgSubtype = trim(strtolower($msgTypes[1]));
  48.             if ($msgMaintype == "application" && $msgSubtype == "zip" || $msgSubtype == "octet-stream"){
  49.                 $is_download = preg_match("/name=/",$msgHeaders["content-disposition"].$msgHeaders["content-type"]);
  50.                 if ($is_download){
  51.                     $msgDisp = $msgHeaders["content-disposition"];
  52.                     $aDisp =  explode(";",$msgDisp);
  53.                     $fname = $aDisp[1];
  54.                     if(preg_match("/filename=(.*)/",$fname,$regs))
  55.                         $filename = $regs[1];
  56.                     if($filename == "" && preg_match("/name=(.*)/",$msgType[1],$regs))
  57.                         $filename = $regs[1];
  58.                     $filename = preg_replace('/\"/', "", $filename);
  59.                     $filename = trim($mailParser->decode_mime_string($filename));
  60.                     $body = $mailParser->compile_body($body, $msgHeaders["content-transfer-encoding"], $msgHeaders['content-type']);
  61.                     if (file_exists(DOC_ROOT."dynamic/zip/".$filename)){
  62.                         unlink(DOC_ROOT."dynamic/zip/".$filename);
  63.                     }
  64.                     $ft = fopen(DOC_ROOT."dynamic/zip/".$filename,"wb");
  65.                     $bites = fwrite($ft,$body);
  66.                     if ($bites){
  67.                         echo "Archive is saved<br/>\r\n";
  68.                         $date = substr($filename, 0, 8);
  69.                         $zip = new ZipArchive;
  70.                         $zipName = DOC_ROOT.'dynamic/zip/'.$filename;
  71.                         if (file_exists($zipName)){
  72.                             if ($zip->open($zipName) === true){
  73.                                 if (file_exists(DOC_ROOT.'dynamic/zip/reports/'.$date.'_CODreport.txt')){
  74.                                     unlink(DOC_ROOT.'dynamic/zip/reports/'.$date.'_CODreport.txt');
  75.                                 }
  76.                                 if (file_exists(DOC_ROOT.'dynamic/zip/reports/'.$date.'_statuses.txt')){
  77.                                     unlink(DOC_ROOT.'dynamic/zip/reports/'.$date.'_statuses.txt');
  78.                                 }
  79.                                 $zip->extractTo(DOC_ROOT.'dynamic/zip/reports');
  80.                                 $zip->close();
  81.                             }else{
  82.                                 echo "Failed to open archive\n";
  83.                             }
  84.                         }else{
  85.                             echo "Archive does not exist\n";
  86.                         }
  87.                         // Parsing txt file with reports
  88.                         $fileReport = DOC_ROOT.'dynamic/zip/reports/'.$date.'_CODreport.txt';
  89.  
  90.                         $reportData = array();
  91.                         if (file_exists($fileReport)){
  92.                             $rfile = fopen($fileReport, 'r');
  93.                             if ($rfile)
  94.                             {
  95.                                 while (!feof($rfile))
  96.                                 {
  97.                                     $arr = explode("|",fgets($rfile));
  98.                                     $reportData[] = array_slice($arr, 0, 5);
  99.                                 }
  100.                                 fclose($rfile);
  101.                             }else{
  102.                                 echo "Failed to open file\n";
  103.                             }
  104.                         }else{
  105.                             echo "File does not exist\n";
  106.                         }
  107.  
  108.                         if (!empty($reportData) && (!end($reportData[count($reportData)-1]) || !end($reportData[count($reportData)-1][0]))){
  109.                             array_pop($reportData);
  110.                         }
  111.  
  112.                         $reportKeys = array(
  113.                             0 => 'shipments_ref',
  114.                             1 => 'barcode',
  115.                             2 => 'delivery_date',
  116.                             3 => 'cod',
  117.                             4 => 'cod_date',
  118.                         );
  119.  
  120.  
  121.                         // Remove special characters and adding to the database
  122.                         if (!empty($reportData[0])){
  123.                             foreach($reportData as $key => $repost){
  124.                                 $reportData[$key] = array_combine($reportKeys,$repost);
  125.                             }
  126.                             foreach ($reportData as $key => &$report){
  127.                                 static $counterRep = 1;
  128.                                 $cod = '';
  129.                                 for ($i = 0; $i < strlen($report['cod']); $i++){
  130.                                     if (ord($report['cod']{$i}) != 194 && ord($report['cod']{$i}) != 160){
  131.                                         $cod .= $report['cod']{$i};
  132.                                     }  
  133.                                 }
  134.                                 $report['cod'] = (int)$cod;
  135.                                 $shipment = '';
  136.                                 for ($i = 0; $i < strlen($report['shipments_ref']); $i++){
  137.                                     if (ord($report['shipments_ref']{$i}) != 239 && ord($report['shipments_ref']{$i}) != 187 && ord($report['shipments_ref']{$i}) != 191){
  138.                                         $shipment .= $report['shipments_ref']{$i};
  139.                                     }  
  140.                                 }
  141.                                 $report['shipments_ref'] = (int)$shipment;
  142.                                 $report['delivery_date'] = date("Y-m-d H:i:s",strtotime($report['delivery_date']));
  143.                                 $report['cod_date'] = date("Y-m-d H:i:s",strtotime($report['cod_date']));
  144.  
  145.                                     $report_id = $GLOBALS['dbh']->getOne("
  146.                                         SELECT mer.id
  147.                                         FROM t_most_express_report AS mer
  148.                                         WHERE mer.shipments_ref = '".$report['shipments_ref']."'
  149.                                             AND mer.barcode = '".$report['barcode']."'
  150.                                             AND mer.delivery_date = '".$report['delivery_date']."'
  151.                                             AND mer.cod = '".$report['cod']."' 
  152.                                             AND mer.cod_date = '".$report['cod_date']."'"
  153.                                     );
  154.                                 $res = false;
  155.                                 if (empty($report_id)){
  156.                                     $res = $GLOBALS['dbh']->autoExecute('t_most_express_report', array(
  157.                                         'shipments_ref' => $report['shipments_ref'],
  158.                                         'barcode' => $report['barcode'],
  159.                                         'delivery_date' => $report['delivery_date'],
  160.                                         'cod' => $report['cod'],
  161.                                         'cod_date' => $report['cod_date']
  162.                                     ));
  163.                                     if ($res){
  164.                                         echo "In the table t_most_express_report added ".$counterRep." row<br/>\n";
  165.                                     }else{
  166.                                         echo "The row not was added to the table<br/>\n";
  167.                                     }
  168.                                 }else{
  169.                                     echo "The table already exists a record<br/>\n";
  170.                                 }
  171.                                 $counterRep++;
  172.                             }
  173.                         }else{
  174.                             echo "File is empty<br/>\n";
  175.                         }
  176.  
  177.                         // Parsing txt file with stutuses
  178.                         $fileStatus = DOC_ROOT.'dynamic/zip/reports/'.$date.'_statuses.txt';
  179.                         $stutusData = array();
  180.                         if (file_exists($fileStatus)){
  181.                             $sfile = fopen($fileStatus, 'r');
  182.                             if ($sfile)
  183.                             {
  184.                                 while (!feof($sfile))
  185.                                 {
  186.                                     $arr = explode("|",fgets($sfile));
  187.                                     $stutusData[] = array_slice($arr, 0, 5);
  188.                                 }
  189.                                 fclose($sfile);
  190.                             }else{
  191.                                 echo "Failed to open file\n";
  192.                             }
  193.                         }else{
  194.                             echo "File does not exist\n";
  195.                         }
  196.  
  197.                         if (!empty($stutusData) && (!end($stutusData[count($stutusData)-1]) || !end($stutusData[count($stutusData)-1][0]))){
  198.                             array_pop($stutusData);
  199.                         }
  200.  
  201.                         $statusKeys = array(
  202.                             0 => 'shipments_ref',
  203.                             1 => 'barcode',
  204.                             2 => 'statuscode',
  205.                             3 => 'status_date_time',
  206.                             4 => 'station_name',
  207.                         );
  208.  
  209.                         if (!empty($stutusData[0])){
  210.                             foreach($stutusData as $key => $status){
  211.                                 $stutusData[$key] = array_combine($statusKeys,$status);
  212.                             }
  213.                             foreach ($stutusData as &$status){
  214.                                 static $counterStat = 1;
  215.                                 $shipment = '';
  216.                                 for ($i = 0; $i < strlen($status['shipments_ref']); $i++){
  217.                                     if (ord($status['shipments_ref']{$i}) != 239 && ord($status['shipments_ref']{$i}) != 187 && ord($status['shipments_ref']{$i}) != 191){
  218.                                         $shipment .= $status['shipments_ref']{$i};
  219.                                     }  
  220.                                 }
  221.                                 $status['shipments_ref'] = (int)$shipment;
  222.                                 $status['status_date_time'] = date("Y-m-d H:i:s",strtotime($status['status_date_time']));
  223.                                     $status_id = $GLOBALS['dbh']->getOne("
  224.                                         SELECT mes.id
  225.                                         FROM t_most_express_status AS mes
  226.                                         WHERE mes.shipments_ref = '".checkData($status['shipments_ref'],'number')."'
  227.                                             AND mes.barcode = '".checkData($status['barcode'],'text')."'
  228.                                             AND mes.statuscode = '".checkData($status['statuscode'],'text')."'
  229.                                             AND mes.status_date_time = '".checkData($status['status_date_time'],'text')."'
  230.                                             AND mes.station_name = '".checkData($status['station_name'],'text')."'"
  231.                                     );
  232.  
  233.                                 $res = false;
  234.                                 if (empty($status_id)){
  235.                                     $res = $GLOBALS['dbh']->autoExecute('t_most_express_status', array(
  236.                                         'shipments_ref' => $status['shipments_ref'],
  237.                                         'barcode' => $status['barcode'],
  238.                                         'statuscode' => $status['statuscode'],
  239.                                         'status_date_time' => $status['status_date_time'],
  240.                                         'station_name' => $status['station_name']
  241.                                     ));
  242.                                     if ($res){
  243.                                         echo "In the table t_most_express_status added ".$counterStat." row<br/>\n";
  244.                                     }else{
  245.                                         echo "The row not was added to the table<br/>\n";
  246.                                     }
  247.                                 }else{
  248.                                     echo "The table already exists a record<br/>\n";
  249.                                 }
  250.                                 $counterStat++;
  251.  
  252.                             }
  253.                         }else{
  254.                             echo "File is empty<br/>\n";
  255.                         }
  256.                     }else{
  257.                         echo "Archive is not saved<br/>\r\n";
  258.                     }
  259.                     fclose($ft);
  260.                 }
  261.             }
  262.         }
  263.     }else{
  264.         echo "No message with an attachment<br>\n";
  265.     }
  266.    
  267. }
  268. imap_close($mbox);
  269.  
  270. $paidStatuses = ginst('Order')->paidStatuses;
  271.  
  272. $orderIds = $GLOBALS['dbh']->getCol(
  273.     "SELECT DISTINCT t_order.id
  274.         FROM t_order
  275.         LEFT JOIN t_event_content ON (t_event_content.id = t_order.event_id AND t_event_content.language_id = 1)
  276.         LEFT JOIN t_ticket ON (t_ticket.order_id = t_order.id)
  277.         LEFT JOIN t_user ON (t_user.id = t_ticket.user_id)
  278.     WHERE t_order.delivery_type = 2 AND paid_status = 8"
  279. );
  280.  
  281. if(!empty($orderIds)){     
  282.     $deliveryInfo = array();
  283.     foreach ($orderIds as $id){
  284.         $deliveryInfo = $GLOBALS['dbh']->getRow("
  285.             SELECT
  286.                 d.order_id,
  287.                 mes.statuscode,
  288.                 mec.name_ru
  289.             FROM
  290.                 t_delivery AS d
  291.             LEFT JOIN
  292.                 t_most_express_status AS mes ON mes.shipments_ref = d.order_id
  293.             JOIN
  294.                 t_most_express_content AS mec ON mec.code = mes.statuscode
  295.             WHERE
  296.                 d.order_id = '".$id."' AND statuscode IN ('000000027', '000000010', '000000015', '000000028')
  297.             ORDER BY
  298.                 mes.status_date_time DESC
  299.             LIMIT 1
  300.         ");
  301.  
  302.         if (!empty($deliveryInfo)){
  303.             if ($deliveryInfo['statuscode'] == '000000027'){
  304.                 $orderInfo = ginst('Order')->get($deliveryInfo['order_id']);
  305.                 if ($orderInfo['paid_status'] != 6){
  306.                     ginst('Order')->apply($deliveryInfo['order_id']);  
  307.                     if (!ginst('Order')->checkLog($deliveryInfo['order_id'])){
  308.                         ginst('Order')->addLog(array(
  309.                             'order_id' => $deliveryInfo['order_id'],
  310.                             'action' => 'confirm-order',
  311.                             'info' => array(
  312.                                 'status' => $paidStatuses[$orderInfo['paid_status']]['title']
  313.                             ),
  314.                             'user_id' => 41518
  315.                         ));
  316.                     }
  317.                 }
  318.             }
  319.             if ($deliveryInfo['statuscode'] == '000000010' || $deliveryInfo['statuscode'] == '000000015' || $deliveryInfo['statuscode'] == '000000028'){
  320.                 $orderInfo = ginst('Order')->get($deliveryInfo['order_id']);
  321.                 ginst('Order')->cancel($orderInfo, $deliveryInfo['name_ru']);
  322.             }
  323.         }
  324.     }          
  325. }
  326.  
  327. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement