<?php
define('DOCUMENT_ROOT_CLASS','../');
define('DOCUMENT_ROOT_INC_FOLDER','');
$filepath = realpath(dirname(__FILE__));
if(!empty($_POST["action"]) and $_POST["action"] == "ajax_call"){
    require "../inc/ajax_header.php";
    $added_by = $_SESSION['id'];
    $last_id = 0;
    
    $html = "
    <!DOCTYPE html>
    <html>
    <head>
        <title>".TITLE."</title>
    </head>
    <body>
    <style>
        .DivIdToPrint{ page-break-before: always; }
    </style>
    <style type='text/css' media='print'>
        @media print { @page { margin-bottom: 0; } }
    </style>
    <div id='DivIdToPrint'>";
    for ($ij=0; $ij < $_POST["number_of_camels"]; $ij++) { 
        $url_bar = '';
        $seriNo = $commonFunctions->getRowDataCount('sys_receipts','');
        $seriNo = $seriNo + 1;
        $seriNo1 = $commonFunctions->random_num(7 - strlen($seriNo));
        $ReceiptNo = date("Y").date("m").date("d").@$seriNo1.@$seriNo;
        $last_id += $commonFunctions->addData('sys_receipts'," ( ReceiptNo,CustomerName,CustomerMobileNumber,Amount,PaymentType,ServiceType,AppointmentDate,NumberOfCamels,BarcodeNo,CreatedBy,MembershipNumber) VALUES ('".$ReceiptNo."','".$_POST['customer_name']."','".$_POST['CustomerMobileNumber']."','".$_POST['amount']."','".$_POST['payment_type']."','".$_POST['service_type']."','".$_POST['appointment_date']."','".$_POST['number_of_camels']."','".$ReceiptNo."','".$added_by."','".$_POST['membership_number']."') ",'');
        $url_bar = '../lib/barcode.php?codetype=Code128&size=40&text='.$ReceiptNo.'&print=false';
        $html .= $commonFunctions->get_print_receipt_html($ReceiptNo,$url_bar); 
    }
    $html .= '</div></body>
    </html>';
    if(!empty($last_id)){
        if(!empty($html)){ 
            echo $html;
            ?>
            <link href="<?php echo DOCUMENT_ROOT_CLASS;?>assets/css/app.min.css" rel="stylesheet" type="text/css" />
            <link href="<?php echo DOCUMENT_ROOT_CLASS;?>assets/css/common.css" rel="stylesheet" type="text/css" />
            <link href="<?php echo DOCUMENT_ROOT_CLASS;?>assets/plugins/bootstrap-colorpicker/css/bootstrap-colorpicker.css" rel="stylesheet" type="text/css" />
            <link href="<?php echo DOCUMENT_ROOT_CLASS;?>assets/plugins/bootstrap-touchspin/css/jquery.bootstrap-touchspin.min.css" rel="stylesheet" />
            <script type="text/javascript">
                var printContents = document.getElementById('DivIdToPrint').innerHTML;
                var originalContents = document.body.innerHTML;
                document.body.innerHTML = printContents;
                window.print();
                document.body.innerHTML = originalContents;
                window.onafterprint = function(){
                   window.location.href = 'receipt.php?state=list';
                }                
            </script>
            <?php
            //echo json_encode(array("data"=>$html,"url"=>""));
            exit();
        }else{
            Session::set('msg', '<div class="alert alert-success alert-dismissible mt-3" id="flash-msg">
              <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
              <strong>Success !</strong> Wow, Your Information added Successfully !</div>');
            //echo "receipt.php?state=list";
            echo json_encode(array("data"=>"","url"=>"receipt.php?state=list"));
            exit();
        }
    }else{
        Session::set('msg', '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg">
<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
<strong>Error !</strong> Data not inserted !</div>');
        echo json_encode(array("data"=>"","url"=>"receipt.php?state=list"));
        exit();
    }
}
require "../inc/header.php";
$msg = Session::get('msg');
Session::set("msg", NULL);
?>
<style type="text/css">
	.select2-container{
		width: 100% !important;
	}
</style>
    <?php $seriNo = $commonFunctions->getRowDataCount('sys_receipts','');
        $seriNo = $seriNo + 1;
        $seriNo1 = $commonFunctions->random_num(7 - strlen($seriNo));
        $rand = date("Y").date("m").date("d").@$seriNo1.@$seriNo;
    ?>
    <div class="row">
        <div class="col-sm-12">
            <div class="page-title-box">
                <div class="row">
                    <div class="col">
                        <h4 class="page-title"><?php echo ($_GET['state'] != "list")?"Receipt Creation Form":" Receipt List "; ?></h4>
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="javascript:void(0);">Generate Receipt</a></li>
                            <li class="breadcrumb-item"><a href="javascript:void(0);"><?php echo ($_GET['state'] != "list")?"$rand":"Receipt"; ?></a></li>
                        </ol>
                    </div><!--end col-->
                    <div class="col-auto align-self-center">

                        <button type="button" class="btn btn-primary save_form btn-sm" id="save_form" style="background: green;border-color: green;">
                            <i class="fa fa-save align-self-center icon-xs"></i> Create Receipt
                        </button>
                    	
                        <a href="receipt.php?state=list"><button type="button" class="btn btn-danger btn-sm">Back to Receipt List </button></a>

                    </div><!--end col-->  
                </div><!--end row-->                                                              
            </div><!--end page-title-box-->
        </div><!--end col-->
    </div><!--end row-->
    <!-- end page title end breadcrumb -->
    <?php if(@$_GET['state'] == "add"): ?>
    <style type="text/css">
        .select2-container--default .select2-selection--single .select2-selection__rendered{
            line-height: 2.8 !important;
        }
        .select2-container .select2-selection--single .select2-selection__rendered {
            font-size: .8125rem !important;
            color: #303e67 !important;
        }
    </style>
    <div class="row">
        <div class="col-12">
            <div class="row justify-content-center">
                <form action="" class="formReceipt" method="post" enctype="multipart/form-data">
                    <input type="hidden" name="action" value="ajax_call">
                    <div class="col-md-12 col-lg-12">
                        <div class="card report-card">
                            <div class="card-body">
                                <div class="row d-flex justify-content-center">
                                    <style type="text/css">
                                        .receipt-logo{ width: 9em; height: 9em; }
                                    </style>
                                    <table border="0" cellpadding="0" cellspacing="0" style="width:100%;">
                                        <tr>
                                            <td style="width: 30%;">
                                                <img src="../uploads/logo-receipt.jpg" class="receipt-logo">
                                            </td>
                                            <td>
                                                <h2 style="text-align: center;" >QATAR GENETIC LAB</h2>
                                                <h5 style="text-align: center;">Racing and Euestrain Club,<br>Al Furousiya Street,<br>Al Rayyan, Doha, Qatar</h5>
                                                <h5 style="text-align: center;">CASH/CHEQUE RECEIPT VOUCHER</h5>
                                            </td>
                                            <td style="text-align: right;">
                                                <img src="../uploads/logo-receipt-1.jpg" class="receipt-logo">
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="width: 25%;">
                                                <input type="hidden" name="receipt_no" value="<?php echo $rand;?>">
                                                <h4>Receipt No. <?php echo $rand;?> </h4>
                                                <img alt='barcode' src='../lib/barcode.php?codetype=Code128&size=40&text=<?php echo $rand;?>&print=false' style='padding-bottom: 0.5em;'/>
                                            </td>
                                            <td>
                                                <table border="1" cellpadding="0" cellspacing="0" style="width:100%;">
                                                    <tr>
                                                        <td style="width: 20%;padding: 0.3em;"><h4>Amount</h4></td>
                                                        <td style="padding: 0.3em;">
                                                            <input type="number" name="amount" value="0" class="form-control amount" min="1" required="">
                                                        </td>
                                                    </tr>
                                                </table>
                                            </td>
                                            <td style="text-align: right;">
                                                <h4>Date: <?php echo date("jS M Y"); ?></h4>
                                            </td>
                                        </tr>
                                    </table>
                                    <table border="2" cellpadding="5" cellspacing="5" style="width:100%;">
                                        <tr>
                                            <td>
                                                <table border="0" cellpadding="0" cellspacing="0" style="width:100%;">
                                                    <tr>
                                                        <td style="width:20%;">
                                                            <h4>Customer Name </h4> 
                                                        </td>
                                                        <td colspan="2">
                                                            <h4 style="width: 100%;">
                                                                <input type="text" name="customer_name" value="" class="form-control customer_name" required="" placeholder="Enter Customer Name...">
                                                            </h4> 
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td style="width:20%;">
                                                            <h4>Customer Mobile Number </h4> 
                                                        </td>
                                                        <td colspan="2">
                                                            <h4 style="width: 100%;">
                                                                <input type="text" name="CustomerMobileNumber" value="" class="form-control CustomerMobileNumber" required="" placeholder="Enter Customer Mobile Number...">
                                                            </h4> 
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td style="width:20%;">
                                                            <h4>Membership Number </h4> 
                                                        </td>
                                                        <td colspan="2">
                                                            <h4 style="width: 100%;">
                                                                <input type="text" name="membership_number" value="0" class="form-control membership_number" required="" placeholder="Enter Membership Number...">
                                                            </h4> 
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td style="width:20%;">
                                                            <h4>Amount </h4> 
                                                        </td>
                                                        <td colspan="2">
                                                            <h4 style="width: 100%;">
                                                                <input type="text" name="amount" value="0" class="form-control amount1"  required="" placeholder="Enter Amount...">
                                                            </h4> 
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td style="width:20%;">
                                                            <h4>Payment Type  </h4> 
                                                        </td>
                                                        <td colspan="2">
                                                            <h4 style="width: 100%;">
                                                                <select name="payment_type" class="form-control select2" required="">
                                                                    <option value="">Select Payment Type</option>
                                                                    <option value="Cash Payment">Cash Payment</option>
                                                                    <option value="Card Payment - POS">Card Payment - POS</option>
                                                                </select>
                                                            </h4>  
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td style="width:20%;">
                                                            <h4>Service Type </h4> 
                                                        </td>
                                                        <td colspan="2">
                                                            <h4 style="width: 100%;">
                                                                <!-- <input type="text" name="service_type" value="" class="form-control service_type"  required="" placeholder="Enter Service Type..."> -->
                                                                <select name="service_type" class="form-control service_type select2"  required="" >
                                                                  <option value="">--Select Service--</option>  
                                                                  <option value="Microchip implantation">Microchip implantation</option>
                                                                  <option value="Genotyping">Genotyping</option>
                                                                  <option value="Genotyping + ID">Genotyping + ID</option>
                                                                  <option value="Genotyping + ID: Urgent">Genotyping + ID: Urgent</option>
                                                                  <option value="ID card">ID card</option>
                                                                  <option value="Genotyping + One parent verification">Genotyping + One parent verification</option>
                                                                  <option value="Genotyping + One parent verification: Urgent">Genotyping + One parent verification: Urgent</option>
                                                                  <option value="Genotyping + One parent verification + ID">Genotyping + One parent verification + ID</option>
                                                                  <option value="Genotyping + One parent verification + ID: Urgent">Genotyping + One parent verification + ID: Urgent</option>
                                                                  <option value="Genotyping + Two parents verification">Genotyping + Two parents verification</option>
                                                                  <option value="Genotyping + Two parents verification: Urgent">Genotyping + Two parents verification: Urgent</option>
                                                                  <option value="Genotyping + Two parents verification + ID">Genotyping + Two parents verification + ID</option>
                                                                  <option value="Genotyping + Two parents verification + ID: Urgent">Genotyping + Two parents verification + ID: Urgent</option>
                                                                  <option value="Re-verification">Re-verification</option>
                                                                  <option value="Re-verification + ID">Re-verification + ID</option>
                                                                  <option value="ID data Editing">ID data Editing</option>
                                                                  <option value="Ownership Transfer">Ownership Transfer</option>
                                                                  <option value="Lost ID card Replacement">Lost ID card Replacement</option>
                                                                  <option value="Parentage with all Bulls">Parentage with all Bulls</option>
                                                                  <option value="Others">Others</option>
                                                                </select>
                                                            </h4>  
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td style="width:20%;">
                                                            <h4>Appointment Date </h4> 
                                                        </td>
                                                        <td colspan="2">
                                                            <h4 style="width: 100%;">
                                                                <input type="text" name="appointment_date" value="" class="form-control appointment_date" required="" placeholder="Enter Appointment Date...">
                                                            </h4>  
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td style="width:20%;">
                                                            <h4>Number of Camel's </h4> 
                                                        </td>
                                                        <td colspan="2">
                                                            <h4 style="width: 100%;">
                                                                <input type="number" name="number_of_camels" value="1" class="form-control number_of_camels" required="" placeholder="Enter Number of Camel's ..." min="1">
                                                            </h4>  
                                                        </td>
                                                    </tr>
                                                </table>
                                                <table border="0" cellspacing="0" cellpadding="0" style="width: 100%;">
                                                    <tr>
                                                        <td style="width: 20%;text-align: center;"> 
                                                            <br><br><br><h4>Receiver Sign</h4><br><br>
                                                            <h4 style=""></h4>
                                                        </td>
                                                        <td style="width: 60%;">

                                                        </td>
                                                        <td style="width: 20%;text-align: center;"> 
                                                            <br><br><br><h4>Accountant Sign</h4><br><br>
                                                            <h4 style=""></h4>
                                                        </td>
                                                    </tr>

                                                </table>                
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>  
                    <div class="col-lg-12 col-xl-12">
                        <div class="float-right d-print-none">
                            <button type="button" class="btn btn-primary btn-sm save_form" id="save_form" style="background: green;border-color: green;">
                                <i class="fa fa-save align-self-center icon-xs"></i> Create Receipt
                            </button>
                            <!-- <a href="sales-invoices-INV-20210623001.html" class="btn btn-soft-success btn-sm">
                                <i class="fa fa-file-invoice align-self-center icon-xs"></i> Create Invoice
                            </a> -->
                        </div>
                    </div><!--end col-->
                </form>
            </div>
        </div>
    </div>
    <?php endif; ?>
        
<?php require "../inc/footer.php"; ?>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.1/jquery.validate.min.js"></script>
<script type="text/javascript">
    $(".appointment_date").datepicker();
    $(".amount1").keyup(function(){
        var amount1 = $(".amount1").val();
        $(".amount").val(amount1);
    });
    $(".amount").keyup(function(){
        var amount = $(".amount").val();
        $(".amount1").val(amount);
    });
    $(".save_form").click(function(){
        if($(".formReceipt").valid()){
            $(".formReceipt").submit();
            /*$.ajax({
                type: 'POST',
                url: "",
                data: $('.formReceipt').serialize()+"&action=ajax_call",
                success: function(response) { 
                    var obj = JSON.parse(response);
                    if(obj.data == ''){
                        window.location.href = obj.url;
                    }else{
                        var printContents = obj.data;
                        var originalContents = document.body.innerHTML;
                        document.body.innerHTML = printContents;
                        window.print();
                        document.body.innerHTML = originalContents;
                        window.location.href = obj.url;
                    }
                }
            });*/
        }
    });
</script>
<script type="text/javascript">
function printDiv(){
    var printContents = document.getElementById('DivIdToPrint').innerHTML;
    var originalContents = document.body.innerHTML;
    document.body.innerHTML = printContents;
    window.print();
    document.body.innerHTML = originalContents;
}
</script>