Exemple #1
0
 /**
  * Get client details.
  *
  * @param int $clientId
  * @return array
  */
 public function getClient($clientId)
 {
     $response = new AjaxResponse();
     // Get client
     $client = Client::where('clients.id', $clientId)->where('clients.user_id', Auth::user()->id)->join('bills', 'clients.id', '=', 'bills.client_id')->select('clients.*', DB::raw('COUNT(bills.id) as total_bills'))->first();
     // Make sure client exists
     if (!$client->id) {
         $response->setFailMessage(trans('clients.client_not_found'));
         $response->addExtraFields(['redirect_to' => url('/clients')]);
         return response($response->get(), $response->getDefaultErrorResponseCode());
     }
     // Get client last unpaid bills
     $client->last_unpaid_bills = Clients::lastUnpaidBills($clientId);
     // Get client last paid bills
     $client->last_paid_bills = Clients::lastPaidBills($clientId);
     // Get client statistics
     $client->statistics = ClientStatistics::all($clientId);
     $client->money_generated = trans('clients.money_generated', ['money' => $client->statistics['earnings']]);
     $client->money_generated_in_current_year = trans('clients.money_generated_by_this_client_in_this_year_more_details', ['money' => $client->statistics['earnings_in_current_year']]);
     $client->number_of_products_sold = trans('clients.number_of_products_sold', ['number' => $client->statistics['number_of_products_ordered']]);
     $client->number_of_products_sold_this_year = trans('clients.number_of_products_sold_this_year', ['number' => $client->statistics['number_of_products_ordered_this_year']]);
     // Money user has to receive from this client
     $client->money_user_has_to_receive = 0;
     if ($client->statistics['money_user_has_to_receive'] > 0) {
         $client->money_user_has_to_receive = trans('clients.client_has_to_pay', ['sum' => $client->statistics['money_user_has_to_receive']]);
     }
     // Money client owes
     $client->money_owed_due_passed_payment_term = 0;
     if ($client->statistics['money_owed_due_passed_payment_term'] > 0) {
         $client->money_owed_due_passed_payment_term = trans('clients.client_has_to_pay_due_passed_payment_term', ['sum' => $client->statistics['money_owed_due_passed_payment_term']]);
     }
     $response->setSuccessMessage('');
     $response->addExtraFields(['data' => $client]);
     return response($response->get());
 }
Exemple #2
0
 /**
  * Make sure method works when there are no unpaid bills.
  */
 public function test_last_unpaid_bills_with_no_unpaid_bills()
 {
     $this->assertEquals(0, Clients::lastUnpaidBills($this->client->id));
 }