'date', '#title' => t('Start date'), '#default_value' => array( 'year' => format_date($start, 'custom', 'Y'), 'month' => format_date($start, 'custom', 'n'), 'day' => format_date($start, 'custom', 'j'), ), '#after_build' => array('_uc_coupon_date_range'), ); $form['end'] = array( '#type' => 'date', '#title' => t('End date'), '#default_value' => array( 'year' => format_date($end, 'custom', 'Y'), 'month' => format_date($end, 'custom', 'n'), 'day' => format_date($end, 'custom', 'j'), ), '#after_build' => array('_uc_coupon_date_range'), ); $form['status'] = array( '#type' => 'select', '#title' => t('Order statuses'), '#description' => t('Only orders with selected statuses will be included in the report.') . '
' . t('Hold Ctrl + click to select multiple statuses.'), '#options' => $options, '#default_value' => $statuses, '#multiple' => TRUE, '#size' => 5, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Display report'), ); return $form; } /** * Submit handler for uc_coupon_reports form. */ function uc_coupon_reports_form_submit($form, &$form_state) { $start = gmmktime(0, 0, 0, $form_state['values']['start']['month'], $form_state['values']['start']['day'], $form_state['values']['start']['year']); $end = gmmktime(23, 59, 59, $form_state['values']['end']['month'], $form_state['values']['end']['day'], $form_state['values']['end']['year']); $statuses = implode(',', array_keys($form_state['values']['status'])); $form_state['redirect'] = 'admin/store/reports/coupon/' . $start . '/' . $end . '/' . $statuses; } /** * Output coupon report. */ function uc_coupon_reports($start = NULL, $end = NULL, $statuses = NULL) { drupal_add_css(drupal_get_path('module', 'uc_coupon') . '/reports.css', array('type' => 'uc_coupon')); if (is_null($statuses)) { $statuses = variable_get('uc_reports_reported_statuses', array('completed')); } else { $statuses = explode(',', $statuses); } $output = drupal_render(drupal_get_form('uc_coupon_reports_form', $start, $end, $statuses)); if (isset($start) && isset($end)) { $result = db_query("SELECT co.cid, co.oid, co.value, co.code, o.order_total, o.created FROM {uc_coupons_orders} AS co LEFT JOIN {uc_orders} AS o ON (co.oid = o.order_id) WHERE o.created > :start AND o.created < :end AND o.order_status IN (:statuses) ORDER BY co.cid, o.created ASC", array(':start' => $start, ':end' => $end, ':statuses' => $statuses)); $total = 0; $row_header = array(t('Order #'), t('Purchase date'), t('Total'), t('Coupon value')); $last_cid = $orders_total = $coupons_total = 0; $data = ''; $num_uses = 0; $coupon_sale_amount = 0; $coupon_amount = 0; foreach ($result as $row) { // Display the table of coupons if this is the next set of coupons if ($row->cid != $last_cid) { if ($last_cid != 0) { $td[] = array('', '' . t('Uses: @total', array('@total' => $num_uses)) . '', '' . uc_currency_format($coupon_sale_amount) . '', '' . uc_currency_format($coupon_amount) . ''); $data .= theme('table', array('header' => $row_header, 'rows' => $td, 'attributes' => array('width' => '100%'))); } $td = array(); $num_uses = 0; $coupon_amount = 0; $coupon_sale_amount = 0; } // if this is the first coupon of the set display the header first if ($row->cid != $last_cid || $last_cid = 0) { $data .= '
' . t('Coupon code: !link', array('!link' => l($row->code, 'admin/store/coupons/' . $row->cid . '/edit'))) . '
'; } $td[] = array(l('#' . $row->oid, 'admin/store/orders/' . $row->oid), format_date($row->created, 'custom', variable_get('date_format_uc_store', 'm/d/Y')), uc_currency_format($row->order_total), uc_currency_format($row->value)); $num_uses++; $coupon_amount += $row->value; $coupon_sale_amount += $row->order_total; $last_cid = $row->cid; $orders_total += $row->order_total; $coupons_total += $row->value; $total++; } $td[] = array('', '' . t('Uses: @total', array('@total' => $num_uses)) . '', '' . uc_currency_format($coupon_sale_amount) . '', '' . uc_currency_format($coupon_amount) . ''); $data .= theme('table', array('header' => $row_header, 'rows' => $td, 'attributes' => array('width' => '100%'))); $output .= '

' . t('Coupon usage report') . '

'; $output .= $data; $output .= '
'; $output .= ''; $output .= ''; $output .= ''; $output .= '
' . t('Coupons used: @total', array('@total' => $total)) . '' . t('Orders total: @total', array('@total' => uc_currency_format($orders_total))) . '' . t('Coupons total: @total', array('@total' => uc_currency_format($coupons_total))) . '
'; } return $output; }