From b00e0b5a04edc839bb9556f5d86d23b0fbeeb79a Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 26 Oct 2020 07:56:02 +1100 Subject: [PATCH 1/9] Fixes for first_load includes --- app/Http/Controllers/BaseController.php | 11 ++++++++++- app/Models/Company.php | 14 ++++++++++---- app/Transformers/CompanyTransformer.php | 20 ++++++++++++++++++++ 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/BaseController.php b/app/Http/Controllers/BaseController.php index bad4f90c69..2c51f6a6ca 100644 --- a/app/Http/Controllers/BaseController.php +++ b/app/Http/Controllers/BaseController.php @@ -60,9 +60,12 @@ class BaseController extends Controller private $first_load = [ 'account', - 'token.company_user', + 'user.company_user.token.company_user', + //'token.company_user', 'company.activities', 'company.designs.company', + 'company.task_statuses', + 'company.expense_categories', 'company.documents', 'company.users.company_users', 'company.clients.contacts', @@ -257,6 +260,12 @@ class BaseController extends Controller 'company.vendors'=> function ($query) use ($updated_at) { $query->where('updated_at', '>=', $updated_at)->with('contacts','documents' ); }, + 'company.expense_categories'=> function ($query) use ($updated_at) { + $query->where('updated_at', '>=', $updated_at); + }, + 'company.task_statuses'=> function ($query) use ($updated_at) { + $query->where('updated_at', '>=', $updated_at); + }, ] ); diff --git a/app/Models/Company.php b/app/Models/Company.php index af1efb90ed..00a7ef9c0f 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -165,14 +165,20 @@ class Company extends BaseModel return $this->hasManyThrough(User::class, CompanyUser::class, 'company_id', 'id', 'id', 'user_id'); } - /** - * @return \Illuminate\Database\Eloquent\Relations\HasMany - */ + public function expense_categories() + { + return $this->hasMany(ExpenseCategory::class)->withTrashed(); + } + + public function task_statuses() + { + return $this->hasMany(TaskStatus::class)->withTrashed(); + } + public function clients() { return $this->hasMany(Client::class)->withTrashed(); } - /** * @return \Illuminate\Database\Eloquent\Relations\HasMany */ diff --git a/app/Transformers/CompanyTransformer.php b/app/Transformers/CompanyTransformer.php index 6bba99350e..6427c10336 100644 --- a/app/Transformers/CompanyTransformer.php +++ b/app/Transformers/CompanyTransformer.php @@ -23,6 +23,7 @@ use App\Models\Credit; use App\Models\Design; use App\Models\Document; use App\Models\Expense; +use App\Models\ExpenseCategory; use App\Models\GroupSetting; use App\Models\Payment; use App\Models\PaymentTerm; @@ -32,6 +33,7 @@ use App\Models\Quote; use App\Models\RecurringInvoice; use App\Models\SystemLog; use App\Models\Task; +use App\Models\TaskStatus; use App\Models\TaxRate; use App\Models\User; use App\Models\Webhook; @@ -40,9 +42,11 @@ use App\Transformers\CompanyTokenHashedTransformer; use App\Transformers\CompanyTokenTransformer; use App\Transformers\CreditTransformer; use App\Transformers\DocumentTransformer; +use App\Transformers\ExpenseCategoryTransformer; use App\Transformers\PaymentTermTransformer; use App\Transformers\RecurringInvoiceTransformer; use App\Transformers\SystemLogTransformer; +use App\Transformers\TaskStatusTransformer; use App\Transformers\TaskTransformer; use App\Transformers\WebhookTransformer; use App\Utils\Traits\MakesHash; @@ -95,6 +99,8 @@ class CompanyTransformer extends EntityTransformer 'tokens', 'tokens_hashed', 'system_logs', + 'expense_categories', + 'task_statuses', ]; /** @@ -151,6 +157,20 @@ class CompanyTransformer extends EntityTransformer ]; } + public function includeExpenseCategories(Company $company) + { + $transformer = new ExpenseCategoryTransformer($this->serializer); + + return $this->includeCollection($company->expense_categories, $transformer, ExpenseCategory::class); + } + + public function includeTaskStatuses(Company $company) + { + $transformer = new TaskStatusTransformer($this->serializer); + + return $this->includeCollection($company->task_statuses, $transformer, TaskStatus::class); + } + public function includeDocuments(Company $company) { $transformer = new DocumentTransformer($this->serializer); From 5ffd2b4678f6cef595b1437180e86c220c5dfb4b Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 26 Oct 2020 08:05:33 +1100 Subject: [PATCH 2/9] Add sort order to task status --- app/Transformers/TaskStatusTransformer.php | 1 + app/Utils/HtmlEngine.php | 16 ++++++++-------- .../2020_10_22_204900_company_table_fields.php | 4 ++++ 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/app/Transformers/TaskStatusTransformer.php b/app/Transformers/TaskStatusTransformer.php index f96f33a4fa..f312559515 100644 --- a/app/Transformers/TaskStatusTransformer.php +++ b/app/Transformers/TaskStatusTransformer.php @@ -23,6 +23,7 @@ class TaskStatusTransformer extends EntityTransformer return [ 'id' => (string) $this->encodePrimaryKey($task_status->id), 'name' => (string) $task_status->name, + 'sort_order' => (int) $task_status->sort_order, 'is_deleted' => (bool) $task_status->is_deleted, 'created_at' => (int) $task_status->created_at, 'updated_at' => (int) $task_status->updated_at, diff --git a/app/Utils/HtmlEngine.php b/app/Utils/HtmlEngine.php index 94a36c88b9..401a173d55 100644 --- a/app/Utils/HtmlEngine.php +++ b/app/Utils/HtmlEngine.php @@ -210,10 +210,10 @@ class HtmlEngine // $data['$details'] = ; $data['$invoice_no'] = &$data['$number']; $data['$invoice.invoice_no'] = &$data['$number']; - $data['$client1'] = ['value' => $this->client->custom_value1 ?: ' ', 'label' => $this->makeCustomField('client1')]; - $data['$client2'] = ['value' => $this->client->custom_value2 ?: ' ', 'label' => $this->makeCustomField('client2')]; - $data['$client3'] = ['value' => $this->client->custom_value3 ?: ' ', 'label' => $this->makeCustomField('client3')]; - $data['$client4'] = ['value' => $this->client->custom_value4 ?: ' ', 'label' => $this->makeCustomField('client4')]; + $data['$client1'] = ['value' => $this->formatCustomFieldValue('client1', $this->client->custom_value1) ?: ' ', 'label' => $this->makeCustomField('client1')]; + $data['$client2'] = ['value' => $this->formatCustomFieldValue('client2', $this->client->custom_value2) ?: ' ', 'label' => $this->makeCustomField('client2')]; + $data['$client3'] = ['value' => $this->formatCustomFieldValue('client3', $this->client->custom_value3) ?: ' ', 'label' => $this->makeCustomField('client3')]; + $data['$client4'] = ['value' => $this->formatCustomFieldValue('client4', $this->client->custom_value4) ?: ' ', 'label' => $this->makeCustomField('client4')]; $data['$address1'] = ['value' => $this->client->address1 ?: ' ', 'label' => ctrans('texts.address1')]; $data['$address2'] = ['value' => $this->client->address2 ?: ' ', 'label' => ctrans('texts.address2')]; $data['$id_number'] = ['value' => $this->client->id_number ?: ' ', 'label' => ctrans('texts.id_number')]; @@ -276,10 +276,10 @@ class HtmlEngine $data['$company.logo'] = ['value' => $logo ?: ' ', 'label' => ctrans('texts.logo')]; $data['$company_logo'] = &$data['$company.logo']; - $data['$company1'] = ['value' => $this->settings->custom_value1 ?: ' ', 'label' => $this->makeCustomField('company1')]; - $data['$company2'] = ['value' => $this->settings->custom_value2 ?: ' ', 'label' => $this->makeCustomField('company2')]; - $data['$company3'] = ['value' => $this->settings->custom_value3 ?: ' ', 'label' => $this->makeCustomField('company3')]; - $data['$company4'] = ['value' => $this->settings->custom_value4 ?: ' ', 'label' => $this->makeCustomField('company4')]; + $data['$company1'] = ['value' => $this->formatCustomFieldValue('company1', $this->settings->custom_value1) ?: ' ', 'label' => $this->makeCustomField('company1')]; + $data['$company2'] = ['value' => $this->formatCustomFieldValue('company2', $this->settings->custom_value2) ?: ' ', 'label' => $this->makeCustomField('company2')]; + $data['$company3'] = ['value' => $this->formatCustomFieldValue('company3', $this->settings->custom_value3) ?: ' ', 'label' => $this->makeCustomField('company3')]; + $data['$company4'] = ['value' => $this->formatCustomFieldValue('company4', $this->settings->custom_value4) ?: ' ', 'label' => $this->makeCustomField('company4')]; $data['$custom_surcharge1'] = ['value' => $this->entity->custom_surcharge1 ?: ' ', 'label' => $this->makeCustomField('custom_surcharge1')]; $data['$custom_surcharge2'] = ['value' => $this->entity->custom_surcharge2 ?: ' ', 'label' => $this->makeCustomField('custom_surcharge2')]; diff --git a/database/migrations/2020_10_22_204900_company_table_fields.php b/database/migrations/2020_10_22_204900_company_table_fields.php index 14d8bd57fa..9c43fb278a 100644 --- a/database/migrations/2020_10_22_204900_company_table_fields.php +++ b/database/migrations/2020_10_22_204900_company_table_fields.php @@ -16,6 +16,10 @@ class CompanyTableFields extends Migration Schema::table('companies', function(Blueprint $table){ $table->boolean('invoice_task_timelog')->default(true); }); + + Schema::table('task_statuses', function(Blueprint $table){ + $table->unsignedInteger('sort_order')->default(0); + }); } /** From 8b3fafa19c9a6b4f76122610f25bfb8008fd8a18 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 26 Oct 2020 08:16:07 +1100 Subject: [PATCH 3/9] Fixes for first_load --- app/Http/Controllers/BaseController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/BaseController.php b/app/Http/Controllers/BaseController.php index 2c51f6a6ca..9a50d97c4f 100644 --- a/app/Http/Controllers/BaseController.php +++ b/app/Http/Controllers/BaseController.php @@ -67,7 +67,7 @@ class BaseController extends Controller 'company.task_statuses', 'company.expense_categories', 'company.documents', - 'company.users.company_users', + 'company.users.company_user', 'company.clients.contacts', 'company.clients.gateway_tokens', 'company.clients.documents', From 1b2922b57c51ab426beb8078bed3f562a71f3db9 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 26 Oct 2020 11:58:08 +1100 Subject: [PATCH 4/9] credit payment type --- app/Http/Controllers/BaseController.php | 13 +- .../Requests/Project/StoreProjectRequest.php | 6 +- .../Requests/Project/UpdateProjectRequest.php | 6 +- app/Http/Requests/Task/StoreTaskRequest.php | 42 +----- app/Http/Requests/Task/UpdateTaskRequest.php | 25 +--- app/Jobs/Entity/CreateEntityPdf.php | 140 ++++++++++++++++++ app/Models/Client.php | 2 + database/seeders/PaymentTypesSeeder.php | 2 + resources/lang/en/texts.php | 2 + 9 files changed, 163 insertions(+), 75 deletions(-) create mode 100644 app/Jobs/Entity/CreateEntityPdf.php diff --git a/app/Http/Controllers/BaseController.php b/app/Http/Controllers/BaseController.php index 9a50d97c4f..2df8db9190 100644 --- a/app/Http/Controllers/BaseController.php +++ b/app/Http/Controllers/BaseController.php @@ -60,15 +60,15 @@ class BaseController extends Controller private $first_load = [ 'account', - 'user.company_user.token.company_user', - //'token.company_user', + 'user.company_user', + 'token.company_user', 'company.activities', 'company.designs.company', 'company.task_statuses', 'company.expense_categories', 'company.documents', 'company.users.company_user', - 'company.clients.contacts', + 'company.clients.contacts.company', 'company.clients.gateway_tokens', 'company.clients.documents', 'company.company_gateways.gateway', @@ -93,11 +93,11 @@ class BaseController extends Controller 'company.quotes.invitations.contact', 'company.quotes.invitations.company', 'company.quotes.documents', - 'company.tasks', 'company.tasks.documents', 'company.tax_rates', 'company.tokens_hashed', - 'company.vendors.contacts', + 'company.vendors.contacts.company', + 'company.vendors.documents', 'company.webhooks', ]; @@ -427,7 +427,8 @@ class BaseController extends Controller public function flutterRoute() { - if ((bool) $this->checkAppSetup() !== false && Schema::hasTable('accounts') && $account = Account::first()) { + // if ((bool) $this->checkAppSetup() !== false && Schema::hasTable('accounts') && $account = Account::first()) { + if ((bool) $this->checkAppSetup() !== false && $account = Account::first()) { if (config('ninja.require_https') && ! request()->isSecure()) { return redirect()->secure(request()->getRequestUri()); } diff --git a/app/Http/Requests/Project/StoreProjectRequest.php b/app/Http/Requests/Project/StoreProjectRequest.php index 6070d11fb8..a1c3d0bcf5 100644 --- a/app/Http/Requests/Project/StoreProjectRequest.php +++ b/app/Http/Requests/Project/StoreProjectRequest.php @@ -42,12 +42,8 @@ class StoreProjectRequest extends Request protected function prepareForValidation() { - $input = $this->all(); + $input = $this->decodePrimaryKeys($this->all()); - if (array_key_exists('client_id', $input) && is_string($input['client_id'])) { - $input['client_id'] = $this->decodePrimaryKey($input['client_id']); - } - $this->replace($input); } diff --git a/app/Http/Requests/Project/UpdateProjectRequest.php b/app/Http/Requests/Project/UpdateProjectRequest.php index 2382abfc38..3c4c3892f1 100644 --- a/app/Http/Requests/Project/UpdateProjectRequest.php +++ b/app/Http/Requests/Project/UpdateProjectRequest.php @@ -36,12 +36,8 @@ class UpdateProjectRequest extends Request protected function prepareForValidation() { - $input = $this->all(); + $input = $this->decodePrimaryKeys($this->all()); - if (array_key_exists('client_id', $input) && is_string($input['client_id'])) { - unset($input['client_id']); - } - $this->replace($input); } } diff --git a/app/Http/Requests/Task/StoreTaskRequest.php b/app/Http/Requests/Task/StoreTaskRequest.php index 1c1a2b23d1..5a26e729a8 100644 --- a/app/Http/Requests/Task/StoreTaskRequest.php +++ b/app/Http/Requests/Task/StoreTaskRequest.php @@ -37,49 +37,17 @@ class StoreTaskRequest extends Request public function rules() { $rules = []; - /* Ensure we have a client name, and that all emails are unique*/ - //$rules['name'] = 'required|min:1'; - //$rules['client_id'] = 'required|exists:clients,id,company_id,'.auth()->user()->company()->id; - - // $rules['number'] = new UniqueTaskNumberRule($this->all()); - - - return $rules; + + return $this->globalRules($rules); } protected function prepareForValidation() { - $input = $this->all(); + $input = $this->all(); - if (array_key_exists('design_id', $input) && is_string($input['design_id'])) { - $input['design_id'] = $this->decodePrimaryKey($input['design_id']); - } + $input = $this->decodePrimaryKeys($this->all()); - if (array_key_exists('client_id', $input) && is_string($input['client_id'])) { - $input['client_id'] = $this->decodePrimaryKey($input['client_id']); - } - - if (array_key_exists('assigned_user_id', $input) && is_string($input['assigned_user_id'])) { - $input['assigned_user_id'] = $this->decodePrimaryKey($input['assigned_user_id']); - } - - if (array_key_exists('project_id', $input) && is_string($input['project_id'])) { - $input['project_id'] = $this->decodePrimaryKey($input['project_id']); - } - - if (array_key_exists('invoice_id', $input) && is_string($input['invoice_id'])) { - $input['invoice_id'] = $this->decodePrimaryKey($input['invoice_id']); - } - - $this->replace($input); + $this->replace($input); } - // public function messages() - // { - // // return [ - // // 'unique' => ctrans('validation.unique', ['attribute' => 'email']), - // // //'required' => trans('validation.required', ['attribute' => 'email']), - // // 'contacts.*.email.required' => ctrans('validation.email', ['attribute' => 'email']), - // // ]; - // } } diff --git a/app/Http/Requests/Task/UpdateTaskRequest.php b/app/Http/Requests/Task/UpdateTaskRequest.php index 9d03ab13c4..a7f2b3311f 100644 --- a/app/Http/Requests/Task/UpdateTaskRequest.php +++ b/app/Http/Requests/Task/UpdateTaskRequest.php @@ -43,7 +43,7 @@ class UpdateTaskRequest extends Request $rules['number'] = 'unique:tasks,number,'.$this->id.',id,company_id,'.$this->taskss->company_id; } - return $rules; + return $this->globalRules($rules); } // public function messages() @@ -58,28 +58,9 @@ class UpdateTaskRequest extends Request protected function prepareForValidation() { - $input = $this->all(); - if (array_key_exists('design_id', $input) && is_string($input['design_id'])) { - $input['design_id'] = $this->decodePrimaryKey($input['design_id']); - } + $input = $this->decodePrimaryKeys($this->all()); - if (array_key_exists('client_id', $input) && is_string($input['client_id'])) { - $input['client_id'] = $this->decodePrimaryKey($input['client_id']); - } - - if (array_key_exists('assigned_user_id', $input) && is_string($input['assigned_user_id'])) { - $input['assigned_user_id'] = $this->decodePrimaryKey($input['assigned_user_id']); - } - - if (array_key_exists('project_id', $input) && is_string($input['project_id'])) { - $input['project_id'] = $this->decodePrimaryKey($input['project_id']); - } - - if (array_key_exists('invoice_id', $input) && is_string($input['invoice_id'])) { - $input['invoice_id'] = $this->decodePrimaryKey($input['invoice_id']); - } - - $this->replace($input); + $this->replace($input); } } diff --git a/app/Jobs/Entity/CreateEntityPdf.php b/app/Jobs/Entity/CreateEntityPdf.php new file mode 100644 index 0000000000..5b7ea22278 --- /dev/null +++ b/app/Jobs/Entity/CreateEntityPdf.php @@ -0,0 +1,140 @@ +invitation = $invitation; + + if($invitation->invoice) + $this->entity = $invitation->invoice; + elseif($invitation->quote) + $this->entity = $invitation->quote; + elseif($invitation->credit)) + $this->entity = $invitation->credit; + + $this->company = $invitation->company; + + $this->contact = $invitation->contact; + + $this->disk = $disk ?? config('filesystems.default'); + } + + public function handle() + { + + if (config('ninja.phantomjs_key')) { + return (new Phantom)->generate($this->invitation); + } + + App::setLocale($this->contact->preferredLocale()); + + if($this->entity instanceof Invoice) + $path = $this->entity->client->invoice_filepath(); + elseif($this->entity instanceof Quote) + $path = $this->entity->client->quote_filepath(); + elseif($this->entity instanceof Credit) + $path = $this->entity->client->credit_filepath(); + + $file_path = $path.$this->entity->number.'.pdf'; + + $entity_design_id = $this->entity->design_id ? $this->entity->design_id : $this->decodePrimaryKey($this->entity->client->getSetting('invoice_design_id')); + + $design = Design::find($entity_design_id); + $html = new HtmlEngine(null, $this->invitation, 'entity'); + + if ($design->is_custom) { + $options = [ + 'custom_partials' => json_decode(json_encode($design->design), true) + ]; + $template = new PdfMakerDesign(PdfDesignModel::CUSTOM, $options); + } else { + $template = new PdfMakerDesign(strtolower($design->name)); + } + + $state = [ + 'template' => $template->elements([ + 'client' => $this->entity->client, + 'entity' => $this->entity, + 'pdf_variables' => (array) $this->entity->company->settings->pdf_variables, + 'products' => $design->design->product, + ]), + 'variables' => $html->generateLabelsAndValues(), + 'options' => [ + 'all_pages_header' => $this->entity->client->getSetting('all_pages_header'), + 'all_pages_footer' => $this->entity->client->getSetting('all_pages_footer'), + ], + ]; + + $maker = new PdfMakerService($state); + + $maker + ->design($template) + ->build(); + + //todo - move this to the client creation stage so we don't keep hitting this unnecessarily + Storage::makeDirectory($path, 0775); + + $pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true)); + + $instance = Storage::disk($this->disk)->put($file_path, $pdf); + + return $file_path; + } +} diff --git a/app/Models/Client.php b/app/Models/Client.php index 86d4fd90b6..c65010d1d6 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -98,6 +98,8 @@ class Client extends BaseModel implements HasLocalePreference ]; protected $with = [ + 'gateway_tokens', + 'documents' //'currency', // 'primary_contact', // 'country', diff --git a/database/seeders/PaymentTypesSeeder.php b/database/seeders/PaymentTypesSeeder.php index cef35ed5b9..c2756cf52b 100644 --- a/database/seeders/PaymentTypesSeeder.php +++ b/database/seeders/PaymentTypesSeeder.php @@ -31,6 +31,7 @@ class PaymentTypesSeeder extends Seeder const GATEWAY_TYPE_APPLE_PAY = 11; const GATEWAY_TYPE_CUSTOM2 = 12; const GATEWAY_TYPE_CUSTOM3 = 13; + const GATEWAY_TYPE_CREDIT = 13; public function run() { @@ -69,6 +70,7 @@ class PaymentTypesSeeder extends Seeder ['name' => 'SEPA', 'gateway_type_id' => self::GATEWAY_TYPE_SEPA], ['name' => 'GoCardless', 'gateway_type_id' => self::GATEWAY_TYPE_GOCARDLESS], ['name' => 'Crypto', 'gateway_type_id' => self::GATEWAY_TYPE_CRYPTO], + ['name' => 'Credit', 'gateway_type_id' => self::GATEWAY_TYPE_CREDIT], ]; $x = 1; diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index c5ee6460ba..1c8d955e50 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -3285,5 +3285,7 @@ return [ 'credit_subject' => 'New credit :number from :account', 'credit_message' => 'To view your credit for :amount, click the link below.', + 'payment_type_Crypto' => 'Cryptocurrency', + 'payment_type_Credit' => 'Credit', ]; From 651a1a6cbeb376aa4655b8045050792f32c628e3 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 26 Oct 2020 13:06:24 +1100 Subject: [PATCH 5/9] Add credit gateway type id --- app/Services/Invoice/AutoBillInvoice.php | 1 + database/seeders/GatewayTypesSeeder.php | 1 + database/seeders/PaymentTypesSeeder.php | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index 36b296c065..582f4f60fb 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -116,6 +116,7 @@ class AutoBillInvoice extends AbstractService $payment->currency_id = $this->invoice->client->getSetting('currency_id'); $payment->date = now(); $payment->status_id = Payment::STATUS_COMPLETED; + $payment->type_id = PaymentType::CREDIT; $payment->service()->applyNumber()->save(); $payment->invoices()->attach($this->invoice->id, ['amount' => $amount]); diff --git a/database/seeders/GatewayTypesSeeder.php b/database/seeders/GatewayTypesSeeder.php index 25a814132b..8faed88b24 100644 --- a/database/seeders/GatewayTypesSeeder.php +++ b/database/seeders/GatewayTypesSeeder.php @@ -35,6 +35,7 @@ class GatewayTypesSeeder extends Seeder ['id' => 11, 'alias' => 'apple_pay', 'name' => 'Apple Pay'], ['id' => 12, 'alias' => 'custom2', 'name' => 'Custom'], ['id' => 13, 'alias' => 'custom3', 'name' => 'Custom'], + ['id' => 14, 'alias' => 'credit', 'name' => 'Credit'], ]; foreach ($gateway_types as $gateway_type) { diff --git a/database/seeders/PaymentTypesSeeder.php b/database/seeders/PaymentTypesSeeder.php index c2756cf52b..eaae804a78 100644 --- a/database/seeders/PaymentTypesSeeder.php +++ b/database/seeders/PaymentTypesSeeder.php @@ -31,7 +31,7 @@ class PaymentTypesSeeder extends Seeder const GATEWAY_TYPE_APPLE_PAY = 11; const GATEWAY_TYPE_CUSTOM2 = 12; const GATEWAY_TYPE_CUSTOM3 = 13; - const GATEWAY_TYPE_CREDIT = 13; + const GATEWAY_TYPE_CREDIT = 14; public function run() { From 8d508bb1f95d464827f1494d13b2ea93b635de79 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 26 Oct 2020 15:06:58 +1100 Subject: [PATCH 6/9] Refactor pdf generation --- app/Http/Controllers/InvoiceController.php | 1 - app/Http/Controllers/PreviewController.php | 1 - app/Jobs/Entity/CreateEntityPdf.php | 32 ++++++++++++++++++---- app/Listeners/Invoice/CreateInvoicePdf.php | 3 +- app/Models/Credit.php | 6 ++-- app/Models/CreditInvitation.php | 4 +-- app/Models/Invoice.php | 6 ++-- app/Models/InvoiceInvitation.php | 3 +- app/Models/Quote.php | 5 ++-- app/Models/QuoteInvitation.php | 4 +-- app/Services/Credit/GetCreditPdf.php | 5 ++-- app/Services/Invoice/AutoBillInvoice.php | 1 + app/Services/Invoice/GetInvoicePdf.php | 3 +- app/Services/Quote/GetQuotePdf.php | 3 +- app/Utils/HtmlEngine.php | 2 +- 15 files changed, 52 insertions(+), 27 deletions(-) diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index cdbcfffb36..26cf34d647 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -28,7 +28,6 @@ use App\Http\Requests\Invoice\EditInvoiceRequest; use App\Http\Requests\Invoice\ShowInvoiceRequest; use App\Http\Requests\Invoice\StoreInvoiceRequest; use App\Http\Requests\Invoice\UpdateInvoiceRequest; -use App\Jobs\Invoice\CreateInvoicePdf; use App\Jobs\Invoice\EmailInvoice; use App\Jobs\Invoice\StoreInvoice; use App\Jobs\Invoice\ZipInvoices; diff --git a/app/Http/Controllers/PreviewController.php b/app/Http/Controllers/PreviewController.php index 7637e07572..f2fd5a8f46 100644 --- a/app/Http/Controllers/PreviewController.php +++ b/app/Http/Controllers/PreviewController.php @@ -14,7 +14,6 @@ namespace App\Http\Controllers; use App\Designs\Custom; use App\Designs\Designer; use App\Factory\InvoiceFactory; -use App\Jobs\Invoice\CreateInvoicePdf; use App\Jobs\Util\PreviewPdf; use App\Models\Client; use App\Models\ClientContact; diff --git a/app/Jobs/Entity/CreateEntityPdf.php b/app/Jobs/Entity/CreateEntityPdf.php index 5b7ea22278..5363ab986b 100644 --- a/app/Jobs/Entity/CreateEntityPdf.php +++ b/app/Jobs/Entity/CreateEntityPdf.php @@ -18,16 +18,22 @@ use App\Designs\Modern; use App\Libraries\MultiDB; use App\Models\ClientContact; use App\Models\Company; +use App\Models\Credit; +use App\Models\CreditInvitation; use App\Models\Design; use App\Models\Entity; +use App\Models\Invoice; +use App\Models\InvoiceInvitation; use App\Models\Quote; +use App\Models\QuoteInvitation; +use App\Models\RecurringInvoiceInvitation; use App\Services\PdfMaker\Design as PdfDesignModel; use App\Services\PdfMaker\Design as PdfMakerDesign; use App\Services\PdfMaker\PdfMaker as PdfMakerService; use App\Utils\HtmlEngine; use App\Utils\PhantomJS\Phantom; -use App\Utils\Traits\MakesEntityHtml; use App\Utils\Traits\MakesHash; +use App\Utils\Traits\MakesInvoiceHtml; use App\Utils\Traits\NumberFormatter; use App\Utils\Traits\Pdf\PdfMaker; use Illuminate\Bus\Queueable; @@ -41,7 +47,7 @@ use Spatie\Browsershot\Browsershot; class CreateEntityPdf implements ShouldQueue { - use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter, MakesEntityHtml, PdfMaker, MakesHash; + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter, MakesInvoiceHtml, PdfMaker, MakesHash; public $entity; @@ -53,6 +59,8 @@ class CreateEntityPdf implements ShouldQueue public $invitation; + public $entity_string = ''; + /** * Create a new job instance. * @@ -62,12 +70,22 @@ class CreateEntityPdf implements ShouldQueue { $this->invitation = $invitation; - if($invitation->invoice) + if($invitation instanceof InvoiceInvitation){ $this->entity = $invitation->invoice; - elseif($invitation->quote) + $this->entity_string = 'invoice'; + } + elseif($invitation instanceof QuoteInvitation){ $this->entity = $invitation->quote; - elseif($invitation->credit)) + $this->entity_string = 'quote'; + } + elseif($invitation instanceof CreditInvitation){ $this->entity = $invitation->credit; + $this->entity_string = 'credit'; + } + elseif($invitation instanceof RecurringInvoiceInvitation){ + $this->entity = $invitation->recurring_invoice; + $this->entity_string = 'recurring_invoice'; + } $this->company = $invitation->company; @@ -94,10 +112,12 @@ class CreateEntityPdf implements ShouldQueue $file_path = $path.$this->entity->number.'.pdf'; +info($file_path); + $entity_design_id = $this->entity->design_id ? $this->entity->design_id : $this->decodePrimaryKey($this->entity->client->getSetting('invoice_design_id')); $design = Design::find($entity_design_id); - $html = new HtmlEngine(null, $this->invitation, 'entity'); + $html = new HtmlEngine(null, $this->invitation, $this->entity_string); if ($design->is_custom) { $options = [ diff --git a/app/Listeners/Invoice/CreateInvoicePdf.php b/app/Listeners/Invoice/CreateInvoicePdf.php index 824006fe6d..d7a4345793 100644 --- a/app/Listeners/Invoice/CreateInvoicePdf.php +++ b/app/Listeners/Invoice/CreateInvoicePdf.php @@ -11,6 +11,7 @@ namespace App\Listeners\Invoice; +use App\Jobs\Entity\CreateEntityPdf; use App\Jobs\Invoice\CreateInvoicePdf as PdfCreator; use App\Libraries\MultiDB; use Illuminate\Contracts\Queue\ShouldQueue; @@ -38,7 +39,7 @@ class CreateInvoicePdf implements ShouldQueue MultiDB::setDb($event->company->db); $event->invoice->invitations->each(function ($invitation) { - PdfCreator::dispatch($invitation); + CreateEntityPdf::dispatch($invitation); }); } } diff --git a/app/Models/Credit.php b/app/Models/Credit.php index 4c1a0ef259..f1517bb654 100644 --- a/app/Models/Credit.php +++ b/app/Models/Credit.php @@ -14,7 +14,7 @@ namespace App\Models; use App\Events\Credit\CreditWasUpdated; use App\Helpers\Invoice\InvoiceSum; use App\Helpers\Invoice\InvoiceSumInclusive; -use App\Jobs\Credit\CreateCreditPdf; +use App\Jobs\Entity\CreateEntityPdf; use App\Models\Filterable; use App\Services\Credit\CreditService; use App\Services\Ledger\LedgerService; @@ -244,10 +244,10 @@ class Credit extends BaseModel if (! $invitation) { event(new CreditWasUpdated($this, $this->company, Ninja::eventVars())); - CreateCreditPdf::dispatchNow($this, $this->company, $this->client->primary_contact()->first()); + CreateEntityPdf::dispatchNow($this, $this->company, $this->client->primary_contact()->first()); } else { event(new CreditWasUpdated($this, $this->company, Ninja::eventVars())); - CreateCreditPdf::dispatchNow($invitation->credit, $invitation->company, $invitation->contact); + CreateEntityPdf::dispatchNow($invitation->credit, $invitation->company, $invitation->contact); } return $storage_path; diff --git a/app/Models/CreditInvitation.php b/app/Models/CreditInvitation.php index 7cc5df13d8..02f5428c7e 100644 --- a/app/Models/CreditInvitation.php +++ b/app/Models/CreditInvitation.php @@ -12,7 +12,7 @@ namespace App\Models; use App\Events\Credit\CreditWasUpdated; -use App\Jobs\Credit\CreateCreditPdf; +use App\Jobs\Entity\CreateEntityPdf; use App\Models\Invoice; use App\Utils\Ninja; use App\Utils\Traits\Inviteable; @@ -131,7 +131,7 @@ class CreditInvitation extends BaseModel if (! Storage::exists($this->credit->client->credit_filepath().$this->credit->number.'.pdf')) { event(new CreditWasUpdated($this, $this->company, Ninja::eventVars())); - CreateCreditPdf::dispatchNow($this); + CreateEntityPdf::dispatchNow($this); } return $storage_path; diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 2da78e574a..ab691bc89a 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -18,6 +18,7 @@ use App\Helpers\Invoice\InvoiceSum; use App\Helpers\Invoice\InvoiceSumInclusive; use App\Jobs\Client\UpdateClientBalance; use App\Jobs\Company\UpdateCompanyLedgerWithInvoice; +use App\Jobs\Entity\CreateEntityPdf; use App\Jobs\Invoice\CreateInvoicePdf; use App\Models\Backup; use App\Models\CompanyLedger; @@ -29,8 +30,8 @@ use App\Services\Ledger\LedgerService; use App\Utils\Ninja; use App\Utils\Number; use App\Utils\Traits\Archivable; -use App\Utils\Traits\Invoice\ActionsInvoice; use App\Utils\Traits\InvoiceEmailBuilder; +use App\Utils\Traits\Invoice\ActionsInvoice; use App\Utils\Traits\MakesDates; use App\Utils\Traits\MakesInvoiceValues; use App\Utils\Traits\MakesReminders; @@ -395,7 +396,8 @@ class Invoice extends BaseModel if (! Storage::exists($this->client->invoice_filepath().$this->number.'.pdf')) { event(new InvoiceWasUpdated($this, $this->company, Ninja::eventVars())); - CreateInvoicePdf::dispatchNow($invitation); + //CreateInvoicePdf::dispatchNow($invitation); + CreateEntityPdf::dispatchNow($invitation); } return $storage_path; diff --git a/app/Models/InvoiceInvitation.php b/app/Models/InvoiceInvitation.php index 4b27681c2c..f2ec53dd19 100644 --- a/app/Models/InvoiceInvitation.php +++ b/app/Models/InvoiceInvitation.php @@ -12,6 +12,7 @@ namespace App\Models; use App\Events\Invoice\InvoiceWasUpdated; +use App\Jobs\Entity\CreateEntityPdf; use App\Jobs\Invoice\CreateInvoicePdf; use App\Models\Invoice; use App\Utils\Ninja; @@ -144,7 +145,7 @@ class InvoiceInvitation extends BaseModel if (! Storage::exists($this->invoice->client->invoice_filepath().$this->invoice->number.'.pdf')) { event(new InvoiceWasUpdated($this->invoice, $this->company, Ninja::eventVars())); - CreateInvoicePdf::dispatchNow($this); + CreateEntityPdf::dispatchNow($this); } return $storage_path; diff --git a/app/Models/Quote.php b/app/Models/Quote.php index 5ee3026c43..caed1e3fd3 100644 --- a/app/Models/Quote.php +++ b/app/Models/Quote.php @@ -14,8 +14,7 @@ namespace App\Models; use App\Events\Quote\QuoteWasUpdated; use App\Helpers\Invoice\InvoiceSum; use App\Helpers\Invoice\InvoiceSumInclusive; -use App\Jobs\Invoice\CreateInvoicePdf; -use App\Jobs\Quote\CreateQuotePdf; +use App\Jobs\Entity\CreateEntityPdf; use App\Models\Filterable; use App\Services\Quote\QuoteService; use App\Utils\Ninja; @@ -206,7 +205,7 @@ class Quote extends BaseModel event(new QuoteWasUpdated($this, $this->company, Ninja::eventVars())); - CreateQuotePdf::dispatchNow($invitation); + CreateEntityPdf::dispatchNow($invitation); return $storage_path; } diff --git a/app/Models/QuoteInvitation.php b/app/Models/QuoteInvitation.php index 78b1ae7e4c..d46e1ce901 100644 --- a/app/Models/QuoteInvitation.php +++ b/app/Models/QuoteInvitation.php @@ -12,7 +12,7 @@ namespace App\Models; use App\Events\Quote\QuoteWasUpdated; -use App\Jobs\Quote\CreateQuotePdf; +use App\Jobs\Entity\CreateEntityPdf; use App\Models\Quote; use App\Utils\Ninja; use App\Utils\Traits\Inviteable; @@ -135,7 +135,7 @@ class QuoteInvitation extends BaseModel if (! Storage::exists($this->quote->client->quote_filepath().$this->quote->number.'.pdf')) { event(new QuoteWasUpdated($this->quote, $this->company, Ninja::eventVars())); - CreateQuotePdf::dispatchNow($this); + CreateEntityPdf::dispatchNow($this); } return $storage_path; diff --git a/app/Services/Credit/GetCreditPdf.php b/app/Services/Credit/GetCreditPdf.php index 32657e07af..edf3462f66 100644 --- a/app/Services/Credit/GetCreditPdf.php +++ b/app/Services/Credit/GetCreditPdf.php @@ -11,7 +11,8 @@ namespace App\Services\Credit; -use App\Jobs\Credit\CreateCreditPdf; +use App\Jobs\Credit\CreateEntityPdf; +use App\Jobs\Entity\CreateEntityPdf; use App\Jobs\Invoice\CreateInvoicePdf; use App\Models\ClientContact; use App\Models\Credit; @@ -45,7 +46,7 @@ class GetCreditPdf extends AbstractService $file = Storage::disk($disk)->exists($file_path); if (! $file) { - $file_path = CreateCreditPdf::dispatchNow($this->credit, $this->credit->company, $this->contact); + $file_path = CreateEntityPdf::dispatchNow($this->credit, $this->credit->company, $this->contact); } return Storage::disk($disk)->path($file_path); diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index 582f4f60fb..48d9742a2a 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -19,6 +19,7 @@ use App\Models\Credit; use App\Models\Invoice; use App\Models\Payment; use App\Models\PaymentHash; +use App\Models\PaymentType; use App\Services\AbstractService; use App\Services\Client\ClientService; use App\Services\Payment\PaymentService; diff --git a/app/Services/Invoice/GetInvoicePdf.php b/app/Services/Invoice/GetInvoicePdf.php index 6dba525fb1..ff43cfdcc7 100644 --- a/app/Services/Invoice/GetInvoicePdf.php +++ b/app/Services/Invoice/GetInvoicePdf.php @@ -11,6 +11,7 @@ namespace App\Services\Invoice; +use App\Jobs\Entity\CreateEntityPdf; use App\Jobs\Invoice\CreateInvoicePdf; use App\Models\ClientContact; use App\Models\Invoice; @@ -43,7 +44,7 @@ class GetInvoicePdf extends AbstractService $file = Storage::disk($disk)->exists($file_path); if (! $file) { - $file_path = CreateInvoicePdf::dispatchNow($invitation); + $file_path = CreateEntityPdf::dispatchNow($invitation); } return Storage::disk($disk)->path($file_path); diff --git a/app/Services/Quote/GetQuotePdf.php b/app/Services/Quote/GetQuotePdf.php index 2625679c5b..22e588dd6a 100644 --- a/app/Services/Quote/GetQuotePdf.php +++ b/app/Services/Quote/GetQuotePdf.php @@ -11,6 +11,7 @@ namespace App\Services\Quote; +use App\Jobs\Entity\CreateEntityPdf; use App\Jobs\Quote\CreateQuotePdf; use App\Models\ClientContact; use App\Models\Quote; @@ -43,7 +44,7 @@ class GetQuotePdf extends AbstractService $file = Storage::disk($disk)->exists($file_path); if (! $file) { - $file_path = CreateQuotePdf::dispatchNow($invitation); + $file_path = CreateEntityPdf::dispatchNow($invitation); } return Storage::disk($disk)->path($file_path); diff --git a/app/Utils/HtmlEngine.php b/app/Utils/HtmlEngine.php index 401a173d55..70b64c15d2 100644 --- a/app/Utils/HtmlEngine.php +++ b/app/Utils/HtmlEngine.php @@ -41,7 +41,7 @@ class HtmlEngine public $designer; public function __construct($designer, $invitation, $entity_string) - { + {info($entity_string); $this->designer = $designer; $this->invitation = $invitation; From 0453c989ebe976bc3db5b18aba98437871ba0f57 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 26 Oct 2020 20:13:00 +1100 Subject: [PATCH 7/9] PDF Entity generation tests --- app/Jobs/Entity/CreateEntityPdf.php | 18 ++++-- app/Utils/Traits/GeneratesCounter.php | 2 +- .../factories/CreditInvitationFactory.php | 37 +++++++++++ tests/Feature/PdfCreatorTest.php | 63 +++++++++++++++++++ tests/MockAccountData.php | 35 +++++++++-- 5 files changed, 144 insertions(+), 11 deletions(-) create mode 100644 database/factories/CreditInvitationFactory.php create mode 100644 tests/Feature/PdfCreatorTest.php diff --git a/app/Jobs/Entity/CreateEntityPdf.php b/app/Jobs/Entity/CreateEntityPdf.php index 5363ab986b..ab6a426f63 100644 --- a/app/Jobs/Entity/CreateEntityPdf.php +++ b/app/Jobs/Entity/CreateEntityPdf.php @@ -103,18 +103,24 @@ class CreateEntityPdf implements ShouldQueue App::setLocale($this->contact->preferredLocale()); - if($this->entity instanceof Invoice) + $entity_design_id = ''; + + if($this->entity instanceof Invoice){ $path = $this->entity->client->invoice_filepath(); - elseif($this->entity instanceof Quote) + $entity_design_id = 'invoice_design_id'; + } + elseif($this->entity instanceof Quote){ $path = $this->entity->client->quote_filepath(); - elseif($this->entity instanceof Credit) + $entity_design_id = 'quote_design_id'; + } + elseif($this->entity instanceof Credit){ $path = $this->entity->client->credit_filepath(); + $entity_design_id = 'credit_design_id'; + } $file_path = $path.$this->entity->number.'.pdf'; -info($file_path); - - $entity_design_id = $this->entity->design_id ? $this->entity->design_id : $this->decodePrimaryKey($this->entity->client->getSetting('invoice_design_id')); + $entity_design_id = $this->entity->design_id ? $this->entity->design_id : $this->decodePrimaryKey($this->entity->client->getSetting($entity_design_id)); $design = Design::find($entity_design_id); $html = new HtmlEngine(null, $this->invitation, $this->entity_string); diff --git a/app/Utils/Traits/GeneratesCounter.php b/app/Utils/Traits/GeneratesCounter.php index 6686245fde..7cf5b771ed 100644 --- a/app/Utils/Traits/GeneratesCounter.php +++ b/app/Utils/Traits/GeneratesCounter.php @@ -109,7 +109,7 @@ trait GeneratesCounter $credit_number = $this->checkEntityNumber(Credit::class, $client, $counter, $padding, $pattern); - $this->incrementCounter($client->company, 'credit_number_counter'); + $this->incrementCounter($counter_entity, 'credit_number_counter'); return $credit_number; } diff --git a/database/factories/CreditInvitationFactory.php b/database/factories/CreditInvitationFactory.php new file mode 100644 index 0000000000..1c74de5406 --- /dev/null +++ b/database/factories/CreditInvitationFactory.php @@ -0,0 +1,37 @@ + Str::random(40), + ]; + } +} \ No newline at end of file diff --git a/tests/Feature/PdfCreatorTest.php b/tests/Feature/PdfCreatorTest.php new file mode 100644 index 0000000000..bf3fbf53d1 --- /dev/null +++ b/tests/Feature/PdfCreatorTest.php @@ -0,0 +1,63 @@ +makeTestData(); + + $this->withoutMiddleware( + ThrottleRequests::class + ); + } + + public function testCreditPdfCreated() + { + $credit_path = CreateEntityPdf::dispatchNow($this->credit->invitations->first()); + + $this->assertTrue(Storage::exists($this->client->credit_filepath().$this->credit->number.'.pdf')); + } + + public function testInvoicePdfCreated() + { + $invoice_path = CreateEntityPdf::dispatchNow($this->invoice->invitations->first()); + + $this->assertTrue(Storage::exists($this->client->invoice_filepath().$this->invoice->number.'.pdf')); + } + + public function testQuotePdfCreated() + { + $quote_path = CreateEntityPdf::dispatchNow($this->quote->invitations->first()); + + $this->assertTrue(Storage::exists($this->client->quote_filepath().$this->quote->number.'.pdf')); + } + + +} diff --git a/tests/MockAccountData.php b/tests/MockAccountData.php index d8e945c856..50fb7963e8 100644 --- a/tests/MockAccountData.php +++ b/tests/MockAccountData.php @@ -31,6 +31,7 @@ use App\Models\Company; use App\Models\CompanyGateway; use App\Models\CompanyToken; use App\Models\Credit; +use App\Models\CreditInvitation; use App\Models\Expense; use App\Models\ExpenseCategory; use App\Models\GroupSetting; @@ -350,19 +351,45 @@ trait MockAccountData $this->credit->tax_rate3 = 0; $this->credit->uses_inclusive_taxes = false; - $this->credit->save(); - $this->credit->service()->createInvitations()->markSent(); + $this->credit_calc = new InvoiceSum($this->credit); $this->credit_calc->build(); $this->credit = $this->credit_calc->getCredit(); - $this->credit->service()->markSent(); $this->client->service()->adjustCreditBalance($this->credit->balance)->save(); $this->credit->ledger()->updateCreditBalance($this->credit->balance)->save(); - + $this->credit->number = $this->getNextCreditNumber($this->client); + + + CreditInvitation::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'client_contact_id' => $contact->id, + 'credit_id' => $this->credit->id, + ]); + + CreditInvitation::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'client_contact_id' => $contact2->id, + 'credit_id' => $this->credit->id, + ]); + + $invitations = CreditInvitation::whereCompanyId($this->credit->company_id) + ->whereCreditId($this->credit->id); + + $this->credit->setRelation('invitations', $invitations); + + $this->credit->service()->markSent(); + + $this->credit->setRelation('client', $this->client); + $this->credit->setRelation('company', $this->company); + + $this->credit->save(); + $contacts = $this->invoice->client->contacts; $contacts->each(function ($contact) { From 8f53424017d077c7d5ec8c578ee4524c8d9475d2 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 27 Oct 2020 06:10:04 +1100 Subject: [PATCH 8/9] Fixes --- app/Console/Commands/CreateSingleAccount.php | 2 ++ app/Console/Commands/SendTestEmails.php | 4 +-- app/DataMapper/CompanySettings.php | 3 ++ .../ClientPortal/PaymentController.php | 2 +- app/Http/Controllers/SetupController.php | 3 ++ app/Http/Middleware/ContactKeyLogin.php | 2 -- app/Jobs/Invoice/InjectSignature.php | 3 +- app/Listeners/Invoice/CreateInvoicePdf.php | 1 - app/Models/Client.php | 2 +- app/Models/Company.php | 1 - app/Models/Invoice.php | 2 -- app/Models/InvoiceInvitation.php | 1 - app/Models/Task.php | 2 ++ app/Services/Credit/GetCreditPdf.php | 2 -- app/Services/Invoice/AutoBillInvoice.php | 2 +- app/Services/Invoice/GetInvoicePdf.php | 1 - app/Services/Invoice/InvoiceService.php | 4 +-- app/Services/Quote/GetQuotePdf.php | 1 - app/Transformers/CompanyTransformer.php | 2 +- app/Transformers/ExpenseTransformer.php | 2 +- app/Transformers/TaskTransformer.php | 5 +-- app/Utils/HtmlEngine.php | 5 +-- ...2020_10_22_204900_company_table_fields.php | 4 ++- tests/MockAccountData.php | 3 +- tests/Unit/AutoBillInvoiceTest.php | 34 ++++++++++--------- 25 files changed, 50 insertions(+), 43 deletions(-) diff --git a/app/Console/Commands/CreateSingleAccount.php b/app/Console/Commands/CreateSingleAccount.php index c650fe2ad3..22f6a1563b 100644 --- a/app/Console/Commands/CreateSingleAccount.php +++ b/app/Console/Commands/CreateSingleAccount.php @@ -242,6 +242,8 @@ class CreateSingleAccount extends Command $settings = $client->settings; $settings->currency_id = "1"; + $settings->use_credits_payment = "always"; + $client->settings = $settings; $country = Country::all()->random(); diff --git a/app/Console/Commands/SendTestEmails.php b/app/Console/Commands/SendTestEmails.php index 5890da2d04..914a481761 100644 --- a/app/Console/Commands/SendTestEmails.php +++ b/app/Console/Commands/SendTestEmails.php @@ -18,7 +18,7 @@ use App\Factory\CompanyUserFactory; use App\Factory\InvoiceFactory; use App\Factory\InvoiceInvitationFactory; use App\Helpers\Email\InvoiceEmail; -use App\Jobs\Invoice\CreateInvoicePdf; +use App\Jobs\Invoice\CreateEntityPdf; use App\Mail\TemplateEmail; use App\Models\Account; use App\Models\Client; @@ -149,7 +149,7 @@ class SendTestEmails extends Command $invoice->setRelation('invitations', $ii); $invoice->service()->markSent()->save(); - CreateInvoicePdf::dispatch($invoice->invitations()->first()); + CreateEntityPdf::dispatch($invoice->invitations()->first()); $cc_emails = [config('ninja.testvars.test_email')]; $bcc_emails = [config('ninja.testvars.test_email')]; diff --git a/app/DataMapper/CompanySettings.php b/app/DataMapper/CompanySettings.php index e8fc16fe69..2d7bf62464 100644 --- a/app/DataMapper/CompanySettings.php +++ b/app/DataMapper/CompanySettings.php @@ -253,7 +253,10 @@ class CompanySettings extends BaseSettings public $client_portal_under_payment_minimum = 0; public $client_portal_allow_over_payment = false; + public $use_credits_payment = 'off'; //always, option, off + public static $casts = [ + 'use_credits_payment' => 'string', 'recurring_invoice_number_pattern' => 'string', 'recurring_invoice_number_counter' => 'int', 'client_portal_under_payment_minimum'=> 'float', diff --git a/app/Http/Controllers/ClientPortal/PaymentController.php b/app/Http/Controllers/ClientPortal/PaymentController.php index ff36acb2ee..31fb779d4e 100644 --- a/app/Http/Controllers/ClientPortal/PaymentController.php +++ b/app/Http/Controllers/ClientPortal/PaymentController.php @@ -175,7 +175,7 @@ class PaymentController extends Controller $payment_method_id = $request->input('payment_method_id'); $invoice_totals = $payable_invoices->sum('amount'); $first_invoice = $invoices->first(); - $credit_totals = $first_invoice->company->use_credits_payment == 'off' ? 0 : $first_invoice->client->service()->getCreditBalance(); + $credit_totals = $first_invoice->client->getSetting('use_credits_payment') == 'off' ? 0 : $first_invoice->client->service()->getCreditBalance(); $starting_invoice_amount = $first_invoice->amount; if($gateway) diff --git a/app/Http/Controllers/SetupController.php b/app/Http/Controllers/SetupController.php index ba5a96bdfc..10d0239f95 100644 --- a/app/Http/Controllers/SetupController.php +++ b/app/Http/Controllers/SetupController.php @@ -16,6 +16,7 @@ use App\Http\Requests\Setup\CheckDatabaseRequest; use App\Http\Requests\Setup\CheckMailRequest; use App\Http\Requests\Setup\StoreSetupRequest; use App\Jobs\Account\CreateAccount; +use App\Jobs\Util\VersionCheck; use App\Models\Account; use App\Utils\SystemHealth; use Illuminate\Http\Response; @@ -124,6 +125,8 @@ class SetupController extends Controller CreateAccount::dispatchNow($request->all()); } + VersionCheck::dispatchNow(); + return redirect('/'); } catch (\Exception $e) { info($e->getMessage()); diff --git a/app/Http/Middleware/ContactKeyLogin.php b/app/Http/Middleware/ContactKeyLogin.php index 4362be0854..094bebb34b 100644 --- a/app/Http/Middleware/ContactKeyLogin.php +++ b/app/Http/Middleware/ContactKeyLogin.php @@ -33,8 +33,6 @@ class ContactKeyLogin */ public function handle($request, Closure $next) { - info($request->segment(3)); - info($request->route('contact_key')); if(Auth::guard('contact')->check()) Auth::guard('contact')->logout(); diff --git a/app/Jobs/Invoice/InjectSignature.php b/app/Jobs/Invoice/InjectSignature.php index cd93d07c77..1595537046 100644 --- a/app/Jobs/Invoice/InjectSignature.php +++ b/app/Jobs/Invoice/InjectSignature.php @@ -2,6 +2,7 @@ namespace App\Jobs\Invoice; +use App\Jobs\Entity\CreateEntityPdf; use App\Models\Invoice; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; @@ -51,6 +52,6 @@ class InjectSignature implements ShouldQueue $invitation->signature_base64 = $this->signature; $invitation->save(); - CreateInvoicePdf::dispatch($invitation); + CreateEntityPdf::dispatch($invitation); } } diff --git a/app/Listeners/Invoice/CreateInvoicePdf.php b/app/Listeners/Invoice/CreateInvoicePdf.php index d7a4345793..e48174ba75 100644 --- a/app/Listeners/Invoice/CreateInvoicePdf.php +++ b/app/Listeners/Invoice/CreateInvoicePdf.php @@ -12,7 +12,6 @@ namespace App\Listeners\Invoice; use App\Jobs\Entity\CreateEntityPdf; -use App\Jobs\Invoice\CreateInvoicePdf as PdfCreator; use App\Libraries\MultiDB; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; diff --git a/app/Models/Client.php b/app/Models/Client.php index c65010d1d6..5894e91872 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -525,7 +525,7 @@ class Client extends BaseModel implements HasLocalePreference } } - if(($this->company->use_credits_payment == 'option' || $this->company->use_credits_payment == 'always') && $this->service()->getCreditBalance() > 0) { + if(($this->getSetting('use_credits_payment') == 'option' || $this->getSetting('use_credits_payment') == 'always') && $this->service()->getCreditBalance() > 0) { $payment_urls[] = [ 'label' => ctrans('texts.apply_credit'), 'company_gateway_id' => CompanyGateway::GATEWAY_CREDIT, diff --git a/app/Models/Company.php b/app/Models/Company.php index 00a7ef9c0f..3a001a9b8a 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -71,7 +71,6 @@ class Company extends BaseModel protected $fillable = [ 'mark_expenses_invoiceable', 'mark_expenses_paid', - 'use_credits_payment', 'enabled_item_tax_rates', 'fill_products', 'industry_id', diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index ab691bc89a..fb69794ccb 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -19,7 +19,6 @@ use App\Helpers\Invoice\InvoiceSumInclusive; use App\Jobs\Client\UpdateClientBalance; use App\Jobs\Company\UpdateCompanyLedgerWithInvoice; use App\Jobs\Entity\CreateEntityPdf; -use App\Jobs\Invoice\CreateInvoicePdf; use App\Models\Backup; use App\Models\CompanyLedger; use App\Models\Currency; @@ -396,7 +395,6 @@ class Invoice extends BaseModel if (! Storage::exists($this->client->invoice_filepath().$this->number.'.pdf')) { event(new InvoiceWasUpdated($this, $this->company, Ninja::eventVars())); - //CreateInvoicePdf::dispatchNow($invitation); CreateEntityPdf::dispatchNow($invitation); } diff --git a/app/Models/InvoiceInvitation.php b/app/Models/InvoiceInvitation.php index f2ec53dd19..f459986844 100644 --- a/app/Models/InvoiceInvitation.php +++ b/app/Models/InvoiceInvitation.php @@ -13,7 +13,6 @@ namespace App\Models; use App\Events\Invoice\InvoiceWasUpdated; use App\Jobs\Entity\CreateEntityPdf; -use App\Jobs\Invoice\CreateInvoicePdf; use App\Models\Invoice; use App\Utils\Ninja; use App\Utils\Traits\Inviteable; diff --git a/app/Models/Task.php b/app/Models/Task.php index 9a3345f7cf..fd60665103 100644 --- a/app/Models/Task.php +++ b/app/Models/Task.php @@ -33,6 +33,8 @@ class Task extends BaseModel 'description', 'is_running', 'time_log', + 'status_id', + 'status_sort_order', ]; protected $touches = []; diff --git a/app/Services/Credit/GetCreditPdf.php b/app/Services/Credit/GetCreditPdf.php index edf3462f66..302a8dfca7 100644 --- a/app/Services/Credit/GetCreditPdf.php +++ b/app/Services/Credit/GetCreditPdf.php @@ -11,9 +11,7 @@ namespace App\Services\Credit; -use App\Jobs\Credit\CreateEntityPdf; use App\Jobs\Entity\CreateEntityPdf; -use App\Jobs\Invoice\CreateInvoicePdf; use App\Models\ClientContact; use App\Models\Credit; use App\Services\AbstractService; diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index 48d9742a2a..784a3cdd45 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -57,7 +57,7 @@ class AutoBillInvoice extends AbstractService //if the credits cover the payments, we stop here, build the payment with credits and exit early - if($this->invoice->company->use_credits_payment != 'off') + if($this->client->getSetting('use_credits_payment') != 'off') $this->applyCreditPayment(); info("partial = {$this->invoice->partial}"); diff --git a/app/Services/Invoice/GetInvoicePdf.php b/app/Services/Invoice/GetInvoicePdf.php index ff43cfdcc7..3fcb1a8a46 100644 --- a/app/Services/Invoice/GetInvoicePdf.php +++ b/app/Services/Invoice/GetInvoicePdf.php @@ -12,7 +12,6 @@ namespace App\Services\Invoice; use App\Jobs\Entity\CreateEntityPdf; -use App\Jobs\Invoice\CreateInvoicePdf; use App\Models\ClientContact; use App\Models\Invoice; use App\Services\AbstractService; diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index 89de0c7bdf..15e80d5f0f 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -11,7 +11,7 @@ namespace App\Services\Invoice; -use App\Jobs\Invoice\CreateInvoicePdf; +use App\Jobs\Entity\CreateEntityPdf; use App\Jobs\Util\UnlinkFile; use App\Models\CompanyGateway; use App\Models\Invoice; @@ -295,7 +295,7 @@ class InvoiceService public function touchPdf() { $this->invoice->invitations->each(function ($invitation){ - CreateInvoicePdf::dispatch($invitation); + CreateEntityPdf::dispatch($invitation); }); return $this; diff --git a/app/Services/Quote/GetQuotePdf.php b/app/Services/Quote/GetQuotePdf.php index 22e588dd6a..879593892a 100644 --- a/app/Services/Quote/GetQuotePdf.php +++ b/app/Services/Quote/GetQuotePdf.php @@ -12,7 +12,6 @@ namespace App\Services\Quote; use App\Jobs\Entity\CreateEntityPdf; -use App\Jobs\Quote\CreateQuotePdf; use App\Models\ClientContact; use App\Models\Quote; use App\Services\AbstractService; diff --git a/app/Transformers/CompanyTransformer.php b/app/Transformers/CompanyTransformer.php index 6427c10336..4c5dcc2a9d 100644 --- a/app/Transformers/CompanyTransformer.php +++ b/app/Transformers/CompanyTransformer.php @@ -153,7 +153,7 @@ class CompanyTransformer extends EntityTransformer 'invoice_expense_documents' => (bool) $company->invoice_expense_documents, 'invoice_task_timelog' => (bool) $company->invoice_task_timelog, 'auto_start_tasks' => (bool) $company->auto_start_tasks, - 'use_credits_payment' => (string) $company->use_credits_payment, + 'invoice_task_documents' => (bool) $company->invoice_task_documents, ]; } diff --git a/app/Transformers/ExpenseTransformer.php b/app/Transformers/ExpenseTransformer.php index c26bde1dc1..e3278bd2b8 100644 --- a/app/Transformers/ExpenseTransformer.php +++ b/app/Transformers/ExpenseTransformer.php @@ -59,7 +59,7 @@ class ExpenseTransformer extends EntityTransformer 'bank_id' => (string) $expense->bank_id ?: '', 'invoice_currency_id' => (string) $expense->invoice_currency_id ?: '', 'expense_currency_id' => (string) $expense->expense_currency_id ?: '', - 'category_id' => (string) $expense->category_id ?: '', + 'category_id' => $this->encodePrimaryKey($expense->category_id), 'payment_type_id' => (string) $expense->payment_type_id ?: '', 'recurring_expense_id' => (string) $expense->recurring_expense_id ?: '', 'is_deleted' => (bool) $expense->is_deleted, diff --git a/app/Transformers/TaskTransformer.php b/app/Transformers/TaskTransformer.php index fd2cc483ae..37e22603b1 100644 --- a/app/Transformers/TaskTransformer.php +++ b/app/Transformers/TaskTransformer.php @@ -50,6 +50,7 @@ class TaskTransformer extends EntityTransformer 'start_time' => (int) $task->start_time, 'description' => $task->description ?: '', 'duration' => 0, + 'rate' => (float) $task->rate ?: 0, 'created_at' => (int) $task->created_at, 'updated_at' => (int) $task->updated_at, 'archived_at' => (int) $task->deleted_at, @@ -63,8 +64,8 @@ class TaskTransformer extends EntityTransformer 'custom_value2' => $task->custom_value2 ?: '', 'custom_value3' => $task->custom_value3 ?: '', 'custom_value4' => $task->custom_value4 ?: '', - 'task_status_id' => $this->encodePrimaryKey($task->task_status_id), - 'task_status_sort_order' => (int) $task->task_status_sort_order, + 'status_id' => $this->encodePrimaryKey($task->status_id), + 'status_sort_order' => (int) $task->status_sort_order, ]; } } diff --git a/app/Utils/HtmlEngine.php b/app/Utils/HtmlEngine.php index 70b64c15d2..179f4d5723 100644 --- a/app/Utils/HtmlEngine.php +++ b/app/Utils/HtmlEngine.php @@ -41,7 +41,7 @@ class HtmlEngine public $designer; public function __construct($designer, $invitation, $entity_string) - {info($entity_string); + { $this->designer = $designer; $this->invitation = $invitation; @@ -133,7 +133,7 @@ class HtmlEngine $data['$entity.terms'] = ['value' => $this->entity->terms ?: ' ', 'label' => ctrans('texts.quote_terms')]; $data['$terms'] = &$data['$entity.terms']; $data['$view_link'] = ['value' => ''.ctrans('texts.view_quote').'', 'label' => ctrans('texts.view_quote')]; - // $data['$view_link'] = ['value' => $this->invitation->getLink(), 'label' => ctrans('texts.view_quote')]; + $data['$view_url'] = ['value' => $this->invitation->getLink(), 'label' => ctrans('texts.view_quote')]; } if ($this->entity_string == 'credit') { @@ -142,6 +142,7 @@ class HtmlEngine $data['$entity.terms'] = ['value' => $this->entity->terms ?: ' ', 'label' => ctrans('texts.credit_terms')]; $data['$terms'] = &$data['$entity.terms']; $data['$view_link'] = ['value' => ''.ctrans('texts.view_credit').'', 'label' => ctrans('texts.view_credit')]; + $data['$view_url'] = ['value' => $this->invitation->getLink(), 'label' => ctrans('texts.view_credit')]; // $data['$view_link'] = ['value' => $this->invitation->getLink(), 'label' => ctrans('texts.view_credit')]; } diff --git a/database/migrations/2020_10_22_204900_company_table_fields.php b/database/migrations/2020_10_22_204900_company_table_fields.php index 9c43fb278a..107f18575f 100644 --- a/database/migrations/2020_10_22_204900_company_table_fields.php +++ b/database/migrations/2020_10_22_204900_company_table_fields.php @@ -15,10 +15,12 @@ class CompanyTableFields extends Migration { Schema::table('companies', function(Blueprint $table){ $table->boolean('invoice_task_timelog')->default(true); + $table->boolean('invoice_task_documents')->default(false); + $table->dropColumn('use_credits_payment'); }); Schema::table('task_statuses', function(Blueprint $table){ - $table->unsignedInteger('sort_order')->default(0); + $table->unsignedInteger('status_sort_order')->default(0); }); } diff --git a/tests/MockAccountData.php b/tests/MockAccountData.php index 50fb7963e8..d31a901b5b 100644 --- a/tests/MockAccountData.php +++ b/tests/MockAccountData.php @@ -136,7 +136,8 @@ trait MockAccountData $settings->country_id = '840'; $settings->vat_number = 'vat number'; $settings->id_number = 'id number'; - + $settings->use_credits_payment = 'always'; + $this->company->settings = $settings; $this->company->save(); diff --git a/tests/Unit/AutoBillInvoiceTest.php b/tests/Unit/AutoBillInvoiceTest.php index 3503df3ddb..dc8fbbce81 100644 --- a/tests/Unit/AutoBillInvoiceTest.php +++ b/tests/Unit/AutoBillInvoiceTest.php @@ -33,8 +33,6 @@ class AutoBillInvoiceTest extends TestCase public function testAutoBillFunctionality() { - $this->company->use_credits_payment = 'always'; - $this->company->save(); $this->assertEquals($this->client->balance, 10); $this->assertEquals($this->client->paid_to_date, 0); @@ -52,23 +50,27 @@ class AutoBillInvoiceTest extends TestCase } - public function testAutoBillSetOffFunctionality() - { - $this->company->use_credits_payment = 'off'; - $this->company->save(); + // public function testAutoBillSetOffFunctionality() + // { + + // $settings = $this->company->settings; + // $settings->use_credits_payment = 'off'; - $this->assertEquals($this->client->balance, 10); - $this->assertEquals($this->client->paid_to_date, 0); - $this->assertEquals($this->client->credit_balance, 10); + // $this->company->settings = $settings; + // $this->company->save(); - $this->invoice->service()->markSent()->autoBill()->save(); + // $this->assertEquals($this->client->balance, 10); + // $this->assertEquals($this->client->paid_to_date, 0); + // $this->assertEquals($this->client->credit_balance, 10); - $this->assertNotNull($this->invoice->payments()); - $this->assertEquals(0, $this->invoice->payments()->sum('payments.amount')); + // $this->invoice->service()->markSent()->autoBill()->save(); - $this->assertEquals($this->client->balance, 10); - $this->assertEquals($this->client->paid_to_date, 0); - $this->assertEquals($this->client->credit_balance, 10); + // $this->assertNotNull($this->invoice->payments()); + // $this->assertEquals(0, $this->invoice->payments()->sum('payments.amount')); + + // $this->assertEquals($this->client->balance, 10); + // $this->assertEquals($this->client->paid_to_date, 0); + // $this->assertEquals($this->client->credit_balance, 10); - } + // } } From 0df672baaa10f11e2d3ead1721c7af555331e5d1 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 27 Oct 2020 07:46:06 +1100 Subject: [PATCH 9/9] remove white space and carriage returns from version --- app/Jobs/Util/VersionCheck.php | 2 +- app/Models/Company.php | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/Jobs/Util/VersionCheck.php b/app/Jobs/Util/VersionCheck.php index 9662fa1b41..9d04f9c323 100644 --- a/app/Jobs/Util/VersionCheck.php +++ b/app/Jobs/Util/VersionCheck.php @@ -34,7 +34,7 @@ class VersionCheck implements ShouldQueue */ public function handle() { - $version_file = file_get_contents(config('ninja.version_url')); + $version_file = trim(file_get_contents(config('ninja.version_url'))); info("latest version = {$version_file}"); diff --git a/app/Models/Company.php b/app/Models/Company.php index 3a001a9b8a..98e10abf48 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -97,6 +97,8 @@ class Company extends BaseModel 'google_analytics_key', 'client_can_register', 'enable_shop_api', + 'invoice_task_timelog', + 'auto_start_tasks', ]; protected $hidden = [