src/Entity/ServiceForm.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Model\BitrixSerialize;
  4. use App\Model\BitrixSerializeList;
  5. use App\Repository\ServiceFormRepository;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use JMS\Serializer\Annotation as JMS;
  8. /**
  9.  * @ORM\Entity(repositoryClass=ServiceFormRepository::class)
  10.  */
  11. class ServiceForm implements \JsonSerializableBitrixSerializeBitrixSerializeList
  12. {
  13.     /**
  14.      * Иконка - идентификатор формы услуги изгоовления бумажного сертификата
  15.      */
  16.     const PAPER_COPY_ICON 'paper-copy';
  17.     private const BITRIX_PLACE_DEMAND_ID 24;
  18.     /**
  19.      * Базовый путь к папке с изображениями
  20.      */
  21.     protected static string $assetsImagesPath 'assets/notaryLandingPage/img/';
  22.     /**
  23.      * @ORM\Id()
  24.      * @ORM\GeneratedValue()
  25.      * @ORM\Column(type="integer")
  26.      * @JMS\Type("integer")
  27.      */
  28.     private $id;
  29.     /**
  30.      * @ORM\Column(type="unescaped_json")
  31.      *
  32.      * @JMS\Type("array")
  33.      */
  34.     private $fields = [];
  35.     /**
  36.      * @ORM\ManyToOne(targetEntity=ServiceSection::class, inversedBy="serviceForms")
  37.      * @JMS\Type("Relation<App\Entity\ServiceSection>")
  38.      */
  39.     private $serviceSection;
  40.     /**
  41.      * @ORM\Column(type="integer")
  42.      *
  43.      * @JMS\Type("integer")
  44.      */
  45.     private $sortNumber;
  46.     /**
  47.      * @ORM\Column(type="string", length=255)
  48.      *
  49.      * @JMS\Type("string")
  50.      */
  51.     private $name;
  52.     /**
  53.      * @ORM\Column(type="integer")
  54.      *
  55.      * @JMS\Type("string")
  56.      */
  57.     private $price;
  58.     /**
  59.      * @ORM\Column(type="string", length=255, nullable=true)
  60.      *
  61.      * @JMS\Type("string")
  62.      */
  63.     private $icon;
  64.     /**
  65.      * @ORM\Column(type="integer", nullable=true)
  66.      * @JMS\Type("integer")
  67.      */
  68.     private $cellWidth;
  69.     /**
  70.      * @ORM\Column(type="integer", nullable=true)
  71.      */
  72.     private $bitrixServiceId;
  73.     /**
  74.      * @ORM\Column(type="integer", nullable=true)
  75.      * @JMS\Type("integer")
  76.      */
  77.     private $clientPrice;
  78.     /**
  79.      * @ORM\Column(type="integer", nullable=true)
  80.      */
  81.     private $vipPrice;
  82.     /**
  83.      * @ORM\Column(type="string", length=255, nullable=true)
  84.      */
  85.     private $announcementConfig;
  86.     /**
  87.      * @ORM\Column(type="integer")
  88.      */
  89.     private $bitrixCategoryId;
  90.     /**
  91.      * @ORM\Column(type="string", length=5, nullable=true)
  92.      */
  93.     private $vehicleCategoryCode;
  94.     /**
  95.      * @ORM\Column(type="boolean", nullable=true)
  96.      */
  97.     private ?bool $isEnabled true;
  98.     /**
  99.      */
  100.     private string $term '';
  101.     public function getId(): ?int
  102.     {
  103.         return $this->id;
  104.     }
  105.     public function getFields(): ?array
  106.     {
  107.         return $this->fields;
  108.     }
  109.     public function setFields(array $fields): self
  110.     {
  111.         $this->fields $fields;
  112.         return $this;
  113.     }
  114.     public function getServiceSection(): ?ServiceSection
  115.     {
  116.         return $this->serviceSection;
  117.     }
  118.     public function setServiceSection(?ServiceSection $serviceSection): self
  119.     {
  120.         $this->serviceSection $serviceSection;
  121.         return $this;
  122.     }
  123.     public function getSortNumber(): ?int
  124.     {
  125.         return $this->sortNumber;
  126.     }
  127.     public function setSortNumber(int $sortNumber): self
  128.     {
  129.         $this->sortNumber $sortNumber;
  130.         return $this;
  131.     }
  132.     /**
  133.      * @param array $allowed
  134.      * @return array
  135.      */
  136.     public function filterFields(array $allowed)
  137.     {
  138.         $newFields = [];
  139.         foreach ($this->getFields() as $formKey => $form) {
  140.             if ($formKey === 'tabs') {
  141.                 foreach ($form as $tab) {
  142.                     foreach ($tab['fields'] as $field) {
  143.                         $added = [];
  144.                         foreach ($field as $key => $value) {
  145.                             if (in_array($key$allowed)) {
  146.                                 $added[$key] = $value;
  147.                             }
  148.                         }
  149.                         if ($added) {
  150.                             $newFields[] = $added;
  151.                         }
  152.                     }
  153.                 }
  154.             }
  155.         }
  156.         return $newFields;
  157.     }
  158.     /**
  159.      * @return array
  160.      */
  161.     public function getAllFieldsIds()
  162.     {
  163.         $newFields = [];
  164.         foreach ($this->getFields() as $formKey => $form) {
  165.             if ($formKey === 'tabs') {
  166.                 foreach ($form as $tab) {
  167.                     foreach ($tab['fields'] as $field) {
  168.                         if (!isset($field['id'])) {
  169.                             continue;
  170.                         }
  171.                         $newFields[] = $field['id'] ?? [];
  172.                     }
  173.                 }
  174.             }
  175.         }
  176.         return $newFields;
  177.     }
  178.     /**
  179.      * @return array
  180.      */
  181.     public function getB24DefaultValue()
  182.     {
  183.         $newFields = [];
  184.         foreach ($this->getFields() as $formKey => $form) {
  185.             if ($formKey === 'tabs') {
  186.                 foreach ($form as $tab) {
  187.                     foreach ($tab['fields'] as $field) {
  188.                         if (!isset($field['id'])) {
  189.                             continue;
  190.                         }
  191.                         foreach ($field['optionsB24'] ?? [] as $value) {
  192.                             $newFields[$field['id']][$value['value']] = $value['b24value'];
  193.                         }
  194.                     }
  195.                 }
  196.             }
  197.         }
  198.         return $newFields;
  199.     }
  200.     public function jsonSerialize(): mixed
  201.     {
  202.         return [
  203.             'id'             => $this->getId(),
  204.             'fields'         => $this->getFields(),
  205.             'sortNumber'     => $this->getSortNumber(),
  206.             'name'           => $this->getName(),
  207.             'price'          => $this->getPrice(),
  208.             'serviceSection' => $this->getServiceSection()->jsonSerializeWithoutForms(),
  209.             'isEnabled'      => $this->isEnabled(),
  210.         ];
  211.     }
  212.     public function importToJsonFile(): array
  213.     {
  214.         return [
  215.             'fields'              => $this->getFields(),
  216.             'announcement_config' => $this->getAnnouncementConfig(),
  217.         ];
  218.     }
  219.     public function getName(): ?string
  220.     {
  221.         return $this->name;
  222.     }
  223.     public function setName(string $name): self
  224.     {
  225.         $this->name $name;
  226.         return $this;
  227.     }
  228.     public function getPrice(): ?int
  229.     {
  230.         return $this->price;
  231.     }
  232.     public function setPrice(int $price): self
  233.     {
  234.         $this->price $price;
  235.         return $this;
  236.     }
  237.     public function getIcon(): ?string
  238.     {
  239.         return $this->icon;
  240.     }
  241.     public function setIcon(?string $icon): self
  242.     {
  243.         $this->icon $icon;
  244.         return $this;
  245.     }
  246.     public function getImagePath(): ?string
  247.     {
  248.         if (empty($this->icon)) {
  249.             return null;
  250.         }
  251.         return static::$assetsImagesPath $this->icon ".jpg";
  252.     }
  253.     public static function setAssetsImagesPath(string $path): void
  254.     {
  255.         static::$assetsImagesPath $path;
  256.     }
  257.     public function getCellWidth(): ?int
  258.     {
  259.         return $this->cellWidth;
  260.     }
  261.     public function setCellWidth(?int $cellWidth): self
  262.     {
  263.         $this->cellWidth $cellWidth;
  264.         return $this;
  265.     }
  266.     public function isSelfPaperForm(): bool
  267.     {
  268.         return $this->getIcon() === 'paper-copy';
  269.     }
  270.     public function serializeBitrix(): array
  271.     {
  272.         return [
  273.             "IBLOCK_TYPE_ID" => "bitrix_processes",
  274.             "IBLOCK_ID"      => 36,
  275.             "NAME"           => $this->getName(),
  276.             "CURRENCY_ID"    => "RUB",
  277.             "PRICE"          => $this->getPrice(),
  278.             "PRICE_CLIENT"   => $this->getClientPrice(),
  279.             "SORT"           => $this->getSortNumber()
  280.         ];
  281.     }
  282.     public function getBitrixServiceId(): ?int
  283.     {
  284.         return $this->bitrixServiceId;
  285.     }
  286.     public function setBitrixServiceId(?int $bitrixServiceId): self
  287.     {
  288.         $this->bitrixServiceId $bitrixServiceId;
  289.         return $this;
  290.     }
  291.     public function getElementCode(): string
  292.     {
  293.         return 'service_form_'$this->getId();
  294.     }
  295.     public function getIBlockId(): int
  296.     {
  297.         return 36;
  298.     }
  299.     public function getClientPrice(): ?int
  300.     {
  301.         return $this->clientPrice;
  302.     }
  303.     public function setClientPrice(?int $clientPrice): self
  304.     {
  305.         $this->clientPrice $clientPrice;
  306.         return $this;
  307.     }
  308.     /**
  309.      * @param Order $order
  310.      * @return $this
  311.      */
  312.     public function setValuesFromOrder(Order $order)
  313.     {
  314.         $values = [];
  315.         foreach ($order->getData() as $datum) {
  316.             $values[$datum['name']] = $datum['value'];
  317.         }
  318.         foreach ($this->fields['tabs'] as &$tab) {
  319.             if (!isset($tab['fields'])) {
  320.                 continue;
  321.             }
  322.             foreach ($tab['fields'] as &$field) {
  323.                 if (isset($values[$field['id']])) {
  324.                     $field['value'] = $values[$field['id']];
  325.                 }
  326.             }
  327.         }
  328.         if (isset($values['client'])) {
  329.             $this->fields['client'] = $values['client'];
  330.         }
  331.         if ($order->getId()) {
  332.             $this->fields['existsOrderId'] = $order->getId();
  333.         }
  334.         return $this;
  335.     }
  336.     public function getVipPrice(): ?int
  337.     {
  338.         return $this->vipPrice;
  339.     }
  340.     public function setVipPrice(?int $vipPrice): self
  341.     {
  342.         $this->vipPrice $vipPrice;
  343.         return $this;
  344.     }
  345.     public function setValueToFields(string $name$value)
  346.     {
  347.         foreach ($this->fields['tabs'] as &$tab) {
  348.             if (!isset($tab['fields'])) {
  349.                 continue;
  350.             }
  351.             foreach ($tab['fields'] as &$field) {
  352.                 if (empty($field['id'])) {
  353.                     continue;
  354.                 }
  355.                 if ($field['id'] === $name) {
  356.                     $field['value'] = $value;
  357.                 }
  358.             }
  359.         }
  360.         return $this;
  361.     }
  362.     public function unSetFieldByName(string $name)
  363.     {
  364.         foreach ($this->fields['tabs'] as &$tab) {
  365.             if (!isset($tab['fields'])) {
  366.                 continue;
  367.             }
  368.             foreach ($tab['fields'] as $key => &$field) {
  369.                 if ($field['id'] === $name) {
  370.                     unset($tab['fields'][$key]);
  371.                 }
  372.             }
  373.         }
  374.         return $this;
  375.     }
  376.     public function getB24FieldCode(?string $fieldName): string
  377.     {
  378.         if (!$fieldName) {
  379.             return '';
  380.         }
  381.         $fieldName str_replace('-''_'$fieldName);
  382.         return 'UF_CRM_' strtoupper($fieldName);
  383.     }
  384.     public function getAnnouncementConfig(): ?string
  385.     {
  386.         return $this->announcementConfig;
  387.     }
  388.     public function setAnnouncementConfig(?string $announcementConfig): self
  389.     {
  390.         $this->announcementConfig $announcementConfig;
  391.         return $this;
  392.     }
  393.     public function getBitrixCategoryId(): ?int
  394.     {
  395.         return $this->bitrixCategoryId;
  396.     }
  397.     public function isPlaceDemand(): bool
  398.     {
  399.         return $this->getBitrixCategoryId() === self::BITRIX_PLACE_DEMAND_ID;
  400.     }
  401.     public function setBitrixCategoryId(int $bitrixCategoryId): self
  402.     {
  403.         $this->bitrixCategoryId $bitrixCategoryId;
  404.         return $this;
  405.     }
  406.     public function getVehicleCategoryCode(): ?string
  407.     {
  408.         return $this->vehicleCategoryCode;
  409.     }
  410.     public function setVehicleCategoryCode(?string $vehicleCategoryCode): self
  411.     {
  412.         $this->vehicleCategoryCode $vehicleCategoryCode;
  413.         return $this;
  414.     }
  415.     public function getTerm(): string
  416.     {
  417.         return $this->term;
  418.     }
  419.     public function setTerm(string $term): self
  420.     {
  421.         $this->term $term;
  422.         return $this;
  423.     }
  424.     public function isEnabled(): bool
  425.     {
  426.         return (bool)$this->isEnabled;
  427.     }
  428.     public function setIsEnabled(bool $isEnabled): self
  429.     {
  430.         $this->isEnabled $isEnabled;
  431.         return $this;
  432.     }
  433.     public function isActive(): bool
  434.     {
  435.         if(!$this->getServiceSection()) {
  436.             return false;
  437.         }
  438.         if(!$this->getPrice()) {
  439.             return false;
  440.         }
  441.         return true;
  442.     }
  443. }