'get last purchase rates for all items'

def get_last_purchase_rate(self):

    conversion_rate = (flt(self.get(u'conversion_rate')) or 1.0)
    for d in self.get(u'items'):
        if d.item_code:
            last_purchase_details = get_last_purchase_details(d.item_code, self.name)
            if last_purchase_details:
                d.base_price_list_rate = (last_purchase_details[u'base_price_list_rate'] * (flt(d.conversion_factor) or 1.0))
                d.discount_percentage = last_purchase_details[u'discount_percentage']
                d.base_rate = (last_purchase_details[u'base_rate'] * (flt(d.conversion_factor) or 1.0))
                d.price_list_rate = (d.base_price_list_rate / conversion_rate)
                d.rate = (d.base_rate / conversion_rate)
            else:
                msgprint(_(u'Last    purchase    rate    not    found'))
                item_last_purchase_rate = frappe.db.get_value(u'Item', d.item_code, u'last_purchase_rate')
                if item_last_purchase_rate:
                    d.base_price_list_rate = d.base_rate = d.price_list_rate = d.rate = item_last_purchase_rate
