CMXX - Dealers

This commit is contained in:
“VeLiTi”
2025-04-07 20:25:37 +02:00
parent 6f938302ff
commit cb18443af9
7 changed files with 427 additions and 54 deletions

105
api/v2/get/dealers.php Normal file
View File

@@ -0,0 +1,105 @@
<?php
defined($security_key) or exit;
//------------------------------------------
// dealers
//------------------------------------------
//Connect to DB
$pdo = dbConnect($dbname);
//SoldTo is empty
if (empty($partner->soldto) || $partner->soldto == ''){$soldto_search = '%';} else {$soldto_search = '-%';}
//default whereclause
list($whereclause,$condition) = getWhereclauselvl2("",$permission,$partner,'get');
//NEW ARRAY
$criterias = [];
$clause = '';
//Check for $_GET variables and build up clause
if(isset($get_content) && $get_content!=''){
//GET VARIABLES FROM URL
$requests = explode("&", $get_content);
//Check for keys and values
foreach ($requests as $y){
$v = explode("=", $y);
//INCLUDE VARIABLES IN ARRAY
$criterias[$v[0]] = $v[1];
if ($v[0] == 'page' || $v[0] =='p' || $v[0] =='totals' || $v[0] =='list' || $v[0] =='history'|| $v[0] =='success_msg'){
//do nothing
}
elseif ($v[0] == 'search') {
//build up search
$clause .= ' AND name like :'.$v[0];
}
else {//create clause
$clause .= ' AND d.'.$v[0].' = :'.$v[0];
}
}
if ($whereclause == '' && $clause !=''){
$whereclause = 'WHERE '.substr($clause, 4);
} else {
$whereclause .= $clause;
}
}
//Define Query
if(isset($criterias['totals']) && $criterias['totals'] ==''){
//Request for total rows
$sql = 'SELECT count(*) as count FROM dealers '.$whereclause.'';
}
else {
//SQL for Paging
$sql = 'SELECT * FROM dealers '.$whereclause.' LIMIT :page,:num_products';
}
$stmt = $pdo->prepare($sql);
//Bind to query
if (str_contains($whereclause, ':condition')){
$stmt->bindValue('condition', $condition, PDO::PARAM_STR);
}
if (!empty($criterias)){
foreach ($criterias as $key => $value){
$key_condition = ':'.$key;
if (str_contains($whereclause, $key_condition)){
if ($key == 'search'){
$search_value = '%'.$value.'%';
$stmt->bindValue($key, $search_value, PDO::PARAM_STR);
}
else {
$stmt->bindValue($key, $value, PDO::PARAM_STR);
}
}
}
}
//Add paging details
if(isset($criterias['totals']) && $criterias['totals']==''){
$stmt->execute();
$messages = $stmt->fetch();
$messages = $messages[0];
}
else {
$current_page = isset($criterias['p']) && is_numeric($criterias['p']) ? (int)$criterias['p'] : 1;
$stmt->bindValue('page', ($current_page - 1) * $page_rows_dealers, PDO::PARAM_INT);
$stmt->bindValue('num_products', $page_rows_dealers, PDO::PARAM_INT);
//Excute Query
$stmt->execute();
//Get results
$messages = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
//------------------------------------------
//JSON_ENCODE
//------------------------------------------
$messages = json_encode($messages, JSON_UNESCAPED_UNICODE);
//Send results
echo $messages;
?>

87
api/v2/post/dealers.php Normal file
View File

@@ -0,0 +1,87 @@
<?php
defined($security_key) or exit;
//------------------------------------------
// dealers
//------------------------------------------
//Connect to DB
$pdo = dbConnect($dbname);
//CONTENT FROM API (POST)
$post_content = json_decode($input,true);
//SoldTo is empty
if (empty($partner->soldto) || $partner->soldto == ''){$soldto_search = '%';} else {$soldto_search = '-%';}
//default whereclause
list($whereclause,$condition) = getWhereclause('',$permission,$partner,'');
//BUILD UP PARTNERHIERARCHY FROM USER
$partner_product = json_encode(array("salesid"=>$partner->salesid,"soldto"=>$partner->soldto), JSON_UNESCAPED_UNICODE);
$id = $post_content['rowID'] ?? ''; //check for rowID
$command = ($id == '')? 'insert' : 'update'; //IF rowID = empty then INSERT
if (isset($post_content['delete'])){$command = 'delete';} //change command to delete
//CREATE EMPTY STRINGS
$clause = '';
$clause_insert ='';
$input_insert = '';
if ($command == 'update'){
$post_content['updatedby'] = $username ;
}
if ($command == 'insert'){
$post_content['createdby'] = $username;
$post_content['accounthierarchy'] = $partner_product;
}
//CREAT NEW ARRAY AND MAP TO CLAUSE
if(isset($post_content) && $post_content!=''){
foreach ($post_content as $key => $var){
if ($key == 'submit' || $key == 'rowID'){
//do nothing
}
else {
$criterias[$key] = $var;
$clause .= ' , '.$key.' = ?';
$clause_insert .= ' , '.$key.'';
$input_insert .= ', ?'; // ? for each insert item
$execute_input[]= $var; // Build array for input
}
}
}
//CLEAN UP INPUT
$clause = substr($clause, 2); //Clean clause - remove first comma
$clause_insert = substr($clause_insert, 2); //Clean clause - remove first comma
$input_insert = substr($input_insert, 1); //Clean clause - remove first comma
//QUERY AND VERIFY ALLOWED
if ($command == 'update' && isAllowed('dealers',$profile,$permission,'U') === 1){
$sql = 'UPDATE dealers SET '.$clause.' WHERE rowID = ? '.$whereclause.'';
$execute_input[] = $id;
$stmt = $pdo->prepare($sql);
$stmt->execute($execute_input);
}
elseif ($command == 'insert' && isAllowed('dealers',$profile,$permission,'C') === 1){
$sql = 'INSERT INTO dealers ('.$clause_insert.') VALUES ('.$input_insert.')';
$stmt = $pdo->prepare($sql);
$stmt->execute($execute_input);
}
elseif ($command == 'delete' && isAllowed('dealers',$profile,$permission,'D') === 1){
$sql = 'DELETE FROM dealers WHERE rowID = ? '.$whereclause;
$stmt = $pdo->prepare($sql);
$stmt->execute([$id]);
//Add deletion to changelog
changelog($dbname,'dealers',$id,'Delete','Delete',$username);
} else
{
//do nothing
}
?>

View File

@@ -54,6 +54,14 @@ $main_menu = [
"name" => "menu_identity"
]
],
"dealers" => [
"main_menu" => [
"url" => "dealers",
"selected" => "dealers",
"icon" => "fas fa-tachometer-alt",
"name" => "menu_dealers"
]
],
"buildtool" => [
"main_menu" => [
"url" => "buildtool",
@@ -307,6 +315,8 @@ $page_rows_discounts = 25;//discounts
$page_rows_shipping = 25;//discounts
$page_rows_transactions = 25; //transactions
$page_rows_invoice = 25; //invoices
$page_rows_dealers = 25; //dealers
//------------------------------------------
// Languages supported
//------------------------------------------

142
dealers.php Normal file
View File

@@ -0,0 +1,142 @@
<?php
defined(page_security_key) or exit;
if (debug && debug_id == $_SESSION['id']){
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
}
include_once './assets/functions.php';
include_once './settings/settings_redirector.php';
//SET ORIGIN FOR NAVIGATION
$prev_page = $_SESSION['prev_origin'] ?? '';
$page = $_SESSION['origin'] = 'dealers';
//Check if allowed
if (isAllowed($page,$_SESSION['profile'],$_SESSION['permission'],'R') === 0){
header('location: index.php');
exit;
}
//GET PARAMETERS
$pagination_page = isset($_GET['p']) ? $_GET['p'] : 1;
$search = isset($_GET['search']) ? '&search='.$_GET['search'] : '';
// Determine the URL
$url = 'index.php?page=dealers'.$search;
//GET Details from URL
$GET_VALUES = urlGETdetails($_GET) ?? '';
//CALL TO API
$api_url = '/v2/dealers/'.$GET_VALUES;
$dealers = ioServer($api_url,'');
//Decode Payload
if (!empty($dealers)){$dealers = json_decode($dealers,true);}else{$dealers = null;}
//Return QueryTotal from API
$api_url = '/v2/dealers/totals=';
$query_total = ioServer($api_url,'');
//Decode Payload
if (!empty($query_total)){$query_total = json_decode($query_total,true);}else{$query_total = null;}
// Handle success messages
if (isset($_GET['success_msg'])) {
if ($_GET['success_msg'] == 1) {
$success_msg = $message_dealers_1 ?? 'Created';
}
if ($_GET['success_msg'] == 2) {
$success_msg = $message_dealers_2 ?? 'Updated';
}
if ($_GET['success_msg'] == 3) {
$success_msg = $message_dealers_3 ?? 'Deleted' ;
}
}
template_header('dealers', 'dealers','view');
$view = '
<div class="content-title">
<div class="title">
<i class="fa-solid fa-truck-fast"></i>
<div class="txt">
<h2>'.($dealers_h2 ?? 'dealers').' ('.$query_total.')</h2>
<p>'.($dealers_p ?? '').'</p>
</div>
</div>
</div>';
if (isset($success_msg)){
$view .= ' <div class="msg success">
<i class="fas fa-check-circle"></i>
<p>'.$success_msg.'</p>
<i class="fas fa-times"></i>
</div>';
}
$view .= '
<div class="content-header responsive-flex-column pad-top-5">
<a href="index.php?page=dealers_manage" class="btn">'.($button_create_dealers ?? 'Create dealer').'</a>
</div>
<div class="content-block">
<div class="table">
<table>
<thead>
<tr>
<td>'.($dealers_id ?? '#').'</td>
<td>'.($dealers_status ?? 'status').'</td>
<td>'.($dealers_name ?? 'Name').'</td>
<td class="responsive-hidden">'.($dealers_created ?? 'Created').'</td>
<td>'.$general_actions.'</td>
</tr>
</thead>
<tbody>';
if (empty($dealers)){
$view .= '<tr>
<td colspan="8" style="text-align:center;">'.($message_no_dealers ?? 'There are no dealers').'</td>
</tr>';
}
else {
foreach ($dealers as $order){
//Translate status INT to STR
$payment_status = 'general_status_'.$dealer['status'];
$view .= '
<tr>
<td>'.$dealer['id'].'</td>
<td>'.(${$dealer_status} ?? $dealer['status']).'</td>
<td>'.$dealer['name'].'</td>
<td class="responsive-hidden">'.getRelativeTime($dealer['created']).'</td>
<td><a href="index.php?page=dealer&id='.$dealer['id'].'" class="btn_link">'.$general_view.'</a></td>
</tr>';
}
}
$view .= '
</tbody>
</table>
</div>
</div>
';
$view.='<div class="pagination">';
if ($pagination_page > 1) {
$page = $pagination_page-1;
$view .= '<a href="'.$url.'&p=1">'.$general_first.'</a>';
$view .= '<a href="'.$url.'&p='.$page.'">'.$general_prev.'</a>';
}
$totals = ceil($query_total / $page_rows_dealers) == 0 ? 1 : ceil($query_total / $page_rows_dealers);
$view .= '<span> '.$general_page.$pagination_page.$general_page_of.$totals.'</span>';
if ($pagination_page * $page_rows_dealers < $query_total){
$page = $pagination_page+1;
$view .= '<a href="'.$url.'&p='.$page.'">'.$general_next.'</a>';
$view .= '<a href="'.$url.'&p='.$totals.'">'.$general_last.'</a>';
}
$view .= '</div>';
//OUTPUT
echo $view;
template_footer();
?>

View File

@@ -54,6 +54,14 @@ $main_menu = [
"name" => "menu_identity"
]
],
"dealers" => [
"main_menu" => [
"url" => "dealers",
"selected" => "dealers",
"icon" => "fas fa-tachometer-alt",
"name" => "menu_dealers"
]
],
"buildtool" => [
"main_menu" => [
"url" => "buildtool",
@@ -307,6 +315,8 @@ $page_rows_discounts = 25;//discounts
$page_rows_shipping = 25;//discounts
$page_rows_transactions = 25; //transactions
$page_rows_invoice = 25; //invoices
$page_rows_dealers = 25; //dealers
//------------------------------------------
// Languages supported
//------------------------------------------

View File

@@ -17,6 +17,9 @@ $all_views = [
"cartests",
"cartest",
"cartest_manage",
"dealers",
"dealer",
"dealer_manage",
"assets",
"equipments",
"equipment",

View File

@@ -15,73 +15,83 @@ $create_allowed = isAllowed($page ,$_SESSION['profile'],$_SESSION['permission'],
if ($create_allowed === 1 && $_POST){
$log_results = [];
foreach ($_POST as $contents){
$tableName = json_decode($_POST['table'],true);
$contents = json_decode($_POST['data'],true);
$contents = json_decode($contents,true);
foreach ($contents as $content){
foreach ($contents as $content){
switch ($tableName) {
//CHECK IF VARIABLE EXISTS
$text_variable = ioServer('/v2/translations/variable='.$content['variable'],'');
if (!empty($text_variable)){$text_variable = json_decode($text_variable,true);}else{$text_variable = null;}
case 'text_variables':
//CHECK IF VARIABLE EXISTS
$text_variable = ioServer('/v2/translations/variable='.$content['variable'],'');
if (!empty($text_variable)){$text_variable = json_decode($text_variable,true);}else{$text_variable = null;}
//IF NO ERRORS PROCES IT
if (is_array($text_variable)){
if (count($text_variable) == 0){
//VARIABLE NOT FOUND -> CREATE
$payload = json_encode(array("variable" => $content['variable']), JSON_UNESCAPED_UNICODE);
//API call
$text_variable_new = ioServer('/v2/translations',$payload);
//returns results
$text_variable_new = json_decode($text_variable_new ,true);
//Provide feedback
$log_results[$content['variable']]['rowID'] = $text_variable_new['rowID'].' created';
}
$text_variable = $text_variable[0] ?? 'new';
//VARIABLE ROWID
$text_variable_rowid = ($text_variable != 'new') ? $text_variable['rowID'] : $text_variable_new['rowID'];
foreach ($content as $key => $var){
if ($var != $content['variable']){
$language_key = strtoupper($key);
//check if variable_id and language_key combination already exists
$text_translation = ioServer('/v2/translations_details/variable_ID='.$text_variable_rowid.'&language_key='.$language_key,'');
if (!empty($text_translation)){$text_translation = json_decode($text_translation,true);}else{$text_translation = null;}
if (count($text_translation) == 0){
//TRANSLATION NOT FOUND ->CREATE
$payload = json_encode(array("variable_ID" => $text_variable_rowid, "language_key" => $language_key, "translation" => $var), JSON_UNESCAPED_UNICODE);
$text_translation_new = ioServer('/v2/translations_details',$payload);
$text_translation_new = json_decode($text_translation_new ,true);
//Provide feedback
$log_results[$content['variable']][$language_key] = $text_translation_new['rowID'].' created';
}
elseif(count($text_translation) > 0){
$text_translation = $text_translation[0];
//TRANSLATION FOUND -> UPDATE
$payload = json_encode(array("rowID" => $text_translation['rowID'] , "translation" => $var), JSON_UNESCAPED_UNICODE);
$text_translation = ioServer('/v2/translations_details',$payload);
$text_translation = json_decode($text_translation ,true);
//Provide feedback
$log_results[$content['variable']][$language_key] = $text_translation['rowID'].' updated';
} else {
$log_results[$content['variable']][$language_key] = 'not updated';
}
//IF NO ERRORS PROCES IT
if (is_array($text_variable)){
if (count($text_variable) == 0){
//VARIABLE NOT FOUND -> CREATE
$payload = json_encode(array("variable" => $content['variable']), JSON_UNESCAPED_UNICODE);
//API call
$text_variable_new = ioServer('/v2/translations',$payload);
//returns results
$text_variable_new = json_decode($text_variable_new ,true);
//Provide feedback
$log_results[$content['variable']]['rowID'] = $text_variable_new['rowID'].' created';
}
$text_variable = $text_variable[0] ?? 'new';
//VARIABLE ROWID
$text_variable_rowid = ($text_variable != 'new') ? $text_variable['rowID'] : $text_variable_new['rowID'];
foreach ($content as $key => $var){
if ($var != $content['variable']){
$language_key = strtoupper($key);
//check if variable_id and language_key combination already exists
$text_translation = ioServer('/v2/translations_details/variable_ID='.$text_variable_rowid.'&language_key='.$language_key,'');
if (!empty($text_translation)){$text_translation = json_decode($text_translation,true);}else{$text_translation = null;}
if (count($text_translation) == 0){
//TRANSLATION NOT FOUND ->CREATE
$payload = json_encode(array("variable_ID" => $text_variable_rowid, "language_key" => $language_key, "translation" => $var), JSON_UNESCAPED_UNICODE);
$text_translation_new = ioServer('/v2/translations_details',$payload);
$text_translation_new = json_decode($text_translation_new ,true);
//Provide feedback
$log_results[$content['variable']][$language_key] = $text_translation_new['rowID'].' created';
}
elseif(count($text_translation) > 0){
$text_translation = $text_translation[0];
//TRANSLATION FOUND -> UPDATE
$payload = json_encode(array("rowID" => $text_translation['rowID'] , "translation" => $var), JSON_UNESCAPED_UNICODE);
$text_translation_update = ioServer('/v2/translations_details',$payload);
$text_translation_update = json_decode($text_translation_update ,true);
//Provide feedback
$log_results[$content['variable']][$language_key] = $text_translation['rowID'].' updated';
} else {
$log_results[$content['variable']][$language_key] = 'not updated';
}
}
}
} else {
$log_results[$content['variable']] = 'error';
}
} else {
$log_results[$content['variable']] = 'error';
}
break;
case 'dealers':
break;
}
}
print_r($log_results);
return $log_results;
}
@@ -177,6 +187,10 @@ $view ='
<div id="tableOutput" style="display:none;">
<h2>Table Preview</h2>
<div class="controls">
<select name="table_name" id="table_name" class="form-control" required>
<option value="text_variables">'.($menu_translations ?? 'Translations').'</option>
<option value="dealers">'.($menu_dealers ?? 'Dealers').'</option>
</select>
<button id="processDataBtn" class="btn">Process Data</button>
<button id="downloadCsvBtn" class="btn">Download as CSV</button>
</div>
@@ -203,6 +217,7 @@ $view ='
const processingResults = document.getElementById(\'processingResults\');
const resultsContent = document.getElementById(\'resultsContent\');
const downloadCsvBtn = document.getElementById(\'downloadCsvBtn\');
const tableName = document.getElementById(\'table_name\').value;
// Convert pasted Excel data to HTML table
convertBtn.addEventListener(\'click\', function() {
@@ -271,6 +286,7 @@ $view ='
// Use standard form submission approach instead of JSON
const form = new FormData();
form.append(\'table\', JSON.stringify(tableName));
form.append(\'data\', JSON.stringify(tableData));
fetch(window.location.href, {