/
Calculation11WithOutProduct.php
439 lines (437 loc) · 20.8 KB
/
Calculation11WithOutProduct.php
1
<?phprequire_once 'Inf_Model.php';Class Calculation extends Inf_Model { public $fromDateCal; public $toDateCal; public $user_arr; public $user_detail_arr; public $user_detail_id_arr; public $obj_module; public $upline_id_arr; public $upline_user_arr; public function __construct() { $this->fromDateCal = null; $this->toDateCal = null; $this->user_arr = array(); $this->user_detail_arr = array(); $this->user_detail_id_arr = array(); $this->upline_id_arr = array(); $this->upline_user_arr=array(); } public function getConfigurationEntries() { require_once 'Settings.php'; $obj_sett = new Settings(); $arr_config = $obj_sett->getSettings(); return $arr_config; } public function calculateLegCount($id, $father_id, $child_position,$referal_id ,$from_id = '') { $obj_arr = $this->getConfigurationEntries(); $pair_price = $obj_arr["pair_price"]; $ceiling_user = $obj_arr['pair_ceiling']; $tds_db = $obj_arr["tds"]; $service_charge_db = $obj_arr["service_charge"]; $week_start_date_db = $obj_arr["startDate"]; $week_end_date_db = $obj_arr["endDate"]; $payout_release = $obj_arr["payout_release"]; if ($payout_release == "month") { $current_date = date("Y-m-d"); $month_arr = $this->getCurrentMonthStartEndDates($current_date); $from_date = $month_arr["month_first_date"]; $to_date = $month_arr["month_end_date"]; } else if ($payout_release == "week") { $week_arr = $this->getWeekStartEndDates($week_start_date_db, $week_end_date_db); $from_date = $week_arr["startDate"]; $to_date = $week_arr["endDate"]; } else if ($payout_release == "daily") { $from_date = date("Y-m-d") . " 00:00:00"; $to_date = date("Y-m-d") . " 23:59:59"; } else { $from_date = date("Y-m-d") . " 00:00:00"; $to_date = date("Y-m-d") . " 23:59:59"; } $this->getAllUplineId($father_id, 0, $child_position); $total_len = count($this->upline_id_arr); // Pair Amount is incrementing + 1 $pair_count = 1; $this->updateAllUpline($pair_count); for ($i = 0; $i < $total_len; $i++) { $user_id = $this->upline_id_arr["detail$i"]["id"]; $left_leg = $this->upline_id_arr["detail$i"]["left_carry"]; $right_leg = $this->upline_id_arr["detail$i"]["right_carry"]; $user_pair = $this->getUserPair($user_id, $left_leg, $right_leg, $ceiling_user, $from_date, $to_date); if ($user_pair > 0) { $total_amount = $user_pair * $pair_price; $tds_amount = ($total_amount * $tds_db ) / 100; $service_charge = ($total_amount * $service_charge_db ) / 100; $amount_payable = $total_amount - ($tds_amount + $service_charge); $date_of_sub = date("Y-m-d H:i:s"); $this->insertInToLegAmount($user_id, $left_leg, $right_leg, $user_pair, $total_amount, $amount_payable, $tds_amount, $service_charge, $date_of_sub,'leg', $from_id); $left_leg = $left_leg - $user_pair; $right_leg = $right_leg - $user_pair; $this->updateFTIndividualForPair($user_id, $left_leg, $right_leg, $user_pair); } else { $balance = min($left_leg, $right_leg); if ($balance > 0) { $left_leg = $left_leg - $balance; $right_leg = $right_leg - $balance; $this->updateFTIndividualForPair($user_id, $left_leg, $right_leg, 0); } } } } public function updateBalanceAmount($user_id, $total_amount) { $this->db->set('balance_amount', "balance_amount + $total_amount", FALSE); $this->db->where('user_id', $user_id); $this->db->limit(1); $res = $this->db->update('user_balance_amount'); return $res; } public function getUserPair($user_id, $left_leg, $right_leg, $ceiling_user, $from_date, $to_date) { $pair = 0; if ($left_leg < $right_leg) { $pair = $left_leg; } else { $pair = $right_leg; } /* -------- Check here for pair ceiling --------- */ $week_total = $this->getWeekTotal($user_id, $from_date, $to_date); $week_added_total = $week_total + $pair; if ($week_added_total > $ceiling_user) { if ($week_total >= $ceiling_user) { $pair = 0; } else { $pair = $ceiling_user - $week_total; } } return $pair; } public function updateAllUpline($pair_count) { $user_left_id = array(); $user_right_id = array(); $total_len = count($this->upline_id_arr); for ($i = 0; $i < $total_len; $i++) { $user_id = $this->upline_id_arr["detail$i"]["id"]; $position = $this->upline_id_arr["detail$i"]["child_position"]; if ($position == "L") { $user_left_id[] = $user_id; $this->upline_id_arr["detail$i"]["left_carry"] += 1; } else if ($position == "R") { $user_right_id[] = $user_id; $this->upline_id_arr["detail$i"]["right_carry"] += 1; } } $letf_id_count = count($user_left_id); if ($letf_id_count > 0) { if ($letf_id_count >= 5000) { $input_array = $user_left_id; // echo "In Codition Count div:".intval($count/3); $split_arr_left = array_chunk($input_array, intval($letf_id_count / 4)); // print_r($split_arr); for ($i = 0; $i < count($split_arr_left); $i++) { $left_id_qry = $this->createQuery($split_arr_left[$i]); $this->db->set('total_left_carry', "total_left_carry + $pair_count", FALSE); $this->db->where($left_id_qry); $result = $this->db->update('ft_individual'); $active = $this->session->userdata('active'); if ($active == 'yes') { $this->db->set('total_left_carry', "total_left_carry + $pair_count", FALSE); $this->db->set('total_left_count', "total_left_count + $pair_count", FALSE); $this->db->set('total_active', "total_active + $pair_count", FALSE); $this->db->where($left_id_qry); } else { $this->db->set('total_left_carry', "total_left_carry + $pair_count", FALSE); $this->db->set('total_left_count', "total_left_count + $pair_count", FALSE); $this->db->set('total_inactive', "total_inactive + $pair_count", FALSE); $this->db->where($left_id_qry); } $result = $this->db->update('leg_details'); //end editing } } else { $left_id_qry = $this->createQuery($user_left_id); $this->db->set('total_left_carry', "total_left_carry + $pair_count", FALSE); $this->db->where($left_id_qry); $result = $this->db->update('ft_individual'); //edited by m.ali.km $active = $this->session->userdata('active'); if ($active == 'yes') { $this->db->set('total_left_carry', "total_left_carry + $pair_count", FALSE); $this->db->set('total_left_count', "total_left_count + $pair_count", FALSE); $this->db->set('total_active', "total_active + $pair_count", FALSE); $this->db->where($left_id_qry); } else { $this->db->set('total_left_carry', "total_left_carry + $pair_count", FALSE); $this->db->set('total_left_count', "total_left_count + $pair_count", FALSE); $this->db->set('total_inactive', "total_inactive + $pair_count", FALSE); $this->db->where($left_id_qry); } $result = $this->db->update('leg_details'); //end editing } } $right_id_count = count($user_right_id); if ($right_id_count > 0) { if ($right_id_count >= 5000) { $input_array = $user_right_id; // echo "In Codition Count div:".intval($count/3); $split_arr_right = array_chunk($input_array, intval($right_id_count / 4)); for ($i = 0; $i < count($split_arr_right); $i++) { $right_id_qry = $this->createQuery($split_arr_right[$i]); $this->db->set('total_right_carry', "total_right_carry + $pair_count", FALSE); $this->db->where($right_id_qry); $result = $this->db->update('ft_individual'); $active = $this->session->userdata('active'); if ($active == 'yes') { $this->db->set('total_right_carry', "total_right_carry + $pair_count", FALSE); $this->db->set('total_right_count', "total_right_count + $pair_count", FALSE); $this->db->set('total_active', "total_active + $pair_count", FALSE); $this->db->where($right_id_qry); } else { $this->db->set('total_right_carry', "total_right_carry + $pair_count", FALSE); $this->db->set('total_right_count', "total_right_count + $pair_count", FALSE); $this->db->set('total_inactive', "total_inactive + $pair_count", FALSE); $this->db->where($right_id_qry); } $result = $this->db->update('leg_details'); } } else { $right_id_qry = $this->createQuery($user_right_id); $this->db->set('total_right_carry', "total_right_carry + $pair_count", FALSE); $this->db->where($right_id_qry); $result = $this->db->update('ft_individual'); $active = $this->session->userdata('active'); if ($active == 'yes') { $this->db->set('total_right_carry', "total_right_carry + $pair_count", FALSE); $this->db->set('total_right_count', "total_right_count + $pair_count", FALSE); $this->db->set('total_active', "total_active + $pair_count", FALSE); $this->db->where($right_id_qry); } else { $this->db->set('total_right_carry', "total_right_carry + $pair_count", FALSE); $this->db->set('total_right_count', "total_right_count + $pair_count", FALSE); $this->db->set('total_inactive', "total_inactive + $pair_count", FALSE); $this->db->where($right_id_qry); } $result = $this->db->update('leg_details'); } } } public function createQuery($all_id) { $len = count($all_id); for ($i = 0; $i < $len; $i++) { if ($i == 0) $qry = "id = $all_id[$i]"; else $qry .= " OR id = $all_id[$i]"; } return $qry; } public function getAllUplineId($id, $i, $child_position = '') { $this->db->select('father_id,total_leg,total_left_carry,total_right_carry,product_id,position'); $this->db->from('ft_individual'); $this->db->where('id', $id); $query = $this->db->get(); $cnt = $query->num_rows(); if ($cnt > 0) { foreach ($query->result() as $row) { $father_id = $row->father_id; $this->upline_id_arr["detail$i"]["id"] = $id; $this->upline_id_arr["detail$i"]["position"] = $row->position; if ($i == 0) { $this->upline_id_arr["detail$i"]["child_position"] = $child_position; } else { $k = $i - 1; $this->upline_id_arr["detail$i"]["child_position"] = $this->upline_id_arr["detail$k"]["position"]; } $this->upline_id_arr["detail$i"]["left_carry"] = $row->total_left_carry; $this->upline_id_arr["detail$i"]["right_carry"] = $row->total_right_carry; $this->upline_id_arr["detail$i"]["product_id"] = $row->product_id; $i = $i + 1; } $this->getAllUplineId($father_id, $i); } return; } public function insertInToLegAmount($user_id, $totalLeft, $totalRight, $totalLeg, $total_amount, $amount_payable, $tds_amount, $service_charge, $date_of_sub, $amount_type = "leg", $from_id = '') { $date_of_sub = strtotime($date_of_sub); $date_of_sub += 1; $date_of_sub = date("Y-m-d H:i:s", $date_of_sub); $obj_arr = $this->getConfigurationEntries(); $payout_release = $obj_arr["payout_release"]; if ($payout_release == 'daily') $nextpayoutreleasedate = date("Y-m-d H:i:s"); else $nextpayoutreleasedate = $this->selectpayoutrelease($date_of_sub); $this->db->set('user_id', $user_id); $this->db->set('from_id', $from_id); $this->db->set('left_leg', $totalLeft); $this->db->set('right_leg', $totalRight); $this->db->set('total_leg', $totalLeg); $this->db->set('total_amount', $total_amount); $this->db->set('amount_payable', $amount_payable); $this->db->set('tds', $tds_amount); $this->db->set('service_charge', $service_charge); $this->db->set('date_of_submission', $date_of_sub); //$this->db->set('released_date', $nextpayoutreleasedate); $this->db->set('amount_type', $amount_type); $result = $this->db->insert('leg_amount'); $result1 = $this->updateBalanceAmount($user_id, $amount_payable); return $result; } public function getWeekStartEndDates($startDate, $endDate) { if (date("l") == $startDate) $this_sat = date("Y-m-d 23:59:59"); else { $a = strtotime("next $startDate"); $this_sat = date("Y-m-d 23:59:59", $a); } if (date("l") == $endDate) $last_sat = date("Y-m-d 00:00:00"); else { $a = strtotime("last $endDate"); $last_sat = date("Y-m-d 00:00:00", $a); } $arr['startDate'] = $last_sat; $arr['endDate'] = $this_sat; return $arr; } public function getCurrentMonthStartEndDates($current_date) { $start_date = ''; $end_date = ''; $date = $current_date; list($yr, $mo, $da) = explode('-', $date); $start_date = date('Y-m-d', mktime(0, 0, 0, $mo, 1, $yr)); $i = 2; list($yr, $mo, $da) = explode('-', $start_date); while (date('d', mktime(0, 0, 0, $mo, $i, $yr)) > 1) { $end_date = date('Y-m-d', mktime(0, 0, 0, $mo, $i, $yr)); $i++; } $ret_arr["month_first_date"] = $start_date; $ret_arr["month_end_date"] = $end_date; return $ret_arr; } function getWeekTotal($user_id, $from_date, $to_date) { $total_ceiling = NULL; $this->db->select_sum('total_leg', 'tot_amt'); $this->db->where('user_id', $user_id); $where = "date_of_submission between '$from_date' and '$to_date'"; $this->db->where($where); $this->db->from('leg_amount'); $query = $this->db->get(); foreach ($query->result() as $row) { $total_ceiling = $row->tot_amt; } return $total_ceiling; } public function updateFTIndividualForPair($user_id, $left_leg, $right_leg, $user_pair) { $this->db->set('total_left_carry', $left_leg, FALSE); $this->db->set('total_right_carry', $right_leg, FALSE); $this->db->set('total_leg', 'total_leg +' . $user_pair, FALSE); $this->db->where('id', $user_id); $this->db->limit(1); $result = $this->db->update('ft_individual'); $data = array( 'total_left_carry' => $left_leg, 'total_right_carry' => $right_leg ); $this->db->where('id', $user_id); $this->db->limit(1); $result1 = $this->db->update('leg_details', $data); return $result; } public function selectpayoutrelease($date_of_sub) { $nextpayoutreleasedate = ""; $this->db->select_min('release_date'); $this->db->from("payout_release_date"); $this->db->where('release_date >=', $date_of_sub); $this->db->limit(1); $query = $this->db->get(); foreach ($query->result() as $row) { $nextpayoutreleasedate = $row->release_date; } return $nextpayoutreleasedate; } public function getReferalId($user_id) { $referal_id = ""; $this->db->select('user_details_ref_user_id'); $this->db->from("user_details"); $this->db->where('user_detail_refid ', $user_id); $query = $this->db->get(); foreach ($query->result() as $row) { $referal_id = $row->user_details_ref_user_id; } return $referal_id; } public function insertReferalAmount($referal_id, $referal_amount, $from_id = '') { $res = ""; if ($referal_id != "") { $obj_arr = $this->getConfigurationEntries(); $service_charge_db = $obj_arr["service_charge"]; $tds_db = $obj_arr["tds"]; $amount_type = "referral"; $total_amount = $referal_amount; $tds_amount = ($total_amount * $tds_db ) / 100; $service_charge = ($total_amount * $service_charge_db ) / 100; $amount_payable = $total_amount - ($tds_amount + $service_charge); $date_of_sub = date("Y-m-d H:i:s"); $res = $this->insertInToLegAmount($referal_id, 0, 0, 0, $total_amount, $amount_payable, $tds_amount, $service_charge, $date_of_sub, $amount_type, $from_id); } return $res; } /* * ********************added by amrutha******************************************** */ function calculateLevelCommission($from_user, $father_id, $product_id = '') { require_once 'Settings.php'; $this->obj_set = new Settings(); require_once 'product_model.php'; $this->obj_prod = new product_model(); $obj_arr = $this->getConfigurationEntries(); $this->getAllUplineUser($father_id, 0, $obj_arr['depth_ceiling']); $type_levelcomission = $obj_arr["percentorflatlvlcomsn"]; $tds_db = $obj_arr["tds"]; $service_charge_db = $obj_arr["service_charge"]; $product_amount = $this->obj_prod->getProduct($product_id); $total_len = count($this->upline_user_arr); $amount_type = "level_commission"; for ($i = 0; $i < $total_len; $i++) { $user_id = $this->upline_user_arr["detail$i"]["id"]; $level = $this->upline_user_arr["detail$i"]["up_level"]; $date_of_sub = date("Y-m-d H:i:s"); if ($product_amount == "") { $reg_amount = $this->obj_set->getRegAmount(); } else { $reg_amount = $product_amount; } if ($type_levelcomission == "Percentage") { $level_percet = $this->obj_set->getLevelOnePercetage($level); $level_percet = $level_percet / 100; $level_amount = $reg_amount * $level_percet; } else { $level_percet = $this->obj_set->getLevelOnePercetage($level); $level_amount = $level_percet; } $tds_amount = ($level_amount * $tds_db) / 100; $service_charge = ($level_amount * $service_charge_db) / 100; $amount_payable = $level_amount - ($tds_amount + $service_charge); $this->insertInToLegAmount($user_id, 0, 0, 0, $level_amount, $amount_payable, $tds_amount, $service_charge, $date_of_sub, $amount_type, $from_user); } } public function getAllUplineUser($id, $i, $limit) { $this->db->select('father_id,product_id'); $this->db->from('ft_individual_unilevel'); $this->db->where('id', $id); $query = $this->db->get(); $cnt = $query->num_rows(); if ($cnt > 0) { foreach ($query->result() as $row) { $father_id = $row->father_id; $this->upline_user_arr["detail$i"]["id"] = $id; $this->upline_user_arr["detail$i"]["up_level"] = $i + 1; $this->upline_user_arr["detail$i"]["product_id"] = $row->product_id; $i = $i + 1; } if ($i < $limit) { $this->getAllUplineUser($father_id, $i, $limit); } } }}