Implemented Selectable Print Orientation (Portrait, Landscape) for all reports in...
[fa-stable.git] / reporting / rep113.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = $_POST['PARAM_0'] == $_POST['PARAM_1'] ?
13         'SA_SALESTRANSVIEW' : 'SA_SALESBULKREP';
14 // ----------------------------------------------------------------
15 // $ Revision:  2.0 $
16 // Creator:     Joe Hunt
17 // date_:       2005-05-19
18 // Title:       Print Credit Notes
19 // ----------------------------------------------------------------
20 $path_to_root="..";
21
22 include_once($path_to_root . "/includes/session.inc");
23 include_once($path_to_root . "/includes/date_functions.inc");
24 include_once($path_to_root . "/includes/data_checks.inc");
25 include_once($path_to_root . "/sales/includes/sales_db.inc");
26
27 //----------------------------------------------------------------------------------------------------
28
29 print_credits();
30
31 //----------------------------------------------------------------------------------------------------
32
33 function print_credits()
34 {
35         global $path_to_root, $alternative_tax_include_on_docs, $suppress_tax_rates;
36         
37         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
38
39         $from = $_POST['PARAM_0'];
40         $to = $_POST['PARAM_1'];
41         $currency = $_POST['PARAM_2'];
42         $email = $_POST['PARAM_3'];
43         $paylink = $_POST['PARAM_4'];
44         $comments = $_POST['PARAM_5'];
45         $orientation = $_POST['PARAM_6'];
46
47         $orientation = ($orientation ? 'L' : 'P');
48         if (!$from || !$to) return;
49
50         $dec = user_price_dec();
51
52         $fno = explode("-", $from);
53         $tno = explode("-", $to);
54         $from = min($fno[0], $tno[0]);
55         $to = max($fno[0], $tno[0]);
56
57         $cols = array(4, 60, 225, 300, 325, 385, 450, 515);
58
59         // $headers in doctext.inc
60         $aligns = array('left', 'left', 'right', 'left', 'right', 'right', 'right');
61
62         $params = array('comments' => $comments);
63
64         $cur = get_company_Pref('curr_default');
65
66         if ($email == 0)
67                 $rep = new FrontReport(_('CREDIT NOTE'), "InvoiceBulk", user_pagesize(), 9, $orientation);
68     if ($orientation == 'L')
69         $rep->recalculate_cols($cols);
70
71         for ($i = $from; $i <= $to; $i++)
72         {
73                         if (!exists_customer_trans(ST_CUSTCREDIT, $i))
74                                 continue;
75                         $sign = -1;
76                         $myrow = get_customer_trans($i, ST_CUSTCREDIT);
77                         $baccount = get_default_bank_account($myrow['curr_code']);
78                         $params['bankaccount'] = $baccount['id'];
79
80                         $branch = get_branch($myrow["branch_code"]);
81                         $branch['disable_branch'] = $paylink; // helper
82                         $sales_order = null;
83                         if ($email == 1)
84                                 $rep = new FrontReport("", "", user_pagesize(), 9, $orientation);
85                         $rep->SetHeaderType('Header2');
86                         $rep->currency = $cur;
87                         $rep->Font();
88                         $rep->title = _('CREDIT NOTE');
89                         $rep->filename = "CreditNote" . $myrow['reference'] . ".pdf";
90                         $rep->Info($params, $cols, null, $aligns);
91
92                         $contacts = get_branch_contacts($branch['branch_code'], 'invoice', $branch['debtor_no'], false);
93                         $rep->SetCommonData($myrow, $branch, $sales_order, $baccount, ST_CUSTCREDIT, $contacts);
94                         $rep->NewPage();
95
96                         $result = get_customer_trans_details(ST_CUSTCREDIT, $i);
97                         $SubTotal = 0;
98                         while ($myrow2=db_fetch($result))
99                         {
100                                 if ($myrow2["quantity"] == 0)
101                                         continue;
102                                         
103                                 $Net = round2($sign * ((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]),
104                                    user_price_dec());
105                                 $SubTotal += $Net;
106                         $DisplayPrice = number_format2($myrow2["unit_price"],$dec);
107                         $DisplayQty = number_format2($sign*$myrow2["quantity"],get_qty_dec($myrow2['stock_id']));
108                         $DisplayNet = number_format2($Net,$dec);
109                         if ($myrow2["discount_percent"]==0)
110                                         $DisplayDiscount ="";
111                         else
112                                         $DisplayDiscount = number_format2($myrow2["discount_percent"]*100,user_percent_dec()) . "%";
113                                 $rep->TextCol(0, 1,     $myrow2['stock_id'], -2);
114                                 $oldrow = $rep->row;
115                                 $rep->TextColLines(1, 2, $myrow2['StockDescription'], -2);
116                                 $newrow = $rep->row;
117                                 $rep->row = $oldrow;
118                                 $rep->TextCol(2, 3,     $DisplayQty, -2);
119                                 $rep->TextCol(3, 4,     $myrow2['units'], -2);
120                                 $rep->TextCol(4, 5,     $DisplayPrice, -2);
121                                 $rep->TextCol(5, 6,     $DisplayDiscount, -2);
122                                 $rep->TextCol(6, 7,     $DisplayNet, -2);
123                                 $rep->row = $newrow;
124                                 //$rep->NewLine(1);
125                                 if ($rep->row < $rep->bottomMargin + (15 * $rep->lineHeight))
126                                         $rep->NewPage();
127                         }
128
129                         $memo = get_comments_string(ST_CUSTCREDIT, $i);
130                         if ($memo != "")
131                         {
132                                 $rep->NewLine();
133                                 $rep->TextColLines(1, 5, $memo, -2);
134                         }
135
136                         $DisplaySubTot = number_format2($SubTotal,$dec);
137                         $DisplayFreight = number_format2($sign*$myrow["ov_freight"],$dec);
138
139                 $rep->row = $rep->bottomMargin + (15 * $rep->lineHeight);
140                         $doctype = ST_CUSTCREDIT;
141
142                         $rep->TextCol(3, 6, _("Sub-total"), -2);
143                         $rep->TextCol(6, 7,     $DisplaySubTot, -2);
144                         $rep->NewLine();
145                         $rep->TextCol(3, 6, _("Shipping"), -2);
146                         $rep->TextCol(6, 7,     $DisplayFreight, -2);
147                         $rep->NewLine();
148                         $tax_items = get_trans_tax_details(ST_CUSTCREDIT, $i);
149                         $first = true;
150                 while ($tax_item = db_fetch($tax_items))
151                 {
152                         if ($tax_item['amount'] == 0)
153                                 continue;
154                         $DisplayTax = number_format2($sign*$tax_item['amount'], $dec);
155                         
156                         if (isset($suppress_tax_rates) && $suppress_tax_rates == 1)
157                                 $tax_type_name = $tax_item['tax_type_name'];
158                         else
159                                 $tax_type_name = $tax_item['tax_type_name']." (".$tax_item['rate']."%) ";
160
161                         if ($myrow['tax_included'])
162                         {
163                                 if (isset($alternative_tax_include_on_docs) && $alternative_tax_include_on_docs == 1)
164                                 {
165                                         if ($first)
166                                         {
167                                                         $rep->TextCol(3, 6, _("Total Tax Excluded"), -2);
168                                                         $rep->TextCol(6, 7,     number_format2($sign*$tax_item['net_amount'], $dec), -2);
169                                                         $rep->NewLine();
170                                         }
171                                                 $rep->TextCol(3, 6, $tax_type_name, -2);
172                                                 $rep->TextCol(6, 7,     $DisplayTax, -2);
173                                                 $first = false;
174                                 }
175                                 else
176                                                 $rep->TextCol(3, 7, _("Included") . " " . $tax_type_name . _("Amount") . ": " . $DisplayTax, -2);
177                                 }
178                         else
179                         {
180                                         $rep->TextCol(3, 6, $tax_type_name, -2);
181                                         $rep->TextCol(6, 7,     $DisplayTax, -2);
182                                 }
183                                 $rep->NewLine();
184                 }
185                 $rep->NewLine();
186                         $DisplayTotal = number_format2($sign*($myrow["ov_freight"] + $myrow["ov_gst"] +
187                                 $myrow["ov_amount"]+$myrow["ov_freight_tax"]),$dec);
188                         $rep->Font('bold');
189                         $rep->TextCol(3, 6, _("TOTAL CREDIT"), - 2);
190                         $rep->TextCol(6, 7, $DisplayTotal, -2);
191                         $words = price_in_words($myrow['Total'], ST_CUSTCREDIT);
192                         if ($words != "")
193                         {
194                                 $rep->NewLine(1);
195                                 $rep->TextCol(1, 7, $myrow['curr_code'] . ": " . $words, - 2);
196                         }       
197                         $rep->Font();
198                         if ($email == 1)
199                         {
200                                 $myrow['dimension_id'] = $paylink; // helper for pmt link
201                                 $rep->End($email);
202                         }
203         }
204         if ($email == 0)
205                 $rep->End();
206 }
207
208 ?>