v_invoice_pdf.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2012
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. require_once "root.php";
  22. require_once "includes/config.php";
  23. require_once "includes/checkauth.php";
  24. if (ifgroup("admin") || ifgroup("superadmin")) {
  25. //access granted
  26. }
  27. else {
  28. echo "access denied";
  29. exit;
  30. }
  31. //action invoice_uuid
  32. if (isset($_REQUEST["id"])) {
  33. $invoice_uuid = check_str($_REQUEST["id"]);
  34. }
  35. //get the invoice details
  36. $sql = "";
  37. $sql .= "select * from v_invoices ";
  38. $sql .= "where domain_uuid = '$domain_uuid' ";
  39. $sql .= "and invoice_uuid = '$invoice_uuid' ";
  40. $sql .= "order by invoice_uuid desc ";
  41. $sql .= "limit 1 ";
  42. $prep_statement = $db->prepare(check_sql($sql));
  43. if ($prep_statement) {
  44. $prep_statement->execute();
  45. $row = $prep_statement->fetch();
  46. $invoice_number = $row['invoice_number'];
  47. $contact_uuid_from = $row['contact_uuid_from'];
  48. $contact_uuid_to = $row['contact_uuid_to'];
  49. $invoice_date = $row['invoice_date'];
  50. $invoice_note = $row['invoice_note'];
  51. unset ($prep_statement);
  52. }
  53. //prepare the invoice date
  54. $invoice_date = date("d", strtotime($invoice_date)).' '.date("M", strtotime($invoice_date)).' '.date("Y", strtotime($invoice_date));
  55. //prepare to use fpdf
  56. define('FPDF_FONTPATH',$_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/includes/fpdf/font/');
  57. require('includes/fpdf/fpdf.php');
  58. //create the fpdf object and add the first page
  59. $pdf = new FPDF();
  60. $pdf->AddPage();
  61. $pdf->SetFont('Arial','B',9);
  62. //get contact from name
  63. $sql = "";
  64. $sql .= "select * from v_contacts ";
  65. $sql .= "where domain_uuid = '$domain_uuid' ";
  66. $sql .= "and contact_uuid = '$contact_uuid_from' ";
  67. $prep_statement = $db->prepare(check_sql($sql));
  68. $prep_statement->execute();
  69. $result = $prep_statement->fetchAll();
  70. foreach ($result as &$row) {
  71. $from_org = $row["org"];
  72. $from_n_given = $row["n_given"];
  73. $from_n_family = $row["n_family"];
  74. break; //limit to 1 row
  75. }
  76. unset ($prep_statement);
  77. //get contact from address
  78. $sql = "";
  79. $sql .= "select * from v_contacts_adr ";
  80. $sql .= "where domain_uuid = '$domain_uuid' ";
  81. $sql .= "and contact_uuid = '$contact_uuid_from' ";
  82. $prep_statement = $db->prepare(check_sql($sql));
  83. $prep_statement->execute();
  84. $result = $prep_statement->fetchAll();
  85. foreach ($result as &$row) {
  86. $from_adr_type = $row["adr_type"];
  87. $from_adr_street = $row["adr_street"];
  88. $from_adr_extended = $row["adr_extended"];
  89. $from_adr_locality = $row["adr_locality"];
  90. $from_adr_region = $row["adr_region"];
  91. $from_adr_postal_code = $row["adr_postal_code"];
  92. $from_adr_country = $row["adr_country"];
  93. break; //limit to 1 row
  94. }
  95. unset ($prep_statement);
  96. $pdf->SetY(10);
  97. $pdf->SetFont('Arial','B',9);
  98. if (strlen($from_org) > 0) {
  99. $pdf->Cell(40,5,$from_org);
  100. $pdf->Ln();
  101. }
  102. else {
  103. if (strlen($from_n_given.$from_n_family) > 0) {
  104. $pdf->Cell(40,5,$from_n_given.' '.$from_n_family);
  105. $pdf->Ln();
  106. }
  107. }
  108. $pdf->SetFont('Arial','',9);
  109. $pdf->Cell(40,5,$from_adr_street.' '.$from_adr_extended);
  110. $pdf->Ln();
  111. $pdf->Cell(40,5,$from_adr_locality.', '.$from_adr_region.' '.$from_adr_country.' '.$from_adr_postal_code);
  112. $pdf->Ln();
  113. $pdf->Ln();
  114. //get contact to name
  115. $sql = "";
  116. $sql .= "select * from v_contacts ";
  117. $sql .= "where domain_uuid = '$domain_uuid' ";
  118. $sql .= "and contact_uuid = '$contact_uuid_to' ";
  119. $prep_statement = $db->prepare(check_sql($sql));
  120. $prep_statement->execute();
  121. $result = $prep_statement->fetchAll();
  122. foreach ($result as &$row) {
  123. $to_org = $row["org"];
  124. $to_n_given = $row["n_given"];
  125. $to_n_family = $row["n_family"];
  126. break; //limit to 1 row
  127. }
  128. unset ($prep_statement);
  129. //get contact to address
  130. $sql = "";
  131. $sql .= "select * from v_contacts_adr ";
  132. $sql .= "where domain_uuid = '$domain_uuid' ";
  133. $sql .= "and contact_uuid = '$contact_uuid_to' ";
  134. $prep_statement = $db->prepare(check_sql($sql));
  135. $prep_statement->execute();
  136. $result = $prep_statement->fetchAll();
  137. foreach ($result as &$row) {
  138. $to_adr_type = $row["adr_type"];
  139. $to_adr_street = $row["adr_street"];
  140. $to_adr_extended = $row["adr_extended"];
  141. $to_adr_locality = $row["adr_locality"];
  142. $to_adr_region = $row["adr_region"];
  143. $to_adr_postal_code = $row["adr_postal_code"];
  144. $to_adr_country = $row["adr_country"];
  145. break; //limit to 1 row
  146. }
  147. unset ($prep_statement);
  148. $pdf->SetY(40);
  149. $pdf->SetFont('Arial','B',9);
  150. if (strlen($to_org) > 0) {
  151. $pdf->Cell(40,5,$to_org);
  152. $pdf->Ln();
  153. }
  154. else {
  155. if (strlen($to_n_given.$to_n_family) > 0) {
  156. $pdf->Cell(40,5,$to_n_given.' '.$to_n_family);
  157. $pdf->Ln();
  158. }
  159. }
  160. $pdf->SetFont('Arial','',9);
  161. $pdf->Cell(40,5,$to_adr_street.' '.$to_adr_extended);
  162. $pdf->Ln();
  163. $pdf->Cell(40,5,$to_adr_locality.', '.$to_adr_region.' '.$to_adr_country.' '.$to_adr_postal_code);
  164. $pdf->Ln();
  165. $pdf->Ln();
  166. $pdf->Ln();
  167. //invoice info
  168. $pdf->SetY(10);
  169. $pdf->Cell(150,10,'');
  170. $pdf->SetFont('Arial','',23);
  171. $pdf->Cell(40,10,"INVOICE");
  172. $pdf->Ln();
  173. $pdf->SetFont('Arial','',9);
  174. $pdf->Cell(150,5,'');
  175. $pdf->Cell(40,5,'Invoice Date: '.$invoice_date);
  176. $pdf->Ln();
  177. $pdf->Cell(150,5,'');
  178. $pdf->Cell(40,5,'Invoice Number: '.$invoice_number);
  179. $pdf->Ln();
  180. $pdf->Ln();
  181. $pdf->Ln();
  182. //set the vertical position
  183. $pdf->SetY(65);
  184. //table headers array
  185. $header = array('Qty', 'Description', 'Unit Price', 'Amount');
  186. //set the table header styles
  187. $pdf->SetFillColor(200,200,200);
  188. //$pdf->SetTextColor(255);
  189. $pdf->SetDrawColor(220,220,220);
  190. $pdf->SetLineWidth(0.3);
  191. //set the table cell widths
  192. $w[0] = 20;
  193. $w[1] = 120;
  194. $w[2] = 25;
  195. $w[3] = 25;
  196. for($i=0;$i<count($header);$i++) {
  197. if ($header[$i] == "Description") {
  198. //left align
  199. $pdf->Cell($w[$i],5,$header[$i],1,0,'L',true);
  200. }
  201. else {
  202. //center
  203. $pdf->Cell($w[$i],5,$header[$i],1,0,'C',true);
  204. }
  205. }
  206. $pdf->Ln();
  207. //set the text and background color
  208. $pdf->SetFillColor(224,235,255);
  209. $pdf->SetTextColor(0);
  210. $pdf->SetFont('Arial','',9);
  211. //itemized list
  212. $sql = "";
  213. $sql .= "select * from v_invoice_items ";
  214. $sql .= "where domain_uuid = '$domain_uuid' ";
  215. $sql .= "and invoice_uuid = '$invoice_uuid' ";
  216. $prep_statement = $db->prepare(check_sql($sql));
  217. $prep_statement->execute();
  218. $result = $prep_statement->fetchAll();
  219. $fill = false;
  220. $total = 0;
  221. foreach ($result as &$row) {
  222. $item_qty = $row["item_qty"];
  223. $item_desc = $row["item_desc"];
  224. //$item_desc = str_replace("\n", "<br />", $item_desc);
  225. $item_desc = wordwrap($item_desc, 70, "\n");
  226. $item_unit_price = $row["item_unit_price"];
  227. $item_sub_total = $item_qty * $item_unit_price;
  228. $item_desc_array = explode ("\n", $item_desc);
  229. $x = 0;
  230. foreach ($item_desc_array as $line) {
  231. //quantity
  232. if ($x == 0) {
  233. $pdf->Cell($w[0],6,$item_qty,'LR',0,'C',$fill);
  234. }
  235. else {
  236. $pdf->Cell($w[0],6," ",'LR',0,'C',$fill);
  237. }
  238. //description
  239. $pdf->Cell($w[1],6,$line,'LR',0,'L',$fill);
  240. //unit price
  241. if ($x == 0) {
  242. $pdf->Cell($w[2],6,$item_unit_price,'LR',0,'R',$fill);
  243. }
  244. else {
  245. $pdf->Cell($w[2],6," ",'LR',0,'R',$fill);
  246. }
  247. //amount
  248. if ($x == 0) {
  249. $pdf->Cell($w[3],6,number_format($item_sub_total,2),'LR',0,'R',$fill);
  250. }
  251. else {
  252. $pdf->Cell($w[3],6," ",'LR',0,'R',$fill);
  253. }
  254. //line feed
  255. $pdf->Ln(6);
  256. $x++;
  257. }
  258. //line seperator
  259. //$pdf->Cell(($w[0]+$w[1]+$w[2]+$w[3]),0.3," ",'TBRL',1,'R',$fill);
  260. //alternate the fill
  261. if ($fill) {
  262. $fill = false;
  263. }
  264. else {
  265. $fill = true;
  266. }
  267. //sub total
  268. $total = $total + $item_sub_total;
  269. }
  270. unset ($prep_statement);
  271. //line seperator
  272. $pdf->Cell(($w[0]+$w[1]+$w[2]+$w[3]),0.1," ",'TBRL',1,'R',$fill);
  273. //show the total
  274. $pdf->Ln();
  275. $pdf->SetFont('Arial','B',9);
  276. $pdf->Cell($w[0],6,'','',0,'L','');
  277. $pdf->Cell($w[1],6,'','',0,'L','');
  278. $pdf->Cell($w[2],6,'','',0,'R','');
  279. $pdf->Cell($w[3],6,'Total: '.number_format($total,2),'',0,'R','');
  280. $pdf->Ln();
  281. if (strlen($invoice_note) > 0) {
  282. $pdf->SetFont('Arial','B',9);
  283. $pdf->Cell($w[0],6,'Notes','',0,'L',$fill);
  284. $pdf->Ln();
  285. $pdf->Cell($w[0],6,''.$invoice_note,'',0,'L',$fill);
  286. $pdf->Ln();
  287. }
  288. //closing line
  289. //$pdf->Cell(array_sum($w),0,'','T');
  290. //show the pdf
  291. $pdf->Output();
  292. ?>