|
|
@@ -3,6 +3,8 @@
|
|
|
namespace App;
|
|
|
|
|
|
use App\Enums\DocumentTypes;
|
|
|
+use Exception;
|
|
|
+use PHPMailer\PHPMailer\PHPMailer;
|
|
|
use RuntimeException;
|
|
|
|
|
|
/**
|
|
|
@@ -413,24 +415,7 @@ $controller = new class($document_type, $document_number) {
|
|
|
$document = $this->requireDocument();
|
|
|
$variant = $this->request->string("variant");
|
|
|
|
|
|
- $host = $_SERVER["HTTP_HOST"];
|
|
|
-
|
|
|
- $render_url = "https://" . $host . $document->getUrl() . "/render-pdf?" . http_build_query([
|
|
|
- "access_token" => $document->getAccessToken(),
|
|
|
- "variant" => $variant
|
|
|
- ]);
|
|
|
-
|
|
|
- chdir($_ENV["DIR_ROOT"]);
|
|
|
-
|
|
|
- $output_filename = $document->id . ".pdf";
|
|
|
- $output_path = "pdf-output/" . $output_filename;
|
|
|
-
|
|
|
- $command = sprintf('node pdf.js "%s" "%s"',
|
|
|
- $render_url,
|
|
|
- $output_filename
|
|
|
- );
|
|
|
-
|
|
|
- shell_exec($command);
|
|
|
+ $output_path = $document->generatePdf($variant);
|
|
|
|
|
|
if(file_exists($output_path)) {
|
|
|
Http::downloadFile($output_path, $document->getPdfFilename(), "application/pdf");
|
|
|
@@ -444,6 +429,68 @@ $controller = new class($document_type, $document_number) {
|
|
|
exit();
|
|
|
}
|
|
|
|
|
|
+ public function emailPdf(): void {
|
|
|
+
|
|
|
+ $document = $this->requireDocument();
|
|
|
+
|
|
|
+ try {
|
|
|
+ if(!$document->contact_email) {
|
|
|
+ throw new RuntimeException(__("Contact Email must be set."));
|
|
|
+ }
|
|
|
+ if(!$document->contact_name) {
|
|
|
+ throw new RuntimeException(__("Contact Name must be set."));
|
|
|
+ }
|
|
|
+
|
|
|
+ $company = $document->getCompany();
|
|
|
+ if(!$company) {
|
|
|
+ throw new RuntimeException(__("Company must be set."));
|
|
|
+ }
|
|
|
+
|
|
|
+ $variant = $this->request->string("variant");
|
|
|
+
|
|
|
+ $pdf_path = $document->generatePdf($variant);
|
|
|
+ if(!file_exists($pdf_path)) {
|
|
|
+ throw new RuntimeException(__("Failed to generate PDF."));
|
|
|
+ }
|
|
|
+
|
|
|
+ $title = $document->getTitle();
|
|
|
+
|
|
|
+ if($document->isSalesOrder()) {
|
|
|
+ if($variant === "commercial") {
|
|
|
+ $title = __("Commercial Invoice %d", $document->number);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $title = __("Proforma Invoice %d", $document->number);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $body = file_get_contents("templates/email/document.html");
|
|
|
+ $search_replace = [
|
|
|
+ "%contact-name%" => $document->contact_name,
|
|
|
+ "%document-title%" => $title,
|
|
|
+ "%company-name%" => $company->name
|
|
|
+ ];
|
|
|
+ $body = str_replace(array_keys($search_replace), array_values($search_replace), $body);
|
|
|
+
|
|
|
+ $mailer = new PHPMailer(true);
|
|
|
+ $mailer->setFrom("no-reply@electrocoin.com", $company->name);
|
|
|
+ $mailer->addAddress($document->contact_email, $document->contact_name);
|
|
|
+ $mailer->CharSet = PHPMailer::CHARSET_UTF8;
|
|
|
+ $mailer->Subject = $title;
|
|
|
+ $mailer->Body = $body;
|
|
|
+ $mailer->AltBody = strip_tags($body);
|
|
|
+ $mailer->addAttachment($pdf_path, $title . ".pdf");
|
|
|
+
|
|
|
+ $mailer->send();
|
|
|
+
|
|
|
+ JSON::success();
|
|
|
+ }
|
|
|
+ catch(Exception $e) {
|
|
|
+ JSON::fail($e->getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
};
|
|
|
|
|
|
switch($action) {
|
|
|
@@ -498,6 +545,10 @@ switch($action) {
|
|
|
$controller->downloadPdf();
|
|
|
break;
|
|
|
|
|
|
+ case "email-pdf":
|
|
|
+ $controller->emailPdf();
|
|
|
+ break;
|
|
|
+
|
|
|
default:
|
|
|
$controller->view();
|
|
|
}
|