Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

335 lines
9.8KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Model\Product;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Lc\CaracoleBundle\Doctrine\Extension\ProductPropertyTrait;
  6. use Lc\CaracoleBundle\Doctrine\Extension\PriceInterface;
  7. use Lc\CaracoleBundle\Doctrine\Extension\ProductPropertyInterface;
  8. use Gedmo\Mapping\Annotation as Gedmo;
  9. use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
  10. use Lc\SovBundle\Doctrine\Extension\SortableInterface;
  11. use Lc\SovBundle\Doctrine\Extension\SortableTrait;
  12. use Lc\SovBundle\Doctrine\Extension\StatusTrait;
  13. use Lc\SovBundle\Doctrine\Pattern\AbstractLightEntity;
  14. /**
  15. * @ORM\MappedSuperclass()
  16. */
  17. abstract class ProductModel extends AbstractLightEntity implements SortableInterface, ProductPropertyInterface,
  18. PriceInterface
  19. {
  20. use SortableTrait;
  21. use ProductPropertyTrait;
  22. use StatusTrait;
  23. /**
  24. * @Gedmo\Blameable(on="create")
  25. * @ORM\ManyToOne(targetEntity="Lc\SovBundle\Model\User\UserInterface")
  26. * @ORM\JoinColumn(nullable=true)
  27. */
  28. protected $createdBy;
  29. /**
  30. * @Gedmo\Blameable(on="update")
  31. * @ORM\ManyToOne(targetEntity="Lc\SovBundle\Model\User\UserInterface")
  32. * @ORM\JoinColumn(nullable=true)
  33. */
  34. protected $updatedBy;
  35. /**
  36. * @ORM\ManyToOne(targetEntity="Lc\CaracoleBundle\Model\Product\ProductFamilyInterface", inversedBy="products", cascade={"persist"})
  37. * @ORM\JoinColumn(nullable=false)
  38. */
  39. protected $productFamily;
  40. /**
  41. * @ORM\Column(type="string", length=255, nullable=true)
  42. */
  43. protected $title;
  44. /**
  45. * @ORM\Column(type="boolean", nullable=true)
  46. */
  47. protected $originProduct;
  48. public function __construct()
  49. {
  50. $this->orderProducts = new ArrayCollection();
  51. $this->status = 1;
  52. }
  53. public function __toString()
  54. {
  55. $title = $this->getProductFamily()->getTitle();
  56. if ($this->getTitle() && strlen($this->getTitle())) {
  57. $title .= ' - ' . $this->getTitle();
  58. }
  59. if ($this->getProductFamily()->hasProductsWithVariousWeight()) {
  60. $title .= ' - ' . $this->getQuantityLabelInherited();
  61. }
  62. return $title;
  63. }
  64. public function isProductSaleStatusOn()
  65. {
  66. if ($this->getProductFamily()->getSaleStatus() != 1) {
  67. return false;
  68. }
  69. $allCategoriesSalesOff = true;
  70. $unavailableSpecificDay = false;
  71. foreach ($this->getProductFamily()->getProductCategories() as $category) {
  72. if ($category->getParent()) {
  73. if ($category->getSaleStatus() && $category->getParent()->getSaleStatus()) {
  74. $allCategoriesSalesOff = false;
  75. }
  76. } else {
  77. if ($category->getSaleStatus()) {
  78. $allCategoriesSalesOff = false;
  79. }
  80. }
  81. // specific day
  82. // @TODO : spécifique pdl ?
  83. $displaySpecificDay = $category->getDisplaySpecificDay();
  84. if ($displaySpecificDay && $displaySpecificDay != date('N')) {
  85. $unavailableSpecificDay = true;
  86. }
  87. }
  88. if ($allCategoriesSalesOff) {
  89. return false;
  90. }
  91. if ($unavailableSpecificDay) {
  92. return false;
  93. }
  94. return true;
  95. }
  96. // isProductAvailable
  97. public function isAvailable($quantityOrder = 0, $checkCart = false, $orderShop = null)
  98. {
  99. if ($this->getStatus() != 1 || $this->getProductFamily()->getStatus() != 1 || !$this->isProductSaleStatusOn()) {
  100. return false;
  101. }
  102. // @TODO : orderShop à définir où est appelé isProductAvailable
  103. if ($checkCart && !$orderShop) {
  104. throw new \Exception("Attention jeune padawan : définir le orderShop à l'endroit où est appelé isProductAvailable");
  105. }
  106. $productFamily = $this->getProductFamily();
  107. $quantityAsked = $quantityOrder;
  108. if ($productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  109. if (!$quantityOrder) {
  110. $quantityAsked = $orderShop->getQuantityOrderByProduct($this, true);
  111. } else {
  112. $quantityAsked = ($this->getQuantityInherited() / $this->getUnitInherited()->getCoefficient()) * $quantityOrder;
  113. }
  114. if ($checkCart) {
  115. $quantityAsked += $orderShop->getQuantityOrderByProduct($this, true);
  116. }
  117. }
  118. if (($productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY
  119. || $productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT)) {
  120. if (!$quantityOrder) {
  121. $quantityAsked = $orderShop->getQuantityOrderByProduct($this);
  122. }
  123. if ($checkCart) {
  124. $quantityAsked += $orderShop->getQuantityOrderByProduct($this);
  125. }
  126. }
  127. if ($this->getAvailableQuantityInherited() >= $quantityAsked
  128. || $productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_UNLIMITED) {
  129. return true;
  130. } else {
  131. return false;
  132. }
  133. }
  134. // getProductQuantityMaxAddCart
  135. public function getQuantityMaxAddCart(OrderShopInterface $orderShop)
  136. {
  137. $productFamily = $this->getProductFamily();
  138. $byWeight = false;
  139. if ($productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  140. $byWeight = true;
  141. }
  142. return $this->getAvailableQuantityInherited() - $orderShop->getQuantityOrderByProduct($this, $byWeight);
  143. }
  144. // getProductQuantity
  145. public function getProductQuantity()
  146. {
  147. $productFamily = $this->getProductFamily();
  148. if ($productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  149. return $this->getQuantityInherited() / $this->getUnitInherited()->getCoefficient();
  150. } else {
  151. return 1;
  152. }
  153. }
  154. public function getBuyingPriceInherited()
  155. {
  156. if ($this->getBuyingPrice()) {
  157. return $this->getBuyingPrice();
  158. } else {
  159. return $this->getProductFamily()->getBuyingPrice();
  160. }
  161. }
  162. public function getBuyingPriceByRefUnitInherited()
  163. {
  164. if ($this->getBuyingPriceByRefUnit()) {
  165. return $this->getBuyingPriceByRefUnit();
  166. } else {
  167. return $this->getProductFamily()->getBuyingPriceByRefUnit();
  168. }
  169. }
  170. public function getPriceInherited()
  171. {
  172. if ($this->getPrice()) {
  173. return $this->getPrice();
  174. } else {
  175. return $this->getProductFamily()->getPrice();
  176. }
  177. }
  178. public function getPriceByRefUnitInherited()
  179. {
  180. if ($this->getPriceByRefUnit()) {
  181. return $this->getPriceByRefUnit();
  182. } else {
  183. return $this->getProductFamily()->getPriceByRefUnit();
  184. }
  185. }
  186. public function getBehaviorPriceInherited()
  187. {
  188. return $this->getProductFamily()->getBehaviorPrice();
  189. }
  190. public function getReductionCatalogInherited()
  191. {
  192. return $this->getProductFamily()->getReductionCatalog();
  193. }
  194. public function getUnitInherited()
  195. {
  196. if ($this->getUnit()) {
  197. return $this->getUnit();
  198. } else {
  199. return $this->getProductFamily()->getUnit();
  200. }
  201. }
  202. public function getTitleInherited()
  203. {
  204. if ($this->getTitle()) {
  205. return $this->getTitle();
  206. } else {
  207. return $this->getProductFamily()->getTitle();
  208. }
  209. }
  210. public function getQuantityInherited()
  211. {
  212. if ($this->getQuantity()) {
  213. return $this->getQuantity();
  214. } else {
  215. return $this->getProductFamily()->getQuantity();
  216. }
  217. }
  218. public function getQuantityLabelInherited()
  219. {
  220. $quantity = $this->getQuantityInherited();
  221. $unit = $this->getUnitInherited();
  222. return $quantity . $unit->getWordingShort();
  223. }
  224. public function getQuantityTitle($productFamily)
  225. {
  226. $title = $this->getQuantityLabelInherited();
  227. if ($productFamily->hasProductsWithVariousWeight()) {
  228. $title .= ', ' . $this->getTitleInherited();
  229. }
  230. return $title;
  231. }
  232. public function getAvailableQuantityInherited()
  233. {
  234. switch ($this->getProductFamily()->getBehaviorCountStock()) {
  235. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY :
  236. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE :
  237. return $this->getProductFamily()->getAvailableQuantity();
  238. break;
  239. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT :
  240. return $this->getAvailableQuantity();
  241. break;
  242. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_UNLIMITED :
  243. return false;
  244. break;
  245. }
  246. }
  247. public function getTaxRateInherited()
  248. {
  249. return $this->getProductFamily()->getTaxRateInherited();
  250. }
  251. public function getProductFamily(): ?ProductFamilyInterface
  252. {
  253. return $this->productFamily;
  254. }
  255. public function setProductFamily(?ProductFamilyInterface $productFamily): self
  256. {
  257. $this->productFamily = $productFamily;
  258. return $this;
  259. }
  260. public function getTitle(): ?string
  261. {
  262. return $this->title;
  263. }
  264. public function setTitle(?string $title): self
  265. {
  266. $this->title = $title;
  267. return $this;
  268. }
  269. public function getOriginProduct(): ?bool
  270. {
  271. return $this->originProduct;
  272. }
  273. public function setOriginProduct(?bool $originProduct): self
  274. {
  275. $this->originProduct = $originProduct;
  276. return $this;
  277. }
  278. }