<?php
include '_connect.php';
$FEE_PRICE = $_POST['FEE_PRICE'];
$BILL_DATE = $_POST['BILL_DATE'];
$BILL_NO = $_POST['BILL_NO'];
$PS_NAME = $_POST['PS_NAME'];
$BRAND_NAME = $_POST['BRAND_NAME'];
$SERIAL_NO = $_POST['SERIAL_NO'];
$CC_SIZE = $_POST['CC_SIZE'];
$NOTE = $_POST['NOTE'];
$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 bill_no as reg_bill_no,plcno,plcdate,plcexp from [EHONGDB].[dbo].[reg_billdatadocument] WHERE FEE_NO = '11' and (plcno is not null) ) ax1 on(ax1.reg_bill_no=a.bill_no)
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)
left outer join(SELECT bill_no as bill_no2,FEE_NO,FEE_NAME,FEE_PRICE AS FEE_PRICE2 FROM [EHONGDB].[dbo].[reg_billdata] WHERE FEE_NO = '12') abc on(a.bill_no=abc.bill_no2)
left outer JOIN(SELECT ACT_ID,FEE_PRICE AS EDITFEE_PRICE, BILL_NO AS EDITBILL_NO,NOTE FROM CONFIG.dbo.ACT_EDIT) edit on(a.bill_no=edit.EDITBILL_NO)
where a.status<>'V' and a.fee_no='11' and a.BILL_NO = '$BILL_NO'
order by a.rectime,a.br_id,a.bill_date";
// echo $sql;
$select=$conn->prepare($sql);
$select->execute();
$fetch=$select->fetch();
$ACT_ID = $fetch['ACT_ID'];
// echo $ACT_ID;
if ($fetch['EDITFEE_PRICE'] == "") {
  // echo "ไม่มีข้อมูล เพิ่มข้อมูล";
  $sql_ib64 = "INSERT INTO CONFIG.dbo.ACT_EDIT
  (FEE_PRICE,BILL_DATE,BILL_NO,PS_NAME,CC_SIZE,BRAND_NAME,SERIAL_NO,NOTE)
  VALUES ('$FEE_PRICE','$BILL_DATE','$BILL_NO','$PS_NAME','$CC_SIZE','$BRAND_NAME','$SERIAL_NO','$NOTE')";
  // echo $sql_ib64;
  $result = $conn->query($sql_ib64);
  if ($result) {
    echo "บันทึกข้อมูลสำเร็จ";
  }
  else {
    echo "Error : " . $sql . "<br>" . $conn->error;
  }
}
else {
  // echo "ข้อมูลตรง ทำการแก้ไข";
  $sql_ib64 = "UPDATE CONFIG.dbo.ACT_EDIT SET
  FEE_PRICE = '$FEE_PRICE'
  WHERE ACT_ID = '$ACT_ID'";
  $result = $conn->query($sql_ib64);
  if ($result) {
    echo "บันทึกข้อมูลสำเร็จ";
  } else {
    echo "Error : " . $sql . "<br>" . $conn->error;
  }
}
?>
