copyValues($params); if ($financialItem->find(TRUE)) { CRM_Core_DAO::storeValues($financialItem, $defaults); return $financialItem; } return NULL; } /** * Add the financial items and financial trxn. * * @param object $lineItem * Line item object. * @param object $contribution * Contribution object. * @param bool $taxTrxnID * * @param int $trxnId * * @return CRM_Financial_DAO_FinancialItem */ 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 = Civi::settings()->get('contribution_invoice_settings'); $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings); $params['amount'] = $lineItem->tax_amount; $params['description'] = $taxTerm; $accountRelName = 'Sales Tax Account is'; } else { $accountRelName = 'Income Account is'; if (property_exists($contribution, 'revenue_recognition_date') && !CRM_Utils_System::isNull($contribution->revenue_recognition_date)) { $accountRelName = 'Deferred Revenue Account is'; } } if ($lineItem->financial_type_id) { $params['financial_account_id'] = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount( $lineItem->financial_type_id, $accountRelName ); } if (empty($trxnId)) { $trxnId['id'] = CRM_Contribute_BAO_Contribution::$_trxnIDs; if (empty($trxnId['id'])) { $trxn = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution->id, 'ASC', TRUE); $trxnId['id'] = $trxn['financialTrxnId']; } } $financialItem = self::create($params, NULL, $trxnId); return $financialItem; } /** * Create the financial Items and financial entity trxn. * * @param array $params * Associated array to create financial items. * @param array $ids * Financial item ids. * @param array $trxnIds * Financial item ids. * * @return CRM_Financial_DAO_FinancialItem */ public static function create(&$params, $ids = NULL, $trxnIds = NULL) { $financialItem = new CRM_Financial_DAO_FinancialItem(); if (!empty($ids['id'])) { CRM_Utils_Hook::pre('edit', 'FinancialItem', $ids['id'], $params); } else { CRM_Utils_Hook::pre('create', 'FinancialItem', NULL, $params); } $financialItem->copyValues($params); if (!empty($ids['id'])) { $financialItem->id = $ids['id']; } $financialItem->save(); $financialtrxnIDS = CRM_Utils_Array::value('id', $trxnIds); if (!empty($financialtrxnIDS)) { if (!is_array($financialtrxnIDS)) { $financialtrxnIDS = array($financialtrxnIDS); } foreach ($financialtrxnIDS as $tID) { $entity_financial_trxn_params = array( 'entity_table' => "civicrm_financial_item", 'entity_id' => $financialItem->id, 'financial_trxn_id' => $tID, 'amount' => $params['amount'], ); if (!empty($ids['entityFinancialTrxnId'])) { $entity_financial_trxn_params['id'] = $ids['entityFinancialTrxnId']; } self::createEntityTrxn($entity_financial_trxn_params); } } if (!empty($ids['id'])) { CRM_Utils_Hook::post('edit', 'FinancialItem', $financialItem->id, $financialItem); } else { CRM_Utils_Hook::post('create', 'FinancialItem', $financialItem->id, $financialItem); } return $financialItem; } /** * Takes an associative array and creates a entity financial transaction object. * * @param array $params * (reference ) an assoc array of name/value pairs. * * @return CRM_Core_BAO_FinancialTrxn */ public static function createEntityTrxn($params) { $entity_trxn = new CRM_Financial_DAO_EntityFinancialTrxn(); $entity_trxn->copyValues($params); $entity_trxn->save(); return $entity_trxn; } /** * Retrive entity financial trxn details. * * @param array $params * (reference ) an assoc array of name/value pairs. * @param bool $maxId * To retrive max id. * * @return array */ public static function retrieveEntityFinancialTrxn($params, $maxId = FALSE) { $financialItem = new CRM_Financial_DAO_EntityFinancialTrxn(); $financialItem->copyValues($params); // retrieve last entry from civicrm_entity_financial_trxn if ($maxId) { $financialItem->orderBy('id DESC'); $financialItem->limit(1); } $financialItem->find(); while ($financialItem->fetch()) { $financialItems[$financialItem->id] = array( 'id' => $financialItem->id, 'entity_table' => $financialItem->entity_table, 'entity_id' => $financialItem->entity_id, 'financial_trxn_id' => $financialItem->financial_trxn_id, 'amount' => $financialItem->amount, ); } if (!empty($financialItems)) { return $financialItems; } else { return NULL; } } /** * Check if contact is present in financial_item table. * * CRM-12929 * * @param array $contactIds * An array contact id's. * * @param array $error * Error to display. * * @return array */ public static function checkContactPresent($contactIds, &$error) { if (empty($contactIds)) { return FALSE; } $allowPermDelete = Civi::settings()->get('allowPermDeleteFinancial'); if (!$allowPermDelete) { $sql = 'SELECT DISTINCT(cc.id), cc.display_name FROM civicrm_contact cc INNER JOIN civicrm_contribution con ON con.contact_id = cc.id WHERE cc.id IN (' . implode(',', $contactIds) . ') AND con.is_test = 0'; $dao = CRM_Core_DAO::executeQuery($sql); if ($dao->N) { while ($dao->fetch()) { $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid=$dao->id"); $not_deleted[$dao->id] = "$dao->display_name"; } $errorStatus = ''; if (is_array($error)) { $errorStatus = '