query($sql_Biz);
foreach ($BizPartner as $biz) {
$bizKey = $biz['BizPartnerId'];
$bizValue = $biz['BizPartnerName'];
$BusinessPartner[$bizKey] = $bizValue;
}
// 取得所有報銷過的發票號碼
$sql_invoiceNo = "SELECT DISTINCT InvoiceNo FROM rib_sub WHERE rib_id !== $rib_id";
$invoiceNo = mysqli_query($link, $sql_invoiceNo);
$invoiceNo_array = array();
foreach ($invoiceNo as $invoice) {
array_push($invoiceNo_array, $invoice['InvoiceNo']);
}
$RowCode = $_POST['RowCode'];
$FeeDate = $_POST['FeeDate'];
$FeeTypeGen = isset($_POST['FeeTypeGen']) ? $_POST['FeeTypeGen'] : null;
$FeeTypeId = $_POST['FeeTypeId'];
$FeeDeptId = $_POST['FeeDeptId'];
$CurrId = empty($_POST['CurrId']) ? null : $_POST['CurrId'];
$WriteOffId = $_POST['WriteOffId'];
$FeeNoteShow = $_POST['FeeNoteShow'];
$ProjectId = empty($_POST['ProjectId']) ? Null : $_POST['ProjectId'];
$CU_MaterialId = empty($_POST['CU_MaterialId']) ? null : $_POST['CU_MaterialId'];
$UnitId = $_POST['UnitId'];
$Price = $CurrId == "TWD" ? round($_POST['Price']) : round($_POST['Price'], 2);
$Quantity = $_POST['Quantity'];
$OAmount = $CurrId == "TWD" ? round($_POST['OAmount']) : round($_POST['OAmount'], 2);
$TaxId = $_POST['TaxId'];
$IsCounteract = $_POST['IsCounteract'];
$InvoiceId = $_POST['InvoiceId'];
$VoucherCount = empty($_POST['VoucherCount']) ? 1 : $_POST['VoucherCount'];
$VoucherType = isset($_POST['VoucherType']) ? $_POST['VoucherType'] : 0;
$InvoiceNo = empty($_POST['InvoiceNo']) ? " ": (in_array(str_replace("-", "",$_POST['InvoiceNo']), $invoiceNo_array) ? " ": str_replace("-", "",$_POST['InvoiceNo']));
$CurrLAmount = empty($_POST['CurrLAmount']) ? null : $_POST['CurrLAmount'];
// $comSupplierName = empty($_POST['comSupplierName']) ? null : $_POST['comSupplierName'];
$FromSourceTag = 0;
$TaxRate = ($TaxId == "ST005") ? 0.05 : 0;
if ($CurrId == "TWD") {
$OTax = isset($_POST['OTax']) ? round($_POST['OTax']) : NULL;
$LTax = isset($_POST['LTax']) ? round($_POST['LTax']) : NULL;
$OAmountUnWithTax = isset($_POST['OAmountUnWithTax']) ? round($_POST['OAmountUnWithTax']) : null;
$LAmountUnWithTax = isset($_POST['LAmountUnWithTax']) ? round($_POST['LAmountUnWithTax']) : null;
$LAmount = isset($_POST['LAmount']) ? round($_POST['LAmount']) : null;
}else{
$OTax = isset($_POST['OTax']) ? round($_POST['OTax'], 2) : NULL;
$LTax = isset($_POST['LTax']) ? round($_POST['LTax'], 2) : NULL;
$OAmountUnWithTax = isset($_POST['OAmountUnWithTax']) ? round($_POST['OAmountUnWithTax'], 2) : null;
$LAmountUnWithTax = isset($_POST['LAmountUnWithTax']) ? round($_POST['LAmountUnWithTax'], 2) : null;
$LAmount = isset($_POST['LAmount']) ? round($_POST['LAmount'], 2) : null;
}
$TaxNo = empty($_POST['TaxNo']) ? '' : $_POST['TaxNo'];
if (empty($TaxNo)) {
$comSupplierName = "雜項請款用";
} else {
if (isset($BusinessPartner[$TaxNo])) {
$comSupplierName = $BusinessPartner[$TaxNo];
} else {
$comSupplierName = "雜項請款用";
}
}
if (isset($comSupplierName) && ($comSupplierName != "雜項請款用")) {
$sql = "SELECT * FROM comBusinessPartner";
$BizPartner = $conn->query($sql);
foreach ($BizPartner as $company) {
if ($company['BizPartnerName'] == $comSupplierName) {
$TaxNo = $company['TaxNo'];
$comSupplierId = $company['BizPartnerId'];
}
}
} else {
$comSupplierId = "V0001";
}
$SQL1 = "";
if ($submit == "add") {
if (($user_id == "M0056") || ($user_id == "M0209")) {
$SQL1 = "INSERT INTO rib_sub (`RowCode`,`rib_id`,`RowNo`,`FeeDate`,`FeeTypeId`,`WriteOffType`,
`BizOrgId`,`WriteOffId`,`FeeOrgId`,`FeeDeptId`,`FeeCompanyId`,`UnitId`,`Quantity`,
`Price`,`OAmount`,`FeeNoteShow`,`CurrId`,`SupplyOrgId`,`CurrOAmount`,`FromSourceTag`,`TaxId`,`IsCounteract`,`InvoiceId`,`TaxRate`,`CreatorId`,`TaxNo`,`CurrentLAmount`,`OTax`,`LTax`,
`OAmountUnWithTax`,`LAmountUnWithTax`,`LAmount`";
$SQL1 .= empty($ProjectId) ? "" : ",`ProjectId`";
$SQL1 .= isset($FeeTypeGen) ? ",`FeeTypeGen`" : "";
$SQL1 .= empty($CU_MaterialId) ? "" : ",`CU_MaterialId`";
$SQL1 .= empty($VoucherCount) ? "" : ",`VoucherCount`";
$SQL1 .= isset($VoucherType) ? ",`VoucherType`" : "";
$SQL1 .= empty($InvoiceNo) ? "" : ",`InvoiceNo`";
$SQL1 .= empty($comSupplierName) ? "" : ",`comSupplierName`";
$SQL1 .= empty($comSupplierId) ? ",`comSupplierId`" : ",`comSupplierId`";
$SQL2 = ") VALUES ($RowCode, $rib_id, $RowCode,$FeeDate,'$FeeTypeId',2,
'1000','$WriteOffId','1000','$FeeDeptId','1000','$UnitId',$Quantity,
$Price,$OAmount,'$FeeNoteShow','$CurrId','1000',1,0,'$TaxId',$IsCounteract,'$InvoiceId',$TaxRate,'$user_id','$TaxNo',$CurrLAmount,$OTax,$LTax,
$OAmountUnWithTax,$LAmountUnWithTax,$LAmount";
$SQL2 .= empty($ProjectId) ? "" : ",'" . $ProjectId . "'";
$SQL2 .= isset($FeeTypeGen) ? "," . $FeeTypeGen : "";
$SQL2 .= empty($CU_MaterialId) ? "" : ",'" . $CU_MaterialId . "'";
$SQL2 .= empty($VoucherCount) ? "" : "," . $VoucherCount;
$SQL2 .= isset($VoucherType) ? "," . $VoucherType : "";
$SQL2 .= empty($InvoiceNo) ? "" : ",'" . $InvoiceNo . "'";
$SQL2 .= empty($comSupplierName) ? "" : ",'" . $comSupplierName . "'";
$SQL2 .= empty($comSupplierId) ? ",' '" : ",'" . $comSupplierId . "'";
$SQL2 .= ")";
} else {
$SQL1 = "INSERT INTO rib_sub (`RowCode`,`rib_id`,`RowNo`,`FeeDate`,`FeeTypeId`,`WriteOffType`,
`BizOrgId`,`WriteOffId`,`FeeOrgId`,`FeeDeptId`,`FeeCompanyId`,`UnitId`,`Quantity`,
`Price`,`OAmount`,`FeeNoteShow`,`CurrId`,`SupplyOrgId`,`CurrOAmount`,`FromSourceTag`,`TaxId`,`IsCounteract`,`InvoiceId`,`TaxRate`,`CreatorId`,`TaxNo`";
$SQL1 .= empty($ProjectId) ? "" : ",`ProjectId`";
$SQL1 .= isset($FeeTypeGen) ? ",`FeeTypeGen`" : "";
$SQL1 .= empty($CU_MaterialId) ? "" : ",`CU_MaterialId`";
$SQL1 .= empty($VoucherCount) ? "" : ",`VoucherCount`";
$SQL1 .= isset($VoucherType) ? ",`VoucherType`" : "";
$SQL1 .= empty($InvoiceNo) ? "" : ",`InvoiceNo`";
$SQL1 .= empty($comSupplierName) ? "" : ",`comSupplierName`";
$SQL1 .= empty($comSupplierId) ? ",`comSupplierId`" : ",`comSupplierId`";
$SQL2 = ") VALUES ($RowCode, $rib_id, $RowCode,$FeeDate,'$FeeTypeId',2,
'1000','$WriteOffId','1000','$FeeDeptId','1000','$UnitId',$Quantity,
$Price,$OAmount,'$FeeNoteShow','$CurrId','1000',1,0,'$TaxId',$IsCounteract,'$InvoiceId',$TaxRate,'$user_id','$TaxNo'";
$SQL2 .= empty($ProjectId) ? "" : ",'" . $ProjectId . "'";
$SQL2 .= isset($FeeTypeGen) ? "," . $FeeTypeGen : "";
$SQL2 .= empty($CU_MaterialId) ? "" : ",'" . $CU_MaterialId . "'";
$SQL2 .= empty($VoucherCount) ? "" : "," . $VoucherCount;
$SQL2 .= isset($VoucherType) ? "," . $VoucherType : "";
$SQL2 .= empty($InvoiceNo) ? "" : ",'" . $InvoiceNo . "'";
$SQL2 .= empty($comSupplierName) ? "" : ",'" . $comSupplierName . "'";
$SQL2 .= empty($comSupplierId) ? ",' '" : ",'" . $comSupplierId . "'";
$SQL2 .= ")";
}
$SQL1 .= $SQL2;
//上傳資料庫
mysqli_query($link, $SQL1);
if (mysqli_affected_rows($link)) {
echo '';
} else {
echo '';
}
//更新表頭
$SQL1 = str_replace("'", '', $SQL1);
$sql_rib = "UPDATE rib SET LastOperatorId = '$user_id', LastOperation='$SQL1' WHERE rib_id=$rib_id";
mysqli_query($link, $sql_rib);
?>
alert("新增/更新 成功")';
} else {
echo '';
};
if ($state == 1) {
//取得所有表身資料以更新表頭資料
$ribsub_query = "SELECT * FROM rib_sub WHERE rib_id = $rib_id";
$ribsub_data = mysqli_query($link, $ribsub_query);
$OAmountWithTax = 0;
$OTax = 0;
$LTax = 0;
$OAmount = 0;
$WriteOffAmount = 0;
$LAmount = 0;
$totalVoucher = 0;
while ($ribsub = mysqli_fetch_assoc($ribsub_data)) {
$OAmountWithTax += $ribsub['OAmount'];
$LAmount += $ribsub['LAmountUnWithTax'];
$OTax += $ribsub['OTax'];
$LTax += $ribsub['LTax'];
$WriteOffAmount += $ribsub['OAmount'];
$OAmount += $ribsub['OAmountUnWithTax'];
$totalVoucher += $ribsub['VoucherCount'];
$CurrLAmount = $ribsub['CurrentLAmount'];
}
$UnPayOAmount = $WriteOffAmount;
$UnPayLAmount = $UnPayOAmount * $CurrLAmount;
$LAmountWithTax = $OAmountWithTax * $CurrLAmount;
// 更新表頭資料
$SQL1 = str_replace("'", '', $SQL1);
$sql_rib = "UPDATE rib SET OAmountWithTax=$OAmountWithTax, LAmountWithTax= $LAmountWithTax, LAmount=$LAmount,OAmount =$OAmount, UnPayOAmount=$OAmountWithTax, Balance = $OAmountWithTax, LBalance= $UnPayLAmount ,UnPaymentLAmt=$UnPayLAmount, UnPayLAmount=$UnPayLAmount,OTax=$OTax, LTax=$LTax, WriteOffAmount=$WriteOffAmount, LastOperatorId='$user_id' WHERE rib_id = $rib_id";
mysqli_query($link, $sql_rib);
?>