From 2cdaed1443c43c58eb789b671c9dce212ce087e5 Mon Sep 17 00:00:00 2001 From: sindhu Date: Fri, 16 Feb 2024 16:52:12 +0700 Subject: [PATCH] step 1 : buat fungsi laporan_pdf_kirim_by_email_user --- pettycash/Report.php | 355 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 282 insertions(+), 73 deletions(-) diff --git a/pettycash/Report.php b/pettycash/Report.php index e57dc00..4b27de4 100644 --- a/pettycash/Report.php +++ b/pettycash/Report.php @@ -1,62 +1,28 @@ db_onedev = $this->load->database("onedev", true); } - function list_total(){ - 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']; - - $debit = 'Rp.'; - $debit_rows = 0 ; - IF($kategoriid !== 0){ - $sql_debit = "SELECT SUM(T_TransactionAmount) as total_debit - FROM t_transaction WHERE T_TransactionType = 'DEBIT' - AND T_TransactionIsActive = 'Y' - AND T_TransactionM_CompanyID = $companyid - AND T_TransactionDate BETWEEN '{$startdate}' AND '{$enddate}' - AND T_TransactionM_CategoryID = $kategoriid"; - $query_debit = $this->db_onedev->query($sql_debit); - //echo $this->db_onedev->last_query(); - if ($query_debit) { - $debit_rows = $query_debit->row()->total_debit; - } else { - $this->sys_error_db("Debit select"); - exit; - } - } - - - $total = 'Rp '.number_format($debit_rows,2,',','.'); - $result = array("total_all"=>$total); - $this->sys_ok($result); - - } catch(Exception $exc) { - $message = $exc->getMessage(); - $this->sys_error($message); - } - } - function list_transaction(){ + function laporan_pdf_kirim_by_email_user(){ try { //# cek token valid /* if (! $this->isLogin) { @@ -67,45 +33,288 @@ class History extends MY_Controller $prm = $this->sys_input; $startdate = $prm['startdate']; $enddate = $prm['enddate']; - $kategoriid = $prm['kategoriid']; + // $kategoriid = $prm['kategoriid']; $companyid = $prm['companyid']; - $sql = "SELECT T_TransactionID as id, - DATE_FORMAT(T_TransactionDate,'%d-%m-%Y') as tanggaltransaksi, - T_TransactionType as tipe, - IFNULL(M_CategoryID,0) as kategoriid, - IFNULL(M_CategoryName,'Saldo') as kategoriname, - T_TransactionNote as note, - CONCAT(REPLACE(CONCAT('Rp ',format(T_TransactionAmount,0)),',','.'),',00') as amount, - T_TransactionSender as sender, - T_TransactionImg as imgurl, - T_TransactionIsConfirm as isconfirm, - IFNULL(DATE_FORMAT(T_TransactionConfirmDate,'%d-%m-%Y'),'-') as tanggalconfirm, - a.M_UserUsername as usertransaksi, - IFNULL(b.M_UserUsername,'') as userconfirm, - IFNULL(DATE_FORMAT(T_TransactionCreated,'%d-%m-%Y %H:%i:%s'),'-') as tanggalcreated - - FROM t_transaction - LEFT JOIN m_user a ON a.M_UserID = T_TransactionUserID - LEFT JOIN m_user b ON b.M_UserID = T_TransactionConfirmUserID - LEFT JOIN m_category ON M_CategoryID = T_TransactionM_CategoryID - WHERE T_TransactionIsActive = 'Y' - AND T_TransactionM_CompanyID = $companyid - AND T_TransactionDate BETWEEN '{$startdate}' AND '{$enddate}' - AND ($kategoriid = 0 OR($kategoriid > 0 AND T_TransactionM_CategoryID = $kategoriid)) - ORDER BY T_TransactionID DESC"; + $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(); + $rows = $query->result_array(); + if(count($rows) > 0){ + $emailUser = $rows[0]['M_UserEmail']; + $username = $rows[0]['M_UserUsername']; + } } else { - $this->sys_error_db("Transaksi select"); + $this->sys_error_db("M_UserEmail select"); exit; } - $this->sys_ok($rows); + 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); } } + + // 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'; + } + } } \ No newline at end of file