0

I am running Active Collab 5.8.7 with PHP 5.6. I am using the API to create an invoice. I am following the API per this documentation. My issue is that the post requires an invoice number and I have no way of telling what the next invoice number should be.

I would like to know if there is a way to use the API to get the next invoice number inline or to add an invoice and let the system pick an invoice number for you.

Currently, I am using a random sequence of characters when I create an invoice through the API and then when we try to manually add an invoice, the invoice number field is blank. There has to be an easier and more consistent way to handle invoice numbers.

Thanks, Larry

1 Answers1

0

I figured this out on my own and wanted to post it here in case someone else needs it:

//get next invoice number
function get_next_ac_invoice_number($client) {
    //get all invoices
    $result = $client->get('/reports/run?type=InvoicesFilter')->getJson();
    $invoices = $result['all']['invoices'];
    usort($invoices,'sortbyInvoiceId');
    $next_invoice_id = strval(intval(explode('-',$invoices[0]['number'])[0]) + 1) . '-' . date("Y");
    return $next_invoice_id;
}
function sortbyInvoiceId($a,$b){
    if ($a == $b) {
    return 0;
    }
    return ($b < $a) ? -1 : 1;
}

EDIT FEB 22 '17

There is a bunch of cruft in here for Braintree but you should be able to get the gist of it. One thing I would make sure you consider is that you clear out AC's trash before posting since the next invoice id function will not return trashed invoices and will cause a duplicate invoice id error.

//get next invoice number
function get_next_ac_invoice_number($client) {

#get all invoices
$trashit = $client->delete('/trash');
$result = $client->get('/reports/run?type=InvoicesFilter')->getJson();
$invoices = $result['all']['invoices'];
usort($invoices,'sortbyInvoiceId');
$next_invoice_id = strval(intval(explode('-',$invoices[0]['number'])[0]) + 1) . '-' . date("Y");
return $next_invoice_id;
}

//creates an invoice in active collab
function create_ac_invoice($customer, $subscription, $transaction, $client) {
//get the next invoice ID
get_next_ac_invoice_number($client);
$plans = Braintree_Plan::all();
$plan;
 foreach ($plans AS $myplan) {
    if (strtolower($myplan->id) == strtolower($subscription->planId)) {
        $plan = $myplan;
    }
 }

if (isset($transaction->discounts[0])) {
    $result = $client->post('invoices', [
        'company_id' => $customer['company_id'],
        'number' => get_next_ac_invoice_number($client),
        'items' => [
            [
                'description' => $plan->name . " - " . $plan->description,
                'quantity' => 1,
                'unit_cost' => $subscription->price
            ],
            [
                'description' => 'Promo Code Discount - ' . $transaction->discounts[0]->name,
                'quantity' => 1,
                'unit_cost' => (float) $transaction->discounts[0]->amount * -1
            ]
        ],
        'private_note' => 'Auto-generated by Braintree'
    ]);
} else {
    $result = $client->post('invoices', [
        'company_id' => $customer['company_id'],
        'number' => get_next_ac_invoice_number($client),
        'items' => [
            [
                'description' => $plan->name . " - " . $plan->description,
                'quantity' => 1,
                'unit_cost' => $subscription->price
            ]
        ],
        'private_note' => 'Auto-generated by Braintree'
    ]);
}

$invoice = $result->getJson();
if (isset($invoice['message'])) {
    //we have an error, let's log and send email
    $dump = print_r($invoice, true) . print_r($customer, true) . print_r($subscription, true);
    logit('ERROR', $dump);
    sendEmail($dump, 'Braintree Webhook Error Creating AC Invoice');
}

//mark the invoice as paid
$result = $client->post('payments', [

    'parent_type' => 'Invoice',
    'parent_id' => $invoice['single']['id'],
    'amount' => getTotalCost($subscription, $transaction),
    'comment' => 'Paid in full'
]);
$result = $result->getJson();
if (isset($result['message'])) {
    //we have an error, let's log and send email
    $dump = print_r($invoice, true) . print_r($customer, true) . print_r($subscription, true);
    logit('ERROR', $dump);
    sendEmail($dump, 'Braintree Webhook Error Creating AC Payment');
}
//send the invoice
$result = $client->put('invoices/' . $invoice['single']['id'] . '/send', [
    'recipients' => [
        $customer['email']
    ],
    'subject' => "New Invoice",
    'message' => "Thanks!",
    'allow_payments' => 2
]);
$result = $result->getJson();
if (isset($result['message'])) {
    //we have an error, let's log and send email
    $dump = print_r($invoice, true) . print_r($customer, true) . print_r($subscription, true);
    logit('ERROR', $dump);
    sendEmail($dump, 'Braintree Webhook Error Sending AC Invoice Email');
}

return $invoice;
}