public function index()
 {
     $product = Product::getInstanceById($this->request->get('id'), ActiveRecord::LOAD_DATA, array('Category'));
     $product->category->get()->getPathNodeSet();
     $additional = $product->getAdditionalCategories();
     foreach ($additional as $category) {
         $category->getPathNodeSet();
     }
     $response = new ActionResponse('product', $product->toArray());
     if ($additional) {
         $response->set('categories', ARSet::buildFromArray($additional)->toArray());
     }
     return $response;
 }
Example #2
0
 public function getNextCustomerOrder()
 {
     if (!($data = $this->loadRecord('SELECT ' . $this->getTablePrefix() . 'orders.*, ' . $this->getTablePrefix() . 'customers.email AS userEmail FROM ' . $this->getTablePrefix() . 'orders LEFT JOIN ' . $this->getTablePrefix() . 'customers ON ' . $this->getTablePrefix() . 'orders.login='******'customers.login'))) {
         return null;
     }
     if (!($user = User::getInstanceByEmail($data['userEmail']))) {
         return $this->getNextCustomerOrder();
     }
     $order = CustomerOrder::getNewInstance($user);
     $order->currency->set(Currency::getInstanceById($this->getDefaultCurrency()));
     $order->dateCompleted->set($data['date']);
     // products
     foreach ($this->getDataBySql('SELECT * FROM ' . $this->getTablePrefix() . 'order_details WHERE orderid=' . $data['orderid']) as $prod) {
         try {
             $product = Product::getInstanceById($this->getRealId('Product', $prod['productid']), true);
             $order->addProduct($product, $prod['amount'], true);
             $item = array_shift($order->getItemsByProduct($product));
             $item->price->set($prod['price']);
         } catch (ARNotFoundException $e) {
             // the product no longer exists
         }
     }
     // addresses
     $order->shippingAddress->set($this->getUserAddress($data, 's_'));
     $order->billingAddress->set($this->getUserAddress($data, 'b_'));
     // assume that all orders are paid and shipped
     $order->status->set(CustomerOrder::STATUS_SHIPPED);
     $order->isPaid->set(true);
     $order->rawData = $data;
     return $order;
 }
Example #3
0
    public function getNextCustomerOrder()
    {
        if (!($data = $this->loadRecord('SELECT *, ' . $this->getTablePrefix() . 'orders.orders_id AS id, ' . $this->getTablePrefix() . 'orders_total.value FROM ' . $this->getTablePrefix() . 'orders
												LEFT JOIN ' . $this->getTablePrefix() . 'orders_total ON (' . $this->getTablePrefix() . 'orders.orders_id=' . $this->getTablePrefix() . 'orders_total.orders_id AND class="ot_shipping")
												LEFT JOIN ' . $this->getTablePrefix() . 'customers ON (' . $this->getTablePrefix() . 'orders.customers_id=' . $this->getTablePrefix() . 'customers.customers_id)
												WHERE ' . $this->getTablePrefix() . 'customers.customers_id IS NOT NULL'))) {
            return null;
        }
        $order = CustomerOrder::getNewInstance(User::getInstanceById($this->getRealId('User', $data['customers_id'])));
        $order->currency->set(Currency::getInstanceById($data['currency']));
        $order->dateCompleted->set($data['date_purchased']);
        // products
        $tax = 0;
        foreach ($this->getDataBySql('SELECT *, ' . $this->getTablePrefix() . 'orders_products.products_quantity AS quant FROM ' . $this->getTablePrefix() . 'orders_products LEFT JOIN ' . $this->getTablePrefix() . 'products ON (' . $this->getTablePrefix() . 'orders_products.products_id=' . $this->getTablePrefix() . 'products.products_id) WHERE ' . $this->getTablePrefix() . 'orders_id=' . $data['id'] . ' AND ' . $this->getTablePrefix() . 'products.products_id IS NOT NULL') as $prod) {
            $product = Product::getInstanceById($this->getRealId('Product', $prod['products_id']));
            if (!$prod['quant']) {
                continue;
            }
            $item = $order->addProduct($product, $prod['quant'], true);
            $item->price->set($prod['products_price']);
            $tax += $prod['products_tax'];
        }
        // addresses
        $order->shippingAddress->set($this->getUserAddress($data, 'delivery_'));
        $order->billingAddress->set($this->getUserAddress($data, 'billing_'));
        // assume that all orders are paid and shipped
        $order->status->set(CustomerOrder::STATUS_SHIPPED);
        $order->isPaid->set(true);
        $data['taxAmount'] = $tax;
        $order->rawData = $data;
        return $order;
    }
Example #4
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;
 }
Example #5
0
 public function save($forceOperation = null)
 {
     // update inventory
     $shipment = $this->shipment->get();
     if (!$shipment && $this->parent->get()) {
         $shipment = $this->parent->get()->shipment->get();
     }
     $order = $this->customerOrder->get();
     if ($shipment && $order->isFinalized->get() && !$order->isCancelled->get() && self::getApplication()->isInventoryTracking()) {
         $product = $this->getProduct();
         // changed product (usually a different variation)
         if ($this->product->isModified()) {
             // unreserve original item
             if ($orig = $this->product->getInitialValue()) {
                 if (is_string($orig)) {
                     $orig = Product::getInstanceById($orig, true);
                 }
                 $this->reserve(true, $orig);
                 $orig->save();
             }
             // reserve new item
             $this->reserve();
         }
         if ($this->reservedProductCount->get() > 0 && $shipment->status->get() == Shipment::STATUS_SHIPPED) {
             $this->removeFromInventory();
         } else {
             if (0 == $this->reservedProductCount->get()) {
                 if ($shipment->status->get() == Shipment::STATUS_RETURNED) {
                     $this->reservedProductCount->set($this->count->get());
                     $product->reservedCount->set($product->reservedCount->get() + $this->count->get());
                 } else {
                     $this->reserve();
                 }
             } else {
                 if ($this->count->isModified()) {
                     $delta = $this->count->get() - $this->reservedProductCount->get();
                     $this->reservedProductCount->set($this->count->get());
                     $product->reservedCount->set($product->reservedCount->get() + $delta);
                     $product->stockCount->set($product->stockCount->get() - $delta);
                 }
             }
         }
     }
     $ret = parent::save($forceOperation);
     // save options
     foreach ($this->removedChoices as $rem) {
         $rem->delete();
     }
     foreach ($this->optionChoices as $choice) {
         $choice->save();
     }
     // save sub-items for bundles
     if ($this->getProduct()->isBundle()) {
         foreach ($this->getSubItems() as $item) {
             $item->save();
         }
     }
     $this->getProduct()->save();
     $this->subItems = null;
     return $ret;
 }
 public function create()
 {
     $request = $this->getRequest();
     $query = $request->get('query');
     if (strlen($query)) {
         $products = $this->getProductsFromSearchQuery($query);
     } else {
         $products = new ARSet();
         $products->add(Product::getInstanceById((int) $this->request->get('productID'), true));
     }
     $saveResponse = array('errors' => array(), 'items' => array());
     $composite = new CompositeJSONResponse();
     $order = CustomerOrder::getInstanceByID((int) $this->request->get('orderID'), true);
     $order->loadAll();
     foreach ($products as $product) {
         if ($product->isDownloadable()) {
             $shipment = $order->getDownloadShipment();
         } else {
             if ((int) $this->request->get('shipmentID')) {
                 $shipment = Shipment::getInstanceById('Shipment', (int) $this->request->get('shipmentID'), true, array('Order' => 'CustomerOrder', 'ShippingService', 'ShippingAddress' => 'UserAddress', 'Currency'));
             }
         }
         if (empty($shipment)) {
             $shipment = $order->getShipments()->get(0);
         }
         if (!$shipment) {
             $shipment = Shipment::getNewInstance($order);
         }
         if (!$shipment->order->get()) {
             $shipment->order->set($order);
         }
         $history = new OrderHistory($order, $this->user);
         $existingItem = false;
         foreach ($shipment->getItems() as $item) {
             if ($item->getProduct() === $product) {
                 if (!$product->getOptions(true)) {
                     $existingItem = $item;
                 }
                 break;
             }
         }
         if ($existingItem) {
             $item = $existingItem;
             if ($product->isDownloadable()) {
                 return new JSONResponse(false, 'failure', $this->translate('_downloadable_item_already_exists_in_this_order'));
             } else {
                 $item->count->set($item->count->get() + 1);
             }
         } else {
             $currency = $shipment->getCurrency();
             $item = OrderedItem::getNewInstance($order, $product);
             $item->count->set(1);
             $item->price->set($currency->round($item->reduceBaseTaxes($product->getPrice($currency->getID()))));
             $order->addItem($item);
             $shipment->addItem($item);
             $shipment->save();
         }
         $resp = $this->save($item, $shipment, $existingItem ? true : false);
         if (array_key_exists('errors', $resp)) {
             $saveResponse['errors'] = array_merge($saveResponse['errors'], $resp['errors']);
         } else {
             if (array_key_exists('item', $resp)) {
                 $saveResponse['items'][] = $resp['item'];
             }
         }
     }
     // for each product
     if (count($saveResponse['errors']) == 0) {
         unset($saveResponse['errors']);
     }
     if (isset($saveResponse['errors'])) {
         $response = new JSONResponse(array('errors' => $validator->getErrorList()), 'failure', $this->translate('_unable_to_update_items_quantity'));
     } else {
         $response = new JSONResponse($saveResponse, 'success', $this->translate('_item_has_been_successfuly_saved'));
     }
     $composite->addResponse('data', $response, $this, 'create');
     $ids = array();
     foreach ($saveResponse['items'] as $item) {
         $ids[] = $item['ID'];
     }
     $composite->addAction('html', 'backend.orderedItem', 'items');
     $this->request->set('item_ids', implode(',', $ids));
     $history->saveLog();
     return $composite;
 }