/**
  * Store a newly created despesa in storage.
  *
  * @return Response
  */
 public function store()
 {
     $validator = Validator::make($data = Input::all(), Despesa::$rules);
     if ($validator->fails()) {
         return Redirect::back()->withErrors($validator)->withInput();
     } else {
         $data['valor'] = number_format($data['valor'], 2, '.', '');
         $data['date'] = new Carbon($data['date'], 'America/Sao_Paulo');
         $data['date'] = $data['date']->format('Y-m-d');
         //return $data['date'];
         $despesa = Despesa::create($data);
         $relatorio = Relatorio::find(@$data['relatorio_id']);
         if ($relatorio) {
             $ids = explode(',', $relatorio->ids);
             $ids[] = $despesa->id;
             $relatorio->ids = implode(',', $ids);
             $relatorio->save();
         }
         // Alert
         $alert[] = ['class' => 'alert-success', 'message' => '<strong><i class="fa fa-check"></i></strong> Despesa registrada com sucesso'];
         Session::flash('alerts', $alert);
         return Redirect::to(URL::previous());
     }
 }
 /**
  * PREVIEW PRINT   
  *
  * @param  string  $id
  * @return Response
  */
 public function printThis($id)
 {
     $relatorio = Relatorio::find($id);
     switch (@$relatorio->type) {
         case 'despesas':
             $despesas_ids = explode(',', $relatorio->ids);
             $despesas = Despesa::whereIn('id', $despesas_ids)->get();
             $relatorio->despesas = $despesas;
             // Total de despesas
             $total = 0;
             foreach ($relatorio->despesas as $despesa) {
                 $total += $despesa->valor;
                 // Fomata R$ despesas
                 $despesa->valor = number_format($despesa->valor, 2, ',', '.');
             }
             // Fomata R$ Total
             $relatorio->total = number_format($total, 2, ',', '.');
             break;
         case 'conversas':
             //Agrupa por cliente
             $groups = $relatorio->conversas->groupBy('cliente_id');
             $clientes_conversas = array();
             foreach ($groups as $cliente_id => $conversas) {
                 $client = Cliente::find($cliente_id);
                 if (count($client)) {
                     $client->conversas = $conversas;
                     $clientes_conversas[] = $client;
                 }
             }
             $relatorio->conversas = $clientes_conversas;
             break;
         default:
             return "???";
             break;
     }
     return View::make('relatorios.' . $relatorio->type . '.print', compact('relatorio'));
     return View::make('relatorios.' . $relatorio->type . '.print', compact('relatorio'));
 }
 /**
  * Store a newly created email in storage.
  * SEND THE MAIL
  *
  * @return Response
  */
 public function store()
 {
     $validator = Validator::make($data = Input::all(), Email::$rules);
     //return Response::json($data);
     if ($validator->fails()) {
         if (Request::ajax()) {
             return Response::json('error', '503')->withErrors($validator);
         } else {
             return Redirect::back()->withErrors($validator)->withInput();
         }
     }
     // echo "<pre>";
     // print_r( $data );
     // echo "</pre>";
     // exit;
     switch ($data['owner_type']) {
         case 'pedido':
             $resource = Pedido::find($data['owner_id']);
             $view = 'layouts.email';
             //Corpo do email
             if ($resource) {
                 $resource->status = '2';
                 $resource->save();
             }
             break;
         case 'relatorio':
             $resource = Relatorio::find($data['owner_id']);
             $view = 'relatorios.email';
             unset($data['attachments']);
             if ($resource and Confide::user()) {
                 $resource->status = '2';
                 $resource->save();
             }
             break;
         case 'cliente':
             $resource = Cliente::find($data['owner_id']);
             $data['resource'] = $resource;
             // print_r($resource)  ;
             // exit;
             $view = 'clientes.email';
             break;
         default:
             $view = 'layouts.email';
             $resource = NULL;
             break;
     }
     /**
      *
      *	SEND THE MAILS
      *
      **/
     // SEND to EACH "TO"
     foreach ($data['to'] as $to) {
         $content = $data;
         $content['to'] = $to;
         // print_r($view);
         // exit;
         // DEBUG
         // return View::make( $view, array('email'=>$content, 'resource'=>$resource) );
         // exit;
         Mail::queue($view, array('email' => $content, 'resource' => $resource), function ($message) use($content, $to) {
             $message->to($to)->subject(@$content['subject']);
             /**
              *    ANEXA O PDF
              */
             // print_r($content['attachments']);
             // exit;
             if (isset($content['attachments']) and is_file($content['attachments'])) {
                 $attachment = $content['attachments'];
                 $message->attach($attachment);
             }
         });
         $email = Email::create($content);
         if ($email) {
             //$downloadLink   = url(  'pedidos/'.$email->owner_id.'/download');
             //$email->pdfLink = $downloadLink;
             //Swift_Preferences::getInstance()->setCacheType('disk')->setTempDir('/tmp');
             // //SEND THE MAIL
             // Mail::send('layouts.email', compact('email'), function($message)use($email){
             // 	//$message->from('*****@*****.**', 'L. Tonet');
             // 	$message->to( $email->to );
             // 	if( isset($email->cc) and !empty($email->cc)) {
             // 		 $message->cc( $email->cc );
             // 		 //append to report record
             // 		 $reportMsg = ' com CC: <'.$email->cc.'>';
             // 	}
             // 	$message->subject( @$email->subject );
             // 	/**
             // 	 *    ANEXA O PDF
             // 	 */
             // 	if(isset($email->attachments)){
             // 		 $file = strtolower($email->owner_type).'-'.$email->owner_id.'.pdf';
             // 		 $file = asset('pdf/'.$file);
             // 		 $attachment = Swift_Attachment::fromPath('pdf/'.$file);
             // 		 // Attach it to the message
             // 		 $message->attach($file);
             // 	}
             // 	//Log this
             // 	// Report::create([
             // 	//       'user_id'        => Auth::id(),
             // 	//       'status'         => 'success',
             // 	//       'event'          => 'sended',
             // 	//       'title'          => 'Pedido '.$email->owner_id.' enviado',
             // 	//       'resource_model' => 'Pedido',
             // 	//       'owner_id'    => $email->owner_id,
             // 	//       'resource_obj'   => json_encode($email),
             // 	//    ]);
             // });
         } else {
             echo 'não criado';
         }
         // Alert
         $alert[] = ['class' => 'alert-success', 'message' => '<strong><i class="fa fa-check"></i></strong> Enviado para <strong>' . $content['to'] . '</strong> !'];
     }
     Session::flash('alerts', $alert);
     return Redirect::to(URL::previous());
 }