/** * {@inheritdoc} */ public function report() { //$page_size = (isset($_GET['nopage'])) ? UC_REPORT_MAX_RECORDS : variable_get('uc_report_table_size', 30); $page_size = 30; $csv_rows = array(); $rows = array(); $header = array(array('data' => $this->t('SKU'), 'field' => 'sku', 'sort' => 'asc'), array('data' => $this->t('Product'), 'field' => 'title'), array('data' => $this->t('Stock'), 'field' => 'stock'), array('data' => $this->t('Threshold'), 'field' => 'threshold'), array('data' => $this->t('Operations'))); $csv_rows[] = array($this->t('SKU'), $this->t('Product'), $this->t('Stock'), $this->t('Threshold')); $query = db_select('uc_product_stock', 's')->extend('\\Drupal\\Core\\Database\\Query\\PagerSelectExtender')->extend('\\Drupal\\Core\\Database\\Query\\TableSortExtender')->orderByHeader($header)->limit($page_size)->fields('s', array('nid', 'sku', 'stock', 'threshold')); $query->leftJoin('node_field_data', 'n', 's.nid = n.nid'); $query->addField('n', 'title'); $query->condition('active', 1)->condition('title', '', '<>'); // @todo: Replace arg() //if (arg(4) == 'threshold') { // $query->where('threshold >= stock'); //} $result = $query->execute(); foreach ($result as $stock) { $op = array(); if ($this->currentUser()->hasPermission('administer product stock')) { $op[] = Link::createFromRoute($this->t('edit'), 'uc_stock.edit', ['node' => $stock->nid], ['query' => ['destination' => 'admin/store/reports/stock']])->toString(); } // Add the data to a table row for display. $rows[] = array('data' => array(array('data' => $stock->sku), array('data' => Link::createFromRoute($stock->title, 'uc_stock.edit', ['node' => $stock->nid])->toString()), array('data' => $stock->stock), array('data' => $stock->threshold), array('data' => implode(' ', $op))), 'class' => array($stock->threshold >= $stock->stock ? 'uc-stock-below-threshold' : 'uc-stock-above-threshold')); // Add the data to the CSV contents for export. $csv_rows[] = array($stock->sku, $stock->title, $stock->stock, $stock->threshold); } // Cache the CSV export. $controller = new Reports(); $csv_data = $controller->store_csv('uc_stock', $csv_rows); $build['form'] = $this->formBuilder()->getForm('\\Drupal\\uc_stock\\Form\\StockReportForm'); $build['report'] = array('#theme' => 'table', '#header' => $header, '#rows' => $rows, '#attributes' => array('width' => '100%', 'class' => array('uc-stock-table'))); $build['pager'] = array('#type' => 'pager'); $build['links'] = array('#prefix' => '<div class="uc-reports-links">', '#suffix' => '</div>'); $build['links']['export_csv'] = array('#markup' => Link::createFromRoute($this->t('Export to CSV file'), 'uc_report.getcsv', ['report_id' => $csv_data['report'], 'user_id' => $csv_data['user']])->toString(), '#suffix' => ' '); // if (isset($_GET['nopage'])) { // $build['links']['toggle_pager'] = array( // '#markup' => Link::createFromRoute($this->t('Show paged records'), 'uc_stock.reports')->toString(), // ); // } // else { $build['links']['toggle_pager'] = array('#markup' => Link::createFromRoute($this->t('Show all records'), 'uc_stock.reports', [], ['query' => ['nopage' => '1']])->toString()); // } return $build; }
/** * Displays the sales tax report form and table. */ public function report($start_date = NULL, $end_date = NULL, $statuses = 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')), 'end_date' => REQUEST_TIME, 'statuses' => uc_report_order_statuses()); } else { $args = array('start_date' => $start_date, 'end_date' => $end_date, 'statuses' => explode(',', $statuses)); } // 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", [':start' => $args['start_date'], ':end' => $args['end_date'], ':statuses[]' => $args['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 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); $build['report'] = array('#theme' => 'table', '#header' => $header, '#rows' => $rows, '#attributes' => array('width' => '100%', 'class' => array('uc-sales-table'))); if ($star_legend) { $build['legend'] = array('#prefix' => '<div class="uc-reports-note"><p>', '#markup' => $star_legend, '#suffix' => '</p></div>'); } $build['export_csv'] = array('#type' => 'link', '#prefix' => '<div class="uc-reports-links">', '#title' => t('Export to CSV file.'), '#url' => Url::fromRoute('uc_report.getcsv', ['report_id' => $csv_data['report'], 'user_id' => $csv_data['user']]), '#suffix' => '</div>'); return $build; }