<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DB;
use App\Commission;
use EF;

class Transfer extends Model
{
   private $generic_condition;
  

    public function __construct()
    {
           
       $this->setCondition(['is_recycle' => '0']);
    }

    public function setCondition($condition = [])
    {
        $this->generic_condition = $condition;
    }

    public function getCondition()
    {
        return $this->generic_condition;
    }

      //STORING NEW BOOKING TRANSFER
    public function storeTransfer($object)
    {

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

          $booking = Booking::find($object['booking_id']);
          
          $label_list = EF::getWhiteLabel();


          if(isset($booking->id))
          {
              $old_owner = $booking->user_id;
              $booking->user_id = $object['user_id'];
                $booking->status  = "Transfer";

              $booking->save();

              $transfer = new Transfer;
              $transfer->user_id                           = $old_owner;
              $transfer->booking_id                        = $object['booking_id'];
              $transfer->transfer_fee                      = $object[$label_list['transfer_fee']];  
              $transfer->ms                                = $this->generateMsNumber();             
              $transfer->finger_print                      = NULL;
              $transfer->print_width                       = NULL;
              $transfer->raw_data                          = NULL;
              $transfer->remarks                           = $object[$label_list['remarks']];
              $transfer->created_at                        = date('Y-m-d',strtotime($object[$label_list['transfer_date']]));

              $transfer->save();

              return with($transfer);
        }
        return 0;

        });
    }

    public function getTransferBookings()
    {
      $condition = $this->getCondition();
     

        return Transfer::where($condition)->orderBy('id','desc')->get();

    }

    public function getBranchWiseTransfer($branch_in = [],$type = NULL)
    {
        $condition = $this->getCondition();

        if(strlen(trim($type)) > 0)
        {
          $condition +=  ['type' => $type];

        }  

        return   Transfer::where($condition)->whereHas('booking',function($query) use ($branch_in){
              
               $query->whereIn('branch_id',$branch_in);

          })->orderBy('id','desc')->get();


    }
    
    public function getTransferByBookingId($booking_id)
    {
       $condition = $this->getCondition();
       $condition += ['booking_id' => $booking_id];
        return Transfer::where($condition)->orderBy('id','asc')->get();

    }

    public function getCurrentTransferByBookingId($booking_id)
    {
       $condition = $this->getCondition();
       $condition += ['booking_id' => $booking_id];
        return Transfer::where($condition)->first();

    }

     public function approveTransfer($id)
    {
          return DB::transaction(function () use ($id) {

            $transfer = Transfer::find($id);

            if(isset($transfer->id))
            {
                  $bookingid = $transfer->booking_id;
                  $booking = Booking::find($bookingid);
                  if(isset($booking->id))
                  {
                    $booking->status = "Approve";
                    $booking->update();
                  }

                $transfer->status  = 'Approve';

                 $transfer->update();


                 return with($transfer);

               
            }
            
            return 0;
        });
    }

      //UPDATE TRANSFER AMOUNT
      public function updateTransfer($object,$id)
    {
        
       return  DB::transaction(function () use ($object,$id)  {
        
            $label_list = EF::getWhiteLabel();

            $transfer = Transfer::find($id);

            if(isset($transfer->id) && isset($object[$label_list['transfer_fee']]))
            {
                $transfer->transfer_fee          = $object[$label_list['transfer_fee']];
                
                $transfer->update();

                return with($transfer);
           }
           else
           {

            return 0;
           }

          


        });

    }
     public function getTransferById($id)
    {
      $condition = $this->getCondition();
      $condition += ['id' => $id];
        return Transfer::where($condition)->first();

    }

    
    public function removeBulkTransfer($booking_id)
    {
        return Transfer::where('booking_id',$booking_id)->update(['is_recycle' => 1]);
    }


    //GENERATE MS NUMBER
    public function generateMsNumber()
    {
        $insert_id = transfer::max('id');
        $serial = 'TF/'.str_pad(($insert_id+1), 4, '0', STR_PAD_LEFT); 
        return $serial;

    }
    public function getowner()
    {
        return $this->belongsTo(User::class,'user_id')->where($this->getCondition())->withDefault();
    }

      public function booking()
    {
        return $this->belongsTo(Booking::class,'booking_id')->where($this->getCondition())->withDefault();
    }
}
