You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.7KB

  1. <?php
  2. namespace Lc\ShopBundle\Services\Order;
  3. use App\Entity\CreditHistory;
  4. use App\Factory\Order\OrderPaymentFactory;
  5. use Lc\ShopBundle\Context\OrderPaymentInterface;
  6. use Lc\ShopBundle\Services\Utils;
  7. trait OrderUtilsPaymentTrait
  8. {
  9. public function createOrderPayment($params = [])
  10. {
  11. $orderPayment = $this->factoryManager->createEntity(OrderPaymentFactory::class, $params);
  12. if(isset($params['meanPayment']) && $params['meanPayment'] == Utils::MEAN_PAYMENT_CREDIT) {
  13. $this->creditUtils->createCreditHistory(CreditHistory::TYPE_DEBIT, $this->security->getUser(), [
  14. 'orderPayment' => $orderPayment
  15. ]);
  16. }
  17. $this->em->create($orderPayment) ;
  18. $this->em->flush() ;
  19. return $orderPayment ;
  20. }
  21. public function isOrderPaid($order, $mergeComplementaryOrderShop = false)
  22. {
  23. $totalOrderPayments = $this->getTotalOrderPayments($order, $mergeComplementaryOrderShop) ;
  24. $totalOrder = $this->priceUtils->getTotalWithTax($order) ;
  25. if ( (abs($totalOrderPayments - $totalOrder) < 0.00001
  26. || $totalOrderPayments >= $totalOrder)
  27. && $totalOrder > 0) {
  28. return true;
  29. }
  30. else {
  31. return false;
  32. }
  33. }
  34. public function getTotalOrderPayments($order, $mergeComplementaryOrderShop = false): float
  35. {
  36. $totalAmount = floatval(0);
  37. foreach ($order->getOrderPayments() as $orderPayment) {
  38. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  39. }
  40. if($mergeComplementaryOrderShop) {
  41. foreach ($order->getComplementaryOrderShops() as $complementaryOrderShop) {
  42. foreach ($complementaryOrderShop->getOrderPayments() as $orderPayment) {
  43. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  44. }
  45. }
  46. }
  47. return $totalAmount;
  48. }
  49. public function getTotalRemainingToBePaid($order)
  50. {
  51. return $this->priceUtils->getTotalWithTax($order) - $this->getTotalOrderPayments($order) ;
  52. }
  53. public function isOrderShopPositiveAmountRemainingToBePaid($orderShop)
  54. {
  55. return $this->getTotalRemainingToBePaid($orderShop) > 0 ;
  56. }
  57. public function addPayment()
  58. {
  59. }
  60. }