You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

193 lines
8.8 KiB

<?php
include "./header.php";
$rib_id = isset($_GET['rib_id']) ? $_GET['rib_id'] : $_POST['rib_id'];
$ribsub_id = isset($_GET['ribsub_id']) ? $_GET['ribsub_id'] : Null;
$submit = $_GET['submit'];
$state = empty($_GET['state']) ? 0 : $_GET['state'];
// 取得供應商
$BusinessPartner = array();
$sql_Biz = "SELECT comBusinessPartner.BizPartnerId, comBusinessPartner.BizPartnerName FROM comBusinessPartner INNER JOIN comSupplier ON comBusinessPartner.BizPartnerId=comSupplier.BizPartnerId WHERE comBusinessPartner.BizPartnerId !='V0001'";
$BizPartner = $conn->query($sql_Biz);
foreach ($BizPartner as $biz) {
$bizKey = $biz['BizPartnerId'];
$bizValue = $biz['BizPartnerName'];
$BusinessPartner[$bizKey]=$bizValue;
}
$RowCode = $_POST['RowCode'];
$FeeDate = $_POST['FeeDate'];
$FeeTypeGen = isset($_POST['FeeTypeGen']) ? $_POST['FeeTypeGen'] : null;
$FeeTypeId = $_POST['FeeTypeId'];
$FeeDeptId = $_POST['FeeDeptId'];
$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 = $_POST['Price'];
$Quantity = $_POST['Quantity'];
$OAmount = $_POST['OAmount'];
$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']) ? NULL : $_POST['InvoiceNo'];
$CurrId = empty($_POST['CurrId']) ? null : $_POST['CurrId'];
// $comSupplierName = empty($_POST['comSupplierName']) ? null : $_POST['comSupplierName'];
$FromSourceTag = 0;
$TaxRate = ($TaxId == "ST005") ? 0.05 : 0;
$OTax = isset($_POST['OTax']) ? $_POST['OTax'] : NULL;
$LTax = isset($_POST['LTax']) ? $_POST['LTax'] : NULL;
$OAmountUnWithTax = isset($_POST['OAmountUnWithTax']) ? $_POST['OAmountUnWithTax'] : null;
$LAmountUnWithTax = isset($_POST['LAmountUnWithTax']) ? $_POST['LAmountUnWithTax'] : null;
$LAmount = isset($_POST['LAmount']) ? $_POST['LAmount'] : 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") {
$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 '<script>alert("新增/更新 成功")</script>';
} else {
echo '<script>alert("新增/更新 失敗")</script>';
}
//更新表頭
$SQL1 = str_replace("'", '', $SQL1);
$sql_rib = "UPDATE rib SET LastOperatorId = '$user_id', LastOperation='$SQL1' WHERE rib_id=$rib_id";
mysqli_query($link, $sql_rib);
?>
<script>
window.location.href = "rib01-check.php?id=<?php echo $rib_id ?>&state=0&<?= $token_link ?>";
</script>
<?php
} elseif ($submit == "edit") {
$SQL1 = "UPDATE rib_sub SET RowCode=$RowCode, RowNo=$RowCode,FeeDate=$FeeDate,FeeTypeId=$FeeTypeId,FeeDeptId=$FeeDeptId,WriteOffId='$WriteOffId',FeeNoteShow='$FeeNoteShow',
UnitId='$UnitId', Price=$Price,Quantity=$Quantity, OAmount=$OAmount, TaxId='$TaxId', IsCounteract=$IsCounteract,InvoiceId= '$InvoiceId', TaxRate=$TaxRate,LastOperatorId='$user_id',TaxNo='$TaxNo',LastOperatorId='$user_id'";
$SQL1 .= empty($ProjectId) ? "" : ",ProjectId='$ProjectId'";
$SQL1 .= isset($FeeTypeGen) ? ",FeeTypeGen=$FeeTypeGen" : "";
$SQL1 .= empty($CU_MaterialId) ? "" : ",CU_MaterialId='$CU_MaterialId'";
$SQL1 .= empty($VoucherCount) ? "" : ",VoucherCount=$VoucherCount";
$SQL1 .= isset($VoucherType) ? ",VoucherType=$VoucherType" : "";
$SQL1 .= empty($InvoiceNo) ? "" : ",InvoiceNo='$InvoiceNo'";
$SQL1 .= empty($comSupplierName) ? ",comSupplierName=' '" : ",comSupplierName='$comSupplierName'";
$SQL1 .= empty($comSupplierId) ? ",comSupplierId=' '" : ",comSupplierId='$comSupplierId'";
$SQL1 .= isset($OTax) ? ",OTax=$OTax" : "";
$SQL1 .= isset($LTax) ? ",LTax=$LTax" : "";
$SQL1 .= isset($OAmountUnWithTax) ? ",OAmountUnWithTax=$OAmountUnWithTax" : "";
$SQL1 .= isset($LAmountUnWithTax) ? ",LAmountUnWithTax=$LAmountUnWithTax" : "";
$SQL1 .= isset($LAmount) ? ",LAmount=$LAmount" : "";
if (($user_id == "M0056" || $user_id == "M0209") && $state == 1) {
$SQL1 .= ",FeeBalance=$LAmountUnWithTax";
$SQL1 .= ",WriteOffAmount=$OAmount";
}
$SQL1 .= " WHERE ribsub_id = $ribsub_id";
//上傳資料庫--表身
mysqli_query($link, $SQL1);
if (mysqli_affected_rows($link)) {
echo '<script>alert("新增/更新 成功")</script>';
} else {
echo '<script>alert("新增/更新 失敗")</script>';
};
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);
?>
<script>
window.location.href = "rib01-check.php?id=<?php echo $rib_id ?>&state=0&<?= $token_link ?>";
</script>
<?php
} else {
$SQL1 = str_replace("'", '', $SQL1);
$sql_rib = "UPDATE rib SET LastOperatorId = '$user_id', LastOperation='$SQL1' WHERE rib_id=$rib_id";
mysqli_query($link, $sql_rib);
?>
<script>
window.location.href = "rib01-check.php?id=<?php echo $rib_id ?>&state=0&<?= $token_link ?>";
</script>
<?php
}
}
?>