-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #286 from liberu-billing/sweep/Add-Customizable-Em…
…ail-Templates-for-Billing-Notifications Add Customizable Email Templates for Billing Notifications
- Loading branch information
Showing
6 changed files
with
211 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
|
||
|
||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\EmailTemplate; | ||
use Illuminate\Http\Request; | ||
|
||
class EmailTemplateController extends Controller | ||
{ | ||
public function index() | ||
{ | ||
$templates = EmailTemplate::where(function($query) { | ||
$query->where('team_id', auth()->user()->currentTeam->id) | ||
->orWhere('is_default', true); | ||
})->get(); | ||
|
||
return view('email-templates.index', compact('templates')); | ||
} | ||
|
||
public function create() | ||
{ | ||
$types = [ | ||
'invoice_generated' => 'Invoice Generated', | ||
'overdue_reminder' => 'Overdue Reminder', | ||
]; | ||
return view('email-templates.create', compact('types')); | ||
} | ||
|
||
public function store(Request $request) | ||
{ | ||
$validated = $request->validate([ | ||
'name' => 'required|string|max:255', | ||
'type' => 'required|string', | ||
'subject' => 'required|string|max:255', | ||
'body' => 'required|string', | ||
]); | ||
|
||
$validated['team_id'] = auth()->user()->currentTeam->id; | ||
|
||
EmailTemplate::create($validated); | ||
|
||
return redirect()->route('email-templates.index') | ||
->with('success', 'Template created successfully'); | ||
} | ||
|
||
public function edit(EmailTemplate $template) | ||
{ | ||
$this->authorize('update', $template); | ||
$types = [ | ||
'invoice_generated' => 'Invoice Generated', | ||
'overdue_reminder' => 'Overdue Reminder', | ||
]; | ||
return view('email-templates.edit', compact('template', 'types')); | ||
} | ||
|
||
public function update(Request $request, EmailTemplate $template) | ||
{ | ||
$this->authorize('update', $template); | ||
|
||
$validated = $request->validate([ | ||
'name' => 'required|string|max:255', | ||
'type' => 'required|string', | ||
'subject' => 'required|string|max:255', | ||
'body' => 'required|string', | ||
]); | ||
|
||
$template->update($validated); | ||
|
||
return redirect()->route('email-templates.index') | ||
->with('success', 'Template updated successfully'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
|
||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use App\Traits\HasTeam; | ||
|
||
class EmailTemplate extends Model | ||
{ | ||
use HasFactory, HasTeam; | ||
|
||
protected $fillable = [ | ||
'name', | ||
'type', | ||
'subject', | ||
'body', | ||
'team_id', | ||
'is_default' | ||
]; | ||
|
||
public static function getTemplate($type, $teamId = null) | ||
{ | ||
return static::where('type', $type) | ||
->where(function ($query) use ($teamId) { | ||
$query->where('team_id', $teamId) | ||
->orWhere('is_default', true); | ||
}) | ||
->orderBy('team_id', 'desc') // Prioritize team templates | ||
->first(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
database/migrations/2024_01_20_000001_create_email_templates_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
|
||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up() | ||
{ | ||
Schema::create('email_templates', function (Blueprint $table) { | ||
$table->id(); | ||
$table->string('name'); | ||
$table->string('type'); // invoice_generated, overdue_reminder, etc. | ||
$table->string('subject'); | ||
$table->text('body'); | ||
$table->foreignId('team_id')->nullable()->constrained()->cascadeOnDelete(); | ||
$table->boolean('is_default')->default(false); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
Schema::dropIfExists('email_templates'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
|
||
<?php | ||
|
||
namespace Database\Seeders; | ||
|
||
use Illuminate\Database\Seeder; | ||
use App\Models\EmailTemplate; | ||
|
||
class EmailTemplateSeeder extends Seeder | ||
{ | ||
public function run() | ||
{ | ||
EmailTemplate::create([ | ||
'name' => 'Default Invoice Generated', | ||
'type' => 'invoice_generated', | ||
'subject' => 'Invoice #{{invoice_number}} Generated', | ||
'body' => 'Dear {{customer_name}}, | ||
Your invoice #{{invoice_number}} has been generated for the amount of {{amount}}. | ||
Due date: {{due_date}} | ||
Thank you for your business!', | ||
'is_default' => true, | ||
]); | ||
|
||
EmailTemplate::create([ | ||
'name' => 'Default Overdue Reminder', | ||
'type' => 'overdue_reminder', | ||
'subject' => 'Overdue Invoice Reminder #{{invoice_number}}', | ||
'body' => 'Dear {{customer_name}}, | ||
This is a reminder that invoice #{{invoice_number}} for {{amount}} is overdue. | ||
Please process the payment as soon as possible. | ||
Thank you for your attention to this matter.', | ||
'is_default' => true, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
|
||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
</head> | ||
<body> | ||
{!! $content !!} | ||
</body> | ||
</html> |