diff --git a/README.md b/README.md index bdc4c76b..bdaec79e 100755 --- a/README.md +++ b/README.md @@ -251,6 +251,9 @@ For Dashboard Settings for supervisor and cashier For Customer Settings On/Off 1) settings/lookups => {type:customer_settings, name:create, value: {1 or 0}} +For TaxProfiles On/Off + 1) settings/lookups => {type:changable_tax, name:change, value: {1 or 0}} + * ToDo list 1. Migration diff --git a/app/controllers/origami/home_controller.rb b/app/controllers/origami/home_controller.rb index 46c7e236..44b3e1e3 100755 --- a/app/controllers/origami/home_controller.rb +++ b/app/controllers/origami/home_controller.rb @@ -130,6 +130,19 @@ class Origami::HomeController < BaseOrigamiController end end end + + #for changable on/off + @changable_tax = true + lookup_changable_tax = Lookup.collection_of('changable_tax') + if !lookup_changable_tax.empty? + lookup_changable_tax.each do |changable_tax| + if changable_tax[0].downcase == "change" + if changable_tax[1] == '0' + @changable_tax = false + end + end + end + end end def check_emp_access_code diff --git a/app/controllers/origami/payments_controller.rb b/app/controllers/origami/payments_controller.rb index 235b5073..4db5e225 100755 --- a/app/controllers/origami/payments_controller.rb +++ b/app/controllers/origami/payments_controller.rb @@ -271,6 +271,20 @@ class Origami::PaymentsController < BaseOrigamiController if !@lookup_pdf.nil? @pdf_view = @lookup_pdf.value end + + #for changable on/off + @changable_tax = true + lookup_changable_tax = Lookup.collection_of('changable_tax') + if !lookup_changable_tax.empty? + lookup_changable_tax.each do |changable_tax| + if changable_tax[0].downcase == "change" + if changable_tax[1] == '0' + @changable_tax = false + end + end + end + end + @shop = shop_detail #show shop info @customer_lists = Customer.where("customer_id = 'CUS-000000000001' or customer_id = 'CUS-000000000002'") diff --git a/app/controllers/origami/request_bills_controller.rb b/app/controllers/origami/request_bills_controller.rb index 521b1eaf..961fc855 100755 --- a/app/controllers/origami/request_bills_controller.rb +++ b/app/controllers/origami/request_bills_controller.rb @@ -12,103 +12,81 @@ class Origami::RequestBillsController < ApplicationController sale_order=SaleOrder.new if !ShiftSale.current_shift.nil? - tax_data = TaxProfile.unscoped.where("group_type='cashier'").pluck(:id) - customer = Customer.find('CUS-000000000001') - - puts "data => " - puts tax_data - puts customer.tax_profiles - arr_data = [] - tax_data.each do |value1| - puts JSON.stringify(customer.tax_profiles) - if customer.include? value1 - arr_data << value1 - end + order_id = params[:id] # order_id + bk_order = BookingOrder.find_by_order_id(order_id) + order = Order.find(order_id) + check_booking = Booking.find_by_booking_id(bk_order.booking_id) + if check_booking.dining_facility_id.to_i > 0 + table = DiningFacility.find(check_booking.dining_facility_id) + else + table = nil end - puts "unique => " - puts arr_data - # tax_profiles = TaxProfile.unscoped.where("id in ?",arr_data) - # puts tax_profiles.to_json - # order_id = params[:id] # order_id - # bk_order = BookingOrder.find_by_order_id(order_id) - # order = Order.find(order_id) - # check_booking = Booking.find_by_booking_id(bk_order.booking_id) - # if check_booking.dining_facility_id.to_i > 0 - # table = DiningFacility.find(check_booking.dining_facility_id) - # else - # table = nil - # end - # if check_booking.sale_id.nil? - # # Create Sale if it doesn't exist - # @status, @sale_id = @sale.generate_invoice_from_booking(check_booking.id,current_login_employee, current_user, order.source) - # @sale_data = Sale.find_by_sale_id(@sale_id) - # @sale_items = SaleItem.where("sale_id=?",@sale_id) + if check_booking.sale_id.nil? + # Create Sale if it doesn't exist + @status, @sale_id = @sale.generate_invoice_from_booking(check_booking.id,current_login_employee, current_user, order.source) + @sale_data = Sale.find_by_sale_id(@sale_id) + @sale_items = SaleItem.where("sale_id=?",@sale_id) - # # in-duty update - # in_duties = InDuty.where("booking_id=?",bk_order.booking_id) - # if !in_duties.empty? - # in_duties.each do |in_duty| - # induty = InDuty.find(in_duty.id) - # induty.sale_id = @sale_id - # induty.save - # end - # end - # else - # @sale_data = Sale.find_by_sale_id(check_booking.sale_id) - # @sale_items = SaleItem.where("sale_id=?",@sale_data.sale_id) - # end + # in-duty update + in_duties = InDuty.where("booking_id=?",bk_order.booking_id) + if !in_duties.empty? + in_duties.each do |in_duty| + induty = InDuty.find(in_duty.id) + induty.sale_id = @sale_id + induty.save + end + end + else + @sale_data = Sale.find_by_sale_id(check_booking.sale_id) + @sale_items = SaleItem.where("sale_id=?",@sale_data.sale_id) + end - # # Bind shift sale id to sale - # # @sale_data.shift_sale_id = shift.id - # # @sale_data.save + # Bind shift sale id to sale + # @sale_data.shift_sale_id = shift.id + # @sale_data.save - # action_by = current_user.name - # type = "REQUEST_BILL" + action_by = current_user.name + type = "REQUEST_BILL" - # remark = "Request bill Receipt No #{@sale_data.receipt_no}" - # sale_audit = SaleAudit.record_audit_sale(@sale_data.sale_id,remark,action_by,type ) + remark = "Request bill Receipt No #{@sale_data.receipt_no}" + sale_audit = SaleAudit.record_audit_sale(@sale_data.sale_id,remark,action_by,type ) - # # Promotion Activation - # Promotion.promo_activate(@sale) + # Promotion Activation + Promotion.promo_activate(@sale) - # #bill channel - # if ENV["SERVER_MODE"] == 'cloud' - # from = request.subdomain + "." + request.domain - # else - # from = "" - # end + #bill channel + if ENV["SERVER_MODE"] == 'cloud' + from = request.subdomain + "." + request.domain + else + from = "" + end - # ActionCable.server.broadcast "bill_channel",table: table, from: from - # if order.source == "quick_service" - # result = {:status=> @status, :data => @sale.sale_id } - # render :json => result.to_json - # else + ActionCable.server.broadcast "bill_channel",table: table, from: from + if order.source == "quick_service" + result = {:status=> @status, :data => @sale.sale_id } + render :json => result.to_json + else - # if ENV["SERVER_MODE"] != "cloud" #no print in cloud server + if ENV["SERVER_MODE"] != "cloud" #no print in cloud server - # printer = PrintSetting.find_by_unique_code("CheckInOutPdf") - # unique_code = "CheckInOutPdf" - # booking = Booking.find_by_booking_id(bk_order.booking_id) + printer = PrintSetting.find_by_unique_code("CheckInOutPdf") + unique_code = "CheckInOutPdf" + booking = Booking.find_by_booking_id(bk_order.booking_id) - # # print when complete click - # order_queue_printer = Printer::OrderQueuePrinter.new(printer) + # print when complete click + order_queue_printer = Printer::OrderQueuePrinter.new(printer) - # if !printer.nil? - # order_queue_printer.print_check_in_out(printer, booking, table) - # end - # end - # end + if !printer.nil? + order_queue_printer.print_check_in_out(printer, booking, table) + end + end + end else @status = false @error_message = "No Current Open Shift for This Employee" end - def difference(other) - h = other.each_with_object(Hash.new(0)) { |e,h| h[e] += 1 } - reject { |e| h[e] > 0 && h[e] -= 1 } - end - # Not Use for these printed bill cannot give customer # unique_code = "ReceiptBillPdf" # #shop detail diff --git a/app/controllers/origami/rooms_controller.rb b/app/controllers/origami/rooms_controller.rb index c833c7dd..abdd8842 100755 --- a/app/controllers/origami/rooms_controller.rb +++ b/app/controllers/origami/rooms_controller.rb @@ -154,6 +154,19 @@ class Origami::RoomsController < BaseOrigamiController end end end + + #for changable on/off + @changable_tax = true + lookup_changable_tax = Lookup.collection_of('changable_tax') + if !lookup_changable_tax.empty? + lookup_changable_tax.each do |changable_tax| + if changable_tax[0].downcase == "change" + if changable_tax[1] == '0' + @changable_tax = false + end + end + end + end end end diff --git a/app/controllers/origami/sales_controller.rb b/app/controllers/origami/sales_controller.rb index 876a8ed3..b9cc40ec 100755 --- a/app/controllers/origami/sales_controller.rb +++ b/app/controllers/origami/sales_controller.rb @@ -25,6 +25,7 @@ class Origami::SalesController < BaseOrigamiController def add_to_existing_invoice dining = params[:dining_id] sale_id = params[:sale_id] + tax_type = params[:tax_type] table = DiningFacility.find(dining) existing_booking = Booking.find_by_sale_id(sale_id) table.bookings.each do |booking| @@ -51,7 +52,7 @@ class Origami::SalesController < BaseOrigamiController end # Re-compute for add - saleobj.compute(order.source) + saleobj.compute(order.source,tax_type) saleobj.save order.save booking.save diff --git a/app/models/sale.rb b/app/models/sale.rb index e353f523..779e5f91 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -322,7 +322,7 @@ class Sale < ApplicationRecord end #compute - invoice total - def compute(order_source = nil) + def compute(order_source = nil, tax_type = nil) sales_items = self.sale_items #Computation Fields @@ -341,7 +341,7 @@ class Sale < ApplicationRecord # total_taxable = total_taxable + (item.taxable_price * item.qty) end - apply_tax(total_taxable, order_source) + apply_tax(total_taxable, order_source, tax_type) self.total_amount = subtotal_price self.total_discount = total_discount @@ -434,8 +434,21 @@ class Sale < ApplicationRecord total_tax_amount = 0 tax_incl_exec = "exclusive" #tax_profile - list by order_by - tax_profiles = TaxProfile.all.order("order_by asc") - customer = Customer.find(sale.customer_id) + # tax_profiles = TaxProfile.all.order("order_by asc") + # customer = Customer.find(sale.customer_id) + arr_tax = [] + arr_tax = unique_tax_profiles(order_source, self.customer_id) + + if !arr_tax.empty? + if tax_type.nil? + tax_profiles = TaxProfile.unscoped.where(:id => arr_tax) + else + tax_profiles = TaxProfile.unscoped.where("group_type=?",order_source) + end + else + tax_profiles = TaxProfile.unscoped.where("group_type=?",order_source) + end + # #Creat new tax records if order_source.to_s == "emenu" order_source = "cashier" @@ -505,7 +518,7 @@ class Sale < ApplicationRecord end # Tax Calculate - def apply_tax(total_taxable, order_source = nil) + def apply_tax(total_taxable, order_source = nil, tax_type = nil) shop = Shop.first #if tax is not apply create new record @@ -522,29 +535,32 @@ class Sale < ApplicationRecord order_source = "cashier" end - tax_data = TaxProfile.unscoped.where("group_type=?",order_source).pluck(:id) - customer = Customer.find(self.customer_id).tax_profiles + # tax_data = TaxProfile.unscoped.where("group_type=?",order_source).pluck(:id) + # customer = Customer.find(self.customer_id).tax_profiles arr_tax = [] - - arr_tax = customer - tax_data + arr_tax = unique_tax_profiles(order_source, self.customer_id) if !arr_tax.empty? - tax_profiles = TaxProfile.unscoped.where("id in ?",arr_tax) + if tax_type.nil? + tax_profiles = TaxProfile.unscoped.where(:id => arr_tax) + else + tax_profiles = TaxProfile.unscoped.where("group_type=?",order_source) + end else tax_profiles = TaxProfile.unscoped.where("group_type=?",order_source) end #Create new tax records tax_profiles.each do |tax| if tax.group_type.to_s == order_source.to_s - # customer.tax_profiles.each do |cus_tax| - # if cus_tax.to_i == tax.id + if tax_type + if tax_type.to_s == tax.name.to_s || tax_type == 'all' sale_tax = SaleTax.new(:sale => self) sale_tax.tax_name = tax.name sale_tax.tax_rate = tax.rate # substract , to give after discount - total_tax = total_taxable - self.total_discount + total_tax = total_taxable - total_discount #include or execulive if tax.inclusive tax_incl_exec = "inclusive" @@ -555,16 +571,43 @@ class Sale < ApplicationRecord sale_tax.tax_payable_amount = total_tax * tax.rate / 100 total_tax_amount = total_tax_amount + sale_tax.tax_payable_amount end - #new taxable amount is standard rule for step by step if shop.calc_tax_order total_taxable = total_taxable + sale_tax.tax_payable_amount end - sale_tax.inclusive = tax.inclusive sale_tax.save - # end - # end + end + else + # customer.tax_profiles.each do |cus_tax| + # if cus_tax.to_i == tax.id + sale_tax = SaleTax.new(:sale => self) + sale_tax.tax_name = tax.name + sale_tax.tax_rate = tax.rate + + # substract , to give after discount + total_tax = total_taxable - self.total_discount + #include or execulive + if tax.inclusive + tax_incl_exec = "inclusive" + rate = tax.rate + divided_value = (100 + rate)/rate + sale_tax.tax_payable_amount = total_tax / divided_value + else + sale_tax.tax_payable_amount = total_tax * tax.rate / 100 + total_tax_amount = total_tax_amount + sale_tax.tax_payable_amount + end + + #new taxable amount is standard rule for step by step + if shop.calc_tax_order + total_taxable = total_taxable + sale_tax.tax_payable_amount + end + + sale_tax.inclusive = tax.inclusive + sale_tax.save + # end + # end + end end end self.tax_type = tax_incl_exec @@ -2844,9 +2887,21 @@ def self.get_sale_data_for_other_payment_credit(sale_id) return query end -def difference(other) - h = other.each_with_object(Hash.new(0)) { |e,h| h[e] += 1 } - reject { |e| h[e] > 0 && h[e] -= 1 } +def unique_tax_profiles(order_source, customer_id) + tax_data = TaxProfile.unscoped.where("group_type='#{order_source}'").pluck(:id) + customer_tax_profiles = Customer.find(customer_id).tax_profiles + + arr_data = [] + if !customer_tax_profiles.empty? + customer_tax_profiles.each do |value1| + if tax_data.include? value1.to_i + arr_data.push(value1.to_i) + end + end + return arr_data + else + return tax_data + end end private diff --git a/app/views/origami/home/show.html.erb b/app/views/origami/home/show.html.erb index eed3d8ce..aef82ecf 100755 --- a/app/views/origami/home/show.html.erb +++ b/app/views/origami/home/show.html.erb @@ -370,7 +370,7 @@ <% else %> No Tax <% end %>
- <%if !@webview %> + <%if !@webview && @changable_tax %> <% end %> @@ -1103,10 +1103,11 @@ var dining_id = "<%= @dining.id %>"; var sale_id = $("#sale_id").val(); //<%= @obj_sale.sale_id rescue "" %> var ajax_url = "/origami/sale/append_order"; + var tax_type = localStorage.getItem("tax_type"); $.ajax({ type: "POST", url: ajax_url, - data: 'dining_id=' + dining_id + "&sale_id=" + sale_id, + data: 'dining_id=' + dining_id + "&sale_id=" + sale_id + "&tax_type=" + tax_type, success: function (result) { swal({ title: "Information!", diff --git a/app/views/origami/payments/show.html.erb b/app/views/origami/payments/show.html.erb index 195719bd..245467f9 100755 --- a/app/views/origami/payments/show.html.erb +++ b/app/views/origami/payments/show.html.erb @@ -118,7 +118,7 @@ <% else %> No Tax <% end %>
- <%if @sale_payment.nil? %> + <%if @sale_payment.nil? && @changable_tax %> <% end %> diff --git a/app/views/origami/rooms/show.html.erb b/app/views/origami/rooms/show.html.erb index 155e1e07..59546349 100755 --- a/app/views/origami/rooms/show.html.erb +++ b/app/views/origami/rooms/show.html.erb @@ -404,7 +404,7 @@ <% else %> No Tax <% end %>
- <%if !@webview %> + <%if !@webview && @changable_tax %> <% end %> @@ -1167,10 +1167,11 @@ $('#add_invoice').on('click',function(){ var dining_id = "<%= @room.id %>" var sale_id = $("#sale_id").val(); //<%= @obj_sale.sale_id rescue "" %> var ajax_url = "/origami/sale/append_order"; + var tax_type = localStorage.getItem("tax_type"); $.ajax({ type: "POST", url: ajax_url, - data: 'dining_id='+ dining_id + "&sale_id=" + sale_id, + data: 'dining_id='+ dining_id + "&sale_id=" + sale_id + "&tax_type=" + tax_type, success:function(result){ swal({ title: "Information!",