# -*- coding: utf-8 -*-
# © 2014-2016 Acsone (http://acsone.eu)
# © 2016 Akretion (http://www.akretion.com/)
# @author Stéphane Bidoul <stephane.bidoul@acsone.eu>
# @author Alexis de Lattre <alexis.delattre@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

from odoo import models, fields, api, _
from odoo.tools import float_is_zero
from odoo.exceptions import ValidationError


class AccountAccountType(models.Model):
    _inherit = "account.account.type"

    partner_policy = fields.Selection([
        ('optional', 'Optional'),
        ('always', 'Always'),
        ('never', 'Never'),
        ], string='Policy for Partner Field',
        required=True, default='optional',
        help="Set the policy for the partner field : if you select "
             "'Optional', the accountant is free to put a partner "
             "on an account move line with this type of account ; "
             "if you select 'Always', the accountant will get an error "
             "message if there is no partner ; if you select 'Never', "
             "the accountant will get an error message if a partner "
             "is present.")


class AccountAccount(models.Model):
    _inherit = 'account.account'

    @api.multi
    def get_partner_policy(self):
        """ Extension point to obtain partner policy for an account """
        self.ensure_one()
        return self.user_type_id.partner_policy


class AccountMoveLine(models.Model):
    _inherit = "account.move.line"

    @api.multi
    def _check_partner_required_msg(self):
        prec = self.env.user.company_id.currency_id.rounding
        for line in self:
            if (
                    float_is_zero(line.debit, precision_rounding=prec) and
                    float_is_zero(line.credit, precision_rounding=prec)):
                continue
            policy = line.account_id.get_partner_policy()
            if policy == 'always' and not line.partner_id:
                return _("Partner policy is set to 'Always' "
                         "with account '%s' but the "
                         "partner is missing in the account "
                         "move line with label '%s'." %
                         (line.account_id.name_get()[0][1],
                          line.name))
            elif policy == 'never' and line.partner_id:
                return _("Partner policy is set to 'Never' "
                         "with account '%s' but the "
                         "account move line with label '%s' "
                         "has a partner '%s'." %
                         (line.account_id.name_get()[0][1],
                          line.name,
                          line.partner_id.name))

    @api.multi
    @api.constrains('partner_id', 'account_id', 'debit', 'credit')
    def _check_partner_required(self):
        for line in self:
            message = line._check_partner_required_msg()
            if message:
                raise ValidationError(message)

@@ -1961,32 +1961,53 @@
        return _(
+%0A                    
 %22Partner policy 
@@ -2029,36 +2029,8 @@
 ys' 
-%22%0A                         %22
 with
@@ -2043,33 +2043,26 @@
 nt '%25s' but 
-the %22%0A   
+%22%0A
             
@@ -2069,19 +2069,21 @@
         
-  %22
+%22the 
 partner 
@@ -2112,36 +2112,8 @@
 unt 
-%22%0A                         %22
 move
@@ -2127,22 +2127,9 @@
 ith 
-label '%25s'.%22 %25
+%22
 %0A   
@@ -2137,36 +2137,48 @@
                 
-    
+%22label '%25s'.%22) %25
  (line.account_i
@@ -2215,32 +2215,44 @@
                 
+        
+    
 line.name))%0A    
@@ -2245,17 +2245,16 @@
 ne.name)
-)
 %0A       
@@ -2327,16 +2327,37 @@
 eturn _(
+%0A                    
 %22Partner
@@ -2386,36 +2386,8 @@
 er' 
-%22%0A                         %22
 with
@@ -2404,20 +2404,16 @@
 %25s' but 
-the 
 %22%0A      
@@ -2426,22 +2426,21 @@
         
-     %22
+%22the 
 account 
@@ -2469,36 +2469,8 @@
 %25s' 
-%22%0A                         %22
 has 
@@ -2483,16 +2483,9 @@
 ner 
-'%25s'.%22 %25
+%22
 %0A   
@@ -2501,20 +2501,26 @@
         
-    
+%22'%25s'.%22) %25
  (line.a
@@ -2539,32 +2539,38 @@
 me_get()%5B0%5D%5B1%5D,%0A
+      
                 
@@ -2612,24 +2612,30 @@
             
+      
 line.partner
@@ -2643,17 +2643,16 @@
 id.name)
-)
 %0A%0A    @a

FIX translation method (bug reported by yvaucher)