| Current Path : /var/www/html/dyna-crm/controllers/ |
| Current File : /var/www/html/dyna-crm/controllers/GrnController.php |
<?php
namespace app\controllers;
use Yii;
use app\models\Grn;
use app\models\GrnSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use yii\helpers\Url;
use yii\helpers\Html;
use app\models\Log;
use app\models\LogSearch;
use app\models\Customer;
use app\models\Company;
use app\models\ParamList;
use app\models\Product;
use app\models\DeliveryOrder;
use app\models\DeliveryOrderItem;
use app\models\StockLog;
use app\models\StockOut;
use app\models\PurchasereturnItem;
use app\models\Purchasereturn;
use app\models\GrnParent;
/**
* GrnController implements the CRUD actions for Grn model.
*/
class GrnController extends Controller {
public $menu_id = 48;
/**
* {@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 Grn models.
* @return mixed
*/
public function actionIndex() {
$searchModel = new GrnSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'menu_id' => $this->menu_id]);
}
public function actionReturn() {
$searchModel = new GrnSearch();
$dataProvider = $searchModel->searchreturn(Yii::$app->request->queryParams);
return $this->render('return', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'menu_id' => 54]);
}
public function actionUpdatereturn() {
//update status_inspection, resale_qty, noresale_qty, inspect_by, inspect_dt
$id = $_POST['id'];
$resale_qty = $_POST['resale_qty'];
$noresale_qty = $_POST['noresale_qty'];
$status = 'failed';
if (($model = Grn::findOne($id)) !== null) {
$model->status_inspection = 'checked';
$model->resale_qty = $resale_qty;
$model->noresale_qty = $noresale_qty;
$model->inspect_by = Yii::$app->user->identity->id;
$model->inspect_dt = date("Y-m-d H:i:s");
if ($model->save())
$status = 'success';
}
return $status;
}
public function actionLog($uuid) {
$searchModel = new LogSearch();
$searchModel->menu_id = $this->menu_id;
$searchModel->data_uuid = $uuid;
$dataProvider = $searchModel->log(Yii::$app->request->queryParams);
return $this->render('log', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'menu_id' => $this->menu_id]);
}
/**
* Displays a single Grn 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 Grn model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate() {
$model = new Grn();
$model->received_dt = date("Y-m-d");
$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->uuid = Uuid::uuid4();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
//save into stock_log
$modelStockLog = new StockLog();
$modelStockLog->product_id = $model->product_id;
$modelStockLog->action_type = 'stockin';
$modelStockLog->table_related = 'grn';
$modelStockLog->data_id = $model->id;
$modelStockLog->company_id = $model->com_id;
$modelStockLog->do_no = $modelStockLog->getdono($modelStockLog->table_related, $modelStockLog->data_id);
$modelStockLog->action_by = Yii::$app->user->identity->id;
$modelStockLog->action_datetime = date("Y-m-d H:i:s");
$modelStockLog->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('grn/index') . "';});");
}
return $this->render('create', ['model' => $model, 'menu_id' => $this->menu_id]);
}
/**
* Updates an existing Grn 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->save()) {
Yii::$app->db->createCommand("UPDATE stock_log SET company_id='$model->com_id' WHERE table_related='grn' AND data_id=" . $model->id)->execute();
$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('grn/index') . "';});");
}
return $this->render('update', ['model' => $model, 'menu_id' => $this->menu_id]);
}
/**
* Deletes an existing Grn 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 Grn model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Grn the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($uuid) {
if (($model = Grn::findOne(['uuid' => $uuid])) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
public function actionGetdodetails() {
$id = $_POST['id'];
$output = '';
if (($model = DeliveryOrder::findOne($id)) !== null) {
$Company = new Company;
$Product = new Product;
$output.= $model->company_id . '<->' . $Company->getname($model->company_id);
$output.= '<->' . $model->customer_id . '<->' . $model->customer_name . '<->';
$modelItem = DeliveryOrderItem::find()->where(['doc_id' => $id])->orderBy(['id' => SORT_ASC])->all();
if (!empty($modelItem)) {
foreach ($modelItem as $rowItem) {
$output.='<option value="' . $rowItem->product_id . '">' . $Product->getname($rowItem->product_id) . '</option>';
}
}
}
return $output;
}
public function actionConvertstock() {
$id = $_POST['id'];
$type = $_POST['type'];
$status = 'failed';
if (($model = Grn::findOne($id)) !== null) {
if ($type == 'Adjustment') {
//insert into stock_out
$modelstockout = new StockOut();
$modelstockout->uuid = Uuid::uuid4();
$modelstockout->company_id = $model->com_id;
$modelstockout->product_id = $model->product_id;
$modelstockout->quantity = $model->noresale_qty;
$modelstockout->type = 'return';
$modelstockout->remarks = 'Stock added from Return Stock Process (not resellable stock)';
$modelstockout->created_dt = date("Y-m-d H:i:s");
$modelstockout->created_by = Yii::$app->user->identity->id;
$modelstockout->updated_dt = date("Y-m-d H:i:s");
$modelstockout->updated_by = Yii::$app->user->identity->id;
//insert into stock_log
if ($modelstockout->save()) {
$modelStockLog = new StockLog();
$modelStockLog->product_id = $modelstockout->product_id;
$modelStockLog->action_type = 'stockout';
$modelStockLog->table_related = 'stock_out';
$modelStockLog->data_id = $modelstockout->id;
$modelStockLog->company_id = $modelstockout->company_id;
$modelStockLog->do_no = '';
$modelStockLog->action_by = Yii::$app->user->identity->id;
$modelStockLog->action_datetime = date("Y-m-d H:i:s");
if ($modelStockLog->save()) {
//update table grn
$model->noresale_action = $type;
$model->noresale_id = $modelstockout->id;
if ($model->save()) {
$status = 'success';
}
}
}
}
if ($type == 'Purchase Return') {
//insert into purchasereturn
$Product = new Product();
$Company = new Company();
$DeliveryOrder = new DeliveryOrder();
$ParamList = new ParamList();
$countdoc = (int) Purchasereturn::find()->where(['company_id' => $model->com_id])->count() + 1;
$modelpurchasereturn = new Purchasereturn();
$modelpurchasereturn->uuid = Uuid::uuid4();
$modelpurchasereturn->doc_date = date("Y-m-d");
$modelpurchasereturn->doc_no = $Company->getshortcode($model->com_id) . '/PRN/' . date("y") . '/' . sprintf("%05d", $countdoc);
$modelpurchasereturn->ref = $DeliveryOrder->getdocno($model->do_id);
$modelpurchasereturn->company_id = $model->com_id;
$vendor_details = $Product->getvendordetails($model->product_id);
if (!empty($vendor_details)) {
$vendor_arr = explode('<->', $vendor_details);
$modelpurchasereturn->vendor_id = $vendor_arr[0];
$modelpurchasereturn->vendor_name = $vendor_arr[1];
$modelpurchasereturn->vendor_address1 = $vendor_arr[2];
$modelpurchasereturn->vendor_address2 = $vendor_arr[3];
$modelpurchasereturn->vendor_postcode = $vendor_arr[4];
$modelpurchasereturn->vendor_city = $vendor_arr[5];
$modelpurchasereturn->vendor_state = $vendor_arr[6];
$modelpurchasereturn->vendor_country = $vendor_arr[7];
$modelpurchasereturn->vendor_pic = $vendor_arr[8];
$modelpurchasereturn->vendor_phone = $vendor_arr[9];
}
$modelpurchasereturn->remarks = 'Stock added from Return Stock Process (not resellable stock)';
$modelpurchasereturn->created_dt = date("Y-m-d H:i:s");
$modelpurchasereturn->created_by = Yii::$app->user->identity->id;
$modelpurchasereturn->updated_dt = date("Y-m-d H:i:s");
$modelpurchasereturn->updated_by = Yii::$app->user->identity->id;
//insert into purchasereturnItem
if ($modelpurchasereturn->save()) {
$modelpurchasereturnitem = new PurchasereturnItem();
$modelpurchasereturnitem->doc_id = $modelpurchasereturn->id;
$modelpurchasereturnitem->product_id = $model->product_id;
$modelpurchasereturnitem->quantity = $model->noresale_qty;
$modelpurchasereturnitem->uom = $ParamList->getlabel(9, $Product->getuom($modelpurchasereturnitem->product_id));
$modelpurchasereturnitem->remarks = 'Stock added from Return Stock Process (not resellable stock)';
$modelpurchasereturnitem->created_dt = date("Y-m-d H:i:s");
$modelpurchasereturnitem->created_by = Yii::$app->user->identity->id;
$modelpurchasereturnitem->updated_dt = date("Y-m-d H:i:s");
$modelpurchasereturnitem->updated_by = Yii::$app->user->identity->id;
if ($modelpurchasereturnitem->save()) {
//insert into stock_log
$modelStockLog = new StockLog();
$modelStockLog->product_id = $modelpurchasereturnitem->product_id;
$modelStockLog->action_type = 'stockout';
$modelStockLog->table_related = 'purchasereturn_item';
$modelStockLog->data_id = $modelpurchasereturnitem->id;
$modelStockLog->company_id = $modelpurchasereturn->company_id;
$modelStockLog->do_no = $modelpurchasereturn->ref;
$modelStockLog->action_by = Yii::$app->user->identity->id;
$modelStockLog->action_datetime = date("Y-m-d H:i:s");
if ($modelStockLog->save()) {
//update table grn
$model->noresale_action = $type;
$model->noresale_id = $modelpurchasereturn->id;
if ($model->save()) {
$status = 'success';
}
}
}
}
}
}
return $status;
}
}