mirror of
https://github.com/OCA/pms.git
synced 2025-01-29 00:17:45 +02:00
[IMP] Clean inherit account invoice
This commit is contained in:
@@ -3,27 +3,44 @@
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
|
||||
from odoo import models, fields, api, _
|
||||
import json
|
||||
from odoo.tools import float_is_zero, float_compare, pycompat
|
||||
from odoo.exceptions import ValidationError, UserError
|
||||
from odoo.tools import float_is_zero
|
||||
|
||||
|
||||
class AccountInvoice(models.Model):
|
||||
_inherit = 'account.invoice'
|
||||
|
||||
@api.model
|
||||
def create(self, vals):
|
||||
if self.env.context.get('invoice_origin', False):
|
||||
vals.update({'origin': self.env.context.get['invoice_origin']})
|
||||
return super(AccountInvoice, self).create(vals)
|
||||
# Field Declarations
|
||||
from_folio = fields.Boolean(
|
||||
compute='_computed_folio_origin')
|
||||
folio_ids = fields.Many2many(
|
||||
comodel_name='hotel.folio',
|
||||
compute='_computed_folio_origin')
|
||||
hotel_id = fields.Many2one('hotel.property')
|
||||
outstanding_folios_debits_widget = fields.Text(
|
||||
compute='_get_outstanding_folios_JSON')
|
||||
has_folios_outstanding = fields.Boolean(
|
||||
compute='_get_outstanding_folios_JSON')
|
||||
|
||||
"""WIP"""
|
||||
# Compute and Search methods
|
||||
@api.multi
|
||||
def _computed_folio_origin(self):
|
||||
for inv in self:
|
||||
folios = inv.mapped('invoice_line_ids.reservation_ids.folio_id')
|
||||
folios |= inv.mapped('invoice_line_ids.service_ids.folio_id')
|
||||
if folios:
|
||||
inv.from_folio = True
|
||||
inv.folio_ids = [(6, 0, folios.ids)]
|
||||
|
||||
# Action methods
|
||||
@api.multi
|
||||
def action_folio_payments(self):
|
||||
self.ensure_one()
|
||||
sales = self.mapped('invoice_line_ids.sale_line_ids.order_id')
|
||||
folios = self.env['hotel.folio'].search([('order_id.id','in',sales.ids)])
|
||||
folios = self.env['hotel.folio'].search([
|
||||
('order_id.id', 'in', sales.ids)
|
||||
])
|
||||
payments_obj = self.env['account.payment']
|
||||
payments = payments_obj.search([('folio_id','in',folios.ids)])
|
||||
payments = payments_obj.search([('folio_id', 'in', folios.ids)])
|
||||
payment_ids = payments.mapped('id')
|
||||
return{
|
||||
'name': _('Payments'),
|
||||
@@ -35,68 +52,54 @@ class AccountInvoice(models.Model):
|
||||
'domain': [('id', 'in', payment_ids)],
|
||||
}
|
||||
|
||||
from_folio = fields.Boolean(compute='_computed_folio_origin')
|
||||
folio_ids = fields.Many2many(
|
||||
comodel_name='hotel.folio', compute='_computed_folio_origin')
|
||||
hotel_id = fields.Many2one('hotel.property')
|
||||
outstanding_folios_debits_widget = fields.Text(compute='_get_outstanding_folios_JSON')
|
||||
has_folios_outstanding = fields.Boolean(compute='_get_outstanding_folios_JSON')
|
||||
|
||||
@api.multi
|
||||
def action_invoice_open(self):
|
||||
to_open_invoices_without_vat = self.filtered(lambda inv: inv.state != 'open' and inv.partner_id.vat == False)
|
||||
to_open_invoices_without_country = self.filtered(lambda inv: inv.state != 'open' and not inv.partner_id.country_id)
|
||||
if (to_open_invoices_without_vat or to_open_invoices_without_country) and \
|
||||
self.env.context.get('validate_vat_number', True):
|
||||
vat_error = _("We need the VAT and Country of the following companies")
|
||||
for invoice in to_open_invoices_without_vat:
|
||||
vat_error += ", " + invoice.partner_id.name
|
||||
for invoice in to_open_invoices_without_country:
|
||||
vat_error += ", " + invoice.partner_id.name
|
||||
raise ValidationError(vat_error)
|
||||
return super(AccountInvoice, self).action_invoice_open()
|
||||
|
||||
@api.multi
|
||||
def _computed_folio_origin(self):
|
||||
for inv in self:
|
||||
folios = inv.mapped('invoice_line_ids.reservation_ids.folio_id')
|
||||
folios |= inv.mapped('invoice_line_ids.service_ids.folio_id')
|
||||
if folios:
|
||||
inv.from_folio = True
|
||||
inv.folio_ids = [(6, 0, folios.ids)]
|
||||
|
||||
# Business methods
|
||||
@api.one
|
||||
def _get_outstanding_folios_JSON(self):
|
||||
self.outstanding_folios_debits_widget = json.dumps(False)
|
||||
if self.from_folio:
|
||||
payment_ids = self.folio_ids.mapped('payment_ids.id')
|
||||
if self.state == 'open':
|
||||
domain = [('account_id', '=', self.account_id.id),
|
||||
('partner_id', '!=', self.env['res.partner']._find_accounting_partner(self.partner_id).id),
|
||||
('reconciled', '=', False),
|
||||
('payment_id', 'in', payment_ids),
|
||||
'|',
|
||||
'&', ('amount_residual_currency', '!=', 0.0), ('currency_id','!=', None),
|
||||
'&', ('amount_residual_currency', '=', 0.0), '&', ('currency_id','=', None), ('amount_residual', '!=', 0.0)]
|
||||
account_partner = self.env['res.partner'].\
|
||||
_find_accounting_partner(self.partner_id).id
|
||||
domain = [
|
||||
('account_id', '=', self.account_id.id),
|
||||
('partner_id', '!=', account_partner),
|
||||
('reconciled', '=', False),
|
||||
('payment_id', 'in', payment_ids),
|
||||
'|', '&',
|
||||
('amount_residual_currency', '!=', 0.0),
|
||||
('currency_id', '!=', None),
|
||||
'&', ('amount_residual_currency', '=', 0.0),
|
||||
'&', ('currency_id', '=', None),
|
||||
('amount_residual', '!=', 0.0)]
|
||||
if self.type in ('out_invoice', 'in_refund'):
|
||||
domain.extend([('credit', '>', 0), ('debit', '=', 0)])
|
||||
type_payment = _('Outstanding credits in Folio')
|
||||
else:
|
||||
domain.extend([('credit', '=', 0), ('debit', '>', 0)])
|
||||
type_payment = _('Outstanding debits')
|
||||
info = {'title': '', 'outstanding': True, 'content': [], 'invoice_id': self.id}
|
||||
info = {'title': '',
|
||||
'outstanding': True,
|
||||
'content': [],
|
||||
'invoice_id': self.id}
|
||||
lines = self.env['account.move.line'].search(domain)
|
||||
currency_id = self.currency_id
|
||||
if len(lines) != 0:
|
||||
for line in lines:
|
||||
# get the outstanding residual value in invoice currency
|
||||
if line.currency_id and line.currency_id == self.currency_id:
|
||||
# get the outstanding residual value in inv. currency
|
||||
if line.currency_id and line.currency_id == \
|
||||
self.currency_id:
|
||||
amount_to_show = abs(line.amount_residual_currency)
|
||||
else:
|
||||
amount_to_show = line.company_id.currency_id.with_context(date=line.date).compute(abs(line.amount_residual), self.currency_id)
|
||||
if float_is_zero(amount_to_show, precision_rounding=self.currency_id.rounding):
|
||||
amount_to_show = line.company_id.currency_id.\
|
||||
with_context(date=line.date).\
|
||||
compute(abs(line.amount_residual), self.currency_id)
|
||||
if float_is_zero(
|
||||
amount_to_show,
|
||||
precision_rounding=self.currency_id.rounding
|
||||
):
|
||||
continue
|
||||
if line.ref :
|
||||
if line.ref:
|
||||
title = '%s : %s' % (line.move_id.name, line.ref)
|
||||
else:
|
||||
title = line.move_id.name
|
||||
@@ -112,10 +115,3 @@ class AccountInvoice(models.Model):
|
||||
info['title'] = type_payment
|
||||
self.outstanding_folios_debits_widget = json.dumps(info)
|
||||
self.has_folio_outstanding = True
|
||||
|
||||
@api.multi
|
||||
def unlink(self):
|
||||
for invoice in self:
|
||||
if invoice.number:
|
||||
raise UserError(_('You cannot delete an invoice after it has been validated (and received a number). You can set it back to "Draft" state and modify its content, then re-confirm it.'))
|
||||
return super(AccountInvoice, self).unlink()
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# Copyright 2017 Alexandre Díaz
|
||||
# Copyright 2017 Alexandre Díaz, Pablo Quesada, Darío Lodeiros
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
from odoo import models, fields, api, _
|
||||
from odoo.exceptions import ValidationError
|
||||
|
||||
@@ -145,7 +145,7 @@ class FolioAdvancePaymentInv(models.TransientModel):
|
||||
'origin': folio.name,
|
||||
'type': 'out_invoice',
|
||||
'reference': False,
|
||||
'folio_ids': [(6, 0, [folio.id])],
|
||||
'folio_ids': [(6, 0, [folio.id])], #REVIEW: Folio_ids is a computed field, Why need this value?
|
||||
'account_id': folio.partner_id.property_account_receivable_id.id,
|
||||
'partner_id': folio.partner_invoice_id.id,
|
||||
'invoice_line_ids': [(0, 0, {
|
||||
|
||||
Reference in New Issue
Block a user