src/Entity/Product.php line 653

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