<?php

ini_set('precision', 14);

/*
  |--------------------------------------------------------------------------
  | Register The Laravel Class Loader
  |--------------------------------------------------------------------------
  |
  | In addition to using Composer, you may use the Laravel class loader to
  | load your controllers and models. This is useful for keeping all of
  | your classes in the "global" namespace without Composer updating.
  |
 */

ClassLoader::addDirectories(array(
		app_path() . '/commands',
		app_path() . '/controllers',
		app_path() . '/models',
		app_path() . '/database/seeds',
		app_path() . '/libraries'
));

/*
  |--------------------------------------------------------------------------
  | Application Error Logger
  |--------------------------------------------------------------------------
  |
  | Here we will configure the error logger setup for the application which
  | is built on top of the wonderful Monolog library. By default we will
  | build a basic log file setup which creates a single file for logs.
  |
 */

Log::useFiles(storage_path() . '/logs/laravel.log');

/*
  |--------------------------------------------------------------------------
  | Application Error Handler
  |--------------------------------------------------------------------------
  |
  | Here you may handle any errors that occur in your application, including
  | logging them or displaying custom views for specific errors. You may
  | even register several error handlers to handle different types of
  | exceptions. If nothing is returned, the default error view is
  | shown, which includes a detailed stack trace during debug.
  |
 */

App::error(function(Exception $exception, $code) {
	Log::error($exception);
});

/*
  |--------------------------------------------------------------------------
  | Require The Filters File
  |--------------------------------------------------------------------------
  |
  | Next we will load the filters file for the application. This gives us
  | a nice separate location to store our route and application filter
  | definitions instead of putting them all in the main routes file.
  |
 */

require __DIR__ . '/../filters.php';

// Require the Observables file.
require __DIR__ . '/../observables.php';

/*
  |--------------------------------------------------------------------------
  | Prep Sentry for dependency Injection
  |--------------------------------------------------------------------------
 */
$app['Cartalyst\Sentry\Sentry'] = $app['sentry'];


Event::listen('cron.collectJobs', function() {
	Cron::add('checkCdek', '*/9 8-18 * * *', function() {
		foreach (Partner::with('user')
						->whereIn('status', array(Partner::STATUS_WAIT_DELIVERING, Partner::STATUS_WAIT_SENDING, Partner::STATUS_DELIVERING))
						->whereNotNull('cdek_dispatchNumber')
						->whereTransportType(Partner::TRANSPORT_TYPE_CDEK)
						->whereClarificationAddress(0)
						->get() as $partner) {
			if ($partner && $partner->statusIsAllow(Partner::ALLOW_CHECK_CDEK)) {
				$res						 = $partner->checkCdek();
				$partner->recall = 0;
				$partner->save();
				if ($res['status'] == 'success' && isset($res['statusChanged']) && $res['statusChanged']) {
					$partner->saveHistory($res['message'], 1, 1);
				}
			}
		}

		return 'Ok';
	});

//	Cron::add('sendStatDaily', '00 22 * * *', function() {
//		$subject					 = 'Legaton - отчет за день';
//		$view							 = 'emails.stat.daily';
//		$data['months']		 = array(
//				'01' => "января", '02' => "февраля", '03' => "марта", '04' => "апреля", '05' => "мая", '06' => "июня", '07' => "июля", '08' => "августа", '09' => "сентября", '10' => "октября", '11' => "ноября", '12' => "декабря",
//				'1'	 => "января", '2'	 => "февраля", '3'	 => "марта", '4'	 => "апреля", '5'	 => "мая", '6'	 => "июня", '7'	 => "июля", '8'	 => "августа", '9'	 => "сентября"
//		);
//		$data['startDate'] = date("Y-m-d");
//
//		$email = 'ifominmail@gmail.com';
//		Mail::queue($view, $data, function($message) use($email, $subject) {
//			$message->to($email)->subject($subject);
//		});
//
//		$email = 'mav309102@gmail.com';
//		Mail::queue($view, $data, function($message) use($email, $subject) {
//			$message->to($email)->subject($subject);
//		});
//
//		$email = 'shop@legaton.com';
//		Mail::queue($view, $data, function($message) use($email, $subject) {
//			$message->to($email)->subject($subject);
//		});
//
//		StatDailyArchive::create(array(
//				'date'		 => date('Y-m-d'),
//				'content'	 => View::make($view)->with('startDate', date('Y-m-d'))->with('months', $data['months'])->render(),
//		));
//
//		return 'Ok';
//	});
//
//    Cron::add('example1', '* * * * *', function() {
//                    // Do some crazy things unsuccessfully every minute
//                    return 'Yes';
//                });
//
//    Cron::add('example2', '*/2 * * * *', function() {
//        // Do some crazy things successfully every two minute
//        return null;
//    });
//
//    Cron::add('disabled job', '0 * * * *', function() {
//        // Do some crazy things successfully every hour
//    }, false);
});
