<?php	
	require_once("includes/config.php");
	$ac = new DbQuery();
    $ac->checkLogin();
    

	$html = '';
	$total = 0.00;
	
	 /****** POS DB Integration**********/
    define('DB_SERVER','localhost');
    define('DB_USER','pos');
    define('DB_PASS' ,'##1Samir##');
    define('DB_NAME', 'pos');
    $con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);

	// Check connection
	if (mysqli_connect_errno())
	{
		echo "Failed to connect to MySQL: " . mysqli_connect_error();
	}

	       
	       /************* PO Request Rows *************/
	       $params = array(	
				'table' => 'po_req',
				'get'   =>'*',
				'where' => array('status'=> '0'),
			);

			$po_req = $ac->select($params,'get_data');
			
		if($po_req){
		    
		   foreach($po_req as $row){
		       
		        /************* Sell Row *************/
    	       $param = array(	
    				'table' => 'sell',
    				'get'   =>'*',
    				'where' => array('calculated_data'=> $row['ID']),
    			);
    
    			$select = $ac->select($param,'get_row');
		    
    		   /****** Contact Row ********/
    	       $contact_query='SELECT * FROM `contacts` WHERE id="'.$row['customer'].'" ';
    	       $contact = $con->query($contact_query);
    	       $contact_result = mysqli_fetch_assoc( $contact );
    	       
    	        $delivery_date = strtotime(date('d-m-Y', strtotime($row['sale_date']) ) ).' ';
                $current_date = strtotime(date('d-m-Y'));
    	       
	    
    	      $html .=' <tr class="tr-shadow">
        	                <td>
        	                    <label class="au-checkbox">
        	                        <input type="checkbox" value="'.$row['ID'].'" name="calculated_data[]" class="calc_row" data-total="'. $row['amount'].'" '.((isset($select) && $select['calculated_data']==$row['ID'])?'checked':'').'>
        	                        <span class="au-checkmark"></span> 
                                </label>
                            </td>
        	                <td class="desc">'.date('Y', strtotime($row['created_date'])).'/00'.date('d', strtotime($row['created_date'])).'/'.$row['ID'].'</td>
        	                <td>'.( (isset($contact_result) && !empty($contact_result['name']) )?$contact_result['name']:'').'</td>
        	                <td>'. date("d-m-Y", strtotime($row['created_date'])).'</td>
        	                <td>'.( ($delivery_date < $current_date)?'<span class="text-danger">'. date("d-m-Y", strtotime($row['sale_date'])).'</span>':date("d-m-Y", strtotime($row['sale_date']))) .'</td>
        	                <td> ₹ '. number_format($row['amount'], 2).'</td>
        	                <td> 
                                <div class="table-data-feature">
                                    <a href="'.ROOT.'create-po-request.php?id='.$ac->mcEncrypt($row['ID']).'" class="item" data-toggle="tooltip" data-placement="top" title="Edit" target="_blank">
                                        <i class="zmdi zmdi-edit"></i>
                                    </a>
                                </div>
                            </td>
                        </tr>
                        <tr class="spacer"></tr>';
		   }
	    
    	}else{
    	   $html .='<tr class="tr-shadow text-center">
        	            <td colspan="8">No Record Found</td>
        	       </tr>';
    	}
	/**************** END Fetching **************/
	/******** END Integration**************/
   $page_body ='
            <div class="main-content">
                <div class="section__content ">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-md-12">
                                <!-- DATA TABLE -->
                                <form action="'.CALLBACK.'sell-manage.php" method="post">
                                    <input type="hidden" name="action" value="save-sell-record" />
                                    <h3 class="title-5 m-b-35">PO Requests</h3>
                                    <div class="table-data__tool float-right">
                                        <div class="table-data__tool-right">
                                            <a href="'.ROOT.'create-po-request.php" class="au-btn au-btn-icon au-btn--green au-btn--small">
                                                <i class="zmdi zmdi-plus"></i>Create New Request</a>
                                        </div>
                                    </div>
                                    <!--div class="table-data__tool">
                                        <div class="table-data__tool-left">
                                            <div class="rs-select2--light rs-select2--md">
                                                <select class="js-select2 business_name" name="property">
                                                    <option value="0" selected="selected">All Business</option>
                                                   
                                                </select>
                                                <div class="dropDownSelect2"></div>
                                            </div>
                                            <div class="rs-select2--light rs-select2--sm">
                                                <select class="js-select2 custom-range-select" name="time">
                                                    <option value="0" selected="selected">Today</option>
                                                    <option value="3">3 Days</option>
                                                    <option value="7">1 Week</option>
                                                    <option value="custom_range">Custom Range</option>
                                                </select>
                                                <div class="dropDownSelect2"></div>
                                            </div>
                                        <label for="start_date" class="custom-range-wrap" style="display:none;">Start Date</label>
                                            <input type="text" name="start_date" id="start_date" class="au-btn-filter custom-range-wrap" style="text-transform: unset;display:none;">
                                        <label for="end_date" style="display:none;" class="custom-range-wrap">End Date</label>
                                             <input type="text" name="end_date" id="end_date" class="au-btn-filter custom-range-wrap" style="text-transform: unset;display:none;">
                                            <button type="button" class="au-btn-filter filter-by">
                                                <i class="zmdi zmdi-filter-list"></i>filters</button>
                                            
                                        </div>
                                        <div class="table-data__tool-right">
                                            <button type="submit" class="au-btn au-btn-icon au-btn--green au-btn--small px-4 "><i class="zmdi zmdi-plus"></i> Save</button>
                                        </div>
                                    </div-->
                                    
                                            <div class="table-responsive table-responsive-data2">
                                            
                                                <table class="table table-data2">
                                                    <thead>
                                                        <tr>
                                                            <th>
                                                                <label class="au-checkbox">
                                                                    <input type="checkbox" class="check_all">
                                                                    <span class="au-checkmark"></span>
                                                                </label>
                                                            </th>
                                                            <th>Invoice No.</th>
                                                            <th>Customer Name</th>
                                                            <th>Created Date</th>
                                                            <th>Delivery Date</th>
                                                            <th>Total Amount</th>
                                                            <th>Action</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        '. $html .' 
                                                    </tbody>
                                                    <tfoot>
                                                        <tr>
                                                          <th class="text-right pr-4" colspan="6">Total :</th>
                                                          <td class="text-right pr-4"> ₹  '.number_format('0', 2).'</td>
                                                        </tr>
                                                   </tfoot>
                                                </table>
                                                <button type="submit" class="au-btn au-btn-icon au-btn--green au-btn--small mt-3 px-5 float-right save-only">Generate PO</button>
                                                <button type="button" class="au-btn au-btn-icon au-btn--green au-btn--small mt-3 px-5 float-right mr-3 execute-po">Execute PO</button> 
                                           
                                            </div>
                                 </form>
                                <!-- END DATA TABLE -->
                            </div>
                        </div>
                    </div>
                </div>
            </div>';
        
    $title = 'PO Requests';
		$body = array(
		"pageTitle"=>$title,
		'content'=>$page_body,
		);
		echo $ac->buildFullPage($body);

?>

   