Skip to content

Commit

Permalink
Merge pull request #92 from bildvitta/feature/unit-price
Browse files Browse the repository at this point in the history
Trava planilha de preço - Fase 2
  • Loading branch information
zerossB authored Sep 5, 2024
2 parents 17b605c + b83ea42 commit ab7856b
Show file tree
Hide file tree
Showing 19 changed files with 252 additions and 24 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
}
},
"require": {
"php": "^8.1|^8.2",
"php": "^8.1|^8.2|^8.3",
"bildvitta/iss-sdk": "^0.1",
"illuminate/contracts": "^8.0|^9.0|^10.0",
"illuminate/database": "^8.0|^9.0|^10.0",
"illuminate/support": "^8.0|^9.0|^10.0",
"illuminate/contracts": "^8.0|^9.0|^10.0|^11.0",
"illuminate/database": "^8.0|^9.0|^10.0|^11.0",
"illuminate/support": "^8.0|^9.0|^10.0|^11.0",
"php-amqplib/php-amqplib": "^3.2.0",
"spatie/laravel-package-tools": "^1.12",
"ramsey/uuid": "^4.2.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ return new class extends Migration
if (Schema::hasTable($this->tableName)) {
Schema::table($this->tableName, function (Blueprint $table) {
$table->after('add_on_value', function (Blueprint $table) {
$table->boolean('editable')->default(true);
$table->string('due_date_type')->nullable();
$table->unsignedInteger('due_dates')->nullable();
if(Schema::hasColumn($this->tableName, 'editable')) {
$table->boolean('editable')->default(true);
}
if(Schema::hasColumn($this->tableName, 'due_date_type')) {
$table->string('due_date_type')->nullable();
}
if(Schema::hasColumn($this->tableName, 'due_dates')) {
$table->unsignedInteger('due_dates')->nullable();
}
});
$table->dropColumn('periodicity_quantity');
});
Expand Down
43 changes: 43 additions & 0 deletions database/migrations/create_sp_produto_unit_prices_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
private string $tableName;

public function __construct()
{
$this->tableName = prefixTableName('unit_prices');
}

/**
* Run the migrations.
*/
public function up(): void
{
if (!Schema::hasTable($this->tableName)) {
Schema::create($this->tableName, function (Blueprint $table) {
$table->id();
$table->uuid();
$table->foreignId('unit_id')->constrained(prefixTableName('units'))->cascadeOnDelete();
$table->date('period');
$table->decimal('fixed_price', 12, 3);
$table->decimal('table_price', 12, 3);
$table->timestamps();

$table->unique(['unit_id', 'period']);
});
}
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('unit_prices');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function handle()
$tableIndex = (int) $optionTableIndex;
}

$worker = new Worker();
$worker = new Worker;
$worker->type = 'sp-produto.dataimport.properties';
$worker->status = 'created';
$worker->schedule = now();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ public function handle()
case 'units_accessories':
$this->units_accessories();
break;
case 'units_prices':
$this->units_prices();
break;
case 'documents':
$this->documents();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ public function handle()

$selectLimit = 500;
if ($optionSelect = $this->option('select')) {
$selectLimit = (int)$optionSelect;
$selectLimit = (int) $optionSelect;
}

$offset = 0;
if ($optionOffset = $this->option('offset')) {
$offset = (int)$optionOffset;
$offset = (int) $optionOffset;
}

$tableIndex = 0;
if ($optionTableIndex = $this->option('table')) {
$tableIndex = (int)$optionTableIndex;
$tableIndex = (int) $optionTableIndex;
}

$worker = new Worker();
$worker = new Worker;
$worker->type = 'sp-produto.dataimport.real_estate_developments';
$worker->status = 'created';
$worker->queue = 'default';
Expand Down Expand Up @@ -142,6 +142,7 @@ private function getTables(): array
if ($this->configHas('units')) {
$tables[] = 'units';
$tables[] = 'units_accessories';
$tables[] = 'units_prices';
}
if ($this->configHas('documents')) {
$tables[] = 'documents';
Expand All @@ -158,6 +159,10 @@ private function getTables(): array
$tables[] = 'personalizations';
}

$tables = [];

$tables[] = 'units_prices';

return $tables;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private function syncData(
}

if (! $newObj = $newObj->first()) {
$newObj = new $model();
$newObj = new $model;
}

$newObj->fill(collect($item)->toArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,23 @@ private function units_accessories()
);
}

private function units_prices()
{
$accessories = $this->getDatabase()->table('unit_prices as up')
->leftJoin('units as un', 'up.unit_id', '=', 'un.id')
->select('up.*', 'un.uuid as unit_uuid');

$this->syncData(
$accessories,
RealEstateDevelopment\UnitPrice::class,
'Prices for Units',
[
'unit' => RealEstateDevelopment\Unit::class,
],
['created_at', 'updated_at']
);
}

private function documents(): void
{
$documents = $this->getDatabase()->table('documents as dc')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

use RuntimeException;

class MessageProcessorException extends RuntimeException
{
}
class MessageProcessorException extends RuntimeException {}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait LogHelper
private function logError(Throwable $exception, $message): void
{
try {
$worker = new Worker();
$worker = new Worker;
$worker->type = 'rabbitmq.worker.error';
$worker->payload = [
'message' => $message,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private function getRealEstateDevelopment(stdClass $message): RealEstateDevelopm
{
$realEstateDevelopment = RealEstateDevelopment::where('uuid', $message->uuid)->first();
if (! $realEstateDevelopment) {
$realEstateDevelopment = new RealEstateDevelopment();
$realEstateDevelopment = new RealEstateDevelopment;
$realEstateDevelopment->uuid = $message->uuid;
$realEstateDevelopment->hub_company_id = $this->getHubCompanyId($message->hub_company_uuid);
$realEstateDevelopment->save();
Expand Down
15 changes: 15 additions & 0 deletions src/Console/Commands/Messages/Resources/Helpers/UnitHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ private function unitUpdateOrCreate(stdClass $message): void
$accessories = Accessory::whereIn('uuid', $message->accessories)->get();
$unit->accessories()->sync($accessories->pluck('id'));

$this->unitPrices($unit, $message->prices);

$this->unitPriceCalc($unit, $message);
$this->unitSaleStep($unit, $realEstateDevelopment);

Expand All @@ -61,6 +63,19 @@ private function unitUpdateOrCreate(stdClass $message): void
}
}

private function unitPrices(BaseUnit $unit, array $prices): void
{
collect($prices)->each(function ($item) use ($unit) {
$unit->prices()->updateOrCreate([
'uuid' => $item->uuid,
], [
'period' => $item->period,
'fixed_price' => $item->fixed_price,
'table_price' => $item->table_price,
]);
});
}

private function unitDelete(stdClass $message): void
{
BaseUnit::where('uuid', $message->uuid)->delete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@ class RealEstateDevelopmentUpdated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public function __construct(public ?string $realEstateDevelopmentUuid)
{
}
public function __construct(public ?string $realEstateDevelopmentUuid) {}
}
2 changes: 0 additions & 2 deletions src/Factories/RealEstateDevelopment/UnitFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ public function definition(): array
'floor' => $this->faker->numberBetween(1, 20),
'square_meters' => $this->faker->numberBetween(200, 400),
'ideal_fraction' => $this->faker->numberBetween(1, 50),
'fixed_price' => $this->faker->numberBetween(10000, 200000),
'table_price' => $this->faker->numberBetween(10000, 200000),
'factor' => $this->faker->numberBetween(1, 10000),
'special_needs' => $this->faker->boolean(),
'observations' => $this->faker->paragraphs(3, true),
Expand Down
35 changes: 35 additions & 0 deletions src/Factories/RealEstateDevelopment/UnitPriceFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Database\Factories\Entities\RealEstateDevelopment;

use Bildvitta\IssVendas\Models\Produto\Unit;
use BildVitta\SpProduto\Models\RealEstateDevelopment\UnitPrice;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends Factory<UnitPrice>
*/
class UnitPriceFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = UnitPrice::class;

/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'unit_id' => Unit::factory(),
'period' => $this->faker->dateTimeThisYear()->format('Y-m-d'),
'fixed_price' => $this->faker->randomFloat(2, 100000, 1000000),
'table_price' => $this->faker->randomFloat(2, 100000, 1000000),
];
}
}
2 changes: 1 addition & 1 deletion src/Models/RealEstateDevelopment.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function __construct(array $attributes = [])
*/
protected static function booted()
{
static::addGlobalScope(new CompanyScope());
static::addGlobalScope(new CompanyScope);
}

/**
Expand Down
35 changes: 35 additions & 0 deletions src/Models/RealEstateDevelopment/Unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use BildVitta\SpProduto\Factories\RealEstateDevelopment\UnitFactory;
use BildVitta\SpProduto\Models\BaseModel;
use BildVitta\SpProduto\Models\RealEstateDevelopment;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
Expand Down Expand Up @@ -139,4 +141,37 @@ public function blueprint()
{
return $this->belongsTo(Blueprint::class);
}

public function prices(): HasMany
{
return $this->hasMany(UnitPrice::class);
}

public function tablePrice(): Attribute
{
return Attribute::get(function () {
$period = date('Y-m-01');
$query = $this->prices()->where('period', $period);

if ($query->exists()) {
return $query->first()->table_price;
}

return '0.00';
});
}

public function fixedPrice(): Attribute
{
return Attribute::get(function () {
$period = date('Y-m-01');
$query = $this->prices()->where('period', $period);

if ($query->exists()) {
return $query->first()->fixed_price;
}

return '0.00';
});
}
}
Loading

0 comments on commit ab7856b

Please sign in to comment.