src/AtechNet/CoreBundle/Entity/DocumentItem.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\AtechNet\CoreBundle\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\ORM\Mapping\Column;
  5. use Doctrine\ORM\Mapping\Entity;
  6. use Doctrine\ORM\Mapping\JoinColumn;
  7. use Doctrine\ORM\Mapping\ManyToOne;
  8. use Doctrine\ORM\Mapping\OneToMany;
  9. use Doctrine\ORM\Mapping\OneToOne;
  10. use Doctrine\ORM\Mapping\Table as Table;
  11. /**
  12.  * DocumentItem
  13.  *
  14.  * @Table(name="document_items")
  15.  * @Entity
  16.  */
  17. class DocumentItem extends AbstractSignedEntity
  18. {
  19.     /**
  20.      * @var Document
  21.      *
  22.      * @ManyToOne(targetEntity="Document", inversedBy="items")
  23.      * @JoinColumn(name="document_id", referencedColumnName="id", nullable=false)
  24.      */
  25.     private $document;
  26.     /**
  27.      * @var Product
  28.      *
  29.      * @ManyToOne(targetEntity="Product")
  30.      * @JoinColumn(name="product_id", referencedColumnName="id", nullable=true)
  31.      */
  32.     private $product;
  33.     /**
  34.      * @var string
  35.      *
  36.      * @Column(name="title", type="text")
  37.      */
  38.     private $title;
  39.     /**
  40.      * @var string
  41.      *
  42.      * @Column(name="description", type="text", nullable=true)
  43.      */
  44.     private $description;
  45.     /**
  46.      * @var int
  47.      *
  48.      * @Column(name="quantity", type="integer")
  49.      */
  50.     private $quantity;
  51.     /**
  52.      * @var string
  53.      *
  54.      * @Column(name="price_per_item", type="decimal", precision=12, scale=3)
  55.      */
  56.     private $pricePerItem = 0;
  57.     /**
  58.      * @var string
  59.      *
  60.      * @Column(name="vat_price_per_item", type="decimal", precision=12, scale=3)
  61.      */
  62.     private $vatPricePerItem = 0;
  63.     /**
  64.      * @var string
  65.      *
  66.      * @Column(name="total_price", type="decimal", precision=12, scale=3)
  67.      */
  68.     private $totalPrice = 0;
  69.     /**
  70.      * @var string
  71.      *
  72.      * @Column(name="total_vat_price", type="decimal", precision=12, scale=3)
  73.      */
  74.     private $totalVatPrice = 0;
  75.     /**
  76.      * @var DocumentItem|null
  77.      *
  78.      * @ManyToOne(targetEntity="DocumentItem", inversedBy="children", cascade={"persist"})
  79.      * @JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
  80.      */
  81.     private $parent;
  82.     /**
  83.      * @var ArrayCollection
  84.      *
  85.      * @OneToMany(targetEntity="DocumentItem", mappedBy="parent", cascade={"persist", "remove"})
  86.      **/
  87.     private $children;
  88.     /**
  89.      * @var ProductContainer|null
  90.      *
  91.      * @ManyToOne(targetEntity="ProductContainer")
  92.      * @JoinColumn(name="product_container_id", referencedColumnName="id", nullable=true)
  93.      */
  94.     private $productContainer;
  95.     /**
  96.      * @var AttributeOption
  97.      *
  98.      * @ManyToOne(targetEntity="AttributeOption")
  99.      * @JoinColumn(name="attribute_option_id", referencedColumnName="id")
  100.      */
  101.     private $attributeOption;
  102.     /**
  103.      * @var ?StockMovementItem
  104.      *
  105.      * @OneToOne(targetEntity="StockMovementItem", mappedBy="documentItem", cascade={"persist", "remove"})
  106.      */
  107.     private $stockMovementItem;
  108.     public function __construct()
  109.     {
  110.         parent::__construct();
  111.         $this->children = new ArrayCollection();
  112.     }
  113.     public function isFreetext()
  114.     {
  115.         return $this->product == null && $this->attributeOption == null;
  116.     }
  117.     /**
  118.      * @return Company
  119.      */
  120.     public function getCompany()
  121.     {
  122.         return $this->getDocument()->getCompany();
  123.     }
  124.     /**
  125.      * @return string
  126.      */
  127.     public function getCurrency()
  128.     {
  129.         return $this->getCompany()->getCurrency();
  130.     }
  131.     /**
  132.      * @return Document
  133.      */
  134.     public function getDocument()
  135.     {
  136.         return $this->document;
  137.     }
  138.     /**
  139.      * @param Document $document
  140.      */
  141.     public function setDocument($document)
  142.     {
  143.         $this->document = $document;
  144.     }
  145.     /**
  146.      * @return Product
  147.      */
  148.     public function getProduct()
  149.     {
  150.         return $this->product;
  151.     }
  152.     /**
  153.      * @param Product $product
  154.      */
  155.     public function setProduct($product)
  156.     {
  157.         $this->product = $product;
  158.     }
  159.     /**
  160.      * @return string
  161.      */
  162.     public function getTitle()
  163.     {
  164.         return $this->title;
  165.     }
  166.     /**
  167.      * @param string $title
  168.      */
  169.     public function setTitle($title)
  170.     {
  171.         $this->title = $title;
  172.     }
  173.     /**
  174.      * @return string
  175.      */
  176.     public function getDescription()
  177.     {
  178.         return $this->description;
  179.     }
  180.     /**
  181.      * @param string $description
  182.      */
  183.     public function setDescription($description)
  184.     {
  185.         $this->description = $description;
  186.     }
  187.     /**
  188.      * @return int
  189.      */
  190.     public function getQuantity()
  191.     {
  192.         return $this->quantity;
  193.     }
  194.     /**
  195.      * @param int $quantity
  196.      */
  197.     public function setQuantity($quantity)
  198.     {
  199.         $this->quantity = $quantity;
  200.     }
  201.     /**
  202.      * @return string
  203.      */
  204.     public function getPricePerItem()
  205.     {
  206.         return $this->pricePerItem;
  207.     }
  208.     /**
  209.      * @param string $pricePerItem
  210.      */
  211.     public function setPricePerItem($pricePerItem)
  212.     {
  213.         $this->pricePerItem = $pricePerItem;
  214.     }
  215.     /**
  216.      * @return string
  217.      */
  218.     public function getVatPricePerItem()
  219.     {
  220.         return $this->vatPricePerItem;
  221.     }
  222.     /**
  223.      * @param string $vatPricePerItem
  224.      */
  225.     public function setVatPricePerItem($vatPricePerItem)
  226.     {
  227.         $this->vatPricePerItem = $vatPricePerItem;
  228.     }
  229.     /**
  230.      * @param string $pricePerItem
  231.      */
  232.     public function setPricesPerItem($pricePerItem)
  233.     {
  234.         $this->setPricePerItem($pricePerItem);
  235.         $this->setVatPricePerItem($this->getCompany()->createVatPrice($this->getPricePerItem(), $this->getDocument()->getVat()));
  236.     }
  237.     /**
  238.      * @return string
  239.      */
  240.     public function getTotalPrice()
  241.     {
  242.         return $this->totalPrice;
  243.     }
  244.     /**
  245.      * Set total price
  246.      */
  247.     public function setTotalPrice()
  248.     {
  249.         $this->totalPrice = number_format($this->pricePerItem * $this->quantity, 2, '.', '');
  250.     }
  251.     /**
  252.      * @return string
  253.      */
  254.     public function getTotalVatPrice()
  255.     {
  256.         return $this->totalVatPrice;
  257.     }
  258.     /**
  259.      * Set total vat price
  260.      */
  261.     public function setTotalVatPrice()
  262.     {
  263.         $this->totalVatPrice = number_format($this->vatPricePerItem * $this->quantity, 2, '.', '');
  264.     }
  265.     public function setTotalPrices()
  266.     {
  267.         $this->setTotalPrice();
  268.         $this->setTotalVatPrice();
  269.     }
  270.     /**
  271.      * @return DocumentItem|null
  272.      */
  273.     public function getParent()
  274.     {
  275.         return $this->parent;
  276.     }
  277.     /**
  278.      * @param DocumentItem|null $parent
  279.      */
  280.     public function setParent(?DocumentItem $parent)
  281.     {
  282.         $this->parent = $parent;
  283.     }
  284.     /**
  285.      * @return ArrayCollection
  286.      */
  287.     public function getChildren()
  288.     {
  289.         return $this->children;
  290.     }
  291.     /**
  292.      * @param ArrayCollection $children
  293.      */
  294.     public function setChildren($children)
  295.     {
  296.         $this->children = $children;
  297.     }
  298.     /**
  299.      * @return ProductContainer
  300.      */
  301.     public function getProductContainer()
  302.     {
  303.         return $this->productContainer;
  304.     }
  305.     /**
  306.      * @param ProductContainer|null $productContainer
  307.      */
  308.     public function setProductContainer(?ProductContainer $productContainer)
  309.     {
  310.         $this->productContainer = $productContainer;
  311.     }
  312.     /**
  313.      * @return AttributeOption
  314.      */
  315.     public function getAttributeOption()
  316.     {
  317.         return $this->attributeOption;
  318.     }
  319.     /**
  320.      * @param AttributeOption $attributeOption
  321.      */
  322.     public function setAttributeOption($attributeOption)
  323.     {
  324.         $this->attributeOption = $attributeOption;
  325.     }
  326.     /**
  327.      * @return StockMovementItem|null
  328.      */
  329.     public function getStockMovementItem(): ?StockMovementItem 
  330.     {
  331.         return $this->stockMovementItem;
  332.     }
  333.     /**
  334.      * @param StockMovementItem|null $stockMovementItem
  335.      * 
  336.      * @return void
  337.      */
  338.     public function setStockMovementItem(?StockMovementItem $stockMovementItem): void
  339.     {
  340.         $this->stockMovementItem = $stockMovementItem;
  341.     }
  342.     public function getItemPriceForOnePiece()
  343.     {
  344.         $price = $this->getPricePerItem();
  345.         foreach ($this->getChildren() as $c) {
  346.             if ($c->getAttributeOption() != null) { // skip container prices
  347.                 $price += $c->getPricePerItem();
  348.             }
  349.         }
  350.         return $price;
  351.     }
  352.     public function getVatItemPriceForOnePiece()
  353.     {
  354.         $price = $this->getVatPricePerItem();
  355.         foreach ($this->getChildren() as $c) {
  356.             if ($c->getAttributeOption() != null) { // skip container prices
  357.                 $price += $c->getVatPricePerItem();
  358.             }
  359.         }
  360.         return $price;//round($price, 2);
  361.     }
  362.     public function getItemFullPrice()
  363.     {
  364.         $fullPrice = $this->quantity * $this->getItemPriceForOnePiece();
  365.         return $fullPrice;//round($fullPrice, 2);
  366.     }
  367.     public function getVatItemFullPrice()
  368.     {
  369.         $fullPrice = $this->quantity * $this->getVatItemPriceForOnePiece();
  370.         return $fullPrice;//round($fullPrice, 2);
  371.     }
  372. }