<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Schedule_user_downgrade extends CI_Controller
{
	public function index()
	{
		$this->load->model('common_model');
		$currentDate = date('Y-m-d', strtotime('+2 days'));
		$getFileData = $this->common_model->getAll('usr_downgraded',array('downgrade_date'=>$currentDate, 'status'=>0));
		//echo '<pre>';print_r($getFileData);die;
		mail('testdevloper007@gmail.com','user schedule downgraded',$this->db->last_query());
		//die;
		if(!empty($getFileData))
		{
			foreach ($getFileData as $value) 
			{
				$userdetails = $this->common_model->getAllRow('usr_users',array('id'=>$value['users_id']));
				$orderdetails = $this->common_model->getAllRow('shop_orders',array('invoiceAddress'=>$userdetails['email'],'payed'=>1,'is_booking'=>0),'','created DESC');

				$base_full_details = $this->common_model->getAllRow('shop_articles',array('id'=>5));
				$base_half_details = $this->common_model->getAllRow('shop_new_articles',array('alias'=>'basic'));
				//pr($orderdetails);
				if($orderdetails)
				{
					if($value['membership_type'] == 1)
					{
						$total_base = $base_half_details['base_price'];
						$time = 182;
					}
					else
					{
						$total_base = $base_full_details['base_price'];
						$time = 365;
					}
					$payment_fee = ($total_base*2.5)/100;
					$total_sum = 0;
					$vat = 0;
					if($orderdetails['vat'] > 0)
					{
						$vat = (($total_base + $payment_fee)*7.7)/100;
					}
					$total_sum = ($total_base + $payment_fee + $vat);
					$this->common_model->UpdateData('shop_orders',array('total_base'=>$total_base,'payment_fee'=>$payment_fee,'vat'=>$vat,'total_sum'=>$total_sum,'time'=>$time),array('order_id'=>$orderdetails['order_id']));
					$this->common_model->UpdateData('shop_articles_carts',array('article_id'=>5),array('cart_id'=>$orderdetails['cart_id']));
					$this->common_model->UpdateData('usr_downgraded',array('status'=>1),array('id'=>$value['id']));
				}
			}
		}
	}
}