Пример #1
0
 /**
  * Test if returned product code and name are valid.
  */
 public function testGetProductCodeAndName()
 {
     $product = factory(\App\ApplicationProduct::class)->create();
     $productQuery = \App\ApplicationProduct::find($product->id);
     $this->assertEquals($product->code, $productQuery->code);
     $this->assertEquals($product->name, $productQuery->name);
 }
Пример #2
0
 /**
  * Return details about given application product.
  *
  * @param string $productCode
  * @param string $productId
  * @return array
  */
 public static function productDetails($productCode, $productId)
 {
     $product = ApplicationProduct::select('id', 'code', 'name', 'created_at')->where('code', $productCode)->where('id', $productId)->first();
     $numberOfUsersThatUseThisProduct = BillApplicationProduct::where('product_id', $productId)->join('bills', 'bills.id', '=', 'bill_application_products.bill_id')->join('users', 'users.id', '=', 'bills.user_id')->groupBy('users.id')->distinct()->count();
     $numberOfBillsThatUseThisProduct = BillApplicationProduct::where('product_id', $productId)->distinct('bill_id')->count();
     $soldPiecesOfThisProduct = BillApplicationProduct::where('product_id', $productId)->sum('quantity');
     $generatedMoneyByThisProduct = BillApplicationProduct::where('product_id', $productId)->sum('final_price');
     return ['id' => $product->id, 'code' => $product->code, 'name' => $product->name, 'created_at' => $product->created_at, 'number_of_users_that_use_this_product' => trans('products_manager.number_of_users_that_use_this_product', ['number' => $numberOfUsersThatUseThisProduct]), 'number_of_bills_that_use_this_product' => trans('products_manager.number_of_bills_that_use_this_product', ['number' => $numberOfBillsThatUseThisProduct]), 'sold_pieces' => trans('products_manager.sold_pieces_of_this_product', ['number' => $soldPiecesOfThisProduct]), 'generated_money' => trans('products_manager.generated_money_by_this_product', ['money' => $generatedMoneyByThisProduct])];
 }
Пример #3
0
 /**
  * Check if given product code is used by some user or not.
  *
  * @param CheckIfProductCodeIsUsedRequest $request
  * @param AjaxResponse $response
  * @return mixed
  */
 public function checkIfCodeIsUsed(CheckIfProductCodeIsUsedRequest $request, AjaxResponse $response)
 {
     $response->setSuccessMessage(trans('common.success'));
     // Assume product is not used and update status if is used
     $used = false;
     if (Product::where('code', $request->get('product_code'))->count() || ApplicationProduct::where('code', $request->get('product_code'))->count()) {
         $used = true;
     }
     $response->addExtraFields(['used' => $used]);
     return response($response->get())->header('Content-Type', 'application/json');
 }
Пример #4
0
 /**
  * Get product data.
  *
  * @param int $productId
  * @param string $productCode
  * @param GetProductRequest $request
  * @param AjaxResponse $response
  * @return mixed
  */
 public function get($productId, $productCode, GetProductRequest $request, AjaxResponse $response)
 {
     // Make sure product exists
     if (!ApplicationProduct::where('code', $productCode)->where('id', $productId)->count()) {
         $response->setFailMessage(trans('common.general_error'));
         return response($response->get())->header('Content-Type', 'application/json');
     }
     $response->setSuccessMessage(trans('common.success'));
     $response->addExtraFields(['product' => ProductsManagerHelper::productDetails($productCode, $productId)]);
     return response($response->get())->header('Content-Type', 'application/json');
 }
Пример #5
0
 public function add()
 {
     $this->applicationProduct->name = $this->inputs['product_name'];
     $this->applicationProduct->code = $this->inputs['product_code'];
     // If product code is used, update with new one (that is not used)
     if (ApplicationProduct::where('code', $this->applicationProduct->code)->count()) {
         // Make sure the code used to replace already used code is not also used
         if (ApplicationProduct::where('code', $this->inputs['not_used_code'])->count()) {
             $this->response->setFailMessage(trans('products_manager.code_already_used'));
             return response($this->response->get());
         }
         ApplicationProduct::where('code', $this->applicationProduct->code)->update(['code' => $this->inputs['not_used_code']]);
         // todo Fire notification to the users
     }
     $this->applicationProduct->save();
     $this->response->setSuccessMessage(trans('products_manager.product_added'));
     return response($this->response->get())->header('Content-Type', 'application/json');
 }
Пример #6
0
 /**
  * Paginate products in json
  *
  * @param Request $request
  * @return mixed
  */
 public function getProducts(Request $request)
 {
     return ApplicationProduct::orderBy('code', 'asc')->paginate(Settings::displayedProducts());
 }
Пример #7
0
 /**
  * Get product details.
  *
  * @param string $productCode
  * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  */
 public static function details($productCode)
 {
     $response = new AjaxResponse();
     $isApplicationProduct = false;
     // Check if is in products table
     $product = Product::where('user_id', Auth::user()->id)->where('code', $productCode)->first();
     if (!$product) {
         $product = ApplicationProduct::where('code', $productCode)->first();
         $isApplicationProduct = true;
     }
     // Check if is in application_products table
     if (!$product) {
         $response->setFailMessage('not found');
         return response($response->get(), $response->getDefaultErrorResponseCode());
     }
     $response->setSuccessMessage('ok');
     if ($isApplicationProduct) {
         $data = ['id' => $product->id, 'code' => $product->code, 'name' => $product->name, 'created_at' => $product->created_at, 'sold_pieces' => self::productSoldPieces($product->id), 'total_price' => self::productTotalPrice($product->id), 'paid_bills' => self::paidBillsThatContainProduct($product->id), 'not_paid_bills' => self::notPaidBillsThatContainProduct($product->id), 'is_application_product' => $isApplicationProduct];
         $response->addExtraFields($data);
         return response($response->get());
     }
     $response->addExtraFields(['id' => $product->id, 'code' => $product->code, 'name' => $product->name, 'created_at' => $product->created_at, 'sold_pieces' => self::productSoldPieces($product->id, true), 'total_price' => self::productTotalPrice($product->id, true), 'paid_bills' => self::paidBillsThatContainProduct($product->id, true), 'not_paid_bills' => self::notPaidBillsThatContainProduct($product->id, true), 'is_application_product' => $isApplicationProduct]);
     return response($response->get());
 }
Пример #8
0
 /**
  * Handle database operations to edit a bill product.
  *
  * @param array $data
  *      @option int billId
  *      @option int productId
  *      @option int billProductId
  *      @option string productCode
  *      @option string columnToUpdate
  *      @option string newValue
  * @return mixed
  */
 public static function handleBillProductEdit($data = [])
 {
     $response = new AjaxResponse();
     // Query for bill
     $bill = Auth::user()->bills()->where('id', $data['billId'])->first();
     // Now make sure exists in database
     if (!$bill) {
         $response->setFailMessage(trans('common.general_error'));
         return response($response->get(), $response->getDefaultErrorResponseCode())->header('Content-Type', 'application/json');
     }
     // Make sure bill product exists and belongs to current user
     if (!Product::where('id', $data['productId'])->count() && !ApplicationProduct::where('id', $data['productId'])->count()) {
         $response->setFailMessage(trans('bill.product_not_found'));
         return response($response->get(), 404)->header('Content-Type', 'application/json');
     }
     // Make sure bill product belongs to current user
     if (!BillProduct::where('id', $data['billProductId'])->count() && !BillApplicationProduct::where('id', $data['billProductId'])->count()) {
         $response->setFailMessage(trans('bill.bill_product_not_found'));
         return response($response->get(), 404)->header('Content-Type', 'application/json');
     }
     // We will use this variable to check if operation was successful
     $success = false;
     // Check if is a custom product
     if (Products::isCustomProduct($data['productId'], $data['productCode'])) {
         // Get product details and update with new data
         $product = BillProduct::where('id', $data['billProductId'])->first();
         BillProduct::where('id', $data['billProductId'])->update(Bills::getDataToUpdateOnEdit($data['columnToUpdate'], $data['newValue'], $product));
         $success = true;
     }
     // Check if is an application product
     if (Products::isApplicationProduct($data['productId'], $data['productCode'])) {
         // Get product details and update with new data
         $product = BillApplicationProduct::where('id', $data['billProductId'])->first();
         BillApplicationProduct::where('id', $data['billProductId'])->update(Bills::getDataToUpdateOnEdit($data['columnToUpdate'], $data['newValue'], $product));
         $success = true;
     }
     // Check if update was successful
     if ($success) {
         $response->setSuccessMessage(trans('bill.' . $data['columnToUpdate'] . '_updated'));
         return response($response->get())->header('Content-Type', 'application/json');
     }
     // If we arrive here something is wrong
     $response->setFailMessage(trans('common.general_error'));
     return response($response->get(), $response->getDefaultErrorResponseCode())->header('Content-Type', 'application/json');
 }
Пример #9
0
 /**
  * Check if given product code is used
  *
  * @param string $code
  * @return bool
  */
 private function isProductCodeAlreadyUsed($code)
 {
     if (ApplicationProduct::where('code', $code)->count() || Product::where('code', $code)->where('user_id', Auth::user()->id)->count()) {
         return true;
     }
     return false;
 }