| Current Path : /var/www/html/dyna-crm/controllers/ |
| Current File : /var/www/html/dyna-crm/controllers/SalessummaryController.php |
<?php
namespace app\controllers;
use Yii;
use app\models\SalesSummary;
use app\models\SalesSummarySearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\Url;
use app\models\Log;
use app\models\LogSearch;
use yii\web\UploadedFile;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use app\models\SalesSummaryData;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
/**
* SalessummaryController implements the CRUD actions for SalesSummary model.
*/
class SalessummaryController extends Controller {
public $menu_id = 83;
/**
* {@inheritdoc}
*/
public function behaviors() {
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all SalesSummary models.
* @return mixed
*/
public function actionIndex() {
$searchModel = new SalesSummarySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'menu_id' => $this->menu_id,]);
}
/**
* Displays a single SalesSummary 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),
'menu_id' => $this->menu_id,
]);
}
public function actionReport() {
return $this->render('report', [
'menu_id' => $this->menu_id,
]);
}
/**
* Creates a new SalesSummary model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate() {
$model = new SalesSummary();
$model->uploaded_at = date("Y-m-d H:i:s");
$model->uploaded_by = Yii::$app->user->identity->id;
if ($model->load(Yii::$app->request->post())) {
$model->file_url = UploadedFile::getInstance($model, 'file_url');
if (!empty($model->file_url)) {
$file_url = 'sales_summary_' . date("YmdHis") . '.' . $model->file_url->extension;
if ($model->file_url->saveAs('uploads/sales/' . $file_url))
$model->file_url = $file_url;
}
if ($model->save()) {
$inputFileType = 'Xlsx';
$inputFileName = 'uploads/sales/' . $file_url;
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load($inputFileName);
$sheetArr = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
foreach ($sheetArr as $sheetData) {
$sales_date = (int) $sheetData['A'];
if ($sales_date != 0) {
$sales_date = date('Y-m-d', (\PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($sales_date) + 14400));
$parent_id = $model->id;
$sales_doc_no = '';
if (!empty($sheetData['B']))
$sales_doc_no = $sheetData['B'];
$sales_person = '';
if (!empty($sheetData['D']))
$sales_person = $sheetData['D'];
$sales_item_no = '';
if (!empty($sheetData['F']))
$sales_item_no = $sheetData['F'];
$sales_descr = '';
if (!empty($sheetData['G']))
$sales_descr = $sheetData['G'];
$sales_tax = 0;
if (!empty($sheetData['I']))
$sales_tax = $sheetData['I'];
$sales_qty = 0;
if (!empty($sheetData['L']))
$sales_qty = $sheetData['L'];
$sales_cost = 0;
if (!empty($sheetData['M']))
$sales_cost = $sheetData['M'];
$sales_price = 0;
if (!empty($sheetData['N']))
$sales_price = $sheetData['N'];
$sales_up = 0;
if (!empty($sheetData['O']))
$sales_up = $sheetData['O'];
$sales_amount = 0;
if (!empty($sheetData['Q']))
$sales_amount = $sheetData['Q'];
$sales_profit = 0;
if (!empty($sheetData['R']))
$sales_profit = $sheetData['R'];
$modeldata = new SalesSummaryData();
$modeldata->parent_id = $parent_id;
$modeldata->sales_date = $sales_date;
$modeldata->sales_doc_no = $sales_doc_no;
$modeldata->sales_person = $sales_person;
$modeldata->sales_item_no = $sales_item_no;
$modeldata->sales_descr = $sales_descr;
$modeldata->sales_tax = $sales_tax;
$modeldata->sales_qty = $sales_qty;
$modeldata->sales_cost = $sales_cost;
$modeldata->sales_price = $sales_price;
$modeldata->sales_up = $sales_up;
$modeldata->sales_amount = $sales_amount;
$modeldata->sales_profit = $sales_profit;
$modeldata->save(false);
}
}
$Log = new Log();
$Log->insertlog($this->menu_id, 'create', $model->id);
$this->getView()->registerJs("swal.fire({position: 'center-center',type: 'success',title: 'Data has been saved',showConfirmButton: false,timer: 1500,}).then(function(result){window.location = '" . Url::toRoute('salessummary/index') . "';});");
}
}
return $this->render('create', ['model' => $model, 'menu_id' => $this->menu_id]);
}
/**
* Updates an existing SalesSummary 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 SalesSummary 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 = $_POST['id'];
if (($model = SalesSummary::findOne($id)) !== null) {
$Log = new Log();
$Log->insertlog($this->menu_id, 'delete', $model->id);
if ($model->delete()) {
SalesSummaryData::deleteAll(['parent_id' => $id]);
return 'success';
}
}
}
/**
* Finds the SalesSummary model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return SalesSummary the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id) {
if (($model = SalesSummary::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}