diff --git a/app/controllers/origami/sale_edit_controller.rb b/app/controllers/origami/sale_edit_controller.rb index 575cc008..70a5a380 100644 --- a/app/controllers/origami/sale_edit_controller.rb +++ b/app/controllers/origami/sale_edit_controller.rb @@ -51,6 +51,8 @@ class Origami::SaleEditController < BaseOrigamiController end def apply_void - + sale_id = params[:sale_id] + saleObj = Sale.find(sale_id) + saleObj.compute_without_void end end diff --git a/app/models/sale.rb b/app/models/sale.rb index f79d8642..791e9369 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -21,9 +21,9 @@ class Sale < ApplicationRecord "daily" => 0, "monthly" => 1, "yearly" => 2 - } + } - SALE_STATUS_COMPLETED = "completed" + SALE_STATUS_COMPLETED = "completed" def generate_invoice_from_booking(booking_id, requested_by) booking = Booking.find(booking_id) @@ -199,6 +199,31 @@ class Sale < ApplicationRecord end + def compute_without_void + sales_items = self.sale_items + + #Computation Fields + subtotal_price = 0 + total_taxable = 0 + rounding_adjustment = 0 + + sales_items.each do |item| + if item.remark != 'void' + #compute each item and added to total + subtotal_price = subtotal_price + item.price + total_taxable = total_taxable + item.taxable_price + end + end + + apply_tax (total_taxable) + self.total_amount = subtotal_price + self.total_discount = total_discount + self.grand_total = (self.total_amount - self.total_discount) + self.total_tax + #compute rounding adjustment + adjust_rounding + + self.save! + end # Tax Calculate def apply_tax(total_taxable) #if tax is not apply create new record @@ -354,28 +379,28 @@ def self.get_by_range_by_saleitems(from,to,status,report_type) mc.name as menu_category_name, mc.id as menu_category_id ") .group('mi.id') - .order("mi.menu_category_id") - - query = query.joins("JOIN sale_items i ON i.sale_id = sales.sale_id - JOIN menu_items mi ON i.product_code = mi.item_code" + - " JOIN menu_categories mc ON mc.id = mi.menu_category_id + .order("mi.menu_category_id") + + query = query.joins("JOIN sale_items i ON i.sale_id = sales.sale_id + JOIN menu_items mi ON i.product_code = mi.item_code" + + " JOIN menu_categories mc ON mc.id = mi.menu_category_id JOIN employees ea ON ea.id = sales.cashier_id") - + query = query.where("receipt_date between ? and ? and sale_status=?",from,to,status) - + case report_type.to_i when REPORT_TYPE["daily"] return query when REPORT_TYPE["monthly"] - - return query.group("MONTH(date)") + + return query.group("MONTH(date)") when REPORT_TYPE["yearly"] - return query.group("YEAR(date)") + return query.group("YEAR(date)") end - + end private diff --git a/app/views/origami/sale_edit/apply_void.json.jbuilder b/app/views/origami/sale_edit/apply_void.json.jbuilder new file mode 100644 index 00000000..08bf292c --- /dev/null +++ b/app/views/origami/sale_edit/apply_void.json.jbuilder @@ -0,0 +1 @@ +json.status true