public function actionBuyItems()
 {
     /* @var $product Products */
     Yii::$app->response->format = 'json';
     $return_data = ['status' => 'error', 'data' => []];
     $basket = Yii::$app->request->post('data');
     $items = [];
     if (isset($basket['items'])) {
         $items = $basket['items'];
     }
     $amount_paid = 0;
     if (isset($basket['amount_paid'])) {
         $amount_paid = $basket['amount_paid'];
     }
     $total_amount = 0;
     if (isset($basket['total_amount'])) {
         $total_amount = $basket['total_amount'];
     }
     if (!empty($items)) {
         $transaction = new Transactions();
         $transaction->total_quantity = 0;
         $transaction->amount_paid = $amount_paid;
         $transaction->user_id = Yii::$app->user->id;
         $transaction->reason_id = Transactions::SELL;
         $transaction->total_amount = $total_amount;
         if ($transaction->save()) {
             $total_quantity = 0;
             foreach ($items as $item) {
                 $transaction_item = new TransactionItems();
                 $transaction_item->product_id = $item['id'];
                 $transaction_item->transaction_id = $transaction->id;
                 $transaction_item->quantity = $item['quantity'];
                 $total_quantity += intval($transaction_item->quantity);
                 $transaction_item->buy_price = $item['buy_price'];
                 $transaction_item->sell_price = $item['sell_price'];
                 $transaction_item->name = $item['name'];
                 if ($transaction_item->save()) {
                     $product = Products::findOne($transaction_item->product_id);
                     $product->quantity = $product->quantity - $transaction_item->quantity;
                     $product->save();
                 }
             }
             $transaction->total_quantity = $total_quantity;
             $transaction->save();
             Yii::$app->getSession()->setFlash('success', 'Transaction was successfully saved.');
             $return_data = ['status' => 'success', 'data' => ['bill_num' => str_pad($transaction->id, 8, 0, STR_PAD_LEFT)]];
         } else {
             $return_data = ['status' => 'error', 'data' => ['Error in Transaction']];
         }
     }
     return $return_data;
 }
 public function actionProducts()
 {
     $posted_data = Yii::$app->request->post();
     $data = ['transactions' => []];
     if (Yii::$app->request->getQueryParam('page')) {
         $posted_data = Yii::$app->session['posted_data'];
     }
     if (!empty($posted_data)) {
         Yii::$app->session['posted_data'] = $posted_data;
         $date_range = $posted_data['date_range'];
         $player_select = $posted_data['player_select'];
         $category_select = $posted_data['category_select'];
         $warehouse_select = $posted_data['warehouse_select'];
         $products_select = $posted_data['products_select'];
         $query = TransactionItems::find();
         $query->joinWith(['transaction', 'product', 'transaction.userAccount']);
         if ($player_select > 0) {
             $query->andWhere([Transactions::tableName() . '.user_id' => $player_select]);
         }
         if ($date_range != '') {
             $date_range = explode(' to ', $date_range);
             $start_date = $date_range[0];
             $end_date = $date_range[1];
             $query->andWhere(['BETWEEN', 'transaction_date', Yii::$app->formatter->asDate($start_date, 'php:Y-m-d') . ' 00:00:00', Yii::$app->formatter->asDate($end_date, 'php:Y-m-d') . ' 23:59:59']);
         }
         if ($category_select > 0) {
             $query->andWhere([Products::tableName() . '.category_id' => $category_select]);
         }
         if ($warehouse_select > 0) {
             $query->andWhere([Products::tableName() . '.warehouse_id' => $warehouse_select]);
         }
         if ($products_select > 0) {
             $query->andWhere(['product_id' => $products_select]);
         }
         $pagination = new Pagination(['totalCount' => $query->count(), 'pageSize' => 10]);
         $total_quantity = $query->sum(TransactionItems::tableName() . '.quantity');
         $total_buy_price = $query->sum(TransactionItems::tableName() . '.buy_price');
         $total_sell_price = $query->sum(TransactionItems::tableName() . '.sell_price');
         $total_amount = $query->sum(TransactionItems::tableName() . '.quantity * ' . TransactionItems::tableName() . '.sell_price');
         //            echo $query->prepare(Yii::$app->db->queryBuilder)->createCommand()->rawSql;
         $transactions = $query->offset($pagination->offset)->limit($pagination->limit)->all();
         $data = ['transactions' => $transactions, 'g_total_quantity' => $total_quantity, 'g_total_buy_price' => $total_buy_price, 'g_total_sell_price' => $total_sell_price, 'g_total_amount' => $total_amount, 'pagination' => $pagination, 'posted_data' => $posted_data];
     }
     return $this->render('products', $data);
 }