<?php

	require_once 'ReportSales.inc';		
		
	/*****************************************************/
	/***************** EXPORT TO EXCEL *******************/
	/*****************************************************/
		/**
		 * PHPExcel
		 *
		 * Copyright (C) 2006 - 2014 PHPExcel
		 *
		 * This library is free software; you can redistribute it and/or
		 * modify it under the terms of the GNU Lesser General Public
		 * License as published by the Free Software Foundation; either
		 * version 2.1 of the License, or (at your option) any later version.
		 *
		 * This library is distributed in the hope that it will be useful,
		 * but WITHOUT ANY WARRANTY; without even the implied warranty of
		 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
		 * Lesser General Public License for more details.
		 *
		 * You should have received a copy of the GNU Lesser General Public
		 * License along with this library; if not, write to the Free Software
		 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
		 *
		 * @category   PHPExcel
		 * @package    PHPExcel
		 * @copyright  Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
		 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt	LGPL
		 * @version    1.8.0, 2014-03-02
		 */

		/** PHPExcel_IOFactory */
		require_once dirname(__FILE__) . '/PhpExcell/Classes/PHPExcel/IOFactory.php';
		
		// Create new PHPExcel object
		echo date('H:i:s') , " Create new PHPExcel object" , EOL;
		$objPHPExcel = new PHPExcel();
		
		// Set document properties
		echo date('H:i:s') , " Set document properties" , EOL;
		$objPHPExcel->getProperties()->setCreator("E5")
		->setLastModifiedBy("E5")
		->setTitle("E5 - Report Sales")
		->setSubject("E5 - Report Document")
		->setDescription("Report Sales - description")
		->setKeywords("e5, report, report sales")
		->setCategory("Reports");
		
		/*****************************************************/
		/******************* EXCELL Styles *******************/
		/*****************************************************/
		// Set default widths and heights
		$objPHPExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(15);
		$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20);
		
		$headerStyle = array(
				'alignment' => array(
						'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
						'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
				),
				'font' => array(
						'bold' => true,
						'color' => array('rgb' => 'FFFFFF'),
						'name' => 'Verdana',
				),
				'fill' => array(
						'type' => PHPExcel_Style_Fill::FILL_SOLID,
						'startcolor' => array(
								'rgb' => '272822',
						),
				),
		);
		
		$categoryStyle = array(
				'alignment' => array(
						'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
						'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
				),
				'font' => array(
						'bold' => true,
						'color' => array('rgb' => 'FFFFFF'),
						'name' => 'Verdana',
				),
				'fill' => array(
						'type' => PHPExcel_Style_Fill::FILL_SOLID,
						'startcolor' => array(
								'rgb' => '4E5044',
						),
				),
		);
		
		$subCategoryStyle = array(
				'alignment' => array(
						'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
						'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
				),
				'font' => array(
						'bold' => true,
						'color' => array('rgb' => 'FFFFFF'),
						'name' => 'Verdana',
				),
				'fill' => array(
						'type' => PHPExcel_Style_Fill::FILL_SOLID,
						'startcolor' => array(
								'rgb' => '8F937D',
						),
				),
		);
		
		$columnAlignRight = array(
				'alignment' => array(
						'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
						'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
				),
		);

		$columnAlignLeft = array(
				'alignment' => array(
						'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
						'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
				),
		);
		
		$objPHPExcel->setActiveSheetIndex(0)->getRowDimension('1')->setRowHeight(25);
		$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(70);
		$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(18);
		$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
		
		$objPHPExcel->setActiveSheetIndex(0)->getStyle("B2:F4000")->applyFromArray($columnAlignRight);
		$objPHPExcel->setActiveSheetIndex(0)->getStyle("C2:F4000")->applyFromArray($columnAlignRight);
		$objPHPExcel->setActiveSheetIndex(0)->getStyle("E2:F4000")->applyFromArray($columnAlignRight);
		$objPHPExcel->setActiveSheetIndex(0)->getStyle("F2:F4000")->applyFromArray($columnAlignRight);
		
		$objPHPExcel->setActiveSheetIndex(0)->getStyle("G2:G4000")->applyFromArray($columnAlignRight);
		$objPHPExcel->setActiveSheetIndex(0)->getStyle("H2:H4000")->applyFromArray($columnAlignRight);
		$objPHPExcel->setActiveSheetIndex(0)->getStyle("A1:F1")->applyFromArray($headerStyle);

		/*****************************************************/
		/********************* EXCELL Data *******************/
		/*****************************************************/
		// Add header
		$objPHPExcel->setActiveSheetIndex(0)
		->setCellValue('A1', $mod_strings['LBL_PRODUCTS'] )
		->setCellValue('B1', $mod_strings['LBL_SOLD_AMOUNT'])
		->setCellValue('C1', $mod_strings['LBL_VALUE_SALES'])
		->setCellValue('D1', $mod_strings['LBL_AVERAGE_PRICE'])
		->setCellValue('E1', $mod_strings['LBL_COST'])
		->setCellValue('F1', $mod_strings['LBL_MARGIN']);	
		
		
		/*****************************************************/
		/********************* EXCELL Data *******************/
		/*****************************************************/
		// Add all data
		$lastRowNumber = 2;
		$rowNumber = 2;
		foreach ( $newArray1 as $categoryName => $category )
		{
			$objPHPExcel->setActiveSheetIndex(0)
			->setCellValue('A'.($rowNumber), $categoryName)
			->setCellValue('B'.($rowNumber), $category["IloscSum"])
			->setCellValue('C'.($rowNumber), number_format($category["SoldSum"], 2, '.', ''))
			->setCellValue('D'.($rowNumber), number_format($category["SredniaSum"], 2, '.', ''))
			->setCellValue('E'.($rowNumber), number_format($category["KosztSum"], 2, '.', ''))
			->setCellValue('F'.($rowNumber), number_format($category["MarzaSum"], 2, '.', ''))
			->getStyle("A".$rowNumber.":F".$rowNumber)->applyFromArray($categoryStyle);

			// Formatowanie walutowe
	        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C'.($rowNumber))->getNumberFormat()->setFormatCode("#,#0.#0 zł");
	        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D'.($rowNumber))->getNumberFormat()->setFormatCode("#,#0.#0 zł");
	        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E'.($rowNumber))->getNumberFormat()->setFormatCode("#,#0.#0 zł");
	        $objPHPExcel->setActiveSheetIndex(0)->getStyle('F'.($rowNumber))->getNumberFormat()->setFormatCode("#,#0.#0 zł");

			// Nazwa kategorii wyrównane do lewej
			$objPHPExcel->setActiveSheetIndex(0)->getStyle("A".$rowNumber)->applyFromArray($columnAlignLeft);
			
			// Wyświetla podkategorie
			foreach ( $category as $subCategoryName => $subCategory )
			{
				if ( $subCategoryName != "IloscSum" && $subCategoryName != "SoldSum" && $subCategoryName != "SredniaSum" && 
					 $subCategoryName != "KosztSum" && $subCategoryName != "MarzaSum" && $subCategoryName != "StanSum" && 
					 $subCategoryName != "WartoscSum" && $subCategoryName != "SoldSumNormal" && $subCategoryName != "IloscNormal" && $subCategoryName != "WartoscSum") 
				{
					$rowNumber++;
					$lastRowNumber++;
					

					$objPHPExcel->setActiveSheetIndex(0)
					->setCellValue('A'.($rowNumber), "    ".$subCategoryName)
					->setCellValue('B'.($rowNumber), $subCategory["IloscSum2"])
					->setCellValue('C'.($rowNumber), number_format($subCategory["SoldSum2"], 2, '.', ''))
					->setCellValue('D'.($rowNumber), number_format($subCategory["SredniaSum2"], 2, '.', ''))
					->setCellValue('E'.($rowNumber), number_format($subCategory["KosztSum2"], 2, '.', ''))
					->setCellValue('F'.($rowNumber), number_format($subCategory["MarzaSum2"], 2, '.', ''))
					->getStyle("A".$rowNumber.":F".$rowNumber)->applyFromArray($subCategoryStyle);

					// Formatowanie walutowe
			        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C'.($rowNumber))->getNumberFormat()->setFormatCode("#,#0.#0 zł");
			        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D'.($rowNumber))->getNumberFormat()->setFormatCode("#,#0.#0 zł");
			        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E'.($rowNumber))->getNumberFormat()->setFormatCode("#,#0.#0 zł");
			        $objPHPExcel->setActiveSheetIndex(0)->getStyle('F'.($rowNumber))->getNumberFormat()->setFormatCode("#,#0.#0 zł");
					
					$objPHPExcel->setActiveSheetIndex(0)->getRowDimension($rowNumber)->setOutlineLevel(1);
					$objPHPExcel->setActiveSheetIndex(0)->getRowDimension($rowNumber)->setVisible(true);

					
					// Nazwa kategorii wyrównane do lewej
					$objPHPExcel->setActiveSheetIndex(0)->getStyle("A".$rowNumber)->applyFromArray($columnAlignLeft);
					
					// Wyświetla produkty
					foreach ( $subCategory as $productId => $product )
					{				
						if ( $product["name"] != "IloscSum" && $product["name"] != "SoldSum" && $product["name"] != "SredniaSum" 
							 && $product["name"] != "KosztSum" && $product["name"] != "MarzaSum"  && $product["name"] != "" )
						{
							$rowNumber++;
							$lastRowNumber++;
								
							$objPHPExcel->setActiveSheetIndex(0)
							->setCellValue('A'.($rowNumber), "         ".$product["name"])
							->setCellValue('B'.($rowNumber), $product["ilosc"])
							->setCellValue('C'.($rowNumber), number_format($product["netto"], 2, '.', ''))
							->setCellValue('D'.($rowNumber), number_format($product["srednia"], 2, '.', ''))
							->setCellValue('E'.($rowNumber), number_format($product["koszt"], 2, '.', ''))
							->setCellValue('F'.($rowNumber), number_format($product["marza"], 2, '.', ''));
							// Formatowanie walutowe
					        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C'.($rowNumber))->getNumberFormat()->setFormatCode("#,#0.#0 zł");
					        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D'.($rowNumber))->getNumberFormat()->setFormatCode("#,#0.#0 zł");
					        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E'.($rowNumber))->getNumberFormat()->setFormatCode("#,#0.#0 zł");
					        $objPHPExcel->setActiveSheetIndex(0)->getStyle('F'.($rowNumber))->getNumberFormat()->setFormatCode("#,#0.#0 zł");
							// Zawijanie wierszy
							$objPHPExcel->setActiveSheetIndex(0)->getRowDimension($rowNumber)->setOutlineLevel(2);	
							$objPHPExcel->setActiveSheetIndex(0)->getRowDimension($rowNumber)->setVisible(false);
						}							
						// Nazwa produktu wyrównane do lewej
						$objPHPExcel->setActiveSheetIndex(0)->getStyle("A".$rowNumber)->applyFromArray($columnAlignLeft);
						
					} // end subcategory
					$objPHPExcel->setActiveSheetIndex(0)->getRowDimension($lastRowNumber+1)->setCollapsed(true);

				} 
			} // end category
			$rowNumber++;
			$lastRowNumber++;
		}

		// Sumy --------------------------------------------------------------------------------------------------
		$objPHPExcel->setActiveSheetIndex(0)
		->setCellValue('A'.($lastRowNumber), "Podsumowanie: ")
		->setCellValue('B'.($lastRowNumber), $sum['IloscSumSum'])
		->setCellValue('C'.($lastRowNumber), number_format($sum['SoldSumSum'], 2, '.', ''))
		->setCellValue('D'.($lastRowNumber), number_format($sum['SredniaSumSum'], 2, '.', ''))
		->setCellValue('E'.($lastRowNumber), number_format($sum['KosztSumSum'], 2, '.', ''))
		->setCellValue('F'.($lastRowNumber), number_format($sum['MarzaSumSum'], 2, '.', ''));
		
		// Formatowanie walutowe ostatniego wiersza
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C'.($lastRowNumber))->getNumberFormat()->setFormatCode("#,#0.#0 zł");
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D'.($lastRowNumber))->getNumberFormat()->setFormatCode("#,#0.#0 zł");
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E'.($lastRowNumber))->getNumberFormat()->setFormatCode("#,#0.#0 zł");
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('F'.($lastRowNumber))->getNumberFormat()->setFormatCode("#,#0.#0 zł");
		// Formatowanie wiersza sum -----------------------------------------------------------------------------
		$objPHPExcel->setActiveSheetIndex(0)->getStyle("A".$lastRowNumber.":F".$lastRowNumber)->applyFromArray($headerStyle);
		// Wyrównanie pól do prawej
		$objPHPExcel->setActiveSheetIndex(0)->getStyle("A".$lastRowNumber)->applyFromArray($columnAlignRight);
		$objPHPExcel->setActiveSheetIndex(0)->getStyle("B".$lastRowNumber)->applyFromArray($columnAlignRight);
		$objPHPExcel->setActiveSheetIndex(0)->getStyle("C".$lastRowNumber)->applyFromArray($columnAlignRight);
		$objPHPExcel->setActiveSheetIndex(0)->getStyle("D".$lastRowNumber)->applyFromArray($columnAlignRight);
		$objPHPExcel->setActiveSheetIndex(0)->getStyle("E".$lastRowNumber)->applyFromArray($columnAlignRight);
		$objPHPExcel->setActiveSheetIndex(0)->getStyle("F".$lastRowNumber)->applyFromArray($columnAlignRight);
		
		// Rename worksheet
		$objPHPExcel->getActiveSheet()->setTitle('Raport sprzedaży');
		
		// Set active sheet index to the first sheet, so Excel opens this as the first sheet
		$objPHPExcel->setActiveSheetIndex(0);
		
		// Save Excel 2007 file
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
		$objWriter->save( __DIR__ . "/ExcelFiles/ReportSales.xls");
		
?>
