| 1 | <?php |
| 2 | // $Id$ |
| 3 | |
| 4 | /* |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | CiviCRM version 4.3 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | | This file is a part of CiviCRM. | |
| 11 | | | |
| 12 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 13 | | under the terms of the GNU Affero General Public License | |
| 14 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 15 | | | |
| 16 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 17 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 19 | | See the GNU Affero General Public License for more details. | |
| 20 | | | |
| 21 | | You should have received a copy of the GNU Affero General Public | |
| 22 | | License and the CiviCRM Licensing Exception along | |
| 23 | | with this program; if not, contact CiviCRM LLC | |
| 24 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 25 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 26 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 27 | +--------------------------------------------------------------------+ |
| 28 | */ |
| 29 | |
| 30 | /** |
| 31 | * File for the CiviCRM APIv3 Pledge functions |
| 32 | * |
| 33 | * @package CiviCRM_APIv3 |
| 34 | * @subpackage API_Pledge_Payment |
| 35 | * |
| 36 | * @copyright CiviCRM LLC (c) 2004-2013 |
| 37 | * @version $Id: PledgePayment.php |
| 38 | * |
| 39 | */ |
| 40 | |
| 41 | /** |
| 42 | * Include utility functions |
| 43 | */ |
| 44 | require_once 'CRM/Pledge/BAO/PledgePayment.php'; |
| 45 | |
| 46 | /** |
| 47 | * Add or update a plege payment. Pledge Payment API doesn't actually add a pledge |
| 48 | * if the request is to 'create' and 'id' is not passed in |
| 49 | * the oldest pledge with no associated contribution is updated |
| 50 | * |
| 51 | * @todo possibly add ability to add payment if there are less payments than pledge installments |
| 52 | * @todo possibily add ability to recalc dates if the schedule is changed |
| 53 | * |
| 54 | * @param array $params input parameters |
| 55 | * {@getfields PledgePayment_create} |
| 56 | * @example PledgePaymentCreate.php |
| 57 | * |
| 58 | * @return array API Result |
| 59 | * @static void |
| 60 | * @access public |
| 61 | */ |
| 62 | function civicrm_api3_pledge_payment_create($params) { |
| 63 | |
| 64 | $paymentParams = $params; |
| 65 | if (empty($params['id']) && !CRM_Utils_Array::value('option.create_new', $params)) { |
| 66 | $paymentDetails = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($params['pledge_id']); |
| 67 | if (empty($paymentDetails)) { |
| 68 | return civicrm_api3_create_error("There are no unmatched payment on this pledge. Pass in the pledge_payment id to specify one or 'option.create_new' to create one"); |
| 69 | } |
| 70 | elseif (is_array($paymentDetails)) { |
| 71 | $paymentParams = array_merge($params, $paymentDetails); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | $dao = CRM_Pledge_BAO_PledgePayment::add($paymentParams); |
| 76 | if(empty($dao->pledge_id)){ |
| 77 | $dao->find(True); |
| 78 | } |
| 79 | _civicrm_api3_object_to_array($dao, $result[$dao->id]); |
| 80 | |
| 81 | |
| 82 | //update pledge status |
| 83 | CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($dao->pledge_id); |
| 84 | |
| 85 | return civicrm_api3_create_success($result, $params, 'pledge_payment', 'create', $dao); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Adjust Metadata for Create action |
| 90 | * |
| 91 | * The metadata is used for setting defaults, documentation & validation |
| 92 | * @param array $params array or parameters determined by getfields |
| 93 | */ |
| 94 | function _civicrm_api3_pledge_payment_create_spec(&$params) { |
| 95 | $params['pledge_id']['api.required'] = 1; |
| 96 | $params['status_id']['api.required'] = 1; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Delete a pledge Payment - Note this deletes the contribution not just the link |
| 101 | * |
| 102 | * @param array $params input parameters |
| 103 | * {@getfields PledgePayment_delete} |
| 104 | * @example PledgePaymentDelete.php |
| 105 | * |
| 106 | * @return array API result |
| 107 | * @static void |
| 108 | * @access public |
| 109 | */ |
| 110 | function civicrm_api3_pledge_payment_delete($params) { |
| 111 | |
| 112 | if (CRM_Pledge_BAO_PledgePayment::del($params['id'])) { |
| 113 | return civicrm_api3_create_success(array('id' => $params['id']), $params,'pledge_payment','delete'); |
| 114 | } |
| 115 | else { |
| 116 | return civicrm_api3_create_error('Could not delete payment'); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Retrieve a set of pledges, given a set of input params |
| 122 | * |
| 123 | * @param array $params input parameters |
| 124 | * {@getfields PledgePayment_get} |
| 125 | * @example PledgePaymentGet.php * |
| 126 | * |
| 127 | * @return array (reference ) array of pledges, if error an array with an error id and error message |
| 128 | * @static void |
| 129 | * @access public |
| 130 | */ |
| 131 | function civicrm_api3_pledge_payment_get($params) { |
| 132 | |
| 133 | |
| 134 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
| 135 | } |
| 136 | |
| 137 | function updatePledgePayments($pledgeId, $paymentStatusId, $paymentIds) { |
| 138 | |
| 139 | $result = updatePledgePayments($pledgeId, $paymentStatusId, $paymentIds = NULL); |
| 140 | return $result; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Gets field for civicrm_pledge_payment functions |
| 145 | * |
| 146 | * @return array fields valid for other functions |
| 147 | */ |
| 148 | function civicrm_api3_pledge_payment_get_spec(&$params) { |
| 149 | $params['option.create_new'] = array('title' => "Create new field rather than update an unpaid payment"); |
| 150 | } |
| 151 | |