rounding decimal to print_settings precision

This commit is contained in:
Thein Lin Kyaw
2019-11-06 17:34:42 +06:30
parent 33670b450e
commit 155cb0a9eb
11 changed files with 467 additions and 802 deletions

View File

@@ -34,7 +34,7 @@
<span class="hidden" id ="table_id" value="<%=@table%>"><%=@table%></span>
<span class="hidden" id="table_type" value="<%=@table%>"><%=@table%></span>
<%end%>
<p id="sale-id" class="hidden"><%=@sale_data.sale_id %></p>
<p class="m-l-10">Receipt No: <span id="receipt_no"><%=@sale_data.receipt_no rescue ' '%></span></p>
<!-- <p>Cashier: <span id="cashier"><%=@sale_data.cashier_name rescue ' '%></span></p> -->
@@ -57,8 +57,8 @@
<tbody>
<% sub_total = 0 %>
<% @sale_data.sale_items.each do |sale_item| %>
<%
sub_total += sale_item.qty*sale_item.unit_price
<%
sub_total += sale_item.qty*sale_item.unit_price
if sale_item.price != 0 && sale_item.status != "void" && sale_item.status != "foc"
%>
<tr class="item-row" id=<%= sale_item.sale_item_id %> >
@@ -76,7 +76,7 @@
</tr>
<%
end
end
end
%>
</tbody>
</table>
@@ -130,18 +130,18 @@
<div class="card-block">
<div class="card-title">
<div class="form-horizontal">
<div class="col-md-12">
<div class="form-group">
<div class="col-md-12">
<div class="form-group">
<input type="text" id="discount-amount" name="discount-amount" value="<%= @sale_data.total_discount rescue 0 %>" class="form-control" />
<span id="discount-amountErr" style="color:red;"></span>
</div>
<br>
<div class="form-group">
<div class="form-group">
<% @accounts.each do |acc| %>
<button id="account-<%= acc.id %>" class="accounts-type btn bg-default"><%= acc.title %></button>
<% end %>
</div>
</div>
</div>
</div>
</div>
@@ -217,11 +217,11 @@
<div class="col-md-4">
<div class="form-group">
<button id="net" class="btn bg-info fluid action-btn">Nett</button>
<button id="percentage" class="btn bg-primary fluid action-btn" style="font-size: 12.4px">Percentage</button>
<button id="percentage" class="btn bg-primary fluid action-btn" style="font-size: 12.4px">Percentage</button>
<button id="remove-item" class="btn bg-default fluid action-btn">Clear Discount</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
@@ -235,7 +235,7 @@
<div class="form-horizontal">
<div class="p-l-20">
<div class="row">
<div class="col-md-8 form-group">
<div class="col-md-8 form-group">
<div class="form-group">
<input type="text" id="promo-code" name="promo-code" value="" class="form-control" /></span>
</div>
@@ -243,11 +243,11 @@
<div class="col-md-4">
<button id="" class="btn bg-primary btn-block action-btn">Promo</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div> -->
</div>
<!-- Column Three -->
@@ -259,7 +259,7 @@
<button id="remove-all" class="btn bg-primary btn-block action-btn">Remove All</button>
<button id="pay-discount" class="btn bg-primary btn-block action-btn">Enter</button>
<% if @member_discount%>
<!-- <hr />
<!-- <hr />
<button id="member-discount" class="btn btn-success btn-block action-btn
<%= @sale_data.customer.membership_id ? " " : "disabled"%>">Member Discount</button> -->
<%end %>
@@ -300,10 +300,10 @@ var cashier_type = "<%= @cashier_type %>";
case 'num':
var input_value = $(this).attr("data-value");
if (original_value == "0.0"){
$('#discount-amount').val(input_value);
$('#discount-amount').val(input_value);
}
else{
$('#discount-amount').val(original_value + '' + input_value);
$('#discount-amount').val(original_value + '' + input_value);
}
break;
case 'add':
@@ -313,10 +313,10 @@ var cashier_type = "<%= @cashier_type %>";
break;
case 'del' :
var discount_text=$('#discount-amount').val();
$('#discount-amount').val(discount_text.substr(0,discount_text.length-1));
$('#discount-amount').val(discount_text.substr(0,discount_text.length-1));
break;
case 'clr':
$('#discount-amount').val("0.0");
$('#discount-amount').val("0.0");
break;
}
event.handled = true;
@@ -374,7 +374,7 @@ var cashier_type = "<%= @cashier_type %>";
}
}
});
}
}
});
// Calculate Net Discount for Payment
@@ -384,7 +384,7 @@ var cashier_type = "<%= @cashier_type %>";
var discount_value = $('#discount-amount').val();
var sub_total = parseFloat($('#order-sub-total').text());
var ajax_url = "/origami/" + sale_id + "/discount";
if(discount_value!=""){
if(discount_value > 0){
if(parseFloat(discount_value) > sub_total){
@@ -393,7 +393,7 @@ var cashier_type = "<%= @cashier_type %>";
}else{
$("#discount-amountErr").html("");
// Selected Items
var sale_items = get_selected_sale_items();
var sale_items = get_selected_sale_items();
// Selected Account
var account_types = get_selected_account_types();
@@ -402,8 +402,8 @@ var cashier_type = "<%= @cashier_type %>";
calculate_overall_discount(0, discount_value);
}
else {
calculate_item_discount(0, discount_value, sale_items, account_types);
}
calculate_item_discount(0, discount_value, sale_items, account_types);
}
// Remove Selection
selection_remove();
@@ -414,7 +414,7 @@ var cashier_type = "<%= @cashier_type %>";
}else{
$("#discount-amountErr").html("can't be blank");
}
});
// Calculate Percentage Discount for Payment
@@ -425,19 +425,19 @@ var cashier_type = "<%= @cashier_type %>";
var ajax_url = "/origami/" + sale_id + "/discount";
// Selected Items
var sale_items = get_selected_sale_items();
var sale_items = get_selected_sale_items();
// Selected Account
var account_types = get_selected_account_types();
if(sale_items.length == 0 && account_types.length == 0){
calculate_overall_discount(1, discount_value);
}
else {
calculate_item_discount(1, discount_value, sale_items, account_types);
calculate_item_discount(1, discount_value, sale_items, account_types);
}
// Remove Selection
selection_remove();
selection_remove();
});
// Remove selected discount items
@@ -447,8 +447,8 @@ var cashier_type = "<%= @cashier_type %>";
var total = 0;
$('.discount-item-row.selected-item').each(function(i){
var amount = parseFloat($(this).find('#item-total-price').text());
total = total + Math.abs(amount);
var amount = parseFloat($(this).find('#item-total-price').text());
total = total + Math.abs(amount);
$(this).remove();
});
$("#order-sub-total").text(origin_sub_total + total);
@@ -464,8 +464,8 @@ var cashier_type = "<%= @cashier_type %>";
var sale_id = $('#sale-id').text();
var discount_items = JSON.stringify(get_discount_item_rows());
var overall_discount = $("#order-discount").text();
var sub_total = $('#order-sub-total').text();
var ajax_url = "/origami/" + sale_id + "/discount";
var sub_total = $('#order-sub-total').text();
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 };
@@ -473,8 +473,8 @@ var cashier_type = "<%= @cashier_type %>";
type: "POST",
url: ajax_url,
data: params,
success:function(result){
$("#loading_wrapper").hide();
success:function(result){
$("#loading_wrapper").hide();
swal({
title: "Information!",
text: result.status,
@@ -489,7 +489,7 @@ var cashier_type = "<%= @cashier_type %>";
window.location.href = "/origami/room/" + result.table_id
}
}
});
});
}
});
});
@@ -500,12 +500,12 @@ var cashier_type = "<%= @cashier_type %>";
var sale_id = $('#sale-id').text();
var discount_items = [];
// Selected Items
var sale_items = get_selected_sale_items();
// Selected Items
var sale_items = get_selected_sale_items();
if(sale_items.length == 0){
//swal("Information!", "You have no selected item!");
swal ( "Oops" , "You have no selected item!" , "error" );
return;
swal ( "Oops" , "You have no selected item!" , "error" );
return;
}
for(var i=0;i < sale_items.length;i++){
@@ -514,7 +514,7 @@ var cashier_type = "<%= @cashier_type %>";
}
else {
swal ("Oops" , "You have no selected item!" , "error" );
return;
return;
}
}
@@ -539,7 +539,7 @@ var cashier_type = "<%= @cashier_type %>";
window.location.href = "/origami/room/" + result.table_id
}
}
});
});
}
});
});
@@ -566,7 +566,7 @@ var cashier_type = "<%= @cashier_type %>";
window.location.href = "/origami/room/" + result.table_id
}
}
});
});
}
});
});
@@ -575,8 +575,8 @@ var cashier_type = "<%= @cashier_type %>";
$("#member-discount").on('click', function(e){
e.preventDefault();
var sale_id = $('#sale-id').text();
var sub_total = $('#order-sub-total').text();
var ajax_url = "/origami/" + sale_id + "/member_discount";
var sub_total = $('#order-sub-total').text();
var ajax_url = "/origami/" + sale_id + "/member_discount";
// Selected Account
//var account_types = JSON.stringify(get_selected_account_types());
@@ -618,7 +618,7 @@ var cashier_type = "<%= @cashier_type %>";
});
}
});
});//end member discount
});
@@ -631,7 +631,7 @@ var cashier_type = "<%= @cashier_type %>";
/* Get Item rows but not discount*/
function get_item_rows(){
var sale_items = [];
var sale_items = [];
$('.item-row').not('.discount-item-row').each(function(i){
var sale_item = {};
sale_item.id = $(this).attr('id').substr(0,16);
@@ -639,13 +639,13 @@ var cashier_type = "<%= @cashier_type %>";
sale_item.account_id = $(this).find('#item-account-type').text();
sale_item.price = $(this).find('#item-total-price').text();
sale_items.push(sale_item);
});
});
return sale_items;
}
/* Get discount Item rows */
function get_discount_item_rows(){
var sale_items = [];
var sale_items = [];
$('.discount-item-row').each(function(i){
var sale_item = {};
sale_item.id = $(this).attr('id');
@@ -653,13 +653,13 @@ var cashier_type = "<%= @cashier_type %>";
sale_item.account_id = $(this).find('#item_account_type').text();
sale_item.price = $(this).find('#item-total-price').text();
sale_items.push(sale_item);
});
});
return sale_items;
}
/* Get discount Item rows */
// function get_selected_discount_items(){
// var sale_items = [];
// var sale_items = [];
// $('.discount-item-row').hasClass('.selected-item').each(function(i){
// var sale_item = {};
// sale_item.id = $(this).attr('id');
@@ -667,13 +667,13 @@ var cashier_type = "<%= @cashier_type %>";
// sale_item.account_id = $(this).find('#item_account_type').text();
// sale_item.price = $(this).find('#item-total-price').text();
// sale_items.push(sale_item);
// });
// });
// return sale_items;
// }
/* Get Selected Sale Item's ID and Price */
function get_selected_sale_items(){
var sale_items = [];
var sale_items = [];
$('.item-row.selected-item').each(function(i){
var sale_item = {};
sale_item.id = $(this).attr('id').substr(0,16);
@@ -681,33 +681,33 @@ function get_selected_sale_items(){
sale_item.account_id = $(this).find('#item-account-type').text();
sale_item.price = $(this).find('#item-total-price').text();
sale_items.push(sale_item);
});
});
return sale_items;
}
/* Get Selected Accounts ID and Price */
function get_selected_account_types(){
function get_selected_account_types(){
var account_types = [];
$('.selected-account').each(function(i){
var account= {};
account.id = $(this).attr('id').substr(8);
account.name = $(this).text();
account.name = $(this).text();
account_types.push(account);
});
});
return account_types;
}
/* Calculate Overall Discount*/
function calculate_overall_discount(type, amount){
var origin_sub_total = parseFloat($("#order-sub-total").text());
var origin_sub_total = parseFloat($("#order-sub-total").text());
var dis_amount = 0;
var sub_total = 0;
var total_discount = 0;
// For Net Pay
if(type == 0){
total_discount = amount;
total_discount = Math.round(amount * Math.pow(10, precision)) / Math.pow(10, precision);
}
// For Percentage Pay
@@ -719,10 +719,10 @@ function calculate_overall_discount(type, amount){
type: "error",
confirmButtonText: 'OK',
confirmButtonColor:"red"
});
});
}
else{
total_discount = (origin_sub_total * amount)/100;
total_discount = Math.round(origin_sub_total * amount / 100 * Math.pow(10, precision)) / Math.pow(10, precision);
}
}
@@ -731,22 +731,23 @@ function calculate_overall_discount(type, amount){
/* 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 = parseFloat($("#order-sub-total").text());
var dis_amount = 0;
var sub_total = 0;
var total_discount = 0;
var arrItemName = "";
var precision = <%= precision %>;
$("#discount_itemsErr").html("");
$("#discount-amountErr").html("");
// For Net Pay
if(origin_sub_total > 0){
if(type == 0){
dis_amount = (0 - amount);
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){
// 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);
@@ -768,7 +769,7 @@ function calculate_item_discount(type, amount, sale_items, account_types){
$("#discount-amountErr").html("Discount is greater than sub total!");
}
}
// }
}
}
@@ -782,11 +783,11 @@ function calculate_item_discount(type, amount, sale_items, account_types){
}else{
$("#discount_itemsErr").html("Discount is greater than "+arrItemName+" price");
}
}
// No Needs For Auto Selected
// if(account_types.length > 0){
// 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++){
@@ -802,7 +803,7 @@ function calculate_item_discount(type, amount, sale_items, account_types){
// }
// else {
// alert("No Items!");
// }
// }
// }
sub_total = parseFloat(origin_sub_total) - parseFloat(total_discount);
@@ -817,7 +818,7 @@ function calculate_item_discount(type, amount, sale_items, account_types){
type: "error",
confirmButtonText: 'OK',
confirmButtonColor:"red"
});
});
}
else{
// Check sale items exists
@@ -825,7 +826,7 @@ function calculate_item_discount(type, amount, sale_items, account_types){
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){
// 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);
@@ -835,9 +836,9 @@ function calculate_item_discount(type, amount, sale_items, account_types){
// }
// }
// else {
dis_amount = 0 - ((sale_items[i].price * amount)/100);
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);
total_discount = total_discount + dis_amount;
if(parseFloat(origin_sub_total) >= parseFloat(total_discount)){
$("#order-items-table tbody").append(discount_item_row);
@@ -852,14 +853,14 @@ function calculate_item_discount(type, amount, sale_items, account_types){
}
// No Needs For Auto Selected
// Check account types exists
// if(account_types.length > 0){
// 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
// // 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);
@@ -870,15 +871,15 @@ function calculate_item_discount(type, amount, sale_items, account_types){
// }
// else {
// alert("No Items!");
// }
// }
// }
// }
}
$("#order-sub-total").text(parseFloat(sub_total).toFixed(<%= precision.to_i %>));
}else{
$("#discount-amountErr").html("Discount is greater than sub total!");
}
}
function item_row_template(type, item, discount_amount, amount){
@@ -887,24 +888,24 @@ function item_row_template(type, item, discount_amount, amount){
dis_str = dis_str + "(" + amount + "%)"
}
var discount_item_row = "<tr class='discount-item-row' id='" + item.id + "'>" +
"<td style='width: 60%; text-align: left;'>" +
"<span id='item_account_type' class='hidden'>" +
"<td style='width: 60%; text-align: left;'>" +
"<span id='item_account_type' class='hidden'>" +
item.account_id +
"</span>" +
"<span id='item-name-price'>" +
"</span>" +
"<span id='item-name-price'>" +
item.name + dis_str +
"</span>" +
"</span>" +
"</td>" +
"<td style='width: 20%; text-align: right;'>" +
"<td style='width: 20%; text-align: right;'>" +
"<span id='item-qty'>1</span>" +
"</td>" +
"<td style='width: 20%; text-align: right;'>" +
"<span id='item-total-price'>" +
"<td style='width: 20%; text-align: right;'>" +
"<span id='item-total-price'>" +
discount_amount +
"</span>" +
"</span>" +
"</td>" +
"</tr>";
return discount_item_row;
return discount_item_row;
}
</script>