<?php $editable=false; if($order[0]['order_status']=='Open'){$editable=true;}?>
<style>                                    
    
    hr{
        border-top: 1px solid #188ae2;
    }
    #tblproducts tr td {
        border: 1px solid #188ae2;
        color: #000 !important;
    }
    #tblproducts tr th {
        background: #188ae2;
        border: 1px solid #188ae2;
        color: #000 !important;
    }
    p{
        margin: 0px !important;
        color: #000 !important;
    }
</style>
<div class="wrapper">
            <div class="container">
                <!-- Page-Title -->
                <div class="row">
                    <div class="col-sm-12">
                        <h4 class="page-title">Franchise Product Return</h4>
                    </div>
                    
                </div>
               
                <div class="row">
                    <div class="col-md-12">
                        <div class="panel panel-default">
                            <!-- <div class="panel-heading">
                                <h4>Invoice</h4>
                            </div> -->
                            <div class="panel-body">
                                <div class="clearfix">
                                    <div class="pull-left">
                                        <p><?php echo $business[0]['business_address'];?></p> 
                                        <p><?php echo $business[0]['business_phone1'];?></p> 
                                    </div>
                                    <div class="pull-right">
                                        <h3 class="logo invoice-logo"><?php if(isset($business)){ echo "<img style='width:150px;' src='".base_url().'assets/images/business/'.$business[0]['business_logo']."' alt='".$business[0]['business_name']."' />";} else {echo 'SalonPK';}?></h3>
                                    </div>
                                </div>
                                <hr>
                                <div class="row">
                                    <h1 style="text-align: center; color:#188ae2;"><strong>RETURN NOTE</strong></h1>
                                </div>
                                     
                                <div class="row">
                                    <div class="col-md-12">

                                        <div class="pull-left m-t-10">
                                            <input name="orderid" id="orderid"  type="hidden" value="<?php echo $order[0]['id_franchise_orders']; ?>"/>
                                            <input name="customerid" id="customerid" type="hidden"  value="<?php echo $franchise->id_franchises; ?>"/>
                                            <input name="customername" id="customername" type="hidden"  value="<?php echo$franchise->franchise_name; ?>">
                                            <input name="customerntn" id="customerntn" type="hidden"  value="<?php echo $franchise->franchise_ntn; ?>">
                                            <p style="background: #188ae2; color:#000 !important;"><strong>Bill To:</strong></p>
                                            <address>
                                                <strong>Company: <span id="companyname"><?php echo $franchise->franchise_name; ?> <?php echo $order[0]['franchise_order_extra'];?></span></strong><br>
                                              <strong>NTN: <?php echo $franchise->franchise_ntn; ?></strong><br>
                                              <strong>Address:<?php echo $franchise->franchise_address; ?></strong><br>
                                              <strong>Email:<?php echo $franchise->franchise_email; ?></stong><br>
                                              <strong>Phone:</strong><?php echo $franchise->franchise_office_phone; ?>
                                            </address>
                                        </div>
                                        <div class="pull-right m-t-10">
                                            <table>
                                                <tr><td><strong>CUSTOMER ID:</strong> </td><td style="float:right"><?php if(null!==$franchise->id_franchises){ echo $franchise->id_franchises;}else{echo '1';}?></td></tr>
                                                <td><strong>INVOICE DATE: </strong></td><td style="float:right"><?php if(null !==$order[0]['invoice_date'] && $order[0]['invoice_date']!==''){echo $order[0]['formatted_invoice_date'];} else{ echo date('d-m-Y');} ?></td></tr>
                                                <tr><td><input type="hidden" id="invoice_date" value = "<?php if(null !==$order[0]['invoice_date'] && $order[0]['invoice_date']!==''){echo $order[0]['invoice_date'];} else{ echo date('Y-m-d H-i-s');} ?>"></td><td></tr>
                                                <tr><td><strong>DUE DATE: </strong></td><td style="float:right"><?php if(null !==$order[0]['due_date'] && $order[0]['due_date']!==''){echo $order[0]['due_date'];} ?></td></tr>
                                                <tr><td><strong>INVOICE #:</strong> </td><td><input id="invoicenumber" style="border:none; text-align: right; float:right; width:80px;" value="<?php if($editable==false){echo $order[0]['invoice_number']; }else{if(null!==$franchiseinvoicenumber->invoice_number){ echo $franchiseinvoicenumber->invoice_number;}else{echo '1';}}?>"></td></tr>
                                            </table>
                                        </div>
                                    </div><!-- end col -->
                                </div>
                                <!-- end row -->
                               
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="table-responsive">
                                            <table class="table m-t-10" id="tblproducts">
                                                <thead>
                                                    <tr>
                                                        <th class="hidden-print">#</th>
                                                        <th class="hidden-print">Brand</th>
                                                        <th>Item</th>
                                                        <th class="hidden-print">Batch</th>
                                                        
                                                        
                                                        <th class="" style='text-align:right;'>Unit Price</th>
                                                        <th>Qty.</th>
                                                        <th class="hidden-print" style='text-align:right;'>Total Price</th>
                                                        <th class="" style='text-align:right;'>Discount / unit</th>
                                                        <th class="" style='text-align:right;'>Discounted Unit Price</th>
                                                        <th class="hidden-print" style='text-align:right;'>Total Discount</th>
                                                        <th style='text-align:right;'>Final Price</th>
                                                        <th>Return Qty</th>
                                                        <th>Return Value</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php $subtotal=0; if(isset($order)){
                                                        $x=1; 
                                                        foreach($order as $product){
                                                            $subtotal=$subtotal+$product['discounted_price'];
                                                        ?>
                                                        <tr id="idtr">
                                                            <td class="hidden-print"><input type="hidden" id="product_id" name="product_id" value="<?php echo $product['product_id'];?>"><?php echo $x;?></td>
                                                            <td class="hidden-print"><?php echo $product['business_brand_name']; ?></td>
                                                            <?php 
                                                                $dateObj   = DateTime::createFromFormat('!m', intval($product['order_month']));
                                                                $monthName = $dateObj->format('F');
                                                            ?>
                                                            <td><?php echo '<span class="productname">'.$product['product_name'].' '.$product['product_detail'].'</span>&nbsp;<span class="category">'.$monthName.'-'.$product['order_year'].'</span>'; ?></td>
                                                            <td class="hidden-print">
                                                                <input type="hidden" id="batch_id" name="batch_id" value="<?php echo $product['batch_id'];?>">
                                                                <input type="hidden" id="batch" name="batch" value="<?php echo $product['batch'];?>">
                                                                <input type="hidden" id="batch_amount" name="batch_amount" value="<?php echo $product['batch_amount'];?>">
                                                                <?php echo $product['batch']; ?>
                                                            </td>
                                                                                                                        
                                                            <td class="" style='text-align:right;'><?php echo $product['unit_price']; ?></td>
                                                            <td><?php echo $product['qty']; ?></td>
                                                            <td class="hidden-print" style='text-align:right;'><?php echo $product['unit_price']*$product['qty']; ?></td>
                                                            <td class="" style='text-align:right;'><?php echo $product['unit_discount']; ?></td>
                                                            <td class="" style='text-align:right;'><?php echo $product['unit_price'] - $product['unit_discount']; $discountedunitprice = $product['unit_price'] - $product['unit_discount']; ?></td>
                                                            <td class="hidden-print" style='text-align:right;'><span id="disc_perc"><?php echo number_format($product['unit_discount']*$product['qty'],2); ?></span</td>
                                                            <td style='text-align:right;'><?php echo CURRENCY_CODE;?>.<span class="combat" id="unitcost<?php echo $product['product_id']; ?>"><?php echo $product['discounted_price']; ?></span></td>
                                                            <td>
                                                            <input class="text-danger" style = "border:none; width: 70px" type= "number" min="0" max="<?php echo (float)$product['qty']; ?>" onKeyUp="if(this.value><?php echo (float)$product['qty']; ?>){this.value='<?php echo (float)$product['qty']; ?>';}else if(this.value<0 || this.value==''){this.value='0';}" onchange="update_return_value(this.value, <?php echo $discountedunitprice.', '.$product['product_id'].', '.$order[0]['invoice_discount_perc']; ?>);" name="return_qty" id= "return_qty_<?php echo $product['product_id']; ?>" value = "0" />    
                                                            <td class="" style='text-align:right;' name="return_value" id= "return_value_<?php echo $product['product_id']; ?>">0</td>
                                                            </td>
                                                        </tr>
                                                        <?php $x++;}} ?>
                                                </tbody>
                                            </table>
                                            
                                        </div>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-sm-5 col-xs-5 col-md-6 col-lg-6 ">
                                        <div class="clearfix m-t-40">
                                            <h5 class="text-inverse font-600">Notes:</h5>
                                            <textarea style="border:none;" rows="8" id="txtremarks" class="form-control" ><?php echo $order[0]['invoice_remarks'];?></textarea>
                                        </div>
                                    </div>
                                    <div class="col-md-3 col-sm-7 col-xs-7 col-md-offset-3">
                                        <p class="text-right"> <?php echo CURRENCY_CODE;?>. <input id="txt_org_subtotal" name="txt_org_subtotal" class="form-inline" readonly="readonly" style="text-align:right; width: 80px; border: none;" value='<?php echo number_format((float)$subtotal, 2, '.', '');?>'></p>
                                        <p style="display: none;" class="text-right hidden-print"><b>After Product Discount: </b>   <?php echo CURRENCY_CODE;?>. <input id="txtsubtotal" class="form-inline" readonly="readonly" style="width: 80px; border: none;" value='<?php echo number_format((float)$subtotal, 2, '.', '');?>'></p>
                                        
                                        <p class="text-right">Discount %: <input <?php if($editable==false){echo " readonly='readonly' "; }?> class="numeric" id="discount_in_percent" class="form-inline " style="text-align:right;width: 80px; border: none;" value="<?php echo $order[0]['invoice_discount_perc']; ?>"/></p>
                                        <p class="text-right">Discount <span id="txtloyalty"></span> <?php echo CURRENCY_CODE;?>.: <input <?php if($editable==false){echo " readonly='readonly' "; }?> class="numeric"  id="txtdiscount" class="form-inline " style="text-align:right;width: 80px; border: none;" value="<?php echo $order[0]['invoice_discount']; ?>"/></p>
                                        
                                        <p class="text-right">Other Charges <?php echo CURRENCY_CODE;?>.: <input <?php if($editable==false){echo " readonly='readonly' "; }?> class="numeric" onchange="updatetotal()" id="txtothercharges" class="form-inline " style="text-align:right;width: 80px; border: none;" value="<?php echo $order[0]['invoice_other_charges']; ?>"/></p>
                                        <hr> 
                                        
                                        <p class="text-right"><b>Amount Exclusive of Taxes: </b>:   <?php echo CURRENCY_CODE;?>. <input id="txtgross" readonly="readonly" class="form-inline " style="text-align:right;width: 80px; border: none;" value="<?php echo $order[0]['invoice_gross_total']; ?>"/></p>
                                        <div class="m-t-10 m-b-10" id="divtaxes">
                                            <p class="text-right">
                                                Amount of Sales Tax 
                                                <input <?php if($editable==false){echo " readonly='readonly' "; }?> class="decimal" id="tax_in_perc" class="form-inline hidden-print" style="text-align:right;width: 60px; border: none;" value="<?php echo $order[0]['invoice_sales_tax_perc']; ?>"/> 
                                                <span >%</span>: 
                                                <input <?php if($editable==false){echo " readonly='readonly' "; }?> class="numeric" onchange="updatetotal()" id="txtsalestax" class="form-inline " style="text-align:right; width: 80px; border: none;" value="<?php echo $order[0]['invoice_sales_tax']; ?>"/></p>
                                        </div>
                                        <hr>
                                        <p class="text-right"><b>Amount Inclusive of Taxes:  <?php echo CURRENCY_CODE;?>.</b> <input id="txttotalpayable" readonly="readonly" class="form-inline " style="text-align:right;width: 80px; border: none; font-weight: bold;" value="<?php echo $order[0]['invoice_total_receivable']; ?>" /></p>
                                        <p class="text-right"><b>Received Amount :</b><input readonly="readonly" class="form-inline " style="text-align:right;width: 80px; border: none; font-weight: bold;" value="<?php echo $received_amount[0]['credit'];?>"/></p>
                                        <p class="text-right"><b>Remaining Amount :</b><input readonly="readonly" class="form-inline " style="text-align:right;width: 80px; border: none; font-weight: bold;" value="<?php echo $received_amount[0]['balance'];?>"/></p>
                                    </div>
                                    
                                    <div class="row" style="">
                                        <div class="col-md-4 col-sm-4 col-xs-4">                                           
                                            <p style="border-top:1px solid #000;">For <?php echo $business[0]['business_name'];?></p>
                                        </div>
                                    </div>
                                    
                                    <div class="row m-t-40">    
                                        <div class="col-md-12 " style="text-align:center;">
                                            <p><i>Please make all checks payable to</i><strong> <?php echo $business[0]['business_name'];?></strong></p>
                                            <p><strong>Thank You For Your Business!</strong></p>
                                            <p>For any queries, please email to <?php echo $business[0]['business_email'];?></p>
                                        </div>
                                    </div>
                                </div>
                                
                                <div class="hidden-print">
                                    <hr>
                                    <div class="pull-left">
                                        
                                    </div>
                                    <div class="pull-right">
                                       
                                        
                                        <?php if($editable==true){?>
                                        <a href="javascript:void(0);" onclick="createinvoice();" class="btn btn-primary disabled waves-effect waves-light" id="btnsubmit">Submit</a>
                                        <?php } else { ?>
                                        <a href="javascript:window.print()" class="btn btn-inverse waves-effect waves-light" id="btnprint"><i class="fa fa-print"></i></a>
                                         <?php if($order[0]['order_status']=='Invoiced'){?>
                                        <form id="cancelform" style="display:inline-block;" method="post" action='<?php echo base_url();?>franchise_controller/cancelorderinvoice'>
                                            <input type="hidden" name="csrf_test_name" id="order_cancel_csrf" value=""/>                                                
                                            <input type="hidden" name='order_id' value='<?php echo $order[0]['id_franchise_orders'];?>'>
                                            <input type="hidden" name='selected_franchise' value='<?php echo $order[0]['franchise_id'];?>'>
                                            <button type="submit" onclick="$('#order_cancel_csrf').val($('#cook').val());" class="btn btn-icon waves-effect waves-light btn-danger "> <i class="fa fa-trash"></i> Cancel </button>  
                                        </form>
                                         <?php } ?>
                                        <?php } ?>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>

                </div>
                <!-- end row -->
             
            </div> 
<script>
    $(document).ready(function(){
        
        $(".numeric").keypress(function (e) {
            //if the letter is not digit then display error and don't type anything
            if (e.which != 13 && e.which != 8 && e.which != 0  && e.which != 46 && (e.which < 48 || e.which > 57)) {
               return false;
           }
        });
        $(".multinumeric").keypress(function (e) {
            //if the letter is not digit then display error and don't type anything
            if (e.which != 13 && e.which != 8 && e.which != 0  && e.which != 46 && e.which != 44 && (e.which < 48 || e.which > 57)) {
               return false;
           }
        });
        $(".decimal").keypress(function(e) {
            //if the letter is not digit then display error and don't type anything
            if (e.which != 13 && e.which != 8 && e.which != 0  && e.which != 46 && (e.which < 48 || e.which > 57) ) {
                
                return false;
            }
        });
        
                
        updatetotal();
        
        toastr.options = {
            "closeButton": true,
            "debug": true,
            "newestOnTop": true,
            "progressBar": true,
            "positionClass": "toast-bottom-center",
            "preventDuplicates": true,
            "onclick": null,
            "showDuration": "100",
            "hideDuration": "500",
            "timeOut": "5000",
            "extendedTimeOut": "1000",
            "showEasing": "swing",
            "hideEasing": "linear",
            "showMethod": "fadeIn",
            "hideMethod": "fadeOut"
        }
        
        $("#txtdiscount").change(function(){
            
            if($('#txtothercharges').val()===""){$('#txtothercharges').val(0);}
            var subtotalextra = parseFloat($('#txtsubtotal').val())+parseFloat($('#txtothercharges').val());
            var parcentage = (parseFloat($(this).val()) / subtotalextra) * 100;
            $('#discount_in_percent').val(parcentage.toFixed(2));
            //$("#paid").val($("#grandtotal").val());
            //$("#balanceamount").val(parseFloat($("#grandtotal").val()) - parseFloat($("#paid").val()));
            updatetotal();
            
        });
        $("#discount_in_percent").change(function(){
            
            if($('#txtothercharges').val()===""){$('#txtothercharges').val(0);}
            var subtotalextra = parseFloat($('#txtsubtotal').val())+parseFloat($('#txtothercharges').val());
            var discount = (parseFloat($(this).val()) * subtotalextra) / 100;
            $('#txtdiscount').val(Math.round(discount));
            //$("#paid").val($("#grandtotal").val());
            //$("#balanceamount").val(parseFloat($("#grandtotal").val()) - parseFloat($("#paid").val()));
            updatetotal();
            
        });
        $("#tax_in_perc").change(function(){
            
            var grosstotal = parseFloat($('#txtgross').val());
            var salestax = (parseFloat($(this).val()) * grosstotal) / 100;
            $('#txtsalestax').val(Math.round(salestax));
            //$("#paid").val($("#grandtotal").val());
            //$("#balanceamount").val(parseFloat($("#grandtotal").val()) - parseFloat($("#paid").val()));
            updatetotal();
            
            
        });

        

    });   
    
       
    function updatetotal(){
        
        if($('#txtdiscount').val()===""){$('#txtdiscount').val('0');}
        if($('#txtothercharges').val()===""){$('#txtothercharges').val('0');}
        
        var grosstotal=(parseFloat($("#txtsubtotal").val()) +  parseFloat($("#txtothercharges").val())) - parseFloat($("#txtdiscount").val());
        var taxabletotal=(parseFloat($("#txtsubtotal").val()) +  parseFloat($("#txtothercharges").val())) - parseFloat($("#txtdiscount").val());
        var orggrosstotal = parseFloat($("#txt_org_subtotal").val()) +  parseFloat($("#txtothercharges").val());
        
        $("#txtgross").val(grosstotal);
        
         var salestax = (parseFloat($('#tax_in_perc').val()) * grosstotal) / 100;
         $('#txtsalestax').val(Math.round(salestax));
       
        var totalpayable=grosstotal +parseFloat(salestax);
        $("#txttotalpayable").val(Math.round(totalpayable));
        $("#grandtotal").val(Math.round(grosstotal));
        
        $('#btnsubmit').removeClass('disabled');
        
        if(parseFloat($("#txtsalestax").val()) > 0){
            $("#saledtaxtag").show();
        }else {
            $("#saledtaxtag").hide();
        }
        
        
    }
    
    function invoice_storeOTblValues(){
    
        var TableData = new Array();
        $('#tblproducts tr').each(function(row, tr) {
            TableData[row] = {
                "seq": $(tr).find('td:eq(0)').text()
                , "productid": $(tr).find('td:eq(0) input').val()
                , "brandname": $(tr).find('td:eq(1)').text()
                , "productname": $(tr).find('td:eq(2) .productname').text()
                , "category": $(tr).find('td:eq(2) .category').text()
                , "batch_id": $(tr).find('td:eq(3) #batch_id').val()
                , "batch": $(tr).find('td:eq(3) #batch').val()
                , "batch_amount": $(tr).find('td:eq(3) #batch_amount').val()
                
                , "staff_name": $(tr).find('td:eq(4)').text()
                , "unit_price": $(tr).find('td:eq(5)').text()
                , "qty": $(tr).find('td:eq(6)').text()
                , "product_price": $(tr).find('td:eq(7)').text()
                , "product_discount": $(tr).find('td:eq(8)').text()
                , "discount_perc": $(tr).find('td:eq(9) #disc_perc').text()
                , "discounted_price": $(tr).find('td:eq(10) .combat').text()
            }
        });
        TableData.shift();  // first row will be empty - so remove
        
        return TableData;
    }
    
    function createinvoice(){
    
            create_new_invoice();   
        
    }

    function create_new_invoice(){
        
        var TableData;
            TableData = invoice_storeOTblValues();
            TableData = $.toJSON(TableData);
        
        
        $('#btnsubmit').addClass('disabled');
        $('#btnsubmit').html('<i class="fa fa-spin fa-spinner"></i> Please wait');
    
        $.ajax({
            type: 'POST',
            url: '<?php echo base_url();?>franchise_controller/updateorderinvoice',
            data: {
                tabledata: TableData,
                orderid: $("#orderid").val(),
                customerid:$("#customerid").val(),
                customername:$("#customername").val(),
                customercell:$("#customercell").val(),
                customerntn: $("#customerntn").val(),
                companyname:$("#companyname").html(),
                
                orgsubtotal:$("#txt_org_subtotal").val(),
                discount:$("#txtdiscount").val(),
                discount_perc: $('#discount_in_percent').val(),
                other_charges: $('#txtothercharges').val(),
                
                grosstotal:$("#txtgross").val(),
                
                tax_in_perc:$("#tax_in_perc").val(), 
                salestax:$("#txtsalestax").val(),                
                grandtotal:$("#grandtotal").val(),
                totalpayable:$("#txttotalpayable").val(),
                
                
                remarks: $("#txtremarks").val(),
                invoicedate: $("#invoice_date").val(),
                invoicenumber : $("#invoicenumber").val()
            },
            success: function(data) {
                var result = data.split("|");
                if (result[0] === "Success") {
//                    $("#txtinvoiceid").val(result[1]);
//                    toastr.success('Order Invoiced!', 'Print the invoice');
//                    $('#btnsubmit').html('Done');
                    location.reload();
                } else {
                    swal({
                        title: "Check Available Stock",
                        text: result[1],
                        type: "error",
                        confirmButtonText: 'OK!'
                    });
                    $('#btnsubmit').removeClass('disabled');
                    $('#btnsubmit').html('Submit');
                }

            }
        });
        
        $("#txtdiscount").attr("readonly",true);
        $("#discount_in_percent").attr("readonly",true);
        $(".discount_by_product").attr("readonly",true);
        $(".perc_discount_product").attr("readonly",true);
        
        
    }
    
    
    function update_return_value(unitprice, returnqty, id, inv_disc_perc){
     console.log(unitprice);
     console.log(returnqty);
     console.log(id);
        var value = unitprice * returnqty;
        
        if(inv_disc_perc>0){
            var value =  value - (value * inv_disc_perc/100);
        }
        
        console.log(value);
        var elem = "#return_value_" + id;
         console.log(elem);
        $(elem).html(value);
    
    }
 
</script>