Jump to content

Saving invoices to directory and use them when user is downloading invoice from WHMCS


webio

Recommended Posts

Hello,

 

I would like to introduce to you not so nice hack but it looks like he is working as he should. I've done this because I want to keep invoices in PDF files in case when user will change his details or another thing will be changed on invoice. As you know when user changes his details in WHMCS he can download his x-years old invoice which will be updated with new informations.

 

I've added this:

 

if ($status=="Paid") {
if(strpos($_SERVER['PHP_SELF'],"ADMINDIRECTORY") === false) {
	if((strpos($_SERVER['PHP_SELF'],"dl.php") !== false) || (strpos($_SERVER['PHP_SELF'],"dl.html") !== false)) {
		if(!file_exists("./INVOICESDIRECTORY/".str_replace("/", "-", $invoicenum).".pdf")) {
			$pdf->Output("./INVOICESDIRECTORY/".str_replace("/", "-", $invoicenum).".pdf", "F");
		}		
		$fullPath = "./INVOICESDIRECTORY/".str_replace("/", "-", $invoicenum).".pdf";	 
		if ($fd = fopen ($fullPath, "r")) {
			$fsize = filesize($fullPath);
			$path_parts = pathinfo($fullPath);
			$ext = strtolower($path_parts["extension"]);
			switch ($ext) {
				case "pdf":
				header("Content-type: application/pdf"); // add here more headers for diff. extensions
				header("Content-Disposition: attachment; filename=\"".str_replace("-", "/", $path_parts["basename"])."\""); // use 'attachment' to force a download
				break;
				default;
				header("Content-type: application/octet-stream");
				header("Content-Disposition: filename=\"".str_replace("-", "/", $path_parts["basename"])."\"");
			}
			header("Content-length: $fsize");
			header("Cache-control: private"); //use this to open files directly
			while(!feof($fd)) {
				$buffer = fread($fd, 2048);
				echo $buffer;
			}
		}
		fclose ($fd);
		exit;
	}
}
else {
	if(!file_exists("./../INVOICESDIRECTORY/".str_replace("/", "-", $invoicenum).".pdf")) {
		$pdf->Output("./../INVOICESDIRECTORY/".str_replace("/", "-", $invoicenum).".pdf", "F");
	}
}
}

 

just before

 

?>

 

of invoicepdf.tpl file in my template.

 

INVOICESDIRECTORY - directory where I'm keeping PDF invoices

ADMINDIRECTORY - administration directory

 

I'm using invoice custom numbering (xx/mm/yyyy) and that's why I'm replacing characters "/" to "-" when saving and reading files. I'm also not saving unpaid invoices (proformas) because I don't need them.

 

I hope someone will be interested in this and probably update it to make it work better.

 

If client wants to change his details on invoice then he must contact support and someone from support will have to delete PDF invoice from INVOICESDIRECTORY.

 

I think this could be updated to dividing invoices per months and years with proper directory structure which make of this quite good invoice archive.

 

Regards

 

P.S. This works only with downloading invoice from administration or client section. When you click send invoice by email in administration section user will get invocie with details downloaded from DB instead of file. Adding invoice as attachment is out of hook and template hands so I coudln't do anything about it.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated