<?php
session_start();
require_once('../Connections/courier.php');
require_once '../functions/functions.php';

$studentinfo = mysql_query("SELECT * FROM stock_items WHERE quantity_available <= min_re_order_qty AND branch_id = '" . $_SESSION['branch_id'] . "' order by item");

if (mysql_num_rows($studentinfo) > 0) {
    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    date_default_timezone_set('Europe/London');

    if (PHP_SAPI == 'cli')
        die('This example should only be run from a Web Browser');



    require_once(dirname(__FILE__) . "/../assets/PHPExcel/Classes/PHPExcel.php");
    $objPHPExcel = new PHPExcel();


    $i2 = 0;

    $objPHPExcel->getProperties()->setCreator('ROBERT AND SONS LIMITED')
            ->setLastModifiedBy('ROBERT AND SONS LIMITED')
            ->setTitle('LIST OF ALL ITEMS THAT NEEDS REPLENISHMENT');

    $objPHPExcel->createSheet();

    $objPHPExcel->setActiveSheetIndex($i2);

    $styleArray = array(
        'font' => array(
            'bold' => true,
            'size' => 14,
            'color' => array('rgb' => 'FF0000'),
        ),
        'alignment' => array(
            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
        )
    );

    $styleArraydept = array(
        'font' => array(
            'bold' => true,
            'size' => 14,
            'color' => array('rgb' => '#7a0026'),
        ),
        'alignment' => array(
            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
        )
    );

    $styleArrayclass = array(
        'font' => array(
            'bold' => true,
            'size' => 14,
            'color' => array('rgb' => 'green'),
        ),
        'alignment' => array(
            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
        )
    );

    $styleArraysub = array(
        'font' => array(
            'bold' => true,
            'size' => 14,
            'color' => array('rgb' => 'FF0000'),
        ),
        'alignment' => array(
            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
        )
    );

    $styleArray2 = array(
        'font' => array(
            'bold' => true,
            'size' => 12,
            'color' => array('rgb' => '000000'),
        ),
        'alignment' => array(
            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
        )
    );

    $styleArray3 = array(
        'font' => array(
            'bold' => true,
            'size' => 12,
    ));

    $styleArray4 = array(
        'alignment' => array(
            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
        )
    );

    $objPHPExcel->getActiveSheet()->getStyle('D7:G7')->applyFromArray($styleArray2);
    $objPHPExcel->getActiveSheet()->getStyle('H7:I7')->applyFromArray($styleArray2);
    $objPHPExcel->getActiveSheet()->getStyle('J7:T7')->applyFromArray($styleArray2);

    $objPHPExcel->getActiveSheet()->getStyle('A2:K2')->applyFromArray($styleArray);
    $objPHPExcel->setActiveSheetIndex($i2)->mergeCells('A2:K2');
    $objPHPExcel->setActiveSheetIndex($i2)->setCellValue('A2', 'ROBERT AND SONS LIMITED');

    $objPHPExcel->getActiveSheet()->getStyle('A3:K3')->applyFromArray($styleArraydept);
    $objPHPExcel->setActiveSheetIndex($i2)->mergeCells('A3:K3');
    $objPHPExcel->setActiveSheetIndex($i2)->setCellValue('A3', 'STORE DEPARTMENT');

    $objPHPExcel->getActiveSheet()->getStyle('A4:K4')->applyFromArray($styleArraydept);
    $objPHPExcel->setActiveSheetIndex($i2)->mergeCells('A4:K4');
    $objPHPExcel->setActiveSheetIndex($i2)->setCellValue('A4', 'LIST OF ALL ITEMS THAT NEEDS REPLENISHMENT');

    $objPHPExcel->getActiveSheet()->getStyle('A7:C7')->applyFromArray($styleArray3);
    $objPHPExcel->getActiveSheet()->getStyle('G7')->applyFromArray($styleArray3);

    $objPHPExcel->setActiveSheetIndex($i2)
            ->setCellValue('A7', 'NO')
            ->setCellValue('B7', 'ITEM CODE')
            ->mergeCells('C7:E7')
            ->setCellValue('C7', 'ITEM NAME')
            ->setCellValue('F7', 'ITEM CATEGORY')
            ->setCellValue('G7', 'PRICE')
            ->setCellValue('H7', 'MIN RE-ORDER QTY')
            ->setCellValue('I7', 'RE-ORDER QTY')
            ->setCellValue('J7', 'QTY AVAIL.')
            ->setCellValue('K7', 'QTY ORDERED');

    $i = 8;
    while ($row = mysql_fetch_assoc($studentinfo)) {
        $objPHPExcel->setActiveSheetIndex($i2)
                ->setCellValue('A' . $i, $i - 7)
                ->mergeCells('C' . $i . ':' . 'E' . $i)
//              ->protectCells('B' . $i, $row['id'])
                ->setCellValue('B' . $i, $row['id'])
                ->setCellValueExplicit('C' . $i, $row['item'], PHPExcel_Cell_DataType::TYPE_STRING)
                ->setCellValue('F' . $i, $row['service'])
                ->setCellValue('G' . $i, $row['price'])
                ->setCellValue('H' . $i, $row['min_re_order_qty'])
                ->setCellValue('I' . $i, $row['re_order_qty'])
                ->setCellValue('J' . $i, $row['quantity_available']);
        $i++;
    }

    $objPHPExcel->getActiveSheet()->setTitle('RE-ORDER ITEMS');
    $objPHPExcel->setActiveSheetIndex($i2);

    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="RE-ORDER ITEMS".xls"');

    header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
    header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
    header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
    header('Pragma: public'); // HTTP/1.0

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output');
//exit;
} else {
    die('Sorry,No Items Found');
}