<?php

// get the config file
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');
//Programsetting
require_once('../models/dao/ProgramsettingsDAO.class.php');
require_once('../models/dto/Programsetting.class.php');
require_once('../models/mysql/ProgramsettingsMySqlDAO.class.php');
require_once('../models/mysql/ext/ProgramsettingsMySqlExtDAO.class.php');
//Availableparcode
require_once('../models/dao/AvailableparcodeDAO.class.php');
require_once('../models/dto/Availableparcode.class.php');
require_once('../models/mysql/AvailableparcodeMySqlDAO.class.php');
require_once('../models/mysql/ext/AvailableparcodeMySqlExtDAO.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();
//Programsetting
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();
$ProgramsettingEX = new ProgramsettingsMySqlExtDAO();
//Availableparcode
$availableParcode = new Availableparcode();
$availableParcodeDAO = new AvailableparcodeMySqlDAO();
$availableParcodeEX = new AvailableparcodeMySqlExtDAO();



$usedParcodesArr = array();





$dbname = ConnectionProperty::getDatabase();
if (isset($_SESSION['dbname']) && !empty($_SESSION['dbname'])) {
    $dbname = $_SESSION['dbname'];
}

//check and use the condition that suite this action
if (empty($do)) {
    //
    $Programsettingdata = $ProgramsettingDAO->load(1);
    if (!isset($Programsettingdata->parcodeDigits) || empty($Programsettingdata->parcodeDigits)) {
        $Programsettingdata->parcodeDigits = 5;
    }

    $sql = 'update product set parcode = CONCAT(lpad(productid, ' . $Programsettingdata->parcodeDigits . ', 0))';
    $productExt->updateBulk($sql);
    //check if it has rows
    $availableParcodeEX->truncate();
    ##write all parcodes possibilities
    $availableParcodeEX->callGenerateParcodesSP($Programsettingdata->parcodeDigits);
    ##remove used parcodes from possibilities ##edit line is also like this
    $availableParcodeEX->deleteUsedParcodesInproductTable();



//    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 products : " . $count . " </span><br/>";
//    print_r("</pre>");
}
##
//here the global templates


/* ===============================
  function in this CONTROLLER
  ================================ */
?>