Esempio n. 1
0
 /**
  * Fires an unstock order event for a purchase to deduct stock from inventory
  *
  * @author Jonathan Davis
  * @since 1.2.1
  *
  * @return void
  **/
 function unstock(AuthedOrderEvent $Event)
 {
     if (!shopp_setting_enabled('inventory')) {
         return false;
     }
     $Purchase = ShoppPurchase();
     if (!isset($Purchase->id) || empty($Purchase->id) || $Event->order != $Purchase->id) {
         $Purchase = new ShoppPurchase($Event->order);
     }
     if ($Purchase->did('unstock')) {
         return true;
     }
     // Unstock already occurred, do nothing
     $Purchase->load_purchased();
     // Reload purchased to ensure we have inventory status
     if (!$Purchase->stocked) {
         return false;
     }
     shopp_add_order_event($Purchase->id, 'unstock');
 }
Esempio n. 2
0
 /**
  * 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));
     }
 }
Esempio n. 3
0
 public function save_item()
 {
     if (false === $this->form('save-item') || false === ($lineid = $this->form('lineid'))) {
         return;
     }
     $Purchase = new ShoppPurchase($this->form('id'));
     if (!$Purchase->exists()) {
         return;
     }
     $new = '' === $lineid;
     $name = $this->form('itemname');
     if ($this->form('product')) {
         list($productid, $priceid) = explode('-', $this->form('product'));
         $Product = new ShoppProduct($productid);
         $Price = new ShoppPrice($priceid);
         $name = $Product->name;
         if (Shopp::__('Price & Delivery') != $Price->label) {
             $name .= ": {$Price->label}";
         }
     }
     // Create a cart representation of the order to recalculate order totals
     $Cart = new ShoppCart();
     $taxcountry = $Purchase->country;
     $taxstate = $Purchase->state;
     if (!empty($Purchase->shipcountry) && !empty($Purchase->shipstate)) {
         $taxcountry = $Purchase->shipcountry;
         $taxstate = $Purchase->shipstate;
     }
     ShoppOrder()->Tax->location($taxcountry, $taxstate);
     if ($new) {
         $NewLineItem = new ShoppPurchased();
         $NewLineItem->purchase = $Purchase->id;
         $Purchase->purchased[] = $NewLineItem;
     }
     foreach ($Purchase->purchased as &$Purchased) {
         $CartItem = new ShoppCartItem($Purchased);
         if ($Purchased->id == $lineid || $new && empty($Purchased->id)) {
             if (!empty($_POST['product'])) {
                 list($CartItem->product, $CartItem->priceline) = explode('-', $this->form('product'));
             } elseif (!empty($_POST['id'])) {
                 list($CartItem->product, $CartItem->priceline) = explode('-', $this->form('id'));
             }
             $CartItem->name = $name;
             $CartItem->unitprice = Shopp::floatval($this->form('unitprice'));
             $Cart->additem((int) $this->form('quantity'), $CartItem);
             $CartItem = $Cart->get($CartItem->fingerprint());
             $Purchased->name = $CartItem->name;
             $Purchased->product = $CartItem->product;
             $Purchased->price = $CartItem->priceline;
             $Purchased->quantity = $CartItem->quantity;
             $Purchased->unitprice = $CartItem->unitprice;
             $Purchased->total = $CartItem->total;
             $Purchased->save();
         } else {
             $Cart->additem($CartItem->quantity, $CartItem);
         }
         $this->notice(Shopp::__('Updates saved.'));
     }
     $Cart->Totals->register(new OrderAmountShipping(array('id' => 'cart', 'amount' => $Purchase->freight)));
     $Purchase->total = $Cart->total();
     $Purchase->subtotal = $Cart->total('order');
     $Purchase->discount = $Cart->total('discount');
     $Purchase->tax = $Cart->total('tax');
     $Purchase->freight = $Cart->total('shipping');
     $Purchase->save();
     $Purchase->load_purchased();
 }