Answer:
From Pharaoh's point of view:
October 5, merchandise sold on account to Ivanhoe Company
Dr Accounts receivable 5,240
 Cr Sales revenue 5,240
Dr Cost of goods sold 3,180
 Cr Inventory 3,180
October 8, defective merchandise is returned
Dr Sales returns and allowances 640
 Cr Accounts receivable 640
Dr Inventory 310
 Cr Cost of goods sold 310
From Ivanhoe's point of view:
October 5, merchandise sold on account from Pharaoh Company
Dr Inventory 5,240
 Cr Accounts payable 5,240
October 8, defective merchandise is returned
Dr Accounts payable 640
 Cr Inventory 640