diff --git a/app/Console/Commands/CreateTemplateCommand.php b/app/Console/Commands/CreateTemplateCommand.php new file mode 100644 index 00000000..69151aa5 --- /dev/null +++ b/app/Console/Commands/CreateTemplateCommand.php @@ -0,0 +1,63 @@ +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; + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 9518fea7..8eeedc42 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -15,6 +15,7 @@ class Kernel extends ConsoleKernel protected $commands = [ Commands\ResetApp::class, Commands\UpdateCommand::class, + Commands\CreateTemplateCommand::class ]; /** diff --git a/app/Http/Controllers/V1/Estimate/ConvertEstimateController.php b/app/Http/Controllers/V1/Estimate/ConvertEstimateController.php index 2b3014d6..099f09c0 100644 --- a/app/Http/Controllers/V1/Estimate/ConvertEstimateController.php +++ b/app/Http/Controllers/V1/Estimate/ConvertEstimateController.php @@ -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([ diff --git a/app/Http/Controllers/V1/Estimate/EstimateTemplatesController.php b/app/Http/Controllers/V1/Estimate/EstimateTemplatesController.php index fafa49d7..cb7d53cb 100644 --- a/app/Http/Controllers/V1/Estimate/EstimateTemplatesController.php +++ b/app/Http/Controllers/V1/Estimate/EstimateTemplatesController.php @@ -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 ]); } } diff --git a/app/Http/Controllers/V1/Estimate/EstimatesController.php b/app/Http/Controllers/V1/Estimate/EstimatesController.php index 9c2b8457..6f14bc6d 100644 --- a/app/Http/Controllers/V1/Estimate/EstimatesController.php +++ b/app/Http/Controllers/V1/Estimate/EstimatesController.php @@ -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', diff --git a/app/Http/Controllers/V1/General/BootstrapController.php b/app/Http/Controllers/V1/General/BootstrapController.php index 860af310..821e3b32 100644 --- a/app/Http/Controllers/V1/General/BootstrapController.php +++ b/app/Http/Controllers/V1/General/BootstrapController.php @@ -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', diff --git a/app/Http/Controllers/V1/Invoice/CloneInvoiceController.php b/app/Http/Controllers/V1/Invoice/CloneInvoiceController.php index 9f0dd647..535e33f0 100644 --- a/app/Http/Controllers/V1/Invoice/CloneInvoiceController.php +++ b/app/Http/Controllers/V1/Invoice/CloneInvoiceController.php @@ -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); diff --git a/app/Http/Controllers/V1/Invoice/InvoiceTemplatesController.php b/app/Http/Controllers/V1/Invoice/InvoiceTemplatesController.php index 96f5d9b6..4f7ca0f9 100644 --- a/app/Http/Controllers/V1/Invoice/InvoiceTemplatesController.php +++ b/app/Http/Controllers/V1/Invoice/InvoiceTemplatesController.php @@ -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, diff --git a/app/Http/Controllers/V1/Invoice/InvoicesController.php b/app/Http/Controllers/V1/Invoice/InvoicesController.php index acd5110c..c16eac1a 100644 --- a/app/Http/Controllers/V1/Invoice/InvoicesController.php +++ b/app/Http/Controllers/V1/Invoice/InvoicesController.php @@ -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', ]); diff --git a/app/Http/Requests/EstimatesRequest.php b/app/Http/Requests/EstimatesRequest.php index 4bb5786a..f0d4c08c 100644 --- a/app/Http/Requests/EstimatesRequest.php +++ b/app/Http/Requests/EstimatesRequest.php @@ -54,8 +54,8 @@ class EstimatesRequest extends FormRequest 'tax' => [ 'required', ], - 'estimate_template_id' => [ - 'required', + 'template_name' => [ + 'required' ], 'items' => [ 'required', diff --git a/app/Http/Requests/InvoicesRequest.php b/app/Http/Requests/InvoicesRequest.php index 7743d28e..e32fe9ac 100644 --- a/app/Http/Requests/InvoicesRequest.php +++ b/app/Http/Requests/InvoicesRequest.php @@ -54,8 +54,8 @@ class InvoicesRequest extends FormRequest 'tax' => [ 'required', ], - 'invoice_template_id' => [ - 'required', + 'template_name' => [ + 'required' ], 'items' => [ 'required', diff --git a/app/Models/Estimate.php b/app/Models/Estimate.php index 7afe7ffa..13a1e8b3 100644 --- a/app/Models/Estimate.php +++ b/app/Models/Estimate.php @@ -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() diff --git a/app/Models/EstimateTemplate.php b/app/Models/EstimateTemplate.php deleted file mode 100644 index 968bc63e..00000000 --- a/app/Models/EstimateTemplate.php +++ /dev/null @@ -1,23 +0,0 @@ -hasMany(Estimate::class); - } - - public function getPathAttribute($value) - { - return url($value); - } -} diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 6445c12b..c78208b2 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -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() diff --git a/app/Models/InvoiceTemplate.php b/app/Models/InvoiceTemplate.php deleted file mode 100644 index 4bfa6c35..00000000 --- a/app/Models/InvoiceTemplate.php +++ /dev/null @@ -1,23 +0,0 @@ -hasMany(Invoice::class); - } - - public function getPathAttribute($value) - { - return url($value); - } -} diff --git a/config/filesystems.php b/config/filesystems.php index d8f32c73..f265b6fb 100644 --- a/config/filesystems.php +++ b/config/filesystems.php @@ -90,6 +90,12 @@ return [ 'app' => env('DROPBOX_APP'), 'root' => env('DROPBOX_ROOT'), ], + + 'views' => [ + 'driver' => 'local', + 'root' => resource_path('views'), + ], + ], /* diff --git a/database/factories/EstimateFactory.php b/database/factories/EstimateFactory.php index 5e70cbea..c65e1b0c 100644 --- a/database/factories/EstimateFactory.php +++ b/database/factories/EstimateFactory.php @@ -3,7 +3,6 @@ namespace Database\Factories; use Crater\Models\Estimate; -use Crater\Models\EstimateTemplate; use Crater\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; @@ -76,7 +75,7 @@ class EstimateFactory extends Factory 'company_id' => User::where('role', 'super admin')->first()->company_id, 'user_id' => User::factory()->create(['role' => 'customer'])->id, 'status' => Estimate::STATUS_DRAFT, - 'estimate_template_id' => EstimateTemplate::find(1) ?? EstimateTemplate::factory(), + 'template_name' => 'estimate1', 'sub_total' => $this->faker->randomDigitNotNull, 'total' => $this->faker->randomDigitNotNull, 'discount_type' => $this->faker->randomElement(['percentage', 'fixed']), diff --git a/database/factories/EstimateTemplateFactory.php b/database/factories/EstimateTemplateFactory.php deleted file mode 100644 index 8e1b4361..00000000 --- a/database/factories/EstimateTemplateFactory.php +++ /dev/null @@ -1,30 +0,0 @@ - $this->faker->word, - 'view' => $this->faker->word, - 'name' => $this->faker->word, - ]; - } -} diff --git a/database/factories/InvoiceFactory.php b/database/factories/InvoiceFactory.php index 9f0c58e4..0a959e87 100644 --- a/database/factories/InvoiceFactory.php +++ b/database/factories/InvoiceFactory.php @@ -3,7 +3,6 @@ namespace Database\Factories; use Crater\Models\Invoice; -use Crater\Models\InvoiceTemplate; use Crater\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; @@ -101,7 +100,7 @@ class InvoiceFactory extends Factory 'invoice_number' => 'INV-'.Invoice::getNextInvoiceNumber('INV'), 'reference_number' => Invoice::getNextInvoiceNumber('INV'), 'user_id' => User::factory()->create(['role' => 'customer'])->id, - 'invoice_template_id' => InvoiceTemplate::find(1) ?? InvoiceTemplate::factory(), + 'template_name' => 'invoice1', 'status' => Invoice::STATUS_DRAFT, 'tax_per_item' => 'NO', 'discount_per_item' => 'NO', diff --git a/database/factories/InvoiceTemplateFactory.php b/database/factories/InvoiceTemplateFactory.php deleted file mode 100644 index c45dd8c6..00000000 --- a/database/factories/InvoiceTemplateFactory.php +++ /dev/null @@ -1,30 +0,0 @@ - $this->faker->word, - 'view' => $this->faker->word, - 'name' => $this->faker->word, - ]; - } -} diff --git a/database/migrations/2017_04_12_090759_create_invoices_table.php b/database/migrations/2017_04_12_090759_create_invoices_table.php index 198d322f..444313e9 100644 --- a/database/migrations/2017_04_12_090759_create_invoices_table.php +++ b/database/migrations/2017_04_12_090759_create_invoices_table.php @@ -34,8 +34,6 @@ class CreateInvoicesTable extends Migration $table->boolean('sent')->default(false); $table->boolean('viewed')->default(false); $table->string('unique_hash')->nullable(); - $table->integer('invoice_template_id')->unsigned()->nullable(); - $table->foreign('invoice_template_id')->references('id')->on('invoice_templates'); $table->integer('user_id')->unsigned()->nullable(); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); $table->integer('company_id')->unsigned()->nullable(); diff --git a/database/migrations/2017_05_05_055609_create_estimates_table.php b/database/migrations/2017_05_05_055609_create_estimates_table.php index 139cff92..c3259e29 100644 --- a/database/migrations/2017_05_05_055609_create_estimates_table.php +++ b/database/migrations/2017_05_05_055609_create_estimates_table.php @@ -32,8 +32,6 @@ class CreateEstimatesTable extends Migration $table->string('unique_hash')->nullable(); $table->integer('user_id')->unsigned()->nullable(); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); - $table->integer('estimate_template_id')->unsigned()->nullable(); - $table->foreign('estimate_template_id')->references('id')->on('estimate_templates'); $table->integer('company_id')->unsigned()->nullable(); $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade'); $table->timestamps(); diff --git a/database/migrations/2017_04_11_140447_create_invoice_templates_table.php b/database/migrations/2020_12_14_044717_add_template_name_to_invoices_table.php similarity index 50% rename from database/migrations/2017_04_11_140447_create_invoice_templates_table.php rename to database/migrations/2020_12_14_044717_add_template_name_to_invoices_table.php index f51eb897..dcbebc66 100644 --- a/database/migrations/2017_04_11_140447_create_invoice_templates_table.php +++ b/database/migrations/2020_12_14_044717_add_template_name_to_invoices_table.php @@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class CreateInvoiceTemplatesTable extends Migration +class AddTemplateNameToInvoicesTable extends Migration { /** * Run the migrations. @@ -13,12 +13,8 @@ class CreateInvoiceTemplatesTable extends Migration */ public function up() { - Schema::create('invoice_templates', function (Blueprint $table) { - $table->increments('id'); - $table->string('name')->nullable(); - $table->string('view'); - $table->string('path'); - $table->timestamps(); + Schema::table('invoices', function (Blueprint $table) { + $table->string('template_name')->nullable(); }); } @@ -29,6 +25,8 @@ class CreateInvoiceTemplatesTable extends Migration */ public function down() { - Schema::dropIfExists('invoice_templates'); + Schema::table('invoices', function (Blueprint $table) { + $table->dropColumn('template_name'); + }); } } diff --git a/database/migrations/2017_05_04_141701_create_estimate_templates_table.php b/database/migrations/2020_12_14_045310_add_template_name_to_estimates_table.php similarity index 50% rename from database/migrations/2017_05_04_141701_create_estimate_templates_table.php rename to database/migrations/2020_12_14_045310_add_template_name_to_estimates_table.php index d8a95a72..0028d9c6 100644 --- a/database/migrations/2017_05_04_141701_create_estimate_templates_table.php +++ b/database/migrations/2020_12_14_045310_add_template_name_to_estimates_table.php @@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class CreateEstimateTemplatesTable extends Migration +class AddTemplateNameToEstimatesTable extends Migration { /** * Run the migrations. @@ -13,12 +13,8 @@ class CreateEstimateTemplatesTable extends Migration */ public function up() { - Schema::create('estimate_templates', function (Blueprint $table) { - $table->increments('id'); - $table->string('name')->nullable(); - $table->string('view'); - $table->string('path'); - $table->timestamps(); + Schema::table('estimates', function (Blueprint $table) { + $table->string('template_name')->nullable(); }); } @@ -29,6 +25,8 @@ class CreateEstimateTemplatesTable extends Migration */ public function down() { - Schema::dropIfExists('estimate_templates'); + Schema::table('estimates', function (Blueprint $table) { + $table->dropColumn('template_name'); + }); } } diff --git a/database/migrations/2020_12_14_051450_remove_template_id_from_invoices_and_estimates_table.php b/database/migrations/2020_12_14_051450_remove_template_id_from_invoices_and_estimates_table.php new file mode 100644 index 00000000..5fe0184b --- /dev/null +++ b/database/migrations/2020_12_14_051450_remove_template_id_from_invoices_and_estimates_table.php @@ -0,0 +1,61 @@ +map(function ($invoice) { + $invoice->template_name = 'invoice'.$invoice->invoice_template_id; + $invoice->save(); + }); + + Schema::table('invoices', function (Blueprint $table) { + $table->dropForeign(['invoice_template_id']); + $table->dropColumn('invoice_template_id'); + }); + } + + if (Schema::hasColumn('estimates', 'estimate_template_id')) + { + $estimates = Estimate::all(); + + $estimates->map(function ($estimate) { + $estimate->template_name = 'estimate'.$estimate->estimate_template_id; + $estimate->save(); + }); + + Schema::table('estimates', function (Blueprint $table) { + $table->dropForeign(['estimate_template_id']); + $table->dropColumn('estimate_template_id'); + }); + } + + Schema::dropIfExists('invoice_templates'); + Schema::dropIfExists('estimate_templates'); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index a57d7663..42076913 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -17,8 +17,6 @@ class DatabaseSeeder extends Seeder $this->call(CurrenciesTableSeeder::class); $this->call(DefaultSettingsSeeder::class); $this->call(CountriesTableSeeder::class); - $this->call(EstimateTemplateSeeder::class); - $this->call(InvoiceTemplateSeeder::class); $this->call(PaymentMethodSeeder::class); $this->call(UnitSeeder::class); } diff --git a/database/seeders/EstimateTemplateSeeder.php b/database/seeders/EstimateTemplateSeeder.php deleted file mode 100644 index a004b208..00000000 --- a/database/seeders/EstimateTemplateSeeder.php +++ /dev/null @@ -1,35 +0,0 @@ - 'Template 1', - 'view' => 'estimate1', - 'path' => '/assets/img/PDF/Template1.png', - ]); - - EstimateTemplate::create([ - 'name' => 'Template 2', - 'view' => 'estimate2', - 'path' => '/assets/img/PDF/Template2.png', - ]); - - EstimateTemplate::create([ - 'name' => 'Template 3', - 'view' => 'estimate3', - 'path' => '/assets/img/PDF/Template3.png', - ]); - } -} diff --git a/database/seeders/InvoiceTemplateSeeder.php b/database/seeders/InvoiceTemplateSeeder.php deleted file mode 100644 index 2fea992c..00000000 --- a/database/seeders/InvoiceTemplateSeeder.php +++ /dev/null @@ -1,35 +0,0 @@ - 'Template 1', - 'view' => 'invoice1', - 'path' => '/assets/img/PDF/Template1.png', - ]); - - InvoiceTemplate::create([ - 'name' => ' Template 2', - 'view' => 'invoice2', - 'path' => '/assets/img/PDF/Template2.png', - ]); - - InvoiceTemplate::create([ - 'name' => 'Template 3', - 'view' => 'invoice3', - 'path' => '/assets/img/PDF/Template3.png', - ]); - } -} diff --git a/public/assets/img/PDF/Template1.png b/public/assets/img/PDF/estimate1.png similarity index 100% rename from public/assets/img/PDF/Template1.png rename to public/assets/img/PDF/estimate1.png diff --git a/public/assets/img/PDF/Template2.png b/public/assets/img/PDF/estimate2.png similarity index 100% rename from public/assets/img/PDF/Template2.png rename to public/assets/img/PDF/estimate2.png diff --git a/public/assets/img/PDF/Template3.png b/public/assets/img/PDF/estimate3.png similarity index 100% rename from public/assets/img/PDF/Template3.png rename to public/assets/img/PDF/estimate3.png diff --git a/public/assets/img/PDF/invoice1.png b/public/assets/img/PDF/invoice1.png new file mode 100644 index 00000000..838cf268 Binary files /dev/null and b/public/assets/img/PDF/invoice1.png differ diff --git a/public/assets/img/PDF/invoice2.png b/public/assets/img/PDF/invoice2.png new file mode 100644 index 00000000..bc43166b Binary files /dev/null and b/public/assets/img/PDF/invoice2.png differ diff --git a/public/assets/img/PDF/invoice3.png b/public/assets/img/PDF/invoice3.png new file mode 100644 index 00000000..06414aa8 Binary files /dev/null and b/public/assets/img/PDF/invoice3.png differ diff --git a/resources/assets/js/components/base/modal/EstimateTemplate.vue b/resources/assets/js/components/base/modal/EstimateTemplate.vue index eb64284f..6fcccd41 100644 --- a/resources/assets/js/components/base/modal/EstimateTemplate.vue +++ b/resources/assets/js/components/base/modal/EstimateTemplate.vue @@ -7,14 +7,19 @@ :key="index" :class="{ 'border border-solid border-primary-500': - selectedTemplate === template.id, + selectedTemplate === template.name, }" - class="relative flex flex-col m-2 border border-gray-200 border-solid cursor-pointer hover:border-primary-300" - @click="selectedTemplate = template.id" + class="relative flex flex-col m-2 border border-gray-200 border-solid cursor-pointer hover:border-primary-300" > - template-image + @@ -53,16 +58,16 @@ import { mapActions, mapGetters } from 'vuex' export default { data() { return { - selectedTemplate: 1, + selectedTemplate: null, isLoading: false, } }, computed: { ...mapGetters('modal', ['modalData']), - ...mapGetters('estimate', ['getTemplateId']), + ...mapGetters('estimate', ['getTemplateName']), }, mounted() { - this.selectedTemplate = this.getTemplateId + this.selectedTemplate = this.getTemplateName }, methods: { ...mapActions('estimate', ['setTemplate']), @@ -77,7 +82,7 @@ export default { } }, closeEstimateModal() { - this.selectedTemplate = this.getTemplateId + this.selectedTemplate = this.getTemplateName this.closeModal() this.resetModalData() }, diff --git a/resources/assets/js/components/base/modal/InvoiceTemplate.vue b/resources/assets/js/components/base/modal/InvoiceTemplate.vue index 4cc8aaad..442d61a5 100644 --- a/resources/assets/js/components/base/modal/InvoiceTemplate.vue +++ b/resources/assets/js/components/base/modal/InvoiceTemplate.vue @@ -7,14 +7,19 @@ :key="index" :class="{ 'border border-solid border-primary-500': - selectedTemplate === template.id, + selectedTemplate === template.name, }" - class="relative flex flex-col m-2 border border-gray-200 border-solid cursor-pointer hover:border-primary-300" - @click="selectedTemplate = template.id" + class="relative flex flex-col m-2 border border-gray-200 border-solid cursor-pointer hover:border-primary-300" > - template-image + { window.axios .get(`/api/v1/estimates/${id}`) .then((response) => { - commit( - types.SET_TEMPLATE_ID, - response.data.estimate.estimate_template_id - ) + commit(types.SET_TEMPLATE_NAME, response.data.estimate.template_name) resolve(response) }) .catch((err) => { @@ -297,7 +294,7 @@ export const resetItem = ({ commit, dispatch, state }) => { export const setTemplate = ({ commit, dispatch, state }, data) => { return new Promise((resolve, reject) => { - commit(types.SET_TEMPLATE_ID, data) + commit(types.SET_TEMPLATE_NAME, data) resolve({}) }) } diff --git a/resources/assets/js/store/modules/estimate/getters.js b/resources/assets/js/store/modules/estimate/getters.js index e0fd5cf3..c43295eb 100644 --- a/resources/assets/js/store/modules/estimate/getters.js +++ b/resources/assets/js/store/modules/estimate/getters.js @@ -1,6 +1,6 @@ export const estimates = (state) => state.estimates export const selectAllField = (state) => state.selectAllField -export const getTemplateId = (state) => state.estimateTemplateId +export const getTemplateName = (state) => state.estimateTemplateName export const selectedEstimates = (state) => state.selectedEstimates export const totalEstimates = (state) => state.totalEstimates export const selectedCustomer = (state) => state.selectedCustomer diff --git a/resources/assets/js/store/modules/estimate/index.js b/resources/assets/js/store/modules/estimate/index.js index 38951632..f14d4892 100644 --- a/resources/assets/js/store/modules/estimate/index.js +++ b/resources/assets/js/store/modules/estimate/index.js @@ -4,7 +4,7 @@ import * as getters from './getters' const initialState = { estimates: [], - estimateTemplateId: 1, + estimateTemplateName: null, selectAllField: false, selectedEstimates: [], totalEstimates: 0, diff --git a/resources/assets/js/store/modules/estimate/mutation-types.js b/resources/assets/js/store/modules/estimate/mutation-types.js index 71ccbd55..bbf66cf4 100644 --- a/resources/assets/js/store/modules/estimate/mutation-types.js +++ b/resources/assets/js/store/modules/estimate/mutation-types.js @@ -9,7 +9,7 @@ export const RESET_CUSTOMER = 'RESET_CUSTOMER' export const RESET_ITEM = 'RESET_ITEM' export const SET_CUSTOMER = 'SET_CUSTOMER' export const SET_ITEM = 'SET_ITEM' -export const SET_TEMPLATE_ID = 'SET_TEMPLATE_ID' +export const SET_TEMPLATE_NAME = 'SET_TEMPLATE_NAME' export const SELECT_CUSTOMER = 'SELECT_CUSTOMER' export const RESET_SELECTED_CUSTOMER = 'RESET_SELECTED_CUSTOMER' export const SET_SELECT_ALL_STATE = 'SET_SELECT_ALL_STATE' diff --git a/resources/assets/js/store/modules/estimate/mutations.js b/resources/assets/js/store/modules/estimate/mutations.js index dfe77d00..e9d14141 100644 --- a/resources/assets/js/store/modules/estimate/mutations.js +++ b/resources/assets/js/store/modules/estimate/mutations.js @@ -52,8 +52,8 @@ export default { state.selectAllField = false }, - [types.SET_TEMPLATE_ID](state, templateId) { - state.estimateTemplateId = templateId + [types.SET_TEMPLATE_NAME](state, templateName) { + state.estimateTemplateName = templateName }, [types.SELECT_CUSTOMER](state, data) { diff --git a/resources/assets/js/store/modules/invoice/actions.js b/resources/assets/js/store/modules/invoice/actions.js index 5e6793d7..8ae462d4 100644 --- a/resources/assets/js/store/modules/invoice/actions.js +++ b/resources/assets/js/store/modules/invoice/actions.js @@ -21,7 +21,7 @@ export const fetchInvoice = ({ commit, dispatch, state }, id) => { window.axios .get(`/api/v1/invoices/${id}`) .then((response) => { - commit(types.SET_TEMPLATE_ID, response.data.invoice.invoice_template_id) + commit(types.SET_TEMPLATE_NAME, response.data.invoice.template_name) resolve(response) }) .catch((err) => { @@ -219,7 +219,7 @@ export const resetCustomer = ({ commit, dispatch, state }) => { export const setTemplate = ({ commit, dispatch, state }, data) => { return new Promise((resolve, reject) => { - commit(types.SET_TEMPLATE_ID, data) + commit(types.SET_TEMPLATE_NAME, data) resolve({}) }) } diff --git a/resources/assets/js/store/modules/invoice/getters.js b/resources/assets/js/store/modules/invoice/getters.js index efbcba5a..ac2dac69 100644 --- a/resources/assets/js/store/modules/invoice/getters.js +++ b/resources/assets/js/store/modules/invoice/getters.js @@ -1,6 +1,6 @@ export const invoices = (state) => state.invoices export const selectAllField = (state) => state.selectAllField -export const getTemplateId = (state) => state.invoiceTemplateId +export const getTemplateName = (state) => state.invoiceTemplateName export const selectedInvoices = (state) => state.selectedInvoices export const totalInvoices = (state) => state.totalInvoices export const selectedCustomer = (state) => state.selectedCustomer diff --git a/resources/assets/js/store/modules/invoice/index.js b/resources/assets/js/store/modules/invoice/index.js index 3ffdb7e6..011cb651 100644 --- a/resources/assets/js/store/modules/invoice/index.js +++ b/resources/assets/js/store/modules/invoice/index.js @@ -4,7 +4,7 @@ import * as getters from './getters' const initialState = { invoices: [], - invoiceTemplateId: 1, + invoiceTemplateName: null, selectedInvoices: [], selectAllField: false, totalInvoices: 0, diff --git a/resources/assets/js/store/modules/invoice/mutation-types.js b/resources/assets/js/store/modules/invoice/mutation-types.js index 35c4c107..80866fab 100644 --- a/resources/assets/js/store/modules/invoice/mutation-types.js +++ b/resources/assets/js/store/modules/invoice/mutation-types.js @@ -12,7 +12,7 @@ export const SET_CUSTOMER = 'SET_CUSTOMER' export const RESET_ITEM = 'RESET_ITEM' export const SET_ITEM = 'SET_ITEM' -export const SET_TEMPLATE_ID = 'SET_TEMPLATE_ID' +export const SET_TEMPLATE_NAME = 'SET_TEMPLATE_NAME' export const SELECT_CUSTOMER = 'SELECT_CUSTOMER' export const RESET_SELECTED_CUSTOMER = 'RESET_SELECTED_CUSTOMER' diff --git a/resources/assets/js/store/modules/invoice/mutations.js b/resources/assets/js/store/modules/invoice/mutations.js index 2a47ccff..631dee91 100644 --- a/resources/assets/js/store/modules/invoice/mutations.js +++ b/resources/assets/js/store/modules/invoice/mutations.js @@ -51,8 +51,8 @@ export default { state.selectedInvoices = [] }, - [types.SET_TEMPLATE_ID](state, templateId) { - state.invoiceTemplateId = templateId + [types.SET_TEMPLATE_NAME](state, templateName) { + state.invoiceTemplateName = templateName }, [types.SELECT_CUSTOMER](state, data) { diff --git a/resources/assets/js/views/estimates/Create.vue b/resources/assets/js/views/estimates/Create.vue index 134b5e67..0cc03220 100644 --- a/resources/assets/js/views/estimates/Create.vue +++ b/resources/assets/js/views/estimates/Create.vue @@ -266,8 +266,8 @@ variant="gray" @click="openTemplateModal" > - - {{ $t('estimates.estimate_template') }} {{ getTemplateId }} + + {{ getTemplateName }} @@ -514,9 +514,9 @@ export default { ...mapGetters('company', ['defaultCurrency']), ...mapGetters('estimate', [ - 'getTemplateId', 'selectedCustomer', 'selectedNote', + 'getTemplateName', ]), ...mapGetters('estimateTemplate', ['getEstimateTemplates']), @@ -708,6 +708,7 @@ export default { 'selectCustomer', 'updateEstimate', 'resetSelectedNote', + 'setTemplate', ]), ...mapActions('item', ['fetchItems']), @@ -789,6 +790,7 @@ export default { this.estimateNumAttribute = res4.data.nextNumber this.estimatePrefix = res4.data.prefix } + this.setTemplate(this.getEstimateTemplates[0].name) } else { this.estimatePrefix = res4.data.prefix } @@ -903,7 +905,7 @@ export default { total: this.total, tax: this.totalTax, user_id: null, - estimate_template_id: this.getTemplateId, + template_name: this.getTemplateName, } if (this.selectedCustomer != null) { diff --git a/resources/assets/js/views/estimates/CustomerSelect.vue b/resources/assets/js/views/estimates/CustomerSelect.vue index db820fcb..52b7f3e1 100644 --- a/resources/assets/js/views/estimates/CustomerSelect.vue +++ b/resources/assets/js/views/estimates/CustomerSelect.vue @@ -183,7 +183,7 @@ export default { }, computed: { - ...mapGetters('estimate', ['getTemplateId', 'selectedCustomer']), + ...mapGetters('estimate', ['getTemplateName', 'selectedCustomer']), }, created() { diff --git a/resources/assets/js/views/invoices/Create.vue b/resources/assets/js/views/invoices/Create.vue index 00f8508b..45a3546c 100644 --- a/resources/assets/js/views/invoices/Create.vue +++ b/resources/assets/js/views/invoices/Create.vue @@ -259,7 +259,7 @@ @click="openTemplateModal" > - {{ $t('invoices.template') }} {{ getTemplateId }} + {{ getTemplateName }} @@ -510,7 +510,7 @@ export default { ...mapGetters('notes', ['notes']), ...mapGetters('invoice', [ - 'getTemplateId', + 'getTemplateName', 'selectedCustomer', 'selectedNote', ]), @@ -710,6 +710,7 @@ export default { 'selectCustomer', 'updateInvoice', 'resetSelectedNote', + 'setTemplate', ]), ...mapActions('invoiceTemplate', ['fetchInvoiceTemplates']), @@ -784,6 +785,7 @@ export default { this.invoiceNumAttribute = res4.data.nextNumber this.invoicePrefix = res4.data.prefix } + this.setTemplate(this.getInvoiceTemplates[0].name) } else { this.invoicePrefix = res4.data.prefix } @@ -899,7 +901,7 @@ export default { total: this.total, tax: this.totalTax, user_id: null, - invoice_template_id: this.getTemplateId, + template_name: this.getTemplateName, } if (this.selectedCustomer != null) { diff --git a/resources/assets/js/views/invoices/CustomerSelect.vue b/resources/assets/js/views/invoices/CustomerSelect.vue index cb50d6f6..34cb8373 100644 --- a/resources/assets/js/views/invoices/CustomerSelect.vue +++ b/resources/assets/js/views/invoices/CustomerSelect.vue @@ -185,7 +185,7 @@ export default { }, computed: { - ...mapGetters('invoice', ['getTemplateId', 'selectedCustomer']), + ...mapGetters('invoice', ['getTemplateName', 'selectedCustomer']), }, created() { diff --git a/tests/Feature/EstimateTest.php b/tests/Feature/EstimateTest.php index bff55112..64d7e25d 100644 --- a/tests/Feature/EstimateTest.php +++ b/tests/Feature/EstimateTest.php @@ -51,7 +51,7 @@ test('create estimate', function () { ->assertStatus(200); $this->assertDatabaseHas('estimates', [ - 'estimate_template_id' => $estimate['estimate_template_id'], + 'template_name' => $estimate['template_name'], 'estimate_number' => $estimate['estimate_number'], 'discount_type' => $estimate['discount_type'], 'discount_val' => $estimate['discount_val'], @@ -97,7 +97,7 @@ test('update estimate', function () { $newEstimate = $response->decodeResponseJson()['estimate']; $this->assertDatabaseHas('estimates', [ - 'estimate_template_id' => $estimate2['estimate_template_id'], + 'template_name' => $estimate2['template_name'], 'estimate_number' => $estimate2['estimate_number'], 'discount_type' => $estimate2['discount_type'], 'discount_val' => $estimate2['discount_val'], diff --git a/tests/Feature/InvoiceTest.php b/tests/Feature/InvoiceTest.php index 8e2bd17e..cd213197 100644 --- a/tests/Feature/InvoiceTest.php +++ b/tests/Feature/InvoiceTest.php @@ -46,7 +46,7 @@ test('create invoice', function () { $response->assertOk(); $this->assertDatabaseHas('invoices', [ - 'invoice_template_id' => $invoice['invoice_template_id'], + 'template_name' => $invoice['template_name'], 'invoice_number' => $invoice['invoice_number'], 'sub_total' => $invoice['sub_total'], 'discount' => $invoice['discount'], @@ -78,7 +78,7 @@ test('create invoice as sent', function () { 'tax' => $invoice['tax'], 'discount' => $invoice['discount'], 'user_id' => $invoice['user_id'], - 'invoice_template_id' => $invoice['invoice_template_id'], + 'template_name' => $invoice['template_name'], ]); $this->assertDatabaseHas('invoice_items', $invoice['items'][0]); @@ -115,7 +115,7 @@ test('update invoice', function () { 'tax' => $invoice2['tax'], 'discount' => $invoice2['discount'], 'user_id' => $invoice2['user_id'], - 'invoice_template_id' => $invoice2['invoice_template_id'], + 'template_name' => $invoice2['template_name'], ]); $this->assertDatabaseHas('invoice_items', $invoice2['items'][0]); diff --git a/tests/Unit/EstimateTemplateTest.php b/tests/Unit/EstimateTemplateTest.php deleted file mode 100644 index 7c6aa983..00000000 --- a/tests/Unit/EstimateTemplateTest.php +++ /dev/null @@ -1,28 +0,0 @@ - 'DatabaseSeeder', '--force' => true]); - Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]); - - $user = User::where('role', 'super admin')->first(); - $this->withHeaders([ - 'company' => $user->company_id, - ]); - Sanctum::actingAs( - $user, - ['*'] - ); -}); - -test('estimate template has many estimates', function () { - $estimateTemplate = EstimateTemplate::factory()->hasEstimates(5)->create(); - - $this->assertCount(5, $estimateTemplate->estimates); - - $this->assertTrue($estimateTemplate->estimates()->exists()); -}); diff --git a/tests/Unit/EstimateTest.php b/tests/Unit/EstimateTest.php index 83d95fa6..8f79a6ae 100644 --- a/tests/Unit/EstimateTest.php +++ b/tests/Unit/EstimateTest.php @@ -46,13 +46,6 @@ test('estimate has many taxes', function () { $this->assertTrue($estimate->taxes()->exists()); }); -test('estimate belongs to estimate template', function () { - $estimate = Estimate::factory()->forEstimateTemplate()->create(); - - $this->assertTrue($estimate->estimateTemplate()->exists()); -}); - - test('get next estimate number', function () { $estimate = Estimate::factory()->create(); @@ -114,7 +107,7 @@ test('create estimate', function () { $this->assertDatabaseHas('estimates', [ 'estimate_number' => $estimate['estimate_number'], 'user_id' => $estimate['user_id'], - 'estimate_template_id' => $estimate['estimate_template_id'], + 'template_name' => $estimate['template_name'], 'sub_total' => $estimate['sub_total'], 'total' => $estimate['total'], 'discount' => $estimate['discount'], @@ -162,7 +155,7 @@ test('update estimate', function () { $this->assertDatabaseHas('estimates', [ 'estimate_number' => $newEstimate['estimate_number'], 'user_id' => $newEstimate['user_id'], - 'estimate_template_id' => $newEstimate['estimate_template_id'], + 'template_name' => $newEstimate['template_name'], 'sub_total' => $newEstimate['sub_total'], 'total' => $newEstimate['total'], 'discount' => $newEstimate['discount'], diff --git a/tests/Unit/InvoiceTemplateTest.php b/tests/Unit/InvoiceTemplateTest.php deleted file mode 100644 index ebda8117..00000000 --- a/tests/Unit/InvoiceTemplateTest.php +++ /dev/null @@ -1,28 +0,0 @@ - 'DatabaseSeeder', '--force' => true]); - Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]); - - $user = User::where('role', 'super admin')->first(); - $this->withHeaders([ - 'company' => $user->company_id, - ]); - Sanctum::actingAs( - $user, - ['*'] - ); -}); - -test('invoice template has many invoices', function () { - $invoiceTemplate = InvoiceTemplate::factory()->hasInvoices(5)->create(); - - $this->assertCount(5, $invoiceTemplate->invoices); - - $this->assertTrue($invoiceTemplate->invoices()->exists()); -}); diff --git a/tests/Unit/InvoiceTest.php b/tests/Unit/InvoiceTest.php index 2a8bfcad..0ecac70b 100644 --- a/tests/Unit/InvoiceTest.php +++ b/tests/Unit/InvoiceTest.php @@ -54,12 +54,6 @@ test('invoice belongs to user', function () { $this->assertTrue($invoice->user()->exists()); }); -test('invoice belongs to invoice template', function () { - $invoice = Invoice::factory()->forInvoiceTemplate()->create(); - - $this->assertTrue($invoice->invoiceTemplate()->exists()); -}); - test('get next invoice number', function () { $invoice = Invoice::factory()->create(); @@ -140,7 +134,7 @@ test('create invoice', function () { 'discount' => $invoice['discount'], 'notes' => $invoice['notes'], 'user_id' => $invoice['user_id'], - 'invoice_template_id' => $invoice['invoice_template_id'], + 'template_name' => $invoice['template_name'], ]); }); @@ -191,7 +185,7 @@ test('update invoice', function () { 'discount' => $newInvoice['discount'], 'notes' => $newInvoice['notes'], 'user_id' => $newInvoice['user_id'], - 'invoice_template_id' => $newInvoice['invoice_template_id'], + 'template_name' => $newInvoice['template_name'], ]); }); diff --git a/tests/Unit/Request/EstimateTest.php b/tests/Unit/Request/EstimateTest.php index 6b788854..911c6005 100644 --- a/tests/Unit/Request/EstimateTest.php +++ b/tests/Unit/Request/EstimateTest.php @@ -33,8 +33,8 @@ test('estimate request validation rules', function () { 'tax' => [ 'required', ], - 'estimate_template_id' => [ - 'required', + 'template_name' => [ + 'required' ], 'items' => [ 'required', diff --git a/tests/Unit/Request/InvoiceTest.php b/tests/Unit/Request/InvoiceTest.php index 26b6f03a..084ce4ed 100644 --- a/tests/Unit/Request/InvoiceTest.php +++ b/tests/Unit/Request/InvoiceTest.php @@ -33,8 +33,8 @@ test('invoice request validation rules', function () { 'tax' => [ 'required', ], - 'invoice_template_id' => [ - 'required', + 'template_name' => [ + 'required' ], 'items' => [ 'required',