src/Entity/Product.php line 695

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. if($this->attributes) {
  529. foreach($this->getAttributes() as $attr) {
  530. if($attr->getAttribute()->getCode() == $key)
  531. return $attr;
  532. }
  533. }
  534. return null;
  535. }
  536. public function getAttributeValue($key) {
  537. $attr = $this->getAttribute($key);
  538. if (is_array($attr)) {
  539. return implode(' / ', array_map(fn($a) => $a ? $a->getDisplayedValue() : '', $attr));
  540. }
  541. return $attr ? $attr->getDisplayedValue() : null;
  542. }
  543. public function getQuantity() {
  544. return $this->quantity;
  545. }
  546. public function getModel() {
  547. return $this->model;
  548. }
  549. public function getModelSAP(): ?string {
  550. return $this->modelSAP;
  551. }
  552. public function getEan(): ?string {
  553. return $this->ean;
  554. }
  555. public function getName() {
  556. if(!empty($this->languageId)){
  557. $description = null;
  558. foreach($this->descriptions as $d){
  559. if($d->getLanguage()->getId()==$this->languageId){
  560. $description = $d;
  561. break;
  562. }
  563. }
  564. }
  565. if(empty($description) && count($this->descriptions)){
  566. $description = $this->descriptions[0];
  567. }
  568. return empty($description)?'':$description->getName();
  569. }
  570. public function getHeadingTitle() {
  571. if(!empty($this->languageId)){
  572. $description = null;
  573. foreach($this->descriptions as $d){
  574. if($d->getLanguage()->getId()==$this->languageId){
  575. $description = $d;
  576. break;
  577. }
  578. }
  579. }
  580. if(empty($description) && count($this->descriptions)){
  581. $description = $this->descriptions[0];
  582. }
  583. return empty($description)?'':$description->getHeadingTitle();
  584. }
  585. public function getDeclinaison() {
  586. $output = '';
  587. if($this->parent){
  588. $name = $this->getName();
  589. $output = substr($name, strlen($this->getParent()->getName())+2, strlen($name));
  590. }
  591. return $output;
  592. }
  593. public function getUrl() {
  594. if(!empty($this->languageId)){
  595. $description = null;
  596. foreach($this->descriptions as $d){
  597. if($d->getLanguage()->getId()==$this->languageId){
  598. $description = $d;
  599. break;
  600. }
  601. }
  602. }
  603. if(empty($description) && count($this->descriptions)){
  604. $description = $this->descriptions[0];
  605. }
  606. return empty($description)?'':$description->getUrl();
  607. }
  608. public function getDescription() {
  609. if(!empty($this->languageId)){
  610. $description = null;
  611. foreach($this->descriptions as $d){
  612. if($d->getLanguage()->getId()==$this->languageId){
  613. $description = $d;
  614. break;
  615. }
  616. }
  617. }
  618. if(empty($description) && count($this->descriptions)){
  619. $description = $this->descriptions[0];
  620. }
  621. return empty($description)?'':$description->getDescription();
  622. }
  623. public function getExtraFields() {
  624. return $this->extraFields;
  625. }
  626. public function getProductDescription($lang='fr') : ?ProductDescription {
  627. foreach($this->getDescriptions() as $desc){
  628. if($desc->getLanguage()->getCode() == $lang)
  629. return $desc;
  630. }
  631. return null;
  632. }
  633. public function getPicture() {
  634. return empty($this->pictures)?null:$this->pictures[0];
  635. }
  636. public function getPictures() {
  637. return $this->pictures;
  638. }
  639. public function getPicturesUrl() {
  640. $output = [];
  641. foreach($this->pictures as $picture){
  642. $output[] = $picture->getUrl();
  643. }
  644. return $output;
  645. }
  646. public function getImage($absolute=false) {
  647. if($absolute)
  648. return 'https://www.dogcat.com/professionnel/images/'.$this->image;
  649. return $this->image;
  650. }
  651. public function getImageMed($absolute=false) {
  652. if($absolute)
  653. return 'https://www.dogcat.com/professionnel/images/'.$this->imageMed;
  654. return $this->imageMed;
  655. }
  656. public function getImageLrg($absolute=false) {
  657. if($absolute)
  658. return 'https://www.dogcat.com/professionnel/images/'.$this->imageLrg;
  659. return $this->imageLrg;
  660. }
  661. public function getImageSm1() {
  662. return $this->imageSm1;
  663. }
  664. public function getImageXl1() {
  665. return $this->imageXl1;
  666. }
  667. public function getImageSm2() {
  668. return $this->imageSm2;
  669. }
  670. public function getImageXl2() {
  671. return $this->imageXl2;
  672. }
  673. public function getImageSm3() {
  674. return $this->imageSm3;
  675. }
  676. public function getImageXl3() {
  677. return $this->imageXl3;
  678. }
  679. public function getImageSm4() {
  680. return $this->imageSm4;
  681. }
  682. public function getImageXl4() {
  683. return $this->imageXl4;
  684. }
  685. public function getImageSm5() {
  686. return $this->imageSm5;
  687. }
  688. public function getImageXl5() {
  689. return $this->imageXl5;
  690. }
  691. public function getImageSm6() {
  692. return $this->imageSm6;
  693. }
  694. public function getImageXl6() {
  695. return $this->imageXl6;
  696. }
  697. public function getPrice($withEcotax = false, ?PriceGroup $priceGroup = null, $withVat = false) {
  698. $price = $this->price;
  699. $tax = 0;
  700. if($this->isGift()) {
  701. return 0;
  702. }
  703. if($priceGroup) {
  704. $price = $this->getPriceByGroup($priceGroup);
  705. }
  706. if($withVat) {
  707. $tax = Encoder::roundPrice($price*self::VAT_RATE);
  708. }
  709. if($withEcotax === true){
  710. return Encoder::roundPrice($price) + Encoder::roundPrice($this->getEcotax()) + $tax;
  711. }
  712. return Encoder::roundPrice($price) + $tax;
  713. }
  714. public function getPrices() {
  715. return $this->prices;
  716. }
  717. public function getPriceByGroup(PriceGroup $priceGroup) {
  718. foreach($this->prices as $price) {
  719. if($price->getGroup() == $priceGroup) {
  720. return $price->getPrice();
  721. }else{
  722. dump($price->getGroup());
  723. dump($priceGroup);
  724. }
  725. }
  726. // throw new \App\Exception\PriceGroupException();
  727. }
  728. public function hasPrice(PriceGroup $priceGroup) {
  729. if($this->hasChildren()) {
  730. foreach($this->getChildren() as $child) {
  731. if($child->hasPrice($priceGroup))
  732. return true;
  733. }
  734. }else{
  735. foreach($this->prices as $price) {
  736. if($price->getGroup() == $priceGroup)
  737. return true;
  738. }
  739. }
  740. return false;
  741. }
  742. public function getEcotax() {
  743. return $this->ecotax;
  744. }
  745. public function getFromProduct(?PriceGroup $priceGroup = null) {
  746. $tmp = null;
  747. $from = null;
  748. foreach($this->children as $product){
  749. if(!$product->isStopped() && $product->isActive()){
  750. if(!empty($priceGroup) && !$product->hasPrice($priceGroup)) {
  751. continue;
  752. }
  753. if(is_null($tmp) || ($tmp>$product->getPrice(false, $priceGroup, false))){
  754. $tmp = $product->getPrice(false, $priceGroup, false);
  755. $from = $product;
  756. }
  757. }
  758. }
  759. return $from;
  760. }
  761. public function getFromPrice($withEcotax = true, ?PriceGroup $priceGroup = null, $withVat = false) {
  762. $tmp = null;
  763. foreach($this->children as $product){
  764. if(!$product->isStopped() && $product->isActive()){
  765. if(!empty($priceGroup) && !$product->hasPrice($priceGroup)) {
  766. continue;
  767. }
  768. if(is_null($tmp) || ($tmp>$product->getPrice($withEcotax, $priceGroup, $withVat))){
  769. $tmp = $product->getPrice($withEcotax, $priceGroup, $withVat);
  770. }
  771. }
  772. }
  773. return is_null($tmp)?0:$tmp;
  774. }
  775. public function getSoleilPrice($withEcotax = true) {
  776. if($this->isNonSoleil()){
  777. return Encoder::roundPrice($this->price * 0.9)+($withEcotax?$this->getEcotax():0);
  778. }else if($this->isSoleil()){
  779. return Encoder::roundPrice($this->price * 0.75)+($withEcotax?$this->getEcotax():0);
  780. }
  781. return Encoder::roundPrice($this->price)+($withEcotax?$this->getEcotax():0);
  782. }
  783. public function getDateAdded() {
  784. return $this->dateAdded;
  785. }
  786. public function getLastModified() {
  787. return $this->lastModified;
  788. }
  789. public function getDateAvailable() {
  790. return $this->dateAvailable;
  791. }
  792. public function getWeight() {
  793. return $this->weight;
  794. }
  795. public function getStatus() {
  796. return $this->status;
  797. }
  798. public function getTaxClass() {
  799. return $this->taxClass;
  800. }
  801. public function getManufacturer() {
  802. return $this->manufacturer;
  803. }
  804. public function getOrdered() {
  805. return $this->ordered;
  806. }
  807. public function getPrice1() {
  808. return $this->price1;
  809. }
  810. public function getPrice2() {
  811. return $this->price2;
  812. }
  813. public function getPrice3() {
  814. return $this->price3;
  815. }
  816. public function getPrice4() {
  817. return $this->price4;
  818. }
  819. public function getPrice5() {
  820. return $this->price5;
  821. }
  822. public function getPrice6() {
  823. return $this->price6;
  824. }
  825. public function getPrice7() {
  826. return $this->price7;
  827. }
  828. public function getPrice8() {
  829. return $this->price8;
  830. }
  831. public function getPrice9() {
  832. return $this->price9;
  833. }
  834. public function getPrice10() {
  835. return $this->price10;
  836. }
  837. public function getPrice11() {
  838. return $this->price11;
  839. }
  840. public function getPrice1Qty() {
  841. return $this->price1Qty;
  842. }
  843. public function getPrice2Qty() {
  844. return $this->price2Qty;
  845. }
  846. public function getPrice3Qty() {
  847. return $this->price3Qty;
  848. }
  849. public function getPrice4Qty() {
  850. return $this->price4Qty;
  851. }
  852. public function getPrice5Qty() {
  853. return $this->price5Qty;
  854. }
  855. public function getPrice6Qty() {
  856. return $this->price6Qty;
  857. }
  858. public function getPrice7Qty() {
  859. return $this->price7Qty;
  860. }
  861. public function getPrice8Qty() {
  862. return $this->price8Qty;
  863. }
  864. public function getPrice9Qty() {
  865. return $this->price9Qty;
  866. }
  867. public function getPrice10Qty() {
  868. return $this->price10Qty;
  869. }
  870. public function getPrice11Qty() {
  871. return $this->price11Qty;
  872. }
  873. public function getProductsQtyBlocks() {
  874. return $this->productsQtyBlocks;
  875. }
  876. public function getSoleil() {
  877. return $this->soleil;
  878. }
  879. public function getLimitePanier() {
  880. return $this->limitePanier;
  881. }
  882. public function getPage() {
  883. return $this->page;
  884. }
  885. public function getGuaranty() {
  886. return $this->guaranty;
  887. }
  888. public function getDefaultCategoryId() {
  889. return $this->defaultCategoryId;
  890. }
  891. public function getDefaultCategory() {
  892. return $this->defaultCategory;
  893. }
  894. public function getIndex() {
  895. if(empty($this->index))
  896. return 0;
  897. return $this->index > 0 ? 1 : 0;
  898. }
  899. public function getMvente() {
  900. return $this->mvente;
  901. }
  902. public function getStrategique() {
  903. return $this->strategique;
  904. }
  905. public function getReapproNon() {
  906. return $this->reapproNon;
  907. }
  908. public function getMultiple() {
  909. return empty($this->multiple)?1:$this->multiple;
  910. }
  911. public function getOrigin() {
  912. return $this->origin;
  913. }
  914. public function getReference() {
  915. return $this->getModel();
  916. }
  917. public function getGift() {
  918. return is_null($this->gift) ? 0 : $this->gift;
  919. }
  920. public function getType(): ?string {
  921. return empty($this->type) ? self::TYPE_PRODUCT : $this->type;
  922. }
  923. public function getCountryRestrictions() {
  924. return $this->countryRestrictions;
  925. }
  926. public function getRestrictedCountries() {
  927. $output = [];
  928. foreach($this->getCountryRestrictions() as $rc) {
  929. $output[] = $rc->getCountry();
  930. }
  931. return $output;
  932. }
  933. public function getWelcome(): int {
  934. return $this->welcome;
  935. }
  936. public function getCode(): ?string {
  937. return $this->code;
  938. }
  939. public function getIsDefaultVariant() {
  940. return $this->isDefaultVariant;
  941. }
  942. // public function getUrl($absolute=false) {
  943. // if(!empty($this->parent))
  944. // return $this->getParent()->getUrl($absolute);
  945. // if(!empty($this->languageId)){
  946. // $language = $this->languageId==4?'fr':'en';
  947. // }
  948. // if(empty($language)){
  949. // $language = 'fr';
  950. // }
  951. //// $url = '/professionnel/product_info.php?products_id='.$this->getId().'&language='.$language;
  952. //// if($absolute)
  953. //// return 'https://www.dogcat.com'.$url;
  954. // return $this->getUrl();
  955. // }
  956. public function setId($id) {
  957. $this->id = $id;
  958. }
  959. public function setAttributes($attributes) {
  960. $this->attributes = $attributes;
  961. }
  962. public function setChildren($children) {
  963. $this->children = $children;
  964. }
  965. public function setProductModel(?ProductModel $productModel): void {
  966. $this->productModel = $productModel;
  967. }
  968. public function setParent($parent) {
  969. $this->parent = $parent;
  970. }
  971. public function setDescriptions($descriptions) {
  972. $this->descriptions = $descriptions;
  973. }
  974. public function setExtraFields($extraFields): void {
  975. $this->extraFields = $extraFields;
  976. }
  977. public function setQuantity($quantity) {
  978. $this->quantity = $quantity;
  979. }
  980. public function setModel($model) {
  981. $this->model = $model;
  982. }
  983. public function setModelSAP(?string $modelSAP): void {
  984. $this->modelSAP = $modelSAP;
  985. }
  986. public function setEan(?string $ean): void {
  987. $this->ean = $ean;
  988. }
  989. public function setImage($image) {
  990. $this->image = $image;
  991. }
  992. public function setImageMed($imageMed) {
  993. $this->imageMed = $imageMed;
  994. }
  995. public function setImageLrg($imageLrg) {
  996. $this->imageLrg = $imageLrg;
  997. }
  998. public function setImageSm1($imageSm1) {
  999. $this->imageSm1 = $imageSm1;
  1000. }
  1001. public function setImageXl1($imageXl1) {
  1002. $this->imageXl1 = $imageXl1;
  1003. }
  1004. public function setImageSm2($imageSm2) {
  1005. $this->imageSm2 = $imageSm2;
  1006. }
  1007. public function setImageXl2($imageXl2) {
  1008. $this->imageXl2 = $imageXl2;
  1009. }
  1010. public function setImageSm3($imageSm3) {
  1011. $this->imageSm3 = $imageSm3;
  1012. }
  1013. public function setImageXl3($imageXl3) {
  1014. $this->imageXl3 = $imageXl3;
  1015. }
  1016. public function setImageSm4($imageSm4) {
  1017. $this->imageSm4 = $imageSm4;
  1018. }
  1019. public function setImageXl4($imageXl4) {
  1020. $this->imageXl4 = $imageXl4;
  1021. }
  1022. public function setImageSm5($imageSm5) {
  1023. $this->imageSm5 = $imageSm5;
  1024. }
  1025. public function setImageXl5($imageXl5) {
  1026. $this->imageXl5 = $imageXl5;
  1027. }
  1028. public function setImageSm6($imageSm6) {
  1029. $this->imageSm6 = $imageSm6;
  1030. }
  1031. public function setImageXl6($imageXl6) {
  1032. $this->imageXl6 = $imageXl6;
  1033. }
  1034. public function setPrice($price) {
  1035. $this->price = $price;
  1036. }
  1037. public function setEcotax( $ecotax) {
  1038. $this->ecotax = $ecotax;
  1039. }
  1040. public function setDateAdded(\DateTime $dateAdded) {
  1041. $this->dateAdded = $dateAdded;
  1042. }
  1043. public function setLastModified(\DateTime $lastModified) {
  1044. $this->lastModified = $lastModified;
  1045. }
  1046. public function setDateAvailable(\DateTime $dateAvailable) {
  1047. $this->dateAvailable = $dateAvailable;
  1048. }
  1049. public function setWeight($weight) {
  1050. $this->weight = $weight;
  1051. }
  1052. public function setStatus($status) {
  1053. $this->status = $status;
  1054. }
  1055. public function setTaxClass($taxClass) {
  1056. $this->taxClass = $taxClass;
  1057. }
  1058. public function setManufacturer($manufacturer) {
  1059. $this->manufacturer = $manufacturer;
  1060. }
  1061. public function setOrdered($ordered) {
  1062. $this->ordered = $ordered;
  1063. }
  1064. public function setParentId($parentId) {
  1065. $this->parentId = $parentId;
  1066. }
  1067. public function setPrice1($price1) {
  1068. $this->price1 = $price1;
  1069. }
  1070. public function setPrice2($price2) {
  1071. $this->price2 = $price2;
  1072. }
  1073. public function setPrice3($price3) {
  1074. $this->price3 = $price3;
  1075. }
  1076. public function setPrice4($price4) {
  1077. $this->price4 = $price4;
  1078. }
  1079. public function setPrice5($price5) {
  1080. $this->price5 = $price5;
  1081. }
  1082. public function setPrice6($price6) {
  1083. $this->price6 = $price6;
  1084. }
  1085. public function setPrice7($price7) {
  1086. $this->price7 = $price7;
  1087. }
  1088. public function setPrice8($price8) {
  1089. $this->price8 = $price8;
  1090. }
  1091. public function setPrice9($price9) {
  1092. $this->price9 = $price9;
  1093. }
  1094. public function setPrice10($price10) {
  1095. $this->price10 = $price10;
  1096. }
  1097. public function setPrice11($price11) {
  1098. $this->price11 = $price11;
  1099. }
  1100. public function setPrice1Qty($price1Qty) {
  1101. $this->price1Qty = $price1Qty;
  1102. }
  1103. public function setPrice2Qty($price2Qty) {
  1104. $this->price2Qty = $price2Qty;
  1105. }
  1106. public function setPrice3Qty($price3Qty) {
  1107. $this->price3Qty = $price3Qty;
  1108. }
  1109. public function setPrice4Qty($price4Qty) {
  1110. $this->price4Qty = $price4Qty;
  1111. }
  1112. public function setPrice5Qty($price5Qty) {
  1113. $this->price5Qty = $price5Qty;
  1114. }
  1115. public function setPrice6Qty($price6Qty) {
  1116. $this->price6Qty = $price6Qty;
  1117. }
  1118. public function setPrice7Qty($price7Qty) {
  1119. $this->price7Qty = $price7Qty;
  1120. }
  1121. public function setPrice8Qty($price8Qty) {
  1122. $this->price8Qty = $price8Qty;
  1123. }
  1124. public function setPrice9Qty($price9Qty) {
  1125. $this->price9Qty = $price9Qty;
  1126. }
  1127. public function setPrice10Qty($price10Qty) {
  1128. $this->price10Qty = $price10Qty;
  1129. }
  1130. public function setPrice11Qty($price11Qty) {
  1131. $this->price11Qty = $price11Qty;
  1132. }
  1133. public function setProductsQtyBlocks($productsQtyBlocks) {
  1134. $this->productsQtyBlocks = $productsQtyBlocks;
  1135. }
  1136. public function setSoleil($soleil) {
  1137. $this->soleil = $soleil;
  1138. }
  1139. public function setLimitePanier($limitePanier) {
  1140. $this->limitePanier = $limitePanier;
  1141. }
  1142. public function setPage($page) {
  1143. $this->page = $page;
  1144. }
  1145. public function setGuaranty($guaranty) {
  1146. $this->guaranty = $guaranty;
  1147. }
  1148. public function setDefaultCategoryId($defaultCategoryId) {
  1149. $this->defaultCategoryId = $defaultCategoryId;
  1150. }
  1151. public function setIndex($index) {
  1152. $this->index = $index;
  1153. }
  1154. public function setMvente($mvente) {
  1155. $this->mvente = $mvente;
  1156. }
  1157. public function setStrategique($strategique) {
  1158. $this->strategique = $strategique;
  1159. }
  1160. public function setReapproNon($reapproNon) {
  1161. $this->reapproNon = $reapproNon;
  1162. }
  1163. public function setMultiple($multiple) {
  1164. return $this->multiple = $multiple;
  1165. }
  1166. public function setOrigin($origin) {
  1167. $this->origin = $origin;
  1168. }
  1169. public function setPrices($prices) {
  1170. $this->prices = $prices;
  1171. }
  1172. public function setGift($gift) {
  1173. $this->gift = $gift;
  1174. }
  1175. public function setType(?string $type): void {
  1176. $this->type = $type;
  1177. }
  1178. public function setCountryRestrictions($countryRestrictions): void {
  1179. $this->countryRestrictions = $countryRestrictions;
  1180. }
  1181. public function setWelcome(int $welcome): void {
  1182. $this->welcome = $welcome;
  1183. }
  1184. public function setCode(string $code): void {
  1185. $this->code = $code;
  1186. }
  1187. public function setIsDefaultVariant($isDefaultVariant): void {
  1188. $this->isDefaultVariant = $isDefaultVariant;
  1189. }
  1190. public function getCaracteristics() {
  1191. $output = [];
  1192. foreach($this->getAttributes() as $attr) {
  1193. if($attr->getAttribute()->getShowInCaracteristic())
  1194. $output[] = $attr;
  1195. }
  1196. return $output;
  1197. }
  1198. public function hasWelcomeDiscount() {
  1199. return !empty($this->welcome);
  1200. }
  1201. public function isMadeInFrance() {
  1202. if(!empty($this->origin))
  1203. return strtoupper($this->origin) == 'FR';
  1204. if($this->hasChildren()){
  1205. foreach($this->children as $child){
  1206. if(!$child->isMadeInFrance()){
  1207. return false;
  1208. }
  1209. }
  1210. return true;
  1211. }
  1212. return false;
  1213. }
  1214. public function isAvailable() {
  1215. return $this->quantity > 0;
  1216. }
  1217. public function isDefaultVariant() {
  1218. return !empty($this->isDefaultVariant);
  1219. }
  1220. public function hasRestrictionForCountry($country) {
  1221. if($country){
  1222. if($this->countryRestrictions){
  1223. foreach($this->getCountryRestrictions() as $restriction){
  1224. if($restriction->getCountry() == $country){
  1225. return true;
  1226. }
  1227. }
  1228. }
  1229. }
  1230. return false;
  1231. }
  1232. public function isAvailableForCountry($country) {
  1233. if($country && $this->countryRestrictions){
  1234. foreach($this->getCountryRestrictions() as $restriction) {
  1235. if($restriction->getCountry() == $country){
  1236. return false;
  1237. }
  1238. }
  1239. }
  1240. if($country && $this->getManufacturer()){
  1241. return $this->getManufacturer()->isAvailableForCountry($country);
  1242. }
  1243. return true;
  1244. }
  1245. public function isStopped() {
  1246. return $this->quantity == -800;
  1247. }
  1248. public function isActive() {
  1249. return $this->getStatus() == 1;
  1250. }
  1251. public function isGift() {
  1252. return $this->getGift() == 1;
  1253. }
  1254. public function isSoleil() {
  1255. return $this->soleil == 1;
  1256. }
  1257. public function isNonSoleil() {
  1258. return $this->soleil == 0;
  1259. }
  1260. public function isVivog() {
  1261. return $this->isSoleil() || $this->isNonSoleil();
  1262. }
  1263. public function isParent() {
  1264. return empty($this->parent);
  1265. }
  1266. public function isProduct() {
  1267. return $this->getType() == self::TYPE_PRODUCT;
  1268. }
  1269. public function hasChildren() {
  1270. return count($this->children)>0;
  1271. }
  1272. public function hasSoleilPrice() {
  1273. return $this->isSoleil() || $this->isNonSoleil();
  1274. }
  1275. public function hasParent() {
  1276. return !empty($this->parent);
  1277. }
  1278. public function hasSoleil() {
  1279. if($this->hasChildren()){
  1280. foreach($this->children as $child){
  1281. if($child->isSoleil()){
  1282. return true;
  1283. }
  1284. }
  1285. return false;
  1286. }
  1287. return $this->isSoleil();
  1288. }
  1289. public function hasNonSoleil() {
  1290. if($this->hasChildren()){
  1291. foreach($this->children as $child){
  1292. if($child->isNonSoleil()){
  1293. return true;
  1294. }
  1295. }
  1296. return false;
  1297. }
  1298. return $this->isNonSoleil();
  1299. }
  1300. public function addPrice(ProductPrice $productPrice): self
  1301. {
  1302. if (!$this->prices->contains($productPrice)) {
  1303. $this->prices->add($productPrice);
  1304. $productPrice->setProduct($this);
  1305. }
  1306. return $this;
  1307. }
  1308. public function removePrice(ProductPrice $productPrice): self
  1309. {
  1310. if ($this->prices->removeElement($productPrice)) {
  1311. if ($productPrice->getProduct() === $this) {
  1312. $productPrice->setProduct(null);
  1313. }
  1314. }
  1315. return $this;
  1316. }
  1317. public function toArray() : array{
  1318. $parent = $this->getParent();
  1319. $manufacturer = $this->getManufacturer();
  1320. $output = [
  1321. 'id' => $this->getId(),
  1322. 'model' => $this->getModel(),
  1323. 'name' => $this->getName(),
  1324. 'description' => $this->getDescription(),
  1325. 'quantity' => $this->getQuantity(),
  1326. 'image' => $this->getImage(),
  1327. 'price' => $this->getPrice(),
  1328. 'dateAdded' => $this->getDateAdded(),
  1329. 'lastModified' => $this->getLastModified(),
  1330. 'dateAvailable' => $this->getDateAvailable(),
  1331. 'weight' => $this->getWeight(),
  1332. 'status' => $this->getStatus()?1:0,
  1333. 'taxClass ' => $this->getTaxClass()?$this->getTaxClass()->toArray():0,
  1334. 'ordered' => $this->getOrdered(),
  1335. 'parent' => empty($parent)?null:$parent->toArray(),
  1336. 'manufacturer' => empty($manufacturer)?null:$manufacturer->toArray(),
  1337. 'productsQtyBlocks' => $this->getProductsQtyBlocks(),
  1338. 'soleil' => $this->getSoleil(),
  1339. 'nbChildren' => count($this->getChildren()),
  1340. 'limitePanier' => $this->getLimitePanier(),
  1341. 'page' => $this->getPage(),
  1342. 'garantie' => $this->getGuaranty(),
  1343. 'defaultCategoryId' => $this->getDefaultCategoryId(),
  1344. 'index' => $this->getIndex(),
  1345. 'mvente' => $this->getMvente(),
  1346. 'strategique' => $this->getStrategique(),
  1347. 'reapproNon' => $this->getReapproNon(),
  1348. 'multiple' => $this->getMultiple(),
  1349. ];
  1350. return $output;
  1351. }
  1352. public function toJson() {
  1353. return json_encode($this->toArray());
  1354. }
  1355. /**
  1356. * @todo Remove once 0 values in the table are converted to NULL.
  1357. */
  1358. public function loadDescriptions(\Doctrine\ORM\EntityManager $em)
  1359. {
  1360. $conn = $em->getConnection();
  1361. $sql = $conn->prepare('select * from products_description where products_id = '.$this->getId());
  1362. $sql->execute();
  1363. $rows = $sql->fetchAll();
  1364. $descriptions = [];
  1365. foreach($rows as $desc){
  1366. $description = new ProductDescription();
  1367. $description->setName($desc['products_name']);
  1368. $description->setDescription($desc['products_description']);
  1369. $description->setLanguage($em->getRepository('App:Language')->find($desc['language_id']==1?1:4));
  1370. $descriptions[] = $description;
  1371. }
  1372. $this->setDescriptions($descriptions);
  1373. }
  1374. public static function getSearchIndex($lang = 'fr')
  1375. {
  1376. return self::SEARCH_INDICES[$lang];
  1377. }
  1378. /**
  1379. * @ORM\PrePersist
  1380. */
  1381. public function prePersit(\Doctrine\ORM\Event\LifecycleEventArgs $args) {
  1382. $this->setDateAdded(new \DateTime);
  1383. }
  1384. /**
  1385. * @ORM\PrePersist
  1386. * @ORM\PreUpdate
  1387. */
  1388. public function preUpdate(\Doctrine\ORM\Event\LifecycleEventArgs $args) {
  1389. if(is_null($this->weight))
  1390. $this->setWeight(0);
  1391. }
  1392. }