示例#1
0
        public void AppsInvoice(CustomerShipmentInvoice method)
        {
            var derivation = new Derivation(this.Strategy.Session);

            if (this.CustomerShipmentState.Equals(new CustomerShipmentStates(this.Strategy.Session).Shipped) &&
                Equals(this.Store.BillingProcess, new BillingProcesses(this.strategy.Session).BillingForShipmentItems))
            {
                this.AppsInvoiceThis(derivation);
            }
        }
示例#2
0
        public void BaseInvoice(CustomerShipmentInvoice method)
        {
            if (this.ShipmentState.Equals(new ShipmentStates(this.Strategy.Session).Shipped) &&
                Equals(this.Store.BillingProcess, new BillingProcesses(this.Strategy.Session).BillingForShipmentItems))
            {
                var invoiceByOrder = new Dictionary <SalesOrder, SalesInvoice>();
                var costsInvoiced  = false;

                foreach (ShipmentItem shipmentItem in this.ShipmentItems)
                {
                    foreach (OrderShipment orderShipment in shipmentItem.OrderShipmentsWhereShipmentItem)
                    {
                        var salesOrder = orderShipment.OrderItem.OrderWhereValidOrderItem as SalesOrder;

                        if (!invoiceByOrder.TryGetValue(salesOrder, out var salesInvoice))
                        {
                            salesInvoice = new SalesInvoiceBuilder(this.Strategy.Session)
                                           .WithStore(salesOrder.Store)
                                           .WithBilledFrom(salesOrder.TakenBy)
                                           .WithAssignedBilledFromContactMechanism(salesOrder.DerivedTakenByContactMechanism)
                                           .WithBilledFromContactPerson(salesOrder.TakenByContactPerson)
                                           .WithBillToCustomer(salesOrder.BillToCustomer)
                                           .WithAssignedBillToContactMechanism(salesOrder.DerivedBillToContactMechanism)
                                           .WithBillToContactPerson(salesOrder.BillToContactPerson)
                                           .WithBillToEndCustomer(salesOrder.BillToEndCustomer)
                                           .WithAssignedBillToEndCustomerContactMechanism(salesOrder.DerivedBillToEndCustomerContactMechanism)
                                           .WithBillToEndCustomerContactPerson(salesOrder.BillToEndCustomerContactPerson)
                                           .WithShipToCustomer(salesOrder.ShipToCustomer)
                                           .WithAssignedShipToAddress(salesOrder.DerivedShipToAddress)
                                           .WithShipToContactPerson(salesOrder.ShipToContactPerson)
                                           .WithShipToEndCustomer(salesOrder.ShipToEndCustomer)
                                           .WithAssignedShipToEndCustomerAddress(salesOrder.DerivedShipToEndCustomerAddress)
                                           .WithShipToEndCustomerContactPerson(salesOrder.ShipToEndCustomerContactPerson)
                                           .WithInvoiceDate(this.Session().Now())
                                           .WithSalesChannel(salesOrder.SalesChannel)
                                           .WithSalesInvoiceType(new SalesInvoiceTypes(this.Strategy.Session).SalesInvoice)
                                           .WithAssignedVatRegime(salesOrder.DerivedVatRegime)
                                           .WithAssignedIrpfRegime(salesOrder.DerivedIrpfRegime)
                                           .WithCustomerReference(salesOrder.CustomerReference)
                                           .WithAssignedPaymentMethod(this.PaymentMethod)
                                           .Build();

                            invoiceByOrder.Add(salesOrder, salesInvoice);

                            foreach (OrderAdjustment orderAdjustment in salesOrder.OrderAdjustments)
                            {
                                OrderAdjustment newAdjustment = null;
                                if (orderAdjustment.GetType().Name.Equals(typeof(DiscountAdjustment).Name))
                                {
                                    newAdjustment = new DiscountAdjustmentBuilder(this.Session()).Build();
                                }

                                if (orderAdjustment.GetType().Name.Equals(typeof(SurchargeAdjustment).Name))
                                {
                                    newAdjustment = new SurchargeAdjustmentBuilder(this.Session()).Build();
                                }

                                if (orderAdjustment.GetType().Name.Equals(typeof(Fee).Name))
                                {
                                    newAdjustment = new FeeBuilder(this.Session()).Build();
                                }

                                if (orderAdjustment.GetType().Name.Equals(typeof(ShippingAndHandlingCharge).Name))
                                {
                                    newAdjustment = new ShippingAndHandlingChargeBuilder(this.Session()).Build();
                                }

                                if (orderAdjustment.GetType().Name.Equals(typeof(MiscellaneousCharge).Name))
                                {
                                    newAdjustment = new MiscellaneousChargeBuilder(this.Session()).Build();
                                }

                                newAdjustment.Amount ??= orderAdjustment.Amount;
                                newAdjustment.Percentage ??= orderAdjustment.Percentage;
                                salesInvoice.AddOrderAdjustment(newAdjustment);
                            }

                            if (!costsInvoiced)
                            {
                                var costs = this.BaseOnDeriveShippingAndHandlingCharges();
                                if (costs > 0)
                                {
                                    salesInvoice.AddOrderAdjustment(new ShippingAndHandlingChargeBuilder(this.Strategy.Session).WithAmount(costs).Build());
                                    costsInvoiced = true;
                                }
                            }

                            foreach (SalesTerm salesTerm in salesOrder.SalesTerms)
                            {
                                if (salesTerm.GetType().Name == typeof(IncoTerm).Name)
                                {
                                    salesInvoice.AddSalesTerm(new IncoTermBuilder(this.Strategy.Session)
                                                              .WithTermType(salesTerm.TermType)
                                                              .WithTermValue(salesTerm.TermValue)
                                                              .WithDescription(salesTerm.Description)
                                                              .Build());
                                }

                                if (salesTerm.GetType().Name == typeof(InvoiceTerm).Name)
                                {
                                    salesInvoice.AddSalesTerm(new InvoiceTermBuilder(this.Strategy.Session)
                                                              .WithTermType(salesTerm.TermType)
                                                              .WithTermValue(salesTerm.TermValue)
                                                              .WithDescription(salesTerm.Description)
                                                              .Build());
                                }

                                if (salesTerm.GetType().Name == typeof(OrderTerm).Name)
                                {
                                    salesInvoice.AddSalesTerm(new OrderTermBuilder(this.Strategy.Session)
                                                              .WithTermType(salesTerm.TermType)
                                                              .WithTermValue(salesTerm.TermValue)
                                                              .WithDescription(salesTerm.Description)
                                                              .Build());
                                }
                            }
                        }

                        var amountAlreadyInvoiced = shipmentItem.ShipmentItemBillingsWhereShipmentItem.Sum(v => v.Amount);
                        var leftToInvoice         = (orderShipment.OrderItem.QuantityOrdered * orderShipment.OrderItem.AssignedUnitPrice) - amountAlreadyInvoiced;

                        if (leftToInvoice > 0)
                        {
                            if (orderShipment.OrderItem is SalesOrderItem salesOrderItem)
                            {
                                var invoiceItem = new SalesInvoiceItemBuilder(this.Strategy.Session)
                                                  .WithInvoiceItemType(new InvoiceItemTypes(this.Strategy.Session).ProductItem)
                                                  .WithProduct(salesOrderItem.Product)
                                                  .WithQuantity(orderShipment.Quantity)
                                                  .WithAssignedUnitPrice(salesOrderItem.UnitPrice)
                                                  .WithAssignedVatRegime(salesOrderItem.AssignedVatRegime)
                                                  .WithDescription(salesOrderItem.Description)
                                                  .WithInternalComment(salesOrderItem.InternalComment)
                                                  .WithMessage(salesOrderItem.Message)
                                                  .Build();

                                salesInvoice.AddSalesInvoiceItem(invoiceItem);

                                new ShipmentItemBillingBuilder(this.Strategy.Session)
                                .WithQuantity(shipmentItem.Quantity)
                                .WithAmount(leftToInvoice)
                                .WithShipmentItem(shipmentItem)
                                .WithInvoiceItem(invoiceItem)
                                .Build();
                            }
                        }
                    }
                }
            }
        }