prestashop 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +56 -0
  3. data/README.md +111 -0
  4. data/Rakefile +9 -0
  5. data/lib/prestashop.rb +21 -0
  6. data/lib/prestashop/api.rb +9 -0
  7. data/lib/prestashop/api/connection.rb +225 -0
  8. data/lib/prestashop/api/converter.rb +152 -0
  9. data/lib/prestashop/api/error.rb +18 -0
  10. data/lib/prestashop/api/refinement.rb +14 -0
  11. data/lib/prestashop/client.rb +19 -0
  12. data/lib/prestashop/client/cache.rb +53 -0
  13. data/lib/prestashop/client/error.rb +9 -0
  14. data/lib/prestashop/client/implementation.rb +33 -0
  15. data/lib/prestashop/mapper.rb +9 -0
  16. data/lib/prestashop/mapper/extension.rb +161 -0
  17. data/lib/prestashop/mapper/model.rb +38 -0
  18. data/lib/prestashop/mapper/models.rb +1 -0
  19. data/lib/prestashop/mapper/models/address.rb +9 -0
  20. data/lib/prestashop/mapper/models/carrier.rb +9 -0
  21. data/lib/prestashop/mapper/models/cart.rb +9 -0
  22. data/lib/prestashop/mapper/models/cart_rule.rb +9 -0
  23. data/lib/prestashop/mapper/models/category.rb +114 -0
  24. data/lib/prestashop/mapper/models/combination.rb +81 -0
  25. data/lib/prestashop/mapper/models/contact.rb +9 -0
  26. data/lib/prestashop/mapper/models/content_management_system.rb +9 -0
  27. data/lib/prestashop/mapper/models/country.rb +15 -0
  28. data/lib/prestashop/mapper/models/currency.rb +9 -0
  29. data/lib/prestashop/mapper/models/customer.rb +9 -0
  30. data/lib/prestashop/mapper/models/customer_message.rb +9 -0
  31. data/lib/prestashop/mapper/models/customer_thread.rb +9 -0
  32. data/lib/prestashop/mapper/models/delivery.rb +9 -0
  33. data/lib/prestashop/mapper/models/employee.rb +9 -0
  34. data/lib/prestashop/mapper/models/group.rb +9 -0
  35. data/lib/prestashop/mapper/models/guest.rb +9 -0
  36. data/lib/prestashop/mapper/models/image.rb +52 -0
  37. data/lib/prestashop/mapper/models/image_type.rb +9 -0
  38. data/lib/prestashop/mapper/models/language.rb +15 -0
  39. data/lib/prestashop/mapper/models/manufacturer.rb +79 -0
  40. data/lib/prestashop/mapper/models/order.rb +9 -0
  41. data/lib/prestashop/mapper/models/order_carrier.rb +9 -0
  42. data/lib/prestashop/mapper/models/order_detail.rb +9 -0
  43. data/lib/prestashop/mapper/models/order_discount.rb +9 -0
  44. data/lib/prestashop/mapper/models/order_history.rb +9 -0
  45. data/lib/prestashop/mapper/models/order_invoice.rb +9 -0
  46. data/lib/prestashop/mapper/models/order_payment.rb +9 -0
  47. data/lib/prestashop/mapper/models/order_state.rb +9 -0
  48. data/lib/prestashop/mapper/models/price_range.rb +9 -0
  49. data/lib/prestashop/mapper/models/product.rb +198 -0
  50. data/lib/prestashop/mapper/models/product_feature.rb +69 -0
  51. data/lib/prestashop/mapper/models/product_feature_value.rb +60 -0
  52. data/lib/prestashop/mapper/models/product_option.rb +72 -0
  53. data/lib/prestashop/mapper/models/product_option_value.rb +56 -0
  54. data/lib/prestashop/mapper/models/product_supplier.rb +31 -0
  55. data/lib/prestashop/mapper/models/search.rb +9 -0
  56. data/lib/prestashop/mapper/models/shop.rb +9 -0
  57. data/lib/prestashop/mapper/models/shop_group.rb +9 -0
  58. data/lib/prestashop/mapper/models/specific_price.rb +9 -0
  59. data/lib/prestashop/mapper/models/specific_price_rule.rb +9 -0
  60. data/lib/prestashop/mapper/models/state.rb +9 -0
  61. data/lib/prestashop/mapper/models/stock.rb +9 -0
  62. data/lib/prestashop/mapper/models/stock_available.rb +35 -0
  63. data/lib/prestashop/mapper/models/stock_movement.rb +9 -0
  64. data/lib/prestashop/mapper/models/stock_movement_reason.rb +9 -0
  65. data/lib/prestashop/mapper/models/store.rb +9 -0
  66. data/lib/prestashop/mapper/models/supplier.rb +45 -0
  67. data/lib/prestashop/mapper/models/supply_order.rb +9 -0
  68. data/lib/prestashop/mapper/models/supply_order_detail.rb +9 -0
  69. data/lib/prestashop/mapper/models/supply_order_history.rb +9 -0
  70. data/lib/prestashop/mapper/models/supply_order_receipt_history.rb +9 -0
  71. data/lib/prestashop/mapper/models/supply_order_state.rb +9 -0
  72. data/lib/prestashop/mapper/models/tag.rb +9 -0
  73. data/lib/prestashop/mapper/models/tax.rb +24 -0
  74. data/lib/prestashop/mapper/models/tax_rule.rb +15 -0
  75. data/lib/prestashop/mapper/models/tax_rule_group.rb +9 -0
  76. data/lib/prestashop/mapper/models/translated_configuration.rb +9 -0
  77. data/lib/prestashop/mapper/models/warehouse.rb +9 -0
  78. data/lib/prestashop/mapper/models/warehouse_product_location.rb +9 -0
  79. data/lib/prestashop/mapper/models/weight_range.rb +9 -0
  80. data/lib/prestashop/mapper/models/zone.rb +9 -0
  81. data/lib/prestashop/mapper/refinement.rb +43 -0
  82. data/lib/prestashop/version.rb +3 -0
  83. metadata +279 -0
@@ -0,0 +1,9 @@
1
+ using Prestashop::Mapper::Refinement
2
+ module Prestashop
3
+ module Mapper
4
+ class PriceRange < Model
5
+ resource :price_ranges
6
+ model :price_range
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,198 @@
1
+ using Prestashop::Mapper::Refinement
2
+ module Prestashop
3
+ module Mapper
4
+ class Product < Model
5
+ resource :products
6
+ model :product
7
+
8
+ attr_accessor :id_manufacturer, :id_supplier, :id_category_default, :new, :cache_default_attribute, :id_tax_rules_group, :position_in_category,
9
+ :manufacturer_name, :quantity, :type, :id_shop_default, :supplier_reference, :location, :width, :height, :depth,
10
+ :weight, :quantity_discount, :ean13, :upc, :cache_is_pack, :cache_has_attachment, :is_virtual, :on_sale, :online_only, :ecotax, :minimal_quantity,
11
+ :price, :wholesale_price, :unity, :unit_price_ratio, :additional_shipping_cost, :customizable, :text_fields, :uploadable_files, :active,
12
+ :redirect_type, :id_product_redirect, :available_for_order, :available_date, :condition, :show_price, :indexed, :visibility, :advanced_stock_management, :description
13
+ attr_accessor :id_lang, :id_categories, :id_features
14
+ attr_writer :id, :name, :description_short, :link_rewrite, :reference, :price, :available_now, :available_later, :meta_description, :meta_keywords, :meta_title
15
+
16
+ def initialize args = {}
17
+ @id = args[:id]
18
+ @id_manufacturer = args[:id_manufacturer]
19
+ @id_supplier = args.fetch(:id_supplier)
20
+ @id_category_default = args[:id_category_default]
21
+ @new = args[:new]
22
+ @cache_default_attribute = args.fetch(:cache_default_attribute, 0)
23
+ @id_tax_rules_group = args[:id_tax_rules_group]
24
+ @position_in_category = args.fetch(:position_in_category, 0)
25
+ @manufacturer_name = args[:manufacturer_name]
26
+ @quantity = args[:quantity]
27
+ @type = args.fetch(:type, 'simple')
28
+ @id_shop_default = args.fetch(:id_shop_default, 1)
29
+ @reference = args.fetch(:reference)
30
+ @supplier_reference = args[:supplier_reference]
31
+ @location = args[:location]
32
+ @width = args[:width]
33
+ @height = args[:height]
34
+ @depth = args[:depth]
35
+ @weight = args[:weight]
36
+ @quantity_discount = args.fetch(:quantity_discount, 0)
37
+ @ean13 = args[:ean13]
38
+ @upc = args[:upc]
39
+ @cache_is_pack = args.fetch(:cache_is_pack, 0)
40
+ @cache_has_attachment = args.fetch(:cache_has_attachment, 0)
41
+ @is_virtual = args.fetch(:is_virtual, 0)
42
+ @on_sale = args.fetch(:on_sale, 0)
43
+ @online_only = args.fetch(:online_only, 0)
44
+ @ecotax = args.fetch(:ecotax, 0)
45
+ @minimal_quantity = args.fetch(:minimal_quantity, 1)
46
+ @price = args[:price]
47
+ @wholesale_price = args[:wholesale_price]
48
+ @unity = args[:unity]
49
+ @unit_price_ratio = args[:unit_price_ratio]
50
+ @additional_shipping_cost = args[:additional_shipping_cost]
51
+ @customizable = args[:customizable]
52
+ @text_fields = args[:text_fields]
53
+ @uploadable_files = args[:uploadable_files]
54
+ @active = args[:active]
55
+ @redirect_type = args[:redirect_type]
56
+ @id_product_redirect = args[:id_product_redirect]
57
+ @available_for_order = args[:available_for_order]
58
+ @available_date = args.fetch(:available_date, Date.today.strftime("%F"))
59
+ @condition = args.fetch(:condition, 'new')
60
+ @show_price = args[:show_price]
61
+ @indexed = 0
62
+ @visibility = args.fetch(:visibility, 'both')
63
+ @advanced_stock_management = args[:advanced_stock_management]
64
+ # date_add
65
+ # date_upd
66
+ @meta_description = args[:meta_description]
67
+ @meta_keywords = args[:meta_keywords]
68
+ @meta_title = args[:meta_title]
69
+ @link_rewrite = args[:link_rewrite]
70
+ @name = args.fetch(:name)
71
+ @description = args[:description]
72
+ @description_short = args[:description_short]
73
+ @available_now = args[:available_now]
74
+ @available_later = args[:available_later]
75
+
76
+ @id_lang = args.fetch(:id_lang)
77
+ @id_categories = args[:id_categories]
78
+ @id_features = args[:id_features]
79
+ end
80
+
81
+ def name
82
+ @name.plain.truncate(125)
83
+ end
84
+
85
+ def description_short
86
+ @description_short ? @description_short.restricted.truncate(252) : (description.truncate(252) if description)
87
+ end
88
+
89
+ def link_rewrite
90
+ @link_rewrite ? @link_rewrite.parameterize : name.parameterize
91
+ end
92
+
93
+ def reference
94
+ if @reference and not @reference.to_s.empty?
95
+ @reference.to_s.length > 32 ? Digest::MD5.hexdigest(@reference) : @reference
96
+ else
97
+ Digest::MD5.hexdigest(name)
98
+ end
99
+ end
100
+
101
+ def available_now
102
+ @available_now.plain.truncate(125)
103
+ end
104
+
105
+ def available_later
106
+ @available_later.plain.truncate(125)
107
+ end
108
+
109
+ def hash
110
+ product = {
111
+ id_supplier: id_supplier,
112
+ id_manufacturer: id_manufacturer,
113
+ id_category_default: id_category_default,
114
+ id_shop_default: id_shop_default,
115
+ id_tax_rules_group: id_tax_rules_group,
116
+ on_sale: on_sale,
117
+ online_only: online_only,
118
+ ean13: ean13,
119
+ upc: upc,
120
+ ecotax: ecotax,
121
+ minimal_quantity: minimal_quantity,
122
+ price: price,
123
+ wholesale_price: wholesale_price,
124
+ reference: reference,
125
+ active: active,
126
+ redirect_type: '404',
127
+ available_for_order: available_for_order,
128
+ condition: condition,
129
+ show_price: show_price,
130
+ name: hash_lang(name, id_lang),
131
+ description: hash_lang(description, id_lang),
132
+ description_short: hash_lang(description_short, id_lang),
133
+ link_rewrite: hash_lang(link_rewrite, id_lang),
134
+ meta_title: hash_lang(meta_title, id_lang),
135
+ meta_description: hash_lang(meta_description, id_lang),
136
+ meta_keywords: hash_lang(meta_keywords, id_lang),
137
+ available_now: hash_lang(available_now, id_lang),
138
+ available_later: hash_lang(available_later, id_lang),
139
+ associations: {} }
140
+ unless id_categories_all.empty?
141
+ product[:associations][:categories] = {}
142
+ product[:associations][:categories][:category] = hash_ids(id_categories_all)
143
+ end
144
+ if features_hash
145
+ product[:associations][:product_features] = {}
146
+ product[:associations][:product_features][:product_feature] = features_hash
147
+ end
148
+ product
149
+ end
150
+
151
+ # Find product by +reference+ and +id_supplier+, returns +id+
152
+ def id
153
+ @id ||= self.class.find_by 'filter[reference]' => reference, 'filter[id_supplier]' => id_supplier
154
+ end
155
+ alias :find? :id
156
+
157
+ # Update product with given options
158
+ def update options = {}
159
+ self.class.update(id, options)
160
+ end
161
+
162
+ # Generate hash of single feature
163
+ def feature_hash id_feature, id_feature_value
164
+ { id: id_feature, id_feature_value: id_feature_value } if id_feature and id_feature_value
165
+ end
166
+
167
+ # Generate hash of features
168
+ def features_hash
169
+ id_features.map{|f| feature_hash(f[:id_feature], f[:id_feature_value])} if id_features
170
+ end
171
+
172
+ def id_categories_all
173
+ [id_category_default, id_categories].flatten.uniq
174
+ end
175
+
176
+ class << self
177
+ def fixed_hash id
178
+ product = find id
179
+ product.delete(:position_in_category)
180
+ product.delete(:manufacturer_name)
181
+ product.delete(:quantity)
182
+ product.delete(:type)
183
+ product.delete(:associations)
184
+ product
185
+ end
186
+
187
+ def deactivate id_supplier
188
+ first = (Date.today-365).strftime("%F")
189
+ last = (Date.today-1).strftime("%F")
190
+ products = where 'filter[date_upd]' => "[#{first},#{last}]", date: 1, 'filter[id_supplier]' => id_supplier, 'filter[active]' => 1, limit: 1000
191
+ if products and !products.empty?
192
+ products.map{|p| update(p, active: 0)}
193
+ end
194
+ end
195
+ end
196
+ end
197
+ end
198
+ end
@@ -0,0 +1,69 @@
1
+ using Prestashop::Mapper::Refinement
2
+ module Prestashop
3
+ module Mapper
4
+ class ProductFeature < Model
5
+ resource :product_features
6
+ model :product_feature
7
+
8
+ attr_accessor :id, :position
9
+ attr_writer :name
10
+ attr_accessor :id_lang
11
+
12
+ def initialize args = {}
13
+ @id = args[:id]
14
+ @position = args[:position]
15
+ @name = args.fetch(:name)
16
+
17
+ @id_lang = args.fetch(:id_lang)
18
+ end
19
+
20
+ def name
21
+ @name.plain
22
+ end
23
+
24
+ def hash
25
+ validate!
26
+ { name: hash_lang(name, id_lang) }
27
+ end
28
+
29
+ def find_or_create
30
+ feature = self.class.find_in_cache name, id_lang
31
+ unless feature
32
+ feature = create
33
+ Client.clear_features_cache
34
+ end
35
+ feature[:id]
36
+ end
37
+
38
+ def validate!
39
+ raise ArgumentError, 'id lang must be number' unless id_lang.kind_of?(Integer)
40
+ raise ArgumentError, 'name must string' unless name.kind_of?(String)
41
+ end
42
+
43
+ class << self
44
+ def find_in_cache name, id_lang
45
+ Client.features_cache.find{|k| k[:name].find_lang(name, id_lang) } if Client.features_cache
46
+ end
47
+
48
+ def cache
49
+ all display: '[id,name]'
50
+ end
51
+
52
+ def create_from_hash resources, id_lang
53
+ resources = [resources] if resources.kind_of?(Hash)
54
+ if resources.kind_of?(Array)
55
+ features = []
56
+ resources.each do |resource|
57
+ if resource[:feature] and !resource[:feature].empty? and resource[:value] and !resource[:value].empty?
58
+ id_f = new(name: resource[:feature], id_lang: id_lang).find_or_create
59
+ id_fv = ProductFeatureValue.new(value: resource[:value], id_feature: id_f, id_lang: id_lang).find_or_create
60
+ features << { id_feature: id_f, id_feature_value: id_fv }
61
+ end
62
+ end
63
+ features
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,60 @@
1
+ using Prestashop::Mapper::Refinement
2
+ module Prestashop
3
+ module Mapper
4
+ class ProductFeatureValue < Model
5
+ resource :product_feature_values
6
+ model :product_feature_value
7
+
8
+ attr_accessor :id, :id_feature, :custom
9
+ attr_writer :value
10
+ attr_accessor :id_lang
11
+
12
+ def initialize args = {}
13
+ @id = args[:id]
14
+ @id_feature = args.fetch(:id_feature)
15
+ @custom = args.fetch(:custom, 0)
16
+ @value = args.fetch(:value)
17
+
18
+ @id_lang = args.fetch(:id_lang)
19
+ end
20
+
21
+ def value
22
+ @value.plain
23
+ end
24
+
25
+ def hash
26
+ validate!
27
+
28
+ { id_feature: id_feature,
29
+ custom: custom,
30
+ value: hash_lang(value, id_lang) }
31
+ end
32
+
33
+ def find_or_create
34
+ result = self.class.find_in_cache id_feature, value, id_lang
35
+ unless result
36
+ result = create
37
+ Client.clear_feature_values_cache
38
+ end
39
+ result[:id]
40
+ end
41
+
42
+ def validate!
43
+ raise ArgumentError, 'id lang must be number' unless id_lang.kind_of?(Integer)
44
+ raise ArgumentError, 'id feature must string' unless id_feature.kind_of?(Integer)
45
+ raise ArgumentError, 'custom must be 0 or 1' unless custom == 0 or custom == 1
46
+ raise ArgumentError, 'value must string' unless value.kind_of?(String)
47
+ end
48
+
49
+ class << self
50
+ def find_in_cache id_feature, value, id_lang
51
+ Client.feature_values_cache.find{|v| v[:id_feature] == id_feature and v[:value].find_lang(value, id_lang)} if Client.feature_values_cache
52
+ end
53
+
54
+ def cache
55
+ all display: '[id,id_feature,value]'
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,72 @@
1
+ using Prestashop::Mapper::Refinement
2
+ module Prestashop
3
+ module Mapper
4
+ class ProductOption < Model
5
+ resource :product_options
6
+ model :product_option
7
+
8
+ attr_accessor :id, :is_color_group, :group_type, :position, :name
9
+ attr_writer :public_name
10
+ attr_accessor :id_lang
11
+
12
+ def initialize args = {}
13
+ @id = args[:id]
14
+ @is_color_group = args.fetch(:is_color_group, 0)
15
+ @group_type = args.fetch(:group_type, 'select')
16
+ @position = args[:position]
17
+ @name = args.fetch(:name)
18
+ @public_name = args[:public_name]
19
+
20
+ @id_lang = args.fetch(:id_lang)
21
+ end
22
+
23
+ def public_name
24
+ @public_name ? @public_name : name
25
+ end
26
+
27
+ def hash
28
+ validate!
29
+
30
+ { is_color_group: is_color_group,
31
+ position: position,
32
+ group_type: group_type,
33
+ name: hash_lang(name, id_lang),
34
+ public_name: hash_lang(public_name, id_lang) }
35
+ end
36
+
37
+ def find_or_create
38
+ option = self.class.find_in_cache name, id_lang
39
+ unless option
40
+ option = create
41
+ Client.clear_options_cache
42
+ end
43
+ option[:id]
44
+ end
45
+
46
+ def validate!
47
+ raise ArgumentError, 'id lang must be number' unless id_lang.kind_of?(Integer)
48
+ raise ArgumentError, 'name must string' unless name.kind_of?(String)
49
+ raise ArgumentError, 'group_type must string' unless group_type.kind_of?(String)
50
+ end
51
+
52
+ class << self
53
+ def create_from_hash product_options, id_lang
54
+ result = []
55
+ product_options.each do |product_option|
56
+ id_o = ProductOption.new(name: product_option[:name], id_lang: id_lang).find_or_create
57
+ result << ProductOptionValue.new(name: product_option[:value], id_attribute_group: id_o, id_lang: id_lang).find_or_create
58
+ end if product_options
59
+ result
60
+ end
61
+
62
+ def find_in_cache name, id_lang
63
+ Client.options_cache.find{|k| k[:name].find_lang(name, id_lang) } if Client.options_cache
64
+ end
65
+
66
+ def cache
67
+ all display: '[id,name]'
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,56 @@
1
+ using Prestashop::Mapper::Refinement
2
+ module Prestashop
3
+ module Mapper
4
+ class ProductOptionValue < Model
5
+ resource :product_option_values
6
+ model :product_option_value
7
+
8
+ attr_accessor :id, :id_attribute_group, :color, :position, :name
9
+ attr_accessor :id_lang
10
+
11
+ def initialize args = {}
12
+ @id = args[:id]
13
+ @id_attribute_group = args.fetch(:id_attribute_group)
14
+ @color = args.fetch(:color, 0)
15
+ @position = args[:position]
16
+ @name = args.fetch(:name)
17
+
18
+ @id_lang = args.fetch(:id_lang)
19
+ end
20
+
21
+ def hash
22
+ validate!
23
+ { name: hash_lang(name, id_lang),
24
+ id_attribute_group: id_attribute_group,
25
+ color: color }
26
+ end
27
+
28
+ def find_or_create
29
+ result = self.class.find_in_cache id_attribute_group, name, id_lang
30
+ unless result
31
+ result = create
32
+ Client.clear_option_values_cache
33
+ end
34
+ result[:id]
35
+ end
36
+
37
+ # Supplier must have 1/0 as active and name must be string
38
+ def validate!
39
+ raise ArgumentError, 'id lang must be number' unless id_lang.kind_of?(Integer)
40
+ raise ArgumentError, 'name must string' unless name.kind_of?(String)
41
+ raise ArgumentError, 'id attribute group must be number' unless id_attribute_group.kind_of?(Integer)
42
+ raise ArgumentError, 'color must be true or false' unless color == 1 or color == 0
43
+ end
44
+
45
+ class << self
46
+ def find_in_cache id_attribute_group, name, id_lang
47
+ Client.option_values_cache.find{|v| v[:id_attribute_group] == id_attribute_group and v[:name].find_lang(name, id_lang)} if Client.option_values_cache
48
+ end
49
+
50
+ def cache
51
+ all display: '[id,id_attribute_group,name]'
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end