2025-08-22 09:28:53 -04:00
|
|
|
<?php
|
|
|
|
namespace Incoviba\Tests\Integration\API\Ventas;
|
|
|
|
|
|
|
|
use DateTimeImmutable;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Faker;
|
|
|
|
use Incoviba\Common\Define;
|
|
|
|
use Incoviba\Common\Implement;
|
|
|
|
use Incoviba\Common\Ideal;
|
|
|
|
use Incoviba\Model;
|
|
|
|
use Incoviba\Service;
|
|
|
|
use Incoviba\Repository;
|
|
|
|
use Tests\Extension\AbstractIntegration;
|
|
|
|
use Tests\Extension\ContainerTrait;
|
|
|
|
use Tests\Extension\Faker\Provider\Rut;
|
|
|
|
|
|
|
|
class ReservationTest extends TestCase
|
|
|
|
{
|
|
|
|
use ContainerTrait;
|
|
|
|
|
|
|
|
public function testAdd(): void
|
|
|
|
{
|
|
|
|
$faker = Faker\Factory::create('es_CL');
|
|
|
|
$faker->addProvider(new Rut($faker));
|
|
|
|
|
|
|
|
$comunas = $this->container->get(Repository\Comuna::class)->fetchAll();
|
|
|
|
$projects = $this->container->get(Repository\Proyecto::class)->fetchAll();
|
|
|
|
$project = $faker->randomElement($projects);
|
|
|
|
$unitTypes = $this->container->get(Repository\Proyecto\TipoUnidad::class)->fetchAll();
|
2025-08-29 18:33:30 -04:00
|
|
|
$projectUnitTypeRepository = $this->container->get(Repository\Proyecto\ProyectoTipoUnidad::class);
|
|
|
|
|
|
|
|
$projectUnitTypes = [];
|
|
|
|
foreach ($unitTypes as $unitType) {
|
|
|
|
$ptuCount = $faker->numberBetween(1, 10);
|
|
|
|
|
|
|
|
for ($i = 0; $i < $ptuCount; $i++) {
|
|
|
|
$ptuData = [
|
|
|
|
'tipo' => $unitType->id,
|
|
|
|
'proyecto' => $project->id,
|
|
|
|
'descripcion' => $faker->word,
|
|
|
|
];
|
|
|
|
$ptu = $projectUnitTypeRepository->create($ptuData);
|
|
|
|
$ptu = $projectUnitTypeRepository->save($ptu);
|
|
|
|
$projectUnitTypes[] = $ptu;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-08-22 09:28:53 -04:00
|
|
|
$unitsRepository = $this->container->get(Repository\Venta\Unidad::class);
|
2025-08-29 18:33:30 -04:00
|
|
|
|
2025-08-22 09:28:53 -04:00
|
|
|
$units = [];
|
|
|
|
$unitsValue = [];
|
|
|
|
$unitsCount = $faker->numberBetween(1, 3);
|
|
|
|
for ($i = 0; $i < $unitsCount; $i++) {
|
2025-08-29 18:33:30 -04:00
|
|
|
$type = $faker->randomElement($projectUnitTypes);
|
|
|
|
$unitData = [
|
|
|
|
'pt' => $type->id,
|
|
|
|
];
|
|
|
|
$unit = $unitsRepository->create($unitData);
|
|
|
|
$unit = $unitsRepository->save($unit);
|
|
|
|
$units[] = $unit->id;
|
2025-08-22 09:28:53 -04:00
|
|
|
$unitsValue[] = $faker->randomFloat(2, 1000, 10000);
|
|
|
|
}
|
|
|
|
|
|
|
|
$brokersCount = $faker->numberBetween(1, 3);
|
|
|
|
for ($i = 0; $i < $brokersCount; $i++) {
|
|
|
|
$rut = $faker->rut(false, false);
|
|
|
|
$brokerData = [
|
|
|
|
'rut' => $rut,
|
|
|
|
'digit' => $faker->digitoVerificador($rut),
|
|
|
|
'name' => $faker->firstName,
|
|
|
|
'legal_name' => $faker->company,
|
|
|
|
'contact' => $faker->name,
|
|
|
|
'email' => $faker->email,
|
|
|
|
'phone' => $faker->phoneNumber
|
|
|
|
];
|
|
|
|
$broker = $this->container->get(Repository\Proyecto\Broker::class)->create($brokerData);
|
|
|
|
$broker = $this->container->get(Repository\Proyecto\Broker::class)->save($broker);
|
|
|
|
|
|
|
|
$contractData = [
|
|
|
|
'project_id' => $project->id,
|
|
|
|
'broker_rut' => $broker->rut,
|
|
|
|
'commission' => $faker->randomFloat(4, 0, 1),
|
|
|
|
];
|
|
|
|
$contract = $this->container->get(Repository\Proyecto\Broker\Contract::class)->create($contractData);
|
|
|
|
$contract = $this->container->get(Repository\Proyecto\Broker\Contract::class)->save($contract);
|
|
|
|
$state = $this->container->get(Repository\Proyecto\Broker\Contract\State::class)->create([
|
|
|
|
'contract_id' => $contract->id,
|
|
|
|
'type' => Model\Proyecto\Broker\Contract\State\Type::ACTIVE->value,
|
|
|
|
'date' => $faker->dateTimeBetween('-2 months')->format('Y-m-d')
|
|
|
|
]);
|
|
|
|
$this->container->get(Repository\Proyecto\Broker\Contract\State::class)->save($state);
|
|
|
|
}
|
|
|
|
$contracts = $this->container->get(Repository\Proyecto\Broker\Contract::class)->fetchActiveByProject($project->id);
|
|
|
|
$broker = $faker->boolean(10) ? $faker->randomElement($contracts)->broker : null;
|
2025-08-29 18:33:30 -04:00
|
|
|
|
|
|
|
$promotionTypes = Model\Venta\Promotion\Type::cases();
|
|
|
|
$promotionsRepository = $this->container->get(Repository\Venta\Promotion::class);
|
|
|
|
for ($i = 0; $i < 10; $i ++) {
|
|
|
|
$promotionData = [
|
|
|
|
'project_id' => $project->id,
|
|
|
|
'type' => $faker->randomElement($promotionTypes)->value,
|
|
|
|
'state' => Model\Venta\Promotion\State::ACTIVE->value,
|
|
|
|
'start_date' => $faker->dateTimeBetween('-2 years', 'now')->format('Y-m-d'),
|
|
|
|
'end_date' => $faker->dateTimeBetween('now', '+2 years')->format('Y-m-d'),
|
|
|
|
'valid_until' => $faker->dateTimeBetween('now', '+2 years')->format('Y-m-d'),
|
|
|
|
'amount' => $faker->randomFloat(2, 0, 1),
|
|
|
|
];
|
|
|
|
$promotion = $promotionsRepository->create($promotionData);
|
|
|
|
$promotionsRepository->save($promotion);
|
|
|
|
}
|
|
|
|
$activePromotions = $promotionsRepository->fetchActiveByProject($project->id);
|
|
|
|
var_dump(__LINE__, $activePromotions);
|
2025-08-22 09:28:53 -04:00
|
|
|
$promotionsCount = $faker->numberBetween(0, 3);
|
|
|
|
$promotions = [];
|
|
|
|
for ($i = 0; $i < $promotionsCount; $i++) {
|
|
|
|
$promotions[] = $faker->randomElement($activePromotions)->id;
|
|
|
|
}
|
|
|
|
$rut = $faker->rut(false, false);
|
|
|
|
$data = [
|
|
|
|
'project_id' => $faker->randomElement($projects)->id,
|
|
|
|
'date' => $faker->dateTimeBetween('-2 years', 'now')->format('Y-m-d'),
|
|
|
|
'buyer_rut' => $rut,
|
|
|
|
'buyer_digit' => $faker->digitoVerificador($rut),
|
|
|
|
'buyer_names' => $faker->firstName,
|
|
|
|
'buyer_last_name' => $faker->lastName,
|
|
|
|
'buyer_last_name2' => $faker->lastName,
|
|
|
|
'buyer_email' => $faker->email,
|
|
|
|
'buyer_phone' => $faker->phoneNumber,
|
|
|
|
'buyer_address_street' => $faker->streetName,
|
|
|
|
'buyer_address_number' => $faker->buildingNumber,
|
|
|
|
'buyer_address_extra' => $faker->streetAddress,
|
|
|
|
'buyer_address_comuna_id' => $faker->randomElement($comunas)->id,
|
|
|
|
'units' => $units,
|
|
|
|
'units_value' => $unitsValue,
|
|
|
|
'broker_rut' => $broker ? $broker->rut : '',
|
|
|
|
];
|
|
|
|
if (count($promotions) > 0) {
|
|
|
|
$data['promotions'] = $promotions;
|
|
|
|
}
|
|
|
|
|
|
|
|
$reservation = $this->container->get(Service\Venta\Reservation::class)->add($data);
|
|
|
|
$this->assertInstanceOf(Model\Venta\Reservation::class, $reservation);
|
|
|
|
$this->assertEquals($data['date'], $reservation->date->format('Y-m-d'));
|
|
|
|
$this->assertEquals($data['project_id'], $reservation->project->id);
|
|
|
|
$this->assertEquals($data['buyer_rut'], $reservation->buyer->rut);
|
|
|
|
$this->assertEquals($data['buyer_digit'], $reservation->buyer->digito);
|
|
|
|
$this->assertEquals($data['buyer_names'], $reservation->buyer->nombres);
|
|
|
|
$this->assertEquals($data['buyer_last_name'], $reservation->buyer->apellido_paterno);
|
|
|
|
$this->assertEquals($data['buyer_last_name2'], $reservation->buyer->apellido_materno);
|
|
|
|
$this->assertEquals($data['buyer_email'], $reservation->buyer->datos->email);
|
|
|
|
$this->assertEquals($data['buyer_phone'], $reservation->buyer->datos->telefono);
|
|
|
|
$this->assertEquals($data['buyer_address_street'], $reservation->buyer->direccion->calle);
|
|
|
|
$this->assertEquals($data['buyer_address_number'], $reservation->buyer->direccion->numero);
|
|
|
|
$this->assertEquals($data['buyer_address_extra'], $reservation->buyer->direccion->extra);
|
|
|
|
$this->assertEquals($data['buyer_address_comuna_id'], $reservation->buyer->direccion->comuna->id);
|
|
|
|
$this->assertEquals($data['broker_rut'], $reservation->broker->rut);
|
|
|
|
}
|
|
|
|
}
|