forked from jimwins/scat
/
item.php
285 lines (261 loc) · 8.83 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?
require 'scat.php';
require 'lib/item.php';
$code= $_GET['code'];
$id= (int)$_GET['id'];
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);
head("Item: " . ashtml($item['name']). " @ Scat", true);
include 'item-searchform.php';
?>
<form class="form-horizontal" role="form">
<div class="form-group">
<label for="code" class="col-sm-2 control-label">
<a class="text-left fa" id="active"
data-bind="css: { 'fa-check-square-o' : item.active(), 'fa-square-o' : !item.active() }"></a>
Code
</label>
<div class="col-sm-8">
<p class="form-control-static" id="code"
data-bind="jeditable: item.code, jeditableOptions: { onupdate: saveItemProperty, onblur: 'cancel' }"></p>
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">
<a data-bind="attr: { href: 'http://rawm.us/' + item.code() }"
target="_blank">
<i class="fa fa-external-link"></i>
</a>
Name
</label>
<div class="col-sm-8">
<p class="form-control-static" id="name"
data-bind="jeditable: item.name, jeditableOptions: { onupdate: saveItemProperty, onblur: 'cancel' }"></p>
</div>
</div>
<div class="form-group">
<label for="brand" class="col-sm-2 control-label">Brand</label>
<div class="col-sm-8">
<p class="form-control-static" id="brand_id"
data-bind="jeditable: item.brand, jeditableOptions: { type: 'select', submit: 'OK', loadurl: 'api/brand-list.php', onupdate: saveItemProperty, onblur: 'cancel' }"></p>
</div>
</div>
<div class="form-group">
<label for="retail_price" class="col-sm-2 control-label">List</label>
<div class="col-sm-8">
<p class="form-control-static" id="retail_price"
data-bind="jeditable: item.retail_price, jeditableOptions: { ondisplay: amount, data: item.retail_price(), onupdate: saveItemProperty, onblur: 'cancel' }"></p>
</div>
</div>
<div class="form-group">
<label for="sale_price" class="col-sm-2 control-label">Sale</label>
<div class="col-sm-8">
<p class="form-control-static"
data-bind="text: amount(item.sale_price())"></p>
</div>
</div>
<div class="form-group">
<label for="discount" class="col-sm-2 control-label">Discount</label>
<div class="col-sm-8">
<p class="form-control-static" id="discount"
data-bind="jeditable: item.discount, jeditableOptions: { ondisplay: function() { return item.discount_label() ? item.discount_label() : item.discount() ? amount(item.discount()) : '...' } , data : item.discount(), onupdate: saveItemProperty, onblur: 'cancel' }"></p>
</div>
</div>
<div class="form-group">
<label for="stock" class="col-sm-2 control-label">Stock</label>
<div class="col-sm-8">
<p class="form-control-static" id="stock"
data-bind="jeditable: item.stock, jeditableOptions: { onupdate: saveItemProperty, onblur: 'cancel' }"></p>
</div>
</div>
<div class="form-group">
<label for="minimum_quantity" class="col-sm-2 control-label">
Minimum Quantity
</label>
<div class="col-sm-8">
<p class="form-control-static" id="minimum_quantity"
data-bind="jeditable: item.minimum_quantity, jeditableOptions: { onupdate: saveItemProperty, onblur: 'cancel' }"></p>
</div>
</div>
<div class="form-group">
<label for="barcodes" class="col-sm-2 control-label"><a id="print" class="fa fa-print"></a> Barcodes</label>
<div class="col-sm-8">
<table id="barcodes" class="table table-condensed">
<tbody data-bind="foreach: item.barcode_list">
<tr>
<td><span data-bind="click: $parent.removeBarcode"><a class="fa fa-trash-o"></a></span></td>
<td><span data-bind="text: $data.code"></span></td>
<td><span data-bind="text: $data.quantity"></span></td>
</tr>
</tbody>
<tfoot>
<tr>
<td id="new-barcode" colspan="3">
<a class="fa fa-plus-square-o"></a>
</td>
</tr>
</tfoot>
</table>
</div>
</div>
</form>
<script>
$('#active').on('click', function(ev) {
ev.preventDefault();
var item= viewModel.item;
$.getJSON("api/item-update.php?callback=?",
{ item: item.id(), active: item.active() ? 0 : 1 },
function (data) {
if (data.error) {
displayError(data);
return;
}
loadItem(data.item);
});
});
function editBarcodeQuantity(value, settings) {
var item= viewModel.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) {
displayError(data);
return;
}
loadItem(data.item);
});
}
$('#new-barcode').editable(function(value, settings) {
var item= viewModel.item;
$.getJSON("api/item-barcode-update.php?callback=?",
{ item: item.id, code: value },
function (data) {
if (data.error) {
displayError(data);
return;
}
loadItem(data.item);
});
return $(this).data('original');
}, {
event: 'click',
style: 'display: inline',
placeholder: '',
data: function(value, settings) {
$(this).data('original', value);
return '';
},
});
$('#print').on('click', function(ev) {
ev.preventDefault();
var item= viewModel.item.id();
$.getJSON("print/labels-price.php?callback=?",
{ id: item },
function (data) {
if (data.error) {
displayError(data);
return;
}
});
});
</script>
<?
$q= "SELECT company Company,
code Code,
retail_price List\$dollar,
net_price Net\$dollar,
promo_price Promo\$dollar,
CONCAT('$', CAST(vendor_item.net_price / 0.6 AS DECIMAL(9,2)),
' - ',
'$', CAST(vendor_item.net_price / 0.5 AS DECIMAL(9,2)))
AS Sale
FROM vendor_item
JOIN person ON vendor_item.vendor = person.id
WHERE item = $id";
echo '<h2 onclick="$(\'#vendors\').toggle()">Vendors</h2>';
echo '<div id="vendors" style="display: none">';
dump_table($db->query($q));
dump_query($q);
echo '</div>';
$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 onclick="$(\'#history\').toggle()">History</h2>';
echo '<div id="history" style="display: none">';
dump_table($db->query($q));
dump_query($q);
echo '</div>';
foot();
?>
<script>
var model= {
search: '<?=ashtml($search);?>',
all: <?=(int)$all?>,
item: <?=json_encode($item);?>,
brands: [],
};
var viewModel= ko.mapping.fromJS(model);
viewModel.removeBarcode= function(place) {
$.getJSON("api/item-barcode-delete.php?callback=?",
{ item: viewModel.item.id, code: place.code },
function (data) {
if (data.error) {
displayError(data);
return;
}
loadItem(data.item);
});
}
ko.applyBindings(viewModel);
function loadItem(item) {
ko.mapping.fromJS({ item: item }, viewModel);
}
function saveItemProperty(value, settings) {
var item= viewModel.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) {
displayError(data);
return;
}
loadItem(data.item);
});
return "...";
}
</script>