⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 billaccessor.cs.svn-base

📁 MIED是用于个人帐务管理的小型软件
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:

        protected override Entity ReadEntity(RecordReader reader)
        {
            Bill bill = this.NewBill();
            BillType type = (BillType)reader.GetInt32(BillSchema.FieldBillType);
            if (bill.BillType != type)
                throw new Exception("Bill Type NO Match");

            bill.ID = reader.GetInt32(BillSchema.FieldID);
            bill.Number = reader.GetString(BillSchema.FieldNumber);
            bill.Date = reader.GetDateTime(BillSchema.FieldDate);
            bill.InvoiceType = (InvoiceType)reader.GetInt32(BillSchema.FieldInvoiceType);
            bill.BillTo.FirstName = reader.GetString(ContactSchema.FieldBillToFirstName);
            bill.BillTo.LastName = reader.GetString(ContactSchema.FieldBillToLastName);
            bill.BillTo.Company = reader.GetString(ContactSchema.FieldBillToCompany);
            bill.BillTo.Address1 = reader.GetString(BillSchema.FieldBillToAddress1);
            bill.BillTo.Address2 = reader.GetString(BillSchema.FieldBillToAddress2);
            bill.BillTo.City = reader.GetString(BillSchema.FieldBillToCity);
            bill.BillTo.State = reader.GetString(BillSchema.FieldBillToState);
            bill.BillTo.Zip = reader.GetString(BillSchema.FieldBillToZip);
            bill.BillTo.Country = reader.GetString(BillSchema.FieldBillToCountry);
            bill.ShipTo.FirstName = reader.GetString(BillSchema.FieldShipToFirstName);
            bill.ShipTo.LastName = reader.GetString(BillSchema.FieldShipToLastName);
            bill.ShipTo.Company = reader.GetString(BillSchema.FieldShipToCompany);
            bill.ShipTo.Address1 = reader.GetString(BillSchema.FieldShipToAddress1);
            bill.ShipTo.Address2 = reader.GetString(BillSchema.FieldShipToAddress2);
            bill.ShipTo.City = reader.GetString(BillSchema.FieldShipToCity);
            bill.ShipTo.State = reader.GetString(BillSchema.FieldShipToState);
            bill.ShipTo.Zip = reader.GetString(BillSchema.FieldShipToZip);
            bill.ShipTo.Country = reader.GetString(BillSchema.FieldShipToCountry);
            bill.General.Info1 = reader.GetString(BillSchema.FieldGeneral1);
            bill.General.Info2 = reader.GetString(BillSchema.FieldGeneral2);
            bill.General.Info3 = reader.GetString(BillSchema.FieldGeneral3);
            bill.General.Info4 = reader.GetString(BillSchema.FieldGeneral4);
            bill.General.Info5 = reader.GetString(BillSchema.FieldGeneral5);
            bill.General.Info6 = reader.GetString(BillSchema.FieldGeneral6);
            bill.PaidDate = reader.GetDateTime(BillSchema.FieldPaidDate);
            bill.Void.Has = reader.GetBoolean(BillSchema.FieldVoid);
            bill.Void.DateTime = reader.GetDateTime(BillSchema.FieldVoidDate);
            bill.LineItemDiscountType = (DiscountType)reader.GetInt32(BillSchema.FieldLineItemDiscountType);
            bill.DiscountPercent = reader.GetDecimal(BillSchema.FieldDiscountPercent);
            bill.ShippingCost = reader.GetDecimal(BillSchema.FieldShippingCost);
            bill.Tax1Enabled = reader.GetBoolean(BillSchema.FieldTax1Enabled);
            bill.Tax1Name = reader.GetString(BillSchema.FieldTax1Name);
            bill.Tax1Percent = reader.GetDecimal(BillSchema.FieldTax1Percent);
            bill.Tax1Amount = reader.GetDecimal(BillSchema.FieldTax1Amount);
            bill.Tax2Enabled = reader.GetBoolean(BillSchema.FieldTax2Enabled);
            bill.Tax2Name = reader.GetString(BillSchema.FieldTax2Name);
            bill.Tax2Percent = reader.GetDecimal(BillSchema.FieldTax2Percent);
            bill.Tax2Amount = reader.GetDecimal(BillSchema.FieldTax2Amount);
            bill.AmountPaid = reader.GetDecimal(BillSchema.FieldAmountPaid);
            bill.Message = reader.GetString(BillSchema.FieldMessage);
            bill.FormTemplateID = reader.GetInt32(BillSchema.FieldFormTemplateID);
            decimal billTotal = reader.GetDecimal(BillSchema.FieldTotal);
            bill.RelationNumber = reader.GetString(BillSchema.FieldRelationNumber);
            bill.CompanyProfileID = reader.GetInt32Null(BillSchema.FieldCompanyProfileID);

            LineItemList list = this.Database.LineItemAccessor.SelectList(bill.ID.Value);
            BillLineItemList.SaveLineItemListToBill(list, bill);
            return bill;
        }

        protected override void SaveBefore(Entity entity, DbTransaction transaction)
        {
            Bill bill = (Bill)entity;
            this.CheckNumberDuplicate(bill);
            this.VerifyLineItems(bill);
        }

        protected override void SaveAfter(Entity entity, Entity entityOld, DbTransaction transaction)
        {
            Bill bill = (Bill)entity;
            Bill billOld = (Bill)entityOld;
            this.SaveLineItems(bill, transaction);
            this.AffectInventory(bill, billOld, transaction);
        }

        protected override DbCommand CommandSelectEntity()
        {
            CommandFieldList contactFields = new CommandFieldList(ContactSchema.TableName);
            contactFields.Add(ContactSchema.FieldBillToFirstName);
            contactFields.Add(ContactSchema.FieldBillToLastName);
            contactFields.Add(ContactSchema.FieldBillToCompany);
            return CommandSelectBill("*", contactFields);
        }

        protected override DbCommand CommandSelectID()
        {
            DbCommand command = base.CommandSelectID();
            CommandFactory.WhereAnd(command, BillSchema.FieldBillType, (int)this.BillType);
            return command;
        }

        protected override void CommandSaveSupplement(DbCommand command)
        {
            SqlCECommandHelper.SetParameterType_NText(command, "@" + BillSchema.FieldMessage);
        }

        private int VerifyContact(Bill bill)
        {
            if (bill.BillType == BillType.PurchaseOrder)
                return this.Database.VendorAccessor.VerifyContact(bill);
            else
                return this.Database.CustomerAccessor.VerifyContact(bill);
        }

        private void VerifyLineItems(Bill bill)
        {
            if (bill.InvoiceType != InvoiceType.Common)
                return;

            foreach (BillLineItem lineItem in bill.LineItemList)
                this.Database.ItemAccessor.Verify(lineItem);
        }

        private void SaveLineItems(Bill bill, DbTransaction transaction)
        {
            LineItemList list = BillLineItemList.GetLineItemList(bill);
            this.Database.LineItemAccessor.SaveList(bill.ID.Value, list, transaction);
        }

        private void CheckNumberDuplicate(Bill bill)
        {
            if (!bill.IsNew)
                return;

            int? id = this.SelectID(BillSchema.FieldNumber, bill.Number);
            if (id != null)
                throw new BillNumberDuplicateException(bill);
        }

        private void AffectInventory(Bill bill, Bill oldBill, DbTransaction transaction)
        {
            if (this.InventoryMode == InventoryMode.NoAffect)
                return;

            if (bill.InvoiceType != InvoiceType.Common)
                return;

            ItemInventoryList oldList = oldBill == null ? new ItemInventoryList() : oldBill.ItemInventoryList;
            ItemInventoryList newList = bill.ItemInventoryList;
            ItemInventoryList updatedList = this.CalculateInventory(oldList, newList);
            this.Database.ItemAccessor.UpdateInventories(updatedList, transaction);
        }

        private string SelectLastNumber()
        {
            int? maxID = SelectMaxID();
            if (maxID == null)
                return null;

            DbCommand command = CommandFactory.SelectField(this.Database.Connection, this.TableName, BillSchema.FieldNumber);
            CommandFactory.WhereAnd(command, BillSchema.FieldID, maxID.Value);
            string number = CommandHelper.ExecuteScalar_String(command);
            return number;
        }

        private int? SelectMaxID()
        {
            DbCommand command = CommandFactory.SelectMaxID(this.Database.Connection, this.TableName);
            CommandFactory.WhereAnd(command, BillSchema.FieldBillType, this.BillType);
            CommandFactory.WhereAnd(command, BillSchema.FieldInvoiceType, (int)InvoiceType.Common);
            int? maxID = CommandHelper.ExecuteScalar_Int(command);
            return maxID;
        }

        private bool NumberExisted(string number)
        {
            int count = CommandHelper.SelectCount(this.Database.Connection, this.TableName, BillSchema.FieldNumber, number);
            return count > 0;
        }

        private ItemInventoryList CalculateInventory(ItemInventoryList oldList, ItemInventoryList newList)
        {
            ItemInventoryList resultList = new ItemInventoryList();
            if (this.InventoryMode == InventoryMode.Add)
            {
                resultList = resultList + oldList;
                resultList = resultList - newList;
            }
            else
            {
                resultList = resultList - oldList;
                resultList = resultList + newList;
            }
            return resultList;
        }

        private BillSummary ReadSummary(RecordReader reader)
        {
            BillSummary summary = new BillSummary();
            summary.ID = reader.GetInt32(BillSchema.FieldID);
            summary.BillType = (BillType)reader.GetInt32(BillSchema.FieldBillType);
            summary.Number = reader.GetString(BillSchema.FieldNumber);
            summary.Date = reader.GetDateTime(BillSchema.FieldDate);
            summary.FirstName = reader.GetString(ContactSchema.FieldBillToFirstName);
            summary.LastName = reader.GetString(ContactSchema.FieldBillToLastName);
            summary.Company = reader.GetString(ContactSchema.FieldBillToCompany);
            summary.Paid = reader.GetDecimal(BillSchema.FieldAmountPaid);
            summary.Amount = reader.GetDecimal(BillSchema.FieldTotal);
            summary.SubTotal = reader.GetDecimal(BillSchema.FieldSubTotal);
            summary.DiscountPercent = reader.GetDecimal(BillSchema.FieldDiscountPercent);
            summary.Tax1Name = reader.GetString(BillSchema.FieldTax1Name);
            summary.Tax1Amount = reader.GetDecimal(BillSchema.FieldTax1Amount);
            summary.Tax1Percent = reader.GetDecimal(BillSchema.FieldTax1Percent);
            summary.Tax1BaseTotal = reader.GetDecimal(BillSchema.FieldTax1BaseTotal);
            summary.Tax2Name = reader.GetString(BillSchema.FieldTax2Name);
            summary.Tax2Amount = reader.GetDecimal(BillSchema.FieldTax2Amount);
            summary.Tax2Percent = reader.GetDecimal(BillSchema.FieldTax2Percent);
            summary.Tax2BaseTotal = reader.GetDecimal(BillSchema.FieldTax2BaseTotal);
            summary.State = reader.GetString(ContactSchema.FieldBillToState);
            summary.PaidDate = reader.GetDateTime(BillSchema.FieldPaidDate);
            return summary;
        }

        private void UpdateTemplateID(DbConnection connection, int id, bool isPurchaseOrder)
        {
            DbCommand command = CommandFactory.Update(connection, this.TableName, BillSchema.FieldFormTemplateID, id);
            if (isPurchaseOrder)
            {
                CommandFactory.WhereAnd(command, BillSchema.FieldBillType, (int)BillType.PurchaseOrder);
            }
            else
            {
                CommandFactory.WhereAnd(command, BillSchema.FieldBillType, (int)BillType.Invoice);
                CommandFactory.WhereOr(command, BillSchema.FieldBillType, (int)BillType.Estimate);
            }

            CommandHelper.ExecuteCommand(command);
        }

        private DbCommand CommandSelectSummary()
        {
            CommandFieldList summaryFields = new CommandFieldList(BillSchema.TableName);
            summaryFields.Add(BillSchema.FieldID);
            summaryFields.Add(BillSchema.FieldBillType);
            summaryFields.Add(BillSchema.FieldNumber);
            summaryFields.Add(BillSchema.FieldDate);
            summaryFields.Add(BillSchema.FieldTotal);
            summaryFields.Add(BillSchema.FieldAmountPaid);
            summaryFields.Add(BillSchema.FieldSubTotal);
            summaryFields.Add(BillSchema.FieldDiscountPercent);
            summaryFields.Add(BillSchema.FieldTax1Name);
            summaryFields.Add(BillSchema.FieldTax1Percent);
            summaryFields.Add(BillSchema.FieldTax1Amount);
            summaryFields.Add(BillSchema.FieldTax1BaseTotal);
            summaryFields.Add(BillSchema.FieldTax2Name);
            summaryFields.Add(BillSchema.FieldTax2Percent);
            summaryFields.Add(BillSchema.FieldTax2Amount);
            summaryFields.Add(BillSchema.FieldTax2BaseTotal);
            summaryFields.Add(BillSchema.FieldBillToState);
            summaryFields.Add(BillSchema.FieldPaidDate);

            CommandFieldList contactFields = new CommandFieldList(ContactSchema.TableName);
            contactFields.Add(ContactSchema.FieldBillToFirstName);
            contactFields.Add(ContactSchema.FieldBillToLastName);
            contactFields.Add(ContactSchema.FieldBillToCompany);

            return CommandSelectBill(summaryFields, contactFields);
        }

        private DbCommand CommandSelectBill(string billFields, string contactFields)
        {
            DbCommand command = CommandFactory.SelectFieldsUnion(this.Database.Connection,
                BillSchema.TableName, ContactSchema.TableName, billFields, contactFields, BillSchema.FieldContactID, ContactSchema.FieldID);
            return command;
        }

        private Bill NewBill()
        {

            Bill bill = (Bill)Activator.CreateInstance(this.BillObjectType);
            return bill;
        }

        private BillType BillType
        {
            get
            {
                Bill bill = this.NewBill();
                return bill.BillType;
            }
        }

        private readonly Type BillObjectType;

        private readonly string FirstNumber = "1001";

        private readonly string NumberPrefix;

        private readonly InventoryMode InventoryMode = InventoryMode.Add;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -