Exemplo n.º 1
0
 public function __construct(CustomerOrder $order, Currency $currency)
 {
     parent::__construct();
     $this->order = $order;
     $order->loadAll();
     // billing address
     if ($address = $order->billingAddress->get()) {
         $fields = array('firstName', 'lastName', 'companyName', 'phone', 'city', 'postalCode', 'countryID' => 'country');
         foreach ($fields as $key => $field) {
             $addressField = is_numeric($key) ? $field : $key;
             $this->{$field}->set($address->{$addressField}->get());
         }
         $this->state->set($this->getStateValue($address));
         $this->address->set($address->address1->get() . ' ' . $address->address2->get());
     }
     // shipping address
     $address = $order->shippingAddress->get();
     if (!$address) {
         $address = $order->billingAddress->get();
     }
     if ($address) {
         foreach ($fields as $key => $field) {
             $addressField = is_numeric($key) ? $field : $key;
             $field = 'shipping' . ucfirst($field);
             $this->{$field}->set($address->{$addressField}->get());
         }
         $this->shippingState->set($this->getStateValue($address));
         $this->shippingAddress->set($address->address1->get() . ' ' . $address->address2->get());
     }
     // amount
     $order->currency->set($currency);
     $this->amount->set(round($order->getDueAmount(), 2));
     $this->currency->set($currency->getID());
     // transaction identification
     $this->invoiceID->set($order->getID());
     if (isset($_SERVER['REMOTE_ADDR'])) {
         $this->ipAddress->set($_SERVER['REMOTE_ADDR']);
     }
     // customer identification
     if ($order->user->get()) {
         $order->user->get()->load();
         $this->shippingEmail->set($order->user->get()->email->get());
         $this->email->set($order->user->get()->email->get());
         $this->clientID->set($order->user->get()->getID());
     }
     // order details
     // load variation data
     $variations = new ProductSet();
     foreach ($order->getShoppingCartItems() as $item) {
         if ($item->product->get() && $item->product->get()->parent->get()) {
             $variations->unshift($item->product->get());
         }
     }
     if ($variations->size()) {
         $variations->loadVariations();
     }
     foreach ($order->getShoppingCartItems() as $item) {
         $product = $item->getProduct();
         $variations = array();
         foreach ($product->getRegisteredVariations() as $variation) {
             $variations[] = $variation->getValueByLang('name');
         }
         $ri = RecurringItem::getInstanceByOrderedItem($item);
         if ($ri && $ri->isExistingRecord()) {
             $ri->load();
         } else {
             $ri = null;
         }
         $this->addLineItem($product->getName() . ($variations ? ' (' . implode(' / ', $variations) . ')' : ''), $item->getPrice(false), $item->count->get(), $product->sku->get(), $ri);
     }
     if ($discount = $order->getFixedDiscountAmount()) {
         $this->addLineItem(CustomerOrder::getApplication()->translate('_discount'), $discount * -1, 1, 'discount');
     }
     foreach ($order->getShipments() as $shipment) {
         if ($rate = $shipment->getSelectedRate()) {
             $rate = $rate->toArray();
             $name = empty($rate['ShippingService']['name_lang']) ? $rate['serviceName'] : $rate['ShippingService']['name_lang'];
             $this->addLineItem($name, $shipment->getShippingTotalBeforeTax(), 1, 'shipping');
         }
     }
     if ($taxes = $order->getTaxBreakdown()) {
         foreach ($taxes as $id => $amount) {
             $tax = Tax::getInstanceById($id, true);
             $this->addLineItem($tax->getValueByLang('name', null), $amount, 1, 'tax');
         }
     }
 }
Exemplo n.º 2
0
 /**
  *	Determines if the necessary steps have been completed, so the order could be finalized
  *
  *	@return RedirectResponse
  *	@return ActionRedirectResponse
  *	@return false
  */
 protected function validateOrder(CustomerOrder $order, $step = 0)
 {
     if ($order->isFinalized->get()) {
         return false;
     }
     // no items in shopping cart
     if (!count($order->getShoppingCartItems())) {
         if ($this->request->isValueSet('return')) {
             return new RedirectResponse($this->router->createUrlFromRoute($this->request->get('return')));
         } else {
             return new ActionRedirectResponse('index', 'index');
         }
     }
     // order is not orderable (too few/many items, etc.)
     $isOrderable = $order->isOrderable(true, false);
     if (!$isOrderable || $isOrderable instanceof OrderException) {
         return new ActionRedirectResponse('order', 'index');
     }
     $valStep = $this->config->get('CHECKOUT_CUSTOM_FIELDS');
     $validateFields = 'CART_PAGE' == $valStep || 'BILLING_ADDRESS_STEP' == $valStep && self::STEP_ADDRESS <= $step || 'SHIPPING_ADDRESS_STEP' == $valStep && (self::STEP_ADDRESS == $step && 'shipping' == $this->request->get('step') || self::STEP_ADDRESS < $step) || 'SHIPPING_METHOD_STEP' == $valStep && self::STEP_SHIPPING < $step;
     $isOrderable = $order->isOrderable(true, $validateFields);
     // custom fields selected in cart page?
     if ('CART_PAGE' == $valStep && !$isOrderable) {
         return new ActionRedirectResponse('order', 'index');
     }
     // shipping address selected
     if ($step >= self::STEP_SHIPPING) {
         if (!$order->shippingAddress->get() && $order->isShippingRequired() && !$order->isMultiAddress->get() || !$order->billingAddress->get() || !$isOrderable) {
             return new ActionRedirectResponse('checkout', 'selectAddress', $this->request->get('step') ? array('step' => $this->request->get('step')) : null);
         }
     }
     // shipping method selected
     if ($step >= self::STEP_PAYMENT && $order->isShippingRequired() || !$isOrderable) {
         foreach ($order->getShipments() as $shipment) {
             if (!$shipment->getSelectedRate() && $shipment->isShippable()) {
                 return new ActionRedirectResponse('checkout', 'shipping');
             }
         }
     }
     return false;
 }