Skip to content

Commit

Permalink
flake it
Browse files Browse the repository at this point in the history
  • Loading branch information
robinkeunen committed Jan 4, 2019
1 parent 1e00ad2 commit 7f2c70a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 34 deletions.
2 changes: 1 addition & 1 deletion invoice_correction/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import account
import account
87 changes: 56 additions & 31 deletions invoice_correction/models/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from openerp.exceptions import UserError, ValidationError
from openerp.tools import float_compare


class AccountInvoice(models.Model):
_inherit = 'account.invoice'

Expand All @@ -13,20 +14,23 @@ class AccountInvoice(models.Model):
origin_account_id = fields.Many2one('account.account', readonly=True)

def init(self, cr):
cr.execute("UPDATE account_invoice SET origin_account_id = account_id WHERE state in ('open', 'paid') AND origin_account_id IS NULL;")
cr.execute(
"UPDATE account_invoice "
"SET origin_account_id = account_id "
"WHERE state in ('open', 'paid') AND origin_account_id IS NULL;")


@api.multi
def write(self, vals):
price_per_id = {rec.id : rec.amount_total for rec in self}
price_per_id = {rec.id: rec.amount_total for rec in self}

res = super(AccountInvoice, self).write(vals)
for rec in self:
if rec.state in ['open', 'paid'] and float_compare(
rec.amount_total,
price_per_id[rec.id],
precision_rounding=rec.currency_id.rounding) != 0:
raise ValidationError(_('You cannot change the amount of a validated invoice'))
rec.amount_total,
price_per_id[rec.id],
precision_rounding=rec.currency_id.rounding) != 0:
raise ValidationError(_('You cannot change the amount of a '
'validated invoice'))
return res

@api.multi
Expand All @@ -40,14 +44,16 @@ def invoice_validate(self):
def start_correction(self):
self.ensure_one()
if not self.env.user.has_group('account.group_account_manager'):
raise UserError(_("Only Account/ Adviser user can start the correction of an invoice"))
raise UserError(_("Only Account/ Adviser user can start the "
"correction of an invoice"))
self.correction = True

@api.multi
def validate_correction(self):
self.ensure_one()
if not self.env.user.has_group('account.group_account_manager'):
raise UserError(_("Only Account/ Adviser user can validate the correction of an invoice"))
raise UserError(_("Only Account/ Adviser user can validate the "
"correction of an invoice"))

main_move = self.env['account.move.line']
to_delete_move = self.env['account.move.line']
Expand All @@ -57,61 +63,80 @@ def validate_correction(self):
else:
to_delete_move |= move_line

#1 Move in draft and reconcile entry inactive
reconcile = (self.move_id.line_ids.mapped('matched_debit_ids') | self.move_id.line_ids.mapped('matched_credit_ids'))
# 1 Move in draft and reconcile entry inactive
reconcile = (
self.move_id.line_ids.mapped('matched_debit_ids')
| self.move_id.line_ids.mapped('matched_credit_ids'))
reconcile.write({'active': False})
self.invalidate_cache()
self.move_id.write({'state': 'draft'})
#2 Main move change account_id
# 2 Main move change account_id
main_move.write({'account_id': self.account_id.id})
#3 prepare line to delete
# 3 prepare line to delete
m2m_command = [(2, rec.id, 0) for rec in to_delete_move]
#4 prepate line to create
# 4 prepate line to create
iml = self.invoice_line_move_line_get() + self.tax_line_move_line_get()
_, _, iml = self.compute_invoice_totals(self.company_id.currency_id, iml)
part = self.env['res.partner']._find_accounting_partner(self.partner_id)
line = [(0, 0, self.line_get_convert(l, part.id)) for l in iml]
line = self.group_lines(iml, line)
line = self.finalize_invoice_move_lines(line)
m2m_command += line
self.move_id.with_context(check_move_validity=False, dont_create_taxes=True).write({'line_ids': m2m_command})
#5 Back to posted
self.move_id.with_context(
check_move_validity=False,
dont_create_taxes=True).write({'line_ids': m2m_command})
# 5 Back to posted
reconcile.write({'active': True})
self.invalidate_cache()
self.move_id.write({'state': 'posted'})
#6 Clean correction
# 6 Clean correction
self.write({
'origin_account_id' : self.account_id.id,
'origin_account_id': self.account_id.id,
'correction': False,
})


class AccountInvoiceLine(models.Model):
_inherit = 'account.invoice.line'

correction = fields.Boolean(related='invoice_id.correction', readonly=True)
state = fields.Selection(related='invoice_id.state', default='draft')
#Make all these fields readonly when the invoice is not in draft
price_unit = fields.Float(readonly=True, states={'draft': [('readonly', False)]})
product_id = fields.Many2one(readonly=True, states={'draft': [('readonly', False)]})
name = fields.Char(readonly=True, states={'draft': [('readonly', False)]})
quantity = fields.Float(readonly=True, states={'draft': [('readonly', False)]})
correction = fields.Boolean(
related='invoice_id.correction',
readonly=True)
state = fields.Selection(
related='invoice_id.state',
default='draft')
# Make all these fields readonly when the invoice is not in draft
price_unit = fields.Float(
readonly=True,
states={'draft': [('readonly', False)]})
product_id = fields.Many2one(
readonly=True,
states={'draft': [('readonly', False)]})
name = fields.Char(
readonly=True,
states={'draft': [('readonly', False)]})
quantity = fields.Float(
readonly=True,
states={'draft': [('readonly', False)]})

@api.multi
def write(self, vals):
price_per_id = {rec.id : rec.price_subtotal for rec in self}
price_per_id = {rec.id: rec.price_subtotal for rec in self}
res = super(AccountInvoiceLine, self).write(vals)
for rec in self:
if rec.state in ['open', 'paid'] and float_compare(
rec.price_subtotal,
price_per_id[rec.id],
precision_rounding=rec.currency_id.rounding) != 0:
raise ValidationError(_('You cannot change the amount of a validated invoice'))
rec.price_subtotal,
price_per_id[rec.id],
precision_rounding=rec.currency_id.rounding) != 0:
raise ValidationError(
_('You cannot change the amount of a validated invoice'))
return res

@api.multi
def unlink(self):
if any([s in ['open', 'paid'] for s in self.mapped('state')]):
raise ValidationError(_('You cannot delete a invoice line from a confirmed invoice'))
raise ValidationError(
_('You cannot delete a invoice line from a confirmed invoice'))
return super(AccountInvoiceLine, self)


Expand Down
3 changes: 1 addition & 2 deletions invoice_correction/views/invoice.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,5 @@
</xpath>
</field>
</record>


</odoo>
</odoo>

0 comments on commit 7f2c70a

Please sign in to comment.