File: /home/mostafedeg/public_html/erp/controllers/_avalableParcodes.php
<?php
##count lines
//$linecount = 0;
//$file = fopen('../avalableParcodes/'.$dbname.'.txt', "r")or exit("Unable to open avalable parcodes file!");
//while (!feof($file)) {
// $line = fgets($file);
// $linecount++;
//}
//fclose($file);
##read rand line
//$lines = file('../avalableParcodes/'.$dbname.'.txt'); //file in to an array
//echo $lines[rand(0, $linecount)];
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();
//Availableparcode
$availableParcode = new Availableparcode();
$availableParcodeDAO = new AvailableparcodeMySqlDAO();
$availableParcodeEX = new AvailableparcodeMySqlExtDAO();
$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;
}
//check if it has rows
$result = $availableParcodeEX->getFirstRow();
//if no rows create parcodes
if (!isset($result) || empty($result)) {
##write all parcodes possibilities
$availableParcodeEX->callGenerateParcodesSP($Programsettingdata->parcodeDigits);
##remove used parcodes from possibilities ##edit line is also like this
$availableParcodeEX->deleteUsedParcodesInproductTable();
}
/* $biggestParcode = '';
for ($i = 1; $i <= (int) $Programsettingdata->parcodeDigits; $i++) {
$biggestParcode .= '9';
}
$biggestParcode = (int) $biggestParcode;
##collect used parcodes
$count = 0;
$start = 0;
$limit = 30;
$end = $start + $limit;
$usedParcodesArr = array();
$all_products_count = $productExt->getProductsCount();
for ($i = $start; $i <= $all_products_count->productId; $i += $limit) {
$allproduct = $productExt->queryAllOrderedLimitedSimple($start, $limit);
foreach ($allproduct as $value) {
array_push($usedParcodesArr, $value->parcode);
$count++;
}
//
$start += $limit;
$end += $limit;
}
##write all parcodes possibilities
$file = fopen('../avalableParcodes/' . $dbname . '.txt', 'w')or exit("Unable to open avalable parcodes file!");
flock($file, LOCK_EX);
for ($i = 0; $i <= $biggestParcode; $i++) {
//fixed length of five
$number = (string) $i;
$numlength = strlen($number);
if ($numlength != $Programsettingdata->parcodeDigits) {
$noDigitsLeft = $Programsettingdata->parcodeDigits - $numlength;
$preDigits = '';
for ($j = 0; $j < $noDigitsLeft; $j++) {
$preDigits .= '0';
}
$number = $preDigits . $number;
}
if ($i != $biggestParcode)
$number = $number . PHP_EOL;
fwrite($file, $number);
}
flock($file, LOCK_UN);
fclose($file);
##remove used parcodes from possibilities ##edit line is also like this
$file = fopen('../avalableParcodes/' . $dbname . '.txt', "r") or exit("Unable to open avalable parcodes file!");
flock($file, LOCK_EX);
$text = '';
while (!feof($file)) {
$lineTxt = trim(fgets($file));
//if (in_array($lineTxt, $usedParcodesArr)) {
$key = array_search($lineTxt, $usedParcodesArr);
if ($key > -1 && $key !== FALSE) {
##in the array dont use it in $text.this means that this value will be removed from txt file
//remove this index from array of used parcodes to make array smaller
unset($usedParcodesArr[$key]);
//echo $lineTxt;
} else {
//lastLine will have biggest parcode as file is created now and we are workig on remove parcode
//no changes yet
$text .= $lineTxt;
if ($lineTxt != $biggestParcode)
$text .= PHP_EOL;
}
}
flock($file, LOCK_UN);
fclose($file);
$file = fopen('../avalableParcodes/' . $dbname . '.txt', "w") or exit("Unable to open avalable parcodes file!");
flock($file, LOCK_EX);
fwrite($file, $text);
flock($file, LOCK_UN);
fclose($file);
*/
echo "<span style = 'color:green'> DONE. </span><br/>";
}
?>