Commit 4f6ffb20 authored by Monish Deb's avatar Monish Deb

AGBU-37: Create 4 new separate fields on Financial Account

parent 6a9ff830
......@@ -74,7 +74,7 @@ class CRM_Financial_BAO_ExportFormat_SyncIntacctAP extends CRM_Financial_BAO_Exp
foreach ($export as $batchID) {
$this->_batchIds = $batchID;
CRM_Core_DAO::executeQuery("INSERT IGNORE INTO civicrm_intacct_batches(`batch_id`, `mode`) VALUES ($batchID, 'AP')");
$batchEntries[$batchID] = CRM_Syncintacct_Util::fetchTransactionrecords($batchID, 'civicrm_contribution');
$batchEntries[$batchID] = CRM_Syncintacct_Util::fetchEntries($batchID, 'AP');
}
// Save the file in the public directory.
......
......@@ -74,7 +74,7 @@ class CRM_Financial_BAO_ExportFormat_SyncIntacctGL extends CRM_Financial_BAO_Exp
foreach ($export as $batchID) {
$this->_batchIds = $batchID;
CRM_Core_DAO::executeQuery("INSERT IGNORE INTO civicrm_intacct_batches(`batch_id`, `mode`) VALUES ($batchID, 'GL')");
$batchEntries[$batchID] = CRM_Syncintacct_Util::fetchTransactionrecords($batchID, 'civicrm_contribution');
$batchEntries[$batchID] = CRM_Syncintacct_Util::fetchEntries($batchID, 'GL');
}
// Save the file in the public directory.
......
......@@ -92,7 +92,10 @@ class CRM_Syncintacct_API {
$journalLineEntry->setTransactionCurrency($entry['CURRENCY']);
$journalLineEntry->setTransactionAmount($entry['AMOUNT']);
// @TODO this is a dummy active location id passed
$journalLineEntry->setLocationId('Elim');
$journalLineEntry->setLocationId($entry['LOCATION']);
$journalLineEntry->setDepartmentId($entry['DEPARTMENT']);
$journalLineEntry->setProjectId($entry['PROJECTID']);
$journalLineEntry->setClassId($entry['CLASSID']);
$journalLineEntry->setMemo($entry['DESCRIPTION']);
$customFields = new CustomAllocationSplit($entry['customfields']);
$journalLineEntry->setCustomAllocationSplits($customFields);
......@@ -126,8 +129,10 @@ class CRM_Syncintacct_API {
$billLineEntry = new BillLineCreate();
$billLineEntry->setGlAccountNumber($entry['ACCOUNTNO']);
$billLineEntry->setTransactionAmount($entry['AMOUNT']);
// @TODO this is a dummy active location id passed
$billLineEntry->setLocationId('Elim');
$billLineEntry->setLocationId($entry['LOCATION']);
$billLineEntry->setDepartmentId($entry['DEPARTMENT']);
$billLineEntry->setProjectId($entry['PROJECTID']);
$billLineEntry->setClassId($entry['CLASSID']);
// TODO: BillLineCreate does not support adding custom fields yet
// $customFields = new CustomAllocationSplit($entry['customfields']);
// $billLineEntry->setCustomAllocationSplits($customFields);
......
......@@ -21,7 +21,7 @@ class CRM_Syncintacct_Util {
return $dao->N;
}
public static function createEntries($batchID, $entityType) {
public static function fetchEntries($batchID, $entityType) {
$entityTable = ($entityType == 'GL') ? 'civicrm_contribution' : 'civicrm_grant';
$sql = "SELECT
ft.id as financial_trxn_id,
......@@ -76,13 +76,22 @@ class CRM_Syncintacct_Util {
$dao = CRM_Core_DAO::executeQuery($sql, $params);
if ($entityType == 'AP') {
return self::createAPEntries(self::formatAPBatchParams($dao, $batchID));
return self::formatAPBatchParams($dao, $batchID);
}
else {
return self::createGLEntries(self::formatGLBatchParams($dao, $batchID));
return self::formatGLBatchParams($dao, $batchID);
}
}
public static function getAccountDataByCode($code) {
$result = CRM_Core_DAO::executeQuery("
SELECT ifad.* FROM civicrm_intacct_financial_account_data ifad
INNER JOIN civicrm_financial_account fa ON ifad.financial_account_id = fa.id AND fa.accounting_code = '$code' ")
->fetchAll();
return CRM_Utils_Array::value(0, $result);
}
public static function formatGLBatchParams($dao, $batchID) {
$batch = civicrm_api3('Batch', 'getsingle', ['id' => $batchID]);
$GLBatch = [
......@@ -92,30 +101,41 @@ class CRM_Syncintacct_Util {
'ENTRIES' => [],
];
while ($dao->fetch()) {
$accountCode = $dao->credit_account ?: $dao->from_credit_account;
$values = self::getAccountDataByCode($accountCode);
$GLBatch['ENTRIES'][] = [
'ACCOUNTNO' => $dao->credit_account ?: $dao->from_credit_account,
'ACCOUNTNO' => $accountCode,
'VENDORID' => $dao->display_name,
'CURRENCY' => $dao->currency,
'AMOUNT' => -$dao->debit_total_amount,
'DESCRIPTION' => $dao->item_description,
'CLASSID' => $values['class_id'],
'DEPARTMENT' => $values['dept_id'],
'LOCATION' => $values['location'],
'PROJECTID' => $values['project_id'],
'customfields' => [
'batch_id' => $batchID,
'financial_trxn_id' => $dao->financial_trxn_id,
'financial_item_id' => $dao->financial_item_id,
'url' => CRM_Utils_System::url('civicrm/contact/view/contribution', "reset=1&id={$dao->entity_id}&cid={$dao->contact_id}&action=view"),
'url' => CRM_Utils_System::url('civicrm/contact/view/contribution', "reset=1&id={$dao->entity_id}&cid={$dao->contact_id}&action=view", TRUE),
]
];
$values = self::getAccountDataByCode($dao->to_account_code);
$GLBatch['ENTRIES'][] = [
'ACCOUNTNO' => $dao->to_account_code,
'VENDORID' => $dao->display_name,
'CURRENCY' => $dao->currency,
'AMOUNT' => $dao->debit_total_amount,
'DESCRIPTION' => $dao->item_description,
'CLASSID' => $values['class_id'],
'DEPARTMENT' => $values['dept_id'],
'LOCATION' => $values['location'],
'PROJECTID' => $values['project_id'],
'customfields' => [
'batch_id' => $batchID,
'financial_trxn_id' => $dao->financial_trxn_id,
'financial_item_id' => $dao->financial_item_id,
'url' => CRM_Utils_System::url('civicrm/contact/view/contribution', "reset=1&id={$dao->entity_id}&cid={$dao->contact_id}&action=view"),
'url' => CRM_Utils_System::url('civicrm/contact/view/contribution', "reset=1&id={$dao->entity_id}&cid={$dao->contact_id}&action=view", TRUE),
],
];
}
......@@ -137,29 +157,31 @@ class CRM_Syncintacct_Util {
$APBatch[$dao->entity_id]['ENTRIES'][] = [
'ACCOUNTNO' => $dao->credit_account ?: $dao->from_credit_account,
'AMOUNT' => -$dao->debit_total_amount,
'CLASSID' => $dao->from_class,
'DEPARTMENT' => $dao->from_dept,
'LOCATION' => $dao->from_location,
'PROJECTID' => $dao->from_project,
'customfields' => [
'batch_id' => $batchID,
'financial_trxn_id' => $dao->financial_trxn_id,
'financial_item_id' => $dao->financial_item_id,
'url' => CRM_Utils_System::url('civicrm/contact/view/grant', "reset=1&id={$dao->entity_id}&cid={$dao->contact_id}&action=view"),
'url' => CRM_Utils_System::url('civicrm/contact/view/grant', "reset=1&id={$dao->entity_id}&cid={$dao->contact_id}&action=view", TRUE),
]
];
$APBatch[$dao->entity_id]['ENTRIES'][] = [
'ACCOUNTNO' => $dao->to_account_code,
'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,
'url' => CRM_Utils_System::url('civicrm/contact/view/grant', "reset=1&id={$dao->entity_id}&cid={$dao->contact_id}&action=view"),
],
];
}
return $APBatch;
}
public static function createEntriesByType($batchEntries, $entityType) {
if ($entityType == 'AP') {
return self::createAPEntries($batchEntries);
}
else {
return self::createGLEntries($batchEntries);
}
}
public static function createAPEntries($batchEntries) {
$syncIntacctConfig = CRM_Syncintacct_API::singleton();
$fetchVendors = $syncIntacctConfig->getVendors(array_unique(CRM_Utils_Array::collect('VENDORID', $batchEntries)));
......@@ -187,7 +209,6 @@ class CRM_Syncintacct_Util {
$response['Trxn ID -' . $trxnID] = $syncIntacctConfig->createAPBatch($entry);
}
CRM_Core_Error::debug_var('a', $response);
return $response;
}
......
......@@ -39,7 +39,10 @@ function civicrm_api3_intacct_SyncFinancialAccount($params) {
function civicrm_api3_intacct_processBatchSyncToIntacct($params) {
$dao = CRM_Core_DAO::executeQuery('SELECT * FROM civicrm_intacct_batches ORDER BY id ASC');
while($dao->fetch()) {
$response = CRM_Syncintacct_Util::createEntries($dao->batch_id, $dao->mode);
$response = CRM_Syncintacct_Util::createEntriesByType(
CRM_Syncintacct_Util::fetchEntries($dao->batch_id, $dao->mode),
$dao->mode
);
CRM_Syncintacct_Util::processSyncIntacctResponse($dao->batch_id, $response);
}
......
......@@ -6,3 +6,13 @@ CREATE TABLE `civicrm_intacct_batches` (
PRIMARY KEY (`id`),
CONSTRAINT UI_batch_id_mode UNIQUE (`batch_id`, `mode`)
) ENGINE = InnoDB;
CREATE TABLE `civicrm_intacct_financial_account_data` (
`financial_account_id` int(10) UNSIGNED NOT NULL COMMENT 'Financial Account ID',
`class_id` varchar(20) DEFAULT NULL COMMENT 'Intacct Class ID',
`dept_id` varchar(20) DEFAULT NULL COMMENT 'Intacct Department ID',
`location` varchar(50) DEFAULT NULL COMMENT 'Intacct Location',
`project_id` varchar(20) DEFAULT NULL COMMENT 'Intacct Project ID',
PRIMARY KEY (`financial_account_id`),
CONSTRAINT UI_intacct_fa_unique UNIQUE (`class_id`,`dept_id`,`location`,`project_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
DROP TABLE IF EXISTS `civicrm_intacct_batches`;
DROP TABLE IF EXISTS `civicrm_intacct_financial_account_data`;
......@@ -93,8 +93,42 @@ function syncintacct_civicrm_buildForm($formName, &$form) {
);
}
}
if ('CRM_Financial_Form_FinancialAccount' == $formName && $form->get('action') & CRM_Core_Action::UPDATE) {
$form->add('text', 'class_id', ts('Class'), [], TRUE);
$form->add('text', 'dept_id', ts('Department'), [], TRUE);
$form->add('text', 'location', ts('Location'), [], TRUE);
$form->add('text', 'project_id', ts('Project'), [], TRUE);
CRM_Core_Region::instance('page-body')->add(array(
'template' => "CRM/Syncintacct/FinancialAccountExtra.tpl",
));
$defaults = CRM_Utils_Array::value(0, CRM_Core_DAO::executeQuery(
'SELECT * FROM civicrm_intacct_financial_account_data WHERE financial_account_id = ' . $form->get('id'))
->fetchAll(), []);
$form->setDefaults($defaults);
}
}
function syncintacct_civicrm_post($op, $objectName, $objectId, &$objectRef) {
if ($objectName == 'FinancialAccount' && $op == 'del') {
CRM_Core_DAO::executeQuery("DLEETE FROM civicrm_intacct_financial_account_data WHERE financial_account_id = " . $objectId);
}
}
function syncintacct_civicrm_postProcess($formName, &$form) {
if ('CRM_Financial_Form_FinancialAccount' == $formName && $form->get('action') & CRM_Core_Action::UPDATE) {
$submitValues = $form->exportValues();
$values = [];
foreach (['class_id', 'dept_id', 'location', 'project_id'] as $column) {
if (!empty($submitValues[$column])) {
$values[$column] = $submitValues[$column];
}
}
$values['financial_account_id'] = $form->get('id');
CRM_Core_DAO::executeQuery(sprintf(' REPLACE INTO civicrm_intacct_financial_account_data(%s) VALUES (\'%s\')', implode(",", array_keys($values)), implode("','", $values)));
}
}
function syncintacct_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
if ($formName == 'CRM_Financial_Form_Export') {
$exporters = [
......
<table>
<tr id="crm-intacct-class">
<td class="label">
{$form.class_id.label}
</td>
<td class="html-adjust">
{$form.class_id.html}<br/>
<span class="description">{ts}Enter the Intacct Class ID, later used for creating GL/AP entries.{/ts}</span>
</td>
</tr>
<tr id="crm-intacct-dept">
<td class="label">
{$form.dept_id.label}
</td>
<td class="html-adjust">
{$form.dept_id.html}<br/>
<span class="description">{ts}Enter the Intacct Department ID, later used for creating GL/AP entries.{/ts}</span>
</td>
</tr>
<tr id="crm-intacct-location">
<td class="label">
{$form.location.label}
</td>
<td class="html-adjust">
{$form.location.html}<br/>
<span class="description">{ts}Enter the Intacct Location, later used for creating GL/AP entries.{/ts}</span>
</td>
</tr>
<tr id="crm-intacct-project">
<td class="label">
{$form.project_id.label}
</td>
<td class="html-adjust">
{$form.project_id.html}<br/>
<span class="description">{ts}Enter the Intacct Project, later used for creating GL/AP entries.{/ts}</span>
</td>
</tr>
</table>
{literal}
<script type="text/javascript">
CRM.$(function($) {
$('.crm-contribution-form-block-accounting_code').after($('#crm-intacct-class'));
$('#crm-intacct-class').after($('#crm-intacct-dept'));
$('#crm-intacct-dept').after($('#crm-intacct-location'));
$('#crm-intacct-location').after($('#crm-intacct-project'));
});
</script>
{/literal}
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