/** * generate the final result * * @return array: * 'is_error': set if there is a fatal error * 'log': array with keys: 'type', 'level', 'timestamp', 'message' * 'download_url: URL to download the result * 'download_name: suggested file name for the download */ public function wrapUp($snapshot_id, $is_test, $is_bulk) { $snapshot = CRM_Donrec_Logic_Snapshot::get($snapshot_id); $reply = array(); // open file $preferredFileName = ts('donation_receipts'); $preferredFileSuffix = ts('.csv', array('domain' => 'de.systopia.donrec')); $temp_file = CRM_Donrec_Logic_File::makeFileName($preferredFileName, $preferredFileSuffix); $handle = fopen($temp_file, 'w'); // get headers $headers = CRM_Donrec_Logic_ReceiptTokens::getFullTokenList(); $headers = $this->flattenTokenData($headers); $headers = array_keys($headers); $header_written = false; // write them all into the file $ids = $snapshot->getIds(); foreach ($ids as $id) { $proc_info = $snapshot->getProcessInformation($id); $csv_data = $proc_info['CSV']['csv_data']; if (!empty($csv_data)) { if (!$header_written) { // extend header by extra fields $headers = array_merge($headers, array_keys($csv_data)); $headers = array_unique($headers); // write header fputcsv($handle, $headers, ';', '"'); $header_written = true; } // create and write a line $line = array(); foreach ($headers as $field) { if (isset($csv_data[$field])) { $line[$field] = $csv_data[$field]; } else { $line[$field] = ''; } } fputcsv($handle, $line, ';', '"'); } } // get process info iterator fclose($handle); // create the file $file = CRM_Donrec_Logic_File::createTemporaryFile($temp_file, $preferredFileName . $preferredFileSuffix); CRM_Core_Error::debug_log_message("de.systopia.donrec: resulting CSV file URL is '{$file}'."); if (!empty($file)) { $reply['download_name'] = $preferredFileName; $reply['download_url'] = $file; } CRM_Donrec_Logic_Exporter::addLogEntry($reply, 'CSV process ended.', CRM_Donrec_Logic_Exporter::LOG_TYPE_INFO); return $reply; }
/** * Get all properties of this receipt token source, so we can e.g. export it or pass the * properties into the $template->generatePDF() function to create another copy * * @return array of properties */ public function getAllTokens() { $values = array(); CRM_Donrec_Logic_ReceiptItem::getCustomFields(); $expected_fields = CRM_Donrec_Logic_ReceiptTokens::$STORED_TOKENS; $receipt_id = $this->Id; $receipt_fields = self::$_custom_fields; $item_fields = CRM_Donrec_Logic_ReceiptItem::$_custom_fields; // TODO: FIX, look up table name! $receipt_table_name = 'civicrm_value_donation_receipt_' . self::$_custom_group_id; $item_table_name = 'civicrm_value_donation_receipt_item_' . CRM_Donrec_Logic_ReceiptItem::$_custom_group_id; // get all the receipt data $query_receipt = "SELECT\n receipt.`id` AS `id`,\n receipt.`entity_id` AS `contributor__id`,\n receipt.`{$receipt_fields['receipt_id']}` AS `receipt_id`,\n receipt.`{$receipt_fields['profile']}` AS `profile`,\n receipt.`{$receipt_fields['type']}` AS `type`,\n receipt.`{$receipt_fields['status']}` AS `status`,\n receipt.`{$receipt_fields['issued_on']}` AS `issued_on`,\n receipt.`{$receipt_fields['issued_by']}` AS `issued_by`,\n receipt.`{$receipt_fields['original_file']}` AS `original_file`,\n receipt.`{$receipt_fields['date_from']}` AS `date_from`,\n receipt.`{$receipt_fields['date_to']}` AS `date_to`,\n\n receipt.`{$receipt_fields['display_name']}` AS `contributor__display_name`,\n receipt.`{$receipt_fields['street_address']}` AS `contributor__street_address`,\n receipt.`{$receipt_fields['supplemental_address_1']}` AS `contributor__supplemental_address_1`,\n receipt.`{$receipt_fields['supplemental_address_2']}` AS `contributor__supplemental_address_2`,\n receipt.`{$receipt_fields['postal_code']}` AS `contributor__postal_code`,\n receipt.`{$receipt_fields['city']}` AS `contributor__city`,\n receipt.`{$receipt_fields['country']}` AS `contributor__country`,\n\n receipt.`{$receipt_fields['shipping_street_address']}` AS `addressee__street_address`,\n receipt.`{$receipt_fields['shipping_supplemental_address_1']}` AS `addressee__supplemental_address_1`,\n receipt.`{$receipt_fields['shipping_supplemental_address_2']}` AS `addressee__supplemental_address_2`,\n receipt.`{$receipt_fields['shipping_postal_code']}` AS `addressee__postal_code`,\n receipt.`{$receipt_fields['shipping_city']}` AS `addressee__city`,\n receipt.`{$receipt_fields['shipping_country']}` AS `addressee__country`,\n\n SUM(item.`{$item_fields['total_amount']}`) AS `total_amount`,\n SUM(item.`{$item_fields['non_deductible_amount']}`) AS `non_deductible_amount`,\n item.`{$item_fields['currency']}` AS `currency`\n\n FROM `{$receipt_table_name}` AS receipt\n\n RIGHT JOIN `{$item_table_name}` AS item\n ON item.`{$item_fields['issued_in']}` = receipt.`id`\n AND item.`{$item_fields['status']}` = receipt.`{$receipt_fields['status']}`\n\n WHERE receipt.`id` = {$receipt_id}"; $result = CRM_Core_DAO::executeQuery($query_receipt); if ($result->fetch()) { foreach ($expected_fields as $key => $value) { // copy all expected values, if they exist if (!is_array($key) && isset($result->{$key})) { $values[$key] = $result->{$key}; } } // also, copy the contributor data foreach ($expected_fields['contributor'] as $key => $value) { $qkey = 'contributor__' . $key; if (isset($result->{$qkey})) { $values['contributor'][$key] = $result->{$qkey}; } } // and the addresse data foreach ($expected_fields['addressee'] as $key => $value) { $qkey = 'addressee__' . $key; if (isset($result->{$qkey})) { $values['addressee'][$key] = $result->{$qkey}; } } } else { CRM_Core_Error::debug_log_message("de.systopia.donrec - couldn't load receipt data."); return $values; } // get receipt-item-infos $query_item = "\n SELECT\n item.`id` AS `id`,\n item.`entity_id` AS `contribution_id`,\n item.`{$item_fields['receive_date']}` AS `receive_date`,\n item.`{$item_fields['total_amount']}` AS `total_amount`,\n item.`{$item_fields['issued_on']}` AS `currency`,\n item.`{$item_fields['non_deductible_amount']}` AS `non_deductible_amount`,\n item.`{$item_fields['financial_type_id']}` AS `financial_type_id`,\n type.`name` AS `type`\n\n FROM `{$item_table_name}` AS item\n INNER JOIN `civicrm_contribution` AS contrib\n ON item.`entity_id` = contrib.`id`\n INNER JOIN `civicrm_financial_type` AS type\n ON type.`id` = contrib.`financial_type_id`\n\n WHERE item.`{$item_fields['issued_in']}` = {$receipt_id}"; $result = CRM_Core_DAO::executeQuery($query_item); while ($result->fetch()) { foreach ($expected_fields['lines'] as $key => $value) { if (isset($result->{$key})) { $values['lines'][$result->id][$key] = $result->{$key}; } } } // add dynamically created tokens CRM_Donrec_Logic_ReceiptTokens::addDynamicTokens($values); return $values; }
/** * Get all properties of this receipt token source, so we can e.g. export it or pass the * properties into the $template->generatePDF() function to create another copy * * @return array of properties */ public function getAllTokens() { $values = array(); // create items $values['receipt_id'] = $this->receipt_id; $values['status'] = $this->is_test ? 'DRAFT' : 'ORIGINAL'; $values['issued_on'] = date('Y-m-d H:i:s'); $values['issued_by'] = CRM_Core_Session::singleton()->get('userID'); $values['total_amount'] = 0.0; $values['non_deductible_amount'] = 0.0; $values['date_from'] = 9999999999.0; $values['date_to'] = 0; $values['lines'] = array(); foreach ($this->snapshot_lines as $snapshot_line) { $snapshot_line_id = $snapshot_line['id']; $receive_date = strtotime($snapshot_line['receive_date']); // create line item $values['lines'][$snapshot_line_id] = array('id' => $snapshot_line['id'], 'receive_date' => $snapshot_line['receive_date'], 'contribution_id' => $snapshot_line['contribution_id'], 'total_amount' => $snapshot_line['total_amount'], 'non_deductible_amount' => $snapshot_line['non_deductible_amount'], 'financial_type_id' => $snapshot_line['financial_type_id']); // update general values $values['id'] = $snapshot_line_id; // just use one of them as ID $values['profile'] = $snapshot_line['profile']; $values['contact_id'] = $snapshot_line['contact_id']; $values['currency'] = $snapshot_line['currency']; $values['date_from'] = $snapshot_line['date_from']; $values['date_to'] = $snapshot_line['date_to']; $values['total_amount'] += $snapshot_line['total_amount']; $values['non_deductible_amount'] += $snapshot_line['non_deductible_amount']; } // add contributor and addressee $values['contributor'] = $this->getContributor($values['contact_id']); $values['addressee'] = $this->getAddressee($values['contact_id']); // add dynamically created tokens CRM_Donrec_Logic_ReceiptTokens::addDynamicTokens($values); return $values; }