/** * Query database for products of a given bill. * * @param int $billId * @return mixed */ public function getBill($billId) { // Make sure bill exists and belongs to current logged in user if (!Bill::where('user_id', Auth::user()->id)->where('id', $billId)->count()) { $response = new AjaxResponse(); $response->setFailMessage(trans('bills.bill_not_found')); return response($response->get(), 404)->header('Content-Type', 'application/json'); } $dataForProductsQuery = ['productIds' => $this->getProductIds($billId), 'applicationProductIds' => $this->getApplicationProductIds($billId), 'billId' => $billId]; // $bill = Auth::user()->bills()->where('bills.id', $billId)->leftJoin('clients', 'clients.id', '=', 'bills.client_id')->leftJoin('campaigns', 'campaigns.id', '=', 'bills.campaign_id')->select('clients.id as client_id', 'clients.name as client_name', 'bills.campaign_order', 'campaigns.number as campaign_number', 'campaigns.year as campaign_year', 'bills.other_details', 'bills.payment_term', 'bills.paid')->first(); $bill->payment_term = BillData::getPaymentTerm($billId); $bill->payment_term_passed = BillData::paymentTermPassed($billId); $billCalculations = BillData::getBillPriceFinalPriceToPaySavedMoneyAndNumberOfProducts($billId); // Check if discount column should be displayed $showDiscount = false; if ($billCalculations['saved_money'] > 0) { $showDiscount = true; } $notAvailableProducts = self::_getBillProducts($dataForProductsQuery, false); if (!$notAvailableProducts) { $notAvailableProducts = false; } $products = self::_getBillProducts($dataForProductsQuery, true); if (!$products) { $products = false; } return ['to_pay' => $billCalculations['final_price'], 'saved_money' => $billCalculations['saved_money'], 'total' => $billCalculations['price'], 'number_of_products' => $billCalculations['number_of_products'], 'show_discount_column' => $showDiscount, 'show_other_details_info' => true, 'products' => $products, 'not_available_products' => $notAvailableProducts, 'data' => $bill]; }
/** * Make sure getPaymentTerm method works. */ public function test_it_return_valid_bill_payment_term() { // Test with bill that has payment term $this->assertEquals(date('d-m-Y', strtotime($this->bill->payment_term)), BillData::getPaymentTerm($this->bill->id)); // Test with bill that does not have payment term $bill = factory(\App\Bill::class)->create(['user_id' => $this->user->id, 'client_id' => $this->client->id, 'payment_term' => '']); $this->assertEquals(false, BillData::getPaymentTerm($bill->id)); }