<?php

namespace App\Inventory;

use Illuminate\Database\Eloquent\Model;
use App\Inventory\Item;
use App\User;
use App\Branch;

class IssueNote extends Model
{
    //
    public function createGIN($object)
      {

          return \DB::transaction(function () use ($object)  {

            $gin                         = new 	IssueNote;
            $gin->item_id      = $object['product_id'];
            $gin->project_id                = $object['project_id'];
            $gin->user_id                   = '0';
            $gin->receiver_id         = $object['receiver_id'];
            $gin->quantity      = $object['quantity'];
            $gin->remarks      = $object['remarks'];
            $gin->floor      = $object['floor'];
            $gin->p_number      = $object['p_number'];
            $gin->type      = $object['type'];
            $gin->au_id      = $object['au_id'];

            $gin->save();

            return with($gin);
          });
      }

      public function createGINService($object)
        {

            return \DB::transaction(function () use ($object)  {

              $gin                         = new 	IssueNote;
              $gin->item_id      = $object['service_id'];
              $gin->project_id                = $object['project_id'];
              $gin->user_id                   = '0';
              $gin->receiver_id         = $object['receiver_id'];
              $gin->remarks      = $object['remarks'];
              $gin->type      = $object['type'];

              $gin->save();

              return with($gin);
            });
        }

      public function getNoteByType($type)
      {
          $curr_note = IssueNote::where('type',$type)->get();
          return $curr_note;
        }

        public function issueNoteByID($id)
        {
            $curr_note = IssueNote::where('id',$id)->first();
            return $curr_note;
          }

        public function item()
        {
            return $this->belongsTo(Item::class,'item_id')->withDefault();
        }
        public function user()
        {
            return $this->belongsTo(User::class,'receiver_id')->withDefault();
        }
        public function project()
        {
            return $this->belongsTo(Branch::class,'project_id')->withDefault();
        }
}
