step 2 : send excel report to email user

This commit is contained in:
sindhu
2024-02-19 15:23:00 +07:00
parent 2cdaed1443
commit 7efd7dc538

View File

@@ -172,6 +172,165 @@ class Report extends MY_Controller
}
}
function laporan_excel_kirim_by_email_user()
{
try {
//# cek token valid
/* if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
*/
$prm = $this->sys_input;
$startdate = $prm['startdate'];
$enddate = $prm['enddate'];
// $kategoriid = $prm['kategoriid'];
$companyid = $prm['companyid'];
$userid = $prm['userid'];
$namaCompany = "";
$filename = "";
$emailUser = "";
$username = "";
$new_start_date = date("d-m-Y", strtotime($startdate));
$new_end_date = date("d-m-Y", strtotime($enddate));
$current_datetime = date("Y-m-d H:i:s");
$sqlx = "SELECT M_CompanyName
FROM m_company
WHERE M_CompanyID = $companyid
AND M_CompanyIsActive = 'Y'";
$queryx = $this->db_onedev->query($sqlx);
//echo $this->db_onedev->last_query();
if ($queryx) {
$rows = $queryx->result_array();
if(count($rows) > 0){
$namaCompany = $rows[0]['M_CompanyName'];
$filename = "petty-cash-".$namaCompany."-".$new_start_date."-".$new_end_date.".xls";
}
} else {
$this->sys_error_db("M_Company select");
exit;
}
$sql = "SELECT M_UserEmail, M_UserUsername
FROM m_user
WHERE M_UserID = $userid
AND M_UserIsActive = 'Y'";
$query = $this->db_onedev->query($sql);
//echo $this->db_onedev->last_query();
if ($query) {
$rows = $query->result_array();
if(count($rows) > 0){
$emailUser = $rows[0]['M_UserEmail'];
$username = $rows[0]['M_UserUsername'];
}
} else {
$this->sys_error_db("M_UserEmail select");
exit;
}
if($emailUser == ""){
echo json_encode(
[
"status" => "ERR",
"message" => 'Email User Kosong'
]
);
}else{
// proses send email
ini_set('max_execution_time', 300);
// echo date('Y-m-d H:i:s');
// Send Email Start
$mail = new PHPMailer();
// $mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
//
$mail->Username = 'sindhu@sismedika.com';
$mail->Password = 'uhlx gafk oiud fded';
// $mail->Username = 'sas.test.2023@gmail.com';
// // $mail->Password = 'ywsewmhajcweskqk';
// $mail->Password = 'ywse wmha jcwe skqk';
$mail->SMTPSecure = "tls"; // PHPMailer::ENCRYPTION_SMTPS;
//sender information
$mail->setFrom('sas.test.2023@gmail.com', 'Sasana');
//
////receiver email address and name
$mail->addAddress($emailUser, $username);
// standar nama file : petty-cash-nama company-startdate-enddate.pdf
$mail->isHTML(true);
// $mail->Subject = "Laporan Transaksi tanggal ".$new_start_date." s.d ".$new_end_date;
// $mail->Body = "Laporan Transaksi tanggal ".$new_start_date." s.d ".$new_end_date;
$mail->Subject = "Petty Cash Report ".$namaCompany." - ".$new_start_date." s.d ".$new_end_date;
$mail->Body = "Petty Cash Excel Report";
$timestamp = strtotime(date('Y-m-d H:i:s'));
$url = 'http://devone.aplikasi.web.id/birt/run?__report=report/one/pettycash/rpt_r_pt_001.rptdesign&__format=xls&PStartDate='.$startdate.'&PEndDate='.$enddate.'&PCompanyID='.$companyid.'&username=adminsas%20&tm='.$timestamp;
// $url = 'http://devone.aplikasi.web.id/birt/run?__report=report/one/pettycash/rpt_r_pt_001.rptdesign&__format=xls&PStartDate=2023-11-01&PEndDate=2023-12-30&PCompanyID=1&username=adminsas%20&tm=123445';
file_put_contents('/home/one/project/one/tmpPettyCash/'.$filename, file_get_contents($url));
// file_put_contents('/home/one/project/one/tmpPettyCash/1.xls', file_get_contents($url));
// print_r(file_get_contents($url));
$filePath = '/home/one/project/one/tmpPettyCash/'.$filename;
if (file_exists($filePath)) {
// echo "sukses";
$mail->addAttachment('/home/one/project/one/tmpPettyCash/'.$filename);
// Send mail
if (!$mail->send()) {
// echo 'Email not sent an error was encountered: ' . $mail->ErrorInfo;
echo json_encode(
[
"status" => "ERR",
"message" => 'Email not sent an error was encountered: ' . $mail->ErrorInfo
]
);
} else {
unlink('/home/one/project/one/tmpPettyCash/'.$filename);
echo json_encode(
[
"status" => "OK",
"message" => "Laporan Excel berhasil dikirim ke email"
]
);
// echo 'Message has been sent.';
}
//
$mail->smtpClose();
}else{
echo json_encode(
[
"status" => "ERR",
"message" => 'File Upload Gagal Di Generate'
]
);
}
}
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
// test file zip
function test_zip()
{