/**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index()
 {
     $enterprises = Enterprise::orderBy('razon_social')->paginate(10);
     return view('enterprise.index', compact('enterprises'));
 }
 public function ventas_a_excel(Request $request)
 {
     $empresas = Enterprise::orderBy('razon_social')->get();
     $ordenes = new SaleOrder();
     $order = $request->input('order') == 'asc' ? 'ASC' : 'DESC';
     $filtros = array();
     if ($request->input('sort') == 'razon_social') {
         $ordenes = $ordenes->orderBy('razon_social', $order);
     } elseif ($request->input('sort') == 'created_at') {
         $ordenes = $ordenes->orderBy('created_at', $order);
     } elseif ($request->input('sort') == 'nro_orden') {
         $ordenes = $ordenes->orderBy('nro_orden', $order);
     } elseif ($request->input('sort') == 'total') {
         $ordenes = $ordenes->orderBy('total', $order);
     }
     $empresa = null;
     if ($request->input('empresa_id')) {
         $ordenes = $ordenes->where('enterprise_id', $request->input('empresa_id'));
         $empresa = Enterprise::find($request->input('empresa_id'));
         $filtros['empresa_id'] = $request->input('empresa_id');
     }
     if ($request->input('fecha_inic') != '' && $request->input('fecha_fin') != '') {
         $inic_arr = explode('/', $request->input('fecha_inic'));
         $inic = $inic_arr[2] . "-" . $inic_arr[1] . "-" . $inic_arr[0] . " 00:00:00";
         $fin_arr = explode('/', $request->input('fecha_fin'));
         $fin = $fin_arr[2] . "-" . $fin_arr[1] . "-" . $fin_arr[0] . " 11:59:59";
         $ordenes = $ordenes->whereBetween('created_at', [$inic, $fin]);
         $filtros['fecha_inic'] = $request->input('fecha_inic');
         $filtros['fecha_fin'] = $request->input('fecha_fin');
     } elseif ($request->input('fecha_inic') != '' && $request->input('fecha_fin') == '') {
         $inic_arr = explode('/', $request->input('fecha_inic'));
         $inic = $inic_arr[2] . "-" . $inic_arr[1] . "-" . $inic_arr[0] . " 00:00:00";
         $ordenes = $ordenes->where('created_at', '>', $inic);
         $filtros['fecha_fin'] = $request->input('fecha_fin');
     } elseif ($request->input('fecha_inic') == '' && $request->input('fecha_fin') != '') {
         $fin_arr = explode('/', $request->input('fecha_fin'));
         $fin = $fin_arr[2] . "-" . $fin_arr[1] . "-" . $fin_arr[0] . " 11:59:59";
         $ordenes = $ordenes->where('created_at', '<', $fin);
         $filtros['fecha_inic'] = $request->input('fecha_inic');
     }
     $ordenes = $ordenes->get();
     \Excel::create('report', function ($excel) use($ordenes, $empresa, $filtros) {
         $excel->sheet('hoja_' . date('d-m-Y'), function ($sheet) use($ordenes, $empresa, $filtros) {
             $inic_table_row = 3;
             $sheet->mergeCells('A1:B1');
             $sheet->row(1, array('Reporte de Ventas'));
             if ($empresa != null) {
                 $inic_table_row++;
                 $sheet->row(2, array('Empresa', $empresa->razon_social));
             }
             $fechas = array();
             if (isset($filtros['fecha_inic'])) {
                 $inic_table_row++;
                 $inic_table_row++;
                 $fechas[] = 'Fecha Inicial';
                 $fechas[] = $filtros['fecha_inic'];
             }
             if (isset($filtros['fecha_inic'])) {
                 $fechas[] = 'Fecha Final';
                 $fechas[] = $filtros['fecha_fin'];
             }
             if (!empty($fechas)) {
                 $sheet->row(3, $fechas);
             }
             $sheet->row($inic_table_row, array('Cliente', 'Empresa', 'Fecha', 'Nro. de orden', 'Monto', 'IVA', 'Total'));
             $sheet->row($inic_table_row, function ($row) {
                 $row->setBackground('#337ab7');
             });
             $sheet->setHeight($inic_table_row, 20);
             $sheet->cells('A' . $inic_table_row . ':G' . $inic_table_row, function ($cells) {
                 $cells->setFontSize(12);
                 $cells->setValignment('top');
             });
             $sheet->setBorder('A' . $inic_table_row . ':G' . $inic_table_row, 'thin');
             $acum = 0;
             foreach ($ordenes as $key => $orden) {
                 $acum += $orden->total;
                 $sheet->row($key + 1 + $inic_table_row, array($orden->razon_social, $orden->enterprise->razon_social, date("d/m/Y h:i:s A", strtotime($orden->created_at)), $orden->nro_orden, number_format($orden->monto, 2, ',', '.'), number_format($orden->iva, 2, ',', '.'), number_format($orden->total, 2, ',', '.')));
                 $sheet->cells('E' . ($key + 1 + $inic_table_row) . ':G' . ($key + 1 + $inic_table_row), function ($cells) {
                     $cells->setAlignment('right');
                 });
             }
             $sheet->row($key + 4 + $inic_table_row, array('Ventas Totales', number_format($acum, 2, ',', '.')));
             $sheet->cells('B' . ($key + 4 + $inic_table_row), function ($cells) {
                 $cells->setAlignment('right');
             });
         });
     })->download('xls');
 }