include "session_check.php";
include "header.php";
include "menu.php";
require_once "util.php";
require_once "DataBase/Product.php";
require_once "DataBase/Customer.php";
require_once "DataBase/Price.php";
require_once "DataBase/Priceset.php";
/* TODO: check if valid $priceset_id and what if no $customer_data found? */
$customer = new Customer();
$customer_data = $customer->get($_SESSION["username"]);
$priceset_id = 0;
if (count($customer_data) > 0) {
    $priceset_id = $customer_data[0][11];
}
$priceset = new Priceset();
$product = new Product();
$price = new Price();
$tmp_priceset = $priceset->get($priceset_id);
$netto = 0;
if (count($tmp_priceset) > 0) {
    $netto = $tmp_priceset[0][2];
}
?>

<div width="100%" align="center">

<?php 
// prepare search/filter params
$search_product = var_get_post("search_product", "");
$search_category = var_get_post("search_category", "");
<?php

require_once "DataBase_Net/Product_Net.php";
require_once "DataBase/Product.php";
require_once "DataBase_Net/Price_Net.php";
require_once "DataBase/Price.php";
require_once "DataBase/Priceset.php";
// sync Product database
$product = new Product();
$product_net = new Product_Net();
$price = new Price();
$price_net = new Price_Net();
$priceset = new Priceset();
$new_product_count = 0;
$del_product_count = 0;
// delete remote products if not locally available anymore
$remote_product = $product_net->get('', array('product_id'));
foreach ($remote_product as $tmp) {
    if ($product->exists($tmp[0], " available=1 ") == false) {
        $product_net->delete($tmp[0]);
        // delete also the prices
        $local_pricesets = $priceset->get('');
        foreach ($local_pricesets as $tmp_priceset) {
            $tmp_price = $price_net->getPrice($tmp_priceset[0], $tmp[0]);
            if (count($tmp_price) > 0) {
                $price_net->delete($tmp_price[0][0]);
            }
        }
        $del_product_count++;
    }
}
<?php

require_once "util.php";
require_once "DataBase/Product.php";
require_once "DataBase/Tax.php";
require_once "DataBase/Category.php";
require_once "DataBase/Priceset.php";
require_once "DataBase/Group.php";
require_once "DataBase/Price.php";
$db_action = var_get_post("db_action", "");
$product = new Product();
$tax = new Tax();
$category = new Category();
$priceset = new Priceset();
$price = new Price();
$group = new Group();
switch ($db_action) {
    case "new":
        $product->create(var_post("product_id", ""), array(var_post("name", ""), var_post("details", ""), var_post("category_id", ""), var_post("tax_id", ""), var_post("available", ""), var_post("group_id", ""), var_post("order_number", ""), var_post("anmerkung", "")));
        $priceset_list = $priceset->get();
        for ($i = 0; $i < count($priceset_list); $i++) {
            $price->create(var_post("priceset_id_" . $i, ""), var_post("product_id", ""), array(var_post("priceset_price_" . $i, "")));
        }
        break;
    case "edit":
        $product->update(var_post("product_id", ""), array(var_post("name", ""), var_post("details", ""), var_post("category_id", ""), var_post("tax_id", ""), var_post("available", ""), var_post("group_id", ""), var_post("order_number", ""), var_post("anmerkung", "")));
        $priceset_list = $priceset->get();
        for ($i = 0; $i < count($priceset_list); $i++) {
            if ($price->exists(var_post("priceset_id_" . $i, ""), var_post("product_id", ""))) {
                $price->update(var_post("priceset_id_" . $i, ""), var_post("product_id", ""), array(var_post("priceset_price_" . $i, "")));
            } else {
require_once "DataBase/Bill.php";
require_once "DataBase/Assistant.php";
require_once "DataBase/Customer.php";
require_once "DataBase/Product.php";
require_once "DataBase/Bill_Product.php";
require_once "DataBase/Price.php";
require_once "DataBase/Priceset.php";
require_once "DataBase/Tax.php";
$bill_id = var_get_post("bill_id", "");
$bill = new Bill();
$tmp = $bill->get($bill_id);
$bill_data = $tmp[0];
$customer = new Customer();
$tmp = $customer->get($bill_data[1]);
$customer_data = $tmp[0];
$assistant = new Assistant();
$tmp = $assistant->get($bill_data[4]);
$assistant_data = $tmp[0];
$priceset = new Priceset();
$tmp = $priceset->get($customer_data[11]);
$priceset_data = $tmp[0];
$bill_product = new Bill_Product();
$bill_product_data = $bill_product->getByBillId($bill_id);
$product = new Product();
$price = new Price();
$tax = new Tax();
include "Druckvorlagen/zeilen.php";
include "Druckvorlagen/rechnung.php";
?>

        if ($search) {
            $search .= " AND (product_id >=" . $var_product_id_from . ") ";
        } else {
            $search .= "(product_id >=" . $var_product_id_from . ") ";
        }
    }
    if ($var_product_id_to) {
        if ($search) {
            $search .= " AND (product_id <=" . $var_product_id_to . ") ";
        } else {
            $search .= "(product_id <=" . $var_product_id_to . ") ";
        }
    }
    $product_data = $product->get('', array(), $search);
    $assistant_data = $assistant->get($var_assistant);
    $priceset = new Priceset();
    $tmp = $priceset->get($var_priceset);
    $priceset_data = $tmp[0];
    include "Druckvorlagen/zeilen.php";
    include "Druckvorlagen/list.php";
} elseif ($var_print == "print_re") {
    $search = "";
    if ($var_product_id) {
        if ($search) {
            $search .= " AND (product_id >=" . $var_product_id . ") ";
        } else {
            $search .= "(product_id >=" . $var_product_id . ") ";
        }
    }
    if ($var_date_from) {
        if ($search) {
$printfile = "";
include "Modules/toolbar.php";
// if($action == "upload_0")
// {
// 	$main_box->add(new NTKLabel("", "Neue Produkte / Preise online stellen"), 200, 10, "font-style: bold; background-color: #dfe7f3;");
//
// 	$button_next = new NTKButton("button_next", "Weiter", "location.replace('websync.php?action=upload_1');");
// 	$main_box->add($button_next);
// }
if ($action == "upload_0") {
    require_once "DataBase/Priceset.php";
    require_once "DataBase/Customer.php";
    require_once "DataBase/Product.php";
    require_once "DataBase/Price.php";
    require_once "DataBase/Category.php";
    $priceset = new Priceset();
    $customer = new Customer();
    $product = new Product();
    $price = new Price();
    $category = new Category();
    $priceset->export("mysql_export/priceset.csv");
    $customer->export("mysql_export/customer.csv");
    $product->export("mysql_export/product.csv");
    $price->export("mysql_export/price.csv");
    $category->export("mysql_export/category.csv");
    // upload all db-files "mysql_export"
    $handle = popen("/var/www/ursprung/bin/upload.sh", "r");
    pclose($handle);
    $main_box->add(new NTKLabel("", "Schritt erfolgreich"), 200, -1, "color: #00ff00; background-color: #dfe7f3;");
    $button_next = new NTKButton("button_next", "Weiter", "location.replace('websync.php?action=upload_1');");
    $main_box->add($button_next, 500);
<?php

include "DataBase/database.php";
include "DataBase/Customer.php";
include "DataBase/Priceset.php";
include "DataBase/Product.php";
include "DataBase/Price.php";
include "DataBase/Category.php";
$customer = new Customer();
$priceset = new Priceset();
$product = new Product();
$price = new Price();
$category = new Category();
$folder = "mysql_import/";
$customer->import($folder . "customer.csv");
echo "kunden import<br>";
$priceset->import($folder . "priceset.csv");
echo "priceset import<br>";
$product->import($folder . "product.csv");
echo "product import<br>";
$price->import($folder . "price.csv");
echo "price import done<br>";
$category->import($folder . "category.csv");
echo "category import done<br>";
?>

<br>
Synchronisation abgeschlossen
<?php

require_once "util.php";
$priceset = new Priceset();
if ($action == "new") {
    $priceset->create(var_post("priceset_id", ""), array(var_post("priceset_name", ""), var_post("priceset_netto", "")));
} elseif ($action == "edit") {
    $priceset->update(var_post("priceset_id", ""), array(var_post("priceset_name", ""), var_post("priceset_netto", "")));
} elseif ($action == "delete") {
    $priceset->delete(var_get("priceset_id", ""));
}
$priceset_box = new NTKVBox("priceset_box", 0, 1, False);
$label = new NTKLabel("priceset_label", "<h3>Preiss&auml;tze</h3>");
$priceset_box->add($label);
$priceset_list = $priceset->get('');
for ($i = 0; $i < count($priceset_list); $i++) {
    $priceset_box->add(new NTKEPriceset($priceset_list[$i][0], $priceset_list[$i][1], $priceset_list[$i][2]));
}
$label = new NTKLabel("priceset_label", "<h4>neuer Preissatz</h4>");
$priceset_box->add($label);
$priceset_box->add(new NTKEPriceset("", "", 1, True), -1);
$main_box->add($priceset_box, -1, -1, "background-color: #dfe7f3; vertical-align: top;");
<?php

require_once "DataBase_Net/Priceset_Net.php";
require_once "DataBase/Priceset.php";
// sync Priceset database
$priceset = new Priceset();
$priceset_net = new Priceset_Net();
$new_priceset_count = 0;
$del_priceset_count = 0;
$remote_pricesets = $priceset_net->get('', array('priceset_id'));
foreach ($remote_pricesets as $tmp) {
    if ($priceset->exists($tmp[0]) == false) {
        $priceset_net->delete($tmp[0]);
        $del_priceset_count++;
    }
}
$remote_pricesets = $priceset_net->get('', array('priceset_id'));
$local_pricesets = $priceset->get('', array('priceset_id'));
foreach ($local_pricesets as $tmp) {
    $found = false;
    foreach ($remote_pricesets as $remote_tmp) {
        if ($remote_tmp[0] == $tmp[0]) {
            $found = true;
            break;
        }
    }
    $tmp_data = $priceset->get($tmp[0], array("name", "netto"));
    if (!$found) {
        $priceset_net->create($tmp[0], $tmp_data[0]);
        $new_priceset_count++;
    } else {