Skip to content

Commit

Permalink
Merge pull request #9902 from SidingsMedia/sum_cost_by_quantity
Browse files Browse the repository at this point in the history
Fixed #5676: Sum cost by quantity
  • Loading branch information
snipe committed Dec 2, 2021
2 parents 4c2d47e + 9381ba2 commit cae62fd
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/Presenters/AccessoryPresenter.php
Expand Up @@ -102,7 +102,7 @@ public static function dataTableLayout()
'searchable' => true,
'sortable' => true,
'title' => trans('general.purchase_cost'),
'footerFormatter' => 'sumFormatter',
'footerFormatter' => 'sumFormatterQuantity',
'class' => 'text-right',
], [
'field' => 'order_number',
Expand Down
2 changes: 1 addition & 1 deletion app/Presenters/ComponentPresenter.php
Expand Up @@ -101,7 +101,7 @@ public static function dataTableLayout()
'sortable' => true,
'title' => trans('general.purchase_cost'),
'visible' => true,
'footerFormatter' => 'sumFormatter',
'footerFormatter' => 'sumFormatterQuantity',
'class' => 'text-right',
],
];
Expand Down
2 changes: 1 addition & 1 deletion app/Presenters/ConsumablePresenter.php
Expand Up @@ -113,7 +113,7 @@ public static function dataTableLayout()
'sortable' => true,
'title' => trans('general.purchase_cost'),
'visible' => true,
'footerFormatter' => 'sumFormatter',
'footerFormatter' => 'sumFormatterQuantity',
'class' => 'text-right',
], [
'field' => 'change',
Expand Down
2 changes: 1 addition & 1 deletion app/Presenters/LicensePresenter.php
Expand Up @@ -136,7 +136,7 @@ public static function dataTableLayout()
'sortable' => true,
'visible' => false,
'title' => trans('general.purchase_cost'),
'footerFormatter' => 'sumFormatter',
'footerFormatter' => 'sumFormatterQuantity',
'class' => 'text-right',
], [
'field' => 'purchase_order',
Expand Down
25 changes: 25 additions & 0 deletions resources/views/partials/bootstrap-table.blade.php
Expand Up @@ -653,6 +653,31 @@ function sumFormatter(data) {
return 'not an array';
}
function sumFormatterQuantity(data){
if(Array.isArray(data)) {
// Prevents issues on page load where data is an empty array
if(data[0] == undefined){
return 0.00
}
// 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';
}
var total_sum = data.reduce(function(sum, row) {
return (sum) + (cleanFloat(row["purchase_cost"])*row[multiplier] || 0);
}, 0);
return numberWithCommas(total_sum.toFixed(2));
}
return 'not an array';
}
function numberWithCommas(value) {
if ((value) && ("{{$snipeSettings->digit_separator}}" == "1.234,56")){
var parts = value.toString().split(".");
Expand Down

0 comments on commit cae62fd

Please sign in to comment.