示例#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');
         }
     }
 }
示例#2
0
 /**
  *  Creates an array representation of the shopping cart
  */
 public function toArray($options = array())
 {
     $currency = $this->getCurrency();
     $id = $currency->getID();
     if (is_array($this->orderedItems)) {
         foreach ($this->orderedItems as $item) {
             if (!$item->getProduct()->isPricingLoaded()) {
                 if (!isset($products)) {
                     $products = new ARSet();
                 }
                 $products->unshift($item->getProduct());
             }
         }
     }
     $array = parent::toArray();
     $array['cartItems'] = array();
     $array['wishListItems'] = array();
     if (is_array($this->orderedItems)) {
         foreach ($this->orderedItems as $item) {
             if ($item->isSavedForLater->get()) {
                 $array['wishListItems'][] = $item->toArray();
             } else {
                 $array['cartItems'][] = $item->toArray();
             }
         }
     }
     $array['basketCount'] = $this->getShoppingCartItemCount();
     $array['wishListCount'] = $this->getWishListItemCount();
     // shipments
     $array['shipments'] = array();
     if ($this->shipments) {
         foreach ($this->shipments as $shipment) {
             if (count($shipment->getItems())) {
                 $array['shipments'][] = $shipment->toArray();
             }
         }
     }
     // total for all currencies
     $total = array();
     $total[$id] = $this->getTotal();
     // taxes
     $array['taxes'] = $taxAmount = array();
     $taxAmount[$id] = 0;
     $array['taxes'][$id] = array();
     foreach ($this->taxes as $taxId => $amount) {
         if ($amount > 0) {
             $taxAmount[$id] += $amount;
             $tax = Tax::getInstanceById($taxId)->toArray();
             $tax['amount'] = $amount;
             $tax['formattedAmount'] = $currency->getFormattedPrice($amount);
             $array['taxes'][$id][] = $tax;
         }
     }
     $array['taxAmount'] = $taxAmount[$id];
     foreach ($this->taxDetails as &$taxRate) {
         $taxRate['formattedAmount'] = $currency->getFormattedPrice($taxRate['amount']);
     }
     $array['taxDetails'] = $this->taxDetails;
     $array['total'] = $total;
     $array['formattedTotal'] = $array['formattedTotalBeforeTax'] = array();
     if (is_array($array['total'])) {
         foreach ($array['total'] as $id => $amount) {
             if (!isset($taxAmount[$id])) {
                 $taxAmount[$id] = 0;
             }
             $array['formattedTotalBeforeTax'][$id] = $currency->getFormattedPrice($amount - $taxAmount[$id]);
             $array['formattedTotal'][$id] = $currency->getFormattedPrice($amount);
         }
     }
     // order type
     $array['isShippingRequired'] = (int) $this->isShippingRequired();
     // status
     $array['isReturned'] = (int) $this->isReturned();
     $array['isShipped'] = (int) $this->isShipped();
     $array['isAwaitingShipment'] = (int) $this->isAwaitingShipment();
     $array['isProcessing'] = (int) $this->isProcessing();
     // payments
     if (isset($options['payments'])) {
         $array['amountPaid'] = $this->getPaidAmount();
         $array['amountNotCaptured'] = $array['amountPaid'] - $array['capturedAmount'];
         if ($array['amountNotCaptured'] < 0) {
             $array['amountNotCaptured'] = 0;
         }
         $array['amountDue'] = $array['totalAmount'] - $array['amountPaid'];
         if ($array['amountDue'] < 0) {
             $array['amountDue'] = 0;
         }
     }
     // items subtotal
     $array['itemSubtotal'] = $array['itemDisplayPriceTotal'] = $array['itemSubtotalWithoutTax'] = 0;
     foreach ($this->getOrderedItems() as $item) {
         $array['itemSubtotal'] += $item->getSubtotal(true);
         $array['itemSubtotalWithoutTax'] += $item->getSubtotal(false);
         $array['itemDisplayPriceTotal'] += $item->getDisplayPrice($currency) * $item->count->get();
     }
     $array['itemDiscount'] = $array['itemDisplayPriceTotal'] - $array['itemSubtotal'];
     $array['itemDiscountReverse'] = $array['itemDiscount'] * -1;
     // shipping subtotal
     $array['shippingSubtotal'] = null;
     $array['shippingSubtotalWithoutTax'] = null;
     if ($this->shipments) {
         foreach ($this->shipments as $shipment) {
             $shipmentShipping = $shipment->getShippingTotalWithTax();
             if (!is_null($shipmentShipping)) {
                 $array['shippingSubtotal'] += $shipment->getShippingTotalWithTax();
                 $array['shippingSubtotalWithoutTax'] += $shipment->getShippingTotalBeforeTax();
             }
         }
     }
     $array['subtotalBeforeTaxes'] = $array['itemSubtotalWithoutTax'] + $array['shippingSubtotalWithoutTax'];
     foreach (array('amountPaid', 'amountNotCaptured', 'amountDue', 'itemSubtotal', 'shippingSubtotal', 'shippingSubtotalWithoutTax', 'subtotalBeforeTaxes', 'totalAmount', 'itemDiscountReverse', 'itemDiscount', 'itemSubtotalWithoutTax') as $key) {
         if (isset($array[$key])) {
             $array['formatted_' . $key] = $currency->getFormattedPrice($array[$key]);
         }
     }
     // discounts
     $array['discountAmount'] = 0;
     foreach (array_merge($this->fixedDiscounts, $this->orderDiscounts) as $key => $discount) {
         $array['discounts'][$discount->getID() ? $discount->getID() : $key] = $discount->toArray();
         $array['discountAmount'] -= $discount->amount->get();
     }
     // percentage discount applied to finalized order
     if (!$array['discountAmount'] && $array['isFinalized'] && $array['subtotalBeforeTaxes'] > $array['totalAmount']) {
         $array['discountAmount'] = $array['totalAmount'] - $array['subtotalBeforeTaxes'] - $array['taxAmount'];
     }
     $array['formatted_discountAmount'] = $this->getCurrency()->getFormattedPrice($array['discountAmount']);
     // coupons
     if (!is_null($this->coupons)) {
         $array['coupons'] = $this->coupons->toArray();
     }
     if (!$array['isFinalized']) {
         //$this->isRulesProcessed = false;
         $isOrderable = $this->isOrderable();
         if ($isOrderable instanceof OrderException) {
             $array['error'] = $isOrderable->toArray();
         }
         $array['isOrderable'] = !$isOrderable instanceof OrderException && $isOrderable;
         $array['isShippingSelected'] = $this->isShippingSelected();
         $array['isAddressSelected'] = $this->shippingAddress->get() && $this->billingAddress->get();
     }
     // otherwise left empty on payment page for some reason...
     if ($this->billingAddress->get()) {
         $array['BillingAddress'] = $this->billingAddress->get()->toArray();
     }
     $array['paymentMethod'] = $this->paymentMethod;
     if ($array['paymentMethod']) {
         $array['paymentMethodName'] = substr($array['paymentMethod'], 0, 7) == 'OFFLINE' ? OfflineTransactionHandler::getMethodName($array['paymentMethod']) : ActiveRecordModel::getApplication()->translate($array['paymentMethod']);
     }
     $this->setArrayData($array);
     return $array;
 }