/**
  * Lists all BillItem models.
  * @return mixed
  */
 public function actionIndex()
 {
     if (isset($_POST['hasEditable']) && $_POST['hasEditable'] == 1) {
         \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
         if (isset($_POST['item'], $_POST['quantity_user'])) {
             if (!isset(Yii::$app->session['item'])) {
                 Yii::$app->session['item'] = [];
             }
             $item = Yii::$app->session['item'];
             $item[$_POST['item']] = $_POST['quantity_user'];
             Yii::$app->session['item'] = $item;
             return ['output' => $_POST['quantity_user'], 'message' => ''];
         } else {
             return ['output' => '', 'message' => 'Validation error'];
         }
     } elseif (isset($_POST['iks'], $_POST['bks'], $_POST['assign_mode'])) {
         \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
         //Create assignment
         if ($_POST['assign_mode'] == 1) {
             $items = Item::findAll($_POST['iks']);
             $bill = Bill::findOne($_POST['bks'][0]);
             $billItem = null;
             $transaction = Bill::getDb()->beginTransaction();
             try {
                 foreach ($items as $item) {
                     $billItem = BillItem::find()->where(['item_id' => $item->id, 'bill_id' => $bill->id])->one();
                     if ($billItem == null) {
                         $billItem = new BillItem();
                         $billItem->bill_id = $bill->id;
                         $billItem->item_id = $item->id;
                     }
                     $itemSession = Yii::$app->session['item'];
                     $oldQuantity = $billItem->quantity;
                     $billItem->quantity = $itemSession[$item->id];
                     if ($oldQuantity > $billItem->quantity) {
                         $item->quantity += $billItem->quantity;
                     } elseif ($oldQuantity < $billItem->quantity) {
                         $item->quantity -= $billItem->quantity;
                     }
                     $item->save(false);
                     if (!$billItem->save(false)) {
                         throw new Exception(Yii::t('app', 'Error saving {model}: {msj}', ['model' => Yii::t('app', ucfirst($billItem->tableName())), 'msj' => print_r($billItem->getErrors(), true)]), 500);
                     }
                 }
                 $transaction->commit();
                 Yii::$app->session['item'] = [];
                 return ['error' => false, 'message' => Yii::t('app', 'Saved')];
             } catch (\Exception $e) {
                 $transaction->rollBack();
                 return ['error' => true, 'message' => print_r($e, true)];
             }
         }
     }
     $searchModel = new BillSearch();
     $dataProvider = $searchModel->searchWithItem(Yii::$app->request->queryParams);
     $itemSearchModel = new ItemSearch();
     $itemDataProvider = $itemSearchModel->searchWithItem(Yii::$app->request->queryParams);
     return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'itemSearchModel' => $itemSearchModel, 'itemDataProvider' => $itemDataProvider]);
 }