Port account.invoice to new API

This commit is contained in:
Guewen Baconnier
2014-10-28 11:58:11 +01:00
parent 892b1288b8
commit 87fafe38ea

View File

@@ -18,69 +18,51 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp.osv import orm, fields
from openerp import models, fields, api
from openerp.tools.translate import _
class AccountInvoice(orm.Model):
class AccountInvoice(models.Model):
"""Check on cancelling of an invoice"""
_inherit = 'account.invoice'
_columns = {
'credit_policy_id': fields.many2one(
'credit.control.policy',
'Credit Control Policy',
help=("The Credit Control Policy used for this "
"invoice. If nothing is defined, it will "
"use the account setting or the partner "
"setting."),
readonly=True,
),
'credit_control_line_ids': fields.one2many(
'credit.control.line',
'invoice_id',
string='Credit Lines',
readonly=True
),
}
credit_policy_id = fields.Many2one(
'credit.control.policy',
string='Credit Control Policy',
help="The Credit Control Policy used for this "
"invoice. If nothing is defined, it will "
"use the account setting or the partner "
"setting.",
readonly=True,
copy=False,
)
def copy_data(self, cr, uid, id, default=None, context=None):
"""Ensure that credit lines and policy are not copied"""
if default is None:
default = {}
else:
default = default.copy()
default['credit_control_line_ids'] = False
default['credit_policy_id'] = False
return super(AccountInvoice, self).copy_data(
cr, uid, id, default=default, context=context)
credit_control_line_ids = fields.One2many(
'credit.control.line', 'invoice_id',
string='Credit Lines',
readonly=True,
copy=False,
)
def action_cancel(self, cr, uid, ids, context=None):
@api.multi
def action_cancel(self):
"""Prevent to cancel invoice related to credit line"""
# We will search if this invoice is linked with credit
cc_line_obj = self.pool.get('credit.control.line')
for invoice_id in ids:
cc_nondraft_line_ids = cc_line_obj.search(
cr, uid,
[('invoice_id', '=', invoice_id),
('state', '!=', 'draft')],
context=context)
if cc_nondraft_line_ids:
raise orm.except_orm(
_('Error!'),
cc_line_obj = self.env['credit.control.line']
for invoice in self:
nondraft_domain = [('invoice_id', '=', invoice.id),
('state', '!=', 'draft')]
cc_nondraft_lines = cc_line_obj.search(nondraft_domain)
if cc_nondraft_lines:
raise api.Warning(
_('You cannot cancel this invoice.\n'
'A payment reminder has already been '
'sent to the customer.\n'
'You must create a credit note and '
'issue a new invoice.')
)
cc_draft_line_ids = cc_line_obj.search(
cr, uid,
[('invoice_id', '=', invoice_id),
('state', '=', 'draft')],
context=context)
cc_line_obj.unlink(cr, uid,
cc_draft_line_ids,
context=context)
return super(AccountInvoice, self).action_cancel(cr, uid, ids,
context=context)
draft_domain = [('invoice_id', '=', invoice.id),
('state', '=', 'draft')]
cc_draft_line = cc_line_obj.search(draft_domain)
cc_draft_line.unlink()
return super(AccountInvoice, self).action_cancel()