mirror of
https://github.com/OCA/pms.git
synced 2025-01-29 00:17:45 +02:00
[ADD]pms_api_rest: Add Patch invoices complete
This commit is contained in:
@@ -19,3 +19,4 @@ class PmsAccountInvoiceInfo(Datamodel):
|
|||||||
saleLines = fields.List(NestedModel("pms.folio.sale.line.info"))
|
saleLines = fields.List(NestedModel("pms.folio.sale.line.info"))
|
||||||
narration = fields.String(required=False, allow_none=True)
|
narration = fields.String(required=False, allow_none=True)
|
||||||
portalUrl = fields.String(required=False, allow_none=True)
|
portalUrl = fields.String(required=False, allow_none=True)
|
||||||
|
moveType = fields.String(required=False, allow_none=True)
|
||||||
|
|||||||
@@ -588,6 +588,7 @@ class PmsFolioService(Component):
|
|||||||
else None,
|
else None,
|
||||||
moveLines=move_lines if move_lines else None,
|
moveLines=move_lines if move_lines else None,
|
||||||
portalUrl=portal_url,
|
portalUrl=portal_url,
|
||||||
|
moveType=move.move_type,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return invoices
|
return invoices
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
from odoo import _, fields
|
from odoo import _, fields
|
||||||
|
from odoo.exceptions import UserError
|
||||||
|
|
||||||
from odoo.addons.base_rest import restapi
|
from odoo.addons.base_rest import restapi
|
||||||
from odoo.addons.base_rest_datamodel.restapi import Datamodel
|
from odoo.addons.base_rest_datamodel.restapi import Datamodel
|
||||||
@@ -23,14 +24,13 @@ class PmsInvoiceService(Component):
|
|||||||
input_param=Datamodel("pms.invoice.info"),
|
input_param=Datamodel("pms.invoice.info"),
|
||||||
auth="jwt_api_pms",
|
auth="jwt_api_pms",
|
||||||
)
|
)
|
||||||
|
# flake8: noqa: C901
|
||||||
def update_invoice(self, invoice_id, pms_invoice_info):
|
def update_invoice(self, invoice_id, pms_invoice_info):
|
||||||
invoice = self.env["account.move"].browse(invoice_id)
|
invoice = self.env["account.move"].browse(invoice_id)
|
||||||
|
if invoice.move_type in ["in_refund", "out_refund"]:
|
||||||
# Build update values dict
|
raise UserError(_("You can't update a refund invoice"))
|
||||||
# TODO: Missing data:
|
if invoice.payment_state == "reversed":
|
||||||
# - invoice comment (narration)
|
raise UserError(_("You can't update a reversed invoice"))
|
||||||
# - add new invoice lines (from saleLines selected?)
|
|
||||||
|
|
||||||
new_vals = {}
|
new_vals = {}
|
||||||
if (
|
if (
|
||||||
pms_invoice_info.partnerId
|
pms_invoice_info.partnerId
|
||||||
@@ -47,97 +47,73 @@ class PmsInvoiceService(Component):
|
|||||||
# send to service, the lines that are not sent we assume that
|
# send to service, the lines that are not sent we assume that
|
||||||
# they have been eliminated
|
# they have been eliminated
|
||||||
if pms_invoice_info.moveLines and pms_invoice_info.moveLines is not None:
|
if pms_invoice_info.moveLines and pms_invoice_info.moveLines is not None:
|
||||||
new_vals["invoice_line_ids"] = []
|
cmd_invoice_lines = self._get_invoice_lines_commands(
|
||||||
for line in invoice.invoice_line_ids:
|
invoice, pms_invoice_info
|
||||||
line_info = [
|
|
||||||
item for item in pms_invoice_info.moveLines if item.id == line.id
|
|
||||||
]
|
|
||||||
if line_info:
|
|
||||||
line_info = line_info[0]
|
|
||||||
line_values = {}
|
|
||||||
if line_info.name and line_info.name != line.name:
|
|
||||||
line_values["name"] = line_info.name
|
|
||||||
if line_info.quantity and line_info.quantity != line.quantity:
|
|
||||||
line_values["quantity"] = line_info.quantity
|
|
||||||
if line_values:
|
|
||||||
new_vals["invoice_line_ids"].append((1, line.id, line_values))
|
|
||||||
else:
|
|
||||||
new_vals["invoice_line_ids"].append((2, line.id))
|
|
||||||
# Get the new lines to add in invoice
|
|
||||||
new_invoice_lines_info = list(
|
|
||||||
filter(lambda item: not item.id, pms_invoice_info.moveLines)
|
|
||||||
)
|
)
|
||||||
if new_invoice_lines_info:
|
if cmd_invoice_lines:
|
||||||
partner = (
|
new_vals["invoice_line_ids"] = cmd_invoice_lines
|
||||||
self.env["res.partner"].browse(pms_invoice_info.partnerId)
|
if new_vals:
|
||||||
if pms_invoice_info.partnerId
|
# Update Invoice
|
||||||
else invoice.partner_id
|
# When modifying an invoice, depending on the company's configuration,
|
||||||
)
|
# and the invoice stateit will be modified directly or a reverse
|
||||||
folios = self.env["pms.folio"].browse(
|
# of the current invoice will be created to later create a new one
|
||||||
list(
|
# with the updated data.
|
||||||
|
# TODO: to create core pms correct_invoice_policy field
|
||||||
|
# if invoice.state != "draft" and company.corrective_invoice_policy == "strict":
|
||||||
|
if invoice.state != "draft":
|
||||||
|
# invoice create refund
|
||||||
|
# new invoice with new_vals
|
||||||
|
move_reversal = (
|
||||||
|
self.env["account.move.reversal"]
|
||||||
|
.with_context(active_model="account.move", active_ids=invoice.ids)
|
||||||
|
.create(
|
||||||
{
|
{
|
||||||
self.env["folio.sale.line"]
|
"date": fields.Date.today(),
|
||||||
.browse(line.saleLineId)
|
"reason": _("Invoice modification"),
|
||||||
.folio_id.id
|
"refund_method": "modify",
|
||||||
for line in list(
|
|
||||||
filter(
|
|
||||||
lambda item: item.name,
|
|
||||||
pms_invoice_info.moveLines,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
new_vals["invoice_line_ids"].extend(
|
reversal_action = move_reversal.reverse_moves()
|
||||||
[
|
reverse_invoice = self.env["account.move"].browse(
|
||||||
item["invoice_line_ids"]
|
reversal_action["res_id"]
|
||||||
for item in folios.get_invoice_vals_list(
|
|
||||||
lines_to_invoice={
|
|
||||||
new_invoice_lines_info[i]
|
|
||||||
.saleLineId: new_invoice_lines_info[i]
|
|
||||||
.quantity
|
|
||||||
for i in range(0, len(new_invoice_lines_info))
|
|
||||||
},
|
|
||||||
partner_invoice_id=partner.id,
|
|
||||||
)
|
|
||||||
][0]
|
|
||||||
)
|
)
|
||||||
if not new_vals:
|
invoice = reverse_invoice
|
||||||
return invoice.id
|
invoice.sudo().action_post()
|
||||||
|
# If change invoice by reversal, and new_vals has invoice_line_ids
|
||||||
|
# we need to mapp the new invoice lines with the new invoice
|
||||||
|
reverse_lines = []
|
||||||
|
for line in new_vals["invoice_line_ids"]:
|
||||||
|
origin_line = self.env["account.move.line"].browse(line[1])
|
||||||
|
sale_line_id = origin_line.sale_line_ids.id
|
||||||
|
reverse_line = reverse_invoice.invoice_line_ids.filtered(
|
||||||
|
lambda item: item.sale_line_ids.id == sale_line_id
|
||||||
|
and item.price_unit == origin_line.price_unit
|
||||||
|
and item.quantity == origin_line.quantity
|
||||||
|
)
|
||||||
|
if line[0] == 2:
|
||||||
|
reverse_lines.append((2, reverse_line[0].id))
|
||||||
|
elif line[0] == 1:
|
||||||
|
reverse_lines.append((1, reverse_line[0].id, line))
|
||||||
|
else:
|
||||||
|
reverse_lines.append(line)
|
||||||
|
if reverse_lines:
|
||||||
|
new_vals["invoice_line_ids"] = reverse_lines
|
||||||
|
|
||||||
# Update Invoice
|
invoice = self._direct_move_update(invoice, new_vals)
|
||||||
# When modifying an invoice, depending on the company's configuration,
|
# Update invoice lines name
|
||||||
# and the invoice stateit will be modified directly or a reverse
|
for item in pms_invoice_info.moveLines:
|
||||||
# of the current invoice will be created to later create a new one
|
if item.saleLineId in invoice.invoice_line_ids.mapped(
|
||||||
# with the updated data.
|
"folio_line_ids.id"
|
||||||
# TODO: to create core pms correct_invoice_policy field
|
):
|
||||||
# if invoice.state != "draft" and company.corrective_invoice_policy == "strict":
|
invoice_line = invoice.invoice_line_ids.filtered(
|
||||||
if invoice.state != "draft":
|
lambda r: item.saleLineId in r.folio_line_ids.ids
|
||||||
# invoice create refund
|
)
|
||||||
# new invoice with new_vals
|
invoice_line.write({"name": item.name})
|
||||||
move_reversal = (
|
if pms_invoice_info.narration is not None:
|
||||||
self.env["account.move.reversal"]
|
invoice.write({"narration": pms_invoice_info.narration})
|
||||||
.with_context(active_model="account.move", active_ids=invoice.ids)
|
if invoice.state == "draft" and pms_invoice_info.state == "confirm":
|
||||||
.create(
|
invoice.action_post()
|
||||||
{
|
|
||||||
"date": fields.Date.today(),
|
|
||||||
"reason": _("Invoice modification"),
|
|
||||||
"refund_method": "modify",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
)
|
|
||||||
reversal_action = move_reversal.reverse_moves()
|
|
||||||
reverse_invoice = self.env["account.move"].browse(reversal_action["res_id"])
|
|
||||||
invoice = reverse_invoice
|
|
||||||
|
|
||||||
invoice = self._direct_move_update(invoice, new_vals)
|
|
||||||
# Update invoice lines name
|
|
||||||
for item in pms_invoice_info.moveLines:
|
|
||||||
if item.saleLineId in invoice.invoice_line_ids.mapped("folio_line_ids.id"):
|
|
||||||
invoice_line = invoice.invoice_line_ids.filtered(
|
|
||||||
lambda r: item.saleLineId in r.folio_line_ids.ids
|
|
||||||
)
|
|
||||||
invoice_line.write({"name": item.name})
|
|
||||||
return invoice.id
|
return invoice.id
|
||||||
|
|
||||||
def _direct_move_update(self, invoice, new_vals):
|
def _direct_move_update(self, invoice, new_vals):
|
||||||
@@ -185,3 +161,59 @@ class PmsInvoiceService(Component):
|
|||||||
}
|
}
|
||||||
template.send_mail(invoice.id, force_send=True, email_values=email_values)
|
template.send_mail(invoice.id, force_send=True, email_values=email_values)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def _get_invoice_lines_commands(self, invoice, pms_invoice_info):
|
||||||
|
cmd_invoice_lines = []
|
||||||
|
for line in invoice.invoice_line_ids:
|
||||||
|
line_info = [
|
||||||
|
item for item in pms_invoice_info.moveLines if item.id == line.id
|
||||||
|
]
|
||||||
|
if line_info:
|
||||||
|
line_info = line_info[0]
|
||||||
|
line_values = {}
|
||||||
|
if line_info.name and line_info.name != line.name:
|
||||||
|
line_values["name"] = line_info.name
|
||||||
|
if line_info.quantity and line_info.quantity != line.quantity:
|
||||||
|
line_values["quantity"] = line_info.quantity
|
||||||
|
if line_values:
|
||||||
|
cmd_invoice_lines.append((1, line.id, line_values))
|
||||||
|
else:
|
||||||
|
cmd_invoice_lines.append((2, line.id))
|
||||||
|
# Get the new lines to add in invoice
|
||||||
|
new_invoice_lines_info = list(
|
||||||
|
filter(lambda item: not item.id, pms_invoice_info.moveLines)
|
||||||
|
)
|
||||||
|
if new_invoice_lines_info:
|
||||||
|
partner = (
|
||||||
|
self.env["res.partner"].browse(pms_invoice_info.partnerId)
|
||||||
|
if pms_invoice_info.partnerId
|
||||||
|
else invoice.partner_id
|
||||||
|
)
|
||||||
|
folios = self.env["pms.folio"].browse(
|
||||||
|
list(
|
||||||
|
{
|
||||||
|
self.env["folio.sale.line"].browse(line.saleLineId).folio_id.id
|
||||||
|
for line in list(
|
||||||
|
filter(
|
||||||
|
lambda item: item.name,
|
||||||
|
pms_invoice_info.moveLines,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
cmd_invoice_lines.extend(
|
||||||
|
[
|
||||||
|
item["invoice_line_ids"]
|
||||||
|
for item in folios.get_invoice_vals_list(
|
||||||
|
lines_to_invoice={
|
||||||
|
new_invoice_lines_info[i]
|
||||||
|
.saleLineId: new_invoice_lines_info[i]
|
||||||
|
.quantity
|
||||||
|
for i in range(0, len(new_invoice_lines_info))
|
||||||
|
},
|
||||||
|
partner_invoice_id=partner.id,
|
||||||
|
)
|
||||||
|
][0]
|
||||||
|
)
|
||||||
|
return cmd_invoice_lines
|
||||||
|
|||||||
Reference in New Issue
Block a user