Ajax additions
[fa-stable.git] / purchasing / supplier_invoice.php
1 <?php
2
3 $page_security=5;
4 $path_to_root="..";
5
6 include_once($path_to_root . "/purchasing/includes/purchasing_db.inc");
7
8 include_once($path_to_root . "/includes/session.inc");
9
10 include_once($path_to_root . "/includes/banking.inc");
11 include_once($path_to_root . "/includes/data_checks.inc");
12
13 include_once($path_to_root . "/purchasing/includes/purchasing_ui.inc");
14 $js = "";
15 if ($use_popup_windows)
16         $js .= get_js_open_window(900, 500);
17 if ($use_date_picker)
18         $js .= get_js_date_picker();
19 page(_("Enter Supplier Invoice"), false, false, "", $js);
20
21
22 //----------------------------------------------------------------------------------------
23
24 check_db_has_suppliers(_("There are no suppliers defined in the system."));
25
26 //---------------------------------------------------------------------------------------------------------------
27
28 if (isset($_GET['AddedID'])) 
29 {
30         $invoice_no = $_GET['AddedID'];
31         $trans_type = 20;
32
33
34     echo "<center>";
35     display_notification_centered(_("Supplier invoice has been processed."));
36     display_note(get_trans_view_str($trans_type, $invoice_no, _("View this Invoice")));
37
38         display_note(get_gl_view_str($trans_type, $invoice_no, _("View the GL Journal Entries for this Invoice")), 1);
39
40     hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another Invoice"), "New=1");
41
42         display_footer_exit();
43 }
44
45 //--------------------------------------------------------------------------------------------------
46
47 if (isset($_GET['New']))
48 {
49         if (isset( $_SESSION['supp_trans']))
50         {
51                 unset ($_SESSION['supp_trans']->grn_items);
52                 unset ($_SESSION['supp_trans']->gl_codes);
53                 unset ($_SESSION['supp_trans']);
54         }
55
56         //session_register("SuppInv");
57         session_register("supp_trans");
58         $_SESSION['supp_trans'] = new supp_trans;
59         $_SESSION['supp_trans']->is_invoice = true;
60 }
61
62 //--------------------------------------------------------------------------------------------------
63
64 function check_data()
65 {
66         If (!$_SESSION['supp_trans']->is_valid_trans_to_post())
67         {
68                 display_error(_("The invoice cannot be processed because the there are no items or values on the invoice.  Invoices are expected to have a charge."));
69                 return false;
70         }
71
72         if (!references::is_valid($_SESSION['supp_trans']->reference)) 
73         {
74                 display_error(_("You must enter an invoice reference."));
75                 set_focus('reference');
76                 return false;
77         }
78
79         if (!is_new_reference($_SESSION['supp_trans']->reference, 20)) 
80         {
81                 display_error(_("The entered reference is already in use."));
82                 set_focus('reference');
83                 return false;
84         }
85
86         if (!references::is_valid($_SESSION['supp_trans']->supp_reference)) 
87         {
88                 display_error(_("You must enter a supplier's invoice reference."));
89                 set_focus('supp_reference');
90                 return false;
91         }
92
93         if (!is_date( $_SESSION['supp_trans']->tran_date))
94         {
95                 display_error(_("The invoice as entered cannot be processed because the invoice date is in an incorrect format."));
96                 set_focus('trans_date');
97                 return false;
98         } 
99         elseif (!is_date_in_fiscalyear($_SESSION['supp_trans']->tran_date)) 
100         {
101                 display_error(_("The entered date is not in fiscal year."));
102                 set_focus('trans_date');
103                 return false;
104         }
105         if (!is_date( $_SESSION['supp_trans']->due_date))
106         {
107                 display_error(_("The invoice as entered cannot be processed because the due date is in an incorrect format."));
108                 set_focus('due_date');
109                 return false;
110         }
111
112         $sql = "SELECT Count(*) FROM ".TB_PREF."supp_trans WHERE supplier_id='" . $_SESSION['supp_trans']->supplier_id . "' AND supp_reference='" . $_POST['supp_reference'] . "'";
113         $result=db_query($sql,"The sql to check for the previous entry of the same invoice failed");
114
115         $myrow = db_fetch_row($result);
116         if ($myrow[0] == 1)
117         {       /*Transaction reference already entered */
118                 display_error(_("This invoice number has already been entered. It cannot be entered again." . " (" . $_POST['supp_reference'] . ")"));
119                 return false;
120         }
121
122         return true;
123 }
124
125 //--------------------------------------------------------------------------------------------------
126
127 function handle_commit_invoice()
128 {
129         copy_to_trans($_SESSION['supp_trans']);
130
131         if (!check_data())
132                 return;
133
134         $invoice_no = add_supp_invoice($_SESSION['supp_trans']);
135
136     $_SESSION['supp_trans']->clear_items();
137     unset($_SESSION['supp_trans']);
138
139         meta_forward($_SERVER['PHP_SELF'], "AddedID=$invoice_no");
140 }
141
142 //--------------------------------------------------------------------------------------------------
143
144 if (isset($_POST['PostInvoice']))
145 {
146         handle_commit_invoice();
147 }
148
149 //--------------------------------------------------------------------------------------------------
150
151 start_form(false, true);
152
153 start_table("$table_style2 width=80%", 8);
154 echo "<tr><td valign=center>"; // outer table
155
156 echo "<center>";
157
158 invoice_header($_SESSION['supp_trans']);
159
160 echo "</td></tr><tr><td valign=center>"; // outer table
161
162 echo "<center>";
163
164 display_grn_items($_SESSION['supp_trans']);
165
166 display_gl_items($_SESSION['supp_trans']);
167
168 //echo "</td></tr><tr><td align=center colspan=2>"; // outer table
169 echo "<br>";
170 invoice_totals($_SESSION['supp_trans']);
171
172 echo "</td></tr>";
173
174 end_table(); // outer table
175
176 echo "<br>";
177 submit_center('PostInvoice', _("Enter Invoice"), true, '', true);
178 echo "<br>";
179
180 end_form();
181
182 //--------------------------------------------------------------------------------------------------
183
184 end_page();
185 ?>