<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Brinvoicepdfadmin extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
         var $menu;
         var $contactus;
        public function __construct()
        {
            parent::__construct();
            $this->load->library(array('session'));
            // $this->load->model('Section', 'section_model');
            // $this->menu = $this->section_model->buildTopMenuItem();
            // $this->breadcrumbs = [];
            // $this->load->model('ContactDetails', 'contactdetails_model');
            // $this->contactus = $this->contactdetails_model->getContactus();

            // $this->load->model('Brinvoicemodel', 'invoice_model');
           $this->load->model('Brshopmodel', 'shop_model');
           $this->load->model('Brcustomermodel', 'customer_model');
           $this->load->model('Bremployeemodel', 'employee_model');
           $this->load->model('Brinvoicemodel', 'invoice_model');
            if (!$this->session->userdata('logged_in'))
            { 
                redirect('/at-cms/login');
            }
    
            
            $this->load->helper('form');
            
        }

    public function createInvoice(){
        $invoice_num = $this->uri->segment(4);

        $invoice_num2 = $this->uri->segment(5);

        $invoicelist = array();
        if(!empty($invoice_num)){
          array_push($invoicelist,$invoice_num);
        }
        if(!empty($invoice_num2)){
          array_push($invoicelist,$invoice_num2);
        }



        $this->load->library('Pdf');
        $pdf = new Pdf('P', 'mm', 'A4', true, 'UTF-8', false);
        $pdf->SetFont('msungstdlight', '', 10);
        $pdf->SetTitle('Beauty Artech');
        $pdf->SetTitle('Beauty Artech - Invoice');
        $pdf->SetSubject('Invoice');
        $pdf->SetKeywords('Beauty Artech, Invoice');
        $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(false);
        ob_end_clean();
        header("Content-Encoding: None", true);
  

        foreach($invoicelist as $invoicenumvalue){


        // $invoice_detial=$this->invoice_model->getInvoiceWithDetail($invoice_num);
        $invoice_detial=$this->invoice_model->getInvoiceWithDetail($invoicenumvalue);

        //new added payment detail
        $payment_list=$this->invoice_model->getInvoicePayment($invoicenumvalue);


         $payment_html="";
        foreach ($payment_list as $payment_list_row) {
          // if(!empty($payment_list_row->payment_amount)){
          if(ceil($payment_list_row->payment_amount)!=0){
          //round up to detect floating point zero

          $payment_method="";
          if($payment_list_row->payment_type==1){
            $payment_method="現金";
          }
          if($payment_list_row->payment_type==2){
            $payment_method="Jarvix";
          }
          if($payment_list_row->payment_type==3){
            $payment_method="EPS";
          }
          if($payment_list_row->payment_type==4){
            $payment_method="CUP";
          }
          if($payment_list_row->payment_type==5){
            $payment_method="VISA";
          }
          if($payment_list_row->payment_type==6){
            $payment_method="MASTER";
          }
          if($payment_list_row->payment_type==7){
            $payment_method="use packages";
          }
          if($payment_list_row->payment_type==8){
            $payment_method="HSBC 分期";
          }
          if($payment_list_row->payment_type==9){
            $payment_method="FPS";
          } 
          if($payment_list_row->payment_type==10){
            $payment_method="Payme";
          } 
          if($payment_list_row->payment_type==11){
            $payment_method="積分";
          } 

            $payment_html=$payment_html.'<tr>

        <td style="width: 86%; line-height: 150%;" align="left"><span>付款方式：'.$payment_method.'</span></td>

        <td style="width: 14%; line-height: 150%;" align="center"><span>$'.$payment_list_row->payment_amount.'</span></td>

    </tr>';
          }//if(!empty($payment_list_row->payment_amount)){
        }//foreach ($payment_list as $payment_list_row) {
        //end new added payment detail 


        //prepare invoice item
        // $invoice_item_list=$this->invoice_model->getInvoiceItemDetail($invoice_num);
        $invoice_item_list=$this->invoice_model->getInvoiceItemDetail($invoicenumvalue);
        $invoice_item_detail="";
        foreach ($invoice_item_list as $invoice_item_row) {

          $outqty=0;
          if (floor($invoice_item_row->qty) == $invoice_item_row->qty){
            $outqty=(int)$invoice_item_row->qty;
          }
          else{
            $outqty=$invoice_item_row->qty;
          }
          $outdiscount="-";
          // if(!is_null($invoice_item_row->discount)){
            if($invoice_item_row->discount!=0.00){
            $outdiscount=$invoice_item_row->discount;
          }

          //for create remark of not
          $print_remark='';
          if(!empty($invoice_item_row->remark)){
            $print_remark=' ('.$invoice_item_row->remark.') ';
          }
          
          //end for create remark of not


          $invoice_item_detail=$invoice_item_detail.'<table style="border-bottom: 1px solid '.THEME_ITEM_COLOR_BORDER_BOTTOM.';">

  <!-- tr as padding -->

  <tr><td style="line-height: 45%;"></td></tr>

  <tr>

    <td style="width: 55%; line-height: 150%;">'.$invoice_item_row->item_name.$print_remark.'

    </td>

   <!-- <td style="width:15%; line-height:150%;" align="center">$'.$invoice_item_row->price.'</td>-->

    <td style="width:15%; line-height:150%;" align="center">'.$outqty.'</td>

    <td style="width:15%; line-height:150%;" align="center">'.$outdiscount.'</td>

    <td style="width:15%; line-height:150%;" align="center">$'.$invoice_item_row->amount.'</td>

  </tr>

  <!--end data row-->

  <!-- tr as padding -->

  <tr><td style="line-height: 45%;"></td></tr>

</table>
          ';
        }//foreach ($invoice_item_list as $invoice_item_row) {
//         $invoice_item_detail=

//         '<table style="border-bottom: 1px solid {$THEME_ITEM_COLOR_BORDER_BOTTOM};">

//   <!-- tr as padding -->

//   <tr><td style="line-height: 45%;"></td></tr>

//   <tr>

//     <td style="width: 40%; line-height: 150%;">{$COURSE_NAME}<br />

//       {$COURSE_TIMESLOT_HTML_STRING}

//     </td>

//     <td style="width:15%; line-height: {$COURSE_TIMESLOT_ROW_TIMES_150}%;" align="center">{$PRICE}</td>

//     <td style="width:15%; line-height: {$COURSE_TIMESLOT_ROW_TIMES_150}%;" align="center">{$QTY}</td>

//     <td style="width:15%; line-height: {$COURSE_TIMESLOT_ROW_TIMES_150}%;" align="center">{$DISCOUNT}</td>

//     <td style="width:15%; line-height: {$COURSE_TIMESLOT_ROW_TIMES_150}%;" align="center">{$TOTAL}</td>

//   </tr>

//   <!--end data row-->

//   <!-- tr as padding -->

//   <tr><td style="line-height: 45%;"></td></tr>

// </table>';
        //end prepare invoice item



        // echo "invoice_num";  
        // echo $invoice_num; 
        

        // $pdf->SetHeaderMargin(30);
        // $pdf->SetTopMargin(20);
        // $pdf->setFooterMargin(20);
        // $pdf->SetAutoPageBreak(true);
        // $pdf->SetAuthor('Author');
        // $pdf->SetMargins(1, 1, 1);
        $pdf->AddPage('P', 'LETTER');
        // $pdf->SetDisplayMode('real', 'default');
        // $pdf->Write(5, 'CodeIgniter TCPDF Integration');
        //write html content
        // $pdf->AddPage();

        $pdfShowName="";
        //prefer to show display name first
        if(!empty($invoice_detial[0]['display_name'])){
          $pdfShowName=$invoice_detial[0]['display_name'];
        }
        else{
           $pdfShowName=$invoice_detial[0]['chi_name'];
        }


        $html = '<table>

  <tr>

    <!-- logo, need to add "/public" if production. -->

    <td style="width: 53%;"><img height="50" src="'.base_url().'assets/img/logo2.jpg"></td>

    <!-- invoice no. and date -->

    <td style="width: 47%;" align="right">

      <table>

        <tr>

          <td style="width: 57%;" align="left">

            <span style="color: '.THEME_COLOR.'; line-height: 50%;">發票號碼 Invoice No:</span>

          </td>

          <td style="width: 43%" align="right">

            '.$invoice_detial[0]['issued_name'].'

          </td>

        </tr>

        <tr>

          <td style="width: 57%" align="left">

            <span style="color: '.THEME_COLOR.';line-height: 50%;">日期Date:</span>

          </td>

          <td style="width: 43%" align="right">

             '.date('Y/m/d' ,$invoice_detial[0]['issued_date']).'

          </td>

        </tr>

        <tr>

          <td style="width: 57%" align="left">

            <span style="color: '.THEME_COLOR.';line-height: 50%;">分店Center:</span>

          </td>

          <td style="width: 43%" align="right">

             '.$invoice_detial[0]['shopname'].'

          </td>

        </tr>

      </table>

    </td>

  </tr>



</table>

<!-- Will omit the first br, br means a new line. Then the system can only read the second br as the first newline. -->

<br><br>



<br><br>

<table>

  <tr>

    <td align="center" style="width: 100%; font-size: 20px;">

      <h1><span style="font-size: x-large; color: {$THEME_COLOR_2};">發票 INVOICE</span></h1>

    </td>

  </tr>

</table>

<table>

  <tr>

    <td><span style="font-size: 12px;">客戶 : '.$pdfShowName.'('.$invoice_detial[0]['mobile'].')'.'</span><br></td>

  </tr>

</table>

<!-- Attribute Labels -->

<table>

  <tr>

    <!-- Do not adjust any indentation -->

    <td style="width:55%;" align="center" bgcolor="'.THEME_THEAD_BGCOLOR.'">

      <span></span><br><strong style="font-size: 16px; color: '.THEME_COLOR.';">項目</strong><br><span style="color: '.THEME_COLOR.';">Item Description</span><br><span></span>

    </td>
<!--
    <td style="width:15%;" align="center" bgcolor="'.THEME_THEAD_BGCOLOR.'">

      <span></span><br><span style="font-size: 16px; color: '.THEME_COLOR.';">收費</span><br><span style="color: '.THEME_COLOR.';">Price</span><br><span></span>

    </td>
 -->


    <td style="width:15%;" align="center" bgcolor="'.THEME_THEAD_BGCOLOR.'">

      <span></span><br><span style="font-size: 16px; color: '.THEME_COLOR.';">數量</span><br><span style="color: '.THEME_COLOR.';">Quantity</span><br><span></span>

    </td>
   
    <td style="width:15%;" align="center" bgcolor="'.THEME_THEAD_BGCOLOR.'">

      <span></span><br><span style="font-size: 16px; color: '.THEME_COLOR.';">折扣</span><br><span style="color: '.THEME_COLOR.';">Discount</span><br><span></span>

    </td>

    <td style="width:15%;" align="center" bgcolor="'.THEME_THEAD_BGCOLOR.'">

      <span></span><br><span style="font-size: 16px; color: '.THEME_COLOR.';">實價</span><br><span style="color: '.THEME_COLOR.';">Total</span><br><span></span>

    </td>

  </tr>

  <tr>

    <td style="width:100%; line-height: 20%;" align="center"><table style="border-bottom: 1px solid '.THEME_THEAD_COLOR_BORDER_BOTTOM.';"><tr><td align="center"></td></tr></table></td>

  </tr>

</table>
'.$invoice_item_detail.'

<br>

<br>

<br>

<br>

<!-- Double line, each line will be illustrated by table border-bottom property-->

<!-- Spaces between line can be adjusted by line-height -->

<table>

  <tr>

    <td style="width: 82%; line-height: 150%;" align="right">

      <span style="color: {$THEME_COLOR};">應繳款項總額 TOTAL AMOUNT DUE: </span>

    </td>

    <td style="width: 18%; line-height: 150%;" align="center">

      <table style="border-bottom: 1px solid {$THEME_THEAD_COLOR_BORDER_BOTTOM};">

        <tr>

          <td align="center">$'.$invoice_detial[0]['total_amount_due'].'</td>

        </tr>

      </table>

    </td>

  </tr>

  <tr>

    <td style="width: 82%; line-height: 35%;" align="right">

    </td>

    <td style="width: 18%; line-height: 35%;" align="center">

      <table style="border-bottom: 1px solid {$THEME_THEAD_COLOR_BORDER_BOTTOM};">

        <tr>

          <td align="center"></td>

        </tr>

      </table>

    </td>

  </tr>

</table>

<br>
<br>
<table style="border-bottom: 1px dashed '.THEME_THEAD_COLOR_BORDER_BOTTOM.'">

    '.$payment_html.'

</table>


<table>

  <tr>

    <td><span>美容師 : '.$invoice_detial[0]['therapist_name'].'</span><br></td>

  </tr>

</table>

<table>

  <tr>

    <td><span>顧問 : '.$invoice_detial[0]['consultant_name'].'</span><br></td>

  </tr>

</table>

<br>

<br>

';

// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');


        //end write html content

        }//foreach($invoicelist as $invoicenumvalue){


        $pdf->Output('pdfexample.pdf', 'I'); 
    }
    //end added function creatInvoice
    public function createReceipt(){
        $invoice_num = $this->uri->segment(4);
        $receipt_row=$this->invoice_model->getReceiptRow($invoice_num);


        $invoice_detial=$this->invoice_model->getInvoiceWithDetail($invoice_num);

        $payment_list=$this->invoice_model->getInvoicePayment($invoice_num);
        //prepare invoice item
        $invoice_item_list=$this->invoice_model->getInvoiceItemDetail($invoice_num);
        $invoice_item_detail="";
        foreach ($invoice_item_list as $invoice_item_row) {

          $outqty=0;
          if (floor($invoice_item_row->qty) == $invoice_item_row->qty){
            $outqty=(int)$invoice_item_row->qty;
          }
          else{
            $outqty=$invoice_item_row->qty;
          }
          $outdiscount="-";
          // if(!is_null($invoice_item_row->discount)){
            if($invoice_item_row->discount!=0.00){
            $outdiscount=$invoice_item_row->discount;
          }

           //for create remark of not
          $print_remark='';
          if(!empty($invoice_item_row->remark)){
            $print_remark=' ('.$invoice_item_row->remark.') ';
          }
          
          //end for create remark of not


          $invoice_item_detail=$invoice_item_detail.'<table style="border-bottom: 1px solid '.THEME_ITEM_COLOR_BORDER_BOTTOM.';">

  <!-- tr as padding -->

  <tr><td style="line-height: 45%;"></td></tr>

  <tr>

    <td style="width: 55%; line-height: 150%;">'.$invoice_item_row->item_name.$print_remark.'

    </td>

   <!-- <td style="width:15%; line-height:150%;" align="center">$'.$invoice_item_row->price.'</td> -->

    <td style="width:15%; line-height:150%;" align="center">'.$outqty.'</td>

    <td style="width:15%; line-height:150%;" align="center">'.$outdiscount.'</td>

    <td style="width:15%; line-height:150%;" align="center">$'.$invoice_item_row->amount.'</td>

  </tr>

  <!--end data row-->

  <!-- tr as padding -->

  <tr><td style="line-height: 45%;"></td></tr>

</table>
          ';
        }//foreach ($invoice_item_list as $invoice_item_row) {


        $payment_html="";
        foreach ($payment_list as $payment_list_row) {
          // if(!empty($payment_list_row->payment_amount)){
          if(ceil($payment_list_row->payment_amount)!=0){
          //round up to detect floating point zero

          $payment_method="";
          if($payment_list_row->payment_type==1){
            $payment_method="現金";
          }
          if($payment_list_row->payment_type==2){
            $payment_method="Jarvix";
          }
          if($payment_list_row->payment_type==3){
            $payment_method="EPS";
          }
          if($payment_list_row->payment_type==4){
            $payment_method="CUP";
          }
          if($payment_list_row->payment_type==5){
            $payment_method="VISA";
          }
          if($payment_list_row->payment_type==6){
            $payment_method="MASTER";
          }
          if($payment_list_row->payment_type==7){
            $payment_method="use packages";
          }
          if($payment_list_row->payment_type==8){
            $payment_method="HSBC 分期";
          }
          if($payment_list_row->payment_type==9){
            $payment_method="FPS";
          }
          if($payment_list_row->payment_type==10){
            $payment_method="Payme";
          }  
          if($payment_list_row->payment_type==11){
            $payment_method="積分";
          }      

            $payment_html=$payment_html.'<tr>

        <td style="width: 86%; line-height: 150%;" align="left"><span>付款方式：'.$payment_method.'</span></td>

        <td style="width: 14%; line-height: 150%;" align="center"><span>$'.$payment_list_row->payment_amount.'</span></td>

    </tr>';
          }//if(!empty($payment_list_row->payment_amount)){
        }//foreach ($payment_list as $payment_list_row) {

//         $invoice_item_detail=

//         '<table style="border-bottom: 1px solid {$THEME_ITEM_COLOR_BORDER_BOTTOM};">

//   <!-- tr as padding -->

//   <tr><td style="line-height: 45%;"></td></tr>

//   <tr>

//     <td style="width: 40%; line-height: 150%;">{$COURSE_NAME}<br />

//       {$COURSE_TIMESLOT_HTML_STRING}

//     </td>

//     <td style="width:15%; line-height: {$COURSE_TIMESLOT_ROW_TIMES_150}%;" align="center">{$PRICE}</td>

//     <td style="width:15%; line-height: {$COURSE_TIMESLOT_ROW_TIMES_150}%;" align="center">{$QTY}</td>

//     <td style="width:15%; line-height: {$COURSE_TIMESLOT_ROW_TIMES_150}%;" align="center">{$DISCOUNT}</td>

//     <td style="width:15%; line-height: {$COURSE_TIMESLOT_ROW_TIMES_150}%;" align="center">{$TOTAL}</td>

//   </tr>

//   <!--end data row-->

//   <!-- tr as padding -->

//   <tr><td style="line-height: 45%;"></td></tr>

// </table>';
        //end prepare invoice item



        // echo "invoice_num";  
        // echo $invoice_num; 
        ob_end_clean();
        header("Content-Encoding: None", true);


        $this->load->library('Pdf');
        $pdf = new Pdf('P', 'mm', 'A4', true, 'UTF-8', false);
        $pdf->SetFont('msungstdlight', '', 10);
        $pdf->SetTitle('Beauty Artech Invoice');
        $pdf->SetTitle('Beauty Artech - Invoice');
        $pdf->SetSubject('Invoice');
        $pdf->SetKeywords('Beauty Artech, Invoice');
        $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(false);
        // $pdf->SetHeaderMargin(30);
        // $pdf->SetTopMargin(20);
        // $pdf->setFooterMargin(20);
        // $pdf->SetAutoPageBreak(true);
        // $pdf->SetAuthor('Author');
        // $pdf->SetMargins(1, 1, 1);
        $pdf->AddPage('P', 'LETTER');
        // $pdf->SetDisplayMode('real', 'default');
        // $pdf->Write(5, 'CodeIgniter TCPDF Integration');
        //write html content
        // $pdf->AddPage();


        $pdfShowName="";
        //prefer to show display name first
        if(!empty($invoice_detial[0]['display_name'])){
          $pdfShowName=$invoice_detial[0]['display_name'];
        }
        else{
           $pdfShowName=$invoice_detial[0]['chi_name'];
        }


        $html = '<table>

  <tr>

    <!-- logo, need to add "/public" if production. -->

    <td style="width: 53%;"><img height="50" src="'.base_url().'assets/img/logo2.jpg"></td>

    <!-- invoice no. and date -->

    <td style="width: 47%;" align="right">

      <table>

        <tr>

          <td style="width: 57%;" align="left">

            <span style="color: '.THEME_COLOR.'; line-height: 50%;">收據號碼 Receipt No:</span>

          </td>

          <td style="width: 43%" align="right">

            '.$receipt_row->receipt_num.'

          </td>

        </tr>

        <tr>

          <td style="width: 57%" align="left">

            <span style="color: '.THEME_COLOR.';line-height: 50%;">日期Date:</span>

          </td>

          <td style="width: 43%" align="right">

             
             '.date('Y/m/d' ,$invoice_detial[0]['issued_date']).'

          </td>

        </tr>

        <tr>

          <td style="width: 57%" align="left">

            <span style="color: '.THEME_COLOR.';line-height: 50%;">分店Center:</span>

          </td>

          <td style="width: 43%" align="right">

             '.$invoice_detial[0]['shopname'].'

          </td>

        </tr>

      </table>

    </td>

  </tr>



</table>

<!-- Will omit the first br, br means a new line. Then the system can only read the second br as the first newline. -->

<br><br>



<br><br>

<table>

  <tr>

    <td align="center" style="width: 100%; font-size: 20px;">

      <h1><span style="font-size: x-large; color: {$THEME_COLOR_2};">正式收據 RECEIPT</span></h1>

    </td>

  </tr>

</table>

<table>

  <tr>

    <td><span style="font-size: 12px;">客戶 : '.$pdfShowName.'('.$invoice_detial[0]['mobile'].')'.'</span><br></td>

  </tr>

</table>

<!-- Attribute Labels -->

<table>

  <tr>

    <!-- Do not adjust any indentation -->

    <td style="width:55%;" align="center" bgcolor="'.THEME_THEAD_BGCOLOR.'">

      <span></span><br><strong style="font-size: 16px; color: '.THEME_COLOR.';">項目</strong><br><span style="color: '.THEME_COLOR.';">Item Description</span><br><span></span>

    </td>

<!--
    <td style="width:15%;" align="center" bgcolor="'.THEME_THEAD_BGCOLOR.'">

      <span></span><br><span style="font-size: 16px; color: '.THEME_COLOR.';">收費</span><br><span style="color: '.THEME_COLOR.';">Price</span><br><span></span>

    </td>
-->
    <td style="width:15%;" align="center" bgcolor="'.THEME_THEAD_BGCOLOR.'">

      <span></span><br><span style="font-size: 16px; color: '.THEME_COLOR.';">數量</span><br><span style="color: '.THEME_COLOR.';">Quantity</span><br><span></span>

    </td>

    <td style="width:15%;" align="center" bgcolor="'.THEME_THEAD_BGCOLOR.'">

      <span></span><br><span style="font-size: 16px; color: '.THEME_COLOR.';">折扣</span><br><span style="color: '.THEME_COLOR.';">Discount</span><br><span></span>

    </td>

    <td style="width:15%;" align="center" bgcolor="'.THEME_THEAD_BGCOLOR.'">

      <span></span><br><span style="font-size: 16px; color: '.THEME_COLOR.';">實價</span><br><span style="color: '.THEME_COLOR.';">Total</span><br><span></span>

    </td>

  </tr>

  <tr>

    <td style="width:100%; line-height: 20%;" align="center"><table style="border-bottom: 1px solid '.THEME_THEAD_COLOR_BORDER_BOTTOM.';"><tr><td align="center"></td></tr></table></td>

  </tr>

</table>
'.$invoice_item_detail.'

<br>

<br>

<br>

<br>

<!-- Double line, each line will be illustrated by table border-bottom property-->

<!-- Spaces between line can be adjusted by line-height -->

<table>

  <tr>

    <td style="width: 82%; line-height: 150%;" align="right">

      <span style="color: '.THEME_COLOR.'">應繳款項總額 TOTAL AMOUNT DUE: </span>

    </td>

    <td style="width: 18%; line-height: 150%;" align="center">

      <table style="border-bottom: 1px solid '.THEME_THEAD_COLOR_BORDER_BOTTOM.'">

        <tr>

          <td align="center">$'.$invoice_detial[0]['total_amount_due'].'</td>

        </tr>

      </table>

    </td>

  </tr>

  <tr>

    <td style="width: 82%; line-height: 35%;" align="right">

    </td>

    <td style="width: 18%; line-height: 35%;" align="center">

      <table style="border-bottom: 1px solid '.THEME_THEAD_COLOR_BORDER_BOTTOM.'">

        <tr>

          <td align="center"></td>

        </tr>

      </table>

    </td>

  </tr>

</table>
<br>
<br>
<table style="border-bottom: 1px dashed '.THEME_THEAD_COLOR_BORDER_BOTTOM.'">

    '.$payment_html.'

</table>


<table>

  <tr>

    <td><span>美容師 : '.$invoice_detial[0]['therapist_name'].'</span><br></td>

  </tr>

</table>

<table>

  <tr>

    <td><span>顧問 : '.$invoice_detial[0]['consultant_name'].'</span><br></td>

  </tr>

</table>

<br>

<br>

';

// output the HTML content
  $pdf->writeHTML($html, true, false, true, false, '');
  $pdf->AddPage();
  $pdf->writeHTML($html, true, false, true, false, '');
        //end write html content


        $pdf->Output('pdfexample.pdf', 'I'); 
    }
    //end createReceipt
    
    // createRefund
    public function createRefund(){
       $invoice_num = $this->uri->segment(4);

        //May be not use
        $receipt_row=$this->invoice_model->getReceiptRow($invoice_num);
        //End May be not use

        $refund_list=$this->invoice_model->getRefundRow($invoice_num);


        $invoice_detial=$this->invoice_model->getInvoiceWithDetail($invoice_num);

        $payment_list=$this->invoice_model->getInvoicePayment($invoice_num);
        //prepare invoice item
        $invoice_item_list=$this->invoice_model->getInvoiceItemDetail($invoice_num);
        $invoice_item_detail="";

        $totalPayment=0;
        foreach ($payment_list as $payment_list_row) {
          $totalPayment=$totalPayment+$payment_list_row->payment_amount;

        }

        $totalRefund=0;
        // foreach ($refund_list as $refund_list_row) {
        //   // $totalRefund=$totalRefund+$refund_list_row->refund_amount;
        //   $totalRefund=$totalRefund+1;
          
        // }
        $totalRefund=$refund_list->refund_amount;

        foreach ($invoice_item_list as $invoice_item_row) {

          $outqty=0;
          if (floor($invoice_item_row->qty) == $invoice_item_row->qty){
            $outqty=(int)$invoice_item_row->qty;
          }
          else{
            $outqty=$invoice_item_row->qty;
          }
          $outdiscount="-";
          // if(!is_null($invoice_item_row->discount)){
          if($invoice_item_row->discount!=0.00){
            $outdiscount=$invoice_item_row->discount;
          }

          //for create remark of not
          $print_remark='';
          if(!empty($invoice_item_row->remark)){
            $print_remark=' ('.$invoice_item_row->remark.') ';
          }
          
          //end for create remark of not


          $invoice_item_detail=$invoice_item_detail.'<table style="border-bottom: 1px solid '.THEME_ITEM_COLOR_BORDER_BOTTOM.';">

  <!-- tr as padding -->

  <tr><td style="line-height: 45%;"></td></tr>

  <tr>

    <td style="width: 55%; line-height: 150%;">'.$invoice_item_row->item_name.$print_remark.'

    </td>

    <!--<td style="width:15%; line-height:150%;" align="center">$'.$invoice_item_row->price.'</td>-->

    <td style="width:15%; line-height:150%;" align="center">'.$outqty.'</td>

    <td style="width:15%; line-height:150%;" align="center">'.$outdiscount.'</td>

    <td style="width:15%; line-height:150%;" align="center">$'.$invoice_item_row->amount.'</td>

  </tr>

  <!--end data row-->

  <!-- tr as padding -->

  <tr><td style="line-height: 45%;"></td></tr>

</table>
          ';
        }//foreach ($invoice_item_list as $invoice_item_row) {


        $payment_html="";
        foreach ($payment_list as $payment_list_row) {
          // if(!empty($payment_list_row->payment_amount)){
          if(ceil($payment_list_row->payment_amount)!=0){
          //round up to detect floating point zero

          $payment_method="";
          if($payment_list_row->payment_type==1){
            $payment_method="現金";
          }
          if($payment_list_row->payment_type==2){
            $payment_method="Jarvix";
          }
          if($payment_list_row->payment_type==3){
            $payment_method="EPS";
          }
          if($payment_list_row->payment_type==4){
            $payment_method="CUP";
          }
          if($payment_list_row->payment_type==5){
            $payment_method="VISA";
          }
          if($payment_list_row->payment_type==6){
            $payment_method="MASTER";
          }
          if($payment_list_row->payment_type==7){
            $payment_method="use packages";
          }
          if($payment_list_row->payment_type==8){
            $payment_method="HSBC 分期";
          }
          if($payment_list_row->payment_type==9){
            $payment_method="FPS";
          }
          if($payment_list_row->payment_type==10){
            $payment_method="Payme";
          }  
          if($payment_list_row->payment_type==11){
            $payment_method="積分";
          }

            $payment_html=$payment_html.'<tr>

        <td style="width: 86%; line-height: 150%;" align="left"><span>付款方式：'.$payment_method.'</span></td>

        <td style="width: 14%; line-height: 150%;" align="center"><span>'.$payment_list_row->payment_amount.'</span></td>

    </tr>';
          }//if(!empty($payment_list_row->payment_amount)){
        }//foreach ($payment_list as $payment_list_row) {

//         $invoice_item_detail=

//         '<table style="border-bottom: 1px solid {$THEME_ITEM_COLOR_BORDER_BOTTOM};">

//   <!-- tr as padding -->

//   <tr><td style="line-height: 45%;"></td></tr>

//   <tr>

//     <td style="width: 40%; line-height: 150%;">{$COURSE_NAME}<br />

//       {$COURSE_TIMESLOT_HTML_STRING}

//     </td>

//     <td style="width:15%; line-height: {$COURSE_TIMESLOT_ROW_TIMES_150}%;" align="center">{$PRICE}</td>

//     <td style="width:15%; line-height: {$COURSE_TIMESLOT_ROW_TIMES_150}%;" align="center">{$QTY}</td>

//     <td style="width:15%; line-height: {$COURSE_TIMESLOT_ROW_TIMES_150}%;" align="center">{$DISCOUNT}</td>

//     <td style="width:15%; line-height: {$COURSE_TIMESLOT_ROW_TIMES_150}%;" align="center">{$TOTAL}</td>

//   </tr>

//   <!--end data row-->

//   <!-- tr as padding -->

//   <tr><td style="line-height: 45%;"></td></tr>

// </table>';
        //end prepare invoice item



        // echo "invoice_num";  
        // echo $invoice_num; 
        ob_end_clean();
        header("Content-Encoding: None", true);


        $this->load->library('Pdf');
        $pdf = new Pdf('P', 'mm', 'A4', true, 'UTF-8', false);
        $pdf->SetFont('msungstdlight', '', 10);
        $pdf->SetTitle('Beauty Artech Invoice');
        $pdf->SetTitle('Beauty Artech - Invoice');
        $pdf->SetSubject('Invoice');
        $pdf->SetKeywords('Beauty Artech, Invoice');
        $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(false);
        // $pdf->SetHeaderMargin(30);
        // $pdf->SetTopMargin(20);
        // $pdf->setFooterMargin(20);
        // $pdf->SetAutoPageBreak(true);
        // $pdf->SetAuthor('Author');
        // $pdf->SetMargins(1, 1, 1);
        $pdf->AddPage('P', 'LETTER');
        // $pdf->SetDisplayMode('real', 'default');
        // $pdf->Write(5, 'CodeIgniter TCPDF Integration');
        //write html content
        // $pdf->AddPage();

        $pdfShowName="";
        //prefer to show display name first
        if(!empty($invoice_detial[0]['display_name'])){
          $pdfShowName=$invoice_detial[0]['display_name'];
        }
        else{
           $pdfShowName=$invoice_detial[0]['chi_name'];
        }



        $html = '<table>

  <tr>

    <!-- logo, need to add "/public" if production. -->

    <td style="width: 53%;"><img height="50" src="'.base_url().'assets/img/logo2.jpg"></td>

    <!-- invoice no. and date -->

    <td style="width: 47%;" align="right">

      <table>

        <tr>

          <td style="width: 57%;" align="left">

            <span style="color: '.THEME_COLOR.'; line-height: 50%;">收據號碼 Receipt No:</span>

          </td>

          <td style="width: 43%" align="right">

            '.$refund_list->refund_num.'

          </td>

        </tr>

        <tr>

          <td style="width: 57%" align="left">

            <span style="color: '.THEME_COLOR.';line-height: 50%;">日期Date:</span>

          </td>

          <td style="width: 43%" align="right">

             '.date('Y/m/d' ,$refund_list->refund_date).'

          </td>

        </tr>

        <tr>

          <td style="width: 57%" align="left">

            <span style="color: '.THEME_COLOR.';line-height: 50%;">分店Center:</span>

          </td>

          <td style="width: 43%" align="right">

             '.$invoice_detial[0]['shopname'].'

          </td>

        </tr>

      </table>

    </td>

  </tr>



</table>

<!-- Will omit the first br, br means a new line. Then the system can only read the second br as the first newline. -->

<br><br>



<br><br>

<table>

  <tr>

    <td align="center" style="width: 100%; font-size: 20px;">

      <h1><span style="font-size: x-large; color: {$THEME_COLOR_2};">正式收據 RECEIPT</span></h1>

    </td>

  </tr>

</table>

<table>

  <tr>

    <td><span style="font-size: 12px;">客戶 : '.$pdfShowName.'('.$invoice_detial[0]['mobile'].')'.'</span><br></td>

  </tr>

</table>

<!-- Attribute Labels -->

<table>

  <tr>

    <!-- Do not adjust any indentation -->

    <td style="width:55%;" align="center" bgcolor="'.THEME_THEAD_BGCOLOR.'">

      <span></span><br><strong style="font-size: 16px; color: '.THEME_COLOR.';">項目</strong><br><span style="color: '.THEME_COLOR.';">Item Description</span><br><span></span>

    </td>

<!--
    <td style="width:15%;" align="center" bgcolor="'.THEME_THEAD_BGCOLOR.'">

      <span></span><br><span style="font-size: 16px; color: '.THEME_COLOR.';">收費</span><br><span style="color: '.THEME_COLOR.';">Price</span><br><span></span>

    </td>
-->

    <td style="width:15%;" align="center" bgcolor="'.THEME_THEAD_BGCOLOR.'">

      <span></span><br><span style="font-size: 16px; color: '.THEME_COLOR.';">數量</span><br><span style="color: '.THEME_COLOR.';">Quantity</span><br><span></span>

    </td>

    <td style="width:15%;" align="center" bgcolor="'.THEME_THEAD_BGCOLOR.'">

      <span></span><br><span style="font-size: 16px; color: '.THEME_COLOR.';">折扣</span><br><span style="color: '.THEME_COLOR.';">Discount</span><br><span></span>

    </td>

    <td style="width:15%;" align="center" bgcolor="'.THEME_THEAD_BGCOLOR.'">

      <span></span><br><span style="font-size: 16px; color: '.THEME_COLOR.';">實價</span><br><span style="color: '.THEME_COLOR.';">Total</span><br><span></span>

    </td>

  </tr>

  <tr>

    <td style="width:100%; line-height: 20%;" align="center"><table style="border-bottom: 1px solid '.THEME_THEAD_COLOR_BORDER_BOTTOM.';"><tr><td align="center"></td></tr></table></td>

  </tr>

</table>
'.$invoice_item_detail.'

<br>

<br>

<br>

<br>

<!-- Double line, each line will be illustrated by table border-bottom property-->

<!-- Spaces between line can be adjusted by line-height -->

<table>

  <tr>

    <td style="width: 82%; line-height: 150%;" align="right">

      <span style="color: '.THEME_COLOR.'">應繳款項總額 TOTAL AMOUNT DUE: </span>

    </td>

    <td style="width: 18%; line-height: 150%;" align="center">

      <table style="border-bottom: 1px solid '.THEME_THEAD_COLOR_BORDER_BOTTOM.'">

        <tr>

          <td align="center">$'.$invoice_detial[0]['total_amount_due'].'</td>

        </tr>

      </table>

    </td>

  </tr>

  <tr>

    <td style="width: 82%; line-height: 35%;" align="right">

    </td>

    <td style="width: 18%; line-height: 35%;" align="center">

      <table style="border-bottom: 1px solid '.THEME_THEAD_COLOR_BORDER_BOTTOM.'">

        <tr>

          <td align="center"></td>

        </tr>

      </table>

    </td>

  </tr>

</table>
<br>
<br>

<table>

  <tr>

    <td style="width: 82%; line-height: 150%;" align="right">

      <span style="color: '.THEME_COLOR.'">已繳款項 AMOUNT PAID: </span>

    </td>

    <td style="width: 18%; line-height: 150%;" align="center">

      <table style="border-bottom: 1px solid '.THEME_THEAD_COLOR_BORDER_BOTTOM.'">

        <tr>

          <td align="center">$'.number_format((float)$totalPayment, 2, '.', '').'</td>

        </tr>

      </table>

    </td>

  </tr>

  

</table>
<br>
<br>

<table>

  <tr>

    <td style="width: 82%; line-height: 150%;" align="right">

      <span style="color: '.THEME_COLOR.'">退還款項 REFUND AMOUNT: </span>

    </td>

    <td style="width: 18%; line-height: 150%;" align="center">

      <table style="border-bottom: 1px solid '.THEME_THEAD_COLOR_BORDER_BOTTOM.'">

        <tr>

          <td align="center">$'.$totalRefund.'</td>

        </tr>

      </table>

    </td>

  </tr>

  

</table>

<br>
<br>



<table>

  <tr>

    <td><span>美容師 : '.$invoice_detial[0]['therapist_name'].'</span><br></td>

  </tr>

</table>

<table>

  <tr>

    <td><span>顧問 : '.$invoice_detial[0]['consultant_name'].'</span><br></td>

  </tr>

</table>

<br>

<br>

';

// output the HTML content
  $pdf->writeHTML($html, true, false, true, false, '');
  $pdf->AddPage();
  $pdf->writeHTML($html, true, false, true, false, '');
        //end write html content


        $pdf->Output('pdfexample.pdf', 'I'); 
    
    }

    //end createRefund

        public function index()
	{

        $userid = $this->session->userdata('userid');
        // $user_data = $this->session->userdata('USER_ID');

        if(empty($userid)){
            // $this->load->view('test1');
            redirect('/at-cms/login', 'refresh');
        }
        $this->load->helper('url');
        //testing
        // $currentURL = current_url(); //for simple URL
        // $params = $_SERVER['QUERY_STRING']; //for parameters
        // $fullURL = $currentURL . '?' . $params; //full URL with parameter
        //end testing


        //for pagination    
         // or you can autoload it in config

// print base_url($this->uri->uri_string());
        // $config["base_url"] = base_url() . "adminmain/index/";
        $config["base_url"] =  base_url() ."at-cms/folderadmin/index";
        // $config["total_rows"] = $this->Countries->record_count();
        
        // $config["total_rows"] = $this->shop_model->record_count();
        
        $config["uri_segment"] = 4;
        $config["per_page"] = 10;
        $config["page"] = 0;

        // $page = ($this->uri->segment(3))? $this->uri->segment(3) : 0;

        $this->load->helper('url');
        if(!empty($this->uri->segment(4))){
            $page = $this->uri->segment(4);  
        }
        else{
            $page = $config["page"];
        }
        // get 11
        if(!empty($this->uri->segment(5))){
            $perpage = $this->uri->segment(5);  
        }
        else{
            $perpage = $config["per_page"];
        }
        // $perpage = $this->uri->segment(4);

        // $choice = $config["total_rows"] / $perpage;
        // $config["num_links"] = round($choice);
        // $config["num_links"] = ceil($choice);
        
        //end for pagination

        // $this->load->view('welcome_message');
        $data = array(
                'title' => 'My Title',
                'heading' => 'My Heading',
                'message' => 'My Message'
            );        
        // $data['total_rows']=$config["total_rows"];
        $data['page']=$page;
        $data['perpage']=$perpage;
        // $data['num_links'] = $config["num_links"];
        // $data['record']=$this->folder_model->getfolderdata($perpage, $page);
        $data['base_url'] =$config["base_url"];
        $data['fullName'] = $this->session->userdata('fullName');  
        $count=1;
        // foreach($data['record'] as $row){
        //     $data['level1'][$count]=$this->folder_model->getlevel1data($row->id);
        //     $data['level1Record'][$count]=$this->folder_model->getflodercount($row->id);
        //     $countLevel2=1;
        //     foreach($data['level1'][$count] as $secondrow){
        //         $data['level2'][$count][$countLevel2]=$this->folder_model->getlevel2data($secondrow->id);
        //         $countLevel3=1;
        //         foreach($data['level2'][$count][$countLevel2] as $thirdrow){
        //             $data['level3'][$count][$countLevel2][$countLevel3]=$this->folder_model->getlevel3data($thirdrow->id);
        //             $countLevel3=$countLevel3+1;
        //         }
        //         $countLevel2=$countLevel2+1;
        //     }
        //     $count++;

        // }
        
        //for side bar menu
        // $data['shop']=$this->shop_model->getAllShop();
        $data['currentshop']= $this->session->userdata('currentshop');
        //end for side bar menu


        //get all shop data
        // $data["invoice_list"] = $this->invoice_model->getInvoiceAll($data['currentshop']);

        //end get all shop data


        $this->load->view('/admin/brinvoiceview',$data);
        // $contactusdata = $this->contactdetails_model->getContactus();
        //     $this->breadcrumbs[] = array (
        //         'label' => '聯絡我們',
        //         'link' => ''
        //     );
        //     $this->twig->display('main', array(
        //         'path' => array(
        //             'vendor' => $this->config->item('vendor_path'),
        //             'script' => $this->config->item('script_path'),
        //             'css' => $this->config->item('css_path'),
        //             'image' => $this->config->item('image_path')
        //         ),
        //         'breadcrumbs' => $this->breadcrumbs,
        //         'menus' => $this->menu,
        //         'contactusdata'=>$contactusdata,
        //         'session' => 'contact',
        //         'contactus' => $this->contactus,
        //         'page' => 'index'
        //     ));
        }


        // end added pdf function



        public function add(){
            $userid = $this->session->userdata('userid');
            // $user_data = $this->session->userdata('USER_ID');

            if(empty($userid)){
            // $this->load->view('test1');
                redirect('/at-cms/login', 'refresh');
            }
            $this->load->helper('url');

            //old data 
             $config["base_url"] =  base_url() ."at-cms/folderadmin/index";
            // $config["total_rows"] = $this->Countries->record_count();
            
            // $config["total_rows"] = $this->shop_model->record_count();
            
            $config["uri_segment"] = 4;
            $config["per_page"] = 10;
            $config["page"] = 0;

            // $page = ($this->uri->segment(3))? $this->uri->segment(3) : 0;

            $this->load->helper('url');
            if(!empty($this->uri->segment(4))){
                $page = $this->uri->segment(4);  
            }
            else{
                $page = $config["page"];
            }
            // get 11
            if(!empty($this->uri->segment(5))){
                $perpage = $this->uri->segment(5);  
            }
            else{
                $perpage = $config["per_page"];
            }
            // $perpage = $this->uri->segment(4);

            // $choice = $config["total_rows"] / $perpage;
            // $config["num_links"] = round($choice);
            // $config["num_links"] = ceil($choice);
            
            //end for pagination

            // $this->load->view('welcome_message');
            $data = array(
                    'title' => 'My Title',
                    'heading' => 'My Heading',
                    'message' => 'My Message'
                );        
            // $data['total_rows']=$config["total_rows"];
            $data['page']=$page;
            $data['perpage']=$perpage;
            // $data['num_links'] = $config["num_links"];
            // $data['record']=$this->folder_model->getfolderdata($perpage, $page);
            $data['base_url'] =$config["base_url"];
            $data['fullName'] = $this->session->userdata('fullName');  
            $count=1;

            //end old data

            //for side bar menu
            $data['shop']=$this->shop_model->getAllShop();
             $data['currentshop']= $this->session->userdata('currentshop');
            //end for side bar menu

              $data['pagemethod']='add';
              //get customer list 
               $data['customerlist']=$this->customer_model->getCustomerAllShop();
               $data['therapistlist']=$this->employee_model->getTheraphistAll($this->session->userdata('currentshop'));
               $data['consultantlist']=$this->employee_model->getConsultantAll($this->session->userdata('currentshop'));
              //end get customer list

            // $data="";
            $this->load->view('/admin/brinvoiceaddview',$data);
        }
        public function edit(){
            $userid = $this->session->userdata('userid');
            // $user_data = $this->session->userdata('USER_ID');

            if(empty($userid)){
            // $this->load->view('test1');
                redirect('/at-cms/login', 'refresh');
            }
            $this->load->helper('url');

            //old data 
             $config["base_url"] =  base_url() ."at-cms/folderadmin/index";
            // $config["total_rows"] = $this->Countries->record_count();
            
            // $config["total_rows"] = $this->shop_model->record_count();
            
            $config["uri_segment"] = 4;
            $config["per_page"] = 10;
            $config["page"] = 0;

            // $page = ($this->uri->segment(3))? $this->uri->segment(3) : 0;

            $this->load->helper('url');
            if(!empty($this->uri->segment(4))){
                $page = $this->uri->segment(4);  
            }
            else{
                $page = $config["page"];
            }
            // get 11
            if(!empty($this->uri->segment(5))){
                $perpage = $this->uri->segment(5);  
            }
            else{
                $perpage = $config["per_page"];
            }
            // $perpage = $this->uri->segment(4);

            // $choice = $config["total_rows"] / $perpage;
            // $config["num_links"] = round($choice);
            // $config["num_links"] = ceil($choice);
            
            //end for pagination

            // $this->load->view('welcome_message');
            $data = array(
                    'title' => 'My Title',
                    'heading' => 'My Heading',
                    'message' => 'My Message'
                );        
            // $data['total_rows']=$config["total_rows"];
            $data['page']=$page;
            $data['perpage']=$perpage;
            // $data['num_links'] = $config["num_links"];
            // $data['record']=$this->folder_model->getfolderdata($perpage, $page);
            $data['base_url'] =$config["base_url"];
            $data['fullName'] = $this->session->userdata('fullName');  
            $count=1;

            //end old data

            //for side bar menu
            $data['shop']=$this->shop_model->getAllShop();
             $data['currentshop']= $this->session->userdata('currentshop');
            //end for side bar menu

              $data['pagemethod']='edit';

            //get shop data
              $id = $this->uri->segment(4);
               $data['shopdetails']=$this->shop_model->getShopbyid($id);
            //end get shop data

            // $data="";
            $this->load->view('/admin/brshopaddview',$data);
        }
        public function detail(){
            $userid = $this->session->userdata('userid');
            // $user_data = $this->session->userdata('USER_ID');

            if(empty($userid)){
            // $this->load->view('test1');
                redirect('/at-cms/login', 'refresh');
            }
            $this->load->helper('url');

            //old data 
             $config["base_url"] =  base_url() ."at-cms/folderadmin/index";
            // $config["total_rows"] = $this->Countries->record_count();
            
            // $config["total_rows"] = $this->shop_model->record_count();
            
            $config["uri_segment"] = 4;
            $config["per_page"] = 10;
            $config["page"] = 0;

            // $page = ($this->uri->segment(3))? $this->uri->segment(3) : 0;

            $this->load->helper('url');
            if(!empty($this->uri->segment(4))){
                $page = $this->uri->segment(4);  
            }
            else{
                $page = $config["page"];
            }
            // get 11
            if(!empty($this->uri->segment(5))){
                $perpage = $this->uri->segment(5);  
            }
            else{
                $perpage = $config["per_page"];
            }
            // $perpage = $this->uri->segment(4);

            // $choice = $config["total_rows"] / $perpage;
            // $config["num_links"] = round($choice);
            // $config["num_links"] = ceil($choice);
            
            //end for pagination

            // $this->load->view('welcome_message');
            $data = array(
                    'title' => 'My Title',
                    'heading' => 'My Heading',
                    'message' => 'My Message'
                );        
            // $data['total_rows']=$config["total_rows"];
            $data['page']=$page;
            $data['perpage']=$perpage;
            // $data['num_links'] = $config["num_links"];
            // $data['record']=$this->folder_model->getfolderdata($perpage, $page);
            $data['base_url'] =$config["base_url"];
            $data['fullName'] = $this->session->userdata('fullName');  
            $count=1;

            //end old data

              $data['pagemethod']='detail';

            //for side bar menu
            $data['shop']=$this->shop_model->getAllShop();
             $data['currentshop']= $this->session->userdata('currentshop');
            //end for side bar menu

            //get shop data
              $id = $this->uri->segment(4);
               $data['shopdetails']=$this->shop_model->getShopbyid($id);
            //end get shop data

            // $data="";
            $this->load->view('/admin/brshopaddview',$data);
        }
        public function dispay(){
            echo "1";
            $this->load->helper('url');
        $page = $this->uri->segment(3);  // get 11
        $perpage = $this->uri->segment(4);
        echo "page";
        echo $page;
        echo "perpage";
        echo $perpage;
        }
}