/** * Check method getFinancialAccount() */ public function testRetrieve() { list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount('Asset', 'Asset Account is'); $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' ")); $financialParams = array('entity_table' => 'civicrm_financial_type', 'entity_id' => $financialType->id, 'account_relationship' => $relationTypeId, 'financial_account_id' => $financialAccount->id); $defaults = array(); $financialAccountType = CRM_Financial_BAO_FinancialTypeAccount::retrieve($financialParams, $defaults); $this->assertEquals($financialAccountType['entity_id'], $financialType->id, 'Verify Entity Id.'); $this->assertEquals($financialAccountType['financial_account_id'], $financialAccount->id, 'Verify Financial Account Id.'); }
/** * Browse all financial types * * * @return void * @access public * @static */ function browse() { // get all financial types sorted by weight $financialType = array(); $dao = new CRM_Financial_DAO_FinancialType(); $dao->orderBy('name'); $dao->find(); while ($dao->fetch()) { $financialType[$dao->id] = array(); CRM_Core_DAO::storeValues($dao, $financialType[$dao->id]); $defaults = $financialAccountId = array(); $financialAccounts = CRM_Contribute_PseudoConstant::financialAccount(); $financialAccountIds = array(); $params['entity_id'] = $dao->id; $params['entity_table'] = 'civicrm_financial_type'; CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, CRM_Core_DAO::$_nullArray, $financialAccountIds); foreach ($financialAccountIds as $key => $values) { if (CRM_Utils_Array::value($values['financial_account_id'], $financialAccounts)) { $financialAccountId[$values['financial_account_id']] = CRM_Utils_Array::value($values['financial_account_id'], $financialAccounts); } } if (!empty($financialAccountId)) { $financialType[$dao->id]['financial_account'] = implode(',', $financialAccountId); } // form all action links $action = array_sum(array_keys($this->links())); // update enable/disable links depending on if it is is_reserved or is_active if ($dao->is_reserved) { $action -= CRM_Core_Action::ENABLE; $action -= CRM_Core_Action::DISABLE; $action -= CRM_Core_Action::DELETE; //continue; } else { if ($dao->is_active) { $action -= CRM_Core_Action::ENABLE; } else { $action -= CRM_Core_Action::DISABLE; } } $financialType[$dao->id]['action'] = CRM_Core_Action::formLink(self::links(), $action, array('id' => $dao->id)); } $this->assign('rows', $financialType); }
/** * Get financial account id has 'Sales Tax Account is' * account relationship with financial type * * @param int $financialTypeId * * @return FinancialAccountId */ public static function getFinancialAccountId($financialTypeId) { $accountRel = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Sales Tax Account is' ")); $searchParams = array('entity_table' => 'civicrm_financial_type', 'entity_id' => $financialTypeId, 'account_relationship' => $accountRel); $result = array(); CRM_Financial_BAO_FinancialTypeAccount::retrieve($searchParams, $result); return CRM_Utils_Array::value('financial_account_id', $result); }
/** * 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; }
/** * Add the financial items and financial trxn. * * @param object $lineItem * Line item object. * @param object $contribution * Contribution object. * @param bool $taxTrxnID * * @return void */ public static function add($lineItem, $contribution, $taxTrxnID = FALSE, $trxnId = NULL) { $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); $financialItemStatus = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialItem', 'status_id'); $itemStatus = NULL; if ($contribution->contribution_status_id == array_search('Completed', $contributionStatuses) || $contribution->contribution_status_id == array_search('Pending refund', $contributionStatuses)) { $itemStatus = array_search('Paid', $financialItemStatus); } elseif ($contribution->contribution_status_id == array_search('Pending', $contributionStatuses) || $contribution->contribution_status_id == array_search('In Progress', $contributionStatuses)) { $itemStatus = array_search('Unpaid', $financialItemStatus); } elseif ($contribution->contribution_status_id == array_search('Partially paid', $contributionStatuses)) { $itemStatus = array_search('Partially paid', $financialItemStatus); } $params = array('transaction_date' => CRM_Utils_Date::isoToMysql($contribution->receive_date), 'contact_id' => $contribution->contact_id, 'amount' => $lineItem->line_total, 'currency' => $contribution->currency, 'entity_table' => 'civicrm_line_item', 'entity_id' => $lineItem->id, 'description' => ($lineItem->qty != 1 ? $lineItem->qty . ' of ' : '') . ' ' . $lineItem->label, 'status_id' => $itemStatus); if ($taxTrxnID) { $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings'); $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings); $params['amount'] = $lineItem->tax_amount; $params['description'] = $taxTerm; $accountRel = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Sales Tax Account is' ")); } else { $accountRel = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' ")); } if ($lineItem->financial_type_id) { $searchParams = array('entity_table' => 'civicrm_financial_type', 'entity_id' => $lineItem->financial_type_id, 'account_relationship' => $accountRel); $result = array(); CRM_Financial_BAO_FinancialTypeAccount::retrieve($searchParams, $result); $params['financial_account_id'] = CRM_Utils_Array::value('financial_account_id', $result); } if (empty($trxnId)) { $trxn = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution->id, 'ASC', TRUE); $trxnId['id'] = $trxn['financialTrxnId']; } return self::create($params, NULL, $trxnId); }
/** * check method getFinancialAccount() */ function testRetrieve() { $params = array('name' => 'TestFinancialAccount_3', 'is_deductible' => 0, 'is_active' => 1, 'is_reserved' => 0); $ids = array(); $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params, $ids); $params['name'] = 'test_financialType3'; $financialType = CRM_Financial_BAO_FinancialType::add($params, $ids); $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' ")); $financialParams = array('entity_table' => 'civicrm_financial_type', 'entity_id' => $financialType->id, 'account_relationship' => $relationTypeId, 'financial_account_id' => $financialAccount->id); CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, $ids); $defaults = array(); $financialAccountType = CRM_Financial_BAO_FinancialTypeAccount::retrieve($financialParams, $defaults); $this->assertEquals($financialAccountType['entity_id'], $financialType->id, 'Verify Entity Id.'); $this->assertEquals($financialAccountType['financial_account_id'], $financialAccount->id, 'Verify Financial Account Id.'); }
/** * global validation rules for the form * * @param array $fields posted values of the form * * @return array list of errors to be posted back to the form * @static * @access 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); $defaults = array(); if ($self->_action == CRM_Core_Action::ADD) { $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; }
/** * function to add the financial items and financial trxn * * @param object $lineItem line item object * @param object $contribution contribution object * * @access public * @static * @return void */ static function add($lineItem, $contribution) { $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); $financialItemStatus = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialItem', 'status_id'); $itemStatus = NULL; if ($contribution->contribution_status_id == array_search('Completed', $contributionStatuses)) { $itemStatus = array_search('Paid', $financialItemStatus); } elseif ($contribution->contribution_status_id == array_search('Pending', $contributionStatuses) || $contribution->contribution_status_id == array_search('In Progress', $contributionStatuses)) { $itemStatus = array_search('Unpaid', $financialItemStatus); } elseif ($contribution->contribution_status_id == array_search('Partially paid', $contributionStatuses)) { $itemStatus = array_search('Partially paid', $financialItemStatus); } $params = array('transaction_date' => CRM_Utils_Date::isoToMysql($contribution->receive_date), 'contact_id' => $contribution->contact_id, 'amount' => $lineItem->line_total, 'currency' => $contribution->currency, 'entity_table' => 'civicrm_line_item', 'entity_id' => $lineItem->id, 'description' => ($lineItem->qty != 1 ? $lineItem->qty . ' of ' : '') . ' ' . $lineItem->label, 'status_id' => $itemStatus); if ($lineItem->financial_type_id) { $searchParams = array('entity_table' => 'civicrm_financial_type', 'entity_id' => $lineItem->financial_type_id, 'account_relationship' => 1); $result = array(); CRM_Financial_BAO_FinancialTypeAccount::retrieve($searchParams, $result); $params['financial_account_id'] = CRM_Utils_Array::value('financial_account_id', $result); } $trxn = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution->id, 'ASC', TRUE); $trxnId['id'] = $trxn['financialTrxnId']; return self::create($params, NULL, $trxnId); }
/** * Browse all financial types. */ public function browse() { // 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.')); } // get all financial types sorted by weight $financialType = array(); $dao = new CRM_Financial_DAO_FinancialType(); $dao->orderBy('name'); $dao->find(); while ($dao->fetch()) { $financialType[$dao->id] = array(); CRM_Core_DAO::storeValues($dao, $financialType[$dao->id]); $defaults = $financialAccountId = array(); $financialAccounts = CRM_Contribute_PseudoConstant::financialAccount(); $financialAccountIds = array(); $params['entity_id'] = $dao->id; $params['entity_table'] = 'civicrm_financial_type'; CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, CRM_Core_DAO::$_nullArray, $financialAccountIds); foreach ($financialAccountIds as $key => $values) { if (!empty($financialAccounts[$values['financial_account_id']])) { $financialAccountId[$values['financial_account_id']] = CRM_Utils_Array::value($values['financial_account_id'], $financialAccounts); } } if (!empty($financialAccountId)) { $financialType[$dao->id]['financial_account'] = implode(',', $financialAccountId); } // form all action links $action = array_sum(array_keys($this->links())); // update enable/disable links depending on if it is is_reserved or is_active if ($dao->is_reserved) { $action -= CRM_Core_Action::ENABLE; $action -= CRM_Core_Action::DISABLE; $action -= CRM_Core_Action::DELETE; } else { if ($dao->is_active) { $action -= CRM_Core_Action::ENABLE; } else { $action -= CRM_Core_Action::DISABLE; } } $financialType[$dao->id]['action'] = CRM_Core_Action::formLink(self::links(), $action, array('id' => $dao->id), ts('more'), FALSE, 'financialType.manage.action', 'FinancialType', $dao->id); } $this->assign('rows', $financialType); }