Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
347e061b | 19 | * This class provides the functionality to email a group of contacts. |
6a488035 TO |
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 | * | |
b67daa72 | 27 | * @var bool |
6a488035 TO |
28 | */ |
29 | public $_single = FALSE; | |
30 | ||
31 | protected $_rows; | |
32 | ||
33 | /** | |
fe482240 | 34 | * Build all the data structures needed to build the form. |
665e5ec7 | 35 | */ |
00be9182 | 36 | public function preProcess() { |
6a488035 TO |
37 | $id = CRM_Utils_Request::retrieve('id', 'Positive', |
38 | $this, FALSE | |
39 | ); | |
40 | ||
41 | if ($id) { | |
be2fb01f | 42 | $this->_contributionIds = [$id]; |
6a488035 TO |
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}"; | |
e03e1641 | 57 | $count = CRM_Core_DAO::singleValueQuery($query); |
6a488035 TO |
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 | /** | |
fe482240 | 68 | * Build the form object. |
9da59513 | 69 | * |
70 | * @throws \CRM_Core_Exception | |
6a488035 TO |
71 | */ |
72 | public function buildQuickForm() { | |
d7d878d2 JJ |
73 | $this->add('checkbox', 'is_email_receipt', ts('Send e-mail receipt')); |
74 | $this->setDefaults(['is_email_receipt' => 1]); | |
6a488035 | 75 | |
9da59513 | 76 | $contribIDs = implode(',', $this->getIDs()); |
6a488035 TO |
77 | $query = " |
78 | SELECT c.id as contact_id, | |
79 | co.id as contribution_id, | |
80 | c.display_name as display_name, | |
81 | co.total_amount as amount, | |
82 | co.receive_date as receive_date, | |
83 | co.source as source, | |
84 | co.payment_instrument_id as paid_by, | |
85 | co.check_number as check_no | |
86 | FROM civicrm_contact c, | |
87 | civicrm_contribution co | |
88 | WHERE co.contact_id = c.id | |
89 | AND co.id IN ( $contribIDs )"; | |
e03e1641 | 90 | $dao = CRM_Core_DAO::executeQuery($query); |
6a488035 TO |
91 | |
92 | // build a row for each contribution id | |
be2fb01f | 93 | $this->_rows = []; |
353ffa53 | 94 | $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution'); |
be2fb01f | 95 | $defaults = []; |
347e321e | 96 | $now = date('Y-m-d'); |
be2fb01f | 97 | $paidByOptions = ['' => ts('- select -')] + CRM_Contribute_PseudoConstant::paymentInstrument(); |
6a488035 TO |
98 | |
99 | while ($dao->fetch()) { | |
100 | $row['contact_id'] = $dao->contact_id; | |
101 | $row['contribution_id'] = $dao->contribution_id; | |
102 | $row['display_name'] = $dao->display_name; | |
103 | $row['amount'] = $dao->amount; | |
104 | $row['source'] = $dao->source; | |
105 | $row['trxn_id'] = &$this->addElement('text', "trxn_id_{$row['contribution_id']}", ts('Transaction ID')); | |
106 | $this->addRule("trxn_id_{$row['contribution_id']}", | |
107 | ts('This Transaction ID already exists in the database. Include the account number for checks.'), | |
108 | 'objectExists', | |
be2fb01f | 109 | ['CRM_Contribute_DAO_Contribution', $dao->contribution_id, 'trxn_id'] |
6a488035 TO |
110 | ); |
111 | ||
6a488035 TO |
112 | $row['fee_amount'] = &$this->add('text', "fee_amount_{$row['contribution_id']}", ts('Fee Amount'), |
113 | $attributes['fee_amount'] | |
114 | ); | |
115 | $this->addRule("fee_amount_{$row['contribution_id']}", ts('Please enter a valid amount.'), 'money'); | |
116 | $defaults["fee_amount_{$row['contribution_id']}"] = 0.0; | |
117 | ||
73409b5b | 118 | $row['trxn_date'] = $this->add('datepicker', "trxn_date_{$row['contribution_id']}", ts('Transaction Date'), [], FALSE, ['time' => FALSE]); |
6a488035 TO |
119 | $defaults["trxn_date_{$row['contribution_id']}"] = $now; |
120 | ||
347e321e | 121 | $this->add('text', "check_number_{$row['contribution_id']}", ts('Check Number')); |
6a488035 TO |
122 | $defaults["check_number_{$row['contribution_id']}"] = $dao->check_no; |
123 | ||
347e321e | 124 | $this->add('select', "payment_instrument_id_{$row['contribution_id']}", ts('Payment Method'), $paidByOptions); |
6a488035 TO |
125 | $defaults["payment_instrument_id_{$row['contribution_id']}"] = $dao->paid_by; |
126 | ||
127 | $this->_rows[] = $row; | |
128 | } | |
129 | ||
130 | $this->assign_by_ref('rows', $this->_rows); | |
131 | $this->setDefaults($defaults); | |
be2fb01f | 132 | $this->addButtons([ |
1330f57a SL |
133 | [ |
134 | 'type' => 'next', | |
347e321e | 135 | 'name' => ts('Record Payments'), |
1330f57a SL |
136 | 'isDefault' => TRUE, |
137 | ], | |
138 | [ | |
139 | 'type' => 'back', | |
140 | 'name' => ts('Cancel'), | |
141 | ], | |
142 | ]); | |
6a488035 | 143 | |
be2fb01f | 144 | $this->addFormRule(['CRM_Contribute_Form_Task_Status', 'formRule']); |
6a488035 TO |
145 | } |
146 | ||
147 | /** | |
fe482240 | 148 | * Global validation rules for the form. |
6a488035 | 149 | * |
014c4014 TO |
150 | * @param array $fields |
151 | * Posted values of the form. | |
6a488035 | 152 | * |
a6c01b45 CW |
153 | * @return array |
154 | * list of errors to be posted back to the form | |
6a488035 | 155 | */ |
00be9182 | 156 | public static function formRule($fields) { |
be2fb01f | 157 | $seen = $errors = []; |
6a488035 TO |
158 | foreach ($fields as $name => $value) { |
159 | if (strpos($name, 'trxn_id_') !== FALSE) { | |
160 | if ($fields[$name]) { | |
161 | if (array_key_exists($value, $seen)) { | |
162 | $errors[$name] = ts('Transaction ID\'s must be unique. Include the account number for checks.'); | |
163 | } | |
164 | $seen[$value] = 1; | |
165 | } | |
166 | } | |
167 | ||
168 | if ((strpos($name, 'check_number_') !== FALSE) && $value) { | |
169 | $contribID = substr($name, 13); | |
170 | ||
593dbb07 | 171 | if ($fields["payment_instrument_id_{$contribID}"] != CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check')) { |
347e321e | 172 | $errors["payment_instrument_id_{$contribID}"] = ts('Payment Method should be Check when a check number is entered for a contribution.'); |
6a488035 TO |
173 | } |
174 | } | |
175 | } | |
176 | return empty($errors) ? TRUE : $errors; | |
177 | } | |
178 | ||
179 | /** | |
fe482240 | 180 | * Process the form after the input has been submitted and validated. |
6a488035 TO |
181 | */ |
182 | public function postProcess() { | |
183 | $params = $this->controller->exportValues($this->_name); | |
6a488035 | 184 | |
cf0a66ac | 185 | // submit the form with values. |
186 | self::processForm($this, $params); | |
187 | ||
fd7525a8 | 188 | CRM_Core_Session::setStatus(ts('Payments have been recorded for selected record(s).'), ts('Payments recorded'), 'success'); |
cf0a66ac | 189 | } |
190 | ||
191 | /** | |
192 | * Process the form with submitted params. | |
bc854509 | 193 | * |
cf0a66ac | 194 | * Also supports unit test. |
bc854509 | 195 | * |
196 | * @param CRM_Core_Form $form | |
197 | * @param array $params | |
198 | * | |
199 | * @throws \Exception | |
cf0a66ac | 200 | */ |
201 | public static function processForm($form, $params) { | |
cf0a66ac | 202 | foreach ($form->_rows as $row) { |
fd7525a8 AH |
203 | $contribData = civicrm_api3('Contribution', 'getSingle', ['id' => $row['contribution_id']]); |
204 | $trxnParams = [ | |
205 | 'contribution_id' => $row['contribution_id'], | |
206 | // We are safe assuming that payments will be for the total amount of | |
207 | // the contribution because the contributions must be in "Pending" | |
208 | // status. | |
209 | 'total_amount' => $contribData['total_amount'], | |
210 | 'fee_amount' => $params["fee_amount_{$row['contribution_id']}"], | |
211 | 'check_number' => $params["check_number_{$row['contribution_id']}"], | |
212 | 'payment_instrument_id' => $params["payment_instrument_id_{$row['contribution_id']}"], | |
213 | 'net_amount' => $contribData['total_amount'] - $params["fee_amount_{$row['contribution_id']}"], | |
214 | // Not sure why to default to invoice_id, but that's what the form has | |
215 | // been doing historically | |
216 | 'trxn_id' => $params["trxn_id_{$row['contribution_id']}"] ?? $contribData['invoice_id'], | |
217 | 'trxn_date' => $params["trxn_date_{$row['contribution_id']}"] ?? 'now', | |
218 | 'is_send_contribution_notification' => !empty($params['is_email_receipt']), | |
219 | ]; | |
220 | $result = civicrm_api3('Payment', 'create', $trxnParams); | |
6a488035 | 221 | } |
6a488035 | 222 | } |
96025800 | 223 | |
6a488035 | 224 | } |