Php Laravel 5.4:循环数组并减去每个对象的数量,直到数量达到0为止
我需要循环遍历数组,并从每个对象减去Php Laravel 5.4:循环数组并减去每个对象的数量,直到数量达到0为止,php,foreach,laravel-5.4,Php,Foreach,Laravel 5.4,我需要循环遍历数组,并从每个对象减去所需的数量 例如所需数量=22 这个数组中有三个对象,每个对象的数量=10 所以会是这样的 从到期日起取10件=2019-03-02库存将变为0 从到期日起取10件=2019-03-10库存将变为0 从过期日期取2件=2019-03-11库存将变为8件 这是数组 foreach ( $orderProducts as $order_product ) { //This the quantity in order deta
所需的数量
例如所需数量=22
这个数组中有三个对象,每个对象的数量=10
所以会是这样的
从到期日起取10件=2019-03-02库存将变为0
从到期日起取10件=2019-03-10库存将变为0
从过期日期取2件=2019-03-11库存将变为8件
这是数组
foreach ( $orderProducts as $order_product ) {
//This the quantity in order details.
$orderQuantity = $order_product->quantity;
$currentInventors = Inventory::whereProductId( $order_product->product_id )->get()->sortBy( 'expire_date' );
if ( $currentInventors != null ) {
foreach ( $currentInventors as $currentInventory ) {
$takeQuantity = $currentInventory->inventory - $orderQuantity; //10-15 = 5
if ( $currentInventory->inventory >= $orderQuantity ) {
Inventory::whereId( $currentInventory->id )->update( [ 'inventory' => $takeQuantity ] );
break;
} elseif ( $currentInventory->inventory < $orderQuantity ) {
Inventory::whereId( $currentInventory->id )->update( [ 'inventory' => 0 ] );
//$takeQuantity = $currentInventory->inventory - $orderQuantity; //10-15 = -5
$newQuantity = $orderQuantity - $currentInventory->inventory; //15-10 = 5
if ( $currentInventory->inventory >= $orderQuantity ) {
Inventory::whereId( $currentInventory->id )->update( [ 'inventory' => $newQuantity ] );
break;
}
}
}
}
}
这是我支持的解决方案。
我的错是我没有在循环中每次减少$orderQuantity
$orderProducts = OrderDetail::whereOrderListsId( $deliveryList->order_lists_id )->get();
foreach ( $orderProducts as $order_product ) {
//This the quantity in order details.
$orderQuantity = $order_product->quantity;
//Here we selected the oldest expire date of the same product in the inventory.
$currentInventors = Inventory::whereProductId( $order_product->product_id )->get()->sortBy( 'expire_date' );
if ( $currentInventors != null ) {
foreach ( $currentInventors as $currentInventory ) {
$takeQuantity = $currentInventory->inventory - $orderQuantity;
if ( $currentInventory->inventory >= $orderQuantity ) {
Inventory::whereId( $currentInventory->id )->update( [ 'inventory' => $takeQuantity ] );
break;
} else {
$orderQuantity = $orderQuantity - $currentInventory->inventory;
Inventory::whereId( $currentInventory->id )->update( [ 'inventory' => 0 ] );
}
}
}
}
现在可以正常工作。包括一个我们可以工作的阵列with@Andreas给你,我更新了我的问题。我们不能使用数组。很好。@Andreas更新了,如果你需要其他东西,请告诉我你希望我证明什么。“关于elseif,如果它是一种尝试[…]”-我的观点是,如果条件与前面的if
完全相反,你不需要elseif
,在这种情况下,一个简单的else
在没有任何进一步条件的情况下也可以实现同样的效果。如果$currentInventory->inventory>=$orderQuantity
不是这种情况,那么当前库存只能小于订购数量,没有其他第三种可能性…
$orderProducts = OrderDetail::whereOrderListsId( $deliveryList->order_lists_id )->get();
foreach ( $orderProducts as $order_product ) {
//This the quantity in order details.
$orderQuantity = $order_product->quantity;
//Here we selected the oldest expire date of the same product in the inventory.
$currentInventors = Inventory::whereProductId( $order_product->product_id )->get()->sortBy( 'expire_date' );
if ( $currentInventors != null ) {
foreach ( $currentInventors as $currentInventory ) {
$takeQuantity = $currentInventory->inventory - $orderQuantity;
if ( $currentInventory->inventory >= $orderQuantity ) {
Inventory::whereId( $currentInventory->id )->update( [ 'inventory' => $takeQuantity ] );
break;
} else {
$orderQuantity = $orderQuantity - $currentInventory->inventory;
Inventory::whereId( $currentInventory->id )->update( [ 'inventory' => 0 ] );
}
}
}
}