X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FPrice%2FBAO%2FLineItem.php;h=f399343cef1e4dd29981530dfafa2e2771edf8fe;hb=7741eb0c53e2208601202bbb26d022870857906b;hp=ab13e1560ddc8ed41db76e016394659715215cd7;hpb=f45a6f46304a7b5efd47e6702b1e0782016c7962;p=civicrm-core.git diff --git a/CRM/Price/BAO/LineItem.php b/CRM/Price/BAO/LineItem.php index ab13e1560d..f399343cef 100644 --- a/CRM/Price/BAO/LineItem.php +++ b/CRM/Price/BAO/LineItem.php @@ -50,21 +50,36 @@ class CRM_Price_BAO_LineItem extends CRM_Price_DAO_LineItem { * * @param array $params (reference) an assoc array of name/value pairs * - * @return object CRM_Price_DAO_LineItem object + * @return CRM_Price_DAO_LineItem object * @access public * @static */ static function create(&$params) { - //create mode only as we don't support editing line items + $id = CRM_Utils_Array::value('id', $params); + if ($id) { + CRM_Utils_Hook::pre('edit', 'LineItem', $id, $params); + } + else { + CRM_Utils_Hook::pre('create', 'LineItem', $params['entity_id'], $params); + } - CRM_Utils_Hook::pre('create', 'LineItem', $params['entity_id'], $params); + // unset entity table and entity id in $params + // we never update the entity table and entity id during update mode + if ($id) { + unset($params['entity_id'], $params['entity_table']); + } $lineItemBAO = new CRM_Price_BAO_LineItem(); $lineItemBAO->copyValues($params); $return = $lineItemBAO->save(); - CRM_Utils_Hook::post('create', 'LineItem', $params['entity_id'], $params); + if ($id) { + CRM_Utils_Hook::post('edit', 'LineItem', $id, $params); + } + else { + CRM_Utils_Hook::post('create', 'LineItem', $params['entity_id'], $params); + } return $return; } @@ -78,7 +93,7 @@ class CRM_Price_BAO_LineItem extends CRM_Price_DAO_LineItem { * @param array $params (reference ) an assoc array of name/value pairs * @param array $defaults (reference ) an assoc array to hold the flattened values * - * @return object CRM_Price_BAO_LineItem object + * @return CRM_Price_BAO_LineItem object * @access public * @static */ @@ -92,8 +107,14 @@ class CRM_Price_BAO_LineItem extends CRM_Price_DAO_LineItem { return NULL; } + /** + * @param int $entityId + * @param $entityTable + * + * @return null|string + */ static function getLineTotal($entityId, $entityTable) { - $sqlLineItemTotal = "SELECT SUM(li.line_total) + $sqlLineItemTotal = "SELECT SUM(li.line_total + COALESCE(li.tax_amount,0)) FROM civicrm_line_item li WHERE li.entity_table = '{$entityTable}' AND li.entity_id = {$entityId} @@ -102,6 +123,16 @@ AND li.entity_id = {$entityId} return $lineItemTotal; } + /** + * Wrapper for line item retrieval when contribution ID is known + * @param int $contributionID + * + * @return array + */ + static function getLineItemsByContributionID($contributionID) { + return self::getLineItems($contributionID,'contribution', NULL, TRUE, TRUE, " WHERE contribution_id = " . (int) $contributionID); + } + /** * Given a participant id/contribution id, * return contribution/fee line items @@ -111,17 +142,25 @@ AND li.entity_id = {$entityId} * * @param null $isQuick * @param bool $isQtyZero + * @param bool $relatedEntity + * + * @param string $overrideWhereClause e.g "WHERE contribution id = 7 " per the getLineItemsByContributionID wrapper. + * this function precedes the convenience of the contribution id but since it does quite a bit more than just a db retrieval we need to be able to use it even + * when we don't want it's entity-id magix * * @return array of line items */ - static function getLineItems($entityId, $entity = 'participant', $isQuick = NULL , $isQtyZero = TRUE) { - $selectClause = $whereClause = $fromClause = NULL; + static function getLineItems($entityId, $entity = 'participant', $isQuick = NULL , $isQtyZero = TRUE, $relatedEntity = FALSE, $overrideWhereClause = '') { + $whereClause = $fromClause = NULL; $selectClause = " SELECT li.id, li.label, + li.contribution_id, li.qty, li.unit_price, li.line_total, + li.entity_table, + li.entity_id, pf.label as field_title, pf.html_type, pfv.membership_type_id, @@ -133,14 +172,21 @@ AND li.entity_id = {$entityId} li.tax_amount, pfv.description"; + $condition = "li.entity_id = %2.id AND li.entity_table = 'civicrm_%2'"; + if ($relatedEntity) { + $condition = "li.contribution_id = %2.id "; + } + $fromClause = " FROM civicrm_%2 as %2 - LEFT JOIN civicrm_line_item li ON ( li.entity_id = %2.id AND li.entity_table = 'civicrm_%2') + LEFT JOIN civicrm_line_item li ON ({$condition}) LEFT JOIN civicrm_price_field_value pfv ON ( pfv.id = li.price_field_value_id ) LEFT JOIN civicrm_price_field pf ON (pf.id = li.price_field_id )"; $whereClause = " WHERE %2.id = %1"; + $orderByClause = " ORDER BY pf.weight, pfv.weight"; + if ($isQuick) { $fromClause .= " LEFT JOIN civicrm_price_set cps on cps.id = pf.price_set_id "; $whereClause .= " and cps.is_quick_config = 0"; @@ -161,8 +207,14 @@ AND li.entity_id = {$entityId} 2 => array($entity, 'Text'), ); - $dao = CRM_Core_DAO::executeQuery("$selectClause $fromClause $whereClause", $params); $getTaxDetails = FALSE; + $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,'contribution_invoice_settings'); + $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings); + if ($overrideWhereClause) { + $whereClause = $overrideWhereClause; + } + + $dao = CRM_Core_DAO::executeQuery("$selectClause $fromClause $whereClause $orderByClause", $params); while ($dao->fetch()) { if (!$dao->id) { continue; @@ -178,7 +230,11 @@ AND li.entity_id = {$entityId} 'field_title' => $dao->field_title, 'html_type' => $dao->html_type, 'description' => $dao->description, - 'entity_id' => $entityId, + // the entity id seems prone to randomness but not sure if it has a reason - so if we are overriding the Where clause we assume + // we also JUST WANT TO KNOW the the entity_id in the DB + 'entity_id' => empty($overrideWhereClause) ? $entityId : $dao->entity_id, + 'entity_table' => $dao->entity_table, + 'contribution_id' => $dao->contribution_id, 'financial_type_id' => $dao->financial_type_id, 'membership_type_id' => $dao->membership_type_id, 'membership_num_terms' => $dao->membership_num_terms, @@ -190,8 +246,12 @@ AND li.entity_id = {$entityId} $getTaxDetails = TRUE; } } - $smarty = CRM_Core_Smarty::singleton(); - $smarty->assign('getTaxDetails', $getTaxDetails); + if ($invoicing) { + $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings); + $smarty = CRM_Core_Smarty::singleton(); + $smarty->assign('taxTerm', $taxTerm); + $smarty->assign('getTaxDetails', $getTaxDetails); + } return $lineItems; } @@ -200,10 +260,10 @@ AND li.entity_id = {$entityId} * processAmount method * * @param int $fid price set field id - * @param array $params referance to form values - * @param array $fields referance to array of fields belonging + * @param array $params reference to form values + * @param array $fields reference to array of fields belonging * to the price set used for particular event - * @param array $values referance to the values array( + * @param array $values reference to the values array( this is * lineItem array) * @@ -256,9 +316,10 @@ AND li.entity_id = {$entityId} 'auto_renew' => CRM_Utils_Array::value('auto_renew', $options[$oid]), 'html_type' => $fields['html_type'], 'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $options[$oid]), + 'tax_amount' => CRM_Utils_Array::value('tax_amount', $options[$oid]), ); - if ($values[$oid]['membership_type_id'] && !isset($values[$oid]['auto_renew'])) { + if ($values[$oid]['membership_type_id'] && empty($values[$oid]['auto_renew'])) { $values[$oid]['auto_renew'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $values[$oid]['membership_type_id'], 'auto_renew'); } } @@ -289,17 +350,15 @@ AND li.entity_id = {$entityId} } /** - * Function to process price set and line items. + * Process price set and line items. * - * @param $entityId + * @param int $entityId * @param array $lineItem line item array * @param object $contributionDetails * @param string $entityTable entity table * * @param bool $update * - * @internal param int $contributionId contribution id - * @internal param \decimal $initAmount amount * @access public * @return void * @static @@ -318,7 +377,19 @@ AND li.entity_id = {$entityId} foreach ($values as $line) { $line['entity_table'] = $entityTable; - $line['entity_id'] = $entityId; + if (empty($line['entity_id'])) { + $line['entity_id'] = $entityId; + } + if(!empty($line['membership_type_id'])) { + $line['entity_table'] = 'civicrm_membership'; + } + if (!empty($contributionDetails->id)) { + $line['contribution_id'] = $contributionDetails->id; + if ($line['entity_table'] == 'civicrm_contribution') { + $line['entity_id'] = $contributionDetails->id; + } + } + // if financial type is not set and if price field value is NOT NULL // get financial type id of price field value if (!empty($line['price_field_value_id']) && empty($line['financial_type_id'])) { @@ -335,6 +406,12 @@ AND li.entity_id = {$entityId} } } + /** + * @param int $entityId + * @param string $entityTable + * @param $amount + * @param array $otherParams + */ public static function syncLineItems($entityId, $entityTable = 'civicrm_contribution', $amount, $otherParams = NULL) { if (!$entityId || CRM_Utils_System::isNull($amount)) return; @@ -384,8 +461,8 @@ AND li.entity_id = {$entityId} } /** - * Function to build line items array. - * @param int $params form values + * Build line items array. + * @param array $params form values * * @param string $entityId entity id * @@ -395,27 +472,40 @@ AND li.entity_id = {$entityId} * @return void * @static */ - static function getLineItemArray(&$params, $entityId = NULL, $entityTable = 'contribution') { + static function getLineItemArray(&$params, $entityId = NULL, $entityTable = 'contribution', $isRelatedID = FALSE) { if (!$entityId) { - $priceSetDetails = CRM_Price_BAO_PriceSet::getDefaultPriceSet(); + $priceSetDetails = CRM_Price_BAO_PriceSet::getDefaultPriceSet($entityTable); + $totalAmount = CRM_Utils_Array::value('total_amount', $params); + $financialType = CRM_Utils_Array::value('financial_type_id', $params); foreach ($priceSetDetails as $values) { + if ($entityTable == 'membership') { + if ($isRelatedID != $values['membership_type_id']) { + continue; + } + if (!$totalAmount) { + $totalAmount = $values['amount']; + } + $financialType = $values['financial_type_id']; + } $params['line_item'][$values['setID']][$values['priceFieldID']] = array( 'price_field_id' => $values['priceFieldID'], 'price_field_value_id' => $values['priceFieldValueID'], 'label' => $values['label'], 'qty' => 1, - 'unit_price' => $params['total_amount'], - 'line_total' => $params['total_amount'], - 'financial_type_id' => $params['financial_type_id'] + 'unit_price' => $totalAmount, + 'line_total' => $totalAmount, + 'financial_type_id' => $financialType, + 'membership_type_id' => $values['membership_type_id'] ); + break; } } else { $setID = NULL; $totalEntityId = count($entityId); foreach ($entityId as $id) { - $lineItems = CRM_Price_BAO_LineItem::getLineItems($id, $entityTable); + $lineItems = CRM_Price_BAO_LineItem::getLineItems($id, $entityTable, NULL, TRUE, $isRelatedID); foreach ($lineItems as $key => $values) { if (!$setID && $values['price_field_id']) { $setID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $values['price_field_id'], 'price_set_id'); @@ -444,10 +534,10 @@ AND li.entity_id = {$entityId} */ public static function calculateTaxRate($lineItemId) { if ($lineItemId['html_type'] == 'Text') { - $tax = (($lineItemId['line_total'] - ($lineItemId['unit_price'] * $lineItemId['qty'])))/($lineItemId['unit_price'] * $lineItemId['qty'])*100; + $tax = $lineItemId['tax_amount']/($lineItemId['unit_price'] * $lineItemId['qty'])*100; } else { - $tax = (($lineItemId['line_total'] - $lineItemId['unit_price'])/$lineItemId['unit_price']) * 100; + $tax = ($lineItemId['tax_amount']/$lineItemId['unit_price']) * 100; } return $tax; }