File: /home/mostafedeg/public_html/erp/controllers/mechandiseReturnRequest.php
<?php
include("../public/impOpreation.php");
$do = $_GET['do'];
$today = date("Y-m-d H:i:s");
$date = date("Y-m-d");
if (empty($do)) {
$smarty->display("header.html");
$smarty->display("mechandiseReturnRequestview/add.html");
$smarty->display("footer.html");
} else if ($do == "appendproduct") {
$itr = filter_input(INPUT_POST, 'itr');
$smarty->assign('itr', $itr);
$smarty->display("mechandiseReturnRequestview/appendproduct.html");
} elseif ($do == "show") {
$smarty->display("header.html");
$smarty->display("mechandiseReturnRequestview/show.html");
// $smarty->assign("realestateunits", 1);
$smarty->display("footer.html");
} elseif ($do == "edit") {
$id = filter_input(INPUT_GET, 'id');
$editdata = R::load('mechandisereturnrequest', $id);
$client = R::getRow('SELECT * FROM `client` WHERE clientid = ?',[$editdata->clientid]);
$editdata->clientname = $client['clientname'];
$smarty->assign('editdata', $editdata);
$mechandisereturnproducts = R::findAll('mechandisereturnproduct','mechandisereturnrequestid = ? and conditions = 0',[$id]);
foreach( $mechandisereturnproducts as $mechandisereturnproduct){
$product = R::getRow('SELECT * FROM `product` WHERE productId = ?',[$mechandisereturnproduct->productid]);
$mechandisereturnproduct->productname = $product['productName'];
}
$smarty->assign('mechandisereturnproducts', $mechandisereturnproducts);
$smarty->display("header.html");
$smarty->display("mechandiseReturnRequestview/edit.html");
$smarty->display("footer.html");
}else if ($do == 'savedata') {
savedata();
}else if ($do == 'showajax') {
showajax();
}else if ($do == 'select2client') {
select2client();
}else if ($do == 'select2product') {
select2product();
}else if ($do == 'removeappend') {
removeappend();
}else if ($do == 'agrees') {
agrees();
}else if ($do == 'remove') {
remove();
}
function agrees() {
$today = date("Y-m-d H:i:s");
$date = date('Y-m-d');
$userid = $_SESSION['userid'];
$id = filter_input(INPUT_POST, 'id');
$agrees = filter_input(INPUT_POST, 'agrees');
$tables = R::load('mechandisereturnrequest',$id);
$tables->agrees = $agrees;
$tables->agreetoday = $today;
$tables->agreeuserid = $userid;
try {
R::store($tables);
echo 1 ;
} catch (Exception $e) {
echo 0 ;
}
}
function select2product() {
$name = $_POST['searchTerm'];
$productsData = R::getAll("SELECT productId, CONCAT(productName) as texts
FROM product WHERE conditions = 0 and CONCAT(productName) LIKE '%" . $name . "%' limit 50");
$return_arr = array();
foreach ($productsData as $pro) {
$row_array = array();
$row_array['id'] = $pro['productId'];
$row_array['text'] = $pro['texts'];
array_push($return_arr, $row_array);
}
echo json_encode($return_arr);
}
function select2client() {
$name = $_POST['searchTerm'];
$productsData = R::getAll("SELECT clientid, CONCAT(clientname,'/',clientphone) as texts
FROM client WHERE conditions = 0 and CONCAT(clientname,'/',clientphone) LIKE '%" . $name . "%' limit 50");
$return_arr = array();
foreach ($productsData as $pro) {
$row_array = array();
$row_array['id'] = $pro['clientid'];
$row_array['text'] = $pro['texts'];
array_push($return_arr, $row_array);
}
echo json_encode($return_arr);
}
function savedata() {
$today = date("Y-m-d H:i:s");
$date = date('Y-m-d');
$userid = $_SESSION['userid'];
$damagedintact = filter_input(INPUT_POST, 'damagedintact');
$clientid = filter_input(INPUT_POST, 'clientid');
$productsitr = filter_input(INPUT_POST, 'productsitr');
$id = filter_input(INPUT_POST, 'id');
if (!$id) {
$realestates = R::dispense('mechandisereturnrequest');
$realestates->conditions = 0;
$realestates->addtoday = $today;
$realestates->adduserid = $userid;
$realestates->agrees = 0;
$realestates->deltoday = '';
$realestates->deluserid = '';
}else{
$realestates = R::load('mechandisereturnrequest',$id);
$realestates->updatetoday = $today;
$realestates->updateuserid = $userid;
}
$realestates->damagedintact = $damagedintact;
$realestates->clientid = $clientid;
try {
$mechandisereturnrequestid = R::store($realestates);
for ($i = 1; $i <= $productsitr; $i++) {
$productid = filter_input(INPUT_POST, 'productid_' . $i);
$comment = filter_input(INPUT_POST, 'comment_' . $i);
$mechandiseReturnProductid = filter_input(INPUT_POST, 'mechandiseReturnProductid_' . $i);
if (!$productid) {continue;}
if (!$mechandiseReturnProductid) {
$realestatesunits = R::dispense('mechandisereturnproduct');
$realestatesunits->addtoday = $today;
$realestatesunits->adduserid = $userid;
$realestatesunits->deltoday = '';
$realestatesunits->deluserid = '';
}else{
$realestatesunits = R::load('mechandisereturnproduct',$mechandiseReturnProductid);
$realestatesunits->updatetoday = $today;
$realestatesunits->updateuserid = $userid;
}
$realestatesunits->mechandisereturnrequestid = $mechandisereturnrequestid;
$realestatesunits->productid = $productid;
$realestatesunits->comment = $comment;
$realestatesunits->conditions = 0;
R::store($realestatesunits);
}
// echo $mechandisereturnrequestid;
header("location:mechandiseReturnRequest.php?do=show");
exit();
} catch (Exception $e) {
echo 0 ;
}
}
function showajax() {
$columns = array('mechandisereturnrequest.id', 'clientname', 'mechandisereturnrequest.damagedintact','', 'mechandisereturnrequest.agrees','mechandisereturnrequest.addtoday', 'employeename', 'realestates.id', 'realestates.id');
$start_date = filter_input(INPUT_POST, 'start_date');
$end_date = filter_input(INPUT_POST, 'end_date');
$conditions = filter_input(INPUT_POST, 'conditions');
$clientid = filter_input(INPUT_POST, 'clientid');
$productid = filter_input(INPUT_POST, 'productid');
$damagedintact = filter_input(INPUT_POST, 'damagedintact');
$searchQuery = " ";
$searchjoin = " ";
if($clientid){
$searchQuery .= " and mechandisereturnrequest.clientid = ".$clientid. " ";
}
if($conditions){
$searchQuery .= " and mechandisereturnrequest.conditions = " .$conditions. " ";
}
if ($damagedintact) {
$searchQuery .= " and mechandisereturnrequest.damagedintact = ".$damagedintact. " ";
}
if($productid != ''){
$searchQuery .= " and mechandisereturnproduct.productid = ".$productid. " ";
$searchjoin .= " LEFT JOIN mechandisereturnproduct ON mechandisereturnrequest.id = mechandisereturnproduct.mechandisereturnrequestid ";
}
if($start_date != '' && $end_date != ''){
$searchQuery .=' and mechandisereturnrequest.addtoday >= "' . $start_date . ' 00-00-00" and mechandisereturnrequest.addtoday <= "' . $end_date . ' 23-59-55" ';
}
if (isset($_POST['searchval']) && $_POST['searchval'] != "") {
$searchQuery .= "and ( mechandisereturnrequest.id LIKE "%'.$_POST["searchval"].'%"
OR clientname LIKE "%'.$_POST["searchval"].'%"
OR mechandisereturnrequest.addtoday LIKE "%'.$_POST["searchval"].'%"
OR employeename LIKE "%'.$_POST["searchval"].'%"
)
";
}
if(isset($_POST["ordercol"]))
{
$searchQuery .= 'ORDER BY '.$columns[$_POST['ordercol']].' '.$_POST['orderdir'].' ';
}
else
{
$searchQuery .= "ORDER BY mechandisereturnrequest.id DESC ";
}
$totals = R::count('mechandisereturnrequest','LEFT JOIN user ON mechandisereturnrequest.adduserid = user.userid
'.$searchjoin.' WHERE 1 '.$searchQuery.' ');
if (isset($_POST['start']) && $_POST['length'] != '-1') {
$searchQuery .= "LIMIT " . intval($_POST['start']) . ", " .
intval($_POST['length']);
}
$rResult = R::getAll('SELECT mechandisereturnrequest.* ,employeename,clientname FROM `mechandisereturnrequest`
LEFT JOIN user ON mechandisereturnrequest.adduserid = user.userid
LEFT JOIN client ON mechandisereturnrequest.clientid = client.clientid '.$searchjoin.' WHERE 1 '.$searchQuery.' ');
$output = array(
"draw" => intval($_POST["draw"]),
"recordsTotal" => count($rResult),
"recordsFiltered" => $totals,
"data" => array()
);
foreach ($rResult as $row) {
$mechandisereturnproduct = R::getAll('SELECT * FROM `mechandisereturnproduct` LEFT JOIN product
ON mechandisereturnproduct.productid = product.productId WHERE mechandisereturnproduct.conditions = 0 ');
$allproductName = '';
foreach($mechandisereturnproduct as $product){
$allproductName .= $product['productName'] . ' / ';
}
$sub_array = array();
$sub_array[] = $row["id"];
$sub_array[] = $row["clientname"] ;
if($row["damagedintact"] == 1){
$sub_array[] = ' تالف ';
}else{
$sub_array[] = ' سليم ';
}
$agrees = '<select name="agrees" data-placeholder="اختر " class="selectnew agrees" data-id="'.$row["id"].'">
<option value="0" ';if($row["agrees"] == 0){$agrees .= 'selected';}$agrees .= '>قيد الانتظار</option>
<option value="1" ';if($row["agrees"] == 1){$agrees .= 'selected';}$agrees .='>قبول</option>
<option value="2" ';if($row["agrees"] == 2){$agrees .= 'selected';}$agrees .='>رفض</option>
</select>';
$sub_array[] = $agrees;
$sub_array[] = $allproductName;
$sub_array[] = $row["addtoday"];
$sub_array[] = $row['employeename'] ;
if($row["del"] < 2){
if($row["agrees"] == 2){
$sub_array[] = ' مرتجع للتسليم ' ;
}else{
$sub_array[] = '<a href="mechandiseReturnRequest.php?do=edit&id='. $row["id"] .'" type="button" class="btn btn-default btn-lg editicon"></a>';
}
$sub_array[] = '<a href="mechandiseReturnRequest.php?do=remove&id='. $row["id"] .'" type="button" class="btn btn-default btn-lg deleteicon"></a>';
}else{
if($row["agrees"] == 2){
$sub_array[] = ' مرتجع للتسليم ' ;
}else{
$sub_array[] = '<a href="mechandiseReturnRequest.php?do=edit&id='. $row["id"] .'" type="button" class="btn btn-default btn-lm ">تفاصيل</a>';
}
$sub_array[] = 'محذوف ';
}
$output['data'][] = $sub_array;
}
echo json_encode($output, JSON_UNESCAPED_SLASHES);
}
function removeappend() {
$today = date("Y-m-d H:i:s");
$date = date('Y-m-d');
$userid = $_SESSION['userid'];
$id = filter_input(INPUT_POST, 'id');
$tables = R::load('mechandisereturnproduct',$id);
$tables->conditions = 1;
$tables->deltoday = $today;
$tables->deluserid = $userid;
try {
R::store($tables);
echo 1;
} catch (Exception $e) {
echo 0 ;
}
}
function remove() {
$today = date("Y-m-d H:i:s");
$date = date('Y-m-d');
$userid = $_SESSION['userid'];
$id = filter_input(INPUT_GET, 'id');
$tables = R::load('mechandiseReturnRequest',$id);
$tables->conditions = 1;
$tables->deltoday = $today;
$tables->deluserid = $userid;
try {
R::store($tables);
header("location:mechandiseReturnRequest.php?do=show");
exit();
} catch (Exception $e) {
echo 0 ;
}
}
?>