Exemplo n.º 1
0
 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;
 }