'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( 'Tax Name' ), 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.title, 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(); $nometa_totals = array(); while( $item = db_fetch_object( $result )) { $name = trim( $item->title ); $amt = floatval( $item->amount ); // get the meta-data out of the serialized array $dat = unserialize( $item->data ); $jur = trim( $dat['tax_jurisdiction'] ); $tamt = floatval( $dat['taxable_amount'] ); $rate = floatval( $dat['tax_rate'] ); // make a line item in the report for each name/jurisdiction/rate $key = strtolower( $name ) . strtolower( $jur ) . number_format( $rate, 5 ); if( !empty( $jur ) && $amt && $tamt ) { // we have meta-data if( empty( $totals[ $key ] )) { $totals[ $key ] = array( $name, $jur, $rate, $tamt, $amt ); } else { $totals[ $key ][3] += $tamt; $totals[ $key ][4] += $amt; } } else if( $amt ) { // old data - no meta-data was stored - just report the amount collected if( empty( $nometa_totals[$key ] )) { $nometa_totals[$key] = array( $name, $amt ); } else { $nometa_totals[$key][1] += $amt; } } } // sort and make this into a report ksort( $totals ); ksort( $nometa_totals ); $tamt = 0; $amt = 0; $starleg = ''; foreach( $totals as $line ) { $row = array( $line[0], $line[1], number_format( $line[2] * 100, 3 ) . '%', uc_currency_format( $line[3] ), uc_currency_format( $line[4] )); $rows[] = $row; $csv_rows[] = $row; $tamt += $line[3]; $amt += $line[4]; } foreach( $nometa_totals as $line ) { $row = array( $line[0], '*', '*', '*', uc_currency_format( $line[1] )); $rows[] = $row; $csv_rows[] = $row; $amt += $line[1]; // we have at least one no-meta-data line - explain why $starleg = t( '*: No information on jurisdiction, tax rate, or taxable amount is available for this line.' ); } // 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')); if( $starleg ) { $output .= '
' . $starleg . '