From fe6820920b90843ca1b5e7ca56b9074da5bc299a Mon Sep 17 00:00:00 2001 From: jekkos-t520 Date: Tue, 21 Oct 2014 21:39:22 +0200 Subject: [PATCH] Add edit form in detailed receivings report Add language lines for detailed sale & receiving validation Add permission for getting updated sale & receiving detail row --- application/controllers/items.php | 13 +- application/controllers/receivings.php | 68 +++++++ application/controllers/reports.php | 32 +++- application/helpers/table_helper.php | 4 +- application/language/en/receivings_lang.php | 15 ++ application/language/en/sales_lang.php | 3 + application/models/receiving.php | 51 ++++++ .../models/reports/detailed_receivings.php | 10 ++ application/models/sale.php | 8 +- application/views/receivings/form.php | 169 ++++++++++++++++++ application/views/reports/tabular_details.php | 8 +- application/views/sales/form.php | 2 +- 12 files changed, 363 insertions(+), 20 deletions(-) create mode 100755 application/views/receivings/form.php diff --git a/application/controllers/items.php b/application/controllers/items.php index be0c60aaa..515d67c0d 100644 --- a/application/controllers/items.php +++ b/application/controllers/items.php @@ -612,10 +612,15 @@ class Items extends Secure_area implements iData_controller return 360; } - function is_sale_store_item($item_number) - { - echo $this->Item->is_sale_store_item_exist($item_number); - } + function item_number_check($item_number) + { + if ($this->Item->get_item_id($item_number) != FALSE) + { + $this->form_validation->set_message('item_number_check', $this->lang->line('items_item_number_exists')); + echo json_encode(array('success'=>true,'message'=>$this->lang->line('items_successful_adding')); + } + echo json_encode(array('success'=>false,'message'=>$this->lang->line('items_error_adding')); + } } ?> \ No newline at end of file diff --git a/application/controllers/receivings.php b/application/controllers/receivings.php index 90e011f81..2c32b3d5c 100644 --- a/application/controllers/receivings.php +++ b/application/controllers/receivings.php @@ -111,12 +111,52 @@ class Receivings extends Secure_area $this->_reload($data); } + + function edit($receiving_id) + { + $data = array(); + + $data['suppliers'] = array('' => 'No Supplier'); + foreach ($this->Supplier->get_all()->result() as $supplier) + { + $data['suppliers'][$supplier->person_id] = $supplier->first_name . ' ' . $supplier->last_name; + } + + $data['employees'] = array(); + foreach ($this->Employee->get_all()->result() as $employee) + { + $data['employees'][$employee->person_id] = $employee->first_name . ' '. $employee->last_name; + } + + $receiving_info = $this->Receiving->get_info($receiving_id)->row_array(); + $person_name = $receiving_info['first_name'] . " " . $receiving_info['last_name']; + $data['selected_supplier'] = !empty($receiving_info['supplier_id']) ? $receiving_info['supplier_id'] . "|" . $person_name : ""; + $data['receiving_info'] = $receiving_info; + + $this->load->view('receivings/form', $data); + } function delete_item($item_number) { $this->receiving_lib->delete_item($item_number); $this->_reload(); } + + function delete($receiving_id = -1, $update_inventory=TRUE) + { + $employee_id=$this->Employee->get_logged_in_employee_info()->person_id; + $receiving_ids=$receiving_id == -1 ? $this->input->post('ids') : array($receiving_id); + + if($this->Receiving->delete_list($receiving_ids, $employee_id, $update_inventory)) + { + echo json_encode(array('success'=>true,'message'=>$this->lang->line('recvs_delete_successful').' '. + count($receiving_ids).' '.$this->lang->line('recvs_one_or_multiple'),'ids'=>$receiving_ids)); + } + else + { + echo json_encode(array('success'=>false,'message'=>$this->lang->line('recvs_delete_unsuccessful'))); + } + } function delete_supplier() { @@ -282,6 +322,34 @@ class Receivings extends Secure_area $this->load->view("receivings/receiving",$data); $this->_remove_duplicate_cookies(); } + + function save($receiving_id) + { + $receiving_data = array( + 'receiving_time' => date('Y-m-d', strtotime($this->input->post('date'))), + 'supplier_id' => $this->input->post('supplier_id') ? $this->input->post('supplier_id') : null, + 'employee_id' => $this->input->post('employee_id'), + 'comment' => $this->input->post('comment'), + 'invoice_number' => $this->input->post('invoice_number') + ); + + if ($this->Receiving->update($receiving_data, $receiving_id)) + { + echo json_encode(array( + 'success'=>true, + 'message'=>$this->lang->line('recvs_successfully_updated'), + 'id'=>$receiving_id) + ); + } + else + { + echo json_encode(array( + 'success'=>false, + 'message'=>$this->lang->line('recvs_unsuccessfully_updated'), + 'id'=>$receiving_id) + ); + } + } function cancel_receiving() { diff --git a/application/controllers/reports.php b/application/controllers/reports.php index 9e03d0c4b..ef5d74d16 100644 --- a/application/controllers/reports.php +++ b/application/controllers/reports.php @@ -12,7 +12,7 @@ class Reports extends Secure_area parent::__construct('reports'); $method_name = $this->uri->segment(2); $exploder = explode('_', $method_name); - preg_match("/(?:inventory)|([^_.]*)(?:_graph)?$/", $method_name, $matches); + preg_match("/(?:inventory)|([^_.]*)(?:_graph|_row)?$/", $method_name, $matches); preg_match("/^(.*?)([sy])?$/", array_pop($matches), $matches); $submodule_id = $matches[1] . ((count($matches) > 2) ? $matches[2] : "s"); $employee_id=$this->Employee->get_logged_in_employee_info()->person_id; @@ -72,7 +72,28 @@ class Reports extends Secure_area to_currency($report_data['profit']), $report_data['payment_type'], $report_data['comment']); - echo get_detailed_sales_data_row($summary_data, $this); + echo get_detailed_data_row($summary_data, $this); + } + + function get_detailed_receivings_row($receiving_id, $receiving_type=1) + { + $this->load->model('reports/Detailed_receivings'); + $model = $this->Detailed_receivings; + + $report_data = $model->getDataByReceivingId($receiving_id, $receiving_type); + + $summary_data = array(anchor('receivings/edit/'.$report_data['receiving_id'] . '/width:'.FORM_WIDTH, + 'RECV '.$report_data['receiving_id'], + array('class' => 'thickbox')), + $report_data['receiving_date'], + $report_data['items_purchased'], + $report_data['employee_name'], + $report_data['supplier_name'], + to_currency($report_data['total']), + $report_data['payment_type'], + $report_data['invoice_number'], + $report_data['comment']); + echo get_detailed_data_row($summary_data, $this); } function get_summary_data($start_date, $end_date = NULL, $sale_type=0) @@ -845,7 +866,7 @@ class Reports extends Secure_area "title" =>$this->lang->line('reports_detailed_sales_report'), "subtitle" => date('m/d/Y', strtotime($start_date)) .'-'.date('m/d/Y', strtotime($end_date)), "headers" => $model->getDataColumns(), - "editable" => true, + "editable" => "sales", "summary_data" => $summary_data, "details_data" => $details_data, "header_width" => intval(100 / count($headers['summary'])), @@ -862,14 +883,14 @@ class Reports extends Secure_area $model = $this->Detailed_receivings; $headers = $model->getDataColumns(); - $report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'receiving_type' => $receiving_type)); + $report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'receiving_type'=>$receiving_type)); $summary_data = array(); $details_data = array(); foreach($report_data['summary'] as $key=>$row) { - $summary_data[] = array(anchor('receivings/receipt/'.$row['receiving_id'], 'RECV '.$row['receiving_id'], array('target' => '_blank')), $row['receiving_date'], $row['items_purchased'], $row['employee_name'], $row['supplier_name'], to_currency($row['total']), $row['payment_type'], $row['invoice_number'], $row['comment']); + $summary_data[] = array(anchor('receivings/edit/'.$row['receiving_id'].'/width:'.FORM_WIDTH, 'RECV '.$row['receiving_id'], array('class' => 'thickbox')), $row['receiving_date'], $row['items_purchased'], $row['employee_name'], $row['supplier_name'], to_currency($row['total']), $row['payment_type'], $row['invoice_number'], $row['comment']); foreach($report_data['details'][$key] as $drow) { @@ -881,6 +902,7 @@ class Reports extends Secure_area "title" =>$this->lang->line('reports_detailed_receivings_report'), "subtitle" => date('m/d/Y', strtotime($start_date)) .'-'.date('m/d/Y', strtotime($end_date)), "headers" => $model->getDataColumns(), + "editable" => "receivings", "summary_data" => $summary_data, "details_data" => $details_data, "overall_summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'receiving_type' => $receiving_type)), diff --git a/application/helpers/table_helper.php b/application/helpers/table_helper.php index b4189612c..72fa6e346 100644 --- a/application/helpers/table_helper.php +++ b/application/helpers/table_helper.php @@ -64,11 +64,11 @@ function get_person_data_row($person,$controller) return $table_data_row; } -function get_detailed_sales_data_row($sale, $controller) +function get_detailed_data_row($row, $controller) { $table_data_row=''; $table_data_row.='+'; - foreach($sale as $cell) + foreach($row as $cell) { $table_data_row.=''; $table_data_row.=$cell; diff --git a/application/language/en/receivings_lang.php b/application/language/en/receivings_lang.php index cbbf80c57..11e1ab094 100644 --- a/application/language/en/receivings_lang.php +++ b/application/language/en/receivings_lang.php @@ -39,4 +39,19 @@ $lang['recvs_stock_destination']='Stock destination'; $lang['recvs_stock_locaiton']='Stock location'; $lang['recvs_error_requisition']='Unable to move inventory from and to the same stock location'; $lang['recvs_invoice_number']='Invoice #'; +$lang['recvs_receipt_number']='Receiving #'; + +$lang['recvs_date'] = 'Receiving Date'; +$lang['recvs_successfully_updated'] = 'Receiving successfully updated'; +$lang['recvs_unsuccessfully_updated'] = 'Receiving unsuccessfully updated'; +$lang['recvs_edit_sale'] = 'Edit Receiving'; +$lang['recvs_successfully_deleted'] = 'Receiving successfully deleted'; +$lang['recvs_unsuccessfully_deleted'] = 'Receiving unsuccessfully deleted'; +$lang['recvs_delete_entire_sale'] = 'Delete entire sale'; +$lang['recvs_comments'] = 'Comments'; +$lang['recvs_basic_information']='Receiving information'; +$lang['recvs_employee']='Employee'; +$lang['recvs_receipt']='Receivings Receipt'; +$lang['recvs_date_required']='A correct date needs to be filled in'; +$lang['recvs_date_type']='Date field is required'; ?> diff --git a/application/language/en/sales_lang.php b/application/language/en/sales_lang.php index be0d2dc4e..83dc494ce 100644 --- a/application/language/en/sales_lang.php +++ b/application/language/en/sales_lang.php @@ -86,4 +86,7 @@ $lang['sales_basic_information']='Sale information'; $lang['sales_stock_location']='Stock location'; $lang['sales_sale']='Sale'; +$lang['sales_receipt_number']='Sale #'; +$lang['sales_date_required']='A correct date needs to be filled in'; +$lang['sales_date_type']='Date field is required'; ?> diff --git a/application/models/receiving.php b/application/models/receiving.php index 264239796..65996ea5b 100644 --- a/application/models/receiving.php +++ b/application/models/receiving.php @@ -4,6 +4,7 @@ class Receiving extends CI_Model function get_info($receiving_id) { $this->db->from('receivings'); + $this->db->join('people', 'people.person_id = receivings.supplier_id', 'LEFT'); $this->db->where('receiving_id',$receiving_id); return $this->db->get(); } @@ -30,6 +31,14 @@ class Receiving extends CI_Model return ($query->num_rows()==1); } + + function update($receiving_data, $receiving_id) + { + $this->db->where('receiving_id', $receiving_id); + $success = $this->db->update('receivings',$receiving_data); + + return $success; + } function save ($items,$supplier_id,$employee_id,$comment,$payment_type,$receiving_id=false,$invoice_number=null) { @@ -102,6 +111,48 @@ class Receiving extends CI_Model return $receiving_id; } + + function delete_list($receiving_ids,$employee_id,$update_inventory=TRUE) + { + $result = TRUE; + foreach($receiving_ids as $receiving_id) { + $result &= $this->delete($receiving_id,$employee_id,$update_inventory); + } + return $result; + } + + function delete($receiving_id,$employee_id,$update_inventory=TRUE) + { + // start a transaction to assure data integrity + $this->db->trans_start(); + if ($update_inventory) { + // defect, not all item deletions will be undone?? + // get array with all the items involved in the sale to update the inventory tracking + $items = $this->get_receiving_items($receiving_id)->result_array(); + foreach($items as $item) { + // create query to update inventory tracking + $inv_data = array + ( + 'trans_date'=>date('Y-m-d H:i:s'), + 'trans_items'=>$item['item_id'], + 'trans_user'=>$employee_id, + 'trans_comment'=>'Deleting sale ' . $receiving_id, + 'trans_inventory'=>$item['quantity_purchased'] + + ); + // update inventory + $this->Inventory->insert($inv_data); + } + } + // delete all items + $this->db->delete('receivings_items', array('receiving_id' => $receiving_id)); + // delete sale itself + $this->db->delete('receivings', array('receiving_id' => $receiving_id)); + // execute transaction + $this->db->trans_complete(); + + return $this->db->trans_status(); + } function get_receiving_items($receiving_id) { diff --git a/application/models/reports/detailed_receivings.php b/application/models/reports/detailed_receivings.php index 668a8ca5a..a1cadb429 100644 --- a/application/models/reports/detailed_receivings.php +++ b/application/models/reports/detailed_receivings.php @@ -14,6 +14,16 @@ class Detailed_receivings extends Report ); } + public function getDataByReceivingId($receiving_id, $sale_type) + { + $this->db->select('receiving_id, DATE_FORMAT(receiving_date, "%d-%m-%Y") AS receiving_date, sum(quantity_purchased) as items_purchased, CONCAT(employee.first_name," ",employee.last_name) as employee_name, suppliers.company_name as supplier_name, sum(subtotal) as subtotal, sum(total) as total, sum(profit) as profit, payment_type, comment, invoice_number', false); + $this->db->from('receivings_items_temp'); + $this->db->join('people as employee', 'receivings_items_temp.employee_id = employee.person_id'); + $this->db->join('suppliers as suppliers', 'receivings_items_temp.supplier_id = suppliers.person_id', 'left'); + $this->db->where('receiving_id', $receiving_id); + return $this->db->get()->row_array(); + } + public function getData(array $inputs) { $this->db->select('receiving_id, receiving_date, sum(quantity_purchased) as items_purchased, CONCAT(employee.first_name," ",employee.last_name) as employee_name, CONCAT(supplier.first_name," ",supplier.last_name) as supplier_name, sum(total) as total, sum(profit) as profit, payment_type, comment, invoice_number', false); diff --git a/application/models/sale.php b/application/models/sale.php index ccd2fb7c2..2963fe5cb 100644 --- a/application/models/sale.php +++ b/application/models/sale.php @@ -3,8 +3,6 @@ class Sale extends CI_Model { public function get_info($sale_id) { - $this->db->select('sales.*', FALSE); - $this->db->select('people.*', FALSE); $this->db->from('sales'); $this->db->join('people', 'people.person_id = sales.customer_id', 'LEFT'); $this->db->where('sale_id',$sale_id); @@ -142,7 +140,8 @@ class Sale extends CI_Model return $sale_id; } - function delete_list($sale_ids, $employee_id,$update_inventory=TRUE) { + function delete_list($sale_ids, $employee_id,$update_inventory=TRUE) + { $result = TRUE; foreach($sale_ids as $sale_id) { $result &= $this->delete($sale_id, $employee_id, $update_inventory); @@ -150,7 +149,8 @@ class Sale extends CI_Model return $result; } - function delete($sale_id,$employee_id,$update_inventory=TRUE) { + function delete($sale_id,$employee_id,$update_inventory=TRUE) + { // start a transaction to assure data integrity $this->db->trans_start(); // first delete all payments diff --git a/application/views/receivings/form.php b/application/views/receivings/form.php new file mode 100755 index 000000000..92abbd1b2 --- /dev/null +++ b/application/views/receivings/form.php @@ -0,0 +1,169 @@ +
+
lang->line('common_fields_required_message'); ?>
+ + +
+ 'recvs_edit_form')); ?> + lang->line("recvs_basic_information"); ?> + +
+ lang->line('recvs_receipt_number').':', 'supplier'); ?> +
+ lang->line('recvs_receipt_number') .$receiving_info['receiving_id'], array('target' => '_blank'));?> +
+
+ +
+ lang->line('recvs_date').':', 'date', array('class'=>'required')); ?> +
+ 'date','value'=>date('m/d/Y', strtotime($receiving_info['receiving_time'])), 'id'=>'date'));?> +
+
+ +
+ lang->line('recvs_supplier').':', 'supplier'); ?> +
+ 'supplier_id', 'value' => $selected_supplier, 'id' => 'supplier_id'));?> +
+
+ +
+ lang->line('recvs_invoice_number').':', 'invoice_number'); ?> +
+ 'invoice_number', 'value' => $receiving_info['invoice_number'], 'id' => 'invoice_number'));?> +
+
+ +
+ lang->line('recvs_employee').':', 'employee'); ?> +
+ +
+
+ +
+ lang->line('recvs_comments').':', 'comment'); ?> +
+ 'comment','value'=>$receiving_info['comment'],'rows'=>'4','cols'=>'23', 'id'=>'comment'));?> +
+
+ + 'submit', + 'value'=>$this->lang->line('common_submit'), + 'class'=> 'submit_button float_right') + ); + ?> + + + 'recvs_delete_form')); ?> + + 'submit', + 'value'=>$this->lang->line('recvs_delete_entire_sale'), + 'class'=>'delete_button float_right') + ); + ?> + +
+
+ + \ No newline at end of file diff --git a/application/views/reports/tabular_details.php b/application/views/reports/tabular_details.php index f8c0303be..c7421d731 100644 --- a/application/views/reports/tabular_details.php +++ b/application/views/reports/tabular_details.php @@ -94,14 +94,14 @@ function post_form_submit(response, row_id) } else { - var sale_id = response.id - $.get('/'+sale_id, function(response) + var row_id = response.id + $.get('/'+row_id, function(response) { //Replace previous row - var row = get_table_row(sale_id).parent().parent(); + var row = get_table_row(row_id).parent().parent(); var sign = row.find("a.expand").text(); row.replaceWith(response); - row = get_table_row(sale_id).parent().parent(); + row = get_table_row(row_id).parent().parent(); update_sortable_table(); animate_row(row); row.find("a.expand").click(expand_handler).text(sign); diff --git a/application/views/sales/form.php b/application/views/sales/form.php index 4e86331ec..358436eef 100755 --- a/application/views/sales/form.php +++ b/application/views/sales/form.php @@ -7,7 +7,7 @@ lang->line("sales_basic_information"); ?>
- lang->line('sales_receipt').':', 'customer'); ?> + lang->line('sales_receipt_number').':', 'customer'); ?>
lang->line('sales_receipt_number') .$sale_info['sale_id'], array('target' => '_blank'));?>