<?php 
    include("../config/defines.php");
    include($AaliLINK_IN."/FPDF/fpdf.php");
    
    if(isset($_GET["ip"])){
        $inv = $_GET["ip"];

        $query = mysqli_query($conn, "SELECT * FROM aalierp_cart WHERE ip_add='".$inv."'");
        if(mysqli_num_rows($query) > 0){
            $row = mysqli_fetch_assoc($query);
            if($row["user_id"] == -1){$user_name = "You are not logged in!";}else{
                $users = mysqli_query($conn, "SELECT * FROM aalierp_user WHERE user_id='".$row["user_id"]."'");
                $usr = mysqli_fetch_assoc($users);
                $user_name = $usr["user_name"];
            }
    
            $pdf = new FPDF();
            $pdf->addPage();
            $pdf->SetFont("Arial","B",16);
            $pdf->Image('https://d101sale.com/cms/uploads/logos/logo.png',10,10,-600);
            $pdf->Cell(70,16,$company_name,0,1,"C");
            $pdf->SetFont("Arial",null,11);
            $pdf->Cell(50,6,"Phone: ".$company_phone.", Mobile: ".$company_mobile,0,1);
            $pdf->Cell(50,6,"Address : ".$company_address." , ".$company_city,0,1);
            $pdf->Cell(50,8," ",0,1);
    
            $pdf->Cell(50,6,"Date",0,0);
            $pdf->Cell(5,6,": ".$row["date"],0,1);
            $pdf->Cell(50,6,"Invoice#",0,0);
            $pdf->Cell(5,6,": 000".$row["id"],0,1);
            $pdf->Cell(50,6,"Customer",0,0);
            $pdf->Cell(5,6,": ".$user_name,0,1);
            $pdf->Cell(50,6,"Payment",0,0);
            $pdf->Cell(5,6,": ",0,1);
    
            $pdf->SetFont("Arial","B",14);
            $pdf->Cell(190,10,"PRODUCTS DETAIL",0,1,"C");
    
            $pdf->SetFont("Arial","B",11);
            $pdf->Cell(45,9,"Product Name",1,0,"C");
            $pdf->Cell(20,9,"Quantity",1,0,"C");
            $pdf->Cell(30,9,"Unit Price",1,0,"C");
            $pdf->Cell(30,9,"Shipment (+)",1,0,"C");
            $pdf->Cell(30,9,"Discount (-)",1,0,"C");
            $pdf->Cell(35,9,"Total Price",1,1,"C");
    
            $pdf->SetFont("Arial",NULL,11);
            
            $invquery = mysqli_query($conn,"SELECT c.id,c.p_id,c.ip_add,c.user_id,c.qty,c.mail,c.ship,c.coupon,c.cdiscount,p.product_id,p.product_name,p.product_price FROM aalierp_cart c, aalierp_product p WHERE c.p_id=p.product_id AND c.ip_add='".$inv."'");
            $i = 1; $sub=0; $net=0; $dis=0; $pay=0; $paid=0; $bal=0;
            while($rec = mysqli_fetch_array($invquery)){$tot=0;
                $sub = $sub + round($rec["qty"]*$rec["product_price"]+$rec["ship"]-$rec["cdiscount"],2);
                $tot = $tot + round($rec["qty"]*$rec["product_price"]+$rec["ship"]-$rec["cdiscount"],2);
                $net = $sub - $dis;
                $bal = $net - $paid;
    
                $pdf->Cell(45,9,($rec["product_name"]),1,0,"C");
                $pdf->Cell(20,9,$rec["qty"],1,0,"C");
                $pdf->Cell(30,9,$cur."".$rec["product_price"],1,0,"C");
                $pdf->Cell(30,9,$cur."".$rec["ship"],1,0,"C");
                $pdf->Cell(30,9,$cur."".$rec["cdiscount"],1,0,"C");
                $pdf->Cell(35,9,$cur."".$tot,1,1,"C");
                $i++;
            }
    
            $pdf->SetFont("Arial",NULL,12);
            $pdf->Cell(120,9,"",0,0,"C");
            $pdf->Cell(35,9,"Sub Total :",0,0,"R");
            $pdf->Cell(35,9,"$".$sub,1,1,"R");
    
            $pdf->Cell(120,9,"",0,0,"C");
            $pdf->Cell(35,9,"Net Total :",0,0,"R");
            $pdf->Cell(35,9,$cur."".$net,1,1,"R");
    
            $pdf->Cell(120,9,"",0,0,"C");
            $pdf->Cell(35,9,"Paid :",0,0,"R");
            $pdf->Cell(35,9,$cur.""."0.00",1,1,"R");
    
            $pdf->Cell(120,9,"We thank you for shopping with ".$company_name,0,0,"L");
            $pdf->Cell(35,9,"Balance :",0,0,"R");
            $pdf->Cell(35,9,$cur."".$bal,1,1,"R");
    
            $pdf->Cell(190,20,"",0,1);
            $pdf->Cell(95,20,"Accountant's Signature",0,0,"L");
            $pdf->Cell(95,20,"Customer's Signature",0,0,"R");
    
            $pdf->Output();
        }else{
            
            $pdf = new FPDF();
            $pdf->addPage();
            $pdf->SetFont("Arial","B",16);
            $pdf->Image('https://d101sale.com/cms/uploads/logos/logo.png',10,10,-600);
            $pdf->Cell(70,16,$company_name,0,1,"C");
            $pdf->SetFont("Arial",null,11);
            $pdf->Cell(50,6,"Phone: ".$company_phone.", Mobile: ".$company_mobile,0,1);
            $pdf->Cell(50,6,"Address : ".$company_address." , ".$company_city,0,1);
            $pdf->Cell(50,8," ",0,1);
    
            $pdf->Cell(50,6,"Date",0,0);
            $pdf->Cell(5,6,": ",0,1);
            $pdf->Cell(50,6,"Invoice#",0,0);
            $pdf->Cell(5,6,": 000",0,1);
            $pdf->Cell(50,6,"Customer",0,0);
            $pdf->Cell(5,6,": ".$user_name,0,1);
            $pdf->Cell(50,6,"Payment",0,0);
            $pdf->Cell(5,6,": ",0,1);
    
            $pdf->SetFont("Arial","B",14);
            $pdf->Cell(190,10,"NO PRODUCTS IN CART",0,1,"C");
            
            $pdf->Output();
        }
    }
    
    if(isset($_GET["usr"])){
        $user = $_GET["usr"];

        $query = mysqli_query($conn, "SELECT * FROM aalierp_cart WHERE user_id='".$user."'");
        if(mysqli_num_rows($query) > 0){
            $row = mysqli_fetch_assoc($query);
            $users = mysqli_query($conn, "SELECT * FROM aalierp_user WHERE user_id='".$row["user_id"]."'");
            $usr = mysqli_fetch_assoc($users);
            $user_name = $usr["user_name"];
    
            $pdf = new FPDF();
            $pdf->addPage();
            $pdf->SetFont("Arial","B",16);
            $pdf->Image('https://d101sale.com/cms/uploads/logos/logo.png',10,10,-600);
            $pdf->Cell(70,16,$company_name,0,1,"C");
            $pdf->SetFont("Arial",null,11);
            $pdf->Cell(50,6,"Phone: ".$company_phone.", Mobile: ".$company_mobile,0,1);
            $pdf->Cell(50,6,"Address : ".$company_address." , ".$company_city,0,1);
            $pdf->Cell(50,8," ",0,1);
    
            $pdf->Cell(50,6,"Date",0,0);
            $pdf->Cell(5,6,": ".$row["date"],0,1);
            $pdf->Cell(50,6,"Invoice#",0,0);
            $pdf->Cell(5,6,": 000".$row["id"],0,1);
            $pdf->Cell(50,6,"Customer",0,0);
            $pdf->Cell(5,6,": ".$user_name,0,1);
            $pdf->Cell(50,6,"Payment",0,0);
            $pdf->Cell(5,6,": ",0,1);
    
            $pdf->SetFont("Arial","B",14);
            $pdf->Cell(190,10,"PRODUCTS DETAIL",0,1,"C");
            $pdf->SetFont("Arial","B",11);
            $pdf->Cell(45,9,"Product Name",1,0,"L");
            $pdf->Cell(20,9,"Quantity",1,0,"C");
            $pdf->Cell(30,9,"Unit Price",1,0,"C");
            $pdf->Cell(30,9,"Shipment (+)",1,0,"C");
            $pdf->Cell(30,9,"Discount (-)",1,0,"C");
            $pdf->Cell(35,9,"Total Price",1,1,"C");
    
            $pdf->SetFont("Arial",NULL,11);
            
            $invquery = mysqli_query($conn,"SELECT c.id,c.p_id,c.ip_add,c.user_id,c.qty,c.mail,c.ship,c.coupon,c.cdiscount,p.product_id,p.product_name,p.product_price FROM aalierp_cart c, aalierp_product p WHERE c.p_id=p.product_id AND c.user_id='".$user."'");
            $i = 1; $sub=0; $net=0; $dis=0; $pay=0; $paid=0; $bal=0;
            while($rec = mysqli_fetch_array($invquery)){$tot=0;
                $sub = $sub + round($rec["qty"]*$rec["product_price"]+$rec["ship"]-$rec["cdiscount"],2);
                $tot = $tot + round($rec["qty"]*$rec["product_price"]+$rec["ship"]-$rec["cdiscount"],2);
                $net = $sub - $dis;
                $bal = $net - $paid;
    
                $pdf->Cell(45,9,($rec["product_name"]),1,0,"C");
                $pdf->Cell(20,9,$rec["qty"],1,0,"C");
                $pdf->Cell(30,9,$cur."".$rec["product_price"],1,0,"C");
                $pdf->Cell(30,9,$cur."".$rec["ship"],1,0,"C");
                $pdf->Cell(30,9,$cur."".$rec["cdiscount"],1,0,"C");
                $pdf->Cell(35,9,$cur."".$tot,1,1,"C");
                $i++;
            }
    
            $pdf->SetFont("Arial",NULL,12);
            $pdf->Cell(120,9,"",0,0,"C");
            $pdf->Cell(35,9,"Sub Total :",0,0,"R");
            $pdf->Cell(35,9,$cur."".$sub,1,1,"R");
    
            $pdf->Cell(120,9,"",0,0,"C");
            $pdf->Cell(35,9,"Net Total :",0,0,"R");
            $pdf->Cell(35,9,$cur."".$net,1,1,"R");
    
            $pdf->Cell(120,9,"",0,0,"C");
            $pdf->Cell(35,9,"Paid :",0,0,"R");
            $pdf->Cell(35,9,$cur.""."0.00",1,1,"R");
    
            $pdf->Cell(120,9,"We thank you for shopping with ".$company_name,0,0,"L");
            $pdf->Cell(35,9,"Balance :",0,0,"R");
            $pdf->Cell(35,9,$cur."".$bal,1,1,"R");
    
            $pdf->Cell(190,20,"",0,1);
            $pdf->Cell(95,20,"Accountant's Signature",0,0,"L");
            $pdf->Cell(95,20,"Customer's Signature",0,0,"R");
    
            $pdf->Output();
        
        }else{
            
            $pdf = new FPDF();
            $pdf->addPage();
            $pdf->SetFont("Arial","B",16);
            $pdf->Image('https://d101sale.com/cms/uploads/logos/logo.png',10,10,-600);
            $pdf->Cell(70,16,$company_name,0,1,"C");
            $pdf->SetFont("Arial",null,11);
            $pdf->Cell(50,6,"Phone: ".$company_phone.", Mobile: ".$company_mobile,0,1);
            $pdf->Cell(50,6,"Address : ".$company_address." , ".$company_city,0,1);
            $pdf->Cell(50,8," ",0,1);
    
            $pdf->Cell(50,6,"Date",0,0);
            $pdf->Cell(5,6,": ",0,1);
            $pdf->Cell(50,6,"Invoice#",0,0);
            $pdf->Cell(5,6,": 000",0,1);
            $pdf->Cell(50,6,"Customer",0,0);
            $pdf->Cell(5,6,": ".$user_name,0,1);
            $pdf->Cell(50,6,"Payment",0,0);
            $pdf->Cell(5,6,": ",0,1);
    
            $pdf->SetFont("Arial","B",14);
            $pdf->Cell(190,10,"NO PRODUCTS IN CART",0,1,"C");
            
            $pdf->Output();
        }
    }

?>