src/Entity/Company.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Model\Currency;
  4. use DateTime;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  10. /**
  11.  * @ORM\Entity(
  12.  *     repositoryClass="App\Repository\CompanyRepository"
  13.  * )
  14.  * @ORM\Table(
  15.  *     name="company",
  16.  *     options={"collate"="utf8_swedish_ci"}
  17.  * )
  18.  * @ORM\HasLifecycleCallbacks
  19.  */
  20. class Company implements EntityInterfaceAddressInterface
  21. {
  22.     use AddressTrait;
  23.     use TermsOfDeliveryTrait;
  24.     const CONTRACT_GENERAL 'general';
  25.     const CONTRACT_OWN     'own';
  26.     /**
  27.      * @ORM\Id
  28.      * @ORM\Column(
  29.      *     type="integer"
  30.      * )
  31.      * @ORM\GeneratedValue(
  32.      *     strategy="AUTO"
  33.      * )
  34.      *
  35.      * @var int|null
  36.      */
  37.     protected ?int $id null;
  38.     /**
  39.      * @ORM\ManyToOne(
  40.      *     targetEntity="BankAccount",
  41.      *     inversedBy="companies"
  42.      * )
  43.      * @ORM\JoinColumn(
  44.      *     name="bank_account_id",
  45.      *     referencedColumnName="id"
  46.      * )
  47.      *
  48.      * @Assert\NotNull
  49.      *
  50.      * @var BankAccount|null
  51.      */
  52.     protected ?BankAccount $bankAccount null;
  53.     /**
  54.      * @ORM\Column(
  55.      *     type="boolean"
  56.      * )
  57.      *
  58.      * @var bool
  59.      */
  60.     protected bool $banned false;
  61.     /**
  62.      * @ORM\OneToMany(
  63.      *     targetEntity="CompanyConsigneeAddress",
  64.      *     mappedBy="company",
  65.      *     cascade={"persist","remove"}
  66.      * )
  67.      * @orm\OrderBy({"name" = "ASC"})
  68.      *
  69.      * @var Collection<CompanyConsigneeAddress>
  70.      */
  71.     protected Collection $consigneeAddresses;
  72.     /**
  73.      * @ORM\Column(
  74.      *     type="string",
  75.      *     length=10,
  76.      *     nullable=true
  77.      * )
  78.      * @Assert\Choice(
  79.      *     callback="getContractChoices"
  80.      * )
  81.      *
  82.      * @var string|null
  83.      */
  84.     protected ?string $contract null;
  85.     /**
  86.      * @ORM\Column(
  87.      *     type="string",
  88.      *     length=200,
  89.      *     nullable=true
  90.      * )
  91.      *
  92.      * @var string|null
  93.      */
  94.     protected ?string $contractNumber null;
  95.     /**
  96.      * @ORM\Column(
  97.      *     type="datetime",
  98.      *     nullable=true
  99.      * )
  100.      *
  101.      * @var DateTime|null
  102.      */
  103.     protected ?DateTime $contractValidTo null;
  104.     /**
  105.      * @ORM\Column(
  106.      *     type="string",
  107.      *     length=3
  108.      * )
  109.      * @Assert\NotNull
  110.      * @Assert\Choice(
  111.      *     callback={"\App\Model\Currency", "getCodeChoices"}
  112.      * )
  113.      *
  114.      * @var string
  115.      */
  116.     protected string $currency Currency::DEFAULT_CURRENCY;
  117.     /**
  118.      * @ORM\OneToMany(
  119.      *     targetEntity="CompanyDeliveryAddress",
  120.      *     mappedBy="company",
  121.      *     cascade={"persist","remove"}
  122.      * )
  123.      * @orm\OrderBy({"name" = "ASC"})
  124.      *
  125.      * @var Collection<CompanyDeliveryAddress>
  126.      */
  127.     protected Collection $deliveryAddresses;
  128.     /**
  129.      * @ORM\Column(
  130.      *     type="string",
  131.      *     length=20,
  132.      *     nullable=true
  133.      * )
  134.      *
  135.      * @var string|null
  136.      */
  137.     protected ?string $electronicInvoicingIdentifier null;
  138.     /**
  139.      * @ORM\Column(
  140.      *     type="string",
  141.      *     length=4,
  142.      *     nullable=true
  143.      * )
  144.      *
  145.      * @var string|null
  146.      */
  147.     protected ?string $electronicInvoicingIdentifierScheme null;
  148.     /**
  149.      * @ORM\Column(
  150.      *     type="string",
  151.      *     length=20,
  152.      *     nullable=true
  153.      * )
  154.      *
  155.      * @var string|null
  156.      */
  157.     protected ?string $electronicInvoicingIntermediator null;
  158.     /**
  159.      * @ORM\OneToMany(
  160.      *     targetEntity="CompanyEndCustomer",
  161.      *     mappedBy="company",
  162.      *     cascade={"persist","remove"}
  163.      * )
  164.      * @orm\OrderBy({"name" = "ASC"})
  165.      *
  166.      * @var Collection<CompanyEndCustomer>
  167.      */
  168.     protected Collection $endCustomers;
  169.     /**
  170.      * @ORM\Column(
  171.      *     type="boolean"
  172.      * )
  173.      *
  174.      * @var bool
  175.      */
  176.     protected bool $indirect false;
  177.     /**
  178.      * @ORM\OneToMany(
  179.      *     targetEntity="CompanyInvoiceAddress",
  180.      *     mappedBy="company",
  181.      *     cascade={"persist","remove"}
  182.      * )
  183.      * @orm\OrderBy({"name" = "ASC"})
  184.      *
  185.      * @var Collection<CompanyDeliveryAddress>
  186.      */
  187.     protected Collection $invoiceAddresses;
  188.     /**
  189.      * @ORM\Column(
  190.      *     type="string",
  191.      *     length=500
  192.      * )
  193.      *
  194.      * @var string
  195.      */
  196.     protected string $name;
  197.     /**
  198.      * @ORM\Column(
  199.      *     type="string",
  200.      *     length=5000,
  201.      *     nullable=true
  202.      * )
  203.      *
  204.      * @var string|null
  205.      */
  206.     protected ?string $notes null;
  207.     /**
  208.      * @ORM\ManyToOne(
  209.      *     targetEntity="CompanySalesPriceGroup",
  210.      *     inversedBy="companies"
  211.      * )
  212.      * @ORM\JoinColumn(
  213.      *     name="price_group_id",
  214.      *     referencedColumnName="id",
  215.      *     onDelete="cascade"
  216.      * )
  217.      *
  218.      * @var CompanySalesPriceGroup|null
  219.      */
  220.     protected ?CompanySalesPriceGroup $priceGroup null;
  221.     /**
  222.      * @ORM\OneToOne(
  223.      *     targetEntity="PotentialCustomerCompany",
  224.      *     mappedBy="company",
  225.      *     cascade={"persist"}
  226.      * )
  227.      *
  228.      * @var PotentialCustomerCompany|null
  229.      */
  230.     protected ?PotentialCustomerCompany $potentialCustomer null;
  231.     /**
  232.      * @ORM\OneToMany(
  233.      *     targetEntity="SalesCase",
  234.      *     mappedBy="company",
  235.      *     cascade={"persist"}
  236.      * )
  237.      *
  238.      * @var Collection<SalesCase>
  239.      */
  240.     protected Collection $salesCases;
  241.     /**
  242.      * @ORM\ManyToMany(
  243.      *     targetEntity="TermsOfPaymentRow",
  244.      *     cascade={"persist","remove"},
  245.      *     orphanRemoval=true
  246.      * )
  247.      * @ORM\JoinTable(
  248.      *     name="company_terms_of_payment_row",
  249.      *     joinColumns={
  250.      *         @ORM\JoinColumn(
  251.      *             name="company_id",
  252.      *             referencedColumnName="id",
  253.      *             onDelete="cascade"
  254.      *         )
  255.      *     },
  256.      *     inverseJoinColumns={
  257.      *         @ORM\JoinColumn(
  258.      *             name="terms_of_payment_row_id",
  259.      *             referencedColumnName="id",
  260.      *             unique=true,
  261.      *             onDelete="cascade"
  262.      *         )
  263.      *     }
  264.      * )
  265.      *
  266.      * @Assert\Valid
  267.      *
  268.      * @var Collection<TermsOfPaymentRow>
  269.      */
  270.     protected Collection $termsOfPaymentRows;
  271.     /**
  272.      * @ORM\Column(
  273.      *     type="text",
  274.      *     nullable=true
  275.      * )
  276.      *
  277.      * @var string|null
  278.      */
  279.     protected ?string $warranty null;
  280.     /**
  281.      * @param string $name
  282.      * @param BankAccount|null $bankAccount
  283.      */
  284.     public function __construct(string $name, ?BankAccount $bankAccount null)
  285.     {
  286.         $this->name $name;
  287.         $this->bankAccount $bankAccount;
  288.         $this->consigneeAddresses = new ArrayCollection();
  289.         $this->deliveryAddresses = new ArrayCollection();
  290.         $this->endCustomers = new ArrayCollection();
  291.         $this->invoiceAddresses = new ArrayCollection();
  292.         $this->salesCases = new ArrayCollection();
  293.         $this->termsOfPaymentRows = new ArrayCollection();
  294.     }
  295.     /**
  296.      * @return string
  297.      */
  298.     public function __toString(): string
  299.     {
  300.         return $this->name;
  301.     }
  302.     /**
  303.      * @param TermsOfPaymentRow $row
  304.      */
  305.     public function addTermsOfPaymentRow(TermsOfPaymentRow $row): void
  306.     {
  307.         $this->termsOfPaymentRows->add($row);
  308.     }
  309.     /**
  310.      * @return int
  311.      */
  312.     public function getAdditionalAddressCount(): int
  313.     {
  314.         return count($this->consigneeAddresses) + count($this->deliveryAddresses) + count($this->invoiceAddresses);
  315.     }
  316.     /**
  317.      * @return BankAccount|null
  318.      */
  319.     public function getBankAccount(): ?BankAccount
  320.     {
  321.         return $this->bankAccount;
  322.     }
  323.     /**
  324.      * @return Collection<CompanyConsigneeAddress>
  325.      */
  326.     public function getConsigneeAddresses(): Collection
  327.     {
  328.         return $this->consigneeAddresses;
  329.     }
  330.     /**
  331.      * @return null|string
  332.      */
  333.     public function getContract(): ?string
  334.     {
  335.         return $this->contract;
  336.     }
  337.     /**
  338.      * @return string[]
  339.      */
  340.     public static function getContractChoices(): array
  341.     {
  342.         return [
  343.             self::CONTRACT_GENERAL,
  344.             self::CONTRACT_OWN,
  345.         ];
  346.     }
  347.     /**
  348.      * @return null|string
  349.      */
  350.     public function getContractNumber(): ?string
  351.     {
  352.         return $this->contractNumber;
  353.     }
  354.     /**
  355.      * @return DateTime|null
  356.      */
  357.     public function getContractValidTo(): ?DateTime
  358.     {
  359.         return $this->contractValidTo;
  360.     }
  361.     /**
  362.      * @return string
  363.      */
  364.     public function getCurrency(): string
  365.     {
  366.         return $this->currency;
  367.     }
  368.     /**
  369.      * @return Collection<CompanyDeliveryAddress>
  370.      */
  371.     public function getDeliveryAddresses(): Collection
  372.     {
  373.         return $this->deliveryAddresses;
  374.     }
  375.     /**
  376.      * @return string|null
  377.      */
  378.     public function getElectronicInvoicingIdentifier(): ?string
  379.     {
  380.         return $this->electronicInvoicingIdentifier;
  381.     }
  382.     /**
  383.      * @return string|null
  384.      */
  385.     public function getElectronicInvoicingIdentifierScheme(): ?string
  386.     {
  387.         return $this->electronicInvoicingIdentifierScheme;
  388.     }
  389.     /**
  390.      * @return string|null
  391.      */
  392.     public function getElectronicInvoicingIntermediator(): ?string
  393.     {
  394.         return $this->electronicInvoicingIntermediator;
  395.     }
  396.     /**
  397.      * @return Collection<CompanyEndCustomer>
  398.      */
  399.     public function getEndCustomers(): Collection
  400.     {
  401.         return $this->endCustomers;
  402.     }
  403.     /**
  404.      * @return int|null
  405.      */
  406.     public function getId(): ?int
  407.     {
  408.         return $this->id;
  409.     }
  410.     /**
  411.      * @return Collection<CompanyInvoiceAddress>
  412.      */
  413.     public function getInvoiceAddresses(): Collection
  414.     {
  415.         return $this->invoiceAddresses;
  416.     }
  417.     /**
  418.      * @return Collection<CompanyInvoiceAddress>
  419.      */
  420.     public function getInvoicingAddresses(): Collection
  421.     {
  422.         return $this->invoiceAddresses;
  423.     }
  424.     /**
  425.      * @return null|string
  426.      */
  427.     public function getNotes(): ?string
  428.     {
  429.         return $this->notes;
  430.     }
  431.     /**
  432.      * @return PotentialCustomerCompany|null
  433.      */
  434.     public function getPotentialCustomer(): ?PotentialCustomerCompany
  435.     {
  436.         return $this->potentialCustomer;
  437.     }
  438.     /**
  439.      * @return CompanySalesPriceGroup|null
  440.      */
  441.     public function getPriceGroup(): ?CompanySalesPriceGroup
  442.     {
  443.         return $this->priceGroup;
  444.     }
  445.     /**
  446.      * @return Collection<SalesCase>
  447.      */
  448.     public function getSalesCases(): Collection
  449.     {
  450.         return $this->salesCases;
  451.     }
  452.     /**
  453.      * @return Collection<TermsOfPaymentRow>
  454.      */
  455.     public function getTermsOfPaymentRows(): Collection
  456.     {
  457.         return $this->termsOfPaymentRows;
  458.     }
  459.     /**
  460.      * @return string|null
  461.      */
  462.     public function getWarranty(): ?string
  463.     {
  464.         return $this->warranty;
  465.     }
  466.     /**
  467.      * @return bool
  468.      */
  469.     public function isBanned(): bool
  470.     {
  471.         return $this->banned;
  472.     }
  473.     /**
  474.      * @return bool
  475.      */
  476.     public function isDefaultCurrency(): bool
  477.     {
  478.         return $this->currency == Currency::DEFAULT_CURRENCY;
  479.     }
  480.     /**
  481.      * @return bool
  482.      */
  483.     public function isIndirect(): bool
  484.     {
  485.         return $this->indirect;
  486.     }
  487.     /**
  488.      * @param TermsOfPaymentRow $row
  489.      */
  490.     public function removeTermsOfPaymentRow(TermsOfPaymentRow $row)
  491.     {
  492.         if ($this->termsOfPaymentRows !== null) {
  493.             $this->termsOfPaymentRows->removeElement($row);
  494.         }
  495.     }
  496.     /**
  497.      * @param BankAccount $bankAccount
  498.      */
  499.     public function setBankAccount(BankAccount $bankAccount)
  500.     {
  501.         $this->bankAccount $bankAccount;
  502.     }
  503.     /**
  504.      * @param bool $banned
  505.      */
  506.     public function setBanned(bool $banned)
  507.     {
  508.         $this->banned $banned;
  509.     }
  510.     /**
  511.      * @param string|null $contract
  512.      */
  513.     public function setContract(?string $contract)
  514.     {
  515.         $this->contract $contract;
  516.     }
  517.     /**
  518.      * @param string|null $contractNumber
  519.      */
  520.     public function setContractNumber(?string $contractNumber)
  521.     {
  522.         $this->contractNumber $contractNumber;
  523.     }
  524.     /**
  525.      * @param DateTime|null $contractValidTo
  526.      */
  527.     public function setContractValidTo(?DateTime $contractValidTo)
  528.     {
  529.         $this->contractValidTo $contractValidTo;
  530.     }
  531.     /**
  532.      * @param string $currency
  533.      */
  534.     public function setCurrency(string $currency)
  535.     {
  536.         $this->currency $currency;
  537.     }
  538.     /**
  539.      * @param string|null $electronicInvoicingIdentifier
  540.      */
  541.     public function setElectronicInvoicingIdentifier(?string $electronicInvoicingIdentifier): void
  542.     {
  543.         $this->electronicInvoicingIdentifier $electronicInvoicingIdentifier;
  544.     }
  545.     /**
  546.      * @param string|null $electronicInvoicingIdentifierScheme
  547.      */
  548.     public function setElectronicInvoicingIdentifierScheme(?string $electronicInvoicingIdentifierScheme): void
  549.     {
  550.         $this->electronicInvoicingIdentifierScheme $electronicInvoicingIdentifierScheme;
  551.     }
  552.     /**
  553.      * @param string|null $electronicInvoicingIntermediator
  554.      */
  555.     public function setElectronicInvoicingIntermediator(?string $electronicInvoicingIntermediator): void
  556.     {
  557.         $this->electronicInvoicingIntermediator $electronicInvoicingIntermediator;
  558.     }
  559.     /**
  560.      * @param bool $indirect
  561.      */
  562.     public function setIndirect(bool $indirect)
  563.     {
  564.         $this->indirect $indirect;
  565.     }
  566.     /**
  567.      * @param string|null $notes
  568.      */
  569.     public function setNotes(?string $notes)
  570.     {
  571.         $this->notes $notes;
  572.     }
  573.     /**
  574.      * @param PotentialCustomerCompany|null $potentialCustomer
  575.      */
  576.     public function setPotentialCustomer(?PotentialCustomerCompany $potentialCustomer)
  577.     {
  578.         $potentialCustomer->setCompany($this);
  579.         $this->potentialCustomer $potentialCustomer;
  580.     }
  581.     /**
  582.      * @param CompanySalesPriceGroup|null $priceGroup
  583.      */
  584.     public function setPriceGroup(?CompanySalesPriceGroup $priceGroup)
  585.     {
  586.         $this->priceGroup $priceGroup;
  587.     }
  588.     /**
  589.      * @param string|null $warranty
  590.      */
  591.     public function setWarranty(?string $warranty)
  592.     {
  593.         $this->warranty $warranty;
  594.     }
  595.     /**
  596.      * @Assert\Callback
  597.      *
  598.      * @param ExecutionContextInterface $context
  599.      */
  600.     public function validatePriceGroupCurrency(ExecutionContextInterface $context)
  601.     {
  602.         // Validate electronic invoicing identifier scheme
  603.         if (null !== $this->electronicInvoicingIdentifier && null === $this->electronicInvoicingIdentifierScheme) {
  604.             $context
  605.                 ->buildViolation('validation.electronicInvoicingIdentifierScheme.empty')
  606.                 ->setTranslationDomain('Company')
  607.                 ->atPath('electronicInvoicingIdentifierScheme')
  608.                 ->addViolation()
  609.             ;
  610.         }
  611.         // Validate electronic invoicing identifier scheme
  612.         if (null !== $this->electronicInvoicingIdentifier && null === $this->electronicInvoicingIntermediator) {
  613.             $context
  614.                 ->buildViolation('validation.electronicInvoicingIntermediator.empty')
  615.                 ->setTranslationDomain('Company')
  616.                 ->atPath('electronicInvoicingIntermediator')
  617.                 ->addViolation()
  618.             ;
  619.         }
  620.         // Validate price group currency
  621.         if ($this->priceGroup !== null && $this->priceGroup->getCurrency() != $this->currency) {
  622.             $context
  623.                 ->buildViolation(
  624.                     'validation.priceGroup.currency_mismatch',
  625.                     [
  626.                         '%price_group_currency%' => $this->priceGroup->getCurrency(),
  627.                         '%currency%'             => $this->currency,
  628.                     ]
  629.                 )
  630.                 ->setTranslationDomain('Company')
  631.                 ->atPath('priceGroup')
  632.                 ->addViolation()
  633.             ;
  634.         }
  635.     }
  636. }