CustomConverter.php 4.06 KB
<?php
namespace common\components\parsers;

use common\components\CustomVarDamp;
use yii\multiparser\Converter;
use backend\models\Details;
use backend\models\DetailsCrosses;
use backend\models\ImportersPrefix;

class CustomConverter extends Converter
{

    public static $sign;
    public static $multiplier;
    public static $importer_id;
    public static $brand;
    public static $begin_of_the_day = true;
    // точность для округления десятичных чисел
    public static $precision = 2;

    public static function convertToDetails(array $row)
    {
        // присвоим полный артикул
        $row['FULL_ARTICLE'] = $row['ARTICLE'];

        return $row;
    }


    public function ConvertToMultiply( array $row )
    {
        $PRICE = $row['PRICE'];
        $sign = self::$sign;
        $multiplier = self::$multiplier;
        //CustomVarDamp::dumpAndDie(self);
        if (isset($sign)) {
            if ($sign == '+') {
                if ($multiplier > 0) {
                    $PRICE += $multiplier;
                }
            } else if ($sign == '-') {
                if ($multiplier > 0) {
                    $PRICE -= $multiplier;
                }
            } else if ($sign == '*') {
                if ($multiplier > 0) {
                    $PRICE *= $multiplier;
                }
            } else if ($sign == '/') {
                if ($multiplier > 0) {
                    $PRICE /= $multiplier;
                }
            }
        }

        $row['PRICE'] = $PRICE;

        return $row;

    }

    public static function convertToArticle( $value )
    {

        if (is_array($value)) {

            $row = $value;
            // 1. Уберем префикс который разделен пробелом (если он есть)
            $words = explode(" ", $row['ARTICLE']);
            if (count($words) > 1) {
                array_shift($words);
                $row['ARTICLE'] = implode(" ", $words);
            }

            if( isset( $row['BRAND'] ) && isset( self::$importer_id ) ){
                // 2. Уберем брендовый префикс (если он есть)
                self::$brand = $row['BRAND'];
                $prefix = '';
                // запрос закешируем
                $prefix = ImportersPrefix::getDb()->cache( function ($db) {
                    return ImportersPrefix::find()->where([ 'importer_id' => self::$importer_id,
                        'brand' => self::$brand ])->one();
                });

                if ($prefix) {
                    $row['BRAND'] = str_replace($prefix, "", $row['BRAND']);
                }
            }

            return $row;

        } else {
            $words = explode( " ", $value );
            if ( count( $words ) > 1) {
                array_shift( $words );
                $value = implode( " ", $words );
            }

            return $value;
        }
    }

    public static function convertToBrand($value)
    {
        $res = self::convertToEncode($value);
        $res = trim(strtoupper($res));
        $res = str_replace("Ä", "A", str_replace("Ö", "O", str_replace("Ü", "U", str_replace("Ë", "E", str_replace("Ò", "O", $res)))));
        $res = str_replace(array('@', '#', '~', '"', "'", "?", "!"), '', $res);

        return $res;
    }

    public static function convertToTimestamp($value)
    {
        if ( self::$begin_of_the_day ){
            $res = mktime(0,0,0,(int)substr($value,3,2),(int)substr($value,0,2),(int)substr($value,6,4));
        } else {
            $res = mktime(23,59,59,(int)substr($value,3,2),(int)substr($value,0,2),(int)substr($value,6,4));
        }

        return $res;
    }

    public static function convertToFloat( $value )
    {
        if ($value == '') {
            $value = 0;
        }
        $value = trim(str_replace(",", ".", $value));
        $value = preg_replace("/[^0-9.]+/", "", strtoupper($value));

        if ($value == '') {
            return '';
        }
        $value = round( (float)$value, self::$precision );

        return $value;
    }

}