diff --git a/app/Factory/InvoiceItemFactory.php b/app/Factory/InvoiceItemFactory.php index 16fa0afded39..6c489c2106f6 100644 --- a/app/Factory/InvoiceItemFactory.php +++ b/app/Factory/InvoiceItemFactory.php @@ -38,6 +38,7 @@ class InvoiceItemFactory $item->custom_value2 = ''; $item->custom_value3 = ''; $item->custom_value4 = ''; + $item->type_id = 1; return $item; } @@ -68,7 +69,8 @@ class InvoiceItemFactory $item->custom_value4 = $faker->realText(10); $item->tax_name1 = 'GST'; $item->tax_rate1 = 10.00; - + $item->type_id = 1; + $data[] = $item; } @@ -101,6 +103,7 @@ class InvoiceItemFactory $item->custom_value4 = $faker->realText(10); $item->tax_name1 = 'GST'; $item->tax_rate1 = 10.00; + $item->type_id = 1; $data[] = $item; } diff --git a/app/Factory/RecurringInvoiceInvitationFactory.php b/app/Factory/RecurringInvoiceInvitationFactory.php new file mode 100644 index 000000000000..fc3e461cfd2b --- /dev/null +++ b/app/Factory/RecurringInvoiceInvitationFactory.php @@ -0,0 +1,38 @@ +company_id = $company_id; + $ii->user_id = $user_id; + $ii->client_contact_id = null; + $ii->recurring_invoice_id = null; + $ii->key = Str::random(config('ninja.key_length')); + $ii->transaction_reference = null; + $ii->message_id = null; + $ii->email_error = ''; + $ii->signature_base64 = ''; + $ii->signature_date = null; + $ii->sent_date = null; + $ii->viewed_date = null; + $ii->opened_date = null; + + return $ii; + } +} diff --git a/app/Http/Controllers/MigrationController.php b/app/Http/Controllers/MigrationController.php index 681f1fa9d084..1556f0d69861 100644 --- a/app/Http/Controllers/MigrationController.php +++ b/app/Http/Controllers/MigrationController.php @@ -270,7 +270,7 @@ class MigrationController extends BaseController // If keys ain't same, but existing company with force. if (! $checks['same_keys'] && $checks['existing_company'] && $checks['with_force']) { - info('Migrating: Different keys, exisiting company with force option.'); + info('Migrating: Different keys, existing company with force option.'); if ($company) { $this->purgeCompany($company); diff --git a/app/Jobs/Util/Import.php b/app/Jobs/Util/Import.php index d86d3ae563f8..fdbcfe897c09 100644 --- a/app/Jobs/Util/Import.php +++ b/app/Jobs/Util/Import.php @@ -500,8 +500,6 @@ class Import implements ShouldQueue foreach ($data as $key => $resource) { - info("importing recurring with key {$key}"); - $modified = $resource; if (array_key_exists('client_id', $resource) && ! array_key_exists('clients', $this->ids)) { @@ -526,6 +524,7 @@ class Import implements ShouldQueue 'old' => $resource['id'], 'new' => $invoice->id, ]; + } RecurringInvoice::reguard(); diff --git a/app/Models/RecurringInvoiceInvitation.php b/app/Models/RecurringInvoiceInvitation.php index eec7d83fd6b3..31aeb6043363 100644 --- a/app/Models/RecurringInvoiceInvitation.php +++ b/app/Models/RecurringInvoiceInvitation.php @@ -11,6 +11,7 @@ namespace App\Models; +use App\Models\RecurringInvoice; use App\Utils\Traits\Inviteable; use App\Utils\Traits\MakesDates; use Illuminate\Database\Eloquent\Model; @@ -31,6 +32,11 @@ class RecurringInvoiceInvitation extends BaseModel return self::class; } + public function entityType() + { + return RecurringInvoice::class; + } + /** * @return mixed */ diff --git a/app/Repositories/Migration/InvoiceMigrationRepository.php b/app/Repositories/Migration/InvoiceMigrationRepository.php index f6fca7eedc39..6c555f39568a 100644 --- a/app/Repositories/Migration/InvoiceMigrationRepository.php +++ b/app/Repositories/Migration/InvoiceMigrationRepository.php @@ -23,6 +23,7 @@ use App\Models\Credit; use App\Models\Invoice; use App\Models\Payment; use App\Models\Quote; +use App\Models\RecurringInvoice; use App\Repositories\ActivityRepository; use App\Repositories\BaseRepository; use App\Repositories\CreditRepository; @@ -54,7 +55,7 @@ class InvoiceMigrationRepository extends BaseRepository $resource = explode('\\', $class->name)[2]; /** This will extract 'Invoice' from App\Models\Invoice */ $lcfirst_resource_id = lcfirst($resource).'_id'; - if ($class->name == Invoice::class || $class->name == Quote::class) { + if ($class->name == Invoice::class || $class->name == Quote::class || $class->name == RecurringInvoice::class) { $state['starting_amount'] = $model->amount; } @@ -83,6 +84,8 @@ class InvoiceMigrationRepository extends BaseRepository $this->saveDocuments($data['documents'], $model); } + info(sprintf('App\\Factory\\%sInvitationFactory', $resource)); + $invitation_factory_class = sprintf('App\\Factory\\%sInvitationFactory', $resource); if (isset($data['client_contacts'])) { @@ -131,17 +134,21 @@ class InvoiceMigrationRepository extends BaseRepository $model->service()->createInvitations(); } +info("saving 3a"); + $model = $model->calc()->getInvoice(); +info("saving 3b"); $state['finished_amount'] = $model->amount; $model = $model->service()->applyNumber()->save(); +info("saving 3c"); if ($model->company->update_products !== false) { UpdateOrCreateProduct::dispatch($model->line_items, $model, $model->company); } - - if ($class->name == Invoice::class) { +info("saving 4"); + if ($class->name == Invoice::class || $class->name == RecurringInvoice::class) { if (($state['finished_amount'] != $state['starting_amount']) && ($model->status_id != Invoice::STATUS_DRAFT)) { // $model->ledger()->updateInvoiceBalance(($state['finished_amount'] - $state['starting_amount'])); @@ -170,7 +177,7 @@ class InvoiceMigrationRepository extends BaseRepository } $model->save(); - +info("saving 5"); return $model->fresh(); } } diff --git a/app/Services/Recurring/ApplyNumber.php b/app/Services/Recurring/ApplyNumber.php new file mode 100644 index 000000000000..e791921a5f5e --- /dev/null +++ b/app/Services/Recurring/ApplyNumber.php @@ -0,0 +1,53 @@ +client = $client; + + $this->recurring_entity = $recurring_entity; + } + + public function run() + { + if ($this->recurring_entity->number != '') { + return $this->recurring_entity; + } + + switch ($this->client->getSetting('counter_number_applied')) { + case 'when_saved': + $this->recurring_entity->number = $this->getNextRecurringInvoiceNumber($this->client); + break; + case 'when_sent': + break; + + default: + // code... + break; + } + + return $this->recurring_entity; + } +} diff --git a/app/Services/Recurring/CreateRecurringInvitations.php b/app/Services/Recurring/CreateRecurringInvitations.php index 7c870a217249..e3509864fe53 100644 --- a/app/Services/Recurring/CreateRecurringInvitations.php +++ b/app/Services/Recurring/CreateRecurringInvitations.php @@ -38,11 +38,18 @@ class CreateRecurringInvitations extends AbstractService $this->entity_name = lcfirst(Str::snake(class_basename($entity))); $this->entity_id_name = $this->entity_name . "_id"; $this->invitation_class = 'App\Models\\' . ucfirst(Str::camel($this->entity_name)) . "Invitation"; - $this->invitation_factory = 'App\Factory\\' . ucfirst(Str::camel($this->entity_name)) . "Factory"; + $this->invitation_factory = 'App\Factory\\' . ucfirst(Str::camel($this->entity_name)) . "InvitationFactory"; } public function run() { + info($this->entity_name); + info($this->entity_id_name); + info($this->invitation_class); + info($this->invitation_factory); + + + try { $this->entity->client->contacts->each(function ($contact) { $invitation = $this->invitation_class::whereCompanyId($this->entity->company_id) @@ -61,6 +68,13 @@ class CreateRecurringInvitations extends AbstractService } }); + } + catch(\Exception $e) + { + info($e->getMessage()); + } + + info("returning the entity"); return $this->entity; } diff --git a/app/Services/Recurring/RecurringService.php b/app/Services/Recurring/RecurringService.php index f4720b0156b9..a6cd1394cd72 100644 --- a/app/Services/Recurring/RecurringService.php +++ b/app/Services/Recurring/RecurringService.php @@ -12,6 +12,7 @@ namespace App\Services\Recurring; use App\Models\RecurringInvoice; +use App\Services\Recurring\ApplyNumber; use App\Services\Recurring\CreateRecurringInvitations; use Illuminate\Support\Carbon; @@ -57,6 +58,17 @@ class RecurringService } + /** + * Applies the invoice number. + * @return $this InvoiceService object + */ + public function applyNumber() + { + $this->recurring_entity = (new ApplyNumber($this->recurring_entity->client, $this->recurring_entity))->run(); + + return $this; + } + public function save() { $this->recurring_entity->save(); diff --git a/app/Transformers/RecurringInvoiceInvitationTransformer.php b/app/Transformers/RecurringInvoiceInvitationTransformer.php index aff8dee99096..e509d15fe2ef 100644 --- a/app/Transformers/RecurringInvoiceInvitationTransformer.php +++ b/app/Transformers/RecurringInvoiceInvitationTransformer.php @@ -22,12 +22,16 @@ class RecurringInvoiceInvitationTransformer extends EntityTransformer public function transform(RecurringInvoiceInvitation $invitation) { return [ - 'id' => $this->encodePrimaryKey($invitation->id), + 'id' => $this->encodePrimaryKey($invitation->id), 'client_contact_id' => $this->encodePrimaryKey($invitation->client_contact_id), - 'key' => $invitation->key, + 'key' => $invitation->key, + 'link' => $invitation->getLink() ?: '', + 'sent_date' => $invitation->sent_date ?: '', + 'viewed_date' => $invitation->viewed_date ?: '', + 'opened_date' => $invitation->opened_date ?: '', 'updated_at' => (int) $invitation->updated_at, 'archived_at' => (int) $invitation->deleted_at, - 'created_at' => (int) $invitation->created_at, + 'created_at' => (int) $invitation->created_at, ]; } } diff --git a/database/migrations/2020_09_27_215800_update_gateway_table_visible_column.php b/database/migrations/2020_09_27_215800_update_gateway_table_visible_column.php index 607bfdb2865e..ca211a45b665 100644 --- a/database/migrations/2020_09_27_215800_update_gateway_table_visible_column.php +++ b/database/migrations/2020_09_27_215800_update_gateway_table_visible_column.php @@ -18,6 +18,19 @@ class UpdateGatewayTableVisibleColumn extends Migration Gateway::whereIn('id', [1,15,20,39])->update(['visible' => 1]); + Schema::table('recurring_invoice_invitations', function ($t) { + $t->string('transaction_reference')->nullable(); + $t->string('message_id')->nullable(); + $t->mediumText('email_error')->nullable(); + $t->text('signature_base64')->nullable(); + $t->datetime('signature_date')->nullable(); + + $t->datetime('sent_date')->nullable(); + $t->datetime('viewed_date')->nullable(); + $t->datetime('opened_date')->nullable(); + + }); + }