File: purchase.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 (78 lines) | stat: -rw-r--r-- 2,889 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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.pyson import Eval
from trytond.transaction import Transaction


class Purchase(metaclass=PoolMeta):
    __name__ = 'purchase.purchase'

    cash_rounding = fields.Boolean(
        "Cash Rounding",
        states={
            'readonly': Eval('state') != 'draft',
            })

    @fields.depends('party')
    def on_change_party(self):
        cursor = Transaction().connection.cursor()
        table = self.__table__()
        super().on_change_party()
        if self.party:
            cursor.execute(*table.select(table.cash_rounding,
                    where=table.party == self.party.id,
                    order_by=table.id,
                    limit=1))
            row = cursor.fetchone()
            if row:
                self.cash_rounding, = row

    @fields.depends(methods=['on_change_lines'])
    def on_change_cash_rounding(self):
        self.on_change_lines()

    @fields.depends('cash_rounding', methods=['_cash_round_total_amount'])
    def on_change_lines(self):
        super().on_change_lines()
        if self.cash_rounding:
            self.total_amount = self._cash_round_total_amount(
                self.total_amount)

    @classmethod
    def get_amount(cls, purchases, names):
        amounts = super().get_amount(purchases, names)
        if 'total_amount' in names:
            total_amounts = amounts['total_amount']
            for purchase in purchases:
                if purchase.cash_rounding:
                    amount = total_amounts[purchase.id]
                    amount = purchase._cash_round_total_amount(amount)
                    total_amounts[purchase.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

    def _get_invoice_purchase(self):
        invoice = super()._get_invoice_purchase()
        invoice.cash_rounding = self.cash_rounding
        return invoice