Esempio n. 1
0
function insert_sql_data( $db_conn, $p_campaigns, $p_headers, $p_details )
// insert the header and detail data into the file
// with appropriate safeguards to ensure full 
// completion or rollback of the transaction.
{
GLOBAL $msg_log;

	$success = TRUE;
// Wrap all this in a transaction
// First, turn off autocommit
	$qry = "set autocommit=0";
//pre_echo( $qry );
	$success = mysql_query( $qry, $db_conn );

// Second, take care of all ALTER TABLE queries.  Due to a (documented)
// glitch in MySQL, these commands force a transaction to commit, 
// which sucks.

	if ($success) 
	{ // Create the temp_header table
		$qry = "CREATE TEMPORARY TABLE temp_header LIKE contract_header";
//pre_echo( $qry );
		$success = mysql_query( $qry, $db_conn );
		if (!$success) 
		{
			message_log_append( $msg_log, mysql_error( $db_conn ), MSG_LOG_ERROR );
		}
	}

	if ($success) 
	{ // Create the temp_detail table
		$qry = "CREATE TEMPORARY TABLE temp_detail LIKE contract_detail";
//pre_echo( $qry );
		$success = mysql_query( $qry, $db_conn );
		if (!$success)
		{
			message_log_append( $msg_log, mysql_error( $db_conn ), MSG_LOG_ERROR );
		}
	}

	if ($success) 
	{ // Delete the Seq field from table temp_header
		$qry = "ALTER TABLE temp_header DROP COLUMN Seq";
//pre_echo( $qry );
		$success = mysql_query( $qry, $db_conn );
		if (!$success)
		{
			message_log_append( $msg_log, mysql_error( $db_conn ), MSG_LOG_ERROR );
		}
	}

    if ($success) 
	{ // Delete the Line column from table temp_detail
		$qry = "ALTER TABLE temp_detail DROP COLUMN Line";
//pre_echo( $qry );
		$success = mysql_query( $qry, $db_conn );
		if (!$success)
		{
			message_log_append( $msg_log, mysql_error( $db_conn ), MSG_LOG_ERROR );
		}
	}

// loop through the campaigns, headers, and details to insert the
// data into the SQL database.  Keep solid track of all error
// results so that we can ROLLBACK on any error.
	if ($success) 
	{
//echo "<pre>";
//var_dump( $p_campaigns );  echo "</pre><br>";
		$success = begin( $db_conn );
		if (!$success)
		{
			message_log_append( $msg_log, "Error in START TRANSACTION: " . mysql_error( $db_conn ), MSG_LOG_ERROR );
		}
	}

// do the work here, and keep track of $success
// If we need to create a new agency record, do that here.
	$new_agency = FALSE;
	if ($success && is_null( $p_campaigns[0][ 'Agency Record' ])) 
	{
		$agent_name = $p_campaigns[0][ 'Agency Name' ];
		$rate = DEFAULT_AGENCY_RATE / 10;
		if ($success = agency_insert( $db_conn, $agent_name, $rate, $aindex )) 
		{
			$p_campaigns[0][ 'Agency Record' ] = agency_record( $agent_name, OPERATOR_NAME );
			$success = !is_null( $p_campaigns[0][ 'Agency Record' ]);
		} // if agency_insert
		if ($success) 
		{
			$new_agency = TRUE;
			message_log_append( $msg_log, "Agency created: " .
			"Seq = $aindex, Name = '$agent_name'", MSG_LOG_WARNING );
		} 
		else 
		{
			message_log_append( $msg_log, "Error while creating " . "Agency '$agent_name': " . mysql_error( $db_conn ), MSG_LOG_ERROR );
		}
	} // if null agency record

// If we need to create a new customer record, do that here.

	$new_customer = FALSE;
	if ($success && is_null( $p_campaigns[0][ 'Customer Record' ])) 
	{
		$cust_name = $p_campaigns[0][ 'Customer Name' ];
		$rate = DEFAULT_CUST_DISCOUNT;
		if ($success = customer_insert( $db_conn, $cust_name, $rate, $cindex )) 
		{
			$p_campaigns[0][ 'Customer Record' ] = cust_record( $cust_name, OPERATOR_NAME );
			$success = !is_null( $p_campaigns[0][ 'Customer Record' ]);
		} // if customer_insert
		if ($success) 
		{
			$new_customer = TRUE;
			message_log_append( $msg_log, "Customer created: " . "Seq = $cindex, Name = '$cust_name'", MSG_LOG_WARNING );
		} 
		else 
		{
			message_log_append( $msg_log, "Error while creating " . "Customer '$cust_name' " . mysql_error( $db_conn ), MSG_LOG_ERROR );
		}
	} // if null customer record

	if ($success) 
	{
// build the list of header fields, in order with 'quote required' flag
//  [n][0] is field name, [n][1] is boolean T=quote required, F=not
		$hdr_flds = build_header_field_array();
// A SQL INSERT statement lead-in
		$hdr_sql  = "INSERT INTO temp_header ( ";
		$hdr_sql .= fld_list( $hdr_flds ) . ") VALUES\n";

// build the list of detail fields, in order with 'quote required' flag
//  [n][0] is field name, [n][1] is boolean T=quote required, F=not

		$det_flds = build_detail_field_array();

// A SQL INSERT statement lead-in
		$det_sql  = "INSERT INTO temp_detail ( ";
		$det_sql .= fld_list( $det_flds ) . ") VALUES ";

// Here we go.  We'll loop through each contract header record,
// and its accompanying detail records.

		$n_inserted = 0;

		while ($success && (list( $key ) = each( $p_headers ))) 
		{
		    if (count( $p_details[ $key ] ) > 0) 
			{
//	If we created a new agency or customer above, update 
//	the respective header fields.
				if ($new_customer) 
				{
					$p_headers[ $key ][ 'CIndex' ] = $cindex;
					$p_headers[ $key ][ 'Discount' ] = $p_campaigns[0][ 'Customer Record' ][ 'Discount' ];
				}
				if ($new_agency) 
				{
					$p_headers[ $key ][ 'AIndex' ] = $aindex;
					$p_headers[ $key ][ 'AgencyComm' ] = $p_campaigns[0][ 'Agency Record' ][ 'Rate' ];
				}
				$row = data_values( $hdr_flds, $p_headers[ $key ] );
				$sql_header  = $hdr_sql;	// INSERT INTO ... VALUES
				$sql_header .= "(" . $row . ");";

				$rows = "";
				foreach ($p_details[ $key ] as $line)
				{
					$rows .= ",\n( " . data_values( $det_flds, $line ) . " )";
				}
				$rows = substr( $rows, 1 );	// remove comma-newline
				$sql_detail  = $det_sql;	// INSERT INTO ... VALUES
				$sql_detail .= $rows;

				if ($success = do_insert( $db_conn, $sql_header, $sql_detail ))
				{
					$n_inserted++;
				}
			} // if detail count > 0
		} // while success and each key
	} // if success

	if ($success) 
	{
		$success = commit( $db_conn );
		if ($success)
		{
			message_log_append( $msg_log, "$n_inserted contract" . ($n_inserted == 1 ? '' : 's') . " imported" );
		}
		else 
		{
			message_log_append( $msg_log, "Error in COMMIT TRANSACTION: " . mysql_error( $db_conn ), MSG_LOG_ERROR );
			if (!rollback( $db_conn ))
			{
				message_log_append( $msg_log, "Error in ROLLBACK TRANSACTION: " . mysql_error( $db_conn ), MSG_LOG_ERROR );
			}
		}
	} 
	else 
	{
		if (!rollback( $db_conn ))
		{
			message_log_append( $msg_log, "Error in ROLLBACK TRANSACTION: " . mysql_error( $db_conn ), MSG_LOG_ERROR );
		}
	} // if success
	return( $success );
} // insert_sql_data
Esempio n. 2
0
     }
     break;
 case 'customer_insert_form':
     customer_insert_page();
     break;
 case 'customer_edit_form':
     customer_edit_page($start_data);
     break;
 case 'customer_search':
     customer_search_page($start_data);
     break;
 case 'customer_list':
     customer_search_page();
     break;
 case 'customer_insert':
     $err = customer_insert($start_data);
     status_report('INSERT', $err);
     customer_search_page();
     break;
 case 'customer_edit':
     $err = customer_edit($start_data);
     status_report('UPDATE', $err);
     customer_search_page();
     break;
 case 'bill_select':
     $_SESSION['select_all'] = 0;
     $err = bill_select();
     if ($err) {
         error_display($err);
     }
     break;