Skip to content

Commit

Permalink
[IMP] sale_gathering: New option to generate only one invoice per dow…
Browse files Browse the repository at this point in the history
…npayment and set the invoice to zero.
  • Loading branch information
nicomacr committed May 11, 2021
1 parent 5015830 commit 9c0b974
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
2 changes: 1 addition & 1 deletion sale_gathering/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
##############################################################################
from . import sale_order
from . import sale_order_line
from . import account_move
# from . import account_move
25 changes: 25 additions & 0 deletions sale_gathering/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,28 @@ class SaleOrder(models.Model):
_inherit = 'sale.order'

is_gathering = fields.Boolean('Is Gathering?')

amount_gathering = fields.Float(compute="_compute_amount_gathering")

def _compute_amount_gathering(self):
for rec in self:
# amount_lines = sum(rec.order_line.filtered(
# lambda x: not x.is_downpayment).mapped(
# lambda x: x.qty_invoiced * x.price_unit))
amount_to_invoice = sum(rec.order_line.filtered(
lambda x: not x.is_downpayment).mapped('untaxed_amount_to_invoice'))
amount_invoiced = sum(rec.order_line.filtered(
lambda x: not x.is_downpayment).mapped('untaxed_amount_invoiced'))
rec.amount_gathering = sum(
rec.order_line.filtered('is_downpayment').mapped('price_unit')) - amount_invoiced - amount_to_invoice


def _get_invoiceable_lines(self, final=False):
"""Return the invoiceable lines for order `self`."""
invoiceable_lines = super()._get_invoiceable_lines(final=False)
if self.is_gathering and self.amount_gathering > 0.0:
for line in self.order_line.filtered('is_downpayment'):
if final:
invoiceable_lines |= line
return invoiceable_lines

12 changes: 9 additions & 3 deletions sale_gathering/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ def product_uom_change(self):

def _prepare_invoice_line(self):
result = super()._prepare_invoice_line()
if self.is_downpayment and self._context.get('invoice_ids', False):
invoices = self.env['account.move'].browse(self._context.get('invoice_ids'))
result['price_unit'] = sum(invoices.mapped('amount_untaxed'))
# if self.is_downpayment and self._context.get('invoice_gathering', False):
# invoices = self.env['account.move'].browse(self._context.get('invoice_ids'))
# result['price_unit'] = sum(invoices.mapped('amount_untaxed'))
# result['quantity'] = 1.0
if self.is_downpayment and self._context.get('invoice_gathering', False):
lines = self.order_id.order_line.filtered(lambda x: not x.is_downpayment and x.qty_to_invoice)
result['price_unit'] = lines and sum(lines.mapped('untaxed_amount_to_invoice')) or 0.0
result['quantity'] = -1.0
return result

3 changes: 3 additions & 0 deletions sale_gathering/views/sale_order_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<button name="%(sale.action_view_sale_advance_payment_inv)d" position="after">
<button name="%(sale.action_view_sale_advance_payment_inv)d" string="Create Gathering Invoice" type="action" context="{'default_advance_payment_method': 'fixed'}" attrs="{'invisible': ['|','|', ('is_gathering', '==', False), ('amount_total', '>', 0.0),('state', '!=' ,'sale')]}"/>
</button>
<field name="amount_total" position="after">
<field name="amount_gathering" widget="monetary" options="{'currency_field': 'currency_id'}"/>
</field>

</field>
</record>
Expand Down
14 changes: 9 additions & 5 deletions sale_gathering/wizards/sale_advance_payment_inv.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
class SaleAdvancePaymentInvWizard(models.TransientModel):
_inherit = "sale.advance.payment.inv"


advance_payment_method = fields.Selection(selection_add=[('invoice_gathering', 'Invoice Gathering')])
advance_payment_method = fields.Selection(selection_add=[
# ('invoice_gathering', 'Factura y NC descontando acopio'),
('invoice_gathering_zero', 'Factura en cero descontando acopio'),
])


def _prepare_so_line(self, order, analytic_tag_ids, tax_ids, amount):
Expand All @@ -21,9 +23,11 @@ def _prepare_so_line(self, order, analytic_tag_ids, tax_ids, amount):
def create_invoices(self):
sale_orders = self.env['sale.order'].browse(self._context.get('active_ids', []))

if self.advance_payment_method == 'invoice_gathering':
moves = sale_orders._create_invoices()
sale_orders.with_context(invoice_ids=moves.ids)._create_invoices(final=True)
# if self.advance_payment_method == 'invoice_gathering':
# sale_orders.with_context(invoice_gathering_no_invoiceable=True)._create_invoices()
# sale_orders.with_context(invoice_gathering=True)._create_invoices(final=True)
if self.advance_payment_method == 'invoice_gathering_zero':
sale_orders.with_context(invoice_gathering=True)._create_invoices(final=True)
else:
return super().create_invoices()
if self._context.get('open_invoices', False):
Expand Down

0 comments on commit 9c0b974

Please sign in to comment.