trinion_mrp-1.0.x-dev/src/Form/BOMForm.php

src/Form/BOMForm.php
<?php

declare(strict_types=1);

namespace Drupal\trinion_mrp\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\Entity\Node;

/**
 * Отчёт BOM
 */
final class BOMForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId(): string {
    return 'trinion_mrp_bom';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state): array {
    $form['container'] = [
      '#type' => 'container',
      '#attributes' => ['class' => ['row', 'mb-3', ]],
    ];
    $form['container']['tovar'] = [
      '#type' => 'entity_autocomplete',
      '#placeholder' => t('Item'),
      '#target_type' => 'node',
      '#selection_handler' => 'views',
      '#wrapper_attributes' => ['class' => ['col-9']],
      '#selection_settings' => [
        'view' => [
          'view_name' => 'mrp_link_to_the_product_with_specification',
          'display_name' => 'entity_reference_1',
          'arguments' => []
        ],
        'match_operator' => 'CONTAINS',
        'match_limit' => 100,
      ]
    ];
    $form['container']['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => t('Create report'),
      '#attributes' => ['class' => ['col-3',]],
    ];
    if ($tovar_id = $form_state->getValue('tovar')) {
      $bom_data = $this->getBOMData($tovar_id);
      $bom_data_total = $this->bomDataAddTotal($bom_data);
      $bom_data[] = [
        ['data' => 'Total number parts', 'colspan' => 3, 'align' => 'right'],
        $bom_data_total['qty'],
        ['data' => 'Total cost', 'colspan' => 2, 'align' => 'right'],
        $bom_data_total['cost'],
      ];
      $form['table'] = [
        '#theme' => 'table',
        '#header' => ['Level', 'Part #', 'Description', 'Qty', 'Unit', 'Unit cost', 'Cost'],
        '#rows' => $bom_data,
        '#attributes' => ['class' => ['bom-table', ],]
      ];
    }

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {
    $form_state->setRebuild(TRUE);
  }

  public function getBOMData($tovar_id) {
    $tovar = Node::load($tovar_id);
    $data = [];
    $specificatiya = \Drupal::service('trinion_tp.helper')->getSpecificatsiyaTovara($tovar->id(), NULL, TRUE);
    if ($specificatiya) {
      foreach ($specificatiya->get('field_tp_stroki') as $item) {
        $qty = $item->entity->get('field_tp_kolichestvo')->getString();
        $cost = floatval($item->entity->get('field_tp_tovar')->first()->entity->get('field_tp_cena')->getString());
        $data[] = [
          1,
          $item->entity->get('field_tp_artikul')->getString(),
          $item->entity->label(),
          1,
          $qty,
          1 * $cost,
          $qty * $cost,
        ];
        $this->dataIteration($item->entity->get('field_tp_tovar')->first()->entity, $data, 1, $qty);
      }
      array_multisort($data);
    }
    return $data;
  }

  public function bomDataAddTotal($bom_data) {
    $qty = $unit_cost = $cost = 0;
    foreach ($bom_data as $row) {
      $qty += $row[3];
      $cost += $row[6];
    }
    return [
      'qty' => $qty,
      'cost' => $cost,
    ];
  }

  public function dataIteration(Node $product, array &$data, $level, $qty) {
    $level++;
    $specificatiya = \Drupal::service('trinion_tp.helper')->getSpecificatsiyaTovara($product->id(), NULL, TRUE);
    if ($specificatiya) {
      foreach ($specificatiya->get('field_tp_stroki') as $item) {
        $units = $item->entity->get('field_tp_kolichestvo')->getString();
        $cost = floatval($item->entity->get('field_tp_tovar')->first()->entity->get('field_tp_cena')->getString());
        $qtys = $qty * $units;
        $data[] = [
          $level,
          $item->entity->get('field_tp_artikul')->getString(),
          $item->entity->label(),
          $units,
          $qtys,
          $units * $cost,
          $qtys * $cost,
        ];
        $this->dataIteration($item->entity->get('field_tp_tovar')->first()->entity, $data, $level, $qtys);
      }
    }
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc