<?php

namespace App\Service\Shop;

use App\Models\ShopWarehouseStocks;
use App\Models\UsedProductDetails;
use App\Models\UsedProductMaster;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class UsedProduct
{
    public static function storeData($data)
    {
        $collect = collect($data);

        $created = UsedProductMaster::create([
            'shop_id' => Auth::user()->shop_id,
            'user_id' => Auth::user()->id,
        ]);

        foreach ($collect['list'] as $item) {

            ShopWarehouseStocks::where('shop_id', Auth::user()->shop_id)->where('product_id', $item['product_id'])->update([
                'quantity' => DB::Raw('quantity -'.$item['used_qty']),

            ]);
            UsedProductDetails::create([
                'used_product_masters_id' => $created->id,
                'product_id' => $item['product_id'],
                'quantity' => $item['used_qty'],
                'unit_id' => $item['unit_id'],
                'category_id' => $item['category_id'],
            ]);
        }
    }
}
