рассчитать общее количество материалов продукта, которые принимают материалы на warehouse_materials

Я должен рассчитать общее количество материалов продукта, которые берут материалы на складе .Материалы. например, один продукт готовит 3 или 2 материала, он берет материалы в таблице warehouse_materials ,

для примера product_id = 1 требуется количество material_id 4 и количество material_id = 2 3 , здесь я готовлю 4 product_id = 1 продукт , требуется количество material_id =1 16, а количество materila_id = 2 12

и мой код , он должен показывать, сколько материала попадает на какой склад ,

мой код показывает только общее количество и неверно показывает склад

    public function status($quantity, $material_id) { $collect_data = [];

    $warehouse_materials = WareHouseMaterial::query()->select(
        'warehouse_materials.id',
        'warehouse_materials.material_id',
        'warehouse_materials.reminder',
        'warehouse_materials.ware_house_id',
        DB::raw('materials.name as material'),
        DB::raw('ware_houses.name as warehouse')
    )
        ->join('materials', 'warehouse_materials.material_id', '=', 'materials.id')
        ->join('ware_houses', 'warehouse_materials.ware_house_id', '=', 'ware_houses.id')
        ->where('warehouse_materials.material_id', $material_id)
        ->orderBy('warehouse_materials.id', 'desc')
        ->get();


    foreach ($warehouse_materials as $warehouse_material) {
        $used_quantity = $quantity - $warehouse_material->reminder;

        if ($used_quantity > 0) {
            $used = [];

            $ware_house_id = $warehouse_material->ware_house_id;
            $reminder = $warehouse_material->reminder;
            $id = $warehouse_material->id;
            $material_id = $warehouse_material->material_id;
            $material_name = $warehouse_material->material;
            $warehouse_name = $warehouse_material->warehouse;
            $need = $used_quantity;

            

            array_push(
                $used,`введите сюда код`
                [
                    'id' => $id,`введите сюда код`
                    'warehouse_id' => $ware_house_id,
                    'warehouse' => $warehouse_name,
                    'material_id' => $material_id,
                    'material' => $material_name,
                    'reminder' => $reminder,
                    'take' => $reminder,
                    'need' => $need
                ]
            );
        } elseif ($used_quantity < 0) {
            $used = [];

            $ware_house_id = $warehouse_material->ware_house_id;
            $reminder = $warehouse_material->reminder;
            $id = $warehouse_material->id;
            $material_id = $warehouse_material->material_id;
            $material_name = $warehouse_material->material;
            $warehouse_name = $warehouse_material->warehouse;
            $optional = abs($used_quantity);

            array_push(
                $used,
                [
                    'id' => $id,
                    'warehouse_id' => $ware_house_id,
                    'warehouse' => $warehouse_name,
                    'material_id' => $material_id,
                    'material' => $material_name,
                    'reminder' => $reminder,
                    'optional' => $optional,
                    'take' => $reminder - $optional,
                ]
            );
        }
    }


    array_push($collect_data, [
        'materials' => [
            'used_quantity' => $used,
            'quantity' => $quantity,
            'material_id' => $material_id
        ]
    ]);

    return $collect_data;
}

public function lackReminder($quantity, $product_id)
{
    $set_reminder = [];

    $product_material = ProductMaterial::query()
        ->select(
            'product_material.*',
            DB::raw("'$quantity'*product_material.quantity as totalqty")
        )
        ->where('product_material.product_id', $product_id)
        ->with(['material'])
        ->get();

    array_push($set_reminder, ['product_material' => $product_material]);

    return $set_reminder;
}

public function getProductsWithMaterials($sales)
{
    $products = [];

    foreach ($sales as $sale) {

        $product = Product::where('id', $sale['product_id'])
            ->select('id', 'name')->get();
    }

    $product = Product::where('id', $sale['product_id'])->select('id', 'name')->get();

    $product_materials = ProductMaterial::select('product_material.*')->where('product_id', $sale['product_id'])->get();

    foreach ($product_materials as $product_material) {
        $status = $this->status(($product_material->quantity * $sale['quantity']), $product_material->material_id);
    }

    array_push(

        $products,

        [

            'quantity' => $sale['quantity'],

            'product' => $product,

            'status' => $status,

            'materials' => $this->lackReminder($sale['quantity'], $sale['product_id']),


        ]
    );


    return $products;
}

public function getProducts($product_id)
{
    $product_data = [];

    $product = Product::query()->select('id', 'name')->where('id', $product_id)->get();

    array_push($product_data, ['products' => $product]);

    return $product_data;
}

public function calculateTotalMaterials()
{
}

public function sale(Request $request)
{

    $products = [];

    if (is_array($sales = $request['sales'])) {

        $products = $this->getProductsWithMaterials($sales);
    }

    return response()->json([
        'result' => [
            'data' => [
                'report_product_materials' => $products,
            ],

            'success' => true
        ],
    ]);
}

Ответы (0 шт):