Files
sx-fc/app/controllers/origami/discounts_controller.rb
2017-09-18 13:53:28 +06:30

264 lines
9.7 KiB
Ruby

class Origami::DiscountsController < BaseOrigamiController
authorize_resource :class => false
#discount page show from origami index with selected order
def index
sale_id = params[:id]
if Sale.exists?(sale_id)
@sale_data = Sale.find(sale_id)
end
@member_discount = MembershipSetting.find_by_discount(1)
@accounts = Account.all
end
#discount page show from origami index with selected order
def create
sale_id = params[:sale_id]
discount_items = JSON.parse(params[:discount_items])
overall_discount = params[:overall_discount]
sub_total = params[:sub_total]
if Sale.exists?(sale_id)
sale = Sale.find(sale_id)
table_id = sale.bookings[0].dining_facility_id
table_type = DiningFacility.find(table_id).type
# sale.total_discount = overall_discount.to_f
# sale.total_amount = sub_total.to_f
# sale.grand_total = (sub_total.to_f - overall_discount.to_f) + sale.total_tax;
# sale.save
if discount_items.length > 0
#save sale item for discount
discount_items.each do |di|
origin_sale_item = SaleItem.find(di["id"])
sale_item = SaleItem.new
sale_item.sale_id = sale_id
sale_item.product_code = origin_sale_item != nil ? origin_sale_item.product_code : sale_id
sale_item.product_name = di["name"]
sale_item.product_alt_name = ""
sale_item.remark = "Discount"
sale_item.qty = 1
sale_item.unit_price = di["price"]
sale_item.taxable_price = di["price"]
sale_item.is_taxable = 0
sale_item.account_id = origin_sale_item.account_id
sale_item.price = di["price"]
sale_item.save
end
end
# Re-calc All Amount in Sale
sale.compute_by_sale_items(sale_id, sale.sale_items, overall_discount.to_f)
result = {:status=> "Success", :table_id => table_id, :table_type => table_type }
else
result = {:status=> "Please, Check Again!", :table_id => table_id, :table_type => table_type }
end
render :json => result.to_json
end
# Remove selected discount Items
def remove_discount_items
sale_id = params[:sale_id]
discount_items = JSON.parse(params[:discount_items])
if Sale.exists?(sale_id)
sale = Sale.find(sale_id)
table_id = sale.bookings[0].dining_facility_id
table_type = DiningFacility.find(table_id).type
if discount_items.length > 0
#destroy sale item for discount
discount_items.each do |di|
sale_item = SaleItem.find(di["id"])
sale.total_amount = (sale.total_amount + sale_item.price.abs)
sale_item.destroy
end
end
# sale.grand_total = (sale.total_amount - sale.total_discount) + sale.total_tax;
# sale.save
# Re-calc All Amount in Sale
sale.compute_by_sale_items(sale_id, sale.sale_items, sale.total_discount)
result = {:status=> "Success", :table_id => table_id, :table_type => table_type }
else
result = {:status=> "Please, Check Again!", :table_id => table_id, :table_type => table_type }
end
render :json => result.to_json
end
# Remove all discount Items
def remove_all_discount
sale_id = params[:id]
if Sale.exists?(sale_id)
sale = Sale.find(sale_id)
table_id = sale.bookings[0].dining_facility_id
table_type = DiningFacility.find(table_id).type
discount_items = []
#destroy all discount sale item
sale.sale_items.each do |si|
if si.remark == "Discount" && si.price < 0
sale.total_amount = (sale.total_amount + si.price.abs)
discount_items.push(si)
end
end
# sale.total_discount = 0
# sale.grand_total = (sale.total_amount - sale.total_discount) + sale.total_tax;
# sale.save
#destroy in sale.sale_items
sale.sale_items.destroy(discount_items)
# Re-calc All Amount in Sale
sale.compute_by_sale_items(sale_id, sale.sale_items, 0)
result = {:status=> "Success", :table_id => table_id, :table_type => table_type }
else
result = {:status=> "Please, Check Again!", :table_id => table_id, :table_type => table_type }
end
render :json => result.to_json
end
# Member Discount
def member_discount
sale_id = params[:sale_id]
is_card = params[:is_card]
sub_total = params[:sub_total]
sale = Sale.find(sale_id)
# Check for Card Payment
is_card_payment = SaleItem.get_sale_payments_by_card(sale.sale_payments)
if is_card_payment != true
account_types = Account.where("discount=?",true)
table_id = sale.bookings[0].dining_facility_id
table_type = DiningFacility.find(table_id).type
# Get Prices for each accounts (eg: food, beverage)
account_price = SaleItem.calculate_price_by_accounts(sale.sale_items)
acc_prices = Array.new;
account_types.each do |at|
account_price.each do |pc|
if pc[:name] == at.title && pc[:price]>0
str={type:pc[:name],amount:pc[:price]}
acc_prices.push(str)
end
end
end
generic_customer_id = sale.customer.membership_id
receipt_no = sale.receipt_no
membership = MembershipSetting.find_by_membership_type("paypar_url")
memberaction = MembershipAction.find_by_membership_type("member_discount")
merchant_uid = memberaction.merchant_account_id.to_s
campaign_type_id = memberaction.additional_parameter["campaign_type_id"]
auth_token = memberaction.auth_token.to_s
url = membership.gateway_url.to_s + memberaction.gateway_url.to_s
# Check for items for Paypar Cloud
if acc_prices.length > 0
begin
response = HTTParty.post(url,
:body => { account_no: nil,
generic_customer_id:generic_customer_id ,
campaign_type_id: campaign_type_id,
receipt_no: receipt_no,
merchant_uid:merchant_uid,
campaign_method:acc_prices.to_json,
total_sale_transaction_amount: sale.grand_total,
is_card: is_card,
auth_token:auth_token}.to_json,
:headers => {
'Content-Type' => 'application/json',
'Accept' => 'application/json'
}, :timeout => 10)
rescue HTTParty::Error
response = {"status": false, "message": "Can't open membership server " }
rescue Net::OpenTimeout
response = {"status": false, "message": "Can't open membership server " }
rescue OpenURI::HTTPError
response = {"status": false, "message": "Can't open membership server " }
rescue SocketError
response = {"status": false, "message": "Can't open membership server " }
end
else
response = {"status": false, "message": "You have no selected discount item" }
end
# Re-calc All Amount in Sale
if response["status"] == true
discount_amount = response["discount_earned"]
if response["discount_bonus_earned"]
discount_amount = discount_amount + response["discount_bonus_earned"]
end
sale.compute_by_sale_items(sale_id, sale.sale_items, discount_amount, 'member_discount')
result = {:status=> "Success",:title=>"Member Discount", :table_id => table_id,:table_type => table_type }
elsif response["status"] == "500"
result = {:status=> response["error"],:title=>"Alert", :table_id => table_id,:table_type => table_type }
else
result = {:status=> response["message"],:title=>"Alert", :table_id => table_id,:table_type => table_type }
end
render :json => result.to_json
end #end Is Card Payment
end
#discount for selected order
# def create
# sale_id = params[:sale_id]
# sale_item_id = params[:sale_item_id]
# discount_type = params[:discount_type]
# discount_value = params[:discount_value]
# discount_amount = params[:discount_amount]
# grand_total = params[:grand_total]
# product_name = "Overall Discount"
# if discount_type == 0
# remark="Discount " + discount_amount + " as net"
# else
# remark="Discount " + discount_amount + " as percentage"
# end
# #update discount for sale
# sale = Sale.find(sale_id)
# sale.total_discount = sale.total_discount + discount_amount.to_f
# sale.grand_total = grand_total
# sale.save
# #save sale item for discount
# if sale_item_id != nil
# origin_sale_item = SaleItem.find(sale_item_id)
# product_name = origin_sale_item.product_name + "-Disocunt"
# end
# sale_item = SaleItem.new
# #pull
# sale_item.sale_id = sale_id
# sale_item.product_code = origin_sale_item != nil ? origin_sale_item.product_code : sale_id
# sale_item.product_name = product_name
# sale_item.remark = remark
# sale_item.qty = 1
# sale_item.unit_price = (0-discount_amount.to_f)
# sale_item.taxable_price = discount_amount
# sale_item.is_taxable = 0
# sale_item.price = sale_item.qty * sale_item.unit_price
# sale_item.save
# redirect_to origami_path(sale_id)
# end
end