<?php require_once 'protect.php'; ?>
<?php if  ($activenr==0) { ?>
<?php echo $textcartempty ?>
<?php } else {?>

<?php
$email1 = $mysqli->real_escape_string(stripslashes($_REQUEST['email1']));
$password1 = $mysqli->real_escape_string(stripslashes($_REQUEST['password1']));
$trid = $mysqli->real_escape_string(strip_tags($_REQUEST['trid']));

$resultm = $mysqli->query("SELECT id FROM members WHERE email='$email1' AND password!='$password1'");
$rowsm = mysqli_num_rows($resultm);
?>
<?php if ($rowsm==0) { ?>

<?php
$result = $mysqli->query("SELECT * FROM members WHERE email='$email1' AND email!='' AND password='$password1' AND password!='' ");
$row = $result->fetch_array();
$rowslog = mysqli_num_rows($result);
$memberid=$row['id'];
$email=$row['email'];
$password=$row['password'];
$name=$row['name'];
$profession=$row['profession'];
$company=$row['company'];
$address=$row['address'];
$postcode=$row['postcode'];
$region=$row['region'];
$city=$row['city'];
$country=$row['country'];
$tel=$row['tel'];
$fax=$row['fax'];
$taxnr=$row['taxnr'];
$taxoffice=$row['taxoffice'];
$active=$row['active'];

$result1 = $mysqli->query("SELECT * FROM cart WHERE cookie_id='$cookieid' AND item_id=ANY (SELECT insert_id FROM items WHERE publish='yes') GROUP BY active_id");
while($row = $result1->fetch_array()) {
$price1=$row['price'];
$weight1=$row['weight'];
$quantity1=$row['quantity'];

$pprice1 = $quantity1 * $price1;
$totalprice1 += $pprice1;

$tweight = $quantity1 * $weight1;
$totalweight += $tweight;
}

$resulttr = $mysqli->query("SELECT * FROM transport WHERE id='$trid'");
$row = $resulttr->fetch_array(); 
$trid=$row['id'];
$destination=$row['name'];
$startcost=$row['startprice'];
$kgcost=$row['kgprice'];

$totalkgcost = $kgcost * $totalweight;
if ($totalweight<$siteweight) { $totalkgcost=0; }
$totaltcost = $startcost + $totalkgcost;
$totaltcost = number_format($totaltcost,2);
$tcost = $totaltcost;
?>

<?php if ($inquiry==0) { ?>

<div>
<span class="titles"><?php echo $textoprice ?></span>  <span class="price price2"> &nbsp; <?php echo $totalprice1 ?> &#8364; </span>
</div>
<?php if ($tcost>0) { ?>
<div class="margtop10">
<div><?php echo $texttransportcost ?></div>
<span class="titles"><?php echo $textdeliverycost ?> (<?php echo $destination ?>)</span>  <span class="price price2"> &nbsp; <?php echo $tcost ?> &#8364; </span>
<?php if ($totalweight>0) { ?><strong><?php echo $textweight ?> <?php echo $totalweight ?> <?php echo $unitweight ?></strong><?php } ?>
</div>
<?php } ?> 

<?php }else{?>
<span class="titles"><?php echo $buttonorder ?></span>
<?php } ?>

<div class="divspace"> <?php echo $textdelivery1 ?> </div>
<hr>

<?php
if (isset($_POST['submit']) AND $_REQUEST['notes']=='') {
$memberid = $mysqli->real_escape_string(strip_tags($_REQUEST['memberid']));
$email = $mysqli->real_escape_string(strip_tags($_REQUEST['email']));
$email=preg_replace('/\s+/', '', $email);
$email = strtolower($email);
$password = $mysqli->real_escape_string(strip_tags($_REQUEST['password']));
$name = $mysqli->real_escape_string(strip_tags($_REQUEST['name']));
$name = strip_tags($name);
$name = preg_replace('/\s+/', ' ',$name);
$name = mb_convert_case($name, MB_CASE_TITLE, "UTF-8");
$profession = $mysqli->real_escape_string(strip_tags($_REQUEST['profession']));
$ritem = $mysqli->real_escape_string(strip_tags($_REQUEST['ritem']));
$address = $mysqli->real_escape_string(strip_tags($_REQUEST['address']));
$region = $mysqli->real_escape_string(strip_tags($_REQUEST['region']));
$postcode = $mysqli->real_escape_string(strip_tags($_REQUEST['postcode']));
$city = $mysqli->real_escape_string(strip_tags($_REQUEST['city']));
$country = $mysqli->real_escape_string(strip_tags($_REQUEST['country']));
$tel = $mysqli->real_escape_string(strip_tags($_REQUEST['tel']));
$fax = $mysqli->real_escape_string(strip_tags($_REQUEST['fax']));
$taxnr = $mysqli->real_escape_string(strip_tags($_REQUEST['taxnr']));
$taxoffice = $mysqli->real_escape_string(strip_tags($_REQUEST['taxoffice']));
$newsletter = $mysqli->real_escape_string(strip_tags($_REQUEST['newsletter']));
$send_name = $mysqli->real_escape_string(strip_tags($_REQUEST['send_name']));
$send_address = $mysqli->real_escape_string(strip_tags($_REQUEST['send_address']));
$send_region = $mysqli->real_escape_string(strip_tags($_REQUEST['send_region']));
$send_postcode = $mysqli->real_escape_string(strip_tags($_REQUEST['send_postcode']));
$send_city = $mysqli->real_escape_string(strip_tags($_REQUEST['send_city']));
$comments = $mysqli->real_escape_string(strip_tags($_REQUEST['comments']));
$comments = str_replace('\r\n', ' ',$comments);
$comments = preg_replace('/\s+/', ' ',$comments);
$paytype = $mysqli->real_escape_string(strip_tags($_REQUEST['paytype']));
$receipt = $mysqli->real_escape_string(strip_tags($_REQUEST['receipt']));
$period = $mysqli->real_escape_string(strip_tags($_REQUEST['period']));
$date = date ("j.m.Y");

if (!$destination) {
$destination = $country;
}

$result = $mysqli->query("SELECT id FROM members WHERE email='$email' AND password='$password'");
$row = $result->fetch_array();
$rowslog = mysqli_num_rows($result);
$memberid=$row['id'];

$resultm = $mysqli->query("SELECT id FROM members WHERE email='$email' AND password!='$password'");
$rowsm = mysqli_num_rows($resultm);

$result = $mysqli->query("SELECT insert_id FROM categories WHERE type=4 AND priority=1 GROUP BY lang_id LIMIT 1");
$row = $result->fetch_array(); 
$ccid=$row['insert_id'];
?>

<?php if ($rowsm==0) { ?>

<?php
if ($rowslog==0) {
$mysqli->query("INSERT INTO members (category_id, email, password, name, address, region, postcode, city, country, tel, fax, taxnr, taxoffice, publish, active, date) VALUES ('$ccid', '$email', '$password', '$name', '$address', '$region', '$postcode', '$city', '$country', '$tel', '$fax', '$taxnr', '$taxoffice', '$publish', '$newsletter', '$date')");
$result = $mysqli->query("SELECT id FROM members WHERE email='$email' AND password='$password'");
$row = $result->fetch_array();
$memberid=$row['id'];
} else {
$mysqli->query("UPDATE members SET category_id='$ccid', name='$name', profession='$profession', address='$address', region='$region', postcode='$postcode', city='$city', country='$country', tel='$tel', fax='$fax', taxnr='$taxnr', taxoffice='$taxoffice', active='$newsletter' WHERE email='$email'");
}

$message="
$fieldname:  $name 
$fieldprof:  $profession - $ritem
$fieldaddress:  $address
$fieldpostcode:  $postcode 
$fieldregion:  $region
$fieldcity:  $city
$fieldcountry:  $country
$fieldtel:  $tel
Fax:  $fax
Email:  $email
$fieldtaxnr:  $taxnr
$fieldtaxoffice:  $taxoffice

Message: $comments
";

if ($inquiry==0) {
$message=$message."
$textdelivery
______________________________________________________________________________________

$fieldname:  $send_name 
$fieldaddress:  $send_address
$fieldpostcode:  $send_postcode
$fieldregion:  $send_region
$fieldcity:  $send_city
$fieldcountry:  $destination
______________________________________________________________________________________

$payway: $paytype
$receipt
";
}

$message=$message."
$pageorder
______________________________________________________________________________________
";

$result2 = $mysqli->query("SELECT * FROM cart WHERE cookie_id='$cookieid' AND item_id=ANY (SELECT insert_id FROM items WHERE publish='yes') GROUP BY active_id ORDER BY item_id, category_id, extra_id");
while($row = $result2->fetch_array()) {
$count++;
$active_id=$row['active_id'];
$item_id=$row['item_id'];
$scategory_id=$row['category_id'];
$extra_id=$row['extra_id'];
$price=$row['price'];
$quantity=$row['quantity'];

$pprice=$quantity*$price;
$totalprice = $totalprice+$pprice;
$totalquantity = $totalquantity+$quantity;

$result3 = $mysqli->query("SELECT * FROM items WHERE insert_id=$item_id AND lang_id=$lang AND publish='yes' ORDER BY category_id, priority");
$row = $result3->fetch_array();
$nrrows3=mysqli_num_rows($result3);
$category_id=$row['category_id'];
$itemname=$row['name'];
$code=$row['code'];
$unit=$row['unit'];

$allitems = $itemname.", ".$allitems;

$result4 = $mysqli->query("SELECT * FROM categories WHERE insert_id=$category_id AND lang_id=$lang");
$row = $result4->fetch_array();
$category=$row['name'];

$message=$message."
$count. $itemname
$textcategory: $category
$textcode: $code";
$result5s = $mysqli->query("SELECT * FROM categories WHERE type=3 AND insert_id IN (SELECT category_id FROM cart WHERE active_id='$active_id') AND lang_id=$lang");
while($row = $result5s->fetch_array()) {
$scatid=$row['insert_id'];
$scategory=$row['name'];

$result6s = $mysqli->query("SELECT * FROM extras WHERE type=1 AND insert_id IN (SELECT extra_id FROM cart WHERE active_id='$active_id') AND category_id='$scatid' AND lang_id=$lang");
$row = $result6s->fetch_array();
$extra=$row['name'];
$message=$message."
$scategory >> $extra";}
$message=$message."
$textquantity: $quantity $unit
";

if ($inquiry==0) {
$message=$message."
$textprice: $price Euro
$texttotal: $pprice Euro
";
}
}
 //end while loop
$mysqli->query("DELETE FROM cart WHERE cookie_id='$cookieid'");
$mysqli->query("ALTER TABLE cart AUTO_INCREMENT = 1");

$finalprice = $totalprice + $tcost;
$amount = $finalprice;

$result7 = $mysqli->query("SELECT id FROM requests ORDER BY id desc LIMIT 1");
$row=mysqli_fetch_array($result7);
$lastid=$row['id'];
$nextid=$lastid + 1;
$orderid1 = $nextid.date("dmY");

$mysqli->query("INSERT INTO requests (orderid, cookie_id, member_id, email, name, address, country, tel, items, amount, currency, period, paytype) VALUES ('$orderid1', '$cookieid', '$memberid', '$email', '$name', '$address $region $postcode $city $country', '$destination', '$tel', '$allitems', '$amount', 'EUR', '$period', '$paytype')");

$result8 = $mysqli->query("SELECT orderid FROM requests WHERE cookie_id='$cookieid' ORDER BY id desc LIMIT 1");
$row=mysqli_fetch_array($result8);
$orderid=$row['orderid'];

$message="
$pageorder $orderid - $domain

$textorderinfo
______________________________________________________________________________________ 
".$message."
______________________________________________________________________________________

$texttotal $textquantity: $totalquantity
______________________________________________________________________________________
";
if ($inquiry==0) {
$message=$message."
$textoprice: $totalprice Euro
$textdeliverycost - $destination: $tcost Euro
______________________________________________________________________________________
$texttotal $textprice: $finalprice Euro

$textinstallm $period
";
}

$message=$message."

www.$domain 
";

$to="$email";
$bcc="$sitemail";
$from="$email";
$reply="$email";
$headers="MIME-Version: 1.0\r\n";
$headers .="Content-type: text/plain; charset=utf-8\r\n";
$headers .="Bcc: $bcc\r\nFrom: $from\r\nReply-To: $reply";
$subject="$pageorder $orderid $domain";
mail ($to, $subject, $message, $headers);
?>

<div class="divspace"><?php echo $textorder ?></div>
<!--sent --> 
<strong><?php echo $name ?></strong> <?php echo $profession ?><br>
<?php echo $address ?> - <?php echo $postcode?> <?php echo $region?> <?php echo $city?> - <?php echo $country?><br>
<?php echo $tel?> - <?php echo $fax?><br>
<?php echo $email?><br>
<?php echo $taxnr?> <?php echo $taxoffice?><br>
<?php if  ($send_name) { ?>
<strong>&raquo; <?php echo $send_name ?></strong><br>
<?php echo $send_address ?> - <?php echo $send_postcode?> <?php echo $send_region?> <?php echo $send_city?><br>
<?php } ?>
<strong><span class="icon fa-map-marker color3"></span> <?php echo $destination?></strong>
<!--end sent --> 

<hr>
<div class="titles color3">Order nr. <?php echo $orderid ?></div>
<span class="titles"><?php echo $texttotal ?> <?php echo $textprice ?> </span>
<span class="price price2"> &nbsp; <?php echo $amount ?> &#8364; </span>

<?php if ($payment==1 AND $amount AND $paytype == "card") { ?>
<?php include 'payment.php';?>
<?php } ?>

<?php } else { ?>
<?php echo $textloginfail ?>
<?php } ?>
 
<?php } else { ?>
<?php include 'orderform.php';?>
<?php } ?>

<?php } else { ?>
<?php echo $textloginfail ?>
<?php } ?>

<?php } ?>