Repository: frappe/erpnext
Pull request: fix: get project from PO into payment entry
Fix for [Issue 16662]( https://github.com/frappe/erpnext/issues/16662)

Now, creating payments for Purchase orders will pull the project from the line items.

![output](https://user-images.githubusercontent.com/3272205/148494005-8d85db4f-47cc-473b-b9e2-2a9908ffe166.gif)

`no-docs`


Problem statement:
Payment entry from Purchase Invoice does NOT catch Project from PO line
I want to check profitability on Project base and encounter one problem with payment entries created from a Purchase Invoice.

The item in my PO is linked to a certain project

### expected behavior 
when I use "Make Payment" from that PO, the Project which is connected to the item lines in the PO should be fetched in the payment entry automatically

### actual behavior
the connected Project is **not** fetched automatically in the PE and has to be selected manually

Hints from issue discussion:
Thanks for reporting this @vrms

Commit message: fix: get project from PO into payment entry

Patch to review:
diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py
index c1b056b9c704..18f35722c0fb 100644
--- a/erpnext/accounts/doctype/payment_entry/payment_entry.py
+++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py
@@ -3,6 +3,7 @@
 
 
 import json
+from functools import reduce
 
 import frappe
 from frappe import ValidationError, _, scrub, throw
@@ -1522,6 +1523,8 @@ def get_payment_entry(dt, dn, party_amount=None, bank_account=None, bank_amount=
 	pe.paid_amount = paid_amount
 	pe.received_amount = received_amount
 	pe.letter_head = doc.get("letter_head")
+	if dt == 'Purchase Order':
+		pe.project = reduce(lambda prev,cur: prev or cur, [x.project for x in doc.get('items')], None) # get first non-empty project from items
 
 	if pe.party_type in ["Customer", "Supplier"]:
 		bank_account = get_party_bank_account(pe.party_type, pe.party)


Code review comments:
diff_hunk: @@ -1522,6 +1523,8 @@ def get_payment_entry(dt, dn, party_amount=None, bank_account=None, bank_amount=
 	pe.paid_amount = paid_amount
 	pe.received_amount = received_amount
 	pe.letter_head = doc.get("letter_head")
+	if dt == 'Purchase Order':
path: erpnext/accounts/doctype/payment_entry/payment_entry.py
text: @user1:
This will also be applicable for other transactions like "Sales Order", "Purchase Receipt", "Delivery Note", "Sales Invoice" and "Purchase Invoice". Can we make this more generic to handle for all doctypes?

@author:
We can avoid 'Purchase Receipt' and 'Delivery Note', as there are no process available to create Payment Entry from them.