<?php
if(!isset($_SESSION)) 
{ 
session_start(); 
} 
if(!isset($_SESSION['is_login']) ){
header("location: login.php");
?>
<script>
window.location="login.php";
</script>
<?php
}

// $permission  = explode(',',$_SESSION['permission_id']);
// if(!in_array(19,$permission) && !in_array(1,$permission)  ){

// $op['is_success'] = false;
// $op['data']    = "عذرا انت غير مخول للوصل الى هذه الصفحة";
// echo json_encode($op);
// exit;

// }

date_default_timezone_set($_SESSION['SYSTimezone']);

require_once 'vendor/autoload.php';
require("db_connect.php");
// for barcode


  
/* Set your desired delimiter. You can make this a true
 * .csv and set $delimiter = ","; but I find that tabs
 * work better as commas can also be present in your data.
 * Note that you must use the .tsv or .xls file extension for Excel
 * to correctly interpret tabs. Otherwise if you are using commas
 * for your delimiter, use .csv for your file extension. */
$delimiter = "\t";
  

        // if ($_POST['type']=="export_alpha") {

            $id = $_GET['id'];


            $res = $db->query("SELECT 
                                        invoice_id, 
                                        invoice_number, 
                                        invoice_date, 
                                        invoice_status, 
                                        invoice_usd_rate, 
                                        invoice_currency,
                                        invoice_total_price_actual, 
                                        invoice_total_net_balance, 
                                        discount,
                                        discount_type, 
                                        I.actual_discount, 
                                        invoice_note,
                                        author,
                                        invoice_sequence_no,
                                        delivery_status, 
                                        delivery_time,
                                        destination, 
                                        offer_number, 
                                        date_of_offer, 
                                        term_and_conditions, 
                                        invoice_barcode,
                                        customer_id_fk, 
                                        pay_terms,
                                        request_for_quta_number,
                                        sell_item_id,
                                        sell_item_quantity,
                                        sell_item_height,
                                        sell_item_width, 
                                        sell_item_height_production, 
                                        sell_item_width_production, 
                                        item_buy_price_per_unit, 
                                        item_sell_price_per_unit, 
                                        tax_per_unit, 
                                        sell_item_barcode,
                                        sell_item_notes,
                                        sell_item_comment,
                                        cell_color,
                                        item_production_stage,
                                        sell_item_delivery_name, 
                                        SI.model_id_fk,
                                        color_product_id_fk, 
                                        customer_full_name,
                                        customer_email,
                                        customer_phone,
                                        customer_address,                                     
                                        customer_company,

                                        M.model_name,
                                        M.model_code,
                                        M.model_type,
                                        M.model_color,

                                        Pr1.product_name AS product_name_color,
                                        Pr1.product_code AS product_code_color,
                                        Pr1.product_type AS product_type_color
                                            
                                        
                                  

                                        FROM invoice I
                                        LEFT JOIN sell_item SI ON (SI.invoice_id_fk = I.invoice_id AND SI.is_deleted=0)
                                        LEFT JOIN model M ON (SI.model_id_fk = M.model_id )
                                        LEFT JOIN product Pr1 ON (SI.color_product_id_fk = Pr1.product_id )
                                        LEFT JOIN customer C ON (C.customer_id = I.customer_id_fk)

                                        WHERE I.invoice_id='$id'  AND I.is_deleted = 0");
            if ($res) {
                
                $res2 = $db->query("SELECT 
                                            invoice_id, 
                                            invoice_number
                                       
                                            FROM invoice I
                                        
                                            WHERE I.invoice_id='$id'  AND I.is_deleted = 0");
            
                $row2 = $res2->fetch_assoc();

                $filename=$row2['invoice_number'].".csv";
  
                //Send headers

                //Send headers
                header("Content-type: application/octet-stream");
                header("Content-Disposition: attachment; filename=$filename");
                header("Pragma: no-cache");
                header("Expires: 0");
              
                $dataRowString = "";

                $op  = array('invoice'=>array(),'details'=>array());
                $x = 1;
                while ($row = $res->fetch_assoc()) {

                    

                    if($row['item_production_stage'] < 3){


                        // add CEK DAR K CAM CAM KAP 

                        $typo = '';

                        if(round($row['sell_item_height_production'],0) <= 185){
                            $typo = 'CEK';                            
                        }elseif(round($row['sell_item_width_production'],0) <= 150){
                            $typo = 'DAR';
                        }elseif(trim($row['sell_item_comment']) != '' && trim($row['sell_item_comment']) != NULL){
                            $typo = trim($row['sell_item_comment']);
                        }else{
                            $typo = 'KAP';
                        }


                        // add space after OS im model


                        $new_model = substr_replace($row['model_code'], " ", 2, 0); 



                        $dataRowString =   $new_model . $typo . $delimiter .round( $row['sell_item_width_production'],0) . $delimiter . round($row['sell_item_height_production'],0) . $delimiter . round($row['sell_item_quantity'],0) . $delimiter . $row['author'] . $delimiter . $row['invoice_number'] . $delimiter . $x . $delimiter . $row['product_code_color'] ;


                        $x = $x + 1;


                        print $dataRowString . "\r\n";
                    }
                    
                   
            
                }       
                
                exit;
            }else{
                echo 0;
                exit;
            }
        // }



?>