New Proposals

  • DB

  • BOQ (Quantity entry screen)



    UI

    
    
    
    
                          <table class="table table-bordered" id="boq_container" style="padding:0px; margin:0px">
                          <?php  
    
                                          if(isset($categories) && count($categories)>0){
                                                foreach($categories as $cIndex=>$cat){
                                                    ?>
    <tr>
                                              <td style="width:1%" valign="top">{{$cIndex+1}}</td>
                                              <td style="width:1%" valign="top">
                                              <?php  if($block_editing!=1) {?> 
                                              <a href="javascript:void(0)"  class="btn_delete" style="display:block;">x</a>
                                              <?php  }?>
                                            </td>
                                              <td style="width:97%">
                                              <input type="hidden" class="boq_category_id" name="categories[{{$cIndex}}][boq_category_id]" value="{{$cat->id}}">
                                              <select class="form-control category_id" name="categories[{{$cIndex}}][category_id]" {{$block_editing!=1 ? '' : 'disabled'}}>
                                                  <option value="">Select</option>
                                                  <?php  if(isset($boqCategory)){
                                                    foreach($boqCategory as $row){?>
                                                          <option value="{{$row->id}}" {{isset($cat) && $cat->category_id==$row->id ? 'selected' : ''}}>{{$row->name}}</option>
                                                          <?php  
                                                    }
                                                  }
                                                  ?>
                                              </select>
                                              <br>
                                              
    
                                                  <table class="table table-bordered item_container" style="padding:0px;margin:0px;">
                                                      <tr>
                                                        <th>SI</th>
                                                        <th></th>
                                                        <th style="width:70%">Item</th>
                                                        <th>Quantity</th>
                                                        <th>Unit</th>
                                                </tr>
                                                <?php 
                                                      
                                                      $boq_items=LeadBoqItemRepository::getAll(['category_id'=>$cat->id]);
                                                      if(isset($boq_items) && count($boq_items)>0){
                                                            foreach($boq_items as $iIndex=>$item){
                                                                ?>
                                                               
                                                             <tr>
                                                                <td>{{$cIndex+1}}.{{$iIndex+1}}</td>
                                                                <td>
                                                                <?php  if($block_editing!=1) {?> 
                                                                <a href="javascript:void(0)"  class="btn_delete" style="display:block;">x</a>
                                                                <?php  }?>   
                                                                </td>
                                                                <td>
                                                                <div style="position:relative"><span class="clear_product">X</span> </div>
                                                                <input type="text" name="categories[{{$cIndex}}][items][{{$iIndex}}][title]" value="{{$item->product_title}}" class="form-control autocomplete" {{$block_editing!=1 ? '' : 'disabled'}}>
                                                                <input type="hidden" class="boq_item_id" name="categories[{{$cIndex}}][items][{{$iIndex}}][boq_item_id]" value="{{$item->id}}">
                                                                <input type="hidden" class="sku_id" name="categories[{{$cIndex}}][items][{{$iIndex}}][sku_id]" value="{{$item->sku_id}}">
                                                                <div class="errormessage"></div>
                                                                <textarea name="categories[{{$cIndex}}][items][{{$iIndex}}][description]" class="form-control item_description" {{$block_editing!=1 ? '' : 'disabled'}}>{{$item->description}}</textarea>
                                                                </td>
                                                                <td><input type="text" name="categories[{{$cIndex}}][items][{{$iIndex}}][quantity]" value="{{$item->quantity}}" class="form-control item_quantity" {{$block_editing!=1 ? '' : 'disabled'}}><div class="errormessage">w</div></td>
                                                                <td><input type="text" name="categories[{{$cIndex}}][items][{{$iIndex}}][unit]" value="{{$item->unit}}" class="form-control item_unit" {{$block_editing!=1 ? '' : 'disabled'}} readonly ></td>
                                                            </tr> 
                                                                                                                
                                                            <?php 
                                                            }
                                                      }
                                                      else{  
                                                      ?>
                                                      <tr>
                                                        <td>1.1</td>
                                                        <td>
                                                        <?php  if($block_editing!=1) {?> 
                                                            <a href="javascript:void(0)"  class="btn_delete" style="display:block;">x</a>
                                                            <?php  } ?>
                                                        </td>
                                                        <td>
                                                        <div style="position:relative"><span class="clear_product">X</span> </div>
                                                          <input type="text" name="categories[0][items][0][title]" class="form-control autocomplete">
                                                         
                                                          <input type="hidden" class="sku_id" name="categories[0][items][0][sku_id]">
                                                          <div class="errormessage"></div>
                                                          <textarea name="categories[0][items][0][description]" class="form-control item_description"></textarea>
                                                        </td>
                                                        <td><input type="text" name="categories[0][items][0][quantity]" class="form-control item_quantity"><div class="errormessage">w</div></td>
                                                        <td><input type="text" name="categories[0][items][0][unit]" class="form-control item_unit" readonly></td>
                                                      </tr>
                                                      <?php  } ?>
    
    
                                                   </table> 
                                                   <?php  if($block_editing!=1) {?> 
                                                    <a href="javascript:void(0)" style="padding:0px 5px;margin:0px;" class="btn btn-basic btn-sm btn_new_items" data-cat_count="{{isset($cIndex) ? $cIndex : 0}}" data-item_count="{{isset($iIndex) ? $iIndex : 0 }}"><i class="fa fa-plus"></i>&nbsp;Add Line Items</a>
                                                    <?php  } ?>
                                              </td>
                                          </tr>
    
                                          <?php 
                                                }
                                          }
                                          else{?>  
                                          <tr>
                                              <td style="width:1%" valign="top">1</td>
                                              <td style="width:1%" valign="top">
                                              <?php  if($block_editing!=1) {?> 
                                              <a href="javascript:void(0)"  class="btn_delete" style="display:block;">x</a>
                                              <?php  } ?>
                                            </td>
                                              <td style="width:97%">
                                              <select class="form-control category_id" name="categories[0][category_id]" >
                                                  <option value="">Select</option>
                                                  <?php  if(isset($boqCategory)){
                                                    foreach($boqCategory as $row){?>
                                                          <option value="{{$row->id}}" >{{$row->name}}</option>
                                                    <?php  
                                                    }
                                                  }
                                                  ?>
                                              </select>
                                              <br>
                                              
    
                                                  <table class="table table-bordered item_container" style="padding:0px;margin:0px;">
                                                      <tr>
                                                        <th>SI</th>
                                                        <th></th>
                                                        <th style="width:70%">Item</th>
                                                        <th>Quantity</th>
                                                        <th>Unit</th>
                                                </tr>
    
                                                      <tr>
                                                        <td>1.1</td>
                                                        <td>
                                                        <?php  if($block_editing!=1) {?> 
                                                            <a href="javascript:void(0)"  class="btn_delete" style="display:block;">x</a>
                                                            <?php  }?>
                                                        </td>
                                                        <td>
                                                        <?php  if($block_editing!=1) {?> 
                                                        <div style="position:relative"><span class="clear_product">X</span> </div>
                                                        <?php  }?>
                                                          <input type="text" name="categories[0][items][0][title]" class="form-control autocomplete">
                                                         
                                                          <input type="hidden" class="sku_id" name="categories[0][items][0][sku_id]">
                                                          <div class="errormessage"></div>
                                                          <textarea name="categories[0][items][0][description]" class="form-control item_description"></textarea>
                                                        </td>
                                                        <td><input type="text" name="categories[0][items][0][quantity]" class="form-control item_quantity"></td>
                                                        <td><input type="text" name="categories[0][items][0][unit]" class="form-control item_unit" readonly></td>
                                                    </tr>
    
    
                                                   </table> 
                                                   <?php if($block_editing!=1) {?> 
                                                    <a href="javascript:void(0)" style="padding:0px 5px;margin:0px;" class="btn btn-basic btn-sm btn_new_items" data-cat_count="0" data-item_count="0"><i class="fa fa-plus"></i>&nbsp;Add Line Items</a>
                                                    <?php } ?>
                                              </td>
                                          </tr>
                                          <?php  } ?>
                                      </table>
                                      
                                      <?php  if($block_editing!=1) {?> 
                                        <a href="javascript:void(0)" style="padding:0px 5px;margin:0px;" class="btn btn-primary btn-sm btn_new_category"><i class="fa fa-plus"></i>&nbsp;Add Line Category</a>
                                        <?php  } ?>
    
                                      

    Script

    
    
                                     
                                      var cat_count='0';    
                                      var item_count=0;
                                      var PRODUCT_SEARCH="";
                                      $('body').on("keyup", ".autocomplete" , function() {
                                          var length=$(this).val().length;
                                          target=$(this);
                                          $(this).parent().parent().find('.clear_product').hide();
                                          if(length>0){
                                              $(this).parent().parent().find('.clear_product').show();
                                          } 
                                          else{
                                                  
                                                  target.parent().parent().find('.sku_id').val('');            
                                                  target.parent().parent().find('.item_description').val('');
                                                  target.parent().parent().find('.item_quantity').val('');
                                                  target.parent().parent().find('.item_unit').val('');
                                                  target.parent().parent().find('.item_total').val(0);  
                                                  updateGrandTotal(); 
                                          }  
                                      });
    
    
    
    
                                      
    $('body').on('focus', '.autocomplete', function (e) {
        target=$(this);
        $(this).autocomplete({
               source: function( request, response ) { 
                                 
                   $.ajax({
                           url: PRODUCT_SEARCH,
                           type: 'get',
                           dataType: "json",
                           data: {
                               search: request.term
                           },
                           headers: {
                                  
                           },
                           success: function( data ) {
                               numItems=Object.keys(data).length;
                               switch(numItems){
                                    case 0:
                                        target.val('');                                  
                                        
                                        target.parent().parent().find('.sku_id').val('');                                    
                                        target.parent().parent().find('.item_description').val('');
                                        target.parent().parent().find('.item_quantity').val('');
                                        target.parent().parent().find('.item_unit').val('');
                                        target.parent().parent().find('.item_total').val(0);
                                        updateGrandTotal();
    
                                        target.focus();
                                    break;
                                    case 1:
                                       // target.val(data[0].label);  
    
                                        target.parent().parent().find('.sku_id').val(data[0].sku_id);                                    
                                        target.parent().parent().find('.item_description').val(data[0].description);
                                        target.parent().parent().find('.item_unit').val(data[0].unit);
                                        target.parent().parent().find('.item_quantity').val('');
                                       // target.parent().parent().find('.item_quantity').focus();
                                    break;
                                    default:
                                        response( data );
                                      
    
                                    break;    
    
                               }
                               
                           }
                   });
               },
               
               select: function (event, ui) {
                      
                   
                   target.val(ui.item.label); // display the selected text               
                   
                   target.parent().parent().find('.sku_id').val(ui.item.sku_id);
                   
                   target.parent().parent().find('.item_description').val(ui.item.description);
                   target.parent().parent().find('.item_unit').val(ui.item.unit);
                   target.parent().parent().find('.item_quantity').val('');
                  // target.parent().parent().find('.item_quantity').focus();
    
                   var keyCode = event.keyCode || event.which;
                   if (keyCode == 9) {                    
                        event.preventDefault(); // stops its action                    
                   }              
                   
                   return false;
    
               },
               focus: function(event, ui){},
               open: function() {},
               close: function(event, ui) {
                    
               },
           });
    });
    
    
    
    
    
    
    
    
    
    
    
    
    $('body').on("click", ".clear_product" , function() {
        $(this).parent().parent().find('.autocomplete').val('');
        target=$(this);    
        target.parent().parent().parent().find('.sku_id').val('');    
        target.parent().parent().parent().find('.item_description').val('');
        target.parent().parent().parent().find('.item_quantity').val('');
        target.parent().parent().parent().find('.item_unit').val('');
        target.parent().parent().parent().find('.item_total').val(0);    
        $(this).hide();
    
        
    });
    
    $('body').on("click", ".btn_delete" , function() {
        $(this).parent().parent().remove();
        
    });
    
    
    
     $('body').on("click", ".btn_new_category" , function() {
          
          //cat_count++; 
          cat_count=$('#boq_container > tbody > tr').length;
          item_count=0; 
          var str='';
    
          
         
          str+='<tr>';
          str+='<td style="width:1%" valign="top">'+cat_count+'</td>';
          str+='<td style="width:1%" valign="top"><a href="javascript:void(0)"  class="btn_delete" style="display:block;">x</a></td>';
          str+='<td style="width:97%">';
          str+='<select class="form-control category_id" name="categories['+cat_count+'][category_id]" >';
          str+='<option value="">Select</option>';
                              str+='</select>';
          str+='<br>';
         
    
          str+='<table class="table table-bordered item_container" style="padding:0px;margin:0px">';
          str+='<tr>';
          str+='<th>SI</th>';
          str+='<th><a href="javascript:void(0)"  class="btn_delete" style="display:block;">x</a></th>';
          str+='<th style="width:70%">Item</th>';
          str+='<th>Quantity</th>';
          str+='<th>Unit</th>';
          str+='</tr>';
    
          str+='<tr>';
          str+='<td>'+cat_count+'.1</td>';
          str+='<td></td>';
          str+='<td>';
          str+='<div style="position:relative"><span class="clear_product">X</span> </div>';        
          str+='<input type="text" class="form-control autocomplete" name="categories['+cat_count+'][items]['+item_count+'][title]"> ';
          str+='<input type="hidden" class="form-control sku_id" name="categories['+cat_count+'][items]['+item_count+'][sku_id]"><div class="errormessage"></div>';
          str+='<textarea name="categories['+cat_count+'][items]['+item_count+'][description]" class="form-control item_description"></textarea>';
          str+='</td>';
          str+='<td><input type="text" name="categories['+cat_count+'][items]['+item_count+'][quantity]" class="form-control item_quantity"><div class="errormessage">w</div></td>';
          str+='<td><input type="text" name="categories['+cat_count+'][items]['+item_count+'][unit]" class="form-control item_unit" readonly></td>';
          str+='</tr>';
    
    
          str+='</table>';
    
          str+='<a href="javascript:void(0)" style="padding:0px 5px; margin:0px" class="btn btn-basic btn-sm btn_new_items" data-cat_count="'+cat_count+'" data-item_count="'+item_count+'" ><i class="fa fa-plus"></i>&nbsp;Add Line Items</a>';
    
          str+='</td>';
          str+='</tr>';
          
                                      
        
          $('#boq_container').append(str);
     });
    
     $('body').on("click", ".btn_new_items" , function() {
          var i_cat_count=$(this).attr('data-cat_count');
          var i_item_count=$(this).attr('data-item_count');
    
          var i_item_count=$(this).parent().find('.item_container > tbody > tr').length-1;
          //alert(i_item_count);
          //i_item_count++;
          $(this).attr('data-item_count', i_item_count);
           
          var str='<tr>';
          str+='<td>'+(parseInt(i_cat_count)+1)+'.'+(parseInt(i_item_count)+1)+'</td>';
          str+='<td><a href="javascript:void(0)"  class="btn_delete" style="display:block;">x</a></td>';
          str+='<td>';
          str+='<div style="position:relative"><span class="clear_product">X</span> </div>'; 
          str+='<input type="text" class="form-control autocomplete" name="categories['+i_cat_count+'][items]['+i_item_count+'][title]"> '; 
          str+='<input type="hidden" class="form-control sku_id" name="categories['+i_cat_count+'][items]['+i_item_count+'][sku_id]"><div class="errormessage">w</div>'; 
          str+='<textarea name="categories['+i_cat_count+'][items]['+i_item_count+'][description]" class="form-control item_description"></textarea>';     
          str+='</td>';
          str+='<td><input type="text" name="categories['+i_cat_count+'][items]['+i_item_count+'][quantity]" class="form-control item_quantity"><div class="errormessage">w</div></td>';
          str+='<td><input type="text" name="categories['+i_cat_count+'][items]['+i_item_count+'][unit]" class="form-control item_unit" readonly></td>';
          str+='</tr>';
          $(this).prev('.item_container').append(str);
    
          
     });
    
    
     
    
    
    
    
    
    
    /* for items */
    $('body').on("click", ".btn_add_items" , function() {
        error=0;
        var i_item_count=$(this).parent().find('.item_container > tbody > tr').length;
        //alert(i_item_count);
        var date=$(this).parent().parent().find('.date').val(); 
        var description=$(this).parent().parent().find('.description').val(); 
        var category_id=$(this).parent().parent().find('.category_id').val();
        var amount=$(this).parent().parent().find('.amount').val();  
    
        if(date=='' || description=='' || category_id=='' || amount==''){        
                error=1;
                Swal.fire({
                    icon: 'warning',
                    title: "Please enter mandatory data.",
                    
                });
        }
    
    
        if(error==0){
                
                //$(this).hide();
                $(this).parent().find('.btn_delete').show();
                
                var str='';            
                str+='<tr >';                            
                str+='<td><a href="javascript:void(0)"  class="btn_delete" style="display:block">x</a></td>';
                str+=' <td ><input type="text" class="form-control description" name="files['+i_item_count+'][title]"  style="text-align:left" ></td>';
                
                str+='<td ><input type="hidden" class="file_name"  name="files['+i_item_count+'][file_name]" >';
                str+='<div class="image_preview_panel" style="width:100px;display:none ">';                                                                       
                                                                           
                str+='</div>';
                str+='<div class="image_browse_panel" >';
                str+='<input type="file" class="form-control file"  value=""  style="text-align:left">';                                                        
                str+='</div>';
    
                str+='</td>';
    
                //str+='<td ><a href="javascript:void(0)"  class="btn_add btn_add_items">+</a><a href="javascript:void(0)"  class="btn_delete" style="display:none">x</a></td>';
                str+='</tr>';
    
                //$(this).parent().parent().parent().parent('.item_container').append(str);
    
                $(this).prev('.item_container').append(str);
    
                //$('.item_container').append(str);
                //$('.autocomplete').focus();
        } 
        
        return false;
    });
    
    
    
    
    
    
                                      

    in controller

    
    
    
                                      $leadBoq=LeadBoq::where('lead_id',$input['lead_id'])->first();
                                      if(isset($leadBoq)){
                                          $leadBoq->comment=$input['comment'];
                                          $leadBoq->save();
                                          $leadBoqId=$leadBoq->id;
                                          $message="Successfully updated";
                                      }
                                      else{
    
                                          $leadBoq=LeadBoq::create([
                                              'lead_id'=>$input['lead_id'],
                                              'created_by' =>$user->master_id,
                                              'comment' =>$input['comment']
                                          ]);
                                          $leadBoqId=$leadBoq->id;
    
                                      }
    
    
    
                                      if(isset($input['categories'])){
                                          foreach($input['categories'] as $key=>$val){  
                                            
                                              if(isset($val['category_id']) && $val['category_id']>0){    
                                                                    
                                                            $val['company_id']=$user->company_id;
                                                            $val['lead_id']=$input['lead_id'];
                                                            $val['boq_id']=$leadBoqId;
                                                            if(isset($val['boq_category_id'])){
                                                                $val['id']=$val['boq_category_id'];
                                                            }
                                                            $categories=LeadBoqCategory::create($val);
                                                            //echo count($val['items']);
                                                          
                                                            if(isset($val['items'])){
                                                                    foreach($val['items'] as $key1=>$val1){
                                                                        if(isset($val1['sku_id']) && $val1['sku_id']>0){
                                                                            if(isset($val1['boq_item_id'])){
                                                                                $val1['id']=$val1['boq_item_id'];
                                                                            }
                                                                            $val1['boq_id']=$leadBoqId;
                                                                            $val1['category_id']=$categories->id;
                                                                            $items=LeadBoqItem::create($val1);
                                                                        }                                         
    
                                                                        //print_r($items);
                                                                      // echo '<br>';
    
                                                                    }
                                                            }
                                                }            
                                          }
                                      }
    
    
    

    Search function

    
    
    public function item_search(Request $request){
    
    $input=$request->all();
    $parts=Item::select('id as item_id','item_name as label' )
                ->where('status','1')
                ->where('item_name','LIKE',"%{$input['search']}%")
                ->get();
    
    return response()->json(
        $parts
     , 200);
    
    }
    
    

    DB

    
    CREATE TABLE `lead_boqs` (
      `id` bigint(20) UNSIGNED NOT NULL, 
      `lead_id` bigint(20) DEFAULT NULL,
      `comment` text DEFAULT NULL, 
      `status` enum('1','0') DEFAULT '1',
      `created_at` timestamp NULL DEFAULT NULL,  
      `created_by` bigint(20) DEFAULT NULL
     
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    
    
    CREATE TABLE `lead_categories` (
      `id` bigint(20) UNSIGNED NOT NULL,  
      `parent_id` int(11) DEFAULT NULL,
      `name` varchar(255) NOT NULL,
      `status` enum('1','0') DEFAULT '1',
     
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    
    
    
    
    CREATE TABLE `lead_boq_categories` (
      `id` bigint(20) UNSIGNED NOT NULL, 
      `boq_id` bigint(20) DEFAULT NULL,
      `category_id` int(11) DEFAULT NULL,
      `status` enum('1','0') DEFAULT '1',
      `created_at` timestamp NULL DEFAULT NULL,
      `created_by` bigint(20) DEFAULT NULL
     
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    
    
    
    
    CREATE TABLE `lead_boq_items` (
      `id` bigint(20) UNSIGNED NOT NULL, 
      `boq_category_id` bigint(20) DEFAULT NULL,
      `sku_id` bigint(20) DEFAULT NULL,
      `title` varchar(250) DEFAULT NULL,
      `description` text DEFAULT NULL,
      `quantity` decimal(20,2) DEFAULT NULL,
      `unit` varchar(50) DEFAULT NULL,
      `status` enum('1','0') DEFAULT '1', 
      `created_at` timestamp NULL DEFAULT NULL, 
      `created_by` bigint(20) DEFAULT NULL,
     
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    
    
    
  • Proposal

    Proposal List

    Proposal price entry screen

    UI

    
    
    <table class="table table-bordered" id="boq_container">
                                          <?php                                       $sub_total_g=0;
                                          $sub_total=0;
                                          $tax_total_g=0;
                                          $tax_total=0;
                                          $round_off_g=0;
                                          $round_off=0;
                                          $grand_total_g=0;
                                          $grand_total=0;
    
                                          if(isset($categories)){
                                                foreach($categories as $cIndex=>$cat){
                                                    ?>
    <tr>
                                              <td style="width:1%" valign="top">{{$cIndex+1}}</td>
                                              
                                              <td style="width:97%">
                                              <input type="text" class="form-control" readonly value="{{$cat->category_name}}">
                                              <input type="hidden" name="categories[{{$cIndex}}][category_id]" class="form-control"  value="{{$cat->category_id}}">
                                              <br>
                                              
    
                                                  <table class="table table-bordered ">
                                                      <tr>
                                                        <th>SI</th>                                                    
                                                        <th style="width:40%">Item</th>
                                                        <th>Quantity</th>
                                                        <th>Unit</th>
                                                        <th>Getting Price</th>
                                                        <th>Vendor</th>
                                                        <th>Tax Rate</th>
                                                        <th>Excl. Tax</th>
                                                        <th>Margin %</th>
                                                        <th>Selling Price</th>
                                                        <th>Excl Tax</th>
                                                     </tr>
                                                      <?php  
                                                      if($view_mode==0){
                                                        $items=LeadBoqItemRepository::getAll(['category_id'=>$cat->id]);
                                                      }
                                                      else{
                                                        $items=LeadProposalItemRepository::getAll(['proposal_category_id'=>$cat->id]);
                                                      }  
                                                      if(isset($items)){
                                                            $gp_excluding_tax=0;
                                                            foreach($items as $iIndex=>$item){
    
                                                                if($view_mode==0){
                                                                    $items_id=$item->id;
                                                                    $proposalItem=LeadProposalItem::join('lead_proposal_categories','lead_proposal_categories.id','lead_proposal_items.proposal_category_id')                                                                    
                                                                        ->join("lead_proposals",function($join) use ($items_id){
                                                                             $join->on("lead_proposals.id","=","lead_proposal_categories.proposal_id")
                                                                                ->on("lead_proposal_items.boq_items_id","=",DB::raw($items_id));
                                                                        })
                                                                        ->where('lead_proposals.id', $proposal_id)
                                                                        ->where('lead_proposal_categories.category_id', $cat->category_id)
                                                                        ->where('lead_proposal_items.sku_id', $item->sku_id)
                                                                        
                                                                        ->first();
    
                                                                        $item->vendor_id=isset($proposalItem->vendor_id) ? $proposalItem->vendor_id : ''; 
    
                                                                        //getting section
                                                                        $item->g_price=isset($proposalItem->g_price) ? $proposalItem->g_price : '';  
                                                                        
                                                                        if($item->g_price>0){
                                                                            $item->gp_excluding_tax=$item->g_price*$item->quantity;
                                                                            $item->gp_tax_amount=0;
                                                                            if($item->tax_rate>0){
                                                                                $item->gp_tax_amount=$item->gp_excluding_tax*($item->tax_rate)/100;
                                                                            }                                                                    
                                                                            $item->gp_including_tax=$item->gp_excluding_tax+$item->gp_tax_amount;
                                                                            
                                                                            //selling section
                                                                            $item->margin_rate=isset($proposalItem->margin_rate) ? $proposalItem->margin_rate : ''; 
                                                                            $item->s_price=$item->g_price; 
                                                                            if($item->margin_rate>0){
                                                                                $item->s_price=$item->g_price+($item->g_price*$item->margin_rate/100);
                                                                            }
                                                                            $item->sp_excluding_tax=$item->s_price*$item->quantity;
                                                                            $item->sp_tax_amount=0; 
                                                                            if($item->tax_rate>0){
                                                                                $item->sp_tax_amount=$item->sp_excluding_tax*$item->tax_rate/100;
                                                                            }                                                                    
                                                                            $item->sp_including_tax=$item->sp_excluding_tax+$item->sp_tax_amount;
                                                                        }    
    
                                                                        //find total
                                                                        $sub_total_g+=$item->gp_excluding_tax;
                                                                        $sub_total+=$item->sp_excluding_tax;
    
                                                                        $tax_total_g+=$item->gp_tax_amount;
                                                                        $tax_total+=$item->sp_tax_amount;
                                                                        
    
                                                                }
                                                                ?>
                                                               
                                                             <tr>
                                                                <td>{{$cIndex+1}}.{{$iIndex+1}}</td>
                                                                
                                                                <td>                                                            
                                                                <input type="text" readonly name="categories[{{$cIndex}}][items][{{$iIndex}}][title]" value="{{$item->product_title}}" class="form-control autocomplete">
                                                                <input type="hidden" class="sku_id" name="categories[{{$cIndex}}][items][{{$iIndex}}][sku_id]" value="{{$item->sku_id}}">
                                                                <input type="hidden" class="boq_items_id" name="categories[{{$cIndex}}][items][{{$iIndex}}][boq_items_id]" value="{{$item->id}}">
                                                                <textarea readonly name="categories[{{$cIndex}}][items][{{$iIndex}}][description]" class="form-control item_description">{{$item->description}}</textarea>
                                                                </td>
                                                                <td><input type="text" name="categories[{{$cIndex}}][items][{{$iIndex}}][quantity]" value="{{$item->quantity}}" class="form-control quantity" readonly></td>
                                                                <td><input type="text" name="categories[{{$cIndex}}][items][{{$iIndex}}][unit]" value="{{$item->unit}}" class="form-control item_unit" readonly></td>
                                                                <td><input type="text" name="categories[{{$cIndex}}][items][{{$iIndex}}][g_price]" value="{{$item->g_price}}" class="form-control g_price" {{$view_mode!=1 ? '' : 'disabled'}}></td>
                                                                <td>
                                                                    <select class="form-control  vendor_id" data-message="Please enter vendor"  id="" name="categories[{{$cIndex}}][items][{{$iIndex}}][vendor_id]" {{$view_mode!=1 ? '' : 'disabled'}}>
                                                                        <option value="">Select Vendor</option>
                                                                        @if(isset($vendors))
                                                                        @foreach($vendors as $val)
                                                                                <option value="{{$val->id}}"  >{{$val->short_name}}</option>
                                                                                @endforeach
                                                                                @endif 
                                                                    </select>
                                                                    <div class="errormessage"></div>
                                                                </td>
                                                                <td><input type="text" name="categories[{{$cIndex}}][items][{{$iIndex}}][tax_rate]" value="{{$item->tax_rate}}" class="form-control tax_rate" readonly {{$view_mode!=1 ? '' : 'disabled'}}></td>
                                                                <td>
                                                                <input type="text" name="categories[{{$cIndex}}][items][{{$iIndex}}][gp_excluding_tax]" value="{{$item->gp_excluding_tax}}" class="form-control gp_excluding_tax" readonly {{$view_mode!=1 ? '' : 'disabled'}}>  
                                                                <input type="text" name="categories[{{$cIndex}}][items][{{$iIndex}}][gp_tax_amount]" value="{{$item->gp_tax_amount}}" class="form-control gp_tax_amount" readonly {{$view_mode!=1 ? '' : 'disabled'}}>  
                                                                <input type="text" name="categories[{{$cIndex}}][items][{{$iIndex}}][gp_including_tax]" value="{{$item->gp_including_tax}}" class="form-control gp_including_tax" readonly {{$view_mode!=1 ? '' : 'disabled'}}></td>
                                                                <td><input type="text" name="categories[{{$cIndex}}][items][{{$iIndex}}][margin_rate]" value="{{$item->margin_rate}}" class="form-control margin_rate" {{$view_mode!=1 ? '' : 'disabled'}} ></td>
                                                                <td><input type="text" name="categories[{{$cIndex}}][items][{{$iIndex}}][s_price]" value="{{$item->s_price}}" class="form-control s_price" readonly {{$view_mode!=1 ? '' : 'disabled'}}></td>
                                                                <td>
                                                                    <input type="text" name="categories[{{$cIndex}}][items][{{$iIndex}}][sp_excluding_tax]" value="{{$item->sp_excluding_tax}}" class="form-control sp_excluding_tax" readonly {{$view_mode!=1 ? '' : 'disabled'}}>
                                                                    <input type="text" name="categories[{{$cIndex}}][items][{{$iIndex}}][sp_tax_amount]" value="{{$item->sp_tax_amount}}" class="form-control sp_tax_amount" readonly {{$view_mode!=1 ? '' : 'disabled'}}>
                                                                    <input type="text" name="categories[{{$cIndex}}][items][{{$iIndex}}][sp_including_tax]" value="{{$item->sp_including_tax}}" class="form-control sp_including_tax" readonly {{$view_mode!=1 ? '' : 'disabled'}}>
                                                               
                                                            </td>
                                                            </tr> 
                                                                                                                
                                                             <?php                                                         }
                                                      }
                                                      
                                                      ?>
                                               
    
    
                                                   </table> 
                                                 
                                              
    
                                              </td>
                                          </tr>
    
                                           <?php                                             }
                                          }
    
                                          if($view_mode==0 && isset($proposalData)){
    
                                                $proposalData->sub_total_g=$sub_total_g;
                                                $proposalData->sub_total=$sub_total;
    
                                                $proposalData->tax_total_g=$tax_total_g;
                                                $proposalData->tax_total=$tax_total;
    
                                                $grand_total_g=$proposalData->sub_total_g+$proposalData->tax_total_g;
                                                $grand_total=$proposalData->sub_total+$proposalData->tax_total;
    
                                                $proposalData->grand_total_g=round($grand_total_g);
                                                $proposalData->grand_total=round($grand_total);
    
                                                $proposalData->round_off_g=$grand_total_g-$proposalData->grand_total_g;
                                                $proposalData->round_off=$grand_total-$proposalData->grand_total;
                                                
                                                $proposalData->margin_total=$proposalData->sub_total-$proposalData->sub_total_g;
    
    
                                          }
                                          ?>  
                                          
                                          
                                      </table>
    
                                        

    Script

    
    
                                        $('body').on("keyup", ".g_price , .margin_rate" , function() {
            var quantity=$(this).parent().parent().find('.quantity').val();
            var margin_rate=$(this).parent().parent().find('.margin_rate').val();
            var tax_rate=$(this).parent().parent().find('.tax_rate').val();
            var g_price=$(this).parent().parent().find('.g_price').val();
            var gp_excluding_tax=0;
            var gp_including_tax=0;
    
            
    
            // getting price
            if(g_price>0){
                gp_excluding_tax=parseFloat(quantity)*parseFloat(g_price);
                gp_including_tax=gp_excluding_tax;
            }
            $(this).parent().parent().find('.gp_excluding_tax').val(gp_excluding_tax);
    
    
            // tax  calculation
            let gp_tax_amount=0;          
            if(tax_rate>0){
                gp_tax_amount=(gp_excluding_tax)*parseFloat(tax_rate)/100; 
                gp_including_tax=gp_excluding_tax+gp_tax_amount;
            }
            $(this).parent().parent().find('.gp_tax_amount').val(gp_tax_amount);
            $(this).parent().parent().find('.gp_including_tax').val(gp_including_tax);
    
    
            //selling price
            let s_price=g_price;
            if(margin_rate>0){
                s_price=parseFloat(g_price)+(parseFloat(g_price)*parseFloat(margin_rate)/100); // need to show
            } 
            $(this).parent().parent().find('.s_price').val(s_price);
    
    
            
            let sp_including_tax=s_price;
            let sp_tax_amount=0;
            let sp_excluding_tax=parseFloat(quantity)*parseFloat(s_price);
            $(this).parent().parent().find('.sp_excluding_tax').val(sp_excluding_tax);
    
            //tax
            if(tax_rate>0){
                sp_tax_amount=(sp_excluding_tax)*parseFloat(tax_rate)/100;
                sp_including_tax=sp_excluding_tax+sp_tax_amount;
            }
            
            $(this).parent().parent().find('.sp_tax_amount').val(sp_tax_amount);
            $(this).parent().parent().find('.sp_including_tax').val(sp_including_tax);
    
            updateGrandTotal();
    
    
        });
    
       
    
        function updateGrandTotal(){
            
            var sub_total_g=0;
            var tax_total_g=0;
            var sub_total_incltax_g=0;
            var grand_total_g=0;
    
            var sub_total_s=0;
            var tax_total_s=0;
            var sub_total_incltax_s=0;
            var grand_total_s=0;
    
    
    
            var quantity=$('.quantity');
            var gp_excluding_tax=$('.gp_excluding_tax');
            var gp_tax_amount=$('.gp_tax_amount');
            var gp_including_tax=$('.gp_including_tax');
           
            var s_price=$('.s_price');
            var sp_excluding_tax=$('.sp_excluding_tax');
            var sp_tax_amount=$('.sp_tax_amount');
            var sp_including_tax=$('.sp_including_tax');
    
            $('.g_price').each(function(index, obj){
               //alert(quantity[index].value);
                if(obj.value>0){
    
                    //getting
                    if(gp_excluding_tax[index].value>0){
                        sub_total_g+=parseFloat(gp_excluding_tax[index].value); 
                    }     
    
                    if(gp_tax_amount[index].value>0){
                        tax_total_g+=parseFloat(gp_tax_amount[index].value);
                    }     
    
                    if(gp_including_tax[index].value>0){
                        sub_total_incltax_g+=parseFloat(gp_including_tax[index].value);
                    }    
    
    
                    //selling
                    if(sp_excluding_tax[index].value>0){
                        sub_total_s+=parseFloat(sp_excluding_tax[index].value);
                    }    
    
                    if(sp_tax_amount[index].value>0){
                        tax_total_s+=parseFloat(sp_tax_amount[index].value);
                    }    
    
                    if(sp_including_tax[index].value>0){
                        sub_total_incltax_s+=parseFloat(sp_including_tax[index].value);
                    }    
                }    
    
            });
    
    
    
          
    
           
            let round_off_amount_g=Math.round(sub_total_incltax_g);
            let round_off_diff_g=round_off_amount_g-sub_total_incltax_g;
    
                   
            let round_off_amount_s=Math.round(sub_total_incltax_s);
            let round_off_diff_s=round_off_amount_s-sub_total_incltax_s;
    
    
            $('.sub_total_g').val(sub_total_g.toFixed(2));
            $('.tax_total_g').val(tax_total_g.toFixed(2));
            $('.round_off_g').val(round_off_diff_g.toFixed(2));
            $('.grand_total_g').val(round_off_amount_g.toFixed(2));
    
    
            $('.sub_total').val(sub_total_s.toFixed(2));
            $('.tax_total').val(tax_total_s.toFixed(2));
            $('.round_off').val(round_off_diff_s.toFixed(2));
            $('.grand_total').val(round_off_amount_s.toFixed(2));
    
            let margin_total=sub_total_s-sub_total_g;
            $('.margin_total').val(margin_total.toFixed(2));
    
    
        }
    
      

    Controller

    
    
      $fYear=$this->_getFinancialYear(date("Y-m-d"), "Y");
    
    if(isset($input['version_id']) && $input['version_id']>0 && $input['version']!='n' ){
                $proposal=LeadProposal::find($input['version_id']);
                
                if(isset($proposal) && $proposal->proposal_master_id>0){
                    $proposal_master_id=$proposal->proposal_master_id;
                }
                else{
                    $proposalMaster=LeadProposalMaster::create([
                        'company_id'=>$user->company_id,
                        'lead_id' =>$input['lead_id']
                    ]);
    
                    $proposal_master_id=$proposalMaster->id;
                }
    
                
                $input['proposal_master_id']=$proposal_master_id;
                $proposal_id=$input['version_id']; 
    
                if($input['version']!='r'){
    
                    $version=LeadProposal::where('proposal_master_id',$input['proposal_master_id'])->count(); 
                    if($version>1){
                        $version=$fYear.' - Rev-'.($version-1); 
                    }
                    else{
                        $version=$fYear;
                    }
    
                    $proposal->mode=$input['mode'];
                    $proposal->status=$status;
                    $proposal->sub_total=$input['sub_total'];
                    $proposal->tax_total=$input['tax_total'];
                    $proposal->grand_total=$input['grand_total'];
                    $proposal->round_off=$input['round_off'];
                    $proposal->sub_total_g=$input['sub_total_g'];
                    $proposal->tax_total_g=$input['tax_total_g'];
                    $proposal->grand_total_g=$input['grand_total_g'];
                    $proposal->round_off_g=$input['round_off_g'];
                    $proposal->margin_total=$input['margin_total'];
    
                    $proposal->version=$version;
                    $proposal->valid_till=$input['valid_till'];
                    $proposal->bank_id=$input['bank_id'];
                    $proposal->save();
                }
                else{
                    $version=LeadProposal::where('proposal_master_id',$input['proposal_master_id'])->count(); 
                    if($version>0){
                        $version=$fYear.' - Rev-'.($version); 
                    }
                    else{
                        $version=$fYear;
                    }
                }
           
    
    }
    else{
    
        $proposalMaster=LeadProposalMaster::create([
            'company_id'=>$user->company_id,
            'lead_id' =>$input['lead_id']
        ]);
    
        $version=$fYear;
        $proposal_master_id=$proposalMaster->id;    
       
    }
    
    
    if($input['version']!='u'){
    
        $proposal=LeadProposal::create([
            'lead_id' =>$input['lead_id'],
            'proposal_master_id' =>$proposal_master_id,
            'company_id' =>$user->company_id,
            'status' =>$status,
            'mode' =>$input['mode'],
            'sub_total'=>$input['sub_total'],
            'tax_total'=>$input['tax_total'],
            'grand_total'=>$input['grand_total'],
            'round_off'=>$input['round_off'],
    
            'sub_total_g'=>$input['sub_total_g'],
            'tax_total_g'=>$input['tax_total_g'],
            'grand_total_g'=>$input['grand_total_g'],
            'round_off_g'=>$input['round_off_g'],
            'margin_total'=>$input['margin_total'],
    
            'created_by'=>$user->master_id,
            'version' =>$version,
            'valid_till'=>isset($input['valid_till']) ? date('Y-m-d') : '',
            'bank_id'=>$input['bank_id']
        ]);
    
        $proposal_id=$proposal->id;
    }
    
    
    
    LeadProposalCategory::where('proposal_id',$proposal_id)->delete();
           LeadProposalItem::where('proposal_id',$proposal_id)->delete();
           if(isset($input['categories'])){
               foreach($input['categories'] as $val){
                   $val['proposal_id']=$proposal_id;
                   $categories=LeadProposalCategory::create($val);
    
                   if($input['mode']=='confirm'){
                        $val['proposal_history_id']=$proposalHistory->id;
                        $leadProCatHistory=LeadProposalCatHistory::create($val);
                   }
                   if($val['items']){
                        
                        foreach($val['items'] as $val1){
                            $val1['proposal_category_id']=$categories->id;
                            if(isset($val1['g_price']) && $val1['g_price']>0 ){
                                $items=LeadProposalItem::create($val1);
    
                                if($input['mode']=='confirm'){
                                    $val1['proposal_category_id']=$leadProCatHistory->id;
                                    LeadProposalItemHistory::create($val1);
                                }
                            }
                        }
                   }
               }
           }
    
          

    Populate form data

    
            if($view_mode==0){
                $this->data['categories']=LeadBoqCategoryRepository::getAll(['lead_id' => $input['lead_id']]);
            }
            else{   
                $this->data['categories']=LeadProposalCategoryRepository::getAll(['lead_id' => $input['lead_id'],'proposal_id' =>$input['id']]);
            }
          

    DB

    
    
          CREATE TABLE `lead_proposal_masters` (
      `id` bigint(20) UNSIGNED NOT NULL,
    
      `lead_id` bigint(20) DEFAULT NULL,
      `status` char(25) DEFAULT '1',
      `priority` int(11) DEFAULT NULL,
      `deleted_at` timestamp NULL DEFAULT NULL,
      `created_at` timestamp NULL DEFAULT NULL,
      `updated_at` timestamp NULL DEFAULT NULL,
      `created_by` bigint(20) DEFAULT NULL,
      `updated_by` bigint(20) DEFAULT NULL,
      `deleted_by` bigint(20) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    
    
    
    
    
    
    CREATE TABLE `lead_proposals` (
      `id` bigint(20) UNSIGNED NOT NULL,
      `company_id` int(11) DEFAULT NULL,
      `lead_id` bigint(20) DEFAULT NULL,
      `proposal_master_id` bigint(20) DEFAULT NULL,
      `version` varchar(500) NOT NULL,
      `status` char(25) DEFAULT '1',
      `mode` char(25) DEFAULT NULL,
      `sub_total` decimal(20,2) DEFAULT NULL,
      `tax_total` decimal(20,2) DEFAULT NULL,
      `round_off` decimal(20,2) DEFAULT NULL,
      `grand_total` decimal(20,2) DEFAULT NULL,
      `sub_total_g` decimal(20,2) DEFAULT NULL,
      `tax_total_g` decimal(20,2) DEFAULT NULL,
      `round_off_g` decimal(20,2) DEFAULT NULL,
      `grand_total_g` decimal(20,2) DEFAULT NULL,
      `margin_total` decimal(20,2) DEFAULT NULL,
      `comment` text DEFAULT NULL,
      `priority` int(11) DEFAULT NULL,
      `valid_till` date DEFAULT NULL,
      `bank_id` int(11) DEFAULT NULL,
      `deleted_at` timestamp NULL DEFAULT NULL,
      `created_at` timestamp NULL DEFAULT NULL,
      `updated_at` timestamp NULL DEFAULT NULL,
      `created_by` bigint(20) DEFAULT NULL,
      `updated_by` bigint(20) DEFAULT NULL,
      `deleted_by` bigint(20) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    
    
    
    
    
    CREATE TABLE `lead_proposal_categories` (
      `id` bigint(20) UNSIGNED NOT NULL,
      `proposal_id` bigint(20) DEFAULT NULL,
      `category_id` int(11) DEFAULT NULL,
      `status` enum('1','0') DEFAULT '1',
      `priority` int(11) DEFAULT NULL,
      `deleted_at` timestamp NULL DEFAULT NULL,
      `created_at` timestamp NULL DEFAULT NULL,
      `updated_at` timestamp NULL DEFAULT NULL,
      `created_by` bigint(20) DEFAULT NULL,
      `updated_by` bigint(20) DEFAULT NULL,
      `deleted_by` bigint(20) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    
    
    
    
    
    CREATE TABLE `lead_proposal_items` (
      `id` bigint(20) UNSIGNED NOT NULL,
      `proposal_id` int(11) DEFAULT NULL,
      `proposal_category_id` bigint(20) DEFAULT NULL,
      `boq_items_id` int(11) DEFAULT NULL,
      `sku_id` bigint(20) DEFAULT NULL,
      `title` varchar(250) DEFAULT NULL,
      `description` text DEFAULT NULL,
      `quantity` decimal(20,2) DEFAULT NULL,
      `unit` varchar(50) DEFAULT NULL,
      `g_price` decimal(20,2) DEFAULT NULL,
      `vendor_id` int(11) DEFAULT NULL,
      `tax_rate` decimal(5,2) DEFAULT NULL,
      `gp_excluding_tax` decimal(20,2) DEFAULT NULL,
      `gp_including_tax` decimal(20,2) DEFAULT NULL,
      `margin_rate` decimal(5,2) DEFAULT NULL,
      `s_price` decimal(20,2) DEFAULT NULL,
      `sp_tax_amount` decimal(20,2) DEFAULT NULL,
      `gp_tax_amount` decimal(20,2) DEFAULT NULL,
      `sp_excluding_tax` decimal(20,2) DEFAULT NULL,
      `sp_including_tax` decimal(20,2) DEFAULT NULL,
      `status` enum('1','0') DEFAULT '1',
      `priority` int(11) DEFAULT NULL,
      `deleted_at` timestamp NULL DEFAULT NULL,
      `created_at` timestamp NULL DEFAULT NULL,
      `updated_at` timestamp NULL DEFAULT NULL,
      `created_by` bigint(20) DEFAULT NULL,
      `updated_by` bigint(20) DEFAULT NULL,
      `deleted_by` bigint(20) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;