/** * @param Affiliation $affiliation * @param $year * @param $period * * @return AffiliationPdf * @throws \Exception */ public function render(Affiliation $affiliation, $year, $period) { /** @var ProjectService $projectService */ $projectService = $this->getProjectService()->setProject($affiliation->getProject()); $latestVersion = $projectService->getLatestProjectVersion(); $versionService = $this->getVersionService()->setVersion($latestVersion); $contactService = clone $this->getContactService()->setContact($affiliation->getContact()); if (!is_null($this->getAffiliationService()->getFinancialContact($affiliation))) { $financialContactService = clone $this->getContactService()->setContact($this->getAffiliationService()->getFinancialContact($affiliation)); } else { $financialContactService = null; } $affiliationService = $this->getAffiliationService()->setAffiliation($affiliation); $versionContributionInformation = $versionService->getProjectVersionContributionInformation($affiliation, $latestVersion, $year); $invoiceMethod = $this->getInvoiceService()->findInvoiceMethod($projectService->getProject()->getCall()->getProgram()); $pdf = new AffiliationPdf(); $pdf->setTemplate($this->getModuleOptions()->getPaymentSheetTemplate()); $pdf->addPage(); $pdf->SetFontSize(9); $pdf->SetTopMargin(55); $pdf->writeHTMLCell(0, 0, '', '', '<h1 style="color: #00a651">' . sprintf($this->translate("txt-payment-sheet-year-%s-period-%s"), $year, $period) . '</h1>', 0, 1, 0, true, '', true); $pdf->Ln(); $pdf->Line(10, 65, 190, 65, ['color' => [0, 166, 81]]); //Project information $pdf->writeHTMLCell(0, 0, '', '', sprintf("<h3>%s</h3>", $this->translate("txt-project-details")), 0, 1, 0, true, '', true); $projectDetails = [[$this->translate("txt-project-number"), $projectService->getProject()->getNumber()], [$this->translate("txt-project-name"), $projectService->getProject()->getProject()], [$this->translate("txt-start-date"), $projectService->parseOfficialDateStart()->format('d-m-Y')], [$this->translate("txt-start-end"), $projectService->parseOfficialDateEnd()->format('d-m-Y')], [$this->translate("txt-version-name"), $versionService->getVersion()->getVersionType()], [$this->translate("txt-version-status"), $versionService->parseStatus()], [$this->translate("txt-version-date"), !is_null($versionService->getVersion()->getDateReviewed()) ? $versionService->getVersion()->getDateReviewed()->format("d-m-Y") : '']]; $pdf->coloredTable([], $projectDetails, [55, 130]); //Partner information $pdf->writeHTMLCell(0, 0, '', '', sprintf("<h3>%s</h3>", $this->translate("txt-project-partner")), 0, 1, 0, true, '', true); $partnersDetails = [[$this->translate("txt-organisation"), $affiliationService->getAffiliation()->getOrganisation()], [$this->translate("txt-organisation-type"), $affiliationService->getAffiliation()->getOrganisation()->getType()], [$this->translate("txt-country"), $affiliationService->getAffiliation()->getOrganisation()->getCountry()], [$this->translate("txt-total-person-years"), $this->parseEffort($versionContributionInformation->totalEffort)], [$this->translate("txt-total-costs"), $this->parseKiloCost($versionContributionInformation->totalCost)], [$this->translate("txt-average-cost"), ($versionContributionInformation->totalEffort > 0 ? $this->parseKiloCost($versionContributionInformation->totalCost / $versionContributionInformation->totalEffort) : '-') . '/PY']]; $pdf->coloredTable([], $partnersDetails, [55, 130]); //Technical contact $pdf->writeHTMLCell(0, 0, '', '', sprintf("<h3>%s</h3>", $this->translate("txt-technical-contact")), 0, 1, 0, true, '', true); $partnersDetails = [[$this->translate("txt-name"), trim($contactService->parseAttention() . ' ' . $contactService->parseFullName())], [$this->translate("txt-email"), $contactService->getContact()->getEmail()]]; $pdf->coloredTable([], $partnersDetails, [55, 130]); if (!is_null($financialContactService)) { //Financial contact $pdf->writeHTMLCell(0, 0, '', '', sprintf("<h3>%s</h3>", $this->translate("txt-financial-contact")), 0, 1, 0, true, '', true); $financialAddress = $financialContactService->getFinancialAddress(); $financialDetails = [[$this->translate("txt-name"), trim($financialContactService->parseAttention() . ' ' . $financialContactService->parseFullName())], [$this->translate("txt-email"), $financialContactService->getContact()->getEmail()], [$this->translate("txt-vat-number"), $affiliationService->parseVatNumber($affiliationService->getAffiliation())], [$this->translate("txt-billing-address"), !is_null($financialAddress) ? sprintf("%s \n %s\n%s\n%s %s\n%s", $this->getOrganisationService()->parseOrganisationWithBranch($affiliationService->getAffiliation()->getFinancial()->getBranch(), $affiliationService->getAffiliation()->getFinancial()->getOrganisation()), trim($financialContactService->parseAttention() . ' ' . $financialContactService->parseFullName()), $financialAddress->getAddress()->getAddress(), $financialAddress->getAddress()->getZipCode(), $financialAddress->getAddress()->getCity(), strtoupper($financialAddress->getAddress()->getCountry())) : "No billing address could be found"], [$this->translate("txt-preferred-delivery"), is_null($affiliationService->getAffiliation()->getFinancial()) || is_null($affiliationService->getAffiliation()->getFinancial()->getOrganisation()->getFinancial()) ? 'No billing organisation known' : ($affiliationService->getAffiliation()->getFinancial()->getOrganisation()->getFinancial()->getEmail() === Financial::EMAIL_DELIVERY ? sprintf($this->translate("txt-by-email-to-%s"), $financialContactService->getContact()->getEmail()) : $this->translate("txt-by-postal-mail"))]]; $pdf->coloredTable([], $financialDetails, [55, 130]); } $pdf->addPage(); $pdf->writeHTMLCell(0, 0, '', '', '<h3>' . $this->translate("txt-contribution-overview") . '</h3>', 0, 1, 0, true, '', true); $pdf->Ln(); //Funding information $header = [$this->translate("txt-period"), $this->translate("txt-funding-status"), $this->translate("txt-costs"), $this->translate("txt-fee-percentage"), $this->translate("txt-contribution"), $this->translate("txt-due"), $this->translate("txt-amount-due")]; $fundingDetails = []; $totalDueBasedOnProjectData = 0; foreach ($projectService->parseYearRange(false, $affiliation) as $projectYear) { $dueFactor = $affiliationService->parseContributionFactorDue($projectYear, $year, $period); $yearData = []; $yearData[] = $projectYear; if ($affiliationService->isSelfFunded()) { $yearData[] = $this->translate("txt-self-funded"); } elseif (!is_null($affiliationService->getFundingInYear($projectYear))) { $yearData[] = $affiliationService->getFundingInYear($projectYear)->getStatus()->getStatus(); } else { $yearData[] = "-"; } if ($invoiceMethod->getId() === Method::METHOD_PERCENTAGE) { if (array_key_exists($projectYear, $versionContributionInformation->cost)) { $dueInYear = $versionContributionInformation->cost[$projectYear] / 100 * $projectService->findProjectFeeByYear($projectYear)->getPercentage(); $yearData[] = $this->parseCost($versionContributionInformation->cost[$projectYear]); } else { $dueInYear = 0; $yearData[] = $this->parseCost(0); } if ($affiliationService->isFundedInYear($projectYear)) { $yearData[] = $this->parsePercent($projectService->findProjectFeeByYear($projectYear)->getPercentage()); } else { $yearData[] = $this->parsePercent(0); } } else { if (array_key_exists($projectYear, $versionContributionInformation->cost)) { $dueInYear = $versionContributionInformation->effort[$projectYear] * $projectService->findProjectFeeByYear($projectYear)->getContribution(); $yearData[] = $this->parseEffort($versionContributionInformation->effort[$projectYear]); } else { $dueInYear = 0; $yearData[] = 0; } if ($affiliationService->isFundedInYear($projectYear)) { $yearData[] = $this->parseCost($projectService->findProjectFeeByYear($projectYear)->getContribution()); } else { $yearData[] = $this->parseCost(0); } } if ($affiliationService->isFundedInYear($projectYear)) { $yearData[] = $this->parseCost($dueInYear); } else { $yearData[] = $this->parseCost(0); } $yearData[] = $this->parsePercent($dueFactor * 100); $yearData[] = $this->parseCost($dueInYear * $dueFactor); $totalDueBasedOnProjectData += $dueInYear * $dueFactor; $fundingDetails[] = $yearData; } //Add the total column $totalColumn = ['', '', '', '', '', $this->translate("txt-total"), $this->parseCost($totalDueBasedOnProjectData)]; $fundingDetails[] = $totalColumn; $pdf->coloredTable($header, $fundingDetails, [15, 25, 35, 25, 25, 25, 35], true); $contributionDue = $affiliationService->parseContributionDue($versionService->getVersion(), $year, $period); $contributionPaid = $affiliationService->parseContributionPaid($year, $period); $balance = $affiliationService->parseBalance($versionService->getVersion(), $year, $period); $total = $affiliationService->parseTotal($versionService->getVersion(), $year, $period); $contribution = $affiliationService->parseContribution($versionService->getVersion(), $year, $period); $pdf->writeHTMLCell(0, 0, '', '', '<h3>' . sprintf($this->translate("txt-already-sent-invoices-upto-year-%s-period-%s"), $year, $period) . '</h3>', 0, 1, 0, true, '', true); $pdf->Ln(); //Funding information $header = [$this->translate("txt-invoice"), $this->translate("txt-period"), $this->translate("txt-date"), $this->translate("txt-contribution"), $this->translate("txt-paid"), $this->translate("txt-invoiced")]; //Old Invoices $previousInvoices = []; foreach ($affiliation->getInvoice() as $affiliationInvoice) { if (!is_null($affiliationInvoice->getInvoice()->getDayBookNumber()) && ($affiliationInvoice->getYear() < $year || $affiliationInvoice->getYear() === $year && $affiliationInvoice->getPeriod() < $period)) { $previousInvoices[] = $affiliationInvoice; } } $currentInvoiceDetails = []; if (sizeof($previousInvoices) === 0) { $currentInvoiceDetails[] = [$this->translate("txt-no-invoices-found")]; } else { /** * @var $affiliationInvoice AffiliationInvoice */ foreach ($previousInvoices as $affiliationInvoice) { $this->getInvoiceService()->setInvoice($affiliationInvoice->getInvoice()); $currentInvoiceDetails[] = [$affiliationInvoice->getInvoice()->getInvoiceNr(), sprintf("%s-%s", $affiliationInvoice->getYear(), $affiliationInvoice->getPeriod()), $affiliationInvoice->getInvoice()->getDateSent()->format('d-m-Y'), $this->parseCost($this->getInvoiceService()->parseSumAmount()), !is_null($affiliationInvoice->getInvoice()->getBookingDate()) ? $affiliationInvoice->getInvoice()->getBookingDate()->format('d-m-Y') : '', $this->parseCost($this->getInvoiceService()->parseTotal())]; } } //Add the total column $currentInvoiceDetails[] = ['', '', $this->translate("txt-total"), $this->parseCost($contributionPaid), '', '']; $pdf->coloredTable($header, $currentInvoiceDetails, [40, 35, 25, 25, 25, 35], true); $pdf->writeHTMLCell(0, 0, '', '', '<h3>' . $this->translate("txt-correction-calculation") . '</h3>', 0, 1, 0, true, '', true); $correctionDetails = [[sprintf($this->translate("txt-total-contribution-invoiced-upto-year-%s-period-%s"), $year, $period), $this->parseCost($contributionPaid)], [sprintf($this->translate("txt-total-contribution-amount-due-upto-year-%s-period-%s"), $year, $period), $this->parseCost($contributionDue)], [$this->translate("txt-correction"), $this->parseCost($balance)]]; $pdf->coloredTable([], $correctionDetails, [95, 85], true); $pdf->writeHTMLCell(0, 0, '', '', '<h3>' . sprintf($this->translate("txt-invoice-for-year-%s-period-%s"), $year, $period) . '</h3>', 0, 1, 0, true, '', true); $pdf->Ln(); //Partner information $header = [$this->translate("txt-period"), $this->translate("txt-contribution"), $this->translate("txt-amount")]; $upcomingDetails = [[sprintf("%s-%s", $year, $period), sprintf($this->translate("txt-%s-contribution-for-%s"), $this->parsePercent($affiliationService->parseContributionFactor($year, $period) * 100), $year), $this->parseCost($contribution)], ['', $this->translate("txt-correction"), $this->parseCost($balance)], ['', $this->translate("txt-total"), $this->parseCost($total)]]; $pdf->coloredTable($header, $upcomingDetails, [25, 70, 85], true); //Funding information $header = [$this->translate("txt-invoice-number"), $this->translate("txt-period"), $this->translate("txt-date"), $this->translate("txt-paid"), $this->translate("txt-total-excl-vat"), $this->translate("txt-total")]; //Old Invoices $upcomingInvoices = []; foreach ($affiliation->getInvoice() as $affiliationInvoice) { if ($affiliationInvoice->getYear() > $year or $affiliationInvoice->getYear() === $year and $affiliationInvoice->getPeriod() > $period) { if (!is_null($affiliationInvoice->getInvoice()->getDateSent())) { $upcomingInvoices[] = $affiliationInvoice; } } } $upcomingInvoiceDetails = []; if (sizeof($upcomingInvoices) > 0) { $pdf->writeHTMLCell(0, 0, '', '', '<h3>' . sprintf($this->translate("txt-already-sent-invoices-after-year-%s-period-%s") . '</h3>', $year, $period), 0, 1, 0, true, '', true); $pdf->Ln(); /** * @var $affiliationInvoice AffiliationInvoice */ foreach ($upcomingInvoices as $affiliationInvoice) { $this->getInvoiceService()->setInvoice($affiliationInvoice->getInvoice()); $upcomingInvoiceDetails[] = [$affiliationInvoice->getInvoice()->getInvoiceNr(), sprintf("%s-%s", $affiliationInvoice->getYear(), $affiliationInvoice->getPeriod()), $affiliationInvoice->getInvoice()->getDateSent()->format('d-m-Y'), !is_null($affiliationInvoice->getInvoice()->getBookingDate()) ? $affiliationInvoice->getInvoice()->getBookingDate()->format('d-m-Y') : '', $this->parseCost($this->getInvoiceService()->parseSumAmount()), $this->parseCost($this->getInvoiceService()->parseTotal())]; } $pdf->coloredTable($header, $upcomingInvoiceDetails, [45, 25, 25, 25, 25, 35], true); } return $pdf; }
/** * Deactivate an affiliation * * @param Affiliation $affiliation */ public function deactivateAffiliation(Affiliation $affiliation) { $projectService = $this->getProjectService()->setProject($affiliation->getProject()); $affiliation->setDateEnd(new \DateTime()); $this->updateEntity($affiliation); $editYearRange = $projectService->parseEditYearRange(); $minEditYear = array_shift($editYearRange); /** * Remove the current cost and effort of the affiliation */ foreach ($affiliation->getEffort() as $effort) { if ($effort->getDateStart()->format('Y') >= $minEditYear) { $this->getProjectService()->removeEntity($effort); } } /** * Remove the current cost and effort of the affiliation */ foreach ($affiliation->getCost() as $cost) { if ($cost->getDateStart()->format('Y') >= $minEditYear) { $this->getProjectService()->removeEntity($cost); } } }
/** * @param Affiliation $affiliation * @param string $action */ public function updateCountryRationaleByAffiliation(Affiliation $affiliation, $action) { switch ($action) { case self::AFFILIATION_DEACTIVATE: $rationale = $this->findRationaleByProjectAndCountry($affiliation->getProject(), $affiliation->getOrganisation()->getCountry()); //We need to check the rationale and maybe delete or update the contact persons. if (!is_null($rationale) && $rationale->getContact()->getId() === $affiliation->getContact()->getId()) { //There is only 1 rationale, and our partner is contact person, so we need to update the contact //or delete the rationale if there are no other countries available for the project $countryFound = false; //The country is still active in the project, we need to assign a new rationale responsible foreach ($this->getAffiliationService()->findAffiliationByProjectAndCountryAndWhich($affiliation->getProject(), $affiliation->getOrganisation()->getCountry()) as $affiliationService) { //Give the country rationale to the first contact in the affiliation $rationale->setContact($affiliationService->getAffiliation()->getContact()); $this->updateEntity($rationale); break 2; } if (!$countryFound) { $this->removeEntity($rationale); } } break; case self::AFFILIATION_REACTIVATE: //Simply use this function as a proxy to the generate function $this->generateCountryRationaleByProject($affiliation->getProject()); break; } }
/** * @param Affiliation $affiliation * * @return array */ public function findContactsInAffiliation(Affiliation $affiliation) { $contacts = []; $contactRole = []; /* * Add the technical contact */ $contacts[$affiliation->getContact()->getId()] = $affiliation->getContact(); $contactRole[$affiliation->getContact()->getId()][] = 'Technical Contact'; /* * Add the financial contact */ if (!is_null($affiliation->getFinancial())) { $contacts[$affiliation->getFinancial()->getContact()->getId()] = $affiliation->getFinancial()->getContact(); $contactRole[$affiliation->getFinancial()->getContact()->getId()][] = 'Financial Contact'; } /* * Add the associates */ foreach ($affiliation->getAssociate() as $associate) { /* * Add the associates */ $contacts[$associate->getId()] = $associate; $contactRole[$associate->getId()][] = 'Associate'; } /* * Add the workpackage leaders */ foreach ($affiliation->getProject()->getWorkpackage() as $workpackage) { /* * Add the work package leaders */ if (!is_null($workpackage->getContact()->getContactOrganisation()) && $workpackage->getContact()->getContactOrganisation()->getOrganisation()->getId() === $affiliation->getOrganisation()->getId()) { $contacts[$workpackage->getContact()->getId()] = $workpackage->getContact(); $contactRole[$workpackage->getContact()->getId()][] = 'Workpackage leader'; } } $contactRole = array_map('array_unique', $contactRole); //Store the values local for the use of the toArray function $this->contacts = $contacts; return ['contacts' => $contacts, 'contactRole' => $contactRole]; }