public async Task <List <OrderDetailData> > PurchaseOrderDetail(string templateID, ListArgs <ReportAdHocFilters> args, DecodedToken decodedToken) { IList <ListFilter> filters = await BuildFilters(templateID, args, decodedToken, "DateSubmitted", "xp.SupplierIDs", "ShippingAddressID"); CosmosListOptions listOptions = new CosmosListOptions() { PageSize = -1, Sort = "OrderID", SortDirection = SortDirection.ASC, Filters = filters, }; IQueryable <OrderDetailData> queryable = _purchaseOrderDetail.GetQueryable() .Where(order => order.PartitionKey == "PartitionValue"); QueryRequestOptions requestOptions = new QueryRequestOptions { MaxItemCount = listOptions.PageSize, MaxConcurrency = -1 }; CosmosListPage <OrderDetailData> purchaseOrderDataResponse = await _purchaseOrderDetail.GetItemsAsync(queryable, requestOptions, listOptions); List <OrderDetailData> purchaseOrderData = purchaseOrderDataResponse.Items; listOptions.ContinuationToken = purchaseOrderDataResponse.Meta.ContinuationToken; while (listOptions.ContinuationToken != null) { CosmosListPage <OrderDetailData> responseWithToken = await _purchaseOrderDetail.GetItemsAsync(queryable, requestOptions, listOptions); purchaseOrderData.AddRange(responseWithToken.Items); listOptions.ContinuationToken = responseWithToken.Meta.ContinuationToken; } var purchaseOrders = new List <OrderDetailData>(); foreach (OrderDetailData purchaseOrder in purchaseOrderData) { purchaseOrder.ShipFromAddressID = purchaseOrder.Data.xp.SelectedShipMethodsSupplierView != null ? purchaseOrder.Data.xp.SelectedShipMethodsSupplierView[0].ShipFromAddressID : null; purchaseOrder.ShipMethod = purchaseOrder.Data.xp.SelectedShipMethodsSupplierView != null ? purchaseOrder.Data.xp.SelectedShipMethodsSupplierView[0].Name : null; purchaseOrders.Add(purchaseOrder); } return(purchaseOrders); }
private async Task UpsertPurchaseOrderDetail(string orderID) { var orders = await _oc.Orders.ListAllAsync <HSOrder>(OrderDirection.Outgoing, filters : $"ID={orderID}-*"); var queryable = _purchaseOrderDetailDataRepo.GetQueryable().Where(order => order.PartitionKey == "PartitionValue"); var requestOptions = BuildQueryRequestOptions(); var salesOrderWorksheet = await _oc.IntegrationEvents.GetWorksheetAsync <HSOrderWorksheet>(OrderDirection.Incoming, orderID); var promos = await _oc.Orders.ListPromotionsAsync(OrderDirection.Incoming, salesOrderWorksheet.Order.ID); var discountedLineItems = new List <HSLineItem>(); if (promos.Items.Count() > 0) { var discountedLineFilter = new Dictionary <string, object> { ["PromotionDiscount"] = ">0" }; discountedLineItems = await _oc.LineItems.ListAllAsync <HSLineItem>(OrderDirection.Incoming, salesOrderWorksheet.Order.ID, filters : discountedLineFilter); } foreach (var order in orders) { order.FromUser = salesOrderWorksheet.Order.FromUser; order.BillingAddress = new HSAddressBuyer() { xp = new BuyerAddressXP() { LocationID = salesOrderWorksheet?.Order?.BillingAddress?.xp?.LocationID } }; var brand = await _oc.Buyers.GetAsync <HSBuyer>(salesOrderWorksheet.Order.FromCompanyID); var supplier = await _oc.Suppliers.GetAsync <HSSupplier>(order.ToCompanyID); order.ShippingCost = GetPurchaseOrderShippingCost(salesOrderWorksheet, order.ToCompanyID); if (salesOrderWorksheet.Order.PromotionDiscount > 0) { order.PromotionDiscount = GetPurchaseOrderPromotionDiscount(salesOrderWorksheet, order.ToCompanyID); } var cosmosPurchaseOrder = new OrderDetailData() { PartitionKey = "PartitionValue", OrderID = order.ID, Data = order, SupplierName = supplier.Name, BrandName = brand.Name, }; if (promos.Items.Count > 0) { cosmosPurchaseOrder.Promos = ReportPromoBuilder.BuildPromoFields(promos, ReportTypeEnum.PurchaseOrderDetail, order.ToCompanyID, discountedLineItems); } var listOptions = BuildListOptions(order.ID); CosmosListPage <OrderDetailData> currentOrderListPage = await _purchaseOrderDetailDataRepo.GetItemsAsync(queryable, requestOptions, listOptions); var cosmosID = ""; if (currentOrderListPage.Items.Count() == 1) { cosmosID = cosmosPurchaseOrder.id = currentOrderListPage.Items[0].id; } await _purchaseOrderDetailDataRepo.UpsertItemAsync(cosmosID, cosmosPurchaseOrder); } }