Your IP : 216.73.216.79


Current Path : /var/www/html/crm/models/
Upload File :
Current File : /var/www/html/crm/models/Cashflow.php

<?php

namespace app\models;

use Yii;
use app\models\ParamList;

/**
 * This is the model class for table "cashflow".
 *
 * @property int $id
 * @property string $uuid
 * @property string $type
 * @property string $amount
 * @property string $remarks
 * @property string $file_url
 * @property string $created_dt
 * @property int $created_by
 * @property string $updated_dt
 * @property int $updated_by
 */
class Cashflow extends \yii\db\ActiveRecord {

    public $file;

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

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['amount', 'type', 'flow_dt'], 'required'],
            [['remarks'], 'string'],
            [['created_dt', 'updated_dt', 'file', 'uuid'], 'safe'],
            [['created_by', 'updated_by'], 'integer'],
            [['amount'], 'number'],
            [['file'], 'file'],
            ['type', 'checktype'],
            [['type', 'amount', 'file_url'], 'string', 'max' => 255],
            ['remarks', 'required', 'when' => function($model) {
                    return $model->type == 'withdraw';
                }, 'enableClientValidation' => false],
            ['file', 'required', 'on' => 'create', 'when' => function($model) {
                    return $model->type == 'withdraw';
                }, 'enableClientValidation' => false]
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function checktype() {
        if ($this->type == 'opening' || $this->type == 'closing') {
            if (empty($this->id)) {
                $modelRecord = static::find()->where(['=', 'type', $this->type])->andwhere(['=', 'flow_dt', $this->flow_dt])->all();
                if (!empty($modelRecord)) {
                    $this->addError('type', 'This type already added for the same transaction date');
                }
            } else {
                $modelRecord = static::find()->where(['=', 'type', $this->type])->andwhere(['=', 'flow_dt', $this->flow_dt])->andwhere(['!=', 'id', $this->id])->all();
                if (!empty($modelRecord)) {
                    $this->addError('type', 'This type already added for the same transaction date');
                }
            }
        }
    }

    public function attributeLabels() {
        return [
            'id' => 'ID',
            'uuid' => 'Uuid',
            'type' => 'Type',
            'amount' => 'Amount',
            'remarks' => 'Remarks',
            'file_url' => 'Upload Attachment',
            'file' => 'Upload Attachment',
            'created_dt' => 'Date & Time',
            'created_by' => 'Recorded By',
            'updated_dt' => 'Updated Dt',
            'updated_by' => 'Updated By',
            'flow_dt' => 'Transaction Date',
        ];
    }

    public function report($start_date = '', $end_date = '') {
        $output = '<table style="width:100%">
            <tr>
                <td align="center">
                    <h4>Cash Reporting</h4>
                    <h4>' . date("j M Y", strtotime($start_date)) . ' until ' . date("j M Y", strtotime($end_date)) . '</h4>
                </td>
            </tr>
        </table><br>';


        $ParamList = new ParamList();
        $startTime = strtotime($start_date);
        $endTime = strtotime($end_date);
        for ($i = $startTime; $i <= $endTime; $i = $i + 86400) {
            $totalDebit = 0;
            $totalCredit = 0;
            $thisDate = date('Y-m-d', $i);
            $opening_cash = '0.00';
            $closing_cash = '0.00';
            $other_transaction = '';

            $today_sales = '0.00';
            $querySales = Yii::$app->db->createCommand("SELECT REPLACE(sales_item.amount,  ',', '') FROM sales INNER JOIN sales_item ON sales.id=sales_item.parent_id WHERE sales.deleted_status=0 AND sales_dt='$thisDate' AND sales_item.type='saleitem01'")->queryScalar();
            if (!empty($querySales)) {
                $totalCredit += $querySales;
                $today_sales = number_format((float) $querySales, 2, '.', ',');
            }

            $model = static::find()->where(['=', 'flow_dt', $thisDate])->orderBy(['id' => SORT_ASC])->all();
            if (!empty($model)) {
                foreach ($model as $row) {
                    if ($row->type == 'opening') {
                        $totalCredit += $row->amount;
                        $opening_cash = $row->amount;
                    } else if ($row->type == 'closing')
                        $closing_cash = $row->amount;
                    else {
                        $descr = $ParamList->getlabel(39, $row->type);
                        if (!empty($row->remarks))
                            $descr .= ' : ' . $row->remarks;
                        $debit_amount = '';
                        $credit_amount = '';
                        $other_amount = '';
                        if ($row->type == 'deposit') {
                            $credit_amount = '+ RM ' . number_format((float) $row->amount, 2, '.', ',');
                            $totalCredit += $row->amount;
                        } else if ($row->type == 'withdraw') {
                            $debit_amount = '- RM ' . number_format((float) $row->amount, 2, '.', ',');
                            $totalDebit += $row->amount;
                        } else
                            $other_amount = 'RM ' . number_format((float) $row->amount, 2, '.', ',');
                        $other_transaction .= '<tr>
                                <td>' . $descr . '</td>
                                <td>' . $debit_amount . '</td>
                                <td>' . $credit_amount . '</td>
                                <td>' . $other_amount . '</td>
                            </tr>';
                    }
                }
            }

            $output .= '<h6>' . date("j M Y", strtotime($thisDate)) . '</h6>
            <table class="table mb-0 table-borderlessx table-striped table-hover table-custom">
                <thead>
                    <tr>
                        <th width="55%">Description</th>
                        <th width="15%">Debit</th>
                        <th width="15%">Credit</th>
                        <th width="15%">Others</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Opening Cash</td>
                        <td></td>
                        <td>+ RM ' . number_format((float) $opening_cash, 2, '.', ',') . '</td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>Sales Collection : Cash Sales</td>
                        <td></td>
                        <td>+ RM ' . $today_sales . '</td>
                        <td></td>
                    </tr>
                    ' . $other_transaction . '
                    <tr>
                        <td>Closing Cash</td>
                        <td></td>
                        <td></td>
                        <td>RM ' . number_format((float) $closing_cash, 2, '.', ',') . '</td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <th>Total</th>
                        <th>- RM ' . number_format((float) $totalDebit, 2, '.', ',') . '</th>
                        <th>+ RM ' . number_format((float) $totalCredit, 2, '.', ',') . '</th>
                        <th></th>
                    </tr>
                </tfoot>
            </table>';

            $final_status = '';
            if ($closing_cash !== '0.00') {
                $balance_cash = $totalCredit - $totalDebit;
                if ($balance_cash == $closing_cash) {
                    $final_status = '<font color="green">Closing Cash for ' . date("j M Y", strtotime($thisDate)) . ' are correct</font>';
                } else if ($balance_cash > $closing_cash) {
                    $diff_cash = $balance_cash - $closing_cash;
                    $final_status = '<font color="red">Closing Cash for ' . date("j M Y", strtotime($thisDate)) . ' <b>-RM ' . number_format((float) $diff_cash, 2, '.', ',') . '</b></font>';
                } else if ($balance_cash < $closing_cash) {
                    $diff_cash = $closing_cash - $balance_cash;
                    $final_status = '<font color="blue">Closing Cash for ' . date("j M Y", strtotime($thisDate)) . ' <b>+RM ' . number_format((float) $diff_cash, 2, '.', ',') . '</b></font>';
                }
            } else {
                $final_status = '<font color="red">Closing cash record not found</font>';
            }

            $output .= '<div style="text-align:right;"><h6>' . $final_status . '</h6></div>';
            //$output.='<br>total in='.$totalCredit.'<br>closing='.$closing_cash;
        }


        //status - same, short, over
        return $output;
    }

}