fix inventory query

This commit is contained in:
Schneider Roland 2022-12-02 19:02:17 +01:00
parent 9bfb0d676e
commit d1f4c279ae

View File

@ -56,13 +56,13 @@ class InventoryItemSearch extends InventoryItem
public function search($params) public function search($params)
{ {
$query = new Query(); $query = new Query();
$query->select([ $query->select([
'inventory_item.id_inventory_item as item_id_inventory_item', 'inventory_item.id_inventory_item as item_id_inventory_item',
'inventory_item.created_at as item_created_at', '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'), 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', 'user.username as user_username',
'coalesce(inventory_item.count_prev ,0)as item_count_prev', 'coalesce(inventory_item.count_prev ,0) as item_count_prev',
'coalesce(inventory_item.count_in,0) as item_count_in', 'coalesce(inventory_item.count_in,0) as item_count_in',
'coalesce(inventory_item.count_sold,0) as item_count_sold', 'coalesce(inventory_item.count_sold,0) as item_count_sold',
'coalesce(inventory_item.count,0) as item_count', 'coalesce(inventory_item.count,0) as item_count',
@ -77,50 +77,50 @@ class InventoryItemSearch extends InventoryItem
'inventory_item.stock_missing_count as item_stock_missing_count', 'inventory_item.stock_missing_count as item_stock_missing_count',
'inventory_item.stock_missing_money as item_stock_missing_money' 'inventory_item.stock_missing_money as item_stock_missing_money'
]); ]);
$query->from(InventoryItem::tableName()); $query->from(InventoryItem::tableName());
$query->leftJoin(User::tableName(), "user.id = inventory_item.id_user"); $query->leftJoin(User::tableName(), "user.id = inventory_item.id_user");
$query->leftJoin(Product::tableName(), "product.id_product = inventory_item.id_product"); $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->leftJoin(InventoryGroup::tableName(), "inventory_group.id_inventory_group = inventory_item.id_inventory_group");
$query->leftJoin(Inventory::tableName(), "inventory.id_inventory = inventory_item.id_inventory"); $query->leftJoin(Inventory::tableName(), "inventory.id_inventory = inventory_item.id_inventory");
$query->leftJoin( "inventory_item as item_prev", "item_prev.id_inventory_item = inventory_item.id_inventory_item_prev" ); $query->leftJoin( "inventory_item as item_prev", "item_prev.id_inventory_item = inventory_item.id_inventory_item_prev" );
$query->leftJoin( "inventory as inventory_prev", "inventory_prev.id_inventory = item_prev.id_inventory" ); $query->leftJoin( "inventory as inventory_prev", "inventory_prev.id_inventory = item_prev.id_inventory" );
$query->andWhere( ['inventory_item.id_inventory' => $this->inventory->id_inventory] );
$this->load($params);
$query->andWhere( ['inventory_item.id_inventory' => $this->inventory->id_inventory] );
$this->load($params);
$dataProvider = new ActiveDataProvider( $dataProvider = new ActiveDataProvider(
['query' => $query , ['query' => $query ,
'pagination' => ( empty($this->output) ? [] : false ), 'pagination' => ( empty($this->output) ? [] : false ),
'sort' => [ 'sort' => [
'defaultOrder' => ['item_name' => SORT_ASC], 'defaultOrder' => ['item_name' => SORT_ASC],
'attributes' => Helper::mkYiiSortItems([ 'attributes' => Helper::mkYiiSortItems([
['item_created_at', 'item_created_at'], ['item_created_at', 'item_created_at'],
['item_name', 'item_name'], ['item_name', 'item_name'],
['user_username', 'user_username'], ['user_username', 'user_username'],
['item_count_prev', 'item_count_prev'], ['item_count_prev', 'item_count_prev'],
['item_count_sold', 'item_count_sold'], ['item_count_sold', 'item_count_sold'],
['item_count_in', 'item_count_in'], ['item_count_in', 'item_count_in'],
['item_count', 'item_count'], ['item_count', 'item_count'],
['item_count_waste', 'item_count_waste'], ['item_count_waste', 'item_count_waste'],
['item_difference', 'item_difference'], ['item_difference', 'item_difference'],
['item_count_system', 'item_count_system'], ['item_count_system', 'item_count_system'],
['inventory.id_inventory', 'inventory_id_inventory'], ['inventory.id_inventory', 'inventory_id_inventory'],
['inventory.name', 'inventory_name'], ['inventory.name', 'inventory_name'],
['inventory_prev_name', 'inventory_prev_name'], ['inventory_prev_name', 'inventory_prev_name'],
['item_price_brutto', 'item_price_brutto'], ['item_price_brutto', 'item_price_brutto'],
['item_purchase_price_net', 'item_purchase_price_net'], ['item_purchase_price_net', 'item_purchase_price_net'],
['item_stock_missing_count', 'item_stock_missing_count'], ['item_stock_missing_count', 'item_stock_missing_count'],
['item_stock_missing_money', 'item_stock_missing_money'], ['item_stock_missing_money', 'item_stock_missing_money'],
]) ])
, ,
] ]
] ]
); );
@ -129,7 +129,7 @@ class InventoryItemSearch extends InventoryItem
$query->where('0=1'); $query->where('0=1');
return $dataProvider; return $dataProvider;
} }
if ( !empty($this->item_type) && is_numeric($this->item_id) ){ if ( !empty($this->item_type) && is_numeric($this->item_id) ){
if ( $this->item_type == 'product'){ if ( $this->item_type == 'product'){
@ -138,7 +138,7 @@ class InventoryItemSearch extends InventoryItem
$query->andFilterWhere(['inventory_group.id_inventory_group' => $this->item_id]); $query->andFilterWhere(['inventory_group.id_inventory_group' => $this->item_id]);
} }
} }
return $dataProvider; return $dataProvider;
} }