<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Booking extends Model
{
    // protected $guarded = [];
    protected $hidden = array(  "user",
                                "rate_id",
                                "rmb_book_amount",
                                "rmb_book_pay_method",
                                "rmb_book_account_name",
                                "rmb_book_bank_name",
                                "rmb_book_acc_no",
                                "rmb_book_bank_branch",
                                "usd_book_amount",
                                "usd_book_pay_method",
                                "usd_book_company_name",
                                "usd_book_company_address",
                                "usd_book_swift_code",
                                "usd_book_cnap",
                                "usd_book_bank_branch");

    protected $fillable = ['id', 'term', 'rate_id', 'user_id', 'amount', 'account_name', 'account_num', 'bank_name',
                            'bank_branch', 'company_name', 'service_charge', 'billing_charge'];
                            
    public $timestamps = true;

    public function scopeCompleteOrders($query){
        return $query->where(function ($query) {
            $query->where(function ($query) {
                $query->where('admin_status', '6')
                    ->where(function ($query) {
                        $query->where('term', 'x2_cash')
                            ->orwhere('term', 'x2_cheque')
                            ->orwhere('term', 'x2_ba');
                    });
            })
                ->orwhere(function ($query) {
                    $query->where('admin_status', '7')
                        ->where(function ($query) {
                            $query->where('term', 'x1_cash')
                                ->orwhere('term', 'x1_cheque')
                                ->orwhere('term', 'x1_ba');
                        });
                });
        })->where('is_cancel', 0);
    }

    public function scopeInCompleteOrders($query){
        return $query->where(function ($query) {
            $query->where(function ($query) {
                $query->where('admin_status', '<', '6')
                    ->where(function ($query) {
                        $query->where('term', 'x2_cash')
                            ->orwhere('term', 'x2_cheque')
                            ->orwhere('term', 'x2_ba');
                    });
            })
            ->orwhere(function ($query) {
                $query->where('status', '<', '7')
                    ->where(function ($query) {
                        $query->where('term', 'x1_cash')
                            ->orwhere('term', 'x1_cheque')
                            ->orwhere('term', 'x1_ba');
                    });
            });
        })->where('is_cancel', 0);
    }
    public function scopeUserBooking($query, $userId){
        if($userId){
            return $query->where('user_id', $userId);
        }
        return $query;
    }
    
    public function user(){
        return $this->belongsTo(User::class);
    }

    public function supplierBooking(){
        return $this->hasOne(SupplierBooking::class);
    }

    public function chinaBankSlip(){
        return $this->hasMany(ChinaBankSlip::class);
    }
    
    public function userBankSlip(){
        return $this->hasOne(UserBankSlip::class);
    }

    public function purchaseOrder(){
        return $this->hasOne(PurchaseOrder::class);
    }

    public function invoice(){
        return $this->hasOne(Invoice::class);
    }


}

