'Sales tax report', 'description' => 'View report on sales tax', 'page callback' => 'uc_tax_report_report_page', 'access arguments' => array('view reports'), 'type' => MENU_NORMAL_ITEM, ); return $items; } // Displays the sales tax report form and table. function uc_tax_report_report_page() { $timezone = _uc_reports_timezone_offset(); $timezone_offset = time() + $timezone; $format = variable_get('uc_date_format_default', 'm/d/Y'); // Use default report parameters if we don't detect values in the URL. if (arg(4) == '') { $args = array( 'start_date' => gmmktime(0, 0, 0, gmdate('n', $timezone_offset), 1, gmdate('Y', $timezone_offset) - 1), 'end_date' => time(), 'status' => FALSE, ); } else { $args = array( 'start_date' => arg(4), 'end_date' => arg(5), 'status' => explode(',', urldecode(arg(6))), ); } // Pull the order statuses into a SQL friendly array. if ($args['status'] === FALSE) { $order_statuses = _uc_reports_order_statuses(); } else { $order_statuses = "('". implode("', '", $args['status']) ."')"; } // Build the header for the report table. $header = array(t('Jurisdiction'), t('Tax rate'), t('Total taxable amount'), t('Total tax collected')); $rows = array(); $csv_rows = array(); $csv_rows[] = $header; // Query to get the tax line items in this date range $result = db_query("SELECT ucoli.amount, ucoli.data FROM {uc_orders} ucord LEFT JOIN {uc_order_statuses} ON order_status_id = order_status LEFT JOIN {uc_order_line_items} ucoli ON ucord.order_id = ucoli.order_id WHERE %d <= created AND created <= %d AND order_status IN $order_statuses AND ucoli.type = 'tax'", $args['start_date'], $args['end_date']); // add up the amounts by jurisdiction $totals = array(); while( $item = db_fetch_object( $result )) { $dat = unserialize( $item->data ); $jur = $dat['tax_jurisdiction']; $tamt = $dat['taxable_amount']; $rate = $dat['tax_rate']; $amt = $item->amount; if( !empty( $jur ) && !empty( $rate ) && !empty( $amt ) && !empty( $tamt )) { // we'll make a line item in the report for each jurisdiction/rate $key = strtolower( $jur ) . number_format( $rate, 5 ); if( empty( $totals[ $key ] )) { $totals[ $key ] = array( $jur, $rate, $tamt, $amt ); } else { $totals[ $key ][2] += $tamt; $totals[ $key ][3] += $amt; } } } // sort and make this into a report ksort( $totals ); $tamt = 0; $amt = 0; foreach( $totals as $line ) { $row = array( $line[0], number_format( $line[1] * 100, 3 ) . '%', uc_currency_format( $line[2] ), uc_currency_format( $line[3] )); $rows[] = $row; $csv_rows[] = $row; $tamt += $line[2]; $amt += $line[3]; } // add a totals line $row = array( t( 'Total' ), '', uc_currency_format( $tamt ), uc_currency_format( $amt )); $rows[] = $row; $csv_rows[] = $row; // Cache the CSV export. $csv_data = uc_reports_store_csv( 'uc_tax_report', $csv_rows ); // Build the page output holding the form, table, and CSV export link. $output = drupal_get_form('uc_tax_report_params_form', $args, $args['status']); $output .= theme('table', $header, $rows, array('width' => '100%', 'class' => 'uc-sales-table')); $output .= ''; return $output; } // Form for parms on the tax report function uc_tax_report_params_form( $form_state, $values ) { $form = array(); $form['params'] = array( '#type' => 'fieldset', '#title' => t('Customize tax report parameters'), '#description' => t('Adjust these values and update the report to build your sales tax report. Once submitted, the report may be bookmarked for easy reference in the future.'), '#collapsible' => TRUE, '#collapsed' => FALSE, ); $form['params']['start_date'] = array( '#type' => 'date', '#title' => t('Start date'), '#default_value' => array( 'month' => format_date($values['start_date'], 'custom', 'n', 0), 'day' => format_date($values['start_date'], 'custom', 'j', 0), 'year' => format_date($values['start_date'], 'custom', 'Y', 0), ), ); $form['params']['end_date'] = array( '#type' => 'date', '#title' => t('End date'), '#default_value' => array( 'month' => format_date($values['end_date'], 'custom', 'n', 0), 'day' => format_date($values['end_date'], 'custom', 'j', 0), 'year' => format_date($values['end_date'], 'custom', 'Y', 0), ), ); $options = array(); foreach (uc_order_status_list() as $status) { $options[$status['id']] = $status['title']; } $stat = $values['status']; if ($stat === FALSE) { $stat = variable_get('uc_reports_reported_statuses', array('completed')); } $form['params']['status'] = array( '#type' => 'select', '#title' => t('Order statuses'), '#description' => t('Only orders with selected statuses will be included in the report.') .'
'. t('Hold Ctrl + click to select multiple statuses.'), '#options' => $options, '#default_value' => $stat, '#multiple' => TRUE, '#size' => 5, ); $form['params']['submit'] = array( '#type' => 'submit', '#value' => t('Update report'), ); return $form; } function uc_tax_report_params_form_validate($form, &$form_state) { if (empty($form_state['values']['status'])) { form_set_error('status', t('You must select at least one order status.')); } } function uc_tax_report_params_form_submit($form, &$form_state) { $timezone_offset = _uc_reports_timezone_offset(); // Build the start and end dates from the form. $start_date = gmmktime(0, 0, 0, $form_state['values']['start_date']['month'], $form_state['values']['start_date']['day'], $form_state['values']['start_date']['year']); $end_date = gmmktime(23, 59, 59, $form_state['values']['end_date']['month'], $form_state['values']['end_date']['day'], $form_state['values']['end_date']['year']); $args = array( $start_date, $end_date, drupal_urlencode(implode(',', array_keys($form_state['values']['status']))), ); drupal_goto('admin/store/reports/tax/'. implode('/', $args)); }