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(); } } } } } }
public void BaseCreateSalesInvoice(PurchaseInvoiceCreateSalesInvoice method) { var salesInvoice = new SalesInvoiceBuilder(this.Strategy.Session) .WithPurchaseInvoice(this) .WithBilledFrom(this.BilledTo) .WithBilledFromContactPerson(this.BilledToContactPerson) .WithBillToCustomer(this.BillToEndCustomer) .WithAssignedBillToContactMechanism(this.DerivedBillToEndCustomerContactMechanism) .WithBillToContactPerson(this.BillToEndCustomerContactPerson) .WithShipToCustomer(this.ShipToEndCustomer) .WithAssignedShipToAddress(this.DerivedShipToEndCustomerAddress) .WithShipToContactPerson(this.ShipToEndCustomerContactPerson) .WithDescription(this.Description) .WithInvoiceDate(this.Session().Now()) .WithSalesInvoiceType(new SalesInvoiceTypes(this.Strategy.Session).SalesInvoice) .WithCustomerReference(this.CustomerReference) .WithAssignedPaymentMethod(this.DerivedBillToCustomerPaymentMethod) .WithComment(this.Comment) .WithInternalComment(this.InternalComment) .Build(); foreach (OrderAdjustment orderAdjustment in this.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); } foreach (PurchaseInvoiceItem purchaseInvoiceItem in this.PurchaseInvoiceItems) { var invoiceItem = new SalesInvoiceItemBuilder(this.Strategy.Session) .WithInvoiceItemType(purchaseInvoiceItem.InvoiceItemType) .WithAssignedUnitPrice(purchaseInvoiceItem.AssignedUnitPrice) .WithProduct(purchaseInvoiceItem.Part as UnifiedGood) .WithSerialisedItem(purchaseInvoiceItem.SerialisedItem) .WithNextSerialisedItemAvailability(new SerialisedItemAvailabilities(this.Session()).Sold) .WithQuantity(purchaseInvoiceItem.Quantity) .WithComment(purchaseInvoiceItem.Comment) .WithInternalComment(purchaseInvoiceItem.InternalComment) .Build(); salesInvoice.AddSalesInvoiceItem(invoiceItem); } var internalOrganisation = (InternalOrganisation)salesInvoice.BilledFrom; if (!internalOrganisation.ActiveCustomers.Contains(salesInvoice.BillToCustomer)) { new CustomerRelationshipBuilder(this.Strategy.Session) .WithCustomer(salesInvoice.BillToCustomer) .WithInternalOrganisation(internalOrganisation) .Build(); } this.AddDeniedPermission(new Permissions(this.Strategy.Session).Get(this.Meta.ObjectType, this.Meta.CreateSalesInvoice, Operations.Execute)); }