trytond-stock_valued_discount / shipment.py

#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 ModelView, ModelSQL, fields
from trytond.pyson import Eval
from trytond.transaction import Transaction
from trytond.pool import Pool
from decimal import Decimal

class ShipmentOut(ModelSQL, ModelView):
    _name = 'stock.shipment.out'

    def get_tax_amount(self, ids, name):
        '''
        Compute tax amount for each ShipmentOut
        '''
        pool = Pool()
        currency_obj = pool.get('currency.currency')
        tax_obj = pool.get('account.tax')
        invoice_obj = pool.get('account.invoice')

        amounts = {}
        for shipment in self.browse(ids):
            if (shipment.state in self._states_cached
                    and shipment.tax_amount_cache is not None):
                amounts[shipment.id] = shipment.tax_amount_cache
                continue
            context = self.get_tax_context(shipment)
            taxes = {}
            for move in shipment.outgoing_moves:
                unit_price = move.unit_price
                if move.discount:
                    unit_price = (move.unit_price - move.unit_price * move.discount * Decimal('0.01'))
                with Transaction().set_context(context):
                    tax_list = tax_obj.compute(
                            [t.id for t in move.product.customer_taxes], 
                            unit_price, move.quantity)
                # Don't round on each line to handle rounding error
                for tax in tax_list:
                    key, val = invoice_obj._compute_tax(tax, 'out_invoice')
                    if not key in taxes:
                        taxes[key] = val['amount']
                    else:
                        taxes[key] += val['amount']
            amount = sum((currency_obj.round(shipment.company.currency, taxes[key])
                    for key in taxes), Decimal(0))
            amounts[shipment.id] = currency_obj.round(shipment.company.currency, amount)
        return amounts


ShipmentOut()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.