Ejemplo n.º 1
0
 public function getCampaignStatistics($campaignNumber, $campaignYear)
 {
     $response = new AjaxResponse();
     $response->setSuccessMessage(trans('common.success'));
     $statistics = CampaignStatistics::all($campaignNumber, $campaignYear);
     $response->addExtraFields(['statistics' => $statistics]);
     return response($response->get())->header('Content-Type', 'application/json');
 }
Ejemplo n.º 2
0
 /**
  * Make sure numberOfBillsWithPassedPaymentTerm method works.
  */
 public function test_number_of_bills_with_passed_payment_term()
 {
     $bill = factory(\App\Bill::class)->create(['client_id' => $this->client->id, 'user_id' => $this->user->id, 'payment_term' => date('Y-m-d', strtotime(date('Y-m-d') . ' - 1 year'))]);
     $product = factory(\App\Product::class)->create(['user_id' => $this->user->id]);
     $billProduct = factory(\App\BillProduct::class)->create(['bill_id' => $bill->id, 'product_id' => $product->id]);
     $this->actingAs($this->user)->assertEquals(1, CampaignStatistics::numberOfBillsWithPassedPaymentTerm($this->campaign->number, $this->campaign->year));
 }
Ejemplo n.º 3
0
 /**
  * Return compared statistics about sold products in two given campaigns.
  *
  * @param array $campaign
  * @param array $campaignToCompare
  * @return array
  */
 public static function soldProductsDetails($campaign, $campaignToCompare)
 {
     // Get number of products for each campaign
     $campaignProducts = CampaignStatistics::numberOfProducts($campaign['number'], $campaign['year']);
     $campaignToCompareProducts = CampaignStatistics::numberOfProducts($campaignToCompare['number'], $campaignToCompare['year']);
     // Base translation data
     $translationData = ['campaign_number' => $campaign['number'], 'campaign_year' => $campaign['year'], 'number' => $campaignProducts, 'other_campaign_number' => $campaignToCompare['number'], 'other_campaign_year' => $campaignToCompare['year']];
     $baseOutput = ['products_sold_in_campaign' => $campaignProducts, 'products_in_campaign_to_compare' => $campaignToCompareProducts, 'sold_products_label' => trans('statistics.sold_products_label', ['campaign_number' => $campaign['number'], 'campaign_year' => $campaign['year']]), 'sold_products_in_campaign_to_compare_label' => trans('statistics.sold_products_label', ['campaign_number' => $campaignToCompare['number'], 'campaign_year' => $campaignToCompare['year']])];
     // Handle case when there are no sold products in both campaigns
     if ($campaignProducts < 1 && $campaignToCompareProducts < 1) {
         $output = ['message' => trans('statistics.sold_products_equal_trend', $translationData), 'title' => trans('statistics.sold_products_equal_trend_title')];
         return array_merge($output, $baseOutput);
     }
     // Handle case when in first campaign are no products
     if ($campaignProducts < 1 && $campaignToCompareProducts > 0) {
         $translationData['minus'] = $campaignToCompareProducts;
         $output = ['message' => trans('statistics.sold_products_down_trend', $translationData), 'title' => trans('statistics.sold_products_down_trend_title', ['percent' => 100])];
         return array_merge($output, $baseOutput);
     }
     // Handle case when in campaign to compare are no products
     if ($campaignProducts > 0 && $campaignToCompareProducts < 1) {
         $translationData['plus'] = $campaignProducts;
         $output = ['message' => trans('statistics.sold_products_up_trend', $translationData), 'title' => trans('statistics.sold_products_up_trend_title', ['percent' => 100])];
         return array_merge($output, $baseOutput);
     }
     // Calculate difference
     $difference = $campaignProducts - $campaignToCompareProducts;
     // Set the biggest value as divider
     if ($campaignProducts > $campaignToCompareProducts) {
         $divider = $campaignProducts;
     } else {
         if ($campaignToCompareProducts > $campaignProducts) {
             $divider = $campaignToCompareProducts;
         } else {
             $divider = $campaignProducts;
         }
     }
     // No changes, same number of products sold
     if ($difference == 0) {
         $upTrend = 0;
     } else {
         if ($difference < 0) {
             // Make difference positive if is negative
             $difference = $difference * -1;
             $upTrend = -1;
         } else {
             $upTrend = 1;
         }
     }
     // Now calculate the percentage
     $percent = number_format($difference * 100 / $divider, 2);
     // Choose right translation
     if ($upTrend > 0) {
         $translationData['plus'] = $difference;
         $translationName = 'statistics.sold_products_up_trend';
         $titleTranslationName = 'statistics.sold_products_up_trend_title';
     } else {
         if ($upTrend < 0) {
             $translationData['minus'] = $difference;
             $translationName = 'statistics.sold_products_down_trend';
             $titleTranslationName = 'statistics.sold_products_down_trend_title';
         } else {
             $translationName = 'statistics.sold_products_equal_trend';
             $titleTranslationName = 'statistics.sold_products_equal_trend_title';
         }
     }
     // Return the response
     $output = ['message' => trans($translationName, $translationData), 'title' => trans($titleTranslationName, ['percent' => $percent])];
     return array_merge($output, $baseOutput);
 }