diff --git a/app/controllers/api/bill_controller.rb b/app/controllers/api/bill_controller.rb index eba40816..2b323d0f 100644 --- a/app/controllers/api/bill_controller.rb +++ b/app/controllers/api/bill_controller.rb @@ -24,6 +24,7 @@ class Api::BillController < Api::ApiController @sale = Sale.new @status, @sale_id = @sale.generate_invoice_from_order(params[:order_id], current_login_employee, get_cashier) end + Promotion.promo_activate(@sale) else @status = false @error_message = "No Current Open Shift" diff --git a/app/controllers/settings/menu_item_instances_controller.rb b/app/controllers/settings/menu_item_instances_controller.rb index 0d276602..fbe1b672 100644 --- a/app/controllers/settings/menu_item_instances_controller.rb +++ b/app/controllers/settings/menu_item_instances_controller.rb @@ -55,7 +55,7 @@ class Settings::MenuItemInstancesController < ApplicationController sets = ItemSet.find(params[:menu_item_instance][:item_sets]) if sets.count > 0 - @settings_menu_item.item_sets = sets + @settings_menu_item_instances.item_sets = sets end end @@ -92,7 +92,7 @@ class Settings::MenuItemInstancesController < ApplicationController sets = ItemSet.find(params[:menu_item_instance][:item_sets]) if sets.count > 0 - @settings_menu_item.item_sets = sets + @settings_menu_item_instances.item_sets = sets end end diff --git a/app/controllers/settings/set_menu_items_controller.rb b/app/controllers/settings/set_menu_items_controller.rb index 4f451de5..a9e91a94 100644 --- a/app/controllers/settings/set_menu_items_controller.rb +++ b/app/controllers/settings/set_menu_items_controller.rb @@ -132,6 +132,6 @@ class Settings::SetMenuItemsController < ApplicationController # Never trust parameters from the scary internet, only allow the white list through. def settings_menu_item_params - params.require(:set_menu_item).permit(:item_code, :name, :alt_name, :type, :image_path, :menu_category_id,:account_id , :item_attributes, :item_options, :min_qty, :is_sub_item, :is_available, :created_by, :item_sets) + params.require(:set_menu_item).permit(:item_code, :name, :alt_name, :type, :image_path, :menu_category_id,:account_id , :item_attributes, :item_options, :min_qty, :is_sub_item, :is_available, :created_by, :item_sets, :unit) end end diff --git a/app/controllers/settings/simple_menu_items_controller.rb b/app/controllers/settings/simple_menu_items_controller.rb index 4cf9318c..418cc2b8 100644 --- a/app/controllers/settings/simple_menu_items_controller.rb +++ b/app/controllers/settings/simple_menu_items_controller.rb @@ -154,6 +154,6 @@ class Settings::SimpleMenuItemsController < ApplicationController # Never trust parameters from the scary internet, only allow the white list through. def settings_menu_item_params - params.require(:simple_menu_item).permit(:item_code, :name, :alt_name, :type, :image_path, :menu_category_id, :account_id, :item_attributes, :item_options, :min_qty, :is_sub_item, :is_available, :created_by, :item_sets) + params.require(:simple_menu_item).permit(:item_code, :name, :alt_name, :type, :image_path, :menu_category_id, :account_id, :item_attributes, :item_options, :min_qty, :is_sub_item, :is_available, :created_by, :item_sets, :unit) end end diff --git a/app/models/promotion.rb b/app/models/promotion.rb index b255ceec..ed727038 100644 --- a/app/models/promotion.rb +++ b/app/models/promotion.rb @@ -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 diff --git a/app/models/sale.rb b/app/models/sale.rb index 71994ca1..9927251d 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -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) diff --git a/app/models/shop.rb b/app/models/shop.rb index dbb0930e..24f4bc1e 100644 --- a/app/models/shop.rb +++ b/app/models/shop.rb @@ -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 diff --git a/app/pdf/crm_order_pdf.rb b/app/pdf/crm_order_pdf.rb index a7f85098..22a381ca 100644 --- a/app/pdf/crm_order_pdf.rb +++ b/app/pdf/crm_order_pdf.rb @@ -1,8 +1,8 @@ class CrmOrderPdf < Prawn::Document attr_accessor :receipt_width,:price_column_width,:p_width, :page_height, :margin, :price_width, :item_width, :header_font_size, :item_font_size,:item_height,:qty_column_width,:item_description_width def initialize(booking,order_items,printer_settings) - self.page_width = PrintSetting.where("name = ?","CRM Order").first.page_width - self.page_height = PrintSetting.where("name = ?","CRM Order").first.page_height + self.page_width = printer_settings.page_width + self.page_height = printer_settings.page_height self.margin = 10 # self.price_width = self.p_width / 2 self.price_width=80 diff --git a/app/pdf/order_item_pdf.rb b/app/pdf/order_item_pdf.rb index 7b2e350b..64df3adb 100644 --- a/app/pdf/order_item_pdf.rb +++ b/app/pdf/order_item_pdf.rb @@ -2,8 +2,8 @@ class OrderItemPdf < Prawn::Document include ActionView::Helpers::NumberHelper attr_accessor :label_width,:price_column_width,:page_width, :page_height, :margin, :price_width, :item_width, :header_font_size, :item_font_size,:item_height,:qty_width,:total_width,:item_description_width def initialize(print_settings,order_item, print_status, options, alt_name) - self.page_width = PrintSetting.where("name = ?","OrderItemPdf").first.page_width - self.page_height = PrintSetting.where("name = ?","OrderItemPdf").first.page_height + self.page_width = print_settings.page_width + self.page_height = print_settings.page_height self.margin = 0 self.price_width = 40 # No Need for item self.qty_width = 40 diff --git a/app/pdf/order_summary_pdf.rb b/app/pdf/order_summary_pdf.rb index 902c68ad..d23f2677 100644 --- a/app/pdf/order_summary_pdf.rb +++ b/app/pdf/order_summary_pdf.rb @@ -2,8 +2,8 @@ class OrderSummaryPdf < Prawn::Document include ActionView::Helpers::NumberHelper attr_accessor :label_width,:price_column_width,:page_width, :page_height, :margin, :price_width, :item_width, :header_font_size, :item_font_size,:item_height,:qty_width,:total_width,:item_description_width def initialize(print_settings,order, print_status, order_items = nil,alt_name) - self.page_width = PrintSetting.where("name = ?","Order Summary").first.page_width - self.page_height = PrintSetting.where("name = ?","Order Summary").first.page_height + self.page_width = print_settings.page_width + self.page_height = print_settings.page_height self.margin = 0 self.price_width = 40 # No Need for item self.qty_width = 40 diff --git a/app/pdf/queue_no_pdf.rb b/app/pdf/queue_no_pdf.rb index cda410ca..49e13bd9 100644 --- a/app/pdf/queue_no_pdf.rb +++ b/app/pdf/queue_no_pdf.rb @@ -1,8 +1,8 @@ class QueueNoPdf < Prawn::Document attr_accessor :label_width,:price_column_width,:page_width, :page_height, :margin, :price_width, :item_width, :header_font_size, :item_font_size,:item_height,:qty_width,:total_width,:item_description_width def initialize(printer_settings, queue) - self.page_width = PrintSetting.where("name = ?","Queue No").first.page_width - self.page_height = PrintSetting.where("name = ?","Queue No").first.page_height + self.page_width = printer_settings.page_width + self.page_height = printer_settings.page_height self.margin = 5 self.price_width = 35 self.qty_width = 20 diff --git a/app/pdf/receipt_bill_pdf.rb b/app/pdf/receipt_bill_pdf.rb index 809f7678..1d9b3f4d 100644 --- a/app/pdf/receipt_bill_pdf.rb +++ b/app/pdf/receipt_bill_pdf.rb @@ -2,8 +2,8 @@ class ReceiptBillPdf < Prawn::Document include ActionView::Helpers::NumberHelper attr_accessor :label_width,:price_column_width,:page_width, :page_height, :margin, :price_width, :item_width, :header_font_size, :item_font_size,:item_height,:qty_width,:total_width,:item_description_width, :description_width, :price_num_width def initialize(printer_settings, sale_items, sale_data, customer_name, item_price_by_accounts, discount_price_by_accounts, member_info = nil,rebate_amount = nil,shop_details, printed_status) - self.page_width = PrintSetting.where("name = ?","Receipt Bill").first.page_width - self.page_height = PrintSetting.where("name = ?","Receipt Bill").first.page_height + self.page_width = printer_settings.page_width + self.page_height = printer_settings.page_height self.margin = 5 self.price_width = 40 self.qty_width = 20 diff --git a/app/views/api/restaurant/menu/_menu_item.json.jbuilder b/app/views/api/restaurant/menu/_menu_item.json.jbuilder index cd8648db..7a450850 100644 --- a/app/views/api/restaurant/menu/_menu_item.json.jbuilder +++ b/app/views/api/restaurant/menu/_menu_item.json.jbuilder @@ -35,6 +35,7 @@ json.account_id item.account_id json.min_qty item.min_qty json.is_available item.is_available json.is_sub_item item.is_sub_item +json.unit item.unit json.item_sets item.item_sets json.attributes attr_format json.options item.item_options diff --git a/app/views/home/dashboard.html.erb b/app/views/home/dashboard.html.erb index 025cfae9..08991091 100644 --- a/app/views/home/dashboard.html.erb +++ b/app/views/home/dashboard.html.erb @@ -251,7 +251,7 @@ font-weight: 400; text-transform: uppercase; transition: color 0.3s ease; - line-height: 48px; + /*line-height: 48px;*/ } .card .card-content { @@ -259,7 +259,7 @@ border-radius: 0 0 2px 2px; background-clip: padding-box; box-sizing: border-box; - height: 120px; + /*height: 120px;*/ transition: color 0.3s ease; text-align: center; } diff --git a/app/views/origami/home/index.html.erb b/app/views/origami/home/index.html.erb index 05094504..2742f249 100644 --- a/app/views/origami/home/index.html.erb +++ b/app/views/origami/home/index.html.erb @@ -108,17 +108,15 @@ - <%if current_login_employee.role == "cashier" && @shop.quick_sale_summary == true%> - - <%end%> + - - - <%if current_login_employee.role == "administrator" || current_login_employee.role == "manager" %> - <%end%> - + diff --git a/app/views/origami/home/show.html.erb b/app/views/origami/home/show.html.erb index 1e39bd8d..dfaa9e45 100644 --- a/app/views/origami/home/show.html.erb +++ b/app/views/origami/home/show.html.erb @@ -310,39 +310,39 @@ - -
- + +
+ - - - <% if @dining.bookings.length >= 1 %> - - <% if @status_order == 'order' && @status_sale != 'sale' %> - - - - - - - - - <% else %> - - - - - - - - - - <% end %> - + + + <% if @dining.bookings.length >= 1 %> + <% if @status_order == 'order' && @status_sale != 'sale' %> + + + + + + + + + + <% else %> + + + + + + + + + + <% end %> + - - <% end %> -
+ + <% end %> +