Merge branch 'master' of bitbucket.org:code2lab/sxrestaurant into addorder

This commit is contained in:
Aung Myo
2017-08-24 13:30:02 +06:30
64 changed files with 1942 additions and 1032 deletions

View File

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

View File

@@ -1,3 +1,6 @@
class Commissioner < ApplicationRecord
has_many :employees
belongs_to :employee, foreign_key: 'emp_id'
belongs_to :commission, foreign_key: 'commission_type'
has_many :in_juties
scope :active, -> { where(is_active: true) }
end

View File

@@ -91,41 +91,50 @@ class Customer < ApplicationRecord
auth_token = memberaction.auth_token.to_s
url = membership.gateway_url.to_s + memberaction.gateway_url.to_s
@customers = Customer.where("membership_type IS NOT NULL AND membership_id IS NULL")
@customers = Customer.where("membership_type IS NOT NULL AND membership_id IS NULL")
@customers.each do |customer|
begin
response = HTTParty.post(url,
:body =>
{ name: customer.name,phone: customer.contact_no,
email: customer.email,dob: customer.date_of_birth,
address: customer.address,nrc:customer.nrc_no,
card_no:customer.card_no,member_group_id: customer.membership_type,
merchant_uid:merchant_uid,auth_token:auth_token
}.to_json,
:headers => {
'Content-Type' => 'application/json',
'Accept' => 'application/json'
})
rescue Net::OpenTimeout
response = { status: false, message: "Server Time out" }
rescue OpenURI::HTTPError
response = { status: false, message: "Can't connect server"}
member_params = { name: customer.name,phone: customer.contact_no,
email: customer.email,dob: customer.date_of_birth,
address: customer.address,nrc:customer.nrc_no,
card_no:customer.card_no,member_group_id: customer.membership_type,
merchant_uid:merchant_uid,auth_token:auth_token}.to_json
rescue SocketError
response = { status: false, message: "Can't connect server"}
end
# Check for paypar account exists
# if paypar_account_no != nil || paypar_account_no != ''
if paypar_account_no.present?
member_params = { name: customer.name,phone: customer.contact_no,
email: customer.email,dob: customer.date_of_birth,
address: customer.address,nrc:customer.nrc_no,
paypar_account_no: customer.paypar_account_no,
card_no:customer.card_no,member_group_id: customer.membership_type,
merchant_uid:merchant_uid,auth_token:auth_token}.to_json
end
if response["status"] == true
status = customer.update_attributes(membership_id: response["customer_datas"]["id"])
end
begin
response = HTTParty.post(url,
:body => member_params,
:headers => {
'Content-Type' => 'application/json',
'Accept' => 'application/json'
})
rescue Net::OpenTimeout
response = { status: false, message: "Server Time out" }
rescue OpenURI::HTTPError
response = { status: false, message: "Can't connect server"}
rescue SocketError
response = { status: false, message: "Can't connect server"}
end
if response["status"] == true
status = customer.update_attributes(membership_id: response["customer_datas"]["id"])
end
end
end
def self.update_rebate
sales = Sale.where("rebate_status = 'false'")
sales.each do |sale|
if sale.customer.membership_id

View File

@@ -3,19 +3,22 @@ class DiningCharge < ApplicationRecord
belongs_to :room
def self.amount_calculate(dining_charges_obj, checkin , checkout)
# note :: the first Charge Block will cost all, the Time rounding block will included in 2nd Charge Block
if !checkin.nil? && !checkout.nil? && !dining_charges_obj.nil?
price = 0
minutes = DiningCharge.time_diff(checkout, checkin)
free_time = DiningCharge.convert_to_minutes(dining_charges_obj.minimum_free_time.strftime('%H:%M'))
free_time = DiningCharge.convert_to_minutes(dining_charges_obj.minimum_free_time.utc.localtime.strftime('%H:%M'))
dining_minutes = minutes - free_time # stayminutes - free minutes
charge_type = dining_charges_obj.charge_type
if charge_type == 'hr'
price = DiningCharge.charges(dining_charges_obj, dining_minutes, 'hr')
elsif charge_type == 'day'
price = charges(dining_charges_obj, dining_minutes, 'day')
if dining_minutes <= 0
price = 0
else
charge_type = dining_charges_obj.charge_type
if charge_type == 'hr'
price = DiningCharge.charges(dining_charges_obj, dining_minutes, 'hr')
elsif charge_type == 'day'
price = charges(dining_charges_obj, dining_minutes, 'day')
end
end
return price
else
puts "<<<<<<<< NO"
@@ -25,14 +28,15 @@ class DiningCharge < ApplicationRecord
def self.charges(chargesObj, dining_minutes, type)
solid_price = 0
block = DiningCharge.convert_to_minutes(chargesObj.charge_block.strftime('%H:%M'))
block = DiningCharge.convert_to_minutes(chargesObj.charge_block.utc.localtime.strftime('%H:%M'))
result = dining_minutes / block
if result.to_i < 1
return chargesObj.unit_price
elsif result.to_i >= 1
solid_price = result * chargesObj.unit_price
remain_value = dining_minutes % block
rounding_block = DiningCharge.convert_to_minutes(chargesObj.time_rounding_block.strftime('%H:%M'))
rounding_block = DiningCharge.convert_to_minutes(chargesObj.time_rounding_block.utc.localtime.strftime('%H:%M'))
roundingblock = remain_value / rounding_block
if roundingblock.to_i < 1
return DiningCharge.check_rounding(chargesObj, solid_price)
@@ -48,7 +52,7 @@ class DiningCharge < ApplicationRecord
end
end
def self.check_rounding(chargesObj)
def self.check_rounding(chargesObj,solid_price)
if chargesObj.time_rounding == "down"
return solid_price
else

View File

@@ -1,6 +1,7 @@
class DiningFacility < ApplicationRecord
belongs_to :zone
has_many :dining_charges
has_many :in_juties
TABLE_TYPE = "Table"
ROOM_TYPE = "Room"

View File

@@ -1,7 +1,8 @@
class Employee < ApplicationRecord
has_secure_password
belongs_to :commissioner
has_many :commissioners
has_many :shit_sales
validates_presence_of :name, :role
validates_presence_of :password, :on => [:create]
validates :emp_id, uniqueness: true, numericality: true, length: {in: 1..4}, allow_blank: true

4
app/models/in_juty.rb Normal file
View File

@@ -0,0 +1,4 @@
class InJuty < ApplicationRecord
belongs_to :dining_facility, foreign_key: 'dinning_id'
belongs_to :commissioner, foreign_key: 'commissioner_ids'
end

View File

@@ -3,6 +3,8 @@ class MenuItem < ApplicationRecord
belongs_to :menu_category, :optional => true
has_many :menu_item_instances
has_many :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"
belongs_to :account

View File

@@ -1,6 +1,6 @@
class MenuItemAttribute < ApplicationRecord
validates_presence_of :attribute_type, :name, :value
def self.collection
MenuItemAttribute.select("name, value").map { |e| [e.name, e.value] }
MenuItemAttribute.select("name").map { |e| [e.name, e.name] }
end
end

View File

@@ -1,6 +1,6 @@
class Product < ApplicationRecord
validates_presence_of :name
# Product Image Uploader
mount_uploader :image_path, ProductImageUploader
end

View File

@@ -6,7 +6,204 @@ class Promotion < ApplicationRecord
accepts_nested_attributes_for :promotion_products , :allow_destroy => true
PROMO_TYPE1 = "Quantity"
PROMO_TYPE2 = "Net_off"
PROMO_TYPE3 = "Net_price"
PROMO_TYPE2 = "Net_off" # 3000 => - 500 => 2500 [total]
PROMO_TYPE3 = "Net_price" # 1800 => 1000 => 1000
PROMO_TYPE4 = "Percentage"
def self.promo_activate(saleObj)
current_day = Time.now.strftime("%Y-%m-%d")
current_time = Time.now.strftime('%H:%M:%S')
day = Date.today.wday
promoList = is_between_promo_datetime(current_day,current_time)
puts "promoList - " + promoList.size.to_s
if promoList.size > 0
itemList = combine_item(saleObj)
is_promo_day(promoList,day, itemList, saleObj.sale_id)
end
end
def self.is_between_promo_datetime(current_day,current_time) #database is not local time
promoList = Promotion.where("(TO_CHAR(promo_start_date, 'YYYY-MM-DD') <=? AND TO_CHAR(promo_end_date, 'YYYY-MM-DD') >=?) AND (promo_start_hour < ? AND promo_end_hour > ?)", current_day, current_day, current_time, current_time)
return promoList
end
def self.combine_item(saleObj)
itemList = saleObj.sale_items.group(:product_code).sum(:qty)
end
def self.is_promo_day(promoList, day, orderitemList, sale_id)
puts "Today date - " + day.to_s
promoList.each do |promo|
dayresult = promo.promo_day.include?(day.to_s)
if dayresult
orderitemList.each do |item|
find_promo_item(promo, item, sale_id)
end
end
end
end
def self.find_promo_item(promo, orderitem, sale_id)
if promo.original_product.to_s == orderitem[0].to_s
if promo.min_qty.to_i > orderitem[1].to_i
return false
else
check_promo_type(promo,orderitem, sale_id)
end
end
end
def self.check_promo_type(promo, orderitem, sale_id)
same, promo_product = check_giveaway_product(promo, orderitem[0])
if promo.promo_type == Promotion::PROMO_TYPE1
if same
give_promotion_same_product(orderitem[1], promo.min_qty, promo_product.min_qty, orderitem, sale_id)
else
give_promotion_second_product(orderitem[1], promo.min_qty, promo_product.item_code, orderitem, sale_id)
end
elsif promo.promo_type == Promotion::PROMO_TYPE2
give_promotion_nett_off(same,promo_product,promo.min_qty, orderitem, sale_id)
elsif promo.promo_type == Promotion::PROMO_TYPE3
give_promotion_nett_price(same,promo_product,promo.min_qty, orderitem, sale_id)
elsif promo.promo_type == Promotion::PROMO_TYPE4
give_promotion_nett_price(same,promo_product,promo.min_qty, orderitem, sale_id)
end
end
def self.check_giveaway_product(promo, orderitem)
promo.promotion_products.each do |promo_product|
if promo_product.item_code == orderitem
return true, promo_product
else
return false, promo_product
end
end
end
def self.give_promotion_same_product(qty, promoqty, foc_min_qty, orderitem, sale_id)
puts " Order qty: " + qty.to_s + " / promoqty: " + promoqty.to_s + " / giveaway: " + foc_min_qty.to_s
multiple = qty.to_i / promoqty.to_i # loop count
charge_qty = 0
foc_qty = 0
if multiple > 0
multiple.times.each do |key|
if qty > promoqty
charge_qty += promoqty
different = qty - promoqty
qty = different
if different == 0
foc_qty += foc_min_qty
else
foc_qty += foc_min_qty
qty = qty - foc_min_qty
end
else
charge_qty += qty
end
end
if multiple == foc_qty
charge_qty += qty
end
else
charge_qty += qty
end
item = OrderItem.find_by_item_code(orderitem[0])
update_existing_item(foc_qty, item, sale_id, "promotion", item.price)
puts "Charged - " + charge_qty.to_s
puts "FOC - " + foc_qty.to_s
end
# AA - 10 # 3 # BB # orderList, #S34345
def self.give_promotion_second_product(orderitem_count, foc_min_qty, promo_product, orderitem, sale_id)
puts "..... orderitem_count: " + orderitem_count.to_s + " / foc_min_qty: " + foc_min_qty.to_s + " /promo_product: " + promo_product + " orderitem: " + orderitem.to_s
promotion_qty = orderitem_count.to_i / foc_min_qty.to_i # get foc item qty
item = OrderItem.find_by_item_code(promo_product)
update_existing_item(promotion_qty, item, sale_id, "promotion", item.price)
end
def self.update_existing_item(foc_qty, item, sale_id, type, item_price)
sale_item = SaleItem.new
sale_item.product_code = item.item_code
sale_item.product_name = item.item_name + "(promotion)"
sale_item.product_alt_name = item.alt_name
sale_item.account_id = item.account_id
sale_item.remark = type
sale_item.qty = foc_qty * (-1)
sale_item.unit_price = item_price * (-1)
sale_item.taxable_price = item_price
sale_item.price = foc_qty * item_price * (-1)
sale_item.is_taxable = item.taxable
sale_item.sale_id = sale_id
sale_item.save
end
def self.give_promotion_nett_off(same, promo_product, foc_min_qty, orderitem, sale_id)
puts " same: " + same.to_s + " promo_product: " + promo_product.item_code.to_s + " foc_min_qty: " + foc_min_qty.to_s + " orderitem: " + orderitem.to_s
if same
foc_qty = orderitem[1].to_i / foc_min_qty
item = OrderItem.find_by_item_code(orderitem[0])
update_existing_item(foc_qty, item, sale_id, "promotion nett off", promo_product.net_off)
else
foc_qty = find_second_item_qty(sale_id, promo_product.item_code)
item = OrderItem.find_by_item_code(promo_product.item_code)
update_existing_item(foc_qty, item, sale_id, "promotion nett off", promo_product.net_off)
end
end
def self.give_promotion_nett_price(same, promo_product, foc_min_qty, orderitem, sale_id)
puts " same: " + same.to_s + " promo_product: " + promo_product.item_code.to_s + " foc_min_qty: " + foc_min_qty.to_s + " orderitem: " + orderitem.to_s
if same
foc_qty = orderitem[1].to_i / foc_min_qty
item = OrderItem.find_by_item_code(orderitem[0])
price = item.price - promo_product.net_price
update_existing_item(foc_qty, item, sale_id, "promotion nett price", price)
else
foc_qty = find_second_item_qty(sale_id, promo_product.item_code)
item = OrderItem.find_by_item_code(promo_product.item_code)
price = item.price - promo_product.net_price
update_existing_item(foc_qty, item, sale_id, "promotion nett price", price)
end
end
def self.give_promotion_discount(same, promo_product, foc_min_qty, orderitem, sale_id)
puts " same: " + same.to_s + " promo_product: " + promo_product.item_code.to_s + " foc_min_qty: " + foc_min_qty.to_s + " orderitem: " + orderitem.to_s
if same
foc_qty = orderitem[1].to_i / foc_min_qty
item = OrderItem.find_by_item_code(orderitem[0])
total = orderitem[1].to_i * item.price
price = calculate_discount(total, promo_product.percentage)
update_existing_item(foc_qty, item, sale_id, "promotion discount", price)
else
foc_qty = find_second_item_qty(sale_id, promo_product.item_code)
item = OrderItem.find_by_item_code(promo_product.item_code)
total = item.price * foc_qty
price = calculate_discount(total, promo_product.percentage)
update_existing_item(foc_qty, item, sale_id, "promotion discount", price)
end
end
def self.find_second_item_qty(sale_id, promo_item)
saleObj = Sale.find_by_sale_id(sale_id)
itemList = combine_item(saleObj)
itemList.each do |item|
if item[0] == promo_item
return item[1]
end
end
end
def self.calculate_discount(total, discount)
self (total.to_i * discount.to_i) / 100
end
end

View File

@@ -298,8 +298,8 @@ class Sale < ApplicationRecord
customer = Customer.find(sale.customer_id)
# #Creat new tax records
tax_profiles.each do |tax|
customer.tax_profiles.each do |cus_tax|
if cus_tax.to_i == tax.id
customer.tax_profiles.each do |cus_tax|
if cus_tax.to_i == tax.id
sale_tax = SaleTax.new(:sale => sale)
sale_tax.tax_name = tax.name
sale_tax.tax_rate = tax.rate
@@ -311,7 +311,7 @@ class Sale < ApplicationRecord
rate = tax.rate
divided_value = (100 + rate)/rate
sale_tax.tax_payable_amount = total_tax / divided_value
else
else
sale_tax.tax_payable_amount = total_tax * tax.rate / 100
total_tax_amount = total_tax_amount + sale_tax.tax_payable_amount
end
@@ -319,7 +319,7 @@ class Sale < ApplicationRecord
# total_taxable = total_taxable + sale_tax.tax_payable_amount
sale_tax.inclusive = tax.inclusive
sale_tax.save
sale_tax.save
end
end
end
@@ -338,13 +338,13 @@ class Sale < ApplicationRecord
total_tax_amount = 0
#tax_profile - list by order_by
tax_profiles = TaxProfile.all.order("order_by asc")
customer = Customer.find(self.customer_id)
customer = Customer.find(self.customer_id)
puts customer
#Create new tax records
tax_profiles.each do |tax|
customer.tax_profiles.each do |cus_tax|
if cus_tax.to_i == tax.id
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
@@ -356,16 +356,16 @@ class Sale < ApplicationRecord
rate = tax.rate
divided_value = (100 + rate)/rate
sale_tax.tax_payable_amount = total_tax / divided_value
else
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
# total_taxable = total_taxable + sale_tax.tax_payable_amount
sale_tax.inclusive = tax.inclusive
sale_tax.save
sale_tax.save
end
end
end
@@ -596,7 +596,7 @@ def self.get_item_query()
query = query.joins(" JOIN accounts acc ON acc.id = mi.account_id")
query = query.group('i.product_code ').order("mi.account_id, mi.menu_category_id")
end
end
def self.get_by_shift_items(shift_sale_range, shift, from, to, status)
# date_type_selection = get_sql_function_for_report_type(report_type)

View File

@@ -1,116 +1,4 @@
class Shop < ApplicationRecord
def promo_activate
current_day = Time.now.strftime("%Y-%d-%m")
current_time = Time.now.strftime('%H:%M')
day = Date.today.wday
promoList = is_between_promo_datetime(current_day,current_time)
if promoList.size > 0
itemList = combine_item(saleObj)
is_promo_day(promoList,day, itemList)
end
end
def is_between_promo_datetime(current_day,current_time)
promoList = Promotion.where('( promo_start_date < ? AND promo_end_date > ?) AND (promo_start_time < ? AND promo_end_time > ?)', current_day, current_day, current_time, current_time)
return promoList
end
def combine_item(saleObj)
itemList = saleObj.sale_items.group(:product_code).sum(:qty)
end
def is_promo_day(promoList, day, orderitemList)
promoList.each do |promo|
dayresult = promo.promo_day.include?(day)
if dayresult
orderitemList.each do |item|
find_promo_item(promo, item)
end
end
end
end
def find_promo_item(promo, orderitem)
if promo.prouduct_item == orderitem
if promo.minmum_qty < orderitem.qty
return false
else
same, promo_item_code = check_giveaway_product(promo, orderitem)
if same
give_promotion_same_product
else
find_promo_item_in_orderlist
end
end
end
end
def check_giveaway_product(promo, orderitem)
promo.promotion_products.each do |promo_product|
if promo_product.item_code == orderitem.item_code
return true, promo_product.item_code
else
return false, promo_product.item_code
end
end
end
def self.give_promotion_same_product(qty, promoqty, foc_min_qty)
multiple = qty / promoqty # loop count
charge_qty = 0
foc_qty = 0
if multiple > 0
multiple.times.each do |key|
if qty >= promoqty
charge_qty += promoqty
different = qty - promoqty
qty = different
if different == 0
Shop.add_promotion_item
foc_qty += foc_min_qty
else
foc_qty += foc_min_qty
qty = qty - foc_min_qty
end
else
if multiple == foc_qty
charge_qty += qty
else
charge_qty += qty
end
end
end
else
charge_qty = qty
end
puts "Charged - " + charge_qty.to_s
puts "FOC - " + foc_qty.to_s
end
def find_promo_item_in_orderlist(promo_item_code, orderitemList)
orderitemList.each do |item|
if item.item_code == promo_item_code
give_promotion_second_product(item)
else
add_promotion_second_item
end
end
end
def give_promotion_second_product(item, foc_min_qty)
if item.qty > foc_min_qty
else
end
end
def self.add_promotion_item
end
def self.add_promotion_second_item
end
end