Example #1
0
        //[ValidateAntiForgeryToken]
        public ActionResult _CreatePost(PurchaseInvoiceLineViewModel svm)
        {
            PurchaseInvoiceLine   s    = Mapper.Map <PurchaseInvoiceLineViewModel, PurchaseInvoiceLine>(svm);
            PurchaseInvoiceHeader temp = new PurchaseInvoiceHeaderService(_unitOfWork).Find(s.PurchaseInvoiceHeaderId);

            if (svm.PurchaseInvoiceLineId <= 0)
            {
                ViewBag.LineMode = "Create";
            }
            else
            {
                ViewBag.LineMode = "Edit";
            }

            if (svm.PurchInvoiceSettings != null)
            {
                if (svm.PurchInvoiceSettings.isMandatoryRate == true && svm.Rate <= 0)
                {
                    ModelState.AddModelError("Rate", "The Rate field is required");
                }
            }

            if (svm.PurchaseGoodsReceiptLineId == 0)
            {
                ModelState.AddModelError("PurchaseGoodsReceiptLineId", "Goods Receipt field is required");
            }

            if (svm.DealQty <= 0)
            {
                ModelState.AddModelError("DealQty", "DealQty field is required");
            }

            bool BeforeSave = true;

            try
            {
                if (svm.PurchaseInvoiceLineId <= 0)
                {
                    BeforeSave = PurchaseInvoiceDocEvents.beforeLineSaveEvent(this, new PurchaseEventArgs(svm.PurchaseInvoiceHeaderId, EventModeConstants.Add), ref db);
                }
                else
                {
                    BeforeSave = PurchaseInvoiceDocEvents.beforeLineSaveEvent(this, new PurchaseEventArgs(svm.PurchaseInvoiceHeaderId, EventModeConstants.Edit), ref db);
                }
            }
            catch (Exception ex)
            {
                string message = _exception.HandleException(ex);
                TempData["CSEXCL"] += message;
                EventException      = true;
            }

            if (!BeforeSave)
            {
                ModelState.AddModelError("", "Validation failed before save.");
            }


            if (ModelState.IsValid && BeforeSave && !EventException)
            {
                if (svm.PurchaseInvoiceLineId <= 0)
                {
                    s.DiscountPer  = svm.DiscountPer;
                    s.CreatedDate  = DateTime.Now;
                    s.ModifiedDate = DateTime.Now;
                    s.Sr           = _PurchaseInvoiceLineService.GetMaxSr(s.PurchaseInvoiceHeaderId);
                    s.CreatedBy    = User.Identity.Name;
                    s.ModifiedBy   = User.Identity.Name;
                    s.ObjectState  = Model.ObjectState.Added;
                    db.PurchaseInvoiceLine.Add(s);
                    //_PurchaseInvoiceLineService.Create(s);

                    new PurchaseOrderLineStatusService(_unitOfWork).UpdatePurchaseQtyOnInvoice(s.PurchaseGoodsReceiptLineId, s.PurchaseInvoiceLineId, temp.DocDate, s.DocQty, ref db, true);



                    if (svm.linecharges != null)
                    {
                        foreach (var item in svm.linecharges)
                        {
                            item.LineTableId   = s.PurchaseInvoiceLineId;
                            item.PersonID      = temp.SupplierId;
                            item.HeaderTableId = s.PurchaseInvoiceHeaderId;
                            item.ObjectState   = Model.ObjectState.Added;
                            db.PurchaseInvoiceLineCharge.Add(item);
                            //new PurchaseInvoiceLineChargeService(_unitOfWork).Create(item);
                        }
                    }

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

                            else
                            {
                                item.HeaderTableId = s.PurchaseInvoiceHeaderId;
                                item.PersonID      = temp.SupplierId;
                                item.ObjectState   = Model.ObjectState.Added;
                                db.PurchaseInvoiceHeaderCharge.Add(item);
                                //new PurchaseInvoiceHeaderChargeService(_unitOfWork).Create(item);
                            }
                        }
                    }



                    if (temp.Status != (int)StatusConstants.Drafted)
                    {
                        temp.Status       = (int)StatusConstants.Modified;
                        temp.ModifiedBy   = User.Identity.Name;
                        temp.ModifiedDate = DateTime.Now;
                        //new PurchaseInvoiceHeaderService(_unitOfWork).Update(temp);
                    }

                    temp.ObjectState = Model.ObjectState.Modified;
                    db.PurchaseInvoiceHeader.Add(temp);

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


                    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", svm));
                    }

                    try
                    {
                        PurchaseInvoiceDocEvents.afterLineSaveEvent(this, new PurchaseEventArgs(s.PurchaseInvoiceHeaderId, s.PurchaseInvoiceLineId, EventModeConstants.Add), ref db);
                    }
                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXCL"] += message;
                    }

                    LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                    {
                        DocTypeId    = temp.DocTypeId,
                        DocId        = temp.PurchaseInvoiceHeaderId,
                        DocLineId    = s.PurchaseInvoiceLineId,
                        ActivityType = (int)ActivityTypeContants.Added,
                        DocNo        = temp.DocNo,
                        DocDate      = temp.DocDate,
                        DocStatus    = temp.Status,
                    }));


                    return(RedirectToAction("_Create", new { id = s.PurchaseInvoiceHeaderId, sid = svm.SupplierId }));
                }
                else
                {
                    List <LogTypeViewModel> LogList = new List <LogTypeViewModel>();

                    PurchaseInvoiceHeader header    = new PurchaseInvoiceHeaderService(_unitOfWork).Find(svm.PurchaseInvoiceHeaderId);
                    StringBuilder         logstring = new StringBuilder();
                    int status = header.Status;
                    PurchaseInvoiceLine temp1 = db.PurchaseInvoiceLine.Find(svm.PurchaseInvoiceLineId);

                    PurchaseInvoiceLine ExRec = new PurchaseInvoiceLine();
                    ExRec = Mapper.Map <PurchaseInvoiceLine>(temp1);


                    temp1.DiscountPer = svm.DiscountPer;
                    temp1.Amount      = svm.Amount;
                    //temp1.PurchaseGoodsReceiptLineId = svm.PurchaseGoodsReceiptLineId;
                    temp1.UnitConversionMultiplier = svm.UnitConversionMultiplier;
                    temp1.DealQty      = svm.DealQty;
                    temp1.DealUnitId   = svm.DealUnitId;
                    temp1.Rate         = svm.Rate;
                    temp1.Remark       = svm.Remark;
                    temp1.ModifiedDate = DateTime.Now;
                    temp1.ModifiedBy   = User.Identity.Name;
                    temp1.ObjectState  = Model.ObjectState.Modified;
                    db.PurchaseInvoiceLine.Add(temp1);
                    //_PurchaseInvoiceLineService.Update(temp1);

                    LogList.Add(new LogTypeViewModel
                    {
                        ExObj = ExRec,
                        Obj   = temp1,
                    });

                    new PurchaseOrderLineStatusService(_unitOfWork).UpdatePurchaseQtyOnInvoice(temp1.PurchaseGoodsReceiptLineId, temp1.PurchaseInvoiceLineId, temp.DocDate, temp1.DocQty, ref db, true);

                    if (header.Status != (int)StatusConstants.Drafted)
                    {
                        header.Status       = (int)StatusConstants.Modified;
                        header.ModifiedBy   = User.Identity.Name;
                        header.ModifiedDate = DateTime.Now;
                        //new PurchaseInvoiceHeaderService(_unitOfWork).Update(header);
                    }

                    header.ObjectState = Model.ObjectState.Modified;
                    db.PurchaseInvoiceHeader.Add(header);

                    if (svm.linecharges != null)
                    {
                        foreach (var item in svm.linecharges)
                        {
                            var productcharge = db.PurchaseInvoiceLineCharge.Find(item.Id);

                            productcharge.Rate        = item.Rate;
                            productcharge.Amount      = item.Amount;
                            productcharge.DealQty     = item.DealQty;
                            productcharge.ObjectState = Model.ObjectState.Modified;
                            db.PurchaseInvoiceLineCharge.Add(productcharge);
                            //new PurchaseInvoiceLineChargeService(_unitOfWork).Update(productcharge);
                        }
                    }


                    if (svm.footercharges != null)
                    {
                        foreach (var item in svm.footercharges)
                        {
                            var footercharge = db.PurchaseInvoiceHeaderCharge.Find(item.Id);

                            footercharge.Rate        = item.Rate;
                            footercharge.Amount      = item.Amount;
                            footercharge.ObjectState = Model.ObjectState.Modified;
                            db.PurchaseInvoiceHeaderCharge.Add(footercharge);
                            //new PurchaseInvoiceHeaderChargeService(_unitOfWork).Update(footercharge);
                        }
                    }

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

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

                    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", svm));
                    }

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

                    //Saving the Activity Log

                    LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                    {
                        DocTypeId       = temp.DocTypeId,
                        DocId           = temp1.PurchaseInvoiceHeaderId,
                        DocLineId       = temp1.PurchaseInvoiceLineId,
                        ActivityType    = (int)ActivityTypeContants.Modified,
                        DocNo           = temp.DocNo,
                        xEModifications = Modifications,
                        DocDate         = temp.DocDate,
                        DocStatus       = temp.Status,
                    }));
                    //End of Saving the Activity Log

                    return(Json(new { success = true }));
                }
            }
            PrepareViewBag(svm);
            return(PartialView("_Create", svm));
        }