Moved all SQL statements from PHP files into relevant *_db.inc files.
[fa-stable.git] / sales / manage / sales_points.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_POSSETUP';
13 $path_to_root = "../..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 page(_($help_context = "POS settings"));
17
18 include_once($path_to_root . "/includes/ui.inc");
19 include_once($path_to_root . "/sales/includes/db/sales_points_db.inc");
20
21 simple_page_mode(true);
22 //----------------------------------------------------------------------------------------------------
23
24 function can_process()
25 {
26         if (strlen($_POST['name']) == 0)
27         {
28                 display_error(_("The POS name cannot be empty."));
29                 set_focus('pos_name');
30                 return false;
31         }
32         if (!check_value('cash') && !check_value('credit'))
33         {
34                 display_error(_("You must allow cash or credit sale."));
35                 set_focus('credit');
36                 return false;
37         }
38
39         return true;
40 }
41
42 //----------------------------------------------------------------------------------------------------
43
44 if ($Mode=='ADD_ITEM' && can_process())
45 {
46         add_sales_point($_POST['name'], $_POST['location'], $_POST['account'],
47                 check_value('cash'), check_value('credit'));
48         display_notification(_('New point of sale has been added'));
49         $Mode = 'RESET';
50 }
51
52 //----------------------------------------------------------------------------------------------------
53
54 if ($Mode=='UPDATE_ITEM' && can_process())
55 {
56
57         update_sales_point($selected_id, $_POST['name'], $_POST['location'],
58                 $_POST['account'], check_value('cash'), check_value('credit'));
59         display_notification(_('Selected point of sale has been updated'));
60         $Mode = 'RESET';
61 }
62
63 //----------------------------------------------------------------------------------------------------
64
65 if ($Mode == 'Delete')
66 {
67         if (key_in_foreign_table($selected_id, 'users', 'print_profile'))
68         {
69                 display_error(_("Cannot delete this POS because it is used in users setup."));
70         } else {
71                 delete_sales_point($selected_id);
72                 display_notification(_('Selected point of sale has been deleted'));
73                 $Mode = 'RESET';
74         }
75 }
76
77 if ($Mode == 'RESET')
78 {
79         $selected_id = -1;
80         $sav = get_post('show_inactive');
81         unset($_POST);
82         $_POST['show_inactive'] = $sav;
83 }
84 //----------------------------------------------------------------------------------------------------
85
86 $result = get_all_sales_points(check_value('show_inactive'));
87
88 start_form();
89 start_table("$table_style");
90
91 $th = array (_('POS Name'), _('Credit sale'), _('Cash sale'), _('Location'), _('Default account'), 
92          '','');
93 inactive_control_column($th);
94 table_header($th);
95 $k = 0;
96
97 while ($myrow = db_fetch($result))
98 {
99     alt_table_row_color($k);
100         label_cell($myrow["pos_name"], "nowrap");
101         label_cell($myrow['credit_sale'] ? _('Yes') : _('No'));
102         label_cell($myrow['cash_sale'] ? _('Yes') : _('No'));
103         label_cell($myrow["location_name"], "");
104         label_cell($myrow["bank_account_name"], "");
105         inactive_control_cell($myrow["id"], $myrow["inactive"], "sales_pos", 'id');
106         edit_button_cell("Edit".$myrow['id'], _("Edit"));
107         delete_button_cell("Delete".$myrow['id'], _("Delete"));
108         end_row();
109 }
110
111 inactive_control_row($th);
112 end_table(1);
113 //----------------------------------------------------------------------------------------------------
114
115 $cash = db_has_cash_accounts();
116
117 if (!$cash) display_note(_("To have cash POS first define at least one cash bank account."));
118
119 start_table($table_style2);
120
121 if ($selected_id != -1)
122 {
123
124         if ($Mode == 'Edit') {
125                 $myrow = get_sales_point($selected_id);
126
127                 $_POST['name']  = $myrow["pos_name"];
128                 $_POST['location']  = $myrow["pos_location"];
129                 $_POST['account']  = $myrow["pos_account"];
130                 if ($myrow["credit_sale"]) $_POST['credit_sale']  = 1;
131                 if ($myrow["cash_sale"]) $_POST['cash_sale'] = 1;
132         }
133         hidden('selected_id', $selected_id);
134
135
136 text_row_ex(_("Point of Sale Name").':', 'name', 20, 30);
137 if($cash) {
138         check_row(_('Allowed credit sale'), 'credit', check_value('credit_sale'));
139         check_row(_('Allowed cash sale'), 'cash',  check_value('cash_sale'));
140         cash_accounts_list_row(_("Default cash account").':', 'account');
141 } else {
142         hidden('credit', 1);
143         hidden('cash', 0);
144         hidden('account', 0);
145 }
146
147 locations_list_row(_("POS location").':', 'location');
148 end_table(1);
149
150 submit_add_or_update_center($selected_id == -1, '', 'both');
151
152 end_form();
153
154 end_page();
155
156 ?>