<?php

include_once("../public/config.php");
include("../public/include_dao.php");
//Product
require_once('../models/dao/ProductDAO.class.php');
require_once('../models/dto/Product.class.php');
require_once('../models/mysql/ProductMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductMySqlExtDAO.class.php');

//get the do the action
$do = $_GET['do'];

/* ======================

  Controller Name :- productController

  OPERTATION in Controller


  ======================== */

//here the global templates
//$smarty->display("header.html");
//here goes the instances and general variables
//Product
$product = new Product();
$productDAO = new ProductMySqlDAO();
$productExt = new ProductMySqlExtDAO();


//check and use the condition that suite this action
//$started_at = microtime(true);

if (empty($do)) {
    //here the permssion check
    $count = 0;
    $start = 0;
    $limit = 30;
    $end = $start + $limit;
    $all_products_count = $productExt->getProductsCount();
    for ($i = $start; $i <= $all_products_count->productId; $i+=$limit) {
        $allproduct = $productExt->queryAllOrderedLimitedSimple($start, $limit);
        $update_sql = '';
        foreach ($allproduct as $value) {
            $value->productSellHalfPrice = $value->productSellAllPrice * 1.12;
            $value->productSellUnitPrice = $value->productSellAllPrice * 1.3;
            //$productDAO->update($value);
            $update_sql .= "UPDATE product SET productSellHalfPrice = $value->productSellHalfPrice,productSellUnitPrice= $value->productSellUnitPrice WHERE productId = $value->productId;";
            $count++;
        }
        if ($update_sql != "") {
            $conn = mysqli_connect(ConnectionProperty::getHost(), ConnectionProperty::getUser(), ConnectionProperty::getPassword(), ConnectionProperty::getDatabase()) or die('unable to connect to db');
            $res = mysqli_multi_query($conn, $update_sql); //mysqli_query for one query , mysqli_multi_query for multi query
            mysqli_close($con);
        }
        //
        $start += $limit;
        $end += $limit;
    }
//    echo 'Cool, that only took ' . (microtime(true) - $started_at) . ' seconds!';

    echo "<span style='color:green'> DONE. </span><br/>";
    print_r("<pre>");
    echo "<div style='border:1px solid green;margin-right:60%'><span style='color:green'> no of queries products fixed : " . $count . " </span><br/>";
    print_r("</pre>");
}
?>