@@ -578,7 +578,11 @@ def process(self):
578578
579579 # get buying rate
580580 if flt (row .qty ):
581- row .buying_rate = flt (row .buying_amount / flt (row .qty ), self .float_precision )
581+ row .buying_rate = (
582+ flt (row .buying_amount / flt (row .qty ), self .float_precision )
583+ if not row .delivered_by_supplier
584+ else None
585+ )
582586 row .base_rate = flt (row .base_amount / flt (row .qty ), self .float_precision )
583587 else :
584588 if self .is_not_invoice_row (row ):
@@ -630,7 +634,8 @@ def update_return_invoices(self, row):
630634 returned_item_row .qty += row .qty
631635 returned_item_row .base_amount += row .base_amount
632636
633- row .buying_amount = flt (flt (row .qty ) * flt (row .buying_rate ), self .currency_precision )
637+ if not row .delivered_by_supplier :
638+ row .buying_amount = flt (flt (row .qty ) * flt (row .buying_rate ), self .currency_precision )
634639
635640 def get_average_rate_based_on_group_by (self ):
636641 for key in list (self .grouped ):
@@ -799,6 +804,26 @@ def get_buying_amount(self, row, item_code):
799804 return self .calculate_buying_amount_from_sle (
800805 row , my_sle , parenttype , parent , item_row , item_code
801806 )
807+ elif (
808+ row .delivered_by_supplier
809+ and row .so_detail
810+ and (
811+ po_details := frappe .get_all (
812+ "Purchase Order Item" ,
813+ filters = {"sales_order_item" : row .so_detail , "docstatus" : 1 },
814+ pluck = "name" ,
815+ )
816+ )
817+ ):
818+ from frappe .query_builder .functions import Sum
819+
820+ table = frappe .qb .DocType ("Purchase Invoice Item" )
821+ query = (
822+ frappe .qb .from_ (table )
823+ .select (Sum (table .stock_qty * table .base_net_rate ))
824+ .where ((table .po_detail .isin (po_details )) & (table .docstatus == 1 ))
825+ )
826+ return flt (query .run ()[0 ][0 ])
802827 elif row .sales_order and row .so_detail :
803828 incoming_amount = self .get_buying_amount_from_so_dn (row .sales_order , row .so_detail , item_code )
804829 if incoming_amount :
@@ -951,6 +976,7 @@ def prepare_invoice_query(self):
951976 SalesInvoice .is_return ,
952977 SalesInvoiceItem .cost_center ,
953978 SalesInvoiceItem .serial_and_batch_bundle ,
979+ SalesInvoiceItem .delivered_by_supplier ,
954980 )
955981
956982 if self .filters .group_by == "Sales Person" :
0 commit comments