Your IP : 216.73.216.124


Current Path : /var/www/html/crm/controllers/
Upload File :
Current File : /var/www/html/crm/controllers/StocktransferController.php

<?php

namespace app\controllers;

use Yii;
use app\models\StockTransfer;
use app\models\StockTransferSearch;
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\Vendor;
use app\models\Company;
use app\models\Product;
use app\models\StockIn;
use app\models\StockOut;
use app\models\StockLog;
use app\models\EasStockTransfer;
use app\models\EasStock;
use app\models\EasStockAdjustment;
use app\models\EasStockLog;

/**
 * StocktransferController implements the CRUD actions for StockTransfer model.
 */
class StocktransferController extends Controller {

    public $menu_id = 75;

    /**
     * {@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 StockTransfer models.
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new StockTransferSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'menu_id' => $this->menu_id,]);
    }

    /**
     * Displays a single StockTransfer 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 StockTransfer model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate($stockout_owner_type = '', $stockout_owner_id = '', $stockout_product_name = '', $stockout_product_id = '') {
        $model = new StockTransfer();
        $model->transfer_dt = date("Y-m-d");
        $model->created_dt = date("Y-m-d H:i:s");
        $model->created_by = Yii::$app->user->identity->id;
        $model->uuid = Uuid::uuid4();
        $model->deleted_status = '0';

        if (!empty($stockout_owner_type))
            $model->stockout_owner_type = $stockout_owner_type;
        if (!empty($stockout_owner_id))
            $model->stockout_owner_id = $stockout_owner_id;
        if (!empty($stockout_product_name))
            $model->stockout_product_name = $stockout_product_name;
        if (!empty($stockout_product_id))
            $model->stockout_product_id = $stockout_product_id;

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

            $status = "success";

            $modelStockIn = new StockIn();
            $addStockIn = $modelStockIn->add($model->stockin_owner_type, $model->stockin_owner_id, $model->stockin_product_id, $model->stockin_quantity, $model->transfer_dt, $model->remarks);
            if (!empty($addStockIn))
                $model->stockin_stock_id = $addStockIn;
            else
                $status = "failed";

            $modelStockOut = new StockOut();
            $addStockOut = $modelStockOut->add($model->stockout_owner_type, $model->stockout_owner_id, $model->stockout_product_id, $model->stockout_quantity, $model->transfer_dt, $model->remarks);
            if (!empty($addStockIn))
                $model->stockout_stock_id = $addStockOut;
            else
                $status = "failed";

            $modelStockTransfer = new EasStockTransfer();
            $addStockTransfer = $modelStockTransfer->add($model->stockin_stock_id, $model->stockout_stock_id, $model->transfer_dt, $model->remarks, $model->stockout_quantity, $model->stockin_quantity, $model->stockout_product_id, $model->stockin_product_id);
            if (!empty($addStockTransfer))
                $model->eas_id = $addStockTransfer;
            else
                $status = "failed";

            if ($status == 'success') {
                if ($model->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('stocktransfer/index') . "';});");
                }
            }
        }
        return $this->render('create', ['model' => $model, 'menu_id' => $this->menu_id]);
    }

    /**
     * Updates an existing StockTransfer 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($id, $uuid) {
        $model = $this->findModel($id, $uuid);
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id, 'uuid' => $model->uuid]);
        }
        return $this->render('update', ['model' => $model,]);
    }

    /**
     * Deletes an existing StockTransfer 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() {
        $status = 'failed';
        if (($model = StockTransfer::findOne(['id' => $_POST['id']])) !== null) {

            /* ------ STOCK IN ------ */
            if (($modelStockIn = StockIn::findOne($model->stockin_stock_id)) !== null) {
                $stockin_id = $modelStockIn->id;
                $stockin_uuid = $modelStockIn->uuid;

                //delete row from exim08_cms > stock_in
                $modelStockIn->delete();

                //delete row from exim08_cms > stock_log (stock_in)
                StockLog::findOne(['data_id' => $stockin_id, 'action_type' => 'stockin', 'table_related' => 'stock_in'])->delete();

                //delete row from exim08_dev > stock
                EasStock::findOne(['uuid' => $stockin_uuid])->delete();

                //delete row from exim08_dev > stock_log (stock)
                EasStockLog::findOne(['data_uuid' => $stockin_uuid, 'action_type' => 'add stock'])->delete();
            }

            /* ------ STOCK OUT ------ */
            if (($modelStockOut = StockOut::findOne($model->stockout_stock_id)) !== null) {
                $stockout_id = $modelStockOut->id;
                $stockout_uuid = $modelStockOut->uuid;

                //delete row from exim08_cms > stock_out
                $modelStockOut->delete();

                //delete row from exim08_cms > stock_log (stock_out)
                StockLog::findOne(['data_id' => $stockout_id, 'action_type' => 'stockout', 'table_related' => 'stock_out'])->delete();

                //delete row from exim08_dev > stock_adjustment
                EasStockAdjustment::findOne(['uuid' => $stockout_uuid])->delete();

                //delete row from exim08_dev > stock_log (stock_adjustment)
                EasStockLog::findOne(['data_uuid' => $stockout_uuid, 'action_type' => 'add adjustment'])->delete();
            }

            /* ------ STOCK TRANSFER ------ */
            if (($modelStockTransfer = EasStockTransfer::findOne($model->eas_id)) !== null) {
                $stocktransfer_uuid = $modelStockTransfer->uuid;

                //delete row from exim08_dev > stock_transfer
                $modelStockTransfer->delete();

                //delete row from exim08_dev > stock_log (stock_transfer)
                EasStockLog::findOne(['data_uuid' => $stocktransfer_uuid, 'action_type' => 'transfer stock'])->delete();
            }

            //update exim08_cms > stock_transfer > deleted_status
            $model->deleted_status = '1';
            if ($model->save(false))
                $status = 'success';
        }
        return $status;
    }

    /**
     * Finds the StockTransfer 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 StockTransfer the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id, $uuid) {
        if (($model = StockTransfer::findOne(['id' => $id, 'uuid' => $uuid])) !== null) {
            return $model;
        }
        throw new NotFoundHttpException('The requested page does not exist.');
    }

    public function actionGetownerlist() {
        $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') {
            $model = Vendor::find()->where(['=', 'status', '1'])->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-label-primary btn-sm" onclick="selectproduct(\'' . $selectvalue . '\')">Select</button> (' . $row->sku . ')' . $row->name . '</div>';
            }
        }
        return $output;
    }

    public function actionSearchproduct2() {
        $Product = new Product();
        $product_id = $_POST['product_id'];
        $product_brand = $Product->getbrand($product_id);
        $output = '';
        //$output = '<option value="' . $product_id . '">(' . $Product->getsku($product_id) . ') ' . $Product->getname($product_id) . '</option>';
        $model = Product::find()->where(['=', 'brand_id', $product_brand])->andwhere(['status' => '1'])->orderBy(['name' => SORT_ASC])->all();
        if (!empty($model)) {
            foreach ($model as $row) {
                $output .= '<option value="' . $row->id . '">(' . $row->sku . ') ' . $row->name . '</option>';
            }
        }
        return $output;
    }

}