<?php
  require_once('../postgreSQL/config.php');
  require_once('../postgreSQL/functions.php');
  include "./vendor/tecnickcom/tcpdf/tcpdf.php"; // include_path配下に設置したtcpdf.phpを読み込む
   
  $cart_no = $_GET["cart_no"];
  
  //ＰＤＦ設定
  $orientation = 'Portrait'; // 用紙の向き
  $unit = 'mm'; // 単位
  $format = 'A4'; // 用紙フォーマット
  $unicode = true; // ドキュメントテキストがUnicodeの場合にTRUEとする
  $encoding = 'UTF-8'; // 文字コード
  $diskcache = false; // ディスクキャッシュを使うかどうか  $tcpdf = new TCPDF();
  
  $tcpdf = new TCPDF($orientation, $unit, $format, $unicode, $encoding, $diskcache);
  $tcpdf->setMargins(0, 0, 0);
  $tcpdf->setPrintHeader(false);
  $tcpdf->setPrintFooter(false);
  $tcpdf->SetFont("kozminproregular", "", 9); // デフォルトで用意されている日本語フォントkozminproregular
  $tcpdf->SetFillColor(200);
  
  $lines = 15;    //ページあたりの印字行数
  $margin = 3;    //トップマージン
  
  //データベース接続
  $dbh = connectDb();
  
  //個別請求書の印字
  $sth = $dbh->prepare("select a.id as id, to_char(now(), 'YYYY/MM/DD') as a, coalesce(a.order_no,'') as b, " .
                       "       coalesce(c.name,'') || coalesce(c.name2,'') as c, " .
                       "       a.item as q, coalesce(e.b2b_name1,'') || chr(13) || chr(10) || coalesce(e.b2b_name2,'') as d, " .
                       "       coalesce(e.b2b_size,'') || chr(13) || chr(10) || coalesce(e.b2b_pattern,'') as e, " .
                       "       a.qty as f, a.pcs as g, a.pcs * a.unit_price as h, a.unit_price as i, " .
                       "       '' as j, coalesce(c.send_to,'') as k, coalesce(c.st_zip,'') as l, " .
                       "       coalesce(e.unit_price,0) as list_price, a.cart_no as cart_no, " .
                       "       coalesce(c.st_adrs1,'') as m, coalesce(c.st_adrs2,'') as n, " .
                       "       coalesce(c.st_phone,'') as o, '' as p, coalesce(d.name,'') as r, " .
                       "       coalesce(f.description,'') as s, coalesce(tax_round,0) as tax_round, coalesce(taxation,0) as taxation " .
                       "  from t_cart a " .
                       "  left outer join t_customer c on (c.code || c.branch = substring(a.user_id, 1, 9)) " .
                       "  left outer join t_b2b_user d on (d.code || d.branch || d.clerk = a.user_id) " .
                       "  left outer join t_items e on (e.code = a.item) " .
                       "  left outer join t_payment f on (c.pay_code = f.code) " .
                       " where a.cart_no = " . $cart_no . " " .
                       " order by 1");
  $sth->execute();
  $userData = array();
  while($row = $sth->fetch(PDO::FETCH_ASSOC)){
    $userData[]=array(
    'order_date'    =>$row['a'],
    'order_no'      =>$row['b'],
    'customer_name' =>$row['c'],
    'name'          =>$row['d'],
    'size'          =>$row['e'],
    'qty'           =>$row['r'],
    'pcs'           =>$row['g'],
    'amount'        =>$row['h'],
    'unit_price'    =>$row['i'],
    'rdd'           =>$row['j'],
    'dest'          =>$row['k'],
    'dest_zip'      =>$row['l'],
    'dest_adrs1'    =>$row['m'],
    'dest_adrs2'    =>$row['n'],
    'dest_phone'    =>$row['o'],
    'asap'          =>$row['p'],
    'item'          =>$row['q'],
    'user_name'     =>$row['r'],
    'payment'       =>$row['s'],
    'cart_no'       =>$row['cart_no'],
    'tax_round'     =>$row['tax_round'],
    'list_price'    =>$row['list_price'],
    'taxation'      =>$row['taxation']
    );
  }
  $sth = null;
  
  $tax_rate = 0;
  $szSql = "select rate as rate from t_taxrate where date = (select max(date) from t_taxrate where date <= '" . $userData[0]['order_date'] . "')";
  $sth = $dbh->prepare($szSql);
  $sth->execute();
  while($row = $sth->fetch(PDO::FETCH_ASSOC)){
    $tax_rate = $row['rate'] / 100;
  }
  $sth = null;
  
  $geta = -3.0;
  $i = 0; $j = 0; $p = 0; $m = 0; $n = 0;
  $nAmount = 0;
  $nTax = 0;
  
  for($i=0; $i<count($userData); $i++) {
    $ar  = $userData[$i];
    $nAmount += intval($ar['amount']);
    if($i == count($userData)-1) {
      $nTax = 0;
      switch($ar['tax_round']) {
        case 0: //切り捨て
          $nTax = floor($nAmount * $tax_rate);
          break;
        case 1: //切り上げ
          $nTax = ceil($nAmount * $tax_rate);
          break;
        case 2: //四捨五入
          $nTax = round($nAmount * $tax_rate);
          break;
      }
      if($ar['taxation'] == 1) {  //内税の場合
        $nTax = 0;
      }
    }
  }
  
  for($i=0; $i<count($userData); $i++) {
    
    $ar  = $userData[$i];
    if($j == 0) {
      $tcpdf->AddPage(); // 新しいpdfページを追加
    }
    
    $tcpdf->setFontSize(9);
    $ar = $userData[$i];
    $tcpdf->MultiCell(  9.0, 10, $j+1,                             0, 'L', false, 1,   8.0, 97.5+$margin+$geta+$j*10, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell( 21.5, 10, $ar['item'],                      0, 'L', false, 1,  17.0, 97.5+$margin+$geta+$j*10, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell( 54.0, 10, $ar['name'],                      0, 'L', false, 1,  38.5, 97.5+$margin+$geta+$j*10, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell( 54.0, 10, $ar['size'],                      0, 'L', false, 1,  92.5, 97.5+$margin+$geta+$j*10, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell( 12.5, 10, number_format($ar['list_price']), 0, 'R', false, 1, 146.5, 97.5+$margin+$geta+$j*10, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell( 12.5, 10, number_format($ar['unit_price']), 0, 'R', false, 1, 159.0, 97.5+$margin+$geta+$j*10, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell( 13.0, 10, number_format($ar['pcs']),        0, 'R', false, 1, 171.5, 97.5+$margin+$geta+$j*10, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell( 20.0, 10, number_format($ar['amount']),     0, 'R', false, 1, 184.5, 97.5+$margin+$geta+$j*10, true, 0, false, true, 0, 'M', true);
    $j++;
    
    if($j >= $lines || $i == count($userData)-1) {
      //見出し・罫線印字
      print_head($tcpdf, $ar['order_date'], $ar['order_no'], $ar['customer_name'], $ar['user_name'], $ar['rdd'], 
                 $ar['dest'], $ar['dest_zip'], $ar['dest_adrs1'], $ar['dest_adrs2'], $ar['dest_phone'], $ar['asap'],
                 $nAmount, $nTax, $ar['cart_no'], $p, $margin, $ar['payment'], ($i==count($userData)-1));
      $j = 0;
      $p++;
    }
    
  }
  
  //データベース切断
  $dbh = null;
  
  $file_name = '../mail/attach/' . 'quotation_' . $cart_no . '.pdf';
  $tcpdf->Output('../mail/attach/' . 'quotation_' . $cart_no . '.pdf', 'F'); // pdf表示設定
  
  function print_head($tcpdf, $order_date, $order_no, $customer_name, $user_name, $rdd, $dest, $dest_zip, 
                      $dest_adrs1, $dest_adrs2, $dest_phone, $asap, $amount, $tax, $cart_no, $p, $margin, $payment, $last_page) {
    $geta = -3.0;
    $tcpdf->SetLineStyle(array('width' => 0.02));
    $tcpdf->setFontSize(14);
    $tcpdf->MultiCell( 196.5, 8.0, "お見積書",     0, 'C', false, 1,  8.0, 17.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->setFontSize(13);
    $tcpdf->MultiCell( 196.5, 8.0, $customer_name . " 御中",   0, 'L', false, 1,  8.0, 27.5+$margin+$geta, true, 0, false, true, 0, 'M', true);
    
    $tcpdf->setFontSize(9);
    $tcpdf->MultiCell(  30.0, 5.5, "Q" . $cart_no,    0, 'L', false, 1, 174.0, 27.5+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell( 100.0, 5.5, $user_name . " 様", 0, 'L', false, 1,  40.0, 33.8+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell(  30.0, 5.5, substr($order_date, 0, 4) . " 年 " . substr($order_date, 5, 2) . " 月 " . substr($order_date, 8, 2) . " 日" ,   0, 'L', false, 1, 174.0, 33.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell( 115.0, 5.5, "下記の通り、お見積申し上げます。",   0, 'L', false, 1,  18.0, 38.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    
    $tcpdf->MultiCell(  30.0, 5.5, "お見積金額：",   0, 'L', false, 1,  16.0, 46.5+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->setFontSize(15);
    $tcpdf->MultiCell(  40.0, 5.5, "￥" . mb_convert_kana(number_format($amount + $tax),'A','utf-8'),   0, 'R', false, 1,  36.0, 46.5+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->SetLineStyle(array('width' => 0.3));
    $tcpdf->line( 15.5,  52.5+$margin+$geta, 77.0,  52.5+$margin+$geta);
    
    $tcpdf->setFontSize(8);
    $tcpdf->MultiCell(  13.5, 5.5, "ご担当:",   0, 'R', false, 1,  25.5, 33.8+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell(  27.0, 5.5, "見積番号:", 0, 'R', false, 1, 146.5, 27.5+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell(  27.0, 5.5, "見積日:",   0, 'R', false, 1, 146.5, 32.5+$margin+$geta, true, 0, false, true, 0, 'M', true);
    
    $geta = 5.0;
    $tcpdf->MultiCell(  30.0, 5.0, "納入先:",    0, 'R', false, 1,   8.0, 48.5+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell(  30.0, 5.0, "希望納品日:",0, 'R', false, 1,   8.0, 73.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell(  30.0, 5.0, "取引条件:",  0, 'R', false, 1,   8.0, 78.5+$margin+$geta, true, 0, false, true, 0, 'M', true);
    
    $tcpdf->MultiCell(  14.5, 5.5, "TEL:",      0, 'R', false, 1, 140.0, 61.5+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell(  14.5, 5.5, "FAX:",      0, 'R', false, 1, 140.0, 66.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell(  14.5, 5.5, "E-mail:",   0, 'R', false, 1, 140.0, 70.5+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell(  14.5, 5.5, "登録番号:", 0, 'R', false, 1, 140.0, 75.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    //$tcpdf->MultiCell(  14.5, 5.5, "担当:",   0, 'R', false, 1, 140.0, 83.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    
    $tcpdf->setFontSize(8);
    $tcpdf->MultiCell( 100.0, 5.0, $dest,       0, 'L', false, 1,  40.0, 48.5+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell( 100.0, 5.0, $dest_zip,   0, 'L', false, 1,  40.0, 53.5+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell( 100.0, 5.0, $dest_adrs1, 0, 'L', false, 1,  40.0, 58.5+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell( 100.0, 5.0, $dest_adrs2, 0, 'L', false, 1,  40.0, 63.5+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell( 100.0, 5.0, $dest_phone, 0, 'L', false, 1,  40.0, 68.5+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell( 100.0, 5.0, $rdd,        0, 'L', false, 1,  40.0, 73.5+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell( 100.0, 5.0, $payment,    0, 'L', false, 1,  40.0, 78.5+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->setFontSize(10);
    $tcpdf->MultiCell(  65.0, 7.0, "株式会社",          0, 'L', false, 1, 142.0, 48.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->setFontSize(14);
    $tcpdf->MultiCell(  47.0, 7.0, "アームカンパニー",  0, 'L', false, 1, 157.0, 48.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->setFontSize(9);
    $tcpdf->MultiCell(  58.0, 5.5, "〒 243-0426",                         0, 'L', false, 1, 145.0, 53.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell(  58.0, 5.5, "神奈川県海老名市門沢橋２－７－２９",  0, 'L', false, 1, 145.0, 57.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell(  49.0, 5.5, "046-240-7172",                        0, 'L', false, 1, 156.0, 61.5+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell(  49.0, 5.5, "046-240-7382",                        0, 'L', false, 1, 156.0, 66.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell(  49.0, 5.5, "sales@ahmcompany.com",                0, 'L', false, 1, 156.0, 70.5+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell(  49.0, 5.5, "T2-0109-0100-0269",                   0, 'L', false, 1, 156.0, 75.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    //$tcpdf->MultiCell(  30.0, 5.5, $stuff,  0, 'L', false, 1, 156.0, 83.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    
    $geta = -3.0;
    $tcpdf->setFontSize(7);
    $tcpdf->MultiCell(   9.0, 5.5, "No",          0, 'C', true, 1,   8.0, 92.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell(  21.5, 5.5, "SKU",         0, 'C', true, 1,  17.0, 92.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell(  54.0, 5.5, "品名",        0, 'C', true, 1,  38.5, 92.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell(  54.0, 5.5, "サイズ等",    0, 'C', true, 1,  92.5, 92.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell(  12.5, 5.5, "定価",        0, 'C', true, 1, 146.5, 92.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell(  12.5, 5.5, "単価",        0, 'C', true, 1, 159.0, 92.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell(  13.0, 5.5, "数量",        0, 'C', true, 1, 171.5, 92.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell(  21.0, 5.5, "金額(税抜）", 0, 'C', true, 1, 184.5, 92.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    
    $tcpdf->setFontSize(8);
    $tcpdf->MultiCell( 25, 5.5, "小　計:" ,  0, 'C', false, 1, 145.0, 247.5+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->MultiCell( 25, 5.5, "消費税:",   0, 'C', false, 1, 145.0, 253.0+$margin+$geta, true, 0, false, true, 0, 'M', true); 
    $tcpdf->MultiCell( 25, 5.5, "合　計:",   0, 'C', false, 1, 145.0, 258.5+$margin+$geta, true, 0, false, true, 0, 'M', true);
    
    if($last_page) {
      $tcpdf->setFontSize(9);
      $tcpdf->MultiCell( 20.0, 5.5, number_format($amount),           0, 'R', false, 1, 184.5, 247.5+$margin+$geta, true, 0, false, true, 0, 'M', true);
      $tcpdf->MultiCell( 20.0, 5.5, number_format($tax),              0, 'R', false, 1, 184.5, 253.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
      $tcpdf->MultiCell( 20.0, 5.5, number_format($amount + $tax),    0, 'R', false, 1, 184.5, 258.5+$margin+$geta, true, 0, false, true, 0, 'M', true);
    }
    
    $tcpdf->setFontSize(7);    
    $tcpdf->MultiCell( 21.5, 10.0, "備 考", 0, 'C', false, 1, 17.0, 266.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    $tcpdf->setFontSize(8);
    $tcpdf->MultiCell(165.0, 10.0, $asap,   0, 'L', false, 1, 42.0, 266.0+$margin+$geta, true, 0, false, true, 0, 'M', true);
    
    //横罫線
    $tcpdf->SetLineStyle(array('width' => 0.3));
    $tcpdf->line(  8.0,  92.0+$margin+$geta, 205.5,  92.0+$margin+$geta);
    $tcpdf->SetLineStyle(array('width' => 0.1));
    for($i=0; $i<15; $i++) {
      $tcpdf->line(  8.0,  97.5+$margin+$geta+$i*10,   205.5,  97.5+$margin+$geta+$i*10);
    }
    $tcpdf->SetLineStyle(array('width' => 0.3));
    $tcpdf->line(   8.0,  97.5+$margin+$geta+$i*10,   205.5,  97.5+$margin+$geta+$i*10);
    $tcpdf->SetLineStyle(array('width' => 0.1));
    $tcpdf->line( 146.5,  97.5+$margin+$geta+$i*10+5.5,   205.5,  97.5+$margin+$geta+$i*10+5.5);
    $tcpdf->line( 146.5,  97.5+$margin+$geta+$i*10+11.0,   205.5,  97.5+$margin+$geta+$i*10+11.0);
    $tcpdf->SetLineStyle(array('width' => 0.3));
    $tcpdf->line( 146.5,  97.5+$margin+$geta+$i*10+16.5,   205.5,  97.5+$margin+$geta+$i*10+16.5);
    //備考欄
    $tcpdf->SetLineStyle(array('width' => 0.1));
    $tcpdf->line(  17.0, 266.0+$margin+$geta,   205.5, 266.0+$margin+$geta);
    $tcpdf->line(  17.0, 276.5+$margin+$geta,   205.5, 276.5+$margin+$geta);
    //タイトル下の二重線
    $tcpdf->line(  92.5,  24.0+$margin+$geta,   121.0,  24.0+$margin+$geta);
    $tcpdf->line(  92.5,  24.4+$margin+$geta,   121.0,  24.4+$margin+$geta);
    $tcpdf->line(   8.0,  33.8+$margin+$geta,   135.0,  33.8+$margin+$geta);
    
    //縦罫線
    $tcpdf->SetLineStyle(array('width' => 0.3));
    $tcpdf->line(   8.0,  92.0+$margin+$geta,  8.0, 247.5+$margin+$geta);
    $tcpdf->SetLineStyle(array('width' => 0.1));
    $tcpdf->line(  17.0,  92.0+$margin+$geta,  17.0, 247.5+$margin+$geta);
    $tcpdf->line(  38.5,  92.0+$margin+$geta,  38.5, 247.5+$margin+$geta);
    $tcpdf->line(  92.5,  92.0+$margin+$geta,  92.5, 247.5+$margin+$geta);
    $tcpdf->line( 146.5,  92.0+$margin+$geta, 146.5, 264.0+$margin+$geta);
    $tcpdf->line( 159.0,  92.0+$margin+$geta, 159.0, 247.5+$margin+$geta);
    $tcpdf->line( 171.5,  92.0+$margin+$geta, 171.5, 264.0+$margin+$geta);
    $tcpdf->line( 185.0,  92.0+$margin+$geta, 185.0, 247.5+$margin+$geta);
    $tcpdf->SetLineStyle(array('width' => 0.3));
    $tcpdf->line( 146.5, 247.5+$margin+$geta, 146.5, 264.0+$margin+$geta);
    $tcpdf->line( 205.5,  92.0+$margin+$geta, 205.5, 264.0+$margin+$geta);
    //備考欄
    $tcpdf->SetLineStyle(array('width' => 0.1));
    $tcpdf->line(  17.0, 266.0+$margin+$geta,  17.0, 276.5+$margin+$geta);
    $tcpdf->line(  38.5, 266.0+$margin+$geta,  38.5, 276.5+$margin+$geta);
    $tcpdf->line( 205.5, 266.0+$margin+$geta, 205.5, 276.5+$margin+$geta);
  }
  
  $retData = array();
  $retData[]= $file_name ;
  
  //jsonとして出力
  header('Content-type: application/json');
  echo json_encode($retData);
  
  
?>