ShippingBillingPayment tracking

I need some help to create the following document:

This document is intended to support a small retail business.

Workflow description:

I send products in different shipments to different customers.

To bill them I want to be able to see how much of each product I have shipped and also how much has already been billed.

Then I want to track different payments that cancel the amount owed by each customer.

Example

  1. May 5th: I send Mark 2 units of product A (worth $1 per unit)and 6 units of product B (worth $2 each)

  2. May 10th: I send Mark 3 units of product A (worth $1 per unit)and 5 units of product B (worth $2 each)

  3. May 30th: I bill Mark for 4 units of A and 4 of B

  4. June 1st: Mark pays me $10

  5. June 10th: I send Mark 3 units of product A (worth $1 per unit)and 5 units of product B (worth $2 each)

  6. First Problem: I want to bill Mark for the remainder of the shipments so i need to add all the Units Sent and subtract the Units that have already been Billed
    The answer is:
    A = 2+3-4+3=4 unit remaining to be billed
    B = 6+5-4+5=12 units remaining to be billed
    I bill him for 4 unit of A and 12 units of B

  7. Second Problem: i want to know how much money mark owes me for billed products. I have to add all the bills sent and subtract payments made.
    The answer is:
    Bills sent:
    First Bill 4x$1 + 4x$2 = 8
    Payment = -10
    Second Bill 4x$1 + 12x$2 = 28
    He still owes me $26