First commit

This commit is contained in:
Theodotos Andreou 2018-01-14 13:10:16 +00:00
commit c6e2478c40
13918 changed files with 2303184 additions and 0 deletions

View file

@ -0,0 +1,219 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* This class generates form components for Financial Type
*/
class CRM_Financial_Form_BatchTransaction extends CRM_Contribute_Form {
static $_links = NULL;
static $_entityID;
/**
* Batch status.
* @var
*/
protected $_batchStatusId;
/**
* Batch status name.
* @string
*/
protected $_batchStatus;
public function preProcess() {
// This reuses some styles from search forms
CRM_Core_Resources::singleton()->addStyleFile('civicrm', 'css/searchForm.css', 1, 'html-header');
self::$_entityID = CRM_Utils_Request::retrieve('bid', 'Positive') ? CRM_Utils_Request::retrieve('bid', 'Positive') : CRM_Utils_Array::value('batch_id', $_POST);
$this->assign('entityID', self::$_entityID);
if (isset(self::$_entityID)) {
$this->_batchStatusId = CRM_Core_DAO::getFieldValue('CRM_Batch_BAO_Batch', self::$_entityID, 'status_id');
$batchStatuses = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id', array('labelColumn' => 'name', 'condition' => " v.value={$this->_batchStatusId}"));
$this->_batchStatus = $batchStatuses[$this->_batchStatusId];
$this->assign('statusID', $this->_batchStatusId);
$this->assign('batchStatus', $this->_batchStatus);
$validStatus = FALSE;
if (in_array($this->_batchStatus, array('Open', 'Reopened'))) {
$validStatus = TRUE;
}
$this->assign('validStatus', $validStatus);
$this->_values = civicrm_api3('Batch', 'getSingle', array('id' => self::$_entityID));
$batchTitle = CRM_Core_DAO::getFieldValue('CRM_Batch_BAO_Batch', self::$_entityID, 'title');
CRM_Utils_System::setTitle(ts('Accounting Batch - %1', array(1 => $batchTitle)));
$columnHeaders = array(
'created_by' => ts('Created By'),
'status' => ts('Status'),
'description' => ts('Description'),
'payment_instrument' => ts('Payment Method'),
'item_count' => ts('Expected Number of Items'),
'assigned_item_count' => ts('Actual Number of Items'),
'total' => ts('Expected Total Amount'),
'assigned_total' => ts('Actual Total Amount'),
'opened_date' => ts('Opened'),
);
$this->assign('columnHeaders', $columnHeaders);
}
}
/**
* Build the form object.
*/
public function buildQuickForm() {
if ($this->_batchStatus == 'Closed') {
$this->add('submit', 'export_batch', ts('Export Batch'));
}
// do not build rest of form unless it is open/reopened batch
if (!in_array($this->_batchStatus, array('Open', 'Reopened'))) {
return;
}
parent::buildQuickForm();
if (CRM_Batch_BAO_Batch::checkBatchPermission('close', $this->_values['created_id'])) {
$this->add('submit', 'close_batch', ts('Close Batch'));
if (CRM_Batch_BAO_Batch::checkBatchPermission('export', $this->_values['created_id'])) {
$this->add('submit', 'export_batch', ts('Close & Export Batch'));
}
}
// text for sort_name
$this->addElement('text',
'sort_name',
ts('Contributor Name or Email'),
CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact',
'sort_name'
)
);
$this->_group = CRM_Core_PseudoConstant::nestedGroup();
// multiselect for groups
if ($this->_group) {
$this->add('select', 'group', ts('Groups'), $this->_group, FALSE,
array('id' => 'group', 'multiple' => 'multiple', 'class' => 'crm-select2')
);
}
$contactTags = CRM_Core_BAO_Tag::getTags();
if ($contactTags) {
$this->add('select', 'contact_tags', ts('Tags'), $contactTags, FALSE,
array('id' => 'contact_tags', 'multiple' => 'multiple', 'class' => 'crm-select2')
);
}
CRM_Contribute_BAO_Query::buildSearchForm($this);
$this->addElement('checkbox', 'toggleSelects', NULL, NULL);
$this->add('select',
'trans_remove',
ts('Task'),
array('' => ts('- actions -')) + array('Remove' => ts('Remove from Batch')));
$this->add('submit', 'rSubmit', ts('Go'),
array(
'class' => 'crm-form-submit',
'id' => 'GoRemove',
));
self::$_entityID = CRM_Utils_Request::retrieve('bid', 'Positive');
$this->addButtons(
array(
array(
'type' => 'submit',
'name' => ts('Search'),
'isDefault' => TRUE,
),
)
);
$this->addElement('checkbox', 'toggleSelect', NULL, NULL);
$this->add('select',
'trans_assign',
ts('Task'),
array('' => ts('- actions -')) + array('Assign' => ts('Assign to Batch')));
$this->add('submit', 'submit', ts('Go'),
array(
'class' => 'crm-form-submit',
'id' => 'Go',
));
$this->applyFilter('__ALL__', 'trim');
$this->addElement('hidden', 'batch_id', self::$_entityID);
$this->add('text', 'name', ts('Batch Name'));
}
/**
* Set the default values for the form.
*/
public function setDefaultValues() {
// do not setdefault unless it is open/reopened batch
if (!in_array($this->_batchStatus, array('Open', 'Reopened'))) {
return;
}
if (isset(self::$_entityID)) {
$paymentInstrumentID = CRM_Core_DAO::getFieldValue('CRM_Batch_BAO_Batch', self::$_entityID, 'payment_instrument_id');
$defaults['payment_instrument_id'] = $paymentInstrumentID;
$this->assign('paymentInstrumentID', $paymentInstrumentID);
}
return $defaults;
}
/**
* Get action links.
*
* @return array
*/
public function &links() {
if (!(self::$_links)) {
self::$_links = array(
'view' => array(
'name' => ts('View'),
'url' => 'civicrm/contact/view/contribution',
'qs' => 'reset=1&id=%%contid%%&cid=%%cid%%&action=view&context=contribution&selectedChild=contribute',
'title' => ts('View Contribution'),
),
'assign' => array(
'name' => ts('Assign'),
'ref' => 'disable-action',
'title' => ts('Assign Transaction'),
'extra' => 'onclick = "assignRemove( %%id%%,\'' . 'assign' . '\' );"',
),
);
}
return self::$_links;
}
}

View file

@ -0,0 +1,181 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* This class provides the functionality to delete a group of
* contributions. This class provides functionality for the actual
* deletion.
*/
class CRM_Financial_Form_Export extends CRM_Core_Form {
/**
* The financial batch id, used when editing the field
*
* @var int
*/
protected $_id;
/**
* Financial batch ids.
*/
protected $_batchIds = array();
/**
* Export status id.
*/
protected $_exportStatusId;
/**
* Export format.
*/
protected $_exportFormat;
/**
* Build all the data structures needed to build the form.
*/
public function preProcess() {
$this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
// this mean it's a batch action
if (!$this->_id) {
if (!empty($_GET['batch_id'])) {
// validate batch ids
$batchIds = explode(',', $_GET['batch_id']);
foreach ($batchIds as $batchId) {
CRM_Utils_Type::validate($batchId, 'Positive');
}
$this->_batchIds = $_GET['batch_id'];
$this->set('batchIds', $this->_batchIds);
}
else {
$this->_batchIds = $this->get('batchIds');
}
if (!empty($_GET['export_format']) && in_array($_GET['export_format'], array('IIF', 'CSV'))) {
$this->_exportFormat = $_GET['export_format'];
}
}
else {
$this->_batchIds = $this->_id;
}
$allBatchStatus = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id');
$this->_exportStatusId = CRM_Utils_Array::key('Exported', $allBatchStatus);
// check if batch status is valid, do not allow exported batches to export again
$batchStatus = CRM_Batch_BAO_Batch::getBatchStatuses($this->_batchIds);
foreach ($batchStatus as $batchStatusId) {
if ($batchStatusId == $this->_exportStatusId) {
$url = CRM_Core_Session::singleton()->readUserContext();
CRM_Core_Error::statusBounce(ts('You cannot export batches which have already been exported.'), $url);
}
}
$session = CRM_Core_Session::singleton();
$session->replaceUserContext(CRM_Utils_System::url('civicrm/financial/financialbatches',
"reset=1&batchStatus={$this->_exportStatusId}"));
}
/**
* Build the form object.
*/
public function buildQuickForm() {
// this mean it's a batch action
if (!empty($this->_batchIds)) {
$batchNames = CRM_Batch_BAO_Batch::getBatchNames($this->_batchIds);
$this->assign('batchNames', $batchNames);
// Skip building the form if we already have batches and an export format
if ($this->_exportFormat) {
$this->postProcess();
}
}
$optionTypes = array(
'IIF' => ts('Export to IIF'),
'CSV' => ts('Export to CSV'),
);
$this->addRadio('export_format', NULL, $optionTypes, NULL, '<br/>', TRUE);
$this->addButtons(
array(
array(
'type' => 'next',
'name' => ts('Export Batch'),
'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
'isDefault' => TRUE,
),
array(
'type' => 'cancel',
'name' => ts('Cancel'),
),
)
);
}
/**
* Process the form after the input has been submitted and validated.
*/
public function postProcess() {
if (!$this->_exportFormat) {
$params = $this->exportValues();
$this->_exportFormat = $params['export_format'];
}
if ($this->_id) {
$batchIds = array($this->_id);
}
elseif (!empty($this->_batchIds)) {
$batchIds = explode(',', $this->_batchIds);
}
// Recalculate totals
$totals = CRM_Batch_BAO_Batch::batchTotals($batchIds);
// build batch params
$session = CRM_Core_Session::singleton();
$batchParams['modified_date'] = date('YmdHis');
$batchParams['modified_id'] = $session->get('userID');
$batchParams['status_id'] = $this->_exportStatusId;
foreach ($batchIds as $batchId) {
$batchParams['id'] = $batchId;
// Update totals
$batchParams = array_merge($batchParams, $totals[$batchId]);
CRM_Batch_BAO_Batch::create($batchParams);
}
CRM_Batch_BAO_Batch::exportFinancialBatch($batchIds, $this->_exportFormat);
}
}

View file

@ -0,0 +1,209 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* This class generates form components for Financial Account
*/
class CRM_Financial_Form_FinancialAccount extends CRM_Contribute_Form {
/**
* Flag if its a AR account type.
*
* @var boolean
*/
protected $_isARFlag = FALSE;
/**
* Set variables up before form is built.
*/
public function preProcess() {
parent::preProcess();
if ($this->_id) {
$params = array(
'id' => $this->_id,
);
$financialAccount = CRM_Financial_BAO_FinancialAccount::retrieve($params, CRM_Core_DAO::$_nullArray);
$financialAccountTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Asset' "));
if ($financialAccount->financial_account_type_id == $financialAccountTypeId
&& strtolower($financialAccount->account_type_code) == 'ar'
&& !CRM_Financial_BAO_FinancialAccount::getARAccounts($this->_id, $financialAccountTypeId)
) {
$this->_isARFlag = TRUE;
if ($this->_action & CRM_Core_Action::DELETE) {
$msg = ts("The selected financial account cannot be deleted because at least one Accounts Receivable type account is required (to ensure that accounting transactions are in balance).");
CRM_Core_Error::statusBounce($msg);
}
}
}
}
/**
* Build the form object.
*/
public function buildQuickForm() {
parent::buildQuickForm();
$this->setPageTitle(ts('Financial Account'));
if ($this->_action & CRM_Core_Action::DELETE) {
return;
}
$this->applyFilter('__ALL__', 'trim');
$attributes = CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialAccount');
$this->add('text', 'name', ts('Name'), $attributes['name'], TRUE);
$this->addRule('name', ts('A financial type with this name already exists. Please select another name.'),
'objectExists', array('CRM_Financial_DAO_FinancialAccount', $this->_id));
$this->add('text', 'description', ts('Description'), $attributes['description']);
$this->add('text', 'accounting_code', ts('Accounting Code'), $attributes['accounting_code']);
$elementAccounting = $this->add('text', 'account_type_code', ts('Account Type Code'), $attributes['account_type_code']);
$this->addEntityRef('contact_id', ts('Owner'), array(
'api' => array('params' => array('contact_type' => 'Organization')),
'create' => TRUE,
));
$this->add('text', 'tax_rate', ts('Tax Rate'), $attributes['tax_rate']);
$this->add('checkbox', 'is_deductible', ts('Tax-Deductible?'));
$elementActive = $this->add('checkbox', 'is_active', ts('Enabled?'));
$this->add('checkbox', 'is_tax', ts('Is Tax?'));
$element = $this->add('checkbox', 'is_default', ts('Default?'));
// CRM-12470 freeze is default if is_default is set
if ($this->_id && CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id, 'is_default')) {
$element->freeze();
}
$financialAccountType = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id');
if (!empty($financialAccountType)) {
$element = $this->add('select', 'financial_account_type_id', ts('Financial Account Type'),
array('' => '- select -') + $financialAccountType, TRUE, array('class' => 'crm-select2 huge'));
if ($this->_isARFlag) {
$element->freeze();
$elementAccounting->freeze();
$elementActive->freeze();
}
elseif ($this->_id && CRM_Financial_BAO_FinancialAccount::validateFinancialAccount($this->_id)) {
$element->freeze();
}
}
if ($this->_action == CRM_Core_Action::UPDATE &&
CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id, 'is_reserved')
) {
$this->freeze(array('name', 'description', 'is_active'));
}
$this->addFormRule(array('CRM_Financial_Form_FinancialAccount', 'formRule'), $this);
}
/**
* Global validation rules for the form.
*
* @param array $values
* posted values of the form
* @param $files
* @param $self
*
* @return array
* list of errors to be posted back to the form
*/
public static function formRule($values, $files, $self) {
$errorMsg = array();
$financialAccountTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Liability' "));
if (isset($values['is_tax'])) {
if ($values['financial_account_type_id'] != $financialAccountTypeId) {
$errorMsg['financial_account_type_id'] = ts('Taxable accounts should have Financial Account Type set to Liability.');
}
if (CRM_Utils_Array::value('tax_rate', $values) == NULL) {
$errorMsg['tax_rate'] = ts('Please enter value for tax rate');
}
}
if ((CRM_Utils_Array::value('tax_rate', $values) != NULL)) {
if ($values['tax_rate'] < 0 || $values['tax_rate'] >= 100) {
$errorMsg['tax_rate'] = ts('Tax Rate Should be between 0 - 100');
}
}
if ($self->_action & CRM_Core_Action::UPDATE) {
if (!(isset($values['is_tax']))) {
$relationshipId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Sales Tax Account is' "));
$params = array(
'financial_account_id' => $self->_id,
'account_relationship' => $relationshipId,
);
$result = CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults);
if ($result) {
$errorMsg['is_tax'] = ts('Is Tax? must be set for this financial account');
}
}
}
return CRM_Utils_Array::crmIsEmptyArray($errorMsg) ? TRUE : $errorMsg;
}
/**
* Set default values for the form.
* the default values are retrieved from the database.
*/
public function setDefaultValues() {
$defaults = parent::setDefaultValues();
if ($this->_action & CRM_Core_Action::ADD) {
$defaults['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id');
}
return $defaults;
}
/**
* Process the form submission.
*/
public function postProcess() {
if ($this->_action & CRM_Core_Action::DELETE) {
if (CRM_Financial_BAO_FinancialAccount::del($this->_id)) {
CRM_Core_Session::setStatus(ts('Selected Financial Account has been deleted.'));
}
else {
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialAccount', "reset=1&action=browse"));
}
}
else {
// store the submitted values in an array
$params = $this->exportValues();
if ($this->_action & CRM_Core_Action::UPDATE) {
$params['id'] = $this->_id;
}
$financialAccount = CRM_Financial_BAO_FinancialAccount::add($params);
CRM_Core_Session::setStatus(ts('The Financial Account \'%1\' has been saved.', array(1 => $financialAccount->name)), ts('Saved'), 'success');
}
}
}

View file

@ -0,0 +1,302 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* This class generates form components for Accounting Batch
*/
class CRM_Financial_Form_FinancialBatch extends CRM_Contribute_Form {
/**
* The financial batch id, used when editing the field
*
* @var int
*/
protected $_id;
/**
* Set variables up before form is built.
*/
public function preProcess() {
$context = CRM_Utils_Request::retrieve('context', 'String', $this);
$this->set("context", $context);
$this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
parent::preProcess();
$session = CRM_Core_Session::singleton();
if ($this->_id) {
$permissions = array(
CRM_Core_Action::UPDATE => array(
'permission' => array(
'edit own manual batches',
'edit all manual batches',
),
'actionName' => 'edit',
),
CRM_Core_Action::DELETE => array(
'permission' => array(
'delete own manual batches',
'delete all manual batches',
),
'actionName' => 'delete',
),
);
$createdID = CRM_Core_DAO::getFieldValue('CRM_Batch_DAO_Batch', $this->_id, 'created_id');
if (!empty($permissions[$this->_action])) {
$this->checkPermissions($this->_action, $permissions[$this->_action]['permission'], $createdID, $session->get('userID'), $permissions[$this->_action]['actionName']);
}
}
}
/**
* Build the form object.
*/
public function buildQuickForm() {
parent::buildQuickForm();
$this->setPageTitle(ts('Financial Batch'));
if (!empty($this->_id)) {
$this->_title = CRM_Core_DAO::getFieldValue('CRM_Batch_DAO_Batch', $this->_id, 'title');
CRM_Utils_System::setTitle($this->_title . ' - ' . ts('Accounting Batch'));
$this->assign('batchTitle', $this->_title);
$contactID = CRM_Core_DAO::getFieldValue('CRM_Batch_DAO_Batch', $this->_id, 'created_id');
$contactName = CRM_Contact_BAO_Contact::displayName($contactID);
$this->assign('contactName', $contactName);
}
$this->applyFilter('__ALL__', 'trim');
$this->addButtons(
array(
array(
'type' => 'next',
'name' => ts('Save'),
'isDefault' => TRUE,
),
array(
'type' => 'next',
'name' => ts('Save and New'),
'subName' => 'new',
),
array(
'type' => 'cancel',
'name' => ts('Cancel'),
),
)
);
if ($this->_action & CRM_Core_Action::UPDATE && $this->_id) {
$batchStatus = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id');
// unset exported status
$exportedStatusId = CRM_Utils_Array::key('Exported', $batchStatus);
unset($batchStatus[$exportedStatusId]);
$this->add('select', 'status_id', ts('Batch Status'), array('' => ts('- select -')) + $batchStatus, TRUE);
$this->freeze(array('status_id'));
}
$attributes = CRM_Core_DAO::getAttribute('CRM_Batch_DAO_Batch');
$this->add('text', 'title', ts('Batch Name'), $attributes['name'], TRUE);
$this->add('textarea', 'description', ts('Description'), $attributes['description']);
$this->add('select', 'payment_instrument_id', ts('Payment Method'),
array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::paymentInstrument(),
FALSE
);
$this->add('text', 'total', ts('Total Amount'), $attributes['total']);
$this->add('text', 'item_count', ts('Number of Transactions'), $attributes['item_count']);
$this->addFormRule(array('CRM_Financial_Form_FinancialBatch', 'formRule'), $this);
}
/**
* Set default values for the form. Note that in edit/view mode
* the default values are retrieved from the database.
*/
public function setDefaultValues() {
$defaults = parent::setDefaultValues();
if ($this->_id) {
$this->assign('modified_date', $defaults['modified_date']);
$this->assign('created_date', $defaults['created_date']);
}
else {
// set batch name default
$defaults['title'] = CRM_Batch_BAO_Batch::generateBatchName();
}
return $defaults;
}
/**
* Global validation rules for the form.
*
* @param array $values
* @param $files
* @param $self
*
* @return array
* list of errors to be posted back to the form
*/
public static function formRule($values, $files, $self) {
$errors = array();
if (!empty($values['contact_name']) && !is_numeric($values['created_id'])) {
$errors['contact_name'] = ts('Please select a valid contact.');
}
if ($values['item_count'] && (!is_numeric($values['item_count']) || $values['item_count'] < 1)) {
$errors['item_count'] = ts('Number of Transactions should a positive number');
}
if ($values['total'] && (!is_numeric($values['total']) || $values['total'] <= 0)) {
$errors['total'] = ts('Total Amount should be a positive number');
}
if (!empty($values['created_date']) && date('Y-m-d') < date('Y-m-d', strtotime($values['created_date']))) {
$errors['created_date'] = ts('Created date cannot be greater than current date');
}
$batchName = $values['title'];
if (!CRM_Core_DAO::objectExists($batchName, 'CRM_Batch_DAO_Batch', $self->_id)) {
$errors['title'] = ts('This name already exists in database. Batch names must be unique.');
}
return CRM_Utils_Array::crmIsEmptyArray($errors) ? TRUE : $errors;
}
/**
* Process the form submission.
*/
public function postProcess() {
$session = CRM_Core_Session::singleton();
$params = $this->exportValues();
$batchStatus = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id');
if ($this->_id) {
$params['id'] = $this->_id;
}
// store the submitted values in an array
$params['modified_date'] = date('YmdHis');
$params['modified_id'] = $session->get('userID');
if (!empty($params['created_date'])) {
$params['created_date'] = CRM_Utils_Date::processDate($params['created_date']);
}
if ($this->_action & CRM_Core_Action::ADD) {
$batchMode = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'mode_id', array('labelColumn' => 'name'));
$params['mode_id'] = CRM_Utils_Array::key('Manual Batch', $batchMode);
$params['status_id'] = CRM_Utils_Array::key('Open', $batchStatus);
$params['created_date'] = date('YmdHis');
if (empty($params['created_id'])) {
$params['created_id'] = $session->get('userID');
}
$details = "{$params['title']} batch has been created by this contact.";
$activityTypeName = 'Create Batch';
}
elseif ($this->_action & CRM_Core_Action::UPDATE && $this->_id) {
$details = "{$params['title']} batch has been edited by this contact.";
if (CRM_Utils_Array::value($params['status_id'], $batchStatus) == 'Closed') {
$details = "{$params['title']} batch has been closed by this contact.";
}
$activityTypeName = 'Edit Batch';
}
$batch = CRM_Batch_BAO_Batch::create($params);
//set batch id
$this->_id = $batch->id;
$activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name');
// create activity.
$activityParams = array(
'activity_type_id' => array_search($activityTypeName, $activityTypes),
'subject' => $batch->title . "- Batch",
'status_id' => 2,
'priority_id' => 2,
'activity_date_time' => date('YmdHis'),
'source_contact_id' => $session->get('userID'),
'source_contact_qid' => $session->get('userID'),
'details' => $details,
);
CRM_Activity_BAO_Activity::create($activityParams);
$buttonName = $this->controller->getButtonName();
$context = $this->get("context");
if ($batch->title) {
CRM_Core_Session::setStatus(ts("'%1' batch has been saved.", array(1 => $batch->title)), ts('Saved'), 'success');
}
if ($buttonName == $this->getButtonName('next', 'new') & $this->_action == CRM_Core_Action::UPDATE) {
$session->replaceUserContext(CRM_Utils_System::url('civicrm/financial/batch',
"reset=1&action=add&context=1"));
}
elseif ($buttonName == $this->getButtonName('next', 'new')) {
$session->replaceUserContext(CRM_Utils_System::url('civicrm/financial/batch',
"reset=1&action=add"));
}
elseif (CRM_Utils_Array::value($batch->status_id, $batchStatus) == 'Closed') {
$session->replaceUserContext(CRM_Utils_System::url('civicrm', 'reset=1'));
}
elseif (($buttonName == $this->getButtonName('next') & $this->_action == CRM_Core_Action::UPDATE) ||
($buttonName == $this->getButtonName('next') & $this->_action == CRM_Core_Action::ADD & $context == 1)
) {
$session->replaceUserContext(CRM_Utils_System::url('civicrm/financial/financialbatches',
"reset=1&batchStatus=1"));
}
else {
$session->replaceUserContext(CRM_Utils_System::url('civicrm/batchtransaction',
"reset=1&bid={$batch->id}"));
}
}
/**
* Global validation rules for the form.
*
* @param $action
* @param $permissions
* @param int $createdID
* @param int $userContactID
* @param string $actionName
*
* list of errors to be posted back to the form
*/
public function checkPermissions($action, $permissions, $createdID, $userContactID, $actionName) {
if ((CRM_Core_Permission::check($permissions[0]) || CRM_Core_Permission::check($permissions[1]))) {
if (CRM_Core_Permission::check($permissions[0]) && $userContactID != $createdID && !CRM_Core_Permission::check($permissions[1])) {
CRM_Core_Error::statusBounce(ts('You dont have permission to %1 this batch'), array(1 => $actionName));
}
}
else {
CRM_Core_Error::statusBounce(ts('You dont have permission to %1 this batch'), array(1 => $actionName));
}
}
}

View file

@ -0,0 +1,134 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* This class generates form components for Financial Type
*/
class CRM_Financial_Form_FinancialType extends CRM_Contribute_Form {
/**
* Set variables up before form is built.
*/
public function preProcess() {
// Check permission for Financial Type when ACL-FT is enabled
if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()
&& !CRM_Core_Permission::check('administer CiviCRM Financial Types')
) {
CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
}
parent::preProcess();
}
/**
* Build the form object.
*/
public function buildQuickForm() {
parent::buildQuickForm();
$this->setPageTitle(ts('Financial Type'));
$this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
if ($this->_id) {
$this->_title = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_id, 'name');
}
if ($this->_action & CRM_Core_Action::DELETE) {
return;
}
$this->applyFilter('__ALL__', 'trim');
$this->add('text', 'name', ts('Name'), CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialType', 'name'), TRUE);
$this->add('text', 'description', ts('Description'), CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialType', 'description'));
$this->add('checkbox', 'is_deductible', ts('Tax-Deductible?'), CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialType', 'is_deductible'));
$this->add('checkbox', 'is_active', ts('Enabled?'), CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialType', 'is_active'));
$this->add('checkbox', 'is_reserved', ts('Reserved?'), CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialType', 'is_reserved'));
if ($this->_action == CRM_Core_Action::UPDATE) {
$this->assign('aid', $this->_id);
}
if ($this->_action == CRM_Core_Action::UPDATE && CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_id, 'is_reserved', 'vid')) {
$this->freeze(array('is_active'));
}
$this->addRule('name', ts('A financial type with this name already exists. Please select another name.'), 'objectExists',
array('CRM_Financial_DAO_FinancialType', $this->_id)
);
}
/**
* Process the form submission.
*/
public function postProcess() {
if ($this->_action & CRM_Core_Action::DELETE) {
$errors = CRM_Financial_BAO_FinancialType::del($this->_id);
if (!empty($errors)) {
CRM_Core_Error::statusBounce($errors['error_message'], CRM_Utils_System::url('civicrm/admin/financial/financialType', "reset=1&action=browse"), ts('Cannot Delete'));
}
CRM_Core_Session::setStatus(ts('Selected financial type has been deleted.'), ts('Record Deleted'), 'success');
}
else {
$params = $ids = array();
// store the submitted values in an array
$params = $this->exportValues();
if ($this->_action & CRM_Core_Action::UPDATE) {
$ids['financialType'] = $this->_id;
}
$financialType = CRM_Financial_BAO_FinancialType::add($params, $ids);
if ($this->_action & CRM_Core_Action::UPDATE) {
$url = CRM_Utils_System::url('civicrm/admin/financial/financialType', 'reset=1&action=browse');
CRM_Core_Session::setStatus(ts('The financial type "%1" has been updated.', array(1 => $financialType->name)), ts('Saved'), 'success');
}
else {
$url = CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', 'reset=1&action=browse&aid=' . $financialType->id);
$statusArray = array(
1 => $financialType->name,
2 => $financialType->name,
3 => CRM_Utils_Array::value(0, $financialType->titles),
4 => CRM_Utils_Array::value(1, $financialType->titles),
5 => CRM_Utils_Array::value(2, $financialType->titles),
);
if (empty($financialType->titles)) {
$text = ts('Your Financial "%1" Type has been created and assigned to an existing financial account with the same title. You should review the assigned account and determine whether additional account relationships are needed.', $statusArray);
}
else {
$text = ts('Your Financial "%1" Type has been created, along with a corresponding income account "%2". That income account, along with standard financial accounts "%3", "%4" and "%5" have been linked to the financial type. You may edit or replace those relationships here.', $statusArray);
}
CRM_Core_Session::setStatus($text, ts('Saved'), 'success', array('expires' => 0));
}
$session = CRM_Core_Session::singleton();
$session->replaceUserContext($url);
}
}
}

View file

@ -0,0 +1,326 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* This class generates form components for Financial Type Account
*/
class CRM_Financial_Form_FinancialTypeAccount extends CRM_Contribute_Form {
/**
* The financial type id saved to the session for an update.
*
* @var int
*/
protected $_aid;
/**
* The financial type accounts id, used when editing the field
*
* @var int
*/
protected $_id;
/**
* The name of the BAO object for this form.
*
* @var string
*/
protected $_BAOName;
/**
* Flag if its a AR account type.
*
* @var boolean
*/
protected $_isARFlag = FALSE;
/**
* Set variables up before form is built.
*/
public function preProcess() {
$this->_aid = CRM_Utils_Request::retrieve('aid', 'Positive', $this);
$this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
if (!$this->_id && ($this->_action & CRM_Core_Action::UPDATE)) {
$this->_id = CRM_Utils_Type::escape($this->_id, 'Positive');
}
$url = CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts',
"reset=1&action=browse&aid={$this->_aid}");
$this->_BAOName = 'CRM_Financial_BAO_FinancialTypeAccount';
if ($this->_aid && ($this->_action & CRM_Core_Action::ADD)) {
$this->_title = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_aid, 'name');
CRM_Utils_System::setTitle($this->_title . ' - ' . ts('Financial Accounts'));
$session = CRM_Core_Session::singleton();
$session->pushUserContext($url);
}
// CRM-12492
if (!($this->_action & CRM_Core_Action::ADD)) {
$relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
$accountRelationship = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $this->_id, 'account_relationship');
if ($accountRelationship == $relationTypeId) {
$this->_isARFlag = TRUE;
if ($this->_action & CRM_Core_Action::DELETE) {
CRM_Core_Session::setStatus(ts("Selected financial type account with 'Accounts Receivable Account is' account relationship cannot be deleted."),
'', 'error');
CRM_Utils_System::redirect($url);
}
}
}
if ($this->_id) {
$financialAccount = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $this->_id, 'financial_account_id');
$fieldTitle = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $financialAccount, 'name');
CRM_Utils_System::setTitle($fieldTitle . ' - ' . ts('Financial Type Accounts'));
}
$breadCrumb = array(
array(
'title' => ts('Financial Type Accounts'),
'url' => $url,
),
);
CRM_Utils_System::appendBreadCrumb($breadCrumb);
}
/**
* Build the form object.
*/
public function buildQuickForm() {
parent::buildQuickForm();
$this->setPageTitle(ts('Financial Type Account'));
if ($this->_action & CRM_Core_Action::DELETE) {
return;
}
if (isset($this->_id)) {
$params = array('id' => $this->_id);
CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults);
$this->setDefaults($defaults);
$financialAccountTitle = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $defaults['financial_account_id'], 'name');
}
$this->applyFilter('__ALL__', 'trim');
if ($this->_action == CRM_Core_Action::UPDATE) {
$this->assign('aid', $this->_id);
// hidden field to catch the group id in profile
$this->add('hidden', 'financial_type_id', $this->_aid);
// hidden field to catch the field id in profile
$this->add('hidden', 'account_type_id', $this->_id);
}
$params['orderColumn'] = 'label';
$AccountTypeRelationship = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship', $params);
if (!empty($AccountTypeRelationship)) {
$element = $this->add('select',
'account_relationship',
ts('Financial Account Relationship'),
array('select' => ts('- Select Financial Account Relationship -')) + $AccountTypeRelationship,
TRUE
);
}
if ($this->_isARFlag) {
$element->freeze();
}
if ($this->_action == CRM_Core_Action::ADD) {
if (!empty($this->_submitValues['account_relationship']) || !empty($this->_submitValues['financial_account_id'])) {
$financialAccountType = CRM_Financial_BAO_FinancialAccount::getfinancialAccountRelations();
$financialAccountType = CRM_Utils_Array::value($this->_submitValues['account_relationship'], $financialAccountType);
$result = CRM_Contribute_PseudoConstant::financialAccount(NULL, $financialAccountType);
$financialAccountSelect = array('' => ts('- select -')) + $result;
}
else {
$financialAccountSelect = array(
'select' => ts('- select -'),
) + CRM_Contribute_PseudoConstant::financialAccount();
}
}
if ($this->_action == CRM_Core_Action::UPDATE) {
$financialAccountType = CRM_Financial_BAO_FinancialAccount::getfinancialAccountRelations();
$financialAccountType = $financialAccountType[$this->_defaultValues['account_relationship']];
$result = CRM_Contribute_PseudoConstant::financialAccount(NULL, $financialAccountType);
$financialAccountSelect = array('' => ts('- select -')) + $result;
}
$this->add('select',
'financial_account_id',
ts('Financial Account'),
$financialAccountSelect,
TRUE
);
$this->addButtons(array(
array(
'type' => 'next',
'name' => ts('Save'),
'isDefault' => TRUE,
),
array(
'type' => 'next',
'name' => ts('Save and New'),
'subName' => 'new',
),
array(
'type' => 'cancel',
'name' => ts('Cancel'),
),
)
);
$this->addFormRule(array('CRM_Financial_Form_FinancialTypeAccount', 'formRule'), $this);
}
/**
* Global validation rules for the form.
*
* @param array $values
* posted values of the form
* @param $files
* @param $self
*
* @return array
* list of errors to be posted back to the form
*/
public static function formRule($values, $files, $self) {
$errorMsg = array();
$errorFlag = FALSE;
if ($self->_action == CRM_Core_Action::DELETE) {
$relationValues = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship');
if (CRM_Utils_Array::value('financial_account_id', $values) != 'select') {
if ($relationValues[$values['account_relationship']] == 'Premiums Inventory Account is' || $relationValues[$values['account_relationship']] == 'Cost of Sales Account is') {
$premiumsProduct = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_PremiumsProduct', $values['financial_type_id'], 'product_id', 'financial_type_id');
$product = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Product', $values['financial_type_id'], 'name', 'financial_type_id');
if (!empty($premiumsProduct) || !empty($product)) {
$errorMsg['account_relationship'] = 'You cannot remove ' . $relationValues[$values['account_relationship']] . ' relationship while the Financial Type is used for a Premium.';
}
}
}
}
if (CRM_Utils_Array::value('account_relationship', $values) == 'select') {
$errorMsg['account_relationship'] = 'Financial Account relationship is a required field.';
}
if (CRM_Utils_Array::value('financial_account_id', $values) == 'select') {
$errorMsg['financial_account_id'] = 'Financial Account is a required field.';
}
if (!empty($values['account_relationship']) && !empty($values['financial_account_id'])) {
$params = array(
'account_relationship' => $values['account_relationship'],
'entity_id' => $self->_aid,
'entity_table' => 'civicrm_financial_type',
);
$defaults = array();
if ($self->_action == CRM_Core_Action::ADD) {
$relationshipId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Sales Tax Account is' "));
$isTax = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $values['financial_account_id'], 'is_tax');
if ($values['account_relationship'] == $relationshipId) {
if (!($isTax)) {
$errorMsg['financial_account_id'] = ts('Is Tax? must be set for respective financial account');
}
}
$result = CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults);
if ($result) {
$errorFlag = TRUE;
}
}
if ($self->_action == CRM_Core_Action::UPDATE) {
if ($values['account_relationship'] == $self->_defaultValues['account_relationship'] && $values['financial_account_id'] == $self->_defaultValues['financial_account_id']) {
$errorFlag = FALSE;
}
else {
$params['financial_account_id'] = $values['financial_account_id'];
$result = CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults);
if ($result) {
$errorFlag = TRUE;
}
}
}
if ($errorFlag) {
$errorMsg['account_relationship'] = ts('This account relationship already exits');
}
}
return CRM_Utils_Array::crmIsEmptyArray($errorMsg) ? TRUE : $errorMsg;
}
/**
* Process the form submission.
*/
public function postProcess() {
if ($this->_action & CRM_Core_Action::DELETE) {
CRM_Financial_BAO_FinancialTypeAccount::del($this->_id, $this->_aid);
CRM_Core_Session::setStatus(ts('Selected financial type account has been deleted.'));
}
else {
$params = $ids = array();
// store the submitted values in an array
$params = $this->exportValues();
if ($this->_action & CRM_Core_Action::UPDATE) {
$ids['entityFinancialAccount'] = $this->_id;
}
if ($this->_action & CRM_Core_Action::ADD || $this->_action & CRM_Core_Action::UPDATE) {
$params['financial_account_id'] = $this->_submitValues['financial_account_id'];
}
$params['entity_table'] = 'civicrm_financial_type';
if ($this->_action & CRM_Core_Action::ADD) {
$params['entity_id'] = $this->_aid;
}
try {
$financialTypeAccount = CRM_Financial_BAO_FinancialTypeAccount::add($params, $ids);
CRM_Core_Session::setStatus(ts('The financial type Account has been saved.'), ts('Saved'), 'success');
}
catch (CRM_Core_Exception $e) {
CRM_Core_Error::statusBounce($e->getMessage());
}
}
$buttonName = $this->controller->getButtonName();
$session = CRM_Core_Session::singleton();
if ($buttonName == $this->getButtonName('next', 'new')) {
CRM_Core_Session::setStatus(ts(' You can add another Financial Account Type.'));
$session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts',
"reset=1&action=add&aid={$this->_aid}"));
}
else {
$session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts',
"reset=1&action=browse&aid={$this->_aid}"));
}
}
}

View file

@ -0,0 +1,125 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
class CRM_Financial_Form_Payment extends CRM_Core_Form {
/**
* @var int
*/
protected $_paymentProcessorID;
protected $currency;
public $_values = array();
/**
* @var array
*/
public $_paymentProcessor;
/**
* @var bool
*/
public $isBackOffice = FALSE;
/**
* Set variables up before form is built.
*/
public function preProcess() {
parent::preProcess();
$this->_values['custom_pre_id'] = CRM_Utils_Request::retrieve('pre_profile_id', 'Integer', $this);
$this->_paymentProcessorID = CRM_Utils_Request::retrieve('processor_id', 'Integer', CRM_Core_DAO::$_nullObject,
TRUE);
$this->currency = CRM_Utils_Request::retrieve('currency', 'String', CRM_Core_DAO::$_nullObject,
TRUE);
$this->paymentInstrumentID = CRM_Utils_Request::retrieve('payment_instrument_id', 'Integer');
$this->isBackOffice = CRM_Utils_Request::retrieve('is_back_office', 'Integer');
$this->assignBillingType();
$this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($this->_paymentProcessorID);
CRM_Core_Payment_ProcessorForm::preProcess($this);
self::addCreditCardJs($this->_paymentProcessorID);
$this->assign('paymentProcessorID', $this->_paymentProcessorID);
$this->assign('currency', $this->currency);
$this->assign('suppressForm', TRUE);
$this->controller->_generateQFKey = FALSE;
}
/**
* @return string
*/
public function getCurrency() {
return $this->currency;
}
/**
* Build quickForm.
*/
public function buildQuickForm() {
CRM_Core_Payment_ProcessorForm::buildQuickForm($this);
}
/**
* Set default values for the form.
*/
public function setDefaultValues() {
$contactID = $this->getContactID();
CRM_Core_Payment_Form::setDefaultValues($this, $contactID);
return $this->_defaults;
}
/**
* Add JS to show icons for the accepted credit cards.
*
* @param int $paymentProcessorID
* @param string $region
*/
public static function addCreditCardJs($paymentProcessorID = NULL, $region = 'billing-block') {
$creditCards = CRM_Financial_BAO_PaymentProcessor::getCreditCards($paymentProcessorID);
$creditCardTypes = CRM_Core_Payment_Form::getCreditCardCSSNames($creditCards);
CRM_Core_Resources::singleton()
// CRM-20516: add BillingBlock script on billing-block region
// to support this feature in payment form snippet too.
->addScriptFile('civicrm', 'templates/CRM/Core/BillingBlock.js', 10, $region, FALSE)
// workaround for CRM-13634
// ->addSetting(array('config' => array('creditCardTypes' => $creditCardTypes)));
->addScript('CRM.config.creditCardTypes = ' . json_encode($creditCardTypes) . ';', '-9999', $region);
}
}

View file

@ -0,0 +1,317 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
class CRM_Financial_Form_PaymentEdit extends CRM_Core_Form {
/**
* The id of the financial trxn.
*
* @var int
*/
protected $_id;
/**
* The id of the related contribution ID
*
* @var int
*/
protected $_contributionID;
/**
* The variable which holds the information of a financial transaction
*
* @var array
*/
protected $_values;
/**
* Explicitly declare the form context.
*/
public function getDefaultContext() {
return 'create';
}
/**
* Set variables up before form is built.
*/
public function preProcess() {
$this->_action = CRM_Core_Action::UPDATE;
parent::preProcess();
$this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
$this->assign('id', $this->_id);
$this->_contributionID = CRM_Utils_Request::retrieve('contribution_id', 'Positive', $this);
$this->_values = civicrm_api3('FinancialTrxn', 'getsingle', array('id' => $this->_id));
if (!empty($this->_values['payment_processor_id'])) {
CRM_Core_Error::statusBounce(ts('You cannot update this payment'));
}
}
/**
* Set default values.
*
* @return array
*/
public function setDefaultValues() {
return $this->_values;
}
/**
* Build quickForm.
*/
public function buildQuickForm() {
CRM_Utils_System::setTitle(ts('Update Payment details'));
$paymentFields = $this->getPaymentFields();
$this->assign('paymentFields', $paymentFields);
foreach ($paymentFields as $name => $paymentField) {
if (!empty($paymentField['add_field'])) {
$attributes = array(
'entity' => 'FinancialTrxn',
'name' => $name,
);
$this->addField($name, $attributes, $paymentField['is_required']);
}
else {
$this->add($paymentField['htmlType'],
$name,
$paymentField['title'],
$paymentField['attributes'],
$paymentField['is_required']
);
}
}
$this->assign('currency', CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_Currency', $this->_values['currency'], 'symbol', 'name'));
$this->addFormRule(array(__CLASS__, 'formRule'), $this);
$this->addButtons(array(
array(
'type' => 'submit',
'name' => ts('Update'),
'isDefault' => TRUE,
),
array(
'type' => 'cancel',
'name' => ts('Cancel'),
),
));
}
/**
* Global form rule.
*
* @param array $fields
* The input form values.
* @param array $files
* The uploaded files if any.
* @param $self
*
* @return bool|array
* true if no errors, else array of errors
*/
public static function formRule($fields, $files, $self) {
$errors = array();
// if Credit Card is chosen and pan_truncation is not NULL ensure that it's value is numeric else throw validation error
if (CRM_Core_PseudoConstant::getName('CRM_Financial_DAO_FinancialTrxn', 'payment_instrument_id', $fields['payment_instrument_id']) == 'Credit Card' &&
!empty($fields['pan_truncation']) &&
!CRM_Utils_Rule::numeric($fields['pan_truncation'])
) {
$errors['pan_truncation'] = ts('Please enter a valid Card Number');
}
return $errors;
}
/**
* Process the form submission.
*/
public function postProcess() {
$params = array(
'id' => $this->_id,
'payment_instrument_id' => $this->_submitValues['payment_instrument_id'],
'trxn_id' => CRM_Utils_Array::value('trxn_id', $this->_submitValues),
'trxn_date' => CRM_Utils_Array::value('trxn_date', $this->_submitValues, date('YmdHis')),
);
$paymentInstrumentName = CRM_Core_PseudoConstant::getName('CRM_Financial_DAO_FinancialTrxn', 'payment_instrument_id', $params['payment_instrument_id']);
if ($paymentInstrumentName == 'Credit Card') {
$params['card_type_id'] = CRM_Utils_Array::value('card_type_id', $this->_submitValues);
$params['pan_truncation'] = CRM_Utils_Array::value('pan_truncation', $this->_submitValues);
}
elseif ($paymentInstrumentName == 'Check') {
$params['check_number'] = CRM_Utils_Array::value('check_number', $this->_submitValues);
}
$this->submit($params);
CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath()));
}
/**
* Wrapper function to process form submission
*
* @param array $submittedValues
*
*/
protected function submit($submittedValues) {
// if payment instrument is changed then
// 1. Record a new reverse financial transaction with old payment instrument
// 2. Record a new financial transaction with new payment instrument
// 3. Add EntityFinancialTrxn records to relate with corresponding financial item and contribution
if ($submittedValues['payment_instrument_id'] != $this->_values['payment_instrument_id']) {
$previousFinanciaTrxn = $this->_values;
$newFinancialTrxn = $submittedValues;
unset($previousFinanciaTrxn['id'], $newFinancialTrxn['id']);
$previousFinanciaTrxn['trxn_date'] = CRM_Utils_Array::value('trxn_date', $submittedValues, date('YmdHis'));
$previousFinanciaTrxn['total_amount'] = -$previousFinanciaTrxn['total_amount'];
$previousFinanciaTrxn['net_amount'] = -$previousFinanciaTrxn['net_amount'];
$previousFinanciaTrxn['fee_amount'] = -$previousFinanciaTrxn['fee_amount'];
$previousFinanciaTrxn['contribution_id'] = $newFinancialTrxn['contribution_id'] = $this->_contributionID;
$newFinancialTrxn['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($submittedValues['payment_instrument_id']);
foreach (array('total_amount', 'fee_amount', 'net_amount', 'currency', 'is_payment', 'status_id') as $fieldName) {
$newFinancialTrxn[$fieldName] = $this->_values[$fieldName];
}
foreach (array($previousFinanciaTrxn, $newFinancialTrxn) as $financialTrxnParams) {
civicrm_api3('FinancialTrxn', 'create', $financialTrxnParams);
$trxnParams = array(
'total_amount' => $financialTrxnParams['total_amount'],
'contribution_id' => $this->_contributionID,
);
$contributionTotalAmount = CRM_Core_DAO::getFieldValue('CRM_Contribute_BAO_Contribution', $this->_contributionID, 'total_amount');
CRM_Contribute_BAO_Contribution::assignProportionalLineItems($trxnParams, $submittedValues['id'], $contributionTotalAmount);
}
}
else {
// simply update the financial trxn
civicrm_api3('FinancialTrxn', 'create', $submittedValues);
}
self::updateRelatedContribution($submittedValues, $this->_contributionID);
}
/**
* Wrapper for unit testing the post process submit function.
*
* @param array $params
*/
public function testSubmit($params) {
$this->_id = $params['id'];
$this->_contributionID = $params['contribution_id'];
$this->_values = civicrm_api3('FinancialTrxn', 'getsingle', array('id' => $params['id']));
$this->submit($params);
}
/**
* Function to update contribution's check_number and trxn_id by
* concatenating values from financial trxn's check_number and trxn_id respectively
*
* @param array $params
* @param int $contributionID
*/
public static function updateRelatedContribution($params, $contributionID) {
$contributionDAO = new CRM_Contribute_DAO_Contribution();
$contributionDAO->id = $contributionID;
$contributionDAO->find(TRUE);
foreach (array('trxn_id', 'check_number') as $fieldName) {
if (!empty($params[$fieldName])) {
if (!empty($contributionDAO->$fieldName)) {
$values = explode(',', $contributionDAO->$fieldName);
// if submitted check_number or trxn_id value is
// already present then ignore else add to $values array
if (!in_array($params[$fieldName], $values)) {
$values[] = $params[$fieldName];
}
$contributionDAO->$fieldName = implode(',', $values);
}
}
}
$contributionDAO->save();
}
/**
* Get payment fields
*/
public function getPaymentFields() {
$paymentFields = array(
'payment_instrument_id' => array(
'is_required' => TRUE,
'add_field' => TRUE,
),
'check_number' => array(
'is_required' => FALSE,
'add_field' => TRUE,
),
// @TODO we need to show card type icon in place of select field
'card_type_id' => array(
'is_required' => FALSE,
'add_field' => TRUE,
),
'pan_truncation' => array(
'is_required' => FALSE,
'add_field' => TRUE,
),
'trxn_id' => array(
'add_field' => TRUE,
'is_required' => FALSE,
),
'trxn_date' => array(
'htmlType' => 'datepicker',
'name' => 'trxn_date',
'title' => ts('Transaction Date'),
'is_required' => TRUE,
'attributes' => array(
'date' => 'yyyy-mm-dd',
'time' => 24,
),
),
'total_amount' => array(
'htmlType' => 'text',
'name' => 'total_amount',
'title' => ts('Total Amount'),
'is_required' => TRUE,
'attributes' => array(
'readonly' => TRUE,
'size' => 6,
),
),
);
return $paymentFields;
}
}

View file

@ -0,0 +1,143 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* @todo Add comments if possible.
*/
class CRM_Financial_Form_Search extends CRM_Core_Form {
public $_batchStatus;
public function preProcess() {
$this->_batchStatus = CRM_Utils_Request::retrieve('batchStatus', 'Positive', CRM_Core_DAO::$_nullObject, FALSE, NULL);
$this->assign('batchStatus', $this->_batchStatus);
}
/**
* @return array
*/
public function setDefaultValues() {
$defaults = array();
$status = CRM_Utils_Request::retrieve('status', 'Positive', CRM_Core_DAO::$_nullObject, FALSE, 1);
$defaults['batch_update'] = $status;
if ($this->_batchStatus) {
$defaults['status_id'] = $this->_batchStatus;
}
return $defaults;
}
public function buildQuickForm() {
CRM_Core_Resources::singleton()
->addScriptFile('civicrm', 'packages/jquery/plugins/jquery.redirect.min.js', 0, 'html-header');
$attributes = CRM_Core_DAO::getAttribute('CRM_Batch_DAO_Batch');
$attributes['total']['class'] = $attributes['item_count']['class'] = 'number';
$this->add('text', 'title', ts('Batch Name'), $attributes['title']);
$batchStatus = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id', array('labelColumn' => 'name'));
$this->add(
'select',
'status_id',
ts('Batch Status'),
array(
'' => ts('- any -'),
array_search('Open', $batchStatus) => ts('Open'),
array_search('Closed', $batchStatus) => ts('Closed'),
array_search('Exported', $batchStatus) => ts('Exported'),
),
FALSE
);
$this->add(
'select',
'payment_instrument_id',
ts('Payment Method'),
array('' => ts('- any -')) + CRM_Contribute_PseudoConstant::paymentInstrument(),
FALSE
);
$this->add('text', 'total', ts('Total Amount'), $attributes['total']);
$this->add('text', 'item_count', ts('Number of Items'), $attributes['item_count']);
$this->add('text', 'sort_name', ts('Created By'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
$this->assign('elements', array('status_id', 'title', 'sort_name', 'payment_instrument_id', 'item_count', 'total'));
$this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('class' => 'select-rows'));
$batchAction = array(
'reopen' => ts('Re-open'),
'close' => ts('Close'),
'export' => ts('Export'),
'delete' => ts('Delete'),
);
foreach ($batchAction as $action => $ignore) {
if (!CRM_Batch_BAO_Batch::checkBatchPermission($action)) {
unset($batchAction[$action]);
}
}
$this->add('select',
'batch_update',
ts('Task'),
array('' => ts('- actions -')) + $batchAction);
$this->add('submit', 'submit', ts('Go'),
array(
'class' => 'crm-form-submit',
'id' => 'Go',
));
$this->addButtons(
array(
array(
'type' => 'refresh',
'name' => ts('Search'),
'isDefault' => TRUE,
),
)
);
parent::buildQuickForm();
}
public function postProcess() {
$batchIds = array();
foreach ($_POST as $key => $value) {
if (substr($key, 0, 6) == "check_") {
$batch = explode("_", $key);
$batchIds[] = $batch[1];
}
}
if (!empty($_POST['batch_update'])) {
CRM_Batch_BAO_Batch::closeReOpen($batchIds, $_POST['batch_update']);
}
}
}