Example #1
0
        public ActionResult DeletePost(PurchaseInvoiceLineViewModel vm)
        {
            bool BeforeSave = true;

            try
            {
                BeforeSave = PurchaseInvoiceDocEvents.beforeLineDeleteEvent(this, new PurchaseEventArgs(vm.PurchaseInvoiceHeaderId, vm.PurchaseInvoiceLineId), ref db);
            }
            catch (Exception ex)
            {
                string message = _exception.HandleException(ex);
                TempData["CSEXC"] += message;
                EventException     = true;
            }

            if (!BeforeSave)
            {
                TempData["CSEXC"] += "Validation failed before delete.";
            }

            if (BeforeSave && !EventException)
            {
                List <LogTypeViewModel> LogList = new List <LogTypeViewModel>();

                PurchaseInvoiceLine PurchaseInvoiceLine = db.PurchaseInvoiceLine.Find(vm.PurchaseInvoiceLineId);

                try
                {
                    PurchaseInvoiceDocEvents.onLineDeleteEvent(this, new PurchaseEventArgs(PurchaseInvoiceLine.PurchaseInvoiceHeaderId, PurchaseInvoiceLine.PurchaseInvoiceLineId), ref db);
                }
                catch (Exception ex)
                {
                    string message = _exception.HandleException(ex);
                    TempData["CSEXCL"] += message;
                    EventException      = true;
                }

                LogList.Add(new LogTypeViewModel
                {
                    ExObj = Mapper.Map <PurchaseInvoiceLine>(PurchaseInvoiceLine),
                });

                PurchaseInvoiceHeader header = db.PurchaseInvoiceHeader.Find(PurchaseInvoiceLine.PurchaseInvoiceHeaderId);

                new PurchaseOrderLineStatusService(_unitOfWork).UpdatePurchaseQtyOnInvoice(PurchaseInvoiceLine.PurchaseGoodsReceiptLineId, PurchaseInvoiceLine.PurchaseInvoiceLineId, header.DocDate, 0, ref db, true);

                var chargeslist = (from p in db.PurchaseInvoiceLineCharge
                                   where p.LineTableId == PurchaseInvoiceLine.PurchaseInvoiceLineId
                                   select p).ToList();

                if (chargeslist != null)
                {
                    foreach (var item in chargeslist)
                    {
                        item.ObjectState = Model.ObjectState.Deleted;
                        db.PurchaseInvoiceLineCharge.Remove(item);
                        //new PurchaseInvoiceLineChargeService(_unitOfWork).Delete(item.Id);
                    }
                }

                PurchaseInvoiceLine.ObjectState = Model.ObjectState.Deleted;
                db.PurchaseInvoiceLine.Remove(PurchaseInvoiceLine);


                if (header.Status != (int)StatusConstants.Drafted && header.Status != (int)StatusConstants.Import)
                {
                    header.Status       = (int)StatusConstants.Modified;
                    header.ModifiedDate = DateTime.Now;
                    header.ModifiedBy   = User.Identity.Name;
                    //new PurchaseInvoiceHeaderService(_unitOfWork).Update(header);
                }
                header.ObjectState = Model.ObjectState.Modified;
                db.PurchaseInvoiceHeader.Add(header);



                if (vm.footercharges != null)
                {
                    foreach (var item in vm.footercharges)
                    {
                        var footer = db.PurchaseInvoiceHeaderCharge.Find(item.Id);
                        footer.Rate        = item.Rate;
                        footer.Amount      = item.Amount;
                        footer.ObjectState = Model.ObjectState.Modified;
                        db.PurchaseInvoiceHeaderCharge.Add(footer);
                        //new PurchaseInvoiceHeaderChargeService(_unitOfWork).Update(footer);
                    }
                }

                XElement Modifications = new ModificationsCheckService().CheckChanges(LogList);

                try
                {
                    if (EventException)
                    {
                        throw new Exception();
                    }

                    db.SaveChanges();
                    //_unitOfWork.Save();
                }

                catch (Exception ex)
                {
                    string message = _exception.HandleException(ex);
                    TempData["CSEXCL"] += message;
                    PrepareViewBag(null);
                    return(PartialView("_Create", vm));
                }

                try
                {
                    PurchaseInvoiceDocEvents.afterLineDeleteEvent(this, new PurchaseEventArgs(PurchaseInvoiceLine.PurchaseInvoiceHeaderId, PurchaseInvoiceLine.PurchaseInvoiceLineId), ref db);
                }
                catch (Exception ex)
                {
                    string message = _exception.HandleException(ex);
                    TempData["CSEXC"] += message;
                }

                LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                {
                    DocTypeId       = header.DocTypeId,
                    DocId           = header.PurchaseInvoiceHeaderId,
                    DocLineId       = PurchaseInvoiceLine.PurchaseInvoiceLineId,
                    ActivityType    = (int)ActivityTypeContants.Deleted,
                    DocNo           = header.DocNo,
                    xEModifications = Modifications,
                    DocDate         = header.DocDate,
                    DocStatus       = header.Status,
                }));
            }

            return(Json(new { success = true }));
        }