diff --git a/delivery_carrier_label_roulier/stock.py b/delivery_carrier_label_roulier/stock.py index fd64c9f91b..160b80e923 100644 --- a/delivery_carrier_label_roulier/stock.py +++ b/delivery_carrier_label_roulier/stock.py @@ -126,38 +126,88 @@ def _should_include_customs(self, package_id): # end of API # Core functions + @api.multi - def _roulier_generate_labels(self, package_ids=None): - # call generate_shipping_labels for each package - # collect answers from generate_shipping_labels - # persist it - self.ensure_one() + def set_label_as_attachment(self, labels_tuple): + shipping_label = self.env['shipping.label'] + package_obj = self.env['stock.quant.package'] - labels = self.generate_shipping_labels(package_ids) - for label in labels: + for package_id, label_dict in labels_tuple: data = { - 'name': label['name'], 'res_id': self.id, 'res_model': 'stock.picking', + 'package_id': package_id, + 'type': 'binary', } - if label.get('package_id'): - data['package_id'] = label['package_id'] - if label.get('url'): - data['url'] = label['url'] - data['type'] = 'url' - elif label.get('data'): - data['datas'] = label['data'].encode('base64') - data['type'] = 'binary' + label_name = "%s_%s.pdf" % ('label', label_dict['parcelNumber']) + cn23_name = "%s_%s.pdf" % ('cn23', label_dict['parcelNumber']) - self.env['shipping.label'].create(data) + label_data = data.copy() + label_data['name'] = label_name + label_data['datas'] = label_dict['label'].encode('base64') + shipping_label.create(label_data) + + if label_dict.get('cn23'): + cn23_data = data.copy() + cn23_data['name'] = cn23_name + cn23_data['datas'] = label_dict['cn23'].encode('base64') + shipping_label.create(cn23_data) + + data = { + 'res_id': package_id, + 'res_model': 'stock.quant.package', + 'package_id': package_id, + 'type': 'binary', + } + label_data = data.copy() + label_data['name'] = label_name + label_data['datas'] = label_dict['label'].encode('base64') + shipping_label.create(label_data) + + if label_dict.get('cn23'): + cn23_data = data.copy() + cn23_data['name'] = cn23_name + cn23_data['datas'] = label_dict['cn23'].encode('base64') + shipping_label.create(cn23_data) + + package_obj.browse(package_id).write( + {'parcel_tracking': label_dict['parcelNumber']} + ) + return True + + @api.multi + def _roulier_generate_labels(self, package_ids=None): + # call generate_shipping_labels for each package + # collect answers from generate_shipping_labels + # persist it + self.ensure_one() + + labels = self.generate_shipping_labels(package_ids) + self.set_label_as_attachment(labels) + # for label in labels: + # data = { + # 'name': label['name'], + # 'res_id': self.id, + # 'res_model': 'stock.picking', + # } + # if label.get('package_id'): + # data['package_id'] = label['package_id'] + + # if label.get('url'): + # data['url'] = label['url'] + # data['type'] = 'url' + # elif label.get('data'): + # data['datas'] = label['data'].encode('base64') + # data['type'] = 'binary' + + # self.env['shipping.label'].create(data) return True @api.multi def _roulier_generate_shipping_labels(self, package_ids=None): """Create as many labels as package_ids or in self.""" self.ensure_one() - packages = [] if package_ids: packages = package_ids @@ -167,7 +217,7 @@ def _roulier_generate_shipping_labels(self, package_ids=None): raise UserError(_('No package found for this picking')) # It's not our responsibility to create the packages labels = [ - self._call_roulier_api(package) + (package.id, self._call_roulier_api(package)) for package in packages ] return labels @@ -208,20 +258,30 @@ def _call_roulier_api(self, package_id): 'weight': weight, } - # sorte d'interceptor ici pour que chacun - # puisse ajouter ses merdes à payload payload = self._before_call(package_id, payload) - # vrai appel a l'api ret = roulier_instance.get_label(payload) # minimum error handling if ret.get('status', '') == 'error': + # try better management of this error + # ret.get('message') is a dictionnary like + # {'message': u"Le poids du colis n'a pas \xe9t\xe9 transmis", + # 'type': 'ERROR', 'id': 30300} raise UserError(_(ret.get('message', 'WebService error'))) # give result to someonelese return self._after_call(package_id, ret) + def get_extracted_fields(self): + return [ + 'name', 'zip', 'city', 'phone', 'mobile', + 'email', 'phone', 'parent_id', 'first_name' + ] + + def get_mapping_dict(self, extract_fields): + return {k: k for k in extract_fields} + # helpers @api.multi def _roulier_convert_address(self, partner): @@ -234,16 +294,15 @@ def _roulier_convert_address(self, partner): """ self.ensure_one() address = {} - extract_fields = [ - 'name', 'zip', 'city', 'phone', 'mobile', - 'email', 'phone', 'parent_id', 'first_name'] + extract_fields = self.get_extracted_fields() + mapping_dict = self.get_mapping_dict(extract_fields) for elm in extract_fields: if elm in partner: # because a value can't be None in odoo's ORM # you don't want to mix (bool) False and None if partner._fields[elm].type != fields.Boolean.type: if partner[elm]: - address[elm] = partner[elm] + address[mapping_dict[elm]] = partner[elm] # else: # it's a None: nothing to do else: # it's a boolean: keep the value diff --git a/delivery_carrier_label_roulier_laposte/company.py b/delivery_carrier_label_roulier_laposte/company.py index 18bcfdf6df..da2b0dcab1 100644 --- a/delivery_carrier_label_roulier_laposte/company.py +++ b/delivery_carrier_label_roulier_laposte/company.py @@ -43,4 +43,3 @@ class ResCompany(models.Model): help=u"Pour ColiPoste International. \nSi coché, un commentaire " u"sera créé dans le bon de livraison\nsi la réponse du " u"web service contient un message additionnel.") - diff --git a/delivery_carrier_label_roulier_laposte/config.py b/delivery_carrier_label_roulier_laposte/config.py index 2e8cddb406..764981d0cd 100644 --- a/delivery_carrier_label_roulier_laposte/config.py +++ b/delivery_carrier_label_roulier_laposte/config.py @@ -23,27 +23,28 @@ class LaposteConfigSettings(models.TransientModel): def _default_company(self): return self.env.user.company_id - company_id = fields.Many2one('res.company', 'Company', required=True, default=_default_company) + company_id = fields.Many2one('res.company', 'Company', required=True, + default=_default_company) laposte_login = fields.Char(related='company_id.laposte_login') laposte_password = fields.Char(related='company_id.laposte_password') - laposte_support_city = fields.Char(related='company_id.laposte_support_city') - laposte_support_city_code = fields.Char(related='company_id.laposte_support_city_code') - - + laposte_support_city = fields.Char( + related='company_id.laposte_support_city') + laposte_support_city_code = fields.Char( + related='company_id.laposte_support_city_code') @api.onchange('company_id') def onchange_company_id(self): if not self.company_id: - #what's the point of this ? + # what's the point of this ? return company = self.company_id - laposte_login = company.laposte_login - laposte_password = company.laposte_password - laposte_support_city = company.laposte_support_city - laposte_support_city_code = company.laposte_support_city_code + self.laposte_login = company.laposte_login + self.laposte_password = company.laposte_password + self.laposte_support_city = company.laposte_support_city + self.laposte_support_city_code = company.laposte_support_city_code def button_send_image_to_printer(self, cr, uid, ids, context=None): """ Implement your own method according to printing solution """ - return LaposteConfig().get_image_data() + return LaposteConfigSettings().get_image_data() diff --git a/delivery_carrier_label_roulier_laposte/stock.py b/delivery_carrier_label_roulier_laposte/stock.py index a8cf6bd026..59c3f3ba95 100644 --- a/delivery_carrier_label_roulier_laposte/stock.py +++ b/delivery_carrier_label_roulier_laposte/stock.py @@ -9,7 +9,6 @@ # Sébastien BEAU ############################################################################## -from openerp.tools.config import config from openerp import models, fields, api from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT import openerp.addons.decimal_precision as dp @@ -82,11 +81,7 @@ def cacl_package_price(package_id): def _laposte_after_call(self, package_id, response): # CN23 is included in the pdf url - return { - 'name': response['parcelNumber'], - 'url': response['url'], - 'type': 'url', - } + return response def _laposte_get_shipping_date(self, package_id): """Estimate shipping date.""" @@ -161,7 +156,9 @@ def _laposte_get_customs(self, package_id): hs = product.product_tmpl_id.get_hs_code_recursively() article['quantity'] = '%.f' % operation.product_qty - article['weight'] = (operation.get_weight() / operation.product_qty) + article['weight'] = ( + operation.get_weight() / operation.product_qty + ) article['originCountry'] = product.origin_country_id.code article['description'] = hs.description article['hs'] = hs.hs_code