$dnum = date(Ymd)*4;
$from=$_POST['fr'];
if($from==""){
$from=$_GET['fr'];
}
if($from!=$dnum){
echo("Unauthorized.");
exit();
}
$dbcnx = @mysql_connect('mysql.plainhost.com', 'ecosmar_ecosmart', 'ecoman1');
$usrn=$_POST['usrn'];
$usr=$_POST['usr'];
$QID=$_POST['QID'];
$CID=$_POST['CID'];
$newCID=$_POST['newCID'];///from select on quote mark-up
mysql_select_db('ecosmar_contacts', $dbcnx);
$getoldcon=mysql_query("SELECT price_level FROM con_main WHERE ID='$CID'");
if(!$getoldcon){
$air[]="Could not check old contact price level. Error: ".mysql_error();
}else{
$old=mysql_fetch_array($getoldcon);
$oldpricelevel=$old[0];
$newcontact = mysql_query("SELECT price_level FROM con_main WHERE ID='$newCID'");
if(!$newcontact){
$air[]="Incomplete Contact Information. Error: ".mysql_error();
echo mysql_error();
exit();
}else{
$row = mysql_fetch_array($newcontact);
$newprice_level = $row['price_level'];
}//got new price level
if($oldpricelevel==$newprice_level){
$straight="yes";
}else{
$straight="no";
}
}//got old price level
//if same price level, just dump items
mysql_select_db('ecosmar_inv', $dbcnx);
if($straight=="yes"){
$act = mysql_query("INSERT INTO quotes SET CID='$newCID',usrn='$usrn',started='$dtnum'");
if(!$act){
$air[]="Could not insert new quote. Error: ".mysql_error();
//echo mysql_error();
exit();
}else{
$newQID = mysql_insert_id();
}//new QID inserted
$getoldprods = mysql_query("SELECT item,description,qty,price,unit FROM q_products WHERE QID='$QID'");
if(!$getoldprods){
$air[]="Could not get old Quote products. Error: ".mysql_error();
}else{
while($row=mysql_fetch_array($getoldprods)){
$newitems[]=$row['item'];
$newdescriptions[]=$row['description'];
$newqtys[]=$row['qty'];
$newprices[]=$row['price'];
$newunits[]=$row['unit'];
}//while getting old products
}///gotoldproductlist
$done="yes";
$countem=count($newitems);
$i=0;
while($i < $countem){
$innewprods=mysql_query("INSERT INTO q_products SET QID='$newQID',item='$newitems[$i]',description='$newdescriptions[$i]',qty='$newqtys[$i]',price='$newprices[$i]',unit='$newunits[$i]'");
if(!$innewprods){
$air[]= $newitems[$i]." not inserted. Error: ".mysql_error();
$done="no";
}
$i=$i+1;
}//while counting new items
if($done=="no"){
echo("CANNOT CONTINUE:
");
foreach($air as $value){
echo("$value
");
}
exit();
}//doneisno
else{
$relocate="eco_quote.php?QID=".$newQID."&CID=".$newCID."&usrn=".$usrn."&usr=".$usr."&fr=".$dnum;
header("Location: $relocate");
}//done is yes
}else{///not same price level//////////////////////////////////////////////////////////////////
$act = mysql_query("INSERT INTO quotes SET CID='$newCID',usrn='$usrn',started='$dtnum'");
if(!$act){
$air[]="Could not insert new quote. Error: ".mysql_error();
//echo mysql_error();
exit();
}else{
$newQID = mysql_insert_id();
}//new QID inserted
$getoldprods = mysql_query("SELECT item,description,qty,price,unit FROM q_products WHERE QID='$QID'");
if(!$getoldprods){
$air[]="Could not get old Quote products. Error: ".mysql_error();
}else{
while($row=mysql_fetch_array($getoldprods)){
$newitems[]=$row['item'];
$newdescriptions[]=$row['description'];
$newqtys[]=$row['qty'];
$newprices[]=$row['price'];
$newunits[]=$row['unit'];
}//while getting old products
}///gotoldproductlist
$done="yes";
$countem=count($newitems);
$i=0;
while($i<$countem){
mysql_select_db('ecosmar_products', $dbcnx);
$getprices = mysql_query("SELECT sku,list,retail,special,dealer,dealer_lo,unit,lo_units,cs,min,description FROM price WHERE item='$newitems'");
if(!$getprices){
$air[]="Could not get price for".$newprod[$i].".";
}else{
$rowg=mysql_fetch_array($getprices);
$skus[]=$rowg['sku'];
$lists[]=$rowg['list'];
$retails[]=$rowg['retail'];
$specials[]=$rowg['special'];
$dealers[]=$rowg['dealer'];
$dealer_los[]=$rowg['dealer_lo'];
$units[]=$rowg['unit'];
$lo_unitss[]=$rowg['lo_units'];
$css[]=$rowg['cs'];
$mins[]=$rowg['min'];
$descriptions[]=$rowg['description'];
}//gotprices
if($newprice_level == "Wholesale"){
$inprice = $specials[$i];
}elseif($newprice_level == "Dealer" and $newqtys[$i]>=$lo_unitss[$i] and ($lo_unitss[$i]*1)!=0){
$inprice = $dealer_los[$i];
}elseif($newprice_level == "Dealer"){
$inprice = $dealers[$i];
}else{
$inprice = $retails[$i];
}
mysql_select_db('ecosmar_inv', $dbcnx);
if($newitems[$i] !="" and $newqtys[$i] !=""){
$inny=mysql_query("INSERT INTO q_products SET QID='$newQID',item='$newitems[$i]',description='$newdescriptions[$i]',qty='$newqtys[$i]',price='$inprice',unit='$newunits[$i]'");
if(!$inny){
$air[]="Could not insert ".$newitems[$i].". Error: ".mysql_error();
$done="no";
}
}//newprod item and qty is not blank
$i=$i+1;
}//while counting down new items
if($done=="no"){
echo("CANNOT DUPLICATE:
");
foreach($air as $value){
echo("$value
");
}
exit();
}//doneisno
else{
$relocate="eco_quote.php?QID=".$newQID."&CID=".$newCID."&usrn=".$usrn."&usr=".$usr."&fr=".$dnum."&dup=yes";
header("Location: $relocate");
}//done is yes
}///not same price level
?>