/// <summary> /// Sets the adjustment type /// </summary> /// <param name="invoiceAddItems"></param> /// <param name="invoiceOrderShipment"></param> /// <returns></returns> public virtual InvoiceAdjustmentType SetInvoiceAdjustmentType(InvoiceAddItems invoiceAddItems, InvoiceOrderShipment invoiceOrderShipment) { if (invoiceOrderShipment.Invoice != null) { // If there is more than one item it's adding products if (invoiceAddItems.IsAddProduct) { return(InvoiceAdjustmentType.AddProducts); } // If there are any with 0 for qty it's a delete if (invoiceAddItems.Items.Any(x => x.Quantity <= 0)) { return(InvoiceAdjustmentType.DeleteProduct); } // Lastly see if any updates if (invoiceAddItems.Items.Any(x => x.NeedsUpdating)) { return(InvoiceAdjustmentType.UpdateProductDetails); } } // Default type which is ignored return(InvoiceAdjustmentType.General); }
public virtual HttpResponseMessage PutInvoiceNewProducts(InvoiceAddItems invoiceAddItems) { var response = Request.CreateResponse(HttpStatusCode.OK); try { if (invoiceAddItems.Items != null) { // Get the invoice and associated data var invoiceOrderShipment = _invoiceService.GetInvoiceOrderShipment(invoiceAddItems.InvoiceKey); if (invoiceOrderShipment.Invoice != null) { //var currentUser = Umbraco.UmbracoContext.Security.CurrentUser; var invoiceAdjustmentResult = new InvoiceAdjustmentResult(invoiceAddItems.LineItemType); // The adjustment type var invoiceAdjustmentType = SetInvoiceAdjustmentType(invoiceAddItems, invoiceOrderShipment); //// Use the merchello helper to get the prdoucts, so the data modifiers are triggered. var merchelloHelper = new MerchelloHelper(); // Get the product service var productService = merchelloHelper.Query.Product; // See if we can get the products foreach (var invoiceAddItem in invoiceAddItems.Items) { if (invoiceAddItems.IsAddProduct && invoiceAddItem.Key != Guid.Empty) { invoiceAddItem.ProductVariant = productService.GetProductVariantByKey(invoiceAddItem.Key); invoiceAddItem.Product = productService.GetByKey(invoiceAddItem.Key); // Complete the Sku too invoiceAddItem.Sku = invoiceAddItem.Product != null ? invoiceAddItem.Product.Sku : invoiceAddItem.ProductVariant.Sku; } else if (!string.IsNullOrWhiteSpace(invoiceAddItem.Sku)) { invoiceAddItem.ProductVariant = productService.GetProductVariantBySku(invoiceAddItem.Sku); invoiceAddItem.Product = productService.GetBySku(invoiceAddItem.Sku); } } // Work out the type of adjustment switch (invoiceAdjustmentType) { case InvoiceAdjustmentType.AddProducts: invoiceAdjustmentResult = AddNewLineItemsToInvoice(invoiceOrderShipment, invoiceAddItems.Items, invoiceAdjustmentResult); break; case InvoiceAdjustmentType.DeleteProduct: invoiceAdjustmentResult = DeleteLineItemsFromInvoice(invoiceOrderShipment, invoiceAddItems.Items, invoiceAdjustmentResult); break; case InvoiceAdjustmentType.UpdateProductDetails: invoiceAdjustmentResult = UpdateCustomProductsOnInvoice(invoiceOrderShipment, invoiceAddItems.Items, invoiceAdjustmentResult); break; } if (!invoiceAdjustmentResult.Success) { response = Request.CreateResponse(HttpStatusCode.Conflict, invoiceAdjustmentResult.Message); MultiLogHelper.Warn <InvoiceApiController>(invoiceAdjustmentResult.Message); } } else { response = Request.CreateResponse(HttpStatusCode.NotFound, "Invoice not found"); } } else { response = Request.CreateResponse(HttpStatusCode.NotFound, "No items to update"); } } catch (Exception ex) { MultiLogHelper.Error <InvoiceApiController>("Failed to adjust invoice", ex); response = Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message); } return(response); }