improve receipt/details reports and implement number formatting

1) eager load reports for receipt/details
2) introduce number_format lookups to replace print_settings for number formatting
3) implement NumberFormattable concern, reference number_format lookups or print_settings if not exist, to get number format settings and number formatting
4) replace rails NumberHelper.number_with_precision with NumberFormattable.number_format hopefully to reduce overhead, formatting numbers for huge lists of data
This commit is contained in:
Thein Lin Kyaw
2019-11-25 23:17:53 +06:30
parent a36e170d94
commit 3c1cc737b5
71 changed files with 1338 additions and 1898 deletions

View File

@@ -2,20 +2,6 @@
<div id="loading_wrapper" style="display:none;">
<div id="loading"></div>
</div>
<% if !@print_settings.nil? %>
<% if @print_settings.precision.to_i > 0
precision = @print_settings.precision
else
precision = 0
end
#check delimiter
if @print_settings.delimiter
delimiter = ","
else
delimiter = ""
end
%>
<% end %>
<div class="row">
<!-- Column One -->
<div class="col-lg-6 col-md-6 col-sm-6">
@@ -96,7 +82,7 @@
<table class="table" id="order-charges-table" border="0">
<tr>
<td class="charges-name"><strong>Sub Total:</strong></td>
<td class="item-attr"><strong id="order-sub-total"><%= number_with_precision(sub_total, precision: precision.to_i ) rescue number_with_precision(0, precision: precision.to_i ) %></strong></td>
<td class="item-attr"><strong id="order-sub-total"><%= number_format(sub_total, precision: precision.to_i ) rescue number_format(0, precision: precision.to_i ) %></strong></td>
</tr>
<!-- <tr>
<td class="charges-name"><strong>Food:</strong></td>
@@ -113,15 +99,15 @@
<td class="charges-name"><strong>Discount:</strong></td>
<%end%>
<td class="item-attr">(<strong id="order-discount"><%= number_with_precision(@sale_data.total_discount, precision: precision.to_i ) rescue number_with_precision(0, precision: precision.to_i ) %></strong>)</td>
<td class="item-attr">(<strong id="order-discount"><%= number_format(@sale_data.total_discount, precision: precision.to_i ) rescue number_format(0, precision: precision.to_i ) %></strong>)</td>
</tr>
<tr class="hidden">
<td class="charges-name"><strong>Tax:</strong></td>
<td class="item-attr"><strong id="order-Tax"><%= number_with_precision(@sale_data.total_tax, precision: precision.to_i ) rescue number_with_precision(0, precision: precision.to_i ) %></strong></td>
<td class="item-attr"><strong id="order-Tax"><%= number_format(@sale_data.total_tax, precision: precision.to_i ) rescue number_format(0, precision: precision.to_i ) %></strong></td>
</tr>
<tr class="hidden">
<td class="charges-name"><strong>Grand Total:</strong></td>
<td class="item-attr"><strong id="order-grand-total"><%= number_with_precision(@sale_data.grand_total, precision: precision.to_i ) rescue number_with_precision(0, precision: precision.to_i ) %></strong></td>
<td class="item-attr"><strong id="order-grand-total"><%= number_format(@sale_data.grand_total, precision: precision.to_i ) rescue number_format(0, precision: precision.to_i ) %></strong></td>
</tr>
</table>
</div>
@@ -283,6 +269,8 @@ var cashier_type = "<%= @cashier_type %>";
var totalAmount = <%= sub_total %>;
var totalDiscount = <%= @sale_data.total_discount %>
var precision = <%= precision %>;
var originalAmount = totalAmount;
var originalDiscount = totalDiscount;
$(document).ready(function(){
setHeaderBreadCrumb(_DISCOUNTS_);
/* check webview loaded*/
@@ -306,13 +294,13 @@ var precision = <%= precision %>;
$(".cashier_number").on('click', function(event){
if(event.handled !== true) {
var original_value=0;
var original_value = 0;
original_value = $('#discount-amount').val();
var input_type = $(this).attr("data-type");
switch (input_type) {
case 'num':
var input_value = $(this).attr("data-value");
if (original_value == "0.0"){
if (parseFloat(original_value) == 0){
$('#discount-amount').val(input_value);
}
else{
@@ -329,7 +317,7 @@ var precision = <%= precision %>;
$('#discount-amount').val(discount_text.substr(0,discount_text.length-1));
break;
case 'clr':
$('#discount-amount').val("0.0");
$('#discount-amount').val("0");
break;
}
event.handled = true;
@@ -379,7 +367,7 @@ var precision = <%= precision %>;
e.preventDefault();
var sale_id = $('#sale-id').text();
var discount_value = $('#discount-amount').val();
var sub_total = parseFloat($('#order-sub-total').text());
var sub_total = totalAmount;
var ajax_url = "/origami/" + sale_id + "/discount";
if(discount_value!=""){
@@ -449,18 +437,15 @@ var precision = <%= precision %>;
// Remove selected discount items
$("#remove-item").on('click', function(e){
e.preventDefault();
var origin_sub_total = parseFloat($("#order-sub-total").text());
// var total = 0;
$('.item-row.new-discount').each(function(i){
var amount = parseFloat($(this).find('#item-total-price').text());
totalAmount += Math.abs(amount)
// total = total + Math.abs(amount);
$(this).remove();
});
$("#order-sub-total").text(totalAmount);
$("#order-discount").text(totalDiscount.toFixed(<%= precision.to_i %>));
$("#order-sub-total").text(originalAmount.toFixed(<%= precision.to_i %>));
$("#order-discount").text(originalDiscount.toFixed(<%= precision.to_i %>));
});
// Pay Discount for Payment
@@ -472,8 +457,8 @@ var precision = <%= precision %>;
$("#loading_wrapper").show();
var sale_id = $('#sale-id').text();
var discount_items = JSON.stringify(get_new_discount_item_rows());
var overall_discount = $("#order-discount").text();
var sub_total = $('#order-sub-total').text();
var overall_discount = totalDiscount;
var sub_total = totalAmount;
var ajax_url = "/origami/" + sale_id + "/discount";
var params = { 'cashier_type' : cashier_type,'sale_id': sale_id, 'sub_total': sub_total, 'discount_items': discount_items, 'overall_discount': overall_discount };
@@ -578,7 +563,7 @@ var precision = <%= precision %>;
$("#member-discount").on('click', function(e){
e.preventDefault();
var sale_id = $('#sale-id').text();
var sub_total = $('#order-sub-total').text();
var sub_total = totalAmount;
var ajax_url = "/origami/" + sale_id + "/member_discount";
// Selected Account
@@ -702,9 +687,7 @@ function get_selected_account_types(){
/* Calculate Overall Discount*/
function calculate_overall_discount(type, amount){
var origin_sub_total = parseFloat($("#order-sub-total").text()) + parseFloat($("#order-discount").text());
var dis_amount = 0;
var sub_total = 0;
var sub_total = totalAmount;
var total_discount = 0;
// For Net Pay
@@ -724,18 +707,16 @@ function calculate_overall_discount(type, amount){
});
}
else{
total_discount = Math.round(totalAmount * amount / 100 * Math.pow(10, precision)) / Math.pow(10, precision);
total_discount = Math.round(sub_total * amount / 100 * Math.pow(10, precision)) / Math.pow(10, precision);
}
}
sub_total = parseFloat(totalAmount) - parseFloat(total_discount)
totalDiscount = total_discount
$("#order-discount").text(parseFloat(total_discount).toFixed(<%= precision.to_i %>));
}
/* Calculate Items Discount*/
function calculate_item_discount(type, amount, sale_items, account_types){
var origin_sub_total = parseFloat($("#order-sub-total").text());
var origin_sub_total = totalAmount;
var dis_amount = 0;
var sub_total = 0;
var total_discount = 0;
@@ -749,17 +730,6 @@ function calculate_item_discount(type, amount, sale_items, account_types){
dis_amount = -Math.round(amount * Math.pow(10, precision)) / Math.pow(10, precision);
if(sale_items.length > 0){
for(var i=0;i < sale_items.length;i++){
// if(account_types.length > 0){
// for(var j=0; j < account_types.length; j++){
// if(sale_items[i].account_id == account_types[j].id){
// // Discount Items
// var discount_item_row = item_row_template(type, sale_items[i], dis_amount, amount);
// $("#order-items-table tbody").append(discount_item_row);
// total_discount = total_discount + amount;
// }
// }
// }
// else {
if(parseFloat(amount) > parseFloat(sale_items[i].price)){
arrItemName += ", " + sale_items[i].name;
}else{
@@ -773,7 +743,6 @@ function calculate_item_discount(type, amount, sale_items, account_types){
$("#discount-amountErr").html("Discount is greater than sub total!");
}
}
// }
}
}
@@ -789,26 +758,6 @@ function calculate_item_discount(type, amount, sale_items, account_types){
}
// No Needs For Auto Selected
// if(account_types.length > 0){
// var item_rows=get_item_rows();
// if(item_rows.length > 0){
// for(var k=0; k < item_rows.length; k++){
// for(var j=0; j < account_types.length; j++){
// if(item_rows[k].account_id == account_types[j].id){
// // Discount Items
// var discount_item_row = item_row_template(type, item_rows[k], dis_amount, amount);
// $("#order-items-table tbody").append(discount_item_row);
// total_discount = total_discount + amount;
// }
// }
// }
// }
// else {
// alert("No Items!");
// }
// }
sub_total = parseFloat(origin_sub_total) - parseFloat(total_discount);
totalAmount = sub_total
$("#order-sub-total").text(parseFloat(sub_total).toFixed(<%= precision.to_i %>));
@@ -829,18 +778,6 @@ function calculate_item_discount(type, amount, sale_items, account_types){
// Check sale items exists
if(sale_items.length > 0){
for(var i=0;i < sale_items.length;i++){
// if(account_types.length > 0){
// for(var j=0; j < account_types.length; j++){
// if(sale_items[i].account_id == account_types[j].id){
// // Discount Items
// dis_amount = 0 - ((sale_items[i].price * amount)/100);
// var discount_item_row = item_row_template(type,sale_items[i], dis_amount, amount);
// $("#order-items-table tbody").append(discount_item_row);
// total_discount = total_discount + dis_amount;
// }
// }
// }
// else {
dis_amount = -Math.round(sale_items[i].price * amount / 100 * Math.pow(10, precision)) / Math.pow(10, precision);
var discount_item_row = item_row_template(type,sale_items[i], dis_amount, amount);
@@ -851,35 +788,12 @@ function calculate_item_discount(type, amount, sale_items, account_types){
total_discount = total_discount - dis_amount;
$("#discount-amountErr").html("Discount is greater than sub total!");
}
// }
}
sub_total = parseFloat(origin_sub_total) + parseFloat(total_discount);
totalAmount = sub_total
$("#order-sub-total").text(parseFloat(sub_total).toFixed(<%= precision.to_i %>));
}
}
// No Needs For Auto Selected
// Check account types exists
// if(account_types.length > 0){
// var item_rows=get_item_rows();
// console.log(account_types);
// if(item_rows.length > 0){
// for(var k=0; k < item_rows.length; k++){
// for(var j=0; j < account_types.length; j++){
// if(item_rows[k].account_id == account_types[j].id){
// // Discount Items
// dis_amount = 0 - ((item_rows[k].price * amount)/100);
// var discount_item_row = item_row_template(type, item_rows[k], dis_amount, amount);
// $("#order-items-table tbody").append(discount_item_row);
// total_discount = total_discount + dis_amount;
// }
// }
// }
// }
// else {
// alert("No Items!");
// }
// }
}
}else{