Your IP : 216.73.216.79


Current Path : /var/www/html/crm/models/
Upload File :
Current File : /var/www/html/crm/models/StockOut.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\EasStockAdjustment;

/**
 * This is the model class for table "stock_out".
 *
 * @property int $id
 * @property string $uuid
 * @property int $company_id
 * @property int $product_id
 * @property int $quantity
 * @property string $type
 * @property string $remarks
 * @property string $created_dt
 * @property int $created_by
 * @property string $updated_dt
 * @property int $updated_by
 */
class StockOut extends \yii\db\ActiveRecord {

    public $product_name;

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

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['uuid', 'product_id', 'quantity', 'type'], 'required'],
            [['company_id', 'product_id', 'quantity', 'created_by', 'updated_by'], 'integer'],
            [['remarks'], 'string'],
            [['created_dt', 'updated_dt', 'owner_type', 'owner_id', 'data_from', 'original_id'], 'safe'],
            [['type'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'uuid' => 'Uuid',
            'company_id' => 'Company',
            'product_id' => 'Product',
            'quantity' => 'Quantity',
            'type' => 'Type',
            'remarks' => 'Remarks',
            'created_dt' => 'Created Dt',
            'created_by' => 'Created By',
            'updated_dt' => 'Updated Dt',
            'updated_by' => 'Updated By',
            'owner_type' => 'Stock Owner',
            'product_name' => 'Product',
            'owner_type' => 'Stock Owner Type',
            'owner_id' => 'Stock Owner'
        ];
    }
    
    public function getuuid($id) {
        $output = '';
        if (($model = static::findOne($id)) !== null) {
            $output = $model->uuid;
        }
        return $output;
    }

    public function add($stockout_owner_type, $stockout_owner_id, $stockout_product_id, $stockout_quantity, $transfer_dt, $remarks) {
        $output = "";

        $model = new StockOut();
        $model->uuid = Uuid::uuid4();
        $model->product_id = $stockout_product_id;
        $model->quantity = $stockout_quantity;
        $model->type = 'transfer';
        $model->remarks = $remarks;
        $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->data_from = 'cms';
        $model->owner_type = $stockout_owner_type;
        $model->owner_id = $stockout_owner_id;
        if ($model->save(false)) {
            Yii::$app->db->createCommand()->update('stock_out', ['original_id' => $model->id], 'id=' . $model->id)->execute();

            $modelStockLog = new StockLog();
            $modelStockLog->product_id = $model->product_id;
            $modelStockLog->action_type = 'stockout';
            $modelStockLog->table_related = 'stock_out';
            $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()) {
                $modelEasStockAdjustment = new EasStockAdjustment();
                $addEasStockAdjustment = $modelEasStockAdjustment->add($model->id);
                if ($addEasStockAdjustment == 'success')
                    $output = $model->id;
            }
        }

        return $output;
    }

}