Merge branch 'crm' of bitbucket.org:code2lab/sxrestaurant

This commit is contained in:
Yan
2017-06-22 09:51:22 +06:30
53 changed files with 1580 additions and 574 deletions

33
app/models/ability.rb Normal file
View File

@@ -0,0 +1,33 @@
class Ability
include CanCan::Ability
def initialize(user)
user ||= Employee.new
if user.role? :administrator
can :manage, :all
elsif user.role? :cashier
can :read, Order
can :update, Order
can :completed_order_item, Order
can :read, Sale
can :update, Sale
elsif user.role? :accountant
can :read, Order
can :update, Order
can :completed_order_item, Order
can :read, Sale
can :update, Sale
can :manual_complete_sale, Sale
end
end
end

View File

@@ -6,9 +6,10 @@ class Customer < ApplicationRecord
has_many :orders
has_many :sales
validates_presence_of :name, :contact_no, :email
validates_presence_of :name, :contact_no, :email,:company,:card_no
validates :contact_no, uniqueness: true
validates :email, uniqueness: true
validates :card_no, uniqueness: true
paginates_per 50
@@ -36,52 +37,52 @@ class Customer < ApplicationRecord
end
def self.get_membership_transactions(customer)
membership = MembershipSetting.find_by_membership_type("paypar_url")
memberaction = MembershipAction.find_by_membership_type("get_member_transactions")
merchant_uid = memberaction.merchant_account_id.to_s
auth_token = memberaction.auth_token.to_s
url = membership.gateway_url.to_s + memberaction.gateway_url.to_s
# urltest =self.url_exist?(url)
begin
response = HTTParty.get(url, :body => { membership_id: customer.membership_id,merchant_uid:merchant_uid,auth_token:auth_token}.to_json,
:headers => {
'Content-Type' => 'application/json',
'Accept' => 'application/json'
},
:timeout => 10
)
rescue Net::OpenTimeout
response = { status: false }
end
return response;
end
def self.search(search)
if search
# find(:all, :conditions => ['name LIKE ? OR contact_no LIKE ?', "%#{search}%", "%#{search}%"])
where("name LIKE ? OR contact_no LIKE ?", "%#{search}%", "%#{search}%",)
where("name LIKE ? OR contact_no LIKE ? OR card_no LIKE ?", "%#{search}%", "%#{search}%", "%#{search}%",)
else
find(:all)
end
end
# require "net/http"
# def self.url_exist?(url_string)
# url = URI.parse(url_string)
# req = Net::HTTP.new(url.host, url.port)
# puts "hhhhhhhhhhhh"
# puts req.to_json
# req.use_ssl = (url.scheme == 'https')
# puts "aaaaaaaaaaaa"
# puts req.use_ssl?
# path = url.path if url.path.present?
# puts "bbbbbbbbbbbbb"
# puts path
# res = req.request_head(path || '/')
# puts "cccccccccccccc"
# puts res.to_json
# puts "ddddddddd"
# puts res.kind_of?(Net::HTTPRedirection)
# if res.kind_of?(Net::HTTPRedirection)
# url_exist?(res['location']) # Go after any redirect and make sure you can access the redirected URL
# else
# ! %W(4 5).include?(res.code[0]) # Not from 4xx or 5xx families
# end
# rescue Errno::ENOENT
# false #false if can't find the server
# end
# def self.search(search)
# where("name LIKE ? OR contact_no LIKE ?", "%#{search}%", "%#{search}%",)
# end
def lastest_invoices
sales.where(:customer_id => self.id).order("created_at desc").limit(5)
end
def self.count_customer
all = self.all.count+1
count = all-2
end
WALKIN = "CUS-000000000001"
TAKEAWAY = "CUS-000000000002"
private
def generate_custom_id
self.customer_id = SeedGenerator.generate_id(self.class.name, "CUS")

View File

@@ -65,11 +65,11 @@ class Printer::ReceiptPrinter < Printer::PrinterWorker
end
#Bill Receipt Print
def print_receipt_bill(printer_settings,sale_items,sale_data, customer_name, food_total, beverage_total, member_info = nil)
def print_receipt_bill(printer_settings,sale_items,sale_data, customer_name, food_total, beverage_total, member_info = nil,rebate_amount=nil)
#Use CUPS service
#Generate PDF
#Print
pdf = ReceiptBillPdf.new(printer_settings, sale_items, sale_data, customer_name, food_total, beverage_total, member_info)
pdf = ReceiptBillPdf.new(printer_settings, sale_items, sale_data, customer_name, food_total, beverage_total, member_info,rebate_amount)
pdf.render_file "tmp/receipt_bill.pdf"
self.print("tmp/receipt_bill.pdf")

View File

@@ -17,6 +17,14 @@ class Sale < ApplicationRecord
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.end_of_day}' AND '#{DateTime.now.utc.beginning_of_day}'") }
REPORT_TYPE = {
"daily" => 0,
"monthly" => 1,
"yearly" => 2
}
SALE_STATUS_COMPLETED = "completed"
def generate_invoice_from_booking(booking_id, requested_by)
booking = Booking.find(booking_id)
status = false
@@ -336,6 +344,40 @@ class Sale < ApplicationRecord
return daily_total
end
def self.get_by_range_by_saleitems(from,to,status,report_type)
query = Sale.select("
mi.item_code as code,(SUM(i.qty) * i.unit_price) as grand_total,
SUM(i.qty) as total_item," +
" i.unit_price as unit_price,
mi.name as product_name,
mc.name as menu_category_name,
mc.id as menu_category_id ")
.group('mi.id')
.order("mi.menu_category_id")
query = query.joins("JOIN sale_items i ON i.sale_id = sales.sale_id
JOIN menu_items mi ON i.product_code = mi.item_code" +
" JOIN menu_categories mc ON mc.id = mi.menu_category_id
JOIN employees ea ON ea.id = sales.cashier_id")
query = query.where("receipt_date between ? and ? and sale_status=?",from,to,status)
case report_type.to_i
when REPORT_TYPE["daily"]
return query
when REPORT_TYPE["monthly"]
return query.group("MONTH(date)")
when REPORT_TYPE["yearly"]
return query.group("YEAR(date)")
end
end
private
def generate_custom_id

View File

@@ -54,8 +54,8 @@ class SalePayment < ApplicationRecord
#record an payment in sale-audit
remark = "Payment #{payment_method}- for Invoice #{invoice.receipt_no} Due [#{amount_due}]| pay amount -> #{cash_amount} | Payment Status ->#{payment_status}"
sale_audit = SaleAudit.record_payment(invoice.id, remark, action_by)
return true, self.sale
return true, self.save
else
#record an payment in sale-audit
remark = "No outstanding Amount - Grand Total [#{invoice.grand_total}] | Due [#{amount_due}] | Paid [#{invoice.amount_received}]"
@@ -84,12 +84,15 @@ class SalePayment < ApplicationRecord
def self.redeem(paypar_url,token,membership_id,received_amount,sale_id)
membership_actions_data = MembershipAction.find_by_membership_type("redeem");
if !membership_actions_data.nil?
url = paypar_url.to_s + membership_actions_data.gateway_url.to_s
merchant_uid = membership_actions_data.merchant_account_id
auth_token = membership_actions_data.auth_token
campaign_type_id = membership_actions_data.additional_parameter["campaign_type_id"]
sale_data = Sale.find_by_sale_id(sale_id)
if sale_data
# Control for Paypar Cloud
begin
@@ -199,6 +202,10 @@ class SalePayment < ApplicationRecord
customer_data = Customer.find_by_customer_id(self.sale.customer_id)
membership_setting = MembershipSetting.find_by_membership_type("paypar_url")
membership_data = SalePayment.redeem(membership_setting.gateway_url,membership_setting.auth_token,customer_data.membership_id,self.received_amount,self.sale.sale_id)
puts 'mmmmmmmmmmmmmmmmmmmmmmmmmmm'
puts membership_data.to_json
puts "amountttttttttttttttttttttt"
puts self.received_amount
if membership_data["status"]==true
self.payment_method = "paypar"
self.payment_amount = self.received_amount
@@ -257,7 +264,7 @@ class SalePayment < ApplicationRecord
def rebat(sObj)
rebate_prices = SaleItem.calculate_food_beverage(sObj.sale_items)
puts "rebate_prices"
puts "eeeeeeeeeeeeeeeeeeeeeeee"
puts rebate_prices
generic_customer_id = sObj.customer.membership_id
if generic_customer_id != nil || generic_customer_id != "" || generic_customer_id != 0
@@ -292,8 +299,8 @@ class SalePayment < ApplicationRecord
rescue Net::OpenTimeout
response = { status: false }
end
# puts response.to_json
return response
# puts response.to_json
end
end
end