[0004212] Work Order Entry: fixed error when voided WO refence is reused.
[fa-stable.git] / reporting / rep104.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 = 'SA_PRICEREP';
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       price Listing
18 // ----------------------------------------------------------------
19 $path_to_root="..";
20
21 include_once($path_to_root . "/includes/session.inc");
22 include_once($path_to_root . "/includes/date_functions.inc");
23 include_once($path_to_root . "/includes/ui/ui_input.inc");
24 include_once($path_to_root . "/includes/data_checks.inc");
25 include_once($path_to_root . "/gl/includes/gl_db.inc");
26 include_once($path_to_root . "/sales/includes/db/sales_types_db.inc");
27 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
28
29 //----------------------------------------------------------------------------------------------------
30
31 print_price_listing();
32
33 function fetch_items($category=0)
34 {
35                 $sql = "SELECT item.stock_id, item.description AS name,
36                                 item.material_cost AS Standardcost,
37                                 item.category_id,item.units,
38                                 category.description
39                         FROM ".TB_PREF."stock_master item,
40                                 ".TB_PREF."stock_category category
41                         WHERE item.category_id=category.category_id AND NOT item.inactive";
42                 if ($category != 0)
43                         $sql .= " AND category.category_id = ".db_escape($category);
44                 $sql .= " AND item.mb_flag<> 'F' ORDER BY item.category_id,
45                                 item.stock_id";
46
47     return db_query($sql,"No transactions were returned");
48 }
49
50 function get_kits($category=0)
51 {
52         $sql = "SELECT i.item_code AS kit_code, i.description AS kit_name, c.category_id AS cat_id, c.description AS cat_name, count(*)>1 AS kit
53                         FROM
54                                 ".TB_PREF."item_codes i
55                                 LEFT JOIN ".TB_PREF."stock_category c ON i.category_id=c.category_id
56                         WHERE !i.is_foreign AND i.item_code!=i.stock_id";
57         if ($category != 0)
58                 $sql .= " AND c.category_id = ".db_escape($category);
59         $sql .= " GROUP BY i.item_code";
60     return db_query($sql,"No kits were returned");
61 }
62
63 //----------------------------------------------------------------------------------------------------
64
65 function print_price_listing()
66 {
67     global $path_to_root, $SysPrefs;
68
69     $currency = $_POST['PARAM_0'];
70     $category = $_POST['PARAM_1'];
71     $salestype = $_POST['PARAM_2'];
72     $pictures = $_POST['PARAM_3'];
73     $showGP = $_POST['PARAM_4'];
74     $comments = $_POST['PARAM_5'];
75         $orientation = $_POST['PARAM_6'];
76         $destination = $_POST['PARAM_7'];
77         if ($destination)
78                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
79         else
80                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
81
82         $orientation = ($orientation ? 'L' : 'P');
83     $dec = user_price_dec();
84
85         $home_curr = get_company_pref('curr_default');
86         if ($currency == ALL_TEXT)
87                 $currency = $home_curr;
88         $curr = get_currency($currency);
89         $curr_sel = $currency . " - " . $curr['currency'];
90         if ($category == ALL_NUMERIC)
91                 $category = 0;
92         if ($salestype == ALL_NUMERIC)
93                 $salestype = 0;
94         if ($category == 0)
95                 $cat = _('All');
96         else
97                 $cat = get_category_name($category);
98         if ($salestype == 0)
99                 $stype = _('All');
100         else
101                 $stype = get_sales_type_name($salestype);
102         if ($showGP == 0)
103                 $GP = _('No');
104         else
105                 $GP = _('Yes');
106
107         $cols = array(0, 100, 360, 385, 450, 515);
108
109         $headers = array(_('Category/Items'), _('Description'), _('UOM'), _('Price'),   _('GP %'));
110
111         $aligns = array('left', 'left', 'left', 'right', 'right');
112
113     $params =   array(  0 => $comments,
114                                     1 => array('text' => _('Currency'), 'from' => $curr_sel, 'to' => ''),
115                                     2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
116                                     3 => array('text' => _('Sales Type'), 'from' => $stype, 'to' => ''),
117                                     4 => array(  'text' => _('Show GP %'),'from' => $GP,'to' => ''));
118
119         if ($pictures)
120                 $user_comp = user_company();
121         else
122                 $user_comp = "";
123
124     $rep = new FrontReport(_('Price Listing'), "PriceListing", user_pagesize(), 9, $orientation);
125     if ($orientation == 'L')
126         recalculate_cols($cols);
127
128     $rep->Font();
129     $rep->Info($params, $cols, $headers, $aligns);
130     $rep->NewPage();
131
132         $result = fetch_items($category);
133
134         $catgor = '';
135         $_POST['sales_type_id'] = $salestype;
136         while ($myrow=db_fetch($result))
137         {
138                 if ($catgor != $myrow['description'])
139                 {
140                         $rep->Line($rep->row  - $rep->lineHeight);
141                         $rep->NewLine(2);
142                         $rep->fontSize += 2;
143                         $rep->TextCol(0, 3, $myrow['category_id'] . " - " . $myrow['description']);
144                         $catgor = $myrow['description'];
145                         $rep->fontSize -= 2;
146                         $rep->NewLine();
147                 }
148                 $rep->NewLine();
149                 $rep->TextCol(0, 1,     $myrow['stock_id']);
150                 $rep->TextCol(1, 2, $myrow['name']);
151                 $rep->TextCol(2, 3, $myrow['units']);
152                 $price = get_price($myrow['stock_id'], $currency, $salestype);
153                 $rep->AmountCol(3, 4, $price, $dec);
154                 if ($showGP)
155                 {
156                         $price2 = get_price($myrow['stock_id'], $home_curr, $salestype);
157                         if ($price2 != 0.0)
158                                 $disp = ($price2 - $myrow['Standardcost']) * 100 / $price2;
159                         else
160                                 $disp = 0.0;
161                         $rep->TextCol(4, 5,     number_format2($disp, user_percent_dec()) . " %");
162                 }
163                 if ($pictures)
164                 {
165                         $image = company_path(). "/images/"
166                                 . item_img_name($myrow['stock_id']) . ".jpg";
167                         if (file_exists($image))
168                         {
169                                 $rep->NewLine();
170                                 if ($rep->row - $SysPrefs->pic_height < $rep->bottomMargin)
171                                         $rep->NewPage();
172                                 $rep->AddImage($image, $rep->cols[1], $rep->row - $SysPrefs->pic_height, 0, $SysPrefs->pic_height);
173                                 $rep->row -= $SysPrefs->pic_height;
174                                 $rep->NewLine();
175                         }
176                 }
177                 else
178                         $rep->NewLine(0, 1);
179         }
180         $rep->Line($rep->row  - 4);
181
182         $result = get_kits($category);
183
184         $catgor = '';
185         while ($myrow=db_fetch($result))
186         {
187                 if ($catgor != $myrow['cat_name'])
188                 {
189                         if ($catgor == '')
190                         {
191                                 $rep->NewLine(2);
192                                 $rep->fontSize += 2;
193                                 $rep->TextCol(0, 3, _("Sales Kits"));
194                                 $rep->fontSize -= 2;
195                         }
196                         $rep->Line($rep->row  - $rep->lineHeight);
197                         $rep->NewLine(2);
198                         $rep->fontSize += 2;
199                         $rep->TextCol(0, 3, $myrow['cat_id'] . " - " . $myrow['cat_name']);
200                         $catgor = $myrow['cat_name'];
201                         $rep->fontSize -= 2;
202                         $rep->NewLine();
203                 }
204                 $rep->NewLine();
205                 $rep->TextCol(0, 1,     $myrow['kit_code']);
206                 $rep->TextCol(1, 3, $myrow['kit_name']);
207                 $price = get_kit_price($myrow['kit_code'], $currency, $salestype);
208                 $rep->AmountCol(3, 4, $price, $dec);
209                 $rep->NewLine(0, 1);
210         }
211         $rep->Line($rep->row  - 4);
212         $rep->NewLine();
213     $rep->End();
214 }
215