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

Allow Quickbooks export of projects from different customers

Alan Lam 7 лет назад
Родитель
Сommit
a10ee139b7

+ 17 - 6
controllers/project_export.php

@@ -11,12 +11,23 @@ else {
 $projects = Project::query(["id" => $ids]);
 if(!$projects) throw new Exception("No projects to export.");
 
-$template_ids = array_column($projects, "template_id");
-$template_ids = array_unique($template_ids);
-if(count($template_ids) != 1) throw new Exception("Cannot create export with different templates.");
+$template_processing = false;
 
-$template = Template::get($template_ids[0]);
-if(!$template) throw new Exception("Template cannot be found.");
+switch($type) {
+	case "admin":
+	case "customer":
+		$template_processing = true;
+		break;
+}
+
+if($template_processing) {
+	$template_ids = array_column($projects, "template_id");
+	$template_ids = array_unique($template_ids);
+	if(count($template_ids) != 1) throw new Exception("Cannot create export with different templates.");
+
+	$template = Template::get($template_ids[0]);
+	if(!$template) throw new Exception("Template cannot be found.");
+}
 
 $tests = Test::query([
 	"project_id" => $ids,
@@ -34,7 +45,7 @@ switch($type) {
 		break;
 
 	case "quickbooks":
-		$template->exportTestsCSV("project_quickbooks_csv.php", $tests);
+		Test::exportCSV("project_quickbooks_csv.php", $tests);
 
 	case "kml":
 		// TODO

+ 20 - 7
controllers/projects.php

@@ -5,13 +5,28 @@ Core::addBreadcrumb("/account/projects", "Projects");
 
 Core::addScript("public/js/app-projects.js");
 
-$customer = Customer::getForAdmin();
+Core::$allow_all_customers = true;
 
-$page_title = "$customer->name Projects";
+$customer_id = http::value("customer_id");
+if(Core::$user->isAdmin() && $customer_id == "ALL") {
 
-Core::addBreadcrumb("/account/projects?customer_id=$customer->id", $page_title);
+	$page_title = "All Projects";
+	Core::addBreadcrumb("/account/projects?customer_id=ALL", $page_title);
 
-$nvp = ["customer_id" => $customer->id];
+	$nvp = [];
+
+	$customer = new Customer;
+	$customer->id = "ALL";
+	$customer->name = "All";
+
+}
+else {
+	$customer = Customer::getForAdmin();
+	$page_title = "$customer->name Projects";
+	Core::addBreadcrumb("/account/projects?customer_id=$customer->id", $page_title);
+
+	$nvp = ["customer_id" => $customer->id];
+}
 
 $id = http::value("id", "int");
 $name = http::value("name", "trim");
@@ -32,10 +47,8 @@ if($year) {
 $projects = Project::query($nvp, ["sort" => "dts_start", "order" => "desc"]);
 $templates = Template::load(array_column($projects, "template_id"));
 
-$filter_count = count($nvp) - 1;
+$filter_count = count($nvp);
 
 View::includeOnce("header", compact("page_title"));
 View::include("projects", compact("customer", "filter_count", "projects", "year"));
 View::includeOnce("footer");
-
-?>

+ 2 - 0
engine/core.php

@@ -17,6 +17,8 @@ class Core {
 	public static $show_alerts = true;
 	public static $show_footer_content = true;
 
+	public static $allow_all_customers = false;
+
 	public static $page = 1;
 	public static $page_count = 1;
 

+ 3 - 6
engine/template.php

@@ -8,6 +8,8 @@
  * @property string $json_file
  * @property bool $practice
  * @property int $pass_stage
+ *
+ * @method Template get(int $id)
  */
 
 class Template extends Model {
@@ -40,15 +42,10 @@ class Template extends Model {
 		});
 
 		// Output
-		header("Content-Type: text/plain");
-		header("Content-Disposition: attachment; filename=\"export.csv\"");
-		header("Cache-Control: private, max-age=0, must-revalidate");
-		header("Pragma: public");
+		HTTP::setDownloadHeaders("export.csv");
 
 		$handle = fopen("php://output", "w");
-
 		require $processing_file_path;
-
 		fclose($handle);
 	}
 

+ 19 - 0
engine/test.php

@@ -114,6 +114,25 @@ class Test extends Model {
 		return "";
 	}
 
+	public static function exportCSV($processing_file, $tests = []) {
+
+		// Preload
+		Project::load(array_column($tests, "project_id"));
+		Location::load(array_column($tests, "location_id"));
+		User::load(array_column($tests, "tester_id"));
+
+		// Get processing file
+		$processing_file_path = ROOT_DIR . "/export_processing/{$processing_file}";
+		if(!file_exists($processing_file_path)) throw new Exception("Processing file not found.");
+
+		// Output
+		HTTP::setDownloadHeaders("export.csv");
+
+		$handle = fopen("php://output", "w");
+		require $processing_file_path;
+		fclose($handle);
+	}
+
 	// Object methods
 	public function sendAssignEmail($message) {
 

+ 1 - 1
template_processing/_common/project_quickbooks_csv.php → export_processing/project_quickbooks_csv.php

@@ -11,7 +11,7 @@ $field_names = [
 	"Terms",
 	"Location",
 	"Memo",
-	"Account",
+	"Tester Fees",
 	"LineDescription",
 	"LineAmount",
 	"LineTaxCode",

+ 12 - 1
views/customer_select.php

@@ -8,6 +8,17 @@
 	$uri_parts = explode("?", $uri);
 	$path = array_shift($uri_parts);
 
+	if(Core::$user->isAdmin() && Core::$allow_all_customers) {
+		$query = $_GET;
+		unset($query["path"]);
+		$query["customer_id"] = "ALL";
+		?>
+		<a class="list-group-item list-group-item-action" href="<?=htmlspecialchars($path)?>?<?=http_build_query($query)?>">
+			<?=__("All")?>
+		</a>
+		<?php
+	}
+
 	foreach($customers as $customer) {
 		$query = $_GET;
 		unset($query["path"]);
@@ -23,4 +34,4 @@
 	?>
 	</div>
 
-</div>
+</div>

+ 2 - 2
views/projects.php

@@ -33,7 +33,7 @@
 			<form method="get">
 				<div class="row">
 					<div class="col-lg">
-						<input type="hidden" name="customer_id" value="<?=http::value("customer_id", "int")?>" />
+						<input type="hidden" name="customer_id" value="<?=htmlspecialchars($customer->id)?>" />
 						<div class="row mb-1">
 							<label class="col-sm-4 col-form-label" for="id">ID</label>
 							<div class="col-sm">
@@ -65,7 +65,7 @@
 						<div class="row">
 							<div class="col-sm offset-sm-4">
 								<button class="btn btn-primary" type="submit">Submit</button>
-								<a class="btn btn-secondary" href="/account/projects?customer_id=<?=$customer->id?>">Clear Filters</a>
+								<a class="btn btn-secondary" href="/account/projects?customer_id=<?=htmlspecialchars($customer->id)?>">Clear Filters</a>
 							</div>
 						</div>
 					</div>