Răsfoiți Sursa

Cleanup code

Alan Lam 3 ani în urmă
părinte
comite
2647cb5986

+ 1 - 6
controllers/customer.php

@@ -2,16 +2,11 @@
 
 namespace App;
 
-use RuntimeException;
-
 /**
  * @var int $id
  */
 
-$customer = Customer::get($id);
-if(!$customer) {
-	throw new RuntimeException(__("Customer not found."));
-}
+$customer = Customer::require($id);
 
 $core = Core::getInstance();
 $core->addBreadcrumb("/customers", __("Customers"));

+ 2 - 5
controllers/customer_form.php

@@ -1,4 +1,4 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
 
 namespace App;
 
@@ -14,10 +14,7 @@ $core->addBreadcrumb("/customers", __("Customers"));
 $user = $core->getUser();
 
 if(isset($id)) {
-	$customer = Customer::get($id);
-	if(!$customer) {
-		throw new RuntimeException(__("Customer not found."));
-	}
+	$customer = Customer::require($id);
 
 	$customer->dts_updated = Database::dts();
 

+ 4 - 20
controllers/document.php

@@ -77,35 +77,19 @@ $controller = new class($document_type, $document_number) {
 	}
 
 	private function requireCompany(int $id): Company {
-		$company = Company::get($id);
-		if(!$company) {
-			throw new RuntimeException(__("Company not found."));
-		}
-		return $company;
+		return Company::require($id);
 	}
 
 	private function requireLocation(int $id): Location {
-		$location = Location::get($id);
-		if(!$location) {
-			throw new RuntimeException(__("Location not found."));
-		}
-		return $location;
+		return Location::require($id);
 	}
 
 	private function requireTicket(int $id): Ticket {
-		$ticket = Ticket::get($id);
-		if(!$ticket) {
-			throw new RuntimeException(__("Ticket not found."));
-		}
-		return $ticket;
+		return Ticket::require($id);
 	}
 
 	private function requireSupplier(int $id): Supplier {
-		$supplier = Supplier::get($id);
-		if(!$supplier) {
-			throw new RuntimeException(__("Supplier not found."));
-		}
-		return $supplier;
+		return Supplier::require($id);
 	}
 
 	public function view(): void {

+ 1 - 6
controllers/download_file.php

@@ -2,8 +2,6 @@
 
 namespace App;
 
-use RuntimeException;
-
 /**
  * @var string $type
  * @var string $id
@@ -11,10 +9,7 @@ use RuntimeException;
 
 switch($type) {
 	case "ticket-comment-attachment":
-		$attachment = TicketCommentAttachment::get($id);
-		if(!$attachment) {
-			throw new RuntimeException(__("Attachment not found."));
-		}
+		$attachment = TicketCommentAttachment::require($id);
 
 		header("Content-Type: $attachment->mime_type");
 		readfile($attachment->getPath());

+ 1 - 6
controllers/location.php

@@ -2,16 +2,11 @@
 
 namespace App;
 
-use RuntimeException;
-
 /**
  * @var int $id
  */
 
-$location = Location::get($id);
-if(!$location) {
-	throw new RuntimeException(__("Location not found."));
-}
+$location = Location::require($id);
 
 $core = Core::getInstance();
 $core->addBreadcrumb("/locations", __("Locations"));

+ 1 - 10
controllers/location_form.php

@@ -16,12 +16,8 @@ $core->addBreadcrumb("/locations", __("Locations"));
 $user = $core->getUser();
 
 if(isset($id)) {
-	$location = Location::get($id);
-	if(!$location) {
-		throw new RuntimeException(__("Location not found."));
-	}
+	$location = Location::require($id);
 
-	$customer = $location->getCustomer();
 	$location->dts_updated = Database::dts();
 
 	$core->addBreadcrumb($location->getUrl(), $location->name);
@@ -31,11 +27,6 @@ if(isset($id)) {
 else {
 	$customer_id = $request->int("customer_id");
 
-	$customer = Customer::get($customer_id);
-	if(!$customer) {
-		throw new RuntimeException(__("Customer not found."));
-	}
-
 	$location = new Location();
 	$location->customer_id = $customer_id;
 	$location->dts_created = Database::dts();

+ 1 - 4
controllers/product_form.php

@@ -14,10 +14,7 @@ $core->addBreadcrumb("/products", __("Products"));
 $user = $core->getUser();
 
 if(isset($id)) {
-	$product = Product::get($id);
-	if(!$product) {
-		throw new RuntimeException(__("Product not found."));
-	}
+	$product = Product::require($id);
 
 	$product->dts_updated = Database::dts();
 

+ 2 - 9
controllers/sale.php

@@ -66,11 +66,7 @@ $controller = new class($number) {
 	}
 
 	private function requireCompany(?int $company_id): Company {
-		$company = Company::get($company_id);
-		if(!$company) {
-			throw new RuntimeException(__("Company not found."));
-		}
-		return $company;
+		return Company::require($company_id);
 	}
 
 	public function view(): void {
@@ -194,10 +190,7 @@ $controller = new class($number) {
 		}
 
 		$product_id = $this->request->int("product_id");
-		$product = Product::get($product_id);
-		if(!$product) {
-			throw new RuntimeException(__("Product not found."));
-		}
+		$product = Product::require($product_id);
 
 		$item->product_id = $product->id;
 		$item->quantity = $this->request->int("quantity");

+ 1 - 6
controllers/supplier.php

@@ -2,16 +2,11 @@
 
 namespace App;
 
-use RuntimeException;
-
 /**
  * @var int $id
  */
 
-$supplier = Supplier::get($id);
-if(!$supplier) {
-	throw new RuntimeException(__("Supplier not found."));
-}
+$supplier = Supplier::require($id);
 
 $core = Core::getInstance();
 $core->addBreadcrumb("/suppliers", __("Suppliers"));

+ 1 - 4
controllers/supplier_form.php

@@ -15,10 +15,7 @@ $core->addBreadcrumb("/suppliers", __("Suppliers"));
 $user = $core->getUser();
 
 if(isset($id)) {
-	$supplier = Supplier::get($id);
-	if(!$supplier) {
-		throw new RuntimeException(__("Supplier not found."));
-	}
+	$supplier = Supplier::require($id);
 
 	$core->addBreadcrumb($supplier->getUrl(), $supplier->name);
 

+ 1 - 4
controllers/ticket.php

@@ -16,10 +16,7 @@ $core->addBreadcrumb("/tickets", __("Tickets"));
 
 $user = $core->getUser();
 
-$ticket = Ticket::get($id);
-if(!$ticket) {
-	throw new RuntimeException(__("Ticket not found."));
-}
+$ticket = Ticket::require($id);
 
 $core->addBreadcrumb("/tickets/$id", $ticket->subject);
 

+ 1 - 5
controllers/tickets.php

@@ -20,11 +20,7 @@ if($batch_submit) {
 	$ticket_ids = $request->int("ticket_ids");
 	$tickets = [];
 	foreach($ticket_ids as $ticket_id) {
-		$ticket = Ticket::get($ticket_id);
-		if(!$ticket) {
-			throw new RuntimeException(__("Ticket not found."));
-		}
-		$tickets[] = $ticket;
+		$tickets[] = Ticket::require($ticket_id);
 	}
 
 	$assign_to_user = null;

+ 2 - 8
controllers/tickets_ajax.php

@@ -65,16 +65,10 @@ $controller = new class($action) {
 
 		$location = null;
 		if($location_id) {
-			$location = Location::get($location_id);
-			if(!$location) {
-				throw new RuntimeException(__("Location not found."));
-			}
+			$location = Location::require($location_id);
 		}
 		else if($customer_id) {
-			$customer = Customer::get($customer_id);
-			if(!$customer) {
-				throw new RuntimeException(__("Customer not found."));
-			}
+			$customer = Customer::require($customer_id);
 
 			if(!$location_name) {
 				throw new RuntimeException(__("Location name is required."));

+ 1 - 4
controllers/user_form.php

@@ -20,10 +20,7 @@ $core->addBreadcrumb("/users", __("Users"));
 $request = Request::fromRequest();
 
 if(isset($id)) {
-	$user = User::get($id);
-	if(!$user) {
-		throw new RuntimeException(__("User not found."));
-	}
+	$user = User::require($id);
 
 	$user->dts_updated = Database::dts();
 

+ 1 - 1
public/js/sale-item-modal.js

@@ -76,7 +76,7 @@ class SaleItemModal {
 		}
 
 		return $.get(`/suppliers/${supplierId}/get-products`).then(response => {
-			const options = [] = response.products.map(product => {
+			const options = response.products.map(product => {
 				const option = document.createElement("option");
 				option.textContent = product.name;
 				option.value = product.id;