Your IP : 216.73.216.79


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

<?php

namespace app\controllers;

use Yii;
use app\models\StockIn;
use app\models\StockInSearch;
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 yii\web\UploadedFile;
use app\models\Product;

/**
 * StockinController implements the CRUD actions for StockIn model.
 */
class StockinController extends Controller {

    public $menu_id = 40;

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

    /**
     * Displays a single StockIn 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),]);
    }

    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]);
    }

    public function actionGetproductlist() {
        $brand_id = $_POST['brand_id'];
        $output = '';
        $modelProduct = Product::find()->where(['brand_id' => $brand_id])->andwhere(['status' => 1])->orderBy(['name' => SORT_ASC])->all();
        if (!empty($modelProduct)) {
            $output .= '<div class="row">';
            foreach ($modelProduct as $rowProduct) {

                $output .= '<div class="col-6">
                        <label class="kt-checkbox">
                            <input type="checkbox" value="' . $rowProduct->id . '" class="product_id">
                            ' . $rowProduct->name . '
                            <label class="kt-badge kt-badge--inline kt-badge--dark ml-3">' . $rowProduct->sku . '</label>
                            <span></span>
                        </label>
                        <input type="hidden" id="modal_product_name_' . $rowProduct->id . '" value="' . $rowProduct->name . '">
                        <input type="hidden" id="modal_product_sku_' . $rowProduct->id . '" value="' . $rowProduct->sku . '">
                    </div>';
            }
            $output .= '</div><div class="kt-font-bold kt-font-danger" id="error_message">Please select at least one (1) product</div>';
        }

        return $output;
    }

    /**
     * Creates a new StockIn model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate($com_id = '', $vendor_id = '', $brand_id = '', $product_id = '', $owner_type = '') {
        $model = new StockIn();
        $model->owner_type = 'company';

        if (!empty($com_id))
            $model->company_id = $com_id;
        if (!empty($vendor_id))
            $model->vendor_id = $vendor_id;
        if (!empty($brand_id))
            $model->brand_id = $brand_id;
        if (!empty($product_id))
            $model->product_id = $product_id;
        if (!empty($owner_type))
            $model->owner_type = $owner_type;

        $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->date_arrived = date("Y-m-d");
        $model->data_from = 'crm';
        if ($model->load(Yii::$app->request->post())) {

            $model->file = UploadedFile::getInstance($model, 'file');
            if (!empty($model->file)) {
                $file_name = 'uploads/delivery_order/' . $model->product_id . '_' . date("YmdHis") . '.' . $model->file->extension;
                if ($model->file->saveAs($file_name))
                    $model->do_file = Url::base(true) . '/' . $file_name;
            }

            if ($model->owner_type == 'vendor')
                $model->owner_id = $model->vendor_id;
            if ($model->owner_type == 'company')
                $model->owner_id = $model->company_id;

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

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


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

    /**
     * Updates an existing StockIn 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);
        $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->file = UploadedFile::getInstance($model, 'file');
            if (!empty($model->file)) {
                $file_name = 'uploads/delivery_order/' . $model->product_id . '_' . date("YmdHis") . '.' . $model->file->extension;
                if ($model->file->saveAs($file_name))
                    $model->do_file = Url::base(true) . '/' . $file_name;
            }
            if ($model->save()) {
                Yii::$app->db->createCommand("UPDATE stock_log SET do_no='$model->do_no' WHERE table_related='stock_in' AND data_id=" . $model->id)->execute();

                $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: 1500,}).then(function(result){window.location = '" . Url::toRoute('stockin/index') . "';});");
            }
        }
        return $this->render('update', ['model' => $model, 'menu_id' => $this->menu_id]);
    }

    /**
     * Deletes an existing StockIn 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 StockIn 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 StockIn the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($uuid) {
        if (($model = StockIn::findOne(['uuid' => $uuid])) !== null) {
            return $model;
        }

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

    public function actionUpdatesoldout() {
        $id = $_POST['id'];
        $status = 'failed';
        if (($model = StockIn::findOne($id)) !== null) {
            $model->soldout = '1';
            $model->updated_dt = date("Y-m-d H:i:s");
            $model->updated_by = Yii::$app->user->identity->id;
            if ($model->save(false)) {
                $status = 'success';
                $Log = new Log();
                $Log->insertlog($this->menu_id, 'update', $model->uuid);
            }
        }
        return $status;
    }

}