<?php
class ControllerToolSinc extends Controller {
	public function index() {
//		 */15 * * * * wget -O /dev/null "http://brech.home/index.php?route=tool/sinc"
		$log = new Log('sync.log');
		$message = " SINC START!";
		$log->write($message);

		$this->load->model('tool/sinc');

		$to_sinc = $this->model_tool_sinc->sincData();
		$data = $this->prepareData($to_sinc);
				
		if (!empty($data)) {
			$this->load->model('extension/module/booking');
			$message = $this->model_extension_module_booking->post($data);

			$this->load->model('extension/module/hotels24');
			$message = $this->model_extension_module_hotels24->post($data);

			$message = 'Передано на букинг: ' . count($data['apartamets']);
			$log->write($message);
		}
		
		
		$message = " SINC END!";
		$log->write($message);
		
	}
	
	public function prepareData($array_products) {
		
		$this->load->model('catalog/product');
		
		$res = array();
		
		foreach ($array_products as $product) {
//			$product_info = $this->model_catalog_product->getProduct($product['product_id']);
//			if (!empty($product_info)) {
				$res['apartamets'][] = [
					'room_id' => [
						'booking' => $product['upc'],
						'hotel24' => $product['ean']
					],
				'product_id'   => $product['product_id'],
				'send1_ok'     => $product['send1_ok'],
				'send2_ok'     => $product['send2_ok'],
				'date'         => $product['booking_date'],
				'room_to_sell' => $product['quantity_new'], 
				'price'        => (int)$product['price_new'],
				'tariff'       => $product['mpn']
				];
//			}
			
		}
//		var_dump($res);
		return $res;

	}	
}