Advertisement
Guest User

Untitled

a guest
Nov 28th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.84 KB | None | 0 0
  1. <?
  2. //3.14 и 3.36
  3. header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  4. header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  5. header("Cache-Control: no-cache, must-revalidate");
  6. header("Pragma: no-cache");
  7. header('Content-type: text/html; charset=utf-8');
  8. $koef=3.14;
  9.     $host = 'localhost';
  10.     $db   = 'db';
  11.     $user = 'users';
  12.     $pass = 'pasw';
  13.     $charset = 'utf8'; 
  14.     $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
  15.     $opt = [
  16.         PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
  17.         PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_BOTH,
  18.         PDO::ATTR_EMULATE_PREPARES   => false,
  19.     ];
  20.  
  21.  
  22. $db = new PDO($dsn, $user, $pass, $opt);
  23.  
  24. require_once 'Classes/PHPExcel.php';
  25. try{
  26. if (isset($_FILES['up_xls'])){
  27. $excel = PHPExcel_IOFactory::load($uploadfile);
  28. }else  
  29. $excel = PHPExcel_IOFactory::load('parser.xls');
  30. } catch(Exception $e){
  31.     print_r($e);
  32. }
  33. Foreach($excel ->getWorksheetIterator() as $worksheet) {
  34.  $lists[] = $worksheet->toArray();
  35. }
  36. $i=0;
  37. $j=0;
  38. foreach($lists as $list){
  39.  //echo '<table border="1">';
  40.  // Перебор строк
  41.  foreach($list as $row){
  42.   // echo '<tr>';
  43.    // Перебор столбцов
  44.    $i=0;
  45.    $search_index=0;
  46. //**************************Поиск нужных нам столбцов*************************************  
  47.    foreach ($row as $col){
  48.        if (strpos($col,"CUSTOMER NO.")!==false){
  49.            $mas_pars['art']=$search_index;
  50.        }
  51.        if (strpos($col,"FINISHING")!==false){
  52.            $mas_pars['color']=$search_index;
  53.        }
  54.        if (strpos($col,"UNIT PRICE")!==false){
  55.            $mas_pars['cena']=$search_index;
  56.        }
  57.        if (strpos($col,"SIZE OF THE CARTON")!==false){
  58.            $mas_pars['size_w']=$search_index;
  59.            $mas_pars['size_h']=$search_index+2;
  60.            $mas_pars['size_l']=$search_index+4;
  61.        }
  62.        if (strpos($col,"TOTAL QTY")!==false){
  63.            $mas_pars['count']=$search_index;
  64.        }       
  65.        $search_index++;
  66.    }
  67. //*****************************************************************************************
  68.    $index=0;
  69.    foreach($row as $col){
  70.      if (($index==$mas_pars['art'])||($index==$mas_pars['color'])||($index==$mas_pars['cena'])||($index==$mas_pars['size_w'])||($index==$mas_pars['size_h'])||($index==$mas_pars['size_l'])||($index==$mas_pars['count'])){  
  71.      //echo '<td>'.$col.'</td>';
  72.      
  73.          if ($col==''){
  74.             break;
  75.          }
  76.      $mas[$j][$i]=$col;
  77.      $i++;
  78.      }
  79.      
  80.      $index++;
  81.  }
  82.  if (count($mas[$j])<3){
  83.      unset($mas[$j]);
  84.      sort($mas);
  85.  }
  86.  
  87.   if (strpos($mas[$j][0],' ')!==false){
  88.  $mas[$j][0]=preg_replace("/\s+/", "", $mas[$j][0]);
  89.  }
  90.   if (strpos($mas[$j][1],' ')!==false){
  91.  $mas[$j][1]=preg_replace("/\s+/", "", $mas[$j][1]);
  92.  }  
  93.   if (strpos($mas[$j][6],' ')!==false){
  94.  $mas[$j][6]=preg_replace("/\s+/", "", $mas[$j][6]);
  95.  }  
  96.  
  97.  if (strpos($mas[$j][0],'AD')===false){
  98.      unset($mas[$j]);
  99.      sort($mas);     
  100.  }else{
  101.  $mas[$j][0]=str_replace("AD", "AD-", $mas[$j][0]);  
  102.  }
  103.   if (strpos($mas[$j][0],'--')==true){
  104.  $mas[$j][0]=str_replace("--", "-", $mas[$j][0]);
  105.  }
  106.  if (strpos($mas[$j][0],'CHROME')==true){
  107.  $mas[$j][0]=str_replace("CHROME", "CR", $mas[$j][0]);
  108.  }  
  109.  if (strpos($mas[$j][0],'WHITE')==true){
  110.  $mas[$j][0]=str_replace("WHITE", "WH", $mas[$j][0]);
  111.  }
  112.   if (strpos($mas[$j][0],'BRONZE')==true){
  113.  $mas[$j][0]=str_replace("BRONZE", "BR", $mas[$j][0]);
  114.  }
  115.   if (strpos($mas[$j][0],'GOLD')==true){
  116.  $mas[$j][0]=str_replace("GOLD", "GO", $mas[$j][0]);
  117.  }
  118.   if (strpos($mas[$j][0],'BLACK')==true){
  119.  $mas[$j][0]=str_replace("BLACK", "BK", $mas[$j][0]);
  120.  }  
  121.   if (strpos($mas[$j][0],'GREY')==true){
  122.  $mas[$j][0]=str_replace("GREY", "GR", $mas[$j][0]);
  123.  }
  124.  
  125.  
  126.  
  127.   if (strpos($mas[$j][0],'WH')==true){
  128.  $mas[$j][0]=str_replace("WH", " WH", $mas[$j][0]);
  129.  }
  130.   if (strpos($mas[$j][0],'GO')==true){
  131.  $mas[$j][0]=str_replace("GO", " GO", $mas[$j][0]);
  132.  }
  133.   if (strpos($mas[$j][0],'BK')==true){
  134.  $mas[$j][0]=str_replace("BK", " BK", $mas[$j][0]);
  135.  }
  136.   if (strpos($mas[$j][0],'GR')==true){
  137.  $mas[$j][0]=str_replace("GR", " GR", $mas[$j][0]);
  138.  }
  139.   if (strpos($mas[$j][0],'BR')==true){
  140.  $mas[$j][0]=str_replace("BR", " BR", $mas[$j][0]);
  141.  }
  142.   if (strpos($mas[$j][0],'BN')==true){
  143.  $mas[$j][0]=str_replace("BN", " BN", $mas[$j][0]);
  144.  }
  145.   if (strpos($mas[$j][0],'CR')==true){
  146.  $mas[$j][0]=str_replace("CR", " CR", $mas[$j][0]);
  147.  }  
  148.   if (strpos($mas[$j][0],'+')==true){
  149.  $mas[$j][0]=str_replace("+", " +", $mas[$j][0]);
  150.  }
  151.    if (strpos($mas[$j][0],'- ')==true){
  152.  $mas[$j][0]=str_replace("- ", " + ", $mas[$j][0]);
  153.  }
  154.   if (strpos($mas[$j][0],' + ')==true){
  155.  $mas[$j][0]=str_replace(" + ", "+", $mas[$j][0]);
  156.  }
  157.   if (strpos($mas[$j][0],'BK+CR')==true){
  158.  $mas[$j][0]=str_replace("BK+CR", "BK", $mas[$j][0]);
  159.  }
  160.   if (strpos($mas[$j][0],'CR+WH')==true){
  161.  $mas[$j][0]=str_replace("CR+WH", "WH+CR", $mas[$j][0]);
  162.  }  
  163.    if (strpos($mas[$j][1],',')==true){
  164.  $mas[$j][1]=preg_replace('/^([^,]*).*$/', '$1', $mas[$j][1]);
  165.  }  
  166.  
  167.   if (strpos($mas[$j][1],'CHROME')!==false){
  168.  $mas[$j][1]=str_replace("CHROME", "CR", $mas[$j][1]);
  169.  }  
  170.  if (strpos($mas[$j][1],'WHITE')!==false){
  171.  $mas[$j][1]=str_replace("WHITE", "WH", $mas[$j][1]);
  172.  }
  173.   if (strpos($mas[$j][1],'BRONZE')!==false){
  174.  $mas[$j][1]=str_replace("BRONZE", "BR", $mas[$j][1]);
  175.  }
  176.   if (strpos($mas[$j][1],'GOLD')!==false){
  177.  $mas[$j][1]=str_replace("GOLD", "GO", $mas[$j][1]);
  178.  }
  179.   if (strpos($mas[$j][1],'BLACK')!==false){
  180.  $mas[$j][1]=str_replace("BLACK", "BK", $mas[$j][1]);
  181.  }  
  182.   if (strpos($mas[$j][1],'GREY')!==false){
  183.  $mas[$j][1]=str_replace("GREY", "GR", $mas[$j][1]);
  184.  }
  185.   if (strpos($mas[$j][6],'$')!==false){
  186.  $mas[$j][6]=str_replace("$", "", $mas[$j][6]);
  187.  }
  188.  if (trim($mas[$j][6])!=''){
  189. $mas[$j][6]=round((trim($mas[$j][6])*$koef),2);
  190.  }
  191. error_reporting(E_ALL);
  192.  $cena=$mas[$j][6];
  193.  $g=explode(".",$cena);
  194. if (strlen($g[1])>2){
  195.     if ($g[1][3]>5){
  196.         $cenaa=substr($g[1],0,2);
  197.         $cenaa++;
  198.     }else{
  199.         $cenaa=substr($g[1],0,2);
  200.     }
  201.     $cenaa=$g[0].".".$cenaa;
  202. }else $cenaa=$mas[$j][6];
  203. if (strlen($g[1])<2){
  204.     $cenaa=$cenaa."0";
  205. }
  206. $size_t=str_replace(",","",trim($mas[$j][2]))."х".str_replace(",","",trim($mas[$j][3]))."х".str_replace(",","",trim($mas[$j][4]));
  207. $size_t=str_replace(".","",trim($mas[$j][2]))."х".str_replace(".","",trim($mas[$j][3]))."х".str_replace(".","",trim($mas[$j][4]));
  208. $kolvo=$mas[$j][5];
  209.  //echo '</tr>';
  210. if (trim($mas[$j][0])!=''){
  211.     try{
  212. //*************************************************************************************************************************************************
  213.     $art=substr(($db->quote(trim($mas[$j][0])."%")),1,strlen($db->quote(trim($mas[$j][0])."%"))-2);
  214.     $r=$db->prepare("select id, cena, art from tovar_test where art LIKE :art");
  215.     $update=$db->prepare("update tovar_test set cena = :cena where id= :id;");
  216.     $insert=$db->prepare("insert into prixod(id_t, art, cena, data, kolvo) values(:id_t, :art, :cena, now(), :kolvo);");
  217.     $insert_t=$db->prepare("insert into tovar_test(id_typ, id_kol, art, naz, cena, visibl, foto, foto1) values('7', '21', :art, 'new', :cena, '0', 'images/no-image.png', 'images/no-image.png');");
  218.     $r->execute([':art'=>$art]);
  219.     //*************************************Одно сопадение*************************************************************
  220.     if ($r->rowCount()==1){
  221.         $art=substr(($db->quote(trim($mas[$j][0]))),1,strlen($db->quote(trim($mas[$j][0])))-2);
  222.         $r->execute([':art'=>$art]);
  223.         if ($r->rowCount()!=0){
  224.             $message.= "<br><br><font color=\"green\"><b>".$j.". '".$art."' - Найден в первом проходе!</b></font><br>";
  225.             foreach ($r as $res){
  226.                 if (trim($res['cena'])!=$cenaa){
  227.                     $message.= "<font color=\"red\">Неверная цена(".$res['cena']." &#8364; <> ".$cenaa." &#8364;)</font><br>";
  228.                     if ($update->execute([':cena'=>$cenaa, ':id'=>$res['id']])){$message.= "<font color=\"green\">Цена успешно изменена</font><br>";}else $message.= "<font color=\"red\">Произошла ошибка при изменении цены</font><br>";
  229.                 }else $message.= "Цена верна(".$res['cena']." &#8364; = ".$cenaa." &#8364;)<br>";
  230.                 if ($insert->execute([':id_t'=>$res['id'], ':art'=>$res['art'], ':cena'=>$cenaa, ':kolvo'=>$kolvo])){$message.= "В таблицу приход запись добавлена<br>";}else $message.= "<font color=\"red\">Произошла ошибка при добавлении записи в таблицу приход</font><br>";
  231.             }
  232.         }else{
  233.             $art=substr(($db->quote(trim($mas[$j][0]." ".$mas[$j][1]))),1,strlen($db->quote(trim($mas[$j][0]." ".$mas[$j][1])))-2);
  234.             $r->execute([':art'=>$art]);
  235.             if ($r->rowCount()!=0){
  236.                 $message.= "<br><br><font color=\"green\"><b>".$j.". '".$art."' - Найден во втором проходе!</b></font><br>";
  237.                 foreach ($r as $res){
  238.                     if (trim($res['cena'])!=$cenaa){
  239.                         $message.= "<font color=\"red\">Неверная цена(".$res['cena']." &#8364; <> ".$cenaa." &#8364;)</font><br>";
  240.                         if ($update->execute([':cena'=>$cenaa, ':id'=>$res['id']])){$message.= "<font color=\"green\">Цена успешно изменена</font><br>";}else $message.= "<font color=\"red\">Произошла ошибка при изменении цены</font><br>";
  241.                     }else $message.= "Цена верна(".$res['cena']." &#8364; = ".$cenaa." &#8364;)<br>";
  242.                     if ($insert->execute([':id_t'=>$res['id'], ':art'=>$res['art'], ':cena'=>$cenaa, ':kolvo'=>$kolvo])){$message.= "В таблицу приход запись добавлена<br>";}else $message.= "<font color=\"red\">Произошла ошибка при добавлении записи в таблицу приход</font><br>";
  243.                 }
  244.             }else $message.= "<br><br><font color=\"red\"><b>".$j.". '".$art."' - Не найден во втором проходе!</b></font><br>";
  245.         }
  246.     }
  247.     //**********************************Несколько совпадений**********************************************************
  248.     if ($r->rowCount()>1){
  249.         $art=substr(($db->quote(trim($mas[$j][0]." ".$mas[$j][1]))),1,strlen($db->quote(trim($mas[$j][0]." ".$mas[$j][1])))-2);
  250.         $r->execute([':art'=>$art]);
  251.         if ($r->rowCount()!=0){
  252.             $message.= "<br><br><font color=\"green\"><b>".$j.". '".$art."' - Найден при множественном выборе!</b></font><br>";
  253.             foreach ($r as $res){
  254.                 if (trim($res['cena'])!=$cenaa){
  255.                     $message.= "<font color=\"red\">Неверная цена(".$res['cena']." &#8364; <> ".$cenaa." &#8364;)</font><br>";
  256.                     if ($update->execute([':cena'=>$cenaa, ':id'=>$res['id']])){$message.= "<font color=\"green\">Цена успешно изменена</font><br>";}else $message.= "<font color=\"red\">Произошла ошибка при изменении цены</font><br>";
  257.                 }else $message.= "Цена верна(".$res['cena']." &#8364; = ".$cenaa." &#8364;)<br>";
  258.                 if ($insert->execute([':id_t'=>$res['id'], ':art'=>$res['art'], ':cena'=>$cenaa, ':kolvo'=>$kolvo])){$message.= "В таблицу приход запись добавлена<br>";}else $message.= "<font color=\"red\">Произошла ошибка при добавлении записи в таблицу приход</font><br>";
  259.             }
  260.         }else $message.= "<br><br><font color=\"red\"><b>".$j.". '".$art."' - Не найден при множественном выборе!</b></font><br>";
  261.     }
  262.     //*************************************Нет совпадений*************************************************************
  263.     if ($r->rowCount()==0){
  264.         $message.= "<br><br><font color=\"red\"><b>".$j.". '".$art."' - Не найден вообще!</b></font><br>";
  265.         $art=substr(($db->quote(trim($mas[$j][0]))),1,strlen($db->quote(trim($mas[$j][0])))-2);
  266.         if ($insert_t->execute([':art'=>$art, ':cena'=>$cenaa])){
  267.             $message.= "Товар успешно добавлен в таблицу товар<br>";
  268.             $r->execute([':art'=>$art]);
  269.             foreach ($r as $res){
  270.                 if ($insert->execute([':id_t'=>$res['id'], ':art'=>$res['art'], ':cena'=>$cenaa, ':kolvo'=>$kolvo])){$message.= "В таблицу приход запись добавлена<br>";}else print "<font color=\"red\">Произошла ошибка при добавлении записи в таблицу приход</font><br>";
  271.             }
  272.         }else $message.= "<font color=\"red\">Произошла ошибка при добавлении в таблицу товар</font><br>";
  273.     }
  274. //****************************************************************************************************************************************************
  275.     } catch(Exception $e){
  276.         print_r($e);
  277.     }
  278.     $message.= "Цена: ".$cenaa." &#8364;<br>";   
  279.     $message.= "Размер коробки: ".$size_t."<br>";   
  280.     $message.= "Количество: ".$kolvo."<br>";   
  281. }
  282.  $j++;
  283.  }
  284.  
  285.  //echo '</table>';
  286.  
  287. }
  288. if (!isset($_REQUEST['page']) && isset($message)){
  289.     print $message;
  290.     print "<pre>";
  291. print_r($mas);
  292. print "</pre>";
  293.    print "<pre>";
  294.    print_r($mas_pars);
  295.    print "</pre>";
  296. }
  297. /*  */
  298. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement