У меня возникла проблема с моим всего. Ну, у меня есть 3 разных стола. billing_payments, billing_entry, и услуги.Общая сумма нескольких столбцов в таблице
Моя проблема в том, что я не могу получить общее количество УСЛУГИ. Я предоставил скриншот, чтобы вы поняли, что я имею в виду.
Вот код отчета. Я добавил несколько комментариев, чтобы указать, где проблема начинается и заканчивается.
<table class="table table-striped table-hover">
<thead>
<tr>
<th>#</th>
<th>PATIENT NAME</th>
<th>CASE</th>
<?php
$servicesquery = $this->db->get('services');
foreach ($servicesquery->result() as $service) {
echo '<th>'.$service->service_name.'</th>';
}
?>
<th>MEDICAL SUPPLIES</th>
<th>PHILHEALTH</th>
<th>DISCOUNT</th>
<th>GROSS</th>
<th>NET</th>
<tr>
</thead>
<tbody>
<?php
$x = 1;
$billquery = $this->db->query('SELECT * FROM `billing_payments` WHERE (`date` BETWEEN "'.$this->input->get('from').'" AND "'.$this->input->get('to').'")');
foreach ($billquery->result() as $data) {
echo '<tr>';
echo '<td>'.$x++.'</td>';
echo '<td>'.$data->patientname.'</td>';
echo '<td>'.$data->session_id.'</td>';
//SERVICES
$servicesquery = $this->db->get('services');
foreach ($servicesquery->result() as $service) {
$this->db->where('billing_serviceid', $service->service_id);
$this->db->where('billing_patientid', $data->patient_id);
$this->db->where('billing_id', $data->billing_id);
$this->db->select_sum('billing_amount');
$billing = $this->db->get('billing_entry');
foreach ($billing->result() as $bill) {
echo '<td>'.$bill->billing_amount.'</td>';
}
}
//MEDICAL SUPPLIES
$this->db->where('billing_id', $data->billing_id);
$this->db->where('billing_servicename', 'MEDICAL SUPPLIES');
$this->db->select_sum('billing_amount');
$medsup = $this->db->get('billing_entry');
foreach ($medsup->result() as $med) {
echo '<td>'.$med->billing_amount.'</td>';
}
//PHILHEALTH
$this->db->where('billing_id', $data->billing_id);
$this->db->select_sum('billing_philhealth');
$philhealth = $this->db->get('billing_entry');
foreach ($philhealth->result() as $phil) {
echo '<td class="bg-info">'.$phil->billing_philhealth.'</td>';
}
//DISCOUNT
$this->db->where('billing_id', $data->billing_id);
$this->db->select_sum('billing_discount');
$philhealth = $this->db->get('billing_entry');
foreach ($philhealth->result() as $phil) {
echo '<td class="bg-info">'.$phil->billing_discount.'</td>';
}
//GROSS
$this->db->where('billing_id', $data->billing_id);
$this->db->select_sum('billing_amount');
$gross = $this->db->get('billing_entry');
foreach ($gross->result() as $gr) {
echo '<td class="bg-warning">'.$gr->billing_amount.'</td>';
}
echo '<td class="bg-danger">'.$data->total_amount.'</td>';
echo '</tr>';
}
echo '<tr>';
echo '<td colspan="3" style="text-align:right"><strong>TOTAL:</strong></td>';
//PROBLEM STARTS HERE
//TOTAL PER SERVICES
$quer = $this->db->get('services');
foreach ($quer->result() as $service) {
$totserv = $this->db->query('SELECT * FROM `billing_payments` WHERE (`date` BETWEEN "'.$this->input->get('from').'" AND "'.$this->input->get('to').'")');
foreach ($totserv->result() as $servdata) {
$id = $servdata->id;
$this->db->where('billing_id', $servdata->billing_id);
$this->db->where('billing_serviceid', $service->service_id);
$this->db->select_sum('billing_amount');
$medsup = $this->db->get('billing_entry');
foreach ($medsup->result() as $med) {
echo '<td class="bg-success">'.$med->billing_amount.'</td>';
}
}
}
//PROBLEM ENDS HERE
//TOTAL NET
$totalamt = $this->db->query('SELECT SUM(total_amount) AS totalamount FROM `billing_payments` WHERE (`date` BETWEEN "'.$this->input->get('from').'" AND "'.$this->input->get('to').'")');
foreach ($totalamt->result() as $data) {
echo '<td>'.$data->totalamount.'</td>';
}
echo '</tr>';
?>
Я думаю, вы не можете использовать цикл для получения 'sum', так как' sum' будет общей суммой данных столбца. вы можете попробовать '$ bill-> row() -> billing_amount;' –
. Пожалуйста, научитесь использовать МОДЕЛИ, такие встроенные запросы - действительно плохая практика. Вы можете сделать это намного проще с помощью 1 сервисной модели, которая имеет несколько методов для возврата услуг и счетов. – Svetoslav