Moved all SQL statements from PHP files into relevant *_db.inc files.
[fa-stable.git] / admin / db / company_db.inc
index 621e986571a85afc31a3ccfcc60c8f735f598f2e..c95ef8de0ebfd340992c5cb3effc1552a5992110 100644 (file)
@@ -114,83 +114,99 @@ function get_company_pref($pref_name, $tbpref = TB_PREF)
        return $prefs[$pref_name];
 }
 
-// fiscal year routines
-function add_fiscalyear($from_date, $to_date, $closed)
+function get_base_sales_type()
 {
-       $from = date2sql($from_date);
-       $to = date2sql($to_date);
-
-       $sql = "INSERT INTO ".TB_PREF."fiscal_year (begin, end, closed)
-               VALUES (".db_escape($from).",".db_escape($to).", ".db_escape($closed).")";
+       $sql = "SELECT base_sales FROM ".TB_PREF."company WHERE coy_code=1";
 
-       db_query($sql, "could not add fiscal year");
+       $result = db_query($sql, "could not get base sales type");
+       $myrow = db_fetch($result);
+       return $myrow[0];
 }
 
-function update_fiscalyear($id, $closed)
-{
-       $sql = "UPDATE ".TB_PREF."fiscal_year SET closed=".db_escape($closed)."
-               WHERE id=".db_escape($id);
+function get_company_extensions($id = -1) {
+       global $path_to_root;
 
-       db_query($sql, "could not update fiscal year");
+       $file = $path_to_root.($id == -1 ? '' : '/company/'.$id).'/installed_extensions.php';
+       $installed_extensions = array();
+       if (is_file($file)) {
+               include($file);
+       }
+       return $installed_extensions;
 }
 
-function get_all_fiscalyears()
+function add_payment_terms($daysOrFoll, $terms, $dayNumber)
 {
-       $sql = "SELECT * FROM ".TB_PREF."fiscal_year ORDER BY begin";
-
-       return db_query($sql, "could not get all fiscal years");
+       if ($daysOrFoll) 
+       {
+               $sql = "INSERT INTO ".TB_PREF."payment_terms (terms,
+                       days_before_due, day_in_following_month)
+                       VALUES (" .
+                       db_escape($terms) . ", " . db_escape($dayNumber) . ", 0)";
+       } 
+       else 
+       {
+               $sql = "INSERT INTO ".TB_PREF."payment_terms (terms,
+                       days_before_due, day_in_following_month)
+                       VALUES (" . db_escape($terms) . ",
+                       0, " . db_escape($dayNumber) . ")";
+       }
+       db_query($sql,"The payment term could not be added");
 }
 
-function get_fiscalyear($id)
+function update_payment_terms($selected_id, $daysOrFoll, $terms, $dayNumber)
 {
-       $sql = "SELECT * FROM ".TB_PREF."fiscal_year WHERE id=".db_escape($id);
-
-       $result = db_query($sql, "could not get fiscal year");
-
-       return db_fetch($result);
+       if ($daysOrFoll) 
+       {
+               $sql = "UPDATE ".TB_PREF."payment_terms SET terms=" . db_escape($terms) . ",
+                       day_in_following_month=0,
+                       days_before_due=" . db_escape($dayNumber) . "
+                       WHERE terms_indicator = " .db_escape($selected_id);
+       } 
+       else 
+       {
+               $sql = "UPDATE ".TB_PREF."payment_terms SET terms=" . db_escape($terms) . ",
+                       day_in_following_month=" . db_escape($dayNumber) . ",
+                       days_before_due=0
+                       WHERE terms_indicator = " .db_escape($selected_id);
+       }
+       db_query($sql,"The payment term could not be updated");
 }
 
-function get_current_fiscalyear()
+function delete_payment_terms($selected_id)
 {
-       $year = get_company_pref('f_year');
-
-       $sql = "SELECT * FROM ".TB_PREF."fiscal_year WHERE id=".db_escape($year);
+       $sql="DELETE FROM ".TB_PREF."payment_terms WHERE terms_indicator=".db_escape($selected_id);
+       db_query($sql,"could not delete a payment terms");
+}
 
-       $result = db_query($sql, "could not get current fiscal year");
+function get_payment_terms($selected_id)
+{
+       $sql = "SELECT * FROM ".TB_PREF."payment_terms WHERE terms_indicator=".db_escape($selected_id);
 
+       $result = db_query($sql,"could not get payment term");
        return db_fetch($result);
 }
 
-function delete_fiscalyear($id)
+function get_payment_terms_all($show_inactive)
 {
-       begin_transaction();
-
-       $sql="DELETE FROM ".TB_PREF."fiscal_year WHERE id=".db_escape($id);
-
-       db_query($sql, "could not delete fiscal year");
-
-       commit_transaction();
+       $sql = "SELECT * FROM ".TB_PREF."payment_terms";
+       if (!$show_inactive) $sql .= " WHERE !inactive";
+       return db_query($sql,"could not get payment terms");
 }
 
-function get_base_sales_type()
+function customer_has_terms($selected_id)
 {
-       $sql = "SELECT base_sales FROM ".TB_PREF."company WHERE coy_code=1";
-
-       $result = db_query($sql, "could not get base sales type");
-       $myrow = db_fetch($result);
-       return $myrow[0];
+       $sql= "SELECT COUNT(*) FROM ".TB_PREF."debtors_master WHERE payment_terms = ".db_escape($selected_id);
+       $result = db_query($sql,"check failed");
+       $myrow = db_fetch_row($result);
+       return ($myrow[0] > 0); 
 }
 
-function get_company_extensions($id = -1) {
-       global $path_to_root;
-
-       $file = $path_to_root.($id == -1 ? '' : '/company/'.$id).'/installed_extensions.php';
-       $installed_extensions = array();
-       if (is_file($file)) {
-               include($file);
-       }
-       return $installed_extensions;
+function supplier_has_terms($selected_id)
+{
+       $sql= "SELECT COUNT(*) FROM ".TB_PREF."suppliers WHERE payment_terms = ".db_escape($selected_id);
+       $result = db_query($sql,"check failed");
+       $myrow = db_fetch_row($result);
+       return ($myrow[0] > 0); 
 }
 
-
 ?>
\ No newline at end of file