Your IP : 216.73.216.79


Current Path : /var/www/html/dyna-crm/controllers/
Upload File :
Current File : /var/www/html/dyna-crm/controllers/StockoutController.php

<?php

namespace app\controllers;

use Yii;
use app\models\StockOut;
use app\models\StockOutSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use yii\helpers\Url;
use app\models\Log;
use app\models\LogSearch;
use app\models\StockLog;
use app\models\Product;
use app\models\Company;
use app\models\Vendor;

/**
 * StockoutController implements the CRUD actions for StockOut model.
 */
class StockoutController extends Controller {

    public $menu_id = 57;

    /**
     * {@inheritdoc}
     */
    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
            'access' => [
                'class' => \yii\filters\AccessControl::className(),
                'only' => ['index', 'create', 'update', 'view'],
                'rules' => [
                    // allow authenticated users
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                // everything else is denied
                ],
            ],
        ];
    }

    /**
     * Lists all StockOut models.
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new StockOutSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'menu_id' => $this->menu_id,]);
    }

    public function actionLog($uuid) {
        $searchModel = new LogSearch();
        $searchModel->menu_id = $this->menu_id;
        $searchModel->data_uuid = $uuid;
        $dataProvider = $searchModel->log(Yii::$app->request->queryParams);
        return $this->render('log', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'menu_id' => $this->menu_id]);
    }

    /**
     * Displays a single StockOut model.
     * @param integer $id
     * @param string $uuid
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id, $uuid) {
        return $this->render('view', [
                    'model' => $this->findModel($id, $uuid),
        ]);
    }

    /**
     * Creates a new StockOut model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate($product_id = '', $product_name = '', $owner_type = '', $owner_id = '') {
        $model = new StockOut();
        
        if(!empty($product_id))
            $model->product_id = $product_id;
        if(!empty($product_name))
            $model->product_name = $product_name;
        if(!empty($owner_type))
            $model->owner_type = $owner_type;
        if(!empty($owner_id))
            $model->owner_id = $owner_id;
        
        $model->created_dt = date("Y-m-d H:i:s");
        $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->uuid = Uuid::uuid4();
        $model->data_from = 'crm';

        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            Yii::$app->db->createCommand()->update('stock_out', ['original_id' => $model->id], 'id=' . $model->id)->execute();

            //save into stock_log
            $modelStockLog = new StockLog();
            $modelStockLog->product_id = $model->product_id;
            $modelStockLog->action_type = 'stockout';
            $modelStockLog->table_related = 'stock_out';
            $modelStockLog->data_id = $model->id;
            //$modelStockLog->company_id = $model->company_id;
            $modelStockLog->do_no = '';
            $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;
            $modelStockLog->save();

            $Log = new Log();
            $Log->insertlog($this->menu_id, 'create', $model->uuid);

            $this->getView()->registerJs("swal.fire({position: 'center-center',type: 'success',title: 'Data has been saved',showConfirmButton: false,timer: 4000,}).then(function(result){window.location = '" . Url::toRoute('stockout/index') . "';});");
        }
        return $this->render('create', ['model' => $model, 'menu_id' => $this->menu_id]);
    }

    /**
     * Updates an existing StockOut model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @param string $uuid
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($uuid) {
        $model = $this->findModel($uuid);

        $Product = new Product();
        $model->product_name = $Product->getname($model->product_id);
        $model->updated_dt = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            if (($modelStockLog = StockLog::findOne(['table_related' => 'stock_out', 'data_id' => $model->id])) !== null) {
                $modelStockLog->owner_type = $model->owner_type;
                $modelStockLog->owner_id = $model->owner_id;
                $modelStockLog->save();
            }

            $Log = new Log();
            $Log->insertlog($this->menu_id, 'update', $model->uuid);
            $this->getView()->registerJs("swal.fire({position: 'center-center',type: 'success',title: 'Data has been saved',showConfirmButton: false,timer: 4000,}).then(function(result){window.location = '" . Url::toRoute('stockout/index') . "';});");
        }
        return $this->render('update', ['model' => $model, 'menu_id' => $this->menu_id]);
    }

    /**
     * Deletes an existing StockOut model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @param string $uuid
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id, $uuid) {
        $this->findModel($id, $uuid)->delete();
        return $this->redirect(['index']);
    }

    /**
     * Finds the StockOut model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @param string $uuid
     * @return StockOut the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($uuid) {
        if (($model = StockOut::findOne(['uuid' => $uuid])) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }

    public function actionListproduct() {
        $com_id = $_POST['com_id'];
        $output = '';
        $model = Product::find()
                        ->where(['=', 'status', '1'])
                        ->andwhere(['=', 'com_id', $com_id])
                        ->orderBy(['name' => SORT_ASC])->all();
        if (!empty($model)) {
            foreach ($model as $row) {
                $output .= '<option value="' . $row->id . '">' . $row->name . '</option>';
            }
        }
        return $output;
    }

    public function actionSearchproduct() {
        $search_product = $_POST['search_product'];
        $output = '';
        $model = Product::find()
                        ->where(['like', 'name', $search_product])
                        ->andwhere(['=', 'status', '1'])
                        ->orderBy(['name' => SORT_ASC])->all();
        if (!empty($model)) {
            foreach ($model as $row) {
                $selectvalue = $row->id . '<--->' . addslashes($row->name);
                $output .= '<div class="mt-2"><button type="button" class="btn btn-outline-primary btn-sm" onclick="selectproduct(\'' . $selectvalue . '\')">Select</button> ' . $row->name . '</div>';
            }
        }
        return $output;
    }

    public function actionGetownerid() {
        $product_id = $_POST['product_id'];
        $owner_type = $_POST['owner_type'];
        $output = '';
        if ($owner_type == 'company') {
            $model = Company::find()->orderBy(['name' => SORT_ASC])->all();
            if (!empty($model)) {
                foreach ($model as $row) {
                    $output .= '<option value="' . $row->id . '">' . $row->name . '</option>';
                }
            }
        }
        if ($owner_type == 'vendor') {
            if (($model = Product::findOne($product_id)) !== null) {
                $Vendor = new Vendor();
                $output = '<option value="' . $model->vendor_id . '">' . $Vendor->getname($model->vendor_id) . '</option>';
            }
        }

        return $output;
    }

}