Your IP : 216.73.216.79


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

<?php

namespace app\controllers;

use yii\web\Controller;
use Yii;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use app\models\Company;
use app\models\Product;
use app\models\User;

class ExcelController extends Controller {

    public function actionDownloadintransit($company_id) {

        $Company = new Company();
        $Product = new Product();

        ini_set('max_execution_time', 3000);
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        $sheet->setCellValue('A1', 'DO Date');
        $sheet->setCellValue('B1', 'Do Number');
        $sheet->setCellValue('C1', 'Company');
        $sheet->setCellValue('D1', 'Customer');
        $sheet->setCellValue('E1', 'Product');
        $sheet->setCellValue('F1', 'Quantity');

        $sheet->getStyle("A1:F1")->getFont()->setBold(true);

        $sqlstmt = "SELECT t1.doc_dt, t1.doc_no, t2.product_id, t2.quantity, t1.company_id, t1.customer_name
            FROM delivery_order t1 
            INNER JOIN delivery_order_item t2 ON t1.id=t2.doc_id 
            WHERE t1.status='In-Transit' AND t1.company_id='$company_id' 
                ORDER BY t1.id DESC";
        $model = Yii::$app->db->createCommand($sqlstmt)->queryAll();
        $count = 1;
        if (!empty($model)) {

            foreach ($model as $row) {
                $count++;
                $sheet->setCellValue('A' . $count, $row['doc_dt']);
                $sheet->setCellValue('B' . $count, $row['doc_no']);
                $sheet->setCellValue('C' . $count, $Company->getname($row['company_id']));
                $sheet->setCellValue('D' . $count, $row['customer_name']);
                $sheet->setCellValue('E' . $count, $Product->getname($row['product_id']));
                $sheet->setCellValue('F' . $count, $row['quantity']);
            }
        }

        $filename = 'Stock In-Transit Report.xlsx';
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
        die();
    }

    public function actionDownloadconsigned($company_id) {

        $Company = new Company();
        $Product = new Product();

        ini_set('max_execution_time', 3000);
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        $sheet->setCellValue('A1', 'DO Date');
        $sheet->setCellValue('B1', 'Do Number');
        $sheet->setCellValue('C1', 'Company');
        $sheet->setCellValue('D1', 'Customer');
        $sheet->setCellValue('E1', 'Product');
        $sheet->setCellValue('F1', 'Qty Send');
        $sheet->setCellValue('G1', 'Qty Return');
        $sheet->setCellValue('H1', 'Total Consigned');

        $sheet->getStyle("A1:H1")->getFont()->setBold(true);

        $sqlstmt = "SELECT t1.doc_dt, t1.doc_no, t2.product_id, t2.quantity, t1.company_id, t1.customer_name, t1.id AS do_id 
            FROM delivery_order t1 
            INNER JOIN delivery_order_item t2 ON t1.id=t2.doc_id 
            WHERE t1.status='Consigned' AND t1.company_id='$company_id'
            ORDER BY t1.id DESC";
        $model = Yii::$app->db->createCommand($sqlstmt)->queryAll();
        $count = 1;
        if (!empty($model)) {

            foreach ($model as $row) {
                $count++;

                $qty_send = $row['quantity'];
                $model_return = Yii::$app->db->createCommand("SELECT SUM(quantity) FROM grn WHERE do_id=" . $row['do_id'] . " AND product_id=" . $row['product_id'])->queryScalar();
                $qty_return = '';
                if (!empty($model_return))
                    $qty_return = $model_return;
                $balance = $qty_send - (int) $qty_return;

                $sheet->setCellValue('A' . $count, $row['doc_dt']);
                $sheet->setCellValue('B' . $count, $row['doc_no']);
                $sheet->setCellValue('C' . $count, $Company->getname($row['company_id']));
                $sheet->setCellValue('D' . $count, $row['customer_name']);
                $sheet->setCellValue('E' . $count, $Product->getname($row['product_id']));
                $sheet->setCellValue('F' . $count, $qty_send);
                $sheet->setCellValue('G' . $count, $qty_return);
                $sheet->setCellValue('H' . $count, $balance);
            }
        }

        $filename = 'Stock Consigned Report.xlsx';
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
        die();
    }

    public function actionDownloadcompanysales($company_id = '', $start_dt = '', $end_dt = '') {
        $Company = new Company();
        ini_set('max_execution_time', 3000);
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        //$sheet->setCellValue('A1', 'Doc Date');
        $sheet->setCellValue('A1', 'Paid Date');
        $sheet->setCellValue('B1', 'Doc Number');
        $sheet->setCellValue('C1', 'Type');
        $sheet->setCellValue('D1', 'Company');
        $sheet->setCellValue('E1', 'Customer');
        $sheet->setCellValue('F1', 'Amount (MYR)');
        $sheet->getStyle("A1:H1")->getFont()->setBold(true);

        $model = Yii::$app->db->createCommand("SELECT * FROM (" . Yii::$app->sales->getCompanySalesQuery($company_id, $start_dt, $end_dt) . ") t1 ORDER BY paid_dt DESC")->queryAll();
        $count = 1;
        if (!empty($model)) {
            foreach ($model as $row) {
                $count++;
                //$sheet->setCellValue('A' . $count, date("d/m/Y", strtotime($row['doc_dt'])));
                $sheet->setCellValue('A' . $count, date("d/m/Y", strtotime($row['paid_dt'])));
                $sheet->setCellValue('B' . $count, $row['doc_no']);
                $sheet->setCellValue('C' . $count, $row['doc_type']);
                $sheet->setCellValue('D' . $count, $Company->getname($row['com_id']));
                $sheet->setCellValue('E' . $count, $row['customer_name']);
                $sheet->setCellValue('F' . $count, $row['grandtotal']);
            }
        }

        $sheet->setCellValue('F' . ($count + 1), "=SUM(F2:F" . $count . ")");

        $filename = 'Company Sales Report.xlsx';
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
        die();
    }

    public function actionDownloadproductsales($product_id = '', $company_id = '', $start_dt = '', $end_dt = '') {

        ini_set('max_execution_time', 3000);
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $Product = new Product();
        $Company = new Company();

        $sheet->setCellValue('A1', 'Paid Date');
        $sheet->setCellValue('B1', 'Doc Number');
        $sheet->setCellValue('C1', 'Type');
        $sheet->setCellValue('D1', 'Company');
        $sheet->setCellValue('E1', 'Customer');
        $sheet->setCellValue('F1', 'Product Name');
        $sheet->setCellValue('G1', 'Quantity Send');
        $sheet->setCellValue('H1', 'Quantity Return');
        $sheet->setCellValue('I1', 'Total Quantity');
        $sheet->setCellValue('J1', 'Unit Price');
        $sheet->setCellValue('K1', 'Total Price');

        $sheet->getStyle("A1:K1")->getFont()->setBold(true);

        $model = Yii::$app->db->createCommand("SELECT * FROM (" . Yii::$app->sales->getProductSalesQuery($company_id, $start_dt, $end_dt, $product_id) . ") t1 ORDER BY paid_dt DESC")->queryAll();
        if (!empty($model)) {
            $count = 1;
            foreach ($model as $row) {
                $count++;
                $sheet->setCellValue('A' . $count, date("d/m/Y", strtotime($row['paid_dt'])));
                $sheet->setCellValue('B' . $count, $row['doc_no']);
                $sheet->setCellValue('C' . $count, $row['doc_type']);
                $sheet->setCellValue('D' . $count, $Company->getname($row['com_id']));
                $sheet->setCellValue('E' . $count, $row['customer_name']);
                $sheet->setCellValue('F' . $count, $Product->getname($row['product_id']));
                $sheet->setCellValue('G' . $count, $row['qty_send']);
                $sheet->setCellValue('H' . $count, $row['qty_return']);
                $sheet->setCellValue('I' . $count, $row['quantity']);
                $sheet->setCellValue('J' . $count, number_format($row['unit_price'], 2, ".", ""));
                $sheet->setCellValue('K' . $count, number_format($row['total_price'], 2, ".", ""));
            }
        }

        $filename = 'Product Sales Report.xlsx';
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
        die();
    }

    public function actionDownloadsalespersonsales($company_id = '', $start_dt = '', $end_dt = '', $user_id = '') {
        ini_set('max_execution_time', 3000);
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $Company = new Company();
        $User = new User();
        
        $sheet->setCellValue('A1', 'Date');
        $sheet->setCellValue('B1', 'Doc Number');
        $sheet->setCellValue('C1', 'Company');
        $sheet->setCellValue('D1', 'Customer');
        $sheet->setCellValue('E1', 'Sales Person');
        $sheet->setCellValue('F1', 'Grand Total');
        $sheet->getStyle("A1:K1")->getFont()->setBold(true);

        $model = Yii::$app->db->createCommand("SELECT * FROM (" . Yii::$app->sales->getSalesPersonSalesQuery($company_id, $start_dt, $end_dt, $user_id) . ") t1 ORDER BY doc_dt DESC")->queryAll();
        if (!empty($model)) {
            $count = 1;
            foreach ($model as $row) {
                $count++;
                $sheet->setCellValue('A' . $count, date("d/m/Y", strtotime($row['doc_dt'])));
                $sheet->setCellValue('B' . $count, $row['doc_no']);
                $sheet->setCellValue('C' . $count, $Company->getname($row['company_id']));
                $sheet->setCellValue('D' . $count, $row['customer_name']);
                $sheet->setCellValue('E' . $count, $User->getusername($row['sales_pic']));
                $sheet->setCellValue('F' . $count, number_format($row['grandtotal'], 2, ".", ""));
            }
        }

        $filename = 'Sales Person - Sales Report.xlsx';
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
        die();
    }
    
    public function actionDownloadsalespersoncollection($company_id = '', $start_dt = '', $end_dt = '', $user_id = '') {
        ini_set('max_execution_time', 3000);
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $Company = new Company();
        $User = new User();
        
        $sheet->setCellValue('A1', 'Paid Date');
        $sheet->setCellValue('B1', 'Doc Number');
        $sheet->setCellValue('C1', 'Company');
        $sheet->setCellValue('D1', 'Customer');
        $sheet->setCellValue('E1', 'Sales Person');
        $sheet->setCellValue('F1', 'Grand Total');
        $sheet->getStyle("A1:K1")->getFont()->setBold(true);

        $model = Yii::$app->db->createCommand("SELECT * FROM (" . Yii::$app->sales->getSalesPersonCollectionQuery($company_id, $start_dt, $end_dt, $user_id) . ") t1 ORDER BY paid_dt DESC")->queryAll();
        if (!empty($model)) {
            $count = 1;
            foreach ($model as $row) {
                $count++;
                $sheet->setCellValue('A' . $count, date("d/m/Y", strtotime($row['paid_dt'])));
                $sheet->setCellValue('B' . $count, $row['invoice_no']);
                $sheet->setCellValue('C' . $count, $Company->getname($row['com_id']));
                $sheet->setCellValue('D' . $count, $row['customer_name']);
                $sheet->setCellValue('E' . $count, $User->getusername($row['sales_pic']));
                $sheet->setCellValue('F' . $count, number_format($row['grandtotal'], 2, ".", ""));
            }
        }

        $filename = 'Sales Person - Sales Report.xlsx';
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
        die();
    }

}