vendor/bluue/sales-bundle/src/Entity/Payment.php line 44

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Quentin CHATELAIN (contact@scaledev.fr)
  4.  * @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
  5.  * @license commercial
  6.  */
  7. declare(strict_types=1);
  8. namespace Bluue\SalesBundle\Entity;
  9. use App\Entity\Context;
  10. use App\Entity\Currency;
  11. use App\Entity\Establishment;
  12. use App\Entity\Traits\EntityManagerServiceEntity;
  13. use App\Services\SoftdeleteFilter;
  14. use DateTimeInterface;
  15. use Doctrine\Common\Collections\ArrayCollection;
  16. use Doctrine\Common\Collections\Collection;
  17. use Bluue\CustomersBundle\Entity\Customer;
  18. use Doctrine\ORM\Mapping as ORM;
  19. use Doctrine\ORM\NonUniqueResultException;
  20. use Symfony\Component\Uid\Uuid;
  21. use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
  22. use Gedmo\Mapping\Annotation as Gedmo;
  23. use Bluue\SalesBundle\Repository\PaymentRepository;
  24. use App\DoctrineExtensions\Timestampable\Traits\UserTimestampableEntity;
  25. use App\DoctrineExtensions\SoftDeleteable\Traits\UserSoftDeleteableEntity;
  26. /**
  27.  * @ORM\Entity(repositoryClass=PaymentRepository::class)
  28.  * @ORM\Table(name="sales_bundle__payment", indexes={
  29.  *  @ORM\Index(name="payment_date", columns={"payment_date"}),
  30.  *  @ORM\Index(name="reference", columns={"reference"}),
  31.  *  @ORM\Index(name="receipt", columns={"receipt"}),
  32.  *  @ORM\Index(name="refund", columns={"refund"}),
  33.  *  @ORM\Index(name="deleted_at", columns={"deleted_at"}),
  34.  *  @ORM\Index(name="created_at", columns={"created_at"}),
  35.  *  @ORM\Index(name="updated_at", columns={"updated_at"})
  36.  * })
  37.  * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false, hardDelete=false)
  38.  */
  39. class Payment
  40. {
  41.     use UserTimestampableEntity;
  42.     use UserSoftDeleteableEntity;
  43.     use EntityManagerServiceEntity;
  44.     /**
  45.      * @ORM\Id
  46.      * @ORM\Column(type="uuid")
  47.      * @ORM\GeneratedValue(strategy="CUSTOM")
  48.      * @ORM\CustomIdGenerator(class=UuidGenerator::class)
  49.      */
  50.     private ?Uuid $id null;
  51.     /**
  52.      * @ORM\ManyToOne(targetEntity=Context::class)
  53.      */
  54.     private ?Context $context null;
  55.     /**
  56.      * @ORM\ManyToOne(targetEntity=Establishment::class)
  57.      */
  58.     private ?Establishment $establishment null;
  59.     /**
  60.      * @ORM\ManyToOne(targetEntity=Customer::class)
  61.      * @ORM\JoinColumn(nullable=false)
  62.      */
  63.     private ?Customer $customer null;
  64.     /**
  65.      * @ORM\ManyToOne(targetEntity=CreditNote::class)
  66.      */
  67.     private ?CreditNote $credit_note null;
  68.     /**
  69.      * @ORM\ManyToOne(targetEntity=Invoice::class)
  70.      */
  71.     private ?Invoice $invoice null;
  72.     /**
  73.      * @ORM\ManyToOne(targetEntity=PaymentMethod::class)
  74.      * @ORM\JoinColumn(nullable=false)
  75.      */
  76.     private ?PaymentMethod $payment_method null;
  77.     /**
  78.      * @ORM\ManyToOne(targetEntity=Bank::class)
  79.      */
  80.     private ?Bank $bank null;
  81.     /**
  82.      * @ORM\ManyToOne(targetEntity=Currency::class)
  83.      * @ORM\JoinColumn(nullable=false)
  84.      */
  85.     private ?Currency $currency null;
  86.     /**
  87.      * @ORM\Column(type="decimal", precision=20, scale=12)
  88.      */
  89.     private ?string $currency_change_rate null;
  90.     /**
  91.      * @ORM\Column(type="string", length=128, nullable="true")
  92.      */
  93.     private ?string $reference null;
  94.     /**
  95.      * @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
  96.      */
  97.     private ?string $receipt null;
  98.     /**
  99.      * @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
  100.      */
  101.     private ?string $refund null;
  102.     /**
  103.      * @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
  104.      */
  105.     private ?string $residual null;
  106.     /**
  107.      * @ORM\Column(type="datetime")
  108.      */
  109.     private ?DateTimeInterface $payment_date null;
  110.     /**
  111.      * @ORM\OneToMany(targetEntity=DocumentPayment::class, mappedBy="payment", fetch="EXTRA_LAZY")
  112.      */
  113.     private Collection $document_payments;
  114.     public function __construct()
  115.     {
  116.         $this->document_payments = new ArrayCollection();
  117.     }
  118.     /**
  119.      * @return Uuid|null
  120.      */
  121.     public function getId(): ?Uuid
  122.     {
  123.         return $this->id;
  124.     }
  125.     /**
  126.      * @return Context|null
  127.      */
  128.     public function getContext(): ?Context
  129.     {
  130.         return $this->context;
  131.     }
  132.     /**
  133.      * @param Context|null $context
  134.      * @return Payment
  135.      */
  136.     public function setContext(?Context $context): self
  137.     {
  138.         $this->context $context;
  139.         return $this;
  140.     }
  141.     /**
  142.      * @return Establishment|null
  143.      */
  144.     public function getEstablishment(): ?Establishment
  145.     {
  146.         return $this->establishment;
  147.     }
  148.     /**
  149.      * @param Establishment|null $establishment
  150.      * @return Payment
  151.      */
  152.     public function setEstablishment(?Establishment $establishment): Payment
  153.     {
  154.         $this->establishment $establishment;
  155.         return $this;
  156.     }
  157.     /**
  158.      * @return Customer|null
  159.      * @throws NonUniqueResultException
  160.      */
  161.     public function getCustomer(): ?Customer
  162.     {
  163.         if ($this->em && $this->customer) {
  164.             SoftdeleteFilter::disable($this->em, [Customer::class]);
  165.             $customer $this->em->getRepository(Customer::class)
  166.                 ->createQueryBuilder('c')
  167.                 ->where('c.id = :customer')
  168.                 ->setParameter('customer'$this->customer->getId()->toBinary())
  169.                 ->getQuery()
  170.                 ->getOneOrNullResult();
  171.             SoftdeleteFilter::enable($this->em, [Customer::class]);
  172.             return $customer;
  173.         }
  174.         return $this->customer;
  175.     }
  176.     /**
  177.      * @param Customer $customer
  178.      * @return $this
  179.      */
  180.     public function setCustomer(Customer $customer): self
  181.     {
  182.         $this->customer $customer;
  183.         return $this;
  184.     }
  185.     /**
  186.      * @return CreditNote|null
  187.      */
  188.     public function getCreditNote(): ?CreditNote
  189.     {
  190.         return $this->credit_note;
  191.     }
  192.     /**
  193.      * @param CreditNote|null $credit_note
  194.      * @return Payment
  195.      * @throws NonUniqueResultException
  196.      */
  197.     public function setCreditNote(?CreditNote $credit_note): self
  198.     {
  199.         $this->credit_note $credit_note;
  200.         $this->setCurrency($credit_note->getCurrency());
  201.         $this->setCurrencyChangeRate($credit_note->getCurrencyChangeRate());
  202.         $this->setReference($credit_note->getReference());
  203.         return $this->setCustomer($credit_note->getCustomer());
  204.     }
  205.     /**
  206.      * @return Invoice|null
  207.      * @throws NonUniqueResultException
  208.      */
  209.     public function getInvoice(): ?Invoice
  210.     {
  211.         if ($this->em && $this->invoice) {
  212.             SoftdeleteFilter::disable($this->em, [Invoice::class]);
  213.             $invoice $this->em->getRepository(Invoice::class)
  214.                 ->createQueryBuilder('i')
  215.                 ->where('i.id = :invoiceId')
  216.                 ->setParameter('invoiceId'$this->invoice->getId()->toBinary())
  217.                 ->getQuery()
  218.                 ->getOneOrNullResult();
  219.             SoftdeleteFilter::enable($this->em, [Invoice::class]);
  220.             return $invoice;
  221.         }
  222.         return $this->invoice;
  223.     }
  224.     /**
  225.      * @param Invoice|null $invoice
  226.      * @return Payment
  227.      * @throws NonUniqueResultException
  228.      */
  229.     public function setInvoice(?Invoice $invoice): self
  230.     {
  231.         $this->invoice $invoice;
  232.         $this->setCurrency($invoice->getCurrency());
  233.         $this->setCurrencyChangeRate($invoice->getCurrencyChangeRate());
  234.         $this->setReference($invoice->getReference());
  235.         return $this->setCustomer($invoice->getCustomer());
  236.     }
  237.     /**
  238.      * @return PaymentMethod|null
  239.      * @throws NonUniqueResultException
  240.      */
  241.     public function getPaymentMethod(): ?PaymentMethod
  242.     {
  243.         if ($this->em && $this->payment_method) {
  244.             SoftdeleteFilter::disable($this->em, [PaymentMethod::class]);
  245.             $payment_method $this->em->getRepository(PaymentMethod::class)
  246.                 ->createQueryBuilder('pm')
  247.                 ->where('pm.id = :paymentMethodId')
  248.                 ->setParameter('paymentMethodId'$this->payment_method->getId()->toBinary())
  249.                 ->getQuery()
  250.                 ->getOneOrNullResult();
  251.             SoftdeleteFilter::enable($this->em, [PaymentMethod::class]);
  252.             return $payment_method;
  253.         }
  254.         return $this->payment_method;
  255.     }
  256.     /**
  257.      * @param PaymentMethod $payment_method
  258.      * @return $this
  259.      */
  260.     public function setPaymentMethod(PaymentMethod $payment_method): self
  261.     {
  262.         $this->payment_method $payment_method;
  263.         return $this;
  264.     }
  265.     /**
  266.      * @return Bank|null
  267.      * @throws NonUniqueResultException
  268.      */
  269.     public function getBank(): ?Bank
  270.     {
  271.         if (!$this->bank) {
  272.             return null;
  273.         } elseif ($this->getId()) {
  274.             SoftdeleteFilter::disable($this->em, [Bank::class]);
  275.             $bank $this->em->getRepository(Bank::class)
  276.                 ->createQueryBuilder('b')
  277.                 ->where('b.id = :bankId')
  278.                 ->setParameter('bankId'$this->bank->getId()->toBinary())
  279.                 ->getQuery()
  280.                 ->getOneOrNullResult();
  281.             SoftdeleteFilter::enable($this->em, [Bank::class]);
  282.             return $bank;
  283.         }
  284.         return null;
  285.     }
  286.     /**
  287.      * @param Bank|null $bank
  288.      */
  289.     public function setBank(?Bank $bank): void
  290.     {
  291.         $this->bank $bank;
  292.     }
  293.     /**
  294.      * @return Currency|null
  295.      */
  296.     public function getCurrency(): ?Currency
  297.     {
  298.         return $this->currency;
  299.     }
  300.     /**
  301.      * @param Currency $currency
  302.      * @return $this
  303.      */
  304.     public function setCurrency(Currency $currency): self
  305.     {
  306.         $this->currency $currency;
  307.         return $this->setCurrencyChangeRate($currency->getChangeRate());
  308.     }
  309.     /**
  310.      * @return string|null
  311.      */
  312.     public function getCurrencyChangeRate(): ?string
  313.     {
  314.         return $this->currency_change_rate;
  315.     }
  316.     /**
  317.      * @param string $currency_change_rate
  318.      * @return $this
  319.      */
  320.     public function setCurrencyChangeRate(string $currency_change_rate): self
  321.     {
  322.         $this->currency_change_rate $currency_change_rate;
  323.         return $this;
  324.     }
  325.     /**
  326.      * @return string|null
  327.      */
  328.     public function getReference(): ?string
  329.     {
  330.         return $this->reference;
  331.     }
  332.     /**
  333.      * @param string|null $reference
  334.      * @return $this
  335.      */
  336.     public function setReference(?string $reference): self
  337.     {
  338.         $this->reference $reference;
  339.         return $this;
  340.     }
  341.     /**
  342.      * @return string|null
  343.      */
  344.     public function getReceipt(): ?string
  345.     {
  346.         return $this->receipt;
  347.     }
  348.     /**
  349.      * @param string|null $receipt
  350.      * @return $this
  351.      */
  352.     public function setReceipt(?string $receipt): self
  353.     {
  354.         $this->receipt $receipt;
  355.         return $receipt $this->setResidual($receipt) : $this;
  356.     }
  357.     /**
  358.      * @return string|null
  359.      */
  360.     public function getRefund(): ?string
  361.     {
  362.         return $this->refund;
  363.     }
  364.     /**
  365.      * @param string|null $refund
  366.      * @return $this
  367.      */
  368.     public function setRefund(?string $refund): self
  369.     {
  370.         $this->refund $refund;
  371.         return $refund $this->setResidual($refund) : $this;
  372.     }
  373.     /**
  374.      * @return string|null
  375.      */
  376.     public function getResidual(): ?string
  377.     {
  378.         return $this->residual;
  379.     }
  380.     /**
  381.      * @param string|null $residual
  382.      * @return Payment
  383.      */
  384.     public function setResidual(?string $residual): self
  385.     {
  386.         $this->residual $residual;
  387.         return $this;
  388.     }
  389.     /**
  390.      * @return DateTimeInterface|null
  391.      */
  392.     public function getPaymentDate(): ?DateTimeInterface
  393.     {
  394.         return $this->payment_date;
  395.     }
  396.     /**
  397.      * @param $payment_date
  398.      * @return $this
  399.      */
  400.     public function setPaymentDate($payment_date): self
  401.     {
  402.         if ($payment_date instanceof DateTimeInterface) {
  403.             $this->payment_date $payment_date;
  404.         } elseif ($payment_date) {
  405.             $this->payment_date = new \DateTime($payment_date);
  406.         } else {
  407.             $this->payment_date null;
  408.         }
  409.         return $this;
  410.     }
  411.     /**
  412.      * @return Collection|DocumentPayment[]
  413.      */
  414.     public function getDocumentPayments(): Collection
  415.     {
  416.         return $this->document_payments;
  417.     }
  418.     /**
  419.      * @return Invoice|CreditNote|null
  420.      * @throws NonUniqueResultException
  421.      */
  422.     public function getDocumentOrigin()
  423.     {
  424.         return $this->getInvoice() ?? $this->getCreditNote();
  425.     }
  426.     /**
  427.      * @return string|null
  428.      */
  429.     public function getAmount(): ?string
  430.     {
  431.         return $this->getReceipt() ?? $this->getRefund();
  432.     }
  433. }