|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- <?php
-
- namespace Lc\CaracoleBundle\Model\Product;
-
- use Doctrine\Common\Collections\ArrayCollection;
- use Doctrine\ORM\Mapping as ORM;
- use Lc\CaracoleBundle\Doctrine\Extension\ProductPropertyTrait;
- use Lc\CaracoleBundle\Doctrine\Extension\PriceInterface;
- use Lc\CaracoleBundle\Doctrine\Extension\ProductPropertyInterface;
- use Gedmo\Mapping\Annotation as Gedmo;
- use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
- use Lc\SovBundle\Doctrine\Extension\SortableInterface;
- use Lc\SovBundle\Doctrine\Extension\SortableTrait;
- use Lc\SovBundle\Doctrine\Extension\StatusTrait;
- use Lc\SovBundle\Doctrine\Pattern\AbstractLightEntity;
-
- /**
- * @ORM\MappedSuperclass()
- */
- abstract class ProductModel extends AbstractLightEntity implements SortableInterface, ProductPropertyInterface,
- PriceInterface
- {
- use SortableTrait;
- use ProductPropertyTrait;
- use StatusTrait;
-
- /**
- * @Gedmo\Blameable(on="create")
- * @ORM\ManyToOne(targetEntity="Lc\SovBundle\Model\User\UserInterface")
- * @ORM\JoinColumn(nullable=true)
- */
- protected $createdBy;
-
- /**
- * @Gedmo\Blameable(on="update")
- * @ORM\ManyToOne(targetEntity="Lc\SovBundle\Model\User\UserInterface")
- * @ORM\JoinColumn(nullable=true)
- */
- protected $updatedBy;
-
- /**
- * @ORM\ManyToOne(targetEntity="Lc\CaracoleBundle\Model\Product\ProductFamilyInterface", inversedBy="products", cascade={"persist"})
- * @ORM\JoinColumn(nullable=false)
- */
- protected $productFamily;
-
- /**
- * @ORM\Column(type="string", length=255, nullable=true)
- */
- protected $title;
-
- /**
- * @ORM\Column(type="boolean", nullable=true)
- */
- protected $originProduct;
-
- public function __construct()
- {
- $this->orderProducts = new ArrayCollection();
- $this->status = 1;
- }
-
- public function __toString()
- {
- $title = $this->getProductFamily()->getTitle();
-
- if ($this->getTitle() && strlen($this->getTitle())) {
- $title .= ' - ' . $this->getTitle();
- }
-
- if ($this->getProductFamily()->hasProductsWithVariousWeight()) {
- $title .= ' - ' . $this->getQuantityLabelInherited();
- }
-
- return $title;
- }
-
- public function isProductSaleStatusOn()
- {
- if ($this->getProductFamily()->getSaleStatus() != 1) {
- return false;
- }
-
- $allCategoriesSalesOff = true;
- $unavailableSpecificDay = false;
-
- foreach ($this->getProductFamily()->getProductCategories() as $category) {
- if ($category->getParent()) {
- if ($category->getSaleStatus() && $category->getParent()->getSaleStatus()) {
- $allCategoriesSalesOff = false;
- }
- } else {
- if ($category->getSaleStatus()) {
- $allCategoriesSalesOff = false;
- }
- }
-
- // specific day
- // @TODO : spécifique pdl ?
- $displaySpecificDay = $category->getDisplaySpecificDay();
- if ($displaySpecificDay && $displaySpecificDay != date('N')) {
- $unavailableSpecificDay = true;
- }
- }
-
- if ($allCategoriesSalesOff) {
- return false;
- }
-
- if ($unavailableSpecificDay) {
- return false;
- }
-
- return true;
- }
-
- // isProductAvailable
- public function isAvailable($quantityOrder = 0, $checkCart = false, $orderShop = null)
- {
- if ($this->getStatus() != 1 || $this->getProductFamily()->getStatus() != 1 || !$this->isProductSaleStatusOn()) {
- return false;
- }
-
- // @TODO : orderShop à définir où est appelé isProductAvailable
- if ($checkCart && !$orderShop) {
- throw new \Exception("Attention jeune padawan : définir le orderShop à l'endroit où est appelé isProductAvailable");
- }
-
- $productFamily = $this->getProductFamily();
- $quantityAsked = $quantityOrder;
-
- if ($productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
- if (!$quantityOrder) {
- $quantityAsked = $orderShop->getQuantityOrderByProduct($this, true);
- } else {
- $quantityAsked = ($this->getQuantityInherited() / $this->getUnitInherited()->getCoefficient()) * $quantityOrder;
- }
-
- if ($checkCart) {
- $quantityAsked += $orderShop->getQuantityOrderByProduct($this, true);
- }
- }
-
- if (($productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY
- || $productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT)) {
-
- if (!$quantityOrder) {
- $quantityAsked = $orderShop->getQuantityOrderByProduct($this);
- }
-
- if ($checkCart) {
- $quantityAsked += $orderShop->getQuantityOrderByProduct($this);
- }
- }
-
- if ($this->getAvailableQuantityInherited() >= $quantityAsked
- || $productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_UNLIMITED) {
- return true;
- } else {
- return false;
- }
- }
-
- // getProductQuantityMaxAddCart
- public function getQuantityMaxAddCart(OrderShopInterface $orderShop)
- {
- $productFamily = $this->getProductFamily();
-
- $byWeight = false;
- if ($productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
- $byWeight = true;
- }
-
- return $this->getAvailableQuantityInherited() - $orderShop->getQuantityOrderByProduct($this, $byWeight);
- }
-
- // getProductQuantity
- public function getProductQuantity()
- {
- $productFamily = $this->getProductFamily();
-
- if ($productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
- return $this->getQuantityInherited() / $this->getUnitInherited()->getCoefficient();
- } else {
- return 1;
- }
- }
-
- public function getBuyingPriceInherited()
- {
- if ($this->getBuyingPrice()) {
- return $this->getBuyingPrice();
- } else {
- return $this->getProductFamily()->getBuyingPrice();
- }
- }
-
- public function getBuyingPriceByRefUnitInherited()
- {
- if ($this->getBuyingPriceByRefUnit()) {
- return $this->getBuyingPriceByRefUnit();
- } else {
- return $this->getProductFamily()->getBuyingPriceByRefUnit();
- }
- }
-
- public function getPriceInherited()
- {
- if ($this->getPrice()) {
- return $this->getPrice();
- } else {
- return $this->getProductFamily()->getPrice();
- }
- }
-
- public function getPriceByRefUnitInherited()
- {
- if ($this->getPriceByRefUnit()) {
- return $this->getPriceByRefUnit();
- } else {
- return $this->getProductFamily()->getPriceByRefUnit();
- }
- }
-
- public function getBehaviorPriceInherited()
- {
- return $this->getProductFamily()->getBehaviorPrice();
- }
-
- public function getReductionCatalogInherited()
- {
- return $this->getProductFamily()->getReductionCatalog();
- }
-
- public function getUnitInherited()
- {
- if ($this->getUnit()) {
- return $this->getUnit();
- } else {
- return $this->getProductFamily()->getUnit();
- }
- }
-
- public function getTitleInherited()
- {
- if ($this->getTitle()) {
- return $this->getTitle();
- } else {
- return $this->getProductFamily()->getTitle();
- }
- }
-
- public function getQuantityInherited()
- {
- if ($this->getQuantity()) {
- return $this->getQuantity();
- } else {
- return $this->getProductFamily()->getQuantity();
- }
- }
-
- public function getQuantityLabelInherited()
- {
- $quantity = $this->getQuantityInherited();
- $unit = $this->getUnitInherited();
- return $quantity . $unit->getWordingShort();
- }
-
- public function getQuantityTitle($productFamily)
- {
- $title = $this->getQuantityLabelInherited();
- if ($productFamily->hasProductsWithVariousWeight()) {
- $title .= ', ' . $this->getTitleInherited();
- }
- return $title;
- }
-
- public function getAvailableQuantityInherited()
- {
- switch ($this->getProductFamily()->getBehaviorCountStock()) {
- case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY :
- case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE :
- return $this->getProductFamily()->getAvailableQuantity();
- break;
- case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT :
- return $this->getAvailableQuantity();
- break;
- case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_UNLIMITED :
- return false;
- break;
- }
- }
-
- public function getTaxRateInherited()
- {
- return $this->getProductFamily()->getTaxRateInherited();
- }
-
- public function getProductFamily(): ?ProductFamilyInterface
- {
- return $this->productFamily;
- }
-
- public function setProductFamily(?ProductFamilyInterface $productFamily): self
- {
- $this->productFamily = $productFamily;
-
- return $this;
- }
-
- public function getTitle(): ?string
- {
- return $this->title;
- }
-
- public function setTitle(?string $title): self
- {
- $this->title = $title;
-
- return $this;
- }
-
- public function getOriginProduct(): ?bool
- {
- return $this->originProduct;
- }
-
- public function setOriginProduct(?bool $originProduct): self
- {
- $this->originProduct = $originProduct;
-
- return $this;
- }
- }
|