forked from sahujaunpuri/smart_retail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathissue_inventory.php
executable file
·460 lines (378 loc) · 14.2 KB
/
issue_inventory.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
<?php
require_once './includes/class.dbc.php';
require_once './includes/functions.php';
require_once './includes/day.php';
//database connection
$db = new dbc();
$dbc = $db->connect();
//function to get product quantity
require_once './get_product_quantity.php';
///////////////
// FUNCTIO TO GETNERATE A UNIQUE ID
function gen_issue_ref()
{
global $dbc;
$query = "SELECT `id` FROM `issue_ref` ORDER BY `id` DESC LIMIT 1";
$result = mysqli_query($dbc, $query)
or die("Error");
$constant = 'ISS-';
if(mysqli_num_rows($result)== 0)
{
$value = '00001';
}
else
{
//grab the value of the id
list($last_idd) = mysqli_fetch_array($result);
$last_id = (int) $last_idd;
$last_id++;
if($last_id < 10)
{
$value = '0000' . $last_id;
}
elseif($last_id < 100)
{
$value = '000' . $last_id;
}
elseif($last_id < 1000)
{
$value = '00' . $last_id;
}
elseif($last_id < 10000)
{
$value = '0' . $last_id;
}
else
{
$value = $last_id;
}
}
return $constant . $value;
}
//now data manipulation
if(isset($_POST['submit']))
{
//grab all the data
$ref = filter($_POST['ref']);
$date = filter($_POST['date']);
$agent = filter($_POST['agent']);
//now the data from the products sold
$product_code[] = $_POST['product_code'];
$product_name[] = $_POST['product_name'];
$quantity[] = $_POST['quantity'];
$unit_price = $_POST['unit_price'];
$total[] = $_POST['total'];
//check if all the items were not entered.
$number = count($product_name[0]);
$items = 0;
for($i = 0; $i < $number; $i++)
{
$index = $product_name[0][$i];
if(!empty($index))
{
$items++;
}
}
if($items == 0)
{
$warning = "Sale Was not made. You did not enter any items";
}
else
{
$final_total = 0;
$item_sold = 0;
for($i = 0; $i < $number; $i++)
{
$index = $product_name[0][$i];
if(!empty($index))
{
$item_sold++;
//grab all the values.
$mtotal = filter(get_money($total[0][$i]));
$mcode = filter($product_code[0][$i]);
$mname = filter($product_name[0][$i]);
$mquantity = filter($quantity[0][$i]);
$mprice = filter($unit_price[$i]);
$final_total = ($mtotal) + $final_total;
//insert into the database;
$query = "INSERT INTO `issue_inventory` ("
. " `id`, `ref`, `date`, `product_code`, `product_name`, "
. " `quantity`, `unit_price`, `total`, "
. " `day`, `month`, `year`, `time_added`, `user_id`)"
. ""
. " VALUE( "
. " 0, '$ref', '$date', '$mcode', '$mname', '$mquantity', "
. " '$mprice', '$mtotal', '$day', "
. " '$month', '$year', NOW(), '$user_id')";
$result = mysqli_query($dbc, $query)
or die("Error");
//get the product quantity;
$qq = get_quantity($mcode); //current quantity in the database;
//final quantity
$fquantity = (int)$qq - (int)$mquantity;
// now reduce the product quantity
$query = "UPDATE `products` SET "
. " `quantity` = '$fquantity'"
. " WHERE `product_code` = '$mcode'";
$result = mysqli_query($dbc, $query)
or die("Error");
//move the inventory
$init = $qq;
$end = $fquantity;
$comment = "Inventory Issued to " . $agent
. " <br> Ref No - " . $ref;
move_inventory($mcode, $init, $end, $comment);
}
}
//now insert into sales referece table
$query = "INSERT INTO `issue_ref` ("
. " `id`, `ref`, `sales_agent`, `date`, "
. " `total_price`, `items_sold`, `final_total`, `day`, "
. " `month`, `year`, `time_added`, `user_id`)"
. " "
. " VALUES("
. " 0, '$ref', '$agent', '$date', "
. " '$final_total', '$item_sold', '0', '$day', "
. " '$month', '$year', NOW(), '$user_id')";
$result = mysqli_query($dbc, $query)
or die("Error");
$success = "Items Issued.";
}
}
/**
* set up page active links
*/
$sidebar = "inventory";
$sublink = "issue_inventory";
/****** END ****/
$ref_id = gen_issue_ref();
//now page content
require_once './includes/header.php';
require_once './includes/sidebar.php';
require_once './includes/heading.php';
?>
<form action="" method="POST">
<div class="row">
<div class="row">
<div>
<h1 class="page-header">Issue Inventory</h1>
</div>
</div>
<?php
require_once './includes/notifications.php';
?>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<table>
<tr style='height:60px;'><td style='width: 50px; margin-right: 50px;'>
<label for="exampleInputEmail1" style='font-weight:normal; margin-right: 50px;'>Ref </label>
</td>
<td>
<input type="text" class="form-control" name='ref' style='width:180px; margin-right: 50px;'
required="" readonly="true" value="<?php echo $ref_id; ?>"></td>
<td>
<label for="exampleInputEmail1" style='font-weight:normal; margin-left: 50px;'>Date</label>
</td>
<td>
<input type="text" class="form-control" name='date' style='width:180px; margin-left: 50px;'
value="<?php echo date("d/m/Y"); ?>">
</td>
</tr>
</table>
</div>
</div>
<div class="row">
<div class="col-md-6" style="margin-top: 10px;">
<label class="col-md-4 text-right">
Sales Agent:
</label>
<div class="col-md-8">
<input type="text" name="agent" class="form-control"
style="border-bottom: 3px solid black;
border-top: transparent;
border-right: transparent;
border-left: transparent;
background-color: #64a7ee;
font-size: 1em;
color: black;
font-weight: bold;
padding-bottom: 2px;
" placeholder="Enter Name" required="true" id="agent">
</div>
</div>
<div class="col-md-6">
</div>
</div>
<br>
<div class="row">
<div class="col-md-12">
<div >
<table id="tb">
<tr>
<th style="text-align: center;">Product Code</th>
<th style="text-align: center;">Product Name</th>
<th style="text-align: center;">Quantity</th>
<th style="text-align: center;">Unit Price</th>
<th style="text-align: center;">Amount</th>
<th><a href="javascript:void(0);" style="font-size:18px;" id="addMore" title="Add More row">
<span class="glyphicon glyphicon-plus"></span>
</a>
</th>
</tr>
<?php
for($i = 1; $i < 7; $i++)
{
?>
<tr>
<td><input type="text" name="product_code[]" class="form-control product_code" readonly="true"></td>
<td><input type="text" name="product_name[]" class="form-control product_name" ></td>
<td><input type="text" name="quantity[]" class="form-control quantity" ></td>
<td><input type="text" name="unit_price[]" class="form-control unit_price" readonly="true"></td>
<td><input type="text" name="total[]" class="form-control total" readonly="true"></td>
<td style="background-color: white; border-bottom: 2px solid #ccc;">
<a href='javascript:void(0);' class='remove'>
<span class='glyphicon glyphicon-remove'></span>
</a>
</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</div>
</div>
<div class="row">
<div class="">
<div class="col-md-4 col-md-offset-1">
</div>
<div class="col-md-6">
<table>
<tr>
<th style="text-align: center;">TOTAL</th>
<td>
<input type="text" style="margin-top: 10px;
padding: 5px 10px; font-weight: bold; font-size: 1.5em;
" name="final_total" class="form-control"
id="final_total">
</td>
</tr>
</table>
</div>
</div>
<br>
<br>
<br>
<br>
<div class="row">
<div class="btn-group col-md-6" style="float: right;">
<div class="btn-group">
<input class="btn btn-success" style="margin-right: 10px;" type="submit" value="Issue" name="submit">
<a class="btn btn-success" style="margin-right: 10px;" href="sell_products.php">Cancel</a>
<a class="btn btn-success" style="margin-right: 10px;" href="index.php">Close</a>
</div>
</div>
</div>
</div>
</form>
<script>
$(function () {
$('#addMore').on('click', function () {
var data = $("#tb tr:eq(1)").clone(true).appendTo("#tb");
data.find("input").val('');
});
$(document).on('click', '.remove', function () {
var trIndex = $(this).closest("tr").index();
if (trIndex > 1) {
$(this).closest("tr").remove();
} else {
alert("Sorry!! Can't remove first row!");
}
});
});
$(document).ready(function(){
$(".product_name").typeahead({
source: function(key, result){
$.ajax({
url: "get_product_names.php",
method: "POST",
data: {key:key},
dataType: "json",
success: function(data){
result($.map(data, function(item){
return item;
}));
}
});
}
});
$("#agent").typeahead({
source: function(key, result){
$.ajax({
url: "get_agent_names.php",
method: "POST",
data: {key:key},
dataType: "json",
success: function(data){
result($.map(data, function(item){
return item;
}));
}
});
}
});
$(".product_name").focusout(function(){
var product_name = $(this).val();
var index = $(this).closest("tr").index();
var $row = $("#tb tr:eq(" + index + ")");
//now grab the index of that row.
$.ajax({
url: "get_product_code.php",
method: "POST",
data: {key: product_name},
dataType : "text",
success: function(data){
$row.find("input[name='product_code[]']").val(data);
}
});
//get the unit price
$.ajax({
url: "get_unit_price.php",
method: "POST",
data: {key: product_name},
dataType : "text",
success: function(data){
$row.find("input[name='unit_price[]']").val(data);
}
});
}); //end of focus out for the product name
$(".quantity").focusout(function(){
var quantity = $(this).val();
var index = $(this).closest("tr").index();
var $row = $("#tb tr:eq(" + index + ")");
//get the unit price
var unit_price = $row.find("input[name='unit_price[]']").val();
var total = unit_price * quantity;
$row.find("input[name='total[]']").val(total);
update_final_total();
});
function update_final_total()
{
var total = 0;
$(".total").each(function(){
var value = $(this).val();
if(value != '')
{
var int_value = parseInt(value);
total = total + int_value;
}
});
var final = total + " FCFA";
$("#final_total").val(final);
}
});
</script>
<?php
require_once './includes/bottom.php';
?>