You are here

function uc_stock_report in Ubercart 6.2

Same name and namespace in other branches
  1. 5 uc_stock/uc_stock.module \uc_stock_report()
  2. 7.3 uc_stock/uc_stock.admin.inc \uc_stock_report()

Displays a stock report for products with stock tracking enabled.

1 string reference to 'uc_stock_report'
uc_stock_menu in uc_stock/uc_stock.module
Implements hook_menu().

File

uc_stock/uc_stock.admin.inc, line 47
Stock administration menu items.

Code

function uc_stock_report() {
  drupal_add_css(drupal_get_path('module', 'uc_stock') . '/uc_stock.css');
  $page_size = isset($_GET['nopage']) ? UC_REPORTS_MAX_RECORDS : variable_get('uc_reports_table_size', 30);
  $csv_rows = array();
  $rows = array();
  $header = array(
    array(
      'data' => t('SKU'),
      'field' => 'sku',
      'sort' => 'asc',
    ),
    array(
      'data' => t('Product'),
      'field' => 'title',
    ),
    array(
      'data' => t('Stock'),
      'field' => 'stock',
    ),
    array(
      'data' => t('Threshold'),
      'field' => 'threshold',
    ),
    array(
      'data' => t('Operations'),
    ),
  );
  $csv_rows[] = array(
    t('SKU'),
    t('Product'),
    t('Stock'),
    t('Threshold'),
  );
  $sql = "SELECT s.nid, sku, title, stock, threshold FROM {uc_product_stock} as s LEFT JOIN {node} as n ON s.nid = n.nid WHERE active = 1 AND title <> ''";
  if (arg(4) == 'threshold') {
    $sql .= ' AND threshold >= stock';
  }
  $result = pager_query($sql . tablesort_sql($header), $page_size, 0, NULL);
  while ($stock = db_fetch_object($result)) {
    $op = array();
    if (user_access('administer product stock')) {
      $op[] = l(t('edit'), 'node/' . $stock->nid . '/edit/stock', $options = array(
        'query' => 'destination=admin/store/reports/stock',
      ));
    }

    // Add the data to a table row for display.
    $rows[] = array(
      'data' => array(
        array(
          'data' => $stock->sku,
        ),
        array(
          'data' => l($stock->title, 'node/' . $stock->nid),
        ),
        array(
          'data' => $stock->stock,
        ),
        array(
          'data' => $stock->threshold,
        ),
        array(
          'data' => implode(' ', $op),
        ),
      ),
      'class' => $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,
    );
  }
  $csv_data = uc_reports_store_csv('uc_stock', $csv_rows);
  $output = drupal_get_form('uc_stock_report_form') . theme('table', $header, $rows, array(
    'width' => '100%',
    'class' => 'uc-stock-table',
  )) . theme('pager', NULL, $page_size);
  $output .= '<div class="uc-reports-links">' . l(t('Export to CSV file'), 'admin/store/reports/getcsv/' . $csv_data['report'] . '/' . $csv_data['user']) . '&nbsp;&nbsp;&nbsp;' . (isset($_GET['nopage']) ? l(t('Show paged records'), 'admin/store/reports/stock') : l(t('Show all records'), 'admin/store/reports/stock', $options = array(
    'query' => 'nopage=1',
  ))) . '</div>';
  return $output;
}