You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

318 lines
8.3KB

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