glysellin 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (173) hide show
  1. checksums.yaml +15 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +100 -0
  4. data/Rakefile +29 -0
  5. data/app/assets/javascripts/glysellin/address.coffee +38 -0
  6. data/app/assets/javascripts/glysellin/async-cart.coffee +139 -0
  7. data/app/assets/javascripts/glysellin/base.js +6 -0
  8. data/app/assets/javascripts/glysellin/cart.coffee +54 -0
  9. data/app/assets/javascripts/glysellin/main.coffee +0 -0
  10. data/app/assets/javascripts/glysellin/product.coffee +0 -0
  11. data/app/assets/stylesheets/glysellin/application.css +13 -0
  12. data/app/assets/stylesheets/glysellin/orders.css +4 -0
  13. data/app/assets/stylesheets/glysellin/products.css +4 -0
  14. data/app/controllers/glysellin/cart/addresses_controller.rb +17 -0
  15. data/app/controllers/glysellin/cart/discount_code_controller.rb +10 -0
  16. data/app/controllers/glysellin/cart/payment_method_controller.rb +17 -0
  17. data/app/controllers/glysellin/cart/products_controller.rb +37 -0
  18. data/app/controllers/glysellin/cart/shipping_method_controller.rb +18 -0
  19. data/app/controllers/glysellin/cart/state_controller.rb +15 -0
  20. data/app/controllers/glysellin/cart_controller.rb +53 -0
  21. data/app/controllers/glysellin/orders_controller.rb +53 -0
  22. data/app/controllers/glysellin/products_controller.rb +12 -0
  23. data/app/helpers/glysellin/application_helper.rb +4 -0
  24. data/app/helpers/glysellin/cart_helper.rb +21 -0
  25. data/app/helpers/glysellin/orders_helper.rb +35 -0
  26. data/app/helpers/glysellin/products_helper.rb +4 -0
  27. data/app/mailers/glysellin/order_admin_mailer.rb +17 -0
  28. data/app/mailers/glysellin/order_customer_mailer.rb +27 -0
  29. data/app/models/glysellin/address.rb +35 -0
  30. data/app/models/glysellin/brand.rb +14 -0
  31. data/app/models/glysellin/discount_code.rb +36 -0
  32. data/app/models/glysellin/discount_type.rb +9 -0
  33. data/app/models/glysellin/order.rb +243 -0
  34. data/app/models/glysellin/order_adjustment.rb +19 -0
  35. data/app/models/glysellin/order_item.rb +38 -0
  36. data/app/models/glysellin/orderer.rb +28 -0
  37. data/app/models/glysellin/payment.rb +46 -0
  38. data/app/models/glysellin/payment_method.rb +36 -0
  39. data/app/models/glysellin/product.rb +136 -0
  40. data/app/models/glysellin/product_image.rb +11 -0
  41. data/app/models/glysellin/product_property.rb +22 -0
  42. data/app/models/glysellin/product_property_type.rb +11 -0
  43. data/app/models/glysellin/product_type.rb +11 -0
  44. data/app/models/glysellin/shipping_method.rb +22 -0
  45. data/app/models/glysellin/taxonomy.rb +35 -0
  46. data/app/models/glysellin/variant.rb +100 -0
  47. data/app/views/glysellin/cart/_added_to_cart_warning.html.erb +21 -0
  48. data/app/views/glysellin/cart/_cart.html.erb +13 -0
  49. data/app/views/glysellin/cart/_navigation.html.erb +11 -0
  50. data/app/views/glysellin/cart/_products.html.erb +125 -0
  51. data/app/views/glysellin/cart/_recap.html.erb +16 -0
  52. data/app/views/glysellin/cart/recap/_addresses.html.erb +13 -0
  53. data/app/views/glysellin/cart/recap/_products.html.erb +63 -0
  54. data/app/views/glysellin/cart/show.html.erb +13 -0
  55. data/app/views/glysellin/cart/states/_addresses.html.erb +25 -0
  56. data/app/views/glysellin/cart/states/_choose_payment_method.html.erb +14 -0
  57. data/app/views/glysellin/cart/states/_choose_shipping_method.html.erb +14 -0
  58. data/app/views/glysellin/cart/states/_filled.html.erb +3 -0
  59. data/app/views/glysellin/cart/states/_init.html.erb +5 -0
  60. data/app/views/glysellin/cart/states/_ready.html.erb +1 -0
  61. data/app/views/glysellin/order_admin_mailer/_order_header.text.erb +5 -0
  62. data/app/views/glysellin/order_admin_mailer/send_check_order_created_email.text.erb +8 -0
  63. data/app/views/glysellin/order_admin_mailer/send_order_paid_email.text.erb +8 -0
  64. data/app/views/glysellin/order_customer_mailer/_order_footer.text.erb +3 -0
  65. data/app/views/glysellin/order_customer_mailer/_order_header.text.erb +5 -0
  66. data/app/views/glysellin/order_customer_mailer/send_order_created_email.text.erb +7 -0
  67. data/app/views/glysellin/order_customer_mailer/send_order_paid_email.text.erb +7 -0
  68. data/app/views/glysellin/order_customer_mailer/send_order_shipped_email.text.erb +8 -0
  69. data/app/views/glysellin/orders/_addresses_fields.html.erb +29 -0
  70. data/app/views/glysellin/orders/_order.html.erb +18 -0
  71. data/app/views/glysellin/orders/_payment_canceled.html.erb +13 -0
  72. data/app/views/glysellin/orders/_payment_successful.html.erb +6 -0
  73. data/app/views/glysellin/orders/payment_response.html.erb +5 -0
  74. data/app/views/glysellin/payment_methods/_paypal-integral.html.erb +22 -0
  75. data/app/views/glysellin/products/_add_to_cart.html.erb +16 -0
  76. data/app/views/glysellin/products/_item.html.erb +21 -0
  77. data/app/views/glysellin/products/filter.html.erb +0 -0
  78. data/app/views/glysellin/products/index.html.erb +3 -0
  79. data/app/views/glysellin/products/show.html.erb +3 -0
  80. data/app/views/glysellin/shared/_address.html.erb +11 -0
  81. data/app/views/glysellin/shared/_address.text.erb +7 -0
  82. data/app/views/glysellin/shared/_order.text.erb +56 -0
  83. data/app/views/glysellin/shared/mailer/_footer.text.erb +2 -0
  84. data/app/views/layouts/glysellin/application.html.erb +14 -0
  85. data/config/locales/glysellin.en.yml +113 -0
  86. data/config/locales/glysellin.fr.yml +119 -0
  87. data/db/migrate/20120206115004_create_glysellin_orders.rb +16 -0
  88. data/db/migrate/20120206115132_create_glysellin_customers.rb +8 -0
  89. data/db/migrate/20120206115657_create_glysellin_addresses.rb +22 -0
  90. data/db/migrate/20120206124505_create_glysellin_products.rb +20 -0
  91. data/db/migrate/20120206124545_create_glysellin_order_items.rb +15 -0
  92. data/db/migrate/20120206124630_create_glysellin_product_images.rb +10 -0
  93. data/db/migrate/20120206125210_add_attachment_image_to_product_image.rb +15 -0
  94. data/db/migrate/20120206130920_create_glysellin_payments.rb +13 -0
  95. data/db/migrate/20120206131042_create_glysellin_payment_methods.rb +10 -0
  96. data/db/migrate/20120207175050_create_glysellin_taxonomies.rb +11 -0
  97. data/db/migrate/20120207175158_create_products_taxonomies_table.rb +12 -0
  98. data/db/migrate/20120515143231_create_brands.rb +9 -0
  99. data/db/migrate/20120515143233_add_attachment_image_to_brands.rb +15 -0
  100. data/db/migrate/20120620151705_create_product_properties.rb +12 -0
  101. data/db/migrate/20130204121211_create_glysellin_discount_types.rb +10 -0
  102. data/db/migrate/20130204121228_create_glysellin_discount_codes.rb +15 -0
  103. data/db/migrate/20130204125310_create_glysellin_order_adjustments.rb +13 -0
  104. data/db/migrate/20130204202102_create_glysellin_shipping_methods.rb +12 -0
  105. data/db/migrate/20130204202109_add_shipping_method_id_to_glysellin_orders.rb +5 -0
  106. data/db/migrate/20130205110036_add_weight_to_glysellin_products.rb +5 -0
  107. data/db/migrate/20130205120541_add_weight_to_glysellin_order_items.rb +5 -0
  108. data/db/migrate/20130208162813_create_glysellin_variants.rb +17 -0
  109. data/db/migrate/20130208165020_create_glysellin_product_property_types.rb +7 -0
  110. data/db/migrate/20130211105557_add_type_id_to_glysellin_product_properties.rb +5 -0
  111. data/db/migrate/20130211112302_add_variant_id_to_glysellin_product_properties.rb +13 -0
  112. data/db/migrate/20130211152651_add_product_id_to_glysellin_variants.rb +5 -0
  113. data/db/migrate/20130212131658_add_weight_to_glysellin_variants.rb +5 -0
  114. data/db/migrate/20130212132253_remove_variant_columns_from_glysellin_products.rb +15 -0
  115. data/db/migrate/20130212154943_remove_eot_price_from_glysellin_products.rb +9 -0
  116. data/db/migrate/20130214184204_create_glysellin_product_types.rb +9 -0
  117. data/db/migrate/20130214184352_create_product_type_behavior.rb +15 -0
  118. data/db/migrate/20130221102200_add_unmarked_price_to_glysellin_variants.rb +5 -0
  119. data/db/migrate/20130311135842_add_addressable_to_glysellin_addresses.rb +6 -0
  120. data/db/migrate/20130314114048_change_addressable_associations_in_glysellin_addresses.rb +15 -0
  121. data/db/migrate/20130325113903_remove_address_ids_from_glysellin_orders.rb +11 -0
  122. data/db/seeds/shipping_carrier/rates/colissimo.csv +16 -0
  123. data/db/seeds/shipping_carrier/rates/lettre-max.csv +9 -0
  124. data/lib/active_model/model.rb +101 -0
  125. data/lib/generators/glysellin/install/USAGE +8 -0
  126. data/lib/generators/glysellin/install/install_generator.rb +50 -0
  127. data/lib/generators/glysellin/install/templates/initializer.rb +119 -0
  128. data/lib/generators/glysellin/install/templates/order_observer.rb +12 -0
  129. data/lib/generators/glysellin/install/utils.rb +11 -0
  130. data/lib/glysellin.rb +124 -0
  131. data/lib/glysellin/acts_as_sellable.rb +14 -0
  132. data/lib/glysellin/cart.rb +21 -0
  133. data/lib/glysellin/cart/address.rb +16 -0
  134. data/lib/glysellin/cart/adjustment.rb +17 -0
  135. data/lib/glysellin/cart/adjustment/base.rb +41 -0
  136. data/lib/glysellin/cart/adjustment/discount_code.rb +43 -0
  137. data/lib/glysellin/cart/adjustment/shipping_method.rb +31 -0
  138. data/lib/glysellin/cart/basket.rb +482 -0
  139. data/lib/glysellin/cart/customer.rb +36 -0
  140. data/lib/glysellin/cart/model_wrapper.rb +90 -0
  141. data/lib/glysellin/cart/nested_resource.rb +39 -0
  142. data/lib/glysellin/cart/payment_method.rb +8 -0
  143. data/lib/glysellin/cart/product.rb +16 -0
  144. data/lib/glysellin/cart/select.rb +31 -0
  145. data/lib/glysellin/cart/serializable.rb +30 -0
  146. data/lib/glysellin/cart/shipping_method.rb +8 -0
  147. data/lib/glysellin/discount_type_calculator.rb +7 -0
  148. data/lib/glysellin/discount_type_calculator/base.rb +30 -0
  149. data/lib/glysellin/discount_type_calculator/order_percentage.rb +16 -0
  150. data/lib/glysellin/engine.rb +14 -0
  151. data/lib/glysellin/engine/routes.rb +61 -0
  152. data/lib/glysellin/gateway.rb +16 -0
  153. data/lib/glysellin/gateway/atos.rb +111 -0
  154. data/lib/glysellin/gateway/base.rb +43 -0
  155. data/lib/glysellin/gateway/check.rb +34 -0
  156. data/lib/glysellin/gateway/paypal_integral.rb +71 -0
  157. data/lib/glysellin/helpers.rb +15 -0
  158. data/lib/glysellin/helpers/controller.rb +22 -0
  159. data/lib/glysellin/helpers/countries.rb +261 -0
  160. data/lib/glysellin/helpers/views.rb +28 -0
  161. data/lib/glysellin/product_methods.rb +66 -0
  162. data/lib/glysellin/products_list.rb +72 -0
  163. data/lib/glysellin/property_finder.rb +13 -0
  164. data/lib/glysellin/shipping_carrier.rb +14 -0
  165. data/lib/glysellin/shipping_carrier/base.rb +55 -0
  166. data/lib/glysellin/shipping_carrier/colissimo.rb +19 -0
  167. data/lib/glysellin/shipping_carrier/flat_rate.rb +19 -0
  168. data/lib/glysellin/shipping_carrier/free_shipping.rb +14 -0
  169. data/lib/glysellin/shipping_carrier/helpers/country_weight_table.rb +65 -0
  170. data/lib/glysellin/shipping_carrier/lettre_max.rb +19 -0
  171. data/lib/glysellin/version.rb +3 -0
  172. data/lib/tasks/glysellin_tasks.rake +37 -0
  173. metadata +341 -0
@@ -0,0 +1,243 @@
1
+ require 'state_machine'
2
+
3
+ module Glysellin
4
+ class Order < ActiveRecord::Base
5
+ include ProductsList
6
+ include Orderer
7
+
8
+ self.table_name = 'glysellin_orders'
9
+
10
+ attr_accessor :discount_code
11
+
12
+ state_machine initial: :ready do
13
+
14
+ event :paid do
15
+ transition any => :paid
16
+ end
17
+
18
+ event :shipped do
19
+ transition paid: :shipped
20
+ end
21
+
22
+ after_transition on: :choose_shipping_method, do: :set_shipping_price
23
+ after_transition on: :paid, do: :set_payment
24
+ end
25
+
26
+ # Relations
27
+ #
28
+ # Order products are used to map order to cloned and simplified products
29
+ # so the Order propererties can't be affected by product updates
30
+ has_many :products, class_name: 'Glysellin::OrderItem',
31
+ foreign_key: 'order_id', dependent: :destroy
32
+
33
+ # The actual buyer
34
+ belongs_to :customer, class_name: "::#{ Glysellin.user_class_name }",
35
+ foreign_key: 'customer_id', :autosave => true
36
+
37
+ # Payment tries
38
+ has_many :payments, inverse_of: :order, dependent: :destroy
39
+
40
+ belongs_to :shipping_method, inverse_of: :orders
41
+
42
+ has_many :order_adjustments, inverse_of: :order, dependent: :destroy
43
+
44
+ # We want to be able to see fields_for addresses
45
+ accepts_nested_attributes_for :products
46
+ # accepts_nested_attributes_for :customer
47
+ accepts_nested_attributes_for :payments
48
+ accepts_nested_attributes_for :order_adjustments
49
+
50
+ attr_accessible :payments, :products, :products_ids,
51
+ :customer, :customer_id, :ref, :user, :payments,
52
+ :payments_attributes, :products_attributes, :paid_on,
53
+ :state, :payment_method_id, :billing_address, :shipping_address,
54
+ :shipping_method_id, :discount_code, :order_adjustments_attributes
55
+
56
+ validates_presence_of :customer, :billing_address, :shipping_address,
57
+ :products
58
+
59
+ before_validation :process_adjustments
60
+ after_save :ensure_ref
61
+ after_create :ensure_customer_addresses
62
+ before_save :set_paid_if_paid_by_check
63
+ before_save :notify_shipped
64
+
65
+ scope :from_customer, lambda { |customer_id| where(customer_id: customer_id) }
66
+
67
+ def quantified_items
68
+ products.map { |product| [product, product.quantity] }
69
+ end
70
+
71
+ # Ensures there is always an order reference
72
+ #
73
+ def ensure_ref
74
+ update_attribute(:ref, self.generate_ref) unless self.ref
75
+ end
76
+
77
+ # Ensures that the customer hash a billing and, if needed shipping, address.
78
+ #
79
+ def ensure_customer_addresses
80
+ unless customer.billing_address
81
+ customer.create_billing_address(billing_address.clone_attributes)
82
+
83
+ unless billing_address.same_as?(shipping_address)
84
+ customer.create_shipping_address(shipping_address.clone_attributes)
85
+ end
86
+ end
87
+ end
88
+
89
+ # If admin sets payment date by hand and order was paid by check,
90
+ # fire :paid event
91
+ #
92
+ def set_paid_if_paid_by_check
93
+ paid! if (paid_on_changed? and ready? and paid_by_check?)
94
+ end
95
+
96
+ def paid_by_check?
97
+ payment and payment.by_check?
98
+ end
99
+
100
+ def set_shipping_price
101
+ build_adjustment_from shipping_method if shipping_method
102
+ end
103
+
104
+ # Callback invoked after event :paid
105
+ def set_payment
106
+ self.payment.new_status Payment::PAYMENT_STATUS_PAID
107
+ update_attribute(:paid_on, payment.last_payment_action_on)
108
+ end
109
+
110
+ # Callback invoked after event :shipped
111
+ def notify_shipped
112
+ if state_changed? && shipped?
113
+ OrderCustomerMailer.send_order_shipped_email(self).deliver
114
+ end
115
+ end
116
+
117
+ def init_payment!
118
+ self.payments.build unless payment
119
+ end
120
+
121
+ # Define model to use it's ref when asked for parameterized
122
+ # representation of itself
123
+ #
124
+ # @return [String] the order ref
125
+ def to_param
126
+ ref
127
+ end
128
+
129
+ # Automatic ref generation for an order that can be overriden
130
+ # within the config initializer, and only executes if there's no
131
+ # existing ref inside for this order
132
+ #
133
+ # @return [String] the generated or existing ref
134
+ def generate_ref
135
+ if ref
136
+ ref
137
+ else
138
+ if Glysellin.order_reference_generator
139
+ Glysellin.order_reference_generator.call(self)
140
+ else
141
+ "#{Time.now.strftime('%Y%m%d%H%M')}-#{id}"
142
+ end
143
+ end
144
+ end
145
+
146
+ # Customer's e-mail directly accessible from the order
147
+ #
148
+ # @return [String] the wanted e-mail string
149
+ def email
150
+ customer && customer.email
151
+ end
152
+
153
+ ########################################
154
+ #
155
+ # Products
156
+ #
157
+ ########################################
158
+
159
+ def products=(attributes)
160
+ products = attributes.reduce([]) do |list, product|
161
+ item = OrderItem.build_from_product(product[:id], product[:quantity])
162
+ item ? (list << item) : list
163
+ end
164
+
165
+ super(products)
166
+ end
167
+
168
+ ########################################
169
+ #
170
+ # Adjustments
171
+ #
172
+ ########################################
173
+
174
+ def build_adjustment_from item
175
+ # Handle replacing duplicate adjustments on the same order
176
+ existing_adjustments = order_adjustments.where(
177
+ adjustment_type: item.class.to_s
178
+ )
179
+ # Destroy exisiting ones
180
+ existing_adjustments.each(&:destroy) if existing_adjustments.length > 0
181
+ # Build new adjustment from existing discount code
182
+ order_adjustments.build(item.to_adjustment(self))
183
+ end
184
+
185
+ def process_adjustments
186
+ build_adjustment_from(shipping_method) if shipping_method
187
+
188
+ if discount_code
189
+ code = Glysellin::DiscountCode.from_code(discount_code)
190
+ build_adjustment_from(code) if code
191
+ end
192
+ end
193
+
194
+ ########################################
195
+ #
196
+ # Payment
197
+ #
198
+ ########################################
199
+
200
+ # Gives the last payment found for that order
201
+ #
202
+ # @return [Payment, nil] the found Payment item or nil
203
+ def payment
204
+ payments.last
205
+ end
206
+
207
+ # Returns the last payment method used if there has already been
208
+ # a payment try
209
+ #
210
+ # @return [PaymentType, nil] the PaymentMethod or nil
211
+ def payment_method
212
+ payment.type rescue nil
213
+ end
214
+
215
+ def payment_method_id=(type_id)
216
+ payment = self.payments.build :status => Payment::PAYMENT_STATUS_PENDING
217
+ payment.type = PaymentMethod.find(type_id)
218
+ end
219
+
220
+
221
+ ########################################
222
+ #
223
+ # Adresses
224
+ #
225
+ ########################################
226
+
227
+ # Set customer from a Hash of attributes
228
+ #
229
+ def customer=(attributes)
230
+ unless (self.customer_id = attributes[:id])
231
+ user = self.build_customer(attributes)
232
+
233
+ if Glysellin.allow_anonymous_orders &&
234
+ !(user.password || user.password_confirmation)
235
+
236
+ password = (rand*(10**20)).to_i.to_s(36)
237
+ user.password = password
238
+ user.password_confirmation = password
239
+ end
240
+ end
241
+ end
242
+ end
243
+ end
@@ -0,0 +1,19 @@
1
+ module Glysellin
2
+ class OrderAdjustment < ActiveRecord::Base
3
+ self.table_name = 'glysellin_order_adjustments'
4
+
5
+ attr_accessible :adjustment_id, :adjustment_type, :name, :order_id, :value,
6
+ :adjustment, :order
7
+
8
+ belongs_to :order
9
+ belongs_to :adjustment, polymorphic: true
10
+
11
+ def type
12
+ adjustment_type.demodulize.underscore.dasherize
13
+ end
14
+
15
+ def valid
16
+ true
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,38 @@
1
+ module Glysellin
2
+ class OrderItem < ActiveRecord::Base
3
+ self.table_name = 'glysellin_order_items'
4
+ belongs_to :order, inverse_of: :products
5
+
6
+ attr_accessible :sku, :name, :eot_price, :vat_rate, :bundle, :price,
7
+ :quantity, :weight
8
+
9
+ # The attributes we getch from a product to build our order item
10
+ PRODUCT_ATTRIBUTES_FOR_ITEM = %w(sku name eot_price vat_rate price weight)
11
+
12
+ class << self
13
+ # Create an item from product or bundle id
14
+ #
15
+ # @param [String] id The id string of the item
16
+ # @param [Boolean] bundle If it's a bundle or just one product
17
+ #
18
+ # @return [OrderItem] The created order item
19
+ def build_from_product id, quantity
20
+ product = Glysellin::Variant.find_by_id(id)
21
+
22
+ attrs = PRODUCT_ATTRIBUTES_FOR_ITEM.map do |key|
23
+ [key, product.public_send(key)]
24
+ end
25
+
26
+ OrderItem.new(Hash[attrs].merge('quantity' => quantity))
27
+ end
28
+ end
29
+
30
+ def total_eot_price
31
+ quantity * eot_price
32
+ end
33
+
34
+ def total_price
35
+ quantity * price
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,28 @@
1
+ module Glysellin
2
+ module Orderer
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ has_one :billing_address, class_name: 'Glysellin::Address',
7
+ as: :billed_addressable, dependent: :destroy, autosave: true
8
+
9
+ has_one :shipping_address, class_name: 'Glysellin::Address',
10
+ as: :shipped_addressable, dependent: :destroy, autosave: true
11
+
12
+ attr_writer :use_another_address_for_shipping
13
+
14
+ attr_accessible :billing_address_attributes, :shipping_address_attributes,
15
+ :use_another_address_for_shipping
16
+
17
+ accepts_nested_attributes_for :billing_address, :shipping_address
18
+ end
19
+
20
+ def use_another_address_for_shipping
21
+ !(shipping_address.new_record? && !super) rescue false
22
+ end
23
+
24
+ def has_shipping_address?
25
+ shipping_address && shipping_address.id.present?
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,46 @@
1
+ module Glysellin
2
+ class Payment < ActiveRecord::Base
3
+ self.table_name = 'glysellin_payments'
4
+
5
+ belongs_to :order, :inverse_of => :payments
6
+ belongs_to :type, :class_name => 'PaymentMethod', :foreign_key => 'type_id', :inverse_of => :payments
7
+
8
+ attr_accessible :status, :type_id, :type, :order, :order_id,
9
+ :last_payment_action_on, :transaction_id
10
+
11
+ # Public: Status const to be used if payment pending
12
+ PAYMENT_STATUS_PENDING = 'pending'
13
+ # Public: Status const to be used if payment paid
14
+ PAYMENT_STATUS_PAID = 'paid'
15
+ # Public: Status const to be used if payment canceled
16
+ PAYMENT_STATUS_CANCELED = 'canceled'
17
+
18
+ def new_status s
19
+ self.status = s
20
+ self.last_payment_action_on = Time.now
21
+ self.save
22
+ end
23
+
24
+ def new_transaction_id!
25
+ last_transaction_with_id = self.class.where('transaction_id > 0').order('updated_at DESC').first
26
+ next_id = last_transaction_with_id ? last_transaction_with_id.transaction_id + 1 : 1
27
+ self.transaction_id = next_id
28
+ self.save
29
+ end
30
+
31
+ def get_new_transaction_id
32
+ self.new_transaction_id!
33
+ self.transaction_id
34
+ end
35
+
36
+ def status_enum
37
+ [PAYMENT_STATUS_PAID, PAYMENT_STATUS_PENDING, PAYMENT_STATUS_CANCELED].map do |s|
38
+ [I18n.t("glysellin.labels.payments.statuses.#{ s }"), s]
39
+ end
40
+ end
41
+
42
+ def by_check?
43
+ type.slug == 'check'
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,36 @@
1
+ module Glysellin
2
+ class PaymentMethod < ActiveRecord::Base
3
+ self.table_name = 'glysellin_payment_methods'
4
+ has_many :payments, :foreign_key => 'type_id'
5
+
6
+ attr_accessible :name, :slug
7
+
8
+ scope :ordered, order("name ASC")
9
+
10
+ class << self
11
+ def gateway_from_order_ref ref
12
+ order = Order.find_by_ref(ref)
13
+ gateway_for(order)
14
+ end
15
+
16
+ def gateway_from_raw_post gateway, raw_post
17
+ order = Glysellin.gateways[gateway].parse_order_id(raw_post)
18
+ gateway_for(order)
19
+ end
20
+
21
+ def gateway_for order
22
+ Glysellin.gateways[order.payment_method.slug].new(order)
23
+ end
24
+ end
25
+
26
+ # Get the payment request button HTML for the specified order
27
+ #
28
+ # @param [Order] order The order to get the payment request for
29
+ #
30
+ # @return [String] The request button HTML
31
+ def request_button order
32
+ g = Glysellin.gateways[order.payment_method.slug].new(order)
33
+ g.render_request_button
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,136 @@
1
+ require 'digest/sha1'
2
+ require 'friendly_id'
3
+
4
+ module Glysellin
5
+ class Product < ActiveRecord::Base
6
+ include ProductMethods
7
+ extend FriendlyId
8
+
9
+ friendly_id :name, use: :slugged
10
+
11
+ self.table_name = 'glysellin_products'
12
+
13
+ # Relations
14
+ #
15
+ # The ProductImage model is used for products and bundles with the same
16
+ has_many :images, as: :imageable, class_name: 'Glysellin::ProductImage',
17
+ inverse_of: :imageable, dependent: :destroy
18
+
19
+ # Can have multiple taxonomies
20
+ has_and_belongs_to_many :taxonomies, :class_name => 'Glysellin::Taxonomy',
21
+ join_table: 'glysellin_products_taxonomies', :foreign_key => 'product_id'
22
+
23
+ # N..N relation between bundles and products
24
+ # has_many :bundle_products, class_name: 'Glysellin::BundleProduct',
25
+ # foreign_key: 'product_id'
26
+ # has_many :bundles, class_name: 'Glysellin::Product',
27
+ # through: :bundle_products
28
+
29
+ # # Bundled products in current_product
30
+ # has_many :product_bundles, class_name: 'Glysellin::BundleProduct',
31
+ # foreign_key: 'bundle_id'
32
+ # has_many :bundled_products, class_name: 'Glysellin::Product',
33
+ # through: :product_bundles
34
+
35
+ # Products can belong to a brand
36
+ belongs_to :brand, :inverse_of => :products
37
+
38
+ belongs_to :product_type
39
+
40
+ has_many :variants, class_name: 'Glysellin::Variant',
41
+ before_add: :set_product_on_variant, dependent: :destroy
42
+
43
+ accepts_nested_attributes_for :images, allow_destroy: true, reject_if: :all_blank
44
+ accepts_nested_attributes_for :variants, allow_destroy: true, reject_if: :all_blank
45
+
46
+ # accepts_nested_attributes_for :bundled_products, allow_destroy: true, reject_if: :all_blank
47
+
48
+ attr_accessible :description, :name, :sku, :slug, :vat_rate,
49
+ :brand, :taxonomies, :images, :published,
50
+ :display_priority, :images_attributes, :taxonomy_ids, :unlimited_stock,
51
+ :position, :brand_id, :variants_attributes, :variants, :product_type_id
52
+
53
+ # :bundled_products_attributes
54
+
55
+ # Validations
56
+ #
57
+ validates_presence_of :name, :slug
58
+ # Validates price related attributes only unless we have bundled products
59
+ # so we can defer validations to them
60
+ validates :vat_rate, presence: true,
61
+ numericality: true # , unless: proc { |p| p.bundled_products.length > 0 }
62
+
63
+ # We check presence of sku if set in global config
64
+ validates :sku, presence: true, if: proc { Glysellin.autoset_sku }
65
+
66
+ # Callbacks
67
+ #
68
+ before_validation :set_sku, :set_vat_rate, :ensure_variant
69
+
70
+ scope :published, where('glysellin_products.published = ?', true)
71
+ scope :with_taxonomy, lambda { |*taxonomies|
72
+ # Ensure we only have slugs so we got a string vector
73
+ taxonomies.map! { |t| t.kind_of?(Glysellin::Taxonomy) ? t.slug : t }
74
+ # Get products with those taxonomies
75
+ includes(:taxonomies).where('glysellin_taxonomies.slug IN (?)', taxonomies)
76
+ }
77
+
78
+ # Master variant methods delegation
79
+ delegate :price, :unmarked_price, :marked_down?, to: :master_variant
80
+
81
+
82
+ # And as for the validation, if the SKU is configured to be autoset,
83
+ # we check generate it
84
+ def set_sku
85
+ unless (sku && sku.length > 0) || !Glysellin.autoset_sku
86
+ self.sku = generate_sku
87
+ end
88
+ end
89
+
90
+ def set_vat_rate
91
+ if !vat_rate
92
+ self.vat_rate = Glysellin.default_vat_rate
93
+ end
94
+ end
95
+
96
+ def ensure_variant
97
+ variants.build(product: self) unless variants.length > 0
98
+ end
99
+
100
+ # Fetches all available variants for the current product
101
+ #
102
+ def available_variants
103
+ variants.available
104
+ end
105
+
106
+ def image
107
+ images.length > 0 ? images.first.image : nil
108
+ end
109
+
110
+ def master_variant
111
+ self.variants.first
112
+ end
113
+
114
+ def set_product_on_variant variant
115
+ variant.product = self
116
+ end
117
+
118
+ # bundle_attribute :price do |product|
119
+ # product.bundled_products.reduce(0) do |total, product|
120
+ # total + product.price
121
+ # end
122
+ # end
123
+
124
+ # bundle_attribute :eot_price do |product, att|
125
+ # product.bundled_products.reduce(0) do |total, product|
126
+ # total + product.eot_price
127
+ # end
128
+ # end
129
+
130
+ # bundle_attribute :vat_rate do |product|
131
+ # product.bundled_products.reduce(0) do |total, product|
132
+ # total + (product.vat_rate * product.price)
133
+ # end / product.price
134
+ # end
135
+ end
136
+ end