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

class Warehouse extends CI_Controller {

	function __construct()
    {
        parent::__construct();
        error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
        
        $tastname = $this->uri->segment("2");        
        if($tastname!="" && $tastname!="checklogin")
        {
			if(!$this->session->userdata('whLoggedIn'))
			{
				redirect("warehouse");
				exit;
			}
		}

		$this->lguserinfo = $this->session->userdata('whLoggedIn');
		$this->userid = $this->lguserinfo["sessID"];
		$this->warehouseid = $this->lguserinfo["sessWHID"];
		$this->clientid = $this->lguserinfo["sessCLIENTID"];
		$this->company = $this->lguserinfo["sessCompanyname"];
		$this->whname = $this->lguserinfo["sessWHName"];
		$this->load->model('warehouse_model');
		$this->load->model('common_model');
		$this->load->library('Ajax_pagination');
		$this->load->library("email");		
		$this->perPage = 100;		
    }

	function index()
	{
		if($this->session->userdata('whLoggedIn'))
		{
			redirect("warehouse/dashboard");
		}
		else
		{
			$this->load->view('warehouse/login');
		}
	}
	function dashboard()
	{
		if($this->session->userdata('whLoggedIn'))
		{
			$data['main_content'] = 'dashboard';
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
		else
		{
			$this->load->view('warehouse/login');
		}
	}
	function checklogin()
	{
		if($this->session->userdata('whLoggedIn'))
		{
			redirect("warehouse/dashboard");
		}
		else
		{
			$username = $this->input->post("username");
			$userpass = $this->input->post("userpass");
			if($username!="" && $userpass!="")
			{
				if($this->warehouse_model->checklogin()==true)
				{
					redirect("warehouse/dashboard");
				}
				else
				{
					$this->session->set_flashdata("message", "Invalid username or password!");
					$this->session->set_flashdata("msgclass", "alert-danger");
					redirect("warehouse");
					exit;
				}
			}
			else
			{
				redirect("warehouse");
				exit;
			}
		}
	}
/* admin defaults */
	function delcomm()
	{
		if($this->lguserinfo["sessType"]=="warehouse")
		{
			$tablnameprefix = $this->uri->segment(3);
			$rsrcid = $this->uri->segment(4);
			if($rsrcid!="" && $tablnameprefix!="")
			{
				$rsrcid = decodeValue($rsrcid);
				$tablname = $tablnameprefix;
				
				if($result = $this->common_model->delcomm($rsrcid, $tablname))
				{
					$this->session->set_flashdata('message',"Info. deleted succesfully!");
					$this->session->set_flashdata('alerttype',"alert-success");
				}
				else
				{
					$this->session->set_flashdata('message',"Something went wrong, please try again!");
					$this->session->set_flashdata('alerttype',"alert-danger");
				}
			}
			else
			{
				$this->session->set_flashdata('message',"Something went wrong, please try again!");
				$this->session->set_flashdata('alerttype',"alert-danger");
			}
			redirect($this->agent->referrer());
			exit;
		}
		else
		{
			redirect($this->agent->referrer());
			//$this->load->view("warehouse/login");
		}
	}


	
    
	function updateinfo()
	{
		$data["info4edit"] = $this->common_model->getinfo($this->lguserinfo["sessID"], "users");
		$data["pgheading"] = "Update Info.";
		$data['main_content'] = "updateinfo";
		$this->load->view('warehouse/common/main.inc.php',$data);
	}

	function manageupdate()
	{
		$rsrcid = $this->input->post("rsrcid");
		$name = $this->input->post("name");
		$email = $this->input->post("email");
		$mobile = $this->input->post("mobile");
		if($rsrcid>0 && $name!="" && $email!="" && $mobile!="")
		{
			$data = array(
				"fld_name" => $name,
				"fld_email" => $email,
				"fld_mobile" => $mobile,
"fld_updatedby" => $this->lguserinfo["sessID"],
"fld_updatedon" => date("Y-m-d H:i:s")
			);
			$resourceid = $this->common_model->managedata($data, "users", $rsrcid);
			if($resourceid>0)
			{
				$this->session->set_flashdata("message","Info. successfully updated!");
				$this->session->set_flashdata('alerttype',"alert-success");
			}
			else
			{
				$this->session->set_flashdata("message","Sorry, something went wrong, please try again!");
				$this->session->set_flashdata('alerttype',"alert-danger");
			}
		}
		else
		{
			$this->session->set_flashdata("message", "Please fill mandatory fields!");
			$this->session->set_flashdata('alerttype', "alert-danger");
		} 
		redirect("warehouse/updateinfo");
		exit;
	}	

	function changepass()
	{
		$data["pgheading"] = "Change Password";
		$data['main_content'] = "changepass";
		$this->load->view('warehouse/common/main.inc.php',$data);
	}

	function managepass()
	{
		$currpass = $this->input->post("currpass");
		$userpass = $this->input->post("userpass");
		$cuserpass = $this->input->post("cuserpass");
		if($currpass!="" && $userpass!="" && $cuserpass!="")
		{
			if($userpass==$cuserpass)
			{
				$data = array(
					"fld_userpass" => md5($userpass)
				);
				$responsecode = $this->warehouse_model->manageuserpass($data, $currpass);
				if($responsecode=="success")
				{
					$this->session->set_flashdata("message","Password successfully changed!");
					$this->session->set_flashdata('alerttype',"alert-success");
				}
				elseif($responsecode=="notmatched")
				{
					$this->session->set_flashdata("message","Current password not matched, try again!");
					$this->session->set_flashdata('alerttype',"alert-danger");
				}
				else
				{
					$this->session->set_flashdata("message","Something went wrong, try again!");
					$this->session->set_flashdata('alerttype',"alert-danger");
				}
			}
			else
			{
				$this->session->set_flashdata("message", "New password and confirm password not matched, try again!");
				$this->session->set_flashdata('alerttype', "alert-danger");
			}
		}
		else
		{
			$this->session->set_flashdata("message", "Please fill mandatory fields!");
			$this->session->set_flashdata('alerttype', "alert-danger");
		} 
		redirect("warehouse/changepass");
		exit;
	}

    function changestatus()
    {
		$pid = $this->input->post("pid");
		$stype = $this->input->post("stype");
		$status = $this->input->post("status");
		$newstatus = "";
		$addstatus = "";
		if($pid!="" && $stype!="" && $status!="")
		{
			if($status == "active")
			{
				$newstatus = "inactive";
				$addstatus = "no";
			}
			elseif($status == "inactive")
			{
				$newstatus = "active";
				$addstatus = "yes";
			}
			$data = array(
			   'fld_status' => $newstatus
			);
			
			$tablename = "tbl_" . $stype;
			$this->db->where('fld_id',$pid);
			$this->db->update($tablename, $data);
		}
		echo trim($newstatus);
	}	

	function logout()
	{
        $last_login = date('Y-m-d H:i:s');
        $last_login_ip = $_SERVER['REMOTE_ADDR'];
        $admuserid = $this->session->userdata('whLoggedIn')['sessID'];
                
        $data = array(
            'fld_lastlogin' => $last_login,
            'fld_lastloginip'   => $last_login_ip
        );
		
		$this->db->where('fld_id',$admuserid);
        $this->db->update('tbl_users',$data);
		$this->session->unset_userdata('whLoggedIn');
		redirect(base_url().'warehouse');
		exit;
	}
	/* end admin defaults */

	/* start mgmt functions*/
	function category()
	{
		$mgmtname = 'category';
		$tasktype = $this->uri->segment("3");
		if($tasktype=="manage")
		{
			$rsrcid = $this->uri->segment("4");
			$data["pgheading"] = "Add ".(ucfirst($mgmtname));
			$data["rsrcid"] = 0;
			if($rsrcid!="")
			{
				$rsrcid = decodeValue($rsrcid);
				if($data["info4edit"] = $this->common_model->getinfo($rsrcid, $mgmtname))
				{
					$data["rsrcid"] = $rsrcid;
					$data["pgheading"] = "Modify ".ucfirst($mgmtname)." Info.";
				}
			}
			$data['mgmtname'] = $mgmtname;
			$data['main_content'] = "manage".$mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
		elseif($tasktype=="managedata")
		{
			$flashdatamsg =  "Please fill mandatory fields!";
			$flashdatacls = 'alert-danger';
				//predata($_REQUEST); exit;
				$rsrcid = $this->input->post("rsrcid");
				$name = $this->input->post("name");

				


				if($name!="")
				{
					
					$data = array(
						"fld_name" => $name,
						"fld_updatedby" => $this->lguserinfo["sessID"],
						"fld_updatedon" => date("Y-m-d H:i:s")						
						
					);
					if($rsrcid=="" || $rsrcid==0)
					{
						$data["fld_datetime"] = date("Y-m-d H:i:s");
					}
					$resourceid = $this->common_model->managedata($data, $mgmtname, $rsrcid);
					
					if($resourceid>0)
					{
						$flashdatacls =  'alert-success';
						if($rsrcid>0)
						{
							$flashdatamsg =  ucfirst($mgmtname)." info. successfully modified!";
						}
						else
						{
							$flashdatamsg =  ucfirst($mgmtname)." info. successfully added!";							
						}						 
					}
					elseif($resourceid==0)
					{
						$flashdatamsg =  "Sorry, info. you are looking for not found in our record!";
					}
					elseif($resourceid<0)
					{
						$flashdatamsg =  "Sorry, info. already exists!";
					}
				}

				$this->session->set_flashdata("message", $flashdatamsg);
				$this->session->set_flashdata('alerttype', $flashdatacls);

				redirect("warehouse/".$mgmtname);
				exit;
		}
		else // for listing
		{			
			$totalRec = $this->common_model->listdata($mgmtname,"count", '');

			//pagination configuration
			$config['base_url']    = base_url().'warehouse/'.$mgmtname;
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $this->perPage;
			$config['target']    = $this->perPage;
			
			//get the list data
			$pageid = $this->uri->segment(3);
			if($pageid=="" || $pageid==0)
			{
				$pageid = 1;
			}
			$start = ($pageid - 1) * $this->perPage;
			
			$config['uri_segment'] =  $start;
			$config['num_links'] = 5;
			$this->ajax_pagination->initialize($config);
			
			$data['results'] = $this->common_model->listdata($mgmtname,"list", array('limit'=>$this->perPage,'start'=>$start));
			
			$data['main_content'] = $mgmtname;
			$data['mgmtname'] = $mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
	}
	function brand()
	{
		$mgmtname = 'brand';
		$tasktype = $this->uri->segment("3");
		if($tasktype=="manage")
		{
			$rsrcid = $this->uri->segment("4");
			$data["pgheading"] = "Add ".(ucfirst($mgmtname));
			$data["rsrcid"] = 0;
			if($rsrcid!="")
			{
				$rsrcid = decodeValue($rsrcid);
				if($data["info4edit"] = $this->common_model->getinfo($rsrcid, $mgmtname))
				{
					$data["rsrcid"] = $rsrcid;
					$data["pgheading"] = "Modify ".ucfirst($mgmtname)." Info.";
				}
			}
			$data['mgmtname'] = $mgmtname;
			$data['main_content'] = "manage".$mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
		elseif($tasktype=="managedata")
		{
			$flashdatamsg =  "Please fill mandatory fields!";
			$flashdatacls = 'alert-danger';
				//predata($_REQUEST); exit;
				$rsrcid = $this->input->post("rsrcid");
				$name = $this->input->post("name");

				


				if($name!="")
				{
					
					$data = array(
						"fld_name" => $name,							
"fld_updatedby" => $this->lguserinfo["sessID"],
"fld_updatedon" => date("Y-m-d H:i:s")						
						
					);
					if($rsrcid=="" || $rsrcid==0)
					{
						$data["fld_datetime"] = date("Y-m-d H:i:s");
					}
					$resourceid = $this->common_model->managedata($data, $mgmtname, $rsrcid);
					
					if($resourceid>0)
					{
						$flashdatacls =  'alert-success';
						if($rsrcid>0)
						{
							$flashdatamsg =  ucfirst($mgmtname)." info. successfully modified!";
						}
						else
						{
							$flashdatamsg =  ucfirst($mgmtname)." info. successfully added!";							
						}						 
					}
					elseif($resourceid==0)
					{
						$flashdatamsg =  "Sorry, info. you are looking for not found in our record!";
					}
					elseif($resourceid<0)
					{
						$flashdatamsg =  "Sorry, info. already exists!";
					}
				}

				$this->session->set_flashdata("message", $flashdatamsg);
				$this->session->set_flashdata('alerttype', $flashdatacls);

				redirect("warehouse/".$mgmtname);
				exit;
		}
		else // for listing
		{			
			$totalRec = $this->common_model->listdata($mgmtname,"count", '');

			//pagination configuration
			$config['base_url']    = base_url().'warehouse/'.$mgmtname;
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $this->perPage;
			$config['target']    = $this->perPage;
			
			//get the list data
			$pageid = $this->uri->segment(3);
			if($pageid=="" || $pageid==0)
			{
				$pageid = 1;
			}
			$start = ($pageid - 1) * $this->perPage;
			
			$config['uri_segment'] =  $start;
			$config['num_links'] = 5;
			$this->ajax_pagination->initialize($config);
			
			$data['results'] = $this->common_model->listdata($mgmtname,"list", array('limit'=>$this->perPage,'start'=>$start));
			
			$data['main_content'] = $mgmtname;
			$data['mgmtname'] = $mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
	}
	function zone()
	{
		$mgmtname = 'zone';
		$tasktype = $this->uri->segment("3");
		if($tasktype=="manage")
		{
			$rsrcid = $this->uri->segment("4");
			$data["pgheading"] = "Add ".(ucfirst($mgmtname));
			$data["rsrcid"] = 0;
			if($rsrcid!="")
			{
				$rsrcid = decodeValue($rsrcid);
				if($data["info4edit"] = $this->common_model->getinfo($rsrcid, $mgmtname))
				{
					$data["rsrcid"] = $rsrcid;
					$data["pgheading"] = "Modify ".ucfirst($mgmtname)." Info.";
				}
			}
			$data['mgmtname'] = $mgmtname;
			$data['main_content'] = "manage".$mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
		elseif($tasktype=="managedata")
		{
			$flashdatamsg =  "Please fill mandatory fields!";
			$flashdatacls = 'alert-danger';
				//predata($_REQUEST); exit;
				$rsrcid = $this->input->post("rsrcid");
				$name = $this->input->post("name");
				if($name!="")
				{
					
					$data = array(
						"fld_name" => $name,
"fld_updatedby" => $this->lguserinfo["sessID"],
"fld_updatedon" => date("Y-m-d H:i:s")							
						
					);
					if($rsrcid=="" || $rsrcid==0)
					{
						$data["fld_userid"] = $this->lguserinfo["sessID"];
						$data["fld_clientid"] = $this->clientid;
						$data["fld_datetime"] = date("Y-m-d H:i:s");
					}
					$resourceid = $this->common_model->managedata($data, $mgmtname, $rsrcid);
					
					if($resourceid>0)
					{
						$flashdatacls =  'alert-success';
						if($rsrcid>0)
						{
							$flashdatamsg =  ucfirst($mgmtname)." info. successfully modified!";
						}
						else
						{
							$flashdatamsg =  ucfirst($mgmtname)." info. successfully added!";							
						}						 
					}
					elseif($resourceid==0)
					{
						$flashdatamsg =  "Sorry, info. you are looking for not found in our record!";
					}
					elseif($resourceid<0)
					{
						$flashdatamsg =  "Sorry, info. already exists!";
					}
				}

				$this->session->set_flashdata("message", $flashdatamsg);
				$this->session->set_flashdata('alerttype', $flashdatacls);

				redirect("warehouse/".$mgmtname);
				exit;
		}
		else // for listing
		{		

			$totalRec = $this->common_model->listdata($mgmtname,"count", '');

			//pagination configuration
			$config['base_url']    = base_url().'warehouse/'.$mgmtname;
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $this->perPage;
			$config['target']    = $this->perPage;
			
			//get the list data
			$pageid = $this->uri->segment(3);
			if($pageid=="" || $pageid==0)
			{
				$pageid = 1;
			}
			$start = ($pageid - 1) * $this->perPage;
			
			$config['uri_segment'] =  $start;
			$config['num_links'] = 5;
			$this->ajax_pagination->initialize($config);
			
			$data['results'] = $this->common_model->listdata($mgmtname,"list", array('limit'=>$this->perPage,'start'=>$start));
			
			$data['main_content'] = $mgmtname;
			$data['mgmtname'] = $mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
	}
	function productsprice()
	{
		$mgmtname = 'productsprice';
		$mgmttbl = 'price';
		
		$tasktype = $this->uri->segment("3");
		if($tasktype=="manage")
		{
			$rsrcid = $this->uri->segment("4");
			$data["pgheading"] = "Add Price";
			$data["rsrcid"] = 0;
			if($rsrcid!="")
			{
				$rsrcid = decodeValue($rsrcid);
				if($data["info4edit"] = $this->common_model->getinfo($rsrcid, $mgmttbl))
				{
					$data["productinfo"] = $this->common_model->getinfo($data["info4edit"]->fld_productid, 'products');
					$data["rsrcid"] = $rsrcid;
					$data["pgheading"] = "Modify Price Info.";
				}
			}
			$data["productlist"] = $this->common_model->getprodlistbycattype('normal');
			$data["zonelist"] = $this->common_model->getlist('zone', 'fld_id,fld_name','fld_name');
			$data['mgmtname'] = $mgmtname;
			$data['main_content'] = "manage".$mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
		elseif($tasktype=="managedata")
		{
			$flashdatamsg =  "Please fill mandatory fields!";
			$flashdatacls = 'alert-danger';
				//predata($_REQUEST); exit;
					$rsrcid = decodeValue($this->input->post("rsrcid"));
					$productid = $this->input->post("productid");
					$zoneids = $this->input->post("zoneid");
					if($data["info4edit"] = $this->common_model->getinfo($rsrcid, $mgmttbl))
					{
						$productid = $data["info4edit"]->fld_productid;
						
					}
					
					$mrp = $this->input->post("mrp");
					$discount = $this->input->post("discount");
					$price = $this->input->post("price");
					$price3bottles = $this->input->post("price3bottles");
					$price6bottles = $this->input->post("price6bottles");
					$price9bottles = $this->input->post("price9bottles");
					$price12bottles = $this->input->post("price12bottles");
					$price24bottles = $this->input->post("price24bottles");
			
					
					if(!is_numeric($price24bottles)) { $price24bottles = 0;	}
					if(!is_numeric($price12bottles)) { $price12bottles = 0;	}
					if(!is_numeric($price9bottles)) { $price9bottles = 0;	}
					if(!is_numeric($price6bottles)) { $price6bottles = 0;	}
					if(!is_numeric($price3bottles)) { $price3bottles = 0;	}

					if($price24bottles==0)
					{
						$price24bottles = $price12bottles; 
					}
					if($price12bottles==0)
					{
						if($price24bottles==0){ $price24bottles = $price9bottles; }
						$price12bottles = $price9bottles; 
					}
					if($price9bottles==0)
					{
						if($price24bottles==0){ $price24bottles = $price6bottles; }
						if($price12bottles==0){ $price12bottles = $price6bottles; }						
						$price9bottles = $price6bottles; 
					}
					if($price6bottles==0)
					{
						if($price24bottles==0){ $price24bottles = $price3bottles; }	
						if($price12bottles==0){ $price12bottles = $price3bottles; }	
						if($price9bottles==0){ $price9bottles = $price3bottles; }	
						$price6bottles = $price3bottles;
					}
					if($price3bottles==0)
					{
						if($price24bottles==0){ $price24bottles = $price; }	
						if($price12bottles==0){ $price12bottles = $price; }	
						if($price9bottles==0){ $price9bottles = $price; }	
						if($price6bottles==0){ $price6bottles = $price; }							
						$price3bottles = $price;
					}
					//echo "<br>24: " . $price24bottles."<br>12: " . $price12bottles."<br>9: " . $price9bottles."<br>6: " . $price6bottles."<br>3: " . $price3bottles;
					
					if($productid!=""  && $mrp>0 && $price>0 && $price3bottles>0 && $price6bottles>0  && $price9bottles>0 && $price12bottles>0 && $price24bottles>0 && $zoneids!="")
					{
							if($rsrcid>0)
							{
								$this->db->select("*");
								$this->db->where("fld_id", $rsrcid);
								$results = $this->db->get('tbl_price');
								//echo $this->db->last_query();
								if($results->num_rows()>0)
								{
									$info4edit = $results->row();

								}
							}
							## items list	
							$ppid = 0;
							$counter = count($this->input->post("zoneid"));				
							if($counter>0)
							{
								for($i=0;$i<$counter;$i++)
								{
									$data = array(							
										"fld_zoneid" => $zoneids[$i],
										"fld_clientid" => $this->clientid,
										"fld_userid" => $this->userid,
										"fld_productid" => $productid,													
										"fld_mrp" => $mrp,				
										"fld_discount" => $discount,				
										"fld_price" => $price,				
										"fld_price3bottles" => $price3bottles,				
										"fld_price6bottles" => $price6bottles,				
										"fld_price9bottles" => $price9bottles,				
										"fld_price12bottles" => $price12bottles,
										"fld_price24bottles" => $price24bottles,
										"fld_updatedby" => $this->lguserinfo["sessID"],
										"fld_updatedon" => date("Y-m-d H:i:s")										
									);
									//predata($data);
									$this->db->select("fld_id");
									$this->db->where("fld_zoneid", $zoneids[$i]);
									$this->db->where("fld_productid", $productid);
									$this->db->where("fld_price", $info4edit->fld_price);
									$this->db->where("fld_price3bottles", $info4edit->fld_price3bottles);
									$this->db->where("fld_price6bottles", $info4edit->fld_price6bottles);
									$this->db->where("fld_price9bottles", $info4edit->fld_price9bottles);
									$this->db->where("fld_price12bottles", $info4edit->fld_price12bottles);
									$this->db->where("fld_price24bottles", $info4edit->fld_price24bottles);
									$this->db->where("fld_del", 0);									
									$results2 = $this->db->get('tbl_price');
									//echo $this->db->last_query();
									if($results2->num_rows()>0)
									{

										$row2 = $results2->row();
										$resourceid = $row2->fld_id;
										$this->db->where('fld_id',$resourceid);
										$this->db->update('tbl_price', $data);
										//echo '<br>Update ==> '. $zoneids[$i];
										
									}
									else
									{

										$this->db->select("fld_id");
										$this->db->where("fld_zoneid", $zoneids[$i]);
										$this->db->where("fld_productid", $productid);
										$this->db->where("fld_del", 0);									
										$results2 = $this->db->get('tbl_price');
										//echo $this->db->last_query();
										if($results2->num_rows()>0)
										{
											$row2 = $results2->row();
											$resourceid = $row2->fld_id;
											$this->db->where('fld_id',$resourceid);
											$this->db->update('tbl_price', $data);
											//echo '<br>Update 2 ==> '. $zoneids[$i];
											
										}
										else
										{
											$this->db->insert('tbl_price', $data);
											$resourceid = $this->db->insert_id();
											//echo '<br>INSERT ==> ZONE '. $zoneids[$i];
										}
									}									
									
								}
							}
							//exit;
						if($resourceid>0)
						{
							$flashdatacls =  'alert-success';
							if($rsrcid>0)
							{
								$flashdatamsg =  "Price info. successfully modified!";
							}
							else
							{
								$flashdatamsg =  "Price info. successfully added!";							
							}						 
						}
						elseif($resourceid==0)
						{
							$flashdatamsg =  "Sorry, info. you are looking for not found in our record!";
						}
						elseif($resourceid<0)
						{
							$flashdatamsg =  "Sorry, info. already exists!";
						}
					}

					$this->session->set_flashdata("message", $flashdatamsg);
					$this->session->set_flashdata('alerttype', $flashdatacls);

					redirect("warehouse/".$mgmtname);
					exit;

				
		}
		else // for listing
		{	
		
			$totalRec = $this->common_model->listpricedata("count", '');

			//pagination configuration
			$config['base_url']    = base_url().'warehouse/'.$mgmtname."/".encodeValue($zoneid); 
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $this->perPage;
			$config['target']    = $this->perPage;
			
			//get the list data
			$pageid = $this->uri->segment(4);
			if($pageid=="" || $pageid==0)
			{
				$pageid = 1;
			}
			$start = ($pageid - 1) * $this->perPage;
			
			$config['uri_segment'] =  $start;
			$config['num_links'] = 5;
			$this->ajax_pagination->initialize($config);
			
			$data['results'] = $this->common_model->listpricedata("list", array('limit'=>$this->perPage,'start'=>$start));
			$data["productlist"] = $this->common_model->getprodlistbycattype('normal');
			$data["zonelist"] = $this->common_model->getlist('zone', 'fld_id,fld_name','fld_name');		
			$data["catlist"] = $this->common_model->getlist('category', 'fld_id,fld_name','fld_name');
			$data['mgmttbl'] = $mgmttbl;
			$data['main_content'] = $mgmtname;
			$data['mgmtname'] = $mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
	

	}	
	function outlets()
	{
		$mgmtname = 'outlets';
		$tasktype = $this->uri->segment("3");
		##check outlets
		$outletcountstatus = 0;
		$this->db->select("fld_noofoutlet");
		$this->db->where("fld_id", $this->clientid);
		$results = $this->db->get('tbl_clients');
		//echo $this->db->last_query();
		if($results->num_rows()>0)
		{
			$row = $results->row();
			$this->db->select("fld_id");
			$this->db->where("fld_clientid", $this->clientid);
			$results2 = $this->db->get('tbl_outlets');					
			$data["noofoutlet"] = $row->fld_noofoutlet;
			$data["totaloutletcreated"] = $results2->num_rows();
			if($results2->num_rows()>=$row->fld_noofoutlet)
			{
				$outletcountstatus = 1;
				$data["outletcountstatus"] = $outletcountstatus ;
				
			}
		}
		if($tasktype=="manage")
		{

			$rsrcid = $this->uri->segment("4");
			$data["pgheading"] = "Add Outlet";
			$data["rsrcid"] = 0;
			if($rsrcid!="")
			{
				$rsrcid = decodeValue($rsrcid);
				if($data["info4edit"] = $this->common_model->getinfo($rsrcid, $mgmtname))
				{
					$data["rsrcid"] = $rsrcid;
					$data["pgheading"] = "Modify Outlet Info.";
				}
			}
			else
			{
				if($outletcountstatus==1)
				{
					$this->session->set_flashdata("message", "Sorry, you cannot create outlet, contact admin!");
					$this->session->set_flashdata('alerttype', 'alert-danger');
					redirect("warehouse/".$mgmtname);
					exit;
				}

			}
			$data["zonelist"] = $this->common_model->getlist('zone', 'fld_id,fld_name','fld_name');
			$data['mgmtname'] = $mgmtname;
			$data['main_content'] = "manage".$mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
		elseif($tasktype=="managedata")
		{
			$flashdatamsg =  "Please fill mandatory fields!";
			$flashdatacls = 'alert-danger';
				//predata($_REQUEST); exit;
				$rsrcid = $this->input->post("rsrcid");
				$zoneid = $this->input->post("zoneid");
				$name = $this->input->post("name");
				
				$invoiceprefix = $this->input->post("invoiceprefix");
				$address = $this->input->post("address");
				$managername = $this->input->post("managername");
				$manageremail = $this->input->post("manageremail");
				$managermobile = $this->input->post("managermobile");

				if($name!="" && $invoiceprefix!="" && $address!=""  && $zoneid>0)
				{					
					
					$data = array(
						"fld_name" => $name,
						"fld_invoiceprefix" => $invoiceprefix,
						"fld_clientid" => $this->clientid,
						"fld_zoneid" => $zoneid,
						"fld_address" => $address,
						"fld_managername" => $managername,
						"fld_manageremail" => $manageremail,
						"fld_managermobile" => $managermobile,
						"fld_updatedby" => $this->lguserinfo["sessID"],
						"fld_updatedon" => date("Y-m-d H:i:s")
					);
					if($rsrcid=="" || $rsrcid==0)
					{
						$code = createOutletCode();
						$data["fld_code"] = $code;
						$data["fld_datetime"] = date("Y-m-d H:i:s");
					}
					$resourceid = $this->common_model->managedata($data, $mgmtname, $rsrcid);
					
					if($resourceid>0)
					{
						$flashdatacls =  'alert-success';
						if($rsrcid>0)
						{
							$flashdatamsg =  ucfirst($mgmtname)." info. successfully modified!";
						}
						else
						{
							$flashdatamsg =  ucfirst($mgmtname)." info. successfully added!";							
						}						 
					}
					elseif($resourceid==0)
					{
						$flashdatamsg =  "Sorry, info. you are looking for not found in our record!";
					}
					elseif($resourceid<0)
					{
						$flashdatamsg =  "Sorry, info. already exists!";
					}
				}

				$this->session->set_flashdata("message", $flashdatamsg);
				$this->session->set_flashdata('alerttype', $flashdatacls);

				redirect("warehouse/".$mgmtname);
				exit;
		}
		else // for listing
		{			
			$totalRec = $this->warehouse_model->listoutletsdata("count", '');

			//pagination configuration
			$config['base_url']    = base_url().'warehouse/'.$mgmtname;
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $this->perPage;
			$config['target']    = $this->perPage;
			
			//get the list data
			$pageid = $this->uri->segment(3);
			if($pageid=="" || $pageid==0)
			{
				$pageid = 1;
			}
			$start = ($pageid - 1) * $this->perPage;
			
			$config['uri_segment'] =  $start;
			$config['num_links'] = 5;
			$this->ajax_pagination->initialize($config);
			
			$data['results'] = $this->warehouse_model->listoutletsdata("list", array('limit'=>$this->perPage,'start'=>$start));
			$data["zonelist"] = $this->common_model->getlist('zone', 'fld_id,fld_name','fld_name');
			$data['main_content'] = $mgmtname;
			$data['mgmtname'] = $mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
	}

	

	function products()
	{
		$mgmtname = 'products';
		$tasktype = $this->uri->segment("3");
		if($tasktype=="manage")
		{
			$rsrcid = $this->uri->segment("4");
			$data["pgheading"] = "Add Product";
			$data["rsrcid"] = 0;
			if($rsrcid!="")
			{
				$rsrcid = decodeValue($rsrcid);
				if($data["info4edit"] = $this->common_model->getinfo($rsrcid, $mgmtname))
				{
					$data["rsrcid"] = $rsrcid;
					$data["pgheading"] = "Modify Product Info.";
				}
			}
			
			$data["catlist"] = $this->common_model->getlist('category', 'fld_id,fld_name','fld_name');
			$data["brandlist"] = $this->common_model->getlist('brand', 'fld_id,fld_name','fld_name');
			$data['mgmtname'] = $mgmtname;
			$data['main_content'] = "manage".$mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
		elseif($tasktype=="managedata")
		{
			$flashdatamsg =  "Please fill mandatory fields!";
			$flashdatacls = 'alert-danger';
				//predata($_REQUEST); exit;
				$rsrcid = $this->input->post("rsrcid");
				
				$name = $this->input->post("name");
				$prodtype = $this->input->post("prodtype");
				$catid = $this->input->post("catid");
				$brandid = $this->input->post("brandid");
				$barcode = $this->input->post("barcode");
				$barcode02 = $this->input->post("barcode02");
				$barcode03 = $this->input->post("barcode03");
				$barcode04 = $this->input->post("barcode04");
				$noofbottlesincase = $this->input->post("noofbottlesincase");
				$minstockvalue = $this->input->post("minstockvalue");

				$shortdesc = $this->input->post("shortdesc");
				if($prodtype!="" && $name!="" && $catid>0 && $brandid!="" && $barcode!="" && $noofbottlesincase>0)
				{

					if($brandid=='other')
					{
						$newbrandname = $this->input->post("newbrandname");
						$this->db->select("fld_id");
						$this->db->where("fld_name", $newbrandname);
						$results2 = $this->db->get('tbl_brand');				
						if($results2->num_rows()>0)
						{
							$row2 = $results2->result();
							$brandid= $row2->fld_id;
						}
						else
						{							
							$data2 = array(
								"fld_name" => $newbrandname,														
								"fld_datetime" => date("Y-m-d H:i:s"),
								"fld_updatedby" => $this->lguserinfo["sessID"],
								"fld_updatedon" => date("Y-m-d H:i:s")

							);
							$brandid = $this->common_model->managedata($data2, 'brand', '');
							
						}
					}
					
					$data = array(
						"fld_type" => $prodtype,						
						"fld_name" => $name,						
						"fld_catid" => $catid,				
						"fld_brandid" => $brandid,				
						"fld_barcode" => $barcode,				
						"fld_barcode02" => $barcode02,				
						"fld_barcode03" => $barcode03,				
						"fld_barcode04" => $barcode04,				
						"fld_noofbottlesincase" => $noofbottlesincase,			
						"fld_minstockvalue" => $minstockvalue,			
						"fld_shortdesc" => $shortdesc,
						"fld_updatedby" => $this->lguserinfo["sessID"],
						"fld_updatedon" => date("Y-m-d H:i:s")

						
					);
					if($rsrcid=="" || $rsrcid==0)
					{
						$data["fld_datetime"] = date("Y-m-d H:i:s");
					}
					$resourceid = $this->common_model->managedata($data, $mgmtname, $rsrcid);
					
					if($resourceid>0)
					{
						$flashdatacls =  'alert-success';
						if($rsrcid>0)
						{
							$flashdatamsg =  ucfirst($mgmtname)." info. successfully modified!";
						}
						else
						{
							$flashdatamsg =  ucfirst($mgmtname)." info. successfully added!";							
						}						 
					}
					elseif($resourceid==0)
					{
						$flashdatamsg =  "Sorry, info. you are looking for not found in our record!";
					}
					elseif($resourceid<0)
					{
						$flashdatamsg =  "Sorry, info. already exists!";
					}
				}

				$this->session->set_flashdata("message", $flashdatamsg);
				$this->session->set_flashdata('alerttype', $flashdatacls);

				redirect("warehouse/".$mgmtname);
				exit;
		}
		else // for listing
		{			
			$totalRec = $this->common_model->listproductdata("count", '','','','','','7,30');

			//pagination configuration
			$config['base_url']    = base_url().'warehouse/'.$mgmtname;
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $this->perPage;
			$config['target']    = $this->perPage;
			
			//get the list data
			$pageid = $this->uri->segment(3);
			if($pageid=="" || $pageid==0)
			{
				$pageid = 1;
			}
			$start = ($pageid - 1) * $this->perPage;
			
			$config['uri_segment'] =  $start;
			$config['num_links'] = 5;
			$this->ajax_pagination->initialize($config);
			
			$data['results'] = $this->common_model->listproductdata("list", array('limit'=>$this->perPage,'start'=>$start),'','stock','category','brand','7,30');
			$data["catlist"] = $this->common_model->getlist('category', 'fld_id,fld_name','fld_name');
			$data["brandlist"] = $this->common_model->getlist('brand', 'fld_id,fld_name','fld_name');
			$data['main_content'] = $mgmtname;
			$data['mgmtname'] = $mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
	}
	function warehouse()
	{
		$mgmtname = 'warehouse';
		$tasktype = $this->uri->segment("3");
		if($tasktype=="manage")
		{
			$rsrcid = $this->uri->segment("4");
			$data["pgheading"] = "Add ".(ucfirst($mgmtname));
			$data["rsrcid"] = 0;
			if($rsrcid!="")
			{
				$rsrcid = decodeValue($rsrcid);
				if($data["info4edit"] = $this->common_model->getinfo($rsrcid, $mgmtname))
				{
					$data["rsrcid"] = $rsrcid;
					$data["pgheading"] = "Modify ".ucfirst($mgmtname)." Info.";
				}
			}
			$data['mgmtname'] = $mgmtname;
			$data['main_content'] = "manage".$mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
		elseif($tasktype=="managedata")
		{
			$flashdatamsg =  "Please fill mandatory fields!";
			$flashdatacls = 'alert-danger';
				//predata($_REQUEST); exit;
				$rsrcid = $this->input->post("rsrcid");
				$name = $this->input->post("name");
				
				if($name!="")
				{
					
					$data = array(
						"fld_clientid" => $this->clientid,
						"fld_name" => $name,
						"fld_updatedby" => $this->lguserinfo["sessID"],
						"fld_updatedon" => date("Y-m-d H:i:s")
					);
					if($rsrcid=="" || $rsrcid==0)
					{
						$data["fld_datetime"] = date("Y-m-d H:i:s");
					}
					$resourceid = $this->common_model->managedata($data, $mgmtname, $rsrcid);
					
					if($resourceid>0)
					{
						$flashdatacls =  'alert-success';
						if($rsrcid>0)
						{
							$flashdatamsg =  ucfirst($mgmtname)." info. successfully modified!";
						}
						else
						{
							$flashdatamsg =  ucfirst($mgmtname)." info. successfully added!";							
						}						 
					}
					elseif($resourceid==0)
					{
						$flashdatamsg =  "Sorry, info. you are looking for not found in our record!";
					}
					elseif($resourceid<0)
					{
						$flashdatamsg =  "Sorry, info. already exists!";
					}
				}

				$this->session->set_flashdata("message", $flashdatamsg);
				$this->session->set_flashdata('alerttype', $flashdatacls);

				redirect("warehouse/".$mgmtname);
				exit;
		}
		else // for listing
		{			
			$totalRec = $this->common_model->listdata($mgmtname,"count", '');

			//pagination configuration
			$config['base_url']    = base_url().'warehouse/'.$mgmtname;
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $this->perPage;
			$config['target']    = $this->perPage;
			
			//get the list data
			$pageid = $this->uri->segment(3);
			if($pageid=="" || $pageid==0)
			{
				$pageid = 1;
			}
			$start = ($pageid - 1) * $this->perPage;
			
			$config['uri_segment'] =  $start;
			$config['num_links'] = 5;
			$this->ajax_pagination->initialize($config);
			
			$data['results'] = $this->common_model->listdata($mgmtname,"list", array('limit'=>$this->perPage,'start'=>$start));
			
			$data['main_content'] = $mgmtname;
			$data['mgmtname'] = $mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
	}


	function users()
	{
		$mgmtname = 'users';
		$outletid = decodeValue($this->uri->segment("3"));
		if($data["outletinfo"] = $this->common_model->getinfo($outletid, 'outlets'))
		{			
			$data['outletid'] =  $outletid;
		$tasktype = $this->uri->segment("4");
		if($tasktype=="manage")
		{
			$rsrcid = $this->uri->segment("5");
			$data["pgheading"] = "Add User";
			$data["rsrcid"] = 0;
			if($rsrcid!="")
			{
				$rsrcid = decodeValue($rsrcid);
				if($data["info4edit"] = $this->common_model->getinfo($rsrcid, $mgmtname))
				{
					$data["rsrcid"] = $rsrcid;
					$data["pgheading"] = "Modify User Info.";
				}
			}
			$data['mgmtname'] = $mgmtname;
			$data['main_content'] = "manage".$mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
		elseif($tasktype=="managedata")
		{
			$flashdatamsg =  "Please fill mandatory fields!";
			$flashdatacls = 'alert-danger';
				//predata($_REQUEST); exit;
				$rsrcid = $this->input->post("rsrcid");
				$type = $this->input->post("type");
				$name = $this->input->post("name");
				$email = $this->input->post("email");
				$mobile = $this->input->post("mobile");
				$username = $this->input->post("username");
				$upass = $this->input->post("upass");
				$otp = $this->input->post("otp");
				
				if(($type=="manager" || $type=="operator") && $name!="" && $username!="" && $outletid>0)
				{
					if($otp=='')
					{
						$otp ='no';
					}
					
					$data = array(
						"fld_clientid" => $this->clientid,
						"fld_outletid" => $outletid,
						"fld_type" => $type,
						"fld_name" => $name,
						"fld_email" => $email,
						"fld_mobile" => $mobile,
						"fld_otp" => $otp,
						"fld_username" => $username,
						"fld_updatedby" => $this->lguserinfo["sessID"],
						"fld_updatedon" => date("Y-m-d H:i:s")

					);
					if($rsrcid=="" || $rsrcid==0)
					{
						$data["fld_datetime"] = date("Y-m-d H:i:s");
					}
					if($upass!="")
					{
						$data["fld_userpass"] = md5($upass);
					}
					$resourceid = $this->common_model->managedata($data, $mgmtname, $rsrcid); //exit;
					
					if($resourceid>0)
					{
						$flashdatacls =  'alert-success';
						if($rsrcid>0)
						{
							$flashdatamsg =  ucfirst($mgmtname)." info. successfully modified!";
						}
						else
						{
							$flashdatamsg =  ucfirst($mgmtname)." info. successfully added!";							
						}						 
					}
					elseif($resourceid==0)
					{
						$flashdatamsg =  "Sorry, info. you are looking for not found in our record!";
					}
					elseif($resourceid<0)
					{
						$flashdatamsg =  "Sorry, info. already exists!";
					}
				}

				$this->session->set_flashdata("message", $flashdatamsg);
				$this->session->set_flashdata('alerttype', $flashdatacls);

				redirect("warehouse/".$mgmtname."/".encodeValue($outletid));
				exit;
		}
		else // for listing
		{			
			$totalRec = $this->common_model->listdata($mgmtname,"count", '');

			//pagination configuration
			$config['base_url']    = base_url().'warehouse/'.$mgmtname;
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $this->perPage;
			$config['target']    = $this->perPage;
			
			//get the list data
			$pageid = $this->uri->segment(3);
			if($pageid=="" || $pageid==0)
			{
				$pageid = 1;
			}
			$start = ($pageid - 1) * $this->perPage;
			
			$config['uri_segment'] =  $start;
			$config['num_links'] = 5;
			$this->ajax_pagination->initialize($config);
			
			$data['results'] = $this->common_model->listdata($mgmtname,"list", array('limit'=>$this->perPage,'start'=>$start));
			
			$data['main_content'] = $mgmtname;
			$data['mgmtname'] = $mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
	
		}
		else
		{
			redirect("warehouse/outlet");
			exit;
		}
	}
	function vendors()
	{
		$mgmtname = 'vendors';	
		$tasktype = $this->uri->segment("3");
		if($tasktype=="manage")
		{
			$rsrcid = $this->uri->segment("4");
			$data["pgheading"] = "Add Vendor";
			$data["rsrcid"] = 0;
			if($rsrcid!="")
			{
				$rsrcid = decodeValue($rsrcid);
				if($data["info4edit"] = $this->common_model->getinfo($rsrcid, $mgmtname))
				{
					$data["rsrcid"] = $rsrcid;
					$data["pgheading"] = "Modify Vendor Info.";
				}
			}
			$data['mgmtname'] = $mgmtname;
			$data['main_content'] = "manage".$mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
		elseif($tasktype=="managedata")
		{
			$flashdatamsg =  "Please fill mandatory fields!";
			$flashdatacls = 'alert-danger';
				//predata($_REQUEST); exit;
				$rsrcid = $this->input->post("rsrcid");
				
				$name = trim($this->input->post("name"));
				$email = $this->input->post("email");
				$mobile = $this->input->post("mobile");
				
				$company = $this->input->post("company");
				$address = $this->input->post("address");
				$cpname = $this->input->post("cpname");
				$cpemail = $this->input->post("cpemail");
				$cpmobile = $this->input->post("cpmobile");
				$remarks = $this->input->post("remarks");
				
				if($name!="")
				{
					
					$data = array(
						"fld_clientid" => $this->clientid,
						"fld_name" => $name,
						"fld_email" => $email,
						"fld_mobile" => $mobile,						
						"fld_address" => $address,
						"fld_cpname" => $cpname,
						"fld_cpemail" => $cpemail,
						"fld_cpmobile" => $cpmobile,
						"fld_remarks" => $remarks,
						"fld_updatedby" => $this->lguserinfo["sessID"],
						"fld_updatedon" => date("Y-m-d H:i:s")

						
					);
					if($rsrcid=="" || $rsrcid==0)
					{
						$data["fld_datetime"] = date("Y-m-d H:i:s");
					}
				
					$resourceid = $this->common_model->managedata($data, $mgmtname, $rsrcid); //exit;					
					if($resourceid>0)
					{
						$flashdatacls =  'alert-success';
						if($rsrcid>0)
						{
							$flashdatamsg =  ucfirst($mgmtname)." info. successfully modified!";
						}
						else
						{
							$flashdatamsg =  ucfirst($mgmtname)." info. successfully added!";							
						}						 
					}
					elseif($resourceid==0)
					{
						$flashdatamsg =  "Sorry, info. you are looking for not found in our record!";
					}
					elseif($resourceid<0)
					{
						$flashdatamsg =  "Sorry, info. already exists!";
					}
				}

				$this->session->set_flashdata("message", $flashdatamsg);
				$this->session->set_flashdata('alerttype', $flashdatacls);
				
				redirect("warehouse/".$mgmtname."/".encodeValue($outletid));
				exit;
		}
		else // for listing
		{			
			$totalRec = $this->common_model->listdata($mgmtname,"count", '');

			//pagination configuration
			$config['base_url']    = base_url().'warehouse/'.$mgmtname;
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $this->perPage;
			$config['target']    = $this->perPage;
			
			//get the list data
			$pageid = $this->uri->segment(3);
			if($pageid=="" || $pageid==0)
			{
				$pageid = 1;
			}
			$start = ($pageid - 1) * $this->perPage;
			
			$config['uri_segment'] =  $start;
			$config['num_links'] = 5;
			$this->ajax_pagination->initialize($config);			
			$data['results'] = $this->common_model->listdata($mgmtname,"list", array('limit'=>$this->perPage,'start'=>$start));			
			$data['main_content'] = $mgmtname;
			$data['mgmtname'] = $mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}		
	}
	
	function saleclients()
	{
		$mgmtname = 'saleclients';	
		$tasktype = $this->uri->segment("3");
		if($tasktype=="manage")
		{
			$rsrcid = $this->uri->segment("4");
			$data["pgheading"] = "Add Client";
			$data["rsrcid"] = 0;
			if($rsrcid!="")
			{
				$rsrcid = decodeValue($rsrcid);
				if($data["info4edit"] = $this->common_model->getinfo($rsrcid, $mgmtname))
				{
					$data["rsrcid"] = $rsrcid;
					$data["pgheading"] = "Modify Client Info.";
				}
			}
			$data['mgmtname'] = $mgmtname;
			$data['main_content'] = "manage".$mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
		elseif($tasktype=="managedata")
		{
			$flashdatamsg =  "Please fill mandatory fields!";
			$flashdatacls = 'alert-danger';
				//predata($_REQUEST); exit;
				$rsrcid = $this->input->post("rsrcid");
				
				$name = trim($this->input->post("name"));
				$email = $this->input->post("email");
				$mobile = $this->input->post("mobile");
				
				$company = $this->input->post("company");
				$address = $this->input->post("address");
				$cpname = $this->input->post("cpname");
				$cpemail = $this->input->post("cpemail");
				$cpmobile = $this->input->post("cpmobile");
				$remarks = $this->input->post("remarks");
				
				if($name!="")
				{
					
					$data = array(
						"fld_clientid" => $this->clientid,
						"fld_name" => $name,
						"fld_email" => $email,
						"fld_mobile" => $mobile,						
						"fld_address" => $address,
						"fld_cpname" => $cpname,
						"fld_cpemail" => $cpemail,
						"fld_cpmobile" => $cpmobile,
						"fld_remarks" => $remarks,
						"fld_updatedby" => $this->lguserinfo["sessID"],
						"fld_updatedon" => date("Y-m-d H:i:s")

						
					);
					if($rsrcid=="" || $rsrcid==0)
					{
						$data["fld_datetime"] = date("Y-m-d H:i:s");
					}
				
					$resourceid = $this->common_model->managedata($data, $mgmtname, $rsrcid); //exit;					
					if($resourceid>0)
					{
						$flashdatacls =  'alert-success';
						if($rsrcid>0)
						{
							$flashdatamsg =  "Client  info. successfully modified!";
						}
						else
						{
							$flashdatamsg =  "Client  info. successfully added!";							
						}						 
					}
					elseif($resourceid==0)
					{
						$flashdatamsg =  "Sorry, info. you are looking for not found in our record!";
					}
					elseif($resourceid<0)
					{
						$flashdatamsg =  "Sorry, info. already exists!";
					}
				}

				$this->session->set_flashdata("message", $flashdatamsg);
				$this->session->set_flashdata('alerttype', $flashdatacls);
				
				redirect("warehouse/".$mgmtname);
				exit;
		}
		else // for listing
		{			
			$totalRec = $this->common_model->listdata($mgmtname,"count", '');

			//pagination configuration
			$config['base_url']    = base_url().'warehouse/'.$mgmtname;
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $this->perPage;
			$config['target']    = $this->perPage;
			
			//get the list data
			$pageid = $this->uri->segment(3);
			if($pageid=="" || $pageid==0)
			{
				$pageid = 1;
			}
			$start = ($pageid - 1) * $this->perPage;
			
			$config['uri_segment'] =  $start;
			$config['num_links'] = 5;
			$this->ajax_pagination->initialize($config);			
			$data['results'] = $this->common_model->listdata($mgmtname,"list", array('limit'=>$this->perPage,'start'=>$start));			
			$data['main_content'] = $mgmtname;
			$data['mgmtname'] = $mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}		
	}

	function purchase()
	{
		$mgmtname = 'purchase';
		$mgmttbl = 'transaction';
		
		$tasktype = $this->uri->segment("3");
		if($tasktype=="manage")
		{
			$rsrcid = $this->uri->segment("4");
			$data["pgheading"] = "Add Purchase";
			$data["rsrcid"] = 0;
			if($rsrcid!="")
			{
				$rsrcid = decodeValue($rsrcid);
				if($data["info4edit"] = $this->warehouse_model->getstockinfo($rsrcid,'stockin'))
				{
					$data["rsrcid"] = $rsrcid;
					$data["pgheading"] = "Modify Purchase Info.";
				}
			}
			$data["vendorlist"] = $this->common_model->getlist('vendors', 'fld_id,fld_name','fld_name');
			$data["warehouselist"] = $this->common_model->getlist('warehouse', 'fld_id,fld_name','fld_name');
			$data["productlist"] = $this->common_model->getlistbywhere('products', 'fld_id,fld_name','fld_name asc','');

			$data['mgmtname'] = $mgmtname;
			$data['main_content'] = "manage".$mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
		elseif($tasktype=="managedata")
		{
			$flashdatamsg =  "Please fill mandatory fields!";
			$flashdatacls = 'alert-danger';
				//predata($_REQUEST); exit;
				$rsrcid = $this->input->post("rsrcid");
				
				$vendorid = $this->input->post("vendorid");
				$warehouseid = $this->warehouseid;
				$refdate = $this->input->post("refdate");				
				$refno = $this->input->post("refno");

				$productid = $this->input->post("productid");
				$qty_case = $this->input->post("qty_case");
				$qty_bottle = $this->input->post("qty_bottle");
				$price_case = $this->input->post("price_case");
				$price_bottle = $this->input->post("price_bottle");
				$subtotal = $this->input->post("subtotal");
				$vatamount = $this->input->post("vatamount");
				$tscamount = $this->input->post("tscamount");
				$discount = $this->input->post("discount");
				$adjustment = $this->input->post("adjustment");

				$productttotalamount = $this->input->post("productttotalamount");
				$producttotalrows = $this->input->post("producttotalrows");
				
				if($vendorid>0 && $warehouseid>0 && $refno!="" && $refdate!="" && $producttotalrows>0)
				{	
					$resourceid = $this->warehouse_model->managestock($this->input->post(),'stockin'); //exit;
					
					if($resourceid>0)
					{
						$flashdatacls =  'alert-success';
						if($rsrcid>0)
						{
							$flashdatamsg =  ucfirst($mgmtname)." info. successfully modified!";
						}
						else
						{
							$flashdatamsg =  ucfirst($mgmtname)." info. successfully added!";							
						}						 
					}
					elseif($resourceid==0)
					{
						$flashdatamsg =  "Sorry, info. you are looking for not found in our record!";
					}
					elseif($resourceid<0)
					{
						$flashdatamsg =  "Sorry, info. already exists!";
					}
				}

				$this->session->set_flashdata("message", $flashdatamsg);
				$this->session->set_flashdata('alerttype', $flashdatacls);
				
				redirect("warehouse/".$mgmtname."?". $_SERVER['QUERY_STRING']);
				exit;
		}
		elseif($tasktype=="view" || $tasktype=="print")
		{
				$rsrcid = $this->uri->segment("4");
				
				if($rsrcid!="")
				{
					$rsrcid = decodeValue($rsrcid);
					if($data["info4view"] = $this->common_model->getinfo($rsrcid, $mgmttbl))
					{
						$data["transinfo4view"] = $this->common_model->gettransinfolist($rsrcid);
						
						$data["rsrcid"] = $rsrcid;
						$data["pgheading"] = "View ".ucfirst($mgmtname)." Info.";
					}
				}
				
				
				$data['tasktype'] = $tasktype;
				$data['mgmtname'] = $mgmtname;
				$data['main_content'] = "view".$mgmtname;
				$this->load->view('warehouse/common/popup.inc.php',$data);
			
		}
		else // for listing
		{	
			
			$fromdt = $this->input->get("fromdate");
			$todt = $this->input->get("todate");
			if($fromdt!="")
			{
				 $fromdate = convertdateFormat($fromdt,'Y-m-d');
				 $data['fromdate'] = $fromdate;
			}
			if($todt!="")
			{				
				$todate = convertdateFormat($todt,'Y-m-d');
				$data['todate'] = $todate;
			}

			if($fromdt=="" && $todt=="")
			{
				$data['fromdate'] = date('Y-m-d');
				$data['todate'] = date('Y-m-d');
			}
			 $totalRec = $this->warehouse_model->liststockdata("count", '','stockin');

			//pagination configuration
			$config['base_url']    = base_url().'warehouse/'.$mgmtname;
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $this->perPage;
			$config['target']    = $this->perPage;
			
			//get the list data
			$pageid = $this->uri->segment(3);
			if($pageid=="" || $pageid==0)
			{
				$pageid = 1;
			}
			$start = ($pageid - 1) * $this->perPage;
			
			$config['uri_segment'] =  $start;
			$config['num_links'] = 5;
			$this->ajax_pagination->initialize($config);
			
			$data['results'] = $this->warehouse_model->liststockdata("list", array('limit'=>$this->perPage,'start'=>$start),'stockin');
			$data["vendorlist"] = $this->common_model->getlist('vendors', 'fld_id,fld_name','fld_name');
					
			$data['main_content'] = $mgmtname;
			$data['mgmtname'] = $mgmtname;
			$data['mgmttbl'] = $mgmttbl;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
	}

	function wbilling()
	{
		$mgmtname = 'wbilling';
		$mgmttbl = 'transaction';
		
		$fromdt = $this->input->get("fromdate");
		$todt = $this->input->get("todate");
		if($fromdt!="")
		{
			 $fromdate = convertdateFormat($fromdt,'Y-m-d');
			 $data['fromdate'] = $fromdate;
		}
		if($todt!="")
		{				
			$todate = convertdateFormat($todt,'Y-m-d');
			$data['todate'] = $todate;
		}

		if($fromdt=="" && $todt=="")
		{
			$data['fromdate'] = date('Y-m-d');
			$data['todate'] = date('Y-m-d');
		}
		if($this->input->get("outletid")!="")
		{
			$outletid = decodeValue($this->input->get("outletid"));		
		}
		
		$tasktype = $this->uri->segment("3");

		if($tasktype=="manage")
		{
			$rsrcid = $this->uri->segment("4");
			$data["pgheading"] = "Transfer Stock To Outlet";
			$data["rsrcid"] = 0;
			if($rsrcid!="")
			{
				$rsrcid = decodeValue($rsrcid);
				if($data["info4edit"] = $this->warehouse_model->getstockinfo($rsrcid,'stockout'))
				{
					$data["rsrcid"] = $rsrcid;
					$data["pgheading"] = "Modify Stock Transfer Info.";
				}
			}
			$data["warehouseinfo"] = $this->common_model->getinfo($this->warehouseid, "warehouse");
			$data["productlist"] = $this->common_model->getwarehousestocklist($this->warehouseid);
			$data["clientlist"] = $this->common_model->getlist('saleclients', 'fld_id,fld_name','fld_name');
			

			$data['mgmtname'] = $mgmtname;
			$data['main_content'] = "manage".$mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
		elseif($tasktype=="managedata")
		{
				$flashdatamsg =  "Please fill mandatory fields!";
				$flashdatacls = 'alert-danger';
				
				$rsrcid = $this->input->post("rsrcid");				
				$clientid = $this->input->post("clientid");
				$warehouseid = $this->warehouseid;
				$refdate = $this->input->post("refdate");				
				$refno = $this->input->post("refno");
				$productid = $this->input->post("productid");
				$qty_case = $this->input->post("qty_case");
				$qty_bottle = $this->input->post("qty_bottle");				
				$price_bottle = $this->input->post("price_bottle");				
				$subtotal = $this->input->post("subtotal");				
				$producttotalrows = $this->input->post("producttotalrows");
				
				if($clientid>0 && $warehouseid>0 && $producttotalrows>0)
				{
					$rtnvalps= $this->warehouse_model->checkproductstock($warehouseid,$productid,$qty_case,$qty_bottle,$rsrcid);
					$rtnvalpscnt=count($rtnvalps);					
					$productinfo ="";
					if($rtnvalpscnt>0)
					{
						$indexno=1;
						foreach($rtnvalps as $row)
						{							
							if($productinfo!="")
							{
								
								if($rtnvalpscnt==$indexno)
								{
									$productinfo .=" and ". $row['name'];
								}
								else
								{
									$productinfo .=",  ". $row['name'];
								}
							}
							else
							{
								$productinfo = $row['name'];
							}
							$indexno++;
						}
						$flashdatamsg  ='Error: the following product(s) are not in stock:
										- '.$productinfo.' ';
						$flashdatacls =  'alert-danger';
						$this->session->set_flashdata("message", $flashdatamsg);
						$this->session->set_flashdata('alerttype', $flashdatacls);
						
						redirect("warehouse/".$mgmtname."?". $_SERVER['QUERY_STRING']);
						exit;						
					}
					else
					{
						$resourceid = $this->warehouse_model->managebilling($this->input->post(),'wsale');
						
						if($resourceid>0)
						{
							$flashdatacls =  'alert-success';
							if($rsrcid>0)
							{
								$flashdatamsg =  "Billing info. successfully modified!";
							}
							else
							{
								$flashdatamsg =  "Billing info. successfully added!";							
							}						 
						}
						elseif($resourceid==0)
						{
							$flashdatamsg =  "Sorry, info. you are looking for not found in our record!";
						}
						elseif($resourceid<0)
						{
							$flashdatamsg =  "Sorry, Pass Number already exists!";
						}
					}
				}

				$this->session->set_flashdata("message", $flashdatamsg);
				$this->session->set_flashdata('alerttype', $flashdatacls);
				
				redirect("warehouse/".$mgmtname."?". $_SERVER['QUERY_STRING']);
				exit;
		}
		elseif($tasktype=="view" || $tasktype=="print")
		{
				$rsrcid = $this->uri->segment("4");
				
				if($rsrcid!="")
				{
					$rsrcid = decodeValue($rsrcid);
					if($data["info4view"] = $this->common_model->getwbillinginfo($rsrcid))
					{
						$data["transinfo4view"] = $this->common_model->gettransinfolist($rsrcid);
						
						$data["rsrcid"] = $rsrcid;
						$data["pgheading"] = "View Bill Details";
					}
				}
				$data['tasktype'] = $tasktype;				
				$data['mgmtname'] = $mgmtname;
				$data['main_content'] = "view".$mgmtname;
				$this->load->view('warehouse/common/popup.inc.php',$data);
			
		}
		elseif($tasktype=="warehouse" || $tasktype=="product" || $tasktype=="category" || $tasktype=="brand" || $tasktype=="producttype" || $tasktype=="productprice")
		{
			if($tasktype=="warehouse")
			{
				$mgmtname = 'warehouse';
				$mgmttbl = 'warehouse';
				$rdurl = 'warehouse/warehouse';
			}
			elseif($tasktype=="product")
			{
				$mgmtname = 'products';
				$mgmttbl = 'products';
				$rdurl = 'warehouse/products';
			}
			elseif($tasktype=="producttype")
			{
				$mgmtname = 'products';
				$mgmttbl = 'products';
				$rdurl = 'warehouse/products';
			}
			elseif($tasktype=="category")
			{
				$mgmtname = 'category';
				$mgmttbl = 'category';
				$rdurl = 'warehouse/category';
			}
			elseif($tasktype=="brand")
			{
				$mgmtname = 'brand';
				$mgmttbl = 'brand';
				$rdurl = 'warehouse/brand';
			}
			$rsrcid = $this->uri->segment("4");
			
			$data["rsrcid"] = 0;
			if($rsrcid!="")
			{
					
					if($tasktype=="producttype")
					{
						
						$data["info4edit"] = arrProductType()[$rsrcid];
					}
					else
					{
						$rsrcid = decodeValue($rsrcid);
						$data["info4edit"] = $this->common_model->getinfo($rsrcid, $mgmttbl);
					}
				if($data["info4edit"])
				{
					$fromdt = trim($this->input->get("fromdate"));
					$todt = trim($this->input->get("todate"));
					if($fromdt!="")
					{
						 $fromdate = convertdateFormat($fromdt,'Y-m-d');
						 $data['fromdate'] = $fromdate;
					}
					if($todt!="")
					{				
						$todate = convertdateFormat($todt,'Y-m-d');
						$data['todate'] = $todate;
					}

					if($fromdt=="" && $todt=="")
					{
						$data['fromdate'] = date('Y-m-d');
						$data['todate'] = date('Y-m-d');
					}

					$data["rsrcid"] = $rsrcid;
					if($tasktype=="warehouse")
					{
						 $data["pgheading"] = $data["info4edit"]->fld_mobile." > View Bill Details";
						 $fldname ='fld_warehouseid';
						 
					}
					elseif($tasktype=="category")
					{
						 $data["pgheading"] = $data["info4edit"]->fld_name." > View Stock Transfer";
						 $fldname ='fld_catid';
					}
					elseif($tasktype=="brand")
					{
						 $data["pgheading"] = $data["info4edit"]->fld_name." > View Stock Transfer";
						 $fldname ='fld_brandid';
					}
					elseif($tasktype=="product")
					{
						 $data["pgheading"] = $data["info4edit"]->fld_name." > View Stock Transfer";
						 $fldname ='fld_productid';

							
					}
					elseif($tasktype=="producttype")
					{
						 $data["pgheading"] = $data["info4edit"]." > View Stock Transfer";
						 $fldname ='fld_type';
					}


					if($tasktype=="producttype")
					{					
						
						$data["page_url"] = base_url().'warehouse/stocktransfer/'.$tasktype."/".$rsrcid;
					}
					else
					{
						$data["page_url"] = base_url().'warehouse/stocktransfer/'.$tasktype."/".encodeValue($rsrcid);
						
					}

					$totalRec = $this->common_model->listallstdata("count", '',$fldname,$rsrcid);

					//pagination configuration
					$config['base_url']    = $data["page_url"];
					$config['total_rows']  = $totalRec;
					$config['per_page']    = $this->perPage;
					$config['target']    = $this->perPage;
					
					//get the list data
					$pageid = $this->uri->segment(5);
					if($pageid=="" || $pageid==0)
					{
						$pageid = 1;
					}
					$start = ($pageid - 1) * $this->perPage;
					
					$config['uri_segment'] =  $start;
					$config['num_links'] = 5;
					$this->ajax_pagination->initialize($config);

					$data['results'] = $this->common_model->listallstdata("list", array('limit'=>$this->perPage,'start'=>$start),$fldname,$rsrcid);
					$data["outletlist"] = $this->common_model->getlistbywhere('outlets', 'fld_id,fld_name','fld_name asc','fld_clientid='.$this->clientid.'');	
					//$data['main_content'] = $tasktype.'orders';
					$data['tasktype'] = $tasktype;
					$data['main_content'] = 'storders';
					$data['mgmtname'] = $mgmtname;
					$this->load->view('warehouse/common/main.inc.php',$data);
				}
				else
				{
					redirect($rdurl);
					exit;
				}
			}
			else
			{
				redirect($rdurl);
				exit;
			}
			
		}
		else // for listing
		{
			$fromdt = $this->input->get("fromdate");
			$todt = $this->input->get("todate");
			if($fromdt!="")
			{
				 $fromdate = convertdateFormat($fromdt,'Y-m-d');
				 $data['fromdate'] = $fromdate;
			}
			if($todt!="")
			{				
				$todate = convertdateFormat($todt,'Y-m-d');
				$data['todate'] = $todate;
			}

			if($fromdt=="" && $todt=="")
			{
				$data['fromdate'] = date('Y-m-d');
				$data['todate'] = date('Y-m-d');
			}
			 $totalRec = $this->warehouse_model->liststockdata("count", '','wsale');

			//pagination configuration
			$config['base_url']    = base_url().'warehouse/'.$mgmtname;
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $this->perPage;
			$config['target']    = $this->perPage;
			
			//get the list data
			$pageid = $this->uri->segment(3);
			if($pageid=="" || $pageid==0)
			{
				$pageid = 1;
			}
			$start = ($pageid - 1) * $this->perPage;
			
			$config['uri_segment'] =  $start;
			$config['num_links'] = 5;
			$this->ajax_pagination->initialize($config);
			
			$data['results'] = $this->warehouse_model->liststockdata("list", array('limit'=>$this->perPage,'start'=>$start),'wsale');
			
			$data["warehouselist"] = $this->common_model->getlist('warehouse', 'fld_id,fld_name','fld_name');
			$data["outletlist"] = $this->common_model->getlist('outlets', 'fld_id,fld_name','fld_name');

			$data['main_content'] = $mgmtname;
			$data['mgmttbl'] = $mgmttbl;
			$data['mgmtname'] = $mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
	}
		
	function wbilling_OLD()
	{
		$mgmtname = 'wbilling';
		$mgmttbl = 'transaction';
		
		$tasktype = $this->uri->segment("3");
		if($tasktype=="manage")
		{
			$rsrcid = $this->uri->segment("4");
			$data["pgheading"] = "Create Bill";
			$data["rsrcid"] = 0;
			if($rsrcid!="")
			{
				$rsrcid = decodeValue($rsrcid);
				if($data["info4edit"] = $this->warehouse_model->getstockinfo($rsrcid,'wsale'))
				{
					$data["rsrcid"] = $rsrcid;
					$data["pgheading"] = "Modify Bill Info.";
				}
			}
			$data["warehouseinfo"] = $this->common_model->getinfo($this->warehouseid, "warehouse");
			$data["clientlist"] = $this->common_model->getlist('saleclients', 'fld_id,fld_name','fld_name');
			$data["warehouselist"] = $this->common_model->getlist('warehouse', 'fld_id,fld_name','fld_name');
			$data["productlist"] = $this->common_model->getlistbywhere('products', 'fld_id,fld_name','fld_name asc','');

			$data['mgmtname'] = $mgmtname;
			$data['main_content'] = "manage".$mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
		elseif($tasktype=="managedata")
		{
			$flashdatamsg =  "Please fill mandatory fields!";
			$flashdatacls = 'alert-danger';
				//predata($_REQUEST); exit;
				$rsrcid = $this->input->post("rsrcid");
				
				$clientid = $this->input->post("clientid");
				$warehouseid = $this->warehouseid;
				$refdate = $this->input->post("refdate");				
				$refno = $this->input->post("refno");

				$productid = $this->input->post("productid");
				$qty_case = $this->input->post("qty_case");
				$qty_bottle = $this->input->post("qty_bottle");
				$price_case = $this->input->post("price_case");
				$price_bottle = $this->input->post("price_bottle");
				$subtotal = $this->input->post("subtotal");
				$vatamount = $this->input->post("vatamount");
				$tscamount = $this->input->post("tscamount");
				$discount = $this->input->post("discount");
				$adjustment = $this->input->post("adjustment");

				$productttotalamount = $this->input->post("productttotalamount");
				$producttotalrows = $this->input->post("producttotalrows");
				
				if($clientid>0 && $warehouseid>0 && $refno!="" && $refdate!="" && $producttotalrows>0)
				{	
					$resourceid = $this->warehouse_model->managestock($this->input->post(),'wsale'); //exit;
					
					if($resourceid>0)
					{
						$flashdatacls =  'alert-success';
						if($rsrcid>0)
						{
							$flashdatamsg =  "Warehouse Billing info. successfully modified!";
						}
						else
						{
							$flashdatamsg =  "Warehouse Billing info. successfully added!";							
						}						 
					}
					elseif($resourceid==0)
					{
						$flashdatamsg =  "Sorry, info. you are looking for not found in our record!";
					}
					elseif($resourceid<0)
					{
						$flashdatamsg =  "Sorry, info. already exists!";
					}
				}

				$this->session->set_flashdata("message", $flashdatamsg);
				$this->session->set_flashdata('alerttype', $flashdatacls);
				
				redirect("warehouse/".$mgmtname."?". $_SERVER['QUERY_STRING']);
				exit;
		}
		elseif($tasktype=="view" || $tasktype=="print")
		{
				$rsrcid = $this->uri->segment("4");
				
				if($rsrcid!="")
				{
					$rsrcid = decodeValue($rsrcid);
					if($data["info4view"] = $this->common_model->getinfo($rsrcid, $mgmttbl))
					{
						$data["transinfo4view"] = $this->common_model->gettransinfolist($rsrcid);
						
						$data["rsrcid"] = $rsrcid;
						$data["pgheading"] = "View Billing Info.";
					}
				}
				$data['tasktype'] = $tasktype;
				$data['mgmtname'] = $mgmtname;
				$data['main_content'] = "view".$mgmtname;
				$this->load->view('warehouse/common/popup.inc.php',$data);
			
		}
		else // for listing
		{	
			
			$fromdt = $this->input->get("fromdate");
			$todt = $this->input->get("todate");
			if($fromdt!="")
			{
				 $fromdate = convertdateFormat($fromdt,'Y-m-d');
				 $data['fromdate'] = $fromdate;
			}
			if($todt!="")
			{				
				$todate = convertdateFormat($todt,'Y-m-d');
				$data['todate'] = $todate;
			}

			if($fromdt=="" && $todt=="")
			{
				$data['fromdate'] = date('Y-m-d');
				$data['todate'] = date('Y-m-d');
			}
			 $totalRec = $this->warehouse_model->liststockdata("count", '','wsale');

			//pagination configuration
			$config['base_url']    = base_url().'warehouse/'.$mgmtname;
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $this->perPage;
			$config['target']    = $this->perPage;
			
			//get the list data
			$pageid = $this->uri->segment(3);
			if($pageid=="" || $pageid==0)
			{
				$pageid = 1;
			}
			$start = ($pageid - 1) * $this->perPage;
			
			$config['uri_segment'] =  $start;
			$config['num_links'] = 5;
			$this->ajax_pagination->initialize($config);
			
			$data['results'] = $this->warehouse_model->liststockdata("list", array('limit'=>$this->perPage,'start'=>$start),'wsale');
			$data["clientlist"] = $this->common_model->getlist('saleclients', 'fld_id,fld_name','fld_name');
					
			$data['main_content'] = $mgmtname;
			$data['mgmtname'] = $mgmtname;
			$data['mgmttbl'] = $mgmttbl;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
	}

	function stocktransfer()
	{
		$mgmtname = 'stocktransfer';
		$mgmttbl = 'transaction';
		
		$fromdt = trim($this->input->get("fromdate"));
		$todt = trim($this->input->get("todate"));
		if($fromdt!="")
		{
			 $fromdate = convertdateFormat($fromdt,'Y-m-d');
			 $data['fromdate'] = $fromdate;
		}
		if($todt!="")
		{				
			$todate = convertdateFormat($todt,'Y-m-d');
			$data['todate'] = $todate;
		}

		if($fromdt=="" && $todt=="")
		{
			$data['fromdate'] = date('Y-m-d');
			$data['todate'] = date('Y-m-d');
		}
		if($this->input->get("outletid")!="")
		{
			$outletid = decodeValue($this->input->get("outletid"));		
		}
		
		$tasktype = $this->uri->segment("3");

		if($tasktype=="manage")
		{
			$rsrcid = $this->uri->segment("4");
			$data["pgheading"] = "Transfer Stock To Outlet";
			$data["rsrcid"] = 0;
			if($rsrcid!="")
			{
				$rsrcid = decodeValue($rsrcid);
				if($data["info4edit"] = $this->warehouse_model->getstockinfo($rsrcid,'stockout'))
				{
					$data["rsrcid"] = $rsrcid;
					$data["pgheading"] = "Modify Stock Transfer Info.";
				}
			}
			$data["productlist"] = $this->common_model->getwarehousestocklist($this->warehouseid);
			$data["outletlist"] = $this->common_model->getlist('outlets', 'fld_id,fld_name','fld_name');
			

			$data['mgmtname'] = $mgmtname;
			$data['main_content'] = "manage".$mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
		elseif($tasktype=="managedata")
		{
				$flashdatamsg =  "Please fill mandatory fields!";
				$flashdatacls = 'alert-danger';
				
				$rsrcid = $this->input->post("rsrcid");				
				$outletid = $this->input->post("outletid");
				$warehouseid = $this->warehouseid;
				$refdate = $this->input->post("refdate");				
				$refno = $this->input->post("refno");
				$productid = $this->input->post("productid");
				$qty_case = $this->input->post("qty_case");
				$qty_bottle = $this->input->post("qty_bottle");				
				$producttotalrows = $this->input->post("producttotalrows");
				
				if($outletid>0 && $warehouseid>0 && $producttotalrows>0)
				{
					$rtnvalps= $this->warehouse_model->checkproductstock($warehouseid,$productid,$qty_case,$qty_bottle,$rsrcid);
					$rtnvalpscnt=count($rtnvalps);					
					$productinfo ="";
					if($rtnvalpscnt>0)
					{
						$indexno=1;
						foreach($rtnvalps as $row)
						{							
							if($productinfo!="")
							{
								
								if($rtnvalpscnt==$indexno)
								{
									$productinfo .=" and ". $row['name'];
								}
								else
								{
									$productinfo .=",  ". $row['name'];
								}
							}
							else
							{
								$productinfo = $row['name'];
							}
							$indexno++;
						}
						$flashdatamsg  ='Error: the following product(s) are not in stock:
										- '.$productinfo.' ';
						$flashdatacls =  'alert-danger';
						$this->session->set_flashdata("message", $flashdatamsg);
						$this->session->set_flashdata('alerttype', $flashdatacls);
						
						redirect("warehouse/".$mgmtname."?". $_SERVER['QUERY_STRING']);
						exit;						
					}
					else
					{
						$resourceid = $this->warehouse_model->managestock($this->input->post(),'stockout');
						
						if($resourceid>0)
						{
							$flashdatacls =  'alert-success';
							if($rsrcid>0)
							{
								$flashdatamsg =  "Stock Transfer info. successfully modified!";
							}
							else
							{
								$flashdatamsg =  "Stock Transfer info. successfully added!";							
							}						 
						}
						elseif($resourceid==0)
						{
							$flashdatamsg =  "Sorry, info. you are looking for not found in our record!";
						}
						elseif($resourceid<0)
						{
							$flashdatamsg =  "Sorry, Challan NO. already exists!";
						}
					}
				}

				$this->session->set_flashdata("message", $flashdatamsg);
				$this->session->set_flashdata('alerttype', $flashdatacls);
				
				redirect("warehouse/".$mgmtname."?". $_SERVER['QUERY_STRING']);
				exit;
		}
		elseif($tasktype=="view" || $tasktype=="print")
		{
				$rsrcid = $this->uri->segment("4");
				
				if($rsrcid!="")
				{
					$rsrcid = decodeValue($rsrcid);
					if($data["info4view"] = $this->common_model->getstocktransferinfo($rsrcid,'warehouse'))
					{
						$data["transinfo4view"] = $this->common_model->gettransinfolist($rsrcid);
						
						$data["rsrcid"] = $rsrcid;
						$data["pgheading"] = "View Stock Transfer";
					}
				}
				$data['tasktype'] = $tasktype;				
				$data['mgmtname'] = $mgmtname;
				$data['main_content'] = "view".$mgmtname;
				$this->load->view('warehouse/common/popup.inc.php',$data);
			
		}
		elseif($tasktype=="warehouse" || $tasktype=="product" || $tasktype=="category" || $tasktype=="brand" || $tasktype=="producttype" || $tasktype=="productprice")
		{
			if($tasktype=="warehouse")
			{
				$mgmtname = 'warehouse';
				$mgmttbl = 'warehouse';
				$rdurl = 'warehouse/warehouse';
			}
			elseif($tasktype=="product")
			{
				$mgmtname = 'products';
				$mgmttbl = 'products';
				$rdurl = 'warehouse/products';
			}
			elseif($tasktype=="producttype")
			{
				$mgmtname = 'products';
				$mgmttbl = 'products';
				$rdurl = 'warehouse/products';
			}
			elseif($tasktype=="category")
			{
				$mgmtname = 'category';
				$mgmttbl = 'category';
				$rdurl = 'warehouse/category';
			}
			elseif($tasktype=="brand")
			{
				$mgmtname = 'brand';
				$mgmttbl = 'brand';
				$rdurl = 'warehouse/brand';
			}
			$rsrcid = $this->uri->segment("4");
			
			$data["rsrcid"] = 0;
			if($rsrcid!="")
			{
					
					if($tasktype=="producttype")
					{
						
						$data["info4edit"] = arrProductType()[$rsrcid];
					}
					else
					{
						$rsrcid = decodeValue($rsrcid);
						$data["info4edit"] = $this->common_model->getinfo($rsrcid, $mgmttbl);
					}
				if($data["info4edit"])
				{
					$fromdt = trim($this->input->get("fromdate"));
					$todt = trim($this->input->get("todate"));
					if($fromdt!="")
					{
						 $fromdate = convertdateFormat($fromdt,'Y-m-d');
						 $data['fromdate'] = $fromdate;
					}
					if($todt!="")
					{				
						$todate = convertdateFormat($todt,'Y-m-d');
						$data['todate'] = $todate;
					}

					if($fromdt=="" && $todt=="")
					{
						$data['fromdate'] = date('Y-m-d');
						$data['todate'] = date('Y-m-d');
					}

					$data["rsrcid"] = $rsrcid;
					if($tasktype=="warehouse")
					{
						 $data["pgheading"] = $data["info4edit"]->fld_mobile." > View Stock Transfer";
						 $fldname ='fld_warehouseid';
						 
					}
					elseif($tasktype=="category")
					{
						 $data["pgheading"] = $data["info4edit"]->fld_name." > View Stock Transfer";
						 $fldname ='fld_catid';
					}
					elseif($tasktype=="brand")
					{
						 $data["pgheading"] = $data["info4edit"]->fld_name." > View Stock Transfer";
						 $fldname ='fld_brandid';
					}
					elseif($tasktype=="product")
					{
						 $data["pgheading"] = $data["info4edit"]->fld_name." > View Stock Transfer";
						 $fldname ='fld_productid';

							
					}
					elseif($tasktype=="producttype")
					{
						 $data["pgheading"] = $data["info4edit"]." > View Stock Transfer";
						 $fldname ='fld_type';
					}


					if($tasktype=="producttype")
					{					
						
						$data["page_url"] = base_url().'warehouse/stocktransfer/'.$tasktype."/".$rsrcid;
					}
					else
					{
						$data["page_url"] = base_url().'warehouse/stocktransfer/'.$tasktype."/".encodeValue($rsrcid);
						
					}

					$totalRec = $this->common_model->listallstdata("count", '',$fldname,$rsrcid);

					//pagination configuration
					$config['base_url']    = $data["page_url"];
					$config['total_rows']  = $totalRec;
					$config['per_page']    = $this->perPage;
					$config['target']    = $this->perPage;
					
					//get the list data
					$pageid = $this->uri->segment(5);
					if($pageid=="" || $pageid==0)
					{
						$pageid = 1;
					}
					$start = ($pageid - 1) * $this->perPage;
					
					$config['uri_segment'] =  $start;
					$config['num_links'] = 5;
					$this->ajax_pagination->initialize($config);

					$data['results'] = $this->common_model->listallstdata("list", array('limit'=>$this->perPage,'start'=>$start),$fldname,$rsrcid);
					$data["outletlist"] = $this->common_model->getlistbywhere('outlets', 'fld_id,fld_name','fld_name asc','fld_clientid='.$this->clientid.'');	
					//$data['main_content'] = $tasktype.'orders';
					$data['tasktype'] = $tasktype;
					$data['main_content'] = 'storders';
					$data['mgmtname'] = $mgmtname;
					$this->load->view('warehouse/common/main.inc.php',$data);
				}
				else
				{
					redirect($rdurl);
					exit;
				}
			}
			else
			{
				redirect($rdurl);
				exit;
			}
			
		}
		elseif($tasktype=="exportpdf")
		{
			$warehouseinfo = $this->common_model->getinfo($this->warehouseid,'warehouse');
			$transfer2show = "Transferred from " . $warehouseinfo->fld_name;
			if($outletid>0)
			{
				$outletinfo = $this->common_model->getinfo($outletid,'outlets');
				$transfer2show .= " To " . $outletinfo->fld_name;
			}
			$datestr2show = "From " . date("d-M-Y", strtotime($fromdate)) . " To " . date("d-M-Y", strtotime($todate));
			
			$results = $this->common_model->stocktransferreditemwise("list", $this->warehouseid, '');
			
$previousstockdate = dfltdtFormat(date('d-M-Y', strtotime('-1 day', strtotime($data['fromdate']))));
			
			
$htmlcode ='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">

<head>
<title>TCPDF Examples</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="description" content="TCPDF is a PHP class for generating PDF documents on the fly" />
<meta name="author" content="Nicola Asuni" />
<meta name="keywords" content="Examples, TCPDF, PDF, PHP class" />
</head>

<body>

<h3 style="text-align:center;padding-bottom:0;margin-bottom:0;">Stock Transferred - Item Wise</h3>
<h4 style="text-align:center;padding-bottom:0;margin-bottom:0;">' . $transfer2show . '<br>' . $datestr2show . '</h4><br>

<table border="1" cellspecing="5" cellpadding="5">
		 
                <tbody>
                	<tr>
                    	<th width="10%" align="left"><strong>Serial</strong></th>                    	
                    	<th width="55%" align="left"><strong>Item</strong></th>
                    	<th width="20%" align="right"><strong>Qty. (Bottles)</strong></th>                       
                        <th width="15%"  align="right"><strong>Qty. (Case)</strong></th>
                    </tr>
                   ';
					

					if(is_array($results) && count(array_filter($results))>0)
					{
						$totalqtyinbottles = 0;
						$totalqtyincases = 0;
						$rowindex = 0;
						$instock = 0;
						foreach($results as  $row)
						{
							$rowindex++;
							$totalqtyinbottles += $row['netstock'];
							$totalqtyincases += $row['netstockincase'];


					
$htmlcode .='<tr>
<td width="10%">'.$rowindex.'</td>
<td width="55%">'.$row['name'].'</td>
<td width="20%" align="right">'.$row['netstock'].'</td>
<td  width="15%" align="right">'.$row['netstockincase'].'</td>
</tr>';
                   
						}
$htmlcode .='<tr>
<td width="10%">TOTAL</td>
<td width="55%"></td>
<td width="20%" align="right">'.$totalqtyinbottles.'</td>
<td  width="15%" align="right">'.$totalqtyincases.'</td>
</tr>';
						
					}
					
                    
                    $htmlcode .='</tbody>
                </table>

</body>
</html>';

				$this->load->library('Pdf');
				$pdf = new Pdf('P', 'mm', 'A4', true, 'UTF-8', false);
				$pdf->SetTitle('Stock Status PDF');
				$pdf->setPrintHeader(false);
				$pdf->setPrintFooter(false);
				$pdf->SetHeaderMargin(30);
				$pdf->SetTopMargin(20);
				$pdf->setFooterMargin(20);
				$pdf->SetAutoPageBreak(true,15);
				$pdf->SetAuthor('Author');
				$pdf->SetDisplayMode('real', 'default');
				$pdf->AddPage();
				//$pdf->Write(5, 'CodeIgniter TCPDF Integration');
				$pdf->writeHTML($htmlcode, true, false, false, false, '');
				//$pdf->Output('stockstatus_'.date('Ymdhsi').'.pdf', 'I');
				$pdf->Output('StockReportItemWise-'.date('Ymdhsi').'.pdf', 'D');
		}
		else // for listing
		{
			$fromdt = $this->input->get("fromdate");
			$todt = $this->input->get("todate");
			if($fromdt!="")
			{
				 $fromdate = convertdateFormat($fromdt,'Y-m-d');
				 $data['fromdate'] = $fromdate;
			}
			if($todt!="")
			{				
				$todate = convertdateFormat($todt,'Y-m-d');
				$data['todate'] = $todate;
			}

			if($fromdt=="" && $todt=="")
			{
				$data['fromdate'] = date('Y-m-d');
				$data['todate'] = date('Y-m-d');
			}
			 $totalRec = $this->warehouse_model->liststockdata("count", '','stockout');

			//pagination configuration
			$config['base_url']    = base_url().'warehouse/'.$mgmtname;
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $this->perPage;
			$config['target']    = $this->perPage;
			
			//get the list data
			$pageid = $this->uri->segment(3);
			if($pageid=="" || $pageid==0)
			{
				$pageid = 1;
			}
			$start = ($pageid - 1) * $this->perPage;
			
			$config['uri_segment'] =  $start;
			$config['num_links'] = 5;
			$this->ajax_pagination->initialize($config);
			
			$data['results'] = $this->warehouse_model->liststockdata("list", array('limit'=>$this->perPage,'start'=>$start),'stockout');
			
			$data["warehouselist"] = $this->common_model->getlist('warehouse', 'fld_id,fld_name','fld_name');
			$data["outletlist"] = $this->common_model->getlist('outlets', 'fld_id,fld_name','fld_name');

			$data['main_content'] = $mgmtname;
			$data['mgmttbl'] = $mgmttbl;
			$data['mgmtname'] = $mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
	}
	
	function checktpnumber()
	{
		$response = "";
		$tprefno = $this->input->get_post("tprefno");
		$tptype = $this->input->get_post("tptype");
		if($tprefno!="" && $tptype!="")
		{
			$response = $this->common_model->checktpnumber("warehouse", $tptype, $this->warehouseid, $tprefno);
		}
		echo $response;
	}
	
	function stockreceivedfromoutlet()
	{
		$mgmtname = 'stockreceivedfromoutlet';
		$mgmttbl = 'transaction';
		$tasktype = $this->uri->segment("3");
		if($tasktype=="confirm")
		{
			$flashdatamsg =  "Please fill mandatory fields!";
			$flashdatacls = 'alert-danger';
			$rsrcid = $this->uri->segment("4");
			if($rsrcid!="")
			{
				$rsrcid = decodeValue($rsrcid);
				if($this->warehouse_model->acknowledgestock($rsrcid))
				{
						$flashdatacls =  'alert-success';
						$flashdatamsg =  " Stock has been acknowledged successfully!";
				}
			}

			$this->session->set_flashdata("message", $flashdatamsg);
			$this->session->set_flashdata('alerttype', $flashdatacls);			
			redirect("warehouse/".$mgmtname."/?".$_SERVER['QUERY_STRING']);
			exit;
			
		}
		elseif($tasktype=="view" || $tasktype=="print")
		{
				$rsrcid = $this->uri->segment("4");
				
				if($rsrcid!="")
				{
					$rsrcid = decodeValue($rsrcid);
					if($data["info4view"] = $this->common_model->getstocktransferinfo($rsrcid,'o2w'))
					{
						$data["transinfo4view"] = $this->common_model->gettransinfolist($rsrcid);
						
						$data["rsrcid"] = $rsrcid;
						$data["pgheading"] = "View Stock Received From Outlet";
					}
				}
				$data['tasktype'] = $tasktype;				
				$data['mgmtname'] = $mgmtname;
				$data['main_content'] = "view".$mgmtname;
				$this->load->view('warehouse/common/popup.inc.php',$data);
			
		}
		elseif($tasktype=="warehouse" || $tasktype=="product" || $tasktype=="category" || $tasktype=="brand" || $tasktype=="producttype" || $tasktype=="productprice")
		{
			if($tasktype=="warehouse")
			{
				$mgmtname = 'warehouse';
				$mgmttbl = 'warehouse';
				$rdurl = 'warehouse/warehouse';
			}
			elseif($tasktype=="product")
			{
				$mgmtname = 'products';
				$mgmttbl = 'products';
				$rdurl = 'warehouse/products';
			}
			elseif($tasktype=="producttype")
			{
				$mgmtname = 'products';
				$mgmttbl = 'products';
				$rdurl = 'warehouse/products';
			}
			elseif($tasktype=="category")
			{
				$mgmtname = 'category';
				$mgmttbl = 'category';
				$rdurl = 'warehouse/category';
			}
			elseif($tasktype=="brand")
			{
				$mgmtname = 'brand';
				$mgmttbl = 'brand';
				$rdurl = 'warehouse/brand';
			}
			$rsrcid = $this->uri->segment("4");
			
			$data["rsrcid"] = 0;
			if($rsrcid!="")
			{
					
					if($tasktype=="producttype")
					{
						
						$data["info4edit"] = arrProductType()[$rsrcid];
					}
					else
					{
						$rsrcid = decodeValue($rsrcid);
						$data["info4edit"] = $this->common_model->getinfo($rsrcid, $mgmttbl);
					}
				if($data["info4edit"])
				{
					$fromdt = trim($this->input->get("fromdate"));
					$todt = trim($this->input->get("todate"));
					if($fromdt!="")
					{
						 $fromdate = convertdateFormat($fromdt,'Y-m-d');
						 $data['fromdate'] = $fromdate;
					}
					if($todt!="")
					{				
						$todate = convertdateFormat($todt,'Y-m-d');
						$data['todate'] = $todate;
					}

					if($fromdt=="" && $todt=="")
					{
						$data['fromdate'] = date('Y-m-d');
						$data['todate'] = date('Y-m-d');
					}

					$data["rsrcid"] = $rsrcid;
					if($tasktype=="warehouse")
					{
						 $data["pgheading"] = $data["info4edit"]->fld_mobile." > View Stock Transfer";
						 $fldname ='fld_warehouseid';
						 
					}
					elseif($tasktype=="category")
					{
						 $data["pgheading"] = $data["info4edit"]->fld_name." > View Stock Transfer";
						 $fldname ='fld_catid';
					}
					elseif($tasktype=="brand")
					{
						 $data["pgheading"] = $data["info4edit"]->fld_name." > View Stock Transfer";
						 $fldname ='fld_brandid';
					}
					elseif($tasktype=="product")
					{
						 $data["pgheading"] = $data["info4edit"]->fld_name." > View Stock Transfer";
						 $fldname ='fld_productid';

							
					}
					elseif($tasktype=="producttype")
					{
						 $data["pgheading"] = $data["info4edit"]." > View Stock Transfer";
						 $fldname ='fld_type';
					}


					if($tasktype=="producttype")
					{					
						
						$data["page_url"] = base_url().'warehouse/stocktransfer/'.$tasktype."/".$rsrcid;
					}
					else
					{
						$data["page_url"] = base_url().'warehouse/stocktransfer/'.$tasktype."/".encodeValue($rsrcid);
						
					}

					$totalRec = $this->common_model->listallstdata("count", '',$fldname,$rsrcid);

					//pagination configuration
					$config['base_url']    = $data["page_url"];
					$config['total_rows']  = $totalRec;
					$config['per_page']    = $this->perPage;
					$config['target']    = $this->perPage;
					
					//get the list data
					$pageid = $this->uri->segment(5);
					if($pageid=="" || $pageid==0)
					{
						$pageid = 1;
					}
					$start = ($pageid - 1) * $this->perPage;
					
					$config['uri_segment'] =  $start;
					$config['num_links'] = 5;
					$this->ajax_pagination->initialize($config);

					$data['results'] = $this->common_model->listallstdata("list", array('limit'=>$this->perPage,'start'=>$start),$fldname,$rsrcid);
					$data["outletlist"] = $this->common_model->getlistbywhere('outlets', 'fld_id,fld_name','fld_name asc','fld_clientid='.$this->clientid.'');	
					$data["warehouselist"] = $this->common_model->getlistbywhere('warehouse', 'fld_id,fld_name','fld_name asc','fld_clientid='.$this->clientid.'');
					//$data['main_content'] = $tasktype.'orders';
					$data['tasktype'] = $tasktype;
					$data['main_content'] = 'storders';
					$data['mgmtname'] = $mgmtname;
					$this->load->view('warehouse/common/main.inc.php',$data);
				}
				else
				{
					redirect($rdurl);
					exit;
				}
			}
			else
			{
				redirect($rdurl);
				exit;
			}
			
		}
		else // for listing
		{
			$fromdt = $this->input->get("fromdate");
			$todt = $this->input->get("todate");
			if($fromdt!="")
			{
				 $fromdate = convertdateFormat($fromdt,'Y-m-d');
				 $data['fromdate'] = $fromdate;
			}
			if($todt!="")
			{				
				$todate = convertdateFormat($todt,'Y-m-d');
				$data['todate'] = $todate;
			}

			if($fromdt=="" && $todt=="")
			{
				$data['fromdate'] = date('Y-m-d');
				$data['todate'] = date('Y-m-d');
			}
			 $totalRec = $this->warehouse_model->liststockdata("count", '','o2w');

			//pagination configuration
			$config['base_url']    = base_url().'warehouse/'.$mgmtname;
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $this->perPage;
			$config['target']    = $this->perPage;
			
			//get the list data
			$pageid = $this->uri->segment(3);
			if($pageid=="" || $pageid==0)
			{
				$pageid = 1;
			}
			$start = ($pageid - 1) * $this->perPage;
			
			$config['uri_segment'] =  $start;
			$config['num_links'] = 5;
			$this->ajax_pagination->initialize($config);
			
			$data['results'] = $this->warehouse_model->liststockdata("list", array('limit'=>$this->perPage,'start'=>$start),'o2w');
			
			$data["warehouselist"] = $this->common_model->getlist('warehouse', 'fld_id,fld_name','fld_name');
			$data["outletlist"] = $this->common_model->getlist('outlets', 'fld_id,fld_name','fld_name');

			$data['main_content'] = $mgmtname;
			$data['mgmttbl'] = $mgmttbl;
			$data['mgmtname'] = $mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
	}

	function stockstatus()
	{
		$mgmtname = 'stockstatus';
		$mgmttbl = 'transaction';
		$tasktype = $this->uri->segment("3");

			$fromdt = $this->input->get("fromdate");
			$todt = $this->input->get("todate");
			if($fromdt!="")
			{
				 $fromdate = convertdateFormat($fromdt,'Y-m-d');
				 $data['fromdate'] = $fromdate;
			}
			if($todt!="")
			{				
				$todate = convertdateFormat($todt,'Y-m-d');
				$data['todate'] = $todate;
			}

			if($fromdt=="" && $todt=="")
			{
				$data['fromdate'] = date('Y-m-d');
				$data['todate'] = date('Y-m-d');
			}
		if($tasktype=="exportcsv")
		{
					$warehouseid = $this->warehouseid;
					$warehouseinfo = $this->common_model->getinfo($warehouseid,'warehouse');					
					$filename = "StockStatus";
					if($warehouseinfo->fld_name!="")
					{
						$filename = "StockStatus_". $warehouseinfo->fld_name;
					}
					//$results = $this->common_model->exportStockList($warehouseid);
					
					
					$results = $this->warehouse_model->liststockstatusdata("list", '');
					//predata($results); exit;
					$previousstockdate = dfltdtFormat(date('d-M-Y', strtotime('-1 day', strtotime($data['fromdate']))));

					
					ob_end_clean();	
					$headerrow = 0;
					// Set headers for file name, MIME type, and caching
					header("Content-type: application/csv");
					header("Content-Disposition: attachment; filename=\"".$filename.".csv\"");
					header("Pragma: no-cache");
					header("Expires: 0");
					$fh = fopen('php://output', 'w');
					$rowtitle = array(
						
						"name" => 'Item',
						"previousstock" => 'Stock On '. $previousstockdate,
						"incoming" => 'Stock IN',
						"outgoing" => 'Stock OUT',
						"instock" => 'Stock In Hand',
					);
					foreach($results as $row)
					{
						unset($row['id']);
						unset($row['minstockvalue']);
						if($headerrow<1)
						{							
							
							$headerrow = 1;
							$mycustomheader = array_values($rowtitle);
							fputcsv($fh, $mycustomheader);
						}
						fputcsv($fh, $row);
					}
					fclose($fh);
				
		}
		elseif($tasktype=="exportpdf")
		{
					$warehouseid = $this->warehouseid;
					$warehouseinfo = $this->common_model->getinfo($warehouseid,'warehouse');					
					$filename = "StockStatus";
					if($warehouseinfo->fld_name!="")
					{
						$filename = "StockStatus_". $warehouseinfo->fld_name;
					}
					//$results = $this->common_model->exportStockList($warehouseid);
					$results = $this->warehouse_model->liststockstatusdata("list", '');
					//predata($results); exit;
					$previousstockdate = dfltdtFormat(date('d-M-Y', strtotime('-1 day', strtotime($data['fromdate']))));
			
			
$htmlcode ='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">

<head>
<title>TCPDF Examples</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="description" content="TCPDF is a PHP class for generating PDF documents on the fly" />
<meta name="author" content="Nicola Asuni" />
<meta name="keywords" content="Examples, TCPDF, PDF, PHP class" />
</head>

<body>

<table border="1" cellspecing="5" cellpadding="5">
		 
                <tbody>
                	<tr>
                    	<th width="10%"><strong>SRNo.</strong></th>                    	
                    	<th width="30%"><strong>Item</strong></th>
						<th width="15%">Stock On <br> '. $previousstockdate.'</th>
                    	<th width="15%" align="center"><strong>Stock IN</strong></th>
                    	<th width="15%" align="center"><strong>Stock OUT</strong></th>                       
                        <th width="15%"  align="center"><strong>Instock</strong></th>
                    </tr>
                    ';
					

					if(is_array($results) && count(array_filter($results))>0)
					{
						$rowindex = 0;
						$instock = 0;
						foreach($results as  $row)
						{
							$rowindex++;


					
$htmlcode .='<tr>
<td width="10%">'.$rowindex.'</td>
<td width="30%">'.$row['name'].'</td>
<td width="15%" align="center">'.$row['previousstock'].'</td>
<td  width="15%" align="center">'.$row['incoming'].'</td>
<td width="15%" align="center">'.$row['outgoing'].'</td>               
<td width="15%" align="center">'.$row['instock'].'</td>
</tr>';
                   
						}
					}
					
                    
                    $htmlcode .='</tbody>
                </table>

</body>
</html>';
				$this->load->library('Pdf');
				$pdf = new Pdf('P', 'mm', 'A4', true, 'UTF-8', false);
				$pdf->SetTitle('');
				$pdf->setPrintHeader(false);
				$pdf->setPrintFooter(false);
				$pdf->SetHeaderMargin(30);
				$pdf->SetTopMargin(20);
				$pdf->setFooterMargin(20);
				$pdf->SetAutoPageBreak(true,15);
				$pdf->SetAuthor('Author');
				$pdf->SetDisplayMode('real', 'default');
				$pdf->AddPage();
				//$pdf->Write(5, 'CodeIgniter TCPDF Integration');
				$pdf->writeHTML($htmlcode, true, false, false, false, '');
				//$pdf->Output('stockstatus_'.date('Ymdhsi').'.pdf', 'I');
				$pdf->Output($filename."_".date('Ymdhsi').'.pdf', 'D');
		}
		else
		{

			 $totalRec = $this->warehouse_model->liststockstatusdata("count", '');

			//pagination configuration
			$config['base_url']    = base_url().'warehouse/'.$mgmtname;
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $this->perPage;
			$config['target']    = $this->perPage;
			
			//get the list data
			$pageid = $this->uri->segment(3);
			if($pageid=="" || $pageid==0)
			{
				$pageid = 1;
			}
			$start = ($pageid - 1) * $this->perPage;
			
			$config['uri_segment'] =  $start;
			$config['num_links'] = 5;
			$this->ajax_pagination->initialize($config);
			
			$data['results'] = $this->warehouse_model->liststockstatusdata("list", array('limit'=>$this->perPage,'start'=>$start));
			
			$data["warehouselist"] = $this->common_model->getlist('warehouse', 'fld_id,fld_name','fld_name');
			$data["outletlist"] = $this->common_model->getlist('outlets', 'fld_id,fld_name','fld_name');
			$data["catlist"] = $this->common_model->getlist('category', 'fld_id,fld_name','fld_name');
			$data["brandlist"] = $this->common_model->getlist('brand', 'fld_id,fld_name','fld_name');			
			
			$data['main_content'] = $mgmtname;
			$data['mgmtname'] = $mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
		
	}
	function outletsstock()
	{
		$mgmtname = 'outletsstock';
		$mgmttbl = 'transaction';
			$fromdt = trim($this->input->get("fromdate"));
			$todt = trim($this->input->get("todate"));
			if($fromdt!="")
			{
				 $fromdate = convertdateFormat($fromdt,'Y-m-d');
				 $data['fromdate'] = $fromdate;
			}
			if($todt!="")
			{				
				$todate = convertdateFormat($todt,'Y-m-d');
				$data['todate'] = $todate;
			}

			if($fromdt=="" && $todt=="")
			{
				$data['fromdate'] = date('Y-m-d');
				$data['todate'] = date('Y-m-d');
			}
		if($this->input->get("outletid")!="")
		{
			$outletid = decodeValue($this->input->get("outletid"));		
		}

		$tasktype = $this->uri->segment("3");
		if($tasktype=="exportcsv" && $outletid>0)
		{
			
			$results = $this->common_model->listoutletsstock("list", '',$outletid);
			
				$previousstockdate = dfltdtFormat(date('d-M-Y', strtotime('-1 day', strtotime($data['fromdate']))));
					//$results = $this->common_model->listoutletsstock("list", '',$outletid);
					$outletinfo = $this->common_model->getinfo($outletid,'outlets');
					//predata($outletinfo); exit;
					ob_end_clean();					

					$filename = "OutletStock_". $outletinfo->fld_name;
					$headerrow = 0;
					// Set headers for file name, MIME type, and caching
					header("Content-type: application/csv");
					header("Content-Disposition: attachment; filename=\"".$filename.".csv\"");
					header("Pragma: no-cache");
					header("Expires: 0");
					$fh = fopen('php://output', 'w');
					$rowtitle = array(
						
						"name" => 'Item',
						"previousstock" => 'Stock On '. $previousstockdate,
						"incoming" => 'Stock IN',
						"outgoing" => 'Stock OUT',
						"instock" => 'Stock In Hand',
					);
					foreach($results as $row)
					{
						unset($row['id']);
						unset($row['minstockvalue']);
						if($headerrow<1)
						{							
							
							$headerrow = 1;
							$mycustomheader = array_values($rowtitle);
							fputcsv($fh, $mycustomheader);
						}
						fputcsv($fh, $row);
					}
					fclose($fh);
				
		}
		elseif($tasktype=="exportpdf" && $outletid>0)
		{
			$results = $this->common_model->listoutletsstock("list", '',$outletid);
			
$previousstockdate = dfltdtFormat(date('d-M-Y', strtotime('-1 day', strtotime($data['fromdate']))));
			
			
$htmlcode ='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">

<head>
<title>TCPDF Examples</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="description" content="TCPDF is a PHP class for generating PDF documents on the fly" />
<meta name="author" content="Nicola Asuni" />
<meta name="keywords" content="Examples, TCPDF, PDF, PHP class" />
</head>

<body>

<table border="1" cellspecing="5" cellpadding="5">
		 
                <tbody>
                	<tr>
                    	<th width="10%"><strong>SRNo.</strong></th>                    	
                    	<th width="30%"><strong>Item</strong></th>
						<th width="15%">Stock On <br> '. $previousstockdate.'</th>
                    	<th width="15%" align="center"><strong>Stock IN</strong></th>
                    	<th width="15%" align="center"><strong>Stock OUT</strong></th>                       
                        <th width="15%"  align="center"><strong>Instock</strong></th>
                    </tr>
                   ';
					

					if(is_array($results) && count(array_filter($results))>0)
					{
						$rowindex = 0;
						$instock = 0;
						foreach($results as  $row)
						{
							$rowindex++;


					
$htmlcode .='<tr>
<td width="10%">'.$rowindex.'</td>
<td width="30%">'.$row['name'].'</td>
<td width="15%" align="center">'.$row['previousstock'].'</td>
<td  width="15%" align="center">'.$row['incoming'].'</td>
<td width="15%" align="center">'.$row['outgoing'].'</td>               
<td width="15%" align="center">'.$row['instock'].'</td>
</tr>';
                   
						}
					}
					
                    
                    $htmlcode .='</tbody>
                </table>

</body>
</html>';
				$this->load->library('Pdf');
				$pdf = new Pdf('P', 'mm', 'A4', true, 'UTF-8', false);
				$pdf->SetTitle('Stock Status PDF');
				$pdf->setPrintHeader(false);
				$pdf->setPrintFooter(false);
				$pdf->SetHeaderMargin(30);
				$pdf->SetTopMargin(20);
				$pdf->setFooterMargin(20);
				$pdf->SetAutoPageBreak(true,15);
				$pdf->SetAuthor('Author');
				$pdf->SetDisplayMode('real', 'default');
				$pdf->AddPage();
				//$pdf->Write(5, 'CodeIgniter TCPDF Integration');
				$pdf->writeHTML($htmlcode, true, false, false, false, '');
				//$pdf->Output('stockstatus_'.date('Ymdhsi').'.pdf', 'I');
				$pdf->Output('stockstatus_'.date('Ymdhsi').'.pdf', 'D');
		}
		else
		{

			
			 $totalRec = $this->common_model->listoutletsstock("count", '',$outletid);

			//pagination configuration
			$config['base_url']    = base_url().'warehouse/'.$mgmtname;
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $this->perPage;
			$config['target']    = $this->perPage;
			
			//get the list data
			$pageid = $this->uri->segment(3);
			if($pageid=="" || $pageid==0)
			{
				$pageid = 1;
			}
			$start = ($pageid - 1) * $this->perPage;
			
			$config['uri_segment'] =  $start;
			$config['num_links'] = 5;
			$this->ajax_pagination->initialize($config);
			
			$data['results'] = $this->common_model->listoutletsstock("list", array('limit'=>$this->perPage,'start'=>$start),$outletid);
			

			
			$data["outletlist"] = $this->common_model->getlist('outlets', 'fld_id,fld_name','fld_name');
			$data["catlist"] = $this->common_model->getlist('category', 'fld_id,fld_name','fld_name');
			$data["brandlist"] = $this->common_model->getlist('brand', 'fld_id,fld_name','fld_name');			
			
			$data['main_content'] = $mgmtname;
			$data['mgmtname'] = $mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
		
	}
	function getwarehousestockinfo()
	{
			//predata($_REQUEST);			
			$warehouseid = $this->input->get_post("warehouseid");
			$rsrcid = $this->input->post("rsrcid");
			if($rsrcid!="" && $rsrcid>0)
			{
				$data['rsrcid'] = $rsrcid;
				$data["info4edit"] = $this->warehouse_model->getstockinfo($rsrcid,'stockout');
			}		
			$data["productlist"] = $this->common_model->getwarehousestocklist($warehouseid);			
			$data['main_content'] = $mgmtname;
			$data['mgmtname'] = $mgmtname;
			$this->load->view('warehouse/getwarehousestockinfo',$data);
	}
	function removecommonrow()
	{
		$response = "failed";
		$task = $this->input->get_post("task");
		$rowid = $this->input->get_post("rowid");
		if($task=="product" || $task=="demandproduct")
		{
			if($rowid>0)
			{
				if($task=="product") { $tablename ='transactioninfo' ;}
				elseif($task=="demandproduct") { $tablename ='demandinfo' ;}
				if($this->common_model->delinfocomm($rowid, $tablename))
				{
					$response = "success";
				}
			}
		}

		echo $response;
	}

	function orders()
	{			
		$tasktype = $this->uri->segment("3");
		if($tasktype=="customer" || $tasktype=="product" || $tasktype=="category" || $tasktype=="brand" || $tasktype=="producttype" || $tasktype=="productprice")
		{
			if($tasktype=="customer")
			{
				$mgmtname = 'customers';
				$mgmttbl = 'customers';
				$rdurl = 'warehouse/customers';
			}
			elseif($tasktype=="product")
			{
				$mgmtname = 'products';
				$mgmttbl = 'products';
				$rdurl = 'warehouse/products';
			}
			elseif($tasktype=="producttype")
			{
				$mgmtname = 'products';
				$mgmttbl = 'products';
				$rdurl = 'warehouse/products';
			}
			elseif($tasktype=="category")
			{
				$mgmtname = 'category';
				$mgmttbl = 'category';
				$rdurl = 'warehouse/category';
			}
			elseif($tasktype=="brand")
			{
				$mgmtname = 'brand';
				$mgmttbl = 'brand';
				$rdurl = 'warehouse/brand';
			}
			$rsrcid = $this->uri->segment("4");
			
			$data["rsrcid"] = 0;
			if($rsrcid!="")
			{
					
					if($tasktype=="producttype")
					{
						
						$data["info4edit"] = arrProductType()[$rsrcid];
					}
					else
					{
						$rsrcid = decodeValue($rsrcid);
						$data["info4edit"] = $this->common_model->getinfo($rsrcid, $mgmttbl);
					}
				if($data["info4edit"])
				{
					$fromdt = trim($this->input->get("fromdate"));
					$todt = trim($this->input->get("todate"));
					if($fromdt!="")
					{
						 $fromdate = convertdateFormat($fromdt,'Y-m-d');
						 $data['fromdate'] = $fromdate;
					}
					if($todt!="")
					{				
						$todate = convertdateFormat($todt,'Y-m-d');
						$data['todate'] = $todate;
					}

					if($fromdt=="" && $todt=="")
					{
						$data['fromdate'] = date('Y-m-d');
						$data['todate'] = date('Y-m-d');
					}

					$data["rsrcid"] = $rsrcid;
					if($tasktype=="customer")
					{
						 $data["pgheading"] = $data["info4edit"]->fld_mobile." > View Orders";
						 $fldname ='fld_customerid';
						 
					}
					elseif($tasktype=="category")
					{
						 $data["pgheading"] = $data["info4edit"]->fld_name." > View Orders";
						 $fldname ='fld_catid';
					}
					elseif($tasktype=="brand")
					{
						 $data["pgheading"] = $data["info4edit"]->fld_name." > View Orders";
						 $fldname ='fld_brandid';
					}
					elseif($tasktype=="product")
					{
						 $data["pgheading"] = $data["info4edit"]->fld_name." > View Orders";
						 $fldname ='fld_productid';
							if($data["info4edit"]->cattype=='gift')
							{
								$fldname ='fld_gprodid';
							}
							
					}
					elseif($tasktype=="producttype")
					{
						 $data["pgheading"] = $data["info4edit"]." > View Orders";
						 $fldname ='fld_type';
					}


					if($tasktype=="producttype")
					{					
						
						$data["page_url"] = base_url().'warehouse/orders/'.$tasktype."/".$rsrcid;
					}
					else
					{
						$data["page_url"] = base_url().'warehouse/orders/'.$tasktype."/".encodeValue($rsrcid);
						
					}
					//$totalRec = $this->common_model->listordersdata("count", '',$fldname,$rsrcid);
					$totalRec = $this->common_model->listallordersdata("count", '',$fldname,$rsrcid);

					//pagination configuration
					$config['base_url']    = $data["page_url"];
					$config['total_rows']  = $totalRec;
					$config['per_page']    = $this->perPage;
					$config['target']    = $this->perPage;
					
					//get the list data
					$pageid = $this->uri->segment(5);
					if($pageid=="" || $pageid==0)
					{
						$pageid = 1;
					}
					$start = ($pageid - 1) * $this->perPage;
					
					$config['uri_segment'] =  $start;
					$config['num_links'] = 5;
					$this->ajax_pagination->initialize($config);
					
					//$data['results'] = $this->common_model->listordersdata("list", array('limit'=>$this->perPage,'start'=>$start),$fldname,$rsrcid);
					$data['results'] = $this->common_model->listallordersdata("list", array('limit'=>$this->perPage,'start'=>$start),$fldname,$rsrcid);
					$data["outletlist"] = $this->common_model->getlistbywhere('outlets', 'fld_id,fld_name','fld_name asc','fld_clientid='.$this->clientid.'');			
					//$data['main_content'] = $tasktype.'orders';
					$data['tasktype'] = $tasktype;
					$data['main_content'] = 'orders';
					$data['mgmtname'] = $mgmtname;
					$this->load->view('warehouse/common/main.inc.php',$data);
				}
				else
				{
					redirect($rdurl);
					exit;
				}
			}
			else
			{
				redirect($rdurl);
				exit;
			}
			
		}
		
		
	}
	function customers()
	{
		$mgmtname = 'customers';
			$fromdt = trim($this->input->get("fromdate"));
			$todt = trim($this->input->get("todate"));
			if($fromdt!="")
			{
				 $fromdate = convertdateFormat($fromdt,'Y-m-d');
				 $data['fromdate'] = $fromdate;
			}
			if($todt!="")
			{				
				$todate = convertdateFormat($todt,'Y-m-d');
				$data['todate'] = $todate;
			}

			if($fromdt=="" && $todt=="")
			{
				$data['fromdate'] = date('Y-m-d');
				$data['todate'] = date('Y-m-d');
			}		
			$totalRec = $this->common_model->listcustomerdata("count", '');

			//pagination configuration
			$config['base_url']    = base_url().'outlet'.$mgmtname;
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $this->perPage;
			$config['target']    = $this->perPage;
			
			//get the list data
			$pageid = $this->uri->segment(3);
			if($pageid=="" || $pageid==0)
			{
				$pageid = 1;
			}
			$start = ($pageid - 1) * $this->perPage;
			
			$config['uri_segment'] =  $start;
			$config['num_links'] = 5;
			$this->ajax_pagination->initialize($config);
			
			$data['results'] = $this->common_model->listcustomerdata("list", array('limit'=>$this->perPage,'start'=>$start));
			$data["outletlist"] = $this->common_model->getlist('outlets', 'fld_id,fld_name','fld_name');
			$data['main_content'] = $mgmtname;
			$data['mgmtname'] = $mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		
	}
	function billing()
	{
		$mgmtname = 'billing';
		$mgmttbl = 'billing';
			$fromdt = trim($this->input->get("fromdate"));
			$todt = trim($this->input->get("todate"));
			if($fromdt!="")
			{
				 $fromdate = convertdateFormat($fromdt,'Y-m-d');
				 $data['fromdate'] = $fromdate;
			}
			if($todt!="")
			{				
				$todate = convertdateFormat($todt,'Y-m-d');
				$data['todate'] = $todate;
			}

			if($fromdt=="" && $todt=="")
			{
				$data['fromdate'] = date('Y-m-d');
				$data['todate'] = date('Y-m-d');
			}
		$tasktype = $this->uri->segment("3");
		if($tasktype=="printbill" || $tasktype=="view")
		{
				$rsrcid = $this->uri->segment("4");				
				if($rsrcid!="")
				{
					$rsrcid = decodeValue($rsrcid);
					if($data["info4view"] = $this->common_model->getinfo($rsrcid, $mgmttbl))
					{
						$data["dbuserinfo"] = $this->common_model->getinfo($data["info4view"]->fld_userid, "users");
						$data["transinfo4view"] = $this->common_model->getbillinginfolist($rsrcid);						
						$data["rsrcid"] = $rsrcid;
						$data["pgheading"] = "View ".ucfirst($mgmtname)." Info.";
					}
				}
				//predata($data["info4edit"]);
				
				$data['tasktype'] = $tasktype;
				$data['mgmtname'] = $mgmtname;
				$data['main_content'] = "view".$mgmtname;
				if($tasktype=='view')
				{
					$this->load->view("warehouse/view".$mgmtname,$data);
				}
				else
				{
					$this->load->view("warehouse/view".$mgmtname,$data);
				}
			
		}
		elseif($tasktype=="cnclrqtsprocess")
		{
			$rsrcid = $this->uri->segment("4");			
			$data["rsrcid"] = 0;
			if($rsrcid!="")
			{
				 $rsrcid = decodeValue($rsrcid);
				if($data["info4edit"] = $this->common_model->getinfo($rsrcid,$mgmtname))
				{					
					$cancellation_reason = $this->input->post("cancellation_reason");
					$remarks = $this->input->post("remarks");
					$status = $this->input->post("status");
					if($data["info4edit"]->fld_status=='pending' && $status!="")
					{						
						$data2 = array(							
							"fld_status" => $status,
							"fld_cancellationdate" => date("Y-m-d H:i:s"),
							"fld_updatedby" => $this->lguserinfo["sessID"],
							"fld_updatedon" => date("Y-m-d H:i:s")	
						);						
						if($this->common_model->updatedata($mgmtname, $rsrcid, $data2))
						{
							$flashdatacls =  'btn btn-success';
							$flashdatamsg =  "Request submited! successfully";
						}
						else
						{
							$flashdatacls =  'btn btn-danger';
							$flashdatamsg =  "something went wrong, Please try again!";
						}
						
						$this->session->set_flashdata("message2", $flashdatamsg);
						$this->session->set_flashdata('alerttype2', $flashdatacls);

					}

					$data["pgheading"] = "Cancel Order Process";
					$data["rsrcid"] = $rsrcid;
					//predata($data["info4edit"]); 
					$data['mgmtname'] = $mgmtname;
					$data['main_content'] = "cnclrqtsprocess";
					$this->load->view('warehouse/common/popup.inc.php',$data);
				}
				
			}
			
		}
		elseif($tasktype=="cnclrqts")
		{
			$outletid = decodeValue($this->input->get("outletid"));
			$totalRec = $this->common_model->listbillingdata("count", '');

			//pagination configuration
			$config['base_url']    = base_url().'warehouse/'.$mgmtname."/cnclrqts";
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $this->perPage;
			$config['target']    = $this->perPage;

			//get the list data
			$pageid = $this->uri->segment(3);
			if($pageid=="" || $pageid==0)
			{
				$pageid = 1;
			}
			$start = ($pageid - 1) * $this->perPage;

			$config['uri_segment'] =  $start;
			$config['num_links'] = 5;
			$this->ajax_pagination->initialize($config);

			$data['results'] = $this->common_model->listbillingdata("list",array('limit'=>$this->perPage,'start'=>$start));

			$data["outletlist"] = $this->common_model->getlist('outlets', 'fld_id,fld_name','fld_name');
			$data['main_content'] = 'cnclrqts';
			$data['mgmtname'] = $mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
		elseif($tasktype=="exportcsv" || $tasktype=="exportpdf")
		{				
					
					$outletid = decodeValue($this->input->get("outletid"));
					$outletinfo = $this->common_model->getinfo($outletid,'outlets');		
					//predata($outletinfo);
					$filedate = date('d-M-Y', strtotime($data['fromdate']));
					$filename = "billing_".$filedate;
					if($outletinfo->fld_name!="")
					{

						$filename = $outletinfo->fld_name."_billing_".$filedate;
					}		
					
					$results = $this->common_model->listbillingdata("list", '');					

					$previousstockdate = dfltdtFormat(date('d-M-Y', strtotime('-1 day', strtotime($data['fromdate']))));
					if($tasktype=="exportcsv")
					{
						ob_end_clean();	
						$headerrow = 0;
						// Set headers for file name, MIME type, and caching
						header("Content-type: application/csv");
						header("Content-Disposition: attachment; filename=\"".$filename.".csv\"");
						header("Pragma: no-cache");
						header("Expires: 0");
						$fh = fopen('php://output', 'w');
						$rowtitle = array(

							"outlet" => 'Outlet',
							"invoiceno" => 'Invoice',
							"customer" => 'Customer',							
							"discount" => 'Discount/Gifts',
							"amount" => 'Amount',

						);

						$rowindex = 0;
						foreach($results as $row)
						{
								$rowindex++;
								$discount ="";
								$pdiscount ="";
								$hightlightcls ="";
								$dicountinfo="";
								if($row['fld_discount']>0)
								{
									$discount = $row['fld_discount'];	
									$amount = $row['fld_netamount']+$discount;
									$pdiscount  = ($discount/$amount)*100;
									$dicountinfo  = round($discount,2) ." (" .round($pdiscount,2). "%)";
												

								}
								else if($row['fld_gqty']>0)
								{
									$dicountinfo = $row['giftname'];
									
								}
								$customer = "";
								if($row['fld_name']!="")
								{
									$customer = $row['fld_name'];
								}
								if($row['fld_mobile']!="")
								{
									$customer = $row['fld_mobile'];
								}
								if($row['fld_mobile']!="" && $row['fld_name']!="")
								{
									$customer = $row['fld_name']." <br> ".$row['fld_mobile'];
								}
								$billdate = "";
								if($row['fld_billdate'])
								{
									$billdate = date("d-M-Y", strtotime($row['fld_billdate']));
								}
								$postedon = "";
								if($row['fld_datetime'])
								{
									$postedon = date("d-M-Y", strtotime($row['fld_datetime']));
								}
								$status  = arrOrderStatus()[$row['fld_status']];
								$invoiceinfo = "";
								
								if($row['fld_billno'])
								{
									$invoiceinfo = $row['fld_billno']." " .$billdate;
								}
								if($status)
								{
									$invoiceinfo .= " ,". $status;
								}
								$rowvalue = array(
									
									"outlet" => $row['outletname'],
									"invoice" => $invoiceinfo,
									"customer" => $customer,									
									"discount" => $dicountinfo,
									"amount" => $row['fld_netamount'],

								);

								if($headerrow<1)
								{							
									$headerrow = 1;
									$mycustomheader = array_values($rowtitle);
									fputcsv($fh, $mycustomheader);
								}
								fputcsv($fh, $rowvalue);
						}
						fclose($fh);
					}
					elseif($tasktype=="exportpdf")
					{
$htmlcode ='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">

<head>
<title>TCPDF Examples</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="description" content="TCPDF is a PHP class for generating PDF documents on the fly" />
<meta name="author" content="Nicola Asuni" />
<meta name="keywords" content="Examples, TCPDF, PDF, PHP class" />
</head>

<body>

<table border="1" cellspecing="5" cellpadding="5">
		 
                <tbody>
                	<tr>
                    	<th width="10%">SRNo.</th>
                    	<th width="20%">Outlet</th>
                    	<th width="20%">Invoice</th>                   	
                    	<th width="20%">Customer</th>
						<th width="16%">Discount/Gifts</th>
						<th width="14%">Amount</th>
                    </tr>
                   ';
					

					if(is_array($results) && count(array_filter($results))>0)
					{
						$rowindex = 0;

						foreach($results as $row)
						{
								$rowindex++;
								$discount ="";
								$pdiscount ="";
								$hightlightcls ="";
								$dicountinfo="";
								if($row['fld_discount']>0)
								{
									$discount = $row['fld_discount'];	
									$amount = $row['fld_netamount']+$discount;
									$pdiscount  = ($discount/$amount)*100;
									$dicountinfo  = round($discount,2) ." (" .round($pdiscount,2). "%)";							

								}
								else if($row['fld_gqty']>0)
								{
									$dicountinfo = $row['giftname'];
									
								}
								$customer = "";
								if($row['fld_name']!="")
								{
									$customer = $row['fld_name'];
								}
								if($row['fld_mobile']!="")
								{
									$customer = $row['fld_mobile'];
								}
								if($row['fld_mobile']!="" && $row['fld_name']!="")
								{
									$customer = $row['fld_name']." <br> ".$row['fld_mobile'];
								}
								$billdate = "";
								if($row['fld_billdate'])
								{
									$billdate = date("d-M-Y", strtotime($row['fld_billdate']));
								}
								$postedon = "";
								if($row['fld_datetime'])
								{
									$postedon = date("d-M-Y", strtotime($row['fld_datetime']));
								}
								$status  = arrOrderStatus()[$row['fld_status']];
								$invoiceinfo = "";
								
								if($row['fld_billno'])
								{
									$invoiceinfo = $row['fld_billno']."<br> " .$billdate;
								}
								if($status)
								{
									$invoiceinfo .= " <br>". $status;
								}
								$rowvalue = array(
									"index" => $rowindex,
									"outlet" => $row['outletname'],
									"invoice" => $invoiceinfo,
									"customer" => $customer,									
									"discount" => $dicountinfo,
									"amount" => $row['fld_amount'],
								);
					
$htmlcode .='<tr>
<td width="10%">'.$rowindex.'</td>
<td width="20%">'.$row['outletname'].'</td>
<td width="20%" align="center">'.$invoiceinfo.'</td>
<td width="20%" align="center">'.$customer.'</td> 
<td width="16%" align="center">'.$dicountinfo.'</td>
<td width="14%" align="center">'.$row['fld_netamount'].'</td>

</tr>';

                	
                   
						}
					}
					
                    
                    $htmlcode .='</tbody>
                </table>

</body>
</html>';

				$this->load->library('Pdf');
				$pdf = new Pdf('P', 'mm', 'A4', true, 'UTF-8', false);
				$pdf->SetTitle('');
				$pdf->setPrintHeader(false);
				$pdf->setPrintFooter(false);
				$pdf->SetHeaderMargin(30);
				$pdf->SetTopMargin(20);
				$pdf->setFooterMargin(20);
				$pdf->SetAutoPageBreak(true,15);
				$pdf->SetAuthor('Author');
				$pdf->SetDisplayMode('real', 'default');
				$pdf->AddPage();
				//$pdf->Write(5, 'CodeIgniter TCPDF Integration');
				$pdf->writeHTML($htmlcode, true, false, false, false, '');
				//$pdf->Output('stockstatus_'.date('Ymdhsi').'.pdf', 'I');
				$pdf->Output($filename.'.pdf', 'D');
					}
				
		}
		else
		{
			

			$totalRec = $this->common_model->listbillingdata("count", '');

			//pagination configuration
			$config['base_url']    = base_url().'warehouse/'.$mgmtname;
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $this->perPage;
			$config['target']    = $this->perPage;

			//get the list data
			$pageid = $this->uri->segment(3);
			if($pageid=="" || $pageid==0)
			{
				$pageid = 1;
			}
			$start = ($pageid - 1) * $this->perPage;

			$config['uri_segment'] =  $start;
			$config['num_links'] = 5;
			$this->ajax_pagination->initialize($config);

			$data['results'] = $this->common_model->listbillingdata("list",array('limit'=>$this->perPage,'start'=>$start));

			$data["outletlist"] = $this->common_model->getlist('outlets', 'fld_id,fld_name','fld_name');
			$data['main_content'] = $mgmtname;
			$data['mgmtname'] = $mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
		
	}
	function report()
	{	
		$fromdt = trim($this->input->get("fromdate"));
		$todt = trim($this->input->get("todate"));
		if($fromdt!="")
		{
			 $fromdate = convertdateFormat($fromdt,'Y-m-d');
			 $data['fromdate'] = $fromdate;
		}
		if($todt!="")
		{				
			$todate = convertdateFormat($todt,'Y-m-d');
			$data['todate'] = $todate;
		}

		if($fromdt=="" && $todt=="")
		{
			$data['fromdate'] = date('Y-m-d');
			$data['todate'] = date('Y-m-d');
		}		
		$tasktype = $this->uri->segment("3");
		if($tasktype=="sale" || $tasktype=="billersale")
		{
			if($tasktype=="billersale")
			{
				//$outletid = decodeValue($this->uri->segment("4"));
				$outletid = decodeValue($this->input->get("outletid"));
				$data['outletid'] =$outletid; 
				if($data["info4edit"] = $this->common_model->getinfo($outletid, 'outlets'))
				{
					$pgheading = $data["info4edit"]->fld_name." > View Billers Sell Report";
					$fldname ='fld_customerid';
				}
				else
				{
					redirect('warehouse/report/sale/');
					exit;
				}
			}
			else
			{
				$pgheading = ucfirst($tasktype). ' Report';
			}
			$totalRec = $this->common_model->listordertotalreport("count", '');

			//pagination configuration			
			if($tasktype=="sale")
			{
				$config['base_url']    = base_url().'warehouse/report/'.$tasktype;
			}
			elseif($tasktype=="billersale")
			{
				$config['base_url']    = base_url().'warehouse/report/'.$tasktype."/".encodeValue($outletid);
			}
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $this->perPage;
			$config['target']    = $this->perPage;
			
			//get the list data
			
			if($tasktype=="sale")
			{
				$pageid = $this->uri->segment(3);
			}
			elseif($tasktype=="billersale")
			{
				$pageid = $this->uri->segment(4);
			}
			if($pageid=="" || $pageid==0)
			{
				$pageid = 1;
			}
			$start = ($pageid - 1) * $this->perPage;
			
			$config['uri_segment'] =  $start;
			$config['num_links'] = 5;
			$this->ajax_pagination->initialize($config);
			
			$data['results'] = $this->common_model->listordertotalreport("list", array('limit'=>$this->perPage,'start'=>$start));
			
			if($tasktype=="billersale")
			{
				$data["billerlist"] = $this->common_model->getlistbywhere('users', 'fld_id,fld_name','fld_name asc','fld_outletid="'.$outletid.'" and fld_type="operator"');
				$data["outletlist"] = $this->common_model->getlistbywhere('outlets', 'fld_id,fld_name','fld_name asc','fld_clientid='.$this->clientid.'');
			}
			elseif($tasktype=="sale")
			{
				$data["outletlist"] = $this->common_model->getlistbywhere('outlets', 'fld_id,fld_name','fld_name asc','fld_clientid='.$this->clientid.'');
			}
			$data['tasktype'] = $tasktype;
			$data['main_content'] = $tasktype.'report';
			$data['pgheading'] = $pgheading;
			$this->load->view('warehouse/common/main.inc.php',$data);			
		}
		elseif($tasktype=="invoices")
		{
			//$outletid = decodeValue($this->uri->segment("4"));
			$outletid = decodeValue($this->input->get("outletid"));

			if($outletid>0)
			{
				if($data["info4edit"] = $this->common_model->getinfo($outletid, 'outlets'))
				{
					$data["outletid"] = $outletid;
					$data["pgheading"] = $data["info4edit"]->fld_name." > View Invoices Report";
					$fldname ='fld_outletid';
					$rsrcid = $outletid;
					$config_base_url = base_url().'warehouse/report/'.$tasktype;
					$pageid = $this->uri->segment(5);
					$data["config_base_url"] = $config_base_url;

					//$userid = decodeValue($this->uri->segment("5"));
					$userid = decodeValue($this->input->get("billerid"));
					if($data["info4edit2"] = $this->common_model->getinfo($userid, 'users'))
					{
						$data["userid"] = $userid;
						$data["pgheading"] = $data["info4edit"]->fld_name." > ".$data["info4edit2"]->fld_name." >View Invoices Report";	
						$fldname ='fld_userid';
						$rsrcid = $userid;
						$config_base_url = base_url().'warehouse/report/'.$tasktype;
						$pageid = $this->uri->segment(6);
						$data["config_base_url"] = $config_base_url;
					}
					
					$totalRec = $this->common_model->listordersdata("count", '',$fldname,$rsrcid);

					
					//pagination configuration
					$config['base_url']    = $config_base_url;
					$config['total_rows']  = $totalRec;
					$config['per_page']    = $this->perPage;
					$config['target']    = $this->perPage;
					
					//get the list data
					//$pageid = $this->uri->segment(2);
					if($pageid=="" || $pageid==0)
					{
						$pageid = 1;
					}
					$start = ($pageid - 1) * $this->perPage;
					
					$config['uri_segment'] =  $start;
					$config['num_links'] = 5;
					$this->ajax_pagination->initialize($config);
					
					$data['results'] = $this->common_model->listordersdata("list", array('limit'=>$this->perPage,'start'=>$start),$fldname,$rsrcid);
				
					$data["billerlist"] = $this->common_model->getlistbywhere('users', 'fld_id,fld_name','fld_name asc','fld_outletid="'.$outletid.'" and fld_type="operator"');
					$data["outletlist"] = $this->common_model->getlistbywhere('outlets', 'fld_id,fld_name','fld_name asc','fld_clientid='.$this->clientid.'');
					$tasktypeexport = $this->uri->segment("4");
					if($tasktypeexport=="exportcsv" || $tasktypeexport=="exportpdf")
					{
						
						//$outletinfo = $this->common_model->getinfo($outletid,'outlets');		
						//predata($outletinfo);
						$filedate = date('d-M-Y', strtotime($data['fromdate']));
						$filename = "invoices_".$filedate;
						if($data["info4edit"]->fld_name!="")
						{

							$filename = $data["info4edit"]->fld_name."_invoices_".$filedate;
						}		
						if($data["info4edit2"]->fld_name!="")
						{

							$filename = $data["info4edit"]->fld_name."_".$data["info4edit2"]->fld_name."_invoices_".$filedate;
						}					
						$results = $data['results'];					

						$previousstockdate = dfltdtFormat(date('d-M-Y', strtotime('-1 day', strtotime($data['fromdate']))));
							if($tasktypeexport=="exportcsv")
							{
								ob_end_clean();	
								$headerrow = 0;
								// Set headers for file name, MIME type, and caching
								header("Content-type: application/csv");
								header("Content-Disposition: attachment; filename=\"".$filename.".csv\"");
								header("Pragma: no-cache");
								header("Expires: 0");
								$fh = fopen('php://output', 'w');
								$rowtitle = array(
									
									"outlet" => 'Outlet',
									"biller" => 'Biller',
									"invoiceno" => 'Invoice',
									"date" => 'Date',
									"amount" => 'Amount',

								);
								$rowindex = 0;
								$totalamount = 0;
								foreach($results as $row)
								{
									$rowindex++;
									$rowvalue = array(
										
										"outlet" => $row->outletname,
										"biller" => $row->billername,
										"invoice" => $row->fld_billno,
										"date" => date("d-M-Y", strtotime($row->fld_billdate)),
										"amount" => $row->fld_netamount,

									);

									if($headerrow<1)
									{							
										$headerrow = 1;
										$mycustomheader = array_values($rowtitle);
										fputcsv($fh, $mycustomheader);
									}
									fputcsv($fh, $rowvalue);
									$totalamount += $row->fld_netamount; 
								}
								if($totalamount>0)
								{
									$rowvalue = array(
										
										"outlet" => "",
										"biller" => "",
										"invoice" => "",
										"date" => "Total",
										"amount" => number_format($totalamount,2),

									);
									fputcsv($fh, $rowvalue);
								}
								fclose($fh);
							}
							elseif($tasktypeexport=="exportpdf")
							{
								
$htmlcode ='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">

<head>
<title>TCPDF Examples</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="description" content="TCPDF is a PHP class for generating PDF documents on the fly" />
<meta name="author" content="Nicola Asuni" />
<meta name="keywords" content="Examples, TCPDF, PDF, PHP class" />
</head>

<body>

<table border="1" cellspecing="5" cellpadding="5">
		 
                <tbody>
                	<tr>
                    	<th width="10%">SRNo.</th>
                    	<th width="20%">Outlet</th>
                    	<th width="20%">Biller</th>                   	
                    	<th width="20%">Invoice</th>
						<th width="14%">Date</th>
						<th width="16%" align="right">Amount</th>
                    </tr>
                    ';
					

					if(is_array($results) && count(array_filter($results))>0)
					{
						$rowindex = 0;
						$totalamount = 0;

						foreach($results as $row)
						{
							$rowindex++;
					
$htmlcode .='<tr>
<td width="10%">'.$rowindex.'</td>
<td width="20%">'.$row->outletname.'</td>
<td width="20%">'.$row->billername.'</td>
<td width="20%">'.$row->fld_billno.'</td> 
<td width="14%" >'.date("d-M-Y", strtotime($row->fld_billdate)).'</td>
<td width="16%" align="right">'.$row->fld_netamount.'</td>

</tr>';

    $totalamount += $row->fld_netamount;            	
                   
						}
$htmlcode .='<tr>

<td width="84%"  align="right">Total</td>
<td width="16%" align="right">'.number_format($totalamount,2).'</td>

</tr>';
					}
					
                    
                    $htmlcode .='</tbody>
                </table>

</body>
</html>';

							$this->load->library('Pdf');
							$pdf = new Pdf('P', 'mm', 'A4', true, 'UTF-8', false);
							$pdf->SetTitle('');
							$pdf->setPrintHeader(false);
							$pdf->setPrintFooter(false);
							$pdf->SetHeaderMargin(30);
							$pdf->SetTopMargin(20);
							$pdf->setFooterMargin(20);
							$pdf->SetAutoPageBreak(true,15);
							$pdf->SetAuthor('Author');
							$pdf->SetDisplayMode('real', 'default');
							$pdf->AddPage();
							//$pdf->Write(5, 'CodeIgniter TCPDF Integration');
							$pdf->writeHTML($htmlcode, true, false, false, false, '');
							//$pdf->Output('stockstatus_'.date('Ymdhsi').'.pdf', 'I');
							$pdf->Output($filename.'.pdf', 'D');
						}
					}
					else
					{
						$data['tasktype'] = $tasktype;
						$data['main_content'] = $tasktype.'report';					
						$this->load->view('warehouse/common/main.inc.php',$data);	
					}
				}
				else
				{
					redirect(base_url().'warehouse/report/sale');
					exit;
				}

			}
			else
			{
				redirect(base_url().'warehouse/report/sale');
				exit;
			}
			
		}
		else
		{	
			redirect('warehouse/dashboard');
			exit;
		}
		
		
	}
	function getoutletbiller()
	{
		$outletid = decodeValue($this->input->post("outletid"));
		$billerid = decodeValue($this->input->post("billerid"));
		$billers ='<option value="">Select Biller</option>';
		if($billerlist = $this->common_model->getlistbywhere("users", "fld_id,fld_name","fld_name asc","fld_type='operator' and fld_outletid='".$outletid."'"))
		{
			
			foreach($billerlist as $billerrows)
			{
				$sel_biller = "";
				if($billerid==$billerrows->fld_id)
				{
					$sel_biller = 'selected="selected"';	
				}
				$billers .='<option value="'.encodeValue($billerrows->fld_id).'" '.$sel_biller.'>'.$billerrows->fld_name.'</option>';
			}
		}
		echo $billers;
	}

	function wdemands()
	{
		$mgmtname = 'wdemands';
		$mgmttbl = 'demand';
		
		$tasktype = $this->uri->segment("3");

		if($tasktype=="manage")
		{
			$rsrcid = $this->uri->segment("4");
			$data["pgheading"] = "Add Demand";
			$data["rsrcid"] = 0;
			if($rsrcid!="")
			{
				$rsrcid = decodeValue($rsrcid);
				if($data["info4edit"] = $this->common_model->getdemandinfo($rsrcid,'w2a'))
				{
					$data["rsrcid"] = $rsrcid;
					$data["pgheading"] = "Modify Demand Info.";
				}
			}
			//$data["productlist"] = $this->common_model->getwarehousestocklist($this->warehouseid);
			$data["productlist"] = $this->common_model->getlist('products', 'fld_id,fld_name','fld_name');
			$data["outletlist"] = $this->common_model->getlist('outlets', 'fld_id,fld_name','fld_name');
			

			$data['mgmtname'] = $mgmtname;
			$data['main_content'] = "manage".$mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
		elseif($tasktype=="managedata")
		{
				$flashdatamsg =  "Please fill mandatory fields!";
				$flashdatacls = 'alert-danger';
				
				$rsrcid = $this->input->post("rsrcid");				
				$outletid = $this->input->post("outletid");
				$warehouseid = $this->warehouseid;
				$refdate = $this->input->post("refdate");				
				$refno = $this->input->post("refno");
				$productid = $this->input->post("productid");
				$qty_case = $this->input->post("qty_case");
				$qty_bottle = $this->input->post("qty_bottle");				
				$producttotalrows = $this->input->post("producttotalrows");
				
				if($warehouseid>0 && $refdate!="" && $producttotalrows>0)
				{
						$resourceid = $this->warehouse_model->managedemind($this->input->post(),'w2a');
						
						if($resourceid>0)
						{
							$flashdatacls =  'alert-success';
							if($rsrcid>0)
							{
								$flashdatamsg =  "Demand info. successfully modified!";
							}
							else
							{
								$flashdatamsg =  "Demand successfully added!";							
							}						 
						}
						elseif($resourceid==0)
						{
							$flashdatamsg =  "Sorry, info. you are looking for not found in our record!";
						}
						elseif($resourceid<0)
						{
							$flashdatamsg =  "Sorry, info. already exists!";
						}					
				}

				$this->session->set_flashdata("message", $flashdatamsg);
				$this->session->set_flashdata('alerttype', $flashdatacls);
				if($_SERVER['QUERY_STRING']!="")
				{
					redirect("warehouse/".$mgmtname."?". $_SERVER['QUERY_STRING']);
				}
				else
				{
					redirect("warehouse/".$mgmtname);
				}
				exit;
		}
		elseif($tasktype=="view" || $tasktype=="print")
		{
				$rsrcid = $this->uri->segment("4");
				
				if($rsrcid!="")
				{
					$rsrcid = decodeValue($rsrcid);
					if($data["info4view"] = $this->common_model->getinfo($rsrcid,$mgmttbl))
					{
						$data["transinfo4view"] = $this->common_model->getdemandinfolist($rsrcid);
						
						$data["rsrcid"] = $rsrcid;
						$data["pgheading"] = "View Stock Transfer";
					}
				}
				$data['tasktype'] = $tasktype;				
				$data['mgmtname'] = $mgmtname;
				$data['main_content'] = "view".$mgmtname;
				$this->load->view('warehouse/common/popup.inc.php',$data);
			
		}		
		else // for listing
		{		
			
			$fromdt = trim($this->input->get("fromdate"));
			$todt = trim($this->input->get("todate"));
			if($fromdt!="")
			{
				 $fromdate = convertdateFormat($fromdt,'Y-m-d');
				 $data['fromdate'] = $fromdate;
			}
			if($todt!="")
			{				
				$todate = convertdateFormat($todt,'Y-m-d');
				$data['todate'] = $todate;
			}

			if($fromdt=="" && $todt=="")
			{
				$data['fromdate'] = date('Y-m-d');
				$data['todate'] = date('Y-m-d');
			}
			 $totalRec = $this->common_model->listdemanddata("count", '','w2a');

			//pagination configuration
			$config['base_url']    = base_url().'warehouse/'.$mgmtname;
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $this->perPage;
			$config['target']    = $this->perPage;
			
			//get the list data
			$pageid = $this->uri->segment(3);
			if($pageid=="" || $pageid==0)
			{
				$pageid = 1;
			}
			$start = ($pageid - 1) * $this->perPage;
			
			$config['uri_segment'] =  $start;
			$config['num_links'] = 5;
			$this->ajax_pagination->initialize($config);
			
			$data['results'] = $this->common_model->listdemanddata("list", array('limit'=>$this->perPage,'start'=>$start),'w2a');
			
			$data["warehouselist"] = $this->common_model->getlist('warehouse', 'fld_id,fld_name','fld_name');
			$data["outletlist"] = $this->common_model->getlist('outlets', 'fld_id,fld_name','fld_name');

			$data['mgmttbl'] = $mgmttbl;
			$data['main_content'] = $mgmtname;
			$data['mgmtname'] = $mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
	}
	function odemands()
	{
		$mgmtname = 'odemands';
		$mgmttbl = 'demand';
			$fromdt = trim($this->input->get("fromdate"));
			$todt = trim($this->input->get("todate"));
			if($fromdt!="")
			{
				 $fromdate = convertdateFormat($fromdt,'Y-m-d');
				 $data['fromdate'] = $fromdate;
			}
			if($todt!="")
			{				
				$todate = convertdateFormat($todt,'Y-m-d');
				$data['todate'] = $todate;
			}

			if($fromdt=="" && $todt=="")
			{
				$data['fromdate'] = date('Y-m-d');
				$data['todate'] = date('Y-m-d');
			}		
		$tasktype = $this->uri->segment("3");

		if($tasktype=="closedemand")
		{
			$rsrcid = $this->uri->segment("4");			
			$data["rsrcid"] = 0;
			if($rsrcid!="")
			{
				 $rsrcid = decodeValue($rsrcid);
				$flashdatacls =  'btn btn-danger';
				$flashdatamsg =  "something went wrong, Please try again!";
				if($data["info4edit"] = $this->common_model->getinfo($rsrcid,$mgmttbl))
				{					
					if($data["info4edit"]->fld_status=='open')
					{						
						$data2 = array(							
							"fld_status" => 'close',
							"fld_closeby" => $this->userid,
							"fld_closedon" => date("Y-m-d H:i:s"),
			"fld_updatedby" => $this->lguserinfo["sessID"],
			"fld_updatedon" => date("Y-m-d H:i:s")	
						);						
						if($this->common_model->updatedata($mgmttbl, $rsrcid, $data2))
						{
							$flashdatacls =  'btn btn-success';
							$flashdatamsg =  "Request submited! successfully";
						}
					
						$this->session->set_flashdata("message2", $flashdatamsg);
						$this->session->set_flashdata('alerttype2', $flashdatacls);

					}

				}
				if($_SERVER['QUERY_STRING']!="")
				{
					redirect("warehouse/".$mgmtname."?". $_SERVER['QUERY_STRING']);
				}
				else
				{
					redirect("warehouse/".$mgmtname);
				}
				exit;
				
			}
			
		}
		elseif($tasktype=="export")
		{
				$rsrcid = $this->uri->segment("4");
				
				if($rsrcid!="")
				{
					$rsrcid = decodeValue($rsrcid);
					if($data["info4view"] = $this->common_model->getinfo($rsrcid,$mgmttbl))
					{
						

						$filename = "outletdemand_". $data["info4view"]->fld_refno;
						
						
						
						$results = $this->common_model->exportdemandinfolist($rsrcid);
						//predata($results); exit;
						

						
						ob_end_clean();	
						$headerrow = 0;
						// Set headers for file name, MIME type, and caching
						header("Content-type: application/csv");
						header("Content-Disposition: attachment; filename=\"".$filename.".csv\"");
						header("Pragma: no-cache");
						header("Expires: 0");
						$fh = fopen('php://output', 'w');
						$rowdata = array(
							"dno" => 'Demand No.: '. $data["info4view"]->fld_refno,
							"date" => 'Date: '. dfltdtFormat($data["info4view"]->fld_refdate),
							"from" => 'Demand From outlet: '. $data["info4view"]->outletname,
							"to" => 'Demand to warehouse: '. $data["info4view"]->warehousename,
						);
						$rowtitle = array(
							
							"name" => 'Item',
							"qty" => 'Qty',
						);
						foreach($results as $row)
						{							
							if($headerrow<1)
							{							
								
								$headerrow = 1;
								$mycustomheader1 = array_values($rowdata);
								fputcsv($fh, $mycustomheader1);

								$mycustomheader = array_values($rowtitle);
								fputcsv($fh, $mycustomheader);
							}

							fputcsv($fh, $row);
						}
						fclose($fh);

						}
				}
			
		}
		elseif($tasktype=="view" || $tasktype=="print")
		{
				$rsrcid = $this->uri->segment("4");
				
				if($rsrcid!="")
				{
					$rsrcid = decodeValue($rsrcid);
					if($data["info4view"] = $this->common_model->getinfo($rsrcid,$mgmttbl))
					{
						$data["transinfo4view"] = $this->common_model->getdemandinfolist($rsrcid);
						
						$data["rsrcid"] = $rsrcid;
						$data["pgheading"] = "View Demand Info.";
					}
				}
				$data['tasktype'] = $tasktype;				
				$data['mgmtname'] = $mgmtname;
				$data['main_content'] = "view".$mgmtname;
				$this->load->view('warehouse/common/popup.inc.php',$data);
			
		}	
		else // for listing
		{		
			
			$fromdt = trim($this->input->get("fromdate"));
			$todt = trim($this->input->get("todate"));
			if($fromdt!="")
			{
				 $fromdate = convertdateFormat($fromdt,'Y-m-d');
				 $data['fromdate'] = $fromdate;
			}
			if($todt!="")
			{				
				$todate = convertdateFormat($todt,'Y-m-d');
				$data['todate'] = $todate;
			}

			if($fromdt=="" && $todt=="")
			{
				$data['fromdate'] = date('Y-m-d');
				$data['todate'] = date('Y-m-d');
			}
			 $totalRec = $this->common_model->listdemanddata("count", '','o2w');

			//pagination configuration
			$config['base_url']    = base_url().'outlet/'.$mgmtname;
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $this->perPage;
			$config['target']    = $this->perPage;
			
			//get the list data
			$pageid = $this->uri->segment(3);
			if($pageid=="" || $pageid==0)
			{
				$pageid = 1;
			}
			$start = ($pageid - 1) * $this->perPage;
			
			$config['uri_segment'] =  $start;
			$config['num_links'] = 5;
			$this->ajax_pagination->initialize($config);
			
			$data['results'] = $this->common_model->listdemanddata("list", array('limit'=>$this->perPage,'start'=>$start),'o2w');
			
			$data["warehouselist"] = $this->common_model->getlist('warehouse', 'fld_id,fld_name','fld_name');
			$data["outletlist"] = $this->common_model->getlist('outlets', 'fld_id,fld_name','fld_name');

			$data['mgmttbl'] = $mgmttbl;
			$data['main_content'] = $mgmtname;
			$data['mgmtname'] = $mgmtname;
			$this->load->view('warehouse/common/main.inc.php',$data);
		}
	}

	function itemledger()
	{
		$resourcetype = "warehouse";
		if($this->input->get("outletid")!="") $resourcetype = "outlet";
		$data["rsproducts"] = $this->common_model->getlist('products', 'fld_id,fld_name','fld_name');
		$data["warehouselist"] = $this->common_model->getlist('warehouse', 'fld_id,fld_name','fld_name');
		$data["outletlist"] = $this->common_model->getlist('outlets', 'fld_id,fld_name','fld_name');
		$productid = $this->input->get("productid");
		if($productid!="") $data["productinfo"] = $this->common_model->getinfo(decodeValue($productid), "products");
		
		$data["results"] = $this->common_model->getitemledger($resourcetype, $this->input->get());
		
		$data['main_content'] = "itemledger";
		$data['mgmtname'] = "itemledger";
		$this->load->view('warehouse/common/main.inc.php',$data);
	}	
}
