[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) countries = orm_country.browse(cr, SUPERUSER_ID, country_ids, context)
states_ids = state_orm.search(cr, SUPERUSER_ID, [], context=context) states_ids = state_orm.search(cr, SUPERUSER_ID, [], context=context)
states = state_orm.browse(cr, SUPERUSER_ID, states_ids, 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 = {} checkout = {}
if not data: if not data:
if request.uid != request.website.user_id.id: 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) ) checkout.update( self.checkout_parse("billing", partner) )
shipping_ids = orm_partner.search(cr, SUPERUSER_ID, [("parent_id", "=", partner.id), ('type', "=", 'delivery')], context=context)
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
else: else:
order = request.website.sale_get_order(force_create=1, context=context) order = request.website.sale_get_order(force_create=1, context=context)
if order.partner_id: if order.partner_id:
@ -325,17 +324,47 @@ class website_sale(http.Controller):
checkout.update( self.checkout_parse("billing", order.partner_id) ) checkout.update( self.checkout_parse("billing", order.partner_id) )
else: else:
checkout = self.checkout_parse('billing', data) 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.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 = { values = {
'countries': countries, 'countries': countries,
'states': states, 'states': states,
'checkout': checkout, 'checkout': checkout,
'shipping_different': checkout.get('shipping_different'), 'shipping_id': partner.id != shipping_id and shipping_id or 0,
'shippings': shippings,
'error': {}, 'error': {},
} }
return values return values
mandatory_billing_fields = ["name", "phone", "email", "street", "city", "country_id", "zip"] 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 if not check_func(cr, uid, vat_country, vat_number, context=None): # simple_vat_check
error["vat"] = 'error' error["vat"] = 'error'
if data.get("shipping_different"): if data.get("shipping_id") == -1:
for field_name in self.mandatory_shipping_fields: for field_name in self.mandatory_shipping_fields:
field_name = 'shipping_' + field_name field_name = 'shipping_' + field_name
if not data.get(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) partner_id = orm_partner.create(cr, SUPERUSER_ID, billing_info, context=context)
# create a new shipping partner # create a new shipping partner
shipping_id = None if checkout.get('shipping_id') == -1:
if checkout.get('shipping_different'):
shipping_info = self.checkout_parse('shipping', checkout, True) shipping_info = self.checkout_parse('shipping', checkout, True)
shipping_info['type'] = 'delivery' shipping_info['type'] = 'delivery'
shipping_info['parent_id'] = partner_id 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 = { order_info = {
'partner_id': partner_id, 'partner_id': partner_id,
'message_follower_ids': [(4, partner_id)], 'message_follower_ids': [(4, partner_id)],
'partner_invoice_id': 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.update(registry.get('sale.order').onchange_partner_id(cr, SUPERUSER_ID, [], partner_id, context=context)['value'])
order_info.pop('user_id') 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) 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) return request.website.render("website_sale.checkout", values)
self.checkout_form_save(values["checkout"]) self.checkout_form_save(values["checkout"])
request.session['sale_last_order_id'] = order.id request.session['sale_last_order_id'] = order.id
return request.redirect("/shop/payment") return request.redirect("/shop/payment")

View File

@ -1,10 +1,17 @@
$(document).ready(function () { $(document).ready(function () {
var $shippingDifferent = $(".oe_website_sale input[name='shipping_different']"); var $shippingDifferent = $(".oe_website_sale select[name='shipping_id']");
if ($shippingDifferent.is(':checked')) { $shippingDifferent.change(function (event) {
$(".oe_website_sale .js_shipping").show(); var value = +$shippingDifferent.val();
} var data = $shippingDifferent.find("option:selected").data();
$shippingDifferent.change(function () { var $snipping = $(".oe_website_sale .js_shipping");
$(".oe_website_sale .js_shipping").toggle(); 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 // change for css

View File

@ -865,7 +865,7 @@
</select> </select>
</div> </div>
<div t-attf-class="form-group #{error.get('country_id') and 'has-error' or ''} col-lg-6"> <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"> <select name="country_id" class="form-control">
<option value="">Country...</option> <option value="">Country...</option>
<t t-foreach="countries or []" t-as="country"> <t t-foreach="countries or []" t-as="country">
@ -876,40 +876,52 @@
<div class="clearfix"/> <div class="clearfix"/>
<div class="form-group col-lg-6"> <div class="form-group col-lg-12">
<label> <label>Shipping</label>
<input type="checkbox" name="shipping_different" t-att-checked="checkout.get('shipping_different')"/> <select name="shipping_id" class="form-control">
<span>Ship to a different address</span> <option value="0">Ship to the same address</option>
</label> <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> </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> <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"> <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> <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', '')"/> <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>
<div t-attf-class="form-group #{error.get('shipping_phone') and 'has-error' or ''} col-lg-6"> <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> <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', '')"/> <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>
<div t-attf-class="form-group #{error.get('shipping_street') and 'has-error' or ''} col-lg-6"> <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> <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', '')"/> <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>
<div class="clearfix"/> <div class="clearfix"/>
<div t-attf-class="form-group #{error.get('shipping_city') and 'has-error' or ''} col-lg-6"> <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> <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', '')"/> <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>
<div t-attf-class="form-group #{error.get('shipping_zip') and 'has-error' or ''} col-lg-6"> <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> <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', '')"/> <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>
<div t-attf-class="form-group #{error.get('shipping_state_id') and 'has-error' or ''} col-lg-6"> <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> <label class="control-label" for="shipping_state_id" style="font-weight: normal">State / Province</label>
<select name="shipping_state_id" class="form-control"> <select name="shipping_state_id" class="form-control" t-att-readonly=" 'readonly' if shipping_id &gt;= 0 else ''">
<option value="">State / Province...</option> <option value="">State / Province...</option>
<t t-foreach="states or []" t-as="state"> <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> <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> </select>
</div> </div>
<div t-attf-class="form-group #{error.get('shipping_country_id') and 'has-error' or ''} col-lg-6"> <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> <label class="control-label" for="shipping_country_id">Country</label>
<select name="shipping_country_id" class="form-control"> <select name="shipping_country_id" class="form-control" t-att-readonly=" 'readonly' if shipping_id &gt;= 0 else ''">
<option value="">Country...</option> <option value="">Country...</option>
<t t-foreach="countries or []" t-as="country"> <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> <option t-att-value="country.id" t-att-selected="country.id == checkout.get('shipping_country_id')"><t t-esc="country.name"/></option>