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 insert($data)
 {
     $data->user_id = $this->userRepository->getAuthenticated()->getId();
     $data->created_at = (new \DateTime())->format('Y-m-d');
     $data->total = 0;
     $items = $data->item;
     unset($data->item);
     $hydrator = new ObjectProperty();
     $orderData = $hydrator->extract($data);
     try {
         $this->repository->beginTransaction();
         $orderId = $this->repository->insert($orderData);
         $total = 0;
         foreach ($items as $key => $item) {
             $product = $this->productRepository->find($item["product_id"]);
             $item['order_id'] = $orderId;
             $item['price'] = $product->getPrice();
             $item['total'] = $items[$key]["total"] = $item['quantity'] * $product->getPrice();
             $total += $item['total'];
             $this->repository->insertItem($item);
         }
         $this->repository->update(['total' => $total], $orderId);
         $this->repository->commitTransaction();
     } catch (Exception $e) {
         error_log($e->getCode());
         error_log($e->getFile());
         error_log($e->getLine());
         error_log($e->getMessage());
         $this->repository->rollbackTransaction();
         return false;
     }
     return $orderId;
 }
 /**
  * Fetch all or a subset of resources
  *
  * @param  array $params
  * @return ApiProblem|mixed
  */
 public function fetchAll($params = array())
 {
     if ($this->usersRepository->getAuthenticated()->getRole() == "admin") {
         return $this->ordersRepository->findAll();
     }
     return $this->ordersRepository->findByUser($this->usersRepository->getAuthenticated()->getId());
 }
 /**
  * Update a resource
  *
  * @param  mixed $id
  * @param  mixed $data
  * @return ApiProblem|mixed
  */
 public function update($id, $data)
 {
     if ($this->usersRepository->getAuthenticated()->getRole() != "admin") {
         return new ApiProblem('405', 'The user has not access to this info.');
     }
     return $this->clientsService->update($id, $data);
 }