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

class Main extends Public_Controller
{

	function __construct()
	{
		parent::__construct();
		$this->load->helper('form');
	}

	public function index()
	{
		die(var_dump($this->session->userdata('type')));
		if ($this->session->userdata('userLevel') == '1') {
			redirect('admin/dashboard', 'refresh');
			redirect('login', 'refresh');
		} else {
			redirect('login', 'refresh');
		}
		$this->template
			->set('template', 'publicMain')
			->set('view', 'pages/index')
			->render();
	}


	public function doCronJob()
	{
		$dataCronJob = $this->db->query("SELECT * FROM `cron_job` WHERE `counter` < `expire`")->result();
		// debug($dataCronJob);
		if (!empty($dataCronJob)) {
			$days = "";
			// echo date("Y/m/d");
			foreach ($dataCronJob as $dataCJ) {
				$days = explode(',', $dataCJ->days);
				// debug($days);
				if (in_array(date("d"), $days)) {
					$SuppleirData = $this->db->query("SELECT * FROM `suppliers_invoices` WHERE `invoice_id` = '$dataCJ->invoice_id'")->row();
					$SuppleirDataItems = $this->db->query("SELECT * FROM `customer_invoice_items` WHERE `invoice_id` = '$dataCJ->invoice_id'")->result();
					$supplierInvoiceData = array(
						'supplier_id'       => $SuppleirData->supplier_id,
						'invoice_number'    => $SuppleirData->invoice_number,
						'invoice_date'      => date('Y-m-d 01:00:00'),
						'invoice_for'       => $SuppleirData->invoice_for,
						'invoice_total'     => $SuppleirData->invoice_total,
						'dateCreation'      => date('Y-m-d H:i:s'),
						'lastUpdate'        => date('Y-m-d H:i:s')
					);
					$supplier_invoice_id = $this->general_model->insertRow('suppliers_invoices', $supplierInvoiceData);
					foreach ($SuppleirDataItems as $item) {
						$suppliers_invoices_items = array(
							'invoice_id'				=> $supplier_invoice_id,
							'product_id'				=> $item->product_id,
							'product_code'				=> $item->product_code,
							'customerId'				=> $item->customerId,
							'item_title'				=> $item->item_title,
							'item_price'				=> $item->item_price,
							'unit'						=> $item->unit,
							'full_price'				=> $item->full_price,
							'store_id'					=> $item->store_id,
							'invoice_date'				=> date('Y-m-d 01:00:00'),
							'dateCreation'				=> date('Y-m-d H:i:s'),
							'lastUpdate'				=> date('Y-m-d H:i:s')
						);
						$isertItem = $this->general_model->insertRow('customer_invoice_items', $suppliers_invoices_items);
					}
					if (!empty($dataCJ->created_Invoice)) {
						$old_invoices = explode(",", $dataCJ->created_Invoice);
						$old_invoices[] = $supplier_invoice_id;

						$updateCronJob = array(
							'invoice_type'			=> $dataCJ->invoice_type,
							'invoice_id'			=> $dataCJ->invoice_id,
							'days'					=> $dataCJ->days,
							'expire'				=> $dataCJ->expire,
							'counter'				=> $dataCJ->counter + 1,
							'created_Invoice'		=> implode(",", $old_invoices),
							'lastUpdate'			=> date('Y-m-d H:i:s'),
							'dateCreation'			=> date('Y-m-d H:i:s')
						);
					}
					else{
						$updateCronJob = array(
							'invoice_type'			=> $dataCJ->invoice_type,
							'invoice_id'			=> $dataCJ->invoice_id,
							'days'					=> $dataCJ->days,
							'expire'				=> $dataCJ->expire,
							'counter'				=> $dataCJ->counter + 1,
							'created_Invoice'		=> $supplier_invoice_id,
							'lastUpdate'			=> date('Y-m-d H:i:s'),
							'dateCreation'			=> date('Y-m-d H:i:s')
						);
					}
					// debug($updateCronJob);
					$cronJob = $this->general_model->updateRowById('cron_job', 'invoice_id', $dataCJ->invoice_id, $updateCronJob);
				}
			}
		}
	}
	public function doCronJobCustomers()
	{
		$dataCronJob = $this->db->query("SELECT * FROM `cron_job` WHERE `counter` < `expire`")->result();
		// debug($dataCronJob);
		if (!empty($dataCronJob)) {
			$days = "";
			// echo date("Y/m/d");
			foreach ($dataCronJob as $dataCJ) {
				$days = explode(',', $dataCJ->days);
				// debug($days);
				if (in_array(date("d"), $days)) {
					$customersData = $this->db->query("SELECT * FROM `customers_invoices` WHERE `invoice_id` = '$dataCJ->invoice_id'")->row();
					$customerDataItems = $this->db->query("SELECT * FROM `customer_invoice_items` WHERE `invoice_id` = '$dataCJ->invoice_id'")->result();
					$customersInvoiceData = array(
						'supplier_id'       => $customersData->customerId,
						// 'invoice_number'    => $customersData->invoice_number,
						'invoice_date'      => date('Y-m-d 01:00:00'),
						// 'invoice_for'       => $customersData->invoice_for,
						'invoice_total'     => $customersData->total,
						'dateCreation'      => date('Y-m-d H:i:s'),
						'lastUpdate'        => date('Y-m-d H:i:s')
					);
					$customers_invoice_id = $this->general_model->insertRow('customers_invoices', $customersInvoiceData);
					foreach ($customerDataItems as $item) {
						$customers_invoices_items = array(
							'invoice_id'				=> $customers_invoice_id,
							'product_id'				=> $item->product_id,
							'product_code'				=> $item->product_code,
							'customerId'				=> $item->customerId,
							'item_title'				=> $item->item_title,
							'item_price'				=> $item->item_price,
							'unit'						=> $item->unit,
							'full_price'				=> $item->full_price,
							'store_id'					=> $item->store_id,
							'invoice_date'				=> date('Y-m-d 01:00:00'),
							'dateCreation'				=> date('Y-m-d H:i:s'),
							'lastUpdate'				=> date('Y-m-d H:i:s')
						);
						$isertItem = $this->general_model->insertRow('customer_invoice_items', $customers_invoices_items);
					}
					if (!empty($dataCJ->created_Invoice)) {
						$old_invoices = explode(",", $dataCJ->created_Invoice);
						$old_invoices[] = $customers_invoice_id;

						$updateCronJob = array(
							'invoice_type'			=> $dataCJ->invoice_type,
							'invoice_id'			=> $dataCJ->invoice_id,
							'days'					=> $dataCJ->days,
							'expire'				=> $dataCJ->expire,
							'counter'				=> $dataCJ->counter + 1,
							'created_Invoice'		=> implode(",", $old_invoices),
							'lastUpdate'			=> date('Y-m-d H:i:s'),
							'dateCreation'			=> date('Y-m-d H:i:s')
						);
					}
					else{
						$updateCronJob = array(
							'invoice_type'			=> $dataCJ->invoice_type,
							'invoice_id'			=> $dataCJ->invoice_id,
							'days'					=> $dataCJ->days,
							'expire'				=> $dataCJ->expire,
							'counter'				=> $dataCJ->counter + 1,
							'created_Invoice'		=> $customers_invoice_id,
							'lastUpdate'			=> date('Y-m-d H:i:s'),
							'dateCreation'			=> date('Y-m-d H:i:s')
						);
					}
					// debug($updateCronJob);
					$cronJob = $this->general_model->updateRowById('cron_job', 'invoice_id', $dataCJ->invoice_id, $updateCronJob);
				}
			}
		}
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
