get table data, store in table by Ajax
table data and getting input data marge store two tables ,
1) purchase_invoice
2)purchase_invoice_details
and also update product table quantity
view
JavaScript
controller
1) purchase_invoice
2)purchase_invoice_details
and also update product table quantity
view
<button class="btn btn-success pull-right" onclick="savePurchaseOrder();">
<span class="glyphicon glyphicon-saved" aria-hidden="true"></span>
Create Order</button>
JavaScript
<script>
// ********************************************************
// * save purchase order *
// ********************************************************
function savePurchaseOrder() {
var e = document.getElementById("supplier_id");
var supplier_id = e.options[e.selectedIndex].value;
var dataString = '&supplier_id=' + supplier_id;
var TableData;
// TableData = $.toJSON(storeTblValues());
TableData = JSON.stringify(storeTblValues());
// console.log(TableData);
function storeTblValues() {
var TableData = new Array();
$('#productlistshow tr').each(function (row, tr) {
var itemname = $(tr).find('th:eq(0)').text();
var itemcode = $(tr).find('td:eq(0)').text();
var itemprice = $(tr).find('td:eq(1)').text();
var itemquantity = $(this).closest('tr').find("td:eq(2) input").val();
var product_id = $(this).closest('tr').find("td:eq(3) input").val();
//var rowid = (row + 1);
// console.log(rowid);
TableData[row] = {
"product_id": product_id,
"itemname": itemname,
"itemcode": itemcode,
"itemprice": itemprice,
"itemquantity": itemquantity
}
console.log(TableData[row]);
});
return TableData;
}
$.ajax({
type: "POST",
url: "<?php echo base_url('OrderController/savePurchaseorder');?>",
data: 'pTableData=' + TableData + '&dataString=' + dataString,
cache: false,
success: function (res) {}
});
}
</script>
controller
//savePurchaseorder
function savePurchaseorder(){
$data['supplier_id'] =$this->input->post('supplier_id');
// $data['itemprice'] =$this->input->post('itemprice');
$data['status']= 1;
$data['date']= date('Y-m-d');
$this->db->insert('purchase_invoice',$data);
$last_id = $this->db->insert_id();
$tableData = $this->input->post('pTableData');
// Decode the JSON array
$tableDataarray = json_decode($tableData,TRUE);
// echo "<pre>";
// print_r($tableDataarray);
if (!empty($last_id)){
$valuedata = array();
foreach($tableDataarray as $key=>$val) {
//get data from product table for update quantiy update
$ProductTableID=$val['product_id'];
$this->db->select("slug,product_id,qty");
$this->db->from('product');
$this->db->where('product_id', $ProductTableID);
$productDataarray = $this->db->get();
$productData = $productDataarray->first_row();
$ProductTableQty=$productData->qty;
$UpdateQty=($ProductTableQty + $val['itemquantity']);
//get purchase order date
$pdata['p_in_id'] = $last_id;
$pdata['supplier_id'] = $this->input->post('supplier_id');
$pdata['product_id'] = $val['product_id'];
$pdata['pro_code'] = $val['itemcode'];
$pdata['order_qty'] = $val['itemquantity'];
$pdata['pro_unit_price'] = $val['itemprice'];
$pdata['itemname'] = $val['itemname'];
$pdata['status'] = 1;
$pdata['date'] = date('Y-m-d');
//insert purchase order item data
$valuedata = $this->db->insert('purchase_invoice_details', $pdata);
/* for Update Product Quantity */
if($valuedata){
$this->db->set('qty', $UpdateQty);
$this->db->where('product_id', $ProductTableID);
$this->db->update('product');
}
}
}
}
Comments
Post a Comment