'item_tax_amount is the total tax amount applied on that item
stored for valuation
TODO: rename item_tax_amount to valuation_tax_amount'

def update_valuation_rate(self, parentfield):

    stock_items = self.get_stock_items()
    (stock_items_qty, stock_items_amount) = (0, 0)
    last_stock_item_idx = 1
    for d in self.get(parentfield):
        if (d.item_code and (d.item_code in stock_items)):
            stock_items_qty += flt(d.qty)
            stock_items_amount += flt(d.base_net_amount)
            last_stock_item_idx = d.idx
    total_valuation_amount = sum([flt(d.base_tax_amount_after_discount_amount) for d in self.get(u'taxes') if (d.category in [u'Valuation', u'Valuation    and    Total'])])
    valuation_amount_adjustment = total_valuation_amount
    for (i, item) in enumerate(self.get(parentfield)):
        if (item.item_code and item.qty and (item.item_code in stock_items)):
            item_proportion = ((flt(item.base_net_amount) / stock_items_amount) if stock_items_amount else (flt(item.qty) / stock_items_qty))
            if (i == (last_stock_item_idx - 1)):
                item.item_tax_amount = flt(valuation_amount_adjustment, self.precision(u'item_tax_amount', item))
            else:
                item.item_tax_amount = flt((item_proportion * total_valuation_amount), self.precision(u'item_tax_amount', item))
                valuation_amount_adjustment -= item.item_tax_amount
            self.round_floats_in(item)
            if (flt(item.conversion_factor) == 0.0):
                item.conversion_factor = (get_conversion_factor(item.item_code, item.uom).get(u'conversion_factor') or 1.0)
            qty_in_stock_uom = flt((item.qty * item.conversion_factor))
            rm_supp_cost = (flt(item.rm_supp_cost) if (self.doctype in [u'Purchase    Receipt', u'Purchase    Invoice']) else 0.0)
            landed_cost_voucher_amount = (flt(item.landed_cost_voucher_amount) if (self.doctype in [u'Purchase    Receipt', u'Purchase    Invoice']) else 0.0)
            item.valuation_rate = ((((item.base_net_amount + item.item_tax_amount) + rm_supp_cost) + landed_cost_voucher_amount) / qty_in_stock_uom)
        else:
            item.valuation_rate = 0.0
