[FIX] website_sale: everytime you want to change both billing & delivery address, any change creates duplicates in the backend

This commit is contained in:
Christophe Matthieu 2014-08-01 17:11:20 +02:00
parent 9bf1203c33
commit ce7212d5d7
3 changed files with 90 additions and 42 deletions

View File

@ -304,18 +304,17 @@ class website_sale(http.Controller):
countries = orm_country.browse(cr, SUPERUSER_ID, country_ids, context)
states_ids = state_orm.search(cr, SUPERUSER_ID, [], context=context)
states = state_orm.browse(cr, SUPERUSER_ID, states_ids, context)
partner = orm_user.browse(cr, SUPERUSER_ID, request.uid, context).partner_id
order = None
shipping_id = None
shipping_ids = []
checkout = {}
if not data:
if request.uid != request.website.user_id.id:
partner = orm_user.browse(cr, SUPERUSER_ID, request.uid, context).partner_id
checkout.update( self.checkout_parse("billing", partner) )
shipping_ids = orm_partner.search(cr, SUPERUSER_ID, [("parent_id", "=", partner.id), ('type', "=", 'delivery')], limit=1, context=context)
if shipping_ids:
shipping = orm_user.browse(cr, SUPERUSER_ID, request.uid, context)
checkout.update( self.checkout_parse("shipping", shipping) )
checkout['shipping_different'] = True
shipping_ids = orm_partner.search(cr, SUPERUSER_ID, [("parent_id", "=", partner.id), ('type', "=", 'delivery')], context=context)
else:
order = request.website.sale_get_order(force_create=1, context=context)
if order.partner_id:
@ -325,17 +324,47 @@ class website_sale(http.Controller):
checkout.update( self.checkout_parse("billing", order.partner_id) )
else:
checkout = self.checkout_parse('billing', data)
if data.get("shipping_different"):
try:
shipping_id = int(data["shipping_id"])
except ValueError:
pass
if shipping_id == -1:
checkout.update(self.checkout_parse('shipping', data))
checkout["shipping_different"] = True
if shipping_id is None:
if not order:
order = request.website.sale_get_order(context=context)
if order and order.partner_shipping_id:
shipping_id = order.partner_shipping_id.id
shipping_ids = list(set(shipping_ids) - set([partner.id]))
if shipping_id == partner.id:
shipping_id = 0
elif shipping_id > 0 and shipping_id not in shipping_ids:
shipping_ids.append(shipping_id)
elif shipping_id is None and shipping_ids:
shipping_id = shipping_ids[0]
ctx = dict(context, show_address=1)
shippings = []
if shipping_ids:
shippings = shipping_ids and orm_partner.browse(cr, SUPERUSER_ID, list(shipping_ids), ctx) or []
if shipping_id > 0:
shipping = orm_partner.browse(cr, SUPERUSER_ID, shipping_id, ctx)
checkout.update( self.checkout_parse("shipping", shipping) )
checkout['shipping_id'] = shipping_id
values = {
'countries': countries,
'states': states,
'checkout': checkout,
'shipping_different': checkout.get('shipping_different'),
'shipping_id': partner.id != shipping_id and shipping_id or 0,
'shippings': shippings,
'error': {},
}
return values
mandatory_billing_fields = ["name", "phone", "email", "street", "city", "country_id", "zip"]
@ -395,7 +424,7 @@ class website_sale(http.Controller):
if not check_func(cr, uid, vat_country, vat_number, context=None): # simple_vat_check
error["vat"] = 'error'
if data.get("shipping_different"):
if data.get("shipping_id") == -1:
for field_name in self.mandatory_shipping_fields:
field_name = 'shipping_' + field_name
if not data.get(field_name):
@ -431,21 +460,20 @@ class website_sale(http.Controller):
partner_id = orm_partner.create(cr, SUPERUSER_ID, billing_info, context=context)
# create a new shipping partner
shipping_id = None
if checkout.get('shipping_different'):
if checkout.get('shipping_id') == -1:
shipping_info = self.checkout_parse('shipping', checkout, True)
shipping_info['type'] = 'delivery'
shipping_info['parent_id'] = partner_id
shipping_id = orm_partner.create(cr, SUPERUSER_ID, shipping_info, context)
checkout['shipping_id'] = orm_partner.create(cr, SUPERUSER_ID, shipping_info, context)
order_info = {
'partner_id': partner_id,
'message_follower_ids': [(4, partner_id)],
'partner_invoice_id': partner_id,
'partner_shipping_id': shipping_id or partner_id
}
order_info.update(registry.get('sale.order').onchange_partner_id(cr, SUPERUSER_ID, [], partner_id, context=context)['value'])
order_info.pop('user_id')
order_info.update(partner_shipping_id=checkout.get('shipping_id') or partner_id)
order_obj.write(cr, SUPERUSER_ID, [order.id], order_info, context=context)
@ -482,6 +510,7 @@ class website_sale(http.Controller):
return request.website.render("website_sale.checkout", values)
self.checkout_form_save(values["checkout"])
request.session['sale_last_order_id'] = order.id
return request.redirect("/shop/payment")

View File

@ -1,10 +1,17 @@
$(document).ready(function () {
var $shippingDifferent = $(".oe_website_sale input[name='shipping_different']");
if ($shippingDifferent.is(':checked')) {
$(".oe_website_sale .js_shipping").show();
}
$shippingDifferent.change(function () {
$(".oe_website_sale .js_shipping").toggle();
var $shippingDifferent = $(".oe_website_sale select[name='shipping_id']");
$shippingDifferent.change(function (event) {
var value = +$shippingDifferent.val();
var data = $shippingDifferent.find("option:selected").data();
var $snipping = $(".oe_website_sale .js_shipping");
var $inputs = $snipping.find("input,select");
$snipping.toggle(!!value);
$inputs.attr("readonly", value <= 0 ? null : "readonly" ).prop("readonly", value <= 0 ? null : "readonly" );
$inputs.each(function () {
$(this).val( data[$(this).attr("name")] || "" );
});
});
// change for css

View File

@ -865,7 +865,7 @@
</select>
</div>
<div t-attf-class="form-group #{error.get('country_id') and 'has-error' or ''} col-lg-6">
<label class="control-label" for="contact_name">Country</label>
<label class="control-label" for="country_id">Country</label>
<select name="country_id" class="form-control">
<option value="">Country...</option>
<t t-foreach="countries or []" t-as="country">
@ -876,40 +876,52 @@
<div class="clearfix"/>
<div class="form-group col-lg-6">
<label>
<input type="checkbox" name="shipping_different" t-att-checked="checkout.get('shipping_different')"/>
<span>Ship to a different address</span>
</label>
<div class="form-group col-lg-12">
<label>Shipping</label>
<select name="shipping_id" class="form-control">
<option value="0">Ship to the same address</option>
<t t-foreach="shippings" t-as="shipping">
<option t-att-value="shipping.id" t-att-selected="shipping.id == shipping_id"
t-att-data-shipping_name="shipping.name"
t-att-data-shipping_phone="shipping.phone"
t-att-data-shipping_street="shipping.street"
t-att-data-shipping_city="shipping.city"
t-att-data-shipping_zip="shipping.zip"
t-att-data-shipping_state_id="shipping.state_id and shipping.state_id.id"
t-att-data-shipping_country_id="shipping.country_id and shipping.country_id.id"
><t t-esc="', '.join('\n'.join(shipping.name_get()[0][1].split(',')).split('\n')[1:])"/></option>
</t>
<option value="-1">-- Create a new address --</option>
</select>
</div>
</div>
<div class="js_shipping row mb16" t-att-style="not checkout.get('shipping_different') and 'display:none' or ''">
<div class="js_shipping row mb16" t-att-style="not shipping_id and 'display:none' or ''">
<h3 class="oe_shipping col-lg-12 mt16">Shipping Information</h3>
<div t-attf-class="form-group #{error.get('shipping_name') and 'has-error' or ''} col-lg-6">
<label class="control-label" for="contact_name">Name (Shipping)</label>
<input type="text" name="shipping_name" class="form-control" t-att-value="checkout.get('shipping_name', '')"/>
<label class="control-label" for="shipping_name">Name (Shipping)</label>
<input type="text" name="shipping_name" class="form-control" t-att-value="checkout.get('shipping_name', '')" t-att-readonly="'readonly' if shipping_id &gt;= 0 else ''"/>
</div>
<div t-attf-class="form-group #{error.get('shipping_phone') and 'has-error' or ''} col-lg-6">
<label class="control-label" for="contact_name">Phone</label>
<input type="tel" name="shipping_phone" class="form-control" t-att-value="checkout.get('shipping_phone', '')"/>
<label class="control-label" for="shipping_phone">Phone</label>
<input type="tel" name="shipping_phone" class="form-control" t-att-value="checkout.get('shipping_phone', '')" t-att-readonly=" 'readonly' if shipping_id &gt;= 0 else ''"/>
</div>
<div t-attf-class="form-group #{error.get('shipping_street') and 'has-error' or ''} col-lg-6">
<label class="control-label" for="contact_name">Street</label>
<input type="text" name="shipping_street" class="form-control" t-att-value="checkout.get('shipping_street', '')"/>
<label class="control-label" for="shipping_street">Street</label>
<input type="text" name="shipping_street" class="form-control" t-att-value="checkout.get('shipping_street', '')" t-att-readonly=" 'readonly' if shipping_id &gt;= 0 else ''"/>
</div>
<div class="clearfix"/>
<div t-attf-class="form-group #{error.get('shipping_city') and 'has-error' or ''} col-lg-6">
<label class="control-label" for="contact_name">City</label>
<input type="text" name="shipping_city" class="form-control" t-att-value="checkout.get('shipping_city', '')"/>
<label class="control-label" for="shipping_city">City</label>
<input type="text" name="shipping_city" class="form-control" t-att-value="checkout.get('shipping_city', '')" t-att-readonly=" 'readonly' if shipping_id &gt;= 0 else ''"/>
</div>
<div t-attf-class="form-group #{error.get('shipping_zip') and 'has-error' or ''} col-lg-6">
<label class="control-label" for="contact_name">Zip / Postal Code</label>
<input type="text" name="shipping_zip" class="form-control" t-att-value="checkout.get('shipping_zip', '')"/>
<label class="control-label" for="shipping_zip">Zip / Postal Code</label>
<input type="text" name="shipping_zip" class="form-control" t-att-value="checkout.get('shipping_zip', '')" t-att-readonly=" 'readonly' if shipping_id &gt;= 0 else ''"/>
</div>
<div t-attf-class="form-group #{error.get('shipping_state_id') and 'has-error' or ''} col-lg-6">
<label class="control-label" for="contact_name" style="font-weight: normal">State / Province</label>
<select name="shipping_state_id" class="form-control">
<label class="control-label" for="shipping_state_id" style="font-weight: normal">State / Province</label>
<select name="shipping_state_id" class="form-control" t-att-readonly=" 'readonly' if shipping_id &gt;= 0 else ''">
<option value="">State / Province...</option>
<t t-foreach="states or []" t-as="state">
<option t-att-value="state.id" t-att-selected="state.id == checkout.get('shipping_state_id')"><t t-esc="state.name"/></option>
@ -917,8 +929,8 @@
</select>
</div>
<div t-attf-class="form-group #{error.get('shipping_country_id') and 'has-error' or ''} col-lg-6">
<label class="control-label" for="contact_name">Country</label>
<select name="shipping_country_id" class="form-control">
<label class="control-label" for="shipping_country_id">Country</label>
<select name="shipping_country_id" class="form-control" t-att-readonly=" 'readonly' if shipping_id &gt;= 0 else ''">
<option value="">Country...</option>
<t t-foreach="countries or []" t-as="country">
<option t-att-value="country.id" t-att-selected="country.id == checkout.get('shipping_country_id')"><t t-esc="country.name"/></option>