У меня есть следующие таблицы, связанные с простой системой управления продажами
store_item
+---------+-----------+-----------------------+--------------+
| item_id | item_code | item_name | retail_price |
+---------+-----------+-----------------------+--------------+
| 1 | 60002332 | Coolant 1L | 250 |
| 2 | 60002333 | Coolant 4L | 950 |
| 3 | 60002334 | GEARON X (SAE90)-20L | 2500 |
| 4 | 60002335 | GEARON X (SAE90)-210L | 30000 |
+---------+-----------+-----------------------+--------------+
store_update_stock
+-----------------+------------+------------+--------------+
| update_stock_id | invoice_no | date | order_status |
+-----------------+------------+------------+--------------+
| 1 | INV001 | 2019-10-01 | purchase |
| 2 | INV002 | 2019-10-02 | purchase |
| 3 | INV003 | 2019-10-03 | purchase |
+-----------------+------------+------------+--------------+
store_update_stock_details
+
-------------------------+-----------------+------+-----+------------+---------------+
| update_stock_details_id | update_stock_id | item | qty | unit_price | available_qty |
+-------------------------+-----------------+------+-----+------------+---------------+
| 1 | 1 | 1 | 2 | 250 | 2 |
| 2 | 1 | 1 | 10 | 260 | 10 |
| 3 | 2 | 2 | 4 | 950 | 4 |
| 4 | 3 | 4 | 5 | 30000 | 5 |
+-------------------------+-----------------+------+-----+------------+---------------+
Я пытался выполнить продажи, используя item_name, qty & unit_price
мой контроллер с именем Issue.php также содержит следующую строку
$this->data['products'] = $this->Item_Model->getExistProducts();
Связанные функции в Item_Model выглядят следующим образом
public function isExistProduct($q)
{
if (!empty($q)) {
$this->db->select("store_item.*,store_update_stock.*,sum(qty) as qty, store_update_stock_details.unit_price");
$this->db->from('store_update_stock_details');
$this->db->join('store_update_stock', 'store_update_stock_details.update_stock_id=store_update_stock.update_stock_id');
$this->db->join('store_item', 'store_update_stock_details.item=store_item.item_id', 'right');
$this->db->where("store_item.item_id= $q");
$this->db->group_by('store_item.item_id, store_update_stock_details.unit_price');
$q1 = $this->db->get();
if ($q1->num_rows() > 0) {
return $q1->result_array();
}
return 0;
}
}
И названное представление addIssue.php включает следующий код JavaScript и HTML-код
<script type="text/javascript">
$(document).on("change", "#item", function () {
// console.log($('#item').val());
$("#salesList").show();
$.ajax({
'url': '<?=site_url("issue/isExistProduct/?q=")?>' + $('#item').val(),
'method': 'GET',
'success': function (data) {
var jData = JSON.parse(data);
if (jData.status == true) {
//console.log(jData);
$('#request_table').append('<tr>' +
'<td ><span id="product" >' + jData.data[0].item_name + '</span>' +
'<input type="hidden" id="item_id[]" name="item_id[]" value="' + jData.data[0].item_id + '">' +
'</td>' +
'<td class="text-center">' + jData.data[0].qty + '</td>' +
'<td class="text-center"><input class="form-control text-center rquantity" data-qty-bal="' + jData.data[0].qty + '" autofocus required type="number" step="any" id="qty[]" name="qty[]" ></td>' +
'<td class="text-center"><input class="form-control text-right" autofocus required type="number" step="any" id="sales_price[]" name="sales_price[]" value="' + jData.data[0].unit_price + '"></td>' +
'<td class="text-center"><input class="form-control text-right" autofocus type="number" step="any" id="discount_price[]" name="discount_price[]" ></td>' +
'<td class="text-center" ><i class="fa fa-remove remove" style="cursor: pointer"></i></td>' +
'</tr>');
}
},
'error': function () {
}
});
});
$(document).on("click", ".remove", function () {
$(this).closest('tr').remove();
});
var old_row_qty;
$(document).on("focus", '.rquantity', function () {
old_row_qty = $(this).val();
}).on("change", '.rquantity', function () {
var row = $(this).closest('tr');
if ($(this).val() > $(this).data('qty-bal')) {
$(row).addClass('danger');
$('#add_sale').attr('disabled', true);
} else {
$(row).removeClass('danger');
$('#add_sale').attr('disabled', false);
}
var amount = 0, subTotal = 0;
$('#request_table >tbody').find('tr').each(function () {
var rate = parseFloat($(this).find('td:eq(2) > .rquantity').val());
// $(this).find('.ssubtotal').text(formatNumber(rate));
amount += rate;
// subTotal += getNumber($(this).find('td:eq(5)').text());
});
$('#request_table >tfoot>tr').find('th:eq(1)').text(amount);
// $('#payTable >tfoot>tr').find('th:eq(2)').text(formatNumber(subTotal));
});
</script>
<script type="text/javascript">
$(document).ready(function () {
$("#salesList").hide();
var oTable = $('#ExData').dataTable({
"aaSorting": [[0, "asc"]],
"aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "ALL"]],
"iDisplayLength": 10,
'bProcessing': true, 'bServerSide': true,
'sAjaxSource': '<?= base_url() . "issue/getIssue" ?>',
'fnServerData': function (sSource, aoData, fnCallback) {
aoData.push({
"name": "type",
"value": "1 "
});
$.ajax({'dataType': 'json', 'type': 'POST', 'url': sSource, 'data': aoData, 'success': fnCallback});
},
'fnRowCallback': function (nRow, aData, iDisplayIndex) {
},
"aoColumns": [{'sName': 'invoice_no'}, {'sName': 'sales_date'}, {'sName': 'rep'}, {'sName': 'dealer_code'}, {'sName': 'dealer_name'}, {'mRender':formatMoney}, {'mRender':formatMoney}, {'mRender': pay_status}, null],
"fnFooterCallback": function (nRow, aaData, iStart, iEnd, aiDisplay) {
}
});
});
</script>
<div class="box box-info">
<div class="box-body" style="display: block">
<form action="<?= site_url('issue/addIssue') ?>" method="post">
<div class="row">
<div class="col-md-3">
<div class="form-group"><label>Sales Date</label>
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-calendar"></i>
</span>
<input type="text" name="sales_date" id="sales_date" class="form-control datepicker"
style="cursor: pointer"
value="<?= date('Y-m-d') ?>" readonly>
</div>
</div>
</div>
<div class="col-md-3">
<div class="form-group"><label>Dealer</label>
<select name="dealer" id="dealer" class="form-control select2" required>
<option value="">Select Dealer</option>
<?php
if (!empty($dealer)) {
foreach ($dealer as $row) {
?>
<option value="<?= $row->dealer_id ?>"><?= $row->dealer_code . " - " . $row->dealer_name ?></option>
<?php
}
}
?>
</select>
</div>
</div>
<div class="col-md-3">
<div class="form-group"><label>Rep Code</label>
<select name="rep" id="rep" class="form-control select2" required>
<option value="">Select Rep</option>
<?php
if (!empty($rep)) {
foreach ($rep as $row) {
?>
<option value="<?= $row->rep_id ?>"><?= $row->rep_name ?></option>
<?php
}
}
?>
</select>
</div>
</div>
<div class="col-md-3">
<div class="form-group"><label>Invoice No</label>
<input type="text" name="invoice_no" id="invoice_no" class="form-control"
value="<?= $autoInv ?>">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group"><label>Select Item</label>
<select name="item" id="item" class="form-control select2" required>
<option value="">Select Item</option>
<?php
if (!empty($products)) {
foreach ($products as $row) {
?>
<option value="<?= $row->item_id ?>"><?= $row->item_name ?></option>
<?php
}
}
?>
</select>
</div>
</div>
</div>
<!--table-->
<div id="salesList">
<div class="col-md-12 column">
<div class="col-md-12">
<div class="control-group table-group">
<label class="table-label">Sale Items *</label>
<div class="controls table-controls">
<table id="request_table" width="100%"
class="table items table-striped table-bordered table-condensed table-hover">
<thead>
<tr style="background-color: #3c8dbc;color: #ffffff;">
<th class="col-md-3">Item Name</th>
<th class="text-center col-md-2">Available Qty</th>
<th class="text-center col-md-2">Issue Qty</th>
<th class="text-center col-md-2">Unit Price</th>
<th class="text-center col-md-2">Discount</th>
</th>
<th class="col-md-1" style="width: 30px !important; text-align: center;">
<i class="fa fa-trash-o"
style="opacity:0.5; filter:alpha(opacity=50);"></i>
</th>
</tr>
</thead>
<tbody></tbody>
<tfoot>
<tr id="tfoot" class="tfoot active">
<th colspan="2">Total</th>
<th class="text-center">0</th>
<th class="text-right"></th>
<th class="text-right"></th>
<th class="text-center"><i class="fa fa-trash-o"
style="opacity:0.5; filter:alpha(opacity=50);"></i>
</th>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-8"></div>
<div class="col-md-4">
<button type="submit" id="add_sale" class="btn btn-primary btn-block"><i
class="fa fa-shopping-cart" aria-hidden="true"></i> Add Sale
</button>
</div>
</div>
</div>
</form>
<!--end of table-->
</div>
Всеработает нормально.
Выпуск
В таблице store_update_stock_details есть две записи item_id -> 1 с разными unit_price (250, 260).
Требуемый вывод
Я хочу показать эти две unit_prices с item_name & qty в виде двух строк в списке при выполнении продаж и обновлении доступных количеств. Элемент и available_qty отображаются правильно, а unit_prices не отображаются в представлении.
Что идет не так. Кто-нибудь может мне помочь?