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