<?php
namespace App\AtechNet\CoreBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\OneToOne;
use Doctrine\ORM\Mapping\Table as Table;
/**
* DocumentItem
*
* @Table(name="document_items")
* @Entity
*/
class DocumentItem extends AbstractSignedEntity
{
/**
* @var Document
*
* @ManyToOne(targetEntity="Document", inversedBy="items")
* @JoinColumn(name="document_id", referencedColumnName="id", nullable=false)
*/
private $document;
/**
* @var Product
*
* @ManyToOne(targetEntity="Product")
* @JoinColumn(name="product_id", referencedColumnName="id", nullable=true)
*/
private $product;
/**
* @var string
*
* @Column(name="title", type="text")
*/
private $title;
/**
* @var string
*
* @Column(name="description", type="text", nullable=true)
*/
private $description;
/**
* @var int
*
* @Column(name="quantity", type="integer")
*/
private $quantity;
/**
* @var string
*
* @Column(name="price_per_item", type="decimal", precision=12, scale=3)
*/
private $pricePerItem = 0;
/**
* @var string
*
* @Column(name="vat_price_per_item", type="decimal", precision=12, scale=3)
*/
private $vatPricePerItem = 0;
/**
* @var string
*
* @Column(name="total_price", type="decimal", precision=12, scale=3)
*/
private $totalPrice = 0;
/**
* @var string
*
* @Column(name="total_vat_price", type="decimal", precision=12, scale=3)
*/
private $totalVatPrice = 0;
/**
* @var DocumentItem|null
*
* @ManyToOne(targetEntity="DocumentItem", inversedBy="children", cascade={"persist"})
* @JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
*/
private $parent;
/**
* @var ArrayCollection
*
* @OneToMany(targetEntity="DocumentItem", mappedBy="parent", cascade={"persist", "remove"})
**/
private $children;
/**
* @var ProductContainer|null
*
* @ManyToOne(targetEntity="ProductContainer")
* @JoinColumn(name="product_container_id", referencedColumnName="id", nullable=true)
*/
private $productContainer;
/**
* @var AttributeOption
*
* @ManyToOne(targetEntity="AttributeOption")
* @JoinColumn(name="attribute_option_id", referencedColumnName="id")
*/
private $attributeOption;
/**
* @var ?StockMovementItem
*
* @OneToOne(targetEntity="StockMovementItem", mappedBy="documentItem", cascade={"persist", "remove"})
*/
private $stockMovementItem;
public function __construct()
{
parent::__construct();
$this->children = new ArrayCollection();
}
public function isFreetext()
{
return $this->product == null && $this->attributeOption == null;
}
/**
* @return Company
*/
public function getCompany()
{
return $this->getDocument()->getCompany();
}
/**
* @return string
*/
public function getCurrency()
{
return $this->getCompany()->getCurrency();
}
/**
* @return Document
*/
public function getDocument()
{
return $this->document;
}
/**
* @param Document $document
*/
public function setDocument($document)
{
$this->document = $document;
}
/**
* @return Product
*/
public function getProduct()
{
return $this->product;
}
/**
* @param Product $product
*/
public function setProduct($product)
{
$this->product = $product;
}
/**
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* @param string $description
*/
public function setDescription($description)
{
$this->description = $description;
}
/**
* @return int
*/
public function getQuantity()
{
return $this->quantity;
}
/**
* @param int $quantity
*/
public function setQuantity($quantity)
{
$this->quantity = $quantity;
}
/**
* @return string
*/
public function getPricePerItem()
{
return $this->pricePerItem;
}
/**
* @param string $pricePerItem
*/
public function setPricePerItem($pricePerItem)
{
$this->pricePerItem = $pricePerItem;
}
/**
* @return string
*/
public function getVatPricePerItem()
{
return $this->vatPricePerItem;
}
/**
* @param string $vatPricePerItem
*/
public function setVatPricePerItem($vatPricePerItem)
{
$this->vatPricePerItem = $vatPricePerItem;
}
/**
* @param string $pricePerItem
*/
public function setPricesPerItem($pricePerItem)
{
$this->setPricePerItem($pricePerItem);
$this->setVatPricePerItem($this->getCompany()->createVatPrice($this->getPricePerItem(), $this->getDocument()->getVat()));
}
/**
* @return string
*/
public function getTotalPrice()
{
return $this->totalPrice;
}
/**
* Set total price
*/
public function setTotalPrice()
{
$this->totalPrice = number_format($this->pricePerItem * $this->quantity, 2, '.', '');
}
/**
* @return string
*/
public function getTotalVatPrice()
{
return $this->totalVatPrice;
}
/**
* Set total vat price
*/
public function setTotalVatPrice()
{
$this->totalVatPrice = number_format($this->vatPricePerItem * $this->quantity, 2, '.', '');
}
public function setTotalPrices()
{
$this->setTotalPrice();
$this->setTotalVatPrice();
}
/**
* @return DocumentItem|null
*/
public function getParent()
{
return $this->parent;
}
/**
* @param DocumentItem|null $parent
*/
public function setParent(?DocumentItem $parent)
{
$this->parent = $parent;
}
/**
* @return ArrayCollection
*/
public function getChildren()
{
return $this->children;
}
/**
* @param ArrayCollection $children
*/
public function setChildren($children)
{
$this->children = $children;
}
/**
* @return ProductContainer
*/
public function getProductContainer()
{
return $this->productContainer;
}
/**
* @param ProductContainer|null $productContainer
*/
public function setProductContainer(?ProductContainer $productContainer)
{
$this->productContainer = $productContainer;
}
/**
* @return AttributeOption
*/
public function getAttributeOption()
{
return $this->attributeOption;
}
/**
* @param AttributeOption $attributeOption
*/
public function setAttributeOption($attributeOption)
{
$this->attributeOption = $attributeOption;
}
/**
* @return StockMovementItem|null
*/
public function getStockMovementItem(): ?StockMovementItem
{
return $this->stockMovementItem;
}
/**
* @param StockMovementItem|null $stockMovementItem
*
* @return void
*/
public function setStockMovementItem(?StockMovementItem $stockMovementItem): void
{
$this->stockMovementItem = $stockMovementItem;
}
public function getItemPriceForOnePiece()
{
$price = $this->getPricePerItem();
foreach ($this->getChildren() as $c) {
if ($c->getAttributeOption() != null) { // skip container prices
$price += $c->getPricePerItem();
}
}
return $price;
}
public function getVatItemPriceForOnePiece()
{
$price = $this->getVatPricePerItem();
foreach ($this->getChildren() as $c) {
if ($c->getAttributeOption() != null) { // skip container prices
$price += $c->getVatPricePerItem();
}
}
return $price;//round($price, 2);
}
public function getItemFullPrice()
{
$fullPrice = $this->quantity * $this->getItemPriceForOnePiece();
return $fullPrice;//round($fullPrice, 2);
}
public function getVatItemFullPrice()
{
$fullPrice = $this->quantity * $this->getVatItemPriceForOnePiece();
return $fullPrice;//round($fullPrice, 2);
}
}