v_invoices_edit.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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/require.php";
  23. require_once "includes/checkauth.php";
  24. if (if_group("admin") || if_group("superadmin")) {
  25. //access granted
  26. }
  27. else {
  28. echo "access denied";
  29. exit;
  30. }
  31. //action add or update
  32. if (isset($_REQUEST["id"])) {
  33. $action = "update";
  34. $invoice_uuid = check_str($_REQUEST["id"]);
  35. }
  36. else {
  37. $action = "add";
  38. }
  39. //get http post variables and set them to php variables
  40. if (count($_POST)>0) {
  41. $invoice_number = check_str($_POST["invoice_number"]);
  42. $contact_uuid_from = check_str($_POST["contact_uuid_from"]);
  43. $contact_uuid_to = check_str($_POST["contact_uuid_to"]);
  44. $invoice_notes = check_str($_POST["invoice_notes"]);
  45. }
  46. if (count($_POST)>0 && strlen($_POST["persistformvar"]) == 0) {
  47. $msg = '';
  48. if ($action == "update") {
  49. $invoice_uuid = check_str($_POST["invoice_uuid"]);
  50. }
  51. //check for all required data
  52. //if (strlen($invoice_number) == 0) { $msg .= "Please provide: Invoice Number<br>\n"; }
  53. //if (strlen($invoice_date) == 0) { $msg .= "Please provide: Date<br>\n"; }
  54. if (strlen($msg) > 0 && strlen($_POST["persistformvar"]) == 0) {
  55. require_once "includes/header.php";
  56. require_once "includes/persistformvar.php";
  57. echo "<div align='center'>\n";
  58. echo "<table><tr><td>\n";
  59. echo $msg."<br />";
  60. echo "</td></tr></table>\n";
  61. persistformvar($_POST);
  62. echo "</div>\n";
  63. require_once "includes/footer.php";
  64. return;
  65. }
  66. //add or update the database
  67. if ($_POST["persistformvar"] != "true") {
  68. if ($action == "add") {
  69. $invoice_uuid = uuid();
  70. $sql = "insert into v_invoices ";
  71. $sql .= "(";
  72. $sql .= "domain_uuid, ";
  73. $sql .= "invoice_uuid, ";
  74. $sql .= "invoice_number, ";
  75. $sql .= "contact_uuid_from, ";
  76. $sql .= "contact_uuid_to, ";
  77. $sql .= "invoice_notes, ";
  78. $sql .= "invoice_date ";
  79. $sql .= ")";
  80. $sql .= "values ";
  81. $sql .= "(";
  82. $sql .= "'$domain_uuid', ";
  83. $sql .= "'$invoice_uuid', ";
  84. $sql .= "'$invoice_number', ";
  85. $sql .= "'$contact_uuid_from', ";
  86. $sql .= "'$contact_uuid_to', ";
  87. $sql .= "'$invoice_notes', ";
  88. $sql .= "now() ";
  89. $sql .= ")";
  90. $db->exec(check_sql($sql));
  91. unset($sql);
  92. //require_once "includes/header.php";
  93. echo "<meta http-equiv=\"refresh\" content=\"2;url=v_invoices.php\">\n";
  94. //echo "<meta http-equiv=\"refresh\" content=\"2;url=v_invoices.php?id=$contact_uuid\">\n";
  95. echo "<div align='center'>\n";
  96. echo "Add Complete\n";
  97. echo "</div>\n";
  98. require_once "includes/footer.php";
  99. return;
  100. } //if ($action == "add")
  101. if ($action == "update") {
  102. $sql = "update v_invoices set ";
  103. $sql .= "invoice_number = '$invoice_number', ";
  104. $sql .= "contact_uuid_from = '$contact_uuid_from', ";
  105. $sql .= "contact_uuid_to = '$contact_uuid_to', ";
  106. $sql .= "invoice_notes = '$invoice_notes' ";
  107. $sql .= "where domain_uuid = '$domain_uuid' ";
  108. $sql .= "and invoice_uuid = '$invoice_uuid' ";
  109. $db->exec(check_sql($sql));
  110. unset($sql);
  111. require_once "includes/header.php";
  112. echo "<meta http-equiv=\"refresh\" content=\"2;url=v_invoices.php\">\n";
  113. echo "<div align='center'>\n";
  114. echo "Update Complete\n";
  115. echo "</div>\n";
  116. require_once "includes/footer.php";
  117. return;
  118. } //if ($action == "update")
  119. } //if ($_POST["persistformvar"] != "true")
  120. } //(count($_POST)>0 && strlen($_POST["persistformvar"]) == 0)
  121. //pre-populate the form
  122. if (count($_GET)>0 && $_POST["persistformvar"] != "true") {
  123. $invoice_uuid = $_GET["id"];
  124. $sql = "";
  125. $sql .= "select * from v_invoices ";
  126. $sql .= "where domain_uuid = '$domain_uuid' ";
  127. $sql .= "and invoice_uuid = '$invoice_uuid' ";
  128. $prep_statement = $db->prepare(check_sql($sql));
  129. $prep_statement->execute();
  130. $result = $prep_statement->fetchAll();
  131. foreach ($result as &$row) {
  132. $invoice_number = $row["invoice_number"];
  133. $invoice_date = $row["invoice_date"];
  134. $contact_uuid_from = $row["contact_uuid_from"];
  135. $contact_uuid_to = $row["contact_uuid_to"];
  136. $invoice_notes = $row["invoice_notes"];
  137. break; //limit to 1 row
  138. }
  139. unset ($prep_statement);
  140. }
  141. //show the header
  142. require_once "includes/header.php";
  143. //get the default invoice number and contact_uuid_from
  144. if ($action == "add") {
  145. $sql = "";
  146. $sql .= "select * from v_invoices ";
  147. $sql .= "where domain_uuid = '$domain_uuid' ";
  148. $sql .= "order by invoice_uuid desc ";
  149. $sql .= "limit 1 ";
  150. $prep_statement = $db->prepare(check_sql($sql));
  151. if ($prep_statement) {
  152. $prep_statement->execute();
  153. $row = $prep_statement->fetch();
  154. $invoice_number = $row['invoice_number'] + 1;
  155. $contact_uuid_from = $row['contact_uuid_from'];
  156. unset ($prep_statement);
  157. }
  158. }
  159. //show the content
  160. echo "<div align='center'>";
  161. echo "<table width='100%' border='0' cellpadding='0' cellspacing=''>\n";
  162. echo "<tr class='border'>\n";
  163. echo " <td align=\"left\">\n";
  164. echo " <br>";
  165. echo "<form method='post' name='frm' action=''>\n";
  166. echo "<div align='center'>\n";
  167. echo "<table width='100%' border='0' cellpadding='6' cellspacing='0'>\n";
  168. echo "<tr>\n";
  169. if ($action == "add") {
  170. echo "<td align='left' width='30%' nowrap='nowrap'><b>Invoice Add</b></td>\n";
  171. }
  172. if ($action == "update") {
  173. echo "<td align='left' width='30%' nowrap='nowrap'><b>Invoice Edit</b></td>\n";
  174. }
  175. echo "<td width='70%' align='right'>\n";
  176. echo " <input type='button' class='btn' name='' alt='back' onclick=\"window.location='v_invoice_pdf.php?id=".$_GET["id"]."'\" value='PDF'>\n";
  177. echo " <input type='button' class='btn' name='' alt='back' onclick=\"history.go(-1);\" value='Back'>\n";
  178. echo "</td>\n";
  179. echo "</tr>\n";
  180. echo "<tr>\n";
  181. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  182. echo " Invoice Number:\n";
  183. echo "</td>\n";
  184. echo "<td class='vtable' align='left'>\n";
  185. echo " <input class='formfld' type='text' name='invoice_number' maxlength='255' value='$invoice_number'>\n";
  186. echo "<br />\n";
  187. echo "Enter the invoice number.\n";
  188. echo "</td>\n";
  189. echo "</tr>\n";
  190. echo "<tr>\n";
  191. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  192. echo " From:\n";
  193. echo "</td>\n";
  194. echo "<td class='vtable' align='left'>\n";
  195. $sql = "";
  196. $sql .= " select contact_uuid, org, n_given, n_family from v_contacts ";
  197. $sql .= " where domain_uuid = '$domain_uuid' ";
  198. $sql .= " order by org asc ";
  199. $prep_statement = $db->prepare(check_sql($sql));
  200. $prep_statement->execute();
  201. $result = $prep_statement->fetchAll();
  202. unset ($prep_statement, $sql);
  203. echo "<select name=\"contact_uuid_from\" id=\"contact_uuid_from\" class=\"formfld\">\n";
  204. echo "<option value=\"\"></option>\n";
  205. foreach($result as $row) {
  206. $contact_name = '';
  207. if (strlen($row['org']) > 0) {
  208. $contact_name = $row['org'];
  209. }
  210. if (strlen($row['n_family']) > 0) {
  211. if (strlen($contact_name) > 0) { $contact_name .= ", "; }
  212. $contact_name .= $row['n_family'];
  213. }
  214. if (strlen($row['n_given']) > 0) {
  215. if (strlen($contact_name) > 0) { $contact_name .= ", "; }
  216. $contact_name .= $row['n_given'];
  217. }
  218. if ($row['contact_uuid'] == $contact_uuid_from) {
  219. echo "<option value=\"".$row['contact_uuid']."\" selected=\"selected\">".$contact_name." $contact_uuid</option>\n";
  220. }
  221. else {
  222. echo "<option value=\"".$row['contact_uuid']."\">".$contact_name."</option>\n";
  223. }
  224. }
  225. unset($sql, $result, $row_count);
  226. echo "</select>\n";
  227. echo "<br />\n";
  228. echo "Select the Contact to send the send the invoice from. \n";
  229. echo "<a href='/app/contacts/v_contacts_edit.php?id=".$contact_uuid_from."'>View</a>\n";
  230. echo "</td>\n";
  231. echo "</tr>\n";
  232. echo "<tr>\n";
  233. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  234. echo " To:\n";
  235. echo "</td>\n";
  236. echo "<td class='vtable' align='left'>\n";
  237. $sql = "";
  238. $sql .= " select contact_uuid, org, n_given, n_family from v_contacts ";
  239. $sql .= " where domain_uuid = '$domain_uuid' ";
  240. $sql .= " order by org asc ";
  241. $prep_statement = $db->prepare(check_sql($sql));
  242. $prep_statement->execute();
  243. $result = $prep_statement->fetchAll();
  244. unset ($prep_statement, $sql);
  245. echo "<select name=\"contact_uuid_to\" id=\"contact_uuid_to\" class=\"formfld\">\n";
  246. echo "<option value=\"\"></option>\n";
  247. foreach($result as $row) {
  248. $contact_name = '';
  249. if (strlen($row['org']) > 0) {
  250. $contact_name = $row['org'];
  251. }
  252. if (strlen($row['n_family']) > 0) {
  253. if (strlen($contact_name) > 0) { $contact_name .= ", "; }
  254. $contact_name .= $row['n_family'];
  255. }
  256. if (strlen($row['n_given']) > 0) {
  257. if (strlen($contact_name) > 0) { $contact_name .= ", "; }
  258. $contact_name .= $row['n_given'];
  259. }
  260. if ($row['contact_uuid'] == $contact_uuid_to) {
  261. echo "<option value=\"".$row['contact_uuid']."\" selected=\"selected\">".$contact_name." $contact_uuid</option>\n";
  262. }
  263. else {
  264. echo "<option value=\"".$row['contact_uuid']."\">".$contact_name."</option>\n";
  265. }
  266. }
  267. unset($sql, $result, $row_count);
  268. echo "</select>\n";
  269. echo "<br />\n";
  270. echo "Select the Contact to send the send the invoice to. \n";
  271. echo "<a href='/app/contacts/v_contacts_edit.php?id=".$contact_uuid_to."'>View</a>\n";
  272. echo "</td>\n";
  273. echo "</tr>\n";
  274. if ($action == "update") {
  275. //prepare the invoice date
  276. $invoice_date = date("d", strtotime($invoice_date)).' '.date("M", strtotime($invoice_date)).' '.date("Y", strtotime($invoice_date));
  277. //show the formatted date
  278. echo "<tr>\n";
  279. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  280. echo " Date:\n";
  281. echo "</td>\n";
  282. echo "<td class='vtable' align='left'>\n";
  283. echo " $invoice_date\n";
  284. echo "</td>\n";
  285. echo "</tr>\n";
  286. }
  287. echo "<tr>\n";
  288. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  289. echo " Notes:\n";
  290. echo "</td>\n";
  291. echo "<td class='vtable' align='left'>\n";
  292. echo " <textarea class='formfld' type='text' name='invoice_notes'>$invoice_notes</textarea>\n";
  293. echo "<br />\n";
  294. echo "Enter the invoice note.\n";
  295. echo "</td>\n";
  296. echo "</tr>\n";
  297. echo " <tr>\n";
  298. echo " <td colspan='2' align='right'>\n";
  299. if ($action == "update") {
  300. echo " <input type='hidden' name='invoice_uuid' value='$invoice_uuid'>\n";
  301. }
  302. echo " <input type='submit' name='submit' class='btn' value='Save'>\n";
  303. echo " </td>\n";
  304. echo " </tr>";
  305. echo "</table>";
  306. echo "</form>";
  307. if ($action == "update") {
  308. require "v_invoice_items.php";
  309. }
  310. echo " </td>";
  311. echo " </tr>";
  312. echo "</table>";
  313. echo "</div>";
  314. //include the footer
  315. require_once "includes/footer.php";
  316. ?>