File: sale.py

package info (click to toggle)
tryton-modules-account-cash-rounding 7.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 396 kB
  • sloc: python: 530; xml: 78; makefile: 11; sh: 3
file content (57 lines) | stat: -rw-r--r-- 2,291 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# This file is part of Tryton.  The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from trytond.model import fields
from trytond.pool import Pool, PoolMeta
from trytond.transaction import Transaction


class Sale(metaclass=PoolMeta):
    __name__ = 'sale.sale'

    @fields.depends('company', methods=['_cash_round_total_amount'])
    def on_change_lines(self):
        pool = Pool()
        Config = pool.get('account.configuration')
        config = Config(1)
        super().on_change_lines()
        cash_rounding = config.get_multivalue(
            'cash_rounding', company=self.company.id if self.company else None)
        if cash_rounding:
            self.total_amount = self._cash_round_total_amount(
                self.total_amount)

    @classmethod
    def get_amount(cls, sales, names):
        pool = Pool()
        Config = pool.get('account.configuration')
        amounts = super().get_amount(sales, names)
        if 'total_amount' in names:
            config = Config(1)
            total_amounts = amounts['total_amount']
            for sale in sales:
                if config.get_multivalue(
                        'cash_rounding', company=sale.company.id):
                    amount = total_amounts[sale.id]
                    amount = sale._cash_round_total_amount(amount)
                    total_amounts[sale.id] = amount
        return amounts

    @fields.depends('currency', 'payment_term', 'company')
    def _cash_round_total_amount(self, amount):
        from trytond.modules.account_invoice.exceptions import (
            PaymentTermComputeError)
        pool = Pool()
        Date = pool.get('ir.date')
        if self.currency:
            amounts = [amount]
            if self.payment_term and self.company:
                with Transaction().set_context(company=self.company.id):
                    today = Date.today()
                try:
                    term_lines = self.payment_term.compute(
                        amount, self.company.currency, today)
                    amounts = [a for _, a in term_lines]
                except PaymentTermComputeError:
                    pass
            amount = sum(map(self.currency.cash_round, amounts))
        return amount