src/Entity/PaymentPeriod.php line 17

  1. <?php
  2. namespace App\Entity;
  3. use App\Enum\CustomerClass;
  4. use App\Enum\EmployeeStatus;
  5. use App\Enum\RegionEnum;
  6. use App\Repository\PaymentPeriodRepository;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\Common\Collections\Criteria;
  10. use Doctrine\DBAL\Types\Types;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Gedmo\Timestampable\Traits\TimestampableEntity;
  13. #[ORM\Entity(repositoryClassPaymentPeriodRepository::class)]
  14. class PaymentPeriod
  15. {
  16.     use TimestampableEntity;
  17.     #[ORM\Id]
  18.     #[ORM\GeneratedValue]
  19.     #[ORM\Column]
  20.     private ?int $id null;
  21.     #[ORM\Column(typeTypes::DATETIME_MUTABLE)]
  22.     private ?\DateTimeInterface $start null;
  23.     #[ORM\Column(typeTypes::DATETIME_MUTABLE)]
  24.     private ?\DateTimeInterface $end null;
  25.     #[ORM\OneToMany(mappedBy'paymentPeriod'targetEntityUserPaymentPeriod::class, cascade: ['REMOVE'], fetch"EXTRA_LAZY")]
  26.     private Collection $userPaymentPeriods;
  27.     private $userFilter null;
  28.     private $filteredUserPaymentPeriods null;
  29.     #[ORM\ManyToOne(inversedBy'paymentPeriods')]
  30.     #[ORM\JoinColumn(nullablefalse)]
  31.     private ?Year $year null;
  32.     public function __construct()
  33.     {
  34.         $this->userPaymentPeriods = new ArrayCollection();
  35.     }
  36.     public function getId(): ?int
  37.     {
  38.         return $this->id;
  39.     }
  40.     public function getStart(): ?\DateTimeInterface
  41.     {
  42.         return $this->start;
  43.     }
  44.     public function setStart(\DateTimeInterface $start): self
  45.     {
  46.         $this->start $start;
  47.         return $this;
  48.     }
  49.     public function getEnd(): ?\DateTimeInterface
  50.     {
  51.         return $this->end;
  52.     }
  53.     public function setEnd(\DateTimeInterface $end): self
  54.     {
  55.         $this->end $end;
  56.         return $this;
  57.     }
  58.     public function __toString()
  59.     {
  60.         return $this->getStart()?->format('m-d-Y') . ' to ' $this->getEnd()?->format('m-d-Y');
  61.     }
  62.     public function getUserPaymentPeriodsFiltered(array $filter = [])
  63.     {
  64.         if (count($filter)) {
  65.             // speeds up report generation
  66.             $ids array_map(fn($u) => $u->getId(), $filter);
  67.             if ($ids == $this->userFilter) {
  68.                 return $this->filteredUserPaymentPeriods;
  69.             }
  70.             $this->userFilter $ids;
  71.             $criteria Criteria::create()->where(Criteria::expr()->in("user"$filter));
  72.             $filtered $this->getUserPaymentPeriods()->matching($criteria);
  73.             $this->filteredUserPaymentPeriods $filtered;
  74.             return $filtered;
  75.         }
  76.         else {
  77.             return $this->getUserPaymentPeriods();
  78.         }
  79.     }
  80.     /**
  81.      * @return Collection<int, UserPaymentPeriod>
  82.      */
  83.     public function getUserPaymentPeriods(array $filter = []): Collection
  84.     {
  85.         return $this->userPaymentPeriods;
  86.     }
  87.     public function addUserPaymentPeriod(UserPaymentPeriod $userPaymentPeriod): self
  88.     {
  89.         if (!$this->userPaymentPeriods->contains($userPaymentPeriod)) {
  90.             $this->userPaymentPeriods->add($userPaymentPeriod);
  91.             $userPaymentPeriod->setPaymentPeriod($this);
  92.         }
  93.         return $this;
  94.     }
  95.     public function removeUserPaymentPeriod(UserPaymentPeriod $userPaymentPeriod): self
  96.     {
  97.         if ($this->userPaymentPeriods->removeElement($userPaymentPeriod)) {
  98.             // set the owning side to null (unless already changed)
  99.             if ($userPaymentPeriod->getPaymentPeriod() === $this) {
  100.                 $userPaymentPeriod->setPaymentPeriod(null);
  101.             }
  102.         }
  103.         return $this;
  104.     }
  105.     public function sumRawSales(?RegionEnum $region null, array $userFilter = [], array $customerClasses = [])
  106.     {
  107.         $amt 0;
  108.         // enables passing [null] as empty
  109.         $userFilter array_filter($userFilter);
  110.         foreach($this->getUserPaymentPeriodsFiltered($userFilter) as $upp) {
  111.             if ($region) {
  112.                 if ($upp->getUser()->getRegion() !== $region) {
  113.                     continue;
  114.                 }
  115.             }
  116.             foreach($upp->getOrders()->toArray() as $order) {
  117.                 if (count($customerClasses)) {
  118.                     if (!in_array($order->getCustomerGeneralClass(), $customerClasses)) {
  119.                         continue;
  120.                     }
  121.                     $amt += $order->getProductAmountInPennies();
  122.                 } else {
  123.                     $amt += $order->getProductAmountInPennies();
  124.                 }
  125.             }
  126.         }
  127.         $amt $amt 100.00;
  128.         return $amt;
  129.     }
  130.     public function sumCommissions(?RegionEnum $region null, array $userFilter = [], array $customerClasses = [])
  131.     {
  132.         $amt 0;
  133.         // enables passing [null] as empty
  134.         $userFilter array_filter($userFilter);
  135.         foreach($this->getUserPaymentPeriodsFiltered($userFilter) as $upp) {
  136.             if ($region) {
  137.                 if ($upp->getUser()->getRegion() !== $region) {
  138.                     continue;
  139.                 }
  140.             }
  141.             foreach($upp->getCommissionResults()->toArray() as $commission) {
  142.                 if (count($customerClasses)) {
  143.                     if (!in_array($commission->getCreditResult()->getOriginalOrder()->getCustomerGeneralClass(), $customerClasses)) {
  144.                         continue;
  145.                     }
  146.                     $amt += $commission->getAmountInPennies();
  147.                 } else {
  148.                     $amt += $commission->getAmountInPennies();
  149.                 }
  150.             }
  151.         }
  152.         $amt $amt 100.00;
  153.         return $amt;
  154.     }
  155.     public function sumGuarantees(?RegionEnum $region null, array $userFilter = [])
  156.     {
  157.         $amt 0;
  158.         // enables passing [null] as empty
  159.         $userFilter array_filter($userFilter);
  160.         foreach($this->getUserPaymentPeriodsFiltered($userFilter) as $upp) {
  161.             if ($region) {
  162.                 if ($upp->getUser()->getRegion() !== $region) {
  163.                     continue;
  164.                 }
  165.             }
  166.             foreach($upp->getGuaranteePayments()->toArray() as $guarantee) {
  167.                 $amt += $guarantee->getAmtInPennies();
  168.             }
  169.         }
  170.         $amt $amt 100.00;
  171.         return $amt;
  172.     }
  173.     public function sumTrueUps(?RegionEnum $region null, array $userFilter = [])
  174.     {
  175.         $amt 0;
  176.         // enables passing [null] as empty
  177.         $userFilter array_filter($userFilter);
  178.         foreach($this->getUserPaymentPeriodsFiltered($userFilter) as $upp) {
  179.             if ($region) {
  180.                 if ($upp->getUser()->getRegion() !== $region) {
  181.                     continue;
  182.                 }
  183.             }
  184.             foreach($upp->getTrueUps()->toArray() as $trueUp) {
  185.                 $amt += $trueUp->getAmtInPennies();
  186.             }
  187.         }
  188.         $amt $amt 100.00;
  189.         return $amt;
  190.     }
  191.     public function sumManualCorrections(?RegionEnum $region null, array $userFilter = [])
  192.     {
  193.         $amt 0;
  194.         // enables passing [null] as empty
  195.         $userFilter array_filter($userFilter);
  196.         foreach($this->getUserPaymentPeriodsFiltered($userFilter) as $upp) {
  197.             if ($region) {
  198.                 if ($upp->getUser()->getRegion() !== $region) {
  199.                     continue;
  200.                 }
  201.             }
  202.             foreach($upp->getTotalDues()->toArray() as $td) {
  203.                 $amt += $td->getManualCorrectionInPennies();
  204.             }
  205.         }
  206.         $amt $amt 100.00;
  207.         return $amt;
  208.     }
  209.     public function sumFinalTotals(?RegionEnum $region null, array $userFilter = [])
  210.     {
  211.         $amt 0;
  212.         // enables passing [null] as empty
  213.         $userFilter array_filter($userFilter);
  214.         foreach($this->getUserPaymentPeriodsFiltered($userFilter) as $upp) {
  215.             if ($region) {
  216.                 if ($upp->getUser()->getRegion() !== $region) {
  217.                     continue;
  218.                 }
  219.             }
  220.             foreach($upp->getTotalDues()->toArray() as $td) {
  221.                 $amt += ($td->getAmtInPennies() + $td->getManualCorrectionInPennies());
  222.             }
  223.         }
  224.         $amt $amt 100.00;
  225.         return $amt;
  226.     }
  227.     public function getYear(): ?Year
  228.     {
  229.         return $this->year;
  230.     }
  231.     public function setYear(?Year $year): self
  232.     {
  233.         $this->year $year;
  234.         return $this;
  235.     }
  236. }