public function insert($data)
 {
     $hydrator = new ObjectProperty();
     //Hidrate StdClass to array
     $data->user_id = $this->usersRepository->getAuthenticated()->getId();
     $data->created_at = (new \DateTime())->format('Y-m-d');
     $data->total = 0;
     $items = $data->item;
     unset($data->item);
     $orderData = $hydrator->extract($data);
     $tableGateway = $this->repository->getTableGateway();
     try {
         $tableGateway->getAdapter()->getDriver()->getConnection()->beginTransaction();
         $order_id = $this->repository->insert($orderData);
         foreach ($items as $key => $item) {
             $product = $this->productsRepository->find($item['product_id']);
             $item['order_id'] = $order_id;
             $item['price'] = $product->getPrice();
             $item['total'] = $items[$key]['total'] = $item['quantity'] * $item['price'];
             $total += $item['total'];
             $this->repository->insertItem($item);
         }
         $this->repository->update(['total' => $total], $order_id);
         $tableGateway->getAdapter()->getDriver()->getConnection()->commit();
         return ['order_id' => $order_id];
     } catch (\Exception $e) {
         $tableGateway->getAdapter()->getDriver()->getConnection()->rollback();
         return 'error';
     }
 }
 public function update($id, $data)
 {
     $hydrator = new ObjectProperty();
     $orderData = $hydrator->extract($data);
     $items = $orderData['item'];
     unset($orderData['item']);
     try {
         $this->repository->beginTransaction();
         $this->repository->update($orderData, $id);
         $listItemsIds = [];
         foreach ($items as $item) {
             if (array_key_exists('id', $item)) {
                 $itemId = $item['id'];
                 $this->repository->updateItem($item, $itemId);
             } else {
                 $item['order_id'] = $id;
                 $itemId = $this->repository->insertItem($item);
             }
             $listItemsIds[] = $itemId;
         }
         $this->repository->deleteItemsNotInArray($id, $listItemsIds);
         $this->repository->commitTransaction();
     } catch (Exception $e) {
         $this->repository->rollbackTransaction();
         return false;
     }
     return $this->repository->find($id);
 }
Exemple #3
0
 public function update($idOrder, $data)
 {
     $order = $this->ordersRepository->find($idOrder);
     $items = $data->items;
     unset($data->items);
     $total = 0;
     $orderTable = $this->ordersRepository->getTableGateway();
     try {
         // Begin Transaction
         $orderTable->getAdapter()->getDriver()->getConnection()->beginTransaction();
         foreach ($items as $key => $item) {
             $item['total'] = $item['quantity'] * $item['price'];
             $items[$key]['total'] = $item['total'];
             $total += $item['total'];
         }
         foreach ($items as $item) {
             if (isset($item['id'])) {
                 $this->ordersRepository->updateItem($item['id'], $item);
             } else {
                 $item['order_id'] = $order->getId();
                 $this->ordersRepository->insertItem($item);
             }
         }
         $data->total = $this->getTotal($order->getId());
         $this->ordersRepository->update($order->getId(), $data);
         // Commit
         $orderTable->getAdapter()->getDriver()->getConnection()->commit();
         return $this->find($order->getId());
     } catch (\Exception $e) {
         // Rollback
         $orderTable->getAdapter()->getDriver()->getConnection()->rollback();
         throw new \Exception('Error processing order.');
     }
 }
 public function insert($data)
 {
     $hydrator = new ObjectProperty();
     $data = $hydrator->extract($data);
     $orderData = $data;
     unset($orderData['item']);
     $items = $data['item'];
     $tableGateway = $this->repository->getTableGateway();
     try {
         $tableGateway->getAdapter()->getDriver()->getConnection()->beginTransaction();
         $orderId = $this->repository->insert($orderData);
         foreach ($items as $item) {
             $item['order_id'] = $orderId;
             $this->repository->insertItem($item);
         }
         $tableGateway->getAdapter()->getDriver()->getConnection()->commit();
         return ['order_id' => $orderId];
     } catch (\Exception $e) {
         $tableGateway->getAdapter()->getDriver()->getConnection()->rollback();
         return 'error';
     }
 }
 public function update($id, $data)
 {
     $hydrator = new ObjectProperty();
     $array = $hydrator->extract($data);
     $orderData = $array;
     unset($orderData['item']);
     $items = $array['item'];
     $tableGateway = $this->repository->getTablegateway();
     try {
         $tableGateway->getAdapter()->getDriver()->getConnection()->beginTransaction();
         $itensAtuais = $this->repository->getOrderItems($id);
         foreach ($items as $item) {
             $item['order_id'] = $id;
             $buscaItem = $this->repository->getOrderItemTablegateway()->select(['order_id' => $item['order_id'], 'product_id' => $item['product_id']]);
             $idItem = 0;
             if (count($buscaItem) > 0) {
                 $idItem = (int) $buscaItem->current()->getId();
                 $this->repository->getOrderItemTablegateway()->update($item, ['id' => $idItem]);
             } else {
                 $this->repository->insertItem($item);
             }
             if (isset($itensAtuais[$idItem])) {
                 unset($itensAtuais[$idItem]);
             }
         }
         foreach ($itensAtuais as $itemExcluir) {
             $this->repository->getOrderItemTablegateway()->delete(['id' => $itemExcluir['id']]);
         }
         $this->repository->update($id, $orderData);
         $tableGateway->getAdapter()->getDriver()->getConnection()->commit();
         return ['order_id' => $id];
     } catch (\Exception $exc) {
         $tableGateway->getAdapter()->getDriver()->getConnection()->rollback();
         return 'error';
         //            echo $exc->getTraceAsString();
     }
 }