Added function to calculate cost based on quantity

sumFormatterQuantity takes the same input as sumFormatter but instead
of calculating the specified columns total it calculates the total
purchase cost of an item based upon its quantity. Also updated affected
pressenters to use this formatter.

Signed-off-by: Computroniks <mnickson@sidingsmedia.com>
This commit is contained in:
Computroniks 2021-08-04 22:09:50 +01:00
parent cdc4940338
commit f994af16da
No known key found for this signature in database
GPG key ID: AB524D88499439A1
5 changed files with 22 additions and 4 deletions

View file

@ -102,7 +102,7 @@ class AccessoryPresenter extends Presenter
"searchable" => true,
"sortable" => true,
"title" => trans('general.purchase_cost'),
"footerFormatter" => 'sumFormatter',
"footerFormatter" => 'sumFormatterQuantity',
], [
"field" => "order_number",
"searchable" => true,

View file

@ -103,7 +103,7 @@ class ComponentPresenter extends Presenter
"sortable" => true,
"title" => trans('general.purchase_cost'),
"visible" => true,
"footerFormatter" => 'sumFormatter',
"footerFormatter" => 'sumFormatterQuantity',
],
];

View file

@ -116,7 +116,7 @@ class ConsumablePresenter extends Presenter
"sortable" => true,
"title" => trans('general.purchase_cost'),
"visible" => true,
"footerFormatter" => 'sumFormatter',
"footerFormatter" => 'sumFormatterQuantity',
],[
"field" => "change",
"searchable" => false,

View file

@ -137,7 +137,7 @@ class LicensePresenter extends Presenter
"sortable" => true,
"visible" => false,
"title" => trans('general.purchase_cost'),
"footerFormatter" => 'sumFormatter',
"footerFormatter" => 'sumFormatterQuantity',
], [
"field" => "purchase_order",
"searchable" => true,

View file

@ -606,6 +606,24 @@
return 'not an array';
}
function sumFormatterQuantity(data){
if(Array.isArray(data)) {
// Check that we are actually trying to sum cost from a table
// that has a quantity column
if(data[0] == undefined){
return 0.00
}
if("qty" in data[0]) {
var total_sum = data.reduce(function(sum, row) {
return (sum) + (parseFloat(row["purchase_cost"])*row["qty"] || 0);
}, 0);
return numberWithCommas(total_sum.toFixed(2));
}
return 'no quantity';
}
return 'not an array';
}
function numberWithCommas(value) {
if ((value) && ("{{$snipeSettings->digit_separator}}" == "1.234,56")){
var parts = value.toString().split(".");