Exemplo n.º 1
0
        public CardRegRespObj AddCard(RegCardObj regObj)
        {
            var response = new CardRegRespObj
            {
                Status = new APIResponseStatus
                {
                    IsSuccessful = false,
                    Message      = new APIResponseMessage()
                }
            };

            try
            {
                if (!EntityValidatorHelper.Validate(regObj, out var valResults))
                {
                    var errorDetail = new StringBuilder();
                    if (!valResults.IsNullOrEmpty())
                    {
                        errorDetail.AppendLine("Following error occurred:");
                        valResults.ForEachx(m => errorDetail.AppendLine(m.ErrorMessage));
                    }
                    else
                    {
                        errorDetail.AppendLine("Validation error occurred! Please check all supplied parameters and try again");
                    }
                    response.Status.Message.FriendlyMessage  = errorDetail.ToString();
                    response.Status.Message.TechnicalMessage = errorDetail.ToString();
                    response.Status.IsSuccessful             = false;
                    return(response);
                }

                if (!HelperMethods.IsUserValid(regObj.AdminUserId, regObj.SysPathCode, HelperMethods.getRequesterRoles(), ref response.Status.Message))
                {
                    return(response);
                }

                if (!DataCheck.IsNumeric(regObj.BatchKey))
                {
                    response.Status.Message.FriendlyMessage  = "Error Occurred! Batch Key Invalid";
                    response.Status.Message.TechnicalMessage = "Batch Prefix Number Must be greater than 0";
                    return(response);
                }
                if (!DataCheck.IsNumeric(regObj.StartBatchId))
                {
                    response.Status.Message.FriendlyMessage  = "Error Occurred! Invalid Start Batch Id";
                    response.Status.Message.TechnicalMessage = "Start Batch Id Is not numeric";
                    return(response);
                }
                if (!DataCheck.IsNumeric(regObj.StopBatchId))
                {
                    response.Status.Message.FriendlyMessage  = "Error Occurred! Invalid Stop Batch Id";
                    response.Status.Message.TechnicalMessage = "Stop Batch Id Is not numeric";
                    return(response);
                }

                if ((int.Parse(regObj.StopBatchId) - int.Parse(regObj.StartBatchId)) + 1 != regObj.NumberOfBatches)
                {
                    response.Status.Message.FriendlyMessage  = "Error Occurred! Incorrect StopBatchId/StartBatchId/NumberOfBatches";
                    response.Status.Message.TechnicalMessage = "Incorrect StopBatchId/StartBatchId/NumberOfBatches";
                    return(response);
                }
                if (regObj.QuantityPerBatch < 1)
                {
                    response.Status.Message.FriendlyMessage  = "Error Occurred! Quantity Per Batch Is Required";
                    response.Status.Message.TechnicalMessage = "Error Occurred! Quantity Per Batch must be greater than zero!";
                    return(response);
                }
                //..................Continue here
                #region Batch Id Computation
                //var qtyPerBatchLength = regObj.QuantityPerBatch.
                #endregion

                //store date for Concurrency...
                var nowDateTime = DateMap.CurrentTimeStamp();
                var nowDate     = nowDateTime.Substring(0, nowDateTime.IndexOf(' '));
                var nowTime     = nowDateTime.Substring(nowDateTime.IndexOf('-') + 1);

                var cardItemList = new List <CardItem>();

                for (int i = int.Parse(regObj.StartBatchId); i < int.Parse(regObj.StopBatchId + 1); i++)
                {
                    cardItemList.Add(new CardItem
                    {
                        CardTypeId           = regObj.CardTypeId,
                        BatchId              = i.ToString(),
                        StartBatchNumber     = i.ToString() + "" + "000", //77001 000
                        StopBatchNumber      = i.ToString() + "" + "999", //77001999
                        DefectiveBatchNumber = "",
                        AvailableQuantity    = 0,
                        BatchQuantity        = 1000,
                        DeliveredQuantity    = 0,
                        MissingQuantity      = 0,
                        DefectiveQuantity    = 0,
                        IssuedQuantity       = 0,
                        RegisteredBy         = regObj.AdminUserId,
                        TimeStampRegisered   = nowDateTime,
                        TimeStampDelivered   = "",
                        TimeStampLastIssued  = "",
                        Status = CardStatus.Registered
                    });
                }

                var card = new Card
                {
                    CardTitle          = $"Card Production On {nowDate} At {nowTime}",
                    CardTypeId         = regObj.CardTypeId,
                    BatchKey           = regObj.BatchKey,
                    StartBatchId       = regObj.BatchKey + "000",
                    StopBatchId        = (Int32.Parse(regObj.BatchKey + "000") + (regObj.NumberOfBatches - 1)).ToString(),
                    NumberOfBatches    = regObj.NumberOfBatches,
                    QuantityPerBatch   = 1000,
                    TotalQuantity      = regObj.NumberOfBatches * regObj.QuantityPerBatch,
                    Status             = CardStatus.Registered,
                    TimeStampRegisered = nowDateTime,
                    CardItems          = cardItemList
                };

                var added = _repository.Add(card);
                _uoWork.SaveChanges();
                if (added.CardId < 1)
                {
                    response.Status.Message.FriendlyMessage  = "Error Occurred! Unable to complete your request. Please try again later";
                    response.Status.Message.TechnicalMessage = "Unable to save to database";
                    return(response);
                }

                response.Status.IsSuccessful = true;
                response.CardId = added.CardId;
            }
            catch (DbEntityValidationException ex)
            {
                ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message);
                response.Status.Message.FriendlyMessage  = "Error Occurred! Please try again later";
                response.Status.Message.TechnicalMessage = "Error: " + ex.GetBaseException().Message;
                response.Status.IsSuccessful             = false;
                return(response);
            }
            catch (Exception ex)
            {
                ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message);
                response.Status.Message.FriendlyMessage  = "Error Occurred! Please try again later";
                response.Status.Message.TechnicalMessage = "Error: " + ex.GetBaseException().Message;
                response.Status.IsSuccessful             = false;
                return(response);
            }

            return(response);
        }
Exemplo n.º 2
0
        public CardRegRespObj UpdateCard(EditCardObj regObj)
        {
            var response = new CardRegRespObj
            {
                Status = new APIResponseStatus
                {
                    IsSuccessful = false,
                    Message      = new APIResponseMessage()
                }
            };

            try
            {
                if (!EntityValidatorHelper.Validate(regObj, out var valResults))
                {
                    var errorDetail = new StringBuilder();
                    if (!valResults.IsNullOrEmpty())
                    {
                        errorDetail.AppendLine("Following error occurred:");
                        valResults.ForEachx(m => errorDetail.AppendLine(m.ErrorMessage));
                    }
                    else
                    {
                        errorDetail.AppendLine("Validation error occurred! Please check all supplied parameters and try again");
                    }
                    response.Status.Message.FriendlyMessage  = errorDetail.ToString();
                    response.Status.Message.TechnicalMessage = errorDetail.ToString();
                    response.Status.IsSuccessful             = false;
                    return(response);
                }

                if (!HelperMethods.IsUserValid(regObj.AdminUserId, regObj.SysPathCode, HelperMethods.getRequesterRoles(), ref response.Status.Message))
                {
                    return(response);
                }

                var thisCardItem = GetCardItemInfo(regObj.CardItemId);
                if (thisCardItem == null)
                {
                    response.Status.Message.FriendlyMessage  = "No Card Item Information found for the specified Card Item Id";
                    response.Status.Message.TechnicalMessage = "No Card Item Information found!";
                    return(response);
                }

                var thisCardDelivery = GetCardDeliveryInfo(regObj.CardId);
                if (thisCardDelivery == null)
                {
                    response.Status.Message.FriendlyMessage  = "No Card Delivery Information found for the specified Card Item Id";
                    response.Status.Message.TechnicalMessage = "No Card Delivery Information found!";
                    return(response);
                }
                if (regObj.MissingQuantityFound > thisCardItem.MissingQuantity)
                {
                    response.Status.Message.FriendlyMessage  = "Quantity Found Cannot be more than Missing quantity";
                    response.Status.Message.TechnicalMessage = "Quantity Found Cannot be more than Missing quantity!";
                    return(response);
                }

                if (regObj.DefectiveQuantityRectified > thisCardItem.DefectiveQuantity)
                {
                    response.Status.Message.FriendlyMessage  = "defective Quantity Rectified Cannot be more than defective quantity";
                    response.Status.Message.TechnicalMessage = "defective Quantity Found Cannot be more than Missing quantity!";
                    return(response);
                }

                using (var db = _uoWork.BeginTransaction())
                {
                    //Update card item
                    thisCardItem.AvailableQuantity = regObj.MissingQuantityFound > 0 || regObj.DefectiveQuantityRectified > 0 ? thisCardItem.AvailableQuantity + regObj.MissingQuantityFound + regObj.DefectiveQuantityRectified : thisCardItem.AvailableQuantity;
                    thisCardItem.MissingQuantity   = regObj.MissingQuantityFound > 0 ? thisCardItem.MissingQuantity - regObj.MissingQuantityFound : thisCardItem.MissingQuantity;
                    thisCardItem.DefectiveQuantity = regObj.DefectiveQuantityRectified > 0 ? thisCardItem.DefectiveQuantity - regObj.DefectiveQuantityRectified : thisCardItem.DefectiveQuantity;

                    var added = _cardItemRepository.Update(thisCardItem);
                    _uoWork.SaveChanges();
                    if (added.CardItemId < 1)
                    {
                        db.Rollback();
                        response.Status.Message.FriendlyMessage  = "Error Occurred! Unable to complete your request. Please try again later";
                        response.Status.Message.TechnicalMessage = "Unable to save to database";
                        return(response);
                    }

                    //Update Card Delivery
                    thisCardDelivery.MissingQuantity   = regObj.MissingQuantityFound > 0 ? thisCardDelivery.MissingQuantity - regObj.MissingQuantityFound : thisCardDelivery.MissingQuantity;
                    thisCardDelivery.DefectiveQuantity = regObj.DefectiveQuantityRectified > 0 ? thisCardDelivery.DefectiveQuantity - regObj.DefectiveQuantityRectified : thisCardDelivery.DefectiveQuantity;

                    var deliveryAdded = _cardDeliveryRepository.Update(thisCardDelivery);
                    _uoWork.SaveChanges();
                    if (deliveryAdded.CardDeliveryId < 1)
                    {
                        db.Rollback();
                        response.Status.Message.FriendlyMessage  = "Error Occurred! Unable to complete your request. Please try again later";
                        response.Status.Message.TechnicalMessage = "Unable to save to database";
                        return(response);
                    }

                    db.Commit();

                    response.Status.IsSuccessful = true;
                    response.CardId = regObj.CardId;
                    response.Status.Message.FriendlyMessage = "Card Item Update Successfully";
                }
            }
            catch (DbEntityValidationException ex)
            {
                ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message);
                response.Status.Message.FriendlyMessage  = "Error Occurred! Please try again later";
                response.Status.Message.TechnicalMessage = "Error: " + ex.GetBaseException().Message;
                response.Status.IsSuccessful             = false;
                return(response);
            }
            catch (Exception ex)
            {
                ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message);
                response.Status.Message.FriendlyMessage  = "Error Occurred! Please try again later";
                response.Status.Message.TechnicalMessage = "Error: " + ex.GetBaseException().Message;
                response.Status.IsSuccessful             = false;
                return(response);
            }
            return(response);
        }