示例#1
0
        public Invoice CreateInvoiceObject(InvoiceCommand invoiceCommand, IEnumerable<Good> goods, Company owner, Company transporter, Company supplier, Invoice invoiceRefrence, List<Order> orderRefrences, Currency currency, List<InvoiceItem> invoiceItems, List<InvoiceAdditionalPrice> invoiceAdditionalPriceList, bool forCalculate)
        {
            var invoice = new Invoice
                (
                invoiceCommand.InvoiceType, invoiceCommand.InvoiceNumber, owner, invoiceCommand.InvoiceDate,
                invoiceCommand.DivisionMethod, invoiceCommand.AccountingType, invoiceRefrence, orderRefrences,currency,invoiceCommand.IsCreditor, transporter,
                supplier, invoiceCommand.Description, invoiceItems, invoiceAdditionalPriceList, invoiceConfigurator, invoiceDomainService,
                invoiceItemDomainService,goodUnitConvertorDomainService,invoiceAdditionalPriceDomainService,balanceDomainService);

            if (!forCalculate)
            {
                var init = workflowRepository.Single
                    (c => c.WorkflowEntity == WorkflowEntities.Invoice && c.CurrentWorkflowStage == WorkflowStages.Initial);
                var invoiceWorkflow = new InvoiceWorkflowLog
                    (invoice.Id, WorkflowEntities.Invoice, DateTime.Now, WorkflowActions.Init,
                    //TODO: Fake ActorId
                    1101, "", init.Id, true);
                invoice.ApproveWorkFlows.Add(invoiceWorkflow);
            }
            return invoice;
        }
 private List<InvoiceAdditionalPrice> GetAdditionalPrice(InvoiceCommand invoiceCommand)
 {
     var effectiveFactors = effectiveFactorDomainService.GetEffectiveFactors();
     var invoiceAdditionalPriceList =
         invoiceCommand.AdditionalPrices.Select
             (
                 additionalPrice =>
                     new InvoiceAdditionalPrice
                     (
                     effectiveFactors.Single(c => c.Id == additionalPrice.EffectiveFactorId), additionalPrice.Price,
                     additionalPrice.Divisionable, additionalPrice.Description)).ToList();
     return invoiceAdditionalPriceList;
 }
 private static List<InvoiceItem> GetInvoiceItems(InvoiceCommand invoiceCommand, IEnumerable<Good> goods)
 {
     var invoiceItems = new List<InvoiceItem>();
     foreach (var invoiceItem in invoiceCommand.InvoiceItems)
     {
         var good = goods.Single(c => c.Id == invoiceItem.GoodId);
         if (good == null)
             throw new ObjectNotFound("Good");
         var goodUnit = good.GoodUnits.SingleOrDefault(d => d.Id == invoiceItem.GoodUnitId);
         if (goodUnit == null)
             throw new ObjectNotFound("GoodUnit");
         invoiceItems.Add
             (new InvoiceItem(invoiceItem.Quantity, invoiceItem.Fee, good, goodUnit, invoiceItem.DivisionPrice, invoiceItem.Description));
     }
     return invoiceItems;
 }
        public Invoice Update(InvoiceCommand invoiceCommand)
        {
            var invoice = invoiceRepository.GetConfiguredInvoice(invoiceCommand.Id, new SingleResultFetchStrategy<Invoice>()
                   .Include(c => c.InvoiceItems).Include(c => c.OrderRefrences)
                   .Include(c => c.OrderRefrences.Select(d => d.OrderItems)), invoiceConfigurator);
            if (invoice == null)
                throw new ObjectNotFound("Invoice", invoiceCommand.Id);

            List<Order> orderRefrences = GetOrderRefrences(invoiceCommand.OrdersRefrenceId);
            Invoice invoiceRefrence = GetInvoiceRefrence(invoiceCommand.InvoiceRefrenceId);

            var transporter = invoiceCommand.TransporterId == null ? null : companyRepository.Single(c => c.Id == invoiceCommand.TransporterId);
            var supplier = invoiceCommand.SupplierId == null ? null : companyRepository.Single(c => c.Id == invoiceCommand.SupplierId);
            var owner = companyRepository.Single(c => c.Id == invoiceCommand.OwnerId);
            if (owner == null)
                throw new ObjectNotFound("Company", invoiceCommand.OwnerId);

            var currency = currencyDomainService.Get(invoiceCommand.CurrencyId);
            if (currency == null)
                throw new ObjectNotFound("Currency", invoiceCommand.CurrencyId);

            var goods = goodDomainService.GetCompanyGoodsWithUnits(invoiceCommand.OwnerId);

            if (invoiceCommand.InvoiceType != invoice.InvoiceType)
                throw new InvalidOperation("Update", "Can not Update Invoice Type");

            var invoiceItems = GetInvoiceItems(invoiceCommand, goods);

            var invoiceAdditionalPriceList = GetAdditionalPrice(invoiceCommand);

            invoice.Update
                (
                    invoiceCommand.InvoiceNumber, invoiceCommand.InvoiceDate, invoiceCommand.DivisionMethod, invoiceRefrence, orderRefrences, currency,
                    invoiceCommand.IsCreditor, transporter, supplier, invoiceCommand.Description, invoiceItems, invoiceAdditionalPriceList,
                    invoiceDomainService, invoiceItemDomainService, goodUnitConvertorDomainService, invoiceAdditionalPriceDomainService, balanceDomainService);

            invoiceRepository.Update(invoice);

            try
            {
                unitOfWorkScope.Commit();
            }
            catch (OptimisticConcurrencyException ex)
            {
                throw new ConcurencyException("Invoice");
            }
            return invoice;
        }
        public Invoice CalculateAdditionalPrice(InvoiceCommand invoiceCommand)
        {
            var invoice = new Invoice();
            if (invoiceCommand.Id != 0)
            {
                invoice = invoiceRepository.GetConfiguredInvoice(invoiceCommand.Id, new SingleResultFetchStrategy<Invoice>()
                    .Include(c => c.InvoiceItems)
                    .Include(c=>c.OrderRefrences)
                     .Include(c => c.AdditionalPrices)
                    .Include(c=>c.OrderRefrences.Select(d=>d.OrderItems)),invoiceConfigurator);

                if (invoice == null)
                    throw new ObjectNotFound("Invoice", invoiceCommand.Id);
            }

            var orderRefrences = GetOrderRefrences(invoiceCommand.OrdersRefrenceId);
            var invoiceRefrence = GetInvoiceRefrence(invoiceCommand.InvoiceRefrenceId);

            var transporter = invoiceCommand.TransporterId == null ? null : companyRepository.Single(c => c.Id == invoiceCommand.TransporterId);
            var supplier = invoiceCommand.SupplierId == null ? null : companyRepository.Single(c => c.Id == invoiceCommand.SupplierId);
            var owner = companyRepository.Single(c => c.Id == invoiceCommand.OwnerId);
            if (owner == null)
                throw new ObjectNotFound("Company", invoiceCommand.OwnerId);

            var currency = currencyDomainService.Get(invoiceCommand.CurrencyId);
            if (currency == null)
                throw new ObjectNotFound("Currency", invoiceCommand.CurrencyId);

            var goods = goodDomainService.GetCompanyGoodsWithUnits(invoiceCommand.OwnerId).ToList();

            if (invoiceCommand.Id != 0 && invoiceCommand.InvoiceType != invoice.InvoiceType)
                throw new InvalidOperation("Update", "Can not Update Invoice Type");

            var invoiceItems = GetInvoiceItems(invoiceCommand, goods).ToList();

            var invoiceAdditionalPriceList = GetAdditionalPrice(invoiceCommand);

            if (invoice.Id == 0)
                invoice = invoiceFactory.CreateInvoiceObject
                    (
                        invoiceCommand, goods, owner, transporter, supplier, invoiceRefrence, orderRefrences, currency, invoiceItems,
                        invoiceAdditionalPriceList, true);
            else
                invoice.Update
                    (
                        invoiceCommand.InvoiceNumber, invoiceCommand.InvoiceDate, invoiceCommand.DivisionMethod, invoiceRefrence, orderRefrences,
                        currency, invoiceCommand.IsCreditor, transporter, supplier, invoiceCommand.Description, invoiceItems,
                        invoiceAdditionalPriceList, invoiceDomainService, invoiceItemDomainService, goodUnitConvertorDomainService,
                        invoiceAdditionalPriceDomainService, balanceDomainService);

            var invoice2 = invoiceAdditionalPriceDomainService.CalculateAdditionalPrice(invoice);

            return invoice2;
        }
        public Invoice Add(InvoiceCommand invoiceCommand)
        {
            List<Order> orderRefrences = GetOrderRefrences(invoiceCommand.OrdersRefrenceId);
            Invoice invoiceRefrence = GetInvoiceRefrence(invoiceCommand.InvoiceRefrenceId);

            var transporter = invoiceCommand.TransporterId == null ? null : companyRepository.Single(c => c.Id == invoiceCommand.TransporterId);
            var supplier = invoiceCommand.SupplierId == null ? null : companyRepository.Single(c => c.Id == invoiceCommand.SupplierId);
            var owner = companyRepository.Single(c => c.Id == invoiceCommand.OwnerId);
            if (owner == null)
                throw new ObjectNotFound("Company", invoiceCommand.OwnerId);

            var currency = currencyDomainService.Get(invoiceCommand.CurrencyId);
            if (currency == null)
                throw new ObjectNotFound("Currency", invoiceCommand.CurrencyId);

            var goods = goodDomainService.GetCompanyGoodsWithUnits(invoiceCommand.OwnerId);

            var invoiceItems = GetInvoiceItems(invoiceCommand, goods);

            var invoiceAdditionalPriceList = GetAdditionalPrice(invoiceCommand);

            var invoice = invoiceFactory.CreateInvoiceObject
                (
                    invoiceCommand, goods, owner, transporter, supplier, invoiceRefrence, orderRefrences, currency, invoiceItems,
                    invoiceAdditionalPriceList, false);

            invoiceRepository.Add(invoice);

            unitOfWorkScope.Commit();

            return invoice;
        }