Add payment structure

This commit is contained in:
2015-04-29 10:21:12 +02:00
parent 9d5e3f684c
commit e4e7198af5
8 changed files with 59 additions and 5 deletions
+6 -5
View File
@@ -44,11 +44,12 @@ class Employee
end
alias_method :settings=, :enrich_with_settings
alias_method :orig_save, :save
def save(*args)
settings.persist!
orig_save(*args)
end
#alias_method :orig_save, :save
#def save(*args)
#settings.persist!
#orig_save(*args)
#end
before_validation(on: :save){ settings.persist! }
def settings
@settings || SupplierEmployeesSettings.new(Supplier.new).for_employee(self)
+5
View File
@@ -17,6 +17,7 @@ class List
belongs_to :table
belongs_to :supplier
belongs_to :section
has_many :list_payments
has_and_belongs_to_many :users, storing_keys: true
has_and_belongs_to_many :employees, storing_keys: true
@@ -274,6 +275,10 @@ class List
state == 'active'
end
def closed?
state == 'closed'
end
def place_order(product_orders: [], user: nil, employee: nil)
return false unless product_orders.any?
order = Order.create list: self, supplier: supplier, user: user, employee: employee, section_id: section_id, table_id: table_id
+16
View File
@@ -0,0 +1,16 @@
class ListPayment
include SimplyStored::Couch
include ActiveModel::SerializerSupport
property :amount
property :source # bitcoin, apple_pay, etc...
belongs_to :list
belongs_to :user
after_save :close_list_when_is_paid
validates :list_id, presence: true
def close_list_when_is_paid
amount_paid = list.list_payments.inject(0.0){|sum, payment| sum + payment.amount}
list.close! if amount_paid >= list.price
end
end
+4
View File
@@ -20,6 +20,10 @@ class Supplier
property :week_starts_on_monday, type: :boolean, default: true
property :employee_settings_storage
# PAYMENT
property :accept_bitpay, type: :boolean, default: false
property :bitpay_number
#LOCATION
property :lat, type: Float #, default: 52.08062426379751
property :lng, type: Float #, default: 4.312562942504883
+1
View File
@@ -19,6 +19,7 @@ class User
has_and_belongs_to_many :lists, storing_keys: false
has_many :orders
has_many :list_payments
validates_uniqueness_of :email
before_save :ensure_authentication_token
+5
View File
@@ -0,0 +1,5 @@
FactoryGirl.define do
factory :list_payment do
end
end
+21
View File
@@ -0,0 +1,21 @@
require 'spec_helper'
describe ListPayment do
it "does completes a list if amount is equal to the list amount" do
list = create :list, price: 10.22
create :list_payment, amount: 10.22, list: list
list.should be_closed
end
it "does completes a list if amount is greater than the list amount" do
list = create :list, price: 10.22
create :list_payment, amount: 12, list: list
list.should be_closed
end
it "does completes a list if amount is less than the list amount, but total payments on list bigger" do
list = create :list, price: 10.22
create :list_payment, amount: 7, list: list
list.should be_active
create :list_payment, amount: 7, list: list
list.should be_closed
end
end
+1
View File
@@ -106,6 +106,7 @@ RSpec.configure do |config|
config.infer_base_class_for_anonymous_controllers = true
config.filter_run_excluding broken: true
config.render_views = true
config.expect_with(:rspec) { |c| c.syntax = [:expect, :should] }
OmniAuth.config.test_mode = true
OmniAuth.config.add_mock :facebook, {