Your IP : 216.73.216.79


Current Path : /var/www/html/dyna-crm/models/
Upload File :
Current File : /var/www/html/dyna-crm/models/BankTransaction.php

<?php

namespace app\models;

use Yii;
use yii\helpers\Url;
use app\models\User;
use app\models\Vendor;
use app\models\Customer;
use app\models\Company;
use app\models\BankReconciliation;
use app\models\Sales;

/**
 * This is the model class for table "bank_transaction".
 *
 * @property int $id
 * @property string $uuid
 * @property string|null $from_type
 * @property int|null $from_id
 * @property string|null $from_name
 * @property string|null $receiver_type
 * @property int|null $receiver_id
 * @property string|null $receiver_name
 * @property string|null $descr
 * @property string|null $method
 * @property string|null $total
 * @property string|null $transaction_dt
 * @property string|null $ref_no
 * @property string|null $attachment
 * @property string|null $remarks
 * @property string|null $created_dt
 * @property int|null $created_by
 * @property string|null $updated_dt
 * @property int|null $updated_by
 */
class BankTransaction extends \yii\db\ActiveRecord {

    public $file;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return 'bank_transaction';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['from_type', 'from_id', 'receiver_type', 'receiver_id', 'method', 'total', 'transaction_dt', 'descr'], 'required'],
            [['from_id', 'receiver_id', 'created_by', 'updated_by'], 'integer'],
            [['transaction_dt', 'created_dt', 'updated_dt', 'uuid', 'file', 'related_table', 'related_id'], 'safe'],
            [['remarks'], 'string'],
            [['from_type', 'receiver_type', 'descr', 'method', 'total', 'ref_no', 'attachment'], 'string', 'max' => 255],
            [['file'], 'file'],
            [['file'], 'required', 'on' => 'create'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'uuid' => 'Uuid',
            'from_type' => 'From User Type',
            'from_id' => 'Transaction From',
            'receiver_type' => 'Receiver User Type',
            'receiver_id' => 'Receiver',
            'descr' => 'Transaction Details',
            'method' => 'Method',
            'total' => 'Total',
            'transaction_dt' => 'Transaction Date',
            'ref_no' => 'Reference No.',
            'attachment' => 'Payment Slip',
            'remarks' => 'Remarks',
            'created_dt' => 'Created Dt',
            'created_by' => 'Created By',
            'updated_dt' => 'Updated Dt',
            'updated_by' => 'Updated By',
            'file' => 'Payment Slip'
        ];
    }

    public function getusername($type, $id) {
        $output = '';
        $model = [];
        if ($type == 'payuser02')
            $model = Company::findOne($id);
        if ($type == 'payuser03')
            $model = Customer::findOne($id);
        if ($type == 'payuser01')
            $model = User::findOne($id);
        if ($type == 'payuser04')
            $model = Vendor::findOne($id);
        if (!empty($model)) {
            $output = $model->name;
        }
        return $output;
    }

    public function reconciliation($start_date = '', $end_date = '', $view_type = 'pdf') {
        $output = '<table style="width:100%">
            <tr>
                <td align="center">
                    <h4>Payment Transaction</h4>
                    <h4>' . date("j M Y", strtotime($start_date)) . ' until ' . date("j M Y", strtotime($end_date)) . '</h4>
                </td>
            </tr>
        </table><br>
        <table class="table">
            <thead>
                <tr>
                    <th>Date</th>
                    <th>Description</th>
                    <th>Reference No</th>
                    <th>Debit</th>
                    <th>Credit</th>
                    <th>Verified</th>
                </tr>
            </thead>
            <tbody>';
        $sql = "SELECT * FROM (
            SELECT transaction_dt, descr, ref_no, total, 'credit' AS type, 
            'Payment from' AS payment_direct, from_type AS payment_user_type, from_id AS payment_user_id, 'bank_transaction' AS table_name, 
            related_table, related_id, remarks, attachment, id 
            FROM bank_transaction
            WHERE 
                    receiver_type='payuser02' AND 
                    receiver_id='1' AND
                    transaction_dt BETWEEN '$start_date' AND '$end_date'
            UNION
            SELECT transaction_dt, descr, ref_no, total, 'debit' AS type, 
            'Payment to' AS payment_direct, receiver_type AS payment_user_type, receiver_id AS payment_user_id, 'bank_transaction' AS table_name, 
            related_table, related_id, remarks, attachment, id
            FROM bank_transaction 
            WHERE 
                    from_type='payuser02' AND 
                    from_id='1' AND
                    transaction_dt BETWEEN '$start_date' AND '$end_date'
            UNION	
            SELECT sales.sales_dt AS  transaction_dt, CONCAT(sales.descr,' - Others Transaction') AS descr, '' AS ref_no, sales_item.amount AS total, 'credit' AS type,
            '' AS payment_direct, '' AS payment_user_type, '' AS payment_user_id, 'sales_item' AS table_name, 
            '' AS related_table, '' AS related_id, '' AS remarks, sales.attachment, sales_item.id
            FROM sales INNER JOIN sales_item ON sales.id=sales_item.parent_id
            WHERE
                sales.deleted_status=0 AND
                sales.com_id=1 AND
                sales_item.type='saleitem02' AND
                sales.sales_dt BETWEEN '$start_date' AND '$end_date'
              ) t ORDER BY transaction_dt DESC";


        $model = Yii::$app->db->createCommand($sql)->queryAll();
        if (!empty($model)) {
            foreach ($model as $row) {
                $debit = '';
                $credit = '';
                if ($row['type'] == 'debit')
                    $debit = '- RM ' . number_format((float) $row['total'], 2, '.', ',');
                if ($row['type'] == 'credit')
                    $credit = '+ RM ' . number_format((float) $row['total'], 2, '.', ',');
                $descr = '';
                if ($row['table_name'] == 'bank_transaction') {
                    if ($row['related_table'] == 'sales') {
                        if (($modelsales = Sales::findOne($row['related_id'])) !== null) {
                            $descr = $modelsales->descr . ' - Cash Sales ';
                        }
                        $descr .= '<br>' . $row['payment_direct'] . ' ' . $this->getusername($row['payment_user_type'], $row['payment_user_id']);
                    } else {
                        $descr = $row['descr'] . '<br>' . $row['payment_direct'] . ' ' . $this->getusername($row['payment_user_type'], $row['payment_user_id']);
                        if (!empty($row['remarks']))
                            $descr .= ' <br>' . $row['remarks'];
                    }
                    if (!empty($row['attachment']))
                        $descr .= '<br><a href="' . Url::base() . '/uploads/bank_transaction/' . $row['attachment'] . '" target="_blank">View receipt</a>';
                }if ($row['table_name'] == 'sales_item') {
                    $descr = $row['descr'];
                    if (!empty($row['attachment']))
                        $descr .= '<br><a href="' . Url::base() . '/uploads/sales/' . $row['attachment'] . '" target="_blank">View receipt</a>';
                }

                $status = '<font color="orange">Pending</font> <a href="#" onclick="create(\'' . $row['table_name'] . '\',' . $row['id'] . ')"><i class="far fa-edit"></i></a>';
                if (($modelreconciliation = BankReconciliation::findOne(['table_name' => $row['table_name'], 'data_id' => $row['id']])) !== null) {
                    if ($modelreconciliation->status == 'verified')
                        $status = '<font color="green">Verified</font> ';
                    if ($modelreconciliation->status == 'missing')
                        $status = '<font color="red">Missing</font>';

                    $status .= '<a href="#" onclick="update(\'' . $row['table_name'] . '\',' . $row['id'] . ',' . $modelreconciliation->id . ',\'' . $modelreconciliation->status . '\',\'' . $modelreconciliation->statement_dt . '\',\'' . addslashes($modelreconciliation->ref_no) . '\',\'' . addslashes($modelreconciliation->remarks) . '\')">
                            <i class="far fa-edit"></i>
                        </a>';
                }

                $output .= '<tr>
                        <td style="white-space: nowrap;">' . date("j M Y", strtotime($row['transaction_dt'])) . '</td>
                        <td>' . $descr . '</td>
                        <td style="white-space: nowrap;">' . $row['ref_no'] . '</td>
                        <td style="white-space: nowrap;">' . $debit . '</td>
                        <td style="white-space: nowrap;">' . $credit . '</td>
                        <td>' . $status . '</td>
                    </tr>';
            }
        }

        $output .= '</tbody>
        </table>';
        return $output;
    }

}