<?php
  include '_connect.php';

  // if ($_GET['CROP_NAME'] != "") {
  //   $DMY_1 = $_GET['DMY_1'];
  //   $DMY_2 = $_GET['DMY_2'];
  //   $CROP_NAMEXC = $_GET['CROP_NAME'];
  //   $SUMXX = $_GET['icar'];
  //   $SUM_PAPER = "";
  //   $SUM_PANCIL = "";
  // }
  // if ($_POST['CROP_NAME'] != "") {
  //   $DMY_1 = $_POST['DMY_1'];
  //   $DMY_2 = $_POST['DMY_2'];
  //   $CROP_NAMEXC = $_POST['CROP_NAME'];
  //   $SUMXX = $_POST['icar'];
  //   $SUM_PAPER = $_POST['SUM_PAPER'];
  //   $SUM_PANCIL = $_POST['SUM_PANCIL'];
  // }

  $DMY_1 = $_POST['DMY_1'];
  $DMY_2 = $_POST['DMY_2'];
  $CROP_NAMEXC = $_POST['CROP_NAME'];
  $SUMXX = $_POST['icar'];
  $SUM_PAPER = $_POST['SUM_PAPER'];
  $SUM_PANCIL = $_POST['SUM_PANCIL'];

  echo "$DMY_1";
  echo "$DMY_2";
  echo "$CROP_NAMEXC";
  echo "$SUMXX";
  echo "$SUM_PAPER";
  echo "$SUM_PANCIL";

  $ACT_DATE = date('Y-m-d');
  //บริษัท
  $query = "SELECT * FROM [EHONGDB].[dbo].[CROPORATION] WHERE CROP_NAME = '$CROP_NAMEXC'";
  // echo "$query";
  $select=$conn->prepare($query);
  $select->execute();
  $row = $select->fetch();
  $CROP_NOX = $row['CROP_NO'];
  //คิดเงิน
  function Convert($amount_number)
  {
      $amount_number = number_format($amount_number, 2, ".","");
      $pt = strpos($amount_number , ".");
      $number = $fraction = "";
      if ($pt === false)
          $number = $amount_number;
      else
      {
          $number = substr($amount_number, 0, $pt);
          $fraction = substr($amount_number, $pt + 1);
      }
      $ret = "";
      $baht = ReadNumber ($number);
      if ($baht != "")
          $ret .= $baht . "บาท";

      $satang = ReadNumber($fraction);
      if ($satang != "")
          $ret .=  $satang . "สตางค์";
      else
          $ret .= "ถ้วน";
      return $ret;
  }
  function ReadNumber($number)
  {
      $position_call = array("แสน", "หมื่น", "พัน", "ร้อย", "สิบ", "");
      $number_call = array("", "หนึ่ง", "สอง", "สาม", "สี่", "ห้า", "หก", "เจ็ด", "แปด", "เก้า");
      $number = $number + 0;
      $ret = "";
      if ($number == 0) return $ret;
      if ($number > 1000000)
      {
          $ret .= ReadNumber(intval($number / 1000000)) . "ล้าน";
          $number = intval(fmod($number, 1000000));
      }
      $divider = 100000;
      $pos = 0;
      while($number > 0)
      {
          $d = intval($number / $divider);
          $ret .= (($divider == 10) && ($d == 2)) ? "ยี่" :
              ((($divider == 10) && ($d == 1)) ? "" :
              ((($divider == 1) && ($d == 1) && ($ret != "")) ? "เอ็ด" : $number_call[$d]));
          $ret .= ($d ? $position_call[$pos] : "");
          $number = $number % $divider;
          $divider = $divider / 10;
          $pos++;
      }
      return $ret;
  }
  // เลขบิล
  $query = "SELECT MAX(ACT_NO2) ACT_NO2 FROM [CONFIG].[dbo].[ACT_RECEIPT]";
  $select=$conn->prepare($query);
  $select->execute();
  $row_3 = $select->fetch();
  // echo "$query";
  $ACTx = $row_3['ACT_NO2']; //ค่าเดิมที่อ่านมา
  $old = str_split($ACTx,4);
  $date = $old[0];
  $number = $old[1];
  $newdate = date("ym");
  if($date!=$newdate)
  {
  		$number = 1;
  		$number = sprintf("%03d",$number);
  }else{
  		$number = sprintf("%03d",++$number);;
  }
  $ACT_NO = $CROP_NOX.$newdate.$number;
  $ACT_NO2 = $newdate.$number;
  // echo $ACT_NO;
?>
<?php
$sql = "SELECT a.bill_no as xbill_no,a.bill_date as xbill_date,*,a.rectime as xrectime from [EHONGDB].[dbo].[reg_billdata] a
left outer join(SELECT * FROM [EHONGDB].[dbo].[reg_bill]) b on(a.bill_no=b.bill_no)
left outer join(SELECT * FROM [EHONGDB].[dbo].[BRANCH]) c on (a.BR_ID=c.BR_ID)
left outer join(SELECT * FROM [EHONGDB].[dbo].[REG_GWORK]) d on(b.g_id=d.G_ID)
left outer join(SELECT BILL_NO as EBILL_NO,ACT_DATE FROM [CONFIG].[dbo].[ACT_EXPOST]) e on(a.bill_no=e.EBILL_NO)
left outer JOIN(SELECT BILL_NO as sBILL_NO,ACT_STATUS,ACT_USDATE FROM CONFIG.dbo.ACT_STATUSEXPOST) dx on(a.bill_no=dx.sBILL_NO)
where a.status<>'V' and fee_no='11' and a.bill_date between '$DMY_1' and '$DMY_2'
and ACT_DATE IS NULL
order by a.rectime,a.br_id,a.bill_date";
echo $sql;
$select=$conn->prepare($sql);
$select->execute();
$item = 1;
$B10 = '10.00';
$SUMFEE_SUMPRICE = '0';
$SUMFEE_PRICE = '0';
$SUMB10 = '0';
$SUMsB10 = '0';
$SUM_x1 = '0';
$SUM_x2 = '0';
$FEE_PRICE = '0';
$SUMxFEE_PRICE = '0';
while($fetch=$select->fetch()){
  include 'act_sum_1.php';
  if ($CROP_NAME == "$CROP_NAMEXC") {
    $FEE_PRICE = number_format($fetch['FEE_PRICE'],2);
    if ($fetch['FEE_SUMPRICE'] != '0') {
      if ($fetch['CC_SIZE'] < '75') {
        $FEE_SUMPRICE = number_format(190,2);
      }
      if ($fetch['CC_SIZE'] >= '75') {
        $FEE_SUMPRICE = number_format(350,2);
      }
      if ($fetch['CC_SIZE'] > '125') {
        $FEE_SUMPRICE = number_format(450,2);
      }
      if ($fetch['CC_SIZE'] > '150') {
        $FEE_SUMPRICE = number_format(700,2);
      }
      $SUM_1 = number_format(($FEE_SUMPRICE-$B10-$FEE_PRICE)/1.07,2);
      $SUM_2 = number_format(($SUM_1*0.07),2);
      $SUMFEE_SUMPRICE += $FEE_SUMPRICE;
      $SUM_x1 += $SUM_1;
      $SUM_x2 += $SUM_2;
    }
    else {
      $FEE_SUMPRICE = '';
      $SUM_1 = '';
      $SUM_2 = '';
    }
    $SUMxFEE_PRICE += $FEE_PRICE;
    $SUMsB10 += $B10;
    //บันทึกลง ACT_EXPOST เก็บรายงาน
    $strSQL = "INSERT INTO CONFIG.dbo.ACT_EXPOST";
    $strSQL .="(BILL_NO,BILL_DATE,BR_ID,BR_NAME,CT_NO,PS_NAME,G_ID,FEE_STATUSX,FEE_STATUSX1,CROP_NAME,CROP_NAME1,BRAND_NAME,SERIAL_NO,CC_SIZE,ACT_DATE,ACT_NO) ";
    $strSQL .="VALUES ";
    $strSQL .="('".$fetch['BILL_NO']."','".$fetch['BILL_DATE']."','".$fetch['BR_ID']."','".$fetch['BR_NAME']."','".$fetch['CT_NO']."',
    '".$fetch['PS_NAME']."','".$fetch['g_id']."','$FEE_STATUSX','$FEE_STATUSX1','".$fetch['CROP_NAME']."',
    '$CROP_NAME','".$fetch['BRAND_NAME']."','".$fetch['SERIAL_NO']."','".$fetch['CC_SIZE']."','$ACT_DATE',
    '$ACT_NO')";
    // $result = $conn->query($strSQL);
    echo $strSQL;
    if ($item == "1") {
      $ACT_DATE1 = $fetch['BILL_DATE'];
    }
    $ACT_DATE2 = $fetch['BILL_DATE'];
    // echo "$item";
    // echo $ACT_DATE2;
?>
<?php $item++; } }
?>
<?php
    $COMPANY = "บริษัท กลางคุ้มครองผู้ประสบภัยจากรถยนต์";
    $PAYER = "นางสาวสวาสดิพร แก้วมาลา";
    $REG_POSITION = "เจ้าหน้าที่ พ.ร.บ.";
    $SUM_PRINCE = $SUMxFEE_PRICE-$SUMsB10;
    // $ACT_DATE1 = '';
    // $ACT_DATE2 = '';
    //บันทึกลง ACT_RECEIPT เก็บรายงาน

    $strSQL = "INSERT INTO CONFIG.dbo.ACT_RECEIPT";
    $strSQL .="(ACT_NO,CROP_NAME,COMPANY,SUMX_PRICE,REG_PRICE,
    SUM_BROKER,SUM_PAPER,SUM_PANCIL,SUM_PRINCE,PAYER,
    REG_POSITION,ACT_DATE1,ACT_DATE2,ACT_DATE,
    ACT_NO2) ";
    $strSQL .="VALUES ";
    $strSQL .="('$ACT_NO','$CROP_NAMEXC','$COMPANY','$SUMXX','$SUMxFEE_PRICE',
    '$SUMsB10','$SUM_PAPER','$SUM_PANCIL','$SUM_PRINCE','$PAYER','$REG_POSITION','$ACT_DATE1','$ACT_DATE2','$ACT_DATE',
    '$ACT_NO2')";
    // $result = $conn->query($strSQL);
    echo $strSQL;
    // echo number_format($SUMxFEE_PRICE,2);
    // echo  "(" .convert($SUMxFEE_PRICE).")";
    // echo $ACT_NO2;
?>
