/
item.php
244 lines (226 loc) · 7.19 KB
/
item.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?
require 'scat.php';
require 'lib/item.php';
head("item");
$code= $_GET['code'];
$id= (int)$_GET['id'];
?>
<form method="get" action="items.php">
<input id="focus" type="text" name="q" value="<?=htmlspecialchars($code)?>">
<input type="submit" value="Find Items">
</form>
<br>
<?
if (!$code && !$id) exit;
if (!$id && $code) {
$r= $db->query("SELECT id FROM item WHERE code = '" .
$db->real_escape_string($code) . "'");
if (!$r) die($m->error);
if (!$r->num_rows)
die("<h2>No item found.</h2>");
$id= $r->fetch_row();
$id= $id[0];
}
$item= item_load($db, $id);
?>
<script>
$(function() {
loadItem(<?=json_encode($item)?>);
});
var protoBarcodeRow= $('<tr><td></td><td class="quantity"></td><td>' +
'<img align="right" class="remove" src="icons/delete.png" width="16" height="16">' +
'</td></tr>');
function loadItem(item) {
$('#item').data('item', item);
var active= parseInt(item.active);
if (active) {
$('#item #active').attr('src', 'icons/accept.png');
} else {
$('#item #active').attr('src', 'icons/cross.png');
}
$('#item #code').text(item.code);
$('#item #name').text(item.name);
$('#item #brand').text(item.brand);
$('#item #retail_price').text(amount(item.retail_price));
$('#item #sale').text(amount(item.sale_price));
$('#item #discount').text(item.discount_label);
$('#item #stock').text(item.stock);
$('#item #minimum_quantity').text(item.minimum_quantity);
$('#item #last_net').text(amount(item.last_net));
$('#item #barcodes tbody').empty();
if (typeof(item.barcodes) != 'undefined') {
var barcodes= item.barcodes.split(/,/);
$.each(barcodes, function(i, barcode) {
var info= barcode.split(/!/);
var row= protoBarcodeRow.clone();
$('td:nth(0)', row).text(info[0]);
$('td:nth(1)', row).text(info[1]);
$('.quantity', row).editable(editBarcodeQuantity);
$('#item #barcodes tbody').append(row);
});
}
}
</script>
<style>
#item th { text-align: right; color: #666; vertical-align: top; }
#item th:after { content: ':'; }
#item td { padding-left: 1em; padding-right: 1em; }
#barcodes tr:nth-child(odd), #barcodes tr:nth-child(even) {
background: inherit;
}
</style>
<table id="item">
<tr><th>Code</th><td><span id="code" class="editable"></span><img id="active" align="right" src="icons/accept.png" height="16" width="16"></td></tr>
<tr><th>Name</th><td id="name" class="editable"></td></tr>
<tr><th>Brand</th><td id="brand"></td></tr>
<tr><th>MSRP</th><td id="retail_price" class="editable"></td></tr>
<tr><th>Sale</th><td id="sale"></td></tr>
<tr><th>Discount</th><td id="discount" class="editable"></td></tr>
<tr><th>Stock</th><td id="stock" class="editable"></td></tr>
<tr><th>Minimum Stock</th><td id="minimum_quantity" class="editable"></td></tr>
<tr><th>Last Net</th><td id="last_net"></td></tr>
<tr>
<th>Barcodes</th>
<td style="padding: 0">
<table id="barcodes" width="100%" style="padding: 0; margin: 0">
<tbody></tbody>
<tfoot>
<tr><td id="new-barcode" style="width:12em"><img src="icons/add.png" width="16" height="16"></td><td></td><td></td></tr>
</tfoot>
</table>
</td>
</tr>
</table>
<script>
$('#item .editable').editable(function(value, settings) {
var item= $('#item').data('item');
var data= { item: item.id };
var key= this.id;
data[key] = value;
$.getJSON("api/item-update.php?callback=?",
data,
function (data) {
if (data.error) {
$.modal(data.error);
return;
}
loadItem(data.item);
});
return "...";
}, {
event: 'dblclick',
style: 'display: inline',
placeholder: '',
});
$('#item #brand').editable(function(value, settings) {
var item= $('#item').data('item');
$.getJSON("api/item-update.php?callback=?",
{ item: item.id, brand: value },
function (data) {
if (data.error) {
$.modal(data.error);
return;
}
loadItem(data.item);
});
return "...";
}, {
event: 'dblclick',
style: 'display: inline',
type: 'select',
submit: 'OK',
loadurl: 'api/brand-list.php',
placeholder: '',
});
$('#item #active').on('dblclick', function(ev) {
ev.preventDefault();
var item= $('#item').data('item');
$.getJSON("api/item-update.php?callback=?",
{ item: item.id, active: parseInt(item.active) ? 0 : 1 },
function (data) {
if (data.error) {
$.modal(data.error);
return;
}
loadItem(data.item);
});
});
function editBarcodeQuantity(value, settings) {
var item= $('#item').data('item');
var row= $(this).closest('tr');
var code= $('td:nth(0)', row).text();
$.getJSON("api/item-barcode-update.php?callback=?",
{ item: item.id, code: code, quantity: value},
function (data) {
if (data.error) {
$.modal(data.error);
return;
}
loadItem(data.item);
});
}
$('#barcodes').on('dblclick', '.remove', function(ev) {
var item= $('#item').data('item');
var row= $(this).closest('tr');
var code= $('td:nth(0)', row).text();
var qty= $('td:nth(1)', row).text();
$.getJSON("api/item-barcode-delete.php?callback=?",
{ item: item.id, code: code },
function (data) {
if (data.error) {
$.modal(data.error);
return;
}
loadItem(data.item);
});
});
$('#barcodes #new-barcode').editable(function(value, settings) {
var item= $('#item').data('item');
$.getJSON("api/item-barcode-update.php?callback=?",
{ item: item.id, code: value },
function (data) {
if (data.error) {
$.modal(data.error);
return;
}
loadItem(data.item);
});
return $(this).data('original');
}, {
event: 'dblclick',
style: 'display: inline',
placeholder: '',
data: function(value, settings) {
$(this).data('original', value);
return '';
},
});
</script>
<?
$r= $db->query("SET @count = 0");
$q= "SELECT DATE_FORMAT(created, '%a, %b %e %Y %H:%i') Date,
CONCAT(txn, '|', txn.type, '|', txn.number) AS Transaction\$txn,
CASE type
WHEN 'customer' THEN IF(allocated <= 0, 'Sale', 'Return')
WHEN 'vendor' THEN 'Stock'
WHEN 'correction' THEN 'Correction'
WHEN 'drawer' THEN 'Till Count'
ELSE type
END Type,
IF(discount_type,
CASE discount_type
WHEN 'percentage' THEN ROUND(retail_price * ((100 - discount) / 100), 2)
WHEN 'relative' THEN (retail_price - discount)
WHEN 'fixed' THEN (discount)
END,
retail_price) AS Price\$dollar,
allocated AS Quantity\$right,
@count := @count + allocated AS Count\$right
FROM txn_line
JOIN txn ON (txn_line.txn = txn.id)
WHERE item = $id
GROUP BY txn
ORDER BY created";
echo '<h2>History</h2>';
dump_table($db->query($q));
dump_query($q);