<?php
namespace WpBookingEventFrontend\app\models;
defined('_WPBOOKINGEVENT_EXEC') or die;

use Cassandra\Date;
use WpBookingEvent\Factory;
use WpBookingEvent\Mvc\Model\Model;
use WpBookingEvent\Mvc\Model\ModelForm;
use WpBookingEvent\Form\Form;
use WpBookingEvent\Mvc\Model\ModelList;
use WpBookingEvent\Registry\Registry;
use WpBookingEventAdmin\app\helpers\AdminHelper;

class BookingModel extends ModelForm
{
    /**
     * @var string
     */
    public $context = "booking";
    /**
     * @var string
     */
    public $table_name = "booking";
    public function book_service($data)
    {

        $employee_id = $data['employee_id'];
        $event_id = isset($data['event_id'])?$data['event_id']:0;
        $number_person = isset($data['number_person'])?$data['number_person']:1;
        $first_name = $data['first_name'];
        $last_name = $data['last_name'];
        $email = $data['email'];
        $phone = $data['phone'];
        $payment_id = $data['payment_id'];
        $created_date = Factory::getDate();

        $customerModel = Model::getInstance('customer');
        $data_customer = [
            "first_name" => $first_name,
            "last_name" => $last_name,
            "mobile" => $phone,
            "email" => $email
        ];
        $customer = $customerModel->save($data_customer);
        $event=array();
        if($event_id > 0) {
            $eventModel = Model::getInstance('event');
            $event = $eventModel->getItem($event_id);
        }

        $service_id = $data['service_id'];
        $serviceModel = Model::getInstance('service');
        $service = $serviceModel->getItem($service_id);
        $list_event=$serviceModel->getEventByServiceId($service_id);
        $paymentModel = Model::getInstance('payment');
        $payment = $paymentModel->getItem($payment_id);
        $start_date = $data['calendar'];
        $newtimestamp = strtotime($start_date.'+'. ($service->duration * 60).' minutes');
        $end_date = date('Y-m-d H:i:s', $newtimestamp);
        $multiply_number = $service->multiply_number;
        $quantity = $number_person;
        $total_event=0;
        foreach ($list_event as $event){
            $total_event +=$event->price;
        }
        if ($multiply_number == 1) {
            $total = (int)$number_person * $service->price + $total_event;
        } else {
            $total = $service->price + $total_event;
        }

        $orderModel = Model::getInstance('order');
        $data_order = [
            "customer_id" => $customer->id,
            "payment_id" => $payment_id,
            "total" => $total,
            "type" => $event?AdminHelper::getWpBookingEventKey():AdminHelper::getWpBookingServiceKey(),
            "created_date"=>$created_date->toSql()
        ];
        $order = $orderModel->save($data_order);
        $orderDetailModel = Model::getInstance('orderdetail');
        $data_order_detail = [
            "id" => 0,
            "order_id" => $order->id,
            "quantity" => $quantity,
            "total" => $total,
            "employee_id" => $employee_id,
            "service_id" => $service_id,
            "location_id" => $event?$event->location_id:$service->location_id,
            "price" => $event?$event->price:$service->price
        ];
        $oder_detail = $orderDetailModel->save($data_order_detail);
        $orderDetailRateModel = Model::getInstance('orderdetailrate');
        $data_order_detail_rate = [
            "id" => 0,
            "order_detail_id" => $oder_detail->id,
            "start_date" => $start_date,
            "end_date" => $end_date,
            "employee_id" => $employee_id,
            "service_id" => $service_id,
            "location_id" => $event?$event->location_id:$service->location_id,
            "total" => $total,
            "price" => $event?$event->price:$service->price,
            "subtotal" => $total,
            "quantity" => $quantity,
        ];
        $order_detail_rate = $orderDetailRateModel->save($data_order_detail_rate);
        return $order;
    }
    public function book_event($data)
    {

        $employee_id = $data['employee_id'];
        $number_person = isset($data['number_person'])?$data['number_person']:1;
        $first_name = $data['first_name'];
        $last_name = $data['last_name'];
        $email = $data['email'];
        $phone = $data['phone'];
        $payment_id = $data['payment_id'];
        $created_date = Factory::getDate();

        $customerModel = Model::getInstance('customer');
        $data_customer = [
            "first_name" => $first_name,
            "last_name" => $last_name,
            "mobile" => $phone,
            "email" => $email,
            "created_date"=>$created_date->toSql()
        ];
        $customer = $customerModel->save($data_customer);
        $event_id = $data['event_id'];
        $eventModel = Model::getInstance('event');
        $event = $eventModel->getItem($event_id);
        $paymentModel = Model::getInstance('payment');
        $payment = $paymentModel->getItem($payment_id);
        $start_date_recurring=$data['start_date_recurring'];
        $end_date_recurring=$data['end_date_recurring'];
        $start_time_recurring= $data['start_time_recurring'];
        if(isset($data['day_of_week'])){
            $day_of_week=$data['day_of_week'];
        }else{
            $day_of_week=[];
        }
        if( in_array( "0" ,$day_of_week ) )
        {
            $sunday=1;
        }else{
            $sunday=0;
        }
        if( in_array( "1" ,$day_of_week ) )
        {
            $monday=1;
        }else{
            $monday=0;
        }
        if( in_array( "2" ,$day_of_week ) )
        {
            $tuesday=1;
        }else{
            $tuesday=0;
        }
        if( in_array( "3" ,$day_of_week ) )
        {
            $wednesday=1;
        }else{
            $wednesday=0;
        }
        if( in_array( "4" ,$day_of_week ) )
        {
            $thursday=1;
        }else{
            $thursday=0;
        }
        if( in_array( "5" ,$day_of_week ) )
        {
            $friday=1;
        }else{
            $friday=0;
        }
        if( in_array( "6" ,$day_of_week ) )
        {
            $saturday=1;
        }else{
            $saturday=0;
        }


        if(isset($data['day_of_month'])){
            $day_of_month=$data['day_of_month'];
        }else{
            $day_of_month=[];
        }

        if( in_array( 1 ,$day_of_month ) )
        {
            $day_1=1;
        }else{
            $day_1=0;
        }
        if( in_array( 2 ,$day_of_month ) )
        {
            $day_2=1;
        }else{
            $day_2=0;
        }
        if( in_array( 3 ,$day_of_month ) )
        {
            $day_3=1;
        }else{
            $day_3=0;
        }
        if( in_array( 4 ,$day_of_month ) )
        {
            $day_4=1;
        }else{
            $day_4=0;
        }
        if( in_array( 5 ,$day_of_month ) )
        {
            $day_5=1;
        }else{
            $day_5=0;
        }
        if( in_array( 6 ,$day_of_month ) )
        {
            $day_6=1;
        }else{
            $day_6=0;
        }
        if( in_array( 7 ,$day_of_month ) )
        {
            $day_7=1;
        }else{
            $day_7=0;
        }
        if( in_array( 8 ,$day_of_month ) )
        {
            $day_8=1;
        }else{
            $day_8=0;
        }
        if( in_array( 9 ,$day_of_month ) )
        {
            $day_9=1;
        }else{
            $day_9=0;
        }
        if( in_array( 10 ,$day_of_month ) )
        {
            $day_10=1;
        }else{
            $day_10=0;
        }
        if( in_array( 11 ,$day_of_month ) )
        {
            $day_11=1;
        }else{
            $day_11=0;
        }
        if( in_array( 12 ,$day_of_month ) )
        {
            $day_12=1;
        }else{
            $day_12=0;
        }
        if( in_array( 13 ,$day_of_month ) )
        {
            $day_13=1;
        }else{
            $day_13=0;
        }
        if( in_array( 14 ,$day_of_month ) )
        {
            $day_14=1;
        }else{
            $day_14=0;
        }
        if( in_array( 15 ,$day_of_month ) )
        {
            $day_15=1;
        }else{
            $day_15=0;
        }
        if( in_array( 16 ,$day_of_month ) )
        {
            $day_16=1;
        }else{
            $day_16=0;
        }
        if( in_array( 17 ,$day_of_month ) )
        {
            $day_17=1;
        }else{
            $day_17=0;
        }
        if( in_array( 18 ,$day_of_month ) )
        {
            $day_18=1;
        }else{
            $day_18=0;
        }
        if( in_array( 19 ,$day_of_month ) )
        {
            $day_19=1;
        }else{
            $day_19=0;
        }
        if( in_array( 20 ,$day_of_month ) )
        {
            $day_20=1;
        }else{
            $day_20=0;
        }
        if( in_array( 21 ,$day_of_month ) )
        {
            $day_21=1;
        }else{
            $day_21=0;
        }
        if( in_array( 22 ,$day_of_month ) )
        {
            $day_22=1;
        }else{
            $day_22=0;
        }
        if( in_array( 23 ,$day_of_month ) )
        {
            $day_23=1;
        }else{
            $day_23=0;
        }
        if( in_array( 24 ,$day_of_month ) )
        {
            $day_24=1;
        }else{
            $day_24=0;
        }
        if( in_array( 25 ,$day_of_month ) )
        {
            $day_25=1;
        }else{
            $day_25=0;
        }
        if( in_array( 26 ,$day_of_month ) )
        {
            $day_26=1;
        }else{
            $day_26=0;
        }
        if( in_array( 27 ,$day_of_month ) )
        {
            $day_27=1;
        }else{
            $day_27=0;
        }
        if( in_array( 28,$day_of_month ) )
        {
            $day_28=1;
        }else{
            $day_28=0;
        }
        if( in_array( 29 ,$day_of_month ) )
        {
            $day_29=1;
        }else{
            $day_29=0;
        }
        if( in_array( 30 ,$day_of_month ) )
        {
            $day_30=1;
        }else{
            $day_30=0;
        }
        if( in_array( 31 ,$day_of_month ) )
        {
            $day_31=1;
        }else{
            $day_31=0;
        }


        $total=$event->price;
        $repeat=$data['repeat'];
        if($repeat=== "does not repeat"){
            $not_recurring =1;
            $daily=0;
            $weekly =0;
            $color="";
            $monthly=0;
            $start_date = $data['start_date'];
            $start_time = $data['start_time'];
            $total=$event->price;
        }else if($repeat==="daily"){
            $daily =1;
            $not_recurring=0;
            $weekly =0;
            $monthly=0;
            $color="green";
            $start_date = $data['start_date_recurring'];
            $start_time = $data['start_time_recurring'];
            $start_daily=strtotime($start_date);
            $end_daily=strtotime($end_date_recurring);
            $between_daily = $end_daily-$start_daily;
            $number_day_recurring=round($between_daily / (60 * 60 * 24));
            $total= $total * $number_day_recurring;
        } else if($repeat==="weekly"){
            $weekly =1;
            $not_recurring=0;
            $daily=0;
            $monthly=0;
            $color="orange";
            $start_date = $data['start_date_recurring'];
            $start_time = $data['start_time_recurring'];
            $end_date_recurring_weekly=date ("Y-m-d", strtotime("-1 day", strtotime($end_date_recurring)));
            $start_date=date ("Y-m-d", strtotime($start_date));
            function number_day($startdate,$enddaterecurring_weekly,$i,$number_day=0){
                while(strtotime($startdate) <= strtotime($enddaterecurring_weekly)){
                    if(date("N",strtotime($startdate))==$i) {
                            $number_day++;
                    }
                    $startdate = date ("Y-m-d", strtotime("+1 day", strtotime($startdate)));
                }
                return $number_day;
            }
            if( in_array( "0" ,$day_of_week ) )
            {
                $number_sun= number_day($start_date,$end_date_recurring_weekly,7);
            } else {
                $number_sun =0;
            }
            if(in_array( "1" ,$day_of_week )){
                $number_mon= number_day($start_date,$end_date_recurring_weekly,1);
            }else{
                $number_mon =0;
            }
            if(in_array( "2" ,$day_of_week )){
                $number_tue= number_day($start_date,$end_date_recurring_weekly,2);
            }else{
                $number_tue=0;
            }
            if(in_array( "3" ,$day_of_week )){
                $number_wed= number_day($start_date,$end_date_recurring_weekly,3);
            }else{
                $number_wed=0;
            }

            if(in_array( "4" ,$day_of_week )){
                $number_thu= number_day($start_date,$end_date_recurring_weekly,4);
            }else{
                $number_thu=0;
            }
            if(in_array( "5" ,$day_of_week )){
                $number_fri= number_day($start_date,$end_date_recurring_weekly,5);
            }else{
                $number_fri=0;
            }

            if(in_array( "6" ,$day_of_week )){
                $number_sat= number_day($start_date,$end_date_recurring_weekly,6);
            }else{
                $number_sat=0;
            }
            $number_day_recurring=$number_sun + $number_mon + $number_tue +$number_wed + $number_thu +$number_fri +$number_sat;
            $total= $total * $number_day_recurring;
        }else if($repeat==="monthly"){
            $monthly =1 ;
            $not_recurring=0;
            $daily=0;
            $weekly =0;
            $color="purple";
            $start_date = $data['start_date_recurring'];
            $start_time = $data['start_time_recurring'];
            $total_number_day_monthly=0;
            function number_day_monthly($startdate,$enddaterecurring_weekly,$i,$number_day=0){
                while(strtotime($startdate) <= strtotime($enddaterecurring_weekly)){
                    if(date("d",strtotime($startdate))==$i) {
                        $number_day++;
                    }
                    $startdate = date ("Y-m-d", strtotime("+1 day", strtotime($startdate)));
                }
                return $number_day;
            }

            foreach ($day_of_month as $item) {
                $numday_day_monthly= number_day_monthly($start_date,$end_date_recurring,$item);
                $total_number_day_monthly +=$numday_day_monthly;
            }
            $total =$total * $total_number_day_monthly;
        }
        $start_date_time = $start_date." ".$start_time.":00";
        $startTime= $start_time_recurring.":00";
        $end_time_re=$end_date_recurring." ".$start_time.":00";
        $endTime=date('H:i:s', strtotime($startTime.'+'.($event->duration * 60).' minutes'));
        $newtimestamp = strtotime($end_time_re.'+'. ($event->duration * 60).' minutes');
        $end_date = date('Y-m-d H:i:s', $newtimestamp);
        $orderModel = Model::getInstance('order');
        $quantity = $number_person;
        $data_order = [
            "customer_id" => $customer->id,
            "payment_id" => $payment_id,
            "total" => $total,
            "type" => AdminHelper::getWpBookingEventKey(),
            "created_date"=>$created_date->toSql(),
            "not_recurring"=>$not_recurring,
            "daily" =>$daily,
            "weekly" =>$weekly,
            "monthly" =>$monthly,
        ];
        $order = $orderModel->save($data_order);
        $orderDetailModel = Model::getInstance('orderdetail');
        $data_order_detail = [
            "id" => 0,
            "order_id" => $order->id,
            "start_date" => $start_date_time,
            "end_date" => $end_date,
            "total" => $total,
            "quantity" => $quantity,
            "customer_id" => $customer->id,
            "employee_id" => $employee_id,
            "event_id" => $event_id,
            "location_id" => $event?$event->location_id:$event->location_id,
            "price" => $event->price
        ];
        $oder_detail = $orderDetailModel->save($data_order_detail);
        $orderDetailRateModel = Model::getInstance('orderdetailrate');
        $data_order_detail_rate = [
            "id" => 0,
            "order_id" => $order->id,
            "order_detail_id" => $oder_detail->id,
            "start_date" => $start_date_time,
            "end_date" => $end_date,
            "employee_id" => $employee_id,
            "event_id" => $event_id,
            "location_id" => $event->location_id,
            "total" => $total,
            "customer_id" => $customer->id,
            "quantity" => $quantity,
            "price" => $event->price,
            "subtotal" => $total,
            "not_recurring"=>$not_recurring,
            "daily" =>$daily,
            "weekly" =>$weekly,
            "monthly" =>$monthly,
            "color_event" =>$color,
            "startRecur" =>$start_date_recurring,
            "endRecur" => $end_date_recurring,
            "startTime"=> $startTime,
            "endTime" => $endTime,
            "re_sunday"=>$sunday,
            "re_monday"=>$monday,
            "re_tuesday"=>$tuesday,
            "re_wednesday"=>$wednesday,
            "re_thursday"=>$thursday,
            "re_friday"=>$friday,
            "re_saturday"=>$saturday,
            "day_1"=>$day_1,
            "day_2"=>$day_2,
            "day_3"=>$day_3,
            "day_4"=>$day_4,
            "day_5"=>$day_5,
            "day_6"=>$day_6,
            "day_7"=>$day_7,
            "day_8"=>$day_8,
            "day_9"=>$day_9,
            "day_10"=>$day_10,
            "day_11"=>$day_11,
            "day_12"=>$day_12,
            "day_13"=>$day_13,
            "day_14"=>$day_14,
            "day_15"=>$day_15,
            "day_16"=>$day_16,
            "day_17"=>$day_17,
            "day_18"=>$day_18,
            "day_19"=>$day_19,
            "day_20"=>$day_20,
            "day_21"=>$day_21,
            "day_22"=>$day_22,
            "day_23"=>$day_23,
            "day_24"=>$day_24,
            "day_25"=>$day_25,
            "day_26"=>$day_26,
            "day_27"=>$day_27,
            "day_28"=>$day_28,
            "day_29"=>$day_29,
            "day_30"=>$day_30,
            "day_31"=>$day_31,
        ];
        $order_detail_rate = $orderDetailRateModel->save($data_order_detail_rate);
        return $order;
    }


    public function add_google_calendar($oder_id){

        $client = self::getClient($oder_id);
        $service = new \Google_Service_Calendar($client);
        $modelOrder = Model::getInstance('order');
        $order = $modelOrder->getItem($oder_id);
        $appConfig = Factory::getAppConfig();
        $timezone = $appConfig->get('timezone','');
        $start = new \DateTime($order->list_rate[0]->start_date, new \DateTimeZone($timezone));
        $end = new \DateTime($order->list_rate[0]->end_date, new \DateTimeZone($timezone));
        $expert = $order->list_rate[0]->employee_fullname;
        $duration = $order->list_rate[0]->duration;
        $description = $order->list_rate[0]->service_description;

        $event = new \Google_Service_Calendar_Event(array(
            'summary' => $order->list_rate[0]->service_name.' '.$oder_id,
            'location' => $order->list_rate[0]->location_address,
            'description' =>"'Expert': $expert \n'Duration': $duration \n'Description': $description",
            'start' => array(
                'dateTime' => $start->format('c'),
                'timeZone' => $timezone
            ),
            'end' => array(
                'dateTime' => $end->format('c'),
                'timeZone' => $timezone
            ),
            'recurrence' => array(
                'RRULE:FREQ=DAILY;COUNT=1'
            ),
            'attendees' => array(
                array('email' => $order->customer[0]->email),
            ),
            'reminders' => array(
                'useDefault' => false,
                'overrides' => array(
                    array('method' => 'email', 'minutes' => 24 * 60),
                    array('method' => 'popup', 'minutes' => 10),
                ),
            ),
        ));
        $calendarId = 'primary';
        $event = $service->events->insert($calendarId, $event);


    }
    public function add_google_calendar_event($oder_id){

        $client = self::getClient($oder_id);
        $input=Factory::getInput();
        $data=$input->getData();
        if(isset($data['day_of_month'])){
            $day_of_month=$data['day_of_month'];
        }else{
            $day_of_month=[];
        }
        $service = new \Google_Service_Calendar($client);
        $modelOrder = Model::getInstance('order');
        $order = $modelOrder->getItem($oder_id);
        $appConfig = Factory::getAppConfig();
        $timezone = $appConfig->get('timezone','');
        $expert = $order->list_rate[0]->employee_fullname;
        $duration = $order->list_rate[0]->event_duration;
        $description = $order->list_rate[0]->event_description;
        $number_day_recurring = ($order->list_rate[0]->total)/($order->list_rate[0]->price);
        $day_of_week=[];
        if($order->list_rate[0]->re_sunday ==1){
            array_push($day_of_week, "SU");
        }
        if($order->list_rate[0]->re_monday ==1){
            array_push($day_of_week, "MO");
        }
        if($order->list_rate[0]->re_tuesday ==1){
            array_push($day_of_week, "TU");
        }
        if($order->list_rate[0]->re_wednesday ==1){
            array_push($day_of_week, "WE");
        }
        if($order->list_rate[0]->re_thursday ==1){
            array_push($day_of_week, "TH");
        }
        if($order->list_rate[0]->re_friday ==1){
            array_push($day_of_week, "FR");
        }
        if($order->list_rate[0]->re_saturday ==1){
            array_push($day_of_week, "SA");
        }
        $byday = implode(',', array_map(
            function ($v) {
                return strtoupper(substr($v,0,2));
            },$day_of_week
        ));
        $bymonthday=implode(',',$day_of_month);
        $start = new \DateTime($order->list_rate[0]->startRecur.' '.$order->list_rate[0]->startTime);
        if($order->not_recurring == 1) {
            $start = new \DateTime($order->list_rate[0]->start_date, new \DateTimeZone($timezone));
            $end = new \DateTime($order->list_rate[0]->end_date, new \DateTimeZone($timezone));
            $dtstart= date('Ymd',strtotime($order->list_rate[0]->startRecur));
            $dtend = date('Ymd',strtotime($order->list_rate[0]->startRecur));
            $recurrence = array('RRULE:FREQ=DAILY;COUNT=1');
        } else if($order->daily ==1) {
            $start = new \DateTime($order->list_rate[0]->startRecur.' '.$order->list_rate[0]->startTime, new \DateTimeZone($timezone));
            $end = new \DateTime($order->list_rate[0]->startRecur.' '.$order->list_rate[0]->endTime, new \DateTimeZone($timezone));
            $dtstart= date('Ymd',strtotime($order->list_rate[0]->startRecur));
            $dtend = date('Ymd',strtotime($order->list_rate[0]->startRecur));
            $recurrence = array('RRULE:FREQ=DAILY;COUNT='.$number_day_recurring);
        } else if($order->weekly ==1){
            $start = new \DateTime($order->list_rate[0]->startRecur.' '.$order->list_rate[0]->startTime, new \DateTimeZone($timezone));
            $end = new \DateTime($order->list_rate[0]->startRecur.' '.$order->list_rate[0]->endTime, new \DateTimeZone($timezone));
            $dtstart= date('Ymd',strtotime($order->list_rate[0]->startRecur));
            $dtend = date('Ymd',strtotime($order->list_rate[0]->startRecur));
            $recurrence = array('RRULE:FREQ=WEEKLY;COUNT='.$number_day_recurring.';BYDAY='.$byday);
        } else if($order->monthly ==1){
            $start = new \DateTime($order->list_rate[0]->startRecur.' '.$order->list_rate[0]->startTime, new \DateTimeZone($timezone));
            $end = new \DateTime($order->list_rate[0]->startRecur.' '.$order->list_rate[0]->endTime, new \DateTimeZone($timezone));
            $dtstart= date('Ymd',strtotime($order->list_rate[0]->startRecur));
            $dtend = date('Ymd',strtotime($order->list_rate[0]->startRecur));
            $recurrence = array('RRULE:FREQ=MONTHLY;INTERVAL=1;COUNT='.$number_day_recurring.';BYMONTHDAY='.$bymonthday);
        }
        $event = new \Google_Service_Calendar_Event(array(
            'summary' => $order->list_rate[0]->event_name.' '.$oder_id,
            'location' => $order->list_rate[0]->location_address,
            'description' =>"'Expert': $expert \n'Duration': $duration \n'Description': $description",
            'start' => array(
                'dateTime' => $start->format('c'),
                'timeZone' => $timezone
            ),
            'dtstart' =>$dtstart,
            'end' => array(
                'dateTime' => $end->format('c'),
                'timeZone' => $timezone
            ),
            'dtend' =>$dtend,
            'recurrence' => $recurrence,
            'attendees' => array(
                array('email' => $order->customer[0]->email),
            ),
            'reminders' => array(
                'useDefault' => false,
                'overrides' => array(
                    array('method' => 'email', 'minutes' => 24 * 60),
                    array('method' => 'popup', 'minutes' => 10),
                ),
            ),
        ));
        $calendarId = 'primary';
        $event = $service->events->insert($calendarId, $event);


    }

    function getClient($oder_id)
    {

        $client = new \Google_Client();


        $client->setScopes(\Google_Service_Calendar::CALENDAR);

        $appConfig = Factory::getAppConfig();
        $file_client_secret = $appConfig->get('client_secret_file','');
        if(isset($file_client_secret) && trim($file_client_secret) !== '' && file_exists(WPBOOKINGEVENT_PATH_ROOT.DS.$file_client_secret)) {
            $client->setAuthConfig(WPBOOKINGEVENT_PATH_ROOT.DS.$file_client_secret);
        } else {
            $client->setAuthConfig(WPBOOKINGEVENT_PATH_ROOT.DS.'client_secret_525629706282-i2g4hf4hqjm82m52cdkg06jsuva8oelt.apps.googleusercontent.com.json');
        }

        if(isset($_SESSION['access_token']) && is_array($_SESSION['access_token'])){
            //save event
            $client->setAccessToken($_SESSION['access_token']);

        }else{
            $auth=$client->createAuthUrl();
            $_SESSION['current_oder_id']=$oder_id;
            header("location:$auth");
            die;
        }
        return $client;
    }

}
