1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Working on recurring migration

This commit is contained in:
David Bomba 2020-10-06 08:46:47 +11:00
parent 45b6c1bcc2
commit 1264486bb9
11 changed files with 161 additions and 12 deletions

View File

@ -38,6 +38,7 @@ class InvoiceItemFactory
$item->custom_value2 = '';
$item->custom_value3 = '';
$item->custom_value4 = '';
$item->type_id = 1;
return $item;
}
@ -68,6 +69,7 @@ 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;
}

View File

@ -0,0 +1,38 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Factory;
use App\Models\RecurringInvoiceInvitation;
use Illuminate\Support\Str;
class RecurringInvoiceInvitationFactory
{
public static function create(int $company_id, int $user_id) :RecurringInvoiceInvitation
{
$ii = new RecurringInvoiceInvitation;
$ii->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;
}
}

View File

@ -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);

View File

@ -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();

View File

@ -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
*/

View File

@ -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();
}
}

View File

@ -0,0 +1,53 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Services\Recurring;
use App\Models\Client;
use App\Services\AbstractService;
use App\Utils\Traits\GeneratesCounter;
class ApplyNumber extends AbstractService
{
use GeneratesCounter;
private $client;
private $recurring_entity;
public function __construct(Client $client, $recurring_entity)
{
$this->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;
}
}

View File

@ -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;
}

View File

@ -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();

View File

@ -25,6 +25,10 @@ class RecurringInvoiceInvitationTransformer extends EntityTransformer
'id' => $this->encodePrimaryKey($invitation->id),
'client_contact_id' => $this->encodePrimaryKey($invitation->client_contact_id),
'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,

View File

@ -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();
});
}