Always enter the line items into the invoice first, and only then apply the advance payment deduction.
If the invoice already shows a zero balance but still appears as unpaid in the statement, you need to delete the deduction from the line items and re-enter it. After doing so, the invoice should appear as correctly paid.
