invoice_pdf.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 "resources/require.php";
  23. require_once "resources/check_auth.php";
  24. if (if_group("admin") || if_group("superadmin")) {
  25. //access granted
  26. }
  27. else {
  28. echo "access denied";
  29. exit;
  30. }
  31. //add multi-lingual support
  32. $language = new text;
  33. $text = $language->get();
  34. //action invoice_uuid
  35. if (isset($_REQUEST["id"])) {
  36. $invoice_uuid = check_str($_REQUEST["id"]);
  37. $type = check_str($_REQUEST["type"]);
  38. }
  39. //get the invoice details
  40. $sql = "select * from v_invoices ";
  41. $sql .= "where domain_uuid = '$domain_uuid' ";
  42. $sql .= "and invoice_uuid = '$invoice_uuid' ";
  43. $sql .= "order by invoice_uuid desc ";
  44. $sql .= "limit 1 ";
  45. $prep_statement = $db->prepare(check_sql($sql));
  46. if ($prep_statement) {
  47. $prep_statement->execute();
  48. $row = $prep_statement->fetch();
  49. $invoice_number = $row['invoice_number'];
  50. $contact_uuid_from = $row['contact_uuid_from'];
  51. $contact_uuid_to = $row['contact_uuid_to'];
  52. $invoice_date = $row['invoice_date'];
  53. $invoice_note = $row['invoice_note'];
  54. unset ($prep_statement);
  55. }
  56. //prepare the invoice date
  57. $invoice_date = date("d", strtotime($invoice_date)).' '.date("M", strtotime($invoice_date)).' '.date("Y", strtotime($invoice_date));
  58. //prepare to use fpdf
  59. define('FPDF_FONTPATH',$_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/resources/fpdf/font/');
  60. require('resources/fpdf/fpdf.php');
  61. //create the fpdf object and add the first page
  62. $pdf = new FPDF();
  63. $pdf->AddPage();
  64. $pdf->SetFont('Arial','B',9);
  65. //get contact from name
  66. $sql = "select * from v_contacts ";
  67. $sql .= "where domain_uuid = '$domain_uuid' ";
  68. $sql .= "and contact_uuid = '$contact_uuid_from' ";
  69. $prep_statement = $db->prepare(check_sql($sql));
  70. $prep_statement->execute();
  71. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  72. foreach ($result as &$row) {
  73. $from_contact_organization = $row["contact_organization"];
  74. $from_contact_name_given = $row["contact_name_given"];
  75. $from_contact_name_family = $row["contact_name_family"];
  76. break; //limit to 1 row
  77. }
  78. unset ($prep_statement);
  79. //get contact from address
  80. $sql = "select * from v_contact_addresses ";
  81. $sql .= "where domain_uuid = '$domain_uuid' ";
  82. $sql .= "and contact_uuid = '$contact_uuid_from' ";
  83. $prep_statement = $db->prepare(check_sql($sql));
  84. $prep_statement->execute();
  85. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  86. foreach ($result as &$row) {
  87. $from_address_type = $row["address_type"];
  88. $from_address_street = $row["address_street"];
  89. $from_address_extended = $row["address_extended"];
  90. $from_address_locality = $row["address_locality"];
  91. $from_address_region = $row["address_region"];
  92. $from_address_postal_code = $row["address_postal_code"];
  93. $from_address_country = $row["address_country"];
  94. break; //limit to 1 row
  95. }
  96. unset ($prep_statement);
  97. $pdf->SetY(10);
  98. $pdf->SetFont('Arial','B',9);
  99. if (strlen($from_contact_organization) > 0) {
  100. $pdf->Cell(40,5,$from_contact_organization);
  101. $pdf->Ln();
  102. }
  103. else {
  104. if (strlen($from_contact_name_given.$from_contact_name_family) > 0) {
  105. $pdf->Cell(40,5,$from_contact_name_given.' '.$from_contact_name_family);
  106. $pdf->Ln();
  107. }
  108. }
  109. $pdf->SetFont('Arial','',9);
  110. $pdf->Cell(40,5,$from_address_street.' '.$from_address_extended);
  111. $pdf->Ln();
  112. $pdf->Cell(40,5,$from_address_locality.', '.$from_address_region.' '.$from_address_country.' '.$from_address_postal_code);
  113. $pdf->Ln();
  114. $pdf->Ln();
  115. //get contact to name
  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(PDO::FETCH_NAMED);
  122. foreach ($result as &$row) {
  123. $to_contact_organization = $row["contact_organization"];
  124. $to_contact_name_given = $row["contact_name_given"];
  125. $to_contact_name_family = $row["contact_name_family"];
  126. }
  127. unset ($prep_statement);
  128. //get contact to address
  129. $sql = "select * from v_contact_addresses ";
  130. $sql .= "where domain_uuid = '$domain_uuid' ";
  131. $sql .= "and contact_uuid = '$contact_uuid_to' ";
  132. $prep_statement = $db->prepare(check_sql($sql));
  133. $prep_statement->execute();
  134. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  135. foreach ($result as &$row) {
  136. $to_address_type = $row["address_type"];
  137. $to_address_street = $row["address_street"];
  138. $to_address_extended = $row["address_extended"];
  139. $to_address_locality = $row["address_locality"];
  140. $to_address_region = $row["address_region"];
  141. $to_address_postal_code = $row["address_postal_code"];
  142. $to_address_country = $row["address_country"];
  143. $to_address_description = $row["address_description"];
  144. break; //limit to 1 row
  145. }
  146. unset ($prep_statement);
  147. $pdf->SetY(40);
  148. $pdf->SetFont('Arial','B',9);
  149. if (strlen($to_contact_organization) > 0) {
  150. $pdf->Cell(40,5,$to_contact_organization);
  151. $pdf->Ln();
  152. }
  153. else {
  154. if (strlen($to_contact_name_given.$to_contact_name_family) > 0) {
  155. $pdf->Cell(40,5,$to_contact_name_given.' '.$to_contact_name_family);
  156. $pdf->Ln();
  157. }
  158. }
  159. $pdf->SetFont('Arial','',9);
  160. $pdf->Cell(40,5,$to_address_street.' '.$to_address_extended);
  161. $pdf->Ln();
  162. if (strtolower($to_address_country) == 'portugal') {
  163. $pdf->Cell(40,5,$to_address_postal_code.' '.$to_address_locality.' '.$to_address_region.' '.$to_address_country);
  164. }
  165. else {
  166. $pdf->Cell(40,5,$to_address_locality.', '.$to_address_region.' '.$to_address_country.' '.$to_address_postal_code);
  167. }
  168. $pdf->Ln();
  169. $pdf->Cell(40,5,$to_address_description);
  170. $pdf->Ln();
  171. $pdf->Ln();
  172. $pdf->Ln();
  173. //invoice info
  174. $pdf->SetY(10);
  175. $pdf->Cell(150,10,'');
  176. $pdf->SetFont('Arial','',23);
  177. if ($type == "quote") {
  178. $pdf->Cell(40,10,$text['label-quote']);
  179. }
  180. else {
  181. $pdf->Cell(40,10,$text['label-invoice']);
  182. }
  183. $pdf->Ln();
  184. $pdf->SetFont('Arial','',9);
  185. $pdf->Cell(150,5,'');
  186. $pdf->Cell(40,5,$text['label-invoice_date'].' '.$invoice_date);
  187. $pdf->Ln();
  188. $pdf->Cell(150,5,'');
  189. $pdf->Cell(40,5,$text['label-invoice_number'].' '.$invoice_number);
  190. $pdf->Ln();
  191. $pdf->Ln();
  192. $pdf->Ln();
  193. //set the vertical position
  194. $pdf->SetY(65);
  195. //table headers array
  196. $header = array($text['label-item_qty'], $text['label-item_desc'], $text['label-item_unit_price'], $text['label-item_amount']);
  197. //set the table header styles
  198. $pdf->SetFillColor(200,200,200);
  199. //$pdf->SetTextColor(255);
  200. $pdf->SetDrawColor(220,220,220);
  201. $pdf->SetLineWidth(0.3);
  202. //set the table cell widths
  203. $w[0] = 20;
  204. $w[1] = 120;
  205. $w[2] = 25;
  206. $w[3] = 25;
  207. for($i=0;$i<count($header);$i++) {
  208. if ($header[$i] == $text['label-item_desc']) {
  209. //left align
  210. $pdf->Cell($w[$i],5,$header[$i],1,0,'L',true);
  211. }
  212. else {
  213. //center
  214. $pdf->Cell($w[$i],5,$header[$i],1,0,'C',true);
  215. }
  216. }
  217. $pdf->Ln();
  218. //set the text and background color
  219. $pdf->SetFillColor(224,235,255);
  220. $pdf->SetTextColor(0);
  221. $pdf->SetFont('Arial','',9);
  222. //itemized list
  223. $sql = "select * from v_invoice_items ";
  224. $sql .= "where domain_uuid = '$domain_uuid' ";
  225. $sql .= "and invoice_uuid = '$invoice_uuid' ";
  226. $prep_statement = $db->prepare(check_sql($sql));
  227. $prep_statement->execute();
  228. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  229. $fill = false;
  230. $total = 0;
  231. foreach ($result as &$row) {
  232. $item_qty = $row["item_qty"];
  233. $item_desc = $row["item_desc"];
  234. //$item_desc = str_replace("\n", "<br />", $item_desc);
  235. $item_desc = wordwrap($item_desc, 70, "\n");
  236. $item_unit_price = $row["item_unit_price"];
  237. $item_sub_total = $item_qty * $item_unit_price;
  238. $item_desc_array = explode ("\n", $item_desc);
  239. $x = 0;
  240. foreach ($item_desc_array as $line) {
  241. //quantity
  242. if ($x == 0) {
  243. $pdf->Cell($w[0],6,$item_qty,'LR',0,'C',$fill);
  244. }
  245. else {
  246. $pdf->Cell($w[0],6," ",'LR',0,'C',$fill);
  247. }
  248. //description
  249. $pdf->Cell($w[1],6,$line,'LR',0,'L',$fill);
  250. //unit price
  251. if ($x == 0) {
  252. $pdf->Cell($w[2],6,$item_unit_price,'LR',0,'R',$fill);
  253. }
  254. else {
  255. $pdf->Cell($w[2],6," ",'LR',0,'R',$fill);
  256. }
  257. //amount
  258. if ($x == 0) {
  259. $pdf->Cell($w[3],6,number_format($item_sub_total,2),'LR',0,'R',$fill);
  260. }
  261. else {
  262. $pdf->Cell($w[3],6," ",'LR',0,'R',$fill);
  263. }
  264. //line feed
  265. $pdf->Ln(6);
  266. $x++;
  267. }
  268. //line seperator
  269. //$pdf->Cell(($w[0]+$w[1]+$w[2]+$w[3]),0.3," ",'TBRL',1,'R',$fill);
  270. //alternate the fill
  271. if ($fill) {
  272. $fill = false;
  273. }
  274. else {
  275. $fill = true;
  276. }
  277. //sub total
  278. $total = $total + $item_sub_total;
  279. }
  280. unset ($prep_statement);
  281. //line seperator
  282. $pdf->Cell(($w[0]+$w[1]+$w[2]+$w[3]),0.1," ",'TBRL',1,'R',$fill);
  283. //show the total
  284. $pdf->Ln();
  285. $pdf->SetFont('Arial','B',9);
  286. $pdf->Cell($w[0],6,'','',0,'L','');
  287. $pdf->Cell($w[1],6,'','',0,'L','');
  288. $pdf->Cell($w[2],6,'','',0,'R','');
  289. $pdf->Cell($w[3],6,$text['label-invoice_total'].' $'.number_format($total,2).' USD','',0,'R','');
  290. $pdf->Ln();
  291. if (strlen($invoice_note) > 0) {
  292. $pdf->SetFont('Arial','B',9);
  293. $pdf->Cell($w[0],6,$text['label-invoice_notes'],'',0,'L',$fill);
  294. $pdf->Ln();
  295. $pdf->Cell($w[0],6,''.$invoice_note,'',0,'L',$fill);
  296. $pdf->Ln();
  297. }
  298. //closing line
  299. //$pdf->Cell(array_sum($w),0,'','T');
  300. //show the pdf
  301. $pdf->Output();
  302. ?>