Your IP : 216.73.216.79


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

<?php

namespace app\controllers;

use Yii;
use app\models\InvoiceInbound;
use app\models\InvoiceInboundSearch;
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 yii\web\UploadedFile;

/**
 * InvoiceinboundController implements the CRUD actions for InvoiceInbound model.
 */
class InvoiceinboundController extends Controller {

    public $menu_id = 74;

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

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

    /**
     * Creates a new InvoiceInbound model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new InvoiceInbound();
        $model->scenario = 'create';
        $model->invoice_dt = date("Y-m-d");
        $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->payment_status = 'Pending';
        $model->deleted_status = '0';
        if ($model->load(Yii::$app->request->post())) {
            $model->doc_url = UploadedFile::getInstance($model, 'doc_url');
            if (!empty($model->doc_url)) {
                $file_name = date("YmdHis") . '.' . $model->doc_url->extension;
                if ($model->doc_url->saveAs('uploads/invoice_inbound/' . $file_name))
                    $model->doc_url = $file_name;
            }
            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: 1500,}).then(function(result){window.location = 'index.php?r=invoiceinbound/index';});");
            }
        }
        return $this->render('create', ['model' => $model, 'menu_id' => $this->menu_id]);
    }

    /**
     * Updates an existing InvoiceInbound model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @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;
        $original_doc = $model->doc_url;
        if ($model->load(Yii::$app->request->post())) {
            $model->doc_url = UploadedFile::getInstance($model, 'doc_url');
            if (!empty($model->doc_url)) {
                $file_name = date("YmdHis") . '.' . $model->doc_url->extension;
                if ($model->doc_url->saveAs('uploads/invoice_inbound/' . $file_name))
                    $model->doc_url = $file_name;
            }else {
                $model->doc_url = $original_doc;
            }
            if ($model->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: 1500,}).then(function(result){window.location = 'index.php?r=invoiceinbound/index';});");
            }
        }
        return $this->render('update', ['model' => $model, '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]);
    }

    /**
     * Deletes an existing InvoiceInbound model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    /*public function actionDelete($id) {
        $this->findModel($id)->delete();
        return $this->redirect(['index']);
    }*/
    public function actionDelete() {
        $id = $_POST['id'];
        $status = 'failed';
        $model = InvoiceInbound::findOne($id);
        if (!empty($model)) {
            $model->deleted_status = 1;
            if ($model->save(false)) {
                $Log = new Log();
                $Log->insertlog($this->menu_id, 'delete', $model->uuid);
                $status = 'success';
            }
        }
        return $status;
    }

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

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

    public function actionUpdatepaymentstatus() {
        $id = $_POST['id'];
        $status = 'failed';
        if (($model = InvoiceInbound::findOne($id)) !== null) {
            $model->payment_status = 'Paid';
            $model->payment_dt = date("Y-m-d");
            $model->updated_dt = date("Y-m-d H:i:s");
            $model->updated_by = Yii::$app->user->identity->id;
            if ($model->save()) {
                $status = 'success';
                $Log = new Log();
                $Log->insertlog($this->menu_id, 'update payment status', $model->uuid);
            }
        }
        return $status;
    }
    
    public function actionCalcpaymentdue() {
        $output = '';
        $invoice_term_arr = explode('_', $_POST['invoice_term']);

        if ($invoice_term_arr[0] == 'end') {
            $start_date = date("Y-m-t", strtotime($_POST['invoice_dt']));
            $output = date('Y-m-t', strtotime($start_date . ' + ' . $invoice_term_arr[1] . ' days'));
        } else {
            $start_date = date("Y-m-d", strtotime($_POST['invoice_dt']));
            $output = date('Y-m-d', strtotime($start_date . ' + ' . $invoice_term_arr[1] . ' days'));
        }

        return $output;
    }

}