Module: Auth::Concerns::Shopping::CartConcern
- Extended by:
- ActiveSupport::Concern
- Includes:
- ChiefModelConcern, OwnerConcern
- Included in:
- Shopping::Cart
- Defined in:
- app/models/auth/concerns/shopping/cart_concern.rb
Overview
NEED A SEPERATE MODEL THAT IMPLEMENTS IT
Class Method Summary collapse
-
.find_cart(cart_id, resource) ⇒ Object
used in initialize vars in cart_controlller_concern.
Instance Method Summary collapse
-
#add_or_remove(item_ids, add_or_remove) ⇒ Object
adds a validation error if the cart items could not be successfully added or removed.
- #add_or_remove_validation ⇒ Object
- #as_json(options = {}) ⇒ Object
-
#can_create_discount_coupons? ⇒ Boolean
@return true/false : override to decide how the cart decides if it can create discount coupons for its contents or not.
-
#debit(amount) ⇒ Object
debits the @amount from the cart credit.
-
#find_cart_items ⇒ Object
set the cart items, [Array] of cart items.
-
#fully_paid ⇒ Object
fully paid.
-
#get_cart_credit ⇒ Object
initially is the same as cart_paid_amount, by calling debit, we can debit from credit, the costs of various items.
- #get_cart_items ⇒ Object
-
#get_cart_paid_amount ⇒ Object
sum total of all payments made to the cart.
- #get_cart_payments ⇒ Object
- #get_cart_pending_balance ⇒ Object
- #get_cart_price ⇒ Object
-
#has_items? ⇒ Boolean
checks if there are any items in the cart.
-
#has_pending ⇒ Object
not fully paid, there is some amount to be taken from the customer yet.
-
#not_paid_at_all ⇒ Object
not paid a penny.
-
#prepare_cart ⇒ Object
sets all the attribute accessors of the cart.
-
#prepare_receipt ⇒ Object
just replaces the cart_items with an array of cart_item_ids replaces the cart_payments with an array of cart_payment_ids then returns a hash with these two instead of the original values.
-
#refund_amount ⇒ Object
returns the amount that needs to be refunded to the customer from this cart.
-
#set_cart_minimum_payable_amount ⇒ Object
takes all the cart items that are not accepted, and sums their “minimum_price_required_to_accept_cart_item” methods.
- #set_cart_paid_amount ⇒ Object
- #set_cart_payments ⇒ Object
- #set_cart_pending_balance ⇒ Object
-
#set_cart_price ⇒ Object
>.
-
#set_discount ⇒ Object
will do first check if the discount id exists, and then if it requires.
Class Method Details
.find_cart(cart_id, resource) ⇒ Object
used in initialize vars in cart_controlller_concern. override this method to allow an admin to also interact with all the actions of the controller.
310 311 312 313 314 315 316 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 310 def self.find_cart(cart_id,resource) if cart_collection = self.where(:id => cart_id, :resource_id => resource_id) cart_collection.size > 0 ? cart_collection.first : nil else nil end end |
Instance Method Details
#add_or_remove(item_ids, add_or_remove) ⇒ Object
adds a validation error if the cart items could not be successfully added or removed. called from the controller. TODO: you should change this to be called before_validation instead, so that all code remains in the model.
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 279 def add_or_remove(item_ids,add_or_remove) item_ids.map {|id| begin #puts "the signed in resource is:" #puts self.signed_in_resource cart_item = Auth.configuration.cart_item_class.constantize.find_self(id,self.signed_in_resource) cart_item.signed_in_resource = self.signed_in_resource puts "Add or remove is: #{add_or_remove}" resp = (add_or_remove == 1) ? cart_item.set_cart_and_resource(self) : cart_item.unset_cart puts "unset cart is:#{resp.to_s}" resp rescue Mongoid::Errors::DocumentNotFound => error #puts "--------------------------------------------DIDNT FIND THE CART ITEM" #puts error.to_s #puts "--------------------------------------------" true end } #self.errors.add(:cart_items,"some cart items could not be added or removed successfully") if ((add_remove_results.size > 1) || (add_remove_results[0] == false)) end |
#add_or_remove_validation ⇒ Object
271 272 273 274 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 271 def add_or_remove_validation add_or_remove(add_cart_item_ids,1) if add_cart_item_ids add_or_remove(remove_cart_item_ids,-1) if remove_cart_item_ids end |
#as_json(options = {}) ⇒ Object
267 268 269 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 267 def as_json(={}) super.merge({:cart_items => self.cart_items, :cart_payments => self.cart_payments, :cart_price => self.cart_price, :cart_paid_amount => self.cart_paid_amount, :cart_pending_balance => self.cart_pending_balance, :cart_credit => self.cart_credit, :cart_minimum_payable_amount => self.cart_minimum_payable_amount}) end |
#can_create_discount_coupons? ⇒ Boolean
@return true/false : override to decide how the cart decides if it can create discount coupons for its contents or not. the current implementation returns true if all items have been fully paid for.
320 321 322 323 324 325 326 327 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 320 def can_create_discount_coupons? #return (self.cart_items.select{|c| c.accepted.nil? || c.accepted == false}.size == 0) puts "CAME TO CAN CREATE DISCOUNT COUPONS." prepare_cart self.cart_pending_balance == 0 end |
#debit(amount) ⇒ Object
debits the @amount from the cart credit. returns the current credit.
212 213 214 215 216 217 218 219 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 212 def debit(amount) #puts "want to debit: #{amount}" #puts "current credit is: #{self.cart_credit}" self.cart_credit-=amount #puts "after debiting: #{self.cart_credit}" self.cart_credit end |
#find_cart_items ⇒ Object
set the cart items, [Array] of cart items.
99 100 101 102 103 104 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 99 def find_cart_items conditions = {:resource_id => get_resource.id.to_s, :parent_id => self.id.to_s} self.cart_items = Auth.configuration.cart_item_class.constantize.where(conditions).order(:created_at => 'asc') self.cart_items end |
#fully_paid ⇒ Object
fully paid.
229 230 231 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 229 def fully_paid get_cart_pending_balance == 0 end |
#get_cart_credit ⇒ Object
initially is the same as cart_paid_amount, by calling debit, we can debit from credit, the costs of various items.
206 207 208 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 206 def get_cart_credit self.cart_credit end |
#get_cart_items ⇒ Object
106 107 108 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 106 def get_cart_items self.cart_items end |
#get_cart_paid_amount ⇒ Object
sum total of all payments made to the cart.
152 153 154 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 152 def get_cart_paid_amount self.cart_paid_amount end |
#get_cart_payments ⇒ Object
133 134 135 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 133 def get_cart_payments self.cart_payments end |
#get_cart_pending_balance ⇒ Object
161 162 163 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 161 def get_cart_pending_balance self.cart_pending_balance end |
#get_cart_price ⇒ Object
116 117 118 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 116 def get_cart_price self.cart_price end |
#has_items? ⇒ Boolean
checks if there are any items in the cart. expected that prepare_cart has been called in advance. return true if there is one or more items in the cart @used_in: payment_concern validation cart_not_empty
262 263 264 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 262 def has_items? get_cart_items.size > 0 end |
#has_pending ⇒ Object
not fully paid, there is some amount to be taken from the customer yet.
224 225 226 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 224 def has_pending get_cart_pending_balance > 0 end |
#not_paid_at_all ⇒ Object
not paid a penny.
234 235 236 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 234 def not_paid_at_all get_cart_pending_balance == get_cart_price end |
#prepare_cart ⇒ Object
sets all the attribute accessors of the cart. @param : a payment object can be passed in. this is used in case there is a new payment which is calling prepare_cart. in that case the new payment has to be also added to the cart_payments. this is alwasy the case when a new payment is made with a status directly set as accepted, i.e for eg a cashier makes a payment on behalf of the customer.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 78 def prepare_cart find_cart_items set_cart_price set_cart_payments set_cart_paid_amount set_cart_pending_balance set_cart_minimum_payable_amount set_discount end |
#prepare_receipt ⇒ Object
just replaces the cart_items with an array of cart_item_ids replaces the cart_payments with an array of cart_payment_ids then returns a hash with these two instead of the original values.
251 252 253 254 255 256 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 251 def prepare_receipt cart_item_ids = cart_items.map{|c| c = c.id.to_s} cart_payment_ids = cart_payments.map{|c| c = c.id.to_s} receipt = self.attributes.merge({:cart_items => cart_item_ids, :cart_payments => cart_payment_ids}) receipt end |
#refund_amount ⇒ Object
returns the amount that needs to be refunded to the customer from this cart.
242 243 244 245 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 242 def refund_amount return 0 unless get_cart_pending_balance < 0 return get_cart_pending_balance end |
#set_cart_minimum_payable_amount ⇒ Object
takes all the cart items that are not accepted, and sums their “minimum_price_required_to_accept_cart_item” methods.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 167 def set_cart_minimum_payable_amount pending_or_unaccepted_cart_items = self.cart_items.select{|c| c.accepted.nil? || c.accepted == false} accepted_cart_items = self.cart_items.select{|c| c.accepted == true} if pending_or_unaccepted_cart_items.size > 0 self.cart_minimum_payable_amount = pending_or_unaccepted_cart_items.map{|c| c = c.minimum_price_required_to_accept_cart_item}.inject(:+) ## it can happen that he has payed somewhere halfway between the required amount and the full price of the cart -> see the tests. self.cart_minimum_payable_amount = self.cart_pending_balance if (self.cart_pending_balance < self.cart_minimum_payable_amount) elsif accepted_cart_items.size > 0 self.cart_minimum_payable_amount = 0 else end ## we can add more stuff here like the credit score of the user. ## so that is a function of the cart_owner. ## so everytime a cart is fully paid, it can add to the credit score of the user, and that can further titrate this.. ## so we have discount and all this. end |
#set_cart_paid_amount ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 137 def set_cart_paid_amount total_paid = 0 payments = get_cart_payments price = get_cart_price payments.each do |payment| total_paid += payment.amount if (payment.payment_success) end self.cart_paid_amount = total_paid self.cart_credit = self.cart_paid_amount self.cart_paid_amount end |
#set_cart_payments ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 121 def set_cart_payments self.cart_payments = [] payments_made_to_this_cart = Auth.configuration.payment_class.constantize.find_payments(get_resource,self) payments_made_to_this_cart.each do |payment| self.cart_payments << payment end self.cart_payments end |
#set_cart_pending_balance ⇒ Object
157 158 159 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 157 def set_cart_pending_balance self.cart_pending_balance = get_cart_price - get_cart_paid_amount end |
#set_cart_price ⇒ Object
>
111 112 113 114 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 111 def set_cart_price self.cart_price = total_value_of_all_items_in_cart = get_cart_items.map{|c| c = c.price*c.quantity}.sum self.cart_price end |
#set_discount ⇒ Object
will do first check if the discount id exists, and then if it requires
196 197 198 199 200 201 202 203 |
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 196 def set_discount begin self.discount = Auth.configuration.discount_class.constantize.find(discount_id) rescue => e end end |