Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Agent Sales Seeder + Fix Sold Appliance page #568

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions collections/Create Agent Sold Appliance (App).bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
meta {
name: Create Agent Sold Appliance (App)
type: http
seq: 16
}

post {
url: {{mpm_backend_url}}/api/app/agents/appliances
body: json
auth: inherit
}

body:json {
{
"person_id": 1,
"down_payment": "10",
"tenure": "69",
"first_payment_date": "2025-04-19",
"agent_assigned_appliance_id": 1
}
}
11 changes: 11 additions & 0 deletions collections/Get Agent Sold Appliances (App).bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
meta {
name: Get Agent Sold Appliances (App)
type: http
seq: 17
}

get {
url: {{mpm_backend_url}}/api/app/agents/appliances
body: json
auth: inherit
}
31 changes: 31 additions & 0 deletions collections/Login (App).bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
meta {
name: Login (App)
type: http
seq: 18
}

post {
url: {{mpm_backend_url}}/api/app/login
body: json
auth: none
}

headers {
device-id: 1
}

body:json {
{
"email": "{{agent_email}}",
"password": "{{agent_password}}"
}
}

assert {
res.status: eq 200
res.body.access_token: isString
}

script:post-response {
bru.setEnvVar("jwt_token", res.body.access_token);
}
2 changes: 2 additions & 0 deletions collections/environments/Local Development.bru
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ vars {
email: [email protected]
password: 123123
mpm_backend_url: http://localhost:8000
agent_email: [email protected]
agent_password: 123456
}
vars:secret [
jwt_token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function show($customerId, Request $request): ApiResource {
* @return ApiResource
*/
public function store(CreateAgentSoldApplianceRequest $request) {
$soldApplianceData = $request->only(['person_id', 'agent_assigned_appliance_id']);
$soldApplianceData = $request->only(['person_id', 'agent_assigned_appliance_id', 'down_payment', 'tenure', 'first_payment_date']);

return ApiResource::make($this->agentSoldApplianceService->create($soldApplianceData));
}
Expand Down
2 changes: 0 additions & 2 deletions src/backend/app/Listeners/SoldApplianceListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ public function __construct(

public function initializeApplianceRates(SoldApplianceDataContainer $soldAppliance): void {
$assetPerson = $soldAppliance->getAssetPerson();
$assetType = $soldAppliance->getAssetType();
$transaction = $soldAppliance->getTransaction();
$asset = $soldAppliance->getAsset();
$buyer = $this->personService->getById($assetPerson->person_id);

$this->applianceRateService->create($assetPerson);
Expand Down
2 changes: 2 additions & 0 deletions src/backend/app/Models/AgentAssignedAppliances.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace App\Models;

use App\Models\Base\BaseModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class AgentAssignedAppliances extends BaseModel {
use HasFactory;
public const RELATION_NAME = 'agent_appliance';
protected $guarded = [];

Expand Down
2 changes: 2 additions & 0 deletions src/backend/app/Models/AgentSoldAppliance.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use App\Models\Base\BaseModel;
use App\Models\Person\Person;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class AgentSoldAppliance extends BaseModel {
use HasFactory;
protected $guarded = [];

public function assignedAppliance(): BelongsTo {
Expand Down
23 changes: 11 additions & 12 deletions src/backend/app/Observers/AgentSoldApplianceObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ public function __construct(
) {}

public function created(AgentSoldAppliance $agentSoldAppliance): void {
if (request()->all()) {
$this->processSaleIfIsNotCreatedByFactory($agentSoldAppliance);
}
$this->processSale($agentSoldAppliance);
}

private function processSaleIfIsNotCreatedByFactory($agentSoldAppliance) {
private function processSale($agentSoldAppliance) {
$assignedApplianceId = $agentSoldAppliance->agent_assigned_appliance_id;
$assignedAppliance = $this->agentAssignedApplianceService->getById($assignedApplianceId);
$appliance = $assignedAppliance->appliance()->first();
Expand All @@ -52,7 +50,7 @@ private function processSaleIfIsNotCreatedByFactory($agentSoldAppliance) {

// assign agent transaction to transaction
$transactionData = [
'amount' => request()->input('down_payment') ?: 0,
'amount' => $agentSoldAppliance->down_payment ?: 0,
'sender' => $agent->device_id,
'message' => '-',
];
Expand All @@ -65,12 +63,12 @@ private function processSaleIfIsNotCreatedByFactory($agentSoldAppliance) {

// assign agent to appliance person
$appliancePersonData = [
'person_id' => request()->input('person_id'),
'first_payment_date' => request()->input('first_payment_date'),
'rate_count' => request()->input('tenure'),
'person_id' => $agentSoldAppliance->person_id,
'first_payment_date' => $agentSoldAppliance->first_payment_date,
'rate_count' => $agentSoldAppliance->tenure,
'total_cost' => $assignedAppliance->cost,
'down_payment' => request()->input('down_payment'),
'asset_type_id' => $assignedAppliance->appliance->id,
'down_payment' => $agentSoldAppliance->down_payment,
'asset_id' => $assignedAppliance->appliance->id,
];
$appliancePerson = $this->appliancePersonService->make($appliancePersonData);
$this->agentAppliancePersonService->setAssignee($agent);
Expand All @@ -81,7 +79,8 @@ private function processSaleIfIsNotCreatedByFactory($agentSoldAppliance) {
$soldApplianceDataContainer = app()->makeWith(
'App\Misc\SoldApplianceDataContainer',
[
'assetType' => $appliance,
'asset' => $appliance,
'assetType' => $appliance->assetType,
'assetPerson' => $appliancePerson,
'transaction' => $transaction,
]
Expand All @@ -92,7 +91,7 @@ private function processSaleIfIsNotCreatedByFactory($agentSoldAppliance) {
// assign agent assigned appliance to agent balance history
$agentBalanceHistoryData = [
'agent_id' => $agent->id,
'amount' => (-1 * request()->input('down_payment')),
'amount' => (-1 * $agentSoldAppliance->down_payment),
'transaction_id' => $transaction->id,
'available_balance' => $agent->balance,
'due_to_supplier' => $agent->due_to_energy_supplier,
Expand Down
6 changes: 6 additions & 0 deletions src/backend/app/Services/AgentSoldApplianceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,10 @@ function ($q) use ($agentId) {
)->latest()
->paginate();
}

public function getAgentsByCustomerId(int $customerId): Collection {
return Agent::whereHas('soldAppliances', function ($query) use ($customerId) {
$query->where('person_id', $customerId);
})->get();
}
}
4 changes: 2 additions & 2 deletions src/backend/app/Services/ApplianceRateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function create($assetPerson, $installmentType = 'monthly'): void {
$this->applianceRate->newQuery()->create(
[
'asset_person_id' => $assetPerson->id,
'rate_cost' => $assetPerson->down_payment,
'rate_cost' => round($assetPerson->down_payment),
'remaining' => 0,
'due_date' => Carbon::parse(date('Y-m-d'))->toDateTimeString(),
'remind' => 0,
Expand Down Expand Up @@ -141,7 +141,7 @@ public function getDownPaymentAsAssetRate($assetPerson): ?AssetRate {
/** @var ?AssetRate $result */
$result = $this->applianceRate->newQuery()
->where('asset_person_id', $assetPerson->id)
->where('rate_cost', $assetPerson->down_payment)
->where('rate_cost', round($assetPerson->down_payment))
->where('remaining', 0)
->first();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use App\Models\AgentAssignedAppliances;
use Illuminate\Database\Eloquent\Factories\Factory;

class AgentAssignedApplianceFactory extends Factory {
class AgentAssignedAppliancesFactory extends Factory {
protected $model = AgentAssignedAppliances::class;

/**
Expand All @@ -16,7 +16,7 @@ class AgentAssignedApplianceFactory extends Factory {
public function definition() {
return [
'agent_id' => $this->faker->numberBetween(1, 10),
'appliance_type_id' => $this->faker->numberBetween(1, 10),
'appliance_id' => $this->faker->numberBetween(1, 10),
'user_id' => $this->faker->numberBetween(1, 10),
'cost' => $this->faker->randomFloat(2, 1, 100),
];
Expand Down
6 changes: 6 additions & 0 deletions src/backend/database/factories/AgentSoldApplianceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace Database\Factories;

use App\Models\AgentSoldAppliance;
use Illuminate\Database\Eloquent\Factories\Factory;

class AgentSoldApplianceFactory extends Factory {
protected $model = AgentSoldAppliance::class;

/**
* Define the model's default state.
*
Expand All @@ -14,6 +17,9 @@ public function definition() {
return [
'person_id' => $this->faker->randomNumber(),
'agent_assigned_appliance_id' => $this->faker->randomNumber(),
'down_payment' => 0,
'tenure' => $this->faker->randomNumber(2, 10),
'first_payment_date' => date('Y-m-d', strtotime('+1 month')),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

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

return new class extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::connection('tenant')->table('agent_sold_appliances', function (Blueprint $table) {
if (!Schema::connection('tenant')->hasColumn('agent_sold_appliances', 'down_payment')) {
$table->double('down_payment')->nullable();
}

if (!Schema::connection('tenant')->hasColumn('agent_sold_appliances', 'tenure')) {
$table->integer('tenure');
}

if (!Schema::connection('tenant')->hasColumn('agent_sold_appliances', 'first_payment_date')) {
$table->date('first_payment_date')->nullable();
}
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::connection('tenant')->table('agent_sold_appliances', function (Blueprint $table) {
if (Schema::connection('tenant')->hasColumn('agent_sold_appliances', 'down_payment')) {
$table->dropColumn('down_payment');
}

if (Schema::connection('tenant')->hasColumn('agent_sold_appliances', 'tenure')) {
$table->dropColumn('tenure');
}

if (Schema::connection('tenant')->hasColumn('agent_sold_appliances', 'first_payment_date')) {
$table->dropColumn('first_payment_date');
}
});
}
};
62 changes: 62 additions & 0 deletions src/backend/database/seeders/AgentApplianceSalesSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Database\Seeders;

use App\Models\Agent;
use App\Models\AgentAssignedAppliances;
use App\Models\AgentSoldAppliance;
use App\Models\Asset;
use App\Models\Person\Person;
use Illuminate\Database\Seeder;

class AgentApplianceSalesSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run() {
// Fetch Existing Agents
$agents = Agent::all();
if ($agents->isEmpty()) {
$this->command->warn('No existing agents found. Skipping seeder.');

return;
}

// Fetch Existing Customers
$customers = Person::where('is_customer', true)->get();
if ($customers->isEmpty()) {
$this->command->warn('No existing customers found. Skipping seeder.');

return;
}

// Fetch Existing Assets
$assets = Asset::all();
if ($assets->isEmpty()) {
$this->command->warn('No existing assets found. Skipping seeder.');

return;
}

// Assign Assets to Agents
$assignedAppliances = $agents->flatMap(function ($agent) use ($assets, $customers) {
return AgentAssignedAppliances::factory()->count(3)->create([
'agent_id' => $agent->id,
'appliance_id' => $assets->random()->id, // Use existing assets
'user_id' => $customers->random()->id, // Use existing customers
]);
});

// Simulate Sales and Trigger Observer
$assignedAppliances->each(function ($assignedAppliance) use ($customers) {
AgentSoldAppliance::factory()->create([
'agent_assigned_appliance_id' => $assignedAppliance->id,
'person_id' => $customers->random()->id,
]);
});

$this->command->info('Agent Appliance Sales Seeded Successfully!');
}
}
29 changes: 0 additions & 29 deletions src/backend/database/seeders/AgentBalanceHistorySeeder.php

This file was deleted.

Loading
Loading