| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
| 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 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
| 32 | */ |
| 33 | class CRM_Contribute_Form_AdditionalInfo { |
| 34 | |
| 35 | /** |
| 36 | * Build the form object for Premium Information. |
| 37 | * |
| 38 | * Called from the CRM_Contribute_Form_Contribute function and seemingly nowhere else. |
| 39 | * |
| 40 | * Probably this should be on the form that uses it since it is not used on multiple forms. |
| 41 | * |
| 42 | * Putting it on this class doesn't seem to reduce complexity. |
| 43 | * |
| 44 | * @param CRM_Core_Form $form |
| 45 | */ |
| 46 | public static function buildPremium(&$form) { |
| 47 | //premium section |
| 48 | $form->add('hidden', 'hidden_Premium', 1); |
| 49 | $sel1 = $sel2 = array(); |
| 50 | |
| 51 | $dao = new CRM_Contribute_DAO_Product(); |
| 52 | $dao->is_active = 1; |
| 53 | $dao->find(); |
| 54 | $min_amount = array(); |
| 55 | $sel1[0] = ts('-select product-'); |
| 56 | while ($dao->fetch()) { |
| 57 | $sel1[$dao->id] = $dao->name . " ( " . $dao->sku . " )"; |
| 58 | $min_amount[$dao->id] = $dao->min_contribution; |
| 59 | $options = explode(',', $dao->options); |
| 60 | foreach ($options as $k => $v) { |
| 61 | $options[$k] = trim($v); |
| 62 | } |
| 63 | if ($options[0] != '') { |
| 64 | $sel2[$dao->id] = $options; |
| 65 | } |
| 66 | $form->assign('premiums', TRUE); |
| 67 | } |
| 68 | $form->_options = $sel2; |
| 69 | $form->assign('mincontribution', $min_amount); |
| 70 | $sel = &$form->addElement('hierselect', "product_name", ts('Premium'), 'onclick="showMinContrib();"'); |
| 71 | $js = "<script type='text/javascript'>\n"; |
| 72 | $formName = 'document.forms.' . $form->getName(); |
| 73 | |
| 74 | for ($k = 1; $k < 2; $k++) { |
| 75 | if (!isset($defaults['product_name'][$k]) || (!$defaults['product_name'][$k])) { |
| 76 | $js .= "{$formName}['product_name[$k]'].style.display = 'none';\n"; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | $sel->setOptions(array($sel1, $sel2)); |
| 81 | $js .= "</script>\n"; |
| 82 | $form->assign('initHideBoxes', $js); |
| 83 | |
| 84 | $form->addDate('fulfilled_date', ts('Fulfilled'), FALSE, array('formatType' => 'activityDate')); |
| 85 | $form->addElement('text', 'min_amount', ts('Minimum Contribution Amount')); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Build the form object for Additional Details. |
| 90 | * |
| 91 | * @param CRM_Core_Form $form |
| 92 | */ |
| 93 | public static function buildAdditionalDetail(&$form) { |
| 94 | //Additional information section |
| 95 | $form->add('hidden', 'hidden_AdditionalDetail', 1); |
| 96 | |
| 97 | $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution'); |
| 98 | |
| 99 | $form->addField('thankyou_date', array('entity' => 'contribution'), FALSE, FALSE); |
| 100 | |
| 101 | // add various amounts |
| 102 | $nonDeductAmount = &$form->add('text', 'non_deductible_amount', ts('Non-deductible Amount'), |
| 103 | $attributes['non_deductible_amount'] |
| 104 | ); |
| 105 | $form->addRule('non_deductible_amount', ts('Please enter a valid monetary value for Non-deductible Amount.'), 'money'); |
| 106 | |
| 107 | if ($form->_online) { |
| 108 | $nonDeductAmount->freeze(); |
| 109 | } |
| 110 | $feeAmount = &$form->add('text', 'fee_amount', ts('Fee Amount'), |
| 111 | $attributes['fee_amount'] |
| 112 | ); |
| 113 | $form->addRule('fee_amount', ts('Please enter a valid monetary value for Fee Amount.'), 'money'); |
| 114 | if ($form->_online) { |
| 115 | $feeAmount->freeze(); |
| 116 | } |
| 117 | |
| 118 | $netAmount = &$form->add('text', 'net_amount', ts('Net Amount'), |
| 119 | $attributes['net_amount'] |
| 120 | ); |
| 121 | $form->addRule('net_amount', ts('Please enter a valid monetary value for Net Amount.'), 'money'); |
| 122 | if ($form->_online) { |
| 123 | $netAmount->freeze(); |
| 124 | } |
| 125 | $element = &$form->add('text', 'invoice_id', ts('Invoice ID'), |
| 126 | $attributes['invoice_id'] |
| 127 | ); |
| 128 | if ($form->_online) { |
| 129 | $element->freeze(); |
| 130 | } |
| 131 | else { |
| 132 | $form->addRule('invoice_id', |
| 133 | ts('This Invoice ID already exists in the database.'), |
| 134 | 'objectExists', |
| 135 | array('CRM_Contribute_DAO_Contribution', $form->_id, 'invoice_id') |
| 136 | ); |
| 137 | } |
| 138 | $element = $form->add('text', 'creditnote_id', ts('Credit Note ID'), |
| 139 | $attributes['creditnote_id'] |
| 140 | ); |
| 141 | if ($form->_online) { |
| 142 | $element->freeze(); |
| 143 | } |
| 144 | else { |
| 145 | $form->addRule('creditnote_id', |
| 146 | ts('This Credit Note ID already exists in the database.'), |
| 147 | 'objectExists', |
| 148 | array('CRM_Contribute_DAO_Contribution', $form->_id, 'creditnote_id') |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | $form->add('select', 'contribution_page_id', |
| 153 | ts('Online Contribution Page'), |
| 154 | array( |
| 155 | '' => ts('- select -'), |
| 156 | ) + |
| 157 | CRM_Contribute_PseudoConstant::contributionPage() |
| 158 | ); |
| 159 | |
| 160 | $form->add('textarea', 'note', ts('Notes'), array("rows" => 4, "cols" => 60)); |
| 161 | |
| 162 | $statusName = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
| 163 | if ($form->_id && $form->_values['contribution_status_id'] == array_search('Cancelled', $statusName)) { |
| 164 | $netAmount->freeze(); |
| 165 | $feeAmount->freeze(); |
| 166 | } |
| 167 | |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * used by CRM/Pledge/Form/Pledge.php |
| 172 | * |
| 173 | * Build the form object for PaymentReminders Information. |
| 174 | * |
| 175 | * @param CRM_Core_Form $form |
| 176 | */ |
| 177 | public static function buildPaymentReminders(&$form) { |
| 178 | //PaymentReminders section |
| 179 | $form->add('hidden', 'hidden_PaymentReminders', 1); |
| 180 | $form->add('text', 'initial_reminder_day', ts('Send Initial Reminder'), array('size' => 3)); |
| 181 | $form->addRule('initial_reminder_day', ts('Please enter a valid reminder day.'), 'positiveInteger'); |
| 182 | $form->add('text', 'max_reminders', ts('Send up to'), array('size' => 3)); |
| 183 | $form->addRule('max_reminders', ts('Please enter a valid No. of reminders.'), 'positiveInteger'); |
| 184 | $form->add('text', 'additional_reminder_day', ts('Send additional reminders'), array('size' => 3)); |
| 185 | $form->addRule('additional_reminder_day', ts('Please enter a valid additional reminder day.'), 'positiveInteger'); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Process the Premium Information. |
| 190 | * |
| 191 | * @param array $params |
| 192 | * @param int $contributionID |
| 193 | * @param int $premiumID |
| 194 | * @param array $options |
| 195 | */ |
| 196 | public static function processPremium($params, $contributionID, $premiumID = NULL, $options = array()) { |
| 197 | $selectedProductID = $params['product_name'][0]; |
| 198 | $selectedProductOptionID = CRM_Utils_Array::value(1, $params['product_name']); |
| 199 | |
| 200 | $dao = new CRM_Contribute_DAO_ContributionProduct(); |
| 201 | $dao->contribution_id = $contributionID; |
| 202 | $dao->product_id = $selectedProductID; |
| 203 | $dao->fulfilled_date = CRM_Utils_Date::processDate($params['fulfilled_date'], NULL, TRUE); |
| 204 | $isDeleted = FALSE; |
| 205 | |
| 206 | //CRM-11106 |
| 207 | $premiumParams = array( |
| 208 | 'id' => $selectedProductID, |
| 209 | ); |
| 210 | |
| 211 | $productDetails = array(); |
| 212 | CRM_Contribute_BAO_ManagePremiums::retrieve($premiumParams, $productDetails); |
| 213 | $dao->financial_type_id = CRM_Utils_Array::value('financial_type_id', $productDetails); |
| 214 | if (!empty($options[$selectedProductID])) { |
| 215 | $dao->product_option = $options[$selectedProductID][$selectedProductOptionID]; |
| 216 | } |
| 217 | if ($premiumID) { |
| 218 | $ContributionProduct = new CRM_Contribute_DAO_ContributionProduct(); |
| 219 | $ContributionProduct->id = $premiumID; |
| 220 | $ContributionProduct->find(TRUE); |
| 221 | if ($ContributionProduct->product_id == $selectedProductID) { |
| 222 | $dao->id = $premiumID; |
| 223 | } |
| 224 | else { |
| 225 | $ContributionProduct->delete(); |
| 226 | $isDeleted = TRUE; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | $dao->save(); |
| 231 | //CRM-11106 |
| 232 | if ($premiumID == NULL || $isDeleted) { |
| 233 | $premiumParams = array( |
| 234 | 'cost' => CRM_Utils_Array::value('cost', $productDetails), |
| 235 | 'currency' => CRM_Utils_Array::value('currency', $productDetails), |
| 236 | 'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $productDetails), |
| 237 | 'contributionId' => $contributionID, |
| 238 | ); |
| 239 | if ($isDeleted) { |
| 240 | $premiumParams['oldPremium']['product_id'] = $ContributionProduct->product_id; |
| 241 | $premiumParams['oldPremium']['contribution_id'] = $ContributionProduct->contribution_id; |
| 242 | } |
| 243 | CRM_Core_BAO_FinancialTrxn::createPremiumTrxn($premiumParams); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Process the Note. |
| 249 | * |
| 250 | * |
| 251 | * @param array $params |
| 252 | * @param int $contactID |
| 253 | * @param int $contributionID |
| 254 | * @param int $contributionNoteID |
| 255 | */ |
| 256 | public static function processNote($params, $contactID, $contributionID, $contributionNoteID = NULL) { |
| 257 | if (CRM_Utils_System::isNull($params['note']) && $contributionNoteID) { |
| 258 | CRM_Core_BAO_Note::del($contributionNoteID); |
| 259 | return; |
| 260 | } |
| 261 | //process note |
| 262 | $noteParams = array( |
| 263 | 'entity_table' => 'civicrm_contribution', |
| 264 | 'note' => $params['note'], |
| 265 | 'entity_id' => $contributionID, |
| 266 | 'contact_id' => $contactID, |
| 267 | ); |
| 268 | $noteID = array(); |
| 269 | if ($contributionNoteID) { |
| 270 | $noteID = array("id" => $contributionNoteID); |
| 271 | $noteParams['note'] = $noteParams['note'] ? $noteParams['note'] : "null"; |
| 272 | } |
| 273 | CRM_Core_BAO_Note::add($noteParams, $noteID); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Process the Common data. |
| 278 | * |
| 279 | * @param array $params |
| 280 | * @param array $formatted |
| 281 | * @param CRM_Core_Form $form |
| 282 | */ |
| 283 | public static function postProcessCommon(&$params, &$formatted, &$form) { |
| 284 | $fields = array( |
| 285 | 'non_deductible_amount', |
| 286 | 'total_amount', |
| 287 | 'fee_amount', |
| 288 | 'net_amount', |
| 289 | 'trxn_id', |
| 290 | 'invoice_id', |
| 291 | 'creditnote_id', |
| 292 | 'campaign_id', |
| 293 | 'contribution_page_id', |
| 294 | ); |
| 295 | foreach ($fields as $f) { |
| 296 | $formatted[$f] = CRM_Utils_Array::value($f, $params); |
| 297 | } |
| 298 | |
| 299 | if (!empty($params['thankyou_date']) && !CRM_Utils_System::isNull($params['thankyou_date'])) { |
| 300 | $formatted['thankyou_date'] = CRM_Utils_Date::processDate($params['thankyou_date'], $params['thankyou_date_time']); |
| 301 | } |
| 302 | else { |
| 303 | $formatted['thankyou_date'] = 'null'; |
| 304 | } |
| 305 | |
| 306 | if (!empty($params['is_email_receipt'])) { |
| 307 | $params['receipt_date'] = $formatted['receipt_date'] = date('YmdHis'); |
| 308 | } |
| 309 | |
| 310 | //special case to handle if all checkboxes are unchecked |
| 311 | $customFields = CRM_Core_BAO_CustomField::getFields('Contribution', |
| 312 | FALSE, |
| 313 | FALSE, |
| 314 | CRM_Utils_Array::value('financial_type_id', |
| 315 | $params |
| 316 | ) |
| 317 | ); |
| 318 | $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($params, |
| 319 | CRM_Utils_Array::value('id', $params, NULL), |
| 320 | 'Contribution' |
| 321 | ); |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Send email receipt. |
| 326 | * |
| 327 | * @param CRM_Core_Form $form |
| 328 | * instance of Contribution form. |
| 329 | * @param array $params |
| 330 | * (reference ) an assoc array of name/value pairs. |
| 331 | * @param bool $ccContribution |
| 332 | * is it credit card contribution. |
| 333 | * |
| 334 | * @return array |
| 335 | */ |
| 336 | public static function emailReceipt(&$form, &$params, $ccContribution = FALSE) { |
| 337 | $form->assign('receiptType', 'contribution'); |
| 338 | // Retrieve Financial Type Name from financial_type_id |
| 339 | $params['contributionType_name'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', |
| 340 | $params['financial_type_id']); |
| 341 | if (!empty($params['payment_instrument_id'])) { |
| 342 | $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument(); |
| 343 | $params['paidBy'] = $paymentInstrument[$params['payment_instrument_id']]; |
| 344 | if ($params['paidBy'] != 'Check' && isset($params['check_number'])) { |
| 345 | unset($params['check_number']); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | // retrieve individual prefix value for honoree |
| 350 | if (isset($params['soft_credit'])) { |
| 351 | $softCreditTypes = $softCredits = array(); |
| 352 | foreach ($params['soft_credit'] as $key => $softCredit) { |
| 353 | $softCredits[$key] = array( |
| 354 | 'Name' => $softCredit['contact_name'], |
| 355 | 'Amount' => CRM_Utils_Money::format($softCredit['amount'], $softCredit['currency']), |
| 356 | ); |
| 357 | $softCreditTypes[$key] = $softCredit['soft_credit_type_label']; |
| 358 | } |
| 359 | $form->assign('softCreditTypes', $softCreditTypes); |
| 360 | $form->assign('softCredits', $softCredits); |
| 361 | } |
| 362 | |
| 363 | // retrieve premium product name and assigned fulfilled |
| 364 | // date to template |
| 365 | if (!empty($params['hidden_Premium'])) { |
| 366 | if (isset($params['product_name']) && |
| 367 | is_array($params['product_name']) && |
| 368 | !empty($params['product_name']) |
| 369 | ) { |
| 370 | $productDAO = new CRM_Contribute_DAO_Product(); |
| 371 | $productDAO->id = $params['product_name'][0]; |
| 372 | $productOptionID = $params['product_name'][1]; |
| 373 | $productDAO->find(TRUE); |
| 374 | $params['product_name'] = $productDAO->name; |
| 375 | $params['product_sku'] = $productDAO->sku; |
| 376 | |
| 377 | if (empty($params['product_option']) && !empty($form->_options[$productDAO->id])) { |
| 378 | $params['product_option'] = $form->_options[$productDAO->id][$productOptionID]; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | if (!empty($params['fulfilled_date'])) { |
| 383 | $form->assign('fulfilled_date', CRM_Utils_Date::processDate($params['fulfilled_date'])); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | $form->assign('ccContribution', $ccContribution); |
| 388 | if ($ccContribution) { |
| 389 | $form->assignBillingName($params); |
| 390 | $form->assign('address', CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters( |
| 391 | $params, |
| 392 | $form->_bltID |
| 393 | )); |
| 394 | |
| 395 | $date = CRM_Utils_Date::format($params['credit_card_exp_date']); |
| 396 | $date = CRM_Utils_Date::mysqlToIso($date); |
| 397 | $form->assign('credit_card_type', CRM_Utils_Array::value('credit_card_type', $params)); |
| 398 | $form->assign('credit_card_exp_date', $date); |
| 399 | $form->assign('credit_card_number', |
| 400 | CRM_Utils_System::mungeCreditCard($params['credit_card_number']) |
| 401 | ); |
| 402 | } |
| 403 | else { |
| 404 | //offline contribution |
| 405 | // assigned various dates to the templates |
| 406 | $form->assign('receipt_date', CRM_Utils_Date::processDate($params['receipt_date'])); |
| 407 | |
| 408 | if (!empty($params['cancel_date'])) { |
| 409 | $form->assign('cancel_date', CRM_Utils_Date::processDate($params['cancel_date'])); |
| 410 | } |
| 411 | if (!empty($params['thankyou_date'])) { |
| 412 | $form->assign('thankyou_date', CRM_Utils_Date::processDate($params['thankyou_date'])); |
| 413 | } |
| 414 | if ($form->_action & CRM_Core_Action::UPDATE) { |
| 415 | $form->assign('lineItem', empty($form->_lineItems) ? FALSE : $form->_lineItems); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | //handle custom data |
| 420 | if (!empty($params['hidden_custom'])) { |
| 421 | $contribParams = array(array('contribution_id', '=', $params['contribution_id'], 0, 0)); |
| 422 | if ($form->_mode == 'test') { |
| 423 | $contribParams[] = array('contribution_test', '=', 1, 0, 0); |
| 424 | } |
| 425 | |
| 426 | //retrieve custom data |
| 427 | $customGroup = array(); |
| 428 | |
| 429 | foreach ($form->_groupTree as $groupID => $group) { |
| 430 | $customFields = $customValues = array(); |
| 431 | if ($groupID == 'info') { |
| 432 | continue; |
| 433 | } |
| 434 | foreach ($group['fields'] as $k => $field) { |
| 435 | $field['title'] = $field['label']; |
| 436 | $customFields["custom_{$k}"] = $field; |
| 437 | } |
| 438 | |
| 439 | //build the array of customgroup contain customfields. |
| 440 | CRM_Core_BAO_UFGroup::getValues($params['contact_id'], $customFields, $customValues, FALSE, $contribParams); |
| 441 | $customGroup[$group['title']] = $customValues; |
| 442 | } |
| 443 | //assign all custom group and corresponding fields to template. |
| 444 | $form->assign('customGroup', $customGroup); |
| 445 | } |
| 446 | |
| 447 | $form->assign_by_ref('formValues', $params); |
| 448 | list($contributorDisplayName, |
| 449 | $contributorEmail |
| 450 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($params['contact_id']); |
| 451 | $form->assign('contactID', $params['contact_id']); |
| 452 | $form->assign('contributionID', $params['contribution_id']); |
| 453 | |
| 454 | if (!empty($params['currency'])) { |
| 455 | $form->assign('currency', $params['currency']); |
| 456 | } |
| 457 | |
| 458 | if (!empty($params['receive_date'])) { |
| 459 | $form->assign('receive_date', CRM_Utils_Date::processDate($params['receive_date'])); |
| 460 | } |
| 461 | |
| 462 | $template = CRM_Core_Smarty::singleton(); |
| 463 | $taxAmt = $template->get_template_vars('dataArray'); |
| 464 | $eventTaxAmt = $template->get_template_vars('totalTaxAmount'); |
| 465 | $prefixValue = Civi::settings()->get('contribution_invoice_settings'); |
| 466 | $invoicing = CRM_Utils_Array::value('invoicing', $prefixValue); |
| 467 | if ((!empty($taxAmt) || isset($eventTaxAmt)) && (isset($invoicing) && isset($prefixValue['is_email_pdf']))) { |
| 468 | $isEmailPdf = TRUE; |
| 469 | } |
| 470 | else { |
| 471 | $isEmailPdf = FALSE; |
| 472 | } |
| 473 | |
| 474 | list($sendReceipt, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate( |
| 475 | array( |
| 476 | 'groupName' => 'msg_tpl_workflow_contribution', |
| 477 | 'valueName' => 'contribution_offline_receipt', |
| 478 | 'contactId' => $params['contact_id'], |
| 479 | 'contributionId' => $params['contribution_id'], |
| 480 | 'from' => $params['from_email_address'], |
| 481 | 'toName' => $contributorDisplayName, |
| 482 | 'toEmail' => $contributorEmail, |
| 483 | 'isTest' => $form->_mode == 'test', |
| 484 | 'PDFFilename' => ts('receipt') . '.pdf', |
| 485 | 'isEmailPdf' => $isEmailPdf, |
| 486 | ) |
| 487 | ); |
| 488 | |
| 489 | return $sendReceipt; |
| 490 | } |
| 491 | |
| 492 | } |