Esempio n. 1
0
        /// <summary>
        /// This method validates that Purchase object is not being used by Production object
        /// prior to deletion. If Purchase object is used by Production object, method doesn't
        /// delete Purchase object and instead surfaces name of Production object to user.
        /// </summary>
        /// <param name="deleteObject"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public ReturnObject DeletePurchaseRecord(int userId, DeleteRecordObject deleteObject)
        {
            ReturnObject delReturn = new ReturnObject();

            int    recordId    = deleteObject.DeleteRecordID;
            string recordType  = deleteObject.DeleteRecordType;
            int    distillerId = _dl.GetDistillerId(userId);

            if (recordId > 0)
            {
                try
                {
                    var res =
                        from rec in _db.ProductionToPurchase
                        join prod2Name in _db.Production on rec.ProductionID equals prod2Name.ProductionID into prod2Name_join
                        where rec.PurchaseID == recordId
                        from prod2Name in prod2Name_join.DefaultIfEmpty()
                        select new
                    {
                        ProductionID   = (int?)prod2Name.ProductionID ?? 0,
                        ProductionName = prod2Name.ProductionName ?? string.Empty
                    };

                    if (res.Count() == 0)
                    {
                        delReturn.ExecuteResult = DeletePurchaseExecute(recordId, userId);
                    }
                    else
                    {
                        foreach (var item in res)
                        {
                            delReturn.ExecuteMessage = item.ProductionName;
                        }
                    }
                }
                catch (Exception e)
                {
                    delReturn.ExecuteResult = false;
                }
            }
            else
            {
                delReturn.ExecuteResult  = false;
                delReturn.ExecuteMessage = "Purchase Id is Null";
            }
            return(delReturn);
        }
Esempio n. 2
0
 public JsonResult DeleteRecord(DeleteRecordObject deleteObject)
 {
     if (deleteObject.DeleteRecordID >= 0 && deleteObject != null)
     {
         if (User.Identity.IsAuthenticated)
         {
             int userId = User.Identity.GetUserId <int>();
             if (userId > 0)
             {
                 ReturnObject returnResult = _production.DeleteProductionRecord(userId, deleteObject);
                 if (returnResult.ExecuteResult)
                 {
                     string message = deleteObject.DeleteRecordType + " Record was deleted successfully";
                     return(Json(message));
                 }
                 else
                 {
                     string message = "Wasn't able to delete " + deleteObject.DeleteRecordType + " Record because it's associated with " + returnResult.ExecuteMessage;
                     return(Json(message));
                 }
             }
             else
             {
                 return(Json("Unable to find UserId!"));
             }
         }
         else
         {
             return(Json("Unauthenticated user!"));
         }
     }
     else
     {
         string message = "ID " + deleteObject.DeleteRecordID + " of the record being deleted is not valid or Production Object being deleted is null";
         return(Json(message));
     }
 }
        public ReturnObject DeleteDictionaryRecord(int userId, DeleteRecordObject deleteObject)
        {
            int          RecordID    = deleteObject.DeleteRecordID;
            string       RecordType  = deleteObject.DeleteRecordType;
            int          distillerId = _dl.GetDistillerId(userId);
            ReturnObject delReturn   = new ReturnObject();

            if (RecordID > 0)
            {
                try
                {
                    if (RecordType == "RawMaterial")
                    {
                        // check is material used in purchase
                        var res1 = from rec in _db.Purchase
                                   where rec.MaterialDictID == RecordID && rec.DistillerID == distillerId
                                   select rec;
                        // check is material used in blending
                        var res2 = (from rec in _db.BlendedComponent
                                    where rec.RecordId == RecordID
                                    select rec).ToList();

                        if (res1.Count() == 0 && res2.Count() == 0)
                        {
                            delReturn.ExecuteResult = DeleteRawMaterial(userId, RecordID);
                        }
                        if (res1.Count() != 0)
                        {
                            foreach (var item in res1)
                            {
                                delReturn.ExecuteMessage = item.PurchaseName;
                            }
                        }
                        else if (res2.Count() != 0)
                        {
                            string recordName;
                            foreach (var item in res2)
                            {
                                recordName =
                                    (from rec in _db.Production
                                     where rec.ProductionID == item.ProductionID && rec.DistillerID == distillerId
                                     select rec.ProductionName).FirstOrDefault();
                                delReturn.ExecuteMessage = recordName;
                            }
                        }
                    }
                    else if (RecordType == "Spirit")
                    {
                        var res =
                            from rec in _db.ProductionToSpirit
                            join prod2Name in _db.Production on rec.ProductionID equals prod2Name.ProductionID into prod2Name_join
                            where rec.SpiritID == RecordID
                            from prod2Name in prod2Name_join.DefaultIfEmpty()
                            select new
                        {
                            ProductionID   = (int?)prod2Name.ProductionID ?? 0,
                            ProductionName = prod2Name.ProductionName ?? string.Empty
                        };

                        if (res.Count() == 0)
                        {
                            delReturn.ExecuteResult = DeleteSpirit(userId, RecordID);
                        }
                        else
                        {
                            foreach (var item in res)
                            {
                                delReturn.ExecuteMessage = item.ProductionName;
                            }
                        }
                    }
                    else if (RecordType == "Storage")
                    {
                        var res = (from rec in _db.StorageToRecord
                                   where rec.StorageID == RecordID
                                   select rec).ToList();

                        if (res.Count() == 0)
                        {
                            delReturn.ExecuteResult = DeleteStorage(userId, RecordID);
                        }
                        else
                        {
                            string recordName;
                            foreach (var item in res)
                            {
                                if (item.TableIdentifier == "pur")
                                {
                                    recordName = (from rec in _db.Purchase
                                                  where rec.PurchaseID == item.RecordId && rec.DistillerID == distillerId
                                                  select rec.PurchaseName).FirstOrDefault();
                                    delReturn.ExecuteMessage = recordName;
                                }
                                if (item.TableIdentifier == "prod")
                                {
                                    recordName = (from rec in _db.Production
                                                  where rec.ProductionID == item.RecordId && rec.DistillerID == distillerId
                                                  select rec.ProductionName).FirstOrDefault();
                                    delReturn.ExecuteMessage = recordName;
                                }
                            }
                        }
                    }
                    else if (RecordType == "Vendor")
                    {
                        var res = from rec in _db.Purchase
                                  where rec.VendorID == RecordID && rec.DistillerID == distillerId
                                  select rec;

                        if (res.Count() == 0)
                        {
                            delReturn.ExecuteResult = DeleteVendor(userId, RecordID);
                        }
                        else
                        {
                            foreach (var item in res)
                            {
                                delReturn.ExecuteMessage = item.PurchaseName;
                            }
                        }
                    }
                    else
                    {
                        delReturn.ExecuteResult = false;
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Failed to delete " + RecordType + ": " + e);
                    delReturn.ExecuteResult = false;
                }
            }
            else
            {
                delReturn.ExecuteResult = false;
            }
            return(delReturn);
        }