/** * Handles orders list loading * * @author Jonathan Davis * @since 1.2.1 * * @return void **/ public function loader() { if (!current_user_can('shopp_orders')) { return; } $defaults = array('page' => false, 'deleting' => false, 'selected' => false, 'update' => false, 'newstatus' => false, 'pagenum' => 1, 'paged' => 1, 'per_page' => 20, 'start' => '', 'end' => '', 'status' => false, 's' => '', 'range' => '', 'startdate' => '', 'enddate' => ''); $args = array_merge($defaults, $_GET); extract($args, EXTR_SKIP); $url = add_query_arg(array_merge($_GET, array('page' => $this->Admin->pagename('orders'))), admin_url('admin.php')); if ($page == "shopp-orders" && !empty($deleting) && !empty($selected) && is_array($selected) && current_user_can('shopp_delete_orders')) { foreach ($selected as $selection) { $Purchase = new ShoppPurchase($selection); $Purchase->load_purchased(); foreach ($Purchase->purchased as $purchased) { $Purchased = new ShoppPurchased($purchased->id); $Purchased->delete(); } $Purchase->delete(); } if (count($selected) == 1) { $this->notice(__('Order deleted.', 'Shopp')); } else { $this->notice(sprintf(__('%d orders deleted.', 'Shopp'), count($selected))); } } $statusLabels = shopp_setting('order_status'); if (empty($statusLabels)) { $statusLabels = array(''); } $txnstatus_labels = Lookup::txnstatus_labels(); if ($update == "order" && !empty($selected) && is_array($selected)) { foreach ($selected as $selection) { $Purchase = new ShoppPurchase($selection); $Purchase->status = $newstatus; $Purchase->save(); } if (count($selected) == 1) { $this->notice(__('Order status updated.', 'Shopp')); } else { $this->notice(sprintf(__('%d orders updated.', 'Shopp'), count($selected))); } } $Purchase = new ShoppPurchase(); $offset = get_option('gmt_offset') * 3600; 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($paged); $start = $per_page * ($pagenum - 1); $where = array(); $joins = array(); if (!empty($status) || $status === '0') { $where[] = "status='" . sDB::escape($status) . "'"; } if (!empty($s)) { $s = stripslashes($s); $search = array(); if (preg_match_all('/(\\w+?)\\:(?="(.+?)"|(.+?)\\b)/', $s, $props, PREG_SET_ORDER) > 0) { foreach ($props as $query) { $keyword = sDB::escape(!empty($query[2]) ? $query[2] : $query[3]); switch (strtolower($query[1])) { case "txn": $search[] = "txnid='{$keyword}'"; break; case "company": $search[] = "company LIKE '%{$keyword}%'"; break; case "gateway": $search[] = "gateway LIKE '%{$keyword}%'"; break; case "cardtype": $search[] = "cardtype LIKE '%{$keyword}%'"; break; case "address": $search[] = "(address LIKE '%{$keyword}%' OR xaddress='%{$keyword}%')"; break; case "city": $search[] = "city LIKE '%{$keyword}%'"; break; case "province": case "state": $search[] = "state='{$keyword}'"; break; case "zip": case "zipcode": case "postcode": $search[] = "postcode='{$keyword}'"; break; case "country": $search[] = "country='{$keyword}'"; break; case "promo": case "discount": $meta_table = ShoppDatabaseObject::tablename(ShoppMetaObject::$table); $joins[$meta_table] = "INNER JOIN {$meta_table} AS m ON m.parent = o.id AND context='purchase'"; $search[] = "m.value LIKE '%{$keyword}%'"; break; case "product": $purchased = ShoppDatabaseObject::tablename(Purchased::$table); $joins[$purchased] = "INNER JOIN {$purchased} AS p ON p.purchase = o.id"; $search[] = "p.name LIKE '%{$keyword}%' OR p.optionlabel LIKE '%{$keyword}%' OR p.sku LIKE '%{$keyword}%'"; break; } } if (empty($search)) { $search[] = "(id='{$s}' OR CONCAT(firstname,' ',lastname) LIKE '%{$s}%')"; } $where[] = "(" . join(' OR ', $search) . ")"; } elseif (strpos($s, '@') !== false) { $where[] = "email='" . sDB::escape($s) . "'"; } else { $where[] = "(id='{$s}' OR CONCAT(firstname,' ',lastname) LIKE '%" . sDB::escape($s) . "%')"; } } if (!empty($starts) && !empty($ends)) { $where[] = "created BETWEEN '" . sDB::mkdatetime($starts) . "' AND '" . sDB::mkdatetime($ends) . "'"; } if (!empty($customer)) { $where[] = "customer=" . intval($customer); } $where = !empty($where) ? "WHERE " . join(' AND ', $where) : ''; $joins = join(' ', $joins); $countquery = "SELECT count(*) as total,SUM(IF(txnstatus IN ('authed','captured'),total,NULL)) AS sales,AVG(IF(txnstatus IN ('authed','captured'),total,NULL)) AS avgsale FROM {$Purchase->_table} AS o {$joins} {$where} ORDER BY o.created DESC LIMIT 1"; $this->ordercount = sDB::query($countquery, 'object'); $query = "SELECT o.* FROM {$Purchase->_table} AS o {$joins} {$where} ORDER BY created DESC LIMIT {$start},{$per_page}"; $this->orders = sDB::query($query, 'array', 'index', 'id'); $num_pages = ceil($this->ordercount->total / $per_page); if ($paged > 1 && $paged > $num_pages) { Shopp::redirect(add_query_arg('paged', null, $url)); } }
/** * Delete an order with a given ID. * * @since 1.4 * * @param string $id The ShoppPurchase ID to delete. * @return bool True if deleted successfully, false otherwise. **/ public function delete($id) { $Purchase = new ShoppPurchase($id); if (!$Purchase->exists()) { return false; } $Purchase->delete_purchased(); $Purchase->delete(); return true; }