← Back to team overview

openerp-community-reviewer team mailing list archive

lp:~camptocamp/carriers-deliveries/7.0-delivery_carrier_label_postlogistics-imp-licenses-yvr into lp:carriers-deliveries

 

Yannick Vaucher @ Camptocamp has proposed merging lp:~camptocamp/carriers-deliveries/7.0-delivery_carrier_label_postlogistics-imp-licenses-yvr into lp:carriers-deliveries.

Requested reviews:
  Stock and Logistic Core Editors (stock-logistic-core-editors)

For more details, see:
https://code.launchpad.net/~camptocamp/carriers-deliveries/7.0-delivery_carrier_label_postlogistics-imp-licenses-yvr/+merge/202334

Implements license management for postlogistics label web service
-- 
https://code.launchpad.net/~camptocamp/carriers-deliveries/7.0-delivery_carrier_label_postlogistics-imp-licenses-yvr/+merge/202334
Your team Stock and Logistic Core Editors is requested to review the proposed merge of lp:~camptocamp/carriers-deliveries/7.0-delivery_carrier_label_postlogistics-imp-licenses-yvr into lp:carriers-deliveries.
=== modified file 'delivery_carrier_label_postlogistics/__openerp__.py'
--- delivery_carrier_label_postlogistics/__openerp__.py	2013-12-23 10:54:36 +0000
+++ delivery_carrier_label_postlogistics/__openerp__.py	2014-01-20 16:16:32 +0000
@@ -73,6 +73,13 @@
 .. _PostLogistics BarCodes WebService: http://www.poste.ch/post-startseite/post-geschaeftskunden/post-logistik/post-e-log/post-e-log-webservices.htm
 .. _Swiss Post E-logistics: http://www.poste.ch/en/post-startseite/post-geschaeftskunden/post-logistik/post-e-log.htm
 
+
+Recommended modules
+-------------------
+
+* PostLogistics labels - logo per Shop
+  If you have multiple shops configure one logo per shop
+
 Technical references
 --------------------
 
@@ -80,18 +87,11 @@
 
 .. _"Barcode" web service documentation: http://www.poste.ch/post-barcode-cug.htm
 
+
 Contributors
 ------------
 
 * Yannick Vaucher <yannick.vaucher@xxxxxxxxxxxxxx>
-
-----
-
-*TODO*:
-
-* *Add onchange to improve carrier method creation*
-* *Identify attachement as label*
-* *Better License management*
 """,
  'website': 'http://www.camptocamp.com/',
  'data': ['res_partner_data.xml',

=== modified file 'delivery_carrier_label_postlogistics/company.py'
--- delivery_carrier_label_postlogistics/company.py	2013-12-03 17:18:48 +0000
+++ delivery_carrier_label_postlogistics/company.py	2014-01-20 16:16:32 +0000
@@ -38,10 +38,10 @@
             type='char'),
         'postlogistics_username': fields.char('Username'),
         'postlogistics_password': fields.char('Password'),
-        # XXX improve license management
-        'postlogistics_license_less_1kg': fields.char('License less than 1kg'),
-        'postlogistics_license_more_1kg': fields.char('License more than 1kg'),
-        'postlogistics_license_vinolog': fields.char('License VinoLog'),
+        'postlogistics_license_ids': fields.one2many(
+            'postlogistics.license',
+            'company_id',
+            'PostLogistics Frankling License'),
         'postlogistics_logo': fields.binary('Company logo for PostLogistics'),
         'postlogistics_office': fields.char('Post office'),
 

=== modified file 'delivery_carrier_label_postlogistics/delivery.py'
--- delivery_carrier_label_postlogistics/delivery.py	2014-01-13 11:23:21 +0000
+++ delivery_carrier_label_postlogistics/delivery.py	2014-01-20 16:16:32 +0000
@@ -20,6 +20,22 @@
 ##############################################################################
 from openerp.osv import orm, fields
 
+class PostlogisticsLicense(orm.Model):
+    _name = 'postlogistics.license'
+    _description = 'PostLogistics Franking License'
+
+    _order = 'sequence'
+
+    _columns = {
+        'name': fields.char('Description', translate=True, required=True),
+        'number': fields.char('Number', required=True),
+        'company_id': fields.many2one('res.company', 'Company', required=True),
+        'sequence': fields.integer(
+            'Sequence',
+            help="Gives the sequence on company to define priority on license"
+                 " when multiple license are available for the same group of "
+                 "service."),
+    }
 
 class PostlogisticsServiceGroup(orm.Model):
     _name = 'postlogistics.service.group'
@@ -28,6 +44,12 @@
     _columns = {
         'name': fields.char('Description', translate=True, required=True),
         'group_extid': fields.integer('Group ID', required=True),
+        'postlogistics_license_ids': fields.many2many(
+            'postlogistics.license',
+            'postlogistics_license_service_groups_rel',
+            'license_id',
+            'group_id',
+            'PostLogistics Frankling License'),
     }
 
     _sql_constraints = [
@@ -194,6 +216,9 @@
         'type': fields.selection(
             _get_carrier_type_selection, 'Type',
             help="Carrier type (combines several delivery methods)"),
+        'postlogistics_license_id': fields.many2one(
+            'postlogistics.license',
+            string='PostLogistics Frankling License'),
         'postlogistics_service_group_id': fields.many2one(
             'postlogistics.service.group',
             string='PostLogistics Service Group',

=== modified file 'delivery_carrier_label_postlogistics/delivery_view.xml'
--- delivery_carrier_label_postlogistics/delivery_view.xml	2013-11-29 08:19:33 +0000
+++ delivery_carrier_label_postlogistics/delivery_view.xml	2014-01-20 16:16:32 +0000
@@ -49,6 +49,7 @@
       <field name="arch" type="xml">
         <field name="type" position="after">
           <field name="postlogistics_service_group_id" attrs="{'invisible': [('type', '!=', 'postlogistics')], 'required': [('type', '=', 'postlogistics')]}"/>
+          <field name="postlogistics_license_id" attrs="{'invisible': [('type', '!=', 'postlogistics')]}"/>
           <field name="allowed_option_ids" invisible="1"/>
         </field>
         <field name="available_option_ids" position="attributes">

=== modified file 'delivery_carrier_label_postlogistics/postlogistics/web_service.py'
--- delivery_carrier_label_postlogistics/postlogistics/web_service.py	2013-12-17 07:45:50 +0000
+++ delivery_carrier_label_postlogistics/postlogistics/web_service.py	2014-01-20 16:16:32 +0000
@@ -97,7 +97,7 @@
             lang = company.partner_id.lang
         lang = self._get_language(lang)
         request = self.client.service.ReadAllowedServicesByFrankingLicense
-        return self._send_request(request, License=license, Language=lang)
+        return self._send_request(request, FrankingLicense=license, Language=lang)
 
     def read_service_groups(self, company, lang):
         """ Get group of services """
@@ -223,13 +223,26 @@
         return resolution
 
     def _get_license(self, picking):
-        """ Get the right license depending on weight """
-        company = picking.company_id
-        #XXX get weight or set it as an option on picking
-        weight = 0
-        if weight > 1.0:
-            return company.postlogistics_license_more_1kg
-        return company.postlogistics_license_less_1kg
+        """ Get the license
+
+        Take it from carrier and if not defined get the first license
+        depending on service group. This needs to have associated 
+        licenses to groups.
+
+        :return: license number
+        """
+        license = picking.carrier_id.postlogistics_license_id
+        if not license:
+            company_licenses = picking.company_id.postlogistics_license_ids
+            group = picking.carrier_id.postlogistics_service_group_id
+            if not company_licenses or not group:
+                return None
+            group_license_ids = [l.id for l in group.postlogistics_license_ids]
+            if not group_license_ids:
+                return None
+            license = [l for l in company_licenses
+                       if l.id in group_license_ids][0]
+        return license.number
 
     def _prepare_attributes(self, picking):
         services = [option.code.split(',') for option in picking.option_ids

=== modified file 'delivery_carrier_label_postlogistics/res_config.py'
--- delivery_carrier_label_postlogistics/res_config.py	2013-12-13 13:44:03 +0000
+++ delivery_carrier_label_postlogistics/res_config.py	2014-01-20 16:16:32 +0000
@@ -43,6 +43,11 @@
         'password': fields.related(
             'company_id', 'postlogistics_password',
             string='Password', type='char'),
+        'license_ids': fields.related(
+            'company_id', 'postlogistics_license_ids',
+            string='Frankling Licenses',
+            type='one2many',
+            relation='postlogistics.license'),
         'license_less_1kg': fields.related(
             'company_id', 'postlogistics_license_less_1kg',
             string='License less than 1kg', type='char'),
@@ -117,15 +122,14 @@
         company = self.pool.get('res.company'
                                 ).browse(cr, uid, company_id, context=context)
 
+        license_ids = [l.id for l in company.postlogistics_license_ids]
         label_layout = company.postlogistics_default_label_layout.id or False
         output_format = company.postlogistics_default_output_format.id or False
         resolution = company.postlogistics_default_resolution.id or False
         values = {
             'username': company.postlogistics_username,
             'password': company.postlogistics_password,
-            'license_less_1kg': company.postlogistics_license_less_1kg,
-            'license_more_1kg': company.postlogistics_license_more_1kg,
-            'license_vinolog': company.postlogistics_license_vinolog,
+            'license_ids': license_ids,
             'logo': company.postlogistics_logo,
             'office': company.postlogistics_office,
             'default_label_layout': label_layout,
@@ -422,3 +426,65 @@
                     continue
                 self._update_service_groups(cr, uid, ids, web_service, company, context=ctx)
         return True
+
+    def _get_allowed_service_group_codes(self, web_service, company,
+                                         license, context=None):
+        """ Get a list of allowed service group codes"""
+        if context is None:
+            context = {}
+
+        lang = context.get('lang', 'en')
+        res = web_service.read_allowed_services_by_franking_license(
+            license.number, company, lang)
+        if 'errors' in res:
+            errors = '\n'.join(res['errors'])
+            error_message = (_('Could not retrieve allowed Postlogistics '
+                               'service groups for the %s licence:\n%s')
+                               % (license.name, errors))
+            raise orm.except_orm(_('Error'), error_message)
+
+        if not res['value']:
+            return []
+
+        if hasattr(res['value'], 'Errors') and res['value'].Errors:
+            for error in res['value'].Errors.Error:
+                message = '[%s] %s' % (error.Code, error.Message)
+            raise orm.except_orm('Error', message)
+
+        service_group_codes = []
+        for group in res['value'].ServiceGroups:
+            service_group_codes.append(group.ServiceGroup.ServiceGroupID)
+
+        return service_group_codes
+
+    def assign_licenses_to_service_groups(self, cr, uid, ids, context=None):
+        """ Check all licenses to assign it to PostLogistics service groups """
+
+        if context is None:
+            context = {}
+
+        user_obj = self.pool.get('res.users')
+        service_group_obj = self.pool.get('postlogistics.service.group')
+        for config in self.browse(cr, uid, ids, context=context):
+            company = config.company_id
+            web_service = PostlogisticsWebService(company)
+
+            relations = {}
+            for license in company.postlogistics_license_ids:
+                service_groups = self._get_allowed_service_group_codes(
+                    web_service, company, license, context=context)
+                group_ids = service_group_obj.search(
+                        cr, uid, [('group_extid', 'in', service_groups)],
+                        context=context)
+                for group_id in group_ids:
+                    if group_id in relations:
+                        relations[group_id].append(license.id)
+                    else:
+                        relations[group_id] = [license.id]
+            for group_id, license_ids in relations.iteritems():
+                vals = {'postlogistics_license_ids': [(6, 0, license_ids)]}
+                service_group_obj.write(cr, uid, group_id, vals,
+                                        context=context)
+
+
+        return True

=== modified file 'delivery_carrier_label_postlogistics/res_config_view.xml'
--- delivery_carrier_label_postlogistics/res_config_view.xml	2013-12-03 17:18:48 +0000
+++ delivery_carrier_label_postlogistics/res_config_view.xml	2014-01-20 16:16:32 +0000
@@ -41,16 +41,13 @@
           <group>
             <div>
               <div>
-                <label for="license_less_1kg"/>
-                <field name="license_less_1kg" class="oe_inline"/>
-              </div>
-              <div>
-                <label for="license_more_1kg"/>
-                <field name="license_more_1kg" class="oe_inline"/>
-              </div>
-              <div>
-                <label for="license_vinolog"/>
-                <field name="license_vinolog" class="oe_inline"/>
+                <field name="license_ids" class="oe_inline">
+                  <tree editable="bottom">
+                    <field name="name"/>
+                    <field name="number"/>
+                    <field name="sequence" invisible="True"/>
+                  </tree>
+                </field>
               </div>
             </div>
           </group>
@@ -88,6 +85,7 @@
             <div>
               <div>
                 <button string="Update PostLogistics Services" type="object" name="update_postlogistics_options" class="oe_highlight"/>
+                <button string="Assign PostLogistics Licenses to service groups" type="object" name="assign_licenses_to_service_groups" class="oe_highlight"/>
               </div>
             </div>
           </group>


Follow ups