Ejemplo n.º 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());
 }
Ejemplo n.º 2
0
 /**
  * Make sure all method works as expected.
  */
 public function test_all_client_statistics_method()
 {
     // Calculate expected earnings
     $expectedEarnings = $this->billProduct->final_price + $this->secondBillProduct->final_price;
     $expectedEarnings += $this->billApplicationProduct->final_price + $this->secondBillApplicationProduct->final_price;
     $expectedEarnings = number_format($expectedEarnings, 2);
     // Calculate earnings in current campaign
     $expectedEarningsInCurrentCampaign = 0;
     // Calculate money user has to receive
     $expectedMoneyUserHasToReceive = 0;
     // Calculate number of products ordered
     $expectedNumberOfProductsOrdered = $this->billProduct->quantity + $this->secondBillProduct->quantity;
     $expectedNumberOfProductsOrdered += $this->billApplicationProduct->quantity + $this->secondBillApplicationProduct->quantity;
     // Calculate total discount received
     $expectedDiscountReceived = ($this->billProduct->price - $this->billProduct->final_price) * $this->billProduct->quantity;
     $expectedDiscountReceived += ($this->secondBillProduct->price - $this->secondBillProduct->final_price) * $this->secondBillProduct->quantity;
     $expectedDiscountReceived += ($this->billApplicationProduct->price - $this->billApplicationProduct->final_price) * $this->billApplicationProduct->quantity;
     $expectedDiscountReceived += ($this->secondBillApplicationProduct->price - $this->secondBillApplicationProduct->final_price) * $this->secondBillApplicationProduct->quantity;
     $this->assertEquals($expectedEarnings, ClientStatistics::earnings($this->client->id));
     $this->assertEquals(floor($expectedMoneyUserHasToReceive), floor(ClientStatistics::moneyUserHasToReceive($this->client->id)));
     $this->assertEquals($expectedNumberOfProductsOrdered, ClientStatistics::totalNumberOfProductsOrdered($this->client->id));
     $this->assertEquals(floor($expectedDiscountReceived), floor(ClientStatistics::totalDiscountReceived($this->client->id)));
 }