You are here

function __prepare_commerce_reports_products_qty_monthly_line_data in Commerce Reporting 7

1 call to __prepare_commerce_reports_products_qty_monthly_line_data()
commerce_reports_handler_field_products_qty_monthly_line::render in includes/views/handlers/commerce_reports_handler_field_products_qty_monthly_line.inc
Render the field.

File

./commerce_reports.module, line 99
Defines additional menu item and order reports functionality.

Code

function __prepare_commerce_reports_products_qty_monthly_line_data($view_result) {
  $data = array();
  foreach ($view_result as $order_details) {
    $order = commerce_order_load($order_details->order_id);
    $wrapper = entity_metadata_wrapper('commerce_order', $order);
    $currency_code = $wrapper->commerce_order_total->currency_code
      ->value();
    $amount = commerce_currency_amount_to_decimal($wrapper->commerce_order_total->amount
      ->value(), $currency_code);
    $month = date("M Y", $order->created);
    if (!isset($data[$month])) {
      $data[$month] = array();
    }
    else {
      $data[$month] += array();
    }
    foreach ($wrapper->commerce_line_items as $delta => $line_item_wrapper) {

      //print $line_item_wrapper->commerce_product->title->value();

      //print '<br />';
      switch ($line_item_wrapper->type
        ->value()) {
        case 'shipping':

          //$line_item_wrapper->line_item_label->value();
          break;
        case 'product':
        default:
          $title = $line_item_wrapper->commerce_product->title
            ->value();
          $qty = round($line_item_wrapper->quantity
            ->value());
          $data[$month]['qty'][$title] = isset($data[$month]['qty'][$title]) ? $data[$month]['qty'][$title] + $qty : $qty;
          break;
      }
    }
  }
  $seen = array();
  $data['cols'] = array();
  foreach ($data as $k => $d) {
    if ($k == 'cols') {
      continue;
    }
    foreach ($d['qty'] as $b => $v) {
      if (!isset($seen[$b])) {
        $data['cols'][] = $b;
        $seen[$b] = 1;
      }
    }
  }
  sort($data['cols']);
  return $data;
}