+
@@ -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,24 +331,40 @@ 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();
- // Selected Items
- var sale_items = get_selected_sale_items();
+ // Selected Account
+ var account_types = get_selected_account_types();
- // Selected Account
- var account_types = get_selected_account_types();
+ if(sale_items.length == 0 && account_types.length == 0){
+ calculate_overall_discount(0, discount_value);
+ }
+ else {
+ calculate_item_discount(0, discount_value, sale_items, account_types);
+ }
- if(sale_items.length == 0 && account_types.length == 0){
- calculate_overall_discount(0, discount_value);
+ // Remove Selection
+ selection_remove();
+ }
+ }else{
+ $("#discount-amountErr").html("Discount must be greater than 0!");
+ }
+ }else{
+ $("#discount-amountErr").html("can't be blank");
}
- else {
- calculate_item_discount(0, discount_value, sale_items, account_types);
- }
-
- // Remove Selection
- selection_remove();
+
});
// Calculate Percentage Discount for Payment
@@ -360,8 +375,7 @@ 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();
@@ -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,113 +682,150 @@ 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(type == 0){
- dis_amount = (0 - amount);
- 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 {
- 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;
- // }
- }
- }
-
- // 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 = origin_sub_total - total_discount;
- }
-
- // For Percentage Pay
- if(type == 1){
- if(amount > 100 ){
- swal({
- title:"Oops!",
- text:'Percentage Value over 100!',
- type: "error",
- confirmButtonText: 'OK',
- confirmButtonColor:"red"
- });
- }
- else{
- // Check sale items exists
+ if(origin_sub_total > 0){
+ if(type == 0){
+ dis_amount = (0 - amount);
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);
+ // 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;
+ // total_discount = total_discount + amount;
// }
// }
// }
// 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(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);
+ }else{
+ total_discount = parseFloat(total_discount) - parseFloat(amount);
+ $("#discount-amountErr").html("Discount is greater than sub total!");
+ }
+ }
+
// }
}
- sub_total = origin_sub_total + total_discount;
}
+
+ 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();
+ // 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);
}
- // 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!");
- // }
- // }
+
+ // For Percentage Pay
+ if(type == 1){
+ if(amount > 100 ){
+ swal({
+ title:"Oops!",
+ text:'Percentage Value over 100!',
+ type: "error",
+ confirmButtonText: 'OK',
+ confirmButtonColor:"red"
+ });
+ }
+ else{
+ // 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 = 0 - ((sale_items[i].price * amount)/100);
+ 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);
+ }else{
+ total_discount = total_discount - dis_amount;
+ $("#discount-amountErr").html("Discount is greater than sub total!");
+ }
+ // }
+ }
+ sub_total = parseFloat(origin_sub_total) + parseFloat(total_discount);
+ }
+ }
+ // 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!");
+ // }
+ // }
+ }
+
+ $("#order-sub-total").text(parseFloat(sub_total).toFixed(2));
+ }else{
+ $("#discount-amountErr").html("Discount is greater than sub total!");
}
- $("#order-sub-total").text(sub_total);
}
function item_row_template(type, item, discount_amount, amount){