%PDF- %PDF-
| Direktori : /mnt/tnb2/git/import-prestashop/ |
| Current File : //mnt/tnb2/git/import-prestashop/StavProdejny.php |
<?php
/**
* Created by PhpStorm.
* User: Ludek
* Date: 19. 10. 2016
* Time: 12:58
*/
include "Nakladatel.php";
include "database.php";
class StavProdejny extends Nakladatel
{
public $idKnihy;
public $cena;
public $stavajiciMnozstvi;
public $pocitadloKolikZpracuji;
public function __construct()
{
}
function zpracujData()
{
$this->pocitadloKolikZpracuji = 0;
$aktivni = 1;
$minus1 = -1;
$nula = 0;
// $a = file_get_contents("news.txt");
// $f =fopen("news.txt", "w");
if (($handle = fopen('php://input', 'r')) !== FALSE)
{
while (($radek = fgetcsv($handle, 0, ";")) !== FALSE)
{
//print_r($radek); echo "\n ---- \n";
$ean = substr((string)$radek[2],0,13);
$mnozstvi = $radek[1];
$nazev = $radek[3];
$vyprodej = $radek[4];
$cenaSklad = $radek[5];
// echo "ean - " . $ean . " mnozstvi - " . (string)$mnozstvi . "\n";
if ($ean == "")
continue; // neimportovat, nema ean
$nasel = $this->zjistiMnozstviNaProdejne($ean);
if (!$nasel)
{
// fwrite($f, "Nenalezen EAN $ean - $nazev\n".$a);
// echo "Nenalezen EAN $ean - $nazev\n";
continue; // neni v eshopu
}
if ($mnozstvi <> $this->stavajiciMnozstvi or $mnozstvi <> 0) // zmena mnozstvi
{
$this->pocitadloKolikZpracuji = $this->pocitadloKolikZpracuji + 1;
//if (!($this->pocitadloKolikZpracuji % 10))
// echo $this->pocitadloKolikZpracuji . " zapsat nove mnozstvi " . $ean . " - " . (string)$mnozstvi . " \n"; // Hlaska pouze kazde desate vety
dibi::update("ps_stock_available", array('quantity' => $mnozstvi))->where("id_product = %i", $this->idKnihy)->execute();
dibi::update("ps_product_shop", array('active' => $aktivni))->where("id_product = %i", $this->idKnihy)->execute(); // zaktivovat
dibi::update("ps_product", array('active' => $aktivni))->where("id_product = %i", $this->idKnihy)->execute(); // zaktivovat
}
else
continue;
// Sleva
if ($vyprodej == "v")
{
echo "$ean - $vyprodej - $cenaSklad - \n";
// fwrite($f, "$ean - $vyprodej - $cenaSklad - \n".$a);
$jeCena = $this->zjistiCenu($ean);
if (!$jeCena)
continue; // neni v eshopu
$jeVeSleve = $this->zjistiSlevu($this->idKnihy);
if ($jeVeSleve)
continue; // neni v tabulce slev
if ($this->cena <> 0)
$sleva = sprintf("%0.2f", ($this->cena-$cenaSklad)/$this->cena); // zaokrouhleni na dve des. mista
else
$sleva = 0;
// echo "Cena v eshopu: $this->cena \n";
// fwrite($f, "Cena v eshopu: $this->cena \n".$a);
$data = array("id_specific_price"=>$this->idKnihy, "id_specific_price_rule"=>$nula, "id_cart"=>$nula, "id_product"=>$this->idKnihy, "id_shop"=>$nula,
"id_shop_group"=>$nula, "id_currency"=>$nula, "id_country"=>$nula, "id_group"=>$nula, "id_customer"=>$nula, "id_product_attribute"=>$nula,
"price"=>$minus1, "from_quantity"=>$aktivni, "reduction"=>$sleva, "reduction_tax"=>$aktivni, "reduction_type"=>"percentage",
"from"=>"0000-00-00 00:00:00", "to"=>"0000-00-00 00:00:00");
dibi::query("INSERT INTO [ps_specific_price]", $data);
// dibi::update("ps_product", array('on_sale' => $aktivni))->where("id_product = %i", $this->idKnihy)->execute();
// dibi::update("ps_product_shop", array('on_sale' => $aktivni))->where("id_product = %i", $this->idKnihy)->execute();
}
if ($vyprodej == "vp")
$zrus = dibi::query( 'delete from [id_specific_price] where [id_product]=%i',$this->idKnihy);
}
fclose($handle);
// fclose($f);
echo $this->pocitadloKolikZpracuji . " celkovy pocet zapsanych zmen \n";
}
}
function zjistiMnozstviNaProdejne($ean)
{
/* $result = dibi::query("select [ps_product].[id_product], [ps_product_lang].[available_later] from [ps_product]
join [ps_product_lang] on [ps_product].[id_product]=[ps_product_lang].[id_product]
where [ps_product_lang].[id_lang]=1 AND [ps_product].[ean13]=%s", $ean)->fetch(); */
$prodId = $this->eanToIdSafe($ean);
if(!$prodId) return false;
$this->idKnihy = $prodId;
$result = dibi::query("SELECT [quantity] FROM [ps_stock_available] WHERE [id_product]=%s", $this->idKnihy);
if(count($result) == 0)
return false;
$row = $result->fetch();
$this->stavajiciMnozstvi = $row->quantity;
return true;
}
function doplnCeny()
{
$this->pocitadloKolikZpracuji = 0;
$aktivni = 1;
if (($handle = fopen('import/monami_ceny.csv', 'r')) !== FALSE)
{
while (($radek = fgetcsv($handle, 0, ",")) !== FALSE)
{
$ean = (string)$radek[0];
$novaCena = floatval((string)$radek[1]);
$sazbaDph = floatval((string)$radek[2]);
if ($sazbaDph == 10)
$dph = 5;
elseif ($sazbaDph == 21)
$dph = 1;
elseif ($sazbaDph == 15)
$dph = 2;
else
$dph = 3;
if ($ean == "")
continue; // neimportovat, nema ean
if ($novaCena < 10)
continue; // neimportovat, male polozky
$jeCena = $this->zjistiCenu($ean);
if (!$jeCena)
continue; // neni v eshopu
if ($this->cena == 0) // zmena ceny
{
$this->pocitadloKolikZpracuji = $this->pocitadloKolikZpracuji + 1;
if (!($this->pocitadloKolikZpracuji % 10))
echo $this->pocitadloKolikZpracuji . " zapsat novou cenu " . $ean . " - \n"; // Hlaska pouze kazde desate vety
dibi::update("ps_product_shop", array('price' => $novaCena))->where("id_product = %i", $this->idKnihy)->execute();
dibi::update("ps_product_shop", array('id_tax_rules_group' => $dph))->where("id_product = %i", $this->idKnihy)->execute();
dibi::update("ps_product_shop", array('active' => $aktivni))->where("id_product = %i", $this->idKnihy)->execute(); // zaktivovat
dibi::update("ps_product", array('price' => $novaCena))->where("id_product = %i", $this->idKnihy)->execute();
dibi::update("ps_product", array('id_tax_rules_group' => $dph))->where("id_product = %i", $this->idKnihy)->execute();
dibi::update("ps_product", array('active' => $aktivni))->where("id_product = %i", $this->idKnihy)->execute(); // zaktivovat
}
else
continue;
}
fclose($handle);
echo $this->pocitadloKolikZpracuji . " celkovy pocet zapsanych zmen \n";
}
}
function zjistiCenu($ean)
{
$prodId = $this->eanToIdSafe($ean);
if(!$prodId) return false;
$this->idKnihy = $prodId;
$result = dibi::query("SELECT [price] FROM [ps_product_shop] WHERE [id_product]=%s", $this->idKnihy);
if(count($result) == 0)
return false;
$row = $result->fetch();
$this->cena = floatval($row->price);
return true;
}
function zjistiSlevu($idKnihy)
{
$result = dibi::query("SELECT [reduction] FROM [ps_specific_price] WHERE [id_product]=%s", $idKnihy);
if(count($result) == 0)
return false;
else
return true;
}
}
$e = new StavProdejny();
$e->zpracujData();