<?php
session_start();  
require_once "main.php";
Logger::configure('log4php.xml');
$log = Logger::getLogger('downloadbatch');
$xid = $_GET['id'];
$log->info($_GET['submitType'].' - user = '.$_SESSION['user'].' - '.$_SESSION['comnum'].' = '.$_GET['id']);
if ($_GET['submitType']=='printbatch') {
    $result = bill_get_customers($_SESSION['comnum']);
    $page1 = 'Account_ID,Account_First_Name,Account_Last_Name,Account_Full_Name,Location_ID,'
    .'Location_Name,Location_Address_Line1,Location_Address_Line2,Location_City,Location_State,'
    .'Location_Zip,Location_Country,Location_Latitude,Location_Longitude,Service_Point_ID,'
    .'Service_Point_Type,Service_Point_Class_Code,Service_Point_Latitude,Service_Point_Longitude,'
    .'Meter_ID,Meter_SN,Meter_Manufacturer,Meter_Model,Meter_Size,Register_Number,'
    .'Register_Unit_Of_Measure,Register_Resolution,Endpoint_SN,Endpoint_Type,Billing_Unit_Of_Measure,Billing_Resolution'.PHP_EOL;
    for($i=0; $i<count($result); $i++){
        $fn1 = explode(",", $result[$i]['name']);
        if( !isset($fn1[1])){
            $fn1[1]="";
        }
        $page1 = $page1 . $result[$i]['account'] . ',' . $fn1['0'] . ',' . $fn1['1']
        . ',"' . $result[$i]['name'] . '",' . $result[$i]['rorder'] . ',,' . $result[$i]['maddr']
        . ',' . $result[$i]['addr1'] . ',' . $result[$i]['city'] . ',' . $result[$i]['state']
        . ',' . $result[$i]['zip'] . ',US,' . $result[$i]['lat'] . ',' . $result[$i]['lon']
        . ',,,,,,' . $result[$i]['meternum'] . ',' . $result[$i]['meternum'] . ',,,,,GAL,1'
        . ',' . $result[$i]['metermiu'] . ',' . $result[$i]['metertype'] . ',GAL,1'.PHP_EOL;
    }
header('Content-Type: application/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename="beacon.csv"');
echo $page1;
}
?>