mirror of
https://github.com/crater-invoice/crater.git
synced 2025-10-27 11:41:09 -04:00
Add File based templates
This commit is contained in:
committed by
Mohit Panjwani
parent
00961bcae1
commit
d1dd704cdf
63
app/Console/Commands/CreateTemplateCommand.php
Normal file
63
app/Console/Commands/CreateTemplateCommand.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace Crater\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class CreateTemplateCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'make:template {name} {--type=}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Create estimate or invoice pdf template. ';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$templateName = $this->argument('name');
|
||||
$type = $this->option('type');
|
||||
|
||||
if (!$type) {
|
||||
$type = $this->choice('Create a template for?', ['invoice', 'estimate']);
|
||||
}
|
||||
|
||||
if (Storage::disk('views')->exists("/app/pdf/{$type}/{$templateName}.blade.php")) {
|
||||
$this->info("Template with given name already exists.");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Storage::disk('views')->copy("/app/pdf/{$type}/{$type}1.blade.php", "/app/pdf/{$type}/{$templateName}.blade.php");
|
||||
copy(public_path("/assets/img/PDF/{$type}1.png"), public_path("/assets/img/PDF/{$templateName}.png"));
|
||||
|
||||
$path = resource_path("app/pdf/{$type}/{$templateName}.blade.php");
|
||||
$type = ucfirst($type);
|
||||
$this->info("{$type} Template created successfully at ". $path);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@ -15,6 +15,7 @@ class Kernel extends ConsoleKernel
|
||||
protected $commands = [
|
||||
Commands\ResetApp::class,
|
||||
Commands\UpdateCommand::class,
|
||||
Commands\CreateTemplateCommand::class
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
@ -21,7 +21,7 @@ class ConvertEstimateController extends Controller
|
||||
*/
|
||||
public function __invoke(Request $request, Estimate $estimate)
|
||||
{
|
||||
$estimate->load(['items', 'items.taxes', 'user', 'estimateTemplate', 'taxes']);
|
||||
$estimate->load(['items', 'items.taxes', 'user', 'taxes']);
|
||||
|
||||
$invoice_date = Carbon::now();
|
||||
$due_date = Carbon::now()->addDays(7);
|
||||
@ -39,7 +39,7 @@ class ConvertEstimateController extends Controller
|
||||
'reference_number' => $estimate->reference_number,
|
||||
'user_id' => $estimate->user_id,
|
||||
'company_id' => $request->header('company'),
|
||||
'invoice_template_id' => 1,
|
||||
'template_name' => 'invoice1',
|
||||
'status' => Invoice::STATUS_DRAFT,
|
||||
'paid_status' => Invoice::STATUS_UNPAID,
|
||||
'sub_total' => $estimate->sub_total,
|
||||
@ -84,8 +84,7 @@ class ConvertEstimateController extends Controller
|
||||
$invoice = Invoice::with([
|
||||
'items',
|
||||
'user',
|
||||
'invoiceTemplate',
|
||||
'taxes',
|
||||
'taxes'
|
||||
])->find($invoice->id);
|
||||
|
||||
return response()->json([
|
||||
|
||||
@ -5,6 +5,8 @@ namespace Crater\Http\Controllers\V1\Estimate;
|
||||
use Crater\Http\Controllers\Controller;
|
||||
use Crater\Models\EstimateTemplate;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class EstimateTemplatesController extends Controller
|
||||
{
|
||||
@ -16,8 +18,17 @@ class EstimateTemplatesController extends Controller
|
||||
*/
|
||||
public function __invoke(Request $request)
|
||||
{
|
||||
$templates = Storage::disk('views')->files('/app/pdf/estimate');
|
||||
$estimateTemplates = [];
|
||||
|
||||
foreach ($templates as $key => $template) {
|
||||
$templateName = Str::before(basename($template), '.blade.php');
|
||||
$estimateTemplates[$key]['name'] = $templateName;
|
||||
$estimateTemplates[$key]['path'] = asset('assets/img/PDF/'.$templateName.'.png');
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'templates' => EstimateTemplate::all(),
|
||||
'templates' => $estimateTemplates
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,7 +18,6 @@ class EstimatesController extends Controller
|
||||
$estimates = Estimate::with([
|
||||
'items',
|
||||
'user',
|
||||
'estimateTemplate',
|
||||
'taxes',
|
||||
'creator',
|
||||
])
|
||||
@ -68,7 +67,6 @@ class EstimatesController extends Controller
|
||||
'items',
|
||||
'items.taxes',
|
||||
'user',
|
||||
'estimateTemplate',
|
||||
'creator',
|
||||
'taxes',
|
||||
'taxes.taxType',
|
||||
|
||||
@ -21,7 +21,9 @@ class BootstrapController extends Controller
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
$default_language = $user->getSettings(['language'])['language'];
|
||||
$default_language = $user->getSettings(['language']);
|
||||
|
||||
$default_language = array_key_exists('language', $default_language) ? $default_language['language'] : 'en';
|
||||
|
||||
$settings = [
|
||||
'moment_date_format',
|
||||
|
||||
@ -32,7 +32,7 @@ class CloneInvoiceController extends Controller
|
||||
'reference_number' => $invoice->reference_number,
|
||||
'user_id' => $invoice->user_id,
|
||||
'company_id' => $request->header('company'),
|
||||
'invoice_template_id' => $invoice->invoice_template_id,
|
||||
'template_name' => 'invoice1',
|
||||
'status' => Invoice::STATUS_DRAFT,
|
||||
'paid_status' => Invoice::STATUS_UNPAID,
|
||||
'sub_total' => $invoice->sub_total,
|
||||
@ -78,8 +78,7 @@ class CloneInvoiceController extends Controller
|
||||
$newInvoice = Invoice::with([
|
||||
'items',
|
||||
'user',
|
||||
'invoiceTemplate',
|
||||
'taxes',
|
||||
'taxes'
|
||||
])
|
||||
->find($newInvoice->id);
|
||||
|
||||
|
||||
@ -3,7 +3,8 @@
|
||||
namespace Crater\Http\Controllers\V1\Invoice;
|
||||
|
||||
use Crater\Http\Controllers\Controller;
|
||||
use Crater\Models\InvoiceTemplate;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class InvoiceTemplatesController extends Controller
|
||||
@ -16,7 +17,14 @@ class InvoiceTemplatesController extends Controller
|
||||
*/
|
||||
public function __invoke(Request $request)
|
||||
{
|
||||
$invoiceTemplates = InvoiceTemplate::all();
|
||||
$templates = Storage::disk('views')->files('/app/pdf/invoice');
|
||||
$invoiceTemplates = [];
|
||||
|
||||
foreach ($templates as $key => $template) {
|
||||
$templateName = Str::before(basename($template), '.blade.php');
|
||||
$invoiceTemplates[$key]['name'] = $templateName;
|
||||
$invoiceTemplates[$key]['path'] = asset('assets/img/PDF/'.$templateName.'.png');
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'invoiceTemplates' => $invoiceTemplates,
|
||||
|
||||
@ -20,7 +20,7 @@ class InvoicesController extends Controller
|
||||
{
|
||||
$limit = $request->has('limit') ? $request->limit : 10;
|
||||
|
||||
$invoices = Invoice::with(['items', 'user', 'creator', 'invoiceTemplate', 'taxes'])
|
||||
$invoices = Invoice::with(['items', 'user', 'creator', 'taxes'])
|
||||
->join('users', 'users.id', '=', 'invoices.user_id')
|
||||
->applyFilters($request->only([
|
||||
'status',
|
||||
@ -78,7 +78,6 @@ class InvoicesController extends Controller
|
||||
'items',
|
||||
'items.taxes',
|
||||
'user',
|
||||
'invoiceTemplate',
|
||||
'taxes.taxType',
|
||||
'fields.customField',
|
||||
]);
|
||||
|
||||
@ -54,8 +54,8 @@ class EstimatesRequest extends FormRequest
|
||||
'tax' => [
|
||||
'required',
|
||||
],
|
||||
'estimate_template_id' => [
|
||||
'required',
|
||||
'template_name' => [
|
||||
'required'
|
||||
],
|
||||
'items' => [
|
||||
'required',
|
||||
|
||||
@ -54,8 +54,8 @@ class InvoicesRequest extends FormRequest
|
||||
'tax' => [
|
||||
'required',
|
||||
],
|
||||
'invoice_template_id' => [
|
||||
'required',
|
||||
'template_name' => [
|
||||
'required'
|
||||
],
|
||||
'items' => [
|
||||
'required',
|
||||
|
||||
@ -2,6 +2,10 @@
|
||||
|
||||
namespace Crater\Models;
|
||||
|
||||
use Crater\Models\Company;
|
||||
use Crater\Models\Tax;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Crater\Models\CompanySetting;
|
||||
use App;
|
||||
use Barryvdh\DomPDF\Facade as PDF;
|
||||
use Carbon\Carbon;
|
||||
@ -9,7 +13,6 @@ use Crater\Mail\SendEstimateMail;
|
||||
use Crater\Traits\GeneratesPdfTrait;
|
||||
use Crater\Traits\HasCustomFieldsTrait;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Spatie\MediaLibrary\HasMedia;
|
||||
use Spatie\MediaLibrary\InteractsWithMedia;
|
||||
@ -130,11 +133,6 @@ class Estimate extends Model implements HasMedia
|
||||
return $this->hasMany(Tax::class);
|
||||
}
|
||||
|
||||
public function estimateTemplate()
|
||||
{
|
||||
return $this->belongsTo('Crater\Models\EstimateTemplate');
|
||||
}
|
||||
|
||||
public function getEstimateNumAttribute()
|
||||
{
|
||||
$position = $this->strposX($this->estimate_number, "-", 1) + 1;
|
||||
@ -315,8 +313,7 @@ class Estimate extends Model implements HasMedia
|
||||
return Estimate::with([
|
||||
'items.taxes',
|
||||
'user',
|
||||
'estimateTemplate',
|
||||
'taxes',
|
||||
'taxes'
|
||||
])
|
||||
->find($estimate->id);
|
||||
}
|
||||
@ -341,11 +338,10 @@ class Estimate extends Model implements HasMedia
|
||||
}
|
||||
|
||||
return Estimate::with([
|
||||
'items.taxes',
|
||||
'user',
|
||||
'estimateTemplate',
|
||||
'taxes',
|
||||
])
|
||||
'items.taxes',
|
||||
'user',
|
||||
'taxes'
|
||||
])
|
||||
->find($this->id);
|
||||
}
|
||||
|
||||
@ -431,7 +427,7 @@ class Estimate extends Model implements HasMedia
|
||||
}
|
||||
}
|
||||
|
||||
$estimateTemplate = EstimateTemplate::find($this->estimate_template_id);
|
||||
$estimateTemplate = self::find($this->id)->template_name;
|
||||
|
||||
$company = Company::find($this->company_id);
|
||||
$locale = CompanySetting::getSetting('language', $company->id);
|
||||
@ -451,7 +447,7 @@ class Estimate extends Model implements HasMedia
|
||||
'taxes' => $taxes,
|
||||
]);
|
||||
|
||||
return PDF::loadView('app.pdf.estimate.'.$estimateTemplate->view);
|
||||
return PDF::loadView('app.pdf.estimate.'.$estimateTemplate);
|
||||
}
|
||||
|
||||
public function getCompanyAddress()
|
||||
|
||||
@ -1,23 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Crater\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class EstimateTemplate extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = ['path', 'view', 'name'];
|
||||
|
||||
public function estimates()
|
||||
{
|
||||
return $this->hasMany(Estimate::class);
|
||||
}
|
||||
|
||||
public function getPathAttribute($value)
|
||||
{
|
||||
return url($value);
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,12 @@
|
||||
|
||||
namespace Crater\Models;
|
||||
|
||||
use Crater\Models\Company;
|
||||
use Crater\Models\CompanySetting;
|
||||
use Crater\Models\Currency;
|
||||
use Crater\Models\Tax;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Crater\Models\Payment;
|
||||
use App;
|
||||
use Barryvdh\DomPDF\Facade as PDF;
|
||||
use Carbon\Carbon;
|
||||
@ -9,7 +15,6 @@ use Crater\Mail\SendInvoiceMail;
|
||||
use Crater\Traits\GeneratesPdfTrait;
|
||||
use Crater\Traits\HasCustomFieldsTrait;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Spatie\MediaLibrary\HasMedia;
|
||||
use Spatie\MediaLibrary\InteractsWithMedia;
|
||||
@ -141,11 +146,6 @@ class Invoice extends Model implements HasMedia
|
||||
return $this->belongsTo('Crater\Models\User', 'creator_id');
|
||||
}
|
||||
|
||||
public function invoiceTemplate()
|
||||
{
|
||||
return $this->belongsTo(InvoiceTemplate::class);
|
||||
}
|
||||
|
||||
public function getInvoicePdfUrlAttribute()
|
||||
{
|
||||
return url('/invoices/pdf/'.$this->unique_hash);
|
||||
@ -366,11 +366,10 @@ class Invoice extends Model implements HasMedia
|
||||
}
|
||||
|
||||
$invoice = Invoice::with([
|
||||
'items',
|
||||
'user',
|
||||
'invoiceTemplate',
|
||||
'taxes',
|
||||
])
|
||||
'items',
|
||||
'user',
|
||||
'taxes'
|
||||
])
|
||||
->find($invoice->id);
|
||||
|
||||
return $invoice;
|
||||
@ -418,11 +417,10 @@ class Invoice extends Model implements HasMedia
|
||||
}
|
||||
|
||||
$invoice = Invoice::with([
|
||||
'items',
|
||||
'user',
|
||||
'invoiceTemplate',
|
||||
'taxes',
|
||||
])
|
||||
'items',
|
||||
'user',
|
||||
'taxes'
|
||||
])
|
||||
->find($this->id);
|
||||
|
||||
return $invoice;
|
||||
@ -512,7 +510,7 @@ class Invoice extends Model implements HasMedia
|
||||
}
|
||||
}
|
||||
|
||||
$invoiceTemplate = InvoiceTemplate::find($this->invoice_template_id);
|
||||
$invoiceTemplate = self::find($this->id)->template_name;
|
||||
|
||||
$company = Company::find($this->company_id);
|
||||
$locale = CompanySetting::getSetting('language', $company->id);
|
||||
@ -532,7 +530,7 @@ class Invoice extends Model implements HasMedia
|
||||
'taxes' => $taxes,
|
||||
]);
|
||||
|
||||
return PDF::loadView('app.pdf.invoice.'.$invoiceTemplate->view);
|
||||
return PDF::loadView('app.pdf.invoice.'.$invoiceTemplate);
|
||||
}
|
||||
|
||||
public function getEmailAttachmentSetting()
|
||||
|
||||
@ -1,23 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Crater\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class InvoiceTemplate extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = ['path', 'view', 'name'];
|
||||
|
||||
public function invoices()
|
||||
{
|
||||
return $this->hasMany(Invoice::class);
|
||||
}
|
||||
|
||||
public function getPathAttribute($value)
|
||||
{
|
||||
return url($value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user