<?php require_once('../auth/auth.php');require_once('../auth/function.php'); ?>
<?php
if (isset($_POST['s_acid'])) {
  $s_acid = $_POST['s_acid'];
  $acv = mysqli_query($conn,"SELECT ac_name,balance,ac_number FROM accounts WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND acid='$s_acid' AND active=1 LIMIT 1") or die (mysqli_error($conn));
  $acr = mysqli_fetch_assoc($acv);

  $s_name    = $acr['ac_name'];
  $balance   = $acr['balance'];
  $ac_number = $acr['ac_number'];
}
  $custom_field= mysqli_query($conn,"SELECT purchase FROM custom_field WHERE coid='$coid' LIMIT 1 ") or die(mysqli_error($conn));
  $rowcf= mysqli_fetch_assoc ($custom_field);

  @$p=$rowcf['purchase'];
  function input_check($ap)
  {
   if (strpos($GLOBALS['p'], $ap) !== false) { return 1;} 
  }
  
  //  mysqli_query($conn,"DELETE FROM imei_tracker_edit WHERE  coid='$coid' AND bid='$bid' AND brid='$brid' AND active=0") or die(mysqli_error($conn));
?>



<section class="content">
  <div class="container-fluid">
    <div class="row">
      <div class="col-md-1"></div>
      <div class="col-md-10">
        <div class="card card-danger card-outline direct-chat direct-chat-primary shadow-none" style="border-left:1px solid #969c9d;border-right:1px solid #969c9d;border-bottom:1px solid #969c9d;">
          <div class="card-header" style="padding: 5px 8px;">
            <h3 class="card-title"><?php echo language(58);?></h3>
            <div class="card-tools">
              <span class="mark1" onclick="copy(this);" title="Copy Invoice"><?php echo $invoice = invoice("EPRET");?></span>
              <div class="btn-group">
                <button type="button" class="btn btn-tool" data-toggle="dropdown"  title="More Tools"><i class="fa-solid fa-ellipsis-vertical"></i>
                </button>
                <ul class="dropdown-menu dropdown-menu-right">
                  <li><a class="dropdown-item"><i class="fas fa-inbox"></i> Others</a></li>
                </ul>
                <button type="button" class="btn btn-tool" data-card-widget="remove" onclick="$.post('../purchase_retrun/order_list_delete.php',{prinvoice:'<?php echo $invoice;?>'});">
                <i class="fas fa-times"></i>
                </button>
              </div>
            </div>
          </div>











          <div class="card-body">
            <div class="col-md-12">
              <table class="ttable mt-1 mb-3" cellpadding="0" cellspacing="0">
                <tbody>
                  <tr>
                    <th class="text-center" width="15%"><?php echo language(134);?></th>
                    <th width="30%"><?php echo $s_name;?></th>
                    <th class="text-center" width="12%"><?php echo language(111);?></th>
                    <th class="text-center" width="15%"><?php echo $ac_number;?></th>
                    <th class="text-center" width="13%"><?php echo language(137);?></th>
                    <th class="text-center" width="15%"><?php echo $balance;?></th>
                  </tr>
                </tbody>
              </table>
 <?php if(input_check('pu_item_import')==1){ ?>
            <form id="import_excel_form" class="mt-1" autocomplete="off" enctype="multipart/form-data">
              <div class="card-body">
                <div class="row">
                    <div class="col-md-4 pl-1 mt-1 text-right">
                    <button type="button"  class="btn btn-danger  btn-sm mt-3">
                    <a href="../customer/account_upload.csv" class="text-white" download="Purchase_sample_file"> <i class="fa fa-download"></i> Sample Download</a> </button>
                 </div> 
                 <div class="col-md-3">
                    <div class="form-group">
                       <label> Select your CSV File</label>
                        <input type="file" name="import_excel"  class="form-control form-control-sm" required="true" style="height:29px;line-height:13px;">
                    </div>
                 </div> 
                 <div class="col-md-2 pl-0">
                    <button type="submit" id="save2" class="btn btn-success btn-sm mt-3"><i class="fa fa-upload"></i> Upload File</button>
                 </div>  
                 <div class="col-md-3"> 
                  <label id="message" style="padding:5px !important;"></label>
                </div> 
                 
                </div>            
              </div>
            </form>
<?php } ?>





             <?php if(input_check('item_scan_status')==1){ ?>
              <form id="pur_sub" action="#" method="post" autocomplete="off">
             <?php } ?>
                <div class="row">
                  <div class="col-md-3"></div>
                  <div class="col-md-6">
                    <div class="form-group">
                      <label> <?php echo language(138);?></label>
                      <input type="text" id="item_details" class="form-control form-control-sm inputselect" required="true" autofocus="true">
                    </div>
                  </div>
                  <div class="col-md-3"></div>
                  <div class="col-md-12">
                    <hr style="margin-top:-5px;margin-bottom: 7px;">
                  </div>
                </div>
            <?php if(input_check('item_scan_status')==1){ ?>
              </form>
            <?php } ?>
            </div>
            <form id="pur_ret_form" action="#" method="post" autocomplete="off">
              <input type="hidden" name="s_acid" value="<?php echo $s_acid;?>">
              <input type="hidden" name="pur_invoice" value="<?php echo $invoice;?>">
              <div class="col-md-12">
                <div class="row">
                  <div class="col-md-12">
                    <div class="card-body table-responsive cscroll" style="max-height: 450px;">
                      <table class="ttable" cellpadding="0" cellspacing="0">
                        <thead>
                          <tr class="sticky-top">
                            <th class="text-center" style="width:2%;"><?php echo language(15);?></th>
                            <th style="width:40%;"><?php echo language(77);?></th>
                            <th class="text-center" style="width:10%;"><?php echo language(139);?></th>
                            <th class="text-center <?php if(input_check('u_type') !==1){echo 'd-none';}?>" style="width:10%;"><?php echo language(98);?></th>
                            <th class="text-center" style="width:15%;"><?php echo language(81);?></th>
                            <th class="text-center" style="width:10%;"><?php echo language(88);?></th>
                            <th class="text-center" style="width:10%;"><?php echo language(141);?></th>
                            <th class="text-center" style="width:10%;"><?php echo language(140);?></th>
                            <th class="text-center" style="width:2%;">#</th>
                          </tr>
                        </thead>


<script>
$(document).ready(function(){
  $('#import_excel_form').on('submit', function(event){
    event.preventDefault();
    $.ajax({
        // it file working Need 
      url:"../purchase_retrun/item_import.php?invoice=<?php echo $invoice;?>", 
      method:"POST",
      data:new FormData(this),
      contentType:false,
      cache:false,
      processData:false,
      beforeSend:function(){
         $('#save2').prop('disabled', true);
   $('#save2').html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> '+'Loading...'); 
      },
      success:function(data)
      {
        $('#message').html(data);
        $('#import_excel_form')[0].reset();
        $('#save2').attr('disabled', false);
        $('#save2').html('Upload File');   
          pagecall('../purchase_retrun/order_list.php?invoice=<?php echo $invoice?>','#row_show<?php echo $invoice;?>',0);  

      }
    })
  });
});
</script>






                      <tbody id="row_show<?php echo $invoice;?>"></tbody>
                    </table>
                  </div>
                </div>
                <div class="col-md-12"><hr style="margin-top: 7px;margin-bottom: 7px;"></div>
                <div class="col-md-7">
                  <div class="form-row">
                    <div class="col-md-12 <?php if(input_check('narration')!==1){ echo 'd-none';}?>">
                      <div class="form-group">
                        <label><?php echo language(150);?></label>
                        <textarea name="details" class="form-control form-control-sm inputselect" rows="2" placeholder="Enter ..."></textarea>
                      </div>
                    </div>
                    <div class="col-md-12 <?php if(input_check('narration')!==1){ echo 'd-none';}?>"><hr style="margin-top: 7px;margin-bottom: 7px;"></div>
                    
                    <div class="col-md-6 <?php if(input_check('m_invoice')!==1){ echo 'd-none';}?>">
                      <div class="form-group">
                        <label><?php echo language(151);?></label>
                        <input type="text" name="m_invoice" class="form-control form-control-sm text-center inputselect">
                      </div>
                    </div>
                    <div class="col-md-6 <?php if(input_check('d_man')!==1){ echo 'd-none';}?>">
                      <div class="form-group">
                        <label><?php echo language(152);?></label>
                        <input type="text" name="d_man" class="form-control form-control-sm inputselect">
                      </div>
                    </div>
                    
                    <div class="col-md-4 <?php if(input_check('wharehouse')!==1){ echo 'd-none';}?>">
                      <div class="form-group">
                        <label><?php echo language(104);?></label>
                        <select name="wh_id" class="form-control select2" style="width: 100%;" required="true">
                          <?php
                          $swh = mysqli_fetch_assoc(mysqli_query($conn,"SELECT bwid,war_location FROM business_w_house WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND active=1 AND default_set='primary' LIMIT 1"));
                          ?>
                          <option value="<?php echo $swh['bwid'];?>"><?php echo $swh['war_location'];?></option>
                          <?php
                          $bw= mysqli_query($conn,"SELECT bwid,war_location FROM business_w_house WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND active=1 ORDER BY bwid ASC") or die(mysqli_error($conn));
                          while($wh= mysqli_fetch_assoc($bw)){
                          echo '<option value='.$wh["bwid"].'>'.$wh["war_location"].'</option>';
                          }?>
                        </select>
                      </div>
                    </div>
                    
                    <div class="col-md-4 <?php if(input_check('invoice_date')!==1){ echo 'd-none';}?>">
                      <div class="form-group">
                        <label><?php echo language(131);?></label>
                        <input type="text" name="tdate" class="form-control form-control-sm" onmouseover="lcdate(this)" style="text-align:center;" readonly="true" required="true" value="<?php echo $date;?>">
                      </div>
                    </div>
                    
                    <div class="col-md-2 <?php if(input_check('email')!==1){ echo 'd-none';}?>">
                      <div class="form-group mt-3">
                        <div class="custom-control custom-checkbox">
                          <input name="email" class="custom-control-input" type="checkbox" id="customCheckbox1" value="yes">
                          <label for="customCheckbox1" class="custom-control-label float-right"><?php echo language(155);?></label>
                        </div>
                      </div>
                    </div>
                    <div class="col-md-2 <?php if(input_check('sms')!==1){ echo 'd-none';}?>">
                      <div class="form-group mt-3">
                        <div class="custom-control custom-checkbox">
                          <input name="sms" class="custom-control-input" type="checkbox" id="customCheckbox2" value="yes">
                          <label for="customCheckbox2" class="custom-control-label float-right"><?php echo language(132);?></label>
                        </div>
                      </div>
                    </div>
                    <div class="col-md-12"><hr style="margin-top: 7px;margin-bottom: 7px;"></div>
                    
               
                    <div class="col-md-6 <?php if(input_check('d_account')!==1){ echo 'd-none';} ?>">
                      <div class="form-group">
                        <label><?php echo language(154);?></label>
                        <dt id="d_show" style="float: right;"></dt>
                        <input type="hidden" name="d_acid">
                        <select id="d_acid" onchange="balance(this.value,'#d_show')" class="form-control select2" style="width: 100%;" required="true">
                          <?php
                          $accat= mysqli_query($conn,"SELECT acid,ac_name,ac_alise,ac_number,balance FROM accounts LEFT JOIN ac_sub_g_category ON(accounts.gcatid=ac_sub_g_category.ac_g_catid) WHERE accounts.coid='$coid' AND bid='$bid' AND brid='$brid' AND active=1 AND g_type IN('dr-exp')") or die(mysqli_error($conn));
                          while($acat= mysqli_fetch_assoc($accat)){
                          echo '<option value='.$acat["acid"].','.$acat["balance"].'>'.$acat["ac_name"]."-".$acat["ac_number"].' '.'('.$acat["ac_alise"].')</option>';
                          }?>
                        </select>
                      </div>
                    </div>
                    
                  </div>
                </div>
                <div class="col-md-1"></div>
                <div class="col-md-4">
                  <input type="hidden" id="sm_totalc<?php echo $invoice;?>" name="sm_totalc">
                  <input type="hidden" id="count_zero_qty_row<?php echo $invoice;?>" name="count_zero_qty_row">

                  <table class="ttable" cellpadding="0" cellspacing="0">
                    <tbody>
                      <tr>
                        <td class="text-right w-50"><dt><?php echo language(88);?></dt></td>
                        <td class="w-50"><dt>
                          <input type="number" oninput="blcal();" step="0.001" name="discount" id="discount" class="form-control form-control-sm inputselect text-right" value="0" style="text-align:center;" required="true">
                        </dt></td>
                      </tr>
                      <tr>
                        <td class="text-right w-50"><dt><?php echo language(143);?></dt></td>
                        <td class="w-50"><dt>
                          <input type="number" id="sub_total" step="0.001" class="form-control form-control-sm inputselect text-right" value="0" style="text-align:center;" required="true" readonly="true">
                        </dt></td>
                      </tr>
                      <tr>
                        <td class="text-right w-50"><dt><?php echo language(144);?></dt></td>
                        <td class="w-50"><dt>
                          <input type="number" id="pre_due" step="0.001" class="form-control form-control-sm inputselect text-right" value="<?php echo $balance;?>" style="text-align:center;" required="true" readonly="true">
                        </dt></td>
                      </tr>
                      <tr>
                        <td class="text-right w-50"><dt><?php echo language(145);?></dt></td>
                        <td class="w-50"><dt>
                          <input type="number" id="grd_total" step="0.001" class="form-control form-control-sm inputselect text-right" value="0" style="text-align:center;" required="true" readonly="true">
                        </dt></td>
                      </tr>
                      <tr>
                        <td class="text-right w-50"><dt><?php echo language(146);?></dt></td>
                        <td class="w-50"><dt>
                          <input type="number" oninput="blcal();" step="0.001" name="payment" id="payment" class="form-control form-control-sm inputselect text-right" value="0" style="text-align:center;" required="true">
                        </dt></td>
                      </tr>
                      <tr>
                        <td class="text-right w-50"><dt><?php echo language(147);?></dt></td>
                        <td class="w-50"><dt>
                          <input type="number" id="cur_due" step="0.001" class="form-control form-control-sm inputselect text-right" value="0" style="text-align:center;" required="true" readonly="true">
                        </dt></td>
                      </tr>
                
                      <tr>
                        <th colspan="2">
                          <div class="form-group">
                            <label><?php echo language(149);?></label>
                            <dt id="m_show" style="float: right;"></dt>
                            <input type="hidden" name="m_acid">
                            <select id="m_acid" onchange="balance(this.value,'#m_show')" class="form-control select2" style="width: 100%;" required="true">
                              <?php
                              $accat= mysqli_query($conn,"SELECT acid,ac_name,ac_alise,ac_number,balance FROM accounts LEFT JOIN ac_sub_g_category ON(accounts.gcatid=ac_sub_g_category.ac_g_catid) WHERE accounts.coid='$coid' AND bid='$bid' AND brid='$brid' AND active=1 AND g_type IN('mcash','bank')") or die(mysqli_error($conn));
                              while($acat= mysqli_fetch_assoc($accat)){
                              echo '<option value='.$acat["acid"].','.$acat["balance"].'>'.$acat["ac_name"]."-".$acat["ac_number"].' '.'('.$acat["ac_alise"].')</option>';
                              }?>
                            </select>
                          </div>
                        </th>
                      </tr>
                    </tbody>
                  </table>
                </div>
              </div>
            </div>
            <div class="card-footer mt-1" align="center" style="padding: 8px 5px;">
              <button type="submit" id="save" class="btn btn-success"><i class="fas fa-save"></i> <?php echo language(23);?></button>
            </div>
          </form>
        </div>
      </div>
    </div>
    <div class="col-md-1"></div>
  </div>
</div>
</section>
<script>
  $("select").select2(); 
  pagecall('../purchase_retrun/order_list.php?invoice=<?php echo $invoice?>','#row_show<?php echo $invoice;?>',0);  
  function balance (value,sid){
      var vvvv = value.split(",");
      $(sid).next().val(Number(vvvv[0]));
      var bl  = Number(vvvv[1]);
      if (bl >=0) {
        $(sid).css("color","green");
        $(sid).text(bden.format(bl) + ' Dr');
      } else {
        $(sid).css("color","red");
        $(sid).text(bden.format(-bl) + ' Cr');
      }
  }
balance ($("#d_acid").val(),"#d_show");
balance ($("#m_acid").val(),"#m_show");
balance ($("#f_acid").val(),"#f_show");

function blcal(){
  let sm_totalc = Number($('[name=sm_totalc]').val());
  let discount  = Number($('#discount').val());
  var sub_total = Number(sm_totalc - discount);
  var pre_due   = Number($('#pre_due').val());
  var grd_total;
  if (pre_due>0) {
     grd_total = Number(sub_total - pre_due);
  } else if (pre_due<0){
     grd_total = Number(sub_total + (-pre_due));
  } else {
     grd_total = Number(sub_total);  
  }
  let payment   = Number($('#payment').val());
  let cur_due   = Number(grd_total-payment);

  $('#sub_total').val(sub_total);
  $('#grd_total').val(grd_total);
  $('#cur_due').val(cur_due);

}   
</script>


<script>
   $(document).ready(function() {
   $('#pur_ret_form').on('submit', function(e){
   e.preventDefault(); 
   if (Number($('[name=sm_totalc]').val()) >0 && Number($('[name=count_zero_qty_row]').val()) =='0' ) {
   $('#save').prop('disabled', true);
   $('#save').html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> '+'Loading...'); 
        $.post("../purchase_retrun/purchase_Ret_db.php",$(this).serialize(),function(data){
        toastr.success(Successfully, yourdata);
    <?php if ($printer_t=='P') { ?>
        $("#modal-sm").modal({backdrop: "static"});
         pagecall('../purchase_retrun/purchase_invoice_pos.php?invoice=<?php echo $invoice;?>','#smdview',0);
    <?php } else { ?>
        $("#modal-lg").modal({backdrop: "static"});
        pagecall('../purchase_retrun/purchase_Ret_invoice.php?invoice=<?php echo $invoice;?>','#mdview',0);
    <?php } ?>
         $.post("../purchase_retrun/pur_retrun_index.php",{s_acid : '<?php echo $s_acid?>'},function(data){ $('#purmview').html(data); });
      });
      } else {
        alert('Please Select Item...??');
      }
     });
   });  
</script>

<script>
  $(function() {
    $( "#item_details" ).autocomplete({
        minLength: 2,
       source: '../purchase_retrun/select_item.php',
       autoFocus: true,
       delay:500,
   //  scroll: true,
       // focus: function( event, ui ) {
       //    $( "#item_details" ).val( ui.item.label );
       //       return false;
       // },
       select: function( event, ui ) {
          $( "#item_details" ).val( ui.item.label );
          $.post("../purchase_retrun/order_list_db.php",
            {item_id : ui.item.value,oinvoice:'<?php echo $invoice;?>'},
         function(data){
          toastr.success(Successfully, yourdata);
          pagecall('../purchase_retrun/order_list.php?invoice=<?php echo $invoice?>','#row_show<?php echo $invoice;?>',0);
        });  
        $( "#item_details" ).val('');        
          return false;
       }
    });
  });
</script>

<!-- for scan  -->
<script>
   $(document).ready(function() {
   $('#pur_sub').on('submit', function(e){
   e.preventDefault();
      let item_code = $('#item_details').val();
        $.post("../purchase_retrun/order_list_db.php",{item_code : item_code,sinvoice:'<?php echo $invoice;?>'},function(data){
          toastr.success(Successfully, yourdata);
          pagecall('../purchase_retrun/order_list.php?invoice=<?php echo $invoice?>','#row_show<?php echo $invoice;?>',0);
          $("#pur_sub")[0].reset();
          $("#item_details").val('');
          return false;
        });
      });
   });  
</script>

