[ADD]pms_api_rest: autoinvoice downpayments policy

This commit is contained in:
Darío Lodeiros
2022-11-13 21:28:58 +01:00
parent d6a9dcd395
commit 0f5c577d2c
10 changed files with 175 additions and 78 deletions

View File

@@ -20,6 +20,11 @@ class AccountJournal(models.Model):
string="For manual payments",
help="Use to pay for reservations",
)
avoid_autoinvoice_downpayment = fields.Boolean(
string="Avoid autoinvoice downpayment",
help="Avoid autoinvoice downpayment",
default=False,
)
is_simplified_invoice = fields.Boolean(
string="Simplified invoice",
help="Use to simplified invoice",

View File

@@ -1,6 +1,8 @@
# Copyright 2017 Dario Lodeiros
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, fields, models
from odoo import _, api, fields, models
class AccountPayment(models.Model):
@@ -143,21 +145,118 @@ class AccountPayment(models.Model):
# if msg:
# self.folio_id.message_post(subject=_("Payment Deleted"), body=msg)
# def post(self):
# rec = super(AccountPayment, self).post()
# if rec and not self._context.get("ignore_notification_post", False):
# for pay in self:
# if pay.folio_id:
# msg = _(
# "Payment of %s %s registered from %s \
# using %s payment method"
# ) % (
# pay.amount,
# pay.currency_id.symbol,
# pay.communication,
# pay.journal_id.name,
# )
# pay.folio_id.message_post(subject=_("Payment"), body=msg)
def action_post(self):
res = super(AccountPayment, self).action_post()
for payment in self:
if (
self.folio_ids
and self.company_id.pms_invoice_downpayment_policy != "no"
and not self.journal_id.avoid_autoinvoice_downpayment
):
checkout_ref = max(self.folio_ids.mapped("last_checkout"))
if (
self.company_id.pms_invoice_downpayment_policy == "all"
and payment.date < checkout_ref
) or (
self.company_id.pms_invoice_downpayment_policy
== "checkout_past_month"
and checkout_ref.month > payment.date.month
and checkout_ref.year >= payment.date.year
):
partner_id = (
payment.partner_id.id
or self.env.ref("pms.various_pms_partner").id
)
if payment.payment_type == "inbound":
invoice_wizard = self.env["folio.advance.payment.inv"].create(
{
"partner_invoice_id": partner_id,
"advance_payment_method": "fixed",
"fixed_amount": payment.amount,
}
)
move = invoice_wizard.with_context(
active_ids=self.folio_ids.ids,
return_invoices=True,
).create_invoices()
move.action_post()
move_lines = move.line_ids.filtered(
lambda line: line.account_id.user_type_id.type
in ("receivable", "payable")
)
payment_lines = payment.move_id.line_ids.filtered(
lambda line: line.account_id == move_lines.account_id
)
if not move_lines.reconciled:
(payment_lines + move_lines).reconcile()
else:
# We try to revert the advance payment invoice
advance_invoices = self.env["account.move"].search(
[
("folio_ids", "in", self.folio_ids.ids),
("state", "=", "posted"),
("payment_state", "=", "paid"),
("move_type", "=", "out_invoice"),
]
)
advance_invoices = advance_invoices.filtered(
lambda inv: any(
[
line.folio_line_ids.is_downpayment
for line in inv.invoice_line_ids
]
)
)
if advance_invoices:
match_inv = advance_invoices.filtered(
lambda inv: round(inv.amount_total, 2)
== round(payment.amount, 2)
)
move_reversal = (
self.env["account.move.reversal"]
.with_context(
active_model="account.move",
active_ids=match_inv.ids
if match_inv
else advance_invoices.ids,
)
.create(
{
"date": fields.Date.today(),
"reason": _("Deposit return"),
"refund_method": "refund",
}
)
)
move_reversal.reverse_moves()
move = move_reversal.new_move_ids
invoice_line = move.invoice_line_ids.filtered(
lambda l: not l.display_type
)
move.write(
{
"invoice_line_ids": [
(
1,
invoice_line.id,
{
"price_unit": payment.amount,
},
)
],
}
)
move_lines = move.line_ids.filtered(
lambda line: line.account_id.user_type_id.type
in ("receivable", "payable")
)
payment_lines = payment.move_id.line_ids.filtered(
lambda line: line.account_id == move_lines.account_id
)
move.sudo().action_post()
if not move_lines.reconciled:
(payment_lines + move_lines).reconcile()
return res
# def modify_payment(self):
# self.ensure_one()

View File

@@ -719,7 +719,7 @@ class FolioSaleLine(models.Model):
Otherwise, the quantity delivered is used.
"""
for line in self:
if line.folio_id.state not in ["draft"] and line.price_total > 0.0:
if line.folio_id.state not in ["draft"]:
line.qty_to_invoice = line.product_uom_qty - line.qty_invoiced
else:
line.qty_to_invoice = 0
@@ -996,13 +996,6 @@ class FolioSaleLine(models.Model):
should be added to the returned invoice line
"""
self.ensure_one()
if (qty > self.qty_to_invoice or qty <= 0) and not self.display_type:
raise ValueError(
_(
"The qty (%s) is wrong." % qty
+ " The quantity pending to invoice is %s" % self.qty_to_invoice
)
)
res = {
"display_type": self.display_type,
"sequence": self.sequence,

View File

@@ -2032,7 +2032,7 @@ class PmsFolio(models.Model):
(making sure to call super() to establish a clean extension chain).
"""
self.ensure_one()
journal = self._get_folio_default_journal(partner_invoice_id)
journal = self.pms_property_id._get_folio_default_journal(partner_invoice_id)
if not journal:
journal = (
self.env["account.move"]
@@ -2074,17 +2074,6 @@ class PmsFolio(models.Model):
}
return invoice_vals
def _get_folio_default_journal(self, partner_invoice_id):
self.ensure_one()
pms_property = self.pms_property_id
partner = self.env["res.partner"].browse(partner_invoice_id)
if not partner or (
not partner._check_enought_invoice_data()
and self._context.get("autoinvoice")
):
return pms_property.journal_simplified_invoice_id
return pms_property.journal_normal_invoice_id
def do_payment(
self,
journal,

View File

@@ -701,6 +701,21 @@ class PmsProperty(models.Model):
if not pms_property.journal_simplified_invoice_id.is_simplified_invoice:
pms_property.journal_simplified_invoice_id.is_simplified_invoice = True
@api.model
def _get_folio_default_journal(self, partner_invoice_id):
self.ensure_one()
partner = self.env["res.partner"].browse(partner_invoice_id)
if (
not partner
or partner.id == self.env.ref("pms.various_pms_partner").id
or (
not partner._check_enought_invoice_data()
and self._context.get("autoinvoice")
)
):
return self.journal_simplified_invoice_id
return self.journal_normal_invoice_id
def _get_adr(self, start_date, end_date, domain=False):
"""
Calculate monthly ADR for a property

View File

@@ -27,3 +27,19 @@ class ResCompany(models.Model):
- VAT, name, street, city, country""",
default=False,
)
pms_invoice_downpayment_policy = fields.Selection(
selection=[
("no", "Manual"),
("all", "All"),
("checkout_past_month", "Checkout past month"),
],
string="Downpayment policy invoce",
help="""
- Manual: Downpayment invoice will be created manually
- All: Downpayment invoice will be created automatically
- Current Month: Downpayment invoice will be created automatically
only for reservations with checkout date past of current month
""",
default="no",
)