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

class Customapi {
	public function listOrders($data = array()){
		$this->cId = $data['cId']; 
        $this->CI =& get_instance();
		//$templates = $this->get_template($pattern);
		$where = array();
		if(!empty($data['fromDate'])){
			$where['t.tDate >='] = $data['fromDate'];
		} 
		if(!empty($data['toDate'])){
			$where['t.tDate <='] = $data['toDate'];
		}
		if($data['bId'] ==-1){
			$where['t.source'] = 'E';
		}else if($data['bId'] >= 0 ){
			$where['t.bId'] = $data['bId'];
		}
		//Trans detail
		$this->CI->db->select("t.id,t.tNo  ,t.taxableAmount,t.totTax as TaxAmount,t.totAmount as TotalAmount,t.bId,t.updatedOn,ifnull(tr.transId,0) as referedId,'Sales' as transStatus,0 as returnAmount,if(t.includeTax=1,'I','E') as taxType, , IFNULL(te.expCurrencyCode,'') as exportCurrencyCode, IFNULL(te.excRate,'') as exchangeRate, IFNULL(te.totForexAmt,'') as totForexAmount");
		$this->CI->db->from('trans AS t');
		$this->CI->db->join("trans_ref AS tr", "tr.refTransId=t.id", "left");
		$this->CI->db->join('trans_export AS te', 'te.transId=t.id and te.cId= t.cId', 'left');
		$this->CI->db->where(array(
			"t.cId" => $this->cId
		));
		$this->CI->db->where($where);
		$this->CI->db->where_in("t.tType", array('SI','SR'));
		//echo $this->CI->db->get_compiled_select();
		$this->CI->db->order_by("t.id","desc");
		$resTransData = $this->CI->db->get()->result_array();
		$pattern = array();
		if(empty($resTransData)){
            echo 'No Records Found';
            exit;
        }
        foreach($resTransData as $key=>$val){
            $ardate = explode(" ",$val['updatedOn']);
            $resTransData[$key]['createdDate'] = $ardate[0];
            $resTransData[$key]['createdTime'] = $ardate[1];
            $resTransData[$key]['currency']  = get_company("currency");
            $resTransData[$key]['isExchange']  = !empty($val['exchangeRate'])?1:0;
            if(!empty($val['referedId'])){
                $this->CI->db->select("t.tType,t.totAmount as returnAmount");
                $this->CI->db->from('trans AS t');
                $this->CI->db->where(array(
                    "t.cId" => $this->cId,
                    "t.id" => $val['referedId']
                ));
                $this->CI->db->where($where);
                //echo $this->CI->db->get_compiled_select();
                $res = $this->CI->db->get()->row_array();
                if(!empty($res)){
                    if($res['tType']=='SC'){
                        $resTransData[$key]['transStatus'] = 'Return';
                        $resTransData[$key]['returnAmount'] = $res['returnAmount'];
                    }
                }
            }
            //Trans Item
            $this->CI->db->select("ti.*,(ti.itemTotal-ti.taxableAmount) as taxAmount ,c.name as catName");
            $this->CI->db->from('trans_items AS ti');
            $this->CI->db->join('items_category as ic', 'ic.itemId =ti.itemId AND ic.cId=ti.cId');
            $this->CI->db->join('category AS c','c.id=ic.catId and c.type = "C"','left');
            $this->CI->db->where(array(
                "ti.cId" => $this->cId,
                "ti.transId" => $val['id']
            ));
           // echo $this->CI->db->get_compiled_select();
    
            //Trans Payment mode
            $resTransData[$key]['details'] = $this->CI->db->get()->result_array();

            foreach($resTransData[$key]['details'] as $itkey=>$itval){
                $resTransData[$key]['totalDiscount'] +=  $itval['discAmount'];
            }
            
            $this->CI->db->select("tp.*, pm.name AS pModeName,a.accCode,a.groupCode, a.name AS pAccName,c.name as pConName");
            $this->CI->db->from("trans_pay AS tp");
            $this->CI->db->join("paymentmode AS pm", "tp.pMode != 0 AND pm.id=tp.pMode", "left");
            $this->CI->db->join("contacts AS c", "tp.pMode = 0 AND tp.pConId != 0 AND c.id=tp.pConId", "left");
            $this->CI->db->join("account AS a", "a.id=tp.pAccId", "left");
            $this->CI->db->where(array(
                "tp.cId" => $this->cId,
                "tp.transId" => $val['id']
            ));
           // echo $this->CI->db->get_compiled_select();
            $resTransData[$key]['payModes'] = $this->CI->db->get()->result_array();
            
        }
        $result = '<?xml version="1.0" encoding=”UTF-8”?>'; $result .= "\n";
        if(file_exists(FCPATH . 'custom/'.$this->cId.'/views/listOrders.php')){
            $result .= $this->CI->load->view('listOrders',array('data'=>$resTransData),true);
        }

        header('headers="Accept=text/xml;charset=UTF-8"');
        header('Content-type:text/plain');
        echo $result;
		
	}
	
}
