<?php
/*
Copyright (c) 2010, Till Brehm, projektfarm Gmbh
All rights reserved.
*/

require_once('../../lib/config.inc.php');
require_once('../../lib/app.inc.php');

//* Check permissions for module
$app->auth->check_module_permissions('billing');

//* Not allowed in demo mode
if($conf['demo_mode'] == true) $app->error("Import disabled in demo mode.");

//* This is only allowed for administrators
if(!$app->auth->is_admin()) die('only allowed for administrators.');

$app->uses('tpl');

$app->tpl->newTemplate('form.tpl.htm');
$app->tpl->setInclude('content_tpl', 'templates/invoice_recurring_item_import.htm');
$msg = '';
$error = '';

// Import the items
if(isset($_FILES['file']['name']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
	$lines = file($_FILES['file']['tmp_name']);
	if(is_array($lines)) {
		// remove the first line
		unset($lines[0]);
		
		foreach($lines as $line) {
			if(trim($line) != '') {
				$parts = explode(';',$line);
				$domain = trim($parts[0]);
				//$reseller = trim($parts[1]);
			
				$start_date = trim($parts[1]);
				$tmp = explode('.',$start_date);
				$start_date_mysql = $tmp[2].'-'.$tmp[1].'-'.$tmp[0];
			
				$next_payment_date = trim($parts[2]);
				$tmp = explode('.',$next_payment_date);
				$tmp[2] = '2010';
				$next_payment_date_mysql = $tmp[2].'-'.$tmp[1].'-'.$tmp[0];
			
				$tmp = $app->db->queryOneRecord("SELECT * FROM invoice_recurring_item WHERE name = '$domain'");
			
				if(!is_array($tmp)) {
					$sql = "INSERT INTO invoice_recurring_item (`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `invoice_item_template_id`, `invoice_company_id`, `client_id`, `name`, `description`, `price`, `setup_fee`, `vat`, `recur_months`, `next_payment_date`, `start_date`, `type`) VALUES (1, 1, 'riud', 'riud', '', 0, 1, 0, '$domain', '', 0, 0, 0, 12, '$next_payment_date_mysql', '$start_date_mysql','domain')";
					//echo $sql;
					$app->db->query($sql);
					$msg .= "Inserted $domain<br />";
				}
			}
		}
	}
}

$app->tpl->setVar('msg',$msg);
$app->tpl->setVar('error',$error);

//* load language file 
$lng_file = 'lib/lang/'.$_SESSION['s']['language'].'_invoice_recurring_item_import.lng';
include($lng_file);
$app->tpl->setVar($wb);

$app->tpl_defaults();
$app->tpl->pparse();


?>