Просмотр исходного кода

Add ability to attach brochures to invoice emails

Alan Lam 3 лет назад
Родитель
Сommit
9c1ac12493

+ 13 - 2
controllers/document.php

@@ -293,6 +293,7 @@ $controller = new class($document_type, $document_number) {
 			$item->dts_created = Database::dts();
 		}
 
+		$item->product_id = $this->request->int("product_id");
 		$item->description = $this->request->string("description");
 		$item->serial_number = $this->request->string("serial_number");
 		$item->quantity = $this->request->int("quantity");
@@ -370,6 +371,7 @@ $controller = new class($document_type, $document_number) {
 		foreach($items as $item) {
 			$document_item = new DocumentItem();
 			$document_item->document_id = $document->id;
+			$document_item->product_id = $item->product_id;
 			$document_item->description = $item->description;
 			$document_item->serial_number = $item->serial_number;
 			$document_item->quantity = $item->quantity;
@@ -432,6 +434,8 @@ $controller = new class($document_type, $document_number) {
 
 	public function emailPdf(): void {
 
+		$include_brochures = $this->request->int("include_brochures");
+
 		$document = $this->requireDocument();
 		$core = Core::getInstance();
 
@@ -494,6 +498,15 @@ $controller = new class($document_type, $document_number) {
 			$mailer->AltBody = strip_tags($body);
 			$mailer->addAttachment($pdf_path, $title . ".pdf");
 
+			if($include_brochures) {
+				foreach($document->getProducts() as $product) {
+					if($product->hasBrochure()) {
+						$attachment_name = $product->name . ".pdf";
+						$mailer->addAttachment($product->getBrochurePath(), $attachment_name);
+					}
+				}
+			}
+
 			$mailer->send();
 
 			Json::success();
@@ -501,9 +514,7 @@ $controller = new class($document_type, $document_number) {
 		catch(Exception $e) {
 			Json::fail($e->getMessage());
 		}
-
 	}
-
 };
 
 switch($action) {

+ 1 - 0
controllers/sale.php

@@ -292,6 +292,7 @@ $controller = new class($number) {
 
 			$item = new DocumentItem();
 			$item->document_id = $document->id;
+			$item->product_id = $product->id;
 			$item->description = $product->name . " ($supplier->name)";
 			$item->quantity = $sale_item->quantity;
 			$item->unit_price = $sale_item->unit_price;

+ 13 - 0
db/migrations/20221230155704_add_product_id_field_to_document_item_table.php

@@ -0,0 +1,13 @@
+<?php
+declare(strict_types=1);
+
+use Phinx\Migration\AbstractMigration;
+
+final class AddProductIdFieldToDocumentItemTable extends AbstractMigration {
+	public function change(): void {
+		$this->table("document_item")
+			->addColumn("product_id", "integer", ["signed" => false, "null" => true, "after" => "document_id"])
+			->addForeignKey("product_id", "product", "id", ["update" => "CASCADE", "delete" => "SET NULL"])
+			->update();
+	}
+}

+ 11 - 0
engine/Constraint/NotNullConstraint.php

@@ -0,0 +1,11 @@
+<?php
+
+namespace App\Constraint;
+
+class NotNullConstraint extends Constraint {
+
+	public function getString(): string {
+		return "IS NOT NULL";
+	}
+
+}

+ 14 - 0
engine/Document.php

@@ -2,6 +2,8 @@
 
 namespace App;
 
+use App\Constraint\Constraint;
+use App\Constraint\NotNullConstraint;
 use App\Enums\AruStatuses;
 use App\Enums\DocumentTypes;
 use App\Enums\SalesOrderStatuses;
@@ -420,4 +422,16 @@ class Document extends Model {
 		]);
 	}
 
+	/**
+	 * @return Product[]
+	 * @throws Exception
+	 */
+	public function getProducts(): array {
+		$products = [];
+		foreach($this->getItems(["product_id" => new NotNullConstraint()]) as $item) {
+			$products[$item->product_id] = $item->getProduct();
+		}
+		return array_values($products);
+	}
+
 }

+ 10 - 1
engine/DocumentItem.php

@@ -10,6 +10,7 @@ use JsonSerializable;
 /**
  * @property int $id
  * @property int $document_id
+ * @property int $product_id
  * @property string $description
  * @property string $serial_number
  * @property int $quantity
@@ -21,7 +22,7 @@ class DocumentItem extends Model implements JsonSerializable {
 	protected static $table = "document_item";
 	protected static $idField = "id";
 	protected static $fillableFields = [
-		"document_id",
+		"document_id", "product_id",
 		"description", "serial_number",
 		"quantity", "unit_price", "insurance_value",
 		"dts_created", "dts_updated", "dts_deleted"
@@ -55,4 +56,12 @@ class DocumentItem extends Model implements JsonSerializable {
 	public function getDocument(): Document {
 		return Document::get($this->document_id);
 	}
+
+	/**
+	 * @return Product|null
+	 * @throws Exception
+	 */
+	public function getProduct(): ?Product {
+		return Product::get($this->product_id);
+	}
 }

+ 8 - 2
public/js/document-page.js

@@ -82,15 +82,21 @@ class DocumentPage {
 				const $p1 = $("<p>").text("Are you sure you want to email the PDF to:");
 				const $p2 = $("<div>").text(`${contactName} <${contactEmail}>`);
 
+				const checked = (variant === "commercial") ? 0 : 1;
+
 				swal.fire({
 					icon: "question",
 					title: "Emailing PDF",
 					html: $p1.get(0).outerHTML + $p2.get(0).outerHTML,
 					confirmButtonText: "Send Email",
 					showCancelButton: true,
-					preConfirm: () => {
+					input: "checkbox",
+					inputValue: checked,
+					inputPlaceholder: "Attach brochures",
+					preConfirm: value => {
 						return $.post(window.location + "/email-pdf", {
-							variant: variant
+							variant: variant,
+							include_brochures: value
 						}).then(() => {
 							toastr.success("Email sent successfully.");
 						}).catch(error => {

+ 15 - 1
views/document_pdf.php

@@ -291,10 +291,24 @@ $is_part_of_aru = $document->isPartOfAru();
 			$items = $document->getItems();
 			$subtotal = "0.00";
 			foreach($items as $item) {
+				$product = $item->getProduct();
 				?>
 				<tr>
 					<td>
-						<?=htmlspecialchars($item->description)?>
+						<?php
+						if($product && $product->url) {
+							?>
+							<a href="<?=htmlspecialchars($product->url)?>">
+								<?=htmlspecialchars($item->description)?>
+							</a>
+							<?php
+						}
+						else {
+							?>
+							<?=htmlspecialchars($item->description)?>
+							<?php
+						}
+						?>
 					</td>
 					<td class="font-monospace">
 						<?=htmlspecialchars($item->serial_number)?>