예제 #1
0
 public function info()
 {
     ClassLoader::importNow("application.helper.getDateFromString");
     $product = Product::getInstanceById($this->request->get('id'), ActiveRecord::LOAD_DATA, array('DefaultImage' => 'ProductImage', 'Manufacturer', 'Category'));
     $thisMonth = date('m');
     $lastMonth = date('Y-m', strtotime(date('m') . '/15 -1 month'));
     $periods = array('_last_1_h' => "-1 hours | now", '_last_3_h' => "-3 hours | now", '_last_6_h' => "-6 hours | now", '_last_12_h' => "-12 hours | now", '_last_24_h' => "-24 hours | now", '_last_3_d' => "-3 days | now", '_this_week' => "w:Monday | now", '_last_week' => "w:Monday ~ -1 week | w:Monday", '_this_month' => $thisMonth . "/1 | now", '_last_month' => $lastMonth . "-1 | " . $lastMonth . "/1", '_this_year' => "January 1 | now", '_last_year' => "January 1 last year | January 1", '_overall' => "now | now");
     $purchaseStats = array();
     $prevCount = 0;
     foreach ($periods as $key => $period) {
         list($from, $to) = explode(' | ', $period);
         $cond = new EqualsCond(new ARFieldHandle('OrderedItem', 'productID'), $product->getID());
         if ('now' != $from) {
             $cond->addAND(new EqualsOrMoreCond(new ARFieldHandle('CustomerOrder', 'dateCompleted'), getDateFromString($from)));
         }
         if ('now' != $to) {
             $cond->addAnd(new EqualsOrLessCond(new ARFieldHandle('CustomerOrder', 'dateCompleted'), getDateFromString($to)));
         }
         $f = new ARSelectFilter($cond);
         $f->mergeCondition(new EqualsCond(new ARFieldHandle('CustomerOrder', 'isFinalized'), true));
         $f->removeFieldList();
         $f->addField('SUM(OrderedItem.count)');
         $query = new ARSelectQueryBuilder();
         $query->setFilter($f);
         $query->includeTable('OrderedItem');
         $query->joinTable('CustomerOrder', 'OrderedItem', 'ID', 'customerOrderID');
         if (($count = array_shift(array_shift(ActiveRecordModel::getDataBySql($query->getPreparedStatement(ActiveRecord::getDBConnection()))))) && ($count > $prevCount || '_overall' == $key)) {
             $purchaseStats[$key] = $count;
         }
         if ($count > $prevCount) {
             $prevCount = $count;
         }
     }
     $response = new ActionResponse();
     $response->set('together', $product->getProductsPurchasedTogether(10));
     $response->set('product', $product->toArray());
     $response->set('purchaseStats', $purchaseStats);
     return $response;
 }
예제 #2
0
    public function updateStartAndEndDates(CustomerOrder $previousOrder, $recurringItems)
    {
        // calling this more than once for same record will end with catastrophe.
        // echo "\n--------------------------\n";
        foreach ($recurringItems as $recurringItem) {
            $recurringItemID = $recurringItem->getID();
            // anyone should work, because $recurringItems should be grouped by type and length. ID is required for getting period length in calendar days (done with sql).
            break;
            // echo $recurringItem->getID(), "\n";
        }
        // find end date (first order does not have endDate)
        //    add one day = start date,
        // find 2 end date periods
        //    new end date.
        $data = ActiveRecordModel::getDataBySql('SELECT
			TIMESTAMP(DATE( /*remove hh:mm:ss */
				ADDDATE(
					IF(TO_DAYS(co.endDate) IS NULL,' . self::sqlForEndDate('co.startDate') . ',
						co.endDate
					),
				INTERVAL 1 DAY)
			)) AS startDate,
			' . self::sqlForEndDate('co.startDate', 2) . ' as endDate
			FROM
				CustomerOrder co
				INNER JOIN RecurringItem ri ON ri.ID=' . $recurringItemID . '
			WHERE
				co.ID = ' . $previousOrder->getID());
        //
        if ($data && count($data) == 1) {
            $data = array_shift($data);
            $this->startDate->set($data['startDate']);
            $this->endDate->set($data['endDate']);
            return true;
        }
        return false;
    }