Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.95 KB | None | 0 0
  1. <?php
  2.  
  3. class SyncCommand extends CConsoleCommand
  4. {
  5.  
  6.     private $ftp_url = '195.2.87.59';
  7.     private $ftp_user = 'roz';
  8.     private $ftp_pass = 'werdxcv1';
  9.  
  10.     /**
  11.      * Обновленте цен (Через FTP):
  12.      */
  13.     public function actionPriceUpdate()
  14.     {
  15.         $ftp = new Ftp($this->ftp_url, $this->ftp_user, $this->ftp_pass);
  16.  
  17.         $file_local = ROOT_PATH . '/mod-tmp/sync/nomenclature.csv';
  18.         $file_remote = '/Trade/InetMag/Nom_B.spr';
  19.  
  20.         $ftp->getFile($file_local, $file_remote);
  21.        
  22.         $tf = new TextFile();
  23.         $data = $tf->nomRead($file_local);
  24.  
  25.         $table = ItemVariants::model()->tableName();
  26.         $values = [];
  27.         foreach ($data as $row) {
  28.             if (!empty($row['Art_No'])) {
  29.                 $iv = ItemVariants::model()->findByAttributes(['articul' => $row['Art_No']]);
  30.                 if ($iv) {
  31.                     $old_price = (int)$row['Price'];
  32.                     $new_price = (int)$row['New_Price'];
  33.                     $price2 = (int)$row['Price2'];
  34.                     $values[] = "({$iv->id},{$old_price},{$new_price},{$price2})";
  35.  
  36.                 }
  37.             }
  38.         }
  39.         echo count($values) . " записей обновится \n";
  40.         $values = implode(',', $values);
  41.         $sql =
  42.             "INSERT INTO
  43.                {$table} (`id`,`old_price`, `new_price`, `price2`)
  44.            VALUES
  45.                {$values}
  46.            ON DUPLICATE KEY UPDATE
  47.                `old_price` = VALUES(`old_price`),
  48.                `new_price` = VALUES(`new_price`),
  49.                `price2` = VALUES(`price2`)";
  50.  
  51.         $connection=Yii::app()->db;
  52.         $command=$connection->createCommand($sql);
  53.         $rowCount = $command->execute();
  54.         print_r($rowCount);
  55.         echo "\n";
  56.  
  57.     }
  58.  
  59.     /**
  60.      * Обновление статусов товаров (через FTP)
  61.      */
  62.     public function actionNomStatusUpdate()
  63.     {
  64.         $ftp = new Ftp($this->ftp_url, $this->ftp_user, $this->ftp_pass);
  65.         $file_local = ROOT_PATH . '/mod-tmp/sync/nomenclature-stats.csv';
  66.         $file_remote =  $ftp->getFiles('/Trade/SklFab');
  67.         if ($file_remote) {
  68.             $ftp->getFile($file_local, $file_remote);
  69.             $tf = new TextFile();
  70.             $data = $tf->nomStatusRead($file_local);
  71.  
  72.             $table = ItemVariants::model()->tableName();
  73.             $values = [];
  74.  
  75.             foreach ($data as $row) {
  76.                 if (!empty($row['articul'])) {
  77.                     $iv = ItemVariants::model()->findByAttributes(['articul' => $row['articul']]);
  78.                     if ($iv and $row['stock']) {
  79.                         $values[] = "({$iv->id},'{$row['stock']}')";
  80.  
  81.                     }
  82.                 }
  83.             }
  84.             echo count($values) . " записей обновится \n";
  85.             $values = implode(',', $values);
  86.  
  87.             $sql =
  88.                 "INSERT INTO
  89.                {$table} (`id`,`status`)
  90.            VALUES
  91.                {$values}
  92.            ON DUPLICATE KEY UPDATE
  93.                `status` = VALUES(`status`)";
  94.             $connection=Yii::app()->db;
  95.             $command=$connection->createCommand($sql);
  96.             $rowCount = $command->execute();
  97.             print_r($rowCount);
  98.             echo "\n";
  99.  
  100.         } else {
  101.             echo "файл не найден \n";
  102.         }
  103.     }
  104.  
  105.     public function actionOrdersUpdate()
  106.     {
  107.         $ftp = new Ftp($this->ftp_url, $this->ftp_user, $this->ftp_pass);
  108.  
  109.         $file_local = ROOT_PATH . '/mod-tmp/sync/orders-stats.csv';
  110.         $file_remote = '/Trade/Zakaz/Zakaz.ord';
  111.  
  112.         $ftp->getFile($file_local, $file_remote);
  113.  
  114.         $tf = new TextFile();
  115.         $data = $tf->ordersRead($file_local);
  116.  
  117.         $table = ItemVariants::model()->tableName();
  118.         $values = [];
  119.         foreach ($data as $row) {
  120.             if (!empty($row['Art_No'])) {
  121.                 $iv = ItemVariants::model()->findByAttributes(['articul' => $row['Art_No']]);
  122.                 if ($iv) {
  123.                     $old_price = (int)$row['Price'];
  124.                     $new_price = (int)$row['New_Price'];
  125.                     $price2 = (int)$row['Price2'];
  126.                     $values[] = "({$iv->id},{$old_price},{$new_price},{$price2})";
  127.  
  128.                 }
  129.             }
  130.         }
  131.         echo count($values) . " записей обновится \n";
  132.         $values = implode(',', $values);
  133.         $sql =
  134.             "INSERT INTO
  135.                {$table} (`id`,`old_price`, `new_price`, `price2`)
  136.            VALUES
  137.                {$values}
  138.            ON DUPLICATE KEY UPDATE
  139.                `old_price` = VALUES(`old_price`),
  140.                `new_price` = VALUES(`new_price`),
  141.                `price2` = VALUES(`price2`)";
  142.  
  143.         $connection=Yii::app()->db;
  144.         $command=$connection->createCommand($sql);
  145.         $rowCount = $command->execute();
  146.  
  147.         print_r($data);
  148.  
  149.  
  150.  
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement