*** empty log message ***
[fa-stable.git] / sales / manage / recurrent_invoices.php
1 <?php
2
3
4 $page_security = 3;
5 $path_to_root="../..";
6 include($path_to_root . "/includes/session.inc");
7 include($path_to_root . "/includes/ui.inc");
8
9 $js = "";
10 if ($use_popup_windows)
11         $js .= get_js_open_window(900, 600);
12 if ($use_date_picker)
13         $js .= get_js_date_picker();
14
15 page(_("Recurrent Invoices"), false, false, "", $js);
16
17
18 simple_page_mode(true);
19
20 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
21 {
22
23         $input_error = 0;
24
25         if (strlen($_POST['description']) == 0) 
26         {
27                 $input_error = 1;
28                 display_error(_("The area description cannot be empty."));
29                 set_focus('description');
30         }
31
32         if ($input_error != 1)
33         {
34         if ($selected_id != -1) 
35         {
36                 $sql = "UPDATE ".TB_PREF."recurrent_invoices SET 
37                         description=".db_escape($_POST['description']).", 
38                         order_no=".db_escape($_POST['order_no']).", 
39                         debtor_no=".db_escape($_POST['debtor_no']).", 
40                         group_no=".db_escape($_POST['group_no']).", 
41                         days=".input_num('days').", 
42                         monthly=".input_num('monthly').", 
43                         begin='".date2sql($_POST['begin'])."', 
44                         end='".date2sql($_POST['end'])."' 
45                         WHERE id = '$selected_id'";
46                         $note = _('Selected recurrent invoice has been updated');
47         } 
48         else 
49         {
50                 $sql = "INSERT INTO ".TB_PREF."recurrent_invoices (description, order_no, debtor_no,
51                         group_no, days, monthly, begin, end, last_sent) VALUES (".db_escape($_POST['description']) . ", "
52                         .db_escape($_POST['order_no']).", ".db_escape($_POST['debtor_no']).", "
53                         .db_escape($_POST['group_no']).", ".input_num('days').", ".input_num('monthly').", '"
54                         .date2sql($_POST['begin'])."', '".date2sql($_POST['end'])."', '".date2sql(Add_Years($_POST['begin'], -5))."')";
55                         $note = _('New recurrent invoice has been added');
56         }
57     
58         db_query($sql,"The recurrent invoice could not be updated or added");
59                 display_notification($note);            
60                 $Mode = 'RESET';
61         }
62
63
64 if ($Mode == 'Delete')
65 {
66
67         $cancel_delete = 0;
68
69         if ($cancel_delete == 0) 
70         {
71                 $sql="DELETE FROM ".TB_PREF."recurrent_invoices WHERE id='" . $selected_id . "'";
72                 db_query($sql,"could not delete recurrent invoice");
73
74                 display_notification(_('Selected recurrent invoice has been deleted'));
75         } //end if Delete area
76         $Mode = 'RESET';
77
78
79 if ($Mode == 'RESET')
80 {
81         $selected_id = -1;
82         unset($_POST);
83 }
84 //-------------------------------------------------------------------------------------------------
85 function get_sales_group_name($group_no)
86 {
87         $sql = "SELECT description FROM ".TB_PREF."groups WHERE id = $group_no";
88         $result = db_query($sql, "could not get group");
89         $row = db_fetch($result);
90         return $row[0];
91 }
92
93 $sql = "SELECT * FROM ".TB_PREF."recurrent_invoices ORDER BY description, group_no, debtor_no";
94 $result = db_query($sql,"could not get recurrent invoices");
95
96 start_form();
97 start_table("$table_style width=70%");
98 $th = array(_("Description"), _("Template No"),_("Customer"),_("Branch")."/"._("Group"),_("Days"),_("Monthly"),_("Begin"),_("End"),_("Last Created"),"", "");
99 table_header($th);
100 $k = 0;
101 while ($myrow = db_fetch($result)) 
102 {
103         $begin = sql2date($myrow["begin"]);
104         $end = sql2date($myrow["end"]);
105         $last_sent = sql2date($myrow["last_sent"]);
106         
107         alt_table_row_color($k);
108                 
109         label_cell($myrow["description"]);
110         label_cell(get_customer_trans_view_str(30, $myrow["order_no"]));
111         if ($myrow["debtor_no"] == 0)
112         {
113                 label_cell("");
114                 label_cell(get_sales_group_name($myrow["group_no"]));
115         }       
116         else
117         {
118                 label_cell(get_customer_name($myrow["debtor_no"]));
119                 label_cell(get_branch_name($myrow['group_no']));
120         }       
121         label_cell($myrow["days"]);
122         label_cell($myrow['monthly']);
123         label_cell($begin);
124         label_cell($end);
125         label_cell($last_sent);
126         edit_button_cell("Edit".$myrow["id"], _("Edit"));
127         edit_button_cell("Delete".$myrow["id"], _("Delete"));
128         end_row();
129 }
130 end_table();
131
132 end_form();
133 echo '<br>';
134
135 //-------------------------------------------------------------------------------------------------
136
137 start_form();
138
139 start_table("$table_style2 width=40%");
140
141 if ($selected_id != -1) 
142 {
143         if ($Mode == 'Edit') {
144                 //editing an existing area
145                 $sql = "SELECT * FROM ".TB_PREF."recurrent_invoices WHERE id='$selected_id'";
146
147                 $result = db_query($sql,"could not get recurrent invoice");
148                 $myrow = db_fetch($result);
149
150                 $_POST['description']  = $myrow["description"];
151                 $_POST['order_no']  = $myrow["order_no"];
152                 $_POST['debtor_no']  = $myrow["debtor_no"];
153                 $_POST['group_no']  = $myrow["group_no"];
154                 $_POST['days']  = $myrow["days"];
155                 $_POST['monthly']  = $myrow["monthly"];
156                 $_POST['begin']  = sql2date($myrow["begin"]);
157                 $_POST['end']  = sql2date($myrow["end"]);
158         }
159         hidden("selected_id", $selected_id);
160
161
162
163 text_row_ex(_("Description:"), 'description', 50); 
164
165 templates_list_row(_("Template:"), 'order_no');
166
167 customer_list_row(_("Customer:"), 'debtor_no', null, " ", true);
168
169 if ($_POST['debtor_no'] > 0)
170         customer_branches_list_row(_("Branch:"), $_POST['debtor_no'], 'group_no', null, false);
171 else    
172         sales_groups_list_row(_("Sales Group:"), 'group_no', null, " ");
173
174 small_amount_row(_("Days:"), 'days', null, null, null, 0);
175
176 small_amount_row(_("Monthly:"), 'monthly', null, null, null, 0);
177
178 date_row(_("Begin:"), 'begin');
179
180 date_row(_("End:"), 'end', null, null, 0, 0, 5);
181
182 end_table(1);
183
184 submit_add_or_update_center($selected_id == -1, '', true);
185
186 end_form();
187
188 end_page();
189 ?>