Skip to content

Commit

Permalink
stock_picking_invoice_link: improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaniszewska-dev committed Aug 2, 2022
1 parent d33bbf9 commit 5dd7704
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 105 deletions.
58 changes: 0 additions & 58 deletions stock_picking_invoice_link/models/account_invoice.py

This file was deleted.

3 changes: 0 additions & 3 deletions stock_picking_invoice_link/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ class AccountMove(models.Model):
string="Related Pickings",
store=True,
compute="_compute_picking_ids",
relation="account_invoice_stock_picking_rel",
column1="account_invoice_id",
column2="stock_picking_id",
help="Related pickings "
"(only when the invoice has been generated from a sale order).",
)
Expand Down
14 changes: 6 additions & 8 deletions stock_picking_invoice_link/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@ class SaleOrderLine(models.Model):
_inherit = "sale.order.line"

def get_stock_moves_link_invoice(self):
return self.mapped('move_ids')._filter_for_invoice_link()
return self.mapped("move_ids")._filter_for_invoice_link()


def _prepare_invoice_line(self, qty):
vals = super()._prepare_invoice_line(qty)
def _prepare_invoice_line(self, **optional_values):
vals = super()._prepare_invoice_line(**optional_values)
stock_moves = self.get_stock_moves_link_invoice()
# Invoice returned moves marked as to_refund
precision = self.env['decimal.precision'].precision_get(
'Product Unit of Measure'
precision = self.env["decimal.precision"].precision_get(
"Product Unit of Measure"
)
if float_compare(
qty, 0.0, precision_digits=precision) < 0:
if float_compare(self.product_uom_qty, 0.0, precision_digits=precision) < 0:
stock_moves = stock_moves.filtered(
lambda m: m.to_refund and not m.invoice_line_ids
)
Expand Down
30 changes: 17 additions & 13 deletions stock_picking_invoice_link/models/stock_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,30 @@ def write(self, vals):
def _filter_for_invoice_link(self):
return self.filtered(
lambda x: (
x.state == 'done' and not (any(
inv.state != 'cancel' for inv in x.invoice_line_ids.mapped(
'invoice_id'))) and not x.scrapped and (
x.location_dest_id.usage == 'customer' or
(x.location_id.usage == 'customer' and
x.to_refund))
x.state == "done"
and not (
any(
inv.state != "cancel"
for inv in x.invoice_line_ids.mapped("move_id")
)
)
and not x.scrapped
and (
x.location_dest_id.usage == "customer"
or (x.location_id.usage == "customer" and x.to_refund)
)
)
)

def _action_done(self):
res = super()._action_done()
precision = self.env['decimal.precision'].precision_get(
'Product Unit of Measure'
def _action_done(self, cancel_backorder=False):
res = super()._action_done(cancel_backorder)
precision = self.env["decimal.precision"].precision_get(
"Product Unit of Measure"
)
moves = self._filter_for_invoice_link()
# Add extra filtering on products with invoice_policy = delivery as
# the link was already set at invoice creation
for move in moves.filtered(
lambda m: m.product_id.invoice_policy != "delivery"
):
for move in moves.filtered(lambda m: m.product_id.invoice_policy != "delivery"):
if float_compare(
move.sale_line_id.qty_to_invoice, 0.0, precision_digits=precision
) < 0 and (not move.to_refund or move.invoice_line_ids):
Expand Down
7 changes: 2 additions & 5 deletions stock_picking_invoice_link/models/stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@ class StockPicking(models.Model):
_inherit = "stock.picking"

invoice_ids = fields.Many2many(
comodel_name='account.invoice',
comodel_name="account.move",
copy=False,
string='Invoices',
string="Invoices",
readonly=True,
relation="account_invoice_stock_picking_rel",
column1="stock_picking_id",
column2="account_invoice_id"
)

def action_view_invoice(self):
Expand Down
31 changes: 15 additions & 16 deletions stock_picking_invoice_link/tests/test_stock_picking_invoice_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ def setUpClass(cls, chart_template_ref=None):
def test_00_sale_stock_invoice_link(self):
pick_obj = self.env["stock.picking"]
# invoice on order
inv_id = self.so.action_invoice_create()
inv_0 = inv_obj.browse(inv_id)
inv_0 = self.so._create_invoices()
# deliver partially
self.assertEqual(
self.so.invoice_status,
Expand Down Expand Up @@ -145,24 +144,24 @@ def test_00_sale_stock_invoice_link(self):
)
# Check links
self.assertEqual(
inv_0.picking_ids, pick_1 | pick_2,
"Invoice 0 must be linked to all the deliveries"
inv_0.picking_ids,
pick_1 | pick_2,
"Invoice 0 must be linked to all the deliveries",
)
self.assertEqual(
inv_0.invoice_line_ids.mapped('move_line_ids'),
pick_1.move_lines.filtered(
lambda x: x.product_id.invoice_policy == "order"
) |
pick_2.move_lines.filtered(
inv_0.invoice_line_ids.mapped("move_line_ids"),
pick_1.move_lines.filtered(lambda x: x.product_id.invoice_policy == "order")
| pick_2.move_lines.filtered(
lambda x: x.product_id.invoice_policy == "order"
)
,
),
"Invoice 0 lines must be link to all delivery lines for "
"ordered quantities"
"ordered quantities",
)
self.assertEqual(
inv_1.picking_ids, pick_1,
"Invoice 1 must link to only First Partial Delivery")
inv_1.picking_ids,
pick_1,
"Invoice 1 must link to only First Partial Delivery",
)
self.assertEqual(
inv_1.invoice_line_ids.mapped("move_line_ids"),
pick_1.move_lines.filtered(
Expand All @@ -182,8 +181,8 @@ def test_00_sale_stock_invoice_link(self):
)
# Invoice view
result = pick_1.action_view_invoice()
self.assertEqual(result['views'][0][1], 'tree')
self.assertEqual(pick_1.invoice_ids, inv_1 | inv_0)
self.assertEqual(result["views"][0][1], "tree")
self.assertEqual(pick_1.invoice_ids.ids, (inv_0 | inv_1).ids)
# Mock multiple invoices linked to a picking
inv_3 = inv_1.copy()
inv_3.picking_ids |= pick_1
Expand Down
4 changes: 2 additions & 2 deletions stock_picking_invoice_link/views/account_invoice_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<odoo>
<!-- Copyright 2013-2014 Alexis de Lattre <[email protected]>
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<record id="invoice_form" model="ir.ui.view">
<field name="name">related.pickings.account.invoice.form</field>
<record id="view_move_form" model="ir.ui.view">
<field name="name">related.pickings.account.move.form</field>
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_form" />
<field name="arch" type="xml">
Expand Down

0 comments on commit 5dd7704

Please sign in to comment.