src/Entity/Product.php line 807

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