Skip to content

Commit

Permalink
Updated the install fee estimatation process in support of #364.
Browse files Browse the repository at this point in the history
  • Loading branch information
uncheckederror committed Aug 1, 2023
1 parent e317be7 commit 55b1726
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 19 deletions.
2 changes: 1 addition & 1 deletion NumberSearch.DataAccess/Models/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public async Task<bool> PutAsync(string connectionString)
.ExecuteAsync("UPDATE public.\"Products\" " +
"SET \"Name\" = @Name, \"Price\" = @Price, \"Description\" = @Description, \"Image\" = @Image, \"Public\" = @Public, \"QuantityAvailable\" = @QuantityAvailable, \"SupportLink\" = @SupportLink, \"DisplayPriority\" = @DisplayPriority, \"VendorPartNumber\" = @VendorPartNumber, \"Type\" = @Type, \"Tags\" = @Tags, \"VendorDescription\" = @VendorDescription, \"VendorFeatures\" = @VendorFeatures, \"MarkdownContent\" = @MarkdownContent, \"InstallTime\" = @InstallTime " +
"WHERE \"ProductId\" = @ProductId",
new { Name, Price, Description, Image, Public, ProductId, QuantityAvailable, SupportLink, DisplayPriority, VendorPartNumber, Type, VendorDescription, VendorFeatures, MarkdownContent, InstallTime })
new { Name, Price, Description, Image, Public, ProductId, QuantityAvailable, SupportLink, DisplayPriority, VendorPartNumber, Type, Tags, VendorDescription, VendorFeatures, MarkdownContent, InstallTime })
.ConfigureAwait(false);

if (result == 1)
Expand Down
110 changes: 94 additions & 16 deletions NumberSearch.Mvc/Controllers/CartController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Ical.Net.Serialization;

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

using NumberSearch.DataAccess;
using NumberSearch.DataAccess.InvoiceNinja;
Expand All @@ -17,7 +16,6 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.ServiceModel.Channels;
using System.Text.Json;
using System.Threading.Tasks;

Expand Down Expand Up @@ -158,6 +156,45 @@ public async Task<IActionResult> CheckoutAsync()
// Create a GUID for an order to prevent multiple order submissions from repeated button clicking.
cart.Order.OrderId = Guid.NewGuid();

// Default to Onsite Installation if there is hardware in the cart.
if (cart?.Products is not null && cart.Products.Count > 0)
{
cart.Order.OnsiteInstallation = true;

// Add the call out charge and install estimate to the Cart
Product onsite = await Product.GetByIdAsync(Guid.Parse("b174c76a-e067-4a6a-abcf-53b6d3a848e4"), _postgresql);
Product estimate = await Product.GetByIdAsync(Guid.Parse("a032b3ba-da57-4ad3-90ec-c59a3505b075"), _postgresql);

// Sum all of the install time estimates.
decimal totalInstallTime = 0m;
foreach (var item in cart.Products)
{
var quantity = cart.ProductOrders?.Where(x => x.ProductId == item.ProductId).FirstOrDefault();

if (item.InstallTime > 0m && quantity is not null)
{
totalInstallTime += item.InstallTime * quantity.Quantity;
}
}

var productOrderOnsite = new ProductOrder
{
ProductOrderId = Guid.NewGuid(),
ProductId = onsite.ProductId,
Quantity = 1
};

var productOrderEstimate = new ProductOrder
{
ProductOrderId = Guid.NewGuid(),
ProductId = estimate.ProductId,
Quantity = decimal.ToInt32(Math.Ceiling(totalInstallTime))
};

_ = cart.AddProduct(onsite, productOrderOnsite);
_ = cart.AddProduct(estimate, productOrderEstimate);
}

_ = cart.SetToSession(HttpContext.Session);
}

Expand Down Expand Up @@ -340,7 +377,7 @@ public async Task<IActionResult> SubmitAsync(Order order)
}
else
{
Log.Error($"[Checkout] Failed automatic address formating.");
Log.Error($"[Checkout] Failed automatic address formatting.");
}

// Fillout the address2 information from its components.
Expand Down Expand Up @@ -419,6 +456,57 @@ public async Task<IActionResult> SubmitAsync(Order order)

if (cart is not null && cart.ProductOrders is not null)
{
if (cart.Products is not null && cart.Products.Count > 0)
{
// Add the call out charge and install estimate to the Cart
Product onsite = await Product.GetByIdAsync(Guid.Parse("b174c76a-e067-4a6a-abcf-53b6d3a848e4"), _postgresql);
Product estimate = await Product.GetByIdAsync(Guid.Parse("a032b3ba-da57-4ad3-90ec-c59a3505b075"), _postgresql);

// Sum all of the install time estimates.
decimal totalInstallTime = 0m;
foreach (var item in cart.Products)
{
var quantity = cart.ProductOrders?.Where(x => x.ProductId == item.ProductId).FirstOrDefault();

if (item.InstallTime > 0m && quantity is not null)
{
totalInstallTime += item.InstallTime * quantity.Quantity;
}
}

var productOrderOnsite = new ProductOrder
{
ProductOrderId = Guid.NewGuid(),
ProductId = onsite.ProductId,
Quantity = 1
};

var productOrderEstimate = new ProductOrder
{
ProductOrderId = Guid.NewGuid(),
ProductId = estimate.ProductId,
Quantity = decimal.ToInt32(Math.Ceiling(totalInstallTime))
};

if (order.OnsiteInstallation)
{
var checkOnsiteExists = cart.Products.FirstOrDefault(x => x.ProductId == Guid.Parse("b174c76a-e067-4a6a-abcf-53b6d3a848e4"));
var checkEstimateExists = cart.Products.FirstOrDefault(x => x.ProductId == Guid.Parse("a032b3ba-da57-4ad3-90ec-c59a3505b075"));

if (checkOnsiteExists is null && checkEstimateExists is null)
{
_ = cart.AddProduct(onsite, productOrderOnsite);
_ = cart.AddProduct(estimate, productOrderEstimate);
}
}
else
{
// Remove
_ = cart.RemoveProduct(onsite, productOrderOnsite);
_ = cart.RemoveProduct(estimate, productOrderEstimate);
}
}

foreach (var productOrder in cart.ProductOrders)
{
productOrder.OrderId = order.OrderId;
Expand Down Expand Up @@ -536,7 +624,7 @@ public async Task<IActionResult> SubmitAsync(Order order)
}
else if (coupon.Type == "Install")
{
// If they have selected onsite installation this coupon removes a $60 charge.
// If they have selected onsite installation this coupon removes a $75 charge.
if (order.OnsiteInstallation)
{
onetimeItems.Add(new Line_Items
Expand Down Expand Up @@ -629,17 +717,7 @@ public async Task<IActionResult> SubmitAsync(Order order)
// Handle hardware installation scenarios, if hardware is in the order.
if (cart?.Products is not null && cart.Products.Any())
{
if (order.OnsiteInstallation)
{
onetimeItems.Add(new Line_Items
{
product_key = "Onsite Hardware Installation",
notes = $"We'll come visit you and get all your phones setup.",
cost = 75,
quantity = 1
});
}
else
if (!order.OnsiteInstallation)
{
onetimeItems.Add(new Line_Items
{
Expand Down Expand Up @@ -677,7 +755,7 @@ public async Task<IActionResult> SubmitAsync(Order order)
}
}

// Handle the tax information for the invoice and fall back to simplier queries if we get failures.
// Handle the tax information for the invoice and fall back to simpler queries if we get failures.
SalesTax specificTaxRate = null!;
if (order.State is "WA" || order.State is "Washington")
{
Expand Down
3 changes: 3 additions & 0 deletions NumberSearch.Mvc/NumberSearch.Mvc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@
<Content Update="wwwroot\images\scaled\MukilteoChamber-320.webp">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="wwwroot\images\scaled\onsiteInstall-400.webp">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="wwwroot\images\scaled\qacc-logo.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Expand Down
4 changes: 2 additions & 2 deletions NumberSearch.Mvc/Views/Cart/Order.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@
</div>
<div class="col-md-8 mb-3">
<p>Remote installation is free, safe, and done virtually. We'll guide you through every step of plugging in your pre-configured phones!</p>
<p>Onsite installation is available in the Greater Seattle Area for a $75 call out charge. Time spent onsite is billed at $125 per hour.</p>
<p>Onsite installation is available in the Greater Seattle Area for a $75 call out charge. Time spent onsite is billed at $125 per hour and estimated based on the hardware you order.</p>
</div>
</div>
}
Expand All @@ -709,7 +709,7 @@
<div class="form-group">
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-outline-secondary">
<input asp-for="Order!.OnsiteInstallation" type="radio" id="ltoption1" value="false" checked> Remote
<input asp-for="Order!.OnsiteInstallation" type="radio" id="ltoption1" value="false"> Remote
</label>
<label class="btn btn-outline-secondary">
<input asp-for="Order!.OnsiteInstallation" type="radio" id="ltoption2" value="true"> Onsite
Expand Down
Binary file not shown.
13 changes: 13 additions & 0 deletions NumberSearch.Tests/Integration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,19 @@ public async Task GetAllBillingClientsByEmailAsync()
output.WriteLine(JsonSerializer.Serialize(result));
}

//[Fact]
//public async Task SetAllHardwareInstallTimesTo15Minutes()
//{
// // Act
// var result = await Product.GetAllAsync(postgresql);

// foreach (var item in result)
// {
// item.InstallTime = 0.25m;
// var checkUpdate = await item.PutAsync(postgresql);
// Assert.True(checkUpdate);
// }
//}

//[Fact]
//public async Task CreateAndSendAnInvoiceAsync()
Expand Down

0 comments on commit 55b1726

Please sign in to comment.