From 7c8822c890fb58181411cbb285df66b7a0311e1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Georg=20Mai=C3=9Fer?= Date: Fri, 31 Jan 2025 13:45:25 +0100 Subject: [PATCH] Improvement: Unfinished show receipt for cashback to users #125 --- classes/local/create_invoice.php | 1 + classes/output/shoppingcart_history_list.php | 4 +++ classes/shopping_cart_credits.php | 3 +- classes/shopping_cart_history.php | 33 ++++++++++++++++++++ classes/table/cash_report_table.php | 12 ++++++- report.php | 2 +- 6 files changed, 52 insertions(+), 3 deletions(-) diff --git a/classes/local/create_invoice.php b/classes/local/create_invoice.php index 5d63679b..ba0fa536 100644 --- a/classes/local/create_invoice.php +++ b/classes/local/create_invoice.php @@ -296,6 +296,7 @@ public static function create_receipt( case 'id': // In this case $identifier stores the ledger id. $items = shopping_cart_history::return_data_from_ledger_via_id($identifier); + $identifier = $items[array_key_first($items)]->identifier; break; case 'identifier': default: diff --git a/classes/output/shoppingcart_history_list.php b/classes/output/shoppingcart_history_list.php index 119e9269..e17151ef 100644 --- a/classes/output/shoppingcart_history_list.php +++ b/classes/output/shoppingcart_history_list.php @@ -133,6 +133,10 @@ public function __construct(int $userid, int $identifier = 0, $fromledger = fals } } else { $items = shopping_cart_history::get_history_list_for_user($userid); + + $ledgeritems = shopping_cart_history::return_extra_lines_from_ledger($userid); + + $items = array_merge($ledgeritems, $items); } $iscashier = false; $context = context_system::instance(); diff --git a/classes/shopping_cart_credits.php b/classes/shopping_cart_credits.php index 9a2e8800..1cff7085 100644 --- a/classes/shopping_cart_credits.php +++ b/classes/shopping_cart_credits.php @@ -463,6 +463,7 @@ public static function credit_paid_back( $ledgerrecord->itemname = get_string('paymentmethodcreditspaidbackcash', 'local_shopping_cart'); break; } + $ledgerrecord->identifier = shopping_cart_history::create_unique_cart_identifier($userid); $ledgerrecord->price = (float) (-1.0) * $data['deductible']; $ledgerrecord->credits = (float) (-1.0) * $data['deductible']; $ledgerrecord->currency = $currency; @@ -588,6 +589,7 @@ public static function creditsmanager_correct_credits(stdClass $data) { $ledgerrecord->credits = (float) $data->creditsmanagercredits; $ledgerrecord->currency = $currency; $ledgerrecord->componentname = 'local_shopping_cart'; + $ledgerrecord->identifier = shopping_cart_history::create_unique_cart_identifier($data->userid); $ledgerrecord->payment = LOCAL_SHOPPING_CART_PAYMENT_METHOD_CREDITS_CORRECTION; $ledgerrecord->paymentstatus = LOCAL_SHOPPING_CART_PAYMENT_SUCCESS; $ledgerrecord->usermodified = $USER->id; @@ -601,5 +603,4 @@ public static function creditsmanager_correct_credits(stdClass $data) { } return true; } - } diff --git a/classes/shopping_cart_history.php b/classes/shopping_cart_history.php index 2b31e7d9..ee8baf15 100644 --- a/classes/shopping_cart_history.php +++ b/classes/shopping_cart_history.php @@ -602,6 +602,39 @@ public static function return_data_from_ledger_via_identifier(int $identifier): return []; } + /** + * Return extra ledger data from DB via userid + * + * @param int $userid + * @return array + */ + public static function return_extra_lines_from_ledger(int $userid): array { + global $DB; + + [$inorequal, $params] = $DB->get_in_or_equal( + [ + LOCAL_SHOPPING_CART_PAYMENT_METHOD_CREDITS_CORRECTION, + LOCAL_SHOPPING_CART_PAYMENT_METHOD_CREDITS_PAID_BACK_BY_CASH, + LOCAL_SHOPPING_CART_PAYMENT_METHOD_CREDITS_PAID_BACK_BY_TRANSFER, + LOCAL_SHOPPING_CART_PAYMENT_METHOD_REBOOKING_CREDITS_CORRECTION, + ], + SQL_PARAMS_NAMED + ); + + $sql = "SELECT * + FROM {local_shopping_cart_ledger} scl + WHERE scl.userid = :userid + AND scl.payment $inorequal + "; + $params['userid'] = $userid; + if ( + $data = $DB->get_records_sql($sql, $params) + ) { + return $data; + } + return []; + } + /** * Return ledger data from DB via ledger id (cash report data). * This function won't return data if the payment is already aborted. diff --git a/classes/table/cash_report_table.php b/classes/table/cash_report_table.php index 85ece6be..a442bdbe 100644 --- a/classes/table/cash_report_table.php +++ b/classes/table/cash_report_table.php @@ -191,7 +191,17 @@ public function col_paymentstatus(object $values): string { */ public function col_receipt(object $values): string { - if (!empty($values->identifier)) { + if ( + !in_array( + $values->payment, + [ + LOCAL_SHOPPING_CART_PAYMENT_METHOD_CREDITS_CORRECTION, + LOCAL_SHOPPING_CART_PAYMENT_METHOD_CREDITS_PAID_BACK_BY_CASH, + LOCAL_SHOPPING_CART_PAYMENT_METHOD_CREDITS_PAID_BACK_BY_TRANSFER, + LOCAL_SHOPPING_CART_PAYMENT_METHOD_REBOOKING_CREDITS_CORRECTION, + ] + ) + ) { $url = new moodle_url( '/local/shopping_cart/receipt.php', [ diff --git a/report.php b/report.php index 53df03e7..ea99c9d6 100755 --- a/report.php +++ b/report.php @@ -183,7 +183,7 @@ $fields = "s1.*"; $from = "(SELECT DISTINCT " . $uniqueidpart . " AS uniqueid, scl.id, scl.userid, scl.identifier, scl.price, scl.discount, scl.credits, scl.fee, scl.currency, - u.lastname, u.firstname, u.email, scl.itemid, scl.itemname, scl.payment, scl.paymentstatus, " . + u.lastname, u.firstname, u.email, scl.itemid, scl.itemname, scl.payment, scl.paymentstatus, scl.schistoryid, " . $ootid . $DB->sql_concat("um.firstname", "' '", "um.lastname") . " as usermodified, scl.timecreated, scl.timemodified, scl.annotation,