Below query works but it shows two different sum of different orders perfectly as i made GROUP BY ORDER_ID as (1000, 2000).
But i want this (1000, 2000) as (3000). If there are 2 orders in a week 1 has percentage based discount and one is without discount and each order has different delivery charge.
Each order has different discount percentage, and different delivery charge.
That's why i did group by order_id in my query and if i removes group by order_id it gives me a different total amount. But With the group by order_id it shows the correct amount of as much orders are in the current week or month or year.
Please help in this regard.
Below is my query what im trying to do.
$totalX = "select sum(price) as price, coupon, city, delivery_type, order_id
from orders where date between '2018-09-12' and '2018-09-13' group by order_id";
$totalXx = $dba2->query($totalX);
while ($total = $totalXx->fetch_assoc()) {
$couponX = "select coupon, price, percent from couponsAll where id = '".$total['coupon']."'";
$couponXx = $dba->query($couponX);
$coupon = $couponXx->fetch_assoc();
$areaBX = "select * from countries_citiesALL where id = '".$total['city']."' ";
$areaBXx = $dba->query($areaBX);
$areaBXxx = $areaBXx->fetch_assoc();
$area6X = "select * from delivery_typeALL where area = '".$total['city']."' and id = '".$total['delivery_type']."'";
$area6Xx = $dba->query($area6X);
$area6xXx = $area6Xx->fetch_assoc();
$dchargezQ = $area6xXx['price'];
if ($coupon['price'] >= 1 && $coupon['percent'] < 1) {
/// this condition is if price based discount
$priceAcoup = $total['price'] - $coupon['price'];
$gtotalx = $priceAcoup + $areaBXxx['price'] + $dchargezQ;
$gtotal = number_format($gtotalx, 3);
echo '<font color="black" style=""><b>'.$gtotal.'</b></font>';
} else {
if ($coupon['price'] < 1 && $coupon['percent'] >= 1) {
/// this condition is if percentage based discount
$priceAcoup = $total['price'] - (($total['price'] * $coupon['percent']) / 100);
$gtotalx = $priceAcoup + $areaBXxx['price'] + $dchargezQ;
$gtotal = number_format($gtotalx, 3);
echo '<font color="black" style=""><b>'.$gtotal.'</b></font>';
} else {
/// this condition is if there is no percentage or price based discount
$gtotalx = $total['price'] + $areaBXxx['price'] + $dchargezQ;
$gtotal = number_format($gtotalx, 3);
echo '<font color="black" style=""><b>'.$gtotal.'</b></font>';
}
}
}
Well issue is resolved as mentioned below.
I changed $weekTotal[] under every condition with $weekTotal1[], $weekTotal2[], $weekTotal3[] and put this $weekTotal1=array();, $weekTotal2=array();, $weekTotal3=array(); above the 2nd query loop and got it working.
Thanks for your kind assistance.
$__xz_rTorders1 = $dba2->query("SELECT count(distinct(order_id)) as count, order_id, date FROM orders_confirmed
group by WEEK(date), MONTH(date)
order by WEEK(date), MONTH(date)
");
while($__xz_rTordersa4 = $__xz_rTorders1->fetch_assoc()) {
$__xz_signupweek = $__xz_rTordersa4['date'];
/*start day*/
for($__xz_i = 0; $__xz_i <7 ; $__xz_i++) {
$__xz_date = date('Y-m-d', strtotime("-".$__xz_i."days", strtotime($__xz_signupweek)));
$__xz_dayName = date('D', strtotime($__xz_date));
if($__xz_dayName == "Sun") {
//echo "<b>From:</b> ". date( "d/m/Y", strtotime($__xz_date))." / ";
$mstart_date=date( "Y-m-d", strtotime($__xz_date));
}
}
/*end day*/
for($__xz_i = 0; $__xz_i <7 ; $__xz_i++) {
$__xz_date = date('Y-m-d', strtotime("+".$__xz_i."days", strtotime($__xz_signupweek)));
$__xz_dayName = date('D', strtotime($__xz_date));
if($__xz_dayName == "Sat") {
//echo "<b>To:</b> ". date( "d/m/Y", strtotime($__xz_date));
$mend_date=date( "Y-m-d", strtotime($__xz_date));
}
}
///////////////////***********///////////////////
$weekTotal1=array();
$weekTotal2=array();
$weekTotal3=array();
///////////////////***********///////////////////
$totalX= "select sum(price) as price, coupon, city, delivery_type, order_id
from orders_confirmed where date between '".$mstart_date."' and '".$mend_date."'
group by order_id
";
$totalXx= $dba2->query($totalX);
while ($total = $totalXx->fetch_assoc()){
$couponX= "select coupon, price, percent
from coupons
where id = '".$total['coupon']."'
";
$couponXx= $dba->query($couponX);
$coupon = $couponXx->fetch_assoc();
$areaBX= "select * from countries_cities
where id = '".$total['city']."' ";
$areaBXx= $dba->query($areaBX);
$areaBXxx= $areaBXx->fetch_assoc();
$area6X= "select * from delivery_type
where area = '".$total['city']."'
and id = '".$total['delivery_type']."'
";
$area6Xx= $dba->query($area6X);
$area6xXx= $area6Xx->fetch_assoc();
$dchargezQ= $area6xXx['price'];
if ($coupon['price'] >= 1 && $coupon['percent'] < 1) {
$priceAcoup1=$total['price']-$coupon['price'];
///////////////////***********///////////////////
$weekTotal1[] = $priceAcoup1+$areaBXxx['price']+$dchargezQ;
///////////////////***********///////////////////
}else if ($coupon['price'] < 1 && $coupon['percent'] >= 1) {
$priceAcoup2=$total['price']-(($total['price']*$coupon['percent'])/100);
///////////////////***********///////////////////
$weekTotal2[] = $priceAcoup2+$areaBXxx['price']+$dchargezQ;
///////////////////***********///////////////////
}else{
///////////////////***********///////////////////
$weekTotal3[] = $total['price']+$areaBXxx['price']+$dchargezQ;
///////////////////***********///////////////////
}
} // End Of While Loop (2nd)
$weekTotal_val3 = array_sum($weekTotal1);
$weekTotal_val4 = array_sum($weekTotal2);
$weekTotal_val5 = array_sum($weekTotal3);
$finalTOTAL=number_format($weekTotal_val3+$weekTotal_val4+$weekTotal_val5,3);
echo '<font color="red" style=""><b>'.$finalTOTAL.'</b></font>';
} // End Of While Loop (1st)
Related
I have all records in sequence from 1 onwards for each record displayed but wish to update each one with that sequence in a field within the table.
Any help?
if(isset($_POST['setorder']))
{
$i = 1;
//$today = date('Y-m-d', strtotime('-1 day'));
$routesql = $mysqli->query("SELECT * FROM routes WHERE status = 1");
while ($routerows = $routesql->fetch_array())
{
$rname = $routerows['routename'];
$routejobs = $mysqli->query("select tracking.*, district.* from tracking left join district on tracking.street = district.street
where tracking.date = '$today' AND tracking.status = 2 and route = '$rname' order by district.sortorder asc");
while ($routejrows = $routejobs->fetch_array())
{
echo $i.' - '.$routejrows['joborder'].' - '.$routejrows['addressto'];
echo '<br/>';
$i++;
}
}
}
This is a query which I tried below.
$sql = "SELECT id,due, datediff(date('Y/m/d'), date) FROM sales
I am writing a sales program for identifying the due payment.
If the due payment is more than 7 error displayed payment pending what I tried so far I attached below of the full coding. when I tried the below code error ouput displayed
Notice: Undefined index: datediff(pay_date, date)
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
$today = date("Y/m/d");
$sql = "SELECT id,due, datediff(date('Y/m/d'), date) FROM sales where due != '0' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
$paydate = $row["datediff(pay_date, date)"] . " " . "Days" . "</br>";
if ($paydate >= 7) {
$status = "Payment pending";
$color = "#47f50b";
} elseif ($paydate >= 3) {
$status = "Payment pending please settle down the amount";
$color = "yellow";
} elseif ($paydate >= 1) {
$status = "kindly pay the payment";
$color = "#00a8ff";
} elseif ($paydate == 0) {
$status = "Disable your Order";
$color = "red";
}
?>
Assuming your due date column is called date, then your query should be
$sql = "SELECT id, due, DATEDIFF(date, CURDATE()) AS paydate FROM sales WHERE due != '0' ";
Note that we use CURDATE() to get the current date in MySQL, and that I have aliased the DATEDIFF result to paydate.
You also have an issue that you are trying to set
$paydate = $row ["datediff(pay_date, date)"]
however in your query your expression does not match datediff(pay_date, date). This is why it's better to use an alias as I did above, then you can change that line to simply
$paydate = $row['paydate'];
and if you change the formula in your query this part of the code will still work.
Finally you are appending " Days" to the value of $paydate. This will make comparisons with numbers e.g. in if($paydate >= 7) problematic and it would be better if you left off the " Days" until you output the value of $paydate.
Try using datediff(pay_date, date) as datedif in the SELECT and print using $row["datedif"]
for($i=0; $i<count($_POST['order_item']); $i++)
{
$order_date=$_POST['order_date'];
$customer_name=$_POST['customer_name'];
$salesman_name=$_POST['salesman_name'];
$area_code=$_POST['area_code'];
$product_name=$_POST['product_name'][$i];
$item_qty=$_POST['item_qty'][$i];//product actual quality
$order_item=$_POST['order_item'][$i]; //order item number
$billed_qty=$_POST['itqty'][$i]; // product billed quantity editable
$prod_mrps=$_POST['mrps'][$i];
$prices=$_POST['price'][$i];
$discount=$_POST['qty'][$i];
$amount=$_POST['total'][$i];
$prod_qty = $_POST['prod_qty'][$i];//available quantity
$batch_no = $_POST['batch_no'][$i];//batch number
$qtyout = 0;
$result = $dbo->query("SELECT `batch_no`, `prod_id`, `prod_name`, `total_qty` FROM `sm_product_batch` where `prod_name` = '$product_name' and `batch_status` = 'Active_batch' order by batch_no asc");
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
if($billed_qty > 0)
{
$batchout = 0;
$rem = max($row['total_qty']-$billed_qty,0);
if($rem == 0)
$batchout = $row['total_qty']; //This means there are no items of this cost remaining
else
$batchout = $billed_qty; //This means there are items remaining and therefore our next loop (within the while) will check for the next expensive item
$billed_qty -= $batchout;
$qtyout += $batchout;
$sql = "Update sm_product_batch set total_qty = (total_qty - $batchout) where prod_name='".$product_name."' AND batch_no = ".$row["batch_no"];
$dbo->query($sql);
$sql1 = "Update sm_product_batch SET batch_status=CASE WHEN total_qty='0' THEN 'Inactive_batch' ELSE 'Active_batch' END where prod_name='".$product_name."'";
$dbo->query($sql1);
}
}
$sql = "INSERT INTO sm_invoice (order_id,invoice_id,product_name,customer_name,salesman_name,order_item,area_code,order_date,invoice_date,item_qty,billed_qty,batch_no,prod_mrp,price,spl_dis,total) VALUES ('".$order_id."','".$order_id."','".$product_name."','".$customer_name."','".$salesman_name."','".$order_item."','".$area_code."','".$order_date."','".$invoice_date."','".$item_qty."','".$_POST['itqty'][$i]."','".$batch_no."','".$prod_mrps."','".$prices."','".$discount."','".$amount."')";
$dbo->query($sql);
}
This is my code to update inventory with batch and invoice table. This code is only working for my first product. Need this to be done for all products of an order. Any help is appreciated.
for($i=0; $i<count($_POST['order_item']); $i++)
{
$order_date=$_POST['order_date'];
$customer_name=$_POST['customer_name'];
$salesman_name=$_POST['salesman_name'];
$area_code=$_POST['area_code'];
$product_name=$_POST['product_name'][$i];
$item_qty=$_POST['item_qty'][$i];//product actual quality
$order_item=$_POST['order_item'][$i]; //order item number
$billed_qty=$_POST['itqty'][$i]; // product billed quantity editable
$prod_mrps=$_POST['mrps'][$i];
$prices=$_POST['price'][$i];
$discount=$_POST['qty'][$i];
$amount=$_POST['total'][$i];
$prod_qty = $_POST['prod_qty'][$i];//available quantity
$batch_no = $_POST['batch_no'][$i];//batch number
$qtyout = 0;
$result = $dbo->query("SELECT `batch_no`, `prod_id`, `prod_name`, `total_qty` FROM `sm_product_batch` where `prod_name` = '$product_name' and `batch_status` = 'Active_batch' order by batch_no asc");
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
if($billed_qty > 0)
{
$batchout = 0;
$rem = max($row['total_qty']-$billed_qty,0);
if($rem == 0)
$batchout = $row['total_qty']; //This means there are no items of this cost remaining
else
$batchout = $billed_qty; //This means there are items remaining and therefore our next loop (within the while) will check for the next expensive item
$billed_qty -= $batchout;
$qtyout += $batchout;
$sql = "Update sm_product_batch set total_qty = (total_qty - $batchout) where prod_name='".$product_name."' AND batch_no = ".$row["batch_no"];
$dbo->query($sql);
$sql1 = "Update sm_product_batch SET batch_status=CASE WHEN total_qty='0' THEN 'Inactive_batch' ELSE 'Active_batch' END where prod_name='".$product_name."'";
$dbo->query($sql1);
$sql = "INSERT INTO sm_invoice (order_id,invoice_id,product_name,customer_name,salesman_name,order_item,area_code,order_date,invoice_date,item_qty,billed_qty,batch_no,prod_mrp,price,spl_dis,total) VALUES ('".$order_id."','".$order_id."','".$_POST['product_name'][$i]."','".$customer_name."','".$salesman_name."','".$_POST['order_item'][$i]."','".$area_code."','".$order_date."','".$invoice_date."','".$item_qty."','".$batchout."','".$row["batch_no"]."','".$_POST['mrps'][$i]."','".$_POST['price'][$i]."','".$_POST['qty'][$i]."','".$_POST['total'][$i]."')";
$dbo->query($sql);
}
}
}
Found the answer after lot of debugging.
Thanks for all your response.
I'm trying to generate barcode tickets after successful booking and it's working fine. But yesterday (06-Jan-2017) night at 11.59 pm, one customer booked 2 tickets but the ticket sl.no was duplicate. Tthat is both tickets sl.no was 1. Actually it should be 2. But the next customer's tickets sl.no was correct, it's 3,4,5. Is there any mistake in the code below where the tickets get generated?
for($j=1;$j<=$cat_array[$i];$j++) {
$sel_max_dt = mysql_query("select MAX(DT) AS maxdt, MAX(REC_NO) AS maxrec FROM tkt_barcode");
//to select max date and max rec no.
$row_max_dt = mysql_fetch_array($sel_max_dt);
$dt_max = $row_max_dt['maxdt'];
//$no_max = $row_max_dt['maxno'];
$recno = $row_max_dt['maxrec'];
if($dt_max == date("Y-m-d") && $recno == $TxnID) {
//if same date and same bkid barcode slno continues.
$sel_no = mysql_query("select MAX(BARCODE_SLNO) AS maxno FROM tkt_barcode WHERE REC_NO=".$TxnID);
$row_sel_no = mysql_fetch_array($sel_no);
$tkt_slno = $row_sel_no['maxno'];
$tkt_slno = $tkt_slno + 1;
}
if($dt_max == date("Y-m-d")&& $recno != $TxnID) {
//if same date and different bkid barcode slno continues.
$sel_no = mysql_query("select MAX(BARCODE_SLNO) AS maxno FROM tkt_barcode WHERE DT='$dt_max' ");
$row_sel_no = mysql_fetch_array($sel_no);
$tkt_slno = $row_sel_no['maxno'];
$tkt_slno = $tkt_slno + 1;
}
if($dt_max != date("Y-m-d") && $recno != $TxnID) {
//if not same date start barcode slno from 1.
$tkt_slno = 1;
}
$sel_sl_no = mysql_query("select MAX(SL_NO) AS slno FROM tkt_barcode WHERE REC_NO=".$TxnID);
$row_slno = mysql_fetch_array($sel_sl_no);
$slno = $row_slno['slno'];
$bartkt_slno = $slno + 1;
if($i==1 && $j>$count_promo_adult && $arr_edit["PROMO_CODE"]!=""){
//adult cat and total adult count > total promo adult count
$disc_adult = $adult_amt;
$discamt=$adult_amt - $disc_adult ; // discount amt
$rate=$adult_amt; //normal rate
$net_amt=$disc_adult; //discount rate
$pro_cd ="";
//promo code is null when total adult count > total promo adult count
} //end if
//string pading digits
$len_txn =strlen($TxnID); // bookid length
$pad = 6; //str_pad digits
if($len_txn > 5) {
if($len_txn==6) { $pad = $pad-1;}// if 6 digit
if($len_txn==7){ $pad = $pad-2;}// if 7 digit
}
$fullbar = $TxnID.str_pad($tkt_slno, $pad, 0, STR_PAD_LEFT).$clnd_date.$counter_no;
//barcode no
$fields = "BARCODE_SLNO,DT,FIN_YEAR,Counter_Code,Branch_cd,REC_NO,BARCODE,TICKET_TYPE,CATG_CD,AMT,DISC_AMT,SL_NO,NET_AMT,BCODE_CATG_SLNO,BCODE_SLNO_GEN,BCODE_SLNO_CATG_TOT,DAY_TYPE,Check_in_date,PROMO_CODE,TAX,S_TAX,conv_base_rate,conv_tax_rate";
$values = "'$tkt_slno','$booked_date','$fin_year','$counter_no','$branch','$TxnID','$fullbar','$cat_name_tkt','$i','$rate','$discamt','$bartkt_slno','$net_amt','$j','$count','$cat_tot','$type_day','$date_check','$pro_cd','$tax_each','$ser_tax_each','$conv_base','$conv_tax'";
$sql_tkt = $objA->insert_fields(TABLEPREFIX.'tkt_barcode',$fields,$values);
}//end for loop
You are selecting a count of existing records
$sel_sl_no = mysql_query("select MAX(SL_NO) AS slno FROM tkt_barcode WHERE REC_NO=".$TxnID);
$row_slno = mysql_fetch_array($sel_sl_no);
$slno = $row_slno['slno'];
$bartkt_slno = $slno + 1;
and using that count to set your sl number. But time passes between reading the count and writing the record. When two threads read the count at the same time you get your problem.
Finally I found the solution by adding another condition given below.
if($dt_max != date("Y-m-d")&& $recno == $TxnID) {
//if different date and same bkid barcode slno continues.
$sel_no = mysql_query("select MAX(BARCODE_SLNO) AS maxno FROM tkt_barcode WHERE DT='$dt_max' ");
$row_sel_no = mysql_fetch_array($sel_no);
$tkt_slno = $row_sel_no['maxno'];
$tkt_slno = $tkt_slno + 1;
}
Thanks guys for the help. I will try to make my code more readable next time. :)
I have table of cities, birthday, and users.
Now, what I am trying to do is to query through database and filter range of ages and city of living (out of a list of cities in the selected region - that is why I am checking if count($city[]))>=1), and finely pull out the name of the user based on uid / entety_id.
First I am checking if the user match the range of age, and then if he lives in one of the cities from the selected list.
If no city was selected, I want to filter only the range of ages.
I don't know what is the problem but as far as I notice,
there is this loop, that I have notice that does not execute at all:
for ($d = 1; $d < (count($the_resaults_array)); $d++)
this is the code:
function toodate_query_db($loest_age, $highest_age, $city){
$sql_user_name = '';
if((count($city[0]))>=1){
for ($i = 0; $i < count($city); $i++) {
$city_item_for_check = $city[$i];
$sql = "SELECT field_data_field_birth_date.entity_id
FROM field_data_field_birth_date, field_data_field_city
WHERE(
(field_birth_date_value >= DATE_ADD(NOW(), INTERVAL -{$highest_age} YEAR))
AND
(field_birth_date_value <= DATE_ADD(NOW(), INTERVAL -{$loest_age} YEAR))
AND
(field_city_tid = {$city_item_for_check})
AND
(field_data_field_birth_date.entity_id = field_data_field_city.entity_id))";
if(db_query($sql)->fetchColumn()){
$the_resaults_array[$i] = db_query($sql)->fetchAll();
}
}
for ($d = 1; $d < (count($the_resaults_array)); $d++) {
$user_id = $the_resaults_array[$d];
$sql_user_name .= '<p><strong>'.
db_query("SELECT name FROM {users} WHERE uid = {$user_id}")->fetchColumn()
.'</p></strong>';
}
} else{
$sql = "SELECT field_data_field_birth_date.entity_id
FROM field_data_field_birth_date
WHERE
field_birth_date_value >= DATE_ADD(NOW(), INTERVAL -{$highest_age} YEAR)
AND field_birth_date_value <= DATE_ADD(NOW(), INTERVAL -{$loest_age} YEAR)";
foreach (db_query($sql) as $result) {
$entity_id = $result->entity_id;
$sql_user_id = db_query("SELECT uid FROM {profile} WHERE pid = {$entity_id}")->fetchColumn();
$sql_user_name .= '<p><strong>'.
db_query("SELECT name FROM {users} WHERE uid = {$sql_user_id}")->fetchColumn()
.'</p></strong>';
}}
return $sql_user_name;
}