CREATE TABLE menu
(`mid` int, `parent` varchar(4), `org` int, `item` varchar(8), `price` varchar(3))
;
INSERT INTO menu
(`mid`, `parent`, `org`, `item`, `price`)
VALUES
(1, NULL, 1, 'Category', '1'),
(2, '1', 1, 'Item 1', '$10'),
(3, '1', 1, 'Item 2', '$12'),
(4, NULL, 1, 'Category', '2'),
(5, '4', 1, 'Item 3', '$12'),
(6, '4', 1, 'Item 4', '$90')
;
<?php
$unique_id = session()->get('unique_id');
if($unique_id) {
$state = $this->request->getPost('state');
$south = $this->db->table('states')->where(array('stateName'=>$state))->get()->getRow('south');
$own_items = $this->db->query("SELECT productType, count(productId) as cnt, IFNULL(sum(weight * qty),0) as weight, IFNULL(sum(total),0) as total FROM cart_temp where unique_id = '".$unique_id."' and productType='own'")->getRowArray();
$not_own_items = $this->db->query("SELECT productType, count(productId) as cnt, IFNULL(sum(weight * qty),0) as weight, IFNULL(sum(total),0) as total FROM cart_temp where unique_id = '".$unique_id."' and productType!='own'")->getRowArray();
//echo "select * from courier_charges where ".$own_items['weight']." between fromKg and toKg"; exit;
if($state!='') {
//{{New code begin
//Weight multiplier
$own_items_weight_multiplier = floor($own_items['weight']);
$not_own_items_weight_multiplier = floor($not_own_items['weight']);
$own_items_fractional_weight=$own_items['weight']-$own_items_weight_multiplier;
$not_own_items_fractional_weight=$not_own_items['weight']-$not_own_items_weight_multiplier;
//New code end }}
$shipping = 0; $Oshipping = 0;
if($own_items['cnt'] > 0) {
$charge = $this->db->query("select * from courier_charges where ".$own_items['weight']." between fromKg and toKg")->getRowArray();
if($south==0) $shipping = $shipping + $charge['tn_own'];
/*else if($south==1) $shipping = $own_items['weight'] * ($shipping + $charge['south_own']);*/
//{{New code begin
else if($south==1){
$charge = $this->db->query("select * from courier_charges where ".$own_items_weight_multiplier." between fromKg and toKg")->getRowArray();
$shipping = $shipping + $charge['south_own']);
}
//New code end}}
else if($south==2) $shipping = $shipping + $charge['roi'];
}
$diffWeight = 0; $extraWghtCost = 0;
if($not_own_items['cnt'] > 0) {
$Ocharge = $this->db->query("select * from courier_charges where ".$not_own_items['weight']." between fromKg and toKg")->getRowArray();
/*if($south==0) $Oshipping = $not_own_items['weight'] * ($Oshipping + $Ocharge['tn_other']);*/
//{{New code begin
if($south==0) $Oshipping = $Oshipping + $Ocharge['tn_other']);
//New code end}}
else if($south==1) {
/*$totWeight = $own_items['weight'] + $not_own_items['weight'];
if(ceil($totWeight) > $totWeight) {
$diffWeight = ceil($totWeight) - $totWeight;
$extraWghtCost = $diffWeight * 20;
}
$Oshipping = $not_own_items['weight'] * ($Oshipping + $Ocharge['south_other']);
*/
//{{New code begin
$Ocharge = $this->db->query("select * from courier_charges where ".$own_items_weight_multiplier." between fromKg and toKg")->getRowArray();
$Oshipping = $Oshipping + $Ocharge['south_other']);
//New code end}}
}
else if($south==2) $Oshipping = $Oshipping + $Ocharge['roi'];
}
//Extra weight calculation
/*$totWeight = $own_items['weight'] + $not_own_items['weight'];
if(ceil($totWeight) > $totWeight) {
$diffWeight = ceil($totWeight) - $totWeight;
$extraWghtCost = $diffWeight * 20;
}*/
//{{New code begin
$totWeight= ceil($own_items_fractional_weight+$not_own_items_fractional_weight);
$extraWghtCost =$totWeight*20;
//New code end}}
//echo $extraWghtCost; exit;
$this->db->table('cart_temp_shipping')->where('unique_id',$unique_id)->delete();
$session_data = session()->get('user_in');
$insert['cusId'] = $session_data->cusId;
$insert['unique_id'] = $unique_id;
$insert['totalWeight'] = $own_items['weight'] + $not_own_items['weight'];
$insert['shipping'] = $shipping + $Oshipping + $extraWghtCost;
$insert['grandTotal'] = $own_items['total'] + $not_own_items['total'] + $shipping + $Oshipping + $extraWghtCost;
$this->db->table('cart_temp_shipping')->insert($insert);
echo 'true';
} else echo 'false';
}
?>