c215f8cd31941cf55e88f5c95aaabc005c514e72
[fa-stable.git] / admin / shipping_companies.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_SHIPPING';
13 $path_to_root="..";
14 include($path_to_root . "/includes/session.inc");
15 page(_("Shipping Company"));
16 include($path_to_root . "/includes/ui.inc");
17
18 simple_page_mode(true);
19 //----------------------------------------------------------------------------------------------
20
21 function can_process() 
22 {
23         if (strlen($_POST['shipper_name']) == 0) 
24         {
25                 display_error(_("The shipping company name cannot be empty."));
26                 set_focus('shipper_name');
27                 return false;
28         }
29         return true;
30 }
31
32 //----------------------------------------------------------------------------------------------
33 if ($Mode=='ADD_ITEM' && can_process()) 
34 {
35
36         $sql = "INSERT INTO ".TB_PREF."shippers (shipper_name, contact, phone, phone2, address)
37                 VALUES (" . db_escape($_POST['shipper_name']) . ", " .
38                 db_escape($_POST['contact']). ", " .
39                 db_escape($_POST['phone']). ", " .
40                 db_escape($_POST['phone2']). ", " .
41                 db_escape($_POST['address']) . ")";
42
43         db_query($sql,"The Shipping Company could not be added");
44         display_notification(_('New shipping company has been added'));
45         $Mode = 'RESET';
46 }
47
48 //----------------------------------------------------------------------------------------------
49
50 if ($Mode=='UPDATE_ITEM' && can_process()) 
51 {
52
53         $sql = "UPDATE ".TB_PREF."shippers SET shipper_name=" . db_escape($_POST['shipper_name']). " ,
54                 contact =" . db_escape($_POST['contact']). " ,
55                 phone =" . db_escape($_POST['phone']). " ,
56                 phone2 =" . db_escape($_POST['phone2']). " ,
57                 address =" . db_escape($_POST['address']). "
58                 WHERE shipper_id = $selected_id";
59
60         db_query($sql,"The shipping company could not be updated");
61         display_notification(_('Selected shipping company has been updated'));
62         $Mode = 'RESET';
63 }
64
65 //----------------------------------------------------------------------------------------------
66
67 if ($Mode == 'Delete')
68 {
69 // PREVENT DELETES IF DEPENDENT RECORDS IN 'sales_orders'
70
71         $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_orders WHERE ship_via='$selected_id'";
72         $result = db_query($sql,"check failed");
73         $myrow = db_fetch_row($result);
74         if ($myrow[0] > 0) 
75         {
76                 $cancel_delete = 1;
77                 display_error(_("Cannot delete this shipping company because sales orders have been created using this shipper."));
78         } 
79         else 
80         {
81                 // PREVENT DELETES IF DEPENDENT RECORDS IN 'debtor_trans'
82
83                 $sql= "SELECT COUNT(*) FROM ".TB_PREF."debtor_trans WHERE ship_via='$selected_id'";
84                 $result = db_query($sql,"check failed");
85                 $myrow = db_fetch_row($result);
86                 if ($myrow[0] > 0) 
87                 {
88                         $cancel_delete = 1;
89                         display_error(_("Cannot delete this shipping company because invoices have been created using this shipping company."));
90                 } 
91                 else 
92                 {
93                         $sql="DELETE FROM ".TB_PREF."shippers WHERE shipper_id=$selected_id";
94                         db_query($sql,"could not delete shipper");
95                         display_notification(_('Selected shipping company has been deleted'));
96                 }
97         }
98         $Mode = 'RESET';
99 }
100
101 if ($Mode == 'RESET')
102 {
103         $selected_id = -1;
104         $sav = get_post('show_inactive');
105         unset($_POST);
106         $_POST['show_inactive'] = $sav;
107 }
108 //----------------------------------------------------------------------------------------------
109
110 $sql = "SELECT * FROM ".TB_PREF."shippers";
111 if (!check_value('show_inactive')) $sql .= " WHERE !inactive";
112 $sql .= " ORDER BY shipper_id";
113 $result = db_query($sql,"could not get shippers");
114
115 start_form();
116 start_table($table_style);
117 $th = array(_("Name"), _("Contact Person"), _("Phone Number"), _("Secondary Phone"), _("Address"), "", "");
118 inactive_control_column($th);
119 table_header($th);
120
121 $k = 0; //row colour counter
122
123 while ($myrow = db_fetch($result)) 
124 {
125         alt_table_row_color($k);
126         label_cell($myrow["shipper_name"]);
127         label_cell($myrow["contact"]);
128         label_cell($myrow["phone"]);
129         label_cell($myrow["phone2"]);
130         label_cell($myrow["address"]);
131         inactive_control_cell($myrow["shipper_id"], $myrow["inactive"], 'shippers', 'shipper_id');
132         edit_button_cell("Edit".$myrow["shipper_id"], _("Edit"));
133         delete_button_cell("Delete".$myrow["shipper_id"], _("Delete"));
134         end_row();
135 }
136
137 inactive_control_row($th);
138 end_table(1);
139
140 //----------------------------------------------------------------------------------------------
141
142 start_table($table_style2);
143
144 if ($selected_id != -1) 
145 {
146         if ($Mode == 'Edit') {
147                 //editing an existing Shipper
148
149                 $sql = "SELECT * FROM ".TB_PREF."shippers WHERE shipper_id=$selected_id";
150
151                 $result = db_query($sql, "could not get shipper");
152                 $myrow = db_fetch($result);
153
154                 $_POST['shipper_name']  = $myrow["shipper_name"];
155                 $_POST['contact']       = $myrow["contact"];
156                 $_POST['phone'] = $myrow["phone"];
157                 $_POST['phone2']        = $myrow["phone2"];
158                 $_POST['address'] = $myrow["address"];
159         }
160         hidden('selected_id', $selected_id);
161 }
162
163 text_row_ex(_("Name:"), 'shipper_name', 40);
164
165 text_row_ex(_("Contact Person:"), 'contact', 30);
166
167 text_row_ex(_("Phone Number:"), 'phone', 32, 30);
168
169 text_row_ex(_("Secondary Phone Number:"), 'phone2', 32, 30);
170
171 text_row_ex(_("Address:"), 'address', 50);
172
173 end_table(1);
174
175 submit_add_or_update_center($selected_id == -1, '', 'both');
176
177 end_form();
178 end_page();
179 ?>