<?php
define('DOCUMENT_ROOT_CLASS','../');
define('DOCUMENT_ROOT_INC_FOLDER','');
$filepath = realpath(dirname(__FILE__));

if(!empty($_GET["action"]) and $_GET["action"] == "ajax_call1"){
    require "../inc/ajax_header.php";
    $keyword = $_GET["keyword"]; 
    $getData = $commonFunctions->getData('crm_cust_details'," WHERE name LIKE '%".$keyword."%' ");
    $html = "";
    $skillData = array(); 
    foreach ($getData as $rw) {
        $data['id'] = $rw['name']; 
        $data['value'] = $rw['name']; 
        array_push($skillData, $data); 
    } 
    echo json_encode($skillData);
    exit();
}
if(!empty($_GET["action"]) and $_GET["action"] == "ajax_call2"){
    require "../inc/ajax_header.php";
    $keyword = $_GET["keyword"];
    $customer_name = $_GET["customer_name"];
    $getData1 = $commonFunctions->getRowData('crm_cust_details'," WHERE name LIKE '%".$customer_name."%' ");
    $id = @$getData1['id'];

    $getData = $commonFunctions->getData('crm_add_bk'," WHERE third_party_id = '".$id."' and mobile LIKE '%".$keyword."%' ");
    $html = "";
    $skillData = array(); 
    foreach ($getData as $rw) {
        $data['id'] = $rw['mobile']; 
        $data['value'] = $rw['mobile']; 
        array_push($skillData, $data); 
    } 
    echo json_encode($skillData);
    exit();
}
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>
    <style>@page { size: A5;margin:20px;margin-top:0px;margin-bottom:0px; }</style>
    <div id='DivIdToPrint'>";
    for ($ij=0; $ij < $_POST["number_of_camels"]; $ij++) { 
        $url_bar = '';
        $seriNo = $commonFunctions->getRowDataCount('fin_accnt_pymt_rpct','');
        $seriNo = $seriNo + 1;
        $seriNo1 = $commonFunctions->random_num(7 - strlen($seriNo));
        $RecieptVoucherNo = date("Y").date("m").date("d").@$seriNo1.@$seriNo;
        $last_id += $commonFunctions->addData('fin_accnt_pymt_rpct'," ( RecieptVoucherNo,CustomerName,CustomerMobileNumber,AmountReceived,PaymentType,ServiceType,AppointmentDate,NumberOfCamels,BarcodeNo,CreatedBy,MembershipNumber,AmountReceivedAgainst,Remark) VALUES ('".$RecieptVoucherNo."','".$_POST['customer_name']."','".$_POST['CustomerMobileNumber']."','".$_POST['amount']."','".$_POST['payment_type']."','".$_POST['service_type']."','".$_POST['appointment_date']."','".$_POST['number_of_camels']."','".$RecieptVoucherNo."','".$added_by."','".$_POST['membership_number']."','".$_POST['AmountReceivedAgainst']."','".$_POST['remark']."') ",'');
        $url_bar = '../lib/barcode.php?codetype=Code128&size=40&text='.$RecieptVoucherNo.'&print=true';
        $html .= $commonFunctions->get_print_receipt_voucher_html($RecieptVoucherNo,$url_bar); 
    }
    $html .= '</div></body>
    </html>';
    if(!empty($last_id)){
        if(!empty($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" />
            <?php echo $html; ?>
            <script type="text/javascript">
                window.onafterprint = function(){
                   window.location.href = 'receipt-voucher.php?state=list';
                }
                if(confirm("do you want to print all the new generated receipts ?")){
                    var printContents = document.getElementById('DivIdToPrint').innerHTML;
                    var originalContents = document.body.innerHTML;
                    document.body.innerHTML = printContents;
                    window.print();
                    document.body.innerHTML = originalContents;         
                }else{
                    window.location.href = 'receipt-voucher.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-voucher.php?state=list";
            echo json_encode(array("data"=>"","url"=>"receipt-voucher.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-voucher.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('fin_accnt_pymt_rpct','');
        $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 Voucher Creation Form":" Receipt Voucher List "; ?></h4>
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="javascript:void(0);">Generate Receipt Voucher</a></li>
                            <li class="breadcrumb-item"><a href="javascript:void(0);"><?php echo ($_GET['state'] != "list")?"$rand":"Receipt Voucher"; ?></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 Voucher
                        </button>
                    	
                        <a href="receipt-voucher.php?state=list"><button type="button" class="btn btn-danger btn-sm">Back to Receipt Voucher 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{ height: 9em; }
                                    </style>
                                    <table border="0" cellpadding="0" cellspacing="0" style="width:100%;">
                                        <tr>
                                            <?php $orgData = $commonFunctions->getRowData("sys_erp_organaization",""); ?>
                                            <td style="width:30%">
                                                <img src="<?php echo DOCUMENT_ROOT_CLASS;?>uploads/admin-setting/company/<?php echo @$orgData['erp_sys_org_logo'];?>" class="receipt-logo">
                                            </td>
                                            <td>
                                                <h2 style="text-align: center;" ><?php echo @$orgData['erp_sys_org_name'];?></h2>
                                                <h5 style="text-align: center;"><?php echo @$orgData['erp_sys_org_address'];?> <br><?php echo $commonFunctions->getName(@$orgData['erp_sys_org_country'],'sys_erp_adm_countries','label','rowid');?>, <?php echo $orgData['erp_sys_org_city'];?><br>P.O.Box - <?php echo $orgData['erp_sys_org_province'];?></h5>
                                                <h5 style="text-align: center;">CASH/CHEQUE RECEIPT VOUCHER</h5>
                                            </td>
                                            <td style="text-align: right;">
                                                <!-- <h4>Receipt No. <?php echo $rand;?> </h4> -->
                                                <img alt='barcode' src='../lib/barcode.php?codetype=Code128&size=40&text=<?php echo $rand;?>&print=true' style="border: 1px solid;padding-top: 1em;" />
                                                <h4>Date: <?php echo date("jS M Y"); ?></h4>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="width: 25%;">
                                                <input type="hidden" name="receipt_no" value="<?php echo $rand;?>">
                                                <!-- <h4>Receipt Voucher 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;'/> -->
                                                <h4>Receipt Voucher No. <?php echo $rand;?> </h4>
                                            </td>
                                            <td>
                                                <table border="1" cellpadding="0" cellspacing="0" style="width:100%;">
                                                    <tr>
                                                        <td style="width: 32%;padding: 0.3em;"><h4>Amount Received</h4></td>
                                                        <td style="padding: 0.3em;">
                                                            <input type="number" name="amount" value="0" class="form-control amount" min="1" required="">
                                                        </td>
                                                    </tr>
                                                </table><br>
                                            </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..." style="text-align: center;">
                                                            </h4> 
                                                        </td>
                                                        <td style="width:20%;text-align: right;font-size: 20px; font-weight:600;">
                                                            <h4>إسم العميل </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..." style="text-align: center;">
                                                            </h4> 
                                                        <td style="width:20%;text-align: right;font-size: 20px; font-weight:600;">
                                                            <h4>رقم هاتف العميل </h4> 
                                                        </td> 
                                                    </tr>
                                                    <tr style="display: none;">
                                                        <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..." style="text-align: center;">
                                                            </h4> 
                                                        </td>
                                                        <td style="width:20%;text-align: right;font-size: 20px; font-weight:600;">
                                                           <h4>رقم عضوية المشارك</h4> 
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td style="width:20%;">
                                                            <h4>Amount Received </h4> 
                                                        </td>
                                                        <td colspan="2">
                                                            <h4 style="width: 100%;">
                                                                <input type="text" name="amount" value="0" class="form-control amount1"  required="" placeholder="Enter Amount..." style="text-align: center;">
                                                            </h4> 
                                                        </td>
                                                        <td style="width:20%;text-align: right;font-size: 20px; font-weight:600;">
                                                            <h4>المبلغ</h4> 
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td style="width:20%;">
                                                            <h4>Payment Type  </h4> 
                                                        </td>
                                                        <td colspan="2">
                                                            <h4 style="width: 100%;text-align: center;">
                                                                <select name="payment_type" class="form-control select2" required="" >
                                                                    <option value="">Select Payment Type</option>
                                                                    <option value="Cash Payment">Cash Payment</option>
                                                                    <option value="Cheque Payment">Cheque Payment</option>
                                                                </select>
                                                            </h4>  
                                                        </td>
                                                        <td style="width:20%;text-align: right;font-size: 20px; font-weight:600;">
                                                            <h4>نوع الدفع  </h4> 
                                                        </td>
                                                    </tr>
                                                    <tr style="display: none;">
                                                        <td style="width:20%;">
                                                            <h4>Service Type </h4> 
                                                        </td>
                                                        <td colspan="2">
                                                            <h4 style="width: 100%;text-align: center;">
                                                                <!-- <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>
                                                        <td style="width:20%;text-align: right;font-size: 20px; font-weight:600;">
                                                            <h4>الفحص المطلوب </h4> 
                                                        </td>
                                                    </tr>
                                                    <tr style="display: none;">
                                                        <td style="width:20%;">
                                                            <h4>Appointment Date </h4> 
                                                        </td>
                                                        <td colspan="2">
                                                            <h4 style="width: 100%;text-align: center;">
                                                                <input type="text" name="appointment_date" value="" class="form-control appointment_date" required="" placeholder="Enter Appointment Date..." style="text-align: center;">
                                                            </h4>  
                                                        </td>
                                                        <td style="width:20%;text-align: right;font-size: 20px; font-weight:600;">
                                                            <h4>تاريخ الموعد </h4> 
                                                        </td>
                                                    </tr>
                                                    <tr style="display: none;">
                                                        <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" style="text-align: center;">
                                                            </h4>  
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td style="width:20%;">
                                                            <h4>Amount Received Against </h4> 
                                                        </td>
                                                        <td colspan="2">
                                                            <h4 style="width: 100%;text-align: center;">
                                                                <input type="text" name="AmountReceivedAgainst" value="" class="form-control AmountReceivedAgainst" required="" placeholder="Enter Amount Received Against..." style="text-align: center;">
                                                            </h4>  
                                                        </td>
                                                        <td style="width:20%;text-align: right;font-size: 20px; font-weight:600;">
                                                            <h4></h4> 
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td style="width:20%;">
                                                            <h4>Remark </h4> 
                                                        </td>
                                                        <td colspan="2">
                                                            <h4 style="width: 100%;text-align: center;">
                                                                <input type="text" name="remark" value="" class="form-control remark" placeholder="Enter Remark ..." >
                                                            </h4>  
                                                        </td>
                                                        <td style="width:20%;text-align: right;font-size: 20px; font-weight:600;">
                                                            <h4></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 Voucher
                            </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;
}

$(function() {
    $(".customer_name").autocomplete({
        source: function(request, response) {
            $.getJSON("", { keyword: request.term,action:"ajax_call1" }, response);
        },
        minLength: 2,
    });
    $(".CustomerMobileNumber").autocomplete({
        source: function(request, response) {
            $.getJSON("", { customer_name: $('.customer_name').val(), keyword: request.term,action:"ajax_call2" }, response);
        },
        minLength: 2,
    });
});
</script>
