/** * 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; }
/** * User tries to edit bill product page with empty then not numeric and then not existent bill product id. */ public function test_user_edit_bill_product_page_with_empty_then_with_not_numeric_and_then_with_not_existent_bill_product_id() { $data = ['product_id' => $this->product->id, 'product_code' => $this->product->code, 'product_page' => rand(1, 99)]; // Make request with empty bill product id $this->actingAs($this->user)->post('/bills/' . $this->bill->id . '/edit-page', $data)->seeJson(['success' => false, 'message' => trans('validation.required', ['attribute' => trans('validation.attributes.bill_product_id')])])->seeInDatabase('bill_products', ['bill_id' => $this->bill->id, 'product_id' => $this->product->id, 'page' => $this->billProduct->page]); // Make request with not numeric bill product id $data['bill_product_id'] = 'str' . $this->billProduct->id; $this->actingAs($this->user)->post('/bills/' . $this->bill->id . '/edit-page', $data)->seeJson(['success' => false, 'message' => trans('validation.numeric', ['attribute' => trans('validation.attributes.bill_product_id')])])->seeInDatabase('bill_products', ['bill_id' => $this->bill->id, 'product_id' => $this->product->id, 'page' => $this->billProduct->page]); // Make request with not existent bill product id $data['bill_product_id'] = rand(100, 999); while (\App\BillProduct::where('id', $data['bill_product_id'])->count()) { $data['bill_product_id'] = rand(100, 999); } $this->actingAs($this->user)->post('/bills/' . $this->bill->id . '/edit-page', $data)->seeJson(['success' => false, 'message' => trans('bill.bill_product_not_found')])->seeInDatabase('bill_products', ['bill_id' => $this->bill->id, 'product_id' => $this->product->id, 'page' => $this->billProduct->page]); }
/** * 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'); }