From 8ee27a9c359e34fcf30c18681bcfa5014d0105c4 Mon Sep 17 00:00:00 2001 From: Zin Moe Date: Tue, 14 Jul 2020 17:56:45 +0630 Subject: [PATCH] add tax profiles api --- app/controllers/api/api_controller.rb | 4 ++++ app/models/tax_profile.rb | 18 ++++++++++++++++++ .../api/api/get_tax_profiles.json.jbuilder | 3 +++ .../api/restaurant/menu/index.json.jbuilder | 1 - config/routes.rb | 1 + 5 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 app/views/api/api/get_tax_profiles.json.jbuilder diff --git a/app/controllers/api/api_controller.rb b/app/controllers/api/api_controller.rb index 8c6aaf65..8cdd684f 100755 --- a/app/controllers/api/api_controller.rb +++ b/app/controllers/api/api_controller.rb @@ -8,6 +8,10 @@ class Api::ApiController < ActionController::API # before_action :lookup_domain helper_method :current_token, :current_login_employee, :get_cashier + def get_tax_profiles + @inclusive_tax, @exclusive_tax = TaxProfile.calculate_tax("online_order") + end + private # ActionView::Rendering override render_to_body breaks render :json, # resulting in an ActionView::MissingTemplate error. diff --git a/app/models/tax_profile.rb b/app/models/tax_profile.rb index 5ae936ba..f06270e6 100755 --- a/app/models/tax_profile.rb +++ b/app/models/tax_profile.rb @@ -4,4 +4,22 @@ class TaxProfile < ApplicationRecord default_scope { order('order_by asc') } # validations validates_presence_of :name, :rate, :group_type + + def self.calculate_tax(group_type) + divided_value =0.0 + exclusive =0.0 + tax_profiles = TaxProfile.where(group_type: group_type, tax_type: 'Percentage') + if !tax_profiles.empty? + tax_profiles.each do |tax| + #include or execulive + if tax.inclusive + rate = tax.rate.to_f + divided_value += (100 + rate)/rate + else + exclusive += tax.rate.to_f / 100 + end + end + end + return divided_value,exclusive + end end diff --git a/app/views/api/api/get_tax_profiles.json.jbuilder b/app/views/api/api/get_tax_profiles.json.jbuilder new file mode 100644 index 00000000..89da4ab0 --- /dev/null +++ b/app/views/api/api/get_tax_profiles.json.jbuilder @@ -0,0 +1,3 @@ +json.status true +json.inclusive_tax @inclusive_tax +json.exclusive_tax @exclusive_tax diff --git a/app/views/api/restaurant/menu/index.json.jbuilder b/app/views/api/restaurant/menu/index.json.jbuilder index 388b064e..3987df6d 100755 --- a/app/views/api/restaurant/menu/index.json.jbuilder +++ b/app/views/api/restaurant/menu/index.json.jbuilder @@ -14,4 +14,3 @@ menu_json = json.array! @menus do |menu| # end # end end - diff --git a/config/routes.rb b/config/routes.rb index 775c8d4e..4ac63e0a 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -50,6 +50,7 @@ scope "(:locale)", locale: /en|mm/ do post 'authenticate' => "authenticate#create" delete 'authenticate' => "authenticate#destroy" get "menu_item_groups" => "menu_item_groups#index" + get 'get_tax_profiles' => 'api#get_tax_profiles' namespace :restaurant do get 'zones' => "zones#index" resources :menu, only: [:index, :show]