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;
 }
Exemplo n.º 2
0
 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';
     }
 }
Exemplo n.º 3
0
 public function find($id)
 {
     $order = $this->ordersRepository->find($id);
     $client = $this->clientsRepository->findBy(['id' => $order->getClientId()])->current();
     $ptype = $this->ptypesRepository->findBy(['id' => $order->getPtypeId()])->current();
     $user = $this->usersRepository->findBy(['id' => $order->getUserId()])->current();
     $sql = $this->ordersRepository->getItemTable()->getSql();
     $select = $sql->select();
     $select->join('products', 'order_items.product_id = products.id', ['product_name' => 'name'])->where(['order_id' => $order->getId()]);
     $items = $this->ordersRepository->getItemTable()->selectWith($select);
     foreach ($items as $item) {
         $order->addItem($item);
     }
     $hydrator = new ClassMethods();
     $hydrator->addStrategy('items', new OrdersItemsHydratorStrategy(new ClassMethods()));
     $order->setClient($hydrator->extract($client));
     $order->setPtype($hydrator->extract($ptype));
     $order->setUser($hydrator->extract($user));
     $result = $hydrator->extract($order);
     unset($result['client_id']);
     unset($result['ptype_id']);
     unset($result['user_id']);
     unset($result['user']['username']);
     unset($result['user']['password']);
     return $result;
 }
 /**
  * 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());
 }
Exemplo n.º 5
0
 public function update($id, $data)
 {
     if (isset($data->password)) {
         if (!empty($data->password)) {
             $data->password = (new Bcrypt())->create($data->password);
         } else {
             unset($data->password);
         }
     }
     return $this->repository->update($id, $data);
 }
 /**
  * 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);
 }
 /**
  * Update a resource
  *
  * @param  mixed $id
  * @param  mixed $data
  * @return ApiProblem|mixed
  */
 public function update($id, $data)
 {
     $user = $this->userRepository->findByUsername($this->getIdentity()->getRoleId());
     if ($user->getRole() != "admin") {
         return new ApiProblem("403", "The user has not access to this info.");
     }
     return $this->repository->update($id, (array) $data);
 }
Exemplo n.º 8
0
 /**
  * Fetch all or a subset of resources
  *
  * @param  array $params
  * @return ApiProblem|mixed
  */
 public function fetchAll($params = array())
 {
     try {
         $this->authService->hasRole('admin');
         return $this->usersRepository->findAll();
     } catch (\Exception $e) {
         return new ApiProblem($e->getCode(), $e->getMessage());
     }
 }
 /**
  * Update a resource
  *
  * @param  mixed $id
  * @param  mixed $data
  * @return ApiProblem|mixed
  */
 public function update($id, $data)
 {
     return $this->repository->update($id, (array) $data);
 }
 /**
  * Update a resource
  *
  * @param  mixed $id
  * @param  mixed $data
  * @return ApiProblem|mixed
  */
 public function update($id, $data)
 {
     return $this->usersRepository->update($data, $id);
 }
Exemplo n.º 11
0
 /**
  * Fetch all or a subset of resources
  *
  * @param  array $params
  * @return ApiProblem|mixed
  */
 public function fetchAll($params = array())
 {
     return $this->repository->findAll();
     //return new ApiProblem(405, 'The GET method has not been defined for collections');
 }
 /**
  * Fetch all or a subset of resources
  *
  * @param  array $params
  * @return ApiProblem|mixed
  */
 public function fetchAll($params = array())
 {
     return $this->repository->findAll();
 }
Exemplo n.º 13
0
 private function isOwnerOfOrder($id)
 {
     $user = $this->usersRepository->findByUsername($this->getIdentity()->getRoleId());
     return $this->repository->find($id, $user) != null;
 }
Exemplo n.º 14
0
 /**
  * Update a resource
  *
  * @param  mixed $id
  * @param  mixed $data
  * @return ApiProblem|mixed
  */
 public function update($id, $data)
 {
     return $this->usersRepository->update($id, $data);
     return new ApiProblem(405, 'The PUT method has not been defined for individual resources');
 }