mirror of
https://github.com/quentin-g-dev/afup.git
synced 2026-03-26 02:02:15 +01:00
105 lines
3.2 KiB
PHP
105 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace AppBundle\Event\Invoice;
|
|
|
|
use AppBundle\Event\Model\Invoice;
|
|
use AppBundle\Event\Model\Repository\InvoiceRepository;
|
|
use AppBundle\Event\Model\Repository\TicketRepository;
|
|
use AppBundle\Event\Model\Ticket;
|
|
|
|
class InvoiceService
|
|
{
|
|
/** @var InvoiceRepository */
|
|
private $invoiceRepository;
|
|
/** @var TicketRepository */
|
|
private $ticketRepository;
|
|
|
|
public function __construct(
|
|
InvoiceRepository $invoiceRepository,
|
|
TicketRepository $ticketRepository
|
|
) {
|
|
$this->invoiceRepository = $invoiceRepository;
|
|
$this->ticketRepository = $ticketRepository;
|
|
}
|
|
|
|
public function handleInvoicing(
|
|
$reference,
|
|
$paymentType,
|
|
$paymentInfos,
|
|
$paymentDate,
|
|
$email,
|
|
$company,
|
|
$lastname,
|
|
$firstname,
|
|
$address,
|
|
$zipcode,
|
|
$city,
|
|
$countryId,
|
|
$eventId,
|
|
$oldReference = null,
|
|
$authorization = null,
|
|
$transaction = null,
|
|
$status = Ticket::STATUS_CREATED
|
|
) {
|
|
$tickets = $this->ticketRepository->getByReference($reference);
|
|
$amount = 0.0;
|
|
foreach ($tickets as $ticket) {
|
|
$amount = round($amount + $ticket->getAmount(), 2);
|
|
}
|
|
$nbTickets = count($tickets);
|
|
$oldInvoice = null;
|
|
if ($oldReference === '') {
|
|
$oldReference = null;
|
|
}
|
|
if (null !== $oldReference) {
|
|
$oldInvoice = $this->invoiceRepository->getByReference($oldReference);
|
|
}
|
|
// Si la reference n'existe pas on l'ajoute sinon on la met à jour...
|
|
if ($nbTickets === 0 || null === $oldInvoice) {
|
|
$invoice = new Invoice();
|
|
$invoice->setReference($reference);
|
|
$invoice->setInvoice(false);
|
|
} else {
|
|
$invoice = $oldInvoice;
|
|
$invoice->setInvoice(true);
|
|
}
|
|
$invoice->setAmount($amount);
|
|
$invoice->setPaymentType($paymentType);
|
|
$invoice->setPaymentInfos($paymentInfos);
|
|
$invoice->setPaymentDate($paymentDate);
|
|
$invoice->setCompany($company);
|
|
$invoice->setLastname($lastname);
|
|
$invoice->setFirstname($firstname);
|
|
$invoice->setEmail($email);
|
|
$invoice->setAddress($address);
|
|
$invoice->setZipcode($zipcode);
|
|
$invoice->setCity($city);
|
|
$invoice->setCountryId($countryId);
|
|
$invoice->setAuthorization($authorization);
|
|
$invoice->setTransaction($transaction);
|
|
$invoice->setStatus($status);
|
|
$invoice->setForumId($eventId);
|
|
$this->invoiceRepository->save($invoice);
|
|
// Si on change de reference
|
|
if (null !== $oldInvoice && $oldReference !== $reference) {
|
|
$this->deleteInvoice($oldInvoice);
|
|
}
|
|
}
|
|
|
|
public function deleteInvoice(Invoice $invoice)
|
|
{
|
|
$tickets = $this->ticketRepository->getByReference($invoice->getReference());
|
|
if (0 === count($tickets)) {
|
|
$this->invoiceRepository->delete($invoice);
|
|
|
|
return;
|
|
}
|
|
$amount = 0.0;
|
|
foreach ($tickets as $ticket) {
|
|
$amount = round($amount + $ticket->getAmount(), 2);
|
|
}
|
|
$invoice->setAmount($amount);
|
|
$this->invoiceRepository->save($invoice);
|
|
}
|
|
}
|