/** * 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])]; }
/** * Set right model for given bill product id. * * @param int $billProductId * @return mixed */ private static function setModel($billProductId) { // Check if is a bill product if (BillProduct::where('id', $billProductId)->count()) { return BillProduct::where('id', $billProductId); } // Check if is bill application product if (BillApplicationProduct::where('id', $billProductId)->count()) { return BillApplicationProduct::where('id', $billProductId); } }
/** * Return number of bills, number user sold products, total price and total discount. * * @return array */ public static function billData() { // Build an array with all user bill ids $billIds = []; $bills = Bill::select('id')->where('user_id', Auth::user()->id)->get(); foreach ($bills as $bill) { $billIds[] = $bill->id; } $data = ['sold_products' => BillApplicationProduct::whereIn('bill_id', $billIds)->count() + BillProduct::whereIn('bill_id', $billIds)->count(), 'bills' => count($billIds), 'total_price' => BillApplicationProduct::whereIn('bill_id', $billIds)->sum('final_price') + BillProduct::whereIn('bill_id', $billIds)->sum('final_price')]; $data['total_discount'] = BillApplicationProduct::whereIn('bill_id', $billIds)->sum('price') + BillProduct::whereIn('bill_id', $billIds)->sum('price') - $data['total_price']; return $data; }
/** * Return bill price, final price, to pay, saved money and number of products. * * @param int $billId * @return array */ public static function getBillPriceFinalPriceToPaySavedMoneyAndNumberOfProducts($billId) { $data = ['final_price' => 0, 'price' => 0, 'to_pay' => self::getBillToPay($billId), 'number_of_products' => 0, 'saved_money' => 0]; $billProducts = BillProduct::where('bill_id', $billId)->get(); $billApplicationProducts = BillApplicationProduct::where('bill_id', $billId)->get(); // Loop trough bill products foreach ($billProducts as $billProduct) { $data['price'] += $billProduct->price; $data['final_price'] += $billProduct->final_price; $data['number_of_products'] += $billProduct->quantity; } // Loop trough bill application products foreach ($billApplicationProducts as $billApplicationProduct) { $data['price'] += $billApplicationProduct->price; $data['final_price'] += $billApplicationProduct->final_price; $data['number_of_products'] += $billApplicationProduct->quantity; } $data['price'] = number_format($data['price'], 2); $data['final_price'] = number_format($data['final_price'], 2); $data['saved_money'] = self::getBillSavedMoney($billId); // dd($data['saved_money'].':::'.; return $data; }
/** * Return bills that contain given product. * * @param int $productId * @param bool $paidBill * @param bool $isCustomProduct * @return array */ private static function billsThatContainProduct($productId, $paidBill = false, $isCustomProduct = false) { $billIds = []; if ($paidBill) { $paidBill = 1; } else { $paidBill = 0; } // Query in function of product type if ($isCustomProduct) { $billProducts = BillProduct::where('product_id', $productId)->get(); } else { $billProducts = BillApplicationProduct::where('product_id', $productId)->get(); } // Make sure products are returned if (!$billProducts) { return []; } // Build array with bill ids foreach ($billProducts as $billProduct) { $billIds[] = $billProduct->bill_id; } return Bill::select('bills.*', 'clients.name as client_name', 'campaigns.year as campaign_year', 'campaigns.number as campaign_number')->leftJoin('clients', 'bills.client_id', '=', 'clients.id')->leftJoin('campaigns', 'campaigns.id', '=', 'bills.campaign_id')->whereIn('bills.id', $billIds)->where('bills.user_id', Auth::user()->id)->where('bills.paid', $paidBill)->get(); }
/** * User tries to delete bill product with all parameters invalid. */ public function test_user_delete_bill_product_with_all_parameters_invalid() { $expectedNumberOfBillProducts = \App\BillProduct::where('bill_id', $this->bill->id)->count() + \App\BillApplicationProduct::where('bill_id', $this->bill->id)->count(); $this->actingAs($this->user)->get('/bills/st' . rand() . '/delete/s' . rand() . '/s' . str_shuffle('19823') . '/st' . rand())->seeJson(['success' => false, 'message' => trans('bill.bill_not_found')])->assertEquals($expectedNumberOfBillProducts, \App\BillProduct::where('bill_id', $this->bill->id)->count() + \App\BillApplicationProduct::where('bill_id', $this->bill->id)->count()); }
/** * 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'); }