mardi 29 janvier 2019

How to reduce the product stock when the item is used

This is my 1st table

public function up()
    {
        Schema::create('perbaikans', function (Blueprint $table) {
            $table->increments('id_perbaikan');
            $table->string('nomor_dokumen_perbaikan', 25)->unique();
            $table->integer('id_teknisi')->unsigned();
            $table->integer('id_kulkas')->unsigned();
            $table->integer('id_tipe_pekerjaan')->unsigned();
            $table->string('temuan_masalah', 100);
            $table->date('tanggal_perbaikan');
            $table->timestamps();
            $table->foreign('id_teknisi')->references('id_teknisi')->on('teknisis');
            $table->foreign('id_kulkas')->references('id_kulkas')->on('kulkas');
            $table->foreign('id_tipe_pekerjaan')->references('id_tipe_pekerjaan')->on('tipe_pekerjaans');
        });
    }

This my 2nd table

public function up()
    {
        Schema::create('sukucadangs', function (Blueprint $table) {
            $table->increments('id_sukucadang');
            $table->string('nomor_sukucadang', 5)->unique();
            $table->string('nama_sukucadang', 35);
            $table->integer('stok');
            $table->integer('id_kategori_sukucadang')->unsigned();
            $table->timestamps();
            $table->foreign('id_kategori_sukucadang')->references('id_kategori_sukucadang')->on('kategori_sukucadangs');
        });
    }

And this is my intermediate table

public function up()
    {
        Schema::create('pemakaian_sukucadangs', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('id_perbaikan')->unsigned();
            $table->integer('id_sukucadang')->unsigned();
            $table->integer('qty');
            $table->foreign('id_perbaikan')->references('id_perbaikan')->on('perbaikans');
            $table->foreign('id_sukucadang')->references('id_sukucadang')->on('sukucadangs');
        });
    }

My Controller

class PerbaikanController extends Controller
{
    public function create()
    {
        $teknisis = Teknisi::all();
        $kulkas = Kulkas::all();
        $tipepekerjaans = TipePekerjaan::all();
        $sukucadangs = Sukucadang::all();
        return view('perbaikan.tambah', compact('teknisis', 'kulkas', 'tipepekerjaans', 'sukucadangs'));
    }


    public function store(Request $request)
    {
      
        $perbaikan = new Perbaikan;

        $perbaikan->nomor_dokumen_perbaikan = $request->nomor_dokumen_perbaikan;
        $perbaikan->id_teknisi = $request->id_teknisi;
        $perbaikan->id_kulkas = $request->id_kulkas;
        $perbaikan->id_tipe_pekerjaan = $request->id_tipe_pekerjaan;
        $perbaikan->temuan_masalah = $request->temuan_masalah;
        $perbaikan->tanggal_perbaikan = $request->tanggal_perbaikan;
        $perbaikan->qty = $request->qty;

        $perbaikan->save();

        $sukucadang = Sukucadang::findOrFail($request->id_sukucadang);
        $sukucadang->stok -= $request->qty;
        $sukucadang->update();

        $perbaikan->sukucadang()->sync($request->sukucadang, false);

    }
}

How to reduce stok in sukucadangs tables if the item is used. For example, I have item1 with stock 10, and item2 20, when I use item1 and item2 with each qty 2 and 3, then the stock of items in the sukucadangs table will automatically decrease Thank you very much



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire