Exemplo n.º 1
0
        public ActionResult GetFilteredSales(IEnumerable <int> customerIds, IEnumerable <SalesState> states,
                                             IEnumerable <Currency> currencies, string invoiceNo)
        {
            var salesByPerson = new SalesDataService().GetSalesBySalesPerson(User.Identity.GetUserId());

            var filteredSales = salesByPerson;

            if (customerIds != null)
            {
                filteredSales = salesByPerson.Where(x => customerIds.Contains(x.Customer.Id)).ToList();
            }
            if (states != null)
            {
                filteredSales = filteredSales.Where(x => states.Contains(x.SalesState)).ToList();
            }
            if (currencies != null)
            {
                filteredSales = filteredSales.Where(x => currencies.Contains(x.Currency)).ToList();
            }
            if (string.IsNullOrEmpty(invoiceNo) == false)
            {
                filteredSales = filteredSales.Where(x => invoiceNo == x.InvoiceNumber).ToList();
            }
            return(new JsonResult()
            {
                Data = filteredSales.Select(x => new OrderViewModel(x)),
                ContentType = "application/json",
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                MaxJsonLength = Int32.MaxValue
            });
        }
Exemplo n.º 2
0
        public ActionResult GetCustomerStatistics(DateTime startDate, DateTime endDate)
        {
            var customerStatistics = new List <CustomerHitListViewModel>();

            var orderDataService = new SalesDataService();
            var sales            = orderDataService.GetSales(startDate, endDate);

            var customerSales = sales.GroupBy(x => x.Customer.Name).ToList();

            foreach (var customerSale in customerSales)
            {
                var customerStatistic = new CustomerHitListViewModel();
                customerStatistic.CustomerName = customerSale.Key;
                var completedOrders = customerSale.Where(x => x.SalesState == DataAccess.Model.SalesState.InvoiceDoneAndPacked).ToList();
                customerStatistic.TotalCompletedOrderNumber       = completedOrders.Count();
                customerStatistic.TotalCompletedOrderIncomeTL     = completedOrders.Where(x => x.Currency == DataAccess.Model.Currency.Tl).Sum(y => y.TotalPrice);
                customerStatistic.TotalCompletedOrderIncomeEuro   = completedOrders.Where(x => x.Currency == DataAccess.Model.Currency.Eur).Sum(y => y.TotalPrice);
                customerStatistic.TotalCompletedOrderIncomeDollar = completedOrders.Where(x => x.Currency == DataAccess.Model.Currency.Usd).Sum(y => y.TotalPrice);

                var ongoingOrders = customerSale.Where(x => x.SalesState != DataAccess.Model.SalesState.InvoiceDoneAndPacked && x.SalesState != DataAccess.Model.SalesState.Rejected).ToList();
                customerStatistic.TotalOngoingOrderNumber       = ongoingOrders.Count();
                customerStatistic.TotalOngoingOrderIncomeTL     = ongoingOrders.Where(x => x.Currency == DataAccess.Model.Currency.Tl).Sum(y => y.TotalPrice);
                customerStatistic.TotalOngoingOrderIncomeEuro   = ongoingOrders.Where(x => x.Currency == DataAccess.Model.Currency.Eur).Sum(y => y.TotalPrice);
                customerStatistic.TotalOngoingOrderIncomeDollar = ongoingOrders.Where(x => x.Currency == DataAccess.Model.Currency.Usd).Sum(y => y.TotalPrice);

                customerStatistics.Add(customerStatistic);
            }


            return(new JsonResult()
            {
                Data = customerStatistics.OrderByDescending(x => (x.TotalCompletedOrderIncomeTL, x.TotalOngoingOrderIncomeTL)).ToList(),
                ContentType = "application/json",
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            });
Exemplo n.º 3
0
        public ActionResult GetStock(string productCode)
        {
            var orkaDataService = new StockDataService();
            var stocks          = orkaDataService.GetStockByCodeWithWildCardFromOrka(productCode).Select(x => new StockViewModel {
                Code = x.Code, Price = x.Price, StockAmount = x.StockAmount
            }).ToList();

            var productIds = new ProductDataService().GetProductByCodeWithWildCard(productCode).Select(x => x.Id).ToList();

            foreach (var productId in productIds)
            {
                var code           = new ProductDataService().GetProductById(productId).Code;
                var reservedAmount = new SalesDataService().GetReservedCountyProductId(productId);
                var stock          = stocks.FirstOrDefault(x => x.Code == code);
                if (stock != null)
                {
                    stock.ReservedAmount = reservedAmount;
                    stock.StockAmount   -= reservedAmount;
                }
            }


            return(new JsonResult()
            {
                Data = stocks,
                ContentType = "application/json",
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            });
        }
Exemplo n.º 4
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));
        }
Exemplo n.º 5
0
        public ActionResult PackOrder(int orderId)
        {
            var salesDataService = new SalesDataService();
            var order            = salesDataService.GetSalesById(orderId);

            ViewBag.Order = new OrderViewModel(order);

            return(PartialView());
        }
Exemplo n.º 6
0
        public int GetStockInformationByProductCode(string productCode)
        {
            var stock         = new StockDataService().GetStockByCodeFromOrka(productCode);
            var productId     = new ProductDataService().GetProductByCode(productCode).Id;
            var reservedStock = new SalesDataService().GetReservedCountyProductId(productId);

            var usableStock = stock.StockAmount - reservedStock;

            return(usableStock);
        }
Exemplo n.º 7
0
        public ActionResult DeleteOrder(int orderId)
        {
            var result = new SalesDataService().DeleteSales(orderId);

            return(new JsonResult()
            {
                Data = result,
                ContentType = "application/json",
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            });
        }
Exemplo n.º 8
0
        public ActionResult GetAllActiveOrders()
        {
            var allActiveOrders     = new SalesDataService().GetAllSales().Where(x => x.SalesState != SalesState.InvoiceDoneAndPacked);
            var orderedActiveOrders = allActiveOrders.OrderByDescending(x => x.SalesStartDate);
            var orderViewModel      = orderedActiveOrders.Select(x => new OrderViewModel(x));

            return(new JsonResult()
            {
                Data = orderViewModel,
                ContentType = "application/json",
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                MaxJsonLength = Int32.MaxValue
            });
        }
Exemplo n.º 9
0
        public ActionResult GetMyOrders()
        {
            var myOrders        = new SalesDataService().GetSalesBySalesPerson(User.Identity.GetUserId());
            var myOrderedOrders = myOrders.OrderByDescending(x => x.SalesStartDate);
            var orderViewModel  = myOrderedOrders.Select(x => new OrderViewModel(x));

            return(new JsonResult()
            {
                Data = orderViewModel,
                ContentType = "application/json",
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                MaxJsonLength = Int32.MaxValue
            });
        }
Exemplo n.º 10
0
        public ActionResult UpdatePurchase(PurchaseViewModel purchaseViewModel)
        {
            var PurchaseDataService = new PurchaseDataService();
            var purchase            = new Purchase()
            {
                PurchaseState     = purchaseViewModel.PurchaseState,
                SupplierId        = purchaseViewModel.SupplierId,
                OrderId           = purchaseViewModel.OrderId,
                ProductCode       = purchaseViewModel.ProductCode,
                Quantity          = purchaseViewModel.Quantity,
                SalesUserName     = purchaseViewModel.SalesUserName,
                PurchaseStartDate = purchaseViewModel.PurchaseStartDate,
                UnitPrice         = purchaseViewModel.UnitPrice,
                Currency          = purchaseViewModel.Currency,
                ProductId         = purchaseViewModel.ProductId,
                PurchaseId        = purchaseViewModel.PurchaseId,
                PurchaserUserGuid = purchaseViewModel.PurchaserUser,
                TotalPrice        = purchaseViewModel.UnitPrice * purchaseViewModel.Quantity,
                RequestedBySales  = purchaseViewModel.RequestedBySales,
                Comment           = purchaseViewModel.Comment
            };

            if (purchaseViewModel.PurchaseState == PurchaseState.PurchaseSuccesful ||
                purchaseViewModel.PurchaseState == PurchaseState.PurchaseFailed)
            {
                purchase.PurchaseCloseDate = DateTime.Now;
            }

            if (purchaseViewModel.OrderId != 0)
            {
                var salesState = new SalesDataService().GetSalesById(purchaseViewModel.OrderId).SalesState;
                if (salesState == SalesState.PurchaseInProgress)
                {
                    if (purchaseViewModel.PurchaseState == PurchaseState.PurchaseSuccesful)
                    {
                        new SalesDataService().UpdateOrderState(purchaseViewModel.OrderId,
                                                                SalesState.PurchaseSuccesful);
                    }
                    else if (purchaseViewModel.PurchaseState == PurchaseState.PurchaseFailed)
                    {
                        new SalesDataService().UpdateOrderState(purchaseViewModel.OrderId,
                                                                SalesState.PurchaseFailed);
                    }
                }
            }

            var result = PurchaseDataService.UpdatePurchase(purchase);

            return(Json(result ? new AjaxResult(true) : new AjaxResult(false)));
        }
Exemplo n.º 11
0
        public ActionResult GetReadyForPackingOrders()
        {
            var allOrders        = new SalesDataService().GetSalesByState(SalesState.WaitForPacking);
            var orderedOrders    = allOrders.OrderBy(x => x.InvoiceDate);
            var allOrdersForView = orderedOrders.Select(x => new OrderViewModel(x));

            return(new JsonResult()
            {
                Data = allOrdersForView,
                ContentType = "application/json",
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                MaxJsonLength = Int32.MaxValue
            });
        }
Exemplo n.º 12
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());
        }
Exemplo n.º 13
0
        public ActionResult GetPackedOrders()
        {
            var allOrders          = new List <Sales>();
            var packingReadyOrders = new SalesDataService().GetSalesByState(SalesState.PackingIsReady);
            var packingReadyWaitingPaymentOrders = new SalesDataService().GetSalesByState(SalesState.PackingIsReadyAndWaitForPayment);

            allOrders.AddRange(packingReadyOrders);
            allOrders.AddRange(packingReadyWaitingPaymentOrders);

            var orderedOrders    = allOrders.OrderBy(x => x.InvoiceDate);
            var allOrdersForView = orderedOrders.Select(x => new OrderViewModel(x));

            return(new JsonResult()
            {
                Data = allOrdersForView,
                ContentType = "application/json",
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                MaxJsonLength = Int32.MaxValue
            });
        }
Exemplo n.º 14
0
        public IList <StockViewModel> GetAllStocks()
        {
            var orkaDataService = new StockDataService();
            var allStocks       = orkaDataService.GetAllStocksFromOrka().Select(x => new StockViewModel {
                Code = x.Code, Price = x.Price, StockAmount = x.StockAmount
            }).ToList();
            var reservedProductIds = new SalesDataService().GetAllProductsForActiveOrders();

            foreach (var reservedProductId in reservedProductIds.Distinct())
            {
                var productCode    = new ProductDataService().GetProductById(reservedProductId).Code;
                var reservedAmount = new SalesDataService().GetReservedCountyProductId(reservedProductId);
                var stock          = allStocks.FirstOrDefault(x => x.Code == productCode);
                if (stock != null)
                {
                    stock.ReservedAmount = reservedAmount;
                    stock.StockAmount   -= reservedAmount;
                }
            }

            return(allStocks);
        }
Exemplo n.º 15
0
        public ActionResult CreatePacking(PackingViewModel packingDetail)
        {
            var packingDataService = new PackingDataService();

            foreach (var packingDetailPallet in packingDetail.Pallets)
            {
                foreach (var packedProduct in packingDetailPallet.Products)
                {
                    packingDataService.CreatePackedProduct(new PackedProduct
                    {
                        OrderId     = packingDetail.OrderId,
                        PalletId    = packingDetailPallet.PalletId,
                        ProductCode = packedProduct.ProductCode,
                        Quantity    = packedProduct.Quantity
                    });
                }
            }

            var salesDataService = new SalesDataService();

            salesDataService.UpdateOrderState(packingDetail.OrderId, SalesState.PackingIsReady);

            return(Json(new AjaxResult(true)));
        }
Exemplo n.º 16
0
        public ActionResult CreateOrder(OrderViewModel order)
        {
            var    sales        = new Sales();
            var    salesDetails = new List <SalesDetail>();
            double totalPrice   = 0;

            foreach (var orderLine in order.OrderLines)
            {
                totalPrice += orderLine.TotalPrice;
                salesDetails.Add(new SalesDetail {
                    ProductCode = orderLine.ProductCode, Quantity = orderLine.Quantity, UnitPrice = orderLine.UnitPrice
                });
                var productId = new ProductDataService().GetProductByCode(orderLine.ProductCode).Id;
                new ProductDataService().UpdateProductLatestPrice(productId, orderLine.UnitPrice);
            }
            sales.Currency       = order.Currency;
            sales.ExchangeRate   = order.ExchangeRate;
            sales.Customer       = new CustomerDataService().GetCustomerById(order.Customer.Id);
            sales.SalesStartDate = DateTime.Now;
            sales.SalesUserGuid  = User.Identity.GetUserId();
            sales.SalesDetails   = salesDetails;
            sales.TotalPrice     = totalPrice;
            sales.InvoiceNumber  = order.InvoiceNumber;
            sales.SalesState     = order.State;
            sales.InvoiceDate    = order.InvoiceDate;
            sales.Exporter       = order.Exporter;
            sales.Comment        = $"{User.Identity.Name}({DateTime.Now}):{order.Comment}";
            sales.DeliveryType   = order.DeliveryType;
            sales.PaymentType    = order.PaymentType;
            sales.TransportCost  = order.TransportCost;

            var isThereGap = order.OrderLines.Any(x => x.Quantity > x.StokQuantity);

            if (isThereGap)
            {
                sales.SalesState = SalesState.PurchaseRequested;
            }

            var orderId = new SalesDataService().CreateOrder(sales);
            var message = "Satış girişi başarıyla yapıldı.";

            foreach (var orderLine in order.OrderLines)
            {
                if (orderLine.Quantity > orderLine.StokQuantity)
                {
                    var gap      = orderLine.Quantity - orderLine.StokQuantity;
                    var purchase = new Purchase
                    {
                        OrderId           = 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, $"{orderId}_*"))
            {
                System.IO.File.Delete(file);
            }

            if (order.Document != null)
            {
                order.Document.SaveAs(Path.Combine(uploadedFilesPath, $"{orderId}_{order.Document.FileName}"));
            }
            return(Json(new AjaxResult(message)));
        }
Exemplo n.º 17
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)));
        }