OrderUtilsPaymentTrait.php 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace Lc\ShopBundle\Services\Order;
  3. use Lc\ShopBundle\Context\OrderPaymentInterface;
  4. trait OrderUtilsPaymentTrait
  5. {
  6. public function createOrderPayment($orderShop, $meanPayment, $amount, $reference = null, $comment = null, $paidAt = null)
  7. {
  8. $classOrderPayment = $this->em->getClassMetadata(OrderPaymentInterface::class)->getName();
  9. $orderPayment = new $classOrderPayment;
  10. $orderPayment->setOrderShop($orderShop);
  11. $orderPayment->setMeanPayment($meanPayment);
  12. $orderPayment->setAmount($amount);
  13. $orderPayment->setReference($reference);
  14. $orderPayment->setComment($comment);
  15. $orderPayment->setEditable(false);
  16. $orderPayment->setCreatedBy($orderShop->getUser()) ;
  17. $orderPayment->setUpdatedBy($orderShop->getUser()) ;
  18. if ($paidAt) {
  19. $orderPayment->setPaidAt($paidAt);
  20. } else {
  21. $orderPayment->setPaidAt(new \DateTime('now'));
  22. }
  23. $this->em->persist($orderPayment);
  24. $this->em->flush();
  25. return $orderPayment ;
  26. }
  27. public function isOrderPaid($order)
  28. {
  29. if ($this->getTotalOrderPayments($order) >= $this->priceUtils->getTotalWithTax($order) && $this->priceUtils->getTotalWithTax($order)>0) {
  30. return true;
  31. } else {
  32. return false;
  33. }
  34. }
  35. public function getTotalOrderPayments($order): float
  36. {
  37. $totalAmount = floatval(0);
  38. foreach ($order->getOrderPayments() as $orderPayment) {
  39. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  40. }
  41. return $totalAmount;
  42. }
  43. }