/**
  * Orders admin flow handlers
  */
 function orders_list()
 {
     global $Shopp, $Orders;
     $db = DB::get();
     $defaults = array('page' => false, 'deleting' => false, 'selected' => false, 'update' => false, 'newstatus' => false, 'pagenum' => 1, 'per_page' => false, 'start' => '', 'end' => '', 'status' => false, 's' => '', 'range' => '', 'startdate' => '', 'enddate' => '');
     $args = array_merge($defaults, $_GET);
     extract($args, EXTR_SKIP);
     if (!current_user_can(SHOPP_USERLEVEL)) {
         wp_die(__('You do not have sufficient permissions to access this page.', 'Shopp'));
     }
     if ($page == "shopp-orders" && !empty($deleting) && !empty($selected) && is_array($selected)) {
         foreach ($selected as $selection) {
             $Purchase = new Purchase($selection);
             $Purchase->load_purchased();
             foreach ($Purchase->purchased as $purchased) {
                 $Purchased = new Purchased($purchased->id);
                 $Purchased->delete();
             }
             $Purchase->delete();
         }
     }
     $statusLabels = $this->Settings->get('order_status');
     if (empty($statusLabels)) {
         $statusLabels = array('');
     }
     $txnStatusLabels = array('PENDING' => __('Pending', 'Shopp'), 'CHARGED' => __('Charged', 'Shopp'), 'REFUNDED' => __('Refunded', 'Shopp'), 'VOID' => __('Void', 'Shopp'));
     if ($update == "order" && !empty($selected) && is_array($selected)) {
         foreach ($selected as $selection) {
             $Purchase = new Purchase($selection);
             $Purchase->status = $newstatus;
             $Purchase->save();
         }
     }
     $Purchase = new Purchase();
     if (!empty($start)) {
         $startdate = $start;
         list($month, $day, $year) = explode("/", $startdate);
         $starts = mktime(0, 0, 0, $month, $day, $year);
     }
     if (!empty($end)) {
         $enddate = $end;
         list($month, $day, $year) = explode("/", $enddate);
         $ends = mktime(23, 59, 59, $month, $day, $year);
     }
     $pagenum = absint($pagenum);
     if (empty($pagenum)) {
         $pagenum = 1;
     }
     if (!$per_page || $per_page < 0) {
         $per_page = 20;
     }
     $start = $per_page * ($pagenum - 1);
     $where = '';
     if (!empty($status) || $status === '0') {
         $where = "WHERE status='{$status}'";
     }
     if (!empty($s)) {
         $s = stripslashes($s);
         if (preg_match_all('/(\\w+?)\\:(?="(.+?)"|(.+?)\\b)/', $s, $props, PREG_SET_ORDER) > 0) {
             foreach ($props as $search) {
                 $keyword = !empty($search[2]) ? $search[2] : $search[3];
                 switch (strtolower($search[1])) {
                     case "txn":
                         $where .= (empty($where) ? "WHERE " : " AND ") . "transactionid='{$keyword}'";
                         break;
                     case "gateway":
                         $where .= (empty($where) ? "WHERE " : " AND ") . "gateway LIKE '%{$keyword}%'";
                         break;
                     case "cardtype":
                         $where .= (empty($where) ? "WHERE " : " AND ") . "cardtype LIKE '%{$keyword}%'";
                         break;
                     case "address":
                         $where .= (empty($where) ? "WHERE " : " AND ") . "(address LIKE '%{$keyword}%' OR xaddress='%{$keyword}%')";
                         break;
                     case "city":
                         $where .= (empty($where) ? "WHERE " : " AND ") . "city LIKE '%{$keyword}%'";
                         break;
                     case "province":
                     case "state":
                         $where .= (empty($where) ? "WHERE " : " AND ") . "state='{$keyword}'";
                         break;
                     case "zip":
                     case "zipcode":
                     case "postcode":
                         $where .= (empty($where) ? "WHERE " : " AND ") . "postcode='{$keyword}'";
                         break;
                     case "country":
                         $where .= (empty($where) ? "WHERE " : " AND ") . "country='{$keyword}'";
                         break;
                 }
             }
             if (empty($where)) {
                 $where .= (empty($where) ? "WHERE " : " AND ") . " (id='{$s}' OR CONCAT(firstname,' ',lastname) LIKE '%{$s}%')";
             }
         } elseif (strpos($s, '@') !== false) {
             $where .= (empty($where) ? "WHERE " : " AND ") . " email='{$s}'";
         } else {
             $where .= (empty($where) ? "WHERE " : " AND ") . " (id='{$s}' OR CONCAT(firstname,' ',lastname) LIKE '%{$s}%')";
         }
     }
     if (!empty($starts) && !empty($ends)) {
         $where .= (empty($where) ? "WHERE " : " AND ") . ' (UNIX_TIMESTAMP(created) >= ' . $starts . ' AND UNIX_TIMESTAMP(created) <= ' . $ends . ')';
     }
     $ordercount = $db->query("SELECT count(*) as total,SUM(total) AS sales,AVG(total) AS avgsale FROM {$Purchase->_table} {$where} ORDER BY created DESC");
     $query = "SELECT * FROM {$Purchase->_table} {$where} ORDER BY created DESC LIMIT {$start},{$per_page}";
     $Orders = $db->query($query, AS_ARRAY);
     $num_pages = ceil($ordercount->total / $per_page);
     $page_links = paginate_links(array('base' => add_query_arg('pagenum', '%#%'), 'format' => '', 'total' => $num_pages, 'current' => $pagenum));
     $ranges = array('all' => __('Show All Orders', 'Shopp'), 'today' => __('Today', 'Shopp'), 'week' => __('This Week', 'Shopp'), 'month' => __('This Month', 'Shopp'), 'quarter' => __('This Quarter', 'Shopp'), 'year' => __('This Year', 'Shopp'), 'yesterday' => __('Yesterday', 'Shopp'), 'lastweek' => __('Last Week', 'Shopp'), 'last30' => __('Last 30 Days', 'Shopp'), 'last90' => __('Last 3 Months', 'Shopp'), 'lastmonth' => __('Last Month', 'Shopp'), 'lastquarter' => __('Last Quarter', 'Shopp'), 'lastyear' => __('Last Year', 'Shopp'), 'lastexport' => __('Last Export', 'Shopp'), 'custom' => __('Custom Dates', 'Shopp'));
     $exports = array('tab' => __('Tab-separated.txt', 'Shopp'), 'csv' => __('Comma-separated.csv', 'Shopp'), 'xls' => __('Microsoft&reg; Excel.xls', 'Shopp'), 'iif' => __('Intuit&reg; QuickBooks.iif', 'Shopp'));
     $formatPref = $Shopp->Settings->get('purchaselog_format');
     if (!$formatPref) {
         $formatPref = 'tab';
     }
     $columns = array_merge(Purchase::exportcolumns(), Purchased::exportcolumns());
     $selected = $Shopp->Settings->get('purchaselog_columns');
     if (empty($selected)) {
         $selected = array_keys($columns);
     }
     include "{$this->basepath}/core/ui/orders/orders.php";
 }