Beispiel #1
0
/**
 * Add or update a Order.
 *
 * @param array $params
 *   Input parameters.
 *
 * @throws API_Exception
 * @return array
 *   Api result array
 */
function civicrm_api3_order_create(&$params)
{
    $contribution = array();
    $entity = NULL;
    $entityIds = array();
    if (CRM_Utils_Array::value('line_items', $params) && is_array($params['line_items'])) {
        $priceSetID = NULL;
        CRM_Contribute_BAO_Contribution::checkLineItems($params);
        foreach ($params['line_items'] as $lineItems) {
            $entityParams = CRM_Utils_Array::value('params', $lineItems, array());
            if (!empty($entityParams) && !empty($lineItems['line_item'])) {
                $item = reset($lineItems['line_item']);
                $entity = str_replace('civicrm_', '', $item['entity_table']);
            }
            if ($entityParams) {
                if (in_array($entity, array('participant', 'membership'))) {
                    $entityParams['skipLineItem'] = TRUE;
                    $entityResult = civicrm_api3($entity, 'create', $entityParams);
                    $params['contribution_mode'] = $entity;
                    $entityIds[] = $params[$entity . '_id'] = $entityResult['id'];
                    foreach ($lineItems['line_item'] as &$items) {
                        $items['entity_id'] = $entityResult['id'];
                    }
                } else {
                    // pledge payment
                }
            }
            if (empty($priceSetID)) {
                $item = reset($lineItems['line_item']);
                $priceSetID = civicrm_api3('PriceField', 'getvalue', array('return' => 'price_set_id', 'id' => $item['price_field_id']));
                $params['line_item'][$priceSetID] = array();
            }
            $params['line_item'][$priceSetID] = array_merge($params['line_item'][$priceSetID], $lineItems['line_item']);
        }
    }
    $contribution = civicrm_api3('Contribution', 'create', $params);
    // add payments
    if ($entity && CRM_Utils_Array::value('id', $contribution)) {
        foreach ($entityIds as $entityId) {
            $paymentParams = array('contribution_id' => $contribution['id'], $entity . '_id' => $entityId);
            // if entity is pledge then build pledge param
            if ($entity == 'pledge') {
                $paymentParams += $entityParams;
            }
            $payments = civicrm_api3($entity . '_payment', 'create', $paymentParams);
        }
    }
    return civicrm_api3_create_success(CRM_Utils_Array::value('values', $contribution), $params, 'Order', 'create');
}
 /**
  * checkLineItems() check if total amount matches the sum of line total
  */
 public function testcheckLineItems()
 {
     $params = array('contact_id' => 202, 'receive_date' => '2010-01-20', 'total_amount' => 100, 'financial_type_id' => 3, 'line_items' => array(array('line_item' => array(array('entity_table' => 'civicrm_contribution', 'price_field_id' => 8, 'price_field_value_id' => 16, 'label' => 'test 1', 'qty' => 1, 'unit_price' => 100, 'line_total' => 100), array('entity_table' => 'civicrm_contribution', 'price_field_id' => 8, 'price_field_value_id' => 17, 'label' => 'Test 2', 'qty' => 1, 'unit_price' => 200, 'line_total' => 200, 'financial_type_id' => 1)), 'params' => array())));
     try {
         $error = CRM_Contribute_BAO_Contribution::checkLineItems($params);
         $this->fail("Missed expected exception");
     } catch (Exception $e) {
         $this->assertEquals("Line item total doesn't match with total amount.", $e->getMessage());
     }
     $this->assertEquals(3, $params['line_items'][0]['line_item'][0]['financial_type_id']);
     $params['total_amount'] = 300;
     CRM_Contribute_BAO_Contribution::checkLineItems($params);
 }