diff --git a/.idea/sxrestaurant.iml b/.idea/sxrestaurant.iml index e1c0cfaa..2a0f8404 100644 --- a/.idea/sxrestaurant.iml +++ b/.idea/sxrestaurant.iml @@ -11,106 +11,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -164,6 +64,7 @@ + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 2cb0533a..0b106937 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,11 +2,22 @@ - + + + + + + + + + + + - + + @@ -20,26 +31,16 @@ - - + - - - - - - - - - - - - + + - + + @@ -48,28 +49,88 @@ - - + + - - + + - - + + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -92,6 +153,8 @@ sale_edit#edit @saleobj edit + sale_item + commissioner @@ -100,12 +163,6 @@ @@ -211,6 +274,12 @@ + + + + + + @@ -605,24 +674,26 @@ - + - + - + + - + @@ -631,7 +702,6 @@ - @@ -646,50 +716,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -747,14 +773,6 @@ - - - - - - - - @@ -801,6 +819,7 @@ + @@ -822,13 +841,6 @@ - - - - - - - @@ -840,14 +852,6 @@ - - - - - - - - @@ -873,6 +877,7 @@ + @@ -887,13 +892,7 @@ - - - - - - - + @@ -901,13 +900,6 @@ - - - - - - - @@ -936,14 +928,6 @@ - - - - - - - - @@ -976,23 +960,39 @@ - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1002,26 +1002,107 @@ - - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + diff --git a/app/controllers/origami/commissions_controller.rb b/app/controllers/origami/commissions_controller.rb index f5a91ad9..db49a188 100644 --- a/app/controllers/origami/commissions_controller.rb +++ b/app/controllers/origami/commissions_controller.rb @@ -74,13 +74,18 @@ class Origami::CommissionsController < BaseOrigamiController def select_sale_item # byebug sale_item_id = params[:sale_item_id] - @sale_item = SaleItem.find_by_sale_item_id(sale_item_id) + @selected_sale_item = SaleItem.find_by_sale_item_id(sale_item_id) + @product_commission = ProductCommission.find_by_sale_item_id(@selected_sale_item.id) + unless @product_commission.nil? + selected_commissioner = @product_commission.commissioner + end @commissioners = Commissioner.active.all - respond_to do |format| - format.json {render json: @commissioners} - # format.html {render @commissioners} - end + # respond_to do |format| + # format.json {render json: {[@commissioners],[@selected_sale_item]}} + # # format.html {render @commissioners} + # end + render json: {commissioner: @commissioners, selected_commissioner: selected_commissioner} end private diff --git a/app/controllers/origami/product_commissions_controller.rb b/app/controllers/origami/product_commissions_controller.rb index f3f05ab9..e2cba46c 100644 --- a/app/controllers/origami/product_commissions_controller.rb +++ b/app/controllers/origami/product_commissions_controller.rb @@ -28,11 +28,11 @@ class Origami::ProductCommissionsController < ApplicationController respond_to do |format| if @product_commission.save - format.html { redirect_to @product_commission, notice: 'Product commission was successfully created.' } - format.json { render :show, status: :created, location: @product_commission } + format.html {redirect_to @product_commission, notice: 'Product commission was successfully created.'} + format.json {render :show, status: :created, location: @product_commission} else - format.html { render :new } - format.json { render json: @product_commission.errors, status: :unprocessable_entity } + format.html {render :new} + format.json {render json: @product_commission.errors, status: :unprocessable_entity} end end end @@ -42,11 +42,11 @@ class Origami::ProductCommissionsController < ApplicationController def update respond_to do |format| if @product_commission.update(product_commission_params) - format.html { redirect_to @product_commission, notice: 'Product commission was successfully updated.' } - format.json { render :show, status: :ok, location: @product_commission } + format.html {redirect_to @product_commission, notice: 'Product commission was successfully updated.'} + format.json {render :show, status: :ok, location: @product_commission} else - format.html { render :edit } - format.json { render json: @product_commission.errors, status: :unprocessable_entity } + format.html {render :edit} + format.json {render json: @product_commission.errors, status: :unprocessable_entity} end end end @@ -56,19 +56,62 @@ class Origami::ProductCommissionsController < ApplicationController def destroy @product_commission.destroy respond_to do |format| - format.html { redirect_to product_commissions_url, notice: 'Product commission was successfully destroyed.' } - format.json { head :no_content } + format.html {redirect_to product_commissions_url, notice: 'Product commission was successfully destroyed.'} + format.json {head :no_content} + end + end + + def set_commissioner_to_sale_item + deselect = false + sale_item_id = params[:sale_item_id] + commissioner_id = params[:commissioner_id] + @sale_item = SaleItem.find(sale_item_id) + @menu_item = MenuItem.find_by_item_code(@sale_item.product_code) + @commission = Commission.where('product_id = ? AND is_active = ?', @menu_item.id, true).take + @commissioner = Commissioner.where('id = ? AND is_active = ?', commissioner_id, true).take + @product_commission = ProductCommission.where('sale_item_id = ?', @sale_item.id).take + # byebug + if !@product_commission.nil? + if @product_commission.commissioner_id == @commissioner.id + @product_commission.destroy + deselect = true + else + @product_commission.commissioner_id = @commissioner.id + deselect = false + end + else + @product_commission = ProductCommission.new + @product_commission.product_id = @menu_item.id + unless @commission.nil? + @product_commission.commission_id = @commission.id + if @commission.commission_type == 'Percentage' + @product_commission.price = @sale_item.unit_price * (@commission.amount / 100.0) + @product_commission.amount = @product_commission.price * @sale_item.qty + elsif @commission.commission_type == 'Net Amount' + @product_commission.price = @commission.amount + @product_commission.amount = @product_commission.price * @sale_item.qty + end + end + @product_commission.commissioner_id = @commissioner.id + @product_commission.qty = @sale_item.qty + @product_commission.sale_id = @sale_item.sale_id + @product_commission.sale_item_id = @sale_item.sale_item_id + end + if @product_commission.save + render json: {status: true, deselect: deselect} + else + render json: {status: false, deselect: deselect} end end private - # Use callbacks to share common setup or constraints between actions. - def set_product_commission - @product_commission = ProductCommission.find(params[:id]) - end + # Use callbacks to share common setup or constraints between actions. + def set_product_commission + @product_commission = ProductCommission.find(params[:id]) + end - # Never trust parameters from the scary internet, only allow the white list through. - def product_commission_params - params.fetch(:product_commission, {}) - end + # Never trust parameters from the scary internet, only allow the white list through. + def product_commission_params + params.fetch(:product_commission, {}) + end end diff --git a/app/models/commission.rb b/app/models/commission.rb index df11eb24..943bb5c1 100644 --- a/app/models/commission.rb +++ b/app/models/commission.rb @@ -1,4 +1,5 @@ class Commission < ApplicationRecord belongs_to :menu_item, foreign_key: 'product_id' has_many :commissioners + has_many :product_commissions end diff --git a/app/models/commissioner.rb b/app/models/commissioner.rb index dd22278a..8bec411d 100644 --- a/app/models/commissioner.rb +++ b/app/models/commissioner.rb @@ -2,5 +2,6 @@ class Commissioner < ApplicationRecord belongs_to :employee, foreign_key: 'emp_id' belongs_to :commission, foreign_key: 'commission_type' has_many :in_juties + has_many :product_commissions scope :active, -> { where(is_active: true) } end diff --git a/app/models/menu_item.rb b/app/models/menu_item.rb index c581a7b0..e7f53dcd 100644 --- a/app/models/menu_item.rb +++ b/app/models/menu_item.rb @@ -4,6 +4,7 @@ class MenuItem < ApplicationRecord belongs_to :menu_category, :optional => true has_many :menu_item_instances has_many :commissions + has_many :product_commissions # belongs_to :parent, :class_name => "MenuItem", foreign_key: "menu_item_id", :optional => true # has_many :children, :class_name => "MenuItem", foreign_key: "menu_item_id" diff --git a/app/models/product_commission.rb b/app/models/product_commission.rb index 70de81f8..59e64b79 100644 --- a/app/models/product_commission.rb +++ b/app/models/product_commission.rb @@ -1,2 +1,11 @@ class ProductCommission < ApplicationRecord + belongs_to :commission, foreign_key: 'commission_id' + belongs_to :commissioner, foreign_key: 'commissioner_id' + belongs_to :menu_item, foreign_key: 'product_id' + belongs_to :sale_item, foreign_key: 'sale_item_id' + belongs_to :sale, foreign_key: 'sale_id' + + def self.check_product_commission(sale_item_id) + + end end diff --git a/app/models/sale.rb b/app/models/sale.rb index 9927251d..5544d1b8 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -13,6 +13,7 @@ class Sale < ApplicationRecord has_many :sale_payments has_many :sale_orders has_many :bookings + has_many :product_commissions scope :open_invoices, -> { where("sale_status = 'new' and receipt_date BETWEEN '#{DateTime.now.utc.end_of_day}' AND '#{DateTime.now.utc.beginning_of_day}'") } scope :complete_sale, -> { where("sale_status = 'completed' and receipt_date BETWEEN '#{DateTime.now.utc.beginning_of_day}' AND '#{DateTime.now.utc.end_of_day}'") } diff --git a/app/models/sale_item.rb b/app/models/sale_item.rb index af2230de..b8ae456d 100644 --- a/app/models/sale_item.rb +++ b/app/models/sale_item.rb @@ -5,6 +5,7 @@ class SaleItem < ApplicationRecord before_create :generate_custom_id belongs_to :sale + has_many :product_commissions #compute items - discount, tax, price_change def compute_item @@ -80,7 +81,7 @@ class SaleItem < ApplicationRecord account_price[:amount] = account_price[:amount] + si.price price = price + si.price end - + end rebate_arr.push(account_price) end diff --git a/app/views/origami/commissions/_commissioners.html.erb b/app/views/origami/commissions/_commissioners.html.erb deleted file mode 100644 index ed9da369..00000000 --- a/app/views/origami/commissions/_commissioners.html.erb +++ /dev/null @@ -1,16 +0,0 @@ -
-
-
COMMISSIONERS
-
-
-
- <% @commissioners.each do |commissioner| %> -
-
- <%= commissioner.name %> -
-
- <% end %> -
-
-
diff --git a/app/views/origami/commissions/load_commissioners.html.erb b/app/views/origami/commissions/load_commissioners.html.erb index 748fb4ef..b0bb5e63 100644 --- a/app/views/origami/commissions/load_commissioners.html.erb +++ b/app/views/origami/commissions/load_commissioners.html.erb @@ -1,11 +1,26 @@
- <%= render 'origami/commissions/commissioners', commissioners: @commissioners %> +
+
+
COMMISSIONERS
+
+
+
+ <% @commissioners.each do |commissioner| %> +
+
+ <%= commissioner.name %> +
+
+ <% end %> +
+
+
-
+
INVOICE DETAILS
@@ -34,11 +49,10 @@ # - Items - QTY - - Price - + Items + QTY + Price + Commissioner @@ -49,22 +63,26 @@ count += 1 sub_total = sub_total + sale_item.price %> - + <% # Can't check for discount unless sale_item.price == 0 %> - + <%= count %> - + <%= sale_item.product_name %> - + <%= sale_item.qty %> - + <%= sale_item.unit_price %> + + <% product_commission = ProductCommission.find_by_sale_item_id(sale_item.id) %> + <%= product_commission.commissioner.name rescue '-' %> + <% end @@ -87,135 +105,78 @@
-
+
- +
diff --git a/app/views/origami/home/index_bk.html.erb b/app/views/origami/home/index_bk.html.erb index ce2b3a94..281478b6 100644 --- a/app/views/origami/home/index_bk.html.erb +++ b/app/views/origami/home/index_bk.html.erb @@ -243,7 +243,7 @@ -

<%= odr.table_name %>

<%= odr.table_name %>

Receipt No : diff --git a/config/routes.rb b/config/routes.rb index b5af4fe2..fecc8d77 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -89,6 +89,8 @@ Rails.application.routes.draw do # commissions get '/table/:table_id/sale/:sale_id/load_commissioners' => 'commissions#load_commissioners', as: 'load_commissioners' post 'select_sale_item' => 'commissions#select_sale_item' + # product_commission + post 'select_commissioner' => 'product_commissions#set_commissioner_to_sale_item' get 'table/:dining_id/movetable' => "movetable#move_dining" get 'table/:dining_id/moveroom' => "moveroom#move_dining" diff --git a/db/migrate/20170822034139_create_product_commissions.rb b/db/migrate/20170822034139_create_product_commissions.rb deleted file mode 100644 index 95f9442d..00000000 --- a/db/migrate/20170822034139_create_product_commissions.rb +++ /dev/null @@ -1,12 +0,0 @@ -class CreateProductCommissions < ActiveRecord::Migration[5.1] - def change - create_table :product_commissions do |t| - t.string :product_code - t.integer :commission_id - t.integer :qty - t.string :sale_id - t.string :sale_item_id - t.timestamps - end - end -end diff --git a/db/migrate/20170823034141_create_product_commissions.rb b/db/migrate/20170823034141_create_product_commissions.rb new file mode 100644 index 00000000..cb0e5a3f --- /dev/null +++ b/db/migrate/20170823034141_create_product_commissions.rb @@ -0,0 +1,16 @@ +class CreateProductCommissions < ActiveRecord::Migration[5.1] + # rake db:migrate:down VERSION=20170823034141 + def change + create_table :product_commissions do |t| + t.string :product_id + t.integer :commission_id + t.integer :commissioner_id + t.decimal :qty, :precision => 10, :scale => 2, :default => 0.00 + t.string :sale_id + t.string :sale_item_id + t.decimal :price, :precision => 10, :scale => 2, :default => 0.00 + t.decimal :amount, :precision => 10, :scale => 2, :default => 0.00 + t.timestamps + end + end +end