Example #1
0
        public ActionResult UpdateOrder(int orderId)
        {
            var salesDataService = new SalesDataService();
            var order            = salesDataService.GetSalesById(orderId);

            foreach (var orderDetail in order.SalesDetails)
            {
                var(description, englishDescription)  = GetProductDescriptionByProductCode(orderDetail.ProductCode);
                orderDetail.ProductDescription        = description;
                orderDetail.ProductEnglishDescription = englishDescription;
            }
            FillViewBag();

            if (order.InvoiceDate.HasValue == false)
            {
                order.InvoiceDate = DateTime.MaxValue;
            }

            var uploadedFilesPath = Path.Combine(Server.MapPath("~"), "UploadedFiles");
            var orderViewModel    = new OrderViewModel(order);
            var fileName          = Directory.GetFiles(uploadedFilesPath, $"{orderId}_*").FirstOrDefault();

            orderViewModel.AttachedDocumentFileName = Path.GetFileName(fileName);



            return(PartialView(orderViewModel));
        }
Example #2
0
        public ActionResult PackOrder(int orderId)
        {
            var salesDataService = new SalesDataService();
            var order            = salesDataService.GetSalesById(orderId);

            ViewBag.Order = new OrderViewModel(order);

            return(PartialView());
        }
Example #3
0
        public ActionResult EditOrderPacking(int orderId)
        {
            var salesDataService   = new SalesDataService();
            var packingDataService = new PackingDataService();

            var order          = salesDataService.GetSalesById(orderId);
            var packedProducts = packingDataService.GetPackedProductsByOrderId(orderId);

            var packing = new PackingViewModel
            {
                OrderId = orderId,
                Pallets = new List <PackingViewModel.PackingPallet>()
            };

            foreach (var packedProduct in packedProducts)
            {
                var packedPrdct = new PackingViewModel.PackingPallet.PackedProduct
                {
                    ProductCode = packedProduct.ProductCode,
                    Quantity    = packedProduct.Quantity
                };

                var palletOfProduct = packing.Pallets.FirstOrDefault(x => x.PalletId == packedProduct.PalletId);

                if (palletOfProduct != null)
                {
                    palletOfProduct.Products.Add(packedPrdct);
                }
                else
                {
                    palletOfProduct = new PackingViewModel.PackingPallet
                    {
                        PalletId = packedProduct.PalletId,
                        Products = new List <PackingViewModel.PackingPallet.PackedProduct>()
                    };
                    palletOfProduct.Products.Add(packedPrdct);
                    packing.Pallets.Add(palletOfProduct);
                }
            }

            ViewBag.Order   = new OrderViewModel(order);
            ViewBag.Packing = packing;

            return(PartialView());
        }
Example #4
0
        public ActionResult UpdateOrder(OrderViewModel order)
        {
            var salesDataService = new SalesDataService();
            var previousComment  = salesDataService.GetSalesById(order.OrderId).Comment;
            int differentIndex   = FindTheIndexOfFirstDifferentCharacter(previousComment, order.Comment);
            var sales            = new Sales
            {
                Id               = order.OrderId,
                Customer         = new CustomerDataService().GetCustomerById(order.Customer.Id),
                Exporter         = order.Exporter,
                InvoiceNumber    = order.InvoiceNumber,
                SalesState       = order.State,
                InvoiceDate      = order.InvoiceDate,
                Currency         = order.Currency,
                ExchangeRate     = order.ExchangeRate,
                LastModifiedDate = DateTime.Now,
                SalesDetails     = order.OrderLines?.Select(x => new SalesDetail()
                {
                    ProductCode               = x.ProductCode,
                    Quantity                  = x.Quantity,
                    ProductDescription        = x.ProductDescription,
                    ProductEnglishDescription = x.ProductEnglishDescription,
                    SalesId   = order.OrderId,
                    UnitPrice = x.UnitPrice
                })?.ToList() ?? new List <SalesDetail>(),
                TotalPrice    = order.OrderLines?.Sum(x => x.TotalPrice) ?? 0,
                Comment       = order.Comment.Insert(differentIndex, $"\n{User.Identity.Name}({DateTime.Now}):"),
                DeliveryType  = order.DeliveryType,
                PaymentType   = order.PaymentType,
                TransportCost = order.TransportCost,
            };

            var existingQuantitiesbyProductCode = new Dictionary <string, int>();
            var orderDetails = salesDataService.GetSalesById(sales.Id).SalesDetails;

            foreach (var detail in orderDetails)
            {
                existingQuantitiesbyProductCode[detail.ProductCode] = detail.Quantity;
            }

            salesDataService.UpdateOrder(sales);



            var message = "Satış güncelleme başarıyla yapıldı.";

            //daha önceden eklenmiş ürünlerde önceki miktar + stok toplamına bakılır
            //yeni eklenen ürünlerde direk eklenmek istenen miktar ile stok karşılaştırılır
            foreach (var orderLine in order.OrderLines.Where(x => (x.IsAlreadyAddedProduct && x.Quantity > existingQuantitiesbyProductCode[x.ProductCode] + x.StokQuantity) ||
                                                             (x.IsAlreadyAddedProduct == false && x.Quantity > x.StokQuantity)))
            {
                var gap = 0;
                //daha önce eklenen bir ürün ise daha önceki sipariş miktarı ve stok beraber ele alınmalı
                if (orderLine.IsAlreadyAddedProduct)
                {
                    //stok negatif olmadıysa stok adedi kullanılabilir
                    if (orderLine.StokQuantity >= 0)
                    {
                        gap = orderLine.Quantity - (orderLine.StokQuantity +
                                                    existingQuantitiesbyProductCode[orderLine.ProductCode]);
                    }
                    //stok negatif olduysa daha önce satın alınma talebi yapıldığı için şuanki sipariş mikarı ile bir öncekini karşılaştırmak satın alma talebi için yeterli
                    else
                    {
                        gap = orderLine.Quantity - existingQuantitiesbyProductCode[orderLine.ProductCode];
                    }
                }
                else
                {
                    gap = orderLine.Quantity - orderLine.StokQuantity;
                }


                //yeni bir ürün eklendiğinde diğer ürünler için gereksiz satın alma talebi yaratılmasını engellemek için if kontrolü yapıldı.
                if (existingQuantitiesbyProductCode.ContainsKey(orderLine.ProductCode) &&
                    existingQuantitiesbyProductCode[orderLine.ProductCode] != orderLine.Quantity)
                {
                    var purchase = new Purchase
                    {
                        OrderId           = order.OrderId,
                        ProductCode       = orderLine.ProductCode,
                        ProductId         = new ProductDataService().GetProductByCode(orderLine.ProductCode).Id,
                        PurchaseStartDate = DateTime.Now,
                        PurchaseState     = PurchaseState.PurchaseRequested,
                        RequestedBySales  = true,
                        SalesUserName     = User.Identity.Name,
                        Quantity          = gap
                    };
                    new PurchaseDataService().CreatePurchase(purchase);
                    message +=
                        $"<br> <b>{orderLine.ProductCode}</b> kodlu ürün yeteri kadar stokta bulunmadığı için <b>{gap}</b> adet satın alma talebi yapıldı.";
                }
            }


            var uploadedFilesPath = Path.Combine(Server.MapPath("~"), "UploadedFiles");

            foreach (var file in Directory.GetFiles(uploadedFilesPath, $"{order.OrderId}_*"))
            {
                System.IO.File.Delete(file);
            }

            if (order.Document != null)
            {
                order.Document.SaveAs(Path.Combine(uploadedFilesPath, $"{order.OrderId}_{order.Document.FileName}"));
            }
            return(Json(new AjaxResult(message)));
        }