📄 paymentaccessor.cs
字号:
using System.Collections;
using System.Data.Common;
using DatabaseUtil;
using Mied.BusinessObject;
using Mied.DAL.TableSchema;
using System;
using Mied.DAL.Exceptions;
using System.Data;
namespace Mied.DAL.Accesses
{
public class PaymentAccessor : AccessorSelect
{
public PaymentAccessor(MiedDatabase database)
: base(database, PaymentSchema.TableName)
{
}
public void ExsitingPay(DateTime time, string paymentMethod, string paymentType, int contactID, Payment.PaymentBillType type, decimal amount, PaymentBillItemList paymentBillItemList, Contact contact)
{
Payment payment = new Payment();
payment.CashDate = time;
payment.PaymentMethod = paymentMethod;
payment.PaymentType = paymentType;
payment.ContactID = contactID;
payment.IsOnlinePay = false;
payment.IsValid = true;
payment.BillType = type;
payment.Amount = amount;
using (DbConnection connection = this.Database.CreateConnection())
{
connection.Open();
DbTransaction transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
try
{
this.Save(payment, transaction);
this.SavePaymentDetails(paymentBillItemList,payment,type,contact,transaction);
CommandHelper.CommitTransaction(transaction);
}
catch (Exception ex)
{
transaction.Rollback();
throw new PaymentException(ex);
}
}
}
public void OnlinePrePay(CreditCardInfo ccInfo, int contactID, DateTime cashTime, decimal amount, Payment payment, CreditCardLog ccLog)
{
using (DbConnection connection = this.Database.CreateConnection())
{
connection.Open();
DbTransaction transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
try
{
//insert a new data line into CreditCardInfo table.
this.Database.CreditCardAccessor.Insert(ccInfo);
//insert a data line into Payment table.
payment.ContactID = contactID;
payment.CashDate = cashTime;
payment.PaymentMethod = ccInfo.CardNumber;
payment.PaymentType = "Credit Card ";
payment.IsOnlinePay = true;
payment.IsValid = false;
payment.Amount = amount;
this.Save(payment);
//insert a data line into CreditCardLog table.
ccLog.PaymentID = payment.ID;
ccLog.CreditCardID = ccInfo.ID;
ccLog.PayStatus = "Fail";
ccLog.Interrupt = "Yes";
ccLog.ContactKey = this.Database.CustomerAccessor.SelectCustomer(contactID).ContactKey;
ccLog.Amount = amount;
this.Database.CreditCardLogAccessor.Save(ccLog);
}
catch (Exception ex)
{
transaction.Rollback();
throw new OnlinePrePayException(ex);
}
}
}
public void SavePaymentDetails(PaymentBillItemList paymentBillItemList, Payment payment, Payment.PaymentBillType type, Contact contact, DbTransaction transaction)
{
foreach (PaymentBillItem p in paymentBillItemList)
{
if (p.Distribution != 0)
{
PaymentDetail paymentDetail = new PaymentDetail();
paymentDetail.PaymentID = payment.ID;
paymentDetail.BillID = p.IsCreditItem ? null : p.BillID;
paymentDetail.Number = p.Number;
paymentDetail.PaidAmount = p.Distribution;
this.Database.PaymentDetailAccessor.Save(paymentDetail, transaction);
if (!p.IsCreditItem)
{
if (type == Payment.PaymentBillType.PaymentForInvoice)
{
this.Database.InvoiceAccessor.UpdatePaid((int)p.BillID, p.PaidToDate + p.Distribution, payment.CashDate, transaction);
}
else
{
this.Database.PurchaseOrderAccessor.UpdatePaid((int)p.BillID, p.PaidToDate + p.Distribution, payment.CashDate, transaction);
}
}
p.PaidToDate += p.Distribution;
p.Distribution = 0;
if (p.IsCreditItem)
{
if (type == Payment.PaymentBillType.PaymentForInvoice)
{
Customer customer = contact as Customer;
customer.CustomerCredit = p.PaidToDate;
customer.CustomerCreditDate = payment.CashDate;
this.Database.CustomerAccessor.Save(customer, transaction);
}
else
{
Vendor vendor = contact as Vendor;
vendor.VendorCredit = p.PaidToDate;
vendor.VendorCreditDate = payment.CashDate;
this.Database.VendorAccessor.Save(vendor, transaction);
}
if (p.Due == 0)
{
paymentBillItemList.HasCreditItem = false;
paymentBillItemList.Remove(p);
break;
}
}
}
}
}
public void SavePaymentDetails(PaymentBillItemList paymentBillItemList, Payment payment, Payment.PaymentBillType type, Contact contact)
{
using (DbConnection connection = this.Database.CreateConnection())
{
connection.Open();
DbTransaction transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
try
{
foreach (PaymentBillItem p in paymentBillItemList)
{
if (p.Distribution != 0)
{
PaymentDetail paymentDetail = new PaymentDetail();
paymentDetail.PaymentID = payment.ID;
paymentDetail.BillID = p.IsCreditItem ? null : p.BillID;
paymentDetail.Number = p.Number;
paymentDetail.PaidAmount = p.Distribution;
this.Database.PaymentDetailAccessor.Save(paymentDetail, transaction);
if (!p.IsCreditItem)
{
if (type == Payment.PaymentBillType.PaymentForInvoice)
{
this.Database.InvoiceAccessor.UpdatePaid((int)p.BillID, p.PaidToDate + p.Distribution, payment.CashDate, transaction);
}
else
{
this.Database.PurchaseOrderAccessor.UpdatePaid((int)p.BillID, p.PaidToDate + p.Distribution, payment.CashDate, transaction);
}
}
p.PaidToDate += p.Distribution;
p.Distribution = 0;
if (p.IsCreditItem)
{
if (type == Payment.PaymentBillType.PaymentForInvoice)
{
Customer customer = contact as Customer;
customer.CustomerCredit = p.PaidToDate;
customer.CustomerCreditDate = payment.CashDate;
this.Database.CustomerAccessor.Save(customer, transaction);
}
else
{
Vendor vendor = contact as Vendor;
vendor.VendorCredit = p.PaidToDate;
vendor.VendorCreditDate = payment.CashDate;
this.Database.VendorAccessor.Save(vendor, transaction);
}
if (p.Due == 0)
{
paymentBillItemList.HasCreditItem = false;
paymentBillItemList.Remove(p);
break;
}
}
}
}
CommandHelper.CommitTransaction(transaction);
}
catch (Exception ex)
{
transaction.Rollback();
throw new PaymentException(ex);
}
}
}
protected override CommandFieldValueList BuildPairList(Entity entity)
{
Payment payment = (Payment)entity;
CommandFieldValueList pairList = new CommandFieldValueList(payment.ID);
pairList.Add(PaymentSchema.FieldBillType, (int)payment.BillType);
pairList.Add(PaymentSchema.FieldCashDate, payment.CashDate);
pairList.Add(PaymentSchema.FieldPaymentMethod, payment.PaymentMethod);
pairList.Add(PaymentSchema.FieldPaymentType, payment.PaymentType);
pairList.Add(PaymentSchema.FieldAmount, payment.Amount);
pairList.Add(PaymentSchema.FieldContactID, payment.ContactID);
pairList.Add(PaymentSchema.FieldIsOnlinePay, payment.IsOnlinePay);
pairList.Add(PaymentSchema.FieldIsValid, payment.IsValid);
return pairList;
}
protected override Entity ReadEntity(RecordReader reader)
{
Payment payment = new Payment();
payment.ID = reader.GetInt32(PaymentSchema.FieldID);
payment.BillType = (Payment.PaymentBillType)reader.GetInt32(PaymentSchema.FieldBillType);
payment.CashDate = reader.GetDateTime(PaymentSchema.FieldCashDate);
payment.PaymentMethod = reader.GetString(PaymentSchema.FieldPaymentMethod);
payment.PaymentType = reader.GetString(PaymentSchema.FieldPaymentType);
payment.Amount = reader.GetDecimal(PaymentSchema.FieldAmount);
payment.ContactID = reader.GetInt32(PaymentSchema.FieldContactID);
payment.IsOnlinePay = reader.GetBoolean(PaymentSchema.FieldIsOnlinePay);
payment.IsValid = reader.GetBoolean(PaymentSchema.FieldIsValid);
if (payment.BillType == Payment.PaymentBillType.PaymentForInvoice)
{
Customer customer = this.Database.CustomerAccessor.SelectCustomer(payment.ContactID.Value);
customer.ContactKeyObj.CopyTo(payment.ContactKey);
}
else
{
Vendor vendor = this.Database.VendorAccessor.SelectVendor(payment.ContactID.Value);
vendor.ContactKeyObj.CopyTo(payment.ContactKey);
}
return payment;
}
public PaymentList SelectAllPaymentWithoutDetailList(bool isValid, Payment.PaymentBillType billType)
{
return (PaymentList)SelectList(PaymentSchema.FieldIsValid, isValid, PaymentSchema.FieldBillType, (int)billType);
}
public PaymentList SelectAllPaymentWithoutDetailListByContact(bool isValid, Payment.PaymentBillType billType, int contactID)
{
DbCommand command = this.CommandSelectEntity();
CommandFactory.WhereAnd(command, PaymentSchema.FieldIsValid, isValid);
CommandFactory.WhereAnd(command, PaymentSchema.FieldBillType, (int)billType);
CommandFactory.WhereAnd(command, PaymentSchema.FieldContactID, contactID);
return (PaymentList)this.SelectList(command);
}
public PaymentList SelectAllPaymentByContact(bool isValid, Payment.PaymentBillType billType, int contactID)
{
DbCommand command = this.CommandSelectEntity();
CommandFactory.WhereAnd(command, PaymentSchema.FieldIsValid, isValid);
CommandFactory.WhereAnd(command, PaymentSchema.FieldBillType, (int)billType);
CommandFactory.WhereAnd(command, PaymentSchema.FieldContactID, contactID);
IList list = this.SelectList(command);
foreach (Payment p in list)
{
p.PaymentDetailList = this.Database.PaymentDetailAccessor.SelectAllPaymentDetails((int)p.ID);
}
return (PaymentList)list;
}
protected override IList CreateEntityList()
{
return new PaymentList();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -