<?php
require_once('modules/Invoice/Invoice.php');
require_once('include/logging.php');
require_once('include/database/PearDatabase.php');
include("modules/Emails/mail.php");
$local_log =& LoggerManager::getLogger('index');
$focus = new Invoice();
//added to fix 4600
$search=vtlib_purify($_REQUEST['search_url']);
global $current_user;
setObjectValuesFromRequest($focus);
$focus->column_fields['currency_id'] = $_REQUEST['inventory_currency'];
$cur_sym_rate = getCurrencySymbolandCRate($_REQUEST['inventory_currency']);
$focus->column_fields['conversion_rate'] = $cur_sym_rate['rate'];
if($_REQUEST['assigntype'] == 'U') {
$focus->column_fields['assigned_user_id'] = $_REQUEST['assigned_user_id'];
} elseif($_REQUEST['assigntype'] == 'T') {
$focus->column_fields['assigned_user_id'] = $_REQUEST['assigned_group_id'];
}
$focus->save("Invoice");
$return_id = $focus->id;
$parenttab = getParentTab();
if(isset($_REQUEST['return_module']) && $_REQUEST['return_module'] != "") $return_module = vtlib_purify($_REQUEST['return_module']);
else $return_module = "Invoice";
if(isset($_REQUEST['return_action']) && $_REQUEST['return_action'] != "") $return_action = vtlib_purify($_REQUEST['return_action']);
else $return_action = "DetailView";
if(isset($_REQUEST['return_id']) && $_REQUEST['return_id'] != "") $return_id = vtlib_purify($_REQUEST['return_id']);
$local_log->debug("Saved record with id of ".$return_id);
//Code added by Ed Lentz to update the invoiceCF table with tax info on 9-13-09****
//ini_set("display_errors", "1");
//error_reporting(E_ALL);
$id= $_POST['record'];
// connect to db
$conn = mysql_connect('localhost','communiq','xxxxxxx');
$db = mysql_select_db('communiq_vtcrm1',$conn);
//**********************************************************************************
//Update Query to do the math for the extended prices of Products sold
$sql = "Update vtiger_inventoryproductrel SET extlistprice = quantity * listprice";
$result = mysql_query($sql, $conn);
//****************************Get Sum of the Products*******************************
$sql1="SELECT
Sum(vtiger_inventoryproductrel.extlistprice)
FROM
vtiger_inventoryproductrel
Inner Join vtiger_products ON vtiger_inventoryproductrel.productid = vtiger_products.productid
WHERE
vtiger_inventoryproductrel.id = $id";
$result = mysql_query($sql1, $conn);
$productstotal = mysql_fetch_row($result);
//*************Update query to insert the Product total into the invoiceCF table****
$sql2="Update vtiger_invoicecf SET cf_603 = $productstotal[0] where vtiger_invoicecf.invoiceid = $id";
$result = mysql_query($sql2, $conn);
$salestax = $productstotal[0] * .06;
//************Math to set the Sales Tax for the products Sold***********************
$sql2="Update vtiger_invoicecf SET cf_604 = $salestax where invoiceid =$id";
$result = mysql_query($sql2, $conn);
//**********Query to get the services total*****************************************
$sql3 = "SELECT
Sum(vtiger_inventoryproductrel.extlistprice)
FROM
vtiger_inventoryproductrel
Inner Join vtiger_service ON vtiger_inventoryproductrel.productid = vtiger_service.serviceid
WHERE
vtiger_inventoryproductrel.id = $id";
$result = mysql_query($sql3, $conn);
$servicestotal=mysql_fetch_row($result);
//*****************Update to insert the Service total into the invoiceCF table********
$sql4="Update vtiger_invoicecf SET cf_602 = $servicestotal[0] where invoiceid = $id";
$result = mysql_query($sql4, $conn);
mysql_close();
// Make a MySQL Connection******For the automatic entry of Trouble ticket Problem/solution***************
mysql_connect("localhost", "communiq", "xxxxxxx") or die(mysql_error());
mysql_select_db("communiq_vtcrm1") or die(mysql_error());
// Get all the data from the table
$result15 = mysql_query("SELECT
vtiger_crmentity.description,
vtiger_troubletickets.solution,
vtiger_troubletickets.ticketid
FROM
vtiger_troubletickets
Inner Join vtiger_crmentity ON vtiger_troubletickets.ticketid = vtiger_crmentity.crmid
WHERE
vtiger_troubletickets.ticketid = 7629")or die(mysql_error());
$row = mysql_fetch_array($result15);
$trouble= $row['description'];
$fix =$row['solution'];
$sql = "Update communiq_vtcrm1.vtiger_invoice x, communiq_vtcrm1.vtiger_crmentity z Set x.terms_conditions= $fix , z.description= $trouble Where z.crmid =$id and x.invoiceid =$id"or die(mysql_error());
$result = mysql_query($sql);
mysql_close();
//*************End Trouble Ticket code ***************************************************
//}
//else
//{
//code added for returning back to the current view after edit from list view
if($_REQUEST['return_viewname'] == '') $return_viewname='0';
if($_REQUEST['return_viewname'] != '')$return_viewname=vtlib_purify($_REQUEST['return_viewname']);
header("Location: index.php?action=$return_action&module=$return_module&parenttab=$parenttab&record=$return_id&viewname=$return_viewname&start=".vtlib_purify($_REQUEST['pagenumber']).$search);
?>