perhaps
=SUMIFS(Sheet2!C$2:C$1000,Sheet2!$K$2:$K$1000,Sheet1!$B$2,Sheet2!$A$2:$A$1000,Sheet1!$C$2,Sheet2!$B$2:$B$1000,Sheet1!$A5)
as the columns on sheet1 match the order on sheet 2 - then i have not tried anything clever with also matching the header
No idea if this works in google sheets - as you did not answer
What version of excel are you using , as solutions can vary a lot depending on the version