af63044b64f211062dc3caba03c7a1cc966fcba3
[fa-stable.git] / purchasing / supplier_credit.php
1 <?php
2
3 $path_to_root="..";
4
5 include($path_to_root . "/purchasing/includes/supp_trans_class.inc");
6
7 $page_security=5;
8
9 include($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 page(_("Supplier Credit Note"), false, false, "", $js);
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 = 21;
30
31
32     echo "<center>";
33     display_notification_centered(_("Supplier credit note has been processed."));
34     display_note(get_trans_view_str($trans_type, $invoice_no, _("View this Credit Note")));
35
36         display_note(get_gl_view_str($trans_type, $invoice_no, _("View the GL Journal Entries for this Credit Note")), 1);
37
38     hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another Credit Note"), "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['supp_trans'] = new supp_trans;
55         $_SESSION['supp_trans']->is_invoice = false;
56 }
57
58 //---------------------------------------------------------------------------------------------------
59
60 function check_data()
61 {
62         global $total_grn_value, $total_gl_value;
63         
64         if (!$_SESSION['supp_trans']->is_valid_trans_to_post())
65         {
66                 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."));
67                 return false;
68         }
69
70         if (!references::is_valid($_SESSION['supp_trans']->reference)) 
71         {
72                 display_error(_("You must enter an credit note reference."));
73                 return false;
74         }
75
76         if (!is_new_reference($_SESSION['supp_trans']->reference, 21)) 
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 credit note reference."));
85                 return false;
86         }
87
88         if (!is_date($_SESSION['supp_trans']->tran_date))
89         {
90                 display_error(_("The credit note as entered cannot be processed because the date entered is not valid."));
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         if ($_SESSION['supp_trans']->ov_amount < ($total_gl_value + $total_grn_value))
105         {
106                 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."));
107                 return false;
108         }
109
110         return true;
111 }
112
113 //---------------------------------------------------------------------------------------------------
114
115 function handle_commit_credit_note()
116 {
117         copy_to_trans($_SESSION['supp_trans']);
118
119         if (!check_data())
120                 return;
121
122         $invoice_no = add_supp_invoice($_SESSION['supp_trans']);
123
124     $_SESSION['supp_trans']->clear_items();
125     unset($_SESSION['supp_trans']);
126
127         meta_forward($_SERVER['PHP_SELF'], "AddedID=$invoice_no");
128 }
129
130 //--------------------------------------------------------------------------------------------------
131
132 if (isset($_POST['PostCreditNote']))
133 {
134         handle_commit_credit_note();
135 }
136
137 //--------------------------------------------------------------------------------------------------
138
139 start_form(false, true);
140
141 start_table("$table_style width=80%", 8);
142 echo "<tr><td valign=center>"; // outer table
143
144 echo "<center>";
145
146 invoice_header($_SESSION['supp_trans']);
147
148 echo "</td></tr><tr><td valign=center>"; // outer table
149
150 $total_grn_value = display_grn_items($_SESSION['supp_trans']);
151
152 $total_gl_value = display_gl_items($_SESSION['supp_trans']);
153
154 echo "</td></tr><tr><td align=center colspan=2>"; // outer table
155
156 invoice_totals($_SESSION['supp_trans']);
157
158 echo "</td></tr>";
159
160 end_table(1); // outer table
161
162 submit_center('PostCreditNote', _("Enter Credit Note"));
163 echo "<br><br>";
164
165 end_form();
166 end_page();
167 ?>