uc_coupon.reports.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * @file
  4. * Discount Coupons reports pages.
  5. */
  6. /**
  7. * Coupon report options form.
  8. */
  9. function uc_coupon_reports_form($form, &$form_state, $start = NULL, $end = NULL, $statuses = NULL) {
  10. module_load_include('inc', 'uc_coupon', 'uc_coupon.admin');
  11. if (is_null($start)) {
  12. $start = REQUEST_TIME;
  13. }
  14. if (is_null($end)) {
  15. $end = REQUEST_TIME;
  16. }
  17. if (is_null($statuses)) {
  18. $statuses = variable_get('uc_reports_reported_statuses', array('completed'));
  19. }
  20. $options = array();
  21. foreach (uc_order_status_list() as $status) {
  22. $options[$status['id']] = $status['title'];
  23. }
  24. $form['start'] = array(
  25. '#type' => 'date',
  26. '#title' => t('Start date'),
  27. '#default_value' => array(
  28. 'year' => format_date($start, 'custom', 'Y'),
  29. 'month' => format_date($start, 'custom', 'n'),
  30. 'day' => format_date($start, 'custom', 'j'),
  31. ),
  32. '#after_build' => array('_uc_coupon_date_range'),
  33. );
  34. $form['end'] = array(
  35. '#type' => 'date',
  36. '#title' => t('End date'),
  37. '#default_value' => array(
  38. 'year' => format_date($end, 'custom', 'Y'),
  39. 'month' => format_date($end, 'custom', 'n'),
  40. 'day' => format_date($end, 'custom', 'j'),
  41. ),
  42. '#after_build' => array('_uc_coupon_date_range'),
  43. );
  44. $form['status'] = array(
  45. '#type' => 'select',
  46. '#title' => t('Order statuses'),
  47. '#description' => t('Only orders with selected statuses will be included in the report.') . '<br />' . t('Hold Ctrl + click to select multiple statuses.'),
  48. '#options' => $options,
  49. '#default_value' => $statuses,
  50. '#multiple' => TRUE,
  51. '#size' => 5,
  52. );
  53. $form['submit'] = array(
  54. '#type' => 'submit',
  55. '#value' => t('Display report'),
  56. );
  57. return $form;
  58. }
  59. /**
  60. * Submit handler for uc_coupon_reports form.
  61. */
  62. function uc_coupon_reports_form_submit($form, &$form_state) {
  63. $start = gmmktime(0, 0, 0, $form_state['values']['start']['month'], $form_state['values']['start']['day'], $form_state['values']['start']['year']);
  64. $end = gmmktime(23, 59, 59, $form_state['values']['end']['month'], $form_state['values']['end']['day'], $form_state['values']['end']['year']);
  65. $statuses = implode(',', array_keys($form_state['values']['status']));
  66. $form_state['redirect'] = 'admin/store/reports/coupon/' . $start . '/' . $end . '/' . $statuses;
  67. }
  68. /**
  69. * Output coupon report.
  70. */
  71. function uc_coupon_reports($start = NULL, $end = NULL, $statuses = NULL) {
  72. drupal_add_css(drupal_get_path('module', 'uc_coupon') . '/reports.css', array('type' => 'uc_coupon'));
  73. if (is_null($statuses)) {
  74. $statuses = variable_get('uc_reports_reported_statuses', array('completed'));
  75. }
  76. else {
  77. $statuses = explode(',', $statuses);
  78. }
  79. $output = drupal_render(drupal_get_form('uc_coupon_reports_form', $start, $end, $statuses));
  80. if (isset($start) && isset($end)) {
  81. $result = db_query("SELECT co.cid, co.oid, co.value, co.code, o.order_total, o.created FROM {uc_coupons_orders} AS co
  82. LEFT JOIN {uc_orders} AS o ON (co.oid = o.order_id)
  83. WHERE o.created > :start AND o.created < :end AND o.order_status IN (:statuses)
  84. ORDER BY co.cid, o.created ASC",
  85. array(':start' => $start, ':end' => $end, ':statuses' => $statuses));
  86. $total = 0;
  87. $row_header = array(t('Order #'), t('Purchase date'), t('Total'), t('Coupon value'));
  88. $last_cid = $orders_total = $coupons_total = 0;
  89. $data = '';
  90. $num_uses = 0;
  91. $coupon_sale_amount = 0;
  92. $coupon_amount = 0;
  93. foreach ($result as $row) {
  94. // Display the table of coupons if this is the next set of coupons
  95. if ($row->cid != $last_cid) {
  96. if ($last_cid != 0) {
  97. $td[] = array('', '<b>' . t('Uses: @total', array('@total' => $num_uses)) . '</b>', '<b>' . uc_currency_format($coupon_sale_amount) . '</b>', '<b>' . uc_currency_format($coupon_amount) . '</b>');
  98. $data .= theme('table', array('header' => $row_header, 'rows' => $td, 'attributes' => array('width' => '100%')));
  99. }
  100. $td = array();
  101. $num_uses = 0;
  102. $coupon_amount = 0;
  103. $coupon_sale_amount = 0;
  104. }
  105. // if this is the first coupon of the set display the header first
  106. if ($row->cid != $last_cid || $last_cid = 0) {
  107. $data .= '<div class="totals">' . t('Coupon code: !link', array('!link' => l($row->code, 'admin/store/coupons/' . $row->cid . '/edit'))) . '</div>';
  108. }
  109. $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));
  110. $num_uses++;
  111. $coupon_amount += $row->value;
  112. $coupon_sale_amount += $row->order_total;
  113. $last_cid = $row->cid;
  114. $orders_total += $row->order_total;
  115. $coupons_total += $row->value;
  116. $total++;
  117. }
  118. $td[] = array('', '<b>' . t('Uses: @total', array('@total' => $num_uses)) . '</b>', '<b>' . uc_currency_format($coupon_sale_amount) . '</b>', '<b>' . uc_currency_format($coupon_amount) . '</b>');
  119. $data .= theme('table', array('header' => $row_header, 'rows' => $td, 'attributes' => array('width' => '100%')));
  120. $output .= '<h2>' . t('Coupon usage report') . '</h2>';
  121. $output .= $data;
  122. $output .= '<br /><table width="100%"><tr>';
  123. $output .= '<td>' . t('Coupons used: @total', array('@total' => $total)) . '</td>';
  124. $output .= '<td>' . t('Orders total: @total', array('@total' => uc_currency_format($orders_total))) . '</td>';
  125. $output .= '<td>' . t('Coupons total: @total', array('@total' => uc_currency_format($coupons_total))) . '</td>';
  126. $output .= '</tr></table>';
  127. }
  128. return $output;
  129. }