Fixes for migrations

This commit is contained in:
David Bomba 2021-02-03 23:29:44 +11:00
parent d26e347d6d
commit 2e2843c27d
9 changed files with 105 additions and 45 deletions

View File

@ -289,30 +289,6 @@ class CheckData extends Command
}
}
private function checkInvoiceBalances()
{
$wrong_balances = 0;
$wrong_paid_to_dates = 0;
foreach (Client::where('is_deleted', 0)->cursor() as $client) {
$invoice_balance = $client->invoices->where('is_deleted', false)->where('status_id', '>', 1)->sum('balance');
$credit_balance = $client->credits->where('is_deleted', false)->sum('balance');
// $invoice_balance -= $credit_balance;//doesn't make sense to remove the credit amount
$ledger = CompanyLedger::where('client_id', $client->id)->orderBy('id', 'DESC')->first();
if ($ledger && number_format($invoice_balance, 4) != number_format($client->balance, 4)) {
$wrong_balances++;
$this->logMessage("# {$client->id} " . $client->present()->name.' - '.$client->number." - Balance Failure - Invoice Balances = {$invoice_balance} Client Balance = {$client->balance} Ledger Balance = {$ledger->balance}");
$this->isValid = false;
}
}
$this->logMessage("{$wrong_balances} clients with incorrect balances");
}
private function checkPaidToDates()
{
$wrong_paid_to_dates = 0;
@ -390,7 +366,9 @@ class CheckData extends Command
$invoice_balance = Invoice::where('client_id', $client->id)->where('is_deleted', false)->where('status_id', '>', 1)->withTrashed()->sum('balance');
$credit_balance = Credit::where('client_id', $client->id)->where('is_deleted', false)->withTrashed()->sum('balance');
// $invoice_balance -= $credit_balance;
/*Legacy - V4 will add credits to the balance - we may need to reverse engineer this and remove the credits from the client balance otherwise we need this hack here and in the invoice balance check.*/
if($client->balance != $invoice_balance)
$invoice_balance -= $credit_balance;
$ledger = CompanyLedger::where('client_id', $client->id)->orderBy('id', 'DESC')->first();
@ -405,6 +383,32 @@ class CheckData extends Command
$this->logMessage("{$wrong_paid_to_dates} clients with incorrect client balances");
}
private function checkInvoiceBalances()
{
$wrong_balances = 0;
$wrong_paid_to_dates = 0;
foreach (Client::where('is_deleted', 0)->cursor() as $client) {
$invoice_balance = $client->invoices->where('is_deleted', false)->where('status_id', '>', 1)->sum('balance');
$credit_balance = $client->credits->where('is_deleted', false)->sum('balance');
if($client->balance != $invoice_balance)
$invoice_balance -= $credit_balance;//doesn't make sense to remove the credit amount
$ledger = CompanyLedger::where('client_id', $client->id)->orderBy('id', 'DESC')->first();
if ($ledger && number_format($invoice_balance, 4) != number_format($client->balance, 4)) {
$wrong_balances++;
$this->logMessage("# {$client->id} " . $client->present()->name.' - '.$client->number." - Balance Failure - Invoice Balances = {$invoice_balance} Client Balance = {$client->balance} Ledger Balance = {$ledger->balance}");
$this->isValid = false;
}
}
$this->logMessage("{$wrong_balances} clients with incorrect balances");
}
private function checkLogoFiles()
{
// $accounts = DB::table('accounts')

View File

@ -266,6 +266,10 @@ class ProjectController extends BaseController
$project->number = empty($project->number) ? $this->getNextProjectNumber($project) : $project->number;
$project->save();
if ($request->has('documents')) {
$this->saveDocuments($request->input('documents'), $project);
}
return $this->itemResponse($project->fresh());
}

View File

@ -649,7 +649,8 @@ class Import implements ShouldQueue
unset($resource['invitations'][$key]['recurring_invoice_id']);
}
$modified['invitations'] = $resource['invitations'];
$modified['invitations'] = $this->deDuplicateInvitations($resource['invitations']);
}
$invoice = $invoice_repository->save(
@ -710,8 +711,10 @@ class Import implements ShouldQueue
unset($resource['invitations'][$key]['invoice_id']);
}
$modified['invitations'] = $resource['invitations'];
$modified['invitations'] = $this->deDuplicateInvitations($resource['invitations']);
}
$invoice = $invoice_repository->save(
$modified,
InvoiceFactory::create($this->company->id, $modified['user_id'])
@ -732,6 +735,13 @@ class Import implements ShouldQueue
$invoice_repository = null;
}
/* Prevent edge case where V4 has inserted multiple invitations for a resource for a client contact */
private function deDuplicateInvitations($invitations)
{
return array_intersect_key($invitations, array_unique(array_column($invitations, 'client_contact_id')));
}
private function processCredits(array $data): void
{
Credit::unguard();
@ -811,6 +821,19 @@ class Import implements ShouldQueue
unset($modified['id']);
if (array_key_exists('invitations', $resource)) {
foreach ($resource['invitations'] as $key => $invite) {
$resource['invitations'][$key]['client_contact_id'] = $this->transformId('client_contacts', $invite['client_contact_id']);
$resource['invitations'][$key]['user_id'] = $modified['user_id'];
$resource['invitations'][$key]['company_id'] = $this->company->id;
unset($resource['invitations'][$key]['invoice_id']);
}
$modified['invitations'] = $this->deDuplicateInvitations($resource['invitations']);
}
$quote = $quote_repository->save(
$modified,
QuoteFactory::create($this->company->id, $modified['user_id'])
@ -950,6 +973,7 @@ class Import implements ShouldQueue
/* No validators since data provided by database is already valid. */
foreach ($data as $resource) {
$modified = $resource;
if (array_key_exists('invoice_id', $resource) && $resource['invoice_id'] && ! array_key_exists('invoices', $this->ids)) {
@ -974,20 +998,27 @@ class Import implements ShouldQueue
$file_name = $resource['name'];
$file_path = sys_get_temp_dir().'/'.$file_name;
file_put_contents($file_path, $this->curlGet($file_url));
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$file_info = $finfo->file($file_path);
try {
file_put_contents($file_path, $this->curlGet($file_url));
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$file_info = $finfo->file($file_path);
$uploaded_file = new UploadedFile(
$file_path,
$file_name,
$file_info,
filesize($file_path),
0,
false
);
$uploaded_file = new UploadedFile(
$file_path,
$file_name,
$file_info,
filesize($file_path),
0,
false
);
$this->saveDocument($uploaded_file, $entity, $is_public = true);
$this->saveDocument($uploaded_file, $entity, $is_public = true);
}
catch(\Exception $e) {
//do nothing, gracefully :)
}
}

View File

@ -34,8 +34,12 @@ class MigrationCompleted extends Mailable
$data['company'] = $this->company;
$data['whitelabel'] = $this->company->account->isPaid() ? true : false;
return $this->from(config('mail.from.address'), config('mail.from.name'))
->view('email.import.completed', $data)
->attach($this->company->invoices->first()->pdf_file_path());
$result = $this->from(config('mail.from.address'), config('mail.from.name'))
->view('email.import.completed', $data);
if($this->company->invoices->count() >=1)
$result->attach($this->company->invoices->first()->pdf_file_path());
return $result;
}
}

View File

@ -310,7 +310,7 @@ class BaseRepository
$model = $model->calc()->getCredit();
$model->ledger()->updateCreditBalance(($state['finished_amount'] - $state['starting_amount']));
// $model->ledger()->updateCreditBalance(-1*($state['finished_amount'] - $state['starting_amount']));
if (! $model->design_id)
$model->design_id = $this->decodePrimaryKey($client->getSetting('credit_design_id'));

View File

@ -182,6 +182,14 @@ class InvoiceMigrationRepository extends BaseRepository
}
}
if($data['is_deleted']){
$model->is_deleted = true;
$model->save();
}
if($data['deleted_at'])
$model->delete();
$model->save();
return $model->fresh();

View File

@ -114,11 +114,18 @@ class PaymentMigrationRepository extends BaseRepository
$payment->invoices()->saveMany($invoices);
$payment->invoices->each(function ($inv) use ($invoice_totals, $refund_totals) {
$inv->pivot->amount = $invoice_totals;
$inv->pivot->refunded = $refund_totals;
$inv->pivot->save();
$inv->paid_to_date += $invoice_totals;
if($inv->balance > 0)
$inv->balance -= $invoice_totals;
$inv->balance = max(0, $inv->balance);
$inv->save();
});
@ -135,7 +142,8 @@ class PaymentMigrationRepository extends BaseRepository
$cre->pivot->amount = $credit_totals;
$cre->pivot->save();
$cre->paid_to_date += $invoice_totals;
$cre->paid_to_date += $credit_totals;
$cre->balance -= $credit_totals;
$cre->save();
});
}

View File

@ -188,7 +188,7 @@ trait Refundable
$client_balance_adjustment = $this->adjustInvoices($data);
$credit_note->ledger()->updateCreditBalance($client_balance_adjustment, $ledger_string);
// $credit_note->ledger()->updateCreditBalance($client_balance_adjustment, $ledger_string);
$this->client->paid_to_date -= $data['amount'];
$this->client->save();

View File

@ -139,4 +139,5 @@ return [
'log_pdf_html' => env('LOG_PDF_HTML', false),
'expanded_logging' => env('EXPANDED_LOGGING', false),
'snappdf_chromium_path' => env('SNAPPDF_CHROMIUM_PATH', false),
'v4_migration_version' => '4.5.31',
];