Skip to content

Commit

Permalink
Merge pull request #184 from globaldyne/v11.2
Browse files Browse the repository at this point in the history
V11.2
  • Loading branch information
globaldyne authored Sep 7, 2024
2 parents cc82631 + c16b9d5 commit 8a91391
Show file tree
Hide file tree
Showing 47 changed files with 1,802 additions and 1,481 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
# CHANGELOG
### Version 11.2
- Refactor of backend
- Fix author name not display properly in the contact form in Marketplace
- IFRA library categories changed to FLOAT
- Added pdf and csv export for formula analysis
- If now numeric value found when IFRA library is looked-up in formula analysis, will return 'No value'
- Fixed an issue preventing formula analysis to show the max allowed value from the IFRA library
- Increase page size for formula analysis
- Add full usage tab for formula per IFRA category
- Ignore non-numeric values in IFRA library when calculating max usage

### Version 11.1
- When an ingredient is excluded from formula calculation will also be excluded from any IFRA validations
- Add a warning in a formula when the ingredient has ifra by passed
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ RUN sed -i \
-e 's~^session.auto_start.*$~session.auto_start = 1~g' \
/etc/php.ini

ENV LANG en_GB.UTF-8
ENV LANG=en_GB.UTF-8

ADD . /html
RUN if [ -f .git/COMMIT_EDITMSG ]; then \
Expand Down
2 changes: 1 addition & 1 deletion VERSION.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
11.1
11.2
2 changes: 1 addition & 1 deletion core/full_formula_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@
$lastValAccepted = null;

for ($c = 1; $c <= 100; $c++) {
$result = validateFormula($meta['fid'], 100, $c, $mg['total_mg'], $defCatClass, $settings['qStep']);
$result = validateFormula($meta['fid'], 100, $c, $mg['total_mg'], $defCatClass, $settings['qStep'],1);

if ($result === 0) {
$lastValAccepted = $c;
Expand Down
79 changes: 39 additions & 40 deletions core/list_SDS_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,60 @@
require_once(__ROOT__.'/inc/opendb.php');
require_once(__ROOT__.'/inc/settings.php');

$row = $_POST['start'] ?: 0;
$limit = $_POST['length'] ?: 10;
$row = isset($_POST['start']) ? (int)$_POST['start'] : 0;
$limit = isset($_POST['length']) ? (int)$_POST['length'] : 10;
$order_by = isset($_POST['order_by']) ? mysqli_real_escape_string($conn, $_POST['order_by']) : 'product_name';
$order = isset($_POST['order_as']) ? mysqli_real_escape_string($conn, $_POST['order_as']) : 'ASC';
$search_value = isset($_POST['search']['value']) ? trim(mysqli_real_escape_string($conn, $_POST['search']['value'])) : '';

$order_by = $_POST['order_by'] ?: 'product_name';
$order = $_POST['order_as'] ?: 'ASC';
$extra = "ORDER BY ".$order_by." ".$order;
$table = 'sds_data';
$extra = "ORDER BY $order_by $order";

$s = trim($_POST['search']['value']);
$t = 'sds_data';
$filter = $search_value !== '' ? "WHERE product_name LIKE '%$search_value%'" : '';

if($s != ''){
$f = "WHERE 1 AND (product_name LIKE '%".$s."%')";
}
$q = mysqli_query($conn, "SELECT * FROM $t $f $extra LIMIT $row, $limit");
while($res = mysqli_fetch_array($q)){
$query = mysqli_query($conn, "SELECT * FROM $table $filter $extra LIMIT $row, $limit");

$rs = [];
while ($res = mysqli_fetch_assoc($query)) {
$rs[] = $res;
}




$rx = [];

foreach ($rs as $rq) {
$r['id'] = (int)$rq['id'];
$r['product_name'] = (string)$rq['product_name'];
$r['product_use'] = (string)$rq['product_use'];
$r['country'] = (string)$rq['country'];
$r['language'] = (string)$rq['language'];
$r['product_type'] = (string)$rq['product_type'];
$r['state_type'] = (string)$rq['state_type'];
$r['supplier_id'] = (int)$rq['supplier_id'];
$r['docID'] = (int)$rq['id'];

$r['updated'] = (string)$rq['updated'] ?: '00:00:00';
$r['created'] = (string)$rq['created'] ?: '00:00:00';

$rx[] = $r;
$rx[] = [
'id' => (int)$rq['id'],
'product_name' => (string)$rq['product_name'],
'product_use' => (string)$rq['product_use'],
'country' => (string)$rq['country'],
'language' => (string)$rq['language'],
'product_type' => (string)$rq['product_type'],
'state_type' => (string)$rq['state_type'],
'supplier_id' => (int)$rq['supplier_id'],
'docID' => (int)$rq['id'],
'updated' => (string)($rq['updated'] ?: '00:00:00'),
'created' => (string)($rq['created'] ?: '00:00:00'),
];
}

$total = mysqli_fetch_assoc(mysqli_query($conn,"SELECT COUNT(id) AS entries FROM $t"));
$filtered = mysqli_fetch_assoc(mysqli_query($conn,"SELECT COUNT(id) AS entries FROM $t ".$f));
$total_query = mysqli_query($conn, "SELECT COUNT(id) AS entries FROM $table");
$total = mysqli_fetch_assoc($total_query)['entries'];

$response = array(
"draw" => (int)$_POST['draw'],
"recordsTotal" => (int)$total['entries'],
"recordsFiltered" => (int)$filtered['entries'],
"data" => $rx
);
$filtered_query = mysqli_query($conn, "SELECT COUNT(id) AS entries FROM $table $filter");
$filtered = mysqli_fetch_assoc($filtered_query)['entries'];

if(empty($r)){
$response['data'] = [];
$response = [
"draw" => isset($_POST['draw']) ? (int)$_POST['draw'] : 1,
"recordsTotal" => (int)$total,
"recordsFiltered" => (int)$filtered,
"data" => $rx
];

if(empty($rx)){
$response['data'] = [];
}

header('Content-Type: application/json; charset=utf-8');
echo json_encode($response);
return;
?>

85 changes: 59 additions & 26 deletions core/list_formula_analysis_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,92 @@

$defCatClass = $settings['defCatClass'];

$stmt = $conn->prepare("SELECT ingredient, quantity FROM formulas WHERE fid = ?");
$stmt->bind_param("s", $_POST["fid"]);
$stmt->execute();
$result = $stmt->get_result();

$q = mysqli_query($conn, "SELECT ingredient, quantity FROM formulas WHERE fid = '".$_POST["fid"]."'");
$total_quantity = 0;
$formula_data = array();
$total_quantity = 0;

while ($rq = mysqli_fetch_array($q)) {
while ($rq = $result->fetch_assoc()) {
$formula_data[] = $rq;
$total_quantity += $rq['quantity']; // Calculate total quantity
$total_quantity += $rq['quantity'];
}

$stmt->close();

if (empty($formula_data)) {
$response = ['data' => []];
header('Content-Type: application/json; charset=utf-8');
echo json_encode($response);
return;
}

$ingredients = array_column($formula_data, 'ingredient');
$placeholders = implode(',', array_fill(0, count($ingredients), '?'));
$stmt2 = $conn->prepare("SELECT id, ing, name, cas, min_percentage, max_percentage FROM ingredient_compounds WHERE ing IN ($placeholders)");
$stmt2->bind_param(str_repeat('s', count($ingredients)), ...$ingredients);
$stmt2->execute();
$result2 = $stmt2->get_result();

$get_data_ings = array();
foreach ($formula_data as $data) {
$q2 = mysqli_query($conn, "SELECT id, ing, name, cas, min_percentage, max_percentage FROM ingredient_compounds WHERE ing = '".$data['ingredient']."'");

while ($res = mysqli_fetch_array($q2)) {
$res['quantity'] = $data['quantity']; // Include quantity in the ingredient data
$get_data_ings[] = $res;
while ($res = $result2->fetch_assoc()) {
foreach ($formula_data as $data) {
if ($res['ing'] === $data['ingredient']) {
$res['quantity'] = $data['quantity'];
$get_data_ings[] = $res;
break;
}
}
}

$response = array();
$stmt2->close();

$response = ['data' => []];
$ingredientIds = [];

foreach ($get_data_ings as $get_data_ing) {
$r = array();

// Cache ingredient ID lookups to reduce redundant queries
if (!isset($ingredientIds[$get_data_ing['ing']])) {
$stmt3 = $conn->prepare("SELECT id FROM ingredients WHERE name = ?");
$stmt3->bind_param("s", $get_data_ing['ing']);
$stmt3->execute();
$ingredientIdResult = $stmt3->get_result();
$ingID = $ingredientIdResult->fetch_assoc();
$ingredientIds[$get_data_ing['ing']] = (int)$ingID['id'];
$stmt3->close();
}

$r['id'] = $ingredientIds[$get_data_ing['ing']];
$r['main_ing'] = (string)$get_data_ing['ing'];
$r['sub_ing'] = (string)$get_data_ing['name'];
$r['cas'] = (string)$get_data_ing['cas'] ?: 'N/A';
$r['min_percentage'] = (float)$get_data_ing['min_percentage'] ?: 0;
$r['max_percentage'] = (float)$get_data_ing['max_percentage'] ?: 0;
$r['avg_percentage'] = ($r['min_percentage'] + $r['max_percentage']) / 2;

$r['formula_percentage'] = ($get_data_ing['quantity'] / $total_quantity) * 100;

$conc_p = number_format(($r['avg_percentage'] / 100 * $get_data_ing['quantity'] * $r['formula_percentage'] ) / 100, 5);
if($settings['multi_dim_perc'] == '1'){
$conc_p = number_format(($r['avg_percentage'] / 100 * $get_data_ing['quantity'] * $r['formula_percentage']) / 100, 5);

$conc_p += multi_dim_perc($conn, $formula_data, $get_data_ing['cas'], $settings['qStep'], $settings['defPercentage'])[$cas['cas']];
}

$r['contained_percentage'] = $conc_p;
$u = searchIFRA($get_data_ing['cas'], $get_data_ing['name'], null, $defCatClass);
if ($settings['multi_dim_perc'] == '1') {
$multi_dim_result = multi_dim_perc($conn, $formula_data, $get_data_ing['cas'], $settings['qStep'], $settings['defPercentage']);
$conc_p += $multi_dim_result[$get_data_ing['cas']] ?? 0;
}

$r['max_allowed'] = $u[0];
$r['contained_percentage'] = $conc_p;

$response['data'][] = $r;
}
$u = searchIFRA($get_data_ing['cas'], $get_data_ing['name'], null, $defCatClass);
$r['max_allowed_val'] = $u['val'] ?? $u['type'] ?? 'No value';
$r['max_allowed_reason'] = $u['risk'] ?? '';

if (empty($response['data'])) {
$response['data'] = [];
$response['data'][] = $r;
}

header('Content-Type: application/json; charset=utf-8');
echo json_encode($response);
return;
?>


?>
44 changes: 25 additions & 19 deletions core/list_formula_attachments_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,36 @@

$ingID = mysqli_real_escape_string($conn, $_POST["id"]);

$q = mysqli_query($conn, "SELECT * FROM documents WHERE ownerID = '$ingID' AND type = '5'");
while($res = mysqli_fetch_array($q)){
$docs[] = $res;
$stmt = $conn->prepare("SELECT * FROM documents WHERE ownerID = ? AND type = ?");
$type = 5;
$stmt->bind_param("ii", $ingID, $type);
$stmt->execute();
$result = $stmt->get_result();

$response = ['data' => []];

while ($doc = $result->fetch_assoc()) {
$r = [
'id' => (int)$doc['id'],
'ownerID' => (int)$doc['ownerID'],
'type' => (int)$doc['type'],
'name' => (string)$doc['name'] ?: 'N/A',
'notes' => (string)$doc['notes'] ?: 'N/A',
'created' => (string)$doc['created'] ?: 'N/A',
'docData' => (string)$doc['docData'],
'docSize' => formatBytes(strlen($doc['docData']))
];

$response['data'][] = $r;
}

foreach ($docs as $doc) {
$stmt->close();

$r['id'] = (int)$doc['id'];
$r['ownerID'] = (int)$doc['ownerID'];
$r['type'] = (int)$doc['type'];
$r['name'] = (string)$doc['name']?:'N/A';
$r['notes'] = (string)$doc['notes']?:'N/A';
$r['created'] = (string)$doc['created']?:'N/A';
$r['docData'] = (string)$doc['docData'];
$r['docSize'] = (string)formatBytes(strlen($doc['docData']));

$response['data'][] = $r;
}

if(empty($r)){
$response['data'] = [];
if (empty($response['data'])) {
$response['data'] = [];
}

header('Content-Type: application/json; charset=utf-8');
echo json_encode($response);
return;
?>
?>
Loading

0 comments on commit 8a91391

Please sign in to comment.