check discount calculation

This commit is contained in:
phyusin
2018-02-28 13:55:24 +06:30
parent a17a240a2e
commit 32340c52e6

View File

@@ -31,6 +31,7 @@
</div>
<div id="order-detail-slimscroll" data-height="160">
<div class="card-text dining">
<span id="discount_itemsErr" style="color:red;"></span>
<table class="table table-default" id="order-items-table">
<thead>
<tr>
@@ -117,6 +118,7 @@
<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">
@@ -254,12 +256,9 @@ var cashier_type = "<%= @cashier_type %>";
var original_value=0;
original_value = $('#discount-amount').val();
var input_type = $(this).attr("data-type");
var grand_total = $('#order-grand-total').text();
console.log(grand_total);
switch (input_type) {
case 'num':
var input_value = $(this).attr("data-value");
console.log(input_value);
if (original_value == "0.0"){
$('#discount-amount').val(input_value);
}
@@ -332,9 +331,17 @@ var cashier_type = "<%= @cashier_type %>";
$("#net").on('click', function(e){
e.preventDefault();
var sale_id = $('#sale-id').text();
var discount_value = parseFloat($('#discount-amount').val());
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){
$("#discount-amount").val("");
$("#discount-amountErr").html("Discount is greater than sub total!");
}else{
$("#discount-amountErr").html("");
// Selected Items
var sale_items = get_selected_sale_items();
@@ -350,6 +357,14 @@ var cashier_type = "<%= @cashier_type %>";
// Remove Selection
selection_remove();
}
}else{
$("#discount-amountErr").html("Discount must be greater than 0!");
}
}else{
$("#discount-amountErr").html("can't be blank");
}
});
// Calculate Percentage Discount for Payment
@@ -361,7 +376,6 @@ var cashier_type = "<%= @cashier_type %>";
// Selected Items
var sale_items = get_selected_sale_items();
// Selected Account
var account_types = get_selected_account_types();
@@ -435,7 +449,6 @@ var cashier_type = "<%= @cashier_type %>";
// Selected Items
var sale_items = get_selected_sale_items();
console.log(sale_items.length);
if(sale_items.length == 0){
//swal("Information!", "You have no selected item!");
swal ( "Oops" , "You have no selected item!" , "error" );
@@ -669,7 +682,11 @@ function calculate_item_discount(type, amount, sale_items, account_types){
var dis_amount = 0;
var sub_total = 0;
var total_discount = 0;
var arrItemName = "";
$("#discount_itemsErr").html("");
$("#discount-amountErr").html("");
// For Net Pay
if(origin_sub_total > 0){
if(type == 0){
dis_amount = (0 - amount);
if(sale_items.length > 0){
@@ -685,13 +702,36 @@ function calculate_item_discount(type, amount, sale_items, account_types){
// }
// }
// else {
if(parseFloat(amount) > parseFloat(sale_items[i].price)){
arrItemName += ", " + sale_items[i].name;
}else{
var discount_item_row = item_row_template(type,sale_items[i], dis_amount, amount);
total_discount = parseFloat(total_discount) + parseFloat(amount);
if(parseFloat(origin_sub_total) >= parseFloat(total_discount)){
$("#order-items-table tbody").append(discount_item_row);
total_discount = total_discount + amount;
}else{
total_discount = parseFloat(total_discount) - parseFloat(amount);
$("#discount-amountErr").html("Discount is greater than sub total!");
}
}
// }
}
}
if(arrItemName!=""){
arrItemName = arrItemName.substr(2);
if(arrItemName.match(/,/g || []) != null){
if(arrItemName.match(/,/g || []).length >= 1){
$("#discount_itemsErr").html("Discount is greater than "+arrItemName+" prices");
}
}else{
$("#discount_itemsErr").html("Discount is greater than "+arrItemName+" price");
}
}
// No Needs For Auto Selected
// if(account_types.length > 0){
// var item_rows=get_item_rows();
@@ -712,7 +752,7 @@ function calculate_item_discount(type, amount, sale_items, account_types){
// }
// }
sub_total = origin_sub_total - total_discount;
sub_total = parseFloat(origin_sub_total) - parseFloat(total_discount);
}
// For Percentage Pay
@@ -744,11 +784,17 @@ function calculate_item_discount(type, amount, sale_items, account_types){
// else {
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;
if(parseFloat(origin_sub_total) >= parseFloat(total_discount)){
$("#order-items-table tbody").append(discount_item_row);
}else{
total_discount = total_discount - dis_amount;
$("#discount-amountErr").html("Discount is greater than sub total!");
}
// }
}
sub_total = origin_sub_total + total_discount;
sub_total = parseFloat(origin_sub_total) + parseFloat(total_discount);
}
}
// No Needs For Auto Selected
@@ -775,7 +821,11 @@ function calculate_item_discount(type, amount, sale_items, account_types){
// }
}
$("#order-sub-total").text(sub_total);
$("#order-sub-total").text(parseFloat(sub_total).toFixed(2));
}else{
$("#discount-amountErr").html("Discount is greater than sub total!");
}
}
function item_row_template(type, item, discount_amount, amount){