<?php	
	require_once("includes/config.php");
	$ac = new DbQuery();
    $ac->checkLogin();
    
    /****** 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();
	}
	
	/********** Fetch Contacts Rows ***************/
	$contacts_query='SELECT * FROM `contacts` where business_id=1 ';
	$contacts_result = $con->query($contacts_query);
	if(!$contacts_result) throw new exception(sprintf("Invalid query : %s", $contacts_query));
    
    $contacts_rows = array();
    if($contacts_result->num_rows <= 0){
        echo "no users found !";
    }
    else{

        $contacts_rows = $contacts_result->fetch_all(MYSQLI_ASSOC); //faster 
    }
    
    $select = array();
    $id = 0;
    $supplier = 'View';
    if(isset($_GET['supplier']) && !empty($_GET['supplier'])){
        $supplier = $ac->mcDecrypt($_GET['supplier']);
    }
    if(isset($_GET['id']) && !empty($_GET['id'])){
        
       $id = $ac->mcDecrypt($_GET['id']);
        /******** Fetch PO Row ********/
        $param = array(	
    					'table' => 'product',
    					'get'   =>'*',
    					'where' => array('vendor'=> $id),
    				);
    
    	$select = $ac->select($param,'get_data'); 
    }
    
    $total = 0.00;
    	/********** Fetch Products Rows ***************/
        	$products_html = $products_rows = '';
            if($select){
                $params = array();
                foreach($select as $selects){
                      $params = array(	
        					'table' => 'po_manage_req',
        					'get'   =>' SUM(PO_MANAGE_REQ.quantity) as quantity, product_id, name, SUM(price) as price, po_req_id',
        					'joinTable' => array(
    										array('table'=>'po_req',
    												'join'=>'INNER',
    												'firstTable'=>'PO_MANAGE_REQ.po_req_id',
    												'secondTable'=>'PO_REQ.ID'
											)
										),
        					'where' => array('product_id'=> $selects['product'], 'status'=> '1', 'vendor_executed'=> '0'),
        					'whereOperator' => array('0'=>'and', '1'=>'and'),
        					'other' =>'GROUP BY product_id',
        				);
        
        			$products_rows = $ac->select($params,'get_data');
        			
        			/********** Fetch Products Rows ***************/
                	$variations_query='SELECT sell_price_inc_tax as price FROM variations where product_id='.$selects['product'];
                	$variations_result = $con->query($variations_query);
                	if(!$variations_result) throw new exception(sprintf("Invalid query : %s", $variations_query));
                    
                    $variations_rows = array();
                    if($variations_result->num_rows > 0){
                       $variations_rows = $variations_result->fetch_all(MYSQLI_ASSOC); //faster
                    }
                    $rate = 0;
        			if($variations_rows){
        			    foreach($variations_rows as $variations_row){
        			        $rate = number_format($variations_row['price'], 2);
        			    }
        			}
        			
        			/********** Fetch Product's Available Quantity Row ***************/
                	$variation_location_details_query='SELECT * FROM `variation_location_details` join products on products.id=variation_location_details.product_id WHERE variation_location_details.product_id='.$selects['product'];
                	$variation_location_details_result = $con->query($variation_location_details_query);
                	if(!$variation_location_details_result) throw new exception(sprintf("Invalid query : %s", $variation_location_details_query));
                    
                    $variation_location_details_rows = array();
                    if($variation_location_details_result->num_rows > 0){
                       $variation_location_details_rows = $variation_location_details_result->fetch_all(MYSQLI_ASSOC); //faster
                    }
                   
                    if($products_rows){
                        foreach($products_rows as $products_row){
                             $qty_available = 0;
                			if($variation_location_details_rows){
                			    foreach($variation_location_details_rows as $variation_location_details_row){
                			        if($products_row['quantity']>number_format($variation_location_details_row['qty_available'], 2)){
                			            $qty_available = $products_row['quantity'] - $variation_location_details_row['qty_available'] + $variation_location_details_row['alert_quantity'];
                			        }
                			    }
                			}
                			if(intval($qty_available) !=0){
                			    $products_html .= '
                                <tr class="product" data-id="'.$products_row['product_id'].'">
                                    <td>'.$products_row['name'].'</td>
                                    <td>'.intval($qty_available).' </td> 
                                    <td>'.$rate.'</td>
                                    <td>'.number_format($qty_available*$rate, 2).'</td>
                                </tr>';
                                $total = intval($total)+intval($qty_available*$rate); 
                			}
                            
                        }
                    }
                }
            }
          
            
    
    $page_body .='
        <!-- MAIN CONTENT-->
            <div class="main-content">
                <div class="section__content section__content--p30">
                    <div class="container-fluid">
                                
                                <h3 class="title-5 ">'.$supplier.'</h3>
                                <button class="au-btn au-btn-icon au-btn--green au-btn--small mt-0 px-5 float-right vendor_executed" data-id="'.$id.'">Execute</button><br><br>
                                    <div class="card">
                                        <div class="card-body">
                                                <div class="row">
                                                    <div class="col-sm-12">
                                                        <div class="table-responsive">
                                                            <table class="table table-condensed table-bordered table-striped table-responsive" id="app_products">
                                                                <thead>
                                        							<tr>
                                        								<th class="text-center">Product</th>
                                        								<th class="text-center">Quantity</th>
                                        								<th class="text-center">Rate</th>
                                        								<th class="text-center">Subtotal</th>
                                        							</tr>
                                        						</thead>
                                        						<tbody>
                                        						'.$products_html.'
                                        						</tbody>
                                                            </table>
        	                                            </div>
        	                                            <div class="table-responsive">
                                                            <table class="table table-condensed table-bordered table-striped table-responsive">
                                                                <tbody>
                                                                    <tr>
                                            							<td>
                                            								<div class="pull-right mr-4 pr-4">
                                            								    <b>Total: </b>
                                            									<span class="price_total">'.number_format($total, 2).'</span> 
                                            								</div>
                                            							</td>
                                            						</tr>
                                            					</tbody>
                                                            </table>
                                                        </div>
                                                    </div>
                                                </div>
                                        </div>
                                    </div>
                    </div>
                </div>
            </div>
        
        
    ';
    
    $title = 'View';
	$body = array(
		"pageTitle"=>$title,
		'content'=>$page_body,
	);
	echo $ac->buildFullPage($body);

?>