Guest User

Untitled

a guest
Nov 23rd, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. # == Schema Information
  2. #
  3. # Table name: orders
  4. #
  5. # id :integer not null, primary key
  6. # eid :string(255)
  7. # location_id :integer
  8. # customer_id :integer
  9. # created_at :datetime
  10. # creator_client_id :integer
  11. # created_by :string(255)
  12. # service_type :string(255)
  13. # expected_time :datetime
  14. # confirmed_time :datetime
  15. # status :string(255)
  16. # customer_notes :text(65535)
  17. # total :string(255)
  18. # total_discrepancy :string(255)
  19. # c_email :string(255)
  20. # c_first_name :string(255)
  21. # c_last_name :string(255)
  22. # c_company_name :string(255)
  23. # c_phone :string(255)
  24. # c_address_1 :string(255)
  25. # c_address_2 :string(255)
  26. # c_postal_code :string(255)
  27. # c_city :string(255)
  28. # c_state :string(255)
  29. # c_country :string(255)
  30. # c_latitude :decimal(9, 6)
  31. # c_longitude :decimal(9, 6)
  32. # c_delivery_notes :text(65535)
  33. # payment_discrepancy :string(255)
  34. #
  35. # Indexes
  36. #
  37. # index_orders_on_customer_id (customer_id)
  38. # index_orders_on_location_id (location_id)
  39. #
  40.  
  41. class HrOrder < HrModel
  42. self.table_name = 'orders'
  43.  
  44. STATUSES = [
  45. NEW_STATUS = 'new',
  46. RECEIVED_STATUS = 'received',
  47. ACCEPTED_STATUS = 'accepted',
  48. IN_PREPARATION_STATUS = 'in_preparation',
  49. AWAITING_SHIPMENT_STATUS = 'awaiting_shipment',
  50. AWAITING_COLLECTION_STATUS = 'awaiting_collection',
  51. IN_DELIVERY_STATUS = 'in_delivery',
  52. COMPLETED_STATUS = 'completed',
  53. REJECTED_STATUS = 'rejected',
  54. CANCELLED_STATUS = 'cancelled',
  55. DELIVERY_FAILED_STATUS = 'delivery_failed',
  56. ]
  57.  
  58. belongs_to :hr_location, foreign_key: :location_id
  59. belongs_to :hr_customer, foreign_key: :customer_id
  60.  
  61. has_one :hr_account, through: :hr_location
  62.  
  63. with_options foreign_key: :order_id, dependent: :destroy, inverse_of: :hr_order do
  64. has_many :hr_order_deals
  65. has_many :hr_order_items
  66. has_many :hr_order_charges
  67. has_many :hr_order_discounts
  68. has_many :hr_order_payments
  69. has_many :hr_loyalty_operations
  70. has_many :hr_private_refs, class_name: HrOrderPrivateRef
  71. end
  72.  
  73. has_many :hr_connection_logs, as: :resource
  74.  
  75. belongs_to :hr_client, foreign_key: :creator_client_id
  76.  
  77. accepts_nested_attributes_for :hr_order_items,
  78. :hr_order_charges,
  79. :hr_order_deals,
  80. :hr_order_discounts,
  81. :hr_order_payments,
  82. :hr_private_refs,
  83. :hr_loyalty_operations
  84.  
  85. money_accessor :total
  86. money_accessor :total_discrepancy
  87. money_accessor :payment_discrepancy
  88.  
  89. validates :total, money: true
  90.  
  91. validates_presence_of :hr_location
  92. validates_inclusion_of :status, in: STATUSES
  93. validates_length_of :c_email,
  94. :c_first_name,
  95. :c_last_name,
  96. :c_company_name,
  97. :c_phone,
  98. :c_address_1,
  99. :c_address_2,
  100. :c_postal_code,
  101. :c_city,
  102. :c_state,
  103. :c_country, maximum: 255
  104. validates_length_of :customer_notes,
  105. :c_delivery_notes, maximum: 65355
  106.  
  107. include PrivateReferable
  108. scope :with_private_ref, ->(hr_client, hr_location, private_ref) {
  109. joins(:hr_private_refs).where(
  110. 'order_private_refs.client_id = ? && order_private_refs.location_id = ? && order_private_refs.private_ref = ?',
  111. hr_client.id, hr_location.id, private_ref
  112. )
  113. }
  114.  
  115. delegate :hr_observing_callbacks, to: :hr_location
  116. delegate :currency, to: :hr_account
  117.  
  118. def recalculate_total
  119. items_total = hr_order_items.map(&:subtotal_m).inject(hr_location.hr_account.null_money, &:+)
  120. charges_total = hr_order_charges.map(&:charge_price_m).inject(hr_location.hr_account.null_money, &:+)
  121.  
  122. total = items_total + charges_total
  123.  
  124. final_total = hr_order_discounts.inject(total) do |total, hr_order_discount|
  125. hr_order_discount.discount.apply(total)
  126. end
  127.  
  128. self.total = final_total.to_s
  129. end
  130. end
Add Comment
Please, Sign In to add comment