<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Bremployeewagemodel extends CI_Model {

	function __construct() {
		parent::__construct();
		date_default_timezone_set('Asia/Hong_Kong');
		$this->load->model('Bremployeewagelevelmodel', 'employeewagelevel_model');
	}
	//added employeemodel function
	function getWageAll($shopid) {
		// $this->db->select("brwage.*,bremployee_group.chi_name as rolename,bremployee_income_type.name as incomename, bshop.name as shopname");
		$this->db->select("brwage.*, bshop.name as shopname");
		$this->db->from('brwage');
		// $this->db->join('bremployee_group', 'bemployee.roleId = bremployee_group.id');
		// $this->db->join('bremployee_income_type', 'bemployee.incomeId = bremployee_income_type.id');
		$this->db->join('bshop', 'brwage.shopid = bshop.id');
		// $this->db->where(array('show' => 1));
		$this->db->where(array('brwage.deleted' => 0));
		$this->db->where(array('bshop.id' => $shopid));
		// $this->db->order_by('rank');
		$query = $this->db->get();
		return $query->result();
	}

	function getTheraphistAll($shopid) {
		$this->db->select("bemployee.*,bremployee_group.chi_name as rolename,bremployee_income_type.name as incomename, bshop.name as shopname");
		$this->db->from('bemployee');
		$this->db->join('bremployee_group', 'bemployee.roleId = bremployee_group.id');
		$this->db->join('bremployee_income_type', 'bemployee.incomeId = bremployee_income_type.id');
		$this->db->join('bshop', 'bemployee.shopid = bshop.id');
		// $this->db->where(array('show' => 1));
		$this->db->where(array('bemployee.deleted' => 0));
		// $this->db->where(array('bshop.id' => $shopid));

		//get empolyee shopid added at 2020-10-27
		$this->db->group_start();
		$this->db->where(array('bemployee.shopid' => $shopid));
		$this->db->or_where('bemployee.second_shopid', $shopid);
		$this->db->group_end();
		//end get empolyee shopid added at 2020-10-27

		//roldId 2 or 3 is for Theraphist
		$this->db->where('(roleId = 2 or roleId = 3)');
		$this->db->order_by('bemployee.display_order asc');
		$query = $this->db->get();
		return $query->result();
	}

	function getTheraphistAllWithDate($shopid, $currentDate) {
		$this->db->select("bemployee.*,bremployee_group.chi_name as rolename,bremployee_income_type.name as incomename, bshop.name as shopname");
		$this->db->from('bemployee');
		$this->db->join('bremployee_group', 'bemployee.roleId = bremployee_group.id');
		$this->db->join('bremployee_income_type', 'bemployee.incomeId = bremployee_income_type.id');
		$this->db->join('bshop', 'bemployee.shopid = bshop.id');
		// $this->db->where(array('show' => 1));
		$this->db->where(array('bemployee.deleted' => 0));

		//get empolyee shopid added at 2020-10-27
		$this->db->group_start();
		$this->db->where(array('bemployee.shopid' => $shopid));
		$this->db->or_where('bemployee.second_shopid', $shopid);
		$this->db->group_end();
		//end get empolyee shopid added at 2020-10-27

		//roldId 2 or 3 is for Theraphist
		$this->db->where('(roleId = 2 or roleId = 3)');

		//get empolyee date
		$this->db->group_start();
		$this->db->where('dateofquit', NULL);
		$this->db->or_where('dateofquit >', $currentDate);
		$this->db->group_end();
		//end get empolyee date

		$this->db->order_by('bemployee.display_order asc');

		// $this->db->order_by('rank');
		$query = $this->db->get();
		// print_r($this->db->last_query());
		return $query->result();
	}

	function getTheraphistAllWithinDate($shopid, $startDate, $endDate) {
		//added at 2020-12-22
		$this->db->select("bemployee.*,bremployee_group.chi_name as rolename,bremployee_income_type.name as incomename, bshop.name as shopname");
		$this->db->from('bemployee');
		$this->db->join('bremployee_group', 'bemployee.roleId = bremployee_group.id');
		$this->db->join('bremployee_income_type', 'bemployee.incomeId = bremployee_income_type.id');
		$this->db->join('bshop', 'bemployee.shopid = bshop.id');
		// $this->db->where(array('show' => 1));
		$this->db->where(array('bemployee.deleted' => 0));

		//get empolyee shopid added at 2020-10-27
		$this->db->group_start();
		$this->db->where(array('bemployee.shopid' => $shopid));
		$this->db->or_where('bemployee.second_shopid', $shopid);
		$this->db->group_end();
		//end get empolyee shopid added at 2020-10-27

		//roldId 2 or 3 is for Theraphist
		$this->db->where('(roleId = 2 or roleId = 3)');

		//get empolyee date
		$this->db->group_start();
		$this->db->where('dateofquit', NULL);
		$this->db->or_where('dateofquit <=', $endDate);
		$this->db->group_end();
		//end get empolyee date
		// $this->db->where('dateofentry <=', $endDate);
		$this->db->where('dateofentry <=', $endDate);

		$this->db->order_by('bemployee.display_order asc');

		// $this->db->order_by('rank');
		$query = $this->db->get();
		// print_r($this->db->last_query());
		return $query->result();
	}

	function getConsultantAll($shopid) {
		$this->db->select("bemployee.*,bremployee_group.chi_name as rolename,bremployee_income_type.name as incomename, bshop.name as shopname");
		$this->db->from('bemployee');
		$this->db->join('bremployee_group', 'bemployee.roleId = bremployee_group.id');
		$this->db->join('bremployee_income_type', 'bemployee.incomeId = bremployee_income_type.id');
		$this->db->join('bshop', 'bemployee.shopid = bshop.id');
		// $this->db->where(array('show' => 1));
		$this->db->where(array('bemployee.deleted' => 0));
		$this->db->where(array('bshop.id' => $shopid));
		//roldId 2 or 3 is for Theraphist
		$this->db->where('(roleId = 4 or roleId = 5)');
		// $this->db->order_by('rank');
		$query = $this->db->get();
		return $query->result();
	}
	function getConsultantAllWithinDate($shopid, $startDate, $endDate) {
		//added at 2020-12-21 for calculate salary
		$this->db->select("bemployee.*,bremployee_group.chi_name as rolename,bremployee_income_type.name as incomename, bshop.name as shopname");
		$this->db->from('bemployee');
		$this->db->join('bremployee_group', 'bemployee.roleId = bremployee_group.id');
		$this->db->join('bremployee_income_type', 'bemployee.incomeId = bremployee_income_type.id');
		$this->db->join('bshop', 'bemployee.shopid = bshop.id');
		// $this->db->where(array('show' => 1));
		$this->db->where(array('bemployee.deleted' => 0));

		//get empolyee shopid added at 2020-10-27
		$this->db->group_start();
		$this->db->where(array('bemployee.shopid' => $shopid));
		$this->db->or_where('bemployee.second_shopid', $shopid);
		$this->db->group_end();
		//end get empolyee shopid added at 2020-10-27

		//roldId 4 or 5 is for Consultant
		$this->db->where('(roleId = 4 or roleId = 5)');

		//get empolyee date
		$this->db->group_start();
		$this->db->where('dateofquit', NULL);
		$this->db->or_where('dateofquit <=', $endDate);
		$this->db->group_end();
		//end get empolyee date
		$this->db->where('dateofentry <=', $endDate);

		$this->db->order_by('bemployee.display_order asc');

		// $this->db->order_by('rank');
		$query = $this->db->get();
		// print_r($this->db->last_query());
		return $query->result();
		//end added at 2020-12-21 for calculate salary
	}
	function chiname_exists($chi_name) {
		$this->db->where('LOWER(chi_name)', $chi_name);
		$this->db->where(array('deleted' => 0));
		$query = $this->db->get('brwage');
		if ($query->num_rows() == 1) {
			return false;
		} else {
			return true;
		}
	}
	function chiname_exists_by_id($chi_name, $id, $shopid) {
		$this->db->where('LOWER(chi_name)', $chi_name);
		$this->db->where('id!=', $id);
		$this->db->where('shopid=', $shopid);
		$this->db->where(array('deleted' => 0));
		$query = $this->db->get('brwage');
		if ($query->num_rows() == 1) {
			return false;
		} else {
			return true;
		}
	}

	function insertWage($inputdata) {

		$this->db->trans_begin();

		$data = array(
			'chi_name' => $inputdata['chi_name'],
			'eng_firstname' => $inputdata['eng_firstname'],
			'roleId' => $inputdata['roleId'],
			'shopid' => $inputdata['shopid'],
			'incomeId' => $inputdata['incomeId'],
			'salary' => $inputdata['salary'],
			'remark' => $inputdata['remark'],
			'status' => 1,
			'display' => 0, // 0 stand for display, 1 stand for not display
			'deleted' => 0, //0 stand for not deleted, 1 stand for deleted
			'dateofentry' => $inputdata['dateofentry'],
			'sole_shop_bonus' => $inputdata['sole_shop_bonus'],
			'average_shop_bonus' => $inputdata['average_shop_bonus'],
		);
		// date_default_timezone_set('UTC');
		//set second shop
		if (!empty($inputdata['second_shopid'])) {
			$data['second_shopid'] = $inputdata['second_shopid'];
		} else {
			$this->db->set('second_shopid', null);
		}
		//end set second shop

		$this->db->set('update_time', time(), FALSE);
		$this->db->set('create_time', time(), FALSE);
		$query = $this->db->insert('brwage', $data);

		$wage_id = $this->db->insert_id();

		//insert wage and employee group
		if (!empty($inputdata['employeegroup'])) {
			$this->insertWageemployeegroup($inputdata['employeegroup'], $wage_id);
		}
		//end insert wage and employee group

		// return $query;
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return false;
		} else {
			$this->db->trans_commit();
			// echo "commit transaction";
			return true;
		}

	} //function insertEmployee($inputdata){
	function insertWageemployeegroup($employeegroup, $wage_id) {
		$employeegrouparray = explode(',', $employeegroup);
		foreach ($employeegrouparray as $groupdetail) {

			$data = array(
				'wageid' => $wage_id,
				'roleid' => $groupdetail,
			);

			//end set second shop

			$this->db->set('update_time', time(), FALSE);
			$this->db->set('create_time', time(), FALSE);
			$query = $this->db->insert('brwageemployeegroup', $data);
		}
	}
	function deleteWageemployeegroup($wage_id) {
		$this->db->where('wageid', $wage_id);
		$this->db->delete('brwageemployeegroup');
	}
	function getWageemployeegroup($wage_id) {
		$this->db->select("brwageemployeegroup.*");
		$this->db->from('brwageemployeegroup');

		$this->db->where(array('brwageemployeegroup.deleted' => 0));
		$this->db->where('wageid=', $wage_id);

		// $this->db->order_by('bemployee.display_order asc');

		// $this->db->order_by('rank');
		$query = $this->db->get();
		// print_r($this->db->last_query());
		return $query->result();

	}
	//get wage employeegroupByRole
	function getWageemployeegroupByrole($roleid) {
		$this->db->select("brwageemployeegroup.*,brwage.chi_name,brwage.sole_shop_bonus,brwage.average_shop_bonus");
		$this->db->from('brwageemployeegroup');
		$this->db->join('brwage ', 'brwage.id = brwageemployeegroup.wageid');
		$this->db->where(array('brwage.deleted' => 0));
		$this->db->where('brwageemployeegroup.roleid=', $roleid);
		// $this->db->order_by("sole_shop_bonus", "asc");
		$this->db->order_by("brwage.sole_shop_bonus", "asc");
		$this->db->order_by("brwage.average_shop_bonus", "asc");

		// $this->db->order_by('rank');
		$query = $this->db->get();
		// print_r($this->db->last_query());
		return $this->modifiedresultWithLevel($query->result());
	} //function getWageemployeegroupByrole($role){

	function modifiedresultWithLevel($result) {
		$modifiedarray = array();
		foreach ($result as $resultRow) {
			$modifiedrow = array();
			$modifiedrow['id'] = $resultRow->id;
			$modifiedrow['wageid'] = $resultRow->wageid;
			$modifiedrow['roleid'] = $resultRow->roleid;
			$modifiedrow['chi_name'] = $resultRow->chi_name;
			$modifiedrow['sole_shop_bonus'] = $resultRow->sole_shop_bonus;
			$modifiedrow['average_shop_bonus'] = $resultRow->average_shop_bonus;
			$wageLevelArray = array();
			$wagelist = $this->employeewagelevel_model->getWageLevelAll($resultRow->wageid);
			// var_dump($wagelist);
			foreach ($wagelist as $wagelistRow) {
				$wageleveldetails = array();
				$wageleveldetails['id'] = $wagelistRow->id;
				$wageleveldetails['wageid'] = $wagelistRow->wageid;
				$wageleveldetails['sales_amount'] = $wagelistRow->sales_amount;
				$wageleveldetails['precentage'] = $wagelistRow->precentage;
				$wageleveldetails['job_pay'] = $wagelistRow->job_pay;
				array_push($wageLevelArray, $wageleveldetails);
			} //foreach ($wagelist as $wagelistRow) {
			$modifiedrow['wageLevelArray'] = $wageLevelArray;
			array_push($modifiedarray, $modifiedrow);
		}
		return $modifiedarray;
	} //function modifiedresultWithLevel($result) {
	function getSalesWageDetails($totalSalesPerMonth, $totalMonthWork, $wageEmployeeGroupPayList, $roleid, $netShopSales, $shopTotalSalesPerMonth, $shopSalesBase, $numOfStaff) {

		//there are three method
		$salesWageList = array();
		foreach ($wageEmployeeGroupPayList as $wageEmployeeGroupPayRow) {
			$salesWageDetails = array();
			// echo "sole_shop_bonus";
			// echo $wageEmployeeGroupPayRow['sole_shop_bonus'];
			// echo "average_shop_bonus";
			// echo $wageEmployeeGroupPayRow['average_shop_bonus'];
			// echo "<br>";
			if ($wageEmployeeGroupPayRow['sole_shop_bonus'] == 0 && $wageEmployeeGroupPayRow['average_shop_bonus'] == 0) {
				$salesWageDetails = $this->firstCalculateSalary($wageEmployeeGroupPayRow, $totalSalesPerMonth, $totalMonthWork, $roleid);
			}
			if ($wageEmployeeGroupPayRow['sole_shop_bonus'] == 1) {
				//Manager Sole Bonus
				$salesWageDetails = $this->secondCalculateManagerSoleBonus($wageEmployeeGroupPayRow, $netShopSales, $shopTotalSalesPerMonth, $shopSalesBase, $roleid);
			}
			if ($wageEmployeeGroupPayRow['average_shop_bonus'] == 1) {
				//Staff Share Bonus
				$salesWageDetails = $this->thirdCalculateShopAverageBonus($wageEmployeeGroupPayRow, $netShopSales, $shopTotalSalesPerMonth, $shopSalesBase, $numOfStaff);
			}
			array_push($salesWageList, $salesWageDetails);
		} //foreach ($wageEmployeeGroupPayList => $wageEmployeeGroupPayRow) {

		return $salesWageList;

	} //function getSalesWageDetails($totalSalesPerMonth, $totalMonthWork, $wageEmployeeGroupList) {
	//end group by wage
	//
	function firstCalculateSalary($wageEmployeeGroupPayRow, $totalSalesPerMonth, $totalMonthWork, $roldid) {
		$levelid = '';
		$levelSales_amount = 0;
		$levelprecentage = 0;
		$job_pay = 0;
		foreach ($wageEmployeeGroupPayRow['wageLevelArray'] as $wageLevelRow) {
			//defaule level
			if (empty($levelid)) {
				$levelid = $wageLevelRow['id'];
				$levelSales_amount = $wageLevelRow['sales_amount'];
				$levelprecentage = $wageLevelRow['precentage'];
				$job_pay = $wageLevelRow['job_pay'];
			}

			//end default level

			if ($totalSalesPerMonth >= $wageLevelRow['sales_amount']) {
				// echo "totalSalesPerMonth";
				// echo $totalSalesPerMonth;
				// echo "sales_amount";
				// echo $wageLevelRow['sales_amount'];
				// echo "<br>";
				$levelid = $wageLevelRow['id'];
				$levelSales_amount = $wageLevelRow['sales_amount'];
				$levelprecentage = $wageLevelRow['precentage'];
				$job_pay = $wageLevelRow['job_pay'];
			}
		}
		$calculateDetails = array();
		$mothlysalecommision = ($totalSalesPerMonth * ($levelprecentage / 100)) + ($totalMonthWork * $job_pay);
		$calculateDetails['list'] = "";
		if (!empty($levelprecentage)) {
			$calculateDetails['list'] = $calculateDetails['list'] . "開單 " . $totalSalesPerMonth . ' X ' . $levelprecentage . '%';
		}
		if (!empty($job_pay)) {
			$calculateDetails['list'] = $calculateDetails['list'] . '+' . '工數 ' . $totalMonthWork . ' X ' . ' 消工 ' . $job_pay;
		}
		$calculateDetails['list'] = $calculateDetails['list'] . '=' . $mothlysalecommision;
		$calculateDetails['title'] = "拆帳";
		$calculateDetails['levelSales_amount'] = $levelSales_amount;
		$calculateDetails['levelprecentage'] = $levelprecentage;
		$calculateDetails['job_pay'] = $job_pay;
		$calculateDetails['mothlysalecommision'] = $mothlysalecommision;

		return $calculateDetails;
	} //function firstCalculateSalary($wageEmployeeGroupPayRow, $totalSalesPerMonth, $totalMonthWork) {
	function secondCalculateManagerSoleBonus($wageEmployeeGroupPayRow, $netShopSales, $shopTotalSalesPerMonth, $shopSalesBase, $roleid) {
		$levelid = '';
		$levelSales_amount = 0;
		$levelprecentage = 0;
		$job_pay = 0;
		foreach ($wageEmployeeGroupPayRow['wageLevelArray'] as $wageLevelRow) {
			//defaule level
			if (empty($levelid)) {
				$levelid = $wageLevelRow['id'];
				$levelSales_amount = $wageLevelRow['sales_amount'];
				$levelprecentage = $wageLevelRow['precentage'];
				$job_pay = $wageLevelRow['job_pay'];
			}

			//end default level

			if ($totalSalesPerMonth >= $wageLevelRow['sales_amount']) {
				// echo "totalSalesPerMonth";
				// echo $totalSalesPerMonth;
				// echo "sales_amount";
				// echo $wageLevelRow['sales_amount'];
				// echo "<br>";
				$levelid = $wageLevelRow['id'];
				$levelSales_amount = $wageLevelRow['sales_amount'];
				$levelprecentage = $wageLevelRow['precentage'];
				$job_pay = $wageLevelRow['job_pay'];
			}
		}
		$calculateDetails = array();
		$managerSoleBonus = $netShopSales * ($levelprecentage / 100);
		$calculateDetails['list'] = "";
		if (!empty($levelprecentage)) {
			$calculateDetails['list'] = $calculateDetails['list'] . "經理全店營利 " . $netShopSales . ' X ' . $levelprecentage . '%';
		}
		// if (!empty($job_pay)) {
		// 	$calculateDetails['list'] = $calculateDetails['list'] . '+' . '工數 ' . $totalMonthWork . ' X ' . ' 消工 ' . $job_pay;
		// }
		$calculateDetails['list'] = $calculateDetails['list'] . '=' . $managerSoleBonus;
		$calculateDetails['title'] = "經理全店營利";
		$calculateDetails['levelSales_amount'] = $levelSales_amount;
		$calculateDetails['levelprecentage'] = $levelprecentage;
		$calculateDetails['job_pay'] = $job_pay;
		$calculateDetails['mothlysalecommision'] = $managerSoleBonus;

		return $calculateDetails;
	} //function secondCalculateManagerSoleBonus($wageEmployeeGroupPayRow, $shopSales, $roleid){
	function thirdCalculateShopAverageBonus($wageEmployeeGroupPayRow, $netShopSales, $shopTotalSalesPerMonth, $shopSalesBase, $numOfStaff) {
		$levelid = '';
		$levelSales_amount = 0;
		$levelprecentage = 0;
		$job_pay = 0;
		foreach ($wageEmployeeGroupPayRow['wageLevelArray'] as $wageLevelRow) {
			//defaule level
			if (empty($levelid)) {
				$levelid = $wageLevelRow['id'];
				$levelSales_amount = $wageLevelRow['sales_amount'];
				$levelprecentage = $wageLevelRow['precentage'];
				$job_pay = $wageLevelRow['job_pay'];
			}

			//end default level

			if ($netShopSales >= $wageLevelRow['sales_amount']) {
				// echo "totalSalesPerMonth";
				// echo $totalSalesPerMonth;
				// echo "sales_amount";
				// echo $wageLevelRow['sales_amount'];
				// echo "<br>";
				$levelid = $wageLevelRow['id'];
				$levelSales_amount = $wageLevelRow['sales_amount'];
				$levelprecentage = $wageLevelRow['precentage'];
				$job_pay = $wageLevelRow['job_pay'];
			}
		}
		$calculateDetails = array();
		$shopAverageBonus = 0;
		if ($numOfStaff != 0) {
			$shopAverageBonus = ($netShopSales * ($levelprecentage / 100)) / $numOfStaff;
		}

		$calculateDetails['list'] = "";
		if (!empty($levelprecentage)) {

			$calculateDetails['list'] = $calculateDetails['list'] . "全店總銷售額" . "$ " . $shopTotalSalesPerMonth . " - " . "全店底數" . "$ " . $shopSalesBase . ' = ' . "全店營利" . "$" . $netShopSales . "<br>";
			$calculateDetails['list'] = $calculateDetails['list'] . "全店平均獎金" . "$ " . $netShopSales . ' X ' . $levelprecentage . '%' . ' / ' . '人數 ' . $numOfStaff;
		}
		// if (!empty($job_pay)) {
		// 	$calculateDetails['list'] = $calculateDetails['list'] . '+' . '工數 ' . $totalMonthWork . ' X ' . ' 消工 ' . $job_pay;
		// }
		$calculateDetails['list'] = $calculateDetails['list'] . '=' . $shopAverageBonus;
		$calculateDetails['title'] = "全店平均獎金";
		$calculateDetails['levelSales_amount'] = $levelSales_amount;
		$calculateDetails['levelprecentage'] = $levelprecentage;
		$calculateDetails['job_pay'] = $job_pay;
		$calculateDetails['mothlysalecommision'] = $shopAverageBonus;

		return $calculateDetails;
	} //function firstCalculateSalary($wageEmployeeGroupPayRow, $totalSalesPerMonth, $totalMonthWork) {
	function updateWage($inputdata) {

		$this->db->trans_begin();

		$data = array(
			'chi_name' => $inputdata['chi_name'],
			// 'eng_firstname' => $inputdata['eng_firstname'],
			// 'roleId' => $inputdata['roleId'],
			// 'shopid' => $inputdata['shopid'],
			// 'incomeId' => $inputdata['incomeId'],
			// 'salary' => $inputdata['salary'],
			'remark' => $inputdata['remark'],
			'status' => 1,
			'display' => 0, // 0 stand for display, 1 stand for not display
			'deleted' => 0, //0 stand for not deleted, 1 stand for deleted
			// 'dateofquit' => $inputdata['dateofquit'],
			// 'changepic' => $inputdata['changepic'],
			'sole_shop_bonus' => $inputdata['sole_shop_bonus'],
			'average_shop_bonus' => $inputdata['average_shop_bonus'],

		);
		// if (!empty($inputdata['dateofentry'])) {
		// 	$data['dateofentry'] = $inputdata['dateofentry'];
		// } else {
		// 	$this->db->set('dateofentry', null);
		// }
		// if (!empty($inputdata['dateofquit'])) {
		// 	$data['dateofquit'] = $inputdata['dateofquit'];
		// } else {
		// 	$this->db->set('dateofquit', null);
		// }

		// //set second shop
		// if (!empty($inputdata['second_shopid'])) {
		// 	$data['second_shopid'] = $inputdata['second_shopid'];
		// } else {
		// 	$this->db->set('second_shopid', null);
		// }
		//end set second shop

		// if (!empty($inputdata['password'])) {
		// 	$data['password'] = $inputdata['password'];
		// 	// array_push($data, ['password' =>$inputdata['password']]);
		// }
		// date_default_timezone_set('UTC');
		$this->db->set('update_time', time(), FALSE);
		$this->db->where('id', $inputdata['id']);
		$query = $this->db->update('brwage', $data);

		//update booking

		//clear wage and employee group
		$this->deleteWageemployeegroup($inputdata['id']);
		//end clear wage and employee group

		//insert wage and employee group
		if (!empty($inputdata['employeegroup'])) {
			$this->insertWageemployeegroup($inputdata['employeegroup'], $inputdata['id']);
		}
		//end insert wage and employee group

		// $inputdata['roleId']

		// rold id 7=boss
		// rold id 6=admin
		// rold id 5=consultant/manager  ->can change shop but only view booking
		// rold id 4=consultant          ->cannot change shop
		// rold id 3=therapist/manager  ->can change shop
		// role id 2=therapist          ->cannot change shop
		//role id 1=manager             ->can change shop
		if ($this->session->userdata('roleId') == 3 || $this->session->userdata('roleId') == 2) {

			//update therapist booking
			if (!empty($inputdata['changepic'])) {

				// $this->db->set('pic_1', $inputdata['changepic'], FALSE);
				// $this->db->where('startdate>=', $inputdata['dateofquit']);
				// $this->db->where('pic_1', $inputdata['id']);
				// $query = $this->db->update('bemployee', $data);

				// $this->db->set('pic_2', $inputdata['changepic'], FALSE);
				// $this->db->where('startdate>=', $inputdata['dateofquit']);
				// $this->db->where('pic_2', $inputdata['id']);
				// $query = $this->db->update('bemployee', $data);

			} //if(!empty($inputdata['changepic'])){

			//add cron job and logging
			//end update therapist booking

		} //end if($this->session->userdata('roleId')==3 || $this->session->userdata('roleId')==2){

		if ($this->session->userdata('roleId') == 4 || $this->session->userdata('roleId') == 5) {
			//update consultant customer

			//end update consultant customer

		} //end if($this->session->userdata('roleId')==3 || $this->session->userdata('roleId')==2){

		//if($this->session->userdata('roleId')==7 || $this->session->userdata('roleId')==6 || $this->session->userdata('roleId')==5 || $this->session->userdata('roleId')==3 || $this->session->userdata('roleId')==1){

		//end update booking

		// return $query;
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return false;
		} else {
			$this->db->trans_commit();
			// echo "commit transaction";
			return true;
		}
	}

	function getWage($id) {
		$this->db->select("brwage.*, bshop.name as shopname");
		$this->db->from('brwage');
		// $this->db->join('bremployee_income_type', 'bemployee.incomeId = bremployee_income_type.id');
		$this->db->join('bshop', 'brwage.shopid = bshop.id');
		// $this->db->where(array('show' => 1));
		$this->db->where(array('brwage.deleted' => 0, 'brwage.id' => $id));
		// $this->db->order_by('rank');
		$query = $this->db->get();
		// return $query->result();
		return $query->row();
	} //function getEmployee($id){
	function getTheraphistById($shopid, $userid) {
		$this->db->select("bemployee.*,bremployee_group.chi_name as rolename,bremployee_income_type.name as incomename, bshop.name as shopname");
		$this->db->from('bemployee');
		$this->db->join('bremployee_group', 'bemployee.roleId = bremployee_group.id');
		$this->db->join('bremployee_income_type', 'bemployee.incomeId = bremployee_income_type.id');
		$this->db->join('bshop', 'bemployee.shopid = bshop.id');
		// $this->db->where(array('show' => 1));
		$this->db->where(array('bemployee.deleted' => 0));
		//$this->db->where(array('bshop.id' => $shopid));

		//get empolyee shopid added at 2020-10-27
		$this->db->group_start();
		$this->db->where(array('bemployee.shopid' => $shopid));
		$this->db->or_where('bemployee.second_shopid', $shopid);
		$this->db->group_end();
		//end get empolyee shopid added at 2020-10-27

		$this->db->where(array('bemployee.id' => $userid));
		$this->db->where(array('bemployee.deleted' => 0));
		//roldId 2 or 3 is for Theraphist
		$this->db->where('(roleId = 2 or roleId = 3)');
		// $this->db->order_by('rank');
		$query = $this->db->get();
		return $query->result();
	}

	function deleteWage($id) {
		$this->db->trans_begin();
		$data = array(

		);
		$this->db->set('deleted', 1, FALSE);
		$this->db->where('id', $id);
		$query = $this->db->update('brwage', $data);

		//clear br wage employee group
		$this->deletbrwageemployeegroup($id);

		//clear br wage level
		$this->deletbrwagelevel($id);

		// return $query;
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return false;
		} else {
			$this->db->trans_commit();
			// echo "commit transaction";
			return true;
		}
	} //function deleteGroup($id){
	function deletbrwageemployeegroup($wageid) {
		$data = array(

		);
		$this->db->set('deleted', 1, FALSE);
		$this->db->where('wageid', $wageid);
		$query = $this->db->update('brwageemployeegroup', $data);
	}
	function deletbrwagelevel($wageid) {
		$data = array(

		);
		$this->db->set('deleted', 1, FALSE);
		$this->db->where('wageid', $wageid);
		$query = $this->db->update('brwagelevel', $data);
	}
	function getUserDetail($id) {
		$this->db->from('bemployee');
		$this->db->where('id', $id);

		$row = $this->db->get()->row();
		return $row;
	}
	function updateStatus($data) {

		$data2 = array(
			'status' => $data['status'],
		);
		date_default_timezone_set('UTC');

		$this->db->set('update_time', time(), FALSE);

		$this->db->where('id', $data['id']);

		$query = $this->db->update('bemployee', $data2);
		return $query;
	}
	function getAllEmpolyeeName() {
		$this->db->select("chi_name,id");
		$this->db->from('bemployee');
		$query = $this->db->get();
		return $query->result();
	}

	function getEmpolyeeName($id) {

		$this->db->from('bemployee');
		$this->db->where('id', $id);

		$chi_name = $this->db->get()->row()->chi_name;
		return $chi_name;

	}
	function checkRole($username, $password) {
		$checkusername = $username;
		$checkpassword = sha1(trim($password));
		$this->db->where('username', $checkusername);
		$this->db->where('password', $checkpassword);
		$this->db->where('deleted', 0);
		$query = $this->db->get('bemployee');
		if ($query->num_rows() >= 1) {
			return $query->row()->roleId;
		} else {
			return "no data";
		}
	}
	//end added employeemodel function

	//added shopmodel function
	function getShopAll() {

		$this->db->from('bshop');
		// $this->db->where(array('show' => 1));
		$this->db->where(array('deleted' => 0));
		// $this->db->order_by('rank');
		$query = $this->db->get();
		return $query->result();
	}
	function shopcode_exists($code) {
		$this->db->where('LOWER(shortcode)', $code);
		$query = $this->db->get('bshop');
		if ($query->num_rows() == 1) {
			return false;
		} else {
			return true;
		}
	}
	function insertShop($inputdata) {
		$data = array(
			'shortcode' => $inputdata['shortcode'],
			'code' => $inputdata['code'],
			'name' => $inputdata['name'],
			'status' => 1,
			'display' => 0, // 0 stand for display, 1 stand for not display
			'deleted' => 0, //0 stand for not deleted, 1 stand for deleted
		);
		date_default_timezone_set('UTC');
		$this->db->set('update_time', time(), FALSE);
		$this->db->set('create_time', time(), FALSE);
		$query = $this->db->insert('bshop', $data);

		return $query;
	}
	function getSecondShopid($id) {

		$this->db->from('bemployee');
		$this->db->where('id', $id);

		$second_shopid = $this->db->get()->row()->second_shopid;
		return $second_shopid;

	}
	//end added shopmodel function

	function getBannerAll() {

		$this->db->from('tblFolders');
		// $this->db->where(array('show' => 1));
		// $this->db->order_by('rank');
		$query = $this->db->get();
		return $query->result();
	}

	// function getAdmindata(){
	//     $this->db->select('id,login, fullName, update_time');
	//     $query = $this->db->get('adminUsers');
	//     return $query->result();
	// }
	// function record_count(){
	//     $this->db->select('id');
	//     $this->db->where('level1', '0');
	//     $this->db->where('level2', NULL);
	//     $query = $this->db->get('tblFolders');
	//     return $query->num_rows();
	// }
	// function count_all_folders(){
	//     $this->db->select('id');
	//     $query = $this->db->get('tblFolders');
	//     return $query->num_rows();
	// }
	// function getflodercount($id){
	//     $this->db->select('id');
	//     $this->db->where('level1', $id);
	//     // $this->db->where('level2!=', NULL);
	//     $query = $this->db->get('tblFolders');
	//     return $query->num_rows();
	// }

	// function getfolderdata($perpage,$page){
	//     $this->db->select('id,name,level1, update_time');
	//     $offset=$page* $perpage;
	//     $this->db->where('level1', 0);
	//     $this->db->limit($perpage, $offset);
	//     $query = $this->db->get('tblFolders');
	//     return $query->result();
	// }

	// function getlevel1data($id)
	// {
	//     $this->db->select('id,name,level1, update_time');
	//     $this->db->where('level1', $id);
	//     $this->db->where('level2', NULL);
	//     $query = $this->db->get('tblFolders');
	//     return $query->result();
	// }
	// function getlevel2data($id)
	// {
	//     $this->db->select('id,name,level1, update_time');
	//     $this->db->where('level2', $id);
	//     $this->db->where('level3', NULL);
	//     $query = $this->db->get('tblFolders');
	//     return $query->result();
	// }
	// function getlevel3data($id){
	//     $this->db->select('id,name,level1, update_time');
	//     $this->db->where('level3', $id);
	//     $this->db->where('level4', NULL);
	//     $query = $this->db->get('tblFolders');
	//     return $query->result();
	// }
	// function getlevel4data($id){
	//     $this->db->select('id,name,level1, update_time');
	//     $this->db->where('level4', $id);
	//     $this->db->where('level5', NULL);
	//     $query = $this->db->get('tblFolders');
	//     return $query->result();
	// }

	// //get all folder data for rightend
	// function getallfolderdata(){
	//     $this->db->select('id,name,level1,level2,level3,level4, update_time');
	//     $query = $this->db->get('tblFolders');
	//     return $query->result();
	// }
	// function getalllevel1datainclude($id)
	// {
	//     $this->db->select('id,name,level1,level2,level3,level4, update_time');
	//     $this->db->where('level1', $id);
	//     $query = $this->db->get('tblFolders');
	//     return $query->result();
	// }
	// function getalllevel2datainclude($id)
	// {
	//     $this->db->select('id,name,level1,level2,level3,level4, update_time');
	//     $this->db->where('level2', $id);
	//     $query = $this->db->get('tblFolders');
	//     return $query->result();
	// }
	// function getalllevel3datainclude($id)
	// {
	//     $this->db->select('id,name,level1,level2,level3,level4, update_time');
	//     $this->db->where('level3', $id);
	//     $query = $this->db->get('tblFolders');
	//     return $query->result();
	// }

	// //end get all folder data for rightend

	// function getAdmindateByid($id){
	//     $this->db->select('id,login, companyName,fullName,tel, update_time');
	//     $this->db->where('id', $id);
	//     $query = $this->db->get('tblFolders');
	//     return $query->result();
	// }

	// function getFolderName($id){
	//     $this->db->select('name');
	//     $this->db->where('id', $id);
	//     $query = $this->db->get('tblFolders');
	//     if ($query->num_rows() > 0) {
	//         // return $query->first_row();
	//          $row = $query->row();
	//         return $row->name;
	//     }
	//     return "";
	// }

	// function insertFolder($data){
	//     // 'inputadminname'=>$inputadminname,
	//     //         'inputloginname'=>$inputloginname,
	//     //         'inputPassword'=>$inputPassword,
	//     $data = array(
	//         'name' => $data['inputflodername'] ,
	//         'level1' => '0' ,

	//      );
	//      $this->db->set('update_time', 'NOW()', FALSE);
	//      $query=$this->db->insert('tblFolders', $data);
	//      return $query;
	// }
	// function insertLevelFolder($data){
	//     // 'inputadminname'=>$inputadminname,
	//     //         'inputloginname'=>$inputloginname,
	//     //         'inputPassword'=>$inputPassword,
	//     if($data['addlevel']==1){
	//         $data = array(
	//             'name' => $data['inputflodernamelevel'] ,
	//             'level1' => '0' ,

	//          );
	//     }
	//     if($data['addlevel']==2){
	//         $data = array(
	//             'name' => $data['inputflodernamelevel'] ,
	//             'level1' => $data['addlevelid'] ,

	//          );
	//     }

	//      $this->db->set('update_time', 'NOW()', FALSE);
	//      $query=$this->db->insert('tblFolders', $data);
	//      return $query;
	// }
	// function insertLevelOneFolder($name,$level){
	//     $data = array(
	//         'name' => $name ,
	//         'level1' => '0' ,

	//      );
	//      $this->db->set('update_time', 'NOW()', FALSE);
	//      $query=$this->db->insert('tblFolders', $data);
	//      return $query;

	// }
	// function insertLevelTwoFolder($name,$addlevel,$addlevelid){
	//     $data = array(
	//         'name' => $name ,
	//         'level1' => $addlevelid ,

	//      );
	//      $this->db->set('update_time', 'NOW()', FALSE);
	//      $query=$this->db->insert('tblFolders', $data);
	//      return $query;
	// }
	// function insertLevelThreeFolder($name,$addlevel,$addlevelid,$secondvalues){
	//     $data = array(
	//         'name' => $name ,
	//         'level1' => $addlevelid ,
	//         'level2' => $secondvalues ,
	//      );
	//      $this->db->set('update_time', 'NOW()', FALSE);
	//      $query=$this->db->insert('tblFolders', $data);
	//      return $query;
	// }

	// function insertLevelFourFolder($name,$addlevel,$addlevelid,$thirdvalues){
	//     $secondlevelid=$this->findLevelThreeid($thirdvalues);
	//     $data = array(
	//         'name' => $name ,
	//         'level1' => $addlevelid ,
	//         'level2' => $secondlevelid ,
	//         'level3' => $thirdvalues ,
	//      );
	//      $this->db->set('update_time', 'NOW()', FALSE);
	//      $query=$this->db->insert('tblFolders', $data);
	//      return $query;
	// }
	// function insertLevelMonthWeekFolder($values,$addlevel,$addlevelid,$secondvalues){
	//     $montharray = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct","Nov","Dec");
	//     $weekarray =  array("Week 1", "Week 2", "Week 3", "Week 4", "Week 5");
	//     foreach($montharray as $month){
	//         if($this->folder_levelThreeexists(strtolower($month),$addlevel,$addlevelid,$secondvalues)==true){
	//              $data = array(
	//             'name' =>  $month ,
	//             'level1' => $addlevelid ,
	//             'level2' => $secondvalues ,
	//          );
	//          $this->db->set('update_time', 'NOW()', FALSE);
	//          $query=$this->db->insert('tblFolders', $data);
	//          $lastinsertid = $this->db->insert_id();
	//          foreach ($weekarray as $week) {

	//              $data = array(
	//             'name' =>  $week ,
	//             'level1' => $addlevelid ,
	//             'level2' => $secondvalues ,
	//             'level3' => $lastinsertid ,
	//              );
	//             $this->db->set('update_time', 'NOW()', FALSE);
	//              $query=$this->db->insert('tblFolders', $data);
	//          }

	//         }//if(folder_levelThreeexists($month,$addlevel,$addlevelid,$secondvalues)==false){
	//     }//foreach($montharray as $month){
	//     return $query;
	// }

	// function updateAdmin($input){
	//     // 'inputadminname'=>$inputadminname,
	//     //         'inputloginname'=>$inputloginname,
	//     //         'inputPassword'=>$inputPassword,

	//     if(empty($input['inputPassword'])){
	//         $data = array(
	//             'login' => $input['inputloginname'] ,
	//             'companyName' => $input['inputcompanynameedit'],
	//             'fullName' => $input['inputfullnameedit'],
	//             'tel' => $input['inputteledit']

	//          );
	//     }
	//     else{
	//         $data = array(
	//             'login' => $input['inputloginname'] ,
	//             'companyName' => $input['inputcompanynameedit'],
	//             'fullName' => $input['inputfullnameedit'],
	//             'tel' => $input['inputteledit']  ,
	//             'pwd' => $input['inputPassword']
	//          );
	//     }

	//      $this->db->set('update_time', 'NOW()', FALSE);

	//      $this->db->where('id', $input['id']);
	//      $query=$this->db->update('tblFolders', $data);
	//      return $query;
	// }

	// function deleteAdmin($id){
	//     $this->db->where('id', $id);
	//     $query = $this->db->get('tblFolders');
	//     $row = $query->row();

	//     //check level of delete
	//     $deleteLevel=0;
	//     if($row->level4 ==NULL  && $row->level3!=NULL){
	//         $deleteLevel=4;
	//     }
	//     if($row->level3 ==NULL  && $row->level2!=NULL){
	//         $deleteLevel=3;
	//     }
	//     if($row->level2 ==NULL  && $row->level1!=NULL && $row->level1!=0){
	//         $deleteLevel=2;
	//     }
	//     if( $row->level1==0){
	//         $deleteLevel=1;
	//     }

	//     //end check level of delete
	//     if($deleteLevel==4){
	//         $this->db->where('id', $id);
	//         $query = $this->db->get('tblFolders');
	//         if($query->num_rows() != 0){
	//             $this->db->where('id', $id);
	//             $query=$this->db->delete('tblFolders');
	//         }
	//     }
	//     if($deleteLevel==3){

	//         $this->db->where('level3 ',$id);
	//         $query = $this->db->get('tblFolders');
	//         if($query->num_rows() != 0){
	//             $this->db->where('level3', $id);
	//              $query=$this->db->delete('tblFolders');
	//         }

	//         $this->db->where('id', $id);
	//         $query = $this->db->get('tblFolders');
	//         if($query->num_rows() != 0){
	//             $this->db->where('id', $id);
	//             $query=$this->db->delete('tblFolders');
	//         }
	//     }
	//     if($deleteLevel==2){
	//         $this->db->where('level2 ',$id);
	//         $query = $this->db->get('tblFolders');
	//         if($query->num_rows() != 0){
	//             $this->db->where('level2', $id);
	//             $query=$this->db->delete('tblFolders');
	//         }

	//         $this->db->where('id', $id);
	//         $query = $this->db->get('tblFolders');
	//         if($query->num_rows() != 0){
	//             $this->db->where('id', $id);
	//             $query=$this->db->delete('tblFolders');
	//         }
	//     }
	//     if($deleteLevel==1){
	//         $this->db->where('level1 ',$id);
	//         $query = $this->db->get('tblFolders');
	//         if($query->num_rows() != 0){
	//             $this->db->where('level1', $id);
	//             $query=$this->db->delete('tblFolders');
	//         }

	//         $this->db->where('id', $id);
	//         $query = $this->db->get('tblFolders');
	//         if($query->num_rows() != 0){
	//             $this->db->where('id', $id);
	//             $query=$this->db->delete('tblFolders');
	//         }
	//     }

	//     return $query;
	// }

	// function folder_exists($foldername){
	//     // $this->db->from('adminUsers');
	//     $this->db->where('LOWER(name)', $foldername);
	//     $this->db->where('LOWER(name)', $foldername);
	//     $query = $this->db->get('tblFolders');
	//     if($query->num_rows() == 1){
	//         return false;
	//     } else {
	//         return true;
	//     }
	// }

	// function folder_levelOneexists($foldername){
	//     // $this->db->from('adminUsers');
	//     $this->db->where('LOWER(name)', $foldername);
	//     $this->db->where('level1', '0');
	//     $query = $this->db->get('tblFolders');
	//     if($query->num_rows() == 1){
	//         return false;
	//     } else {
	//         return true;
	//     }
	// }

	// function folder_levelTwoexists($foldername,$level,$id){
	//     $this->db->where('LOWER(name)', $foldername);
	//     $this->db->where('level1', $id);
	//     $query = $this->db->get('tblFolders');
	//     if($query->num_rows() == 1){
	//         return false;
	//     } else {
	//         return true;
	//     }
	// }
	// function folder_levelThreeexists($foldername,$level,$id,$secondllevelid){
	//     $this->db->where('LOWER(name)', $foldername);
	//     $this->db->where('level1', $id);
	//     $this->db->where('level2', $secondllevelid);
	//     $query = $this->db->get('tblFolders');
	//     if($query->num_rows() == 1){
	//         return false;
	//     } else {
	//         return true;
	//     }
	// }
	// function folder_levelFourexists($foldername,$level,$id,$thirdllevelid){
	//     $secondlevelid=$this->findLevelThreeid($thirdllevelid);
	//     $this->db->where('LOWER(name)', $foldername);
	//     $this->db->where('level1', $id);
	//     $this->db->where('level2', $secondlevelid);
	//     $this->db->where('level3', $thirdllevelid);
	//     $query = $this->db->get('tblFolders');
	//     if($query->num_rows() == 1){
	//         return false;
	//     } else {
	//         return true;
	//     }

	// }

	// function folder_levelTwoMonthWeekexists($values,$addlevel,$addlevelid,$secondvalues){
	//     $montharray = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct","Nov","Dec");
	//     foreach($montharray as $month){
	//         $this->db->select("id");
	//         $this->db->where('name', $month);
	//         $this->db->where('level1', $addlevelid);
	//         $this->db->where('level2', NULL);
	//         $query = $this->db->get('tblFolders');
	//         if($query->num_rows() == 1){
	//             return false;
	//             //check week

	//             //end check week

	//         } else {

	//         }
	//     }

	//     return true;
	// }

	// function checkleveltwoid($secondlevelid){
	//     $this->db->where('id', $secondlevelid);
	//     $this->db->where('level1 !=',0);
	//     $this->db->where('level2 ', NULL);
	//     $query = $this->db->get('tblFolders');
	//     if($query->num_rows() == 1){
	//         return false;
	//     } else {
	//         return true;
	//     }
	// }

	// function checklevelthreeid($thirdllevelid){
	//     $this->db->where('id', $thirdllevelid);
	//     $this->db->where('level2 !=', NULL);
	//     $this->db->where('level3 ', NULL);
	//     $query = $this->db->get('tblFolders');
	//     if($query->num_rows() == 1){
	//         return false;
	//     } else {
	//         return true;
	//     }
	// }
	// function findLevelThreeid($thirdllevelid){
	//     $this->db->select("level2");
	//     $this->db->where('id', $thirdllevelid);
	//     $this->db->from('tblFolders');
	//     $query = $this->db->get();
	//     // $ret = $query->row();
	//     // return $ret->level2;
	//     if ($query->num_rows() > 0)
	//     {
	//         $row = $query->row();
	//         return $row->level2;
	//     }
	//     return NULL;
	// }

	// function login_existsbyid($loginname,$id){
	//     // $this->db->from('adminUsers');
	//     $this->db->where('LOWER(login)', $loginname);
	//     $this->db->where('id!=', $id);
	//     $query = $this->db->get('tblFolders');
	//     if($query->num_rows() == 1){
	//         return false;
	//     } else {
	//         return true;
	//     }
	// }

	// function admin_exists($loginname){
	//     // $this->db->from('adminUsers');
	//     $this->db->where('LOWER(fullName)', $loginname);
	//     $query = $this->db->get('tblFolders');
	//     if($query->num_rows() == 1){
	//         return false;
	//     } else {
	//         return true;
	//     }
	// }

	// function admin_existsbyid($loginname,$id){
	//     // $this->db->from('adminUsers');
	//     $this->db->where('LOWER(fullName)', $loginname);
	//     $this->db->where('id!=', $id);
	//     $query = $this->db->get('tblFolders');
	//     if($query->num_rows() == 1){
	//         return false;
	//     } else {
	//         return true;
	//     }
	// }

	// //process login
	// function check(){
	//     $userName =  $this->input->post('userName');
	//     $password =  $this->input->post('password');

	//     $this->db->where('login',$userName);
	//     $this->db->where('pwd',$password);
	//     // $this->db->where('user_active','yes');
	//     return $this->db->get('tblFolders')->row_array();
	// }

	// function processLogin($userName=NULL,$password){

	//     $this->db->select("id,login,fullName");
	//     $whereCondition = $array = array('login' =>$userName,'pwd'=>$password);
	//     $this->db->where($whereCondition);
	//     $this->db->from('tblFolders');
	//     $query = $this->db->get();
	//     return $query;
	// }//function processLogin(){
	// //end process login

	// function buildTopMenuItem()
	// {
	//     // $menu = array();

	//     // $result = $this->getSectionAll();
	//     // foreach ($result as $row)
	//     // {
	//     //     $menu[] = array(
	//     //         'name_zh' => $row->cp_name,
	//     //         'name_cn' => $row->sp_name,
	//     //         'name_en' => $row->ep_name,
	//     //         'link' => $row->link
	//     //     );
	//     // }
	//     // return $menu;

	// }
}