public string GenerateFormAndReturnError(int i, DataRow row, LogDS.ImportFormLogDetailRow logDetailRow)
    {
        SqlTransaction transaction = null;
        string errorInfor = string.Empty;
        try {
            FormTableAdapter TAForm = new FormTableAdapter();
            FormSaleSettlementTableAdapter TAFormSettlement = new FormSaleSettlementTableAdapter();
            FormSalePaymentTableAdapter TASalePayment = new FormSalePaymentTableAdapter();
            FormSalePaymentDetailTableAdapter TASalePaymentDetail = new FormSalePaymentDetailTableAdapter();
            FormSettlementExpenseDetailTableAdapter TAFormSettlementExpenseDetail = new FormSettlementExpenseDetailTableAdapter();

            transaction = TableAdapterHelper.BeginTransaction(TAForm);
            TableAdapterHelper.SetTransaction(TASalePayment, transaction);
            TableAdapterHelper.SetTransaction(TASalePaymentDetail, transaction);

            FormDS.FormDataTable tbForm = new FormDS.FormDataTable();
            FormDS.FormSalePaymentDataTable tbPayment = new FormDS.FormSalePaymentDataTable();
            FormDS.FormSalePaymentDetailDataTable tbPaymentDetail = new FormDS.FormSalePaymentDetailDataTable();
            FormDS.FormRow rowForm = null;
            FormDS.FormSalePaymentRow rowPayment = null;
            FormDS.FormSalePaymentDetailRow rowPaymentDetail = null;

            string settlementFormNo = string.Empty;
            DateTime SubmitDate = DateTime.Now;
            decimal PaymentAmount = 0;
            bool IsValid = true;
            if (CheckData(row) != null) {
                errorInfor = "第" + (i + 1) + "行有错:" + CheckData(row);
                IsValid = false;
            } else {
                settlementFormNo = row[0].ToString().Trim();
                SubmitDate = DateTime.Parse(row[1].ToString());
                PaymentAmount = decimal.Parse(row[2].ToString().Trim());

                logDetailRow.SettlementFormNo = settlementFormNo;
                logDetailRow.PaymentAmount = PaymentAmount;

                FormDS.FormDataTable tbSettlement = TAForm.GetDataByFormNo(settlementFormNo);
                if (tbSettlement.Rows.Count <= 0) {
                    errorInfor = "第" + (i + 1) + "行有错:系统中找不到结案单《" + settlementFormNo + "》";
                    IsValid = false;
                }
                FormDS.FormRow settlementForm = tbSettlement[0];
                FormDS.FormSaleSettlementRow rowSettlement = TAFormSettlement.GetDataByID(settlementForm.FormID)[0];

                if (rowSettlement.PaymentTypeID != 2 && rowSettlement.PaymentTypeID != 5) {
                    errorInfor = "第" + (i + 1) + "行有错:该单据支付方式,不是票扣或者调整因子《" + settlementFormNo + "》";
                    IsValid = false;
                }

                if (rowSettlement.IsClose) {
                    errorInfor = "第" + (i + 1) + "行有错:该单据已标记为支付完成《" + settlementFormNo + "》";
                    IsValid = false;
                }

                if (rowSettlement.PaymentTypeID == 2 && PaymentAmount > rowSettlement.AmountRMB) {
                    errorInfor = "第" + (i + 1) + "行有错:支付金额超过结案金额《" + settlementFormNo + "》";
                    IsValid = false;
                }
                if (IsValid) {
                    //生成单据
                    rowForm = tbForm.NewFormRow();
                    rowPayment = tbPayment.NewFormSalePaymentRow();

                    //生成Form
                    rowForm.SetRejectedFormIDNull();
                    //申请人取结案单申请人
                    rowForm.UserID = settlementForm.UserID;
                    UtilityBLL utility = new UtilityBLL();
                    rowForm.FormNo = utility.GetFormNo(utility.GetFormTypeString((int)SystemEnums.FormType.SalePayment));
                    rowForm.SetProxyUserIDNull();
                    rowForm.SetProxyPositionIDNull();
                    //申请人部门取结案单申请人所在部门
                    rowForm.OrganizationUnitID = settlementForm.OrganizationUnitID;
                    rowForm.PositionID = settlementForm.PositionID;
                    rowForm.FormTypeID = (int)SystemEnums.FormType.SalePayment;
                    rowForm.StatusID = (int)SystemEnums.FormStatus.ApproveCompleted;
                    rowForm.SubmitDate = SubmitDate;
                    rowForm.LastModified = SubmitDate;
                    rowForm.InTurnUserIds = "P";//待改动
                    rowForm.InTurnPositionIds = "P";//待改动
                    rowForm.PageType = (int)SystemEnums.PageType.PaymentCash;
                    rowForm.CostCenterID = settlementForm.CostCenterID;
                    rowForm.ApprovedDate = SubmitDate;
                    //是否创建凭证?
                    rowForm.IsCreateVoucher = false;
                    rowForm.IsExportLock = false;
                    rowForm.IsCompletePayment = false;
                    rowForm.IsInvoiceReturned = false;
                    tbForm.AddFormRow(rowForm);
                    TAForm.Update(rowForm);

                    //生成FormPayment
                    rowPayment.FormSalePaymentID = rowForm.FormID;
                    rowPayment.FormSaleSettlementID = settlementForm.FormID;
                    //生成时,不考虑预付款
                    rowPayment.SetFormSaleApplyIDNull();
                    rowPayment.InvoiceStatusID = (int)SystemEnums.InvoiceStatus.No;
                    rowPayment.PaymentTypeID = rowSettlement.PaymentTypeID;
                    //报销申请单备注
                    rowPayment.Remark = "此单据为自动生成单据。";
                    rowPayment.SetAttachedFileNameNull();
                    rowPayment.SetRealAttachedFileNameNull();
                    //报销金额
                    rowPayment.AmountRMB = PaymentAmount;
                    rowPayment.VatTypeID = 1;
                    rowPayment.AmountBeforeTax = rowPayment.AmountRMB;
                    rowPayment.TaxAmount = 0;
                    rowPayment.IsAdvanced = false;
                    //VendorID设置什么值
                    rowPayment.SetVendorIDNull();
                    tbPayment.AddFormSalePaymentRow(rowPayment);
                    TASalePayment.Update(rowPayment);

                    //生成FormPaymentDetail
                    decimal UsedAmount = 0;
                    FormDS.FormSettlementExpenseDetailDataTable tbSettlementExpenseDetail = TAFormSettlementExpenseDetail.GetDataByFormSaleSettlementID(rowSettlement.FormSaleSettlementID);
                    FormDS.FormSettlementExpenseDetailRow rowSettlementExpenseDetail = null;
                    for (int j = 0; j < tbSettlementExpenseDetail.Rows.Count; j++) {
                        rowSettlementExpenseDetail = (FormDS.FormSettlementExpenseDetailRow)tbSettlementExpenseDetail.Rows[j];
                        rowPaymentDetail = tbPaymentDetail.NewFormSalePaymentDetailRow();

                        rowPaymentDetail.FormSalePaymentID = rowPayment.FormSalePaymentID;
                        rowPaymentDetail.FormSaleApplyID = rowSettlementExpenseDetail.FormSaleApplyID;
                        rowPaymentDetail.FormSaleExpenseDetailID = rowSettlementExpenseDetail.FormSaleExpenseDetailID;
                        rowPaymentDetail.ApplyFormNo = rowSettlementExpenseDetail.ApplyFormNo;
                        rowPaymentDetail.ApplyPeriod = rowSettlementExpenseDetail.ApplyPeriod;
                        rowPaymentDetail.ApplyProjectName = rowSettlementExpenseDetail.ApplyProjectName;
                        rowPaymentDetail.ExpenseItemID = rowSettlementExpenseDetail.ExpenseItemID;
                        if (!rowSettlementExpenseDetail.IsShopNameNull()) {
                            rowPaymentDetail.ShopName = rowSettlementExpenseDetail.ShopName;
                        }
                        rowPaymentDetail.SKUID = rowSettlementExpenseDetail.SKUID;
                        rowPaymentDetail.ApplyAmount = rowSettlementExpenseDetail.ApplyAmount;
                        rowPaymentDetail.ApplyAmountRMB = rowSettlementExpenseDetail.ApplyAmountRMB;
                        rowPaymentDetail.SettlementAmount = rowSettlementExpenseDetail.AmountRMB;
                        rowPaymentDetail.TaxAmount = 0;

                        //待改动
                        if (j == tbSettlementExpenseDetail.Rows.Count - 1) {
                            rowPaymentDetail.AmountRMB = PaymentAmount - UsedAmount;
                            rowPaymentDetail.AmountBeforeTax = rowPaymentDetail.AmountRMB;
                            rowPaymentDetail.TaxAmount = rowPaymentDetail.TaxAmount;
                            rowPaymentDetail.PayedAmount = 0;
                            rowPaymentDetail.RemainAmount = 0;
                            UsedAmount += rowPaymentDetail.AmountRMB;
                        } else {
                            rowPaymentDetail.AmountRMB = decimal.Round((rowSettlementExpenseDetail.AmountRMB / rowSettlement.AmountRMB) * PaymentAmount, 2);
                            rowPaymentDetail.AmountBeforeTax = rowPaymentDetail.AmountRMB;
                            rowPaymentDetail.TaxAmount = 0;
                            rowPaymentDetail.PayedAmount = 0;
                            rowPaymentDetail.RemainAmount = 0;
                            UsedAmount += rowPaymentDetail.AmountRMB;
                        }
                        tbPaymentDetail.AddFormSalePaymentDetailRow(rowPaymentDetail);
                        TASalePaymentDetail.Update(tbPaymentDetail);
                    }
                    logDetailRow.PaymentFormNo = rowForm.FormNo;
                }
            }
            transaction.Commit();
        } catch (Exception e) {
            transaction.Rollback();
            errorInfor = e.Message.ToString();
        } finally {
            transaction.Dispose();
        }
        return errorInfor;
    }
示例#2
0
        public void AddPaymentCash(int? RejectedFormID, int UserID, int? ProxyUserID, int? ProxyPositionID, int OrganizationUnitID, int PositionID, SystemEnums.FormType FormTypeID, SystemEnums.FormStatus StatusID,
                        SystemEnums.PageType PageType, int FormSaleSettlementID, int InvoiceStatusID, int PaymentTypeID, string Remark, string AttachedFileName, string RealAttachedFileName, int CostCenterID, int? VendorID, int FormPOID, int? VatTypeID)
        {
            SqlTransaction transaction = null;
            try {
                //事务开始
                transaction = TableAdapterHelper.BeginTransaction(this.TAForm);
                TableAdapterHelper.SetTransaction(this.TAFormSalePayment, transaction);
                TableAdapterHelper.SetTransaction(this.TAFormSalePaymentDetail, transaction);
                TableAdapterHelper.SetTransaction(this.TAFormInvoice, transaction);

                //处理Form的内容
                FormDS.FormRow formRow = this.FormDataSet.Form.NewFormRow();
                if (RejectedFormID != null) {
                    formRow.RejectedFormID = RejectedFormID.GetValueOrDefault();
                }
                formRow.UserID = UserID;

                UtilityBLL utility = new UtilityBLL();
                if (StatusID == SystemEnums.FormStatus.Awaiting) {
                    string formTypeString = utility.GetFormTypeString((int)FormTypeID);
                    formRow.FormNo = utility.GetFormNo(formTypeString);
                } else {
                    formRow.SetFormNoNull();
                }
                if (ProxyUserID != null) {
                    formRow.ProxyUserID = ProxyUserID.GetValueOrDefault();
                }
                if (ProxyPositionID != null) {
                    formRow.ProxyPositionID = ProxyPositionID.GetValueOrDefault();
                }
                formRow.OrganizationUnitID = OrganizationUnitID;
                formRow.PositionID = PositionID;
                formRow.FormTypeID = (int)FormTypeID;
                formRow.StatusID = (int)StatusID;
                formRow.SubmitDate = DateTime.Now;
                formRow.LastModified = DateTime.Now;
                formRow.InTurnUserIds = "P";//待改动
                formRow.InTurnPositionIds = "P";//待改动
                formRow.PageType = (int)PageType;
                formRow.CostCenterID = CostCenterID;
                formRow.IsCreateVoucher = false;
                formRow.IsExportLock = false;
                formRow.IsCompletePayment = false;
                formRow.IsInvoiceReturned = false;
                this.FormDataSet.Form.AddFormRow(formRow);
                this.TAForm.Update(formRow);

                //处理Payment的内容
                FormDS.FormSalePaymentRow formPaymentRow = this.FormDataSet.FormSalePayment.NewFormSalePaymentRow();
                formPaymentRow.FormSalePaymentID = formRow.FormID;
                formPaymentRow.FormSaleSettlementID = FormSaleSettlementID;
                formPaymentRow.InvoiceStatusID = InvoiceStatusID;
                formPaymentRow.PaymentTypeID = PaymentTypeID;
                if (Remark != null && Remark != string.Empty) {
                    formPaymentRow.Remark = Remark;
                }
                if (AttachedFileName != null && AttachedFileName != string.Empty) {
                    formPaymentRow.AttachedFileName = AttachedFileName;
                }
                if (RealAttachedFileName != null && RealAttachedFileName != string.Empty) {
                    formPaymentRow.RealAttachedFileName = RealAttachedFileName;
                }
                formPaymentRow.AmountRMB = 0;
                formPaymentRow.IsAdvanced = false;
                if (VendorID != null) {
                    formPaymentRow.VendorID = VendorID.GetValueOrDefault();
                }
                if (FormPOID > 0) {
                    formPaymentRow.FormPOID = FormPOID;
                }
                if (VatTypeID != null) {
                    formPaymentRow.VatTypeID = VatTypeID.GetValueOrDefault();
                }
                this.FormDataSet.FormSalePayment.AddFormSalePaymentRow(formPaymentRow);
                this.TAFormSalePayment.Update(formPaymentRow);

                //发票
                if (RejectedFormID != null) {
                    FormDS.FormInvoiceDataTable newInvoiceTable = new FormDS.FormInvoiceDataTable();
                    foreach (FormDS.FormInvoiceRow invoiceRow in this.FormDataSet.FormInvoice) {
                        if (invoiceRow.RowState != DataRowState.Deleted) {
                            FormDS.FormInvoiceRow newInvoiceRow = newInvoiceTable.NewFormInvoiceRow();
                            newInvoiceRow.FormID = formPaymentRow.FormSalePaymentID;
                            newInvoiceRow.InvoiceNo = invoiceRow.InvoiceNo;
                            newInvoiceRow.InvoiceDate = invoiceRow.InvoiceDate;
                            newInvoiceRow.InvoiceAmount = invoiceRow.InvoiceAmount;
                            newInvoiceRow.Remark = invoiceRow.IsRemarkNull() ? "" : invoiceRow.Remark;
                            newInvoiceRow.SystemInfo = invoiceRow.IsSystemInfoNull() ? "" : invoiceRow.SystemInfo;
                            newInvoiceTable.AddFormInvoiceRow(newInvoiceRow);
                        }
                        this.TAFormInvoice.Update(newInvoiceTable);
                    }
                } else {
                    foreach (FormDS.FormInvoiceRow invoiceRow in this.FormDataSet.FormInvoice) {
                        // 与父表绑定
                        if (invoiceRow.RowState != DataRowState.Deleted) {
                            invoiceRow.FormID = formPaymentRow.FormSalePaymentID;
                        }
                    }
                }
                this.TAFormInvoice.Update(this.FormDataSet.FormInvoice);

                //处理明细
                decimal totalAmountBeforeTax = 0;
                decimal totalTaxAmount = 0;
                decimal totalAmountRMB = 0;
                FormDS.FormSalePaymentDetailDataTable newDetailTable = new FormDS.FormSalePaymentDetailDataTable();
                foreach (FormDS.FormSalePaymentDetailRow detailRow in this.FormDataSet.FormSalePaymentDetail) {
                    // 与父表绑定
                    if (detailRow.RowState != DataRowState.Deleted) {
                        totalAmountBeforeTax += detailRow.AmountBeforeTax;
                        totalTaxAmount += detailRow.TaxAmount;
                        totalAmountRMB += detailRow.AmountRMB;
                        FormDS.FormSalePaymentDetailRow newDetailRow = newDetailTable.NewFormSalePaymentDetailRow();
                        newDetailRow.FormSalePaymentID = formPaymentRow.FormSalePaymentID;
                        newDetailRow.FormSaleApplyID = detailRow.FormSaleApplyID;
                        newDetailRow.FormSaleExpenseDetailID = detailRow.FormSaleExpenseDetailID;
                        newDetailRow.ApplyFormNo = detailRow.ApplyFormNo;
                        newDetailRow.ApplyPeriod = detailRow.ApplyPeriod;
                        if (!detailRow.IsApplyProjectNameNull()) {
                            newDetailRow.ApplyProjectName = detailRow.ApplyProjectName;
                        }
                        newDetailRow.ExpenseItemID = detailRow.ExpenseItemID;
                        if (!detailRow.IsShopNameNull()) {
                            newDetailRow.ShopName = detailRow.ShopName;
                        }
                        if (!detailRow.IsSKUIDNull()) {
                            newDetailRow.SKUID = detailRow.SKUID;
                        }
                        newDetailRow.ApplyAmount = detailRow.ApplyAmount;
                        newDetailRow.ApplyAmountRMB = detailRow.ApplyAmountRMB;
                        if (!detailRow.IsSettlementAmountNull()) {
                            newDetailRow.SettlementAmount = detailRow.SettlementAmount;
                        }
                        if (!detailRow.IsPayedAmountNull()) {
                            newDetailRow.PayedAmount = detailRow.PayedAmount;
                        }
                        if (!detailRow.IsRemainAmountNull()) {
                            newDetailRow.RemainAmount = detailRow.RemainAmount;
                        }
                        newDetailRow.AmountBeforeTax = detailRow.AmountBeforeTax;
                        newDetailRow.TaxAmount = detailRow.TaxAmount;
                        newDetailRow.AmountRMB = detailRow.AmountRMB;

                        newDetailTable.AddFormSalePaymentDetailRow(newDetailRow);
                    }
                }
                this.TAFormSalePaymentDetail.Update(newDetailTable);

                formPaymentRow.AmountBeforeTax = totalAmountBeforeTax;
                formPaymentRow.TaxAmount = totalTaxAmount;
                formPaymentRow.AmountRMB = totalAmountRMB;
                this.TAFormSalePayment.Update(formPaymentRow);
                //作废之前的单据
                if (RejectedFormID != null) {
                    FormDS.FormRow oldRow = this.TAForm.GetDataByID(RejectedFormID.GetValueOrDefault())[0];
                    if (oldRow.StatusID == (int)SystemEnums.FormStatus.Rejected) {
                        oldRow.StatusID = (int)SystemEnums.FormStatus.Scrap;
                        this.TAForm.Update(oldRow);
                    }
                }

                // 正式提交
                if (StatusID == SystemEnums.FormStatus.Awaiting) {
                    Dictionary<string, object> dic = new Dictionary<string, object>();
                    dic["TotalAmount"] = totalAmountRMB;
                    MasterData.CustomerRow customer = new MasterDataBLL().GetCustomerById(this.TAFormSaleSettlement.GetDataByID(FormSaleSettlementID)[0].CustomerID)[0];
                    dic["CustomerChannel"] = new MasterDataBLL().GetCustomerChannelById(customer.CustomerChannelID)[0].CustomerChannelCode;
                    dic["KAType"] = customer.IsKaTypeNull() ? "" : customer.KaType;
                    dic["InvoiceStatus"] = formPaymentRow.InvoiceStatusID;
                    dic["PaymentTypeID"] = PaymentTypeID;
                    APResult result = new APFlowBLL().CreateProcess(new APParameter(formRow.FormID, formRow.FormNo, formRow.OrganizationUnitID, formRow.UserID, (int)StatusID, formRow.FormTypeID, formRow.PositionID, dic));
                    if (result != null) {
                        formRow.InTurnPositionIds = result.InTurnPositionIds;
                        formRow.InTurnUserIds = result.InTurnUserIds;
                        formRow.SubmitDate = result.ApprovedDate.GetValueOrDefault();
                        formRow.StatusID = result.StatusID;
                        formRow.ProcID = result.ProcID;
                        formRow.FinanceRemark = formPaymentRow.Remark;
                        this.TAForm.Update(formRow);
                    }
                }
                transaction.Commit();
            } catch (Exception ex) {
                transaction.Rollback();
                throw new ApplicationException("Save Fail!" + ex.ToString());
            } finally {
                transaction.Dispose();
            }
        }