Files
one-api-pettycash/pettycash/Report.php
2024-02-19 15:23:00 +07:00

479 lines
17 KiB
PHP

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
require FCPATH . "vendor/PHPMailer/src/Exception.php";
require FCPATH . "vendor/PHPMailer/src/PHPMailer.php";
require FCPATH . "vendor/PHPMailer/src/SMTP.php";
class Report extends MY_Controller
{
var $db_onedev;
public function index()
{
echo "BRANCH API";
}
public function __construct()
{
parent::__construct();
// echo "FC PATH : " . FCPATH ;
$this->db_onedev = $this->load->database("onedev", true);
}
function laporan_pdf_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.".pdf";
}
} 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 PDF Report";
$url = 'http://devone.aplikasi.web.id/birt/run?__report=report/one/pettycash/rpt_r_pt_001.rptdesign&__format=pdf&PStartDate='.$startdate.'&PEndDate='.$enddate.'&PCompanyID='.$companyid.'&username=adminsas%20&tm='.date('Y-m-d H:i:s');
file_put_contents('/home/one/project/one/tmpPettyCash/'.$filename, 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 PDF 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);
}
}
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()
{
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'];
$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");
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 = 'azogprophet@gmail.com';
// $mail->Password = 'zdnh ykfv udme lldm';
$mail->SMTPSecure = "tls"; //PHPMailer::ENCRYPTION_SMTPS;
//sender information
$mail->setFrom('azogprophet@gmail.com', 'Azog');
//
////receiver email address and name
$mail->addAddress('albertussindhu15@gmail.com', 'Sindhu');
//
// Add cc or bcc
// $mail->addCC('email@mail.com');
// $mail->addBCC('user@mail.com');
$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 = "Laporan Transaksi tanggal ";
$mail->Body = "Laporan Transaksi tanggal ";
$url = 'http://localhost/birt/run?__report=report/one/pettycash/rpt_r_pt_001.rptdesign&__format=pdf&PStartDate=2023-11-01&PEndDate=2023-12-30&PCompanyID=0&username=adminsas%20&tm=1701327096267';
// $mail->addAttachment('/home/one/project/one/tmpPettyCash/1_compressed.pdf','laporan_pettycash_2023-11-01.pdf');
$mail->addAttachment('/home/one/project/one/tmpPettyCash/contoh_zip.zip','contoh_zip.zip');
// $url = 'https://devone.aplikasi.web.id/tmpPettyCash/1.pdf';
// $attachmentContent = file_get_contents($url);
// Tambahkan file sebagai lampiran
// $mail->addStringAttachment(base64_encode($attachmentContent), '1.pdf');
// Send mail
if (!$mail->send()) {
echo 'Email not sent an error was encountered: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
//
$mail->smtpClose();
echo date('Y-m-d H:i:s');
// if (file_put_contents('/home/one/project/one/tmpPettyCash/1.pdf', file_get_contents($url))) {
// // echo "sukses";
// $mail->addAttachment('/home/one/project/one/tmpPettyCash/1.pdf');
// // Send mail
// if (!$mail->send()) {
// echo 'Email not sent an error was encountered: ' . $mail->ErrorInfo;
// } else {
// echo 'Message has been sent.';
// }
// //
// $mail->smtpClose();
// }else{
// echo "gagal";
// }
// Send Email End
// $this->sys_ok($rows);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function test_zip2()
{
// Nama file PDF yang akan dimasukkan ke dalam zip
$pdfFileName = '/home/one/project/one/tmpPettyCash/1.pdf';
// Nama file zip yang akan dibuat
$zipFileName = '/home/one/project/one/tmpPettyCash/contoh_zip.zip';
// Inisialisasi objek ZipArchive
$zip = new ZipArchive();
if ($zip->open($zipFileName, ZipArchive::CREATE
| ZipArchive::OVERWRITE | ZipArchive::CM_BZIP2) === true) {
// Tambahkan file PDF ke dalam zip
$zip->addFile($pdfFileName);
$zip->setCompressionIndex(0, ZipArchive::CM_STORE);
$zip->setCompressionIndex(1, ZipArchive::CM_DEFLATE);
// Tutup zip
$zip->close();
// Jika file zip berhasil dibuat, echo sukses
// exec("zip -50 -j {$zipFileName} {$pdfFileName}");
echo 'Sukses membuat file zip: ' . $zipFileName;
// // Set header untuk mengirim file zip sebagai respons
// header('Content-Type: application/zip');
// header('Content-disposition: attachment; filename=' . $zipFileName);
// header('Content-Length: ' . filesize($zipFileName));
// // Baca dan kirim isi file zip ke output
// readfile($zipFileName);
// Hapus file zip setelah dikirim
// unlink($zipFileName);
} else {
echo 'Gagal membuat file zip';
}
}
}