การเพิ่มแถวคำนวณ VAT ในโปรแกรม PHP CI MANIA (ลดเวลาเขียนโค้ดด้วย CodeIgniter + Boostrap Framework)

โดย SONGCHAI SAETERN
ตัวอย่างการแทรกแถวคำนวณ VAT และผลรวมของรายการสั่งซื้อ


ในส่วนของ Views


   <tfoot> <tr> <th colspan="6" class="text-right">ราคาสินค้า</th> <th class="text-right">{total_product_price}</th> <th></th> </tr> <tr> <th colspan="6" class="text-right">ภาษีมูลค่าเพิ่ม 7%</th> <th class="text-right">{total_vat}</th> <th></th> </tr> <tr> <th colspan="6" class="text-right">รวมทั้งสิ้น</th> <th class="text-right">{grand_total}</th> <th></th> </tr> </tfoot>

ในส่วนของ Controllers

ส่วนของฟังก์ชั่น preview()
เพิ่มโค้ดก่อนการ render_view();

    // add other code // คำนวณ VAT $total_price = $this->data['order_total_price']; $total_vat = 0; $product_price = $total_price; $grand_total = $total_price; switch($results['vat_type']){ case 1: // สินค้าไม่รวม VAT (เพิ่ม VAT) $arr_vat = excludingVat($total_price); $product_price = $arr_vat['product_price']; $total_vat = $arr_vat['vat']; $grand_total = $product_price + $total_vat; break; case 2: //// สินค้ารวม VAT (ถอด VAT) $arr_vat = includingVat($total_price); $product_price = $arr_vat['product_price']; $total_vat = $arr_vat['vat']; $grand_total = $total_price; } $this->data['total_product_price'] = $product_price; $this->data['total_vat'] = $total_vat; $this->data['grand_total'] = $grand_total; $this->render_view('module_name/product_order_master/preview_view');

ส่วนของฟังก์ชั่น setDetailDataListFormat()
ส่วนหัวฟังก์ชั่น

private function setDetailDataListFormat($lists_data, $start_row=0)
{

$total_price = 0;


ส่วนท้ายฟังก์ชั่น


// add other code
$this->data['order_total_price'] = $total_price;

return $data;
}



ฟังก์ชั่นคำนวณ VAT ใน application/helpers/ci_utilities_helper.php

// สินค้าไม่รวม VAT (เพิ่ม VAT)
function excludingVat($price, $vat_percent = 7){
$product_price = $price;
$vat = round(($price * $vat_percent) / 100, 2);
return array('product_price' => $product_price, 'vat' => $vat);
}

// สินค้ารวม VAT (ถอด VAT)
function includingVat($price, $vat_percent = 7){
$vatDivisor = 1 + ($vat_percent / 100);
$total_price = $price;
$product_price = round($total_price / $vatDivisor, 2);
$vat = round($total_price - $product_price, 2);
return array('product_price' => $product_price, 'vat' => $vat);
}



วิดีโอตัวอย่างการเขียนโค้ด







PHP CI MANIA - PHP Code Generator 

โปรแกรมช่วยสร้างโค้ด "ลดเวลาการเขียนโปรแกรม"
 http://fastcoding.phpcodemania.com/index.php/#purchase-order
ราคาสุดคุ้ม  4,500  บาท
http://fastcoding.phpcodemania.com