<?php 

	include ('includes/db1config.php');
	include ('../includes/mysql_connect.php');
	
	$checkmaster = "SELECT master_record from customer_invoices where customer_id =".$_POST['customer_id']." and invoice_number = '".$_POST['invoice_number']."' ";  
	$rt          = mysql_query($checkmaster); // query executed 
    echo mysql_error();      // if any error is there that will be printed to the screen
    $nt          = mysql_fetch_array($rt);

    if( !isset($nt['master_record']) && $nt['master_record'] != 1 ){

		$query = "INSERT INTO customer_invoices (invoice_date,due_date,invoice_total,master_record,customer_id,paid,invoice_number,recurring) VALUES ('".mysql_escape_string($_POST['invoice_date'])."','".mysql_escape_string($_POST['due_date'])."','".mysql_escape_string($_POST['invoice_total'])."','1','".$_POST['customer_id']."','".mysql_escape_string($_POST['paid'])."','".mysql_escape_string($_POST['invoice_number'])."','".$_POST['recurring']."')";
		mysql_query($query) or die(mysql_error());
    }


	if( isset($_POST) ){

		for ($i=0; $i < count($_POST['itemdesc']) ; $i++) { 
			
	    	$query = "INSERT INTO customer_invoices (customer_id,invoice_number,item_description,quantity,item_charge,extended_charge,master_record,paid,recurring) VALUES ('".mysql_escape_string($_POST['customer_id'])."','".mysql_escape_string($_POST['invoice_number'])."','".mysql_escape_string($_POST['itemdesc'][$i])."','".mysql_escape_string($_POST['quantity'][$i])."','".mysql_escape_string($_POST['itemcharge'][$i])."','".mysql_escape_string($_POST['extendedcharge'][$i])."','0','".mysql_escape_string($_POST['paid'])."','".$_POST['recurring']."')";
	    	mysql_query($query) or die(mysql_error());
		}

	}

	echo 'Successfully saved your invoice';
		
?>