Search only for not voided transactions.
[fa-stable.git] / admin / payment_terms.php
index 2b323d2775a62ea038a13491e39accfb0e82cb45..4e07c6e4a95706d9fe7e76e13d537361dc8d891d 100644 (file)
@@ -1,19 +1,19 @@
 <?php
 /**********************************************************************
     Copyright (C) FrontAccounting, LLC.
-       Released under the terms of the GNU Affero General Public License,
-       AGPL, as published by the Free Software Foundation, either version 
-       of the License, or (at your option) any later version.
+       Released under the terms of the GNU General Public License, GPL, 
+       as published by the Free Software Foundation, either version 3 
+       of the License, or (at your option) any later version.
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
-    See the License here <http://www.gnu.org/licenses/agpl-3.0.html>.
+    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
 ***********************************************************************/
-$page_security = 10;
+$page_security = 'SA_PAYTERMS';
 $path_to_root="..";
 include($path_to_root . "/includes/session.inc");
 
-page(_("Payment Terms"));
+page(_($help_context = "Payment Terms"));
 
 include($path_to_root . "/includes/ui.inc");
 
@@ -57,43 +57,15 @@ if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
        {
        if ($selected_id != -1) 
        {
-               if (check_value('DaysOrFoll')) 
-               {
-                       $sql = "UPDATE ".TB_PREF."payment_terms SET terms=" . db_escape($_POST['terms']) . ",
-                                       day_in_following_month=0,
-                                       days_before_due=" . db_escape($_POST['DayNumber']) . "
-                                       WHERE terms_indicator = " .db_escape($selected_id);
-               } 
-               else 
-               {
-                       $sql = "UPDATE ".TB_PREF."payment_terms SET terms=" . db_escape($_POST['terms']) . ",
-                                       day_in_following_month=" . db_escape($_POST['DayNumber']) . ",
-                                       days_before_due=0
-                                       WHERE terms_indicator = " .db_escape( $selected_id );
-               }
+               update_payment_terms($selected_id, check_value('DaysOrFoll'), $_POST['terms'], $_POST['DayNumber']); 
                        $note = _('Selected payment terms have been updated');
        } 
        else 
        {
-
-               if (check_value('DaysOrFoll')) 
-               {
-                       $sql = "INSERT INTO ".TB_PREF."payment_terms (terms,
-                                       days_before_due, day_in_following_month)
-                                       VALUES (" .
-                                       db_escape($_POST['terms']) . ", " . db_escape($_POST['DayNumber']) . ", 0)";
-               } 
-               else 
-               {
-                       $sql = "INSERT INTO ".TB_PREF."payment_terms (terms,
-                                       days_before_due, day_in_following_month)
-                                       VALUES (" . db_escape($_POST['terms']) . ",
-                                       0, " . db_escape($_POST['DayNumber']) . ")";
-               }
+                       add_payment_terms(check_value('DaysOrFoll'), $_POST['terms'], $_POST['DayNumber']);
                        $note = _('New payment terms have been added');
        }
        //run the sql from either of the above possibilites
-       db_query($sql,"The payment term could not be added or updated");
                display_notification($note);
                $Mode = 'RESET';
        }
@@ -102,29 +74,20 @@ if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
 if ($Mode == 'Delete')
 {
        // PREVENT DELETES IF DEPENDENT RECORDS IN debtors_master
-
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."debtors_master WHERE payment_terms = '$selected_id'";
-       $result = db_query($sql,"check failed");
-       $myrow = db_fetch_row($result);
-       if ($myrow[0] > 0) 
+       if (key_in_foreign_table($selected_id, 'debtors_master', 'payment_terms'))
        {
                display_error(_("Cannot delete this payment term, because customer accounts have been created referring to this term."));
        } 
        else 
        {
-               $sql= "SELECT COUNT(*) FROM ".TB_PREF."suppliers WHERE payment_terms = '$selected_id'";
-               $result = db_query($sql,"check failed");
-               $myrow = db_fetch_row($result);
-               if ($myrow[0] > 0) 
+               if (key_in_foreign_table($selected_id, 'suppliers', 'payment_terms'))
                {
                        display_error(_("Cannot delete this payment term, because supplier accounts have been created referring to this term"));
                } 
                else 
                {
                        //only delete if used in neither customer or supplier accounts
-
-                       $sql="DELETE FROM ".TB_PREF."payment_terms WHERE terms_indicator='$selected_id'";
-                       db_query($sql,"could not delete a payment terms");
+                       delete_payment_terms($selected_id);
                        display_notification(_('Selected payment terms have been deleted'));
                }
        }
@@ -135,15 +98,18 @@ if ($Mode == 'Delete')
 if ($Mode == 'RESET')
 {
        $selected_id = -1;
+       $sav = get_post('show_inactive');
        unset($_POST);
+       $_POST['show_inactive'] = $sav;
 }
 //-------------------------------------------------------------------------------------------------
 
-$sql = "SELECT * FROM ".TB_PREF."payment_terms";
-$result = db_query($sql,"could not get payment terms");
+$result = get_payment_terms_all(check_value('show_inactive'));
+
 start_form();
-start_table($table_style);
+start_table(TABLESTYLE);
 $th = array(_("Description"), _("Following Month On"), _("Due After (Days)"), "", "");
+inactive_control_column($th);
 table_header($th);
 
 $k = 0; //row colour counter
@@ -172,6 +138,7 @@ while ($myrow = db_fetch($result))
     label_cell($myrow["terms"]);
     label_cell($full_text);
     label_cell($after_text);
+       inactive_control_cell($myrow["terms_indicator"], $myrow["inactive"], 'payment_terms', "terms_indicator");
        edit_button_cell("Edit".$myrow["terms_indicator"], _("Edit"));
        delete_button_cell("Delete".$myrow["terms_indicator"], _("Delete"));
     end_row();
@@ -179,30 +146,24 @@ while ($myrow = db_fetch($result))
 
 } //END WHILE LIST LOOP
 
-end_table();
-end_form();
-echo '<br>';
+inactive_control_row($th);
+end_table(1);
 
 //-------------------------------------------------------------------------------------------------
 
-start_form();
-
-start_table($table_style2);
+start_table(TABLESTYLE2);
 
 $day_in_following_month = $days_before_due = 0;
 if ($selected_id != -1) 
 {
        if ($Mode == 'Edit') {
                //editing an existing payment terms
-               $sql = "SELECT * FROM ".TB_PREF."payment_terms
-                       WHERE terms_indicator='$selected_id'";
-
-               $result = db_query($sql,"could not get payment term");
-               $myrow = db_fetch($result);
+               $myrow = get_payment_terms($selected_id);
 
                $_POST['terms']  = $myrow["terms"];
                $days_before_due  = $myrow["days_before_due"];
                $day_in_following_month  = $myrow["day_in_following_month"];
+               unset($_POST['DayNumber']);
        }
        hidden('selected_id', $selected_id);
 }
@@ -222,7 +183,7 @@ text_row_ex(_("Days (Or Day In Following Month):"), 'DayNumber', 3);
 
 end_table(1);
 
-submit_add_or_update_center($selected_id == -1, '', true);
+submit_add_or_update_center($selected_id == -1, '', 'both');
 
 end_form();