Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # == Schema Information
- #
- # Table name: orders
- #
- # id :integer not null, primary key
- # eid :string(255)
- # location_id :integer
- # customer_id :integer
- # created_at :datetime
- # creator_client_id :integer
- # created_by :string(255)
- # service_type :string(255)
- # expected_time :datetime
- # confirmed_time :datetime
- # status :string(255)
- # customer_notes :text(65535)
- # total :string(255)
- # total_discrepancy :string(255)
- # c_email :string(255)
- # c_first_name :string(255)
- # c_last_name :string(255)
- # c_company_name :string(255)
- # c_phone :string(255)
- # c_address_1 :string(255)
- # c_address_2 :string(255)
- # c_postal_code :string(255)
- # c_city :string(255)
- # c_state :string(255)
- # c_country :string(255)
- # c_latitude :decimal(9, 6)
- # c_longitude :decimal(9, 6)
- # c_delivery_notes :text(65535)
- # payment_discrepancy :string(255)
- #
- # Indexes
- #
- # index_orders_on_customer_id (customer_id)
- # index_orders_on_location_id (location_id)
- #
- class HrOrder < HrModel
- self.table_name = 'orders'
- STATUSES = [
- NEW_STATUS = 'new',
- RECEIVED_STATUS = 'received',
- ACCEPTED_STATUS = 'accepted',
- IN_PREPARATION_STATUS = 'in_preparation',
- AWAITING_SHIPMENT_STATUS = 'awaiting_shipment',
- AWAITING_COLLECTION_STATUS = 'awaiting_collection',
- IN_DELIVERY_STATUS = 'in_delivery',
- COMPLETED_STATUS = 'completed',
- REJECTED_STATUS = 'rejected',
- CANCELLED_STATUS = 'cancelled',
- DELIVERY_FAILED_STATUS = 'delivery_failed',
- ]
- belongs_to :hr_location, foreign_key: :location_id
- belongs_to :hr_customer, foreign_key: :customer_id
- has_one :hr_account, through: :hr_location
- with_options foreign_key: :order_id, dependent: :destroy, inverse_of: :hr_order do
- has_many :hr_order_deals
- has_many :hr_order_items
- has_many :hr_order_charges
- has_many :hr_order_discounts
- has_many :hr_order_payments
- has_many :hr_loyalty_operations
- has_many :hr_private_refs, class_name: HrOrderPrivateRef
- end
- has_many :hr_connection_logs, as: :resource
- belongs_to :hr_client, foreign_key: :creator_client_id
- accepts_nested_attributes_for :hr_order_items,
- :hr_order_charges,
- :hr_order_deals,
- :hr_order_discounts,
- :hr_order_payments,
- :hr_private_refs,
- :hr_loyalty_operations
- money_accessor :total
- money_accessor :total_discrepancy
- money_accessor :payment_discrepancy
- validates :total, money: true
- validates_presence_of :hr_location
- validates_inclusion_of :status, in: STATUSES
- validates_length_of :c_email,
- :c_first_name,
- :c_last_name,
- :c_company_name,
- :c_phone,
- :c_address_1,
- :c_address_2,
- :c_postal_code,
- :c_city,
- :c_state,
- :c_country, maximum: 255
- validates_length_of :customer_notes,
- :c_delivery_notes, maximum: 65355
- include PrivateReferable
- scope :with_private_ref, ->(hr_client, hr_location, private_ref) {
- joins(:hr_private_refs).where(
- 'order_private_refs.client_id = ? && order_private_refs.location_id = ? && order_private_refs.private_ref = ?',
- hr_client.id, hr_location.id, private_ref
- )
- }
- delegate :hr_observing_callbacks, to: :hr_location
- delegate :currency, to: :hr_account
- def recalculate_total
- items_total = hr_order_items.map(&:subtotal_m).inject(hr_location.hr_account.null_money, &:+)
- charges_total = hr_order_charges.map(&:charge_price_m).inject(hr_location.hr_account.null_money, &:+)
- total = items_total + charges_total
- final_total = hr_order_discounts.inject(total) do |total, hr_order_discount|
- hr_order_discount.discount.apply(total)
- end
- self.total = final_total.to_s
- end
- end
Add Comment
Please, Sign In to add comment