Fix button name on updated form
[civicrm-core.git] / CRM / Contribute / Form / Task / Status.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class provides the functionality to email a group of contacts.
20 */
21 class CRM_Contribute_Form_Task_Status extends CRM_Contribute_Form_Task {
22
23 /**
24 * Are we operating in "single mode", i.e. updating the task of only
25 * one specific contribution?
26 *
27 * @var bool
28 */
29 public $_single = FALSE;
30
31 protected $_rows;
32
33 /**
34 * Build all the data structures needed to build the form.
35 */
36 public function preProcess() {
37 $id = CRM_Utils_Request::retrieve('id', 'Positive',
38 $this, FALSE
39 );
40
41 if ($id) {
42 $this->_contributionIds = [$id];
43 $this->_componentClause = " civicrm_contribution.id IN ( $id ) ";
44 $this->_single = TRUE;
45 $this->assign('totalSelectedContributions', 1);
46 }
47 else {
48 parent::preProcess();
49 }
50
51 // check that all the contribution ids have pending status
52 $query = "
53 SELECT count(*)
54 FROM civicrm_contribution
55 WHERE contribution_status_id != 2
56 AND {$this->_componentClause}";
57 $count = CRM_Core_DAO::singleValueQuery($query);
58 if ($count != 0) {
59 CRM_Core_Error::statusBounce(ts('Please select only online contributions with Pending status.'));
60 }
61
62 // we have all the contribution ids, so now we get the contact ids
63 parent::setContactIDs();
64 $this->assign('single', $this->_single);
65 }
66
67 /**
68 * Build the form object.
69 */
70 public function buildQuickForm() {
71 $this->add('checkbox', 'is_email_receipt', ts('Send e-mail receipt'));
72 $this->setDefaults(['is_email_receipt' => 1]);
73
74 $contribIDs = implode(',', $this->_contributionIds);
75 $query = "
76 SELECT c.id as contact_id,
77 co.id as contribution_id,
78 c.display_name as display_name,
79 co.total_amount as amount,
80 co.receive_date as receive_date,
81 co.source as source,
82 co.payment_instrument_id as paid_by,
83 co.check_number as check_no
84 FROM civicrm_contact c,
85 civicrm_contribution co
86 WHERE co.contact_id = c.id
87 AND co.id IN ( $contribIDs )";
88 $dao = CRM_Core_DAO::executeQuery($query);
89
90 // build a row for each contribution id
91 $this->_rows = [];
92 $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution');
93 $defaults = [];
94 $now = date('Y-m-d');
95 $paidByOptions = ['' => ts('- select -')] + CRM_Contribute_PseudoConstant::paymentInstrument();
96
97 while ($dao->fetch()) {
98 $row['contact_id'] = $dao->contact_id;
99 $row['contribution_id'] = $dao->contribution_id;
100 $row['display_name'] = $dao->display_name;
101 $row['amount'] = $dao->amount;
102 $row['source'] = $dao->source;
103 $row['trxn_id'] = &$this->addElement('text', "trxn_id_{$row['contribution_id']}", ts('Transaction ID'));
104 $this->addRule("trxn_id_{$row['contribution_id']}",
105 ts('This Transaction ID already exists in the database. Include the account number for checks.'),
106 'objectExists',
107 ['CRM_Contribute_DAO_Contribution', $dao->contribution_id, 'trxn_id']
108 );
109
110 $row['fee_amount'] = &$this->add('text', "fee_amount_{$row['contribution_id']}", ts('Fee Amount'),
111 $attributes['fee_amount']
112 );
113 $this->addRule("fee_amount_{$row['contribution_id']}", ts('Please enter a valid amount.'), 'money');
114 $defaults["fee_amount_{$row['contribution_id']}"] = 0.0;
115
116 $row['trxn_date'] = $this->add('datepicker', "trxn_date_{$row['contribution_id']}", ts('Transaction Date'), [], FALSE, ['time' => FALSE]);
117 $defaults["trxn_date_{$row['contribution_id']}"] = $now;
118
119 $this->add('text', "check_number_{$row['contribution_id']}", ts('Check Number'));
120 $defaults["check_number_{$row['contribution_id']}"] = $dao->check_no;
121
122 $this->add('select', "payment_instrument_id_{$row['contribution_id']}", ts('Payment Method'), $paidByOptions);
123 $defaults["payment_instrument_id_{$row['contribution_id']}"] = $dao->paid_by;
124
125 $this->_rows[] = $row;
126 }
127
128 $this->assign_by_ref('rows', $this->_rows);
129 $this->setDefaults($defaults);
130 $this->addButtons([
131 [
132 'type' => 'next',
133 'name' => ts('Record Payments'),
134 'isDefault' => TRUE,
135 ],
136 [
137 'type' => 'back',
138 'name' => ts('Cancel'),
139 ],
140 ]);
141
142 $this->addFormRule(['CRM_Contribute_Form_Task_Status', 'formRule']);
143 }
144
145 /**
146 * Global validation rules for the form.
147 *
148 * @param array $fields
149 * Posted values of the form.
150 *
151 * @return array
152 * list of errors to be posted back to the form
153 */
154 public static function formRule($fields) {
155 $seen = $errors = [];
156 foreach ($fields as $name => $value) {
157 if (strpos($name, 'trxn_id_') !== FALSE) {
158 if ($fields[$name]) {
159 if (array_key_exists($value, $seen)) {
160 $errors[$name] = ts('Transaction ID\'s must be unique. Include the account number for checks.');
161 }
162 $seen[$value] = 1;
163 }
164 }
165
166 if ((strpos($name, 'check_number_') !== FALSE) && $value) {
167 $contribID = substr($name, 13);
168
169 if ($fields["payment_instrument_id_{$contribID}"] != CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check')) {
170 $errors["payment_instrument_id_{$contribID}"] = ts('Payment Method should be Check when a check number is entered for a contribution.');
171 }
172 }
173 }
174 return empty($errors) ? TRUE : $errors;
175 }
176
177 /**
178 * Process the form after the input has been submitted and validated.
179 */
180 public function postProcess() {
181 $params = $this->controller->exportValues($this->_name);
182
183 // submit the form with values.
184 self::processForm($this, $params);
185
186 CRM_Core_Session::setStatus(ts('Payments have been recorded for selected record(s).'), ts('Payments recorded'), 'success');
187 }
188
189 /**
190 * Process the form with submitted params.
191 *
192 * Also supports unit test.
193 *
194 * @param CRM_Core_Form $form
195 * @param array $params
196 *
197 * @throws \Exception
198 */
199 public static function processForm($form, $params) {
200 foreach ($form->_rows as $row) {
201 $contribData = civicrm_api3('Contribution', 'getSingle', ['id' => $row['contribution_id']]);
202 $trxnParams = [
203 'contribution_id' => $row['contribution_id'],
204 // We are safe assuming that payments will be for the total amount of
205 // the contribution because the contributions must be in "Pending"
206 // status.
207 'total_amount' => $contribData['total_amount'],
208 'fee_amount' => $params["fee_amount_{$row['contribution_id']}"],
209 'check_number' => $params["check_number_{$row['contribution_id']}"],
210 'payment_instrument_id' => $params["payment_instrument_id_{$row['contribution_id']}"],
211 'net_amount' => $contribData['total_amount'] - $params["fee_amount_{$row['contribution_id']}"],
212 // Not sure why to default to invoice_id, but that's what the form has
213 // been doing historically
214 'trxn_id' => $params["trxn_id_{$row['contribution_id']}"] ?? $contribData['invoice_id'],
215 'trxn_date' => $params["trxn_date_{$row['contribution_id']}"] ?? 'now',
216 'is_send_contribution_notification' => !empty($params['is_email_receipt']),
217 ];
218 $result = civicrm_api3('Payment', 'create', $trxnParams);
219 }
220 }
221
222 }