| Current Path : /var/www/html/crm/controllers/ |
| Current File : /var/www/html/crm/controllers/SalesController.php |
<?php
namespace app\controllers;
use Yii;
use app\models\Sales;
use app\models\SalesItem;
use app\models\SalesSearch;
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\ParamList;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
/**
* SalesController implements the CRUD actions for Sales model.
*/
class SalesController extends Controller {
public $menu_id = 68;
/**
* {@inheritdoc}
*/
public function behaviors() {
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
'access' => [
'class' => \yii\filters\AccessControl::className(),
'only' => ['index', 'create', 'update', 'view', 'report'],
'rules' => [
// allow authenticated users
[
'allow' => true,
'roles' => ['@'],
],
// everything else is denied
],
],
];
}
public function actionReport($start_date = '', $end_date = '') {
if (empty($start_date))
$start_date = Yii::$app->db->createCommand("SELECT sales_dt FROM sales ORDER BY sales_dt ASC LIMIT 1")->queryScalar();
if (empty($end_date))
$end_date = Yii::$app->db->createCommand("SELECT sales_dt FROM sales ORDER BY sales_dt DESC LIMIT 1")->queryScalar();
return $this->render('report', ['menu_id' => 71, 'start_date' => $start_date, 'end_date' => $end_date]);
}
/**
* Lists all Sales models.
* @return mixed
*/
public function actionIndex() {
$searchModel = new SalesSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'menu_id' => $this->menu_id,]);
}
/**
* Displays a single Sales 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 Sales model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate() {
$model = new Sales();
$model->uuid = Uuid::uuid4();
$model->created_dt = date("Y-m-d H:i:s");
$model->created_by = Yii::$app->user->identity->id;
$model->updated_dt = date("Y-m-d H:i:s");
$model->updated_by = Yii::$app->user->identity->id;
$model->sales_dt = date("Y-m-d");
if ($model->load(Yii::$app->request->post())) {
$model->file = UploadedFile::getInstance($model, 'file');
if (!empty($model->file)) {
$file_name = 'file1_' . date("YmdHis") . '.' . $model->file->extension;
if ($model->file->saveAs('uploads/sales/' . $file_name))
$model->attachment = $file_name;
}
$model->file2 = UploadedFile::getInstance($model, 'file2');
if (!empty($model->file2)) {
$file_name2 = 'file2_' . date("YmdHis") . '.' . $model->file2->extension;
if ($model->file2->saveAs('uploads/sales/' . $file_name2))
$model->attachment2 = $file_name2;
}
if ($model->save()) {
//insert new item into payment_voucher_item
if (!empty($_POST['new_item'])) {
$item_arr = $_POST['new_item'];
foreach ($item_arr as $item_no) {
$model2 = new SalesItem();
$model2->parent_id = $model->id;
$model2->type = $_POST['type_new_' . $item_no];
$model2->quantity = $_POST['quantity_new_' . $item_no];
$model2->amount = $_POST['amount_new_' . $item_no];
$model2->created_dt = date("Y-m-d H:i:s");
$model2->created_by = Yii::$app->user->identity->id;
$model2->updated_dt = date("Y-m-d H:i:s");
$model2->updated_by = Yii::$app->user->identity->id;
$model2->save();
}
}
$Log = new Log();
$Log->insertlog($this->menu_id, 'create', $model->uuid);
$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('sales/index') . "';});");
}
}
return $this->render('create', ['model' => $model, 'menu_id' => $this->menu_id]);
}
/**
* Updates an existing Sales 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($uuid) {
$model = $this->findModel($uuid);
$model->updated_dt = date("Y-m-d H:i:s");
$model->updated_by = Yii::$app->user->identity->id;
if ($model->load(Yii::$app->request->post())) {
$model->file = UploadedFile::getInstance($model, 'file');
if (!empty($model->file)) {
$file_name = 'file1_' . date("YmdHis") . '.' . $model->file->extension;
if ($model->file->saveAs('uploads/sales/' . $file_name))
$model->attachment = $file_name;
}
$model->file2 = UploadedFile::getInstance($model, 'file2');
if (!empty($model->file2)) {
$file_name2 = 'file2_' . date("YmdHis") . '.' . $model->file2->extension;
if ($model->file2->saveAs('uploads/sales/' . $file_name2))
$model->attachment2 = $file_name2;
}
if ($model->save()) {
//insert new item into sales_item
if (!empty($_POST['new_item'])) {
$item_arr = $_POST['new_item'];
foreach ($item_arr as $item_no) {
$model2 = new SalesItem();
$model2->parent_id = $model->id;
$model2->type = $_POST['type_new_' . $item_no];
$model2->quantity = $_POST['quantity_new_' . $item_no];
$model2->amount = $_POST['amount_new_' . $item_no];
$model2->created_dt = date("Y-m-d H:i:s");
$model2->created_by = Yii::$app->user->identity->id;
$model2->updated_dt = date("Y-m-d H:i:s");
$model2->updated_by = Yii::$app->user->identity->id;
$model2->save();
}
}
//update item into sales_item
if (!empty($_POST['old_item'])) {
$item_arr2 = $_POST['old_item'];
foreach ($item_arr2 as $item_no2) {
if (($model3 = SalesItem::findOne($item_no2)) !== null) {
$model3->type = $_POST['type_old_' . $item_no2];
$model3->quantity = $_POST['quantity_old_' . $item_no2];
$model3->amount = $_POST['amount_old_' . $item_no2];
$model3->updated_dt = date("Y-m-d H:i:s");
$model3->updated_by = Yii::$app->user->identity->id;
$model3->save();
}
}
}
$Log = new Log();
$Log->insertlog($this->menu_id, 'update', $model->uuid);
$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('sales/index') . "';});");
}
}
return $this->render('update', ['model' => $model, 'menu_id' => $this->menu_id]);
}
/**
* Deletes an existing Sales 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'];
$status = 'failed';
$model = Sales::findOne($id);
if (!empty($model)) {
$model->deleted_status = 1;
if ($model->save(false)) {
$Log = new Log();
$Log->insertlog($this->menu_id, 'delete', $model->uuid);
$status = 'success';
}
}
return $status;
}
/**
* Finds the Sales model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Sales the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($uuid) {
if (($model = Sales::findOne(['uuid' => $uuid])) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
public function actionAdditem($no) {
$ParamList = new ParamList();
$output = '<tr id="new_item_' . $no . '">
<td>' . Html::input('hidden', 'new_item[]', $no) .
Html::dropDownList('type_new_' . $no, null, $ParamList->getparamlist(38), ['id' => 'type_new_' . $no, 'class' => 'form-control kt-selectpicker', 'data-live-search' => 'true']) . '</td>
<td>' . Html::input('text', 'quantity_new_' . $no, '', $options = ['class' => 'form-control']) . '</td>
<td>' . Html::input('text', 'amount_new_' . $no, '', $options = ['class' => 'form-control']) . '</td>
<td><span class="kt-font-danger icon-custom" title="Delete"><i class="far fa-trash-alt fa-lg" style="cursor:pointer;" onclick="deletenewitem(' . $no . ')"></i></span></td>
</tr>';
return $output;
}
}