-
-
Notifications
You must be signed in to change notification settings - Fork 9
Add support to account id, payment type etc. #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,6 +97,12 @@ public function push($params = [], $limit = PHP_INT_MAX) { | |
} | ||
|
||
if ($accountsInvoice->Id) { | ||
// Get invoice SyncToken to avoid Stale object error: | ||
// You and XXXX were working on this at the same time. XXX | ||
// finished before you did, so your work was not saved. | ||
$invoiceExiting = $this->getInvoiceFromQBO($record); | ||
$accountsInvoice->SyncToken = $invoiceExiting->SyncToken; | ||
|
||
$result = $dataService->Update($accountsInvoice); | ||
|
||
if ($last_error = $dataService->getLastError()) { | ||
|
@@ -224,27 +230,51 @@ public static function pushPayments($contribution_id, $account_invoice) { | |
|
||
$dataService = CRM_Quickbooks_APIHelper::getAccountingDataServiceObject(); | ||
$result = []; | ||
|
||
$quickbooksAccountId = civicrm_api3('Setting', 'getvalue', [ | ||
'name' => "quickbooks_account_id", | ||
'group' => 'civiquickbooks', | ||
]); | ||
$paymentInstrument = CRM_Contribute_BAO_Contribution::buildOptions('payment_instrument_id', 'create'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think buildOptions is deprecated, this could be an API call? |
||
foreach ($payments['values'] as $payment) { | ||
$txnDate = $payment['trxn_date']; | ||
$total = sprintf('%.5f', $payment['total_amount']); | ||
$QBOPayment = \QuickBooksOnline\API\Facades\Payment::create( | ||
[ | ||
'TotalAmt' => $total, | ||
'CustomerRef' => $account_invoice->CustomerRef, | ||
'CurrencyRef' => $account_invoice->CurrencyRef, | ||
'TxnDate' => $txnDate, | ||
'Line' => [ | ||
'Amount' => $total, | ||
'LinkedTxn' => [ | ||
[ | ||
'TxnType' => 'Invoice', | ||
'TxnId' => $account_invoice->Id, | ||
], | ||
$paymentInput = [ | ||
'TotalAmt' => $total, | ||
'CustomerRef' => $account_invoice->CustomerRef, | ||
'CurrencyRef' => $account_invoice->CurrencyRef, | ||
'TxnDate' => $txnDate, | ||
'Line' => [ | ||
'Amount' => $total, | ||
'LinkedTxn' => [ | ||
[ | ||
'TxnType' => 'Invoice', | ||
'TxnId' => $account_invoice->Id, | ||
], | ||
], | ||
] | ||
); | ||
], | ||
]; | ||
// check payment instrument present on record | ||
if (!empty($payment['payment_instrument_id'])) { | ||
$paymentMethodId = self::getPaymentMethod($paymentInstrument[$payment['payment_instrument_id']]); | ||
if (!empty($paymentMethodId)) { | ||
$paymentInput['PaymentMethodRef'] = ['value' => $paymentMethodId]; | ||
} | ||
} | ||
|
||
// set Account ID | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My understanding is that on the Quickbooks side, each product or service should have an Income account set, and we should defer where the payments go to this. Is there a particular reason for having a setting for one particular income account that overrides this behaviour? It doesn't sound like the correct approach |
||
if ($quickbooksAccountId) { | ||
$paymentInput['DepositToAccountRef'] = ['value' => $quickbooksAccountId]; | ||
} | ||
|
||
// set Transaction ID | ||
if (!empty($payment['trxn_id'])) { | ||
$paymentInput['PaymentRefNum'] = $payment['trxn_id']; | ||
} | ||
else if (!empty($payment['check_number'])) { | ||
$paymentInput['PaymentRefNum'] = $payment['check_number']; | ||
} | ||
|
||
$QBOPayment = \QuickBooksOnline\API\Facades\Payment::create($paymentInput); | ||
$result[] = $dataService->Add($QBOPayment); | ||
} | ||
|
||
|
@@ -628,7 +658,10 @@ protected function mapToAccounts($db_contribution, $accountsID, $SyncToken, $qb_ | |
} | ||
|
||
$lineTotal = $line_item['line_total']; | ||
|
||
// Do not sync line items with zero quantity. | ||
if (empty($line_item['qty'])) { | ||
continue; | ||
} | ||
$tmp = [ | ||
'Id' => $i . '', | ||
'LineNum' => $i, | ||
|
@@ -641,6 +674,9 @@ protected function mapToAccounts($db_contribution, $accountsID, $SyncToken, $qb_ | |
], | ||
'UnitPrice' => $lineTotal / $line_item['qty'] * 1.00, | ||
'Qty' => $line_item['qty'] * 1, | ||
'TaxCodeRef' => [ | ||
'value' => $line_item_tax_ref, | ||
], | ||
], | ||
]; | ||
|
||
|
@@ -853,6 +889,28 @@ public static function getQBOTaxCode($name) { | |
return $codes[$name]; | ||
} | ||
|
||
/** | ||
* get Payment Method for syncing | ||
* @param $name | ||
*/ | ||
public static function getPaymentMethod($name) { | ||
$name = strtolower($name); | ||
$paymentMethods =& \Civi::$statics[__CLASS__][__FUNCTION__]; | ||
if (!isset($paymentMethods[$name])) { | ||
$query = 'SELECT * From PaymentMethod'; | ||
$dataService = CRM_Quickbooks_APIHelper::getAccountingDataServiceObject(); | ||
$result = $dataService->Query($query); | ||
if (empty($result)) { | ||
return; | ||
} | ||
foreach ($result as $paymentMethodObject) { | ||
$paymentMethods[strtolower($paymentMethodObject->Name)] = $paymentMethodObject->Id; | ||
} | ||
} | ||
|
||
return $paymentMethods[$name]; | ||
} | ||
|
||
/** | ||
* Map fields for a cancelled contribution to be updated to QuickBooks. | ||
* | ||
|
@@ -945,7 +1003,9 @@ protected function generateTaxDetails($line_items) { | |
* @throws \CiviCRM_API3_Exception | ||
*/ | ||
protected function findPushContributions($params, $limit) { | ||
// quickbook does not accept negative amount. | ||
$accountInvoices = AccountInvoice::get() | ||
->addJoin('Contribution AS contribution', 'INNER', ['contribution.total_amount', '>=', 0]) | ||
->addWhere('plugin', '=', $this->plugin) | ||
->addWhere('connector_id', '=', 0) | ||
->addWhere('accounts_status_id:name', 'NOT IN', ['completed']) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have always been a little apprehensive about doing this in the push function, however on balance of things I guess if an important change had been made, it would have been pulled in during the InvoicePull call already anyway.
Approved