<?php
namespace App\Entity;
use App\Model\Currency;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* @ORM\Entity(
* repositoryClass="App\Repository\CompanyRepository"
* )
* @ORM\Table(
* name="company",
* options={"collate"="utf8_swedish_ci"}
* )
* @ORM\HasLifecycleCallbacks
*/
class Company implements EntityInterface, AddressInterface
{
use AddressTrait;
use TermsOfDeliveryTrait;
const CONTRACT_GENERAL = 'general';
const CONTRACT_OWN = 'own';
/**
* @ORM\Id
* @ORM\Column(
* type="integer"
* )
* @ORM\GeneratedValue(
* strategy="AUTO"
* )
*
* @var int|null
*/
protected ?int $id = null;
/**
* @ORM\ManyToOne(
* targetEntity="BankAccount",
* inversedBy="companies"
* )
* @ORM\JoinColumn(
* name="bank_account_id",
* referencedColumnName="id"
* )
*
* @Assert\NotNull
*
* @var BankAccount|null
*/
protected ?BankAccount $bankAccount = null;
/**
* @ORM\Column(
* type="boolean"
* )
*
* @var bool
*/
protected bool $banned = false;
/**
* @ORM\OneToMany(
* targetEntity="CompanyConsigneeAddress",
* mappedBy="company",
* cascade={"persist","remove"}
* )
* @orm\OrderBy({"name" = "ASC"})
*
* @var Collection<CompanyConsigneeAddress>
*/
protected Collection $consigneeAddresses;
/**
* @ORM\Column(
* type="string",
* length=10,
* nullable=true
* )
* @Assert\Choice(
* callback="getContractChoices"
* )
*
* @var string|null
*/
protected ?string $contract = null;
/**
* @ORM\Column(
* type="string",
* length=200,
* nullable=true
* )
*
* @var string|null
*/
protected ?string $contractNumber = null;
/**
* @ORM\Column(
* type="datetime",
* nullable=true
* )
*
* @var DateTime|null
*/
protected ?DateTime $contractValidTo = null;
/**
* @ORM\Column(
* type="string",
* length=3
* )
* @Assert\NotNull
* @Assert\Choice(
* callback={"\App\Model\Currency", "getCodeChoices"}
* )
*
* @var string
*/
protected string $currency = Currency::DEFAULT_CURRENCY;
/**
* @ORM\OneToMany(
* targetEntity="CompanyDeliveryAddress",
* mappedBy="company",
* cascade={"persist","remove"}
* )
* @orm\OrderBy({"name" = "ASC"})
*
* @var Collection<CompanyDeliveryAddress>
*/
protected Collection $deliveryAddresses;
/**
* @ORM\Column(
* type="string",
* length=20,
* nullable=true
* )
*
* @var string|null
*/
protected ?string $electronicInvoicingIdentifier = null;
/**
* @ORM\Column(
* type="string",
* length=4,
* nullable=true
* )
*
* @var string|null
*/
protected ?string $electronicInvoicingIdentifierScheme = null;
/**
* @ORM\Column(
* type="string",
* length=20,
* nullable=true
* )
*
* @var string|null
*/
protected ?string $electronicInvoicingIntermediator = null;
/**
* @ORM\OneToMany(
* targetEntity="CompanyEndCustomer",
* mappedBy="company",
* cascade={"persist","remove"}
* )
* @orm\OrderBy({"name" = "ASC"})
*
* @var Collection<CompanyEndCustomer>
*/
protected Collection $endCustomers;
/**
* @ORM\Column(
* type="boolean"
* )
*
* @var bool
*/
protected bool $indirect = false;
/**
* @ORM\OneToMany(
* targetEntity="CompanyInvoiceAddress",
* mappedBy="company",
* cascade={"persist","remove"}
* )
* @orm\OrderBy({"name" = "ASC"})
*
* @var Collection<CompanyDeliveryAddress>
*/
protected Collection $invoiceAddresses;
/**
* @ORM\Column(
* type="string",
* length=500
* )
*
* @var string
*/
protected string $name;
/**
* @ORM\Column(
* type="string",
* length=5000,
* nullable=true
* )
*
* @var string|null
*/
protected ?string $notes = null;
/**
* @ORM\ManyToOne(
* targetEntity="CompanySalesPriceGroup",
* inversedBy="companies"
* )
* @ORM\JoinColumn(
* name="price_group_id",
* referencedColumnName="id",
* onDelete="cascade"
* )
*
* @var CompanySalesPriceGroup|null
*/
protected ?CompanySalesPriceGroup $priceGroup = null;
/**
* @ORM\OneToOne(
* targetEntity="PotentialCustomerCompany",
* mappedBy="company",
* cascade={"persist"}
* )
*
* @var PotentialCustomerCompany|null
*/
protected ?PotentialCustomerCompany $potentialCustomer = null;
/**
* @ORM\OneToMany(
* targetEntity="SalesCase",
* mappedBy="company",
* cascade={"persist"}
* )
*
* @var Collection<SalesCase>
*/
protected Collection $salesCases;
/**
* @ORM\ManyToMany(
* targetEntity="TermsOfPaymentRow",
* cascade={"persist","remove"},
* orphanRemoval=true
* )
* @ORM\JoinTable(
* name="company_terms_of_payment_row",
* joinColumns={
* @ORM\JoinColumn(
* name="company_id",
* referencedColumnName="id",
* onDelete="cascade"
* )
* },
* inverseJoinColumns={
* @ORM\JoinColumn(
* name="terms_of_payment_row_id",
* referencedColumnName="id",
* unique=true,
* onDelete="cascade"
* )
* }
* )
*
* @Assert\Valid
*
* @var Collection<TermsOfPaymentRow>
*/
protected Collection $termsOfPaymentRows;
/**
* @ORM\Column(
* type="text",
* nullable=true
* )
*
* @var string|null
*/
protected ?string $warranty = null;
/**
* @param string $name
* @param BankAccount|null $bankAccount
*/
public function __construct(string $name, ?BankAccount $bankAccount = null)
{
$this->name = $name;
$this->bankAccount = $bankAccount;
$this->consigneeAddresses = new ArrayCollection();
$this->deliveryAddresses = new ArrayCollection();
$this->endCustomers = new ArrayCollection();
$this->invoiceAddresses = new ArrayCollection();
$this->salesCases = new ArrayCollection();
$this->termsOfPaymentRows = new ArrayCollection();
}
/**
* @return string
*/
public function __toString(): string
{
return $this->name;
}
/**
* @param TermsOfPaymentRow $row
*/
public function addTermsOfPaymentRow(TermsOfPaymentRow $row): void
{
$this->termsOfPaymentRows->add($row);
}
/**
* @return int
*/
public function getAdditionalAddressCount(): int
{
return count($this->consigneeAddresses) + count($this->deliveryAddresses) + count($this->invoiceAddresses);
}
/**
* @return BankAccount|null
*/
public function getBankAccount(): ?BankAccount
{
return $this->bankAccount;
}
/**
* @return Collection<CompanyConsigneeAddress>
*/
public function getConsigneeAddresses(): Collection
{
return $this->consigneeAddresses;
}
/**
* @return null|string
*/
public function getContract(): ?string
{
return $this->contract;
}
/**
* @return string[]
*/
public static function getContractChoices(): array
{
return [
self::CONTRACT_GENERAL,
self::CONTRACT_OWN,
];
}
/**
* @return null|string
*/
public function getContractNumber(): ?string
{
return $this->contractNumber;
}
/**
* @return DateTime|null
*/
public function getContractValidTo(): ?DateTime
{
return $this->contractValidTo;
}
/**
* @return string
*/
public function getCurrency(): string
{
return $this->currency;
}
/**
* @return Collection<CompanyDeliveryAddress>
*/
public function getDeliveryAddresses(): Collection
{
return $this->deliveryAddresses;
}
/**
* @return string|null
*/
public function getElectronicInvoicingIdentifier(): ?string
{
return $this->electronicInvoicingIdentifier;
}
/**
* @return string|null
*/
public function getElectronicInvoicingIdentifierScheme(): ?string
{
return $this->electronicInvoicingIdentifierScheme;
}
/**
* @return string|null
*/
public function getElectronicInvoicingIntermediator(): ?string
{
return $this->electronicInvoicingIntermediator;
}
/**
* @return Collection<CompanyEndCustomer>
*/
public function getEndCustomers(): Collection
{
return $this->endCustomers;
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @return Collection<CompanyInvoiceAddress>
*/
public function getInvoiceAddresses(): Collection
{
return $this->invoiceAddresses;
}
/**
* @return Collection<CompanyInvoiceAddress>
*/
public function getInvoicingAddresses(): Collection
{
return $this->invoiceAddresses;
}
/**
* @return null|string
*/
public function getNotes(): ?string
{
return $this->notes;
}
/**
* @return PotentialCustomerCompany|null
*/
public function getPotentialCustomer(): ?PotentialCustomerCompany
{
return $this->potentialCustomer;
}
/**
* @return CompanySalesPriceGroup|null
*/
public function getPriceGroup(): ?CompanySalesPriceGroup
{
return $this->priceGroup;
}
/**
* @return Collection<SalesCase>
*/
public function getSalesCases(): Collection
{
return $this->salesCases;
}
/**
* @return Collection<TermsOfPaymentRow>
*/
public function getTermsOfPaymentRows(): Collection
{
return $this->termsOfPaymentRows;
}
/**
* @return string|null
*/
public function getWarranty(): ?string
{
return $this->warranty;
}
/**
* @return bool
*/
public function isBanned(): bool
{
return $this->banned;
}
/**
* @return bool
*/
public function isDefaultCurrency(): bool
{
return $this->currency == Currency::DEFAULT_CURRENCY;
}
/**
* @return bool
*/
public function isIndirect(): bool
{
return $this->indirect;
}
/**
* @param TermsOfPaymentRow $row
*/
public function removeTermsOfPaymentRow(TermsOfPaymentRow $row)
{
if ($this->termsOfPaymentRows !== null) {
$this->termsOfPaymentRows->removeElement($row);
}
}
/**
* @param BankAccount $bankAccount
*/
public function setBankAccount(BankAccount $bankAccount)
{
$this->bankAccount = $bankAccount;
}
/**
* @param bool $banned
*/
public function setBanned(bool $banned)
{
$this->banned = $banned;
}
/**
* @param string|null $contract
*/
public function setContract(?string $contract)
{
$this->contract = $contract;
}
/**
* @param string|null $contractNumber
*/
public function setContractNumber(?string $contractNumber)
{
$this->contractNumber = $contractNumber;
}
/**
* @param DateTime|null $contractValidTo
*/
public function setContractValidTo(?DateTime $contractValidTo)
{
$this->contractValidTo = $contractValidTo;
}
/**
* @param string $currency
*/
public function setCurrency(string $currency)
{
$this->currency = $currency;
}
/**
* @param string|null $electronicInvoicingIdentifier
*/
public function setElectronicInvoicingIdentifier(?string $electronicInvoicingIdentifier): void
{
$this->electronicInvoicingIdentifier = $electronicInvoicingIdentifier;
}
/**
* @param string|null $electronicInvoicingIdentifierScheme
*/
public function setElectronicInvoicingIdentifierScheme(?string $electronicInvoicingIdentifierScheme): void
{
$this->electronicInvoicingIdentifierScheme = $electronicInvoicingIdentifierScheme;
}
/**
* @param string|null $electronicInvoicingIntermediator
*/
public function setElectronicInvoicingIntermediator(?string $electronicInvoicingIntermediator): void
{
$this->electronicInvoicingIntermediator = $electronicInvoicingIntermediator;
}
/**
* @param bool $indirect
*/
public function setIndirect(bool $indirect)
{
$this->indirect = $indirect;
}
/**
* @param string|null $notes
*/
public function setNotes(?string $notes)
{
$this->notes = $notes;
}
/**
* @param PotentialCustomerCompany|null $potentialCustomer
*/
public function setPotentialCustomer(?PotentialCustomerCompany $potentialCustomer)
{
$potentialCustomer->setCompany($this);
$this->potentialCustomer = $potentialCustomer;
}
/**
* @param CompanySalesPriceGroup|null $priceGroup
*/
public function setPriceGroup(?CompanySalesPriceGroup $priceGroup)
{
$this->priceGroup = $priceGroup;
}
/**
* @param string|null $warranty
*/
public function setWarranty(?string $warranty)
{
$this->warranty = $warranty;
}
/**
* @Assert\Callback
*
* @param ExecutionContextInterface $context
*/
public function validatePriceGroupCurrency(ExecutionContextInterface $context)
{
// Validate electronic invoicing identifier scheme
if (null !== $this->electronicInvoicingIdentifier && null === $this->electronicInvoicingIdentifierScheme) {
$context
->buildViolation('validation.electronicInvoicingIdentifierScheme.empty')
->setTranslationDomain('Company')
->atPath('electronicInvoicingIdentifierScheme')
->addViolation()
;
}
// Validate electronic invoicing identifier scheme
if (null !== $this->electronicInvoicingIdentifier && null === $this->electronicInvoicingIntermediator) {
$context
->buildViolation('validation.electronicInvoicingIntermediator.empty')
->setTranslationDomain('Company')
->atPath('electronicInvoicingIntermediator')
->addViolation()
;
}
// Validate price group currency
if ($this->priceGroup !== null && $this->priceGroup->getCurrency() != $this->currency) {
$context
->buildViolation(
'validation.priceGroup.currency_mismatch',
[
'%price_group_currency%' => $this->priceGroup->getCurrency(),
'%currency%' => $this->currency,
]
)
->setTranslationDomain('Company')
->atPath('priceGroup')
->addViolation()
;
}
}
}