Your IP : 216.73.216.124


Current Path : /var/www/html/crm/controllers/
Upload File :
Current File : /var/www/html/crm/controllers/StocklogController.php

<?php

namespace app\controllers;

use Yii;
use app\models\StockLog;
use app\models\StockLogSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use app\models\Product;
use app\models\Vendor;
use app\models\Company;

/**
 * StocklogController implements the CRUD actions for StockLog model.
 */
class StocklogController extends Controller {

    public $menu_id = 51;

    /**
     * {@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 StockLog models.
     * @return mixed
     */
    public function actionIndex() {
        /* $StockLog = new StockLog();
          $model = StockLog::find()->where(['!=', 'table_related', 'debitnote_item'])->orderBy(['id' => SORT_ASC])->all();
          if (!empty($model)) {
          foreach ($model as $row) {
          $ori_dono = $row->do_no;
          $new_dono = $StockLog->getdono($row->table_related, $row->data_id);
          if ($ori_dono != $new_dono) {
          Yii::$app->db->createCommand("UPDATE stock_log SET do_no='$new_dono' WHERE id=$row->id")->execute();
          }
          }
          } */
        $searchModel = new StockLogSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'menu_id' => $this->menu_id]);
    }

    /**
     * Displays a single StockLog model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id) {
        return $this->render('view', [
                    'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new StockLog model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new StockLog();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('create', [
                    'model' => $model,
        ]);
    }

    /**
     * Updates an existing StockLog model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('update', [
                    'model' => $model,
        ]);
    }

    /**
     * Deletes an existing StockLog model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id) {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the StockLog model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return StockLog the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = StockLog::findOne($id)) !== null) {
            return $model;
        }

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

    public function actionDownload($searchkeyword = '', $searchdo = '', $searchdatestart = '', $searchdateend = '', array $table_related = []) {
        //print_r($table_related);
        //exit();
        $query = StockLog::find();
        if ( !empty(array_filter($table_related)))
            $query->andFilterWhere(['IN', 'stock_log.table_related', $table_related]);
        if (!empty($searchdatestart) && !empty($searchdateend))
            $query->andFilterWhere(['BETWEEN', 'DATE(action_datetime)', $searchdatestart, $searchdateend]);
        if (!empty($searchkeyword)) {
            $query->innerJoin("product", "stock_log.product_id=product.id");
            $query->andFilterWhere(['LIKE', 'product.name', $searchkeyword]);
        }
        if (!empty($searchdo))
            $query->andFilterWhere(['=', 'stock_log.do_no', $searchdo]);
        $query->groupBy(['stock_log.id']);
        $query->orderBy(['stock_log.id' => SORT_DESC]);
        $model = $query->all();

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

        $sheet->setCellValue('A1', '#');
        $sheet->setCellValue('B1', 'Date');
        $sheet->setCellValue('C1', 'Movement');
        $sheet->setCellValue('D1', 'Action');
        $sheet->setCellValue('E1', 'DO Number');
        $sheet->setCellValue('F1', 'Quantity');
        $sheet->setCellValue('G1', 'Product');
        $sheet->setCellValue('H1', 'Stock Owner Type');
        $sheet->setCellValue('I1', 'Stock Owner');

        $sheet->getStyle("A1:I1")->getFont()->setBold(true);
        $count = 1;
        $count2 = 0;
        if (!empty($model)) {
            foreach ($model as $row) {
                $count++;
                $count2++;
                $sheet->setCellValue('A' . $count, $count2);
                $sheet->setCellValue('B' . $count, date("d M Y", strtotime($row['action_datetime'])));

                if ($row['action_type'] == 'stockin')
                    $sheet->setCellValue('C' . $count, 'Stock-In');
                if ($row['action_type'] == 'stockout')
                    $sheet->setCellValue('C' . $count, 'Stock-Out');

                if ($row['table_related'] == 'debitnote_item')
                    $sheet->setCellValue('D' . $count, 'Debit Note');
                if ($row['table_related'] == 'stock_in')
                    $sheet->setCellValue('D' . $count, 'Stock-In');
                if ($row['table_related'] == 'stock_out')
                    $sheet->setCellValue('D' . $count, 'Stock Adjustment');
                if ($row['table_related'] == 'delivery_order_item')
                    $sheet->setCellValue('D' . $count, 'Delivery Order');
                if ($row['table_related'] == 'grn')
                    $sheet->setCellValue('D' . $count, 'Sales Return');
                if ($row['table_related'] == 'purchasereturn_item')
                    $sheet->setCellValue('D' . $count, 'Goods Return');
                if ($row['table_related'] == 'invoice_item')
                    $sheet->setCellValue('D' . $count, 'Cash Sales Invoice');

                if (!empty($row['do_no']))
                    $sheet->setCellValue('E' . $count, $row['do_no']);
                else
                    $sheet->setCellValue('E' . $count, '');

                $StockLog = new StockLog();
                $sheet->setCellValue('F' . $count, strip_tags($StockLog->getquantity($row['table_related'], $row['data_id'])));

                $Product = new Product();
                $sheet->setCellValue('G' . $count, $Product->getname($row['product_id']));

                $sheet->setCellValue('H' . $count, ucwords($row['owner_type']));

                if ($row->owner_type == 'vendor') {
                    $Vendor = new Vendor();
                    $sheet->setCellValue('I' . $count, $Vendor->getname($row->owner_id));
                }
                if ($row->owner_type == 'company') {
                    $Company = new Company();
                    $sheet->setCellValue('I' . $count, $Company->getname($row->owner_id));
                }
            }
        }

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

}