public function fetchSalesData($start = null, $end = null) { $model = (new \Shop\Models\Orders())->setState('filter.status_excludes', \Shop\Constants\OrderStatus::cancelled)->setState('filter.financial_status', array(\Shop\Constants\OrderFinancialStatus::paid, \Shop\Constants\OrderFinancialStatus::authorized)); if (!empty($start)) { $model->setState('filter.created_after', $start); } if (!empty($end)) { $model->setState('filter.created_before', $end); } $conditions = $model->conditions(); $agg = \Shop\Models\Orders::collection()->aggregate(array(array('$match' => $conditions), array('$group' => array('_id' => null, 'total' => array('$sum' => '$grand_total'), 'count' => array('$sum' => 1))))); //\Dsc\System::addMessage( \Dsc\Debug::dump($conditions) ); //\Dsc\System::addMessage( \Dsc\Debug::dump($start) ); //\Dsc\System::addMessage( \Dsc\Debug::dump($end) ); //\Dsc\System::addMessage( \Dsc\Debug::dump($agg) ); $total = 0; if (!empty($agg['ok']) && !empty($agg['result'])) { $total = (double) $agg['result'][0]['total']; } return (double) $total; }
/** * Get a paginated list of the customer's purchased products * * @param \Users\Models\Users $user * @param \Shop\Models\Products $product * @return boolean */ public static function purchasedProducts(\Users\Models\Users $user, $options = array()) { $options = $options + array('limit' => 10, 'offset' => 0, 'keyword' => null); $limit = $options['limit']; $offset = $options['offset']; $model = (new \Shop\Models\Orders())->setState('filter.keyword', $options['keyword'])->setState('filter.user', $user->id)->setState('filter.status_excludes', \Shop\Constants\OrderStatus::cancelled)->setState('filter.financial_status', array(\Shop\Constants\OrderFinancialStatus::paid, \Shop\Constants\OrderFinancialStatus::authorized, \Shop\Constants\OrderFinancialStatus::pending)); $conditions = $model->conditions(); $pipeline = array(array('$match' => $conditions), array('$sort' => array('metadata.created.time' => -1)), array('$unwind' => '$items'), array('$project' => array('_id' => 0, 'title' => '$items.product.title', 'slug' => '$items.product.slug', 'product_id' => '$items.product_id', 'variant_id' => '$items.variant_id', 'price' => '$items.price', 'quantity' => '$items.quantity', 'attribute_title' => '$items.attribute_title', 'sku' => '$items.sku', 'model_number' => '$items.model_number', 'order_created' => '$metadata.created', 'order_id' => '$_id')), array('$match' => array('product_id' => array('$nin' => array('', null))))); if (!empty($options['keyword'])) { $key = new \MongoRegex('/' . $options['keyword'] . '/i'); $pipeline[] = array('$match' => array('$or' => array(array('title' => $key), array('sku' => $key), array('order_id' => $key)))); } if (isset($options['is_reviewed']) && is_bool($options['is_reviewed'])) { // get the product_ids that have been reviewed $reviewed_product_ids = \Shop\Models\ProductReviews::collection()->distinct('product_id', array('user_id' => $user->id)); if ($options['is_reviewed']) { // Add an $in filter to the pipeline for product_id $pipeline[] = array('$match' => array('product_id' => array('$in' => $reviewed_product_ids))); } else { // Add an $nin filter to the pipeline for product_id $pipeline[] = array('$match' => array('product_id' => array('$nin' => $reviewed_product_ids))); } } $count_pipeline = $pipeline; $count_pipeline[] = array('$group' => array('_id' => null, 'count' => array('$sum' => 1))); $pipeline[] = array('$skip' => $offset * $limit); $pipeline[] = array('$limit' => $limit); $agg = \Shop\Models\Orders::collection()->aggregate($pipeline); $result = null; //\Dsc\System::addMessage(\Dsc\Debug::dump($agg)); if (!empty($agg['ok']) && !empty($agg['result'])) { $agg_count = \Shop\Models\Orders::collection()->aggregate($count_pipeline); $total = isset($agg_count['result'][0]['count']) ? $agg_count['result'][0]['count'] : \Shop\Models\Orders::collection()->count($conditions); $result = new \Dsc\Pagination($total, $limit); $items = array(); foreach ($agg['result'] as $doc) { if (empty($doc['product_id'])) { continue; } $item = (new \Shop\Models\Products())->setState('filter.id', $doc['product_id'])->getItem(); if (!empty($item->id)) { $item->order_item = $doc; if (empty($item->order_item['variant_id'])) { $item->order_item['variant_id'] = null; } $item->order_item['image'] = $item->variantImage($item->order_item['variant_id']); $items[] = $item; } } $result->items = $items; } return $result; }
/** * Calculates the total sales involving this coupon * during the specified time period * * @param string $refresh * @return number */ public function fetchCountSales($start = null, $end = null) { $model = (new \Shop\Models\Orders())->setState('filter.coupon_id', $this->id)->setState('filter.status_excludes', \Shop\Constants\OrderStatus::cancelled)->setState('filter.financial_status', array(\Shop\Constants\OrderFinancialStatus::paid, \Shop\Constants\OrderFinancialStatus::authorized)); if (!empty($start)) { $model->setState('filter.created_after', $start); } if (!empty($end)) { $model->setState('filter.created_before', $end); } $conditions = $model->conditions(); $total = \Shop\Models\Orders::collection()->count($conditions); return (double) $total; }