Skip to content

Commit

Permalink
Added lead prefix & deal prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewdrake committed Aug 24, 2024
1 parent 6790b0c commit 7703234
Show file tree
Hide file tree
Showing 15 changed files with 192 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Kanban boards
- Custom Fields
- Disabled double click on form submits
- Lead prefix
- Deal prefix
### Changed
- Improved logo sizing on pdfs
- Vertical navigation for settings
Expand Down
38 changes: 38 additions & 0 deletions database/migrations/add_prefix_to_laravel_crm_deals_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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::table(config('laravel-crm.db_table_prefix').'deals', function (Blueprint $table) {
$table->string('deal_id')->after('description')->nullable();
$table->string('prefix')->after('deal_id')->nullable();
$table->integer('number')->nullable()->after('prefix');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table(config('laravel-crm.db_table_prefix').'deals', function (Blueprint $table) {
$table->dropColumn([
'lead_id',
'prefix',
'number',
]);
});
}
};
38 changes: 38 additions & 0 deletions database/migrations/add_prefix_to_laravel_crm_leads_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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::table(config('laravel-crm.db_table_prefix').'leads', function (Blueprint $table) {
$table->string('lead_id')->after('description')->nullable();
$table->string('prefix')->after('lead_id')->nullable();
$table->integer('number')->nullable()->after('prefix');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table(config('laravel-crm.db_table_prefix').'leads', function (Blueprint $table) {
$table->dropColumn([
'lead_id',
'prefix',
'number',
]);
});
}
};
2 changes: 2 additions & 0 deletions resources/lang/en/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,4 +509,6 @@
'create_pipeline_stage' => 'create pipeline stage',
'back_to_pipeline_stages' => 'back to pipeline stages',
'edit_pipeline_stage' => 'edit pipeline stage',
'lead_prefix' => 'lead prefix',
'deal_prefix' => 'deal prefix',
];
2 changes: 1 addition & 1 deletion resources/views/livewire/kanban-board/record.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'limit' => 3
])
<div class="mt-2">
<a href="{{ url(route('laravel-crm.'.\Illuminate\Support\Str::plural($model).'.show', $record['id'])) }}">LD-{{ $record['id'] }}</a>
<a href="{{ url(route('laravel-crm.'.\Illuminate\Support\Str::plural($model).'.show', $record['id'])) }}">{{ $record['number'] }}</a>
<div class="mb-0 d-inline-block float-right"><i class="fa fa-user-circle" aria-hidden="true"></i></div>
</div>
</div>
Expand Down
14 changes: 14 additions & 0 deletions resources/views/settings/partials/fields.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@
'value' => old('tax_rate', $taxRateSetting->value ?? null),
'append' => '%'
])
@hasleadsenabled
@include('laravel-crm::partials.form.text',[
'name' => 'lead_prefix',
'label' => ucfirst(trans('laravel-crm::lang.lead_prefix')),
'value' => old('lead_prefix', $leadPrefix->value ?? null)
])
@endhasleadsenabled
@hasdealsenabled
@include('laravel-crm::partials.form.text',[
'name' => 'deal_prefix',
'label' => ucfirst(trans('laravel-crm::lang.deal_prefix')),
'value' => old('deal_prefix', $dealPrefix->value ?? null)
])
@endhasdealsenabled
@hasquotesenabled
@include('laravel-crm::partials.form.text',[
'name' => 'quote_prefix',
Expand Down
22 changes: 22 additions & 0 deletions src/Console/LaravelCrmUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use Illuminate\Console\Command;
use Illuminate\Support\Composer;
use VentureDrake\LaravelCrm\Models\Deal;
use VentureDrake\LaravelCrm\Models\Delivery;
use VentureDrake\LaravelCrm\Models\Invoice;
use VentureDrake\LaravelCrm\Models\InvoiceLine;
use VentureDrake\LaravelCrm\Models\Lead;
use VentureDrake\LaravelCrm\Models\Order;
use VentureDrake\LaravelCrm\Models\OrderProduct;
use VentureDrake\LaravelCrm\Models\Person;
Expand Down Expand Up @@ -260,6 +262,26 @@ public function handle()
'--class' => 'VentureDrake\LaravelCrm\Database\Seeders\LaravelCrmPipelineTablesSeeder',
]);

foreach (Lead::whereNull('number')->get() as $lead) {
$this->info('Updating Laravel CRM lead #'.$lead->id);

$lead->update([
'lead_id' => $this->settingService->get('lead_prefix')->value.(1000 + $lead->id),
'prefix' => $this->settingService->get('lead_prefix')->value,
'number' => 1000 + $lead->id,
]);
}

foreach (Deal::whereNull('number')->get() as $deal) {
$this->info('Updating Laravel CRM deal #'.$deal->id);

$deal->update([
'deal_id' => $this->settingService->get('deal_prefix')->value.(1000 + $deal->id),
'prefix' => $this->settingService->get('deal_prefix')->value,
'number' => 1000 + $deal->id,
]);
}

$this->settingService->set('db_update_1200', 1);
$this->info('Updating Laravel CRM pipeline tables complete.');
}
Expand Down
12 changes: 12 additions & 0 deletions src/Http/Controllers/SettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public function edit()
$currency = $this->settingService->get('currency');
$timezoneSetting = $this->settingService->get('timezone');
$logoFile = $this->settingService->get('logo_file');
$leadPrefix = $this->settingService->get('lead_prefix');
$dealPrefix = $this->settingService->get('deal_prefix');
$quotePrefix = $this->settingService->get('quote_prefix');
$orderPrefix = $this->settingService->get('order_prefix');
$invoicePrefix = $this->settingService->get('invoice_prefix');
Expand All @@ -62,6 +64,8 @@ public function edit()
'currency' => $currency,
'timezoneSetting' => $timezoneSetting,
'logoFile' => $logoFile,
'leadPrefix' => $leadPrefix,
'dealPrefix' => $dealPrefix,
'quotePrefix' => $quotePrefix,
'orderPrefix' => $orderPrefix,
'invoicePrefix' => $invoicePrefix,
Expand Down Expand Up @@ -112,6 +116,14 @@ public function update(UpdateSettingRequest $request)
$this->settingService->set('tax_rate', $request->tax_rate);
}

if($request->lead_prefix) {
$this->settingService->set('lead_prefix', $request->lead_prefix);
}

if($request->deal_prefix) {
$this->settingService->set('deal_prefix', $request->deal_prefix);
}

if($request->quote_prefix) {
$this->settingService->set('quote_prefix', $request->quote_prefix);
}
Expand Down
1 change: 1 addition & 0 deletions src/Http/Livewire/LiveDealBoard.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function records(): Collection
'title' => $deal->title,
'labels' => $deal->labels,
'stage' => $deal->pipelineStage->id ?? $this->firstStageId(),
'number' => $deal->deal_id
];
});
}
Expand Down
1 change: 1 addition & 0 deletions src/Http/Livewire/LiveLeadBoard.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function records(): Collection
'title' => $lead->title,
'labels' => $lead->labels,
'stage' => $lead->pipelineStage->id ?? $this->firstStageId(),
'number' => $lead->lead_id
];
});
}
Expand Down
1 change: 1 addition & 0 deletions src/Http/Livewire/LiveQuoteBoard.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function records(): Collection
'title' => $quote->title,
'labels' => $quote->labels,
'stage' => $quote->pipelineStage->id ?? $this->firstStageId(),
'number' => $quote->quote_id
];
});
}
Expand Down
12 changes: 12 additions & 0 deletions src/Http/Middleware/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ public function handle($request, Closure $next)
'value' => config('laravel-crm.tax_rate') ?? 0,
]);

Setting::firstOrCreate([
'name' => 'lead_prefix',
], [
'value' => 'LD-',
]);

Setting::firstOrCreate([
'name' => 'deal_prefix',
], [
'value' => 'DL-',
]);

Setting::firstOrCreate([
'name' => 'quote_prefix',
], [
Expand Down
2 changes: 2 additions & 0 deletions src/LaravelCrmServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,8 @@ function ($perPage = 30, $page = null, $options = []) {
__DIR__ . '/../database/migrations/create_laravel_crm_pipeline_stages_table.php.stub' => $this->getMigrationFileName($filesystem, 'create_laravel_crm_pipeline_stages_table.php', 100),
__DIR__ . '/../database/migrations/add_pipeline_to_laravel_crm_models_table.php.stub' => $this->getMigrationFileName($filesystem, 'add_pipeline_to_laravel_crm_models_table.php', 101),
__DIR__ . '/../database/migrations/add_user_to_laravel_crm_settings_table.php.stub' => $this->getMigrationFileName($filesystem, 'add_user_to_laravel_crm_settings_table.php', 102),
__DIR__ . '/../database/migrations/add_prefix_to_laravel_crm_leads_table.php.stub' => $this->getMigrationFileName($filesystem, 'add_prefix_to_laravel_crm_leads_table.php', 103),
__DIR__ . '/../database/migrations/add_prefix_to_laravel_crm_deals_table.php.stub' => $this->getMigrationFileName($filesystem, 'add_prefix_to_laravel_crm_deals_table.php', 104),
], 'migrations');

// Publishing the seeders
Expand Down
23 changes: 23 additions & 0 deletions src/Observers/DealObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@

namespace VentureDrake\LaravelCrm\Observers;

use Ramsey\Uuid\Uuid;
use VentureDrake\LaravelCrm\Models\Deal;
use VentureDrake\LaravelCrm\Services\SettingService;

class DealObserver
{
/**
* @var SettingService
*/
private $settingService;

public function __construct(SettingService $settingService)
{
$this->settingService = $settingService;
}

/**
* Handle the deal "creating" event.
*
Expand All @@ -14,9 +26,20 @@ class DealObserver
*/
public function creating(Deal $deal)
{
$deal->external_id = Uuid::uuid4()->toString();

if (! app()->runningInConsole()) {
$deal->user_created_id = auth()->user()->id ?? null;
}

if($lastDeal = Deal::withTrashed()->orderBy('number', 'DESC')->first()) {
$deal->number = $lastDeal->number + 1;
} else {
$deal->number = 1000;
}

$deal->prefix = $this->settingService->get('deal_prefix')->value;
$deal->deal_id = $deal->prefix.$deal->number;
}

/**
Expand Down
23 changes: 23 additions & 0 deletions src/Observers/LeadObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@

namespace VentureDrake\LaravelCrm\Observers;

use Ramsey\Uuid\Uuid;
use VentureDrake\LaravelCrm\Models\Lead;
use VentureDrake\LaravelCrm\Services\SettingService;

class LeadObserver
{
/**
* @var SettingService
*/
private $settingService;

public function __construct(SettingService $settingService)
{
$this->settingService = $settingService;
}

/**
* Handle the lead "creating" event.
*
Expand All @@ -14,9 +26,20 @@ class LeadObserver
*/
public function creating(Lead $lead)
{
$lead->external_id = Uuid::uuid4()->toString();

if (! app()->runningInConsole()) {
$lead->user_created_id = auth()->user()->id ?? null;
}

if($lastLead = Lead::withTrashed()->orderBy('number', 'DESC')->first()) {
$lead->number = $lastLead->number + 1;
} else {
$lead->number = 1000;
}

$lead->prefix = $this->settingService->get('lead_prefix')->value;
$lead->lead_id = $lead->prefix.$lead->number;
}

/**
Expand Down

0 comments on commit 7703234

Please sign in to comment.