/** * Updates a customer at Acumulus * @param $vars : $vars from the hook for API credentials * @param $clientid : clientid to update * @param null $customerid : customerid at Acumulus to update * @throws Exception * @return $customerid : new/updated customerid at Acumulus */ function updateCustomer($vars, $clientid, $customerid = null) { global $whmcs; $whmcs->load_function('invoice'); $clientQuery = mysql_query('SELECT tblclients.*, tblcustomfieldsvalues.value AS vatnumber FROM tblclients LEFT JOIN tblcustomfieldsvalues ON tblclients.id = tblcustomfieldsvalues.relid AND tblcustomfieldsvalues.fieldid = (SELECT id FROM tblcustomfields WHERE type = "client" AND fieldname = "' . $vars['vat_field'] . '" LIMIT 1) WHERE tblclients.id = ' . $clientid . ' LIMIT 1'); if (mysql_num_rows($clientQuery) != 1) { throw new Exception('Failed to receive client ' . $clientid); } $clientFetch = mysql_fetch_assoc($clientQuery); $api = new api($vars['code'], $vars['username'], $vars['password']); $api->setCategory('contacts')->setAction('contact_manage'); if (isset($vars['debug']) && $vars['debug'] == 'on') { $api->enableDebug($vars['debug_email']); } if ($clientFetch['acumulusid'] != null) { $api->setParam('contact/contactid', $clientFetch['acumulusid']); } if ($customerid != null) { $api->setParam('contact/contactid', $customerid); } if ($clientFetch['country'] == 'NL') { $api->setParam('contact/contactlocationcode', 1); } elseif (inEurope($clientFetch['country'])) { $api->setParam('contact/contactlocationcode', 2); } else { $api->setParam('contact/contactlocationcode', 3); } $taxData = getTaxRate(1, $clientQuery['state'], $clientQuery['country']); $api->setParams(array('contact' => array('contactemail' => $clientFetch['email'], 'contacttype' => 1, 'overwriteifexists' => 1, 'contactname1' => ucfirst($clientFetch['firstname']) . ' ' . $clientFetch['lastname'], 'contactname2' => '', 'contactperson' => '', 'contactsalutation' => '', 'contactaddress1' => $clientFetch['address1'], 'contactaddress2' => $clientFetch['address2'], 'contactpostalcode' => $clientFetch['postcode'], 'contactcity' => $clientFetch['city'], 'contactcountrycode' => inEurope($clientFetch['country']) ? $clientFetch['country'] : '', 'contactvatnumber' => $clientFetch['vatnumber'], 'contactvatratebase' => $clientFetch['taxexempt'] == 'on' ? -1 : round($taxData['rate']), 'contacttelephone' => $clientFetch['phonenumber'], 'contactfax' => '', 'contactsepaincassostatus' => 'FRST', 'contactinvoicetemplateid' => '', 'contactstatus' => 1))); if (!empty($clientFetch['companyname'])) { $api->setParams(array('contact' => array('contactname1' => $clientFetch['companyname'], 'contactperson' => ucfirst($clientFetch['firstname']) . ' ' . $clientFetch['lastname']))); } $api->execute(); $response = $api->getResponse(); if ($api->hasErrors()) { $errors = ''; foreach ($api->getErrors() as $error) { $errors = $error['code'] . ' - ' . $error['message'] . ', '; } logActivity('Acumulus API error(s): ' . substr($errors, 0, -2)); return false; } else { mysql_query('UPDATE tblclients SET acumulusid = ' . $response['contact']['contactid'] . ' WHERE id = ' . $clientid . ' LIMIT 1'); return $response['contact']['contactid']; } }
function acumulus_add_invoice($vars) { $vars = array_merge($vars, getAddonVars()); $dataQuery = mysql_query('SELECT tblclients.*, tblinvoices.*, tblpaymentgateways.value AS acumulusBank FROM tblinvoices LEFT JOIN tblclients ON tblclients.id = tblinvoices.userid LEFT JOIN tblpaymentgateways ON tblpaymentgateways.gateway = tblinvoices.paymentmethod AND tblpaymentgateways.setting = "acumulusAccount" WHERE tblinvoices.id = ' . $vars['invoiceid']); if (mysql_num_rows($dataQuery) != 1) { logActivity('Acumulus Add Invoice: received ' . mysql_num_rows($dataQuery) . ' invoices for ID ' . $vars['invoiceid']); return; } $dataFetch = mysql_fetch_assoc($dataQuery); if ($dataFetch['total'] == '0.00' || $dataFetch['total'] == 0) { //Invoice fully paid using credit or no items return; } $customerId = updateCustomer($vars, $dataFetch['userid']); $api = new api($vars['code'], $vars['username'], $vars['password']); $api->setCategory('invoices')->setAction('invoice_add'); if (isset($vars['debug']) && $vars['debug'] == 'on') { $api->enableDebug($vars['debug_email']); } $taxRate = round($dataFetch['taxrate']); if ($dataFetch['country'] == 'NL') { // All Dutch customers needs to get vatType 1 and the defined taxRate $vatType = 1; } else { if (inEurope($dataFetch['country'])) { //If in Europe, then if (!empty($dataFetch['companyname'])) { // Check if customer IS a company $vatType = 3; $taxRate = -1; } else { $vatType = 6; } } else { // If not in Europe, then defaults to outside EU $vatType = 4; $taxRate = -1; } } $api->setParams(array('customer' => array('contactid' => $customerId, 'countrycode' => $dataFetch['country'], 'invoice' => array('concept' => 0, 'number' => empty($dataFetch['invoicenum']) ? $dataFetch['id'] : $dataFetch['invoicenum'], 'vattype' => $vatType, 'issuedate' => $dataFetch['date'], 'paymentstatus' => 2, 'paymentdate' => @date('Y-m-d', @strtotime($dataFetch['datepaid'])), 'accountnumber' => $dataFetch['acumulusBank'], 'costcenter' => $vars['cost_center'], 'template' => $vars['invoice_template'])))); $btwCheckQuery = mysql_query('SELECT SUM(amount) as amount FROM tblinvoiceitems WHERE invoiceid = ' . $dataFetch['id']); $btwCheckFetch = mysql_fetch_assoc($btwCheckQuery); $removeVat = false; if ($btwCheckFetch['amount'] != $dataFetch['subtotal']) { $removeVat = true; } $invoiceLinesQuery = mysql_query('SELECT * FROM tblinvoiceitems WHERE invoiceid = ' . $dataFetch['id']); while ($invoiceLinesFetch = mysql_fetch_assoc($invoiceLinesQuery)) { $api->setParams(array('customer' => array('invoice' => array('line_' . $invoiceLinesFetch['id'] => array('product' => $invoiceLinesFetch['description'], 'unitprice' => $removeVat ? $invoiceLinesFetch['amount'] / 1.21 : $invoiceLinesFetch['amount'], 'vatrate' => $taxRate, 'quantity' => 1))))); } if ($dataFetch['credit'] != '0.00') { $vatrate = 0; if ($taxRate != -1) { $vatrate = $taxRate; } $price = $dataFetch['credit'] / 1.21; $api->setParams(array('customer' => array('invoice' => array('line_credit' => array('product' => 'Betaald d.m.v. credit (€ ' . $dataFetch['credit'] . ')', 'unitprice' => '-' . $price, 'vatrate' => $vatrate, 'quantity' => 1))))); } $api->execute(); $response = $api->getResponse(); if ($api->hasErrors()) { $errors = ''; foreach ($api->getErrors() as $error) { $errors = $error['code'] . ' - ' . $error['message'] . ', '; } logActivity('Acumulus API error(s): ' . substr($errors, 0, -2)); } else { mysql_query('UPDATE tblinvoices SET acumulusid = ' . $response['invoice']['entryid'] . ' WHERE id = ' . $vars['invoiceid'] . ' LIMIT 1'); } }