Beispiel #1
0
        public static int AreEquals(this List <SupplierReportItem> items, SupplierReportItem item)
        {
            if (items.Any())
            {
                var j = 0;
                foreach (var i in items)
                {
                    if (i.Equals(item))
                    {
                        return(j);
                    }

                    j++;
                }
            }

            return(-1);
        }
Beispiel #2
0
        public async Task <IActionResult> ReportOrdersFromSupplier([FromRoute] Guid supplierId)
        {
            try
            {
                var supplier = await _uow.Repository <Supplier>().GetAll()
                               .FirstOrDefaultAsync(s => s.SupplierId == supplierId);

                if (supplier == null)
                {
                    return(Ok(new { info = "There is no such supplier." }));
                }

                var productsSupplier = _uow.Repository <Order>().GetAll()
                                       .Join(_uow.Repository <OrderedItem>().GetAll(), order => order.OrderId,
                                             item => item.OrderId,
                                             (order, item) => new
                {
                    OrderId        = order.OrderId,
                    OrderStartDate = order.OrderStartDate,
                    OrderStopDate  = order.OrderStopDate,
                    OrderedItemId  = item.OrderedItemId,
                    ProductId      = item.ProductId,
                    Quantity       = item.Quantity
                })
                                       .Join(_uow.Repository <Product>().GetAll(),
                                             orderItem => orderItem.ProductId,
                                             product => product.ProductId,
                                             (orderItem, product) => new
                {
                    OrderId        = orderItem.OrderId,
                    OrderStartDate = orderItem.OrderStartDate,
                    OrderStopDate  = orderItem.OrderStopDate,
                    OrderedItemId  = orderItem.OrderedItemId,
                    ProductId      = orderItem.ProductId,
                    Quantity       = orderItem.Quantity,
                    ProductName    = product.ProductName,
                    Price          = product.Price,
                    UnitName       = product.Unit.UnitName,
                    SupplierId     = product.SupplierId
                })
                                       .Where(x => x.SupplierId == supplierId);

                if (ControllerContext.ActionDescriptor.AttributeRouteInfo?.Name == "LastOrderGrande")
                {
                    var lastOrderGrandeId = _uow.Repository <Order>().GetAll()
                                            .OrderByDescending(x => x.OrderStopDate)
                                            .FirstOrDefault()?.OrderId;
                    productsSupplier = productsSupplier.Where(x => x.OrderId == lastOrderGrandeId);
                }

                if (!productsSupplier.Any())
                {
                    return(Ok(new { info = "The supplier did not sell any products." }));
                }

                var groupOrder = productsSupplier
                                 .AsEnumerable()
                                 .GroupBy(x => x.OrderId);

                var supplierReport = new SupplierReport
                {
                    SupplierId   = supplier.SupplierId,
                    SupplierName = supplier.SupplierName,
                    SupplierAbbr = supplier.SupplierAbbr,
                    Email        = supplier.Email
                };

                foreach (var group in groupOrder)
                {
                    var groupKey      = group.Key;
                    var supplierOrder = new SupplierReportOrder
                    {
                        OrderId = groupKey,
                    };

                    foreach (var groupItem in group)
                    {
                        if (supplierOrder.OrderId == groupItem.OrderId)
                        {
                            supplierOrder.OrderStartDate = groupItem.OrderStartDate;
                            supplierOrder.OrderStopDate  = groupItem.OrderStopDate;
                            supplierOrder.SupplierReport = supplierReport;
                        }

                        var supplierItem = new SupplierReportItem
                        {
                            ProductId           = groupItem.ProductId,
                            ProductName         = groupItem.ProductName,
                            Price               = groupItem.Price,
                            Quantity            = groupItem.Quantity,
                            UnitName            = groupItem.UnitName,
                            SupplierReportOrder = supplierOrder,
                            TotalPrice          = Math.Round((decimal)groupItem.Price * groupItem.Quantity, 2,
                                                             MidpointRounding.AwayFromZero)
                        };

                        supplierOrder.OrderTotalPrice += supplierItem.TotalPrice;

                        if (supplierOrder.SupplierReportItems.AreEquals(supplierItem) is var index && index > -1)
                        {
                            supplierOrder.SupplierReportItems[index].Quantity   += groupItem.Quantity;
                            supplierOrder.SupplierReportItems[index].TotalPrice += supplierItem.TotalPrice;
                        }
                        else
                        {
                            supplierOrder.SupplierReportItems.Add(supplierItem);
                        }
                    }