function uc_csv_generate_report in Ubercart CSV 7.2
1 call to uc_csv_generate_report()
- uc_csv_select_report_to_export_submit in ./
uc_csv.module - Put together an export based on the report id
File
- ./
uc_csv.module, line 647
Code
function uc_csv_generate_report($report) {
// If we have specified that only certain status levels are eligible for export, then
// assign them here so we know what they are later
$statuses = array();
foreach (unserialize($report->statuses) as $key => $value) {
if ($value != '0') {
$statuses[] = $value;
}
}
$selects = array();
$country = array();
$zone = array();
$headers = array(
'order_id',
'last_name',
'first_name',
'email_address',
'order_total',
'product_count',
);
/* cache the list of countries and states in memory so we don't have to do repeated lookups */
$result = db_query("SELECT country_id,country_name FROM {uc_countries}");
while ($sdata = $result
->fetchObject()) {
$country[$sdata->country_id] = $sdata->country_name;
}
$result = db_query("SELECT zone_id,zone_code FROM {uc_zones}");
$date_range = NULL;
if (isset($report->start_date) && $report->start_date) {
$start_date = strtotime($report->start_date);
$date_range = 'AND created >= ' . $start_date . ' ';
}
if (isset($report->end_date) && $report->end_date) {
$end_date = strtotime($report->end_date);
if (!empty($date_range)) {
$date_range .= 'AND created <= ' . $end_date . ' ';
}
else {
$date_range = 'AND created <= ' . $end_date . ' ';
}
}
while ($sdata = $result
->fetchObject()) {
$zone[$sdata->zone_id] = $sdata->zone_code;
}
if ($report->shipping_address == 1) {
$selects[] = 'o.delivery_first_name,o.delivery_last_name,o.delivery_phone,o.delivery_company,o.delivery_street1,o.delivery_street2,o.delivery_city,o.delivery_zone,o.delivery_postal_code,o.delivery_country';
$headers = array_merge($headers, array(
'shipping_first_name',
'shipping_last_name',
'shipping_phone',
'shipping_company',
'shipping_street1',
'shipping_street2',
'shipping_city',
'shipping_state',
'shipping_zipcode',
'shipping_country',
));
}
if ($report->billing_address == 1) {
$selects[] = 'o.billing_phone,o.billing_company,o.billing_street1,o.billing_street2,o.billing_city,o.billing_zone,o.billing_postal_code,o.billing_country';
$headers = array_merge($headers, array(
'billing_phone',
'billing_company',
'billing_street1',
'billing_street2',
'billing_city',
'billing_state',
'billing_zipcode',
'billing_country',
));
}
if ($report->products == 1) {
$headers = array_merge($headers, array(
'products',
'tax',
'shipping',
'weight',
));
}
if ($report->orderby == 'orderid') {
$orderby = 'o.order_id ASC';
}
elseif ($report->orderby == 'last_name') {
$orderby = 'o.billing_last_name ASC';
}
else {
$orderby = 'o.order_id ASC, o.billing_last_name ASC';
}
if (count($selects) > 0) {
$sel = ',' . join(',', $selects);
}
else {
$sel = NULL;
}
/* reports may have been trackable at one time and then switched to non-trackable. as such we */
/* need to track the state of the report. if it is not trackable, then set last_order_id to */
/* 0 so that we get all the orders */
if (isset($report->track) && $report->track < 1) {
$report->last_order_id = 0;
}
$complete_order = array();
$result = db_query("SELECT o.order_id, o.billing_last_name, o.billing_first_name, o.primary_email,o.order_total, o.product_count" . $sel . "\n FROM {uc_orders} o\n WHERE o.order_id > :order_id && o.order_status IN ('" . join("','", $statuses) . "') " . $date_range . "\n ORDER BY " . $orderby, array(
':order_id' => $report->last_order_id,
));
while ($order = $result
->fetchObject()) {
/* only deal with the billing stuff if it was selected as part of our order configuration */
if ($report->shipping_address == 1) {
$order->delivery_zone = $zone[$order->delivery_zone];
$order->delivery_country = $country[$order->delivery_country];
}
/* only deal with the billing stuff if it was selected as part of our order configuration */
if ($report->billing_address == 1) {
$order->billing_zone = $zone[$order->billing_zone];
$order->billing_country = $country[$order->billing_country];
}
if ($report->products == 1) {
$product = NULL;
$calc_attribute = array();
$total_weight = 0;
$presults = db_query("SELECT model, title, qty, price, data, weight\n FROM {uc_order_products} \n WHERE order_id=:order_id", array(
':order_id' => $order->order_id,
));
while ($pdata = $presults
->fetchObject()) {
$total_weight += $pdata->weight;
$data = unserialize($pdata->data);
if (isset($data['attributes']) && is_array($data['attributes'])) {
foreach ($data['attributes'] as $key => $attribute) {
foreach ($attribute as $display_attribute) {
$calc_attribute[] = $key . ": " . $display_attribute;
}
}
}
if (count($calc_attribute) > 0) {
$title = $pdata->title . " - " . join(',', $calc_attribute);
}
else {
$title = $pdata->title;
}
$product .= $pdata->qty . ' - ' . $title . ' (' . $pdata->model . '): $' . number_format($pdata->price, 2) . 'ea.';
}
$order->products = $product;
/* now we need to get the shipping and sales tax data for this order */
/* we will do this in two queries for now - first we do tax */
$stResult = db_query("SELECT amount AS sales_tax\n FROM {uc_order_line_items}\n WHERE order_id = :order_id\n AND type=:type", array(
':order_id' => $order->order_id,
':type' => 'tax',
));
$stdata = $stResult
->fetchObject();
$order->sales_tax = isset($stdata->sales_tax) ? $stdata->sales_tax : "N/A";
/* use the same sort of query to get the shipping amount */
$stResult = db_query("SELECT amount AS shipping\n FROM {uc_order_line_items}\n WHERE order_id = :order_id\n AND type=:type", array(
':order_id' => $order->order_id,
':type' => 'shipping',
));
$stdata = $stResult
->fetchObject();
$order->shipping = isset($stdata->shipping) ? $stdata->shipping : "N/A";
// Assign our total weight
$order->weight = $total_weight;
}
$complete_order[] = (array) $order;
}
return array(
'order_data' => $complete_order,
'headers' => $headers,
);
}