Skip to content

Instantly share code, notes, and snippets.

@jongravois
Created January 4, 2021 18:09
Show Gist options
  • Save jongravois/0642d055c61453cc020d965341fdf396 to your computer and use it in GitHub Desktop.
Save jongravois/0642d055c61453cc020d965341fdf396 to your computer and use it in GitHub Desktop.
<?php
namespace Tests\Feature\Jobs;
use App\Jobs\ProcessInvoiceJob;
use App\Models\ConsignmentBand;
use App\Models\PostedInvoice;
use App\Models\Project;
use App\Models\ProjectFin;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class ProcessInvoiceJobsTest extends TestCase
{
/** @test */
public function invoices_can_be_filtered_by_month_and_year()
{
$invc1 = PostedInvoice::factory()->create([
'post_date' => '2020-07-15'
]);
$invc2 = PostedInvoice::factory()->create([
'post_date' => '2020-03-03'
]);
$invc3 = PostedInvoice::factory()->create([
'post_date' => '2019-02-02'
]);
$dt = Carbon::parse('2020-07-15');
$this->assertEquals(3, PostedInvoice::all()->count());
$this->assertEquals(1, PostedInvoice::span($dt)->count());
} // end test
/** @test */
public function invoices_can_be_sorted_chronologically()
{
$invc1 = PostedInvoice::factory()->create([
'post_date' => '2020-07-15',
'invc_number' => 202020
]);
$invc2 = PostedInvoice::factory()->create([
'post_date' => '2020-07-15',
'invc_number' => 202015
]);
$invc3 = PostedInvoice::factory()->create([
'post_date' => '2020-07-12',
'invc_number' => 202010
]);
$invc4 = PostedInvoice::factory()->create([
'post_date' => '2020-07-12',
'invc_number' => 202000
]);
$invoices = PostedInvoice::chronological()->get()->pluck('invc_number');
$this->assertEquals(['202000', '202010', '202015', '202020'], $invoices->toArray());
} // end test
/** @test */
public function invoices_can_be_sorted_chronologically_by_lot()
{
$invc1 = PostedInvoice::factory()->create([
'post_date' => '2020-07-15',
'invc_number' => 202020,
'consignment_code' => 'N007JG'
]);
$invc2 = PostedInvoice::factory()->create([
'post_date' => '2020-07-15',
'invc_number' => 202015,
'consignment_code' => 'N007JG'
]);
$invc3 = PostedInvoice::factory()->create([
'post_date' => '2020-07-12',
'invc_number' => 202010,
'consignment_code' => 'N007JG'
]);
$invc4 = PostedInvoice::factory()->create([
'post_date' => '2020-07-12',
'invc_number' => 202002,
'consignment_code' => '2-JWG'
]);
$invc5 = PostedInvoice::factory()->create([
'post_date' => '2020-07-12',
'invc_number' => 202003,
'consignment_code' => '2-JWG'
]);
$invc6 = PostedInvoice::factory()->create([
'post_date' => '2020-07-12',
'invc_number' => 202012,
'consignment_code' => '2-JWG'
]);
$invc7 = PostedInvoice::factory()->create([
'post_date' => '2020-07-12',
'invc_number' => 202000,
'consignment_code' => 'N007JG'
]);
$invoices = PostedInvoice::byLot('N007JG')->chronological()->get()->pluck('invc_number');
$this->assertEquals(['202000', '202010', '202015', '202020'], $invoices->toArray());
} // end test
/** @test */
public function an_invoice_without_a_consignment_code_is_unhandled()
{
$invoice = PostedInvoice::factory()->create([
'route_code' => 'S',
'consignment_code' => null,
'unit_price' => 2000,
'unit_cost' => 1000,
'qty_invoiced' => 1,
]);
$job = new ProcessInvoiceJob($invoice);
$job->handle();
$invc = PostedInvoice::find($invoice->id);
$this->assertEquals(0, $invc->gp_percent_used);
$this->assertEquals(0, $invc->profit);
$this->assertTrue($invc->processed);
} // end test
/** @test */
public function profit_is_calculated_for_fixed_lot()
{
$project = Project::factory()->create([
'lot_code' => 'N007JG',
'fixed' => true,
'current_rate' => 0.1,
'current_gross' => 0,
'current_net' => 0
]);
$invoice = PostedInvoice::factory()->create([
'route_code' => 'S',
'consignment_code' => 'N007JG',
'gross' => 2000,
'net' => 1000,
]);
$proj = Project::find($project->id);
$invc = PostedInvoice::find($invoice->id);
$job = new ProcessInvoiceJob($invc);
$job->handle();
$proj->refresh();
$invc->refresh();
$this->assertEquals(0.1, $invc->gp_percent_used);
$this->assertFalse($invc->split_bands);
$this->assertEquals(100, $invc->profit);
$this->assertTrue($invc->processed);
$this->assertEquals(2000, $proj->current_gross);
$this->assertEquals(1000, $proj->current_net);
} // end test
/** @test */
public function profit_is_calculated_for_banded_lot_in_same_band()
{
$project = Project::factory()->create([
'lot_code' => 'N007JG',
'fixed' => false,
'current_rate' => 0.1,
'current_gross' => 0,
'current_net' => 0
]);
$band1 = ConsignmentBand::factory()->create([
'project_id' => $project->id,
'consignment_code' => 'N007JG',
'low' => 0,
'high' => 3000,
'percent' => 0.1
]);
$band2 = ConsignmentBand::factory()->create([
'project_id' => $project->id,
'consignment_code' => 'N007JG',
'low' => 3001,
'high' => 10000,
'percent' => 0.5
]);
$band3 = ConsignmentBand::factory()->create([
'project_id' => $project->id,
'consignment_code' => 'N007JG',
'low' => 10001,
'high' => 999999999,
'percent' => 0.75
]);
$invoice = PostedInvoice::factory()->create([
'route_code' => 'S',
'consignment_code' => 'N007JG',
'gross' => 2000,
'net' => 1000,
]);
$this->assertDatabaseCount('consignment_bands', 3);
$proj = Project::find($project->id);
$invc = PostedInvoice::find($invoice->id);
$job = new ProcessInvoiceJob($invc);
$job->handle();
$proj->refresh();
$invc->refresh();
$this->assertEquals(0.1, $invc->gp_percent_used);
$this->assertFalse($invc->split_bands);
$this->assertEquals(100, $invc->profit);
$this->assertTrue($invc->processed);
$this->assertEquals(2000, $proj->current_gross);
$this->assertEquals(1000, $proj->current_net);
} // end test
/** @test */
public function profit_is_calculated_for_banded_lot_with_split_bands()
{
$project = Project::factory()->create([
'lot_code' => 'N007JG',
'fixed' => false,
'current_rate' => 0.1,
'current_gross' => 2000,
'current_net' => 1500
]);
$band1 = ConsignmentBand::factory()->create([
'consignment_id' => $project->id,
'consignment_code' => 'N007JG',
'low' => 0,
'high' => 2000,
'percent' => 0.1
]);
$band2 = ConsignmentBand::factory()->create([
'consignment_id' => $project->id,
'consignment_code' => 'N007JG',
'low' => 2001,
'high' => 10000,
'percent' => 0.5
]);
$band3 = ConsignmentBand::factory()->create([
'consignment_id' => $project->id,
'consignment_code' => 'N007JG',
'low' => 10001,
'high' => 999999999,
'percent' => 0.75
]);
$invoice = PostedInvoice::factory()->create([
'route_code' => 'S',
'consignment_code' => 'N007JG',
'gross' => 2000,
'net' => 1000,
]);
$proj = Project::find($project->id);
$invc = PostedInvoice::find($invoice->id);
$job = new ProcessInvoiceJob($invc);
$job->handle();
$proj->refresh();
$invc->refresh();
$this->assertEquals(0.5, $invc->gp_percent_used);
$this->assertTrue($invc->split_bands);
$this->assertEquals(300, $invc->profit);
$this->assertTrue($invc->processed);
$this->assertEquals(4000, $proj->current_gross);
$this->assertEquals(2500, $proj->current_net);
} // end test
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment