|
- <?php
-
- namespace common\logic\Payment\Service;
-
- use common\helpers\MeanPayment;
- use common\logic\AbstractService;
- use common\logic\Order\Order\Model\Order;
- use common\logic\Payment\Model\Payment;
- use common\logic\SolverInterface;
-
- class PaymentSolver extends AbstractService implements SolverInterface
- {
- public function isTypeDebit(Payment $payment): bool
- {
- return in_array($payment->getType(), [
- Payment::TYPE_DEBIT,
- Payment::TYPE_PAYMENT,
- ]);
- }
-
- public function isTypeCredit(Payment $payment): bool
- {
- return in_array($payment->getType(), [
- Payment::TYPE_CREDIT,
- Payment::TYPE_INITIAL_CREDIT,
- Payment::TYPE_REFUND
- ]);
- }
-
- public function sumAmountPaid(Payment $payment, float $amount = 0)
- {
- if($this->isTypeDebit($payment)) {
- $amount += $payment->amount;
- }
- else {
- $amount -= $payment->amount;
- }
-
- return $amount;
- }
-
- public function getAmount(Payment $payment, bool $format = false): string
- {
- if ($format) {
- return number_format($payment->getAmount(), 2) . ' €';
- } else {
- return $payment->getAmount();
- }
- }
-
- /**
- * Retourne le libellé du CreditHistory informant de son type et
- * éventuellement de la date de sa commande associée.
- *
- */
- public function getStrWording(Payment $payment, Order $order = null): string
- {
- $str = '';
- $type = $payment->getType();
-
- if (Payment::TYPE_INITIAL_CREDIT == $type) {
- $str = 'Crédit initial';
- } elseif (Payment::TYPE_CREDIT == $type) {
- $str = 'Crédit';
- } elseif (Payment::TYPE_PAYMENT == $type) {
- $str = 'Débit';
- } elseif (Payment::TYPE_REFUND == $type) {
- $str = 'Recrédit';
- } elseif (Payment::TYPE_DEBIT == $type) {
- $str = 'Débit';
- }
-
- if (Payment::TYPE_PAYMENT == $type || Payment::TYPE_REFUND == $type) {
-
- // Optimisation
- if(!$order) {
- $order = $payment->order;
- }
-
- if ($order && $order->distribution) {
- $str .= '<br />Commande : ' . date('d/m/Y', strtotime($order->distribution->date));
- } else {
- $str .= '<br />Commande supprimée';
- }
- }
-
- return $str;
- }
-
- public function getDate(Payment $payment, bool $format = false): string
- {
- $date = $payment->getDate();
-
- if ($format) {
- return date('d/m/Y à H:i:s', strtotime($date));
- } else {
- return $date;
- }
- }
-
- public function isMeanPaymentCredit(Payment $payment): bool
- {
- return $payment->mean_payment == MeanPayment::CREDIT;
- }
-
- public function getStrMeanPayment(Payment $payment): string
- {
- return MeanPayment::getStrBy($payment->getMeanPayment());
- }
-
- public function getStrType(Payment $payment): string
- {
- switch($payment->type) {
- case Payment::TYPE_PAYMENT:
- return 'Paiement';
- case Payment::TYPE_DEBIT:
- return 'Débit';
- case Payment::TYPE_CREDIT:
- return 'Crédit';
- case Payment::TYPE_REFUND:
- return 'Remboursement';
- case Payment::TYPE_INITIAL_CREDIT:
- return 'Crédit initial';
- }
-
- return 'Type de paiement inconnu';
- }
-
- public function getStrUserAction(Payment $payment): string
- {
- $userAction = $payment->getUserActionObject();
-
- if ($userAction) {
- return $userAction->getName() . ' ' . $userAction->getlastname();
- } else {
- return 'Système';
- }
- }
- }
|