Add/view on F4 in supplier selector.
[fa-stable.git] / purchasing / supplier_credit.php
1 <?php
2
3 $path_to_root="..";
4
5 include_once($path_to_root . "/purchasing/includes/supp_trans_class.inc");
6
7 $page_security=5;
8
9 include_once($path_to_root . "/includes/session.inc");
10
11 include_once($path_to_root . "/includes/data_checks.inc");
12
13 include_once($path_to_root . "/purchasing/includes/purchasing_db.inc");
14 include_once($path_to_root . "/purchasing/includes/purchasing_ui.inc");
15 $js = "";
16 if ($use_popup_windows)
17         $js .= get_js_open_window(900, 500);
18 if ($use_date_picker)
19         $js .= get_js_date_picker();
20 page(_("Supplier Credit Note"), false, false, "", $js);
21
22 //----------------------------------------------------------------------------------------
23
24 check_db_has_suppliers(_("There are no suppliers defined in the system."));
25
26 //---------------------------------------------------------------------------------------------------------------
27 if ($ret = context_restore()) {
28  // return from supplier editor
29         copy_from_trans($_SESSION['supp_trans']);
30         if(isset($ret['supplier_id']))
31                 $_POST['supplier_id'] = $ret['supplier_id'];
32 }
33 if (isset($_POST['_supplier_id_editor'])) {
34         copy_to_trans($_SESSION['supp_trans']);
35         context_call($path_to_root.'/purchasing/manage/suppliers.php?supplier_id='.$_POST['supplier_id'], 'supp_trans');
36 }
37
38 //---------------------------------------------------------------------------------------------------------------
39
40 if (isset($_GET['AddedID'])) 
41 {
42         $invoice_no = $_GET['AddedID'];
43         $trans_type = 21;
44
45
46     echo "<center>";
47     display_notification_centered(_("Supplier credit note has been processed."));
48     display_note(get_trans_view_str($trans_type, $invoice_no, _("View this Credit Note")));
49
50         display_note(get_gl_view_str($trans_type, $invoice_no, _("View the GL Journal Entries for this Credit Note")), 1);
51
52     hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another Credit Note"), "New=1");
53
54         display_footer_exit();
55 }
56
57 //---------------------------------------------------------------------------------------------------
58
59 if (isset($_GET['New']))
60 {
61         if (isset( $_SESSION['supp_trans']))
62         {
63                 unset ($_SESSION['supp_trans']->grn_items);
64                 unset ($_SESSION['supp_trans']->gl_codes);
65                 unset ($_SESSION['supp_trans']);
66         }
67
68         $_SESSION['supp_trans'] = new supp_trans;
69         $_SESSION['supp_trans']->is_invoice = false;
70 }
71
72 //---------------------------------------------------------------------------------------------------
73
74 function check_data()
75 {
76         global $total_grn_value, $total_gl_value;
77         
78         if (!$_SESSION['supp_trans']->is_valid_trans_to_post())
79         {
80                 display_error(_("The credit note cannot be processed because the there are no items or values on the invoice.  Credit notes are expected to have a charge."));
81                 set_focus('');
82                 return false;
83         }
84
85         if (!references::is_valid($_SESSION['supp_trans']->reference)) 
86         {
87                 display_error(_("You must enter an credit note reference."));
88                 set_focus('reference');
89                 return false;
90         }
91
92         if (!is_new_reference($_SESSION['supp_trans']->reference, 21)) 
93         {
94                 display_error(_("The entered reference is already in use."));
95                 set_focus('reference');
96                 return false;
97         }
98
99         if (!references::is_valid($_SESSION['supp_trans']->supp_reference)) 
100         {
101                 display_error(_("You must enter a supplier's credit note reference."));
102                 set_focus('supp_reference');
103                 return false;
104         }
105
106         if (!is_date($_SESSION['supp_trans']->tran_date))
107         {
108                 display_error(_("The credit note as entered cannot be processed because the date entered is not valid."));
109                 set_focus('tran_date');
110                 return false;
111         } 
112         elseif (!is_date_in_fiscalyear($_SESSION['supp_trans']->tran_date)) 
113         {
114                 display_error(_("The entered date is not in fiscal year."));
115                 set_focus('tran_date');
116                 return false;
117         }
118         if (!is_date( $_SESSION['supp_trans']->due_date))
119         {
120                 display_error(_("The invoice as entered cannot be processed because the due date is in an incorrect format."));
121                 set_focus('due_date');
122                 return false;
123         }
124
125         if ($_SESSION['supp_trans']->ov_amount < ($total_gl_value + $total_grn_value))
126         {
127                 display_error(_("The credit note total as entered is less than the sum of the the general ledger entires (if any) and the charges for goods received. There must be a mistake somewhere, the credit note as entered will not be processed."));
128                 return false;
129         }
130
131         return true;
132 }
133
134 //---------------------------------------------------------------------------------------------------
135
136 function handle_commit_credit_note()
137 {
138         copy_to_trans($_SESSION['supp_trans']);
139
140         if (!check_data())
141                 return;
142
143         $invoice_no = add_supp_invoice($_SESSION['supp_trans']);
144
145     $_SESSION['supp_trans']->clear_items();
146     unset($_SESSION['supp_trans']);
147
148         meta_forward($_SERVER['PHP_SELF'], "AddedID=$invoice_no");
149 }
150
151 //--------------------------------------------------------------------------------------------------
152
153 if (isset($_POST['PostCreditNote']))
154 {
155         handle_commit_credit_note();
156 }
157
158 //--------------------------------------------------------------------------------------------------
159
160 start_form(false, true);
161
162 start_table("$table_style width=80%", 8);
163 echo "<tr><td valign=center>"; // outer table
164
165 echo "<center>";
166
167 invoice_header($_SESSION['supp_trans']);
168 if ($_POST['supplier_id']=='') 
169         display_error('No supplier found for entered search text');
170 else {
171         echo "</td></tr><tr><td valign=center>"; // outer table
172
173         $total_grn_value = display_grn_items($_SESSION['supp_trans']);
174
175         $total_gl_value = display_gl_items($_SESSION['supp_trans']);
176
177         echo "</td></tr><tr><td align=center colspan=2>"; // outer table
178
179         invoice_totals($_SESSION['supp_trans']);
180 }
181 echo "</td></tr>";
182
183 end_table(1); // outer table
184
185 submit_center('PostCreditNote', _("Enter Credit Note"), true, '', true);
186 echo "<br><br>";
187
188 end_form();
189 end_page();
190 ?>