Active Collab API - 获取下一个发票编号

时间:2016-08-08 19:53:08

标签: php activecollab

我正在使用PHP 5.6运行Active Collab 5.8.7。我正在使用API​​创建发票。我按照this文档关注API。我的问题是该帖子需要一个发票号码,我无法告知下一个发票号码应该是什么。

我想知道是否有办法使用API​​获取内联的下一个发票号或添加发票,让系统为您选择发票号。

目前,我在通过API创建发票时使用随机字符序列,然后当我们尝试手动添加发票时,发票编号字段为空白。必须有一种更简单,更一致的方式来处理发票号码。

谢谢, 拉里

1 个答案:

答案 0 :(得分:0)

我自己想出来并希望在此发布,万一其他人需要它:

//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;
}

编辑FEB 22 '17

Braintree在这里有很多东西,但你应该能够得到它的要点。我要确保您考虑的一件事是您在发布之前清除了AC的垃圾,因为下一个发票ID功能不会返回已删除的发票,并且会导致发票ID重复错误。

//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;
}