[IMP] account_analytic_analysis : Add recurring field to product and add product...
[odoo/odoo.git] / addons / website_sale / static / src / js / website_sale.js
1 $(document).ready(function () {
2 $('.oe_website_sale').each(function () {
3     var oe_website_sale = this;
4
5     var $shippingDifferent = $("select[name='shipping_id']", oe_website_sale);
6     $shippingDifferent.change(function (event) {
7         var value = +$shippingDifferent.val();
8         var data = $shippingDifferent.find("option:selected").data();
9         var $snipping = $(".js_shipping", oe_website_sale);
10         var $inputs = $snipping.find("input,select");
11
12         $snipping.toggle(!!value);
13         $inputs.attr("readonly", value <= 0 ? null : "readonly" ).prop("readonly", value <= 0 ? null : "readonly" );
14
15         $inputs.each(function () {
16             $(this).val( data[$(this).attr("name")] || "" );
17         });
18     });
19
20     // change for css
21     $(oe_website_sale).on('mouseup touchend', '.js_publish', function (ev) {
22         $(ev.currentTarget).parents(".thumbnail").toggleClass("disabled");
23     });
24
25     $(oe_website_sale).on("change", ".oe_cart input.js_quantity", function () {
26         var $input = $(this);
27         var value = parseInt($input.val(), 10);
28         var line_id = parseInt($input.data('line-id'),10);
29         if (isNaN(value)) value = 0;
30         openerp.jsonRpc("/shop/cart/update_json", 'call', {
31             'line_id': line_id,
32             'product_id': parseInt($input.data('product-id'),10),
33             'set_qty': value})
34             .then(function (data) {
35                 if (!data.quantity) {
36                     location.reload();
37                     return;
38                 }
39                 var $q = $(".my_cart_quantity");
40                 $q.parent().parent().removeClass("hidden", !data.quantity);
41                 $q.html(data.cart_quantity).hide().fadeIn(600);
42
43                 $input.val(data.quantity);
44                 $('.js_quantity[data-line-id='+line_id+']').val(data.quantity).html(data.quantity);
45                 $("#cart_total").replaceWith(data['website_sale.total']);
46             });
47     });
48
49     // hack to add and rome from cart with json
50     $(oe_website_sale).on('click', 'a.js_add_cart_json', function (ev) {
51         ev.preventDefault();
52         var $link = $(ev.currentTarget);
53         var $input = $link.parent().parent().find("input");
54         var min = parseFloat($input.data("min") || 0);
55         var max = parseFloat($input.data("max") || Infinity);
56         var quantity = ($link.has(".fa-minus").length ? -1 : 1) + parseFloat($input.val(),10);
57         $input.val(quantity > min ? (quantity < max ? quantity : max) : min);
58         $('input[name="'+$input.attr("name")+'"]').val(quantity > min ? (quantity < max ? quantity : max) : min);
59         $input.change();
60         return false;
61     });
62
63     $('.a-submit', oe_website_sale).off('click').on('click', function () {
64         $(this).closest('form').submit();
65     });
66     $('form.js_attributes input, form.js_attributes select', oe_website_sale).on('change', function () {
67         $(this).closest("form").submit();
68     });
69
70     // change price when they are variants
71     $('form.js_add_cart_json label', oe_website_sale).on('mouseup touchend', function (ev) {
72         var $label = $(this);
73         var $price = $label.parents("form:first").find(".oe_price .oe_currency_value");
74         if (!$price.data("price")) {
75             $price.data("price", parseFloat($price.text()));
76         }
77         var value = $price.data("price") + parseFloat($label.find(".badge span").text() || 0);
78         var dec = value % 1;
79         $price.html(value + (dec < 0.01 ? ".00" : (dec < 1 ? "0" : "") ));
80     });
81     // hightlight selected color
82     $('.css_attribute_color input', oe_website_sale).on('change', function (ev) {
83         $('.css_attribute_color').removeClass("active");
84         $('.css_attribute_color:has(input:checked)').addClass("active");
85     });
86
87     function price_to_str(price) {
88         price = Math.round(price * 100) / 100;
89         var dec = Math.round((price % 1) * 100);
90         return price + (dec ? '' : '.0') + (dec%10 ? '' : '0');
91     }
92
93     $(oe_website_sale).on('change', 'input.js_product_change', function (ev) {
94         var $parent = $(this).closest('.js_product');
95         $parent.find(".oe_default_price:first .oe_currency_value").html( price_to_str(+$(this).data('lst_price')) );
96         $parent.find(".oe_price:first .oe_currency_value").html(price_to_str(+$(this).data('price')) );
97
98         var $img = $(this).closest('tr.js_product, .oe_website_sale').find('span[data-oe-model^="product."][data-oe-type="image"] img, img.product_detail_img');
99         $img.attr("src", "/website/image/product.product/" + $(this).val() + "/image");
100     });
101
102     $(oe_website_sale).on('change', 'input.js_variant_change, select.js_variant_change', function (ev) {
103         var $ul = $(this).parents('ul.js_add_cart_variants:first');
104         var $parent = $ul.closest('.js_product');
105         var $product_id = $parent.find('input.product_id').first();
106         var $price = $parent.find(".oe_price:first .oe_currency_value");
107         var $default_price = $parent.find(".oe_default_price:first .oe_currency_value");
108         var variant_ids = $ul.data("attribute_value_ids");
109         var values = [];
110         $parent.find('input.js_variant_change:checked, select.js_variant_change').each(function () {
111             values.push(+$(this).val());
112         });
113
114         $parent.find("label").removeClass("text-muted css_not_available");
115
116         var product_id = false;
117         for (var k in variant_ids) {
118             if (_.isEmpty(_.difference(variant_ids[k][1], values))) {
119                 $price.html(price_to_str(variant_ids[k][2]));
120                 $default_price.html(price_to_str(variant_ids[k][3]));
121                 if (variant_ids[k][3]-variant_ids[k][2]>0.2) {
122                     $default_price.closest('.oe_website_sale').addClass("discount");
123                 } else {
124                     $default_price.closest('.oe_website_sale').removeClass("discount");
125                 }
126                 product_id = variant_ids[k][0];
127                 break;
128             }
129         }
130
131         if (product_id) {
132             var $img = $(this).closest('tr.js_product, .oe_website_sale').find('span[data-oe-model^="product."][data-oe-type="image"] img, img.product_detail_img');
133             $img.attr("src", "/website/image/product.product/" + product_id + "/image");
134             $img.parent().attr('data-oe-model', 'product.product').attr('data-oe-id', product_id)
135                 .data('oe-model', 'product.product').data('oe-id', product_id);
136         }
137
138         $parent.find("input.js_variant_change:radio, select.js_variant_change").each(function () {
139             var $input = $(this);
140             var id = +$input.val();
141             var values = [id];
142
143             $parent.find("ul:not(:has(input.js_variant_change[value='" + id + "'])) input.js_variant_change:checked, select").each(function () {
144                 values.push(+$(this).val());
145             });
146
147             for (var k in variant_ids) {
148                 if (!_.difference(values, variant_ids[k][1]).length) {
149                     return;
150                 }
151             }
152             $input.closest("label").addClass("css_not_available");
153             $input.find("option[value='" + id + "']").addClass("css_not_available");
154         });
155
156         if (product_id) {
157             $parent.removeClass("css_not_available");
158             $product_id.val(product_id);
159             $parent.find(".js_check_product").removeAttr("disabled");
160         } else {
161             $parent.addClass("css_not_available");
162             $product_id.val(0);
163             $parent.find(".js_check_product").attr("disabled", "disabled");
164         }
165     });
166     $('ul.js_add_cart_variants', oe_website_sale).each(function () {
167         $('input.js_variant_change, select.js_variant_change', this).first().trigger('change');
168     });
169
170     $(oe_website_sale).on('change', "select[name='country_id']", function () {
171         var $select = $("select[name='state_id']");
172         $select.find("option:not(:first)").hide();
173         var nb = $select.find("option[data-country_id="+($(this).val() || 0)+"]").show().size();
174         $select.parent().toggle(nb>1);
175     });
176     $(oe_website_sale).find("select[name='country_id']").change();
177
178     $(oe_website_sale).on('change', "select[name='shipping_country_id']", function () {
179         var $select = $("select[name='shipping_state_id']");
180         $select.find("option:not(:first)").hide();
181         var nb = $select.find("option[data-country_id="+($(this).val() || 0)+"]").show().size();
182         $select.parent().toggle(nb>1);
183     });
184     $(oe_website_sale).find("select[name='shipping_country_id']").change();
185 });
186 });