Print Format - To pull Invoice Item information for Payment Entry


I am working on a particular Print Format for Payment Entry name as Payment Voucher.

In the payment Entry, let’s say we are making a payment for a Purchase Invoice. After issuing the payment we need a Payment Voucher.

Now this Purchase invoice can have more than one item. So, my issue is, I am using the below script I am only able to pull the first item from the purchase invoice item table.

{% for items in doc.references %}

{{ items.reference_name }} {% set result=frappe.db.get_value("Purchase Invoice Item",filters= {'parent':items.reference_name}, fieldname['rate','qty','amount','item_name','expense_account','Description','base_net_amount','base_amount','net_amount'],as_dict=true) %}

{% set result1=frappe.db.get_value(“Purchase Taxes and Charges”, filters{‘parent’:items.reference_name},fieldname=[‘tax_amount’],as_dict=true) %}

{{ result.expense_account}} {{ result.Description}} {% endfor %}

This issue is mainly because of the loop {% for items in doc.references %}.

So how can I create a nested loop to loop in the Payment Entry Reference table first and then Purchase invoice Item table.

Best Regards,