src/Entity/Product.php line 681

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