CSS
.clear_product {
display:none;
position: absolute;
border-radius: none;
left:96.5%;
z-index: 2;
border: none;
top:5px;
cursor: pointer;
color: white;
background-color: #f21505;
transform: translateX(2px);
padding:3px;
}
.table th, .table td{
padding:1px;
}
.form-control{
border-radius:0px;
}
.table td{
border:none;
}
.btn_add{
background-color: #166e23;
padding:1px 10px;
color: white;
position:relative;
top:-2px;
font-size:28px;
font-weight:bold;
}
.btn_delete{
background-color: #f21505;
padding: 0px 12px 0px 12px;
color: white;
position:relative;
top:-2px;
font-size:22px;
font-weight:bold;
}
View
Script
$('.datetimepicker').datetimepicker({
dayOfWeekStart : 0,
lang:DATE_TIME_PICKER_LANG,
format:'d-m-Y',
timepicker:false,
// datepicker:false,
// maxDate:DATE_TIME_PICKER_MINDATE,
allowTimes:DATE_TIME_PICKER_TIMES,
});
$('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('.fish_id').val('');
target.parent().parent().find('.quantity').val('');
target.parent().parent().find('.price').val('');
//target.parent().parent().find('.price').val('');
target.parent().parent().find('.item_total').val(0);
updateGrandTotal();
}
});
$('body').on("click", ".clear_product" , function() {
$(this).parent().parent().find('.autocomplete').val('');
target=$(this);
target.parent().parent().parent().find('.fish_id').val('');
target.parent().parent().parent().find('.quantity').val('');
target.parent().parent().parent().find('.price').val('');
// target.parent().parent().parent().find('.price').val('');
target.parent().parent().parent().find('.item_total').val(0);
$(this).hide();
updateGrandTotal();
});
$('body').on('focus', '.autocomplete', function (e) {
target=$(this);
$(this).autocomplete({
source: function( request, response ) {
$.ajax({
url: FISH_SEARCH,
type: 'get',
dataType: "json",
data: {
search: request.term
},
headers: {
// 'X-MAC-ID': $('#mac_id').val(),
// 'X-CLIENT-KEY' : $('#client_key').val(),
// 'Authorization' : 'Bearer '+token
},
success: function( data ) {
numItems=Object.keys(data).length;
switch(numItems){
case 0:
target.val('');
target.parent().parent().find('.fish_id').val('');
target.parent().parent().find('.quantity').val('');
target.parent().parent().find('.price').val('');
//target.parent().parent().find('.price').val('');
target.parent().parent().find('.item_total').val(0);
updateGrandTotal();
target.focus();
break;
case 1:
target.val(data[0].label);
target.parent().parent().find('.fish_id').val(data[0].id);
target.parent().parent().find('.quantity').val('');
target.parent().parent().find('.price').val('');
target.parent().parent().find('.item_total').focus();
break;
default:
response( data );
break;
}
}
});
},
select: function (event, ui) {
target.val(ui.item.label); // display the selected text
target.parent().parent().find('.fish_id').val(ui.item.id);
target.parent().parent().find('.quantity').val(ui.item.manufaure_name);
target.parent().parent().find('.price').val(ui.item.model);
target.parent().parent().find('.item_total').val(0);
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('blur', '.autocomplete', function (e) {
target=$(this);
if(target.parent().parent().find('.id').val()==''){
target.val('');
}
});
$('body').on("click", ".btn_new_items" , function() {
error=0;
if(error==0){
var str='';
str+='';
str+='x | ';
str+=' X | ';
str+=' | ';
str+=' | ';
str+=' | ';
str+=' | ';
str+='
';
$('.item_container').append(str);
//$(this).parent().find('.item_container').append(str);
$('.autocomplete').focus();
}
});
$('body').on("click", ".btn_delete" , function() {
$(this).parent().parent().remove();
updateGrandTotal();
});
$('body').on("keyup", ".quantity , .price" , function() {
var quantity=$(this).parent().parent().find('.quantity').val();
var price=$(this).parent().parent().find('.price').val();
var item_total=0;
if(quantity>0 && price>0){
item_total=parseFloat(quantity)*parseFloat(price);
}
$(this).parent().parent().find('.item_total').val(item_total);
updateGrandTotal();
});
$('body').on("keyup", "#discount_amount" , function() {
updateGrandTotal();
});
function updateGrandTotal(){
var sub_total=0;
var advance_amount=0;
var discount_amount=$('#discount_amount').val();
var grand_total=0;
$('.item_total').each(function(index, obj){
sub_total+=parseFloat(obj.value);
});
grand_total=sub_total;
if(discount_amount>0){
grand_total=grand_total-discount_amount;
}
$('#sub_total').val(sub_total);
$('#grand_total').val(grand_total);
}
Form data in controller
$responseData=[];
if(isset($input['id']) && $input['id']>0){
$this->data['formData']=PurchaseRepository::getOne($input['id']);
$this->data['itemsData']=PurchaseRepository::getItems($input['id']);
//role based
if(empty($this->data['permissions']['generate_invoice'])){
$this->data['block_editing']=1;
}
if($this->data['formData']->status=='despatched'){
unset($this->data['permissions']['invoice']);
unset($this->data['permissions']['despatch']);
$this->data['block_editing']=1;
}
}
else{
$this->data['formData']=new Purchase();
$this->data['formData']->date=date("Y-m-d");
}
Save function
$input['date']=isset($input['date']) ? date('Y-m-d' , strtotime($input['date'])) : '0000-00-00';
//$input['company_id']=$user->company_id;
if(isset($input['id']) && $input['id']>0){
PurchaseDetails::where('purchase_id', $input['id'])->delete();
$object = Purchase::find($input['id']);
$object->update($input);
$message="Successfully updated.";
$input['purchase_id']=$input['id'];
}
else{
$lastBooking=Purchase::orderBy('id','DESC')->first();
$year=date("Y");
if(isset($lastBooking) && $lastBooking->purchase_no!=''){
$bookingNo=explode("/",$lastBooking->purchase_no);
if($year==$bookingNo[1]){
$reg_no=$bookingNo[2]+1;
}
else{
$reg_no=10001;
}
$invoice_no='AJ/'.$year.'/'.$reg_no;
}
else{
$invoice_no='AJ/'.$year.'/10001';
}
$input['purchase_no'] =$invoice_no;
$input['created_by']=$user->id;
$object = Purchase::create($input);
$message="Successfully created.";
$input['purchase_id']=$object->id;
}
if(isset($input['fish_id'])){
foreach($input['fish_id'] as $key=>$val){
$fish= Fish::find($val);
PurchaseDetails::create([
'quantity'=>$input['quantity'][$key],
'price'=>$input['price'][$key],
'purchase_date'=>isset($input['purchase_date'][$key]) ? date('Y-m-d' , strtotime($input['purchase_date'][$key])) : '0000-00-00',
'item_total'=>$input['item_total'][$key],
'fish_id' => $val,
'title' => $fish->fish_name,
'purchase_id'=>$input['purchase_id']
]);
}
}