Commit 3386e9ce authored by jaapjansma's avatar jaapjansma
Browse files

Update issue 5909

parent c0f39c1a
......@@ -100,16 +100,32 @@ class CRM_Banking_PluginImpl_Matcher_BkfNewContact extends CRM_Banking_PluginMod
* @return html code snippet
*/
function visualize_match( CRM_Banking_Matcher_Suggestion $match, $btx) {
$location_types = civicrm_api3('Address', 'getoptions', ['field' => 'location_type_id', 'api_action' => 'create']);
$contact_types = civicrm_api3('Contact', 'getoptions', ['field' => 'contact_type', 'api_action' => 'create']);
$skipContributionFields = $this->_plugin_config->skip_fields;
$smarty_vars['data'] = $btx->getDataParsed();
$data_parsed = $btx->getDataParsed();
$bankKontoCustomFieldId = null;
$defaultCampiagnId = null;
try {
$bankKontoCustomFieldId = civicrm_api3('CustomField', 'getvalue', ['name' => 'bankkonto', 'custom_group_id' => 'contribution_bankkonto', 'return' => 'id']);
$defaultCampiagnId = civicrm_api3('Campaign', 'getvalue', ['name' => 'default_campaign', 'return' => 'id']);
} catch (\Exception $e) {
// Do nothing
}
$fields = [
'contact.contact_type' => ['label' => E::ts('Contact type'), 'required' => true, 'options' => $contact_types['values']],
'contact.first_name' => ['label' => E::ts('First name'), 'required' => true, 'size' => 64],
'contact.last_name' => ['label' => E::ts('Last name'), 'required' => true, 'size' => 64],
'contact.street_address' => ['label' => E::ts('Street address'), 'required' => false, 'size' => 64],
'contact.postal_code' => ['label' => E::ts('Postal code'), 'required' => false, 'size' => 64],
'contact.city' => ['label' => E::ts('City'), 'required' => false, 'size' => 64],
'contact.household_name' => ['label' => E::ts('Household name'), 'required' => true, 'size' => 64],
'contact.organization_name' => ['label' => E::ts('Organization name'), 'required' => true, 'size' => 64],
'address.location_type_id' => ['label' => E::ts('Location type'), 'required' => true, 'options' => $location_types['values']],
'address.supplemental_address_1' => ['label' => E::ts('Supplemental address'), 'required' => 'false', 'size' => 64],
'address.street_address' => ['label' => E::ts('Street address'), 'required' => false, 'size' => 64],
'address.postal_code' => ['label' => E::ts('Postal code'), 'required' => false, 'size' => 64],
'address.city' => ['label' => E::ts('City'), 'required' => false, 'size' => 64],
];
$contributionFieldsApi = civicrm_api3('Contribution', 'getfields', ['api_action' => 'create']);
......@@ -134,12 +150,19 @@ class CRM_Banking_PluginImpl_Matcher_BkfNewContact extends CRM_Banking_PluginMod
$contact_params = $match->getParameter('contact_params');
if (!is_array($contact_params)) {
$contact_params = [
$contact_params['contact_type'] = 'Individual';
}
$address_params = $match->getParameter('address_params');
if (!is_array($address_params)) {
$address_params = [
'street_address' => isset($data_parsed['street_address']) ? $data_parsed['street_address'] : null,
'postal_code' => isset($data_parsed['postal_code']) ? $data_parsed['postal_code'] : null,
'city' => isset($data_parsed['city']) ? $data_parsed['city'] : null,
];
}
if (!isset($address_params['location_type_id'])) {
$address_params['location_type_id'] = civicrm_api3('LocationType', 'getvalue', ['is_default' => 1, 'return' => 'id']);
}
$contribution_params = $match->getParameter('contribution_params');
if (!is_array($contribution_params)) {
$contribution_params = [
......@@ -147,11 +170,20 @@ class CRM_Banking_PluginImpl_Matcher_BkfNewContact extends CRM_Banking_PluginMod
'financial_type_id' => 1, // Gave
'note' => isset($data_parsed['purpose']) ? $data_parsed['purpose'] : null,
];
if ($bankKontoCustomFieldId && isset($data_parsed['_NBAN_NO'])) {
$contribution_params['custom_'.$bankKontoCustomFieldId] = $data_parsed['_NBAN_NO'];
}
if ($defaultCampiagnId) {
$contribution_params['campaign_id'] = $defaultCampiagnId;
}
}
$values = [];
foreach($contact_params as $k => $v) {
$values["contact.".$k] = $v;
}
foreach($address_params as $k => $v) {
$values["address.".$k] = $v;
}
foreach($contribution_params as $k => $v) {
$values["contribution.".$k] = $v;
}
......@@ -175,14 +207,18 @@ class CRM_Banking_PluginImpl_Matcher_BkfNewContact extends CRM_Banking_PluginMod
public function update_parameters(CRM_Banking_Matcher_Suggestion $match, $parameters) {
$contact_params = [];
$contribution_params = [];
$address_params = [];
foreach($parameters as $key => $value) {
if (stripos($key, "contact_") === 0) {
$contact_params[substr($key, 8)] = $value;
} elseif (stripos($key, "address_") === 0) {
$address_params[substr($key, 8)] = $value;
} elseif (stripos($key, "contribution_") === 0) {
$contribution_params[substr($key, 13)] = $value;
}
}
$match->setParameter('contact_params', $contact_params);
$match->setParameter('address_params', $address_params);
$match->setParameter('contribution_params', $contribution_params);
}
......@@ -201,9 +237,16 @@ class CRM_Banking_PluginImpl_Matcher_BkfNewContact extends CRM_Banking_PluginMod
public function execute( $match, $btx ) {
$skipContributionFields = $this->_plugin_config->skip_fields;
$contact_params = $match->getParameter('contact_params');
$address_params = $match->getParameter('address_params');
$contribution_params = $match->getParameter('contribution_params');
if (empty($contact_params['first_name']) || empty($contact_params['last_name'])) {
if ($contact_params['contact_type'] == 'Individual' && (empty($contact_params['first_name']) || empty($contact_params['last_name']))) {
return FALSE;
}
if ($contact_params['contact_type'] == 'Household' && empty($contact_params['household_name'])) {
return FALSE;
}
if ($contact_params['contact_type'] == 'Organization' && empty($contact_params['organization_name'])) {
return FALSE;
}
$contributionFieldsApi = civicrm_api3('Contribution', 'getfields', ['api_action' => 'create']);
......@@ -213,10 +256,14 @@ class CRM_Banking_PluginImpl_Matcher_BkfNewContact extends CRM_Banking_PluginMod
}
}
$contact_params['contact_type'] = 'Individual';
$result = civicrm_api3('Contact', 'create', $contact_params);
$contact_id = $result['id'];
if (count($address_params) > 1) {
$address_params['contact_id'] = $contact_id;
civicrm_api3('Address', 'create', $address_params);
}
$contribution_params['contact_id'] = $contact_id;
$contribution_params['total_amount'] = $btx->amount;
$contribution_params['receive_date'] = $btx->value_date;
......@@ -233,8 +280,9 @@ class CRM_Banking_PluginImpl_Matcher_BkfNewContact extends CRM_Banking_PluginMod
// wrap it up
$newStatus = banking_helper_optionvalueid_by_groupname_and_name('civicrm_banking.bank_tx_status', 'Processed');
$btx->setStatus($newStatus);
parent::execute($match, $btx);
//$btx->setStatus($newStatus);
//parent::execute($match, $btx);
return FALSE;
return TRUE;
}
......
......@@ -52,6 +52,31 @@
{/if}
{/foreach}
{literal}
$('[name="contact.contact_type"]').on('change', function(){
var val = $(this).val();
if (val =='Individual') {
$('[name="contact.first_name"]').parent().parent().show();
$('[name="contact.last_name"]').parent().parent().show();
$('[name="contact.household_name"]').parent().parent().hide();
$('[name="contact.organization_name"]').parent().parent().hide();
} else if (val =='Household') {
$('[name="contact.first_name"]').parent().parent().hide();
$('[name="contact.last_name"]').parent().parent().hide();
$('[name="contact.organization_name"]').parent().parent().hide();
$('[name="contact.household_name"]').parent().parent().show();
} else if (val =='Organization') {
$('[name="contact.first_name"]').parent().parent().hide();
$('[name="contact.last_name"]').parent().parent().hide();
$('[name="contact.household_name"]').parent().parent().hide();
$('[name="contact.organization_name"]').parent().parent().show();
}
});
$('[name="contact.contact_type"]').trigger('change');
});
</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