src/Entity/Product.php line 621

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\ProductDescription;
  4. use App\Entity\ProductModel;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
  7. use App\Helpers\Encoder;
  8. /**
  9. * Products
  10. *
  11. * @ORM\Table(name="products")
  12. * @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
  13. * @ORM\HasLifecycleCallbacks()
  14. */
  15. class Product extends TranslatedEntity
  16. {
  17. public const VAT_RATE = 0.2;
  18. public const SEARCH_INDICES = [
  19. 'fr' => 'fr-products',
  20. 'en' => 'en-products',
  21. ];
  22. public const TYPE_PRODUCT = 'product';
  23. public const TYPE_SAV = 'sav';
  24. protected $tranlatedEntity = 'ProductDescription';
  25. /**
  26. * @var int
  27. *
  28. * @ORM\Column(name="products_id", type="integer", nullable=false)
  29. * @ORM\Id
  30. * @ORM\GeneratedValue(strategy="IDENTITY")
  31. */
  32. private $id;
  33. /**
  34. * @ORM\OneToMany(targetEntity="App\Entity\ProductAttribute", mappedBy="product")
  35. */
  36. private $attributes;
  37. /**
  38. * @ORM\OneToMany(targetEntity="App\Entity\Product", mappedBy="parent")
  39. */
  40. private $children = [];
  41. /**
  42. * @ORM\ManyToOne(targetEntity="App\Entity\Product", inversedBy="children")
  43. * @ORM\JoinColumn(name="products_parent_id", referencedColumnName="products_id", nullable=true)
  44. */
  45. private $parent;
  46. /**
  47. * @var ?ProductModel
  48. *
  49. * @ORM\ManyToOne(targetEntity="App\Entity\ProductModel", inversedBy="products")
  50. * @ORM\JoinColumn(name="products_model_id", referencedColumnName="id", nullable=true)
  51. */
  52. private $productModel;
  53. /**
  54. * @ORM\OneToMany(targetEntity="App\Entity\ProductDescription", mappedBy="product")
  55. */
  56. private $descriptions = [];
  57. /**
  58. * @ORM\OneToMany(targetEntity="App\Entity\PictureProduct", mappedBy="product")
  59. * @ORM\OrderBy({"rank" = "ASC", "id" = "ASC"})
  60. */
  61. private $pictures = [];
  62. /**
  63. * @var App\Entity\Manufacturer|null
  64. *
  65. * @ORM\ManyToOne(targetEntity="App\Entity\Manufacturer")
  66. * @ORM\JoinColumn(name="manufacturers_id", referencedColumnName="manufacturers_id", nullable=true)
  67. */
  68. private $manufacturer;
  69. /**
  70. * @ORM\OneToMany(targetEntity="App\Entity\ProductCountryRestriction", mappedBy="product")
  71. */
  72. private $countryRestrictions;
  73. /**
  74. * @ORM\OneToMany(targetEntity="App\Entity\ProductToProductExtraField", mappedBy="product")
  75. */
  76. private $extraFields = [];
  77. /**
  78. * @ORM\OneToMany(targetEntity="App\Entity\ProductPrice", mappedBy="product", cascade={"persist", "remove"}, orphanRemoval=true)
  79. */
  80. private $prices = [];
  81. /**
  82. * @var int
  83. *
  84. * @ORM\Column(name="products_quantity", type="integer", nullable=false)
  85. */
  86. private $quantity = '0';
  87. /**
  88. * @var string|null
  89. *
  90. * @ORM\Column(name="products_model", type="string", length=256, nullable=true)
  91. */
  92. private $model;
  93. /**
  94. * @var string|null
  95. *
  96. * @ORM\Column(name="products_model_sap", type="string", length=50, nullable=true)
  97. */
  98. private $modelSAP;
  99. /**
  100. * @var string|null
  101. *
  102. * @ORM\Column(name="products_ean", type="string", length=50, nullable=true)
  103. */
  104. private $ean;
  105. /**
  106. * @var string|null
  107. *
  108. * @ORM\Column(name="products_image", type="string", length=140, nullable=true)
  109. */
  110. private $image;
  111. /**
  112. * @var string|null
  113. *
  114. * @ORM\Column(name="products_image_med", type="string", length=140, nullable=true)
  115. */
  116. private $imageMed;
  117. /**
  118. * @var string|null
  119. *
  120. * @ORM\Column(name="products_image_lrg", type="string", length=140, nullable=true)
  121. */
  122. private $imageLrg;
  123. /**
  124. * @var string|null
  125. *
  126. * @ORM\Column(name="products_image_sm_1", type="string", length=140, nullable=true)
  127. */
  128. private $imageSm1;
  129. /**
  130. * @var string|null
  131. *
  132. * @ORM\Column(name="products_image_xl_1", type="string", length=140, nullable=true)
  133. */
  134. private $imageXl1;
  135. /**
  136. * @var string|null
  137. *
  138. * @ORM\Column(name="products_image_sm_2", type="string", length=140, nullable=true)
  139. */
  140. private $imageSm2;
  141. /**
  142. * @var string|null
  143. *
  144. * @ORM\Column(name="products_image_xl_2", type="string", length=140, nullable=true)
  145. */
  146. private $imageXl2;
  147. /**
  148. * @var string|null
  149. *
  150. * @ORM\Column(name="products_image_sm_3", type="string", length=140, nullable=true)
  151. */
  152. private $imageSm3;
  153. /**
  154. * @var string|null
  155. *
  156. * @ORM\Column(name="products_image_xl_3", type="string", length=140, nullable=true)
  157. */
  158. private $imageXl3;
  159. /**
  160. * @var string|null
  161. *
  162. * @ORM\Column(name="products_image_sm_4", type="string", length=140, nullable=true)
  163. */
  164. private $imageSm4;
  165. /**
  166. * @var string|null
  167. *
  168. * @ORM\Column(name="products_image_xl_4", type="string", length=140, nullable=true)
  169. */
  170. private $imageXl4;
  171. /**
  172. * @var string|null
  173. *
  174. * @ORM\Column(name="products_image_sm_5", type="string", length=140, nullable=true)
  175. */
  176. private $imageSm5;
  177. /**
  178. * @var string|null
  179. *
  180. * @ORM\Column(name="products_image_xl_5", type="string", length=140, nullable=true)
  181. */
  182. private $imageXl5;
  183. /**
  184. * @var string|null
  185. *
  186. * @ORM\Column(name="products_image_sm_6", type="string", length=140, nullable=true)
  187. */
  188. private $imageSm6;
  189. /**
  190. * @var string|null
  191. *
  192. * @ORM\Column(name="products_image_xl_6", type="string", length=140, nullable=true)
  193. */
  194. private $imageXl6;
  195. /**
  196. * @var float
  197. *
  198. * @ORM\Column(name="products_price", type="decimal", precision=15, scale=4, nullable=false, options={"default"="0.0000"})
  199. */
  200. private $price = '0.0000';
  201. /**
  202. * @var float
  203. * @ORM\Column(name="products_ecotax", type="decimal", precision=15, scale=4, nullable=false, options={"default"="0.0000"})
  204. */
  205. private $ecotax = '0.0000';
  206. /**
  207. * @var \DateTime
  208. *
  209. * @ORM\Column(name="products_date_added", type="datetime", nullable=false)
  210. */
  211. private $dateAdded;
  212. /**
  213. * @var \DateTime|null
  214. *
  215. * @ORM\Column(name="products_last_modified", type="datetime", nullable=true)
  216. */
  217. private $lastModified;
  218. /**
  219. * @var \DateTime|null
  220. *
  221. * @ORM\Column(name="products_date_available", type="datetime", nullable=true)
  222. */
  223. private $dateAvailable;
  224. /**
  225. * @var string
  226. *
  227. * @ORM\Column(name="products_weight", type="decimal", precision=5, scale=2, nullable=false, options={"default"="0.00"})
  228. */
  229. private $weight = '0.00';
  230. /**
  231. * @var bool
  232. *
  233. * @ORM\Column(name="products_status", type="boolean", nullable=false)
  234. */
  235. private $status = '0';
  236. /** *
  237. * @ORM\ManyToOne(targetEntity="App\Entity\TaxClass", inversedBy="rates")
  238. * @ORM\JoinColumn(name="products_tax_class_id", referencedColumnName="tax_class_id")
  239. */
  240. private $taxClass;
  241. /**
  242. * @var int
  243. *
  244. * @ORM\Column(name="products_ordered", type="integer", nullable=false)
  245. */
  246. private $ordered = '0';
  247. /**
  248. * @var string
  249. *
  250. * @ORM\Column(name="products_price1", type="decimal", precision=15, scale=4, nullable=false, options={"default"="0.0000"})
  251. */
  252. private $price1 = '0.0000';
  253. /**
  254. * @var string
  255. *
  256. * @ORM\Column(name="products_price2", type="decimal", precision=15, scale=4, nullable=false, options={"default"="0.0000"})
  257. */
  258. private $price2 = '0.0000';
  259. /**
  260. * @var string
  261. *
  262. * @ORM\Column(name="products_price3", type="decimal", precision=15, scale=4, nullable=false, options={"default"="0.0000"})
  263. */
  264. private $price3 = '0.0000';
  265. /**
  266. * @var string
  267. *
  268. * @ORM\Column(name="products_price4", type="decimal", precision=15, scale=4, nullable=false, options={"default"="0.0000"})
  269. */
  270. private $price4 = '0.0000';
  271. /**
  272. * @var string
  273. *
  274. * @ORM\Column(name="products_price5", type="decimal", precision=15, scale=4, nullable=false, options={"default"="0.0000"})
  275. */
  276. private $price5 = '0.0000';
  277. /**
  278. * @var string
  279. *
  280. * @ORM\Column(name="products_price6", type="decimal", precision=15, scale=4, nullable=false, options={"default"="0.0000"})
  281. */
  282. private $price6 = '0.0000';
  283. /**
  284. * @var string
  285. *
  286. * @ORM\Column(name="products_price7", type="decimal", precision=15, scale=4, nullable=false, options={"default"="0.0000"})
  287. */
  288. private $price7 = '0.0000';
  289. /**
  290. * @var string
  291. *
  292. * @ORM\Column(name="products_price8", type="decimal", precision=15, scale=4, nullable=false, options={"default"="0.0000"})
  293. */
  294. private $price8 = '0.0000';
  295. /**
  296. * @var string
  297. *
  298. * @ORM\Column(name="products_price9", type="decimal", precision=15, scale=4, nullable=false, options={"default"="0.0000"})
  299. */
  300. private $price9 = '0.0000';
  301. /**
  302. * @var string
  303. *
  304. * @ORM\Column(name="products_price10", type="decimal", precision=15, scale=4, nullable=false, options={"default"="0.0000"})
  305. */
  306. private $price10 = '0.0000';
  307. /**
  308. * @var string
  309. *
  310. * @ORM\Column(name="products_price11", type="decimal", precision=15, scale=4, nullable=false, options={"default"="0.0000"})
  311. */
  312. private $price11 = '0.0000';
  313. /**
  314. * @var int
  315. *
  316. * @ORM\Column(name="products_price1_qty", type="integer", nullable=false)
  317. */
  318. private $price1Qty = '0';
  319. /**
  320. * @var int
  321. *
  322. * @ORM\Column(name="products_price2_qty", type="integer", nullable=false)
  323. */
  324. private $price2Qty = '0';
  325. /**
  326. * @var int
  327. *
  328. * @ORM\Column(name="products_price3_qty", type="integer", nullable=false)
  329. */
  330. private $price3Qty = '0';
  331. /**
  332. * @var int
  333. *
  334. * @ORM\Column(name="products_price4_qty", type="integer", nullable=false)
  335. */
  336. private $price4Qty = '0';
  337. /**
  338. * @var int
  339. *
  340. * @ORM\Column(name="products_price5_qty", type="integer", nullable=false)
  341. */
  342. private $price5Qty = '0';
  343. /**
  344. * @var int
  345. *
  346. * @ORM\Column(name="products_price6_qty", type="integer", nullable=false)
  347. */
  348. private $price6Qty = '0';
  349. /**
  350. * @var int
  351. *
  352. * @ORM\Column(name="products_price7_qty", type="integer", nullable=false)
  353. */
  354. private $price7Qty = '0';
  355. /**
  356. * @var int
  357. *
  358. * @ORM\Column(name="products_price8_qty", type="integer", nullable=false)
  359. */
  360. private $price8Qty = '0';
  361. /**
  362. * @var int
  363. *
  364. * @ORM\Column(name="products_price9_qty", type="integer", nullable=false)
  365. */
  366. private $price9Qty = '0';
  367. /**
  368. * @var int
  369. *
  370. * @ORM\Column(name="products_price10_qty", type="integer", nullable=false)
  371. */
  372. private $price10Qty = '0';
  373. /**
  374. * @var int
  375. *
  376. * @ORM\Column(name="products_price11_qty", type="integer", nullable=false)
  377. */
  378. private $price11Qty = '0';
  379. /**
  380. * @var int
  381. *
  382. * @ORM\Column(name="products_qty_blocks", type="integer", nullable=false, options={"default"="1"})
  383. */
  384. private $productsQtyBlocks = '1';
  385. /**
  386. * @var bool
  387. *
  388. * @ORM\Column(name="products_soleil", type="integer", nullable=false)
  389. */
  390. private $soleil = '0';
  391. /**
  392. * @var int
  393. *
  394. * @ORM\Column(name="products_limite_panier", type="integer", nullable=false)
  395. */
  396. private $limitePanier = '0';
  397. /**
  398. * @var int|null
  399. *
  400. * @ORM\Column(name="products_page", type="integer", nullable=true)
  401. */
  402. private $page;
  403. /**
  404. * @var bool
  405. *
  406. * @ORM\Column(name="products_garantie", type="integer", nullable=false)
  407. */
  408. private $guaranty = '0';
  409. /**
  410. * @var int
  411. *
  412. * @ORM\Column(name="products_cat", type="integer", nullable=true)
  413. */
  414. private $defaultCategoryId = 0;
  415. private $defaultCategory;
  416. /**
  417. * @var int
  418. *
  419. * @ORM\Column(name="products_index", type="integer", nullable=false, options={"default"="1"})
  420. */
  421. private $index = '1';
  422. /**
  423. * @var int
  424. *
  425. * @ORM\Column(name="products_mvente", type="integer", nullable=false, options={"default"="1"})
  426. */
  427. private $mvente = '1';
  428. /**
  429. * @var int
  430. *
  431. * @ORM\Column(name="products_strategique", type="integer", nullable=false)
  432. */
  433. private $strategique = '0';
  434. /**
  435. * @var int
  436. *
  437. * @ORM\Column(name="products_reappro_non", type="integer", nullable=false)
  438. */
  439. private $reapproNon = '0';
  440. /**
  441. * @var int|null
  442. *
  443. * @ORM\Column(name="products_multiple", type="integer", nullable=true)
  444. */
  445. private $multiple = 1;
  446. /**
  447. * @var string|null
  448. *
  449. * @ORM\Column(name="products_origin", type="string", length=3, nullable=true)
  450. */
  451. private $origin = 1;
  452. /**
  453. * @var bool
  454. *
  455. * @ORM\Column(name="products_gift", type="boolean", nullable=true)
  456. */
  457. private $gift = '0';
  458. /**
  459. * @var ?string
  460. *
  461. * @ORM\Column(name="products_type", type="string", length=20, nullable=true)
  462. */
  463. private $type;
  464. /**
  465. * @var int
  466. *
  467. * @ORM\Column(name="products_welcome", type="boolean", nullable=false, options={"default"="1"})
  468. */
  469. private $welcome = '1';
  470. /**
  471. * @var string
  472. *
  473. * @ORM\Column(name="products_code", type="string", length=50, nullable=true)
  474. */
  475. private $code;
  476. /**
  477. * @var boolean
  478. *
  479. * @ORM\Column(name="is_default_variant", type="boolean", nullable=false, options={"default"="0"})
  480. */
  481. private $isDefaultVariant = false;
  482. public function __construct()
  483. {
  484. $this->children = new \Doctrine\Common\Collections\ArrayCollection();
  485. $this->prices = new \Doctrine\Common\Collections\ArrayCollection();
  486. $this->descriptions = new \Doctrine\Common\Collections\ArrayCollection();
  487. }
  488. public function getId() {
  489. return $this->id;
  490. }
  491. public function getChildren() {
  492. return $this->children;
  493. }
  494. public function getParent() {
  495. if(!empty($this->parent) && !empty($this->languageId)){
  496. $this->parent->setLanguageId($this->languageId);
  497. }
  498. return $this->parent;
  499. }
  500. public function getProductModel(): ?ProductModel {
  501. return $this->productModel;
  502. }
  503. public function getTopProductModel(): ?ProductModel {
  504. $tmp = $this->productModel;
  505. while(!empty($tmp)) {
  506. if($tmp->hasParent()) {
  507. $tmp = $tmp->getParent();
  508. }else{
  509. return $tmp;
  510. }
  511. }
  512. return $tmp;
  513. }
  514. public function hasProductModel() {
  515. return !empty($this->productModel);
  516. }
  517. public function getDescriptions() {
  518. return $this->descriptions;
  519. }
  520. public function getAttributes() {
  521. return $this->attributes;
  522. }
  523. public function getAttribute($key) {
  524. if (strpos($key, ',') !== false) {
  525. $codes = array_values(array_filter(array_map('trim', explode(',', $key))));
  526. return array_map(fn($code) => $this->getAttribute($code), $codes);
  527. }
  528. foreach($this->getAttributes() as $attr) {
  529. if($attr->getAttribute()->getCode() == $key)
  530. return $attr;
  531. }
  532. return null;
  533. }
  534. public function getAttributeValue($key) {
  535. $attr = $this->getAttribute($key);
  536. if (is_array($attr)) {
  537. return implode(' / ', array_map(fn($a) => $a ? $a->getDisplayedValue() : '', $attr));
  538. }
  539. return $attr ? $attr->getDisplayedValue() : null;
  540. }
  541. public function getQuantity() {
  542. return $this->quantity;
  543. }
  544. public function getModel() {
  545. return $this->model;
  546. }
  547. public function getModelSAP(): ?string {
  548. return $this->modelSAP;
  549. }
  550. public function getEan(): ?string {
  551. return $this->ean;
  552. }
  553. public function getName() {
  554. if(!empty($this->languageId)){
  555. $description = null;
  556. foreach($this->descriptions as $d){
  557. if($d->getLanguage()->getId()==$this->languageId){
  558. $description = $d;
  559. break;
  560. }
  561. }
  562. }
  563. if(empty($description) && count($this->descriptions)){
  564. $description = $this->descriptions[0];
  565. }
  566. return empty($description)?'':$description->getName();
  567. }
  568. public function getHeadingTitle() {
  569. if(!empty($this->languageId)){
  570. $description = null;
  571. foreach($this->descriptions as $d){
  572. if($d->getLanguage()->getId()==$this->languageId){
  573. $description = $d;
  574. break;
  575. }
  576. }
  577. }
  578. if(empty($description) && count($this->descriptions)){
  579. $description = $this->descriptions[0];
  580. }
  581. return empty($description)?'':$description->getHeadingTitle();
  582. }
  583. public function getDeclinaison() {
  584. $output = '';
  585. if($this->parent){
  586. $name = $this->getName();
  587. $output = substr($name, strlen($this->getParent()->getName())+2, strlen($name));
  588. }
  589. return $output;
  590. }
  591. public function getUrl() {
  592. if(!empty($this->languageId)){
  593. $description = null;
  594. foreach($this->descriptions as $d){
  595. if($d->getLanguage()->getId()==$this->languageId){
  596. $description = $d;
  597. break;
  598. }
  599. }
  600. }
  601. if(empty($description) && count($this->descriptions)){
  602. $description = $this->descriptions[0];
  603. }
  604. return empty($description)?'':$description->getUrl();
  605. }
  606. public function getDescription() {
  607. if(!empty($this->languageId)){
  608. $description = null;
  609. foreach($this->descriptions as $d){
  610. if($d->getLanguage()->getId()==$this->languageId){
  611. $description = $d;
  612. break;
  613. }
  614. }
  615. }
  616. if(empty($description) && count($this->descriptions)){
  617. $description = $this->descriptions[0];
  618. }
  619. return empty($description)?'':$description->getDescription();
  620. }
  621. public function getExtraFields() {
  622. return $this->extraFields;
  623. }
  624. public function getProductDescription($lang='fr') : ?ProductDescription {
  625. foreach($this->getDescriptions() as $desc){
  626. if($desc->getLanguage()->getCode() == $lang)
  627. return $desc;
  628. }
  629. return null;
  630. }
  631. public function getPicture() {
  632. return empty($this->pictures)?null:$this->pictures[0];
  633. }
  634. public function getPictures() {
  635. return $this->pictures;
  636. }
  637. public function getPicturesUrl() {
  638. $output = [];
  639. foreach($this->pictures as $picture){
  640. $output[] = $picture->getUrl();
  641. }
  642. return $output;
  643. }
  644. public function getImage($absolute=false) {
  645. if($absolute)
  646. return 'https://www.dogcat.com/professionnel/images/'.$this->image;
  647. return $this->image;
  648. }
  649. public function getImageMed($absolute=false) {
  650. if($absolute)
  651. return 'https://www.dogcat.com/professionnel/images/'.$this->imageMed;
  652. return $this->imageMed;
  653. }
  654. public function getImageLrg($absolute=false) {
  655. if($absolute)
  656. return 'https://www.dogcat.com/professionnel/images/'.$this->imageLrg;
  657. return $this->imageLrg;
  658. }
  659. public function getImageSm1() {
  660. return $this->imageSm1;
  661. }
  662. public function getImageXl1() {
  663. return $this->imageXl1;
  664. }
  665. public function getImageSm2() {
  666. return $this->imageSm2;
  667. }
  668. public function getImageXl2() {
  669. return $this->imageXl2;
  670. }
  671. public function getImageSm3() {
  672. return $this->imageSm3;
  673. }
  674. public function getImageXl3() {
  675. return $this->imageXl3;
  676. }
  677. public function getImageSm4() {
  678. return $this->imageSm4;
  679. }
  680. public function getImageXl4() {
  681. return $this->imageXl4;
  682. }
  683. public function getImageSm5() {
  684. return $this->imageSm5;
  685. }
  686. public function getImageXl5() {
  687. return $this->imageXl5;
  688. }
  689. public function getImageSm6() {
  690. return $this->imageSm6;
  691. }
  692. public function getImageXl6() {
  693. return $this->imageXl6;
  694. }
  695. public function getPrice($withEcotax = false, ?PriceGroup $priceGroup = null, $withVat = false) {
  696. $price = $this->price;
  697. $tax = 0;
  698. if($this->isGift()) {
  699. return 0;
  700. }
  701. if($priceGroup) {
  702. $price = $this->getPriceByGroup($priceGroup);
  703. }
  704. if($withVat) {
  705. $tax = Encoder::roundPrice($price*self::VAT_RATE);
  706. }
  707. if($withEcotax === true){
  708. return Encoder::roundPrice($price) + Encoder::roundPrice($this->getEcotax()) + $tax;
  709. }
  710. return Encoder::roundPrice($price) + $tax;
  711. }
  712. public function getPrices() {
  713. return $this->prices;
  714. }
  715. public function getPriceByGroup(PriceGroup $priceGroup) {
  716. foreach($this->prices as $price) {
  717. if($price->getGroup() == $priceGroup) {
  718. return $price->getPrice();
  719. }else{
  720. dump($price->getGroup());
  721. dump($priceGroup);
  722. }
  723. }
  724. // throw new \App\Exception\PriceGroupException();
  725. }
  726. public function hasPrice(PriceGroup $priceGroup) {
  727. if($this->hasChildren()) {
  728. foreach($this->getChildren() as $child) {
  729. if($child->hasPrice($priceGroup))
  730. return true;
  731. }
  732. }else{
  733. foreach($this->prices as $price) {
  734. if($price->getGroup() == $priceGroup)
  735. return true;
  736. }
  737. }
  738. return false;
  739. }
  740. public function getEcotax() {
  741. return $this->ecotax;
  742. }
  743. public function getFromProduct(?PriceGroup $priceGroup = null) {
  744. $tmp = null;
  745. $from = null;
  746. foreach($this->children as $product){
  747. if(!$product->isStopped() && $product->isActive()){
  748. if(!empty($priceGroup) && !$product->hasPrice($priceGroup)) {
  749. continue;
  750. }
  751. if(is_null($tmp) || ($tmp>$product->getPrice(false, $priceGroup, false))){
  752. $tmp = $product->getPrice(false, $priceGroup, false);
  753. $from = $product;
  754. }
  755. }
  756. }
  757. return $from;
  758. }
  759. public function getFromPrice($withEcotax = true, ?PriceGroup $priceGroup = null, $withVat = false) {
  760. $tmp = null;
  761. foreach($this->children as $product){
  762. if(!$product->isStopped() && $product->isActive()){
  763. if(!empty($priceGroup) && !$product->hasPrice($priceGroup)) {
  764. continue;
  765. }
  766. if(is_null($tmp) || ($tmp>$product->getPrice($withEcotax, $priceGroup, $withVat))){
  767. $tmp = $product->getPrice($withEcotax, $priceGroup, $withVat);
  768. }
  769. }
  770. }
  771. return is_null($tmp)?0:$tmp;
  772. }
  773. public function getSoleilPrice($withEcotax = true) {
  774. if($this->isNonSoleil()){
  775. return Encoder::roundPrice($this->price * 0.9)+($withEcotax?$this->getEcotax():0);
  776. }else if($this->isSoleil()){
  777. return Encoder::roundPrice($this->price * 0.75)+($withEcotax?$this->getEcotax():0);
  778. }
  779. return Encoder::roundPrice($this->price)+($withEcotax?$this->getEcotax():0);
  780. }
  781. public function getDateAdded() {
  782. return $this->dateAdded;
  783. }
  784. public function getLastModified() {
  785. return $this->lastModified;
  786. }
  787. public function getDateAvailable() {
  788. return $this->dateAvailable;
  789. }
  790. public function getWeight() {
  791. return $this->weight;
  792. }
  793. public function getStatus() {
  794. return $this->status;
  795. }
  796. public function getTaxClass() {
  797. return $this->taxClass;
  798. }
  799. public function getManufacturer() {
  800. return $this->manufacturer;
  801. }
  802. public function getOrdered() {
  803. return $this->ordered;
  804. }
  805. public function getPrice1() {
  806. return $this->price1;
  807. }
  808. public function getPrice2() {
  809. return $this->price2;
  810. }
  811. public function getPrice3() {
  812. return $this->price3;
  813. }
  814. public function getPrice4() {
  815. return $this->price4;
  816. }
  817. public function getPrice5() {
  818. return $this->price5;
  819. }
  820. public function getPrice6() {
  821. return $this->price6;
  822. }
  823. public function getPrice7() {
  824. return $this->price7;
  825. }
  826. public function getPrice8() {
  827. return $this->price8;
  828. }
  829. public function getPrice9() {
  830. return $this->price9;
  831. }
  832. public function getPrice10() {
  833. return $this->price10;
  834. }
  835. public function getPrice11() {
  836. return $this->price11;
  837. }
  838. public function getPrice1Qty() {
  839. return $this->price1Qty;
  840. }
  841. public function getPrice2Qty() {
  842. return $this->price2Qty;
  843. }
  844. public function getPrice3Qty() {
  845. return $this->price3Qty;
  846. }
  847. public function getPrice4Qty() {
  848. return $this->price4Qty;
  849. }
  850. public function getPrice5Qty() {
  851. return $this->price5Qty;
  852. }
  853. public function getPrice6Qty() {
  854. return $this->price6Qty;
  855. }
  856. public function getPrice7Qty() {
  857. return $this->price7Qty;
  858. }
  859. public function getPrice8Qty() {
  860. return $this->price8Qty;
  861. }
  862. public function getPrice9Qty() {
  863. return $this->price9Qty;
  864. }
  865. public function getPrice10Qty() {
  866. return $this->price10Qty;
  867. }
  868. public function getPrice11Qty() {
  869. return $this->price11Qty;
  870. }
  871. public function getProductsQtyBlocks() {
  872. return $this->productsQtyBlocks;
  873. }
  874. public function getSoleil() {
  875. return $this->soleil;
  876. }
  877. public function getLimitePanier() {
  878. return $this->limitePanier;
  879. }
  880. public function getPage() {
  881. return $this->page;
  882. }
  883. public function getGuaranty() {
  884. return $this->guaranty;
  885. }
  886. public function getDefaultCategoryId() {
  887. return $this->defaultCategoryId;
  888. }
  889. public function getDefaultCategory() {
  890. return $this->defaultCategory;
  891. }
  892. public function getIndex() {
  893. if(empty($this->index))
  894. return 0;
  895. return $this->index > 0 ? 1 : 0;
  896. }
  897. public function getMvente() {
  898. return $this->mvente;
  899. }
  900. public function getStrategique() {
  901. return $this->strategique;
  902. }
  903. public function getReapproNon() {
  904. return $this->reapproNon;
  905. }
  906. public function getMultiple() {
  907. return empty($this->multiple)?1:$this->multiple;
  908. }
  909. public function getOrigin() {
  910. return $this->origin;
  911. }
  912. public function getReference() {
  913. return $this->getModel();
  914. }
  915. public function getGift() {
  916. return is_null($this->gift) ? 0 : $this->gift;
  917. }
  918. public function getType(): ?string {
  919. return empty($this->type) ? self::TYPE_PRODUCT : $this->type;
  920. }
  921. public function getCountryRestrictions() {
  922. return $this->countryRestrictions;
  923. }
  924. public function getRestrictedCountries() {
  925. $output = [];
  926. foreach($this->getCountryRestrictions() as $rc) {
  927. $output[] = $rc->getCountry();
  928. }
  929. return $output;
  930. }
  931. public function getWelcome(): int {
  932. return $this->welcome;
  933. }
  934. public function getCode(): ?string {
  935. return $this->code;
  936. }
  937. public function getIsDefaultVariant() {
  938. return $this->isDefaultVariant;
  939. }
  940. // public function getUrl($absolute=false) {
  941. // if(!empty($this->parent))
  942. // return $this->getParent()->getUrl($absolute);
  943. // if(!empty($this->languageId)){
  944. // $language = $this->languageId==4?'fr':'en';
  945. // }
  946. // if(empty($language)){
  947. // $language = 'fr';
  948. // }
  949. //// $url = '/professionnel/product_info.php?products_id='.$this->getId().'&language='.$language;
  950. //// if($absolute)
  951. //// return 'https://www.dogcat.com'.$url;
  952. // return $this->getUrl();
  953. // }
  954. public function setId($id) {
  955. $this->id = $id;
  956. }
  957. public function setAttributes($attributes) {
  958. $this->attributes = $attributes;
  959. }
  960. public function setChildren($children) {
  961. $this->children = $children;
  962. }
  963. public function setProductModel(?ProductModel $productModel): void {
  964. $this->productModel = $productModel;
  965. }
  966. public function setParent($parent) {
  967. $this->parent = $parent;
  968. }
  969. public function setDescriptions($descriptions) {
  970. $this->descriptions = $descriptions;
  971. }
  972. public function setExtraFields($extraFields): void {
  973. $this->extraFields = $extraFields;
  974. }
  975. public function setQuantity($quantity) {
  976. $this->quantity = $quantity;
  977. }
  978. public function setModel($model) {
  979. $this->model = $model;
  980. }
  981. public function setModelSAP(?string $modelSAP): void {
  982. $this->modelSAP = $modelSAP;
  983. }
  984. public function setEan(?string $ean): void {
  985. $this->ean = $ean;
  986. }
  987. public function setImage($image) {
  988. $this->image = $image;
  989. }
  990. public function setImageMed($imageMed) {
  991. $this->imageMed = $imageMed;
  992. }
  993. public function setImageLrg($imageLrg) {
  994. $this->imageLrg = $imageLrg;
  995. }
  996. public function setImageSm1($imageSm1) {
  997. $this->imageSm1 = $imageSm1;
  998. }
  999. public function setImageXl1($imageXl1) {
  1000. $this->imageXl1 = $imageXl1;
  1001. }
  1002. public function setImageSm2($imageSm2) {
  1003. $this->imageSm2 = $imageSm2;
  1004. }
  1005. public function setImageXl2($imageXl2) {
  1006. $this->imageXl2 = $imageXl2;
  1007. }
  1008. public function setImageSm3($imageSm3) {
  1009. $this->imageSm3 = $imageSm3;
  1010. }
  1011. public function setImageXl3($imageXl3) {
  1012. $this->imageXl3 = $imageXl3;
  1013. }
  1014. public function setImageSm4($imageSm4) {
  1015. $this->imageSm4 = $imageSm4;
  1016. }
  1017. public function setImageXl4($imageXl4) {
  1018. $this->imageXl4 = $imageXl4;
  1019. }
  1020. public function setImageSm5($imageSm5) {
  1021. $this->imageSm5 = $imageSm5;
  1022. }
  1023. public function setImageXl5($imageXl5) {
  1024. $this->imageXl5 = $imageXl5;
  1025. }
  1026. public function setImageSm6($imageSm6) {
  1027. $this->imageSm6 = $imageSm6;
  1028. }
  1029. public function setImageXl6($imageXl6) {
  1030. $this->imageXl6 = $imageXl6;
  1031. }
  1032. public function setPrice($price) {
  1033. $this->price = $price;
  1034. }
  1035. public function setEcotax( $ecotax) {
  1036. $this->ecotax = $ecotax;
  1037. }
  1038. public function setDateAdded(\DateTime $dateAdded) {
  1039. $this->dateAdded = $dateAdded;
  1040. }
  1041. public function setLastModified(\DateTime $lastModified) {
  1042. $this->lastModified = $lastModified;
  1043. }
  1044. public function setDateAvailable(\DateTime $dateAvailable) {
  1045. $this->dateAvailable = $dateAvailable;
  1046. }
  1047. public function setWeight($weight) {
  1048. $this->weight = $weight;
  1049. }
  1050. public function setStatus($status) {
  1051. $this->status = $status;
  1052. }
  1053. public function setTaxClass($taxClass) {
  1054. $this->taxClass = $taxClass;
  1055. }
  1056. public function setManufacturer($manufacturer) {
  1057. $this->manufacturer = $manufacturer;
  1058. }
  1059. public function setOrdered($ordered) {
  1060. $this->ordered = $ordered;
  1061. }
  1062. public function setParentId($parentId) {
  1063. $this->parentId = $parentId;
  1064. }
  1065. public function setPrice1($price1) {
  1066. $this->price1 = $price1;
  1067. }
  1068. public function setPrice2($price2) {
  1069. $this->price2 = $price2;
  1070. }
  1071. public function setPrice3($price3) {
  1072. $this->price3 = $price3;
  1073. }
  1074. public function setPrice4($price4) {
  1075. $this->price4 = $price4;
  1076. }
  1077. public function setPrice5($price5) {
  1078. $this->price5 = $price5;
  1079. }
  1080. public function setPrice6($price6) {
  1081. $this->price6 = $price6;
  1082. }
  1083. public function setPrice7($price7) {
  1084. $this->price7 = $price7;
  1085. }
  1086. public function setPrice8($price8) {
  1087. $this->price8 = $price8;
  1088. }
  1089. public function setPrice9($price9) {
  1090. $this->price9 = $price9;
  1091. }
  1092. public function setPrice10($price10) {
  1093. $this->price10 = $price10;
  1094. }
  1095. public function setPrice11($price11) {
  1096. $this->price11 = $price11;
  1097. }
  1098. public function setPrice1Qty($price1Qty) {
  1099. $this->price1Qty = $price1Qty;
  1100. }
  1101. public function setPrice2Qty($price2Qty) {
  1102. $this->price2Qty = $price2Qty;
  1103. }
  1104. public function setPrice3Qty($price3Qty) {
  1105. $this->price3Qty = $price3Qty;
  1106. }
  1107. public function setPrice4Qty($price4Qty) {
  1108. $this->price4Qty = $price4Qty;
  1109. }
  1110. public function setPrice5Qty($price5Qty) {
  1111. $this->price5Qty = $price5Qty;
  1112. }
  1113. public function setPrice6Qty($price6Qty) {
  1114. $this->price6Qty = $price6Qty;
  1115. }
  1116. public function setPrice7Qty($price7Qty) {
  1117. $this->price7Qty = $price7Qty;
  1118. }
  1119. public function setPrice8Qty($price8Qty) {
  1120. $this->price8Qty = $price8Qty;
  1121. }
  1122. public function setPrice9Qty($price9Qty) {
  1123. $this->price9Qty = $price9Qty;
  1124. }
  1125. public function setPrice10Qty($price10Qty) {
  1126. $this->price10Qty = $price10Qty;
  1127. }
  1128. public function setPrice11Qty($price11Qty) {
  1129. $this->price11Qty = $price11Qty;
  1130. }
  1131. public function setProductsQtyBlocks($productsQtyBlocks) {
  1132. $this->productsQtyBlocks = $productsQtyBlocks;
  1133. }
  1134. public function setSoleil($soleil) {
  1135. $this->soleil = $soleil;
  1136. }
  1137. public function setLimitePanier($limitePanier) {
  1138. $this->limitePanier = $limitePanier;
  1139. }
  1140. public function setPage($page) {
  1141. $this->page = $page;
  1142. }
  1143. public function setGuaranty($guaranty) {
  1144. $this->guaranty = $guaranty;
  1145. }
  1146. public function setDefaultCategoryId($defaultCategoryId) {
  1147. $this->defaultCategoryId = $defaultCategoryId;
  1148. }
  1149. public function setIndex($index) {
  1150. $this->index = $index;
  1151. }
  1152. public function setMvente($mvente) {
  1153. $this->mvente = $mvente;
  1154. }
  1155. public function setStrategique($strategique) {
  1156. $this->strategique = $strategique;
  1157. }
  1158. public function setReapproNon($reapproNon) {
  1159. $this->reapproNon = $reapproNon;
  1160. }
  1161. public function setMultiple($multiple) {
  1162. return $this->multiple = $multiple;
  1163. }
  1164. public function setOrigin($origin) {
  1165. $this->origin = $origin;
  1166. }
  1167. public function setPrices($prices) {
  1168. $this->prices = $prices;
  1169. }
  1170. public function setGift($gift) {
  1171. $this->gift = $gift;
  1172. }
  1173. public function setType(?string $type): void {
  1174. $this->type = $type;
  1175. }
  1176. public function setCountryRestrictions($countryRestrictions): void {
  1177. $this->countryRestrictions = $countryRestrictions;
  1178. }
  1179. public function setWelcome(int $welcome): void {
  1180. $this->welcome = $welcome;
  1181. }
  1182. public function setCode(string $code): void {
  1183. $this->code = $code;
  1184. }
  1185. public function setIsDefaultVariant($isDefaultVariant): void {
  1186. $this->isDefaultVariant = $isDefaultVariant;
  1187. }
  1188. public function getCaracteristics() {
  1189. $output = [];
  1190. foreach($this->getAttributes() as $attr) {
  1191. if($attr->getAttribute()->getShowInCaracteristic())
  1192. $output[] = $attr;
  1193. }
  1194. return $output;
  1195. }
  1196. public function hasWelcomeDiscount() {
  1197. return !empty($this->welcome);
  1198. }
  1199. public function isMadeInFrance() {
  1200. if(!empty($this->origin))
  1201. return strtoupper($this->origin) == 'FR';
  1202. if($this->hasChildren()){
  1203. foreach($this->children as $child){
  1204. if(!$child->isMadeInFrance()){
  1205. return false;
  1206. }
  1207. }
  1208. return true;
  1209. }
  1210. return false;
  1211. }
  1212. public function isAvailable() {
  1213. return $this->quantity > 0;
  1214. }
  1215. public function isDefaultVariant() {
  1216. return !empty($this->isDefaultVariant);
  1217. }
  1218. public function hasRestrictionForCountry($country) {
  1219. if($country){
  1220. if($this->countryRestrictions){
  1221. foreach($this->getCountryRestrictions() as $restriction){
  1222. if($restriction->getCountry() == $country){
  1223. return true;
  1224. }
  1225. }
  1226. }
  1227. }
  1228. return false;
  1229. }
  1230. public function isAvailableForCountry($country) {
  1231. if($country && $this->countryRestrictions){
  1232. foreach($this->getCountryRestrictions() as $restriction) {
  1233. if($restriction->getCountry() == $country){
  1234. return false;
  1235. }
  1236. }
  1237. }
  1238. if($country && $this->getManufacturer()){
  1239. return $this->getManufacturer()->isAvailableForCountry($country);
  1240. }
  1241. return true;
  1242. }
  1243. public function isStopped() {
  1244. return $this->quantity == -800;
  1245. }
  1246. public function isActive() {
  1247. return $this->getStatus() == 1;
  1248. }
  1249. public function isGift() {
  1250. return $this->getGift() == 1;
  1251. }
  1252. public function isSoleil() {
  1253. return $this->soleil == 1;
  1254. }
  1255. public function isNonSoleil() {
  1256. return $this->soleil == 0;
  1257. }
  1258. public function isVivog() {
  1259. return $this->isSoleil() || $this->isNonSoleil();
  1260. }
  1261. public function isParent() {
  1262. return empty($this->parent);
  1263. }
  1264. public function isProduct() {
  1265. return $this->getType() == self::TYPE_PRODUCT;
  1266. }
  1267. public function hasChildren() {
  1268. return count($this->children)>0;
  1269. }
  1270. public function hasSoleilPrice() {
  1271. return $this->isSoleil() || $this->isNonSoleil();
  1272. }
  1273. public function hasParent() {
  1274. return !empty($this->parent);
  1275. }
  1276. public function hasSoleil() {
  1277. if($this->hasChildren()){
  1278. foreach($this->children as $child){
  1279. if($child->isSoleil()){
  1280. return true;
  1281. }
  1282. }
  1283. return false;
  1284. }
  1285. return $this->isSoleil();
  1286. }
  1287. public function hasNonSoleil() {
  1288. if($this->hasChildren()){
  1289. foreach($this->children as $child){
  1290. if($child->isNonSoleil()){
  1291. return true;
  1292. }
  1293. }
  1294. return false;
  1295. }
  1296. return $this->isNonSoleil();
  1297. }
  1298. public function addPrice(ProductPrice $productPrice): self
  1299. {
  1300. if (!$this->prices->contains($productPrice)) {
  1301. $this->prices->add($productPrice);
  1302. $productPrice->setProduct($this);
  1303. }
  1304. return $this;
  1305. }
  1306. public function removePrice(ProductPrice $productPrice): self
  1307. {
  1308. if ($this->prices->removeElement($productPrice)) {
  1309. if ($productPrice->getProduct() === $this) {
  1310. $productPrice->setProduct(null);
  1311. }
  1312. }
  1313. return $this;
  1314. }
  1315. public function toArray() : array{
  1316. $parent = $this->getParent();
  1317. $manufacturer = $this->getManufacturer();
  1318. $output = [
  1319. 'id' => $this->getId(),
  1320. 'model' => $this->getModel(),
  1321. 'name' => $this->getName(),
  1322. 'description' => $this->getDescription(),
  1323. 'quantity' => $this->getQuantity(),
  1324. 'image' => $this->getImage(),
  1325. 'price' => $this->getPrice(),
  1326. 'dateAdded' => $this->getDateAdded(),
  1327. 'lastModified' => $this->getLastModified(),
  1328. 'dateAvailable' => $this->getDateAvailable(),
  1329. 'weight' => $this->getWeight(),
  1330. 'status' => $this->getStatus()?1:0,
  1331. 'taxClass ' => $this->getTaxClass()?$this->getTaxClass()->toArray():0,
  1332. 'ordered' => $this->getOrdered(),
  1333. 'parent' => empty($parent)?null:$parent->toArray(),
  1334. 'manufacturer' => empty($manufacturer)?null:$manufacturer->toArray(),
  1335. 'productsQtyBlocks' => $this->getProductsQtyBlocks(),
  1336. 'soleil' => $this->getSoleil(),
  1337. 'nbChildren' => count($this->getChildren()),
  1338. 'limitePanier' => $this->getLimitePanier(),
  1339. 'page' => $this->getPage(),
  1340. 'garantie' => $this->getGuaranty(),
  1341. 'defaultCategoryId' => $this->getDefaultCategoryId(),
  1342. 'index' => $this->getIndex(),
  1343. 'mvente' => $this->getMvente(),
  1344. 'strategique' => $this->getStrategique(),
  1345. 'reapproNon' => $this->getReapproNon(),
  1346. 'multiple' => $this->getMultiple(),
  1347. ];
  1348. return $output;
  1349. }
  1350. public function toJson() {
  1351. return json_encode($this->toArray());
  1352. }
  1353. /**
  1354. * @todo Remove once 0 values in the table are converted to NULL.
  1355. */
  1356. public function loadDescriptions(\Doctrine\ORM\EntityManager $em)
  1357. {
  1358. $conn = $em->getConnection();
  1359. $sql = $conn->prepare('select * from products_description where products_id = '.$this->getId());
  1360. $sql->execute();
  1361. $rows = $sql->fetchAll();
  1362. $descriptions = [];
  1363. foreach($rows as $desc){
  1364. $description = new ProductDescription();
  1365. $description->setName($desc['products_name']);
  1366. $description->setDescription($desc['products_description']);
  1367. $description->setLanguage($em->getRepository('App:Language')->find($desc['language_id']==1?1:4));
  1368. $descriptions[] = $description;
  1369. }
  1370. $this->setDescriptions($descriptions);
  1371. }
  1372. public static function getSearchIndex($lang = 'fr')
  1373. {
  1374. return self::SEARCH_INDICES[$lang];
  1375. }
  1376. /**
  1377. * @ORM\PrePersist
  1378. */
  1379. public function prePersit(\Doctrine\ORM\Event\LifecycleEventArgs $args) {
  1380. $this->setDateAdded(new \DateTime);
  1381. }
  1382. /**
  1383. * @ORM\PrePersist
  1384. * @ORM\PreUpdate
  1385. */
  1386. public function preUpdate(\Doctrine\ORM\Event\LifecycleEventArgs $args) {
  1387. if(is_null($this->weight))
  1388. $this->setWeight(0);
  1389. }
  1390. }