/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, $values = NULL) { $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.')); $form['params']['start_date'] = array('#type' => 'date', '#title' => t('Start date'), '#default_value' => array('month' => \Drupal::service('date.formatter')->format($values['start_date'], 'custom', 'n'), 'day' => \Drupal::service('date.formatter')->format($values['start_date'], 'custom', 'j'), 'year' => \Drupal::service('date.formatter')->format($values['start_date'], 'custom', 'Y'))); $form['params']['end_date'] = array('#type' => 'date', '#title' => t('End date'), '#default_value' => array('month' => \Drupal::service('date.formatter')->format($values['end_date'], 'custom', 'n'), 'day' => \Drupal::service('date.formatter')->format($values['end_date'], 'custom', 'j'), 'year' => \Drupal::service('date.formatter')->format($values['end_date'], 'custom', 'Y'))); $stat = $values['status']; if ($stat === FALSE) { $stat = uc_report_order_statuses(); } $form['params']['status'] = array('#type' => 'select', '#title' => t('Order statuses'), '#description' => t('Only orders with selected statuses will be included in the report.') . '<br />' . t('Hold Ctrl + click to select multiple statuses.'), '#options' => OrderStatus::getOptionsList(), '#default_value' => $stat, '#multiple' => TRUE, '#size' => 5); $form['params']['actions'] = array('#type' => 'actions'); $form['params']['actions']['submit'] = array('#type' => 'submit', '#value' => t('Update report')); return $form; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, $values, $statuses) { $form['search'] = array('#type' => 'details', '#title' => $this->t('Customize sales report parameters'), '#description' => $this->t('Adjust these values and update the report to build your custom sales summary. Once submitted, the report may be bookmarked for easy reference in the future.')); $form['search']['start_date'] = array('#type' => 'date', '#title' => $this->t('Start date'), '#default_value' => array('month' => \Drupal::service('date.formatter')->format($values['start_date'], 'custom', 'n'), 'day' => \Drupal::service('date.formatter')->format($values['start_date'], 'custom', 'j'), 'year' => \Drupal::service('date.formatter')->format($values['start_date'], 'custom', 'Y'))); $form['search']['end_date'] = array('#type' => 'date', '#title' => $this->t('End date'), '#default_value' => array('month' => \Drupal::service('date.formatter')->format($values['end_date'], 'custom', 'n'), 'day' => \Drupal::service('date.formatter')->format($values['end_date'], 'custom', 'j'), 'year' => \Drupal::service('date.formatter')->format($values['end_date'], 'custom', 'Y'))); $form['search']['length'] = array('#type' => 'select', '#title' => $this->t('Results breakdown'), '#description' => $this->t('Large daily reports may take a long time to display.'), '#options' => array('day' => $this->t('daily'), 'week' => $this->t('weekly'), 'month' => $this->t('monthly'), 'year' => $this->t('yearly')), '#default_value' => $values['length']); if ($statuses === FALSE) { $statuses = uc_report_order_statuses(); } $form['search']['status'] = array('#type' => 'checkboxes', '#title' => $this->t('Order statuses'), '#description' => $this->t('Only orders with selected statuses will be included in the report.'), '#options' => OrderStatus::getOptionsList(), '#default_value' => $statuses); $form['search']['detail'] = array('#type' => 'checkbox', '#title' => $this->t('Show a detailed list of products ordered.'), '#default_value' => $values['detail']); $form['search']['actions'] = array('#type' => 'actions'); $form['search']['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Update report')); return $form; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, $args = NULL) { if (!isset($args['start_date'])) { $args['start_date'] = REQUEST_TIME; } if (!isset($args['end_date'])) { $args['end_date'] = REQUEST_TIME; } if (!isset($args['statuses'])) { $args['statuses'] = uc_report_order_statuses(); } $form['params'] = array('#type' => 'fieldset', '#title' => $this->t('Customize tax report parameters'), '#description' => $this->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.')); $form['params']['start_date'] = array('#type' => 'datetime', '#title' => $this->t('Start date'), '#date_date_element' => 'date', '#date_time_element' => 'none', '#default_value' => DrupalDateTime::createFromTimestamp($args['start_date'])); $form['params']['end_date'] = array('#type' => 'datetime', '#title' => $this->t('End date'), '#date_date_element' => 'date', '#date_time_element' => 'none', '#default_value' => DrupalDateTime::createFromTimestamp($args['end_date'])); $form['params']['statuses'] = array('#type' => 'select', '#title' => $this->t('Order statuses'), '#description' => $this->t('Only orders with selected statuses will be included in the report.') . '<br />' . $this->t('Hold Ctrl + click to select multiple statuses.'), '#options' => OrderStatus::getOptionsList(), '#default_value' => $args['statuses'], '#multiple' => TRUE, '#size' => 5); $form['params']['actions'] = array('#type' => 'actions'); $form['params']['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Update report')); return $form; }
/** * Displays the sales tax report form and table. */ public static function report($start_date = NULL, $end_date = NULL, $status = NULL) { // Use default report parameters if we don't detect values in the URL. if ($start_date == '') { $args = array('start_date' => mktime(0, 0, 0, date('n'), 1, date('Y') - 1), 'end_date' => REQUEST_TIME, 'status' => FALSE); } else { $args = array('start_date' => $start_date, 'end_date' => $end_date, 'status' => explode(',', $status)); } // Pull the order statuses into a SQL friendly array. if ($args['status'] === FALSE) { $order_statuses = uc_report_order_statuses(); } else { $order_statuses = $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 li.amount, li.title, li.data FROM {uc_orders} o LEFT JOIN {uc_order_line_items} li ON o.order_id = li.order_id WHERE :start <= created AND created <= :end AND order_status IN (:statuses[]) AND li.type = :type", array(':start' => $args['start_date'], ':end' => $args['end_date'], ':statuses[]' => $order_statuses, ':type' => 'tax')); // add up the amounts by jurisdiction $totals = array(); $no_meta_totals = array(); foreach ($result as $item) { $name = trim($item->title); $amount = floatval($item->amount); // get the meta-data out of the serialized array $data = unserialize($item->data); $jurisdiction = trim($data['tax_jurisdiction']); $taxable_amount = floatval($data['taxable_amount']); $rate = floatval($data['tax_rate']); // make a line item in the report for each name/jurisdiction/rate $key = strtolower($name) . strtolower($jurisdiction) . number_format($rate, 5); if (!empty($jurisdiction) && $amount && $taxable_amount) { // we have meta-data if (empty($totals[$key])) { $totals[$key] = array('name' => $name, 'jurisdiction' => $jurisdiction, 'rate' => $rate, 'taxable_amount' => $taxable_amount, 'amount' => $amount); } else { $totals[$key]['taxable_amount'] += $taxable_amount; $totals[$key]['amount'] += $amount; } } elseif ($amount) { // Old data: no meta-data was stored. Just report the amount collected. if (empty($no_meta_totals[$key])) { $no_meta_totals[$key] = array('name' => $name, 'amount' => $amount); } else { $no_meta_totals[$key]['amount'] += $amount; } } } // sort and make this into a report ksort($totals); ksort($no_meta_totals); $taxable_amount = 0; $amount = 0; $star_legend = ''; foreach ($totals as $line) { $row = array($line['name'], $line['jurisdiction'], number_format($line['rate'] * 100, 3) . '%', array('#theme' => 'uc_price', '#price' => $line['taxable_amount']), array('#theme' => 'uc_price', '#price' => $line['amount'])); $rows[] = $row; // Remove HTML for CSV files. $row[3] = $line['taxable_amount']; $row[4] = $line['amount']; $csv_rows[] = $row; $taxable_amount += $line['taxable_amount']; $amount += $line['amount']; } foreach ($no_meta_totals as $line) { $row = array($line['name'], '*', '*', '*', array('#theme' => 'uc_price', '#price' => $line['amount'])); $rows[] = $row; // Remove HTML for CSV files. $row[4] = $line['amount']; $csv_rows[] = $row; $amount += $line['amount']; // We have at least one no-meta-data line. Explain why. $star_legend = t('* No information on jurisdiction, tax rate, or taxable amount is available for this line.'); } // Add a totals line. $row = array(t('Total'), '', '', array('#theme' => 'uc_price', '#price' => $taxable_amount), array('#theme' => 'uc_price', '#price' => $amount)); $rows[] = $row; // Removes HTML for CSV files. $row[3] = $taxable_amount; $row[4] = $amount; $csv_rows[] = $row; // Cache the CSV export. $controller = new \Drupal\uc_report\Controller\Reports(); $csv_data = $controller->store_csv('uc_tax_report', $csv_rows); // Build the page output holding the form, table, and CSV export link. $build['form'] = \Drupal::formBuilder()->getForm('\\Drupal\\uc_tax_report\\Form\\ParametersForm', $args, $args['status']); $build['report'] = array('#theme' => 'table', '#header' => $header, '#rows' => $rows, '#attributes' => array('width' => '100%', 'class' => array('uc-sales-table'))); if ($star_legend) { $build['legend'] = array('#markup' => $star_legend, '#prefix' => '<div class="uc-reports-note"><p>', '#suffix' => '</p></div>'); } $build['export_csv'] = array('#markup' => \Drupal::l(t('Export to CSV file.'), Url::fromRoute('uc_report.getcsv', ['report_id' => $csv_data['report'], 'user_id' => $csv_data['user']])), '#prefix' => '<div class="uc-reports-links">', '#suffix' => '</div>'); return $build; }
/** * Returns sales that occurred in a given time period. * * @param $time * A UNIX timestamp representing the time in which to get sales data. * @param $interval * The amount of time over which to count sales (e.g. [1] day, month, year). * * @return * An associative array containing information about sales: * - date: A string representing the day counting was started. * - income: The total revenue that occurred during the time period. * - total: The total number of orders completed during the time period. * - average: The average revenue produced for each order. */ public function get_sales($start, $interval = 'day') { // Add one to the granularity chosen, and use it to calc the new time. $end = strtotime('+1 ' . $interval, $start) - 1; // Set up the default SQL for getting orders with the proper status // within this period. $order_statuses = uc_report_order_statuses(); // Get the total value of the orders. $output = array('income' => 0); $orders = db_query("SELECT o.order_total FROM {uc_orders} o WHERE o.order_status IN (:statuses[]) AND :start <= created AND created <= :end", array(':statuses[]' => $order_statuses, ':start' => $start, ':end' => $end)); while ($order = $orders->fetchObject()) { $output['income'] += $order->order_total; } // Get the total amount of orders. $count = db_query("SELECT COUNT(o.order_total) FROM {uc_orders} o WHERE o.order_status IN (:statuses[]) AND :start <= created AND created <= :end", array(':statuses[]' => $order_statuses, ':start' => $start, ':end' => $end))->fetchField(); $output['total'] = $count; // Average for this period. $output['average'] = $count != 0 ? round($output['income'] / $count, 2) : 0; return $output; }