Saya perlu mengulang array dan minus needed_quantity dari setiap objek

Mantan. needed_quantity = 22

Dan ada tiga objek dalam array ini, setiap objek kuantitas = 10

Jadi akan menjadi seperti ini.

  1. ambil 10 buah dari tanggal kedaluwarsa = 2019-03-02 inventaris akan menjadi 0
  2. ambil 10 buah dari tanggal kedaluwarsa = 2019-03-10 inventaris akan menjadi 0
  3. ambil 2 buah dari expired_date = 2019-03-11 persediaan akan menjadi 8

Berikut arraynya

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;
                }
            }
        }
    }
}

Sekarang ketika saya memperbarui saya mendapatkan ketiga nilai objek 0

Perbarui susunan

Collection {#715
  #items: array:3 [
    0 => Inventory {#744
      #fillable: array:3 [
        0 => "product_id"
        1 => "inventory"
        2 => "expire_date"
      ]
      #attributes: array:6 [
        "id" => 10
        "product_id" => 857
        "inventory" => 10
        "expire_date" => "2019-03-02"
        "created_at" => "2019-03-07 11:13:21"
        "updated_at" => "2019-03-27 11:16:01"
      ]
    }
    1 => Inventory {#745
      #fillable: array:3 [
        0 => "product_id"
        1 => "inventory"
        2 => "expire_date"
      ]
      #attributes: array:6 [
        "id" => 13
        "product_id" => 857
        "inventory" => 10
        "expire_date" => "2019-03-10"
        "created_at" => "2019-03-10 16:53:21"
        "updated_at" => "2019-03-27 11:16:01"
      ]
    }
    2 => Inventory {#746
      #fillable: array:3 [
        0 => "product_id"
        1 => "inventory"
        2 => "expire_date"
      ]
      #attributes: array:6 [
        "id" => 16
        "product_id" => 857
        "inventory" => 0
        "expire_date" => "2019-03-11"
        "created_at" => "2019-03-10 17:01:14"
        "updated_at" => "2019-03-27 11:16:01"
      ]
    }
  ]
}
-1
Yousef Altaf 27 Maret 2019, 11:29

1 menjawab

Jawaban Terbaik

Inilah solusi yang saya inginkan. Kesalahan saya, saya tidak mengurangi $orderQuantity setiap kali dalam perulangan.

$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 ] );
            }
        }
    }
}

Sekarang bekerja dengan baik.

1
Yousef Altaf 27 Maret 2019, 13:30