GoodsView.php 2.81 KB
<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "{{%goods_view}}".
 *
 * @property string $name
 * @property string $brand
 * @property string $box
 * @property string $add_box
 * @property string $importer_id
 * @property string $importer_name
 * @property double $rate
 * @property string $currency_id
 * @property string $delivery
 * @property string $description
 * @property string $article
 * @property string $ID
 * @property string $image
 * @property string $tecdoc_id
 * @property double $price
 * @property string $brand_id

 */
class GoodsView extends \backend\components\base\BaseActiveRecord
{
    /**
     * @inheritdoc
     */


    public static function tableName()
    {
        return '{{%goods_view}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'brand', 'box', 'importer_id', 'importer_name', 'rate', 'delivery', 'price'], 'required'],
            [['box', 'add_box', 'importer_id', 'currency_id', 'ID', 'tecdoc_id', 'brand_id'], 'integer'],
            [['rate', 'price'], 'number'],
            [['name', 'brand'], 'string', 'max' => 100],
            [['importer_name', 'delivery'], 'string', 'max' => 254],
            [['description'], 'string', 'max' => 255],
            [['article'], 'string', 'max' => 150],
            [['image'], 'string', 'max' => 229]
        ];
    }

    /**
     * @return string
     */
    public function getOutputPrice()
    {
        $price_margin_id = Yii::$app->session->getFlash('price_margin_id',1);
        $price_currency_id = Yii::$app->session->getFlash('price_currency_id',1);

        $koef =  Margins::getDb()->cache( function ($db) use ($price_margin_id) {
            return (float) Margins::findOne($price_margin_id)->koef;
        });

        $rate = Currency::getDb()->cache( function ($db) use ($price_currency_id) {
            return (float) Currency::findOne($price_currency_id)->rate;
        });

        if(!$rate)
            $rate = 1; // если 0, то 1

        return round($this->price * ($this->rate/$rate) * $koef, 2);
    }

    public static function primaryKey()
    {
        return ['name','brand'];
    }


    public function attributeLabels()
    {
        return [
            'name' => 'Name',
            'brand' => 'Brand',
            'box' => 'Box',
            'add_box' => 'Add Box',
            'importer_id' => 'Importer ID',
            'importer_name' => 'Importer Name',
            'rate' => 'Rate',
            'currency_id' => 'Currency ID',
            'delivery' => 'Delivery',
            'description' => 'Description',
            'article' => 'Article',
            'ID' => 'ID',
            'image' => 'Image',
            'tecdoc_id' => 'Tecdoc ID',
            'price' => 'Price',
            'brand_id' => 'Brand ID',
        ];
    }
}