public void SaveDeliveryStock(DeliveryStockDTO deliveryStockDTO)
        {
            var deliveryStock = new DeliveryStock()
            {
                StockId    = deliveryStockDTO.StockId,
                DeliveryId = deliveryStockDTO.DeliveryId,
                CreatedOn  = DateTime.Now,
                TimeStamp  = DateTime.Now
            };

            this.UnitOfWork.Get <DeliveryStock>().AddNew(deliveryStock);
            this.UnitOfWork.SaveChanges();
        }
Example #2
0
        public long SaveDelivery(Delivery delivery, string userId)
        {
            long         deliveryId   = 0;
            MakeDelivery makeDelivery = new MakeDelivery();



            if (delivery.OrderId != 0)
            {
                //var order = _orderService.GetOrder(delivery.OrderId);
                if (delivery.ProductId == 2)
                {
                    var deliveryDTO = new DTO.DeliveryDTO()
                    {
                        StoreId              = delivery.StoreId,
                        CustomerId           = delivery.CustomerId,
                        DeliveryCost         = delivery.DeliveryCost,
                        OrderId              = delivery.OrderId,
                        VehicleNumber        = delivery.VehicleNumber,
                        BranchId             = delivery.BranchId,
                        SectorId             = delivery.SectorId,
                        PaymentModeId        = delivery.PaymentModeId,
                        BatchId              = delivery.BatchId,
                        Price                = delivery.Price,
                        Quantity             = delivery.Quantity,
                        ProductId            = delivery.ProductId,
                        Amount               = delivery.Amount,
                        Location             = delivery.Location,
                        TransactionSubTypeId = delivery.TransactionSubTypeId,
                        MediaId              = delivery.MediaId,
                        DeliveryId           = delivery.DeliveryId,
                        DriverName           = delivery.DriverName,
                        DriverNIN            = delivery.DriverNIN,
                        Deleted              = delivery.Deleted,
                        CreatedBy            = delivery.CreatedBy,
                        CreatedOn            = delivery.CreatedOn,
                    };
                    makeDelivery = MakeBrandDeliveryRecord(delivery.StoreId, delivery, userId);
                    if (makeDelivery.StockReduced && (makeDelivery.OrderQuantityBalance == 0 || makeDelivery.OrderQuantityBalance > 0))
                    {
                        deliveryId = this._dataService.SaveDelivery(deliveryDTO, userId);


                        //StringBuilder sb = new StringBuilder();
                        //string strNewPath = System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["MbaleReceipt"]);
                        //using (StreamReader sr = new StreamReader(strNewPath))
                        //{
                        //    while (!sr.EndOfStream)
                        //    {
                        //        sb.Append(sr.ReadLine());
                        //    }
                        //}

                        //string body = sb.ToString().Replace("#CUSTOMERNAME#", deliveryDTO.CustomerId);
                        //body = body.Replace("#AMOUNT#", deliveryDTO.Amount);


                        //try
                        //{

                        //}
                        //catch (Exception ex)
                        //{

                        //    logger.Debug("receipt Not generated: " + ex.Message);
                        //}


                        var deliveryStockDTO = new DeliveryStockDTO()
                        {
                            StockId    = makeDelivery.StockId,
                            DeliveryId = deliveryId,
                        };
                        _dataService.SaveDeliveryStock(deliveryStockDTO);
                        _orderService.UpdateOrderWithCompletedStatus(delivery.OrderId, orderStatusIdComplete, userId);
                    }
                    else if (makeDelivery.StockReduced && makeDelivery.OrderQuantityBalance < 0)
                    {
                        deliveryId = this._dataService.SaveDelivery(deliveryDTO, userId);
                        if (makeDelivery.OrderQuantityBalance != 0)
                        {
                            var deliveryStockDTO = new DeliveryStockDTO()
                            {
                                StockId    = makeDelivery.StockId,
                                DeliveryId = deliveryId,
                            };
                            _dataService.SaveDeliveryStock(deliveryStockDTO);
                            _orderService.UpdateOrderWithInProgressStatus(delivery.OrderId, orderStatusIdInProgress, userId);
                        }
                    }
                }
                else
                {
                    //if order.ProductId == 1
                    var deliveryDTO = new DTO.DeliveryDTO()
                    {
                        StoreId              = delivery.StoreId,
                        CustomerId           = delivery.CustomerId,
                        DeliveryCost         = delivery.DeliveryCost,
                        OrderId              = delivery.OrderId,
                        VehicleNumber        = delivery.VehicleNumber,
                        BranchId             = delivery.BranchId,
                        SectorId             = delivery.SectorId,
                        PaymentModeId        = delivery.PaymentModeId,
                        BatchId              = delivery.BatchId,
                        Price                = delivery.Price,
                        ProductId            = delivery.ProductId,
                        Amount               = delivery.Amount,
                        Quantity             = delivery.Quantity,
                        Location             = delivery.Location,
                        TransactionSubTypeId = delivery.TransactionSubTypeId,
                        MediaId              = delivery.MediaId,
                        DeliveryId           = delivery.DeliveryId,
                        DriverName           = delivery.DriverName,
                        DriverNIN            = delivery.DriverNIN,
                        Deleted              = delivery.Deleted,
                        CreatedBy            = delivery.CreatedBy,
                        CreatedOn            = delivery.CreatedOn,
                    };
                    makeDelivery = MakeFlourDeliveryRecord(delivery.StoreId, delivery, userId);
                    if (makeDelivery.StockReduced && makeDelivery.OrderQuantityBalance == 0)
                    {
                        deliveryId = this._dataService.SaveDelivery(deliveryDTO, userId);
                        var deliveryStockDTO = new DeliveryStockDTO()
                        {
                            StockId    = makeDelivery.StockId,
                            DeliveryId = deliveryId,
                        };
                        _dataService.SaveDeliveryStock(deliveryStockDTO);
                        _orderService.UpdateOrderWithCompletedStatus(delivery.OrderId, orderStatusIdComplete, userId);
                        List <DeliveryGradeSize> deliveryGradeSizeList = new List <DeliveryGradeSize>();

                        foreach (var grade in delivery.Grades)
                        {
                            long   sizeId = 0;
                            double amount = 0, price = 0, quantity = 0;

                            foreach (var denomination in grade.Denominations)
                            {
                                sizeId   = denomination.DenominationId;
                                price    = denomination.Price;
                                quantity = denomination.Quantity;
                                amount   = (denomination.Quantity * denomination.Price);

                                var deliveryGradeSize = new DeliveryGradeSize()
                                {
                                    DeliveryId = deliveryId,
                                    GradeId    = grade.GradeId,
                                    SizeId     = sizeId,
                                    Quantity   = quantity,
                                    Price      = price,
                                    Amount     = amount,
                                    TimeStamp  = DateTime.Now,
                                };
                                deliveryGradeSizeList.Add(deliveryGradeSize);
                            }
                        }
                        this._dataService.PurgeDeliveryGradeSize(deliveryId);
                        this.SaveDeliveryGradeSizeList(deliveryGradeSizeList);
                    }
                    else
                    {
                        deliveryId = this._dataService.SaveDelivery(deliveryDTO, userId);
                        if (makeDelivery.OrderQuantityBalance != 0)
                        {
                            var deliveryStockDTO = new DeliveryStockDTO()
                            {
                                StockId    = makeDelivery.StockId,
                                DeliveryId = deliveryId,
                            };
                            _dataService.SaveDeliveryStock(deliveryStockDTO);
                            _orderService.UpdateOrderWithInProgressStatus(delivery.OrderId, orderStatusIdInProgress, userId);

                            List <DeliveryGradeSize> deliveryGradeSizeList = new List <DeliveryGradeSize>();

                            foreach (var grade in delivery.Grades)
                            {
                                long   sizeId = 0;
                                double amount = 0, price = 0, quantity = 0;

                                foreach (var denomination in grade.Denominations)
                                {
                                    sizeId   = denomination.DenominationId;
                                    price    = denomination.Price;
                                    quantity = denomination.Quantity;
                                    amount   = (denomination.Quantity * denomination.Price);

                                    var deliveryGradeSize = new DeliveryGradeSize()
                                    {
                                        DeliveryId = deliveryId,
                                        GradeId    = grade.GradeId,
                                        SizeId     = sizeId,
                                        Quantity   = quantity,
                                        Price      = price,
                                        Amount     = amount,
                                        TimeStamp  = DateTime.Now,
                                    };
                                    deliveryGradeSizeList.Add(deliveryGradeSize);
                                }
                            }
                            this._dataService.PurgeDeliveryGradeSize(deliveryId);
                            this.SaveDeliveryGradeSizeList(deliveryGradeSizeList);
                        }
                    }

                    long transactionSubTypeId = 0;
                    var  notes = string.Empty;
                    if (delivery.ProductId == 1)
                    {
                        transactionSubTypeId = flourTransactionSubTypeId;
                        notes = "Maize Flour Sale";
                    }
                    else if (delivery.ProductId == 2)
                    {
                        transactionSubTypeId = branTransactionSubTypeId;
                        notes = "Bran Sale";
                    }
                    var paymentMode     = _accountTransactionActivityService.GetPaymentMode(delivery.PaymentModeId);
                    var paymentModeName = paymentMode.Name;
                    if (paymentModeName == "Credit")
                    {
                        var accountActivity = new AccountTransactionActivity()
                        {
                            AspNetUserId         = delivery.CustomerId,
                            Amount               = delivery.Amount,
                            Notes                = notes,
                            Action               = "-",
                            BranchId             = delivery.BranchId,
                            TransactionSubTypeId = transactionSubTypeId,
                            SectorId             = delivery.SectorId,
                            Deleted              = delivery.Deleted,
                            CreatedBy            = userId,
                            PaymentMode          = paymentModeName,
                        };
                        var accountActivityId = this._accountTransactionActivityService.SaveAccountTransactionActivity(accountActivity, userId);
                    }


                    if (delivery.Amount == 0)
                    {
                        return(deliveryId);
                    }
                    else
                    {
                        long transactionTypeId  = 0;
                        var  transactionSubtype = _transactionSubTypeService.GetTransactionSubType(delivery.TransactionSubTypeId);
                        if (transactionSubtype != null)
                        {
                            transactionTypeId = transactionSubtype.TransactionTypeId;
                        }

                        var transaction = new TransactionDTO()
                        {
                            BranchId             = delivery.BranchId,
                            SectorId             = delivery.SectorId,
                            Amount               = delivery.DeliveryCost,
                            TransactionSubTypeId = delivery.TransactionSubTypeId,
                            TransactionTypeId    = transactionTypeId,
                            CreatedOn            = DateTime.Now,
                            TimeStamp            = DateTime.Now,
                            CreatedBy            = userId,
                            Deleted              = false,
                        };
                        var transactionId = _transactionDataService.SaveTransaction(transaction, userId);
                        return(deliveryId);
                    }
                }
            }
            return(deliveryId);
        }