<?php




  //invoice.php  




  include('db_con.php');




  include('header.php');




  include('function.php');




  




  if (isset($_SESSION['user_id'])) {




    $user_type = $_SESSION['user_type'];




    $user_id = $_SESSION['user_id'];




    $user_name = $_SESSION['user_name'];




    $branch_id = $_SESSION['branch_id'];




    $branch_name = $_SESSION['branch_name'];




    $store_id = $_SESSION['store_id'];




    $store_name = $_SESSION['store_name'];




}else{




  header('Location: login');




  die();




}




$dated = date('Y-m-d');




$sql = "SELECT MAX(jv_no) FROM jv WHERE branch_id = '".$branch_id."' and source ='GL Jv'";




            $statement = $connect->prepare($sql);




            $statement->execute(); // no need to add `$sql` here, you can take that out




            $jv_no = $statement->fetchColumn();




            settype($jv_no, "integer");









            $jv_no++;









  $jv_id = $_GET["id"];




  settype($jv_id, "integer");



















  if(isset($_POST["save"]))




  {




   




      $source = 'GL jv';




    $statement = $connect->prepare("




      INSERT INTO jv 




        (jv_no,branch_id,dated,source,ref_id,description,cur_name,cur_rate,user_id,status)




        VALUES (:jv_no, :branch_id, :dated, :source, :ref_id, :description, :cur_name, :cur_rate, :user_id, :status)




    ");




    $statement->execute(




      array(




          ':jv_no'             =>  $jv_no,




          ':branch_id'             =>  $branch_id,




          ':dated'             =>  $_POST['dated'],




          ':source'          =>  $source,




          ':ref_id'          =>  $jv_no,




          ':description'       =>  $_POST["description"],




          ':cur_name'          =>  $_POST["cur_name"],




          ':cur_rate'          =>  trim($_POST["cur_rate"]),          




          ':user_id'       =>  $user_id,




           ':status'       =>  'Active'




      )




    );









     $statement = $connect->query("SELECT LAST_INSERT_ID()");




      $jv_id = $statement->fetchColumn();









      for($count=0; $count<$_POST["total_item"]; $count++)




      {














        $statement = $connect->prepare("




          INSERT INTO jv_detail 




          (jv_id,acc_no,detail_desc,debit,credit,status,cl_date)




          VALUES(:jv_id, :acc_no, :detail_desc, :debit, :credit, :status, :cl_date)




        ");









        $statement->execute(




          array(




            ':jv_id'               =>  $jv_id,




            ':acc_no'              =>  trim($_POST["acc_no"][$count]),




            ':detail_desc'              =>  $_POST["detail_desc"][$count],




            ':debit'          =>  trim($_POST["debit"][$count]),




            ':credit'           =>  trim($_POST["credit"][$count]),




            ':status'             =>  'Active',




            ':cl_date'             =>  $_POST['dated']




            




          )




        );




      }









      $sql = "update jv_detail d, jv m  set d.or_debit = d.debit*m.cur_rate,d.or_credit = d.credit*m.cur_rate where m.jv_id=d.jv_id and m.jv_id = '".$jv_id."'";




      




        $statement = $connect->prepare($sql);




        $statement->execute();




     




      header("location:jv.php");




  }




      









     




  




  ?>




  <!DOCTYPE html>




<html lang="en">




  <head>




    <title></title>




    




    <style>




      /* Remove the navbar's default margin-bottom and rounded borders */ 




      .navbar {




      margin-bottom: 4px;




      border-radius: 0;




      }




      /* Add a gray background color and some padding to the footer */




      footer {




      background-color: #f2f2f2;




      padding: 25px;




      }




      .carousel-inner img {




      width: 100%; /* Set width to 100% */




      margin: auto;




      min-height:200px;




      }




      




      .navbar-brand:hover




      {




      background-color:#ffffff;




      }




      /* Hide the carousel text when the screen is less than 600 pixels wide */




      @media (max-width: 600px) {




      .carousel-caption {




      display: none; 




      }




      }




    </style>




     









    <title>New General Journal</title>









    <!-- Bootstrap Core CSS -->




   




  </head>




  <body>




    <style>




      .box




      {




      width: 100%;




      max-width: 1390px;




      border-radius: 5px;




      border:1px solid #ccc;




      padding: 15px;




      margin: 0 auto;                




      margin-top:50px;




      box-sizing:border-box;




      }




    </style>




    <link rel="stylesheet" href="css/datepicker.css">




    <script src="js/bootstrap-datepicker1.js"></script>




    <link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">




    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.3/css/bootstrap-select.min.css" />




    <script>




      $(document).ready(function(){




        $('#dated').datepicker({




          format: "yyyy-mm-dd",




          autoclose: true




        });




      });




    </script>




    <div class="container-fluid">




  <?php




if(isset($_GET["update"]) && isset($_GET["id"]))




      {




        $jv_id = $_GET["id"];




        $statement = $connect->prepare("




          SELECT * FROM jv 




            WHERE jv_id = :jv_id




            LIMIT 1




        ");




        $statement->execute(




          array(




            ':jv_id'       =>  $_GET["id"]




            )




          );




        $result = $statement->fetchAll();




        foreach($result as $row)




        {




        ?>




        <script>




        $(document).ready(function(){




          




          




          $('#description').val("<?php echo $row["description"]; ?>");




            $('#cur_name').val("<?php echo $row["cur_name"]; ?>");




          $('#cur_rate').val("<?php echo $row["cur_rate"]; ?>");




      




          




        });




        </script>









        <?php include('top-pannel.php');?>




        <form method="post" id="jv_form">




      <br />




      




        <div class="table-responsive">




          <table class="table table-bordered">




            <tr>




              <td colspan="2" align="center"><h2 style="margin-top:10.5px">Net Jv Detail</h2></td>




            </tr>




            <tr>




            <span id="doc_status" class="doc_status"></span>




            </tr>




            <tr>




                <td colspan="2">




                  <div class="row">




                    <div class="col-md-8">




                      <br />




                     




                      <b>Jv No.:</b> <span><?php echo $jv_no; ?></span><br />           




              




                 




                  




                  <input type="text" name="description" id="description" class="form-control input-sm" placeholder="Description" required />




                   <br />




                    </div>









                    <div class="col-md-4">




                                                    




                      <input type="text" name="dated" id="dated" class="form-control input-sm" readonly value="<?php echo $dated; ?>" AutoComplete="off" required />




                  <br />









                    <select name="cur_name" id="cur_name" class="form-control cur_name" />




                        <?php echo fill_currency($connect);?>




                      </select>




                      <br />




                      




                      <input type="text" name="cur_rate" id="cur_rate" class="form-control number_only cur_rate" placeholder="Currency rate" />




                      <br />




                    </div>




                  </div>




                  









                  <table id="jv-item-table" class="table table-bordered">




                    <tr>




                      <th width="5%">Sr#.</th>




                      <th width="40%">Account Name</th>




                        <th width="35%">Description</th>




                      <th width="10%">Debit</th>




                      <th width="10%">Credit</th>




                      




                      




                    </tr>




                    <?php




                    $statement = $connect->prepare("




                      SELECT m.acc_name,d.acc_no,d.detail_desc,d.debit,d.credit,d.or_debit,d.or_credit FROM jv_detail d,accounts m




                      WHERE m.acc_no=d.acc_no and d.jv_id = :jv_id




                    ");




                    $statement->execute(




                      array(




                        ':jv_id'       =>  $_GET["id"]




                      )




                    );




                    $item_result = $statement->fetchAll();




                    $m = 0;




                    foreach($item_result as $sub_row)




                    {




                      $m = $m + 1;




                    ?>




                    <tr>




                      <td><span id="sr_no"><?php echo $m; ?></span></td>




                        <td><select name="acc_no[]" id="acc_no<?php echo $m; ?>" class="form-control input-sm acc_no" required />




                        <option value="<?php echo $sub_row["acc_no"]; ?>"><?php echo $sub_row["acc_name"]; ?></option>




                          <?php echo fill_accounts($connect);?>









                        </select></td>




                        <td><input type="text" name="detail_desc[]" id="detail_desc<?php echo $m; ?>"  class="form-control input-sm number_only detail_desc" value="<?php echo $sub_row["detail_desc"]; ?>" /></td>




                      <td><input type="text" name="debit[]" id="debit<?php echo $m; ?>"  class="form-control input-sm number_only debit" value="<?php echo $sub_row["debit"]; ?>" /></td>




                      <td><input type="text" name="credit[]" id="credit<?php echo $m; ?>" class="form-control input-sm number_only credit" value="<?php echo $sub_row["credit"]; ?>"/>




                      <input type="hidden" name="or_debit[]" id="or_debit<?php echo $m; ?>"  class="form-control input-sm number_only or_debit" value="<?php echo $sub_row["or_debit"]; ?>"/>




                      <input type="hidden" name="or_credit[]" id="or_credit<?php echo $m; ?>"  class="form-control input-sm number_only or_credit" value="<?php echo $sub_row["or_credit"]; ?>"/></td>




                      




                      <td></td>




                    </tr>




                     <?php




                    }




                    ?>




                  </table>














                  <div align="right">




                    <button type="button" name="add_row" id="add_row" class="btn btn-success btn-xs">+</button>




                  </div>




                  </td>




                  </tr>




                




             




              <tr>




                <td align="left"><b>Total Debit : <b><span id="total_debit"></span></b></td>




               




                </tr><tr>




                <td align="left"><b>Total Credit :</b> <b><span id="total_credit"></span></b></td>   




                




               




              </tr>




              <tr>




                <td colspan="2"></td>




              </tr>




             




              <tr>




                <td colspan="2" align="center">




                  <input type="hidden" name="total_item" id="total_item" value="<?php echo $m; ?>" />




                  <input type="submit" name="save" id="save" class="btn btn-info" value="Save" />




                </td>




              </tr>




          </table>




        </div>




      </form>




       <script>




      $(document).ready(function(){




        var final_total_amt = $('#total_amount').text();




        var count = "<?php echo $m; ?>";




        




        $(document).on('click', '#add_row', function(){




          count++;




          $('#total_item').val(count);




          var html_code = '';




          html_code += '<tr id="row_id_'+count+'">';




          html_code += '<td><span id="sr_no">'+count+'</span></td>';          




          html_code += '<td><select name="acc_no[]" id="acc_no'+count+'" data-srno="'+count+'" class="form-control acc_no selectpicker" data-show-subtext="true" data-live-search="true"><?php echo fill_accounts($connect); ?></select></td>';




          html_code += '<td><input type="text" name="detail_desc[]" id="detail_desc'+count+'" data-srno="'+count+'" class="form-control detail_desc"></td>';




          html_code += '<td><input type="text" name="debit[]" id="debit'+count+'" data-srno="'+count+'" class="form-control input-sm number_only debit" /></td>';




          html_code += '<td><input type="text" name="credit[]" id="credit'+count+'" data-srno="'+count+'" class="form-control input-sm number_only credit" /></td>';          




          html_code += '<input type="hidden" name="or_debit[]" id="or_debit'+count+'" data-srno="'+count+'" class="form-control input-sm or_debit" />';




          html_code += '<input type="hidden" name="or_credit[]" id="or_credit'+count+'" data-srno="'+count+'" class="form-control input-sm or_credit" /></td>';




          html_code += '<td><button type="button" name="remove_row" id="'+count+'" class="btn btn-danger btn-xs remove_row">X</button></td>';




          html_code += '</tr>';




          $('#jv-item-table').append(html_code);




          $('.selectpicker').selectpicker();




          $('#acc_no'+count).focus();




          




        });




        




       $(document).on('click', '.remove_row', function(){









          var row_id = $(this).attr("id");




          var total_item_amount = $('#item_amount'+row_id).val();




          var final_amount = $('#total_amount').text();




          var result_amount = parseFloat(final_amount) - parseFloat(total_item_amount);




          $('#total_amount').text(result_amount);




          $('#row_id_'+row_id).remove();




          count--;




          $('#total_item').val(count);




        });














           $(document).on('blur', '.cur_name', function(){




          var cur_name = $('#cur_name').val();




          




          




          $.ajax({




                url:"cur_data.php",




                method:"POST",




                data:{cur_name:cur_name},




                dataType:"json",




                success:function(data){




                     $('#cur_rate').val(data.cur_rate);




                     




                }




           });









        });














        function cal_final_total(count)




        {




          var total_debit_amt = 0;




          var total_credit_amt = 0;




          var or_debit = 0;




            var or_credit = 0;




          









          for(j=1; j<=count; j++)




          {




            









            




            var debit_amt = $('#debit'+j).val();




            var credit_amt = $('#credit'+j).val();




           




            




            














            









            if(debit_amt>0 && credit_amt >0)




            {




              alert("Enter Only Debit Or Credit Amount")




              return false;




            }




            




            




            if(debit_amt > 0)




            {









              total_debit_amt = parseFloat(total_debit_amt) + parseFloat(debit_amt);




              




              or_debit = debit_amt;




              $('#or_debit'+j).val(or_debit);









              









             




            }else




            {




              $('#or_debit'+j).val(0);




            }









            if(credit_amt > 0)




            {









              total_credit_amt = parseFloat(total_credit_amt) + parseFloat(credit_amt);




              




              or_credit = credit_amt;




              $('#or_credit'+j).val(or_credit);




               




             




            }else




            {




              $('#or_credit'+j).val(0);




            }




            




          




            




          }




          




          $('#total_debit').text(total_debit_amt);




          $('#total_credit').text(total_credit_amt);




        }









        









        //$("#item_type"+count).change(function(){




        //var itemtype = $(".item_type option:selected").val();




       




      




        









        $(document).on('blur', '.debit', function(){




          




          cal_final_total(count);




          









          




          














        });




        $(document).on('blur', '.credit', function(){




          cal_final_total(count);




         




        });
























        




        









        $('#save').click(function(){









          cal_final_total(count);









           var total_debit = $('#total_debit').text();




           var total_credit = $('#total_credit').text();




           if(total_debit =='' &&  total_credit=='')




          {




            alert("Not Record Found For Save");




           




            return false;




          }









           if(total_debit != total_credit)




          {




            alert("Debit Or Credit Not Equal");




           




            return false;




          }









          




         









          









          









          $('#jv_form').submit();









        });









      });




      </script>









       <?php




     }




   }




?>