diff --git a/app/models/employee.rb b/app/models/employee.rb index 596e25e0..491e7c3d 100644 --- a/app/models/employee.rb +++ b/app/models/employee.rb @@ -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) diff --git a/app/models/list.rb b/app/models/list.rb index f9b82de4..a9ee56a2 100644 --- a/app/models/list.rb +++ b/app/models/list.rb @@ -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 diff --git a/app/models/list_payment.rb b/app/models/list_payment.rb new file mode 100644 index 00000000..85e136a3 --- /dev/null +++ b/app/models/list_payment.rb @@ -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 diff --git a/app/models/supplier.rb b/app/models/supplier.rb index 9fd99bc4..999343b9 100644 --- a/app/models/supplier.rb +++ b/app/models/supplier.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 51035670..11f38fca 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/spec/factories/list_payment.rb b/spec/factories/list_payment.rb new file mode 100644 index 00000000..f1458303 --- /dev/null +++ b/spec/factories/list_payment.rb @@ -0,0 +1,5 @@ +FactoryGirl.define do + factory :list_payment do + + end +end diff --git a/spec/models/list_payment_spec.rb b/spec/models/list_payment_spec.rb new file mode 100644 index 00000000..fde2dbaf --- /dev/null +++ b/spec/models/list_payment_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index bdc8fc5a..74e7d3d0 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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, {