From 64c2996c740014dfe7708b2571d4624240265f71 Mon Sep 17 00:00:00 2001 From: Steve Ireland Date: Sun, 19 Nov 2017 11:34:27 -0500 Subject: [PATCH] Improve the ability to restore canceled sales. --- application/controllers/Reports.php | 28 +++++++++++-- application/controllers/Sales.php | 40 +++++++++++++------ application/language/en-US/common_lang.php | 1 + application/language/en-US/sales_lang.php | 8 ++-- application/models/Sale.php | 31 +++++++------- .../views/partial/bootstrap_tables_locale.php | 11 +++-- application/views/sales/form.php | 5 +++ public/js/manage_tables.js | 40 ++++++++++++++++++- 8 files changed, 126 insertions(+), 38 deletions(-) diff --git a/application/controllers/Reports.php b/application/controllers/Reports.php index 3600a7c40..8c1941024 100644 --- a/application/controllers/Reports.php +++ b/application/controllers/Reports.php @@ -974,6 +974,17 @@ class Reports extends Secure_Controller $report_data = $model->getDataBySaleId($sale_id); + if($report_data['sale_status'] == CANCELED) + { + $button_key = 'data-btn-restore'; + $button_label = $this->lang->line('common_restore'); + } + else + { + $button_key = 'data-btn-delete'; + $button_label = $this->lang->line('common_delete'); + } + $summary_data = $this->xss_clean(array( 'sale_id' => $report_data['sale_id'], 'sale_date' => $report_data['sale_date'], @@ -988,8 +999,7 @@ class Reports extends Secure_Controller 'payment_type' => $report_data['payment_type'], 'comment' => $report_data['comment'], 'edit' => anchor('sales/edit/'. $report_data['sale_id'], '', - array('class'=>'modal-dlg print_hide', 'data-btn-delete' => $this->lang->line('common_delete'), 'data-btn-submit' => $this->lang->line('common_submit'), 'title' => $this->lang->line('sales_update')) - ) + array('class'=>'modal-dlg print_hide', $button_key => $button_label, 'data-btn-submit' => $this->lang->line('common_submit'), 'title' => $this->lang->line('sales_update'))) )); echo json_encode(array($sale_id => $summary_data)); @@ -1034,6 +1044,17 @@ class Reports extends Secure_Controller foreach($report_data['summary'] as $key => $row) { + if($row['sale_status'] == CANCELED) + { + $button_key = 'data-btn-restore'; + $button_label = $this->lang->line('common_restore'); + } + else + { + $button_key = 'data-btn-delete'; + $button_label = $this->lang->line('common_delete'); + } + $summary_data[] = $this->xss_clean(array( 'id' => $row['sale_id'], 'type_code' => $row['type_code'], @@ -1049,8 +1070,7 @@ class Reports extends Secure_Controller 'payment_type' => $row['payment_type'], 'comment' => $row['comment'], 'edit' => anchor('sales/edit/'.$row['sale_id'], '', - array('class' => 'modal-dlg print_hide', 'data-btn-delete' => $this->lang->line('common_delete'), 'data-btn-submit' => $this->lang->line('common_submit'), 'title' => $this->lang->line('sales_update')) - ) + array('class' => 'modal-dlg print_hide', $button_key => $button_label, 'data-btn-submit' => $this->lang->line('common_submit'), 'title' => $this->lang->line('sales_update'))) )); foreach($report_data['details'][$key] as $drow) diff --git a/application/controllers/Sales.php b/application/controllers/Sales.php index 9eb5a9bf4..cd70e2693 100644 --- a/application/controllers/Sales.php +++ b/application/controllers/Sales.php @@ -1128,20 +1128,11 @@ class Sales extends Secure_Controller else { $sale_ids = $sale_id == -1 ? $this->input->post('ids') : array($sale_id); - $reactivate = FALSE; - if($this->Sale->delete_list($sale_ids, $employee_id, $update_inventory, $reactivate)) + if($this->Sale->delete_list($sale_ids, $employee_id, $update_inventory)) { - if(!$reactivate) - { - echo json_encode(array('success' => TRUE, 'message' => $this->lang->line('sales_successfully_deleted') . ' ' . - count($sale_ids) . ' ' . $this->lang->line('sales_one_or_multiple'), 'ids' => $sale_ids)); - } - else - { - echo json_encode(array('success' => TRUE, 'message' => $this->lang->line('sales_successfully_reactivated') . ' ' . - count($sale_ids) . ' ' . $this->lang->line('sales_one_or_multiple'), 'ids' => $sale_ids)); - } + echo json_encode(array('success' => TRUE, 'message' => $this->lang->line('sales_successfully_deleted') . ' ' . + count($sale_ids) . ' ' . $this->lang->line('sales_one_or_multiple'), 'ids' => $sale_ids)); } else { @@ -1150,6 +1141,31 @@ class Sales extends Secure_Controller } } + public function restore($sale_id = -1, $update_inventory = TRUE) + { + $employee_id = $this->Employee->get_logged_in_employee_info()->person_id; + $has_grant = $this->Employee->has_grant('sales_delete', $employee_id); + + if(!$has_grant) + { + echo json_encode(array('success' => FALSE, 'message' => $this->lang->line('sales_not_authorized'))); + } + else + { + $sale_ids = $sale_id == -1 ? $this->input->post('ids') : array($sale_id); + + if($this->Sale->restore_list($sale_ids, $employee_id, $update_inventory)) + { + echo json_encode(array('success' => TRUE, 'message' => $this->lang->line('sales_successfully_restored') . ' ' . + count($sale_ids) . ' ' . $this->lang->line('sales_one_or_multiple'), 'ids' => $sale_ids)); + } + else + { + echo json_encode(array('success' => FALSE, 'message' => $this->lang->line('sales_unsuccessfully_restored'))); + } + } + } + /** * This saves the sale from the update sale view (sales/form). * It only updates the sales table and payments. diff --git a/application/language/en-US/common_lang.php b/application/language/en-US/common_lang.php index 42a05e5ec..1f61a7269 100644 --- a/application/language/en-US/common_lang.php +++ b/application/language/en-US/common_lang.php @@ -51,6 +51,7 @@ $lang["common_price"] = "Price"; $lang["common_print"] = "Print"; $lang["common_remove"] = "Remove"; $lang["common_required"] = "Required"; +$lang["common_restore"] = "Restore"; $lang["common_return_policy"] = "Return Policy"; $lang["common_search"] = "Search"; $lang["common_search_options"] = "Search options"; diff --git a/application/language/en-US/sales_lang.php b/application/language/en-US/sales_lang.php index cd575db3e..cec84fdb3 100644 --- a/application/language/en-US/sales_lang.php +++ b/application/language/en-US/sales_lang.php @@ -18,7 +18,8 @@ $lang["sales_comment"] = "Comment"; $lang["sales_comments"] = "Comments"; $lang["sales_complete_sale"] = "Complete"; $lang["sales_confirm_cancel_sale"] = "Are you sure you want to clear this sale? All items will cleared."; -$lang["sales_confirm_delete"] = "Are you sure you want to delete or reactivate the selected Sale(s)?"; +$lang["sales_confirm_delete"] = "Are you sure you want to delete the selected Sale(s)?"; +$lang["sales_confirm_restore"] = "Are you sure you want to restore the selected Sale(s)?"; $lang["sales_credit"] = "Credit Card"; $lang["sales_credit_deposit"] = "Credit Deposit"; $lang["sales_customer"] = "Name"; @@ -85,7 +86,7 @@ $lang["sales_no_filter"] = "All"; $lang["sales_no_items_in_cart"] = "There are no Items in the cart."; $lang["sales_no_sales_to_display"] = "No Sales to display."; $lang["sales_none_selected"] = "You have not selected any Sale(s) to delete."; -$lang["sales_not_authorized"]= "You are not authorized to delete sales transactions"; +$lang["sales_not_authorized"]= "This action is not authorized."; $lang["sales_one_or_multiple"] = "Sale(s)"; $lang["sales_payment"] = "Payment Type"; $lang["sales_payment_amount"] = "Amount"; @@ -131,7 +132,7 @@ $lang["sales_stock"] = "Stock"; $lang["sales_stock_location"] = "Stock Location"; $lang["sales_sub_total"] = "Subtotal"; $lang["sales_successfully_deleted"] = "You have successfully deleted"; -$lang["sales_successfully_reactivated"] = "You have successfully reactivated"; +$lang["sales_successfully_restored"] = "You have successfully restored"; $lang["sales_successfully_suspended_sale"] = "Sale suspend successful."; $lang["sales_successfully_updated"] = "Sale update successful."; $lang["sales_suspend_sale"] = "Suspend"; @@ -147,6 +148,7 @@ $lang["sales_total_tax_exclusive"] = "Tax excluded"; $lang["sales_transaction_failed"] = "Sales Transaction failed."; $lang["sales_unable_to_add_item"] = "Item add to Sale failed"; $lang["sales_unsuccessfully_deleted"] = "Sale(s) delete failed."; +$lang["sales_unsuccessfully_restored"] = "Sale(s) restore failed."; $lang["sales_unsuccessfully_suspended_sale"] = "Sale suspend failed."; $lang["sales_unsuccessfully_updated"] = "Sale update failed."; $lang["sales_unsuspend"] = "Unsuspend"; diff --git a/application/models/Sale.php b/application/models/Sale.php index e2039ac1f..4330065a0 100644 --- a/application/models/Sale.php +++ b/application/models/Sale.php @@ -808,24 +808,37 @@ class Sale extends CI_Model /** * Deletes list of sales */ - public function delete_list($sale_ids, $employee_id, &$reactivated, $update_inventory = TRUE) + public 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, $reactivated, $update_inventory); + $result &= $this->delete($sale_id, $employee_id, $update_inventory); } return $result; } + /** + * Restores list of sales + */ + public function restore_list($sale_ids, $employee_id, $update_inventory = TRUE) + { + foreach($sale_ids as $sale_id) + { + $this->update_sale_status($sale_id, SUSPENDED); + } + + return TRUE; + } + /** * Delete sale. Hard deletes are not supported for sales transactions. * When a sale is "deleted" it is simply changed to a status of canceled. * However, if applicable the inventory still needs to be updated */ - public function delete($sale_id, $employee_id, &$reactivated, $update_inventory = TRUE) + public function delete($sale_id, $employee_id, $update_inventory = TRUE) { // start a transaction to assure data integrity $this->db->trans_start(); @@ -860,17 +873,7 @@ class Sale extends CI_Model } } - // Set the sale_status is canceled the reactive the sale by setting the status to suspended - // otherwise set the status to canceled - if($sale_status == CANCELED) - { - $this->update_sale_status($sale_id, SUSPENDED); - $reactivated = TRUE; - } - else - { - $this->update_sale_status($sale_id, CANCELED); - } + $this->update_sale_status($sale_id, CANCELED); // execute transaction $this->db->trans_complete(); diff --git a/application/views/partial/bootstrap_tables_locale.php b/application/views/partial/bootstrap_tables_locale.php index 8610e4aa9..612c2bd7e 100644 --- a/application/views/partial/bootstrap_tables_locale.php +++ b/application/views/partial/bootstrap_tables_locale.php @@ -1,7 +1,7 @@ (function ($) { - 'use strict'; + 'use strict'; - $.fn.bootstrapTable.locales[''] = { + $.fn.bootstrapTable.locales[''] = { formatLoadingMessage: function () { return "lang->line('tables_loading');?>"; }, @@ -35,9 +35,12 @@ }, formatConfirmDelete : function() { return "lang->line((isset($editable) ? $editable : $controller_name). "_confirm_delete")?>"; + }, + formatConfirmRestore : function() { + return "lang->line((isset($editable) ? $editable : $controller_name). "_confirm_restore")?>"; } - }; + }; - $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales[""]); + $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales[""]); })(jQuery); \ No newline at end of file diff --git a/application/views/sales/form.php b/application/views/sales/form.php index d50e22327..6c4bda4ce 100755 --- a/application/views/sales/form.php +++ b/application/views/sales/form.php @@ -160,6 +160,11 @@ $(document).ready(function() table_support.do_delete('', ); }); + $('button#restore').click(function() { + dialog_support.hide(); + table_support.do_restore('', ); + }); + var submit_form = function() { $(this).ajaxSubmit( diff --git a/public/js/manage_tables.js b/public/js/manage_tables.js index f3799ed7e..16a8603c1 100644 --- a/public/js/manage_tables.js +++ b/public/js/manage_tables.js @@ -172,6 +172,36 @@ } }; + var do_restore = function (url, ids) { + if (confirm($.fn.bootstrapTable.defaults.formatConfirmRestore())) { + $.post((url || options.resource) + '/restore', {'ids[]': ids || selected_ids()}, function (response) { + //restore was successful, remove checkbox rows + if (response.success) { + var selector = ids ? row_selector(ids) : selected_rows(); + table().collapseAllRows(); + $(selector).each(function (index, element) { + $(this).find("td").animate({backgroundColor: "green"}, 1200, "linear") + .end().animate({opacity: 0}, 1200, "linear", function () { + table().remove({ + field: options.uniqueId, + values: selected_ids() + }); + if (index == $(selector).length - 1) { + refresh(); + enable_actions(); + } + }); + }); + $.notify(response.message, { type: 'success' }); + } else { + $.notify(response.message, { type: 'danger' }); + } + }, "json"); + } else { + return false; + } + }; + var load_success = function(callback) { return function(response) { typeof options.load_callback == 'function' && options.load_callback(); @@ -241,6 +271,7 @@ })); enable_actions(); init_delete(); + init_restore(); toggle_column_visbility(); dialog_support.init("button.modal-dlg"); }; @@ -251,6 +282,12 @@ }); }; + var init_restore = function (confirmMessage) { + $("#restore").click(function (event) { + do_restore(); + }); + }; + var refresh = function() { table().refresh(); } @@ -266,7 +303,7 @@ var rows = $(selector.join(",")).length; if (rows > 0 && rows < 15) { var ids = response.id.split(":"); - $.get([url || resource + '/get_row', id].join("/"), {}, function (response) { + $.get([url || resource + '/get_row', id].join("/"), {}, function (response) { $.each(selector, function (index, element) { var id = $(element).data('uniqueid'); table().updateByUniqueId({id: id, row: response[id] || response}); @@ -297,6 +334,7 @@ handle_submit: handle_submit, init: init, do_delete: do_delete, + do_restore: do_restore, refresh : refresh, selected_ids : selected_ids, });