invoice_edit.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 (permission_exists('invoice_add') || permission_exists('invoice_edit')) {
  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 add or update
  35. if (isset($_REQUEST["id"])) {
  36. $action = "update";
  37. $invoice_uuid = check_str($_REQUEST["id"]);
  38. $back = check_str($_REQUEST['back']);
  39. }
  40. else {
  41. $action = "add";
  42. }
  43. //get http post variables and set them to php variables
  44. if (count($_POST) > 0) {
  45. $invoice_number = check_str($_POST["invoice_number"]);
  46. $invoice_type = check_str($_POST["invoice_type"]);
  47. $contact_uuid_from = check_str($_POST["contact_uuid_from"]);
  48. $contact_uuid_to = check_str($_POST["contact_uuid_to"]);
  49. $invoice_notes = check_str($_POST["invoice_notes"]);
  50. }
  51. if (count($_POST)>0 && strlen($_POST["persistformvar"]) == 0) {
  52. $msg = '';
  53. if ($action == "update") {
  54. $invoice_uuid = check_str($_POST["invoice_uuid"]);
  55. }
  56. //check for all required data
  57. //if (strlen($invoice_uuid) == 0) { $msg .= $text['message-required']." ".$text['label-invoice_uuid']."<br>\n"; }
  58. //if (strlen($domain_uuid) == 0) { $msg .= $text['message-required']." ".$text['label-domain_uuid']."<br>\n"; }
  59. //if (strlen($contact_uuid_from) == 0) { $msg .= $text['message-required']." ".$text['label-contact_uuid_from']."<br>\n"; }
  60. //if (strlen($contact_uuid_to) == 0) { $msg .= $text['message-required']." ".$text['label-contact_uuid_to']."<br>\n"; }
  61. //if (strlen($invoice_number) == 0) { $msg .= $text['message-required']." ".$text['label-invoice_number']."<br>\n"; }
  62. //if (strlen($invoice_date) == 0) { $msg .= $text['message-required']." ".$text['label-invoice_date']."<br>\n"; }
  63. //if (strlen($invoice_notes) == 0) { $msg .= $text['message-required']." ".$text['label-invoice_notes']."<br>\n"; }
  64. if (strlen($msg) > 0 && strlen($_POST["persistformvar"]) == 0) {
  65. require_once "resources/header.php";
  66. require_once "resources/persist_form_var.php";
  67. echo "<div align='center'>\n";
  68. echo "<table><tr><td>\n";
  69. echo $msg."<br />";
  70. echo "</td></tr></table>\n";
  71. persistformvar($_POST);
  72. echo "</div>\n";
  73. require_once "resources/footer.php";
  74. return;
  75. }
  76. //add or update the database
  77. if ($_POST["persistformvar"] != "true") {
  78. if ($action == "add" && permission_exists('invoice_add')) {
  79. $invoice_uuid = uuid();
  80. $sql = "insert into v_invoices ";
  81. $sql .= "(";
  82. $sql .= "domain_uuid, ";
  83. $sql .= "invoice_uuid, ";
  84. $sql .= "invoice_number, ";
  85. $sql .= "invoice_type, ";
  86. $sql .= "contact_uuid_from, ";
  87. $sql .= "contact_uuid_to, ";
  88. $sql .= "invoice_notes, ";
  89. $sql .= "invoice_date ";
  90. $sql .= ")";
  91. $sql .= "values ";
  92. $sql .= "(";
  93. $sql .= "'$domain_uuid', ";
  94. $sql .= "'$invoice_uuid', ";
  95. $sql .= "'$invoice_number', ";
  96. $sql .= "'$invoice_type', ";
  97. $sql .= "'$contact_uuid_from', ";
  98. $sql .= "'$contact_uuid_to', ";
  99. $sql .= "'$invoice_notes', ";
  100. $sql .= "now() ";
  101. $sql .= ")";
  102. $db->exec(check_sql($sql));
  103. unset($sql);
  104. //set redirect
  105. $_SESSION['message'] = $text['message-add'];
  106. header("Location: invoices.php");
  107. exit;
  108. } //if ($action == "add")
  109. if ($action == "update" && permission_exists('invoice_edit')) {
  110. $invoice_paid = check_str($_POST["invoice_paid"]);
  111. if ($invoice_paid == '1') {
  112. $invoice_paid_date = check_str($_POST["invoice_paid_date"]);
  113. $invoice_paid_method = check_str($_POST["invoice_paid_method"]);
  114. $invoice_paid_method_ref = check_str($_POST["invoice_paid_method_ref"]);
  115. }
  116. //set defaults
  117. $invoice_paid = ($invoice_paid != '1') ? 'null' : $invoice_paid;
  118. $invoice_paid_date = ($invoice_paid_date == '') ? 'null' : "'".$invoice_paid_date."'";
  119. $invoice_paid_method = ($invoice_paid_method == '') ? 'null' : "'".$invoice_paid_method."'";
  120. $invoice_paid_method_ref = ($invoice_paid_method_ref == '') ? 'null' : "'".$invoice_paid_method_ref."'";
  121. $sql = "update v_invoices set ";
  122. $sql .= "invoice_number = '$invoice_number', ";
  123. $sql .= "invoice_type = '$invoice_type', ";
  124. $sql .= "contact_uuid_from = '$contact_uuid_from', ";
  125. $sql .= "contact_uuid_to = '$contact_uuid_to', ";
  126. $sql .= "invoice_paid = $invoice_paid, ";
  127. $sql .= "invoice_paid_date = $invoice_paid_date, ";
  128. $sql .= "invoice_paid_method = $invoice_paid_method, ";
  129. $sql .= "invoice_paid_method_ref = $invoice_paid_method_ref, ";
  130. $sql .= "invoice_notes = '$invoice_notes' ";
  131. $sql .= "where domain_uuid = '$domain_uuid' ";
  132. $sql .= "and invoice_uuid = '$invoice_uuid' ";
  133. $db->exec(check_sql($sql));
  134. unset($sql);
  135. //set redirect
  136. $_SESSION['message'] = $text['message-update'];
  137. header("Location: ".(($back != '') ? $back : "invoices.php"));
  138. exit;
  139. } //if ($action == "update")
  140. } //if ($_POST["persistformvar"] != "true")
  141. } //(count($_POST)>0 && strlen($_POST["persistformvar"]) == 0)
  142. //pre-populate the form
  143. if (count($_GET)>0 && $_POST["persistformvar"] != "true") {
  144. $invoice_uuid = check_str($_GET["id"]);
  145. $sql = "select * from v_invoices ";
  146. $sql .= "where domain_uuid = '$domain_uuid' ";
  147. $sql .= "and invoice_uuid = '$invoice_uuid' ";
  148. $prep_statement = $db->prepare(check_sql($sql));
  149. $prep_statement->execute();
  150. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  151. foreach ($result as &$row) {
  152. $invoice_number = $row["invoice_number"];
  153. $invoice_type = $row["invoice_type"];
  154. $invoice_date = $row["invoice_date"];
  155. $invoice_paid = $row["invoice_paid"];
  156. $invoice_paid_date = $row["invoice_paid_date"];
  157. $invoice_paid_method = $row["invoice_paid_method"];
  158. $invoice_paid_method_ref = $row["invoice_paid_method_ref"];
  159. $contact_uuid_from = $row["contact_uuid_from"];
  160. $contact_uuid_to = $row["contact_uuid_to"];
  161. $invoice_notes = $row["invoice_notes"];
  162. break; //limit to 1 row
  163. }
  164. unset ($prep_statement);
  165. //format paid date (if any)
  166. if ($invoice_paid_date != '') {
  167. $tmp = explode(' ',$invoice_paid_date);
  168. $invoice_paid_date = $tmp[0];
  169. }
  170. }
  171. //show the header
  172. require_once "resources/header.php";
  173. //get the default invoice number and contact_uuid_from
  174. if ($action == "add") {
  175. $sql = "select * from v_invoices ";
  176. $sql .= "where domain_uuid = '$domain_uuid' ";
  177. $sql .= "order by invoice_number desc ";
  178. $sql .= "limit 1 ";
  179. $prep_statement = $db->prepare(check_sql($sql));
  180. if ($prep_statement) {
  181. $prep_statement->execute();
  182. $row = $prep_statement->fetch();
  183. $invoice_number = $row['invoice_number'] + 1;
  184. $contact_uuid_from = $row['contact_uuid_from'];
  185. unset ($prep_statement);
  186. }
  187. }
  188. //show the content
  189. echo "<form method='post' name='frm' action=''>\n";
  190. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  191. echo "<tr>\n";
  192. echo "<td align='left' width='30%' valign='top' nowrap='nowrap'><b>".$text['title-invoice']."</b></td>\n";
  193. echo "<td width='70%' align='right' valign='top'>\n";
  194. echo " <input type='button' class='btn' name='' alt='".$text['button-back']."' onclick=\"window.location='".(($back != '') ? $back : "invoices.php")."'\" value='".$text['button-back']."'>\n";
  195. if ($action == "update") {
  196. echo " <input type='button' class='btn' name='' alt='".$text['button-pdf']."' onclick=\"window.open('invoice_pdf.php?id=".$_GET["id"]."&type=' + document.getElementById('invoice_type').options[document.getElementById('invoice_type').selectedIndex].value);\" value='".$text['button-pdf']."'>\n";
  197. }
  198. echo "</td>\n";
  199. echo "</tr>\n";
  200. echo "<tr>\n";
  201. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  202. echo " ".$text['label-invoice_number']."\n";
  203. echo "</td>\n";
  204. echo "<td class='vtable' align='left'>\n";
  205. echo " <input class='formfld' type='text' name='invoice_number' maxlength='255' value='$invoice_number'>\n";
  206. echo "<br />\n";
  207. echo $text['description-invoice_number']."\n";
  208. echo "</td>\n";
  209. echo "</tr>\n";
  210. echo "<tr>\n";
  211. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  212. echo " ".$text['label-invoice_type']."\n";
  213. echo "</td>\n";
  214. echo "<td class='vtable' align='left'>\n";
  215. echo " <select name='invoice_type' id='invoice_type' class='formfld'>\n";
  216. echo " <option value='invoice' ".(($invoice_type == 'invoice') ? "selected" : null).">".$text['label-invoice_type_invoice']."</option>";
  217. echo " <option value='quote' ".(($invoice_type == 'quote') ? "selected" : null).">".$text['label-invoice_type_quote']."</option>";
  218. echo " </select>";
  219. echo "<br />\n";
  220. echo $text['description-invoice_type']."\n";
  221. echo "</td>\n";
  222. echo "</tr>\n";
  223. echo "<tr>\n";
  224. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  225. echo " ".$text['label-contact_uuid_from']."\n";
  226. echo "</td>\n";
  227. echo "<td class='vtable' align='left'>\n";
  228. $sql = "";
  229. $sql .= " select contact_uuid, contact_organization, contact_name_given, contact_name_family from v_contacts ";
  230. $sql .= " where domain_uuid = '$domain_uuid' ";
  231. $sql .= " order by contact_organization asc ";
  232. $prep_statement = $db->prepare(check_sql($sql));
  233. $prep_statement->execute();
  234. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  235. unset ($prep_statement, $sql);
  236. echo "<select name=\"contact_uuid_from\" id=\"contact_uuid_from\" class=\"formfld\">\n";
  237. echo "<option value=\"\"></option>\n";
  238. foreach($result as $row) {
  239. $contact_name = '';
  240. if (strlen($row['contact_organization']) > 0) {
  241. $contact_name = $row['contact_organization'];
  242. }
  243. if (strlen($row['contact_name_family']) > 0) {
  244. if (strlen($contact_name) > 0) { $contact_name .= ", "; }
  245. $contact_name .= $row['contact_name_family'];
  246. }
  247. if (strlen($row['contact_name_given']) > 0) {
  248. if (strlen($contact_name) > 0) { $contact_name .= ", "; }
  249. $contact_name .= $row['contact_name_given'];
  250. }
  251. if ($row['contact_uuid'] == $contact_uuid_from) {
  252. echo "<option value=\"".$row['contact_uuid']."\" selected=\"selected\">".$contact_name." $contact_uuid</option>\n";
  253. }
  254. else {
  255. echo "<option value=\"".$row['contact_uuid']."\">".$contact_name."</option>\n";
  256. }
  257. }
  258. unset($sql, $result, $row_count);
  259. echo "</select>\n";
  260. echo "<br />\n";
  261. echo $text['description-contact_uuid_from']." \n";
  262. echo "<a href='".PROJECT_PATH."/app/contacts/contact_edit.php?id=".$contact_uuid_from."'>".$text['button-view']."</a>\n";
  263. echo "</td>\n";
  264. echo "</tr>\n";
  265. echo "<tr>\n";
  266. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  267. echo " ".$text['label-contact_uuid_to']."\n";
  268. echo "</td>\n";
  269. echo "<td class='vtable' align='left'>\n";
  270. $sql = "";
  271. $sql .= " select contact_uuid, contact_organization, contact_name_given, contact_name_family from v_contacts ";
  272. $sql .= " where domain_uuid = '$domain_uuid' ";
  273. $sql .= " order by contact_organization asc ";
  274. $prep_statement = $db->prepare(check_sql($sql));
  275. $prep_statement->execute();
  276. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  277. unset ($prep_statement, $sql);
  278. echo "<select name=\"contact_uuid_to\" id=\"contact_uuid_to\" class=\"formfld\">\n";
  279. echo "<option value=\"\"></option>\n";
  280. foreach($result as $row) {
  281. $contact_name = '';
  282. if (strlen($row['contact_organization']) > 0) {
  283. $contact_name = $row['contact_organization'];
  284. }
  285. if (strlen($row['contact_name_family']) > 0) {
  286. if (strlen($contact_name) > 0) { $contact_name .= ", "; }
  287. $contact_name .= $row['contact_name_family'];
  288. }
  289. if (strlen($row['contact_name_given']) > 0) {
  290. if (strlen($contact_name) > 0) { $contact_name .= ", "; }
  291. $contact_name .= $row['contact_name_given'];
  292. }
  293. if ($row['contact_uuid'] == $contact_uuid_to) {
  294. echo "<option value=\"".$row['contact_uuid']."\" selected=\"selected\">".$contact_name." $contact_uuid</option>\n";
  295. }
  296. else {
  297. echo "<option value=\"".$row['contact_uuid']."\">".$contact_name."</option>\n";
  298. }
  299. }
  300. unset($sql, $result, $row_count);
  301. echo "</select>\n";
  302. echo "<br />\n";
  303. echo $text['description-contact_uuid_to']." \n";
  304. echo "<a href='".PROJECT_PATH."/app/contacts/contact_edit.php?id=".$contact_uuid_to."'>".$text['button-view']."</a>\n";
  305. echo "</td>\n";
  306. echo "</tr>\n";
  307. if ($action == "update") {
  308. //prepare the invoice date
  309. $invoice_date = date("d", strtotime($invoice_date)).' '.date("M", strtotime($invoice_date)).' '.date("Y", strtotime($invoice_date));
  310. //show the formatted date
  311. echo "<tr>\n";
  312. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  313. echo " ".$text['label-invoice_created']."\n";
  314. echo "</td>\n";
  315. echo "<td class='vtable' align='left'>\n";
  316. echo " ".$invoice_date."\n";
  317. echo "</td>\n";
  318. echo "</tr>\n";
  319. echo "<tr>\n";
  320. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  321. echo " ".$text['label-invoice_paid']."\n";
  322. echo "</td>\n";
  323. echo "<td class='vtable' align='left'>\n";
  324. echo " <table cellpadding='0' cellspacing='0' border='0'>";
  325. echo " <tr>";
  326. echo " <td><input type='checkbox' class='formfld' name='invoice_paid' id='invoice_paid' value='1' ".(($invoice_paid) ? "checked='checked'" : null)." onchange=\"$('#td_paid_details').fadeToggle('fast');\"></td>";
  327. echo " <td id='td_paid_details' style='".((!$invoice_paid) ? "display: none;" : null)." padding: 0px 3px 0px 8px;'>";
  328. echo " <input type='text' class='formfld' style='min-width: 85px; max-width: 85px;' name='invoice_paid_date' data-calendar=\"{format: '%Y-%m-%d', listYears: true, hideOnPick: true, fxName: null, showButtons: true}\" placeholder='Date' value='".$invoice_paid_date."'>";
  329. echo " <select name='invoice_paid_method' id='invoice_paid_method' class='formfld' onchange=\"document.getElementById('invoice_paid_method_ref').focus();\">\n";
  330. echo " <option value=''></option>";
  331. echo " <option value='pp' ".(($invoice_paid_method == 'pp') ? "selected" : null).">".$text['label-invoice_method_paypal']."</option>";
  332. echo " <option value='chk' ".(($invoice_paid_method == 'chk') ? "selected" : null).">".$text['label-invoice_method_check']."</option>";
  333. echo " <option value='cc' ".(($invoice_paid_method == 'cc') ? "selected" : null).">".$text['label-invoice_method_credit_card']."</option>";
  334. echo " <option value='csh' ".(($invoice_paid_method == 'csh') ? "selected" : null).">".$text['label-invoice_method_cash']."</option>";
  335. echo " </select>";
  336. echo " <input type='text' class='formfld' style='min-width: 85px;' name='invoice_paid_method_ref' id='invoice_paid_method_ref' placeholder='Ref #' value='".$invoice_paid_method_ref."'>";
  337. echo " </td>";
  338. echo " </tr>";
  339. echo " </table>";
  340. echo "</td>\n";
  341. echo "</tr>\n";
  342. }
  343. echo "<tr>\n";
  344. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  345. echo " ".$text['label-invoice_notes']."\n";
  346. echo "</td>\n";
  347. echo "<td class='vtable' align='left'>\n";
  348. echo " <textarea class='formfld' type='text' name='invoice_notes'>$invoice_notes</textarea>\n";
  349. echo "<br />\n";
  350. echo $text['description-invoice_notes']."\n";
  351. echo "</td>\n";
  352. echo "</tr>\n";
  353. echo " <tr>\n";
  354. echo " <td colspan='2' align='right'>\n";
  355. if ($action == "update") {
  356. if ($back != '') {
  357. echo " <input type='hidden' name='back' value='".$back."'>";
  358. }
  359. echo " <input type='hidden' name='invoice_uuid' value='$invoice_uuid'>\n";
  360. }
  361. echo " <br><input type='submit' name='submit' class='btn' value='".$text['button-save']."'>\n";
  362. echo " </td>\n";
  363. echo " </tr>";
  364. echo "</table>";
  365. echo "</form>";
  366. if ($action == "update") {
  367. require "invoice_items.php";
  368. }
  369. //include the footer
  370. require_once "resources/footer.php";
  371. ?>