Advertisement
amaenta

presta import

Mar 27th, 2019
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 59.17 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * import z pliku CSV dla idea-prints.pl
  5.  **/
  6. class Panel_ImportController extends IberoBasicController
  7. {
  8. //objItems
  9. //objProducers
  10. //cat_items
  11. //objAmount
  12. //objUsers
  13. //objOrders
  14. //objOrdersItems
  15.     public function init()
  16.     {
  17.         parent::init();
  18.         ini_set('display_errors', 1);
  19.         error_reporting(E_ALL);
  20.         set_time_limit(0);
  21.         ini_set('memory_limit', '2024M');
  22.         $this->db = Zend_Registry::get('dbAdapter');
  23.         $this->lang = Zend_Registry::get('lang');
  24.     }
  25.  
  26.  
  27.     public function connect()
  28.     {
  29.         $link = mysqli_connect("localhost", "centr11_test", "3t148FsNo0", "centr11_test") or die("Error " . mysqli_error($link));
  30.         $link->query("SET NAMES 'utf8'");
  31.         return $link;
  32.     }
  33.  
  34.     public function indexAction()
  35.     {
  36.         echo getcwd();
  37.     }
  38.  
  39.     public function prodAction()
  40.     {
  41.         ini_set('display_errors', 1);
  42.  
  43.         $link = $this->connect();
  44.  
  45.         $sql = "SELECT id_manufacturer,name FROM `ps_manufacturer` WHERE active=1 ORDER BY id_manufacturer ASC";
  46.         $result1 = $link->query($sql);
  47.         $i = 0;
  48.         while ($row1 = mysqli_fetch_array($result1)) {
  49.  
  50.  
  51.             if (strlen($row1['name']) > 1) {
  52.                 $prod = trim($row1['name']);
  53.  
  54.  
  55.                 if ($this->objProducers->isExist($prod)) {
  56.                     // istnieje
  57.                 } else {
  58.                     // dodaj
  59.                     $data = array();
  60.                     $data['producer_name'] = $prod;
  61.                     $data['producer_desc'] = "";
  62.                     //print_r($data);
  63.  
  64.                     $this->objProducers->add($data);
  65.                 }
  66.  
  67.             }
  68.         }
  69.  
  70.         die('import complete without errors');
  71.  
  72.     }
  73.  
  74.     public function catAction()
  75.     {
  76.  
  77.         ini_set('display_errors', 1);
  78.         ini_set('memory_limit', '1024M');
  79.         $lang = 1;
  80.         $db = Zend_Registry::get('dbAdapter');
  81.         $link = $this->connect();
  82.  
  83.  
  84.         $sql = "SELECT c.*, l.name, l.description as description,l.meta_description,l.meta_keywords,l.meta_title
  85. FROM `ps_category` c INNER JOIN ps_category_lang l ON (l.id_category=c.id_category AND l.id_lang=6)
  86.            WHERE c.id_parent='1'ORDER BY c.id_category DESC";
  87.         $result1 = $link->query($sql);
  88.         while ($row1 = mysqli_fetch_array($result1)) {
  89.  
  90.             // level 1
  91.             $arr = array();
  92.             // $arr['id'] = $row1['category_id'];
  93.             $arr['name' . $lang] = $row1['name'];
  94.             $arr['google_title' . $lang] = $row1['meta_title'];
  95.             $arr['google_desc' . $lang] = $row1['meta_description'];
  96.             $arr['google_key_words' . $lang] = $row1['meta_keywords'];
  97.             $arr['campaign_active'] = 0;
  98.             $lastID1 = $this->treeCat->add($arr, 1);
  99.             $this->treeCat->setup();
  100.  
  101.             try {
  102.                 $db->query("INSERT INTO kategorie (stara,nowa) VALUES('" . $row1['id_category'] . "','$lastID1')");
  103.             } catch (Exception $e) {
  104.                 print_r($e);
  105.                 die();
  106.             }
  107.  
  108.             if ($row1['description'] != '') {
  109.                 $desc = array();
  110.                 $desc['cat_id'] = $lastID1;
  111.                 $desc['cat_desc'] = $row1['description'];  // print_r($lastID1    );  die('...');
  112.                 $this->objCategoriesDesc->add($desc);
  113.             }
  114.  
  115.  
  116.             $sql = "SELECT c.*, l.name, l.description as description,l.meta_description,l.meta_keywords,l.meta_title
  117. FROM `ps_category` c INNER JOIN ps_category_lang l ON (l.id_category=c.id_category AND l.id_lang=6)
  118.                     WHERE c.id_parent='" . $row1['id_category'] . "' ORDER BY c.id_category DESC";
  119.             $result2 = $link->query($sql);
  120.  
  121.             if (count($result2) > 0) {
  122.                 while ($row2 = mysqli_fetch_array($result2)) {
  123.                     // level 2
  124.                     $arr = array();
  125.  
  126.                     $arr['name' . $lang] = $row2['name'];
  127.                     $arr['google_title' . $lang] = $row2['meta_title'];
  128.                     $arr['google_desc' . $lang] = $row2['meta_description'];
  129.                     $arr['google_key_words' . $lang] = $row2['meta_keywords'];
  130.                     $arr['campaign_active'] = 0;
  131.  
  132.  
  133.                     $lastID2 = $this->treeCat->add($arr, $lastID1);
  134.                     $this->treeCat->setup();
  135.                     try {
  136.                         $db->query("INSERT INTO kategorie (stara,nowa) VALUES('" . $row2['id_category'] . "','$lastID2')");
  137.                     } catch (Exception $e) {
  138.                         print_r($e);
  139.                         die();
  140.                     }
  141.  
  142.                     //$db->query("INSERT INTO kategorie (stara,nowa) VALUES('".$row2['id_category']."','$lastID2')");
  143.  
  144.  
  145.                     if ($row2['description'] != '') {
  146.                         $desc = array();
  147.                         $desc['cat_id'] = $lastID2;
  148.                         $desc['cat_desc'] = $row2['description'];
  149.                         $this->objCategoriesDesc->add($desc);
  150.                     }
  151.  
  152.                     $sql = "SELECT c.*, l.name, l.description as description,l.meta_description,l.meta_keywords,l.meta_title
  153. FROM `ps_category` c INNER JOIN ps_category_lang l ON (l.id_category=c.id_category AND l.id_lang=6)
  154.                     WHERE c.id_parent='" . $row2['id_category'] . "' ORDER BY c.id_category DESC";
  155.                     $result3 = $link->query($sql);
  156.                     while ($row3 = mysqli_fetch_array($result3)) {
  157.                         // level 3
  158.                         $arr = array();
  159.                         // $arr['id'] = $row3['category_id'];
  160.                         $arr['name' . $lang] = $row3['name'];
  161.                         $arr['google_title' . $lang] = $row3['meta_title'];
  162.                         $arr['google_desc' . $lang] = $row3['meta_description'];
  163.                         $arr['google_key_words' . $lang] = $row3['meta_keywords'];
  164.                         $arr['campaign_active'] = 0;
  165.  
  166.                         $lastID3 = $this->treeCat->add($arr, $lastID2);
  167.                         $this->treeCat->setup();
  168.  
  169.                         $db->query("INSERT INTO kategorie (stara,nowa) VALUES('" . $row3['id_category'] . "','$lastID3')");
  170.                         if ($row3['description'] != '') {
  171.                             $desc = array();
  172.                             $desc['cat_id'] = $lastID3;
  173.                             $desc['cat_desc'] = $row3['description'];
  174.                             $this->objCategoriesDesc->add($desc);
  175.                         }
  176.  
  177.                         $sql = "SELECT c.*, l.name, l.description as description,l.meta_description,l.meta_keywords,l.meta_title
  178. FROM `ps_category` c INNER JOIN ps_category_lang l ON (l.id_category=c.id_category AND l.id_lang=6)
  179.                     WHERE c.id_parent='" . $row3['id_category'] . "' ORDER BY c.id_category DESC";
  180.                         $result4 = $link->query($sql);
  181.                         while ($row4 = mysqli_fetch_array($result4)) {
  182.                             // level 3
  183.                             $arr = array();
  184.  
  185.                             $arr['name' . $lang] = $row4['name'];
  186.                             $arr['google_title' . $lang] = $row4['meta_title'];
  187.                             $arr['google_desc' . $lang] = $row4['meta_description'];
  188.                             $arr['google_key_words' . $lang] = $row4['meta_keywords'];
  189.                             $arr['campaign_active'] = 0;
  190.  
  191.                             $lastID4 = $this->treeCat->add($arr, $lastID3);
  192.                             $this->treeCat->setup();
  193.  
  194.                             $link->query("INSERT INTO kategorie (stara,nowa) VALUES('" . $row4['id_category'] . "','$lastID4')");
  195.  
  196.                             if ($row4['description'] != '') {
  197.                                 $desc = array();
  198.                                 $desc['cat_id'] = $lastID4;
  199.                                 $desc['cat_desc'] = $row4['description'];
  200.                                 $this->objCategoriesDesc->add($desc);
  201.                             }
  202.  
  203.  
  204.                             $sql = "SELECT c.*, l.name, l.description as description,l.meta_description,l.meta_keywords,l.meta_title
  205. FROM `ps_category` c INNER JOIN ps_category_lang l ON (l.id_category=c.id_category AND l.id_lang=6)
  206.                     WHERE c.id_parent='" . $row4['id_category'] . "' ORDER BY c.id_category DESC";
  207.                             $result5 = $link->query($sql);
  208.                             while ($row5 = mysqli_fetch_array($result5)) {
  209.                                 // level 3
  210.                                 $arr = array();
  211.  
  212.                                 $arr['name' . $lang] = $row5['name'];
  213.                                 $arr['google_title' . $lang] = $row5['meta_title'];
  214.                                 $arr['google_desc' . $lang] = $row5['meta_description'];
  215.                                 $arr['google_key_words' . $lang] = $row5['meta_keywords'];
  216.                                 $arr['campaign_active'] = 0;
  217.  
  218.                                 $lastID5 = $this->treeCat->add($arr, $lastID4);
  219.                                 $this->treeCat->setup();
  220.  
  221.                                 $link->query("INSERT INTO kategorie (stara,nowa) VALUES('" . $row5['id_category'] . "','$lastID5')");
  222.  
  223.                                 if ($row5['description'] != '') {
  224.                                     $desc = array();
  225.                                     $desc['cat_id'] = $lastID5;
  226.                                     $desc['cat_desc'] = $row5['description'];
  227.                                     $this->objCategoriesDesc->add($desc);
  228.                                 }
  229.  
  230.                             }
  231.  
  232.  
  233.                         }
  234.  
  235.  
  236.                     }
  237.  
  238.  
  239.                 }
  240.             }
  241.  
  242.  
  243.         }
  244.  
  245.  
  246.         //pre($cat);
  247.  
  248.         die('import complete without errors');
  249.     }
  250.  
  251.     public function propertiesShowAction()
  252.     {
  253.         ini_set('display_errors', 1);
  254.         ini_set('memory_limit', '1024M');
  255.         $lang = 1;
  256.  
  257.         $db = Zend_Registry::get('dbAdapter');
  258.         $link = $this->connect();
  259.  
  260.         $res = array();
  261.         $sql = "SELECT GROUP_CONCAT( CAST( id_attribute AS CHAR ) ) as dupa
  262. FROM ps_product_attribute_combination
  263. GROUP BY id_product_attribute
  264. HAVING count( * ) >1";
  265.         $result = $link->query($sql);
  266.         while ($row = mysqli_fetch_array($result)) {
  267.             $sql = "SELECT id_attribute_group FROM `ps_attribute` WHERE id_attribute IN (" . $row['dupa'] . ")";
  268.             $result2 = $link->query($sql);
  269.             $a = array();
  270.             while ($row2 = mysqli_fetch_array($result2)) {
  271.                 $a[] = $row2['id_attribute_group'];
  272.             }
  273.             $res[implode(",", array_unique($a))] += 1;
  274.         }
  275.  
  276.         pre($res);
  277.  
  278.  
  279.         die("OK");
  280.     }
  281.  
  282.     public function importGroups2Action()
  283.     {
  284.  
  285.         $db = Zend_Registry::get('dbAdapter');
  286.         $link = $this->connect();
  287.  
  288.         $res = array();
  289.         $sql = "SELECT GROUP_CONCAT( CAST( id_attribute AS CHAR ) ) as dupa
  290. FROM ps_product_attribute_combination
  291. GROUP BY id_product_attribute
  292. HAVING count( * ) >1";
  293.         $result = $link->query($sql);
  294.         while ($row = mysqli_fetch_array($result)) {
  295.             $sql = "SELECT id_attribute_group FROM `ps_attribute` WHERE id_attribute IN (" . $row['dupa'] . ")";
  296.             $result2 = $link->query($sql);
  297.             $a = array();
  298.             while ($row2 = mysqli_fetch_array($result2)) {
  299.                 $a[] = $row2['id_attribute_group'];
  300.             }
  301.             sort($a);
  302.             $res[implode(",", array_unique($a))] += 1;
  303.         }
  304.  
  305.  
  306.         foreach ($res as $r => $r2) {
  307.             $a = explode(",", $r);
  308.             if (count($a) == 1) continue;
  309.  
  310.             $name1 = $name2 = $name3 = '';
  311.  
  312.             $sql = "SELECT name FROM `ps_attribute_group_lang` WHERE id_lang=6 AND id_attribute_group=$a[0]";
  313.             $result = $link->query($sql);
  314.             while ($row = mysqli_fetch_array($result)) {
  315.                 $name1 = $row['name'];
  316.             }
  317.             $sql = "SELECT name FROM `ps_attribute_group_lang` WHERE id_lang=6 AND id_attribute_group=$a[1]";
  318.             $result = $link->query($sql);
  319.             while ($row = mysqli_fetch_array($result)) {
  320.                 $name2 = $row['name'];
  321.             }
  322.             if (count($a) == 3) {
  323.                 $sql = "SELECT name FROM `ps_attribute_group_lang` WHERE id_lang=6 AND id_attribute_group=$a[2]";
  324.                 $result = $link->query($sql);
  325.                 while ($row = mysqli_fetch_array($result)) {
  326.                     $name3 = $row['name'];
  327.                 }
  328.                 if ($name3 == '') die("BLAD3");
  329.             }
  330.  
  331.             if ($name1 == '' OR $name2 == '') die("BLAD!");
  332.  
  333.             $wyn = array();
  334.             $wyn['pg_name'] = $name1 . ' + ' . $name2;
  335.             if ($name3 != '') {
  336.                 $wyn['pg_name'] = $name1 . ' + ' . $name2 . ' + ' . $name3;
  337.             }
  338.             $wyn['super'] = 0;
  339.             $wyn['pg_type'] = 1;
  340.             $this->objPropertiesGroups->add($wyn);
  341.             $id = $this->objPropertiesGroups->getLastId();
  342.  
  343.             $jest = $db->fetchOne("SELECT p_id FROM properties WHERE p_iname='$name1'  AND p_id < 30");
  344.             $w = array();
  345.             $w['pg_id'] = $id;
  346.             $w['p_id'] = $jest;
  347.             $w['p_order'] = 0;
  348.             $this->objPropertiesGroupsVal->add($w);
  349.  
  350.             $jest = $db->fetchOne("SELECT p_id FROM properties WHERE p_iname='$name2'  AND p_id < 30");
  351.             $w = array();
  352.             $w['pg_id'] = $id;
  353.             $w['p_id'] = $jest;
  354.             $w['p_order'] = 1;
  355.             $this->objPropertiesGroupsVal->add($w);
  356.  
  357.             if ($name3 != '') {
  358.                 $jest = $db->fetchOne("SELECT p_id FROM properties WHERE p_iname='$name3' AND p_id < 30 ");
  359.                 $w = array();
  360.                 $w['pg_id'] = $id;
  361.                 $w['p_id'] = $jest;
  362.                 $w['p_order'] = 2;
  363.                 $this->objPropertiesGroupsVal->add($w);
  364.             }
  365.  
  366.             $data = $db->fetchAll("SELECT p_id FROM properties WHERE p_id > 29");
  367.             foreach ($data as $k => $v) {
  368.                 $w = array();
  369.                 $w['pg_id'] = $id;
  370.                 $w['p_id'] = $v['p_id'];
  371.                 $w['p_order'] = 3;
  372.                 $this->objPropertiesGroupsVal->add($w);
  373.             }
  374.  
  375.         }
  376.  
  377.         die("OK!");
  378.     }
  379.  
  380.     public function importPropertiesValuesAction()
  381.     {
  382.         ini_set('display_errors', 1);
  383.         ini_set('memory_limit', '1024M');
  384.         $lang = 1;
  385.  
  386.         $db = Zend_Registry::get('dbAdapter');
  387.         $link = $this->connect();
  388.  
  389.         try {
  390.             $sql = "SELECT f.id_feature,name FROM `ps_feature` f INNER JOIN ps_feature_lang l ON (f.id_feature=l.id_feature) ORDER BY f.id_feature ASC";
  391.             $result = $link->query($sql);
  392.             while ($row = mysqli_fetch_array($result)) {
  393.                 $id_feature = $row['id_feature'];
  394.  
  395.                 $abc = $link->query("SELECT COUNT(l.value) as ile FROM ps_feature_value f INNER JOIN ps_feature_value_lang l ON
  396.                        (f.id_feature_value=l.id_feature_value) WHERE f.id_feature=$id_feature");
  397.                 while ($row5 = mysqli_fetch_array($abc)) {
  398.                     $jest = $row5['ile'];
  399.                 }
  400.                 if ($jest > 50) {
  401.                     $ind = 2;
  402.                 } else {
  403.                     $ind = 3;
  404.                 }
  405.  
  406.                 $wyn = array();
  407.                 $wyn['p_id'] = $id_feature + 29;
  408.                 $wyn['p_name'] = $row['name'];
  409.                 $wyn['p_iname'] = $row['name'];
  410.                 $wyn['p_model'] = 2;
  411.                 $wyn['p_type'] = $ind;
  412.                 $wyn['p_hide'] = 0;
  413.                 $wyn['p_filter'] = 0;
  414.                 $this->objProperties->add($wyn);
  415.                 $id = $this->objProperties->getLastId();
  416.  
  417.                 if ($ind != 2) {
  418.  
  419.                     $sql2 = "SELECT DISTINCT value FROM ps_feature_value f INNER JOIN ps_feature_value_lang l ON (f.id_feature_value=l.id_feature_value) "
  420.                         . "WHERE id_feature=$id_feature";
  421.                     $result2 = $link->query($sql2);
  422.                     while ($row2 = mysqli_fetch_array($result2)) {
  423.                         $row2['value'] = stripslashes($row2['value']);
  424.  
  425.                         $jest = $db->fetchOne("SELECT pv_id FROM `properties_values` WHERE p_id='$id' AND pv_value1='" . addslashes($row2['value']) . "'");
  426.                         if ($jest < 1) {
  427.                             $this->objPropertiesValues->add($id, $row2['value']);
  428.                         }
  429.                     }
  430.  
  431.                 }
  432.  
  433.             }
  434.         } catch (Exception $e) {
  435.             pre($e->getMessage());
  436.         }
  437.  
  438.         die("ok");
  439.  
  440.     }
  441.  
  442.     public function itemsAction()
  443.     {
  444.         ini_set('display_errors', 1);
  445.         ini_set('memory_limit', '2048M');
  446.         set_time_limit(0);
  447.         try {
  448.  
  449.             $link = $this->connect();
  450.  
  451.             $db = Zend_Registry::get('dbAdapter');
  452.  
  453.  
  454.             $path = '/home/centr11/domains/centrumnawadniania.ebexo.com.pl/public_html/images/items/';
  455.             $type = array('_big', '_top', '_medium', '_small', '_micro', '_prop');
  456.  
  457.  
  458.             $sql = "SELECT id_manufacturer,name FROM `ps_manufacturer` WHERE active=1 ORDER BY id_manufacturer ASC";
  459.             $result1 = $link->query($sql);
  460.             $i = 0;
  461.             while ($row1 = mysqli_fetch_array($result1)) {
  462.                 if (strlen($row1['name']) > 1) {
  463.                     $prod = trim($row1['name']);
  464.                     $producers[$row1['id_manufacturer']] = $prod;
  465.                 }
  466.             }
  467.  
  468.             $lastID = 0;
  469.             $abc = 0;
  470.             $nowe = 0;
  471.             $result1 = $link->query("SELECT p.*,l.name,l.description,l.description_short,l.meta_description,l.meta_keywords,l.meta_title FROM `ps_product` p INNER JOIN ps_product_lang l
  472.                    ON (p.id_product=l.id_product AND l.id_lang='6') WHERE p.id_product > 0 ORDER BY p.id_product ASC");
  473.             while ($v = mysqli_fetch_array($result1)) {
  474.                 try {
  475.  
  476.                     $item_id = $v['id_product'];
  477.                     // if ($item_id <= 14730) continue;
  478.  
  479.                     //$one1 = $this->objItems->getOneById($item_id);
  480.                     //if ($one1['item_id'] < 1) continue;
  481.  
  482.                     $item = array();
  483.                     $item['item_buy_price'] = $v['wholesale_price'];
  484.  
  485.  
  486.                     $item['item_id'] = $item_id;
  487.                     $item['item_name'] = $v['name'];
  488.                     $item['item_desc_short'] = $v['description_short'];
  489.                     $item['item_desc'] = $v['description'];
  490.                     switch ($v['id_tax_rules_group']) {
  491.                         case 1:
  492.                             $tax = 23;
  493.                             break;
  494.                         case 2:
  495.                             $tax = 8;
  496.                             break;
  497.                         case 3:
  498.                             $tax = 5;
  499.                             break;
  500.                         case 4:
  501.                             $tax = 0;
  502.                             break;
  503.                     }
  504.                     $item['item_vat'] = $tax;
  505.                     $item['staff_id'] = 1;
  506.                     $item['item_weight'] = $v['weight'];
  507.                     $item['item_active'] = $v['active'];
  508.                     $item['item_shipping_time'] = 1;
  509.  
  510.                     $item['item_exp_store'] = 0;
  511.                     $item['pg_id'] = 1;
  512.                     if ($v['id_manufacturer'] > 0) {
  513.                         $producent = $producers[$v['id_manufacturer']];
  514.                     }
  515.  
  516.                     if ($producent != '') {
  517.                         $p = $this->objProducers->getOneByName($producent);
  518.                         if ($p['producer_id'] > 1) {
  519.                             $item['producer_id'] = $p['producer_id'];
  520.                         } else {
  521.                             $item['producer_id'] = 1;
  522.                         }
  523.                     } else {
  524.                         $item['producer_id'] = 1;
  525.                     }
  526.  
  527.  
  528.                     $item['item_code'] = (string)$v['reference'];
  529.                     $item['item_ean'] = (string)$v['ean13'];
  530.                     $item['item_new'] = 0;
  531.  
  532.                     $priceBrutto = $v['price'] * (1 + $item['item_vat'] / 100);
  533.                     if (isset($v['reduction_price']) && ($v['reduction_price'] > 0)) {
  534.  
  535.                         $price = $priceBrutto - $v['reduction_price'];
  536.  
  537.                         if ($v['reduction_from'] == $v['reduction_to']) {
  538.                             $item['item_promo_price_from'] = '2017-10-03';
  539.                             $item['item_promo_price_to'] = '2020-12-31';
  540.                         } else {
  541.                             $item['item_promo_price_from'] = $v['reduction_from'];
  542.                             $item['item_promo_price_to'] = $v['reduction_to'];
  543.                         }
  544.  
  545.                         $item['item_price'] = $v['price'];
  546.                         $item['item_promo'] = 1;
  547.                         $item['item_promo_price'] = $price;
  548.                         $item['item_real_promo'] = 1;
  549.                         $item['item_actual_price'] = $item['item_promo_price'];
  550.                     } else {
  551.                         $item['item_promo'] = 0;
  552.                         $item['item_price'] = $v['price'];
  553.                         $item['item_actual_price'] = $priceBrutto;
  554.  
  555.                     }
  556.  
  557.                     // dodanie produktu
  558.                     try {
  559.                         $this->objItems->add($item);
  560.                     } catch (Exception $e) {
  561.                         print_r($e);
  562.                         die('aa');
  563.                     }
  564.  
  565.                     /*$sql = "SELECT name FROM `ps_category_lang` WHERE id_category = {$v['id_category_default']}";
  566.                     $cat_connections = $link->query($sql);
  567.                     while ($fetched = mysqli_fetch_array($cat_connections)) {
  568.                         $cat_name = $fetched['name'];
  569.                         $row = $db->fetchRow("SELECT id FROM cat WHERE name1 = '" . $cat_name . "'");
  570.                         $insert_conn = "INSERT INTO cat_items (cat_id, item_id) VALUES ({$row['id']},{$v['id_product']})";
  571.                         try {
  572.  
  573.                             $db->query($insert_conn);
  574.                         } catch (Exception $e) {
  575.                             print_r($e);
  576.                             die();
  577.                         }
  578.                         //print_r($row['id']); print_r($v['id_product']); die('aa');
  579.                     }*/
  580.                     $lastID = $item_id;
  581.  
  582.                     $this->_setPropMag($item_id);
  583.                     if ($this->pg_id > 0) {
  584.                         //        $item2 = array();
  585.                         //        $item2['item_exp_store'] = 1;
  586.                         //        $item2['pg_id'] = $this->pg_id;
  587.                         //        $this->objItems->save($item_id,$item2);
  588.                     } else {
  589.                         // magazyn - to dla produktow bez cech mag.
  590.                         $qt = "SELECT quantity FROM `ps_stock_available` WHERE id_product = {$item_id}";
  591.                         $quantity = $link->query($qt);
  592.                         while ($fetchedqt = mysqli_fetch_array($quantity)) {
  593.                             $amount = (int)$fetchedqt['quantity'];
  594.                         }
  595.                         $db->query("INSERT INTO items_amount (item_id,depot_id,ia_amount,ia_nolimit) VALUES('$lastID','1','$amount','0')");
  596.                     }
  597.  
  598.                 } catch (Exception $e) {
  599.                     print_r($e->getMessage());
  600.                 }
  601.  
  602.  
  603.                 //  continue;
  604.  
  605.                 $this->_setPropInf($item_id);
  606.  
  607.  
  608.                 // foto
  609.  
  610.                 //   @mkdir("{$path}{$lastID}");
  611.  
  612.  
  613.                 $fetched_images = $link->query("SELECT * FROM `ps_image` where id_product='" . $lastID . "' order by position ASC");
  614.                 while ($foto = mysqli_fetch_array($fetched_images)) {
  615.  
  616.                     //    10779-19481.jpg
  617.                     $img = $lastID . '-' . $foto['id_image'] . '.jpg';
  618.  
  619.  
  620.                     if ($img != '') {
  621.  
  622.                         // file_put_contents($path . $lastID . '/' . $foto['id_product'] . '-' . $foto['id_image'] . '_big.jpg', file_get_contents('http://www.centrumnawadniania.com/img/p/' . $img));
  623.  
  624.                         $base = $foto['id_product'] . '-' . $foto['id_image'] . '.jpg';
  625.                         foreach ($type as $ftype) {
  626.                             $fname = $foto['id_product'] . '-' . $foto['id_image'] . $ftype . '.jpg';
  627.                             if ($foto['cover'] == 1) {
  628.                                 $if_root = 1;
  629.                             } else {
  630.                                 $if_root = 0;
  631.                             }
  632.  
  633.                             $db->query("INSERT INTO items_foto (item_id,if_root,if_base_name,if_name,if_type,prop)
  634.                                                    VALUES('$lastID','$if_root','$base','$fname','$ftype','0');");
  635.                         }
  636.                     }
  637.                 }
  638.                 // die('photos');
  639.                 // end foto
  640.                 // continue;
  641.  
  642.                 // kategorie
  643.                 $sql = "INSERT INTO cat_items (cat_id,item_id) VALUES ('1','$lastID')";
  644.                 $db->query($sql);
  645.  
  646.                 $i_id = $lastID;
  647.                 $old_cats = 'SELECT id_category FROM ps_category_product WHERE id_product = "'.$item_id.'"';
  648.                 $res_old_cats = $link->query($old_cats);
  649.                 //$sql6 = "SELECT k.nowa FROM `cat` c INNER JOIN kategorie k ON (k.stara=c.id_category) WHERE id_product='$item_id'";
  650.                 //$res6 = $link->query($sql6);
  651.                 while($row6 = mysqli_fetch_array($res_old_cats)) {
  652.                     $new_cats = 'SELECT nowa FROM kategorie WHERE stara="'.$row6['id_category'].'"';
  653.                     $res_new_cats = $db->fetchAll($new_cats);
  654.                     print_r($res_new_cats);
  655.                     foreach($res_new_cats as $proper_cat){
  656.                         $c_id = $cat = $proper_cat['nowa'];
  657.                         $sql = "INSERT INTO cat_items (cat_id,item_id) VALUES ('$cat','$lastID')";
  658.  
  659.                         $db->query($sql);
  660.  
  661.                         $level = $this->treeCat->getLevel($cat);
  662.                         if($level==4) {
  663.                             $parent = $this->treeCat->getParent($c_id);
  664.                             $c_id = $parent['id'];
  665.                             $sql = "INSERT INTO cat_items (cat_id,item_id) VALUES ('$c_id','$i_id')";
  666.                             try {
  667.                                 $db->query($sql);
  668.                             } catch (Exception $e) {
  669.                                 echo $sql.' ';
  670.                                 print_r($e->getMessage());
  671.                             }
  672.                             $parent = $this->treeCat->getParent($c_id);
  673.                             $c_id = $parent['id'];
  674.                             $sql = "INSERT INTO cat_items (cat_id,item_id) VALUES ('$c_id','$i_id')";
  675.                             try {
  676.                                 $db->query($sql);
  677.                             } catch (Exception $e) {
  678.                                 echo $sql.' ';
  679.                                 print_r($e->getMessage());
  680.                             }
  681.                             $parent = $this->treeCat->getParent($c_id);
  682.                             $c_id = $parent['id'];
  683.                             $sql = "INSERT INTO cat_items (cat_id,item_id) VALUES ('$c_id','$i_id')";
  684.                             try {
  685.                                 $db->query($sql);
  686.                             } catch (Exception $e) {
  687.                                 echo $sql.' ';
  688.                                 print_r($e->getMessage());
  689.                             }
  690.                         }
  691.                         if($level==3) {
  692.                             $parent = $this->treeCat->getParent($c_id);
  693.                             $c_id = $parent['id'];
  694.                             $sql = "INSERT INTO cat_items (cat_id,item_id) VALUES ('$c_id','$i_id')";
  695.                             try {
  696.                                 $db->query($sql);
  697.                             } catch (Exception $e) {
  698.                                 echo $sql.' ';
  699.                                 print_r($e->getMessage());
  700.                             }
  701.                             $parent = $this->treeCat->getParent($c_id);
  702.                             $c_id = $parent['id'];
  703.                             $sql = "INSERT INTO cat_items (cat_id,item_id) VALUES ('$c_id','$i_id')";
  704.                             try {
  705.                                 $db->query($sql);
  706.                             } catch (Exception $e) {
  707.                                 echo $sql.' ';
  708.                                 print_r($e->getMessage());
  709.                             }
  710.                         }
  711.                         elseif($level==2) {
  712.                             $parent = $this->treeCat->getParent($c_id);
  713.                             $c_id = $parent['id'];
  714.                             $sql = "INSERT INTO cat_items (cat_id,item_id) VALUES ('$c_id','$i_id')";
  715.                             try {
  716.                                 $db->query($sql);
  717.                             } catch (Exception $e) {
  718.                                 echo $sql.' ';
  719.                                 print_r($e->getMessage());
  720.                             }
  721.                         }
  722.                     }
  723.  
  724.  
  725.                 }
  726.                 //die('jeden');
  727.             }
  728.         } catch (Exception $e) {
  729.             print_r($e->getMessage());
  730.         }
  731.         die('import complete without errors');
  732.     }
  733.  
  734.     private function _setPropInf($id)
  735.     {
  736.         $ind = array(0);
  737.         $db = Zend_Registry::get('dbAdapter');
  738.         $link = $this->connect();
  739.  
  740.  
  741.         $sql = "SELECT f.id_feature, f.id_feature_value, name, l2.value
  742. FROM `ps_feature_product` f
  743. INNER JOIN ps_feature_lang l ON ( f.id_feature = l.id_feature )
  744. INNER JOIN ps_feature_value v ON ( v.id_feature_value = f.id_feature_value )
  745. INNER JOIN ps_feature_value_lang l2 ON ( l2.id_feature_value = v.id_feature_value )
  746. WHERE `id_product` =$id";
  747.         $result = $link->query($sql);
  748.         while ($row = mysqli_fetch_array($result)) {
  749.             // id_feature name value
  750.  
  751.             $aa = $db->fetchRow("SELECT p_id,p_type FROM properties WHERE p_iname='" . $row['name'] . "' AND p_id>'29'");
  752.             $pid = $aa['p_id'];
  753.             $type = $aa['p_type'];
  754.             if ($pid > 0) {
  755.                 if ($type == 2) {
  756.                     $this->addPropInd($pid, $id, $row['value']);
  757.                 } else {
  758.                     $pv_id = $db->fetchOne("SELECT pv_id FROM `properties_values` WHERE p_id='$pid' AND pv_value1='" . addslashes($row['value']) . "'");
  759.                     if ($pv_id > 0) {
  760.                         $this->addProp($pid, $id, $pv_id);
  761.                     }
  762.  
  763.                 }
  764.             }
  765.  
  766.         }
  767.  
  768.     }
  769.  
  770.     private function addProp($id, $item_id, $val)
  771.     {
  772.         try {
  773.             $d = array();
  774.             $d['p_id'] = $id;
  775.             $d['item_id'] = $item_id;
  776.             $d['pv_id'] = $val;
  777.             $this->objPropertiesValMulti->add($d);
  778.         } catch (Exception $e) {
  779.         }
  780.     }
  781.  
  782.     private function addPropInd($id, $item_id, $val)
  783.     {
  784.         try {
  785.             $d = array();
  786.             $d['p_id'] = $id;
  787.             $d['item_id'] = $item_id;
  788.             $d['pvi_value'] = $val;
  789.             $this->objPropertiesValIndividual->add($d);
  790.         } catch (Exception $e) {
  791.         }
  792.     }
  793.  
  794.     private function _setPropMag($id)
  795.     {
  796.         $this->pg_id = 0;
  797.         $db = Zend_Registry::get('dbAdapter');
  798.         $link = $this->connect();
  799.  
  800.  
  801.         $sql = "SELECT a.*,c.id_attribute,l.name,p2.id_attribute_group FROM `ps_product_attribute` a INNER JOIN ps_product_attribute_combination c
  802. ON (c.id_product_attribute=a.id_product_attribute) INNER JOIN ps_attribute_lang l ON (l.id_attribute=c.id_attribute AND l.id_lang=3)
  803. INNER JOIN ps_attribute p2 ON (p2.id_attribute = c.id_attribute) WHERE a.id_product=$id ORDER BY a.id_product_attribute ASC, p2.id_attribute_group ASC ";
  804.         $result = $link->query($sql);
  805.         $i = 0;
  806.         $opt = array();
  807.         while ($row = mysqli_fetch_array($result)) {
  808.             $opt[] = $row;
  809.         }
  810.         if (count($opt) < 1) {
  811.             return 1; // brak cech mag.
  812.         }
  813.         if ($opt[0]['id_product_attribute'] == $opt[1]['id_product_attribute'] && $opt[1]['id_product_attribute'] == $opt[2]['id_product_attribute']) {
  814.             //2 cechy
  815.  
  816.             $name = [];
  817.             $sql = "SELECT name, p.id_attribute_group
  818.                FROM `ps_attribute_group` a
  819.                INNER JOIN ps_attribute_group_lang l ON ( l.id_attribute_group = a.id_attribute_group )
  820.                INNER JOIN ps_attribute p ON ( p.id_attribute_group = a.id_attribute_group )
  821.                WHERE   l.id_lang = 3 AND p.id_attribute =" . $opt[0]['id_attribute'];
  822.             $result2 = $link->query($sql);
  823.             while ($row2 = mysqli_fetch_array($result2)) {
  824.                 $name[$row2['name']] = $row2['id_attribute_group'];
  825.             }
  826.  
  827.             $sql = "SELECT name, p.id_attribute_group
  828.                FROM `ps_attribute_group` a
  829.                INNER JOIN ps_attribute_group_lang l ON ( l.id_attribute_group = a.id_attribute_group )
  830.                INNER JOIN ps_attribute p ON ( p.id_attribute_group = a.id_attribute_group )
  831.                WHERE   l.id_lang = 3 AND p.id_attribute =" . $opt[1]['id_attribute'];
  832.             $result2 = $link->query($sql);
  833.             while ($row2 = mysqli_fetch_array($result2)) {
  834.                 $name[$row2['name']] = $row2['id_attribute_group'];
  835.             }
  836.  
  837.             $sql = "SELECT name, p.id_attribute_group
  838.                FROM `ps_attribute_group` a
  839.                INNER JOIN ps_attribute_group_lang l ON ( l.id_attribute_group = a.id_attribute_group )
  840.                INNER JOIN ps_attribute p ON ( p.id_attribute_group = a.id_attribute_group )
  841.                WHERE   l.id_lang = 3 AND p.id_attribute =" . $opt[2]['id_attribute'];
  842.             $result2 = $link->query($sql);
  843.             while ($row2 = mysqli_fetch_array($result2)) {
  844.                 $name[$row2['name']] = $row2['id_attribute_group'];
  845.             }
  846.  
  847.             asort($name);
  848.             $names = array_keys($name);
  849.             $group = substr(implode(" + ", $names), 0, 32);
  850.  
  851.             $pg_id = $db->fetchOne("SELECT pg_id FROM `properties_groups` WHERE pg_name='" . $group . "'");
  852.  
  853.             if ($pg_id > 0) {
  854.                 $this->pg_id = $pg_id;
  855.  
  856.                 $pid = [];
  857.                 $pid[] = $db->fetchOne("SELECT p_id FROM properties WHERE p_iname='" . $names[0] . "'  AND p_id < 30");
  858.                 $pid[] = $db->fetchOne("SELECT p_id FROM properties WHERE p_iname='" . $names[1] . "'  AND p_id < 30");
  859.                 $pid[] = $db->fetchOne("SELECT p_id FROM properties WHERE p_iname='" . $names[2] . "'  AND p_id < 30");
  860.  
  861.  
  862.                 if (count($pid) == 2 && $pid[0] != '' && $pid[1] != '' && $pid[2] != '') {
  863.                     $i = 0;
  864.                     $grupuj = [];
  865.                     foreach ($opt as $k => $v) {
  866.                         $grupuj[$v['id_product_attribute']][] = $v;
  867.                     }
  868.  
  869.                     foreach ($grupuj as $id_product_attribute => $z) {
  870.                         $c = 0;
  871.                         $pv_id = [];
  872.                         foreach ($z as $k2 => $v) {
  873.                             $pv_id[] = $db->fetchOne("SELECT pv_id FROM `properties_values` WHERE p_id='$pid[$c]'
  874.                                                    AND pv_value1='" . addslashes($v['name']) . "'");
  875.                             $c++;
  876.                         }
  877.  
  878.                         $es = array();
  879.                         $es['item_id'] = $id;
  880.                         $es['pv_id_1'] = $pv_id[0];
  881.                         $es['pv_id_2'] = $pv_id[1];
  882.                         $es['pv_id_3'] = $pv_id[2];
  883.                         $es['item_code'] = (string)$v['reference'];
  884.                         $es['item_price'] = 0;
  885.                         $es['item_weight'] = $v['weight'];
  886.                         //  $this->objItemsES->add($es);
  887.                         $aa = $this->objItemsES->getOneByAllId($id, $pv_id[0], $pv_id[1], $pv_id[2]);
  888.                         if ($aa['es_id'] > 0) {
  889.                             $amount = array();
  890.                             $amount['item_id'] = $id;
  891.                             $amount['depot_id'] = 1;
  892.                             $amount['mag_id'] = $aa['es_id'];
  893.                             $amount['ia_amount'] = (int)$v['quantity'];
  894.                             $amount['ia_nolimit'] = 0;
  895.                             $this->objAmount->add($amount);
  896.                         }
  897.                     }
  898.  
  899.                 }
  900.  
  901.             }
  902.  
  903.         } elseif ($opt[0]['id_product_attribute'] == $opt[1]['id_product_attribute']) {
  904.             //2 cechy
  905.  
  906.             $name = [];
  907.             $sql = "SELECT name, p.id_attribute_group
  908.                FROM `ps_attribute_group` a
  909.                INNER JOIN ps_attribute_group_lang l ON ( l.id_attribute_group = a.id_attribute_group )
  910.                INNER JOIN ps_attribute p ON ( p.id_attribute_group = a.id_attribute_group )
  911.                WHERE   l.id_lang = 3 AND p.id_attribute =" . $opt[0]['id_attribute'];
  912.             $result2 = $link->query($sql);
  913.             while ($row2 = mysqli_fetch_array($result2)) {
  914.                 $name[$row2['name']] = $row2['id_attribute_group'];
  915.             }
  916.  
  917.             $sql = "SELECT name, p.id_attribute_group
  918.                FROM `ps_attribute_group` a
  919.                INNER JOIN ps_attribute_group_lang l ON ( l.id_attribute_group = a.id_attribute_group )
  920.                INNER JOIN ps_attribute p ON ( p.id_attribute_group = a.id_attribute_group )
  921.                WHERE   l.id_lang = 3 AND p.id_attribute =" . $opt[1]['id_attribute'];
  922.             $result2 = $link->query($sql);
  923.             while ($row2 = mysqli_fetch_array($result2)) {
  924.                 $name[$row2['name']] = $row2['id_attribute_group'];
  925.             }
  926.  
  927.             asort($name);
  928.             $names = array_keys($name);
  929.             $group = substr(implode(" + ", $names), 0, 32);
  930.  
  931.             $pg_id = $db->fetchOne("SELECT pg_id FROM `properties_groups` WHERE pg_name='" . $group . "'");
  932.  
  933.             if ($pg_id > 0) {
  934.                 $this->pg_id = $pg_id;
  935.  
  936.                 $pid = [];
  937.                 $pid[] = $db->fetchOne("SELECT p_id FROM properties WHERE p_iname='" . $names[0] . "'  AND p_id < 30");
  938.                 $pid[] = $db->fetchOne("SELECT p_id FROM properties WHERE p_iname='" . $names[1] . "'  AND p_id < 30");
  939.  
  940.  
  941.                 if (count($pid) == 2 && $pid[0] != '' && $pid[1] != '') {
  942.                     $i = 0;
  943.                     $grupuj = [];
  944.                     foreach ($opt as $k => $v) {
  945.                         $grupuj[$v['id_product_attribute']][] = $v;
  946.                     }
  947.  
  948.  
  949.                     foreach ($grupuj as $id_product_attribute => $z) {
  950.                         $c = 0;
  951.                         $pv_id = [];
  952.                         foreach ($z as $k2 => $v) {
  953.                             $pv_id[] = $db->fetchOne("SELECT pv_id FROM `properties_values` WHERE p_id='$pid[$c]'
  954.                                                    AND pv_value1='" . addslashes($v['name']) . "'");
  955.                             $c++;
  956.                         }
  957.  
  958.                         $es = array();
  959.                         $es['item_id'] = $id;
  960.                         $es['pv_id_1'] = $pv_id[0];
  961.                         $es['pv_id_2'] = $pv_id[1];
  962.                         $es['pv_id_3'] = 0;
  963.                         $es['item_code'] = (string)$v['reference'];
  964.                         $es['item_price'] = 0;
  965.                         $es['item_weight'] = $v['weight'];
  966.                         //     $this->objItemsES->add($es);
  967.                         $aa = $this->objItemsES->getOneByAllId($id, $pv_id[0], $pv_id[1], 0);
  968.                         if ($aa['es_id'] > 0) {
  969.                             $amount = array();
  970.                             $amount['item_id'] = $id;
  971.                             $amount['depot_id'] = 1;
  972.                             $amount['mag_id'] = $aa['es_id'];
  973.                             $amount['ia_amount'] = (int)$v['quantity'];
  974.                             $amount['ia_nolimit'] = 0;
  975.                             $this->objAmount->add($amount);
  976.                         }
  977.                     }
  978.  
  979.                 }
  980.  
  981.             }
  982.  
  983.         } else {
  984.             // 1 cecha
  985.             $sql = "SELECT name, p.id_attribute_group
  986.                FROM `ps_attribute_group` a
  987.                INNER JOIN ps_attribute_group_lang l ON ( l.id_attribute_group = a.id_attribute_group )
  988.                INNER JOIN ps_attribute p ON ( p.id_attribute_group = a.id_attribute_group )
  989.                WHERE  l.id_lang = 3 AND p.id_attribute =" . $opt[0]['id_attribute'];
  990.             $result2 = $link->query($sql);
  991.             while ($row2 = mysqli_fetch_array($result2)) {
  992.                 $this->pg_id = $row2['id_attribute_group'] + 1;
  993.                 $name = $row2['name'];
  994.             }
  995.  
  996.             $pid = $db->fetchOne("SELECT p_id FROM properties WHERE p_iname='" . $name . "' AND p_id < 30");
  997.             if ($pid > 0) {
  998.                 foreach ($opt as $k => $v) {
  999.  
  1000.                     $pv_id = $db->fetchOne("SELECT pv_id FROM `properties_values` WHERE p_id='$pid' AND pv_value1='" . addslashes($v['name']) . "'");
  1001.  
  1002.                     $es = array();
  1003.                     $es['item_id'] = $id;
  1004.                     $es['pv_id_1'] = $pv_id;
  1005.                     $es['pv_id_2'] = 0;
  1006.                     $es['pv_id_3'] = 0;
  1007.                     $es['item_code'] = (string)$v['reference'];
  1008.                     $es['item_price'] = 0;
  1009.                     $es['item_weight'] = $v['weight'];
  1010.                     //    $this->objItemsES->add($es);
  1011.                     $aa = $this->objItemsES->getOneByAllId($id, $pv_id[0], 0, 0);
  1012.  
  1013.                     //    $es_id =  $db->fetchOne("SELECT es_id FROM `itemses` WHERE item_id='$id' AND pv_id_1='".$pv_id."'");
  1014.                     //    if($es_id<1) continue;
  1015.                     $atr = $v['id_product_attribute'];
  1016.                     if ($aa['es_id'] > 0) {
  1017.                         $amount = array();
  1018.                         $amount['item_id'] = $id;
  1019.                         $amount['depot_id'] = 1;
  1020.                         $amount['mag_id'] = $aa['es_id'];
  1021.                         $amount['ia_amount'] = (int)$v['quantity'];
  1022.                         $amount['ia_nolimit'] = 0;
  1023.                         $this->objAmount->add($amount);
  1024.                     }
  1025.                 }
  1026.             }
  1027.         }
  1028.     }
  1029.  
  1030.     public function usersAction()
  1031.     {
  1032.         $i=0;
  1033.         ini_set('display_errors', 1);
  1034.         // sprawdz czy klient o takim mailu istnieje
  1035.         // jesli nie to dodaj
  1036.         // potem dodaj zamowienie
  1037.         // potem produkty z zamowienia
  1038.  
  1039.         $link = $this->connect();
  1040.  
  1041.         try {
  1042.             $result1 = $link->query("SELECT DISTINCT email,c.id_customer,c.firstname,c.lastname,a.address1,a.city,a.postcode,c.newsletter,
  1043.                    a.phone,a.company,a.phone_mobile
  1044.                    FROM `ps_customer` c
  1045.                    LEFT JOIN ps_address a ON (a.id_customer=c.id_customer)
  1046.                    GROUP BY email  ORDER BY c.id_customer ASC  ");
  1047.  
  1048.             while ($v = mysqli_fetch_array($result1)) {
  1049.                 $email = $v['email'];
  1050.  
  1051.  
  1052.                 if ($this->objUsers->isExist($email)) {
  1053.                     $user = $this->objUsers->getOneByEmail($email);
  1054.                     $u_id = $user['user_id'];
  1055.                 } else {
  1056.  
  1057.                     $user = array();
  1058.                     $user['user_id'] = $v['id_customer'];
  1059.                     $user['user_type'] = strlen($v['company']) > 3 ? 2 : 1;
  1060.                     $user['user_password'] = rand(1000, 10000);
  1061.                     if ($user['user_type'] == 2) {
  1062.                         $user['user_firm'] = $v['company'];
  1063.                     } else {
  1064.                         $user['user_firm'] = '';
  1065.                     }
  1066.                     $user['user_name'] = $v['firstname'];
  1067.                     $user['user_surname'] = $v['lastname'];
  1068.                     $phone = $v['phone'];
  1069.                     if ($phone == '') $phone = $v['phone_mobile'];
  1070.                     $user['user_phone'] = str_replace(' ', '', str_replace('-', '', trim($phone)));
  1071.                     $user['user_email'] = $email;
  1072.                     $user['user_street'] = $v['address1'];
  1073.                     $user['user_postcode'] = $v['postcode'];
  1074.                     $user['user_city'] = $v['city'];
  1075.                     $user['user_country'] = 'pl';
  1076.                     $user['user_nip'] = '';
  1077.                     $user['user_vat'] = $user['user_type'] == 2 ? 1 : 0;
  1078.                     $user['user_newsletter'] = $v['newsletter'];
  1079.                     $user['user_activate'] = 1;
  1080.                     $user['user_active'] = 1;
  1081.                     try {
  1082.                         $this->objUsers->add($user);
  1083.                     } catch (Exception $e) {
  1084.                         print_r($e->getMessage());
  1085.                     }
  1086.                     $u_id = $this->objUsers->getLastId();
  1087.                 }
  1088.             }
  1089.  
  1090.             die('import complete without errors');
  1091.         } catch (Exception $e) {
  1092.             print_r($e->getMessage());
  1093.         }
  1094.  
  1095.  
  1096.     }
  1097.  
  1098.     public function ordersAction()
  1099.     {
  1100.         set_time_limit(0);
  1101.         ini_set('memory_limit', '1024M');
  1102.         $lang = 1;
  1103.         ini_set('display_errors', 1);
  1104.         $link = $this->connect();
  1105.  
  1106.  
  1107.         $db = Zend_Registry::get('dbAdapter');
  1108.  
  1109.         $statusy = array();
  1110.         $statusy[10] = 100;
  1111.         $statusy[26] = 1;
  1112.         $statusy[17] = 1;
  1113.         $statusy[5] = 1;
  1114.         $statusy[24] = 1;
  1115.         $statusy[11] = 2;
  1116.         $statusy[21] = 2;
  1117.         $statusy[32] = 2;
  1118.         $statusy[28] = 2;
  1119.         $statusy[14] = 1;
  1120.         $statusy[4] = 1;
  1121.         $statusy[7] = 1;
  1122.         $statusy[30] = 1;
  1123.         $statusy[1] = 1;
  1124.         $statusy[2] = 1;
  1125.         $statusy[15] = 90;
  1126.         $statusy[29] = 1;
  1127.         $statusy[33] = 1;
  1128.         $statusy[13] = 1;
  1129.         $statusy[18] = 9;
  1130.         $statusy[8] = 1;
  1131.         $statusy[25] = 1;
  1132.         $statusy[19] = 1;
  1133.         $statusy[20] = 1;
  1134.         $statusy[31] = 1;
  1135.         $statusy[6] = 1;
  1136.         $statusy[3] = 9;
  1137.         $statusy[22] = 5;
  1138.         $statusy[16] = 9;
  1139.         $statusy[9] = 9;
  1140.         $statusy[27] = 9;
  1141.         $statusy[23] = 2;
  1142.         $statusy[12] = 2;
  1143.  
  1144.         try {
  1145.  
  1146.             $sql = "SELECT * FROM ps_orders ORDER  BY id_order ASC";
  1147.             $result1 = $link->query($sql);
  1148.             $i = 0;
  1149.             $elo = 0;
  1150.             while ($row = mysqli_fetch_array($result1)) {
  1151.                 $u_id = $row['id_customer'];
  1152.  
  1153.                 $sql1 = "SELECT id_order_state  FROM `ps_order_history` WHERE `id_order` = '" . $row['id_order'] . "' order by id_order_history DESC LIMIT 1";
  1154.                 $res2 = $link->query($sql1);
  1155.                 while ($row2 = mysqli_fetch_array($res2)) {
  1156.                     $state = $row2['id_order_state'];
  1157.                 }
  1158.  
  1159.                 $ss = $statusy[$state];
  1160.                 if ($ss < 1) $ss = 100;
  1161.  
  1162.                 // zamowienie
  1163.                 $order = array();
  1164.                 $order['order_id'] = $row['id_order'];
  1165.                 $order['order_status'] = $ss;
  1166.                 try {
  1167.                     $user = $this->objUsers->getOneById($u_id);
  1168.                 } catch (Exception $e) {
  1169.                     print_r($e);
  1170.                 }
  1171.                 if ($user['user_id'] > 0) {
  1172.                     $order['user_id'] = $u_id;
  1173.                 } else {
  1174.  
  1175.                     $sql4 = "SELECT * FROM ps_customer WHERE id_customer='$u_id'";
  1176.                     $result4 = $link->query($sql4);
  1177.                     while ($row4 = mysqli_fetch_array($result4)) {
  1178.  
  1179.                         $email = $row4['email'];
  1180.                     }
  1181.  
  1182.                     $user = $this->objUsers->getOneByEmail($email);
  1183.                     if (!$user) {
  1184.                         $user = array();
  1185.                         $user['user_type'] = 1;
  1186.                         $user['user_email'] = $email;
  1187.                         $user['user_name'] = $row4['firstname'];
  1188.                         $user['user_surname'] = $row4['lastname'];
  1189.                     }
  1190.                     if ($user['user_email'] == '') {
  1191.                         $user['user_email'] = ' ';
  1192.                     }
  1193.  
  1194.                 }
  1195.  
  1196.                 $order['order_date'] = $row['date_add'];
  1197.                 $order['order_email'] = $user['user_email'];
  1198.                 $order['order_payer'] = $user['user_type'];
  1199.                 $order['order_new'] = 0;
  1200.  
  1201.                 if ($user['user_type'] == 2) {
  1202.                     $who = $user['user_firm'];
  1203.                 } else {
  1204.                     $who = $user['user_name'] . ' ' . $user['user_surname'];
  1205.                 }
  1206.                 $order['order_pay_to'] = $who . "\n" . $user['user_street'] . '||' . $user['user_home'] . '||' . $user['user_flat'] . '||' . $user['user_postcode'] . '||' . $user['user_city'] . '||Polska';
  1207.                 if ($user['user_nip'] != '')
  1208.                     $order['order_pay_to'] .= "\n" . $user['user_nip'];
  1209.                 else
  1210.                     $order['order_pay_to'] .= "\n";
  1211.  
  1212.                 if ($user['user_phone'] != '') {
  1213.                     $order['order_pay_to'] .= "\n" . $user['user_phone'];
  1214.                 } else {
  1215.                     $order['order_pay_to'] .= "\n";
  1216.                 }
  1217.  
  1218.                 $who = $user['user_name'] . ' ' . $user['user_surname'];
  1219.                 $order['order_send_to'] = $who . "\n" . $user['user_street'] . '||' . $user['user_home'] . '||' . $user['user_flat'] . '||' . $user['user_postcode'] . '||' . $user['user_city'] . '||Polska';
  1220.                 if ($user['user_phone'] != '') {
  1221.                     $order['order_send_to'] .= "\n\n" . $user['user_phone'];
  1222.                 }
  1223.  
  1224.                 $order['order_fvat'] = $user['user_type'] == 2 ? 1 : 0;
  1225.  
  1226.  
  1227.                 $amount = 0;
  1228.                 $suma = 0;
  1229.                 $positions = $link->query("SELECT * FROM ps_order_detail WHERE id_order='$row[id_order]'");
  1230.  
  1231.                 while ($b = mysqli_fetch_array($positions)) {
  1232.                     $amount += $b['product_quantity'];
  1233.  
  1234.                     $cena = $b['unit_price_tax_incl'];
  1235.  
  1236.                     $suma += ($cena * $b['product_quantity']);
  1237.                 }
  1238.                 $order['order_amount'] = $amount;
  1239.                 $order['order_price'] = $suma;
  1240.                 $order['order_delivery_country_code'] = 'pl';
  1241.                 $order['order_supplier_name'] = 'kurier';
  1242.                 $order['order_payment_name'] = $row['payment'];
  1243.                 $order['order_payment_type'] = $row['module'] == 'cashondelivery' ? 'cod' : 'transfer';
  1244.                 $order['order_delivery_price'] = $row['total_shipping'];
  1245.                 $order['order_topay'] = $row['total_paid'];
  1246.  
  1247.                 $order['order_currency'] = 'PLN';
  1248.                 $order['order_exchange'] = 1;
  1249.                 $order['order_lang'] = 'pl';
  1250.                 $order['order_source'] = 'prestashop';
  1251.                 $this->objOrders->add($order);
  1252.                 $order_id = $this->objOrders->getLastId();
  1253.  
  1254.                 $positions = $link->query("SELECT * FROM ps_order_detail WHERE id_order='$row[id_order]'");
  1255.                 while ($b = mysqli_fetch_array($positions)) {
  1256.  
  1257.                     //  $prod = $link->query("SELECT nowe FROM prod WHERE stare='$b[product_id]'");
  1258.                     //   $iii = mysql_fetch_row($prod);
  1259.  
  1260.                     $vat = 0.23;
  1261.                     $cena = $b['unit_price_tax_incl'];
  1262.                     $cena2 = $b['product_quantity_discount'];
  1263.  
  1264.  
  1265.                     $oi = array();
  1266.                     $item = $this->objItems->getOneById($b[product_id]);
  1267.                     if ($b['product_reference'] == '') $b['product_reference'] = ' ';
  1268.                     if ($item['item_id'] > 0) {
  1269.                         $oi['oi_item_id'] = $b[product_id];
  1270.                         $oi['oi_item_code'] = $b['product_reference'];
  1271.                         $oi['oi_item_name'] = $b['product_name'];
  1272.                     } else {
  1273.                         $oi['oi_item_id'] = NULL;
  1274.                         $oi['oi_item_code'] = $b['product_reference'];
  1275.                         $oi['oi_item_name'] = $b['product_name'];
  1276.                     }
  1277.                     $oi['order_id'] = $order_id;
  1278.                     $oi['oi_type'] = 1;
  1279.                     $oi['oi_mag'] = 0;
  1280.                     $oi['oi_mag_id'] = NULL;
  1281.                     $oi['oi_item_desc'] = '';
  1282.  
  1283.                     $oi['oi_item_price'] = $cena;
  1284.                     $oi['oi_item_vat'] = 23;
  1285.  
  1286.                     if ($cena2 > 0) {
  1287.                         $oi['oi_promo'] = 1;
  1288.                         $oi['oi_promo_price'] = $cena2;
  1289.                     }
  1290.  
  1291.                     $oi['oi_amount'] = $b['product_quantity'];
  1292.                     $this->objOrdersItems->add($oi);
  1293.                 }
  1294.  
  1295.                 // Zapisujemy informacje dot. transportu jako pozycje w orders_items
  1296.                 // start : saveTG
  1297.                 $d = '';
  1298.                 $d['order_id'] = $order_id;
  1299.                 $d['oi_type'] = 2; // - transport
  1300.                 $d['oi_item_id'] = null;
  1301.                 $d['oi_mag'] = null;
  1302.                 $d['oi_mag_id'] = null;
  1303.                 $d['oi_item_name'] = "Transport";
  1304.                 $d['oi_item_desc'] = null;
  1305.                 $d['oi_item_code'] = "";
  1306.                 $d['oi_item_price'] = $order['order_delivery_price'];
  1307.                 $d['oi_item_vat'] = 23;
  1308.                 $d['oi_amount'] = 1;
  1309.  
  1310.                 $this->objOrdersItems->saveTG($d);
  1311.                 // end : saveTG
  1312.  
  1313.             }
  1314.  
  1315.             die('import complete without errors');
  1316.         } catch (Exception $e) {
  1317.             print_r($e->getMessage());
  1318.         }
  1319.  
  1320.         die('import WITH errors');
  1321.     }
  1322.  
  1323.     public function blogcatAction()
  1324.     {
  1325.         ini_set('display_errors', 1);
  1326.         $link = $this->connect();
  1327.         $db = Zend_Registry::get('dbAdapter');
  1328.         try {
  1329.             $result1 = $link->query("SELECT b.id_smart_blog_category as id, bl.meta_title as title, bl.link_rewrite as url,
  1330.                                      b.id_parent as parent FROM `ps_smart_blog_category` b
  1331.                    LEFT JOIN ps_smart_blog_category_lang bl ON (b.id_smart_blog_category=bl.id_smart_blog_category)");
  1332.  
  1333.             while ($blog = mysqli_fetch_array($result1)) {
  1334.                 $id = ++$blog['id'];
  1335.                 $title = $blog['title'];
  1336.                 $url = $blog['url'];
  1337.                 $parent = ++$blog['parent'];
  1338.  
  1339.                 try {
  1340.                     $db->query('INSERT INTO news_cat (id, name1, name2, name3, name4, name5, cat_url1,parent,root) VALUES (' . $id . ',"' . $title . '","","","","","' . $url . '",' . $parent . ',0)');
  1341.                 } catch (Exception $e) {
  1342.                     print_r($e);
  1343.                 }
  1344.             }
  1345.             die('import complete without errors');
  1346.         } catch (Exception $e) {
  1347.             print_r($e->getMessage());
  1348.         }
  1349.  
  1350.     }
  1351.  
  1352.     public function blogpostsAction()
  1353.     {
  1354.         ini_set('display_errors', 1);
  1355.         $link = $this->connect();
  1356.         $db = Zend_Registry::get('dbAdapter');
  1357.         try {
  1358.             $result1 = $link->query("SELECT b.id_smart_blog_post as id, b.id_category as cat,
  1359.                                            bl.meta_title as title, bl.link_rewrite as url,
  1360.                                            bl.meta_description as des, bl.meta_keyword as keyw,
  1361.                                          bl.content as post FROM `ps_smart_blog_post` b
  1362.                    LEFT JOIN ps_smart_blog_post_lang bl ON (b.id_smart_blog_post=bl.id_smart_blog_post) WHERE b.active = 1");
  1363.  
  1364.             while ($blog = mysqli_fetch_array($result1)) {
  1365.                 $id = $blog['id'];
  1366.                 $cat = ++$blog['cat'];
  1367.                 $title = $blog['title'];
  1368.                 $url = strtolower(removePolishChars($blog['url']));
  1369.                 $post = addslashes($blog['post']);
  1370.                 $desc = $blog['des'];
  1371.                 $keyw = $blog['keyw'];
  1372.                 //print_r($id); die('dd')
  1373.  
  1374.                 if ($id == 58) {
  1375.  
  1376.                     try {
  1377.  
  1378.  
  1379.                         $db->query('INSERT INTO news (news_id, news_author,
  1380.                  news_title1, news_title2, news_title3, news_title4, news_title5, news_url1,
  1381.                  news_content5, news_content4, news_content3, news_content2, news_content1,
  1382.                  google_title1, google_title2, google_title3, google_title4, google_title5,
  1383.                  google_desc1, google_desc2, google_desc3, google_desc4, google_desc5,
  1384.                  google_key_words1, google_key_words2, google_key_words3, google_key_words4, google_key_words5)
  1385.          VALUES (' . $id . ',"admin",
  1386.                  "' . $title . '","","","","","' . $url . '",
  1387.                  "", "", "", "", "' . $post . '",
  1388.                  "' . $title . '", "", "", "", "",
  1389.                  "' . $desc . '", "", "", "", "",
  1390.                  "' . $keyw . '", "", "", "", "")');
  1391.  
  1392.                         //die('asd');
  1393.                     } catch (Exception $e) {
  1394.                         print_r($e);
  1395.                         die('aa');
  1396.                     }
  1397.                     try {
  1398.                         $db->query('INSERT INTO news_items (cat_id, news_id, hi_order) VALUES (' . $cat . ',' . $id . ', 0)');
  1399.                     } catch (Exception $e) {
  1400.                         print_r($e);
  1401.                     }
  1402.  
  1403.                     try {
  1404.                         $path = '/home/centr11/domains/centrumnawadniania.ebexo.com.pl/public_html/images/news/';
  1405.                         $type = ['_big', '_top', '_medium', '_small', '_micro', '_prop'];
  1406.                         @mkdir("{$path}{$id}");
  1407.  
  1408.                         $img = $id . '.jpg';
  1409.  
  1410.                         if ($img != '') {
  1411.  
  1412.                             file_put_contents($path . $id . '/' . $id . '_big.jpg', file_get_contents('https://www.centrumnawadniania.com/modules//smartblog/images/' . $id . '-single-default.jpg'));
  1413.                             foreach ($type as $ftype) {
  1414.                                 $fname = $id . $ftype . '.jpg';
  1415.  
  1416.                                 $db->query("INSERT INTO news_foto (news_id,nf_root,nf_base_name,nf_name,nf_type, nf_order)
  1417.                                                    VALUES('$id','1','$img','$fname','$ftype', 0)");
  1418.                             }
  1419.                         }
  1420.                     } catch (Exception $e) {
  1421.                         print_r($e);
  1422.                         die();
  1423.                     }
  1424.                 }
  1425.             }
  1426.             die('import complete without errors');
  1427.         } catch (Exception $e) {
  1428.             print_r($e->getMessage());
  1429.         }
  1430.  
  1431.     }
  1432. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement