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