<?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');
//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();
//
$sizeColorStoreDetail = new Sizecolorstoredetail();
$sizeColorStoreDetailDAO = new SizecolorstoredetailMySqlDAO();
$sizeColorStoreDetailEX = new SizecolorstoredetailMySqlExtDAO();
//Availableparcode
$availableParcode = new Availableparcode();
$availableParcodeDAO = new AvailableparcodeMySqlDAO();
$availableParcodeEX = new AvailableparcodeMySqlExtDAO();

//check and use the condition that suite this action
if (empty($do)) {
    $count = 0;
    $allproduct = $productExt->getProductsCount();
    $repeatedParcodes = $productExt->queryAllrepateparcode($allproduct->productId);
    foreach ($repeatedParcodes as $parcode) {
        $productsWithSameParcode = $productDAO->queryByParcode($parcode->parcode);
        $i = 0;
        foreach ($productsWithSameParcode as $product) {
            if ($i > 0) {//to skip first product
                generateParcodeAndUpdateIt($product);
                $count++;
            }
            $i++;
        }
    }

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


/* ===============================
  function in this CONTROLLER
  ================================ */

function generateParcodeAndUpdateIt($product) {
    global $productDAO;
    global $productExt;

    $randomString = generateParcode();
    $product->parcode = $randomString;
    $productExt->updatebarcode($product);
}

function generateParcode() {
    global $availableParcodeEX;
    $parcode = $availableParcodeEX->getAvailableParcodeValue();
    //check if this parcode already exists
    $data = checkbarcode2($parcode);
    if ($data != 2) {
        $parcode = generateParcode();
    }
    return $parcode;
}

function checkbarcode2($barcod) {
    global $productDAO;
    global $availableParcodeDAO;

    $parcodeResult = $availableParcodeDAO->queryByValue($barcod);
    if (isset($parcodeResult) && count($parcodeResult) > 0) {
        //if value in the table it is not repeated
        $flag = 2;
        return $flag;
    }


    //check if this parcode already exists in db
    $data = $productDAO->queryByParcode($barcod);
    $flag2;
    if (count($data) > 0) {//> 1 as now i check for barcode after in
        $flag2 = 1;
    } else {
        $flag2 = 2;
    }

    $flagToreturn = 1;
    if ($flag == 2 || $flag2 == 2) {
        $flagToreturn = 2;
    }


    return $flagToreturn;
}

?>