Your IP : 216.73.216.79


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

<?php

namespace app\models;

use Yii;
use app\models\DeliveryOrder;

/**
 * This is the model class for table "invoice".
 *
 * @property int $id
 * @property string $uuid
 * @property int $com_id
 * @property string $do_no
 * @property string $invoice_no
 * @property string $term
 * @property string $created_dt
 * @property int $created_by
 * @property string $updated_dt
 * @property int $updated_by
 */
class Invoice extends \yii\db\ActiveRecord {

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

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['uuid'], 'required'],
            [['com_id', 'created_by', 'updated_by'], 'integer'],
            [['created_dt', 'updated_dt', 'payment_status', 'tax', 'remarks', 'payment_due', 'grandtotal', 'paid_dt', 'doc_dt', 'payment_due',
            'customer_id', 'customer_name', 'customer_address1', 'customer_address2', 'customer_postcode',
            'customer_city', 'customer_state', 'customer_pic', 'customer_phone', 'po_ref', 'sales_pic'], 'safe'],
            [['do_no', 'invoice_no', 'term'], 'string', 'max' => 255],
            [['term', 'sales_pic'], 'required', 'on' => 'update'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'uuid' => 'Uuid',
            'com_id' => 'Company',
            'do_no' => 'Do No',
            'invoice_no' => 'Invoice No',
            'term' => 'Invoice Term',
            'created_dt' => 'Invoice Date',
            'created_by' => 'Created By',
            'updated_dt' => 'Updated Dt',
            'updated_by' => 'Updated By',
            'payment_status' => 'Payment Status',
            'tax' => 'Tax',
            'remarks' => 'Remarks',
            'payment_due' => 'Payment Due',
            'sales_pic' => 'Sales Person',
            'doc_dt' => 'Invoice Date',
            'customer_id' => 'Customer',
            'customer_address1' => 'Customer Address',
            'customer_postcode' => 'Postcode',
            'customer_city' => 'City',
            'customer_state' => 'State',
            'customer_pic' => 'Contact Person',
            'customer_phone' => 'Contact Number',
            'po_ref' => 'Customer PO ref ',
        ];
    }

    public function checkinvoice($do_no) {
        $output = '';
        if (($model = static::findOne(['do_no' => $do_no])) !== null) {
            $output = $model->uuid;
        }
        return $output;
    }

    public function sales($product_id, $company_id) {
        //(Invoice paid - grn) + debit note + cash sales invoice
        $sql_ext = "";
        $sql_ext2 = "";
        if ($company_id != 0) {
            $sql_ext = " AND t1.com_id='$company_id'";
            $sql_ext2 = " AND t1.company_id='$company_id'";
        }

        $model = Yii::$app->db->createCommand("SELECT t1.do_no, t2.quantity, t2.product_id FROM invoice t1 
            INNER JOIN delivery_order_item t2 ON t1.do_no=t2.doc_id 
            WHERE t1.payment_status='Paid' AND t1.type='do' AND t2.product_id='$product_id'" . $sql_ext)->queryAll();
        $totalinvoice = 0;
        if (!empty($model)) {
            foreach ($model as $row) {
                $totalreturn = (int) Yii::$app->db->createCommand("SELECT SUM(quantity) FROM grn 
                    WHERE do_id='" . $row['do_no'] . "' AND product_id='$product_id'")->queryScalar();
                $totalperdo = $row['quantity'] - $totalreturn;
                $totalinvoice += $totalperdo;
            }
        }

        $totaldebitnote = (int) Yii::$app->db->createCommand("SELECT SUM(t2.quantity) FROM debitnote t1 
            INNER JOIN debitnote_item t2 ON t1.id=t2.doc_id 
            WHERE t2.type='product' AND t2.product_id='$product_id'" . $sql_ext2)->queryScalar();

        $totalcashsales = (int) Yii::$app->db->createCommand("SELECT SUM(t2.quantity) FROM invoice t1 
            INNER JOIN invoice_item t2 ON t1.id=t2.doc_id 
            WHERE t1.payment_status='Paid' AND t1.type='cash' AND t2.descr='$product_id'" . $sql_ext)->queryScalar();

        $total = $totalinvoice + $totaldebitnote + $totalcashsales;

        return $total;
    }

    public function calcpaymentdue($invoice_date, $invoice_term) {
        $start_date = date("Y-m-d", strtotime($invoice_date));
        $invoice_term_arr = explode('_', $invoice_term);
        if ($invoice_term_arr[0] == 'end') {
            $start_date = date("Y-m-t", strtotime($invoice_date));
        }
        return date('Y-m-d', strtotime($start_date . ' + ' . $invoice_term_arr[1] . ' days'));
    }

    public function getcustomername($id) {
        $output = "";
        if (($model = static::findOne($id)) !== null) {
            if ($model->type != 'do') {
                $output = $model->customer_name;
            } else {
                if (($modelDO = DeliveryOrder::findOne($model->do_no)) !== null) {
                    $output = $modelDO->customer_name;
                }
            }
        }
        return $output;
    }
    
    public function getuuid($id) {
        $output = '';
        if (($model = static::findOne($id)) !== null) {
            $output = $model->uuid;
        }
        return $output;
    }

}