Commit fcc5a77b authored by Monish Deb's avatar Monish Deb

Add APIs to create GL entries

parent fac18494
......@@ -36,17 +36,6 @@
*/
class CRM_Financial_BAO_ExportFormat_SyncIntacctGL extends CRM_Financial_BAO_ExportFormat {
/**
* For this phase, we always output these records too so that there isn't data
* referenced in the journal entries that isn't defined anywhere.
*
* Possibly in the future this could be selected by the user.
*/
public static $complementaryTables = array(
'ACCNT',
'CUST',
);
/**
* Class constructor.
*/
......@@ -70,9 +59,9 @@ class CRM_Financial_BAO_ExportFormat_SyncIntacctGL extends CRM_Financial_BAO_Exp
civicrm_api3('Batch', 'create', [
'id' => $batchID,
'data' => 'Not Synchronized',
])
]);
return $batchID,
return $batchID;
}
/**
......@@ -82,24 +71,8 @@ class CRM_Financial_BAO_ExportFormat_SyncIntacctGL extends CRM_Financial_BAO_Exp
*/
public function makeExport($export) {
foreach ($export as $batchID) {
CRM_Core_DAO::execute("INSERT IGNORE INTO civicrm_intacct_batches(`batch_id`, `mode`) VALUES ($batchID, 'GL')");
CRM_Core_DAO::executeQuery("INSERT IGNORE INTO civicrm_intacct_batches(`batch_id`, `mode`) VALUES ($batchID, 'GL')");
}
}
/**
* @return string
*/
public function getFileExtension() {
return 'csv';
}
public function exportACCNT() {
}
public function exportCUST() {
}
public function exportTRANS() {
}
}
......@@ -5,6 +5,11 @@ require __DIR__ . '/../../vendor/autoload.php';
use Intacct\ClientConfig;
use Intacct\OnlineClient;
use Intacct\Functions\Common\ReadByQuery;
use Intacct\Functions\AccountsPayable\VendorCreate;
use Intacct\Functions\Common\Query\QueryString;
use Intacct\Functions\Traits\CustomFieldsTrait;
use Intacct\Functions\GeneralLedger\JournalEntryCreate;
use Intacct\Functions\GeneralLedger\JournalEntryLineCreate;
/**
* Class to send Moodle API request
......@@ -56,11 +61,44 @@ class CRM_Syncintacct_API {
return self::$_singleton;
}
public function createVendors($displayName) {
$vendorCreate = new VendorCreate();
$vendorCreate->setVendorName($displayName);
return $this->sendRequest($vendorCreate);
}
/**
* Function to fetch vendors
*/
public function getVendors($searchParams = ['RECORDNO', 'VENDORID', 'NAME']) {
return $this->sendRequest('VENDOR', $searchParams);
public function getVendors($displayNames, $searchParams = ['RECORDNO', 'VENDORID', 'NAME']) {
$queryString = new QueryString(sprintf("NAME IN ('%s')", implode("', '", $displayNames)))
$query = new ReadByQuery();
$query->setObjectName('VENDOR');
$query->setQuery($queryString);
$query->setFields($searchParams);
return $this->sendRequest($query);
}
public function createGLEntry($entry) {
$journalLineEntry = new JournalEntryLineCreate();
$journalLineEntry->setGlAccountNumber($entry['ACCOUNTNO']);
$journalLineEntry->setVendorId($entry['VENDORID']);
$journalLineEntry->setTransactionCurrency($entry['CURRENCY']);
$journalLineEntry->setTransactionAmount($entry['AMOUNT']);
$journalLineEntry->setMemo($entry['description']);
$journalLineEntry->setCustomAllocationSplits(CustomFieldsTrait($entry['customfields']));
return $journalEntry;
}
public function createGLBatch($GLBatch) {
$journalEntry = new JournalEntryCreate();
$journalEntry->setJournalSymbol($GLBatch['JOURNAL']);
$journalEntry->setPostingDate($GLBatch['BATCH_DATE']);
$journalEntry->setDescription($GLBatch['BATCH_TITLE']);
$journalEntry->setLines($GLBatch['ENTRIES']);
return $this->sendRequest($journalEntry);
}
/**
......@@ -71,12 +109,7 @@ class CRM_Syncintacct_API {
*
* @return array
*/
public function sendRequest($entity, $searchParams) {
$query = new ReadByQuery();
$query->setObjectName($entity);
$query->setPageSize(1);
$query->setFields($searchParams);
public function sendRequest($query) {
return $this->_client->execute($query)->getResult();
}
......
......@@ -6,22 +6,6 @@
class CRM_Syncintacct_Util {
/**
* IF the given array of batch IDs consist of any transactions related to grant payment
*/
public static function batchesByEntityTable($batchIDs, $entityTable) {
$sql = "SELECT COUNT(eb.batch_id)
FROM civicrm_entity_batch eb
INNER JOIN civicrm_financial_trxn tx ON tx.id = eb.entity_id AND eb.entity_table = 'civicrm_financial_trxn'
INNER JOIN civicrm_entity_financial_trxn eft ON eft.financial_trxn_id = tx.id AND eft.entity_table = '{$entityTable}'
INNER JOIN civicrm_batch b ON b.id = eb.batch_id
WHERE eb.batch_id IN (" . implode(',', $batchIDs) . ")
GROUP BY eb.batch_id";
$dao = CRM_Core_DAO::executeQuery($sql);
return $dao->N;
}
public static function fetchTransactionrecords($batchID, $entityType) {
$sql = "SELECT
ft.id as financial_trxn_id,
......@@ -36,6 +20,7 @@ class CRM_Syncintacct_Util {
c.source AS source,
c.id AS contribution_id,
c.contact_id AS contact_id,
cc.display_name,
eb.batch_id AS batch_id,
ft.currency AS currency,
cov_status.label AS status,
......@@ -50,7 +35,8 @@ class CRM_Syncintacct_Util {
fac.account_type_code AS from_credit_account_type_code,
fac.accounting_code AS from_credit_account,
fac.name AS from_credit_account_name,
fi.description AS item_description
fi.description AS item_description,
fi.id AS financial_item_id
FROM civicrm_entity_batch eb
LEFT JOIN civicrm_financial_trxn ft ON (eb.entity_id = ft.id AND eb.entity_table = 'civicrm_financial_trxn')
LEFT JOIN civicrm_financial_account fa_to ON fa_to.id = ft.to_financial_account_id
......@@ -59,6 +45,7 @@ class CRM_Syncintacct_Util {
LEFT JOIN civicrm_option_value cov ON (cov.value = ft.payment_instrument_id AND cov.option_group_id = cog.id)
LEFT JOIN civicrm_entity_financial_trxn eftc ON (eftc.financial_trxn_id = ft.id AND eftc.entity_table = '{$entityType}')
LEFT JOIN civicrm_contribution c ON c.id = eftc.entity_id
LEFT JOIN civicrm_contact cc ON cc.id = c.contact_id
LEFT JOIN civicrm_option_group cog_status ON cog_status.name = 'contribution_status'
LEFT JOIN civicrm_option_value cov_status ON (cov_status.value = ft.status_id AND cov_status.option_group_id = cog_status.id)
LEFT JOIN civicrm_entity_financial_trxn efti ON (efti.financial_trxn_id = ft.id AND efti.entity_table = 'civicrm_financial_item')
......@@ -68,9 +55,57 @@ class CRM_Syncintacct_Util {
WHERE eb.batch_id = ( %1 )";
$params = array(1 => array(, 'Integer'));
$params = array(1 => array($batchID, 'Integer'));
$dao = CRM_Core_DAO::executeQuery($sql, $params);
$batch = civicrm_api3('Batch', 'getsingle', ['id' => $batchID]);
$GLBatch = [
'JOURNAL' => 'CIVIBATCH' . $batchID,
'BATCH_DATE' => new DateTime($batch['created_date']),
'BATCH_TITLE' => $batch['title'],
'ENTRIES' => [],
];
while ($dao->fetch()) {
$GLBatch['ENTRIES'][] = [
'ACCOUNTNO' => $dao->to_account_code,
'VENDORID' => $dao->display_name,
'CURRENCY' => $dao->currency,
'AMOUNT' => $dao->debit_total_amount,
'DESCRIPTION' => $dao->item_description,
'customfields' => [
'batch_id' => $batchID,
'financial_trxn_id' => $dao->financial_trxn_id,
'financial_item_id' => $dao->financial_item_id,
]
];
}
return $GLBatch;
}
public static function createGLEntries($batchEntries) {
$displayNames = array_flip(CRM_Utils_Array::collect('VENDORID', $batchEntries['ENTRIES']));
$fetchVendors = CRM_Syncintacct_API::singleton()
->getVendors($displayNames)
->getData();
foreach ($fetchVendors as $vendor) {
$displayNames[$vendor->NAME] = $vendor->VENDORID;
}
foreach ($batchEntries['ENTRIES'] as $key => $entry) {
$vendorID = CRM_Utils_Array::value($entry['VENDORID'], $displayNames);
if (is_string($vendorID)) {
$batchEntries['ENTRIES'][$key]['VENDORID'] = $vendorID;
}
else {
$batchEntries['ENTRIES'][$key]['VENDORID'] = CRM_Syncintacct_API::singleton()->createVendors($entry['VENDORID'])->getData()[0]->VENDORID;
}
$batchEntries['ENTRIES'][$key] = CRM_Syncintacct_API::singleton()->createGLEntry($entry);
}
$response = CRM_Syncintacct_API::singleton()->createGLBatch($batchEntries);
}
}
......@@ -14,7 +14,8 @@ function civicrm_api3_job_processbatchsynctointacct($params) {
$dao = CRM_Core_DAO::executeQuery('SELECT * FROM civicrm_intacct_batches ORDER BY id ASC');
while($dao->fetch()) {
$entityTable = ($dao->mode == 'GL') ? 'civicrm_contribution' : 'civicrm_grant';
$transactionDAO = CRM_Syncintacct_Util::fetchTransactionrecords($dao->batch_id, $entityTable);
$batchEntries = CRM_Syncintacct_Util::fetchTransactionrecords($dao->batch_id, $entityTable);
CRM_Syncintacct_Util::createGLEntries($batchEntries);
}
}
......@@ -3,5 +3,6 @@ CREATE TABLE `civicrm_intacct_batches` (
`id` INT NOT NULL AUTO_INCREMENT ,
`batch_id` INT NOT NULL ,
`mode` CHAR(2) NOT NULL ,
PRIMARY KEY (`id`)
PRIMARY KEY (`id`),
CONSTRAINT UI_batch_id_mode UNIQUE (`batch_id`, `mode`)
) ENGINE = InnoDB;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment