/var/www/vhosts/nabawater/backend/controllers
Edit: /var/www/vhosts/nabawater/backend/controllers/ItemMappingController.php (12160B)
render('index',
[
'searchModel' => $searchModel,
'dataProvider' => $searchModel->search(Yii::$app->request->queryParams)
]
);
}
/**
*
* @return string
*
* @Title("Create")
*/
public function actionCreate()
{
$model = new ItemMapping();
$modelGroup = new ItemMappingGroup;
$post = Yii::$app->getRequest()->post();
if ($model->load($post) && $model->validate() && $modelGroup->load($post)) {
if ($model->save()) {
ItemMappingGroup::deleteAll(['item_mapping_id' => $model->getPrimaryKey()]);
foreach ($model->getMappingType() as $mappingTypeKey => $mappingTypeValue) {
if ($mappingTypeKey != $modelGroup::MAPPING_TYPE_INGREDIENT) {
if (isset($modelGroup->item_ingredient_group_id[$mappingTypeKey]) && $modelGroup->item_ingredient_group_id[$mappingTypeKey][0] != '') {
foreach ($modelGroup->item_ingredient_group_id[$mappingTypeKey] as $key => $value) {
$modelMapping = new ItemMappingGroup();
$modelMapping->item_mapping_id = $model->getPrimaryKey();
$modelMapping->item_ingredient_group_id = $value;
$modelMapping->mapping_type = $mappingTypeKey;
if(!$modelMapping->save()) {
echo'
';
print_r($modelMapping->getErrors());
exit;
}
}
}
} else {
if (isset($modelGroup->item_ingredient_group_id[$mappingTypeKey]) && $modelGroup->item_ingredient_group_id[$mappingTypeKey][0] != '') {
foreach (explode(',', $modelGroup->item_ingredient_group_id[$mappingTypeKey][0]) as $key => $value) {
$modelMapping = new ItemMappingGroup();
$modelMapping->item_mapping_id = $model->getPrimaryKey();
$modelMapping->item_ingredient_group_id = $value;
$modelMapping->mapping_type = $mappingTypeKey;
if (isset($post['ingredient_default']) && in_array($value, $post['ingredient_default'])) {
$modelMapping->is_drink = 1;
} else {
$modelMapping->is_drink = 0;
}
$modelMapping->save();
}
}
}
}
Com::successFlash('Created Successfully');
return $this->redirect(['index']);
}
}
return $this->render('create', [
'model' => $model,
'modelGroup' => $modelGroup,
'group_ids' => ''
]);
}
/**
*
* @param type $id
* @return string
*
* @Title("Update")
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
$post = Yii::$app->getRequest()->post();
$modelGroup = new ItemMappingGroup();
foreach ($model->getMappingType() as $mappingTypeKey => $mappingTypeValue) {
if ($mappingTypeKey != $modelGroup::MAPPING_TYPE_INGREDIENT) {
$modelMap = ItemMappingGroup::find()->where(['item_mapping_id' => $model->getPrimaryKey(), 'mapping_type' => $mappingTypeKey])->one();
if ($modelMap === null) {
$MapArr['item_ingredient_group_id'][$mappingTypeKey] = '';
} else {
$MapArr['item_ingredient_group_id'][$mappingTypeKey] = $modelMap->item_ingredient_group_id;
}
} else {
$modelMap = ItemMappingGroup::find()->select(['GROUP_CONCAT(item_ingredient_group_id) AS item_ingredient_group_id'])->where(['item_mapping_id' => $model->getPrimaryKey(), 'mapping_type' => $mappingTypeKey])->asArray()->one();
if ($modelMap == null) {
$MapArr['item_ingredient_group_id'][$mappingTypeKey] = '';
} else {
$MapArr['item_ingredient_group_id'][$mappingTypeKey] = $modelMap['item_ingredient_group_id'];
}
}
}
$modelGroup->item_ingredient_group_id = $MapArr['item_ingredient_group_id'];
if ($model->load($post) && $model->validate() && $modelGroup->load($post)) {
if ($model->save()) {
ItemMappingGroup::deleteAll(['item_mapping_id' => $model->getPrimaryKey()]);
$ingredientGroup = array();
foreach ($model->getMappingType() as $mappingTypeKey => $mappingTypeValue) {
if ($mappingTypeKey != $modelGroup::MAPPING_TYPE_INGREDIENT) {
if (isset($modelGroup->item_ingredient_group_id[$mappingTypeKey]) && $modelGroup->item_ingredient_group_id[$mappingTypeKey][0] != '') {
foreach ($modelGroup->item_ingredient_group_id[$mappingTypeKey] as $key => $value) {
$modelMapping = ItemMappingGroup::find()->where([
'item_mapping_id' => $model->getPrimaryKey(),
'mapping_type' => $mappingTypeKey,
'item_ingredient_group_id' => $value,
])->one();
if ($modelMapping === null) {
$modelMapping = new ItemMappingGroup();
$modelMapping->item_mapping_id = $model->getPrimaryKey();
$modelMapping->item_ingredient_group_id = $value;
$modelMapping->mapping_type = $mappingTypeKey;
}
$modelMapping->save();
array_push($ingredientGroup, $value);
}
}
} else {
if (isset($modelGroup->item_ingredient_group_id[$mappingTypeKey]) && $modelGroup->item_ingredient_group_id[$mappingTypeKey][0] != '') {
foreach (explode(',', $modelGroup->item_ingredient_group_id[$mappingTypeKey][0]) as $key => $value) {
$modelMapping = ItemMappingGroup::find()->where([
'item_mapping_id' => $model->getPrimaryKey(),
'mapping_type' => $mappingTypeKey,
'item_ingredient_group_id' => $value,
])->one();
if ($modelMapping === null) {
$modelMapping = new ItemMappingGroup();
$modelMapping->item_mapping_id = $model->getPrimaryKey();
$modelMapping->item_ingredient_group_id = $value;
$modelMapping->mapping_type = $mappingTypeKey;
}
if (isset($post['ingredient_default']) && in_array($value, $post['ingredient_default'])) {
$modelMapping->is_drink = 1;
} else {
$modelMapping->is_drink = 0;
}
$modelMapping->save();
array_push($ingredientGroup, $value);
}
}
}
}
Com::successFlash('Updated Successfully');
return $this->redirect(['index']);
}
}
return $this->render('update', [
'model' => $model,
'modelGroup' => $modelGroup,
]);
}
/**
*
* @param type $id
* @return string
*
* @Title("Delete")
*/
public function actionDelete($id)
{
$model = $this->findModel($id);
if ($model === null) {
Com::failureFlash('Unable to find the Mapped Item');
return $this->redirect(['index']);
}
$model->item_mapping_status = ItemMapping::DELETE;
$model->save();
Com::successFlash('Deleted Successfully');
return $this->redirect(['index']);
}
/**
*
* @param type $id
* @return $model
* @throws NotFoundHttpException
*/
protected function findModel($id)
{
if (($model = ItemMapping::findOne(['item_mapping_key' => $id])) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
/**
*
* @return Json
* @throws \yii\base\InvalidParamException
*
*
* @Title("Item Mapping Status")
*/
public function actionChangeStatus()
{
$params = ['key', 'status'];
$post = Yii::$app->getRequest()->post();
$result = ['status' => STATUS_FAIL, 'msg' => ''];
foreach ($params as $param) {
if (!array_key_exists($param, $post)) {
$result['msg'] = 'Required param missing';
goto skip;
}
}
$model = ItemMapping::findOne(['item_mapping_key' => $post['key']]);
if ($model === null) {
$result['msg'] = 'Unable to find the Mapped Item';
goto skip;
}
$model->item_mapping_status = $post['status'];
$model->save(false);
$result['msg'] = 'Mapped Item deactivated successfully';
if ((int)$model->item_mapping_status === $model::ACTIVE) {
$result['msg'] = 'Mapped Item activated successfully';
}
$result['status'] = STATUS_SUCCESS;
skip:
return $this->asJson($result);
}
public function actionGetDataVendorBased()
{
$post = Yii::$app->request->post();
$result = ['status' => STATUS_FAIL, 'msg' => ''];
$modelItem = Item::getVendorItemData($post['id']);
//print_r($modelItem); die;
$option = Html::tag('option', 'Please Select Item', ['value' => '']);
foreach ($modelItem as $key => $value) {
$option .= Html::tag('option', $value, ['value' => $key]);
}
$result['item'] = $option;
$modelIm = ItemMapping::getGroups('', 'NOT IN', '', '', $post['id']);
$result['html'] = $this->renderPartial('item_mapping_ajax',['ItemMapping' => $modelIm]);
return $this->asJson($result);
}
}