Beispiel #1
0
 public static function getInstance(OrderedItem $item, ProductFile $file)
 {
     $instance = $item->getRelatedRecordSet('OrderedFile', select(eq('OrderedFile.productFileID', $file->getID())))->shift();
     if (!$instance) {
         $instance = self::getNewInstance($item, $file);
     }
     return $instance;
 }
 public static function createFromOrderedItem(OrderedItem $item)
 {
     if (!($product = $item->getProduct())) {
         return;
     }
     $instance = self::createFromArray($product->toArray());
     $instance->setCount($item->getCount());
     $instance->setItemPrice($item->getPrice());
     return $instance;
 }
Beispiel #3
0
 public function add_to_cart()
 {
     $request = $this->application->getRequest();
     $productID = $request->get('productID');
     $customerOrderID = $request->get('customerOrderID');
     $count = $request->get('count');
     if (!isset($customerOrderID) && intval($customerOrderID == 0)) {
         throw new Exception('Order ID is required');
     }
     $order = CustomerOrder::getInstanceById($customerOrderID);
     $order->load(true);
     $order->loadAll();
     //throw new Exception('order : ' . $order->getTotal(true));
     $product = Product::getInstanceByID($productID, true, true);
     $product->load(true);
     //$variations = !$product->parent->get() ? $product->getVariationData($this->application) : array('1','2');
     //throw new Exception('variation ' . json_encode($variations) . ' parent : ' . $product->getID() . ' productID ' . $productID);
     if (!$product->isAvailable()) {
         throw new Exception('Product ' . $productID . ' is not Available ');
     } else {
         if ($count < $product->getMinimumQuantity()) {
             $count = $product->getMinimumQuantity();
         }
         ActiveRecordModel::beginTransaction();
         $item = $order->addProduct($product, $count);
         if ($item instanceof OrderedItem) {
             if ($order->isMultiAddress->get()) {
                 $item->save();
             }
         }
         if ($product->parent->get()) {
             $order->mergeItems();
         } else {
             $item->save();
         }
         //$order->mergeItems();
         $order->getTotal(true);
         $order->totalAmount->set($order->getTotal(true));
         $order->getTaxAmount();
         $order->save(true);
         ActiveRecordModel::commit();
     }
     $response = new LiveCartSimpleXMLElement('<response datetime="' . date('c') . '"></response>');
     if ($item->getID() > 0) {
         $parser = $this->getParser();
         $apiFieldNames = $parser->getApiFieldNames();
         $selFilter = new ARSelectFilter();
         $selFilter->mergeCondition(new EqualsCond(new ARFieldHandle('OrderedItem', 'ID'), $item->getID()));
         $orderedItem = OrderedItem::getRecordSetArray('OrderedItem', $selFilter);
         while ($item = array_shift($orderedItem)) {
             $orderedItemXml = $response->addChild('ordered_item');
             foreach ($item as $k => $v) {
                 if (in_array($k, $apiFieldNames)) {
                     $orderedItemXml->addChild($k, htmlentities($v));
                 }
             }
         }
     }
     return new SimpleXMLResponse($response);
 }
Beispiel #4
0
 public function loadItems()
 {
     if (empty($this->items) && $this->isExistingRecord()) {
         $filter = new ARSelectFilter();
         $filter->setCondition(new EqualsCond(new ARFieldHandle('OrderedItem', 'shipmentID'), $this->getID()));
         foreach (OrderedItem::getRecordSet('OrderedItem', $filter, array('Product', 'Category', 'DefaultImage' => 'ProductImage')) as $item) {
             $this->items[] = $item;
         }
     }
 }
Beispiel #5
0
 public function getFormFieldName(OrderedItem $item, $option)
 {
     $optionID = $option instanceof ProductOption ? $option->getID() : $option['ID'];
     return 'itemOption_' . $item->getID() . '_' . $optionID;
 }
Beispiel #6
0
 private static function getARSelectFilterByOrderedItem(OrderedItem $item)
 {
     $filter = new ARSelectFilter();
     $filter->setCondition(new EqualsCond(new ARFieldHandle(__CLASS__, 'orderedItemID'), $item->getID()));
     return $filter;
 }
Beispiel #7
0
 public function getItemPrice(OrderedItem $item, $applyRounding = true, Currency $currency = null)
 {
     $currency = $currency ? $currency : $item->getCurrency();
     $currencyCode = $currency->getID();
     $price = $this->getPricingHandler()->getPriceByCurrencyCode($currencyCode)->getItemPrice($item, $applyRounding);
     return $price;
 }
Beispiel #8
0
 /**
  *  Get per item charge for particular item depending on its shipping class
  */
 public function getItemCharge(OrderedItem $item)
 {
     $product = $item->getProduct()->getParent();
     if (!$product->isLoaded()) {
         $product->load();
     }
     $class = $product->shippingClass->get();
     if (!$class) {
         return $this->perItemCharge->get();
     }
     $charge = $this->getValueByLang('perItemChargeClass', $class->getID());
     if (is_null($charge) || !strlen($charge)) {
         return $this->perItemCharge->get();
     }
     return $charge;
 }
Beispiel #9
0
 public function getDisplayTaxPrice($price, $product)
 {
     if (!$product) {
         return $price;
     }
     if (!$this->config->get('INCLUDE_BASE_TAXES')) {
         ClassLoader::import('application.model.order.OrderedItem');
         $price = OrderedItem::reduceBaseTaxes($price, $product);
     }
     /*
     else
     {
     	$price = $price * 1.25;
     }
     */
     return $price;
 }
Beispiel #10
0
 private function getGroupPrice(OrderedItem $item, $groupID, $rules)
 {
     $itemCnt = 0;
     // include other variations of the same product?
     if ($parent = $item->getProduct()->parent->get()) {
         $order = $item->customerOrder->get();
         foreach ($order->getShoppingCartItems() as $orderItem) {
             if ($orderItem->isVariationDiscountsSummed()) {
                 $orderProduct = $orderItem->product->get()->getParent();
                 if ($orderProduct->getID() == $parent->getID()) {
                     $itemCnt += $orderItem->count->get();
                 }
             }
         }
     }
     if (!$itemCnt) {
         $itemCnt = $item->count->get();
     }
     // include past orders
     $dateRange = $item->isPastOrdersInQuantityPrices();
     if (!is_null($dateRange)) {
         $from = $dateRange ? strtotime('now -' . (int) $dateRange . ' days') : 0;
         $orders = $item->customerOrder->get()->getBusinessRuleContext()->getPastOrdersBetween($from, time());
         $id = $item->getProduct()->getID();
         foreach ($orders as $order) {
             foreach ($order->getPurchasedItems() as $i) {
                 $product = $i->getProduct();
                 if ($id == $product['ID']) {
                     $itemCnt += $i->getCount();
                 }
             }
         }
     }
     return self::getProductGroupPrice($groupID, $rules, $itemCnt);
 }
Beispiel #11
0
 /**
  *  "Close" the order for modifications and fix its state
  *
  *  1) fix current product prices and total (so the total doesn't change if product prices change)
  *  2) save created shipments
  *
  *  @return CustomerOrder New order instance containing wishlist items
  */
 public function finalize($options = array())
 {
     if ($this->isFinalized->get() && empty($options['allowRefinalize'])) {
         return;
     }
     self::beginTransaction();
     $this->event('before-finalize');
     $this->loadAll();
     $currency = $this->getCurrency();
     foreach ($this->getShipments() as $shipment) {
         if ($shipment->isExistingRecord()) {
             $shipment->deleteRecordSet('ShipmentTax', new ARDeleteFilter());
         }
         // clone shipping addresses
         if ($shipment->shippingAddress->get()) {
             $shippingAddress = clone $shipment->shippingAddress->get();
             $shippingAddress->save();
             $shipment->shippingAddress->set($shippingAddress);
         }
         $shipment->order->set($this);
         $shipment->save();
     }
     $reserveProducts = self::getApplication()->isInventoryTracking();
     foreach ($this->getShoppingCartItems() as $item) {
         if (empty($options['customPrice'])) {
             $item->price->set($item->getSubTotalBeforeTax() / $item->getCount());
         }
         $item->name->set($item->getProduct()->getParent()->name->get());
         $item->setValueByLang('name', 'sku', $item->getProduct()->sku->get());
         $item->save();
         // create sub-items for bundled products
         if ($item->getProduct()->isBundle()) {
             foreach ($item->getProduct()->getBundledProducts() as $bundled) {
                 $bundledItem = OrderedItem::getNewInstance($this, $bundled->relatedProduct->get(), $bundled->getCount());
                 $bundledItem->parent->set($item);
                 $bundledItem->save();
             }
         }
         // reserve products if inventory is enabled
         if ($reserveProducts) {
             $item->reserve();
             $item->save();
         }
     }
     if (!$this->shippingAddress->get() && $this->user->get() && $this->user->get()->defaultShippingAddress->get() && $this->isShippingRequired()) {
         $this->shippingAddress->set($this->user->get()->defaultShippingAddress->get()->userAddress->get());
     }
     if (!$this->billingAddress->get() && $this->user->get() && $this->user->get()->defaultBillingAddress->get()) {
         $this->billingAddress->set($this->user->get()->defaultBillingAddress->get()->userAddress->get());
     }
     // clone billing/shipping addresses
     if (!$this->isFinalized->get()) {
         foreach (array('billingAddress', 'shippingAddress') as $address) {
             if ($this->{$address}->get()) {
                 $this->{$address}->get()->load();
                 $this->{$address}->get()->getSpecification();
                 $cloned = clone $this->{$address}->get();
                 $cloned->save();
                 $cloned->loadEav();
                 $this->{$address}->set($cloned);
             }
         }
     }
     // move wish list items to a separate order
     if ($this->getWishListItems()) {
         $wishList = CustomerOrder::getNewInstance($this->user->get());
         foreach ($this->getWishListItems() as $item) {
             $wishList->addItem($item);
         }
         $wishList->save();
     } else {
         $wishList = null;
     }
     // set order total
     $this->totalAmount->set($this->getTotal(true));
     // save shipment taxes
     foreach ($this->shipments as $shipment) {
         $shipment->save();
     }
     // save discounts
     foreach ($this->orderDiscounts as $discount) {
         $discount->save();
     }
     // @todo: remove the 0.99 multiplicator for currency conversion "tolerance" (a cent going missing when converting amounts between currencies back and forth)
     if (round($this->totalAmount->get(), 2) * 0.99 <= round($this->getPaidAmount(), 2)) {
         $this->isPaid->set(true);
     }
     $this->dateCompleted->set(new ARSerializableDateTime());
     $this->isFinalized->set(true);
     // @todo: fix order total calculation
     $shipments = $this->shipments;
     unset($this->shipments);
     if (!$this->invoiceNumber->get()) {
         $generator = InvoiceNumberGenerator::getGenerator($this);
         $saved = false;
         while (!$saved) {
             try {
                 $this->invoiceNumber->set($generator->getNumber());
                 $this->save();
                 $saved = true;
             } catch (SQLException $e) {
             }
         }
     }
     $this->event('after-finalize');
     self::commit();
     // @todo: see above
     $this->shipments = $shipments;
     // force updating array representation
     $this->resetArrayData();
     return $wishList;
 }
Beispiel #12
0
 public function registerSubItem(OrderedItem $item)
 {
     if (is_null($this->subItems)) {
         $this->subItems = new ARSet();
     }
     $id = $item->getID();
     foreach ($this->subItems as $subItem) {
         if ($subItem->getID() == $id) {
             return false;
         }
     }
     $this->subItems->add($item);
 }
 private function getItemResponse(OrderedItem $item)
 {
     $item->customerOrder->get()->load();
     $item->customerOrder->get()->loadItems();
     if ($image = $item->getProduct()->defaultImage->get()) {
         $image->load();
     }
     $this->application->getLocale()->translationManager()->loadFile('backend/Shipment');
     $response = new ActionResponse('item', $item->toArray());
     // load product options and variations
     $response->set('allOptions', ProductOption::loadOptionsForProductSet($item->getProduct()->getParent()->initSet()));
     $response->set('variations', $item->getProduct()->getParent()->initSet()->getVariationData($this->application));
     return $response;
 }
Beispiel #14
0
 /**
  *  "Close" the order for modifications and fix its state
  *
  *  1) fix current product prices and total (so the total doesn't change if product prices change)
  *  2) save created shipments
  *
  *  @return CustomerOrder New order instance containing wishlist items
  */
 public function finalize($options = array())
 {
     $rebillsLeft = 0;
     if ($this->isFinalized->get() && empty($options['allowRefinalize'])) {
         return;
     }
     self::beginTransaction();
     $this->event('before-finalize');
     $currency = $this->getCurrency();
     $this->loadAll();
     foreach ($this->getShipments() as $shipment) {
         if ($shipment->isExistingRecord()) {
             $shipment->deleteRecordSet('ShipmentTax', new ARDeleteFilter());
         }
         $shipment->order->set($this);
         $shipment->save();
         // clone shipping addresses
         if ($shipment->shippingAddress->get()) {
             $shippingAddress = clone $shipment->shippingAddress->get();
             $shippingAddress->save();
             $shipment->shippingAddress->set($shippingAddress);
         }
     }
     $reserveProducts = self::getApplication()->isInventoryTracking();
     $rebillsLeft = 0;
     $groupedRebillsLeft = array();
     $isFirstOrder = !$this->parentID->get();
     foreach ($this->getShoppingCartItems() as $item) {
         // workround for failing tests.
         //if (!empty($options['customPrice']))
         //{
         $item->price->set($item->getSubTotalBeforeTax() / $item->getCount());
         //}
         $item->name->set($item->getProduct()->getParent()->name->get());
         $item->setValueByLang('name', 'sku', $item->getProduct()->sku->get());
         $item->save();
         // create sub-items for bundled products
         if ($item->getProduct()->isBundle()) {
             foreach ($item->getProduct()->getBundledProducts() as $bundled) {
                 $bundledItem = OrderedItem::getNewInstance($this, $bundled->relatedProduct->get(), $bundled->getCount());
                 $bundledItem->parent->set($item);
                 $bundledItem->save();
             }
         }
         // reserve products if inventory is enabled
         if ($reserveProducts) {
             $item->reserve();
             $item->save();
         }
         if ($isFirstOrder) {
             $ri = RecurringItem::getInstanceByOrderedItem($item);
             if ($ri && $ri->isExistingRecord()) {
                 $rebillCount = $ri->rebillCount->get();
                 // also here recurring item grouping
                 $key = sprintf('%s_%s_%s', $ri->periodType->get(), $ri->periodLength->get(), $rebillCount === null ? 'NULL' : $rebillCount);
                 if ($rebillCount !== null) {
                     $groupedRebillsLeft[$key] = $rebillCount;
                 } else {
                     $groupedRebillsLeft[$key] = -1;
                     // -1 means infinite rebill count
                 }
                 $this->isRecurring->set(true);
                 // orders with at least one recurring billing plan must have isRecurring flag, if already not set.
             }
         } else {
             $rparentItem = $item->recurringParentID->get();
             if ($rparentItem) {
                 $ri = RecurringItem::getInstanceByOrderedItem($rparentItem, true);
                 $ri->reload();
                 // if was bulk update, then cached data are outdated.
                 if ($ri && $ri->isExistingRecord()) {
                     // are RecurringItems grouped?
                     // probably yes..
                     $rebillsLeft = $ri->rebillCount->get() - $ri->processedRebillCount->get();
                 }
             }
         }
     }
     if ($isFirstOrder) {
         foreach ($groupedRebillsLeft as $value) {
             if ($value == -1) {
                 $rebillsLeft = -1;
                 break;
             } else {
                 $rebillsLeft += $value;
             }
         }
     }
     if (!$this->shippingAddress->get() && $this->user->get() && $this->user->get()->defaultShippingAddress->get() && $this->isShippingRequired()) {
         $this->shippingAddress->set($this->user->get()->defaultShippingAddress->get()->userAddress->get());
     }
     if (!$this->billingAddress->get() && $this->user->get() && $this->user->get()->defaultBillingAddress->get()) {
         $this->billingAddress->set($this->user->get()->defaultBillingAddress->get()->userAddress->get());
     }
     // clone billing/shipping addresses
     if (!$this->isFinalized->get()) {
         foreach (array('billingAddress', 'shippingAddress') as $address) {
             if ($this->{$address}->get()) {
                 $this->{$address}->get()->load();
                 $this->{$address}->get()->getSpecification();
                 $cloned = clone $this->{$address}->get();
                 $cloned->save();
                 $cloned->loadEav();
                 $this->{$address}->set($cloned);
             }
         }
     }
     // move wish list items to a separate order
     if ($this->getWishListItems()) {
         $wishList = CustomerOrder::getNewInstance($this->user->get());
         foreach ($this->getWishListItems() as $item) {
             $wishList->addItem($item);
         }
         $wishList->save();
     } else {
         $wishList = null;
     }
     // set order total
     $this->totalAmount->set($this->getTotal(true));
     // save shipment taxes
     foreach ($this->shipments as $shipment) {
         $shipment->save();
     }
     // save discounts
     foreach ($this->orderDiscounts as $discount) {
         $discount->save();
     }
     // @todo: remove the 0.99 multiplicator for currency conversion "tolerance" (a cent going missing when converting amounts between currencies back and forth)
     if (round($this->totalAmount->get(), 2) * 0.99 <= round($this->getPaidAmount(), 2)) {
         $this->isPaid->set(true);
     }
     $this->dateCompleted->set(new ARSerializableDateTime());
     $this->isFinalized->set(true);
     // @todo: fix order total calculation
     $shipments = $this->shipments;
     unset($this->shipments);
     if (!$this->invoiceNumber->get()) {
         $generator = InvoiceNumberGenerator::getGenerator($this);
         $saved = false;
         while (!$saved) {
             try {
                 $this->invoiceNumber->set($generator->getNumber());
                 $this->save();
                 $saved = true;
             } catch (SQLException $e) {
             }
         }
     }
     if ($this->isRecurring->get()) {
         $changed = false;
         if (!strlen($this->startDate->get())) {
             $this->startDate->set(date('Y-m-d H:i:s', time()));
             $changed = true;
         }
         if ($rebillsLeft != $this->rebillsLeft->get()) {
             $this->rebillsLeft->set($rebillsLeft);
             $changed = true;
         }
         if ($changed) {
             $this->save();
         }
     }
     $this->event('after-finalize');
     self::commit();
     // @todo: see above
     $this->shipments = $shipments;
     // force updating array representation
     $this->resetArrayData();
     return $wishList;
 }
Beispiel #15
0
 public function set()
 {
     $request = $this->application->getRequest();
     $orderedItemID = $request->get('orderedItemID');
     $choiceID = $request->get('choiceID');
     $optionText = $request->get('optionText');
     if (!isset($orderedItemID)) {
         throw new Exception("Ordered item is required");
     }
     if (!isset($choiceID)) {
         throw new Exception("Choice item is required");
     }
     //throw new Exception(" response : " . $orderedItemID);
     //$orderItem = ActiveRecordModel::getInstanceByID('OrderedItem', $orderedItemID, ActiveRecordModel::LOAD_DATA, array('Product'));
     $orderItem = OrderedItem::getInstanceByID('OrderedItem', $orderedItemID, true, true);
     $orderItem->load(true);
     $orderItem->loadOptions();
     $product_option_choice = ProductOptionChoice::getInstanceByID($choiceID, true, true);
     $product_option_choice->load(true);
     if (!isset($product_option_choice)) {
         throw new Exception('Option not loaded');
     }
     $choice = $orderItem->addOptionChoice($product_option_choice);
     $orderItem->save();
     $response = new LiveCartSimpleXMLElement('<response datetime="' . date('c') . '"></response>');
     $response->addChild("message", "Success");
     if (isset($choice)) {
         $choice->load(true);
         if (isset($optionText)) {
             $choice->optionText->set($optionText);
             $choice->save();
         }
     }
     return new SimpleXMLResponse($response);
     //good
     /*$ordered_item = OrderedItemOption::getNewInstance($orderItem,$product_option_choice);
       $ordered_item->load(true);
       //$ordered_item->updatePriceDiff();
       $ordered_item->save();
       throw new Exception('is set --- ');*/
     /*$productOption = ProductOption::getInstanceByID($product_option_choice->optionID->get(), true);
             $productOption->load(true);
     
             $choice = $orderItem->getOptionChoice($productOption);
     
             if(isset($choice)) {
                 $choice->load(true);
                 $orderItem->removeOptionChoice($choice->choice->get());
                 $orderItem->save();
                 //$orderItem->removeOption($productOption);
                 //$orderItem->addOptionChoice($choice);
                 //$orderItem->save();
                 throw new Exception('is set');
                 //throw new Exception('is set ' . $choice->choice->get()->option->get()->getID());
             } else {
                 //$orderItem->addOption($productOption->option->get());
                 //$orderItem->addOptionChoice($productOption->getChoiceByID($product_option_choice->ID->get()));
                 //$orderItem->save();
     
                 $orderItem->addOptionChoice($product_option_choice);
                 $orderItem->save();
                 throw new Exception('is not set ');
             }*/
 }
Beispiel #16
0
 /**
  *  Import or update an ordered product
  */
 protected function set_OrderedItem_sku($instance, $value, $record, CsvImportProfile $profile)
 {
     if (!$value) {
         return;
     }
     $product = Product::getInstanceBySKU($value);
     if (!$product) {
         return;
     }
     $items = $instance->getItemsByProduct($product);
     // create initial shipment
     if (!$instance->getShipments()->size()) {
         $shipment = Shipment::getNewInstance($instance);
         $shipment->save();
     }
     // any particular shipment?
     $shipment = $item = null;
     if ($profile->isColumnSet('OrderedItem.shipment')) {
         // internal indexes are 0-based, but the import references are 1-based
         $shipmentNo = $this->getColumnValue($record, $profile, 'OrderedItem.shipment') - 1;
         if (is_numeric($this->getColumnValue($record, $profile, 'OrderedItem.shipment'))) {
             foreach ($instance->getShipments() as $key => $shipment) {
                 if ($key == $shipmentNo) {
                     break;
                 }
                 $shipment = null;
             }
             // create a new shipment
             if (!$shipment) {
                 $shipment = Shipment::getNewInstance($instance);
                 $shipment->save();
             }
             foreach ($items as $item) {
                 if ($item->shipment->get() == $shipment) {
                     break;
                 }
                 unset($item);
             }
         }
     }
     if (!$item) {
         $item = array_shift($items);
     }
     if (!$item) {
         $count = $this->getColumnValue($record, $profile, 'OrderedItem.count');
         $item = OrderedItem::getNewInstance($instance, $product, max(1, $count));
         $instance->addItem($item);
     }
     if ($profile->isColumnSet('OrderedItem.count')) {
         $count = $this->getColumnValue($record, $profile, 'OrderedItem.count');
         $item->count->set(max(1, $count));
     }
     if ($profile->isColumnSet('OrderedItem.price')) {
         $item->price->set($this->getColumnValue($record, $profile, 'OrderedItem.price'));
     }
     if (!$shipment) {
         $shipment = $instance->getShipments()->get(0);
     }
     $item->shipment->set($shipment);
     $item->save();
     $instance->finalize(array('customPrice' => true, 'allowRefinalize' => true));
 }