-
Notifications
You must be signed in to change notification settings - Fork 287
/
website_sale.js
245 lines (222 loc) · 11.5 KB
/
website_sale.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
75
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
$(document).ready(function () {
$('.oe_website_sale').each(function () {
var oe_website_sale = this;
var $shippingDifferent = $("select[name='shipping_id']", oe_website_sale);
$shippingDifferent.change(function (event) {
var value = +$shippingDifferent.val();
var data = $shippingDifferent.find("option:selected").data();
var $snipping = $(".js_shipping", oe_website_sale);
var $inputs = $snipping.find("input");
var $selects = $snipping.find("select");
$snipping.toggle(!!value);
$inputs.attr("readonly", value <= 0 ? null : "readonly" ).prop("readonly", value <= 0 ? null : "readonly" );
$selects.attr("disabled", value <= 0 ? null : "disabled" ).prop("disabled", value <= 0 ? null : "disabled" );
$inputs.each(function () {
$(this).val( data[$(this).attr("name")] || "" );
});
});
// change for css
$(oe_website_sale).on('mouseup touchend', '.js_publish', function (ev) {
$(ev.currentTarget).parents(".thumbnail").toggleClass("disabled");
});
$(oe_website_sale).on("change", ".oe_cart input.js_quantity", function () {
var $input = $(this);
var value = parseInt($input.val(), 10);
var line_id = parseInt($input.data('line-id'),10);
if (isNaN(value)) value = 0;
openerp.jsonRpc("/shop/cart/update_json", 'call', {
'line_id': line_id,
'product_id': parseInt($input.data('product-id'),10),
'set_qty': value})
.then(function (data) {
if (!data.quantity) {
location.reload();
return;
}
var $q = $(".my_cart_quantity");
$q.parent().parent().removeClass("hidden", !data.quantity);
$q.html(data.cart_quantity).hide().fadeIn(600);
$input.val(data.quantity);
$('.js_quantity[data-line-id='+line_id+']').val(data.quantity).html(data.quantity);
$("#cart_total").replaceWith(data['website_sale.total']);
});
});
// hack to add and rome from cart with json
$(oe_website_sale).on('click', 'a.js_add_cart_json', function (ev) {
ev.preventDefault();
var $link = $(ev.currentTarget);
var $input = $link.parent().parent().find("input");
var min = parseFloat($input.data("min") || 0);
var max = parseFloat($input.data("max") || Infinity);
var quantity = ($link.has(".fa-minus").length ? -1 : 1) + parseFloat($input.val(),10);
$input.val(quantity > min ? (quantity < max ? quantity : max) : min);
$('input[name="'+$input.attr("name")+'"]').val(quantity > min ? (quantity < max ? quantity : max) : min);
$input.change();
return false;
});
$('.oe_website_sale .a-submit, #comment .a-submit').off('click').on('click', function () {
$(this).closest('form').submit();
});
$('form.js_attributes input, form.js_attributes select', oe_website_sale).on('change', function () {
$(this).closest("form").submit();
});
// change price when they are variants
$('form.js_add_cart_json label', oe_website_sale).on('mouseup touchend', function (ev) {
var $label = $(this);
var $price = $label.parents("form:first").find(".oe_price .oe_currency_value");
if (!$price.data("price")) {
$price.data("price", parseFloat($price.text()));
}
var value = $price.data("price") + parseFloat($label.find(".badge span").text() || 0);
var dec = value % 1;
$price.html(value + (dec < 0.01 ? ".00" : (dec < 1 ? "0" : "") ));
});
// hightlight selected color
$('.css_attribute_color input', oe_website_sale).on('change', function (ev) {
$('.css_attribute_color').removeClass("active");
$('.css_attribute_color:has(input:checked)').addClass("active");
});
function price_to_str(price) {
price = Math.round(price * 100) / 100;
var dec = Math.round((price % 1) * 100);
return price + (dec ? '' : '.0') + (dec%10 ? '' : '0');
}
$(oe_website_sale).on('change', 'input.js_product_change', function (ev) {
var $parent = $(this).closest('.js_product');
$parent.find(".oe_default_price:first .oe_currency_value").html( price_to_str(+$(this).data('lst_price')) );
$parent.find(".oe_availability").html(+$(this).data('stock_state') );
$parent.find(".oe_price:first .oe_currency_value").html(price_to_str(+$(this).data('price')) );
var $img = $(this).closest('tr.js_product, .oe_website_sale').find('span[data-oe-model^="product."][data-oe-type="image"] img:first, img.product_detail_img');
$img.attr("src", "/website/image/product.product/" + $(this).val() + "/image");
});
$(oe_website_sale).on('change', 'input.js_variant_change, select.js_variant_change', function (ev) {
/* This method is the reason why everithing was copied from original, the final functionality is
to retrieve an extra value on the product variant and show wheter is available or not
this code was made by Oscar Alcala @oscarolar, please forgive the stupid thing I had to do,
but since the assholes on Odoo did not created any inheritance mechanism, I had to fuck
things up.*/
var $ul = $(this).parents('ul.js_add_cart_variants:first');
var $parent = $ul.closest('.js_product');
var $product_id = $parent.find('input.product_id').first();
var $price = $parent.find(".oe_price:first .oe_currency_value");
var $default_price = $parent.find(".oe_default_price:first .oe_currency_value");
var variant_ids = $ul.data("attribute_value_ids");
var $stock_state = $(".stock_state");
var $stock_location_state = $(".stock_location_state");
var $div_location_state = $(".product_variant_states");
var $stock_delay = $(".stock_delay");
var values = [];
$parent.find('input.js_variant_change:checked, select.js_variant_change').each(function () {
values.push(+$(this).val());
});
$parent.find("label").removeClass("text-muted css_not_available");
var product_id = false;
for (var k in variant_ids) {
if (_.isEmpty(_.difference(variant_ids[k][1], values))) {
$price.html(price_to_str(variant_ids[k][2]));
$default_price.html(price_to_str(variant_ids[k][3]));
if (variant_ids[k][3]-variant_ids[k][2]>0.2) {
$default_price.closest('.oe_website_sale').addClass("discount");
} else {
$default_price.closest('.oe_website_sale').removeClass("discount");
}
/*<oscar>*/
$stock_state.removeClass (function (index, css){
return(css.match(/(^|\s)label-\S+/g) || []).join(' ');
});
var new_class = 'label-default';
var text = '';
switch(variant_ids[k][4]){
case 1:
new_class = 'label-success'
text = 'Available'
$("#add_to_cart").removeClass('disabled')
$("#similar_products_vx").addClass('hidden')
// $stock_delay.addClass('hidden')
break;
case 2:
new_class = 'label-danger'
text = 'Not Available'
$("#add_to_cart").addClass('disabled')
$("#similar_products_vx").removeClass('hidden')
// $stock_delay.removeClass('hidden')
break;
case 3:
new_class = 'label-warning'
text = 'Low Availability'
$("#add_to_cart").removeClass('disabled')
$("#similar_products_vx").addClass('hidden')
// $stock_delay.addClass('hidden')
break;
case 4:
new_class = 'label-primary'
text = 'On Request'
$("#add_to_cart").removeClass('disabled')
$("#similar_products_vx").addClass('hidden')
// $stock_delay.removeClass('hidden')
break;
}
if (variant_ids[k][5]) {
days = variant_ids[k][5];
// $stock_delay.text("Product Available in "+days[0]+'/'+days[1]+'/'+days[2]+"");
}
$stock_state.addClass(new_class);
$stock_state.text(text);
$div_location_state.addClass('hidden');
select =".product_variant_states#product-variant-" + variant_ids[k][0];
$(select).removeClass('hidden');
/*</oscar>*/
product_id = variant_ids[k][0];
break;
}
}
if (product_id) {
var $img = $(this).closest('tr.js_product, .oe_website_sale').find('span[data-oe-model^="product."][data-oe-type="image"] img:first, img.product_detail_img');
$img.attr("src", "/website/image/product.product/" + product_id + "/image");
$img.parent().attr('data-oe-model', 'product.product').attr('data-oe-id', product_id)
.data('oe-model', 'product.product').data('oe-id', product_id);
}
$parent.find("input.js_variant_change:radio, select.js_variant_change").each(function () {
var $input = $(this);
var id = +$input.val();
var values = [id];
$parent.find("ul:not(:has(input.js_variant_change[value='" + id + "'])) input.js_variant_change:checked, select").each(function () {
values.push(+$(this).val());
});
for (var k in variant_ids) {
if (!_.difference(values, variant_ids[k][1]).length) {
return;
}
}
$input.closest("label").addClass("css_not_available");
$input.find("option[value='" + id + "']").addClass("css_not_available");
});
if (product_id) {
$parent.removeClass("css_not_available");
$product_id.val(product_id);
$parent.find(".js_check_product").removeAttr("disabled");
} else {
$parent.addClass("css_not_available");
$product_id.val(0);
$parent.find(".js_check_product").attr("disabled", "disabled");
}
});
$('ul.js_add_cart_variants', oe_website_sale).each(function () {
$('input.js_variant_change, select.js_variant_change', this).first().trigger('change');
});
$(oe_website_sale).on('change', "select[name='country_id']", function () {
var $select = $("select[name='state_id']");
$select.find("option:not(:first)").hide();
var nb = $select.find("option[data-country_id="+($(this).val() || 0)+"]").show().size();
$select.parent().toggle(nb>1);
});
$(oe_website_sale).find("select[name='country_id']").change();
$(oe_website_sale).on('change', "select[name='shipping_country_id']", function () {
var $select = $("select[name='shipping_state_id']");
$select.find("option:not(:first)").hide();
var nb = $select.find("option[data-country_id="+($(this).val() || 0)+"]").show().size();
$select.parent().toggle(nb>1);
});
$(oe_website_sale).find("select[name='shipping_country_id']").change();
});
});