commission in sale edit

This commit is contained in:
Zin Lin Phyo
2017-08-25 13:44:22 +06:30
parent 283c59c5dc
commit b4eb27b8a9
16 changed files with 417 additions and 422 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -1,4 +1,5 @@
class Commission < ApplicationRecord
belongs_to :menu_item, foreign_key: 'product_id'
has_many :commissioners
has_many :product_commissions
end

View File

@@ -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

View File

@@ -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"

View File

@@ -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

View File

@@ -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}'") }

View File

@@ -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

View File

@@ -1,16 +0,0 @@
<div class="card">
<div class="card-header">
<div><strong id="order-title">COMMISSIONERS </strong></div>
</div>
<div class="card-block">
<div class="card-columns" id="commissioners" style="float: left; padding-top:10px; column-gap: 1.2rem; min-height:600px; max-height:600px; overflow-x:scroll;">
<% @commissioners.each do |commissioner| %>
<div class="card tables green text-white" data-id="<%= commissioner.id %>">
<div class="card-block">
<%= commissioner.name %>
</div>
</div>
<% end %>
</div>
</div>
</div>

View File

@@ -1,11 +1,26 @@
<div class="row">
<!-- Column One -->
<div class="col-lg-5 col-md-5 col-sm-5" style="height: 100%">
<%= render 'origami/commissions/commissioners', commissioners: @commissioners %>
<div class="card">
<div class="card-header">
<div><strong id="order-title">COMMISSIONERS </strong></div>
</div>
<div class="card-block">
<div class="card-columns" id="commissioners" style="float: left; padding-top:10px; column-gap: 1.2rem; min-height:600px; max-height:600px; overflow-x:scroll;">
<% @commissioners.each do |commissioner| %>
<div class="card tables green text-white commissioner" data-commissioner-id="<%= commissioner.id %>" data-commissioner-name="<%= commissioner.name %>">
<div class="card-block">
<%= commissioner.name %>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>
<!-- Column Two -->
<div class="col-lg-5 col-md-5 col-sm-5">
<div class="col-lg-6 col-md-6 col-sm-6">
<div class="card">
<div class="card-header">
<div><strong id="order-title">INVOICE DETAILS </strong></div>
@@ -34,11 +49,10 @@
<thead>
<tr>
<th width="4%">#</th>
<th class="" width="60%">Items</th>
<th style="" width="20%">QTY
</td>
<th class="" width="30%">Price
</td>
<th class="" width="40%">Items</th>
<th style="" width="20%">QTY</th>
<th class="" width="20%">Price</th>
<th class="" width="20%">Commissioner</th>
</tr>
</thead>
<tbody>
@@ -49,22 +63,26 @@
count += 1
sub_total = sub_total + sale_item.price
%>
<input type="hidden" id="sale_id" value="<%= @saleobj.sale_id %>">
<input type="hidden" id="sale_id" class="sale_id" value="<%= @saleobj.sale_id %>">
<%
# Can't check for discount
unless sale_item.price == 0
%>
<tr id="sale_item" data-sale-item="<%= sale_item.sale_item_id %>">
<tr id="sale_item" class="sale_item" data-sale-item="<%= sale_item.sale_item_id %>">
<td width="4%"><%= count %></td>
<td class='' width="60%">
<td class='product_name' width="40%">
<%= sale_item.product_name %>
</td>
<td class='' width="20%">
<td class='qty' width="20%">
<%= sale_item.qty %>
</td>
<td class='' width="20%">
<td class='unit_price' width="20%">
<%= sale_item.unit_price %>
</td>
<td class='commissioner' width="20%">
<% product_commission = ProductCommission.find_by_sale_item_id(sale_item.id) %>
<%= product_commission.commissioner.name rescue '-' %>
</td>
</tr>
<%
end
@@ -87,135 +105,78 @@
</div>
<!-- Column Three -->
<div class="col-lg-2 col-md-2 col-sm-2">
<div class="col-lg-1 col-md-1 col-sm-1">
<!-- Waiter Buttons -->
<button type="button" class="btn btn-primary btn-block" id='back'>Back</button>
<button type="button" class="btn btn-primary btn-block" id='apply'>Apply</button>
<!--<button type="button" class="btn btn-primary btn-block" id='apply'>Apply</button>-->
</div>
</div>
<script>
// Bill Request
$(document).ready(function () {
var selected_sale_item = 0;
$(".update").on('click', function () {
var sale_item_id = $(this).attr('data-id');
var qty = $('#' + sale_item_id + "_qty").val();
var price = $('#' + sale_item_id + "_price").val();
console.log(qty + "|" + price)
var ajax_url = "/origami/item_edit";
$('.sale_item').on('click', function () {
$('#order-items-table tr').siblings().css( "background-color", "white" );
this.style.backgroundColor = 'lightgray';
var sale_item_id = this.getAttribute('data-sale-item');
var ajax_url = "/origami/select_sale_item";
selected_sale_item = sale_item_id;
localStorage.setItem("selected_sale_item", selected_sale_item);
$.ajax({
type: "POST",
url: ajax_url,
data: 'sale_item_id=' + sale_item_id + "&update_qty=" + qty + "&update_price=" + price,
data: 'sale_item_id=' + sale_item_id,
success: function (result) {
$.confirm({
title: 'Alert!',
content: 'Qty and Price was successfully Updated',
buttons: {
confirm: {
text: 'Ok',
btnClass: 'btn-green btn-lg',
action: function () {
location.reload();
if (result != null){
$('#commissioners').empty();
result.commissioner.forEach(function (c) {
if (result.selected_commissioner != null){
if (c.id == result.selected_commissioner.id){
sale_item = "<div class=\"card tables blue text-white commissioner\" onclick=\"click_commissioner(this)\" data-commissioner-id=" + c.id + " data-commissioner-name=\"" + c.name + "\">" + "<div class=\"card-block\">" + c.name + "</div>" + "</div>"
}else {
sale_item = "<div class=\"card tables green text-white commissioner\" onclick=\"click_commissioner(this)\" data-commissioner-id=" + c.id + " data-commissioner-name=\"" + c.name + "\">" + "<div class=\"card-block\">" + c.name + "</div>" + "</div>"
}
}else {
sale_item = "<div class=\"card tables green text-white commissioner\" onclick=\"click_commissioner(this)\" data-commissioner-id=" + c.id + " data-commissioner-name=\"" + c.name + "\">" + "<div class=\"card-block\">" + c.name + "</div>" + "</div>"
}
}
});
$('#commissioners').append(sale_item);
})
}
}
});
});
$('.void').on('click', function () {
var sale_item_id = $(this).attr('data-id');
var ajax_url = "/origami/item_void";
$.ajax({
type: "POST",
url: ajax_url,
data: 'sale_item_id=' + sale_item_id,
success: function (result) {
location.reload();
}
});
})
$('#back').on('click', function () {
var table_id = '<%= @table_id %>';
window.location.href = '/origami/table/' + table_id;
});
$('.foc').on('click', function () {
var sale_item_id = $(this).attr('data-id');
var ajax_url = "/origami/item_foc";
$.ajax({
type: "POST",
url: ajax_url,
data: 'sale_item_id=' + sale_item_id,
success: function (result) {
location.reload();
}
});
})
});
$('.cancel').on('click', function () {
var sale_item_id = $(this).attr('data-id');
var ajax_url = "/origami/item_void_cancel";
$.ajax({
type: "POST",
url: ajax_url,
data: 'sale_item_id=' + sale_item_id,
success: function (result) {
location.reload();
}
});
})
$('.commissioner').on('click', function () {
alert('click');
});
})
$('#back').on('click', function () {
var table_id = '<%= @table_id %>'
window.location.href = '/origami/table/' + table_id;
})
$('#cancel_all_void').on('click', function () {
var sale_id = "<%= @saleobj.sale_id %>"
var ajax_url = "/origami/cancel_all_void";
function click_commissioner(commissioner) {
var commissioner_id = commissioner.getAttribute('data-commissioner-id');
var selected_sale_item = localStorage.getItem("selected_sale_item");
$(commissioner).removeClass("green").addClass("blue");
$(commissioner).siblings().removeClass("blue").addClass("green");
$("tr[data-sale-item=" + selected_sale_item + "] td.commissioner").text(commissioner.getAttribute('data-commissioner-name'));
var ajax_url = "/origami/select_commissioner";
var param_data = 'sale_item_id=' + selected_sale_item + '&commissioner_id=' + commissioner_id;
$.ajax({
type: "POST",
url: ajax_url,
data: 'sale_id=' + sale_id,
data: param_data,
success: function (result) {
location.reload();
console.log(selected_sale_item + "," +commissioner_id);
if(result.deselect == true){
$(commissioner).removeClass("blue").addClass("green");
$("tr[data-sale-item=" + selected_sale_item + "] td.commissioner").text('-');
}
}
});
})
};
$('#apply').on('click', function () {
var sale_id = "<%= @saleobj.sale_id %>"
var ajax_url = "/origami/apply_void";
var table_id = '<%= @table_id %>'
$.ajax({
type: "POST",
url: ajax_url,
data: 'sale_id=' + sale_id,
success: function (result) {
window.location.href = '/origami/table/' + table_id;
}
});
})
$('#sale_item').on('click', function () {
var sale_item_id = this.getAttribute('data-sale-item');
var ajax_url = "/origami/select_sale_item";
var table_id = '<%= @table_id %>'
$.ajax({
type: "POST",
url: ajax_url,
data: 'sale_item_id=' + sale_item_id,
success: function (result) {
$('#commissioners').empty();
result.forEach(function (t) {
sale_item = "<div class=\"card tables green text-white\" data-id=" + t.id + ">" + "<div class=\"card-block\">" + t.name + "</div>" + "</div>"
$('#commissioners').append(sale_item);
})
}
});
})
</script>

View File

@@ -243,7 +243,7 @@
<p class="hidden orders-id"><%= unique_id %></p>
<p class="hidden customer-id"><%= customer_id %></p>
<p class="hidden order-cid"><%= odr.order_id %></p>
<h4 class="card-title orders-table"><%= odr.table_name %></h4
<h4 class="card-title orders-table"><%= odr.table_name %></h4>
<p class="card-text">
Receipt No :
<span class="orders-receipt-no">