65 lines
2.4KB

  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, $mergeComplementaryOrderShop = false)
  28. {
  29. if ($this->getTotalOrderPayments($order, $mergeComplementaryOrderShop) >= $this->priceUtils->getTotalWithTax($order) && $this->priceUtils->getTotalWithTax($order) > 0) {
  30. return true;
  31. } else {
  32. return false;
  33. }
  34. }
  35. public function getTotalOrderPayments($order, $mergeComplementaryOrderShop = false): float
  36. {
  37. $totalAmount = floatval(0);
  38. foreach ($order->getOrderPayments() as $orderPayment) {
  39. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  40. }
  41. if($mergeComplementaryOrderShop) {
  42. foreach ($order->getComplementaryOrderShops() as $complementaryOrderShop) {
  43. foreach ($complementaryOrderShop->getOrderPayments() as $orderPayment) {
  44. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  45. }
  46. }
  47. }
  48. return $totalAmount;
  49. }
  50. }