public function getCompareCampaignsData($campaignNumber, $campaignYear, $otherCampaignNumber, $otherCampaignYear) { $firstCampaign = ['number' => $campaignNumber, 'year' => $campaignYear]; $secondCampaign = ['number' => $otherCampaignNumber, 'year' => $otherCampaignYear]; $response = new AjaxResponse(); $response->setSuccessMessage(trans('common.success')); $response->addExtraFields(['statistics' => CompareCampaignsStatistics::all($firstCampaign, $secondCampaign)]); return response($response->get())->header('Content-Type', 'application/json'); }
/** * Make sure detailsAboutNumberOfClients works as expected when both campaigns have same number of clients. */ public function test_details_about_number_of_clients_when_both_campaigns_have_same_number_of_clients() { $clients = factory(\App\Client::class, 4)->create(['user_id' => $this->user->id]); foreach ($clients as $client) { factory(\App\Bill::class)->create(['client_id' => $client->id, 'user_id' => $this->user->id, 'campaign_id' => \App\Campaign::where('number', $this->firstCampaign['number'])->where('year', $this->firstCampaign['year'])->first()->id]); factory(\App\Bill::class)->create(['client_id' => $client->id, 'user_id' => $this->user->id, 'campaign_id' => \App\Campaign::where('number', $this->secondCampaign['number'])->where('year', $this->secondCampaign['year'])->first()->id]); } $this->translationData['clients'] = 4; $expected = ['message' => trans('statistics.details_about_number_of_clients_equal_trend', $this->translationData), 'title' => trans('statistics.details_about_number_of_clients_equal_trend_title'), 'number_of_clients' => 4, 'number_of_clients_in_campaign_to_compare' => 4]; $expected = array_merge($expected, $this->baseExpected); $this->actingAs($this->user)->assertEquals($expected, \App\Helpers\Statistics\CompareCampaignsStatistics::detailsAboutNumberOfClients($this->firstCampaign, $this->secondCampaign)); }
/** * Test soldProductsDetails works as expected then are more products in compared campaign. */ public function test_sold_products_with_more_products_in_compared_campaign() { $product = factory(\App\Product::class)->create(['user_id' => $this->user->id]); factory(\App\BillProduct::class)->create(['bill_id' => $this->billInFirstCampaign->id, 'product_id' => $product->id, 'quantity' => 18]); factory(\App\BillProduct::class)->create(['bill_id' => $this->billInComparedCampaign->id, 'product_id' => $product->id, 'quantity' => 20]); $expectedStatistics = ['message' => trans('statistics.sold_products_down_trend', ['campaign_number' => $this->firstCampaign['number'], 'campaign_year' => $this->firstCampaign['year'], 'minus' => 2, 'number' => 18, 'other_campaign_number' => $this->secondCampaign['number'], 'other_campaign_year' => $this->secondCampaign['year']]), 'title' => trans('statistics.sold_products_down_trend_title', ['percent' => '10.00']), 'products_sold_in_campaign' => '18', 'products_in_campaign_to_compare' => '20']; $expectedStatistics = array_merge($expectedStatistics, $this->baseExpected); $this->actingAs($this->user)->assertEquals($expectedStatistics, \App\Helpers\Statistics\CompareCampaignsStatistics::soldProductsDetails($this->firstCampaign, $this->secondCampaign)); }
/** * Test offeredDiscountDetails works as expected when both campaigns have the same discount. */ public function test_offered_discount_when_both_campaigns_have_same_discount() { $product = factory(\App\Product::class)->create(['user_id' => $this->user->id]); factory(\App\BillProduct::class)->create(['bill_id' => $this->billInFirstCampaign->id, 'product_id' => $product->id, 'price' => 100, 'discount' => 50, 'calculated_discount' => 50, 'final_price' => 50, 'quantity' => 1]); factory(\App\BillProduct::class)->create(['bill_id' => $this->billInCampaignToCompare->id, 'product_id' => $product->id, 'price' => 100, 'discount' => 50, 'calculated_discount' => 50, 'final_price' => 50, 'quantity' => 1]); $this->translationData['money'] = '50.00'; $expected = ['message' => trans('statistics.offered_discount_equal_trend', $this->translationData), 'title' => trans('statistics.offered_discount_equal_trend_title'), 'discount_offered' => '50.00', 'discount_offered_in_campaign_to_compare' => '50.00']; $expected = array_merge($expected, $this->baseExpected); $this->actingAs($this->user)->assertEquals($expected, \App\Helpers\Statistics\CompareCampaignsStatistics::offeredDiscountDetails($this->firstCampaign, $this->secondCampaign)); }
/** * Make sure detailsAboutSales works as expected when both campaigns have same sales. */ public function test_details_about_sales_when_both_campaigns_have_the_same_sales() { $bill = factory(\App\Bill::class)->create(['user_id' => $this->user->id, 'client_id' => $this->client->id, 'campaign_id' => \App\Campaign::where('number', $this->firstCampaign['number'])->where('year', $this->firstCampaign['year'])->first()->id]); $secondBill = factory(\App\Bill::class)->create(['user_id' => $this->user->id, 'client_id' => $this->client->id, 'campaign_id' => \App\Campaign::where('number', $this->secondCampaign['number'])->where('year', $this->secondCampaign['year'])->first()->id]); $product = factory(\App\Product::class)->create(['user_id' => $this->user->id]); factory(\App\BillProduct::class)->create(['bill_id' => $bill->id, 'product_id' => $product->id, 'price' => 100, 'quantity' => 1, 'final_price' => 100, 'discount' => 0, 'calculated_discount' => 0]); factory(\App\BillProduct::class)->create(['bill_id' => $secondBill->id, 'product_id' => $product->id, 'price' => 100, 'quantity' => 1, 'final_price' => 100, 'discount' => 0, 'calculated_discount' => 0]); $this->translationData['sales'] = number_format(100, 2); $expected = ['message' => trans('statistics.details_about_sales_equal_trend', $this->translationData), 'title' => trans('statistics.details_about_sales_equal_trend_title'), 'sales' => $this->translationData['sales'], 'sales_in_campaign_to_compare' => number_format(100, 2)]; $expected = array_merge($expected, $this->baseExpected); $this->actingAs($this->user)->assertEquals($expected, \App\Helpers\Statistics\CompareCampaignsStatistics::detailsAboutSales($this->firstCampaign, $this->secondCampaign)); }