select([ 'inventory_item.id_inventory_item as item_id_inventory_item', 'inventory_item.created_at as item_created_at', new Expression('case when inventory_item.id_product is null then inventory_group.name else product.name end as item_name'), 'user.username as user_username', 'coalesce(inventory_item.count_prev ,0)as item_count_prev', 'coalesce(inventory_item.count_in,0) as item_count_in', 'coalesce(inventory_item.count_sold,0) as item_count_sold', 'coalesce(inventory_item.count,0) as item_count', new Expression('(inventory_item.count - ( inventory_item.count_prev + inventory_item.count_in - inventory_item.count_sold )) as item_difference'), ]); $query->from(InventoryItem::tableName()); $query->innerJoin(User::tableName(), "user.id = inventory_item.id_user"); $query->leftJoin(Product::tableName(), "product.id_product = inventory_item.id_product"); $query->leftJoin(InventoryGroup::tableName(), "inventory_group.id_inventory_group = inventory_item.id_inventory_group"); $query->andWhere( ['id_inventory' => $this->inventory->id_inventory] ); $dataProvider = new ActiveDataProvider( ['query' => $query , 'sort' => [ 'attributes' => Helper::mkYiiSortItems([ ['item_created_at', 'item_created_at'], ['item_name', 'item_name'], ['user_username', 'user_username'], ['item_count_prev', 'item_count_prev'], ['item_count_sold', 'item_count_sold'], ['item_count_in', 'item_count_in'], ['item_count', 'item_count'], ['item_count', 'item_count'], ['item_difference', 'item_difference'], ]) , ] ] ); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere([ 'id_inventory_item' => $this->id_inventory_item, 'id_inventory' => $this->id_inventory, 'count_in' => $this->count_in, 'count_sold' => $this->count_sold, 'count' => $this->count, 'type' => $this->type, 'id_product' => $this->id_product, 'id_inventory_group' => $this->id_inventory_group, 'id_user' => $this->id_user, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, ]); return $dataProvider; } }