From f994af16dabbae5cf04561b3d5f72c21c3e53bc3 Mon Sep 17 00:00:00 2001 From: Computroniks Date: Wed, 4 Aug 2021 22:09:50 +0100 Subject: [PATCH 1/3] 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 --- app/Presenters/AccessoryPresenter.php | 2 +- app/Presenters/ComponentPresenter.php | 2 +- app/Presenters/ConsumablePresenter.php | 2 +- app/Presenters/LicensePresenter.php | 2 +- .../views/partials/bootstrap-table.blade.php | 18 ++++++++++++++++++ 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/app/Presenters/AccessoryPresenter.php b/app/Presenters/AccessoryPresenter.php index bf7074ad5..6295d5174 100644 --- a/app/Presenters/AccessoryPresenter.php +++ b/app/Presenters/AccessoryPresenter.php @@ -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, diff --git a/app/Presenters/ComponentPresenter.php b/app/Presenters/ComponentPresenter.php index 669b5c651..ae85a0720 100644 --- a/app/Presenters/ComponentPresenter.php +++ b/app/Presenters/ComponentPresenter.php @@ -103,7 +103,7 @@ class ComponentPresenter extends Presenter "sortable" => true, "title" => trans('general.purchase_cost'), "visible" => true, - "footerFormatter" => 'sumFormatter', + "footerFormatter" => 'sumFormatterQuantity', ], ]; diff --git a/app/Presenters/ConsumablePresenter.php b/app/Presenters/ConsumablePresenter.php index 334398fab..a7511098c 100644 --- a/app/Presenters/ConsumablePresenter.php +++ b/app/Presenters/ConsumablePresenter.php @@ -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, diff --git a/app/Presenters/LicensePresenter.php b/app/Presenters/LicensePresenter.php index be5a600c4..9b79d7e79 100644 --- a/app/Presenters/LicensePresenter.php +++ b/app/Presenters/LicensePresenter.php @@ -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, diff --git a/resources/views/partials/bootstrap-table.blade.php b/resources/views/partials/bootstrap-table.blade.php index 13de846d3..2a319ef64 100644 --- a/resources/views/partials/bootstrap-table.blade.php +++ b/resources/views/partials/bootstrap-table.blade.php @@ -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("."); From 8121d904e7158a881a60a0eab0cdb2c3b0b5b2ee Mon Sep 17 00:00:00 2001 From: Computroniks Date: Wed, 4 Aug 2021 22:33:39 +0100 Subject: [PATCH 2/3] Licence cost calculation Licences use diffrent key to track quantity. sumFormatterQuantity has been modified to detect which key to use. Signed-off-by: Computroniks --- .../views/partials/bootstrap-table.blade.php | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/resources/views/partials/bootstrap-table.blade.php b/resources/views/partials/bootstrap-table.blade.php index 2a319ef64..1065453d8 100644 --- a/resources/views/partials/bootstrap-table.blade.php +++ b/resources/views/partials/bootstrap-table.blade.php @@ -608,18 +608,25 @@ function sumFormatterQuantity(data){ if(Array.isArray(data)) { - // Check that we are actually trying to sum cost from a table - // that has a quantity column + + // Prevents issues on page load where data is an empty array 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)); + // Check that we are actually trying to sum cost from a table + // that has a quantity column. We must perform this check to + // support licences which use seats instead of qty + if('qty' in data[0]) { + var multiplier = 'qty'; + } else if('seats' in data[0]) { + var multiplier = 'seats'; + } else { + return 'no quantity'; } - return 'no quantity'; + var total_sum = data.reduce(function(sum, row) { + return (sum) + (parseFloat(row["purchase_cost"])*row[multiplier] || 0); + }, 0); + return numberWithCommas(total_sum.toFixed(2)); } return 'not an array'; } From 90ca66834bd9fafa8c35269278c09fed55721f1b Mon Sep 17 00:00:00 2001 From: Matthew Nickson Date: Sat, 30 Oct 2021 22:48:45 +0100 Subject: [PATCH 3/3] Fixed sumFormatterQuantity if using 1.234,56 fomat Previously sumFormatterQuantity used the parseFloat to convert the string purchase_cost to a floating point number. parseFloat does not return the correct value when using the comma format. To fix this sumFormatterQuantity now used the cleanFloat function to convert purchase_cost to a float. Signed-off-by: Matthew Nickson --- resources/views/partials/bootstrap-table.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/partials/bootstrap-table.blade.php b/resources/views/partials/bootstrap-table.blade.php index 1cce39f95..61ac230f6 100644 --- a/resources/views/partials/bootstrap-table.blade.php +++ b/resources/views/partials/bootstrap-table.blade.php @@ -639,7 +639,7 @@ return 'no quantity'; } var total_sum = data.reduce(function(sum, row) { - return (sum) + (parseFloat(row["purchase_cost"])*row[multiplier] || 0); + return (sum) + (cleanFloat(row["purchase_cost"])*row[multiplier] || 0); }, 0); return numberWithCommas(total_sum.toFixed(2)); }