Skip to content
This repository has been archived by the owner on Mar 26, 2023. It is now read-only.

Commit

Permalink
SKU
Browse files Browse the repository at this point in the history
SKU prompt in Product Edit is controlled and data must be unique for the account.  A special warning message will be shown if an used SKU is entered, and it will not be possible to save the change. #40
  • Loading branch information
Dreller committed May 25, 2021
1 parent cd3a128 commit 0f09a11
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 9 deletions.
67 changes: 67 additions & 0 deletions js/glutax.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var modalPurchReceipt;
var temporaryObjectStore;

$(document).ready(function(){
modalPurchReceipt = new bootstrap.Modal(document.getElementById('purchaseReceipt'), {
Expand Down Expand Up @@ -203,6 +204,68 @@ function wrapForm(formID){
}
}

/**
* Send a GET request to the server
* @param {String} queryType Type of query: SKU.
* @param {String} queryData
*/
function queryDB(queryType, queryData, queryCallback){
var engineURL = "php/gtQuery.php";

var url = engineURL + "?type=" + queryType + "&" + queryData;
console.log("Query to: " + url);

var http = new XMLHttpRequest();
http.open("GET", url, true);

http.onreadystatechange = function(){
if( http.readyState === 4 && http.status === 200){
window[queryCallback](http.responseText);
}
}
http.send();
}

/**
*
* @param {String} sku SKU to validate
* @returns SKU if it can be used, or empty if SKU is already used.
*/
function validateSKU(obj){
var wip = '';
temporaryObjectStore = obj;
var sku = obj.value + '';

if( sku == '' ){
temporaryObjectStore.classList.remove("is-invalid");
document.getElementById(temporaryObjectStore.id + "-invalid").innerHTML = "";
return false;
}

wip = sku.split(' ').join('');
wip = wip.toUpperCase();

var db = "";
var queryString = "sku=" + wip + "&p=" + $("#id").val();
db = queryDB('SKU', queryString, 'validateSKU_cb');
}


function validateSKU_cb(data){
console.log('My response = ' + data);
var resp = JSON.parse(data);

if( resp.result != 'ok' ){
temporaryObjectStore.classList.add("is-invalid");
document.getElementById(temporaryObjectStore.id + "-invalid").innerHTML = resp.msg;
$("#Save").addClass("disabled");
} else {
temporaryObjectStore.classList.remove("is-invalid");
document.getElementById(temporaryObjectStore.id + "-invalid").innerHTML = "";
$("#Save").removeClass("disabled");
}
}

/**
* Display a message as a toast.
* @param {String} message Message to show in the Toast.
Expand All @@ -218,6 +281,10 @@ function toast(message){
toastList.forEach(toast => toast.show());
}

/**
* Display a message in a modal, the user will have to click OK.
* @param {String} message Message to show in the Modal.
*/
function tell(message){

document.getElementById('tellModalText').innerHTML = message;
Expand Down
11 changes: 2 additions & 9 deletions php/gtForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,8 @@ function createControl($type, $id, $value){
$wip.= "</select>";
break;
case "sku":
$wip = "<input type='text' id='$id' name='$id' class='form-control' value='$value' autocomplete='off' onblur='this.value=cleanSKU(this.value);'>";
$wip.= "<script language='javascript' type='text/javascript'>
function cleanSKU(string){
var wip = '';
wip = string.split(' ').join('');
wip = wip.toUpperCase();
return wip;
}
</script>";
$wip = "<input type='text' id='$id' name='$id' class='form-control' value='$value' autocomplete='off' onblur='validateSKU(this);'>";
$wip.= "<div class='invalid-feedback' id='$id-invalid'></div>";
break;
default:
# By default, it's a regular and basic Input.
Expand Down
42 changes: 42 additions & 0 deletions php/gtQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
session_start();
include_once( 'lang/' . $_SESSION['accountLanguage'] . '.php' );
include_once('gtMap.php');

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require('gtDb.php');
$db = new gtDb();

$http = 0;
$json = Array();

$result = "";
$message = "";

switch($_GET['type']){
case "SKU":
$sku = $_GET['sku'];
$prod= $_GET['p'];
$db->where(_SQL_PRO_ACCOUNT, $_SESSION[_SQL_ACC_ID]);
$db->where(_SQL_PRO_SKU, $sku);
$db->where(_SQL_PRO_ID, $prod, "<>");
$temp = $db->getOne(_SQL_PRO);
if( $db->count > 0 ){
$result = "error";
$message = sprintf(_ERROR_SKU_USED, $sku, $temp[_SQL_PRO_NAME]);
}else{
$result = "ok";
$message = $sku;
}
}

$json['result'] = $result;
if( $message != "" ){
$json['msg'] = $message;
}
header('Content-Type: application/json');
echo json_encode($json);
?>
2 changes: 2 additions & 0 deletions php/lang/EN.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,6 @@
define("_TOAST_TABLE_ADDED", "New item added");
define("_TOAST_TABLE_UPDATED", "Item updated");

define("_ERROR_SKU_USED", "%s already used by product %s");

?>
2 changes: 2 additions & 0 deletions php/lang/FR.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,6 @@
define("_TOAST_TABLE_ADDED", "Nouvel item ajout&eacute;");
define("_TOAST_TABLE_UPDATED", "Item modifi&eacute;");

define("_ERROR_SKU_USED", "<strong>%s</strong> d&eacute;j&agrave; utilis&eacute; par le produit <strong>%s</strong>");

?>

0 comments on commit 0f09a11

Please sign in to comment.