diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index a57375b47612..b1d2bd5bc02f 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -74,7 +74,7 @@ class CheckData extends Command /** * @var string */ - protected $signature = 'ninja:check-data {--database=} {--fix=} {--client_id=} {--vendor_id=} {--paid_to_date=} {--client_balance=} {--ledger_balance=}'; + protected $signature = 'ninja:check-data {--database=} {--fix=} {--client_id=} {--vendor_id=} {--paid_to_date=} {--client_balance=} {--ledger_balance=} {--balance_status=}'; /** * @var string @@ -89,6 +89,9 @@ class CheckData extends Command protected $wrong_balances = 0; + protected $wrong_paid_status = 0; + + public function handle() { $time_start = microtime(true); @@ -109,6 +112,7 @@ class CheckData extends Command $this->checkVendorContacts(); $this->checkEntityInvitations(); $this->checkCompanyData(); + $this->checkBalanceVsPaidStatus(); if(Ninja::isHosted()) $this->checkAccountStatuses(); @@ -856,4 +860,44 @@ class CheckData extends Command }); } + public function checkBalanceVsPaidStatus() + { + $this->wrong_paid_status = 0; + + foreach(Invoice::with(['payments'])->whereHas('payments')->where('status_id', 4)->where('balance', '>', 0)->where('is_deleted',0)->cursor() as $invoice) + { + + $this->logMessage("# {$invoice->id} " . ' - '.$invoice->number." - Marked as paid, but balance = {$invoice->balance}"); + + if($this->option('balance_status')){ + + $val = $invoice->balance; + + $invoice->balance = 0; + $invoice->paid_to_date=$val; + $invoice->save(); + + $p = $invoice->payments->first(); + + if($p && (int)$p->amount == 0) + { + $p->amount = $val; + $p->applied = $val; + $p->save(); + + $pivot = $p->paymentables->first(); + $pivot->amount = $val; + $pivot->save(); + } + + + $this->logMessage("Fixing {$invoice->id} settings payment to {$val}"); + + } + + } + + $this->logMessage($this->wrong_paid_status." wrong invoices with bad balance state"); + + } } \ No newline at end of file diff --git a/app/DataMapper/CompanySettings.php b/app/DataMapper/CompanySettings.php index eef7c3f41555..b253b9397f99 100644 --- a/app/DataMapper/CompanySettings.php +++ b/app/DataMapper/CompanySettings.php @@ -249,6 +249,9 @@ class CompanySettings extends BaseSettings public $primary_color = '#298AAB'; public $secondary_color = '#7081e0'; + public $page_numbering = false; + public $page_numbering_alignment = 'C'; //C,R,L + public $hide_paid_to_date = false; //@TODO where? public $embed_documents = false; //@TODO where? public $all_pages_header = false; //@deprecated 31-05-2021 @@ -274,6 +277,7 @@ class CompanySettings extends BaseSettings public $auto_archive_invoice_cancelled = false; public static $casts = [ + 'page_numbering' => 'bool', 'auto_archive_invoice_cancelled' => 'bool', 'email_from_name' => 'string', 'show_all_tasks_client_portal' => 'string', diff --git a/app/Jobs/Entity/CreateEntityPdf.php b/app/Jobs/Entity/CreateEntityPdf.php index ba7bfb75dd50..54bba3c058b7 100644 --- a/app/Jobs/Entity/CreateEntityPdf.php +++ b/app/Jobs/Entity/CreateEntityPdf.php @@ -34,6 +34,8 @@ use App\Utils\PhantomJS\Phantom; use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesInvoiceHtml; use App\Utils\Traits\NumberFormatter; +use App\Utils\Traits\Pdf\PageNumbering; +use App\Utils\Traits\Pdf\PDF; use App\Utils\Traits\Pdf\PdfMaker; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; @@ -43,10 +45,11 @@ use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Lang; use Illuminate\Support\Facades\Storage; +use setasign\Fpdi\PdfParser\StreamReader; class CreateEntityPdf implements ShouldQueue { - use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter, MakesInvoiceHtml, PdfMaker, MakesHash; + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter, MakesInvoiceHtml, PdfMaker, MakesHash, PageNumbering; public $entity; @@ -102,6 +105,7 @@ class CreateEntityPdf implements ShouldQueue public function handle() { + MultiDB::setDb($this->company->db); /* Forget the singleton*/ @@ -186,9 +190,15 @@ class CreateEntityPdf implements ShouldQueue if(config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja'){ $pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true)); + $pdf = $this->pageNumbering($pdf, $this->company); + } else { + $pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true)); + + $pdf = $this->pageNumbering($pdf, $this->company); + } } catch (\Exception $e) { @@ -203,10 +213,10 @@ class CreateEntityPdf implements ShouldQueue try{ - if(!Storage::disk($this->disk)->exists($path)) - + if(!Storage::disk($this->disk)->exists($path)) Storage::disk($this->disk)->makeDirectory($path, 0775); - Storage::disk($this->disk)->put($file_path, $pdf, 'public'); + + Storage::disk($this->disk)->put($file_path, $pdf, 'public'); } catch(\Exception $e) @@ -216,7 +226,7 @@ class CreateEntityPdf implements ShouldQueue } } - + return $file_path; } @@ -225,4 +235,5 @@ class CreateEntityPdf implements ShouldQueue } + } diff --git a/app/Jobs/Util/PreviewPdf.php b/app/Jobs/Util/PreviewPdf.php index 3a5b23f7ce0d..fda53302cd19 100644 --- a/app/Jobs/Util/PreviewPdf.php +++ b/app/Jobs/Util/PreviewPdf.php @@ -12,6 +12,7 @@ namespace App\Jobs\Util; use App\Models\Company; +use App\Utils\Traits\Pdf\PageNumbering; use App\Utils\Traits\Pdf\PdfMaker; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; @@ -21,7 +22,7 @@ use Illuminate\Queue\SerializesModels; class PreviewPdf implements ShouldQueue { - use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, PdfMaker; + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, PdfMaker, PageNumbering; public $company; @@ -46,6 +47,10 @@ class PreviewPdf implements ShouldQueue public function handle() { - return $this->makePdf(null, null, $this->design_string); + $pdf = $this->makePdf(null, null, $this->design_string); + + $pdf = $this->pageNumbering($pdf, $this->company); + + return $pdf; } } diff --git a/app/Models/Company.php b/app/Models/Company.php index 40300ae8972b..8a08e6f6bb37 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -11,6 +11,7 @@ namespace App\Models; +use App\DataMapper\CompanySettings; use App\Models\Language; use App\Models\Presenters\CompanyPresenter; use App\Models\User; @@ -402,6 +403,12 @@ class Company extends BaseModel return $this->settings->{$setting}; } + $cs = CompanySettings::defaults(); + + if (property_exists($cs, $setting) != false) { + return $cs->{$setting}; + } + return null; } diff --git a/app/Utils/Traits/Pdf/PDF.php b/app/Utils/Traits/Pdf/PDF.php index 9e1e505d96ee..3e966b2c440d 100644 --- a/app/Utils/Traits/Pdf/PDF.php +++ b/app/Utils/Traits/Pdf/PDF.php @@ -1,4 +1,13 @@ SetY(-7); - // $this->SetX(-50); $this->SetXY(0, -5); - // Arial italic 8 - $this->SetFont('Arial','I',8); - // Page number - $this->Cell(0,5, ctrans('texts.page').' '.$this->PageNo().'/{nb}',0,0,'C'); + $this->SetFont('Arial','I',9); + + $trans = ctrans('texts.pdf_page_info', ['current' => $this->PageNo(), 'total' => '{nb}']); + + $this->Cell(0,5, $trans ,0,0, $this->text_alignment); + } + + public function setAlignment($alignment) + { + $this->text_alignment = $alignment; + + return $this; + } + } \ No newline at end of file diff --git a/app/Utils/Traits/Pdf/PageNumbering.php b/app/Utils/Traits/Pdf/PageNumbering.php index e012675c25bd..571853c2017d 100644 --- a/app/Utils/Traits/Pdf/PageNumbering.php +++ b/app/Utils/Traits/Pdf/PageNumbering.php @@ -1,5 +1,4 @@ setSourceFile(StreamReader::createByString($pdf_data_object)); + try + { + $pdf = new PDF(); - $pdf->AliasNbPages(); - for ($i=1; $i <= $pageCount; $i++) { - //import a page then get the id and will be used in the template - $tplId = $pdf->importPage($i); + $pdf->setAlignment($company->getSetting('page_numbering_alignment')); - //create a page - $templateSize = $pdf->getTemplateSize($tplId); - $pdf->AddPage('', [$templateSize['width'], $templateSize['height']]); + $pageCount = $pdf->setSourceFile(StreamReader::createByString($pdf_data_object)); - $pdf->useTemplate($tplId); - } - - - return $pdf->Output(); + $pdf->AliasNbPages(); - } + for ($i=1; $i <= $pageCount; $i++) { + //import a page then get the id and will be used in the template + $tplId = $pdf->importPage($i); -} \ No newline at end of file + //create a page + $templateSize = $pdf->getTemplateSize($tplId); + + $pdf->AddPage($templateSize['orientation'], [$templateSize['width'], $templateSize['height']]); + + $pdf->useTemplate($tplId); + } + + ob_end_flush(); + return $pdf->Output(); + + } + catch(\Exception $e) { + nlog($e->getMessage()); + + } + } +} diff --git a/app/Utils/Traits/Pdf/PdfMaker.php b/app/Utils/Traits/Pdf/PdfMaker.php index 7c33990b48e5..f888e66b847f 100644 --- a/app/Utils/Traits/Pdf/PdfMaker.php +++ b/app/Utils/Traits/Pdf/PdfMaker.php @@ -46,7 +46,6 @@ trait PdfMaker if($generated) return $generated; - throw new InternalPDFFailure('There was an issue generating the PDF locally'); } }