Spreadsheet

This commit is contained in:
2018-07-29 21:26:49 +02:00
parent 30535f7048
commit b86c3fc4b9
26 changed files with 1286 additions and 432 deletions
@@ -0,0 +1,80 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 29/07/2018
* Time: 20:40
*/
class InvoiceSheet {
const HEADERS = [
'A' => 'project',
'B' => 'klant',
'C' => 'Totaal te factureren excl. btw',
'D' => 'Totaal te factureren incl. btw',
'E' => 'Gefactureerd overige jaren incl. btw',
'F' => 'Gefactureerd lopende boekjaar incl. btw',
'G' => 'Gefactureerd totaal incl. btw',
'H' => 'Nog te factureren incl. btw'
];
private $invoiceOverview;
private $sheet;
private $row;
public function __construct( $invoiceOverview, $sheet )
{
$this->invoiceOverview = $invoiceOverview;
$this->sheet = $sheet;
$this->handle();
}
private function handle()
{
$this->setHeader();
$this->setBody();
$this->setTotals();
}
private function setHeader()
{
foreach ( self::HEADERS as $cell => $header )
{
$this->sheet->setCellValue( $cell . '1', $header );
$this->sheet->getColumnDimension( $cell )->setAutoSize( TRUE );
}
}
private function setBody()
{
$this->row = 2;
foreach ( $this->invoiceOverview AS $project )
{
$this->sheet->setCellValue( 'A' . $this->row, $project['projectname'] );
$this->sheet->setCellValue( 'B' . $this->row, $project['customer'] );
$this->sheet->setCellValue( 'C' . $this->row, str_replace( '.', '', $project['totalToInvoicingExcl'] ) );
$this->sheet->setCellValue( 'D' . $this->row, str_replace( '.', '', $project['totalToInvoicingIncl'] ) );
$this->sheet->setCellValue( 'E' . $this->row, str_replace( '.', '', $project['totalInvoicedOtherYears'] ) );
$this->sheet->setCellValue( 'F' . $this->row, str_replace( '.', '', $project['totalInvoicedThisYear'] ) );
$this->sheet->setCellValue( 'G' . $this->row, str_replace( '.', '', $project['invoicesTotal'] ) );
$this->sheet->setCellValue( 'H' . $this->row, str_replace( '.', '', $project['needInvoice'] ) );
$this->row ++;
}
}
private function setTotals()
{
$lastRow = $this->row - 1;
$this->sheet->setCellValue( 'C' . $this->row, '=SUM(C2:C' . $lastRow . ')' );
$this->sheet->setCellValue( 'D' . $this->row, '=SUM(D2:D' . $lastRow . ')' );
$this->sheet->setCellValue( 'E' . $this->row, '=SUM(E2:E' . $lastRow . ')' );
$this->sheet->setCellValue( 'F' . $this->row, '=SUM(F2:F' . $lastRow . ')' );
$this->sheet->setCellValue( 'G' . $this->row, '=SUM(G2:G' . $lastRow . ')' );
$this->sheet->setCellValue( 'H' . $this->row, '=SUM(H2:H' . $lastRow . ')' );
}
}
@@ -0,0 +1,88 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 29/07/2018
* Time: 20:04
*/
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
class InvoiceSpreadsheet {
private $invoiceReport;
private $spreadsheet;
private $invoiceSheet;
private $fullpathname;
private $filename;
private $ci;
public function __construct( $invoiceReport )
{
$this->ci =& get_instance();
$this->invoiceReport = $invoiceReport;
$this->ci->load->helper( 'download' );
$this->handle();
}
private function handle()
{
$this->setSpreadsheet();
$this->setInvoiceSheet();
$this->setFilename();
$this->setFullPathname();
$this->createFile();
$this->downloadSpreadsheet();
}
private function setSpreadsheet()
{
$this->spreadsheet = new Spreadsheet();
}
private function setInvoiceSheet()
{
$invoiceSheet = new Worksheet( $this->spreadsheet, 'Facturen' );
$this->invoiceSheet =
$this->spreadsheet->addSheet( $invoiceSheet );
new InvoiceSheet( $this->invoiceReport['invoiceOverview'], $this->invoiceSheet );
}
private function setFilename()
{
$this->filename = 'facturen' . date( 'YmdHis' ) . '.xlsx';
}
private function setFullPathname()
{
$this->fullpathname = MYROOTPATH . 'documents/spreadsheets/' . $this->filename;
}
private function createFile()
{
$writer = new Xlsx( $this->spreadsheet );
$writer->save( $this->fullpathname );
}
private function downloadSpreadsheet()
{
$data = file_get_contents( $this->fullpathname );
force_download( $this->filename, $data );
}
}
@@ -0,0 +1,124 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 29/07/2018
* Time: 13:14
*/
class InvoiceForecast {
private $year;
private $project;
private $customer;
private $forecast;
private $currentYear;
private $projectname;
private $needInvoice;
private $projectDetails;
private $forecastThisYear = 0;
private $forecastRemaining;
private $forecastOtherYears = 0;
public function __construct( $project, $needInvoice, $year )
{
$this->project = $project;
$this->needInvoice = $needInvoice;
$this->year = $year;
$this->handle();
}
public function getProjectDetails()
{
return $this->projectDetails;
}
public function getNeedInvoice()
{
return $this->needInvoice;
}
public function getForecastOtherYear()
{
return $this->forecastOtherYears;
}
public function getForecastRemaining()
{
return $this->forecastRemaining;
}
private function handle()
{
$this->setCurrentyear();
$this->setProjectname();
$this->setCustomer();
$this->loopForecasts();
$this->setForecastRemaining();
$this->setProjectDetails();
}
private function setCurrentyear()
{
$this->currentYear = date( 'Y' );
}
private function setProjectname()
{
$this->projectname = $this->project->projectname . '(' . $this->project->projectnumber . ')';
}
private function setCustomer()
{
$this->customer = $this->project->customer->name;
}
private function loopForecasts()
{
if ( is_null( $this->project->forecasts ) ) return $this->forecastThisYear = 0;
foreach ( $this->project->forecasts as $this->forecast )
{
$this->setForecastThisYear();
$this->setForecastOtherYears();
}
}
private function setForecastThisYear()
{
if ( $this->forecast->year == $this->year )
$this->forecastThisYear += $this->forecast->forecastamount;
}
private function setForecastOtherYears()
{
if ( $this->forecast->year != $this->year )
$this->forecastOtherYears += $this->forecast->forecastamount;
}
private function setForecastRemaining()
{
$this->forecastRemaining =
$this->needInvoice - $this->forecastThisYear - $this->forecastOtherYears;
}
private function setProjectDetails()
{
$this->projectDetails = [
'projectname' => $this->projectname,
'customer' => $this->customer,
'needInvoice' => number_format( $this->needInvoice, 0, ',', '.' ),
'forecastThisYear' => number_format( $this->forecastThisYear, 0, ',', '.' ),
'forecastOtherYears' => number_format( $this->forecastOtherYears, 0, ',', '.' ),
'forecastRemaining' => number_format( $this->forecastRemaining, 0, ',', '.' ),
];
}
}
@@ -0,0 +1,129 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 29/07/2018
* Time: 10:29
*/
class InvoiceOverview {
private $project;
private $currentYear;
private $projectname;
private $customer;
private $totalToInvoicingExcl;
private $totalToInvoicingIncl;
private $totalInvoicedOtherYears;
private $totalInvoicedThisYear;
private $invoicesTotal;
private $needInvoice;
private $projectDetails;
public function __construct( $project )
{
$this->project = $project;
$this->handle();
}
public function getProjectDetails()
{
return $this->projectDetails;
}
public function getNeedinvoice( )
{
return $this->needInvoice;
}
private function handle()
{
$this->setCurrentyear();
$this->setProjectname();
$this->setCustomer();
$this->setTotalToInvoicingExcl();
$this->setTotalToInvoicingIncl();
$this->setTotalInvoicedOtherYears();
$this->setTotalInvoicedThisYear();
$this->setInvoicesTotal();
$this->setNeedInvoice();
$this->setProjectDetails();
}
private function setCurrentyear()
{
$this->currentYear = date( 'Y' );
}
private function setProjectname()
{
$this->projectname = $this->project->projectname . '(' . $this->project->projectnumber . ')';
}
private function setCustomer()
{
$this->customer = $this->project->customer->name;
}
private function setTotalToInvoicingExcl()
{
$offer = $this->project->offer;
$this->totalToInvoicingExcl =
$offer->own_revenue + $offer->other_revenue + $offer->costs;
}
private function setTotalToInvoicingIncl()
{
$this->totalToInvoicingIncl =
$this->totalToInvoicingExcl * ( 1 + ( $this->project->vat->vatrate / 100 ) );
}
private function setTotalInvoicedOtherYears()
{
$this->totalInvoicedOtherYears =
Invoice::invoicePerYear( $this->project->id, $this->currentYear, FALSE );
}
private function setTotalInvoicedThisYear()
{
$this->totalInvoicedThisYear =
Invoice::invoicePerYear( $this->project->id, $this->currentYear, TRUE );
}
private function setInvoicesTotal()
{
$this->invoicesTotal =
$this->totalInvoicedOtherYears + $this->totalInvoicedThisYear;
}
private function setNeedInvoice()
{
$this->needInvoice =
$this->totalToInvoicingIncl - $this->invoicesTotal;
}
private function setProjectDetails()
{
$this->projectDetails = [
'projectname' => $this->projectname,
'customer' => $this->customer,
'totalToInvoicingExcl' => number_format( $this->totalToInvoicingExcl, 0, ',', '.' ),
'totalToInvoicingIncl' => number_format( $this->totalToInvoicingIncl, 0, ',', '.' ),
'totalInvoicedOtherYears' => number_format( $this->totalInvoicedOtherYears, 0, ',', '.' ),
'totalInvoicedThisYear' => number_format( $this->totalInvoicedThisYear, 0, ',', '.' ),
'invoicesTotal' => number_format( $this->invoicesTotal, 0, ',', '.' ),
'needInvoice' => number_format( $this->needInvoice, 0, ',', '.' )
];
}
}
@@ -0,0 +1,104 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 29/07/2018
* Time: 10:19
*/
class Invoices {
private $year;
private $project;
private $projects;
private $monthPayment;
private $monthPayments;
private $monthForecast;
private $monthForecasts;
private $invoiceOverview;
private $invoiceForecast;
private $invoiceForecasts;
private $invoiceOverviews = [];
private $invoiceReport = [];
public function __construct( $year )
{
$this->year = $year;
$this->handle();
}
public function getInvoiceReport()
{
return $this->invoiceReport;
}
private function handle()
{
$this->setProjects();
$this->loopProjects();
$this->setInvoiceReport();
}
private function setProjects()
{
$this->projects = Project::getProjectsForReport();
}
private function loopProjects()
{
foreach ( $this->projects as $this->project )
{
$this->setInvoiceOverview();
$this->setInvoiceForecasts();
$this->setMonthForecasts();
$this->setMonthPayments();
}
}
private function setInvoiceOverview()
{
$this->invoiceOverview = new InvoiceOverview( $this->project, $this->year );
$this->invoiceOverviews[] = $this->invoiceOverview->getProjectDetails();
}
private function setInvoiceForecasts()
{
$this->invoiceForecast =
new InvoiceForecast( $this->project, $this->invoiceOverview->getNeedinvoice(), $this->year );
$this->invoiceForecasts[] = $this->invoiceForecast->getProjectDetails();
}
public function setMonthForecasts()
{
$this->monthForecast =
new MonthForecast( $this->invoiceForecast, $this->project, $this->year );
$this->monthForecasts[] = $this->monthForecast->getProjectDetails();
}
public function setMonthPayments()
{
$this->monthPayment =
new MonthPayments( $this->project, $this->year );
$this->monthPayments[] = $this->monthPayment->getProjectDetails();
}
private function setInvoiceReport()
{
$this->invoiceReport = [
'invoiceOverview' => $this->invoiceOverviews,
'forecasts' => $this->invoiceForecasts,
'monthForecasts' => $this->monthForecasts,
'monthPayments' => $this->monthPayments
];
}
}
@@ -0,0 +1,91 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 29/07/2018
* Time: 14:23
*/
class MonthForecast {
private $year;
private $project;
private $forecast;
private $customer;
private $projectname;
private $forecastMonths;
private $projectDetails;
private $invoiceForecast;
public function __construct( InvoiceForecast $invoiceForecast, $project, $year )
{
$this->project = $project;
$this->invoiceForecast = $invoiceForecast;
$this->year = $year;
$this->handle();
}
public function getProjectDetails()
{
return $this->projectDetails;
}
private function handle()
{
$this->setProjectname();
$this->setCustomer();
$this->setInitialsForecastMonths();
$this->loopForecasts();
$this->setProjectDetails();
}
private function setProjectname()
{
$this->projectname = $this->project->projectname . '(' . $this->project->projectnumber . ')';
}
private function setCustomer()
{
$this->customer = $this->project->customer->name;
}
private function setInitialsForecastMonths()
{
for ( $i = 1; $i <= 12; $i ++ )
{
$this->forecastMonths[ $i ] = ['forecastamount' => 0];
}
}
private function loopForecasts()
{
foreach ( $this->project->forecasts as $this->forecast )
{
$this->setMonthForecast();
}
}
private function setMonthForecast()
{
if ( $this->forecast->year == $this->year )
$this->forecastMonths[ $this->forecast->month_id ]['forecastamount'] += $this->forecast->forecastamount;
}
private function setProjectDetails()
{
$this->projectDetails = [
'projectname' => $this->projectname,
'customer' => $this->customer,
'needInvoice' => number_format( $this->invoiceForecast->getNeedInvoice(), 0, ',', '.' ),
'forecastOtherYears' => number_format( $this->invoiceForecast->getForecastOtherYear(), 0, ',', '.' ),
'forecastRemaining' => number_format( $this->invoiceForecast->getForecastRemaining(), 0, ',', '.' ),
'forecastMonths' => $this->forecastMonths
];
}
}
@@ -0,0 +1,100 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 29/07/2018
* Time: 14:23
*/
class MonthPayments {
private $year;
private $project;
private $payment;
private $customer;
private $projectname;
private $monthPayments;
private $projectDetails;
private $totalProjectPayments;
public function __construct( $project, $year )
{
$this->project = $project;
$this->year = $year;
$this->handle();
}
public function getProjectDetails()
{
return $this->projectDetails;
}
private function handle()
{
$this->setProjectname();
$this->setCustomer();
$this->setInitialsMonthPayments();
$this->loopPayments();
$this->setProjectDetails();
}
private function setProjectname()
{
$this->projectname = $this->project->projectname . '(' . $this->project->projectnumber . ')';
}
private function setCustomer()
{
$this->customer = $this->project->customer->name;
}
private function setInitialsMonthPayments()
{
for ( $i = 1; $i <= 12; $i ++ )
{
$this->monthPayments[ $i ] = ['paymentamount' => 0];
}
}
private function loopPayments()
{
foreach ( $this->project->invoices as $invoice )
{
foreach($invoice->payments as $this->payment)
{
$this->setMonthPayments();
$this->setTotalProjectPayments();
}
}
}
private function setMonthPayments()
{
$paymentYear = date( 'Y', strtotime( $this->payment->paymentdate ) );
$paymentMonth = date( 'n', strtotime( $this->payment->paymentdate ) );
if ( $paymentYear == $this->year )
$this->monthPayments[ $paymentMonth ]['paymentamount'] += $this->payment->paymentamount;
}
private function setTotalProjectPayments()
{
$this->totalProjectPayments += $this->payment->paymentamount;
}
private function setProjectDetails()
{
$this->projectDetails = [
'projectname' => $this->projectname,
'customer' => $this->customer,
'monthPayments' => $this->monthPayments,
'totalProjectPayments' => number_format( $this->totalProjectPayments, 0, ',', '.' )
];
}
}