Your IP : 216.73.216.79


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

<?php

namespace app\controllers;

use Yii;
use app\models\PaymentVoucher;
use app\models\PaymentVoucherSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\Url;
use app\models\Log;
use app\models\LogSearch;
use yii\web\UploadedFile;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use app\models\User;
use app\models\Vendor;
use app\models\Customer;
use app\models\Company;
use yii\helpers\Html;
use app\models\ParamList;
use yii\helpers\ArrayHelper;
use app\models\PaymentVoucherItem;
use kartik\mpdf\Pdf;

/**
 * PaymentvoucherController implements the CRUD actions for PaymentVoucher model.
 */
class PaymentvoucherController extends Controller {

    public $menu_id = 67;

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

    /**
     * Displays a single PaymentVoucher 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 PaymentVoucher model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $Company = new Company();
        $model = new PaymentVoucher();
        $model->uuid = Uuid::uuid4();
        $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->receiver_type = 'payuser04';
        $model->deleted_status = '0';
        if ($model->load(Yii::$app->request->post())) {
            $countdoc = (int) PaymentVoucher::find()->where(['company_id' => $model->company_id])->count() + 1;
            $model->doc_no = $Company->getshortcode($model->company_id) . '/PV/' . date("y") . '/' . sprintf("%05d", $countdoc);
            if ($model->save()) {

                //insert new item into payment_voucher_item
                if (!empty($_POST['new_item'])) {
                    $item_arr = $_POST['new_item'];
                    foreach ($item_arr as $item_no) {
                        $model2 = new PaymentVoucherItem();
                        $model2->doc_id = $model->id;
                        $model2->type = $_POST['type_new_' . $item_no];
                        $model2->ref_no = $_POST['ref_new_' . $item_no];
                        $model2->remarks = $_POST['remarks_new_' . $item_no];
                        $model2->total = $_POST['total_new_' . $item_no];

                        $model2->attachment = UploadedFile::getInstanceByName('attachment_new_' . $item_no);
                        if (!empty($model2->attachment)) {
                            $file_name = date("YmdHis") . $item_no . '.' . $model2->attachment->extension;
                            if ($model2->attachment->saveAs('uploads/payment_voucher/' . $file_name))
                                $model2->attachment = $file_name;
                        }

                        $model2->created_dt = date("Y-m-d H:i:s");
                        $model2->created_by = Yii::$app->user->identity->id;
                        $model2->updated_dt = date("Y-m-d H:i:s");
                        $model2->updated_by = Yii::$app->user->identity->id;
                        $model2->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 = '" . Url::toRoute('paymentvoucher/index') . "';});");
            }
        }
        return $this->render('create', ['model' => $model, 'menu_id' => $this->menu_id]);
    }

    /**
     * Updates an existing PaymentVoucher 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())) {
            if ($model->save()) {

                //insert new item into payment_voucher_item

                if (!empty($_POST['new_item'])) {
                    $item_arr = $_POST['new_item'];
                    foreach ($item_arr as $item_no) {
                        $model2 = new PaymentVoucherItem();
                        $model2->doc_id = $model->id;
                        $model2->type = $_POST['type_new_' . $item_no];
                        $model2->ref_no = $_POST['ref_new_' . $item_no];
                        $model2->remarks = $_POST['remarks_new_' . $item_no];
                        $model2->total = $_POST['total_new_' . $item_no];

                        $model2->attachment = UploadedFile::getInstanceByName('attachment_new_' . $item_no);
                        if (!empty($model2->attachment)) {
                            $file_name = date("YmdHis") . $item_no . '.' . $model2->attachment->extension;
                            if ($model2->attachment->saveAs('uploads/payment_voucher/' . $file_name))
                                $model2->attachment = $file_name;
                        }

                        $model2->created_dt = date("Y-m-d H:i:s");
                        $model2->created_by = Yii::$app->user->identity->id;
                        $model2->updated_dt = date("Y-m-d H:i:s");
                        $model2->updated_by = Yii::$app->user->identity->id;
                        $model2->save();
                    }
                }

                //update item into payment_voucher_item
                if (!empty($_POST['old_item'])) {
                    $item_arr2 = $_POST['old_item'];
                    foreach ($item_arr2 as $item_no2) {
                        if (($model3 = PaymentVoucherItem::findOne($item_no2)) !== null) {

                            $model3->type = $_POST['type_old_' . $item_no2];
                            $model3->ref_no = $_POST['ref_old_' . $item_no2];
                            $model3->remarks = $_POST['remarks_old_' . $item_no2];
                            $model3->total = $_POST['total_old_' . $item_no2];
                            $attachment_original = $model3->attachment;

                            $model3->attachment = UploadedFile::getInstanceByName('attachment_old_' . $item_no2);
                            if (!empty($model3->attachment)) {
                                $file_name = date("YmdHis") . $item_no2 . '.' . $model3->attachment->extension;
                                if ($model3->attachment->saveAs('uploads/payment_voucher/' . $file_name))
                                    $model3->attachment = $file_name;
                            } else
                                $model3->attachment = $attachment_original;

                            $model3->updated_dt = date("Y-m-d H:i:s");
                            $model3->updated_by = Yii::$app->user->identity->id;
                            $model3->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 = '" . Url::toRoute('paymentvoucher/index') . "';});");
            }
        }
        return $this->render('update', ['model' => $model, 'menu_id' => $this->menu_id]);
    }

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

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

    public function actionGetreceiverdetails($type, $id) {
        $name = '';
        $address1 = '';
        $address2 = '';
        $postcode = '';
        $city = '';
        $state = '';
        $contact_person = '';
        $contact_number = '';
        $account_bank = '';
        $account_name = '';
        $account_no = '';

        //company
        if ($type == 'payuser02') {
            if (($model = Company::findOne($id)) !== null) {
                $name = $model->name;
                $address1 = $model->address1;
                $address2 = $model->address2;
                $postcode = $model->postcode;
                $city = $model->city;
                $state = $model->state;
                $contact_person = $model->contact_name;
                $contact_number = $model->contact_no;
                $account_bank = $model->account_bank;
                $account_name = $model->account_name;
                $account_no = $model->account_number;
            }
        }

        //Customer
        if ($type == 'payuser03') {
            if (($model = Customer::findOne($id)) !== null) {
                $name = $model->name;
                $address1 = $model->address1;
                $address2 = $model->address2;
                $postcode = $model->postcode;
                $city = $model->city;
                $state = $model->state;
                $contact_person = $model->contact_name;
                $contact_number = $model->contact_no;
                $account_bank = $model->account_bank;
                $account_name = $model->account_name;
                $account_no = $model->account_number;
            }
        }
        //Staff
        if ($type == 'payuser01') {
            if (($model = User::findOne($id)) !== null) {
                $name = $model->name;
                $address1 = $model->address1;
                $address2 = $model->address2;
                $postcode = $model->postcode;
                $city = $model->city;
                $state = $model->state;
                $contact_person = $model->name;
                $contact_number = "+601" . $model->phone;
                $account_bank = $model->account_bank;
                $account_name = $model->account_name;
                $account_no = $model->account_number;
            }
        }

        //Vendor
        if ($type == 'payuser04') {
            if (($model = Vendor::findOne($id)) !== null) {
                $name = $model->name;
                $address1 = $model->address1;
                $address2 = $model->address2;
                $postcode = $model->postcode;
                $city = $model->city;
                $state = $model->state;
                $contact_person = $model->contact_name;
                $contact_number = $model->contact_no;
                $account_bank = $model->account_bank;
                $account_name = $model->account_name;
                $account_no = $model->account_number;
            }
        }


        return $name . '<--->' .
                $address1 . '<--->' .
                $address2 . '<--->' .
                $postcode . '<--->' .
                $city . '<--->' .
                $state . '<--->' .
                $contact_person . '<--->' .
                $contact_number . '<--->' .
                $account_bank . '<--->' .
                $account_name . '<--->' .
                $account_no;
    }

    public function actionAdditem($no) {
        //type
        //upload doc
        //delete button
        $ParamList = new ParamList();

        $output = '<tr id="new_item_' . $no . '">
                <td>' . Html::input('hidden', 'new_item[]', $no) . Html::dropDownList('type_new_' . $no, null, $ParamList->getparamlist(34), ['id' => 'type_new_' . $no, 'class' => 'form-control kt-selectpicker', 'data-live-search' => 'true']) . '</td>
                <td>' . Html::input('text', 'ref_new_' . $no, '', $options = ['class' => 'form-control item_input']) . '</td>
                <td>' . Html::input('text', 'remarks_new_' . $no, '', $options = ['class' => 'form-control item_input']) . '</td>
                <td>' . Html::input('text', 'total_new_' . $no, '', $options = ['id' => 'total_new_' . $no, 'class' => 'form-control item_input', 'onkeyup' => 'checkvalue(this.id)']) . '</td>
                <td>' . Html::input('file', 'attachment_new_' . $no, '', $options = ['class' => 'item_input']) . '</td>
                <td><span class="kt-font-danger icon-custom" title="Delete"><i class="far fa-trash-alt fa-lg" style="cursor:pointer;" onclick="deletenewitem(' . $no . ')"></i></span></td>
            </tr>';
        return $output;
    }

    public function actionDownload($uuid) {
        $ParamList = new ParamList();
        $User = new User();
        $model = $this->findModel($uuid);
        if (!empty($model)) {
            $modelcom = Company::findOne($model->company_id);
            if (!empty($modelcom)) {
                $header = '<table id="table-header" border="0" width="100%" cellpadding="0" cellspacing="0">
                    <tr>
                        <td><img src="uploads/' . $modelcom->logo . '" height="50"></td>
                        <td align="right"><b>' . $modelcom->tagline . '</b><br/>' . $modelcom->website . '</td>
                    </tr>
                </table>';

                $content = '<br><table border="0" width="100%" cellpadding="0" cellspacing="0">
                        <tr>
                            <td width="50%" valign="top">
                                <b>PAY TO</b>
                                <table cellpadding="0" cellspacing="0">
                                    <tr>
                                        <td valign="top" colspan="3">' . $model->receiver_name . '</td>
                                    </tr>
                                    <tr><td valign="top" colspan="3">' . $model->receiver_address1 . ' ' . $model->receiver_address2 . ' ' . $model->receiver_postcode . ' ' . $model->receiver_city . ' ' . $ParamList->getlabel(3, $model->receiver_state) . '</td>
                                    </tr>';
                if ($model->receiver_type != 'payuser01') {
                    $content .= '<tr>
                                        <td valign="top" colspan="3">
                                            Contact Person : ' . $model->receiver_pic . '&nbsp;&nbsp;&nbsp;&nbsp;
                                            Tel : ' . $model->receiver_phone . '
                                        </td>
                                    </tr>';
                }
                $content .= '<tr><td valign="top" colspan="3"><p><br><b>Account Bank ' . $ParamList->getlabel(31, $model->receiver_account_bank) . '</b></p> ' . $model->receiver_account_number . ' (' . $model->receiver_account_name . ')</td>
                                    </tr>
                                    
                                </table>
                            </td>
                            <td width="50%" valign="top" align="right">
                                <table cellpadding="0" cellspacing="0" style="padding-right:1em;">
                                    <tr><td colspan="3" align="center"><h4><b>PAYMENT VOUCHER</b></h4><br/><br/></td></tr>
                                    <tr>
                                        <td valign="top">Voucher No</td>
                                        <td valign="top">&nbsp;&nbsp;:&nbsp;&nbsp;</td>
                                        <td valign="top" align="left">' . $model->doc_no . '</td>
                                    </tr>
                                    <tr>
                                        <td valign="top">Date</td>
                                        <td valign="top">&nbsp;&nbsp;:&nbsp;&nbsp;</td>
                                        <td valign="top" align="left">' . date("dS M Y", strtotime($model->created_dt)) . '</td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>';

                $grandtotal = 0;


                $content .= '<br><table width="100%" cellpadding="5" cellspacing="1" border="0" style="background-color:black;" id="table-product">
                    <thead>
                      <tr>
                        <th width="5%">No</th>
                        <th align="left">Type</th>
                        <th align="left">Reference No.</th>
                        <th align="left">Remarks</th>
                        <th  align="left" width="15%">Total</th>
                      </tr>
                    </thead>
                  <tbody>';

                $modelItem = PaymentVoucherItem::find()->where(['doc_id' => $model->id])->orderBy(['id' => SORT_ASC])->all();
                if (!empty($modelItem)) {
                    $countItem = 0;
                    foreach ($modelItem as $rowItem) {
                        $countItem++;
                        $content .= '<tr>
                            <td align="center">' . $countItem . '</td>
                            <td>' . $ParamList->getlabel(34, $rowItem->type) . '</td>
                            <td>' . $rowItem->ref_no . '</td>
                            <td>' . $rowItem->remarks . '</td>
                            <td>RM ' . number_format((float) $rowItem->total, 2, '.', ',') . '</td>
                        </tr>';
                        $grandtotal += $rowItem->total;
                    }
                } else {
                    $content .= '<tr><td colspan="5">No record found</td></tr>';
                }

                $content .= '</tbody><tfoot>';

                $content .= '<tr>
                  <td colspan="4" align="right">Grand Total</td>
                  <td><b>RM ' . number_format((float) $grandtotal, 2, '.', ',') . '</b></td>
                  </tr>';

                $content .= '</tfoot></table>';

                if (!empty($model->remarks))
                    $content .= '<br>Remarks : ' . $model->remarks . '<br>';

                $content .= Yii::$app->sharedfunction->convertMoneyToText($grandtotal);

                $company_stamp = '<tr><td align="center" height="110"></td></tr>';
                if (!empty($modelcom->stamp))
                    $company_stamp = '<tr><td align="center"><img src="uploads/' . $modelcom->stamp . '" height="100"></td></tr>';

                $content .= '<table width="100%">
                        ' . $company_stamp . '
                        <tr>
                            <td align="center" style="border-top:1px solid black;" width="30%">Authorized Signature</td>
                            <td></td>
                            <td align="center" style="border-top:1px solid black;" width="30%">Customer Chop and sign</td>
                        </tr>
                    </table>';

                $footer = '<table id="table-footer" border="0" width="100%" cellpadding="0" cellspacing="0">
                    <tr><td align="center">' . $modelcom->address1 . ' ' .
                        $modelcom->address2 . ' ' .
                        $modelcom->postcode . ' ' .
                        $modelcom->city . ' ' . $ParamList->getlabel(3, $modelcom->state) . '
                        </td>
                    </tr>
                </table>';
                // setup kartik\mpdf\Pdf component
                $pdf = new Pdf([
                    'mode' => Pdf::MODE_CORE,
                    'filename' => 'Payment Voucher ' . $model->doc_no . '.pdf',
                    'format' => Pdf::FORMAT_A4,
                    'orientation' => Pdf::ORIENT_PORTRAIT,
                    'destination' => Pdf::DEST_BROWSER,
                    'cssFile' => '@vendor/kartik-v/yii2-mpdf/src/assets/kv-mpdf-bootstrap.min.css',
                    'cssInline' => 'body,table td{font-size:11px;Arial, Helvetica, sans-serif;line-height:1.7em;}
                        #table-header{padding-bottom:10px;}
                        #table-footer{padding-top:10px;}
                        #table-product th{background-color:black;color:white;text-align:center;}
                        #table-product tr:nth-child(odd) td{background-color:white;}
                        #table-product tr:nth-child(even) td{background-color:whitesmoke;}',
                    'methods' => ['SetHeader' => [$header], 'SetFooter' => $footer],
                    'content' => $content,
                    'options' => ['setAutoTopMargin' => 'stretch'],
                ]);
                return $pdf->render();
            }
        }
    }

}