/**
  * Order shipment with packages for receipt
  * @param $order_id
  */
 public function packages($order_id)
 {
     $packages = [];
     if (!empty($order_id)) {
         $shipments = $this->OrderShipment->find('all', ['contain' => ['OrderItem' => ['Item' => ['ItemContent']]], 'fields' => ['OrderShipment.id', 'OrderShipment.carrier', 'OrderShipment.tracking_number', 'OrderShipment.date_shipped'], 'conditions' => ['OrderShipment.order_id' => $order_id, 'OrderShipment.date_shipped IS NOT NULL']]);
         foreach ($shipments as $os) {
             $shipment = [];
             $shipment['tracking'] = Hash::extract($os, 'OrderShipment');
             $shipment['tracking']['url'] = $this->OrderShipment->getTrackingLink($shipment['tracking']['tracking_number'], $shipment['tracking']['carrier']);
             $shipment['tracking']['date_shipped'] = date("m/d/Y", strtotime($shipment['tracking']['date_shipped']));
             foreach ($os['OrderItem'] as $item) {
                 cakelog::debug(print_r($item['Item'], true));
                 $name = !empty($item['Item']['ItemContent']['display_name']) ? $item['Item']['ItemContent']['display_name'] : $item['Item']['name'];
                 $shipment['items'][] = ['name' => $name, 'qty' => $item['quantity']];
             }
             $packages[] = $shipment;
         }
     }
     $this->set(['result' => $packages, '_serialize' => ['result']]);
 }
 /**
  * Add Free Items to Order
  *
  * @param Order $Model
  * @param null $id
  * @return bool
  */
 public function addFreeItems(Order $Model, $id = null)
 {
     if (empty($id)) {
         $id = $Model->id;
     }
     $Model->id = $id;
     $order = $Model->loadDisplayDetails();
     $this->OrderItem = ClassRegistry::init('OrderItem');
     $this->Item = ClassRegistry::init('Item');
     /**
      * Add Free items
      */
     foreach ($this->free_items as $free_item) {
         $already_added = false;
         $shipment_id = null;
         $ns_warehouse_id = null;
         $qualifying_qty = 0;
         $item = $this->Item->find('first', ['conditions' => ['Item.sku' => $free_item['sku']], 'fields' => ['Item.id']]);
         /**
          * Check if free item is backordered
          */
         if (!empty($free_item['ns_warehouse_id'])) {
             if ($this->OrderItem->NsWarehouse->backordered($free_item['sku'], $free_item['ns_warehouse_id'])) {
                 $hold_code = OrderItemHoldCode::BACKORDERED;
                 $item_type = OrderItemType::TYPE_BACK_ORDER;
             } else {
                 $hold_code = OrderItemHoldCode::NONE;
                 $item_type = OrderItemType::TYPE_RETAIL;
             }
         }
         /**
          * Check if market qualifies for item
          */
         if ($free_item['market_id'] == null || $free_item['market_id'] == $order['Order']['order_market_id']) {
             $qualifying_market = true;
         } else {
             $qualifying_market = false;
         }
         /**
          * Check order if qualifies to add free item
          */
         foreach ($order['OrderCustomer']['OrderItem'] as $order_item) {
             if (empty($free_item['qualifying'])) {
                 $shipment_id = $order_item['order_shipment_id'];
                 $ns_warehouse_id = $order_item['ns_warehouse_id'];
             } else {
                 if (in_array($order_item['Item']['sku'], $free_item['qualifying'])) {
                     if (!$this->OrderItem->NsWarehouse->backordered($order_item['Item']['sku'], $order_item['ns_warehouse_id'])) {
                         $hold_code = OrderItemHoldCode::NONE;
                         $item_type = OrderItemType::TYPE_RETAIL;
                         //check to make sure we actually have stock of the free item in the warehouse
                         if ($this->OrderItem->NsWarehouse->backordered($free_item['sku'], $order_item['ns_warehouse_id'])) {
                             cakelog::debug('this free item is on backorder!');
                             $hold_code = OrderItemHoldCode::BACKORDERED;
                             $item_type = OrderItemType::TYPE_BACK_ORDER;
                         } else {
                             $shipment_id = $order_item['order_shipment_id'];
                             $ns_warehouse_id = $order_item['ns_warehouse_id'];
                         }
                     }
                     $qualifying_qty += $order_item['quantity'];
                 }
             }
             /**
              * Check if the free item has already been added
              */
             if ($order_item['Item']['sku'] == $free_item['sku']) {
                 $already_added = TRUE;
             }
         }
         /**
          * Add free item to shipment if not already added
          */
         if (!empty($item) && $qualifying_market && !empty($shipment_id) && !$already_added && !empty($ns_warehouse_id)) {
             $this->OrderItem->clear();
             if ($free_item['ns_warehouse_id'] == NULL) {
                 $item_data = ['order_customer_id' => $order['OrderCustomer']['id'], 'order_shipment_id' => $shipment_id, 'order_item_hold_code_id' => $hold_code, 'item_id' => $item['Item']['id'], 'order_item_type_id' => $item_type, 'promotion_id' => 0, 'price' => 0, 'commission' => 0, 'quantity' => $qualifying_qty, 'extended_price' => 0, 'tax_amount' => 0, 'handling_fee' => 0, 'points' => 0, 'date_created' => date('Y-m-d H:i:s'), 'ns_warehouse_id' => $ns_warehouse_id];
             } else {
                 $item_data = ['order_customer_id' => $order['OrderCustomer']['id'], 'order_shipment_id' => $shipment_id, 'order_item_hold_code_id' => $hold_code, 'item_id' => $item['Item']['id'], 'order_item_type_id' => $item_type, 'promotion_id' => 0, 'price' => 0, 'commission' => 0, 'quantity' => $free_item['quantity'], 'extended_price' => 0, 'tax_amount' => 0, 'handling_fee' => 0, 'points' => 0, 'date_created' => date('Y-m-d H:i:s'), 'ns_warehouse_id' => $free_item['ns_warehouse_id']];
             }
             $this->OrderItem->save($item_data);
         }
     }
     return true;
 }
 public function checkout()
 {
     $shipping_addresses = YouniqueAPI::call("/user/getUserShippingAddresses/" . $this->userId);
     cakelog::debug($this->Session->read('order.user_id'));
     $this->set('shipping_addresses', $shipping_addresses);
     cakelog::debug(print_r($shipping_addresses, true));
     if ($this->Session->check('activate_link')) {
         $this->setWebsiteInfo($this->Session->read('activate_link'));
     }
     if ($this->isUserLoggedIn()) {
         $cart_items = $this->Session->read('shoppingcart');
         foreach ($cart_items as $item) {
             if ($item['sku'] == 'US-91516-14') {
                 $this->setWebsiteInfo();
                 break;
             }
         }
     }
     $bt_token = YouniqueAPI::call("/shoppingcart/getToken");
     $this->set("bt_token", $bt_token);
     $this->set('consumption_check', $this->personalConsumptionCheck());
     $processor = YouniqueAPI::call("/shoppingcart/determineProcessor/" . $this->marketId);
     $this->set("processor", $processor);
     $this->set("hideshopnav", true);
     //set to TRUE to activate PayPal Payments (not the cc processing, just the pay with PayPal option
     $paypal_active = FALSE;
     if (!empty($this->request->query['productcredits'])) {
         $this->set("productcredits", $this->request->query['productcredits']);
     }
     $sofort_authorized = YouniqueAPI::call("/user/topSofort/" . $this->presenterId);
     if ($sofort_authorized != 'FALSE') {
         $this->set('sofort_authorized', 'true');
     } else {
         $this->set('sofort_authorized', 'false');
     }
     $giro_authorized = YouniqueAPI::call("/user/topGiro/" . $this->presenterId);
     if ($giro_authorized != 'FALSE') {
         $this->set('giro_authorized', 'true');
     } else {
         $this->set('giro_authorized', 'false');
     }
     $this->set("paypal_active", $paypal_active);
     if ($this->isUserLoggedIn()) {
         $user_data = YouniqueAPI::call("user/getUserdata/" . $this->userId);
         if (isset($this->presenterMarketId) && $this->presenterMarketId != 0) {
             $temp_market = $this->presenterMarketId;
         } elseif (isset($user_data->User->user_default_locale)) {
             $temp_market = YouniqueAPI::call('language/marketFromLanguage/' . $user_data->User->user_default_locale)[0];
         }
     } else {
         $temp_market = isset($this->marketId) ? $this->marketId : 1;
     }
     $user_default_locale = $this->Session->read('Config.language') ? $this->Session->read('Config.language') : 'en_US';
     $this->set('user_default_locale', $user_default_locale);
     $this->set('temp_market', $temp_market);
     if (!empty($this->partyId)) {
         $this->set("referlink", $this->generateUrl(array("controller" => "party", "partyid" => $this->partyId, "action" => "index"), true));
     } else {
         $this->set("referlink", $this->generateUrl(array("controller" => "products", "action" => "/"), true));
     }
 }