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

Re: Create direct invoice programmatically

To save people's time, I got a better solution that works (after I had fixed some missing join columns in the customer and branch tables!), using routines in the sales_order_ui include:

    $entry = new Cart(ST_SALESINVOICE, 0, true);
    get_customer_details_to_order($entry, $customer_id, $branch_id);
    add_to_order($entry, $item_code, 1, $gross, 0, $item_title);
    $entry->cust_ref = $ref;
    $taxes = $entry->get_taxes($shipping);
    $entry->write(0);

When I had created the customer and branch records prior to creating the invoice, I had missed location, and shipper id, both of which are needed in order to re-read the sales order when it generates delivery notes and invoices.

When I have the customer payment allocation working, I'll submit the module to Joe in case anyone else finds it useful.

Re: Create direct invoice programmatically

I, for one, would find a transaction import module very helpful. Have you made progress on this?