src/Entity/Wallet.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\WalletRepository;
  4. use DateTime;
  5. use Doctrine\DBAL\Types\Types;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Gedmo\Blameable\Traits\BlameableEntity;
  8. use Gedmo\Timestampable\Traits\TimestampableEntity;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. #[ORM\Entity(repositoryClassWalletRepository::class)]
  11. class Wallet
  12. {
  13.     use BlameableEntity//Hook blameable behaviour. Updates createdBy, updatedBy fields
  14.     use TimestampableEntity//Hook timestampable behaviour. Updates createdAt, updatedAt fields 
  15.     const TYPE_SHARE_REWARD 'TYPE_SHARE_REWARD'// Coins earned by the user because of sharing.
  16.     const TYPE_INFLUENCER_SHARE_REWARD 'TYPE_INFLUENCER_SHARE_REWARD'// Coins earned by the user because of sharing.
  17.     const TYPE_WALLET_SPENT 'TYPE_WALLET_SPENT'// Coins spent by the user.
  18.     const TYPE_ACHIEVEMENT_ACHIEVED 'TYPE_ACHIEVEMENT_ACHIEVED';
  19.     const TYPE_ADMIN_REWARD 'TYPE_ADMIN_REWARD'// Coins earned/spent by the user. Set by the admin
  20.     #[ORM\Id]
  21.     #[ORM\GeneratedValue(strategy"IDENTITY")]
  22.     #[ORM\Column]
  23.     private ?int $id null;
  24.     #[ORM\ManyToOne(inversedBy'wallets')]
  25.     #[ORM\JoinColumn(nullablefalse)]
  26.     #[Assert\NotNull()]
  27.     private ?User $owner null;
  28.     #[ORM\Column(typeTypes::DECIMALprecision10scale2)]
  29.     #[Assert\NotNull()]
  30.     private ?string $amount null;
  31.     #[ORM\OneToOne(cascade: ['persist''remove'])]
  32.     private ?OrderDetail $orderDetail null;
  33.     #[ORM\Column(length50)]
  34.     #[Assert\NotNull()]
  35.     #[Assert\Length(max50)]
  36.     private ?string $type null;
  37.     #[ORM\OneToOne(inversedBy'walletSpent'cascade: ['persist''remove'])]
  38.     private ?Order $spentOrder null;
  39.     #[ORM\Column(length511nullabletrue)]
  40.     #[Assert\Length(max511)]
  41.     private ?string $description null;
  42.     #[ORM\Column]
  43.     private ?bool $orderPaid true;
  44.     #[ORM\OneToOne(inversedBy'walletSpentRefund'cascade: ['persist''remove'])]
  45.     private ?Order $spentOrderRefund null;
  46.     public function __construct()
  47.     {
  48.     }
  49.     public static function createShareRewardFromUser(UserShared $userSharedOrderDetail $newOrderDetail)
  50.     {
  51.         $instance = (new Wallet())->setType(Wallet::TYPE_SHARE_REWARD);
  52.         $amount =
  53.             ($userShared->getReferredPoints() ?? 0) +
  54.             ($userShared->getExtraReferredPoints() ?? 0);
  55.         $instance->setAmount($amount);
  56.         $instance->setOwner($userShared->getOriginOrderDetail()->getHeader()->getBuyer());
  57.         $instance->setOrderDetail($newOrderDetail);
  58.         $instance->setOrderPaid(true);
  59.         return $instance;
  60.     }
  61.     public static function createShareRewardFromInfluencer(UserShared $userSharedOrderDetail $newOrderDetail)
  62.     {
  63.         $instance = (new Wallet())->setType(Wallet::TYPE_INFLUENCER_SHARE_REWARD);
  64.         $instance->setAmount($userShared->getReferredPoints() ?? 0);
  65.         $instance->setOwner($userShared->getInfluencer());
  66.         $instance->setOrderDetail($newOrderDetail);
  67.         $instance->setOrderPaid(true);
  68.         return $instance;
  69.     }
  70.     public static function createWalletSpent(float $amountOrder $newOrder)
  71.     {
  72.         $instance = (new Wallet())->setType(Wallet::TYPE_WALLET_SPENT);
  73.         $instance->setAmount($amount);
  74.         $instance->setOwner($newOrder->getBuyer());
  75.         $instance->setSpentOrder($newOrder);
  76.         $instance->setOrderPaid(false);
  77.         return $instance;
  78.     }
  79.     public static function createWalletSpentRefund(Order $spentOrderRefund): ?Wallet
  80.     {
  81.         $walletSpent $spentOrderRefund->getWalletSpent();
  82.         if (!$walletSpent) { 
  83.             return null;
  84.         }
  85.         $instance = (new Wallet())->setType(Wallet::TYPE_WALLET_SPENT);
  86.         $instance->setAmount(-$walletSpent->getAmount());
  87.         $instance->setOwner($spentOrderRefund->getBuyer());
  88.         $instance->setSpentOrderRefund($spentOrderRefund);
  89.         $instance->setOrderPaid(true);
  90.         return $instance;
  91.     }
  92.     public static function createAchievementAchieved()
  93.     {
  94.         return (new Wallet())->setType(Wallet::TYPE_ACHIEVEMENT_ACHIEVED);
  95.     }
  96.     public static function createAdminReward()
  97.     {
  98.         return (new Wallet())->setType(Wallet::TYPE_ADMIN_REWARD);
  99.     }
  100.     public function getId(): ?int
  101.     {
  102.         return $this->id;
  103.     }
  104.     public function getOwner(): ?User
  105.     {
  106.         return $this->owner;
  107.     }
  108.     public function setOwner(?User $owner): self
  109.     {
  110.         $this->owner $owner;
  111.         return $this;
  112.     }
  113.     public function getAmount(): ?string
  114.     {
  115.         return $this->amount;
  116.     }
  117.     public function setAmount(string $amount): self
  118.     {
  119.         $this->amount $amount;
  120.         return $this;
  121.     }
  122.     
  123.     public function getOrderDetail(): ?OrderDetail
  124.     {
  125.         return $this->orderDetail;
  126.     }
  127.     public function setOrderDetail(?OrderDetail $orderDetail): self
  128.     {
  129.         $this->orderDetail $orderDetail;
  130.         return $this;
  131.     }
  132.     public function getType(): ?string
  133.     {
  134.         return $this->type;
  135.     }
  136.     public function setType(?string $type): self
  137.     {
  138.         $this->type $type;
  139.         return $this;
  140.     }
  141.     public function isPending(): bool
  142.     {
  143.         if (
  144.             $this->getType() == Wallet::TYPE_SHARE_REWARD ||
  145.             $this->getType() == Wallet::TYPE_INFLUENCER_SHARE_REWARD
  146.         ) {
  147.             if (
  148.                 $this->getOrderDetail()->getHeader()->getStatus()->getKey() == OrderStatus::PAID
  149.             ) {
  150.                 if ($this->getOrderDetail()->getProduct()->getAllowReturn()) {
  151.                     $endOffer $this->getOrderDetail()->getProduct()->getEndOfferDate();
  152.                     $days $this->getOrderDetail()->getProduct()->getNDaysAllowedForReturn() ?? 0;
  153.                     $dateToCompare = new DateTime('-' $days 'day');
  154.                     return ($endOffer $dateToCompare);
  155.                 } else {
  156.                     return false;
  157.                 }
  158.             } 
  159.             return true;
  160.         }
  161.         return false;
  162.     }
  163.     public function isCancelled(): bool
  164.     {
  165.         if (
  166.             $this->getType() == Wallet::TYPE_SHARE_REWARD ||
  167.             $this->getType() == Wallet::TYPE_INFLUENCER_SHARE_REWARD
  168.         ) {
  169.             if (
  170.                 $this->getOrderDetail()->getHeader()->getStatus()->getKey() == OrderStatus::NOT_SOLD ||
  171.                 $this->getOrderDetail()->getHeader()->getStatus()->getKey() == OrderStatus::CANCELED
  172.             ) {
  173.                 return true;
  174.             } 
  175.         }
  176.         return false;
  177.     }
  178.     public function getSpentOrder(): ?Order
  179.     {
  180.         return $this->spentOrder;
  181.     }
  182.     public function setSpentOrder(?Order $spentOrder): self
  183.     {
  184.         $this->spentOrder $spentOrder;
  185.         return $this;
  186.     }
  187.     public function getDescription(): ?string
  188.     {
  189.         return $this->description;
  190.     }
  191.     public function setDescription(?string $description): self
  192.     {
  193.         $this->description $description;
  194.         return $this;
  195.     }
  196.     public function getOrderPaid(): ?bool
  197.     {
  198.         return $this->orderPaid;
  199.     }
  200.     public function setOrderPaid(bool $orderPaid): self
  201.     {
  202.         $this->orderPaid $orderPaid;
  203.         return $this;
  204.     }
  205.     public function getSpentOrderRefund(): ?Order
  206.     {
  207.         return $this->spentOrderRefund;
  208.     }
  209.     public function setSpentOrderRefund(?Order $spentOrderRefund): self
  210.     {
  211.         $this->spentOrderRefund $spentOrderRefund;
  212.         return $this;
  213.     }
  214. }