Class: Dscf::Marketplace::Order
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Dscf::Marketplace::Order
- Defined in:
- app/models/dscf/marketplace/order.rb
Class Method Summary collapse
- .create_from_listing(listing, user, quantity, dropoff_address = nil, payment_method = nil) ⇒ Object
- .create_from_quotation(quotation, dropoff_address = nil, payment_method = nil) ⇒ Object
- .ransackable_associations(_auth_object = nil) ⇒ Object
-
.ransackable_attributes(_auth_object = nil) ⇒ Object
Ransack configuration for secure filtering.
Instance Method Summary collapse
- #calculate_total_amount ⇒ Object
- #can_be_completed? ⇒ Boolean
- #confirm! ⇒ Object
- #delivery? ⇒ Boolean
- #requires_delivery_order? ⇒ Boolean
-
#self_pickup? ⇒ Boolean
Fulfillment type methods.
- #supplier ⇒ Object
- #total_amount ⇒ Object
Class Method Details
.create_from_listing(listing, user, quantity, dropoff_address = nil, payment_method = nil) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/models/dscf/marketplace/order.rb', line 76 def self.create_from_listing(listing, user, quantity, dropoff_address = nil, payment_method = nil) return nil unless listing.status == "active" && quantity <= listing.quantity attributes = { order_type: :direct_listing, status: :pending, fulfillment_type: dropoff_address.present? ? :delivery : :self_pickup, listing: listing, user: user, # Keep for backward compatibility ordered_by: user, ordered_to: listing.business, dropoff_address: dropoff_address, total_amount: listing.price * quantity } attributes[:payment_method] = payment_method if payment_method.present? order = create!(attributes) order.order_items.create!( listing: listing, product: listing.supplier_product.product, unit: listing.supplier_product.product.unit, quantity: quantity, unit_price: listing.price, status: :pending ) order end |
.create_from_quotation(quotation, dropoff_address = nil, payment_method = nil) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/dscf/marketplace/order.rb', line 44 def self.create_from_quotation(quotation, dropoff_address = nil, payment_method = nil) return nil unless quotation.accepted? attributes = { order_type: :rfq_based, status: :processing, fulfillment_type: dropoff_address.present? ? :delivery : :self_pickup, quotation: quotation, user: quotation.request_for_quotation.user, # Keep for backward compatibility ordered_by: quotation.request_for_quotation.user, ordered_to: quotation.business, dropoff_address: dropoff_address, total_amount: quotation.total_price } attributes[:payment_method] = payment_method if payment_method.present? order = create!(attributes) quotation.quotation_items.each do |item| order.order_items.create!( quotation_item: item, product: item.product, unit: item.unit, quantity: item.quantity, unit_price: item.unit_price, status: :pending ) end order end |
.ransackable_associations(_auth_object = nil) ⇒ Object
40 41 42 |
# File 'app/models/dscf/marketplace/order.rb', line 40 def self.ransackable_associations(_auth_object = nil) %w[quotation listing user ordered_by ordered_to delivery_order dropoff_address order_items] end |
.ransackable_attributes(_auth_object = nil) ⇒ Object
Ransack configuration for secure filtering
36 37 38 |
# File 'app/models/dscf/marketplace/order.rb', line 36 def self.ransackable_attributes(_auth_object = nil) %w[id quotation_id listing_id user_id ordered_by_id ordered_to_id delivery_order_id dropoff_address_id order_type status fulfillment_type payment_method received_bank_name transaction_reference total_amount created_at updated_at] end |
Instance Method Details
#calculate_total_amount ⇒ Object
156 157 158 |
# File 'app/models/dscf/marketplace/order.rb', line 156 def calculate_total_amount self.total_amount = order_items.sum { |item| item.quantity * item.unit_price } end |
#can_be_completed? ⇒ Boolean
137 138 139 140 141 142 143 144 145 |
# File 'app/models/dscf/marketplace/order.rb', line 137 def can_be_completed? if self_pickup? # Self-pickup orders can be completed immediately after confirmation confirmed? || processing? else # Delivery orders require delivery_order association delivery_order.present? && delivery_order.delivered? end end |
#confirm! ⇒ Object
147 148 149 150 151 152 153 154 |
# File 'app/models/dscf/marketplace/order.rb', line 147 def confirm! return false unless pending? update!(status: :confirmed) # Update order items without reloading to avoid association issues order_items.update_all(status: OrderItem.statuses[:confirmed]) true end |
#delivery? ⇒ Boolean
129 130 131 |
# File 'app/models/dscf/marketplace/order.rb', line 129 def delivery? fulfillment_type == "delivery" end |
#requires_delivery_order? ⇒ Boolean
133 134 135 |
# File 'app/models/dscf/marketplace/order.rb', line 133 def requires_delivery_order? delivery? && !completed? end |
#self_pickup? ⇒ Boolean
Fulfillment type methods
125 126 127 |
# File 'app/models/dscf/marketplace/order.rb', line 125 def self_pickup? fulfillment_type == "self_pickup" end |
#supplier ⇒ Object
116 117 118 119 120 121 122 |
# File 'app/models/dscf/marketplace/order.rb', line 116 def supplier if rfq_based? quotation&.business elsif direct_listing? listing&.business end end |
#total_amount ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'app/models/dscf/marketplace/order.rb', line 106 def total_amount # Return stored value if it exists and is greater than 0, otherwise calculate stored_value = super calculated_value = order_items.sum { |item| item.quantity * item.unit_price } # Return stored value if it's set and valid, otherwise return calculated value return stored_value if stored_value.present? && stored_value > 0 calculated_value end |