Sunday, October 10, 2021

Caltex RM5 cashback with CaltexGO pump-and-go mobile app

As you might have been aware, Caltex Malaysia has recently introduced the CaltexGO pump-and-go mobile app, available in Google Play Store and Apple App Store.

Apparently, it is the same CaltexGO mobile app already in used in Caltex Singapore and Caltex Thailand for the same purpose of enabling contactless and cashless petrol refueling at their petrol station.

This is an enhancement to the existing Caltax tap, pump and go system, which you need to slot in your JOURNEY card to collect BPoints, then tap your credit card or debit card at the petrol pump terminal to refuel your vehicle.

Now, with the CaltexGO mobile app, you can bind your JOURNEY card and your credit/debit card with your user account, and you can refuel your vehicle at Caltex petrol station even without bringing those cards along with you.

CaltexGO will auto-detect the Caltex petrol station you are located at with your mobile phone GPS location detection service. You just need to enter your petrol pump terminal number into the app, then lift the petrol pump and start refueling. When you put back the pump to the terminal, your bill will be instantly generated, and you just tap a confirm button to pay for it. Your JOURNEY card loyalty points will be automatically credited too.

Currently CaltexGO in Malaysia only support VISA or Mastercard credit card or debit card. Hopefully e-wallets will be included too in the future.

As a welcome offer to encourage usage of CaltexGO mobile app, Caltex Malaysia is currently giving out RM5 cashback for petrol refueling of RM30 and above, until 31st December 2021.


Your RM5 cashback will be automatically used during your next petrol refueling of RM30 and above at Caltex station using CaltexGO. You will see it as "promotion discount" in your bill, as shown above. Your bills are paperless and stored in your app, so you can always track back your historical petrol refueling transactions in Caltex station.

This means that you will not immediately get the RM5 cashback during your first time using CaltexGO to pump petrol of RM30 and above. The next time and all subsequent times you do the same, you will get the RM5 cashback automatically, until the end of promotion period on 31st December 2021.

RM5 is equivalent to 16.67% of RM30, which is a pretty good offer, isn't it?



Friday, October 8, 2021

Malaysia year 2022 national and state public holidays

The official 2022 national and state public holidays in Malaysia has been announced by Cabinet, Constitution and Inter-Government Relation Division (BKPP), Prime Minister Department.

There are 14 days of national holiday (12 national holidays, whereby Chinese New Year and Hari Raya Puasa have 2 days of holiday respectively, and all the rest are 1 day).

1 of them fall on Friday, 1 fall on Saturday, 5 fall on Sunday, and 2 fall on Monday. Most of the states will have around 4 state holidays in addition to the national holidays. 

Kelantan and Terengganu will have 1 additional holiday for Hari Raya Qurban.

Chinese New Year falls on Tuesday and Wednesday (1st and 2nd February 2022). I believe many people will take a few days leave during the period, namely Monday, Thursday and Friday, so that to enjoy a whole week holiday.

Hari Raya Puasa falls on Tuesday and Wednesday too (3rd and 4th May 2022). Good news is, Monday 2nd May 2022 is also a replacement holiday for Labour's Day which falls on Sunday 1st May. Therefore, you can have another whole week holiday by taking leave on Thursday and Friday.

Deepavali falls on Monday (24th October 2022), so you will have a long weekend.

Click here to download the table of Malaysia year 2021 gazetted national and state public holidays.


Thursday, October 7, 2021

PHP: Exporting data table array to Excel xlsx file with PhpSpreadsheet library

In many occasions when a data table is queried from database and/or displayed on the screen of web application, there is a need to enable the user to download the table, preferably as an MS Excel file.

Traditionally, the method of doing so is to export the table as CSV (comma-separated values) or TSV (tab-separated values) text file, and to use MS Excel to import the CSV or TSV into a worksheet.

There are a lot of shortcomings using this method, including but not limited to:

  • You need to open a new webpage in order to perform the file export, which looks weird to the user experience.
  • The separator character might be different for different users. Some uses comma (","), some uses semicolon (";"), some uses vertical bar pipe ("|"), and so on. Libre Calc in LibreOffice is more flexible in handling the separator character during data import, whereas in MS Excel, if the separator character in the file is not the same with your computer's regional format setting, you will probably get rows of long data in single column.
  • The CSV/TSV is in plain text, and no cell formatting can be made.
  • You need to properly escape and handle the data to be exported, to ensure your data export is successful without error.
  • Numeric fields will most probably be treated as number in the worksheet, although you might want to treat them as text string instead. Common occasions including handling of telephone numbers, batch numbers, serial numbers, etc.
Over the years, there is a trick to export a TSV as XLS (old Excel file format), by putting these 2 lines in the page header:

header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");

There is a matured sample source code by Chirp Internet (www.chirpinternet.eu) that includes a function to do data cleansing before the export. You can click here to look for the source code of that script.

However, the exported file is actually just a TSV instead of real XLS file. Newer version of MS Excel will actually prompt out warning message when you try to open such exported XLS file.

A much better and proper method is to use the PhpSpreadsheet library to generate the real MS Excel file in xlsx (Microsoft Open XML Spreadsheet) format.

By using this method, you can export the Excel file without the need of opening a new webpage that confuse the user. You can also perform many Excel file manipulation by using the functions, classes and methods provided by the PhpSpreadsheet library.

The PHP codes below are originally posted by billynoah in Stack Overflow forum.

Step 1: Install PhpSpreadsheet library using composer, if you haven't done so.

If you are using Laragon WAMP platform, you can install the library by opening the Terminal window (the 4th button in Laragon control panel) and type the following command:


composer require phpoffice/phpspreadsheet



Step 2: Add these 3 lines at the beginning of your PHP script.

require '/vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

Step 3: Prepare your dataset in an array. Normally, this is queried from SQL database server using SELECT statement. Note that in order to use the writeXLSX function with source codes in Step 4 below, the dataset needs to be stored in an array instead of an object.

Below is a simple example of such codes, utilizing ezSQL library class:

$db = new ezSQL_mysqli(MY_USR, MY_PWD, MY_DB, MY_HOST);
$sql = "SELECT * FROM ".MY_TABLE;
$items =$db->get_results($sql, $output=ARRAY_A);
$keys = array_keys(current($items));

Step 4: Put the following function somewhere in your script.

function writeXLSX($filename, $rows, $keys = [], $formats = []) {
// instantiate the class
$doc = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $doc->getActiveSheet();

// $keys are for the header row.  If they are supplied we start writing at row 2
if ($keys) {
$offset = 2;
} else {
$offset = 1;
}

// write the rows
$i = 0;
foreach($rows as $row) {
$doc->getActiveSheet()->fromArray($row, null, 'A' . ($i++ + $offset));
}

// write the header row from the $keys
if ($keys) {
$doc->setActiveSheetIndex(0);
$doc->getActiveSheet()->fromArray($keys, null, 'A1');
}

// get last row and column for formatting
$last_column = $doc->getActiveSheet()->getHighestColumn();
$last_row = $doc->getActiveSheet()->getHighestRow();

// if $keys, freeze the header row and make it bold
if ($keys) {
$doc->getActiveSheet()->freezePane('A2');
$doc->getActiveSheet()->getStyle('A1:' . $last_column . '1')->getFont()->setBold(true);
}
// format all columns as text
$doc->getActiveSheet()->getStyle('A2:' . $last_column . $last_row)->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
if ($formats) {
foreach ($formats as $col => $format) {
$doc->getActiveSheet()->getStyle($col . $offset . ':' . $col . $last_row)->getNumberFormat()->setFormatCode($format);
}
}

// enable autofilter
$doc->getActiveSheet()->setAutoFilter($doc->getActiveSheet()->calculateWorksheetDimension());

// autosize all columns to content width
for ($i = 'A'; $i <= $last_column; $i++) {
$doc->getActiveSheet()->getColumnDimension($i)->setAutoSize(TRUE);
}

// write and save the file
$writer = new PhpOffice\PhpSpreadsheet\Writer\Xlsx($doc);
$writer->save($filename);
}

Step 5: Call the writeXLSX function to generate your MS Excel file.

writeXLSX(EXCELFILE, $items, $keys);

That's all about it. Pretty easy and straightforward, isn't it?


Hint: Click on the "Older Posts" link to continue reading, or click here for a listing of all my past 3 months articles.