invoice_pdf.php 10 KB

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