<?php
    
    namespace common\modules\product\models;
    
    use common\modules\rubrication\models\TaxGroup;
    use yii\base\Model;
    
    class Export extends Model
    {
        
        public $lang;
        
        public $file;
        
        public $errors = [];
        
        public $output = [];
        
        public function process($filename = NULL, $from = 0)
        {
            
            $limit = 100;
            if(empty( $filename )) {
                $filename = 'products_' . date('d_m_Y_H_i') . '.csv';
                $handle = fopen(\Yii::getAlias('@storage/sync/') . $filename, "w");
            } else {
                $handle = fopen(\Yii::getAlias('@storage/sync/') . $filename, "a");
            }
            
            $products = Product::find()
                               ->with([
                                   'variantsWithFilters',
                                   'brand',
                                   'categories',
                                   'filters'])
                               ->limit($limit)
                               ->offset($from)
                               ->all();
            $filesize = Product::find()
                               ->count();
            foreach($products as $product) {
                
                                $mods = [];
                                $filterString = $this->convertFilterToString($product->filters);
                
                                foreach($product->variantsWithFilters as $variant) {
                                    $color = $variant->name;
                                    $mods[] = $variant->sku . '=' . $this->convertFilterToString($variant->filters) . '=' . $color . '=' . ( ( !empty( $variant->image ) ) ? $variant->image->image : '' ) . '=' . $variant->stock;
                                }
                
                                $fotos = [];
                
                                $categories = [];
                                foreach($product->categories as $value) {
                                    $categories[] = $value->name;
                                }
                
                                $categories = implode(',', $categories);
                
                                $list = [
                                    $categories,
                                    !empty($product->brand) ? $product->brand ->name :'',
                                    $product->name,
                                    '',
                                    ( ( !empty( $product->description ) ) ? $product->description : '' ),
                                    $filterString,
                                    ( !empty( $product->variant ) ) ? $product->variant->price_old : '',
                                    ( !empty( $product->variant ) ) ? $product->variant->price : '',
                                    intval($product->akciya),
                                    '',
                                    intval($product->is_new),
                                    intval($product->is_top),
                                    $product->video,
                                    implode(',', $fotos),
                                ];
                                $to_write = array_merge($list, $mods);
                fputcsv($handle, $to_write, ';');
                unset( $product );
            }
    
            fclose($handle);
    
            $from += $limit;
            $end = false;
            if($from > $filesize) {
                $end = true;
            }
            
            $result = [
                'end'       => $end,
                'from'      => $from,
                'totalsize' => $filesize,
                'filename'  => $filename,
            ];
            
            if($end) {
                $result = array_merge($result, [
                    'link' => '/storage/sync/'.$filename,
                ]);
            }
            
            return $result;
            
        }
        
        public function convertFilterToString($filters)
        {
            if(!empty($filters)){
                $fittersArray = [];
                foreach($filters as $filter) {
                    if($filter->taxGroup instanceof  TaxGroup){
                        $fittersArray[ $filter->taxGroup->alias ][] = $filter->value;
                    }

                }
                $filterString = [];

                foreach($fittersArray as $filterName => $filterRows) {
                    $row = implode(',', $filterRows);
                    $filterString[] = "[{$filterName}:{$row}]";
                }
                return implode('*', $filterString);
            }

        }
    }