Your IP : 216.73.216.79


Current Path : /var/www/html/dyna-crm/models/
Upload File :
Current File : /var/www/html/dyna-crm/models/StockIn.php

<?php

namespace app\models;

use Yii;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use app\models\Product;
use app\models\StockLog;
use app\models\EasStock;

/**
 * This is the model class for table "stock_in".
 *
 * @property int $id
 * @property string $uuid
 * @property int $vendor_id
 * @property int $product_id
 * @property string $do_no
 * @property string $do_file
 * @property int $quantity
 * @property string $expire_dt
 * @property string $location
 * @property string $remarks
 * @property string $created_dt
 * @property int $created_by
 * @property string $updated_dt
 * @property int $updated_by
 */
class StockIn extends \yii\db\ActiveRecord {

    public $file;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return 'stock_in';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['uuid', 'vendor_id', 'product_id', 'do_no', 'quantity', 'date_arrived', 'brand_id', 'expire_status', 'company_id'], 'required'],
            [['vendor_id', 'product_id', 'quantity', 'created_by', 'updated_by'], 'integer'],
            [['expire_dt', 'created_dt', 'updated_dt', 'soldout', 'stock_val', 'data_from', 'original_id', 'owner_type', 'owner_id'], 'safe'],
            [['remarks'], 'string'],
            [['do_no', 'do_file', 'location'], 'string', 'max' => 255],
            [['uuid'], 'unique'],
            [['file'], 'file'],
            [['expire_status'], 'default', 'value' => '0'],
                //[['expire_dt'], 'required', 'on' => 'hasexpired'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'uuid' => 'Uuid',
            'company_id' => 'Stock-In by Company',
            'vendor_id' => 'Vendor',
            'product_id' => 'Product Name',
            'do_no' => 'DO Number',
            'do_file' => 'Do File',
            'quantity' => 'Quantity',
            'expire_dt' => 'Expire Date',
            'location' => 'Stock Location',
            'remarks' => 'Remarks',
            'created_dt' => 'Created Dt',
            'created_by' => 'Created By',
            'updated_dt' => 'Updated Dt',
            'updated_by' => 'Updated By',
            'date_arrived' => 'Date Stock Arrived',
            'brand_id' => 'Brand',
            'file' => 'DO Document',
            'expire_status' => 'Expire Status',
            'stock_val' => 'Stock Value',
            'owner_type' => 'Stock Owner'
        ];
    }
    
    public function getuuid($id) {
        $output = '';
        if (($model = static::findOne($id)) !== null) {
            $output = $model->uuid;
        }
        return $output;
    }

    public function add($stockin_owner_type, $stockin_owner_id, $stockin_product_id, $stockin_quantity, $transfer_dt, $remarks) {
        $output = "";
        $Product = new Product();

        $model = new StockIn();
        $model->uuid = Uuid::uuid4();
        $model->vendor_id = $Product->getvendor($stockin_product_id);
        $model->company_id = $Product->getcompany($stockin_product_id);
        $model->brand_id = $Product->getbrand($stockin_product_id);
        $model->product_id = $stockin_product_id;
        $model->quantity = $stockin_quantity;
        $model->date_arrived = $transfer_dt;
        $model->expire_status = '0';
        $model->remarks = $remarks;
        $model->soldout = '0';
        $model->created_dt = date("Y-m-d H:i:s", strtotime($transfer_dt));
        $model->created_by = Yii::$app->user->identity->id;
        $model->updated_dt = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $model->stock_val = '0';
        $model->data_from = 'cms';
        $model->owner_type = $stockin_owner_type;
        $model->owner_id = $stockin_owner_id;
        if ($model->save(false)) {
            
            Yii::$app->db->createCommand()->update('stock_in', ['original_id' => $model->id], 'id=' . $model->id)->execute();
            
            $modelStockLog = new StockLog();
            $modelStockLog->product_id = $model->product_id;
            $modelStockLog->action_type = 'stockin';
            $modelStockLog->table_related = 'stock_in';
            $modelStockLog->data_id = $model->id;
            $modelStockLog->action_by = Yii::$app->user->identity->id;
            $modelStockLog->action_datetime = date("Y-m-d H:i:s");
            $modelStockLog->owner_type = $model->owner_type;
            $modelStockLog->owner_id = $model->owner_id;
            if ($modelStockLog->save()) {

                $modelEasStock = new EasStock();
                $addEasStock = $modelEasStock->add($model->id);
                if ($addEasStock == 'success')
                    $output = $model->id;
            }
        }
        return $output;
    }

}