Topic: Create direct invoice programmatically
I'm struggling with an add-on I'm writing to import transactions from paypal. I have a version working fine creating bank deposits as cash sales, but what I'd really like to do is create/update customer records, create direct invoices, and then allocate payments. The reason being that many customers might start with a paypal payment but later get invoiced directly for other work. Having all their details already in the system makes it less work later.
I have tried stepping though the cart class as a direct invoice is entered in the screen, and as far as I can see the following code might be the minimum required:
$entry = new Cart(ST_SALESINVOICE, 0);
$entry->set_customer($customer_id, $company, $currency, 0, -1, 0);
$myrow = get_sales_type($paypal_sales_type_id);
$entry->set_sales_type($myrow['id'], $myrow['sales_type'],
$myrow['tax_included'], $myrow['factor']);
$entry->add_to_cart(0, $item_code, 1, $gross, 0, 0, $item_title, 0, 0, 0);
$taxes = $entry->get_taxes($shipping);
$entry->write(1);
There is a value in every variable, and the customer records have been set up, and I've created direct invoices through FA using those generated customers.
But when I run my import I get an error for every transaction:
You have missing or invalid sales document in database (type:30, number:x).
Is anyone able to help?
Many thanks
Alastair