public ActionResult UploadFile()
        {
            string filename = string.Empty;
            //HttpPostedFileBase uploadFile = Request.Files["uploadFile"] as HttpPostedFileBase;
            string path   = AppSetting.GetFileUploadTempPath;
            bool   result = UploadFileService.UploadFile(/*uploadFile,*/ path, out filename);

            return(Json(new { result = result, filename = filename }));
        }
Exemplo n.º 2
0
        public async Task <JsonResult> EditInvoice(InvoiceViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new { Error = "Complete all required fields" }));
            }
            if (model.Id.HasValue)
            {
                var isAdmin = User.IsInRole("admin");
                var invoice = _invoiceService.AllIncluding(item => item.Assignment, item => item.Assignment.Customer, item => item.Assignment.Invoices, item => item.InvoiceAttachments, item => item.InvoiceAttachments.Select(e => e.Attachment)).SingleOrDefault(item => item.Id == model.Id.Value && (isAdmin || item.Assignment.CustomerId == CustomerId));
                if (invoice != null)
                {
                    var assignment = invoice.Assignment;
                    if (!isAdmin && assignment.StatusId != 1)
                    {
                        return(Json(new { Error = "Cannot edit submitted or approved invoices" }));
                    }
                    _uow.BeginTransaction();
                    invoice.Amount       = model.Amount;
                    invoice.PaidAmount   = invoice.Amount - (invoice.Amount * assignment.Customer.Rate / 100);
                    invoice.CustomerName = model.CustomerName;
                    invoice.Number       = model.Number;
                    invoice.WONumber     = model.WONumber;
                    invoice.HoldReason   = model.HoldReason;
                    Attachment attachment = null;

                    //if check amount < paid amount and overpayment is null then create overpayment
                    decimal?adjustment = null;
                    if (isAdmin)
                    {
                        invoice.CheckAmount = model.CheckAmount;
                        invoice.CheckNumber = model.CheckNumber;
                        if (model.CheckDate != null)
                        {
                            invoice.CheckDate = model.CheckDate;
                        }
                        //upload check

                        if (Request.Files.Count > 0 && Request.Files["check"] != null)
                        {
                            var ext          = Request.Files["check"].FileName.Substring(Request.Files["check"].FileName.LastIndexOf(".", StringComparison.Ordinal)).ToLower();
                            var tempFileName = UploadFileService.UploadFile(Request.Files["check"]);
                            var invoiceCheck = invoice.InvoiceAttachments.Where(item => item.IsCheck).FirstOrDefault();
                            if (invoiceCheck?.Attachment != null)
                            {
                                invoiceCheck.Attachment.Path        = "/PdfFiles/" + tempFileName + ".pdf";
                                invoiceCheck.Attachment.OrgFilePath = "/OrgFiles/" + tempFileName + ext;
                                invoiceCheck.Attachment.Name        = Request.Form["Check_Name"];
                                _attachmentService.Update(invoiceCheck.Attachment);
                            }
                            else
                            {
                                invoiceCheck = new InvoiceAttachment
                                {
                                    IsCheck    = true,
                                    Attachment = new Attachment
                                    {
                                        Path        = "/PdfFiles/" + tempFileName + ".pdf",
                                        OrgFilePath = "/OrgFiles/" + tempFileName + ext,
                                        Name        = Request.Form["Check_Name"]
                                    }
                                };

                                if (invoice.InvoiceAttachments == null)
                                {
                                    invoice.InvoiceAttachments = new List <InvoiceAttachment>();
                                }
                                invoice.InvoiceAttachments.Add(invoiceCheck);
                            }
                            attachment = invoiceCheck.Attachment;
                        }
                        if (model.CheckAmount >= 0 && !string.IsNullOrEmpty(model.CheckNumber) &&
                            model.CheckDate.HasValue)
                        {
                            if (model.CheckAmount == 0)
                            {
                                invoice.StatusId = 5;
                            }
                            else
                            {
                                invoice.StatusId = 2;
                            }
                            var oldAdjustment = _adjustmentService.FindBy(item => item.OriginInvoiceId == invoice.Id).FirstOrDefault();

                            if (oldAdjustment != null)
                            {
                                oldAdjustment.Amount = invoice.CheckAmount - invoice.Amount;
                                if (oldAdjustment.Amount == 0)
                                {
                                    _adjustmentService.Delete(oldAdjustment);
                                }
                                else
                                {
                                    _adjustmentService.Update(oldAdjustment);
                                }
                            }
                            else
                            {
                                if (invoice.CheckAmount != invoice.Amount)
                                {
                                    var newAdjustment = new Adjustment
                                    {
                                        OriginInvoice = invoice,
                                        CustomerId    = assignment.CustomerId,
                                        StatusId      = 1,
                                        Amount        = invoice.CheckAmount - invoice.Amount
                                    };
                                    _adjustmentService.Insert(newAdjustment);
                                }
                            }
                        }
                        //implement hold invoices
                        if (model.StatusId.HasValue && (model.StatusId == 3 || model.StatusId == 4))
                        {
                            //create new assignment
                            var newAssignment = Mapper.Map <Assignment>(assignment);
                            SetAssignmnetLetter(newAssignment);
                            newAssignment.Invoices = new List <Invoice>();
                            _assignmentService.Insert(newAssignment);
                            _uow.SaveChanges();
                            invoice.AssignmentId = newAssignment.Id;
                            newAssignment.Invoices.Add(invoice);
                            assignment.Invoices.Remove(invoice);

                            _invoiceService.Update(invoice);
                            _uow.SaveChanges();
                            _assignmentService.Recaculate(newAssignment);
                            _uow.SaveChanges();
                            //send email to user
                            var user = UserManager.Users.SingleOrDefault(item => item.Id == invoice.Assignment.UserId);
                            var msg  = GetInvoiceHoldEmail(invoice, user);
                            MailService.SendMail(user.Email, "Invoice on Hold", msg);
                        }
                    }

                    _invoiceService.Update(invoice);
                    _assignmentService.Recaculate(assignment);
                    if (assignment.Invoices.Count == 0)
                    {
                        assignment.StatusId = 5;
                    }
                    _uow.SaveChanges();
                    _uow.Commit();
                    if (isAdmin)
                    {
                        adjustment = _adjustmentService.All.FirstOrDefault(item => item.OriginInvoiceId == invoice.Id)?.Amount;
                        assignment = _assignmentService.Find(assignment.Id);
                        return(Json(new { Done = 1, Adjustment = adjustment, Path = attachment?.Path, CheckId = attachment?.Id, Total = assignment.Total, TotalPayable = assignment.TotalPayable, Funded = assignment.Funded }));
                    }
                    return(Json(new { Done = 1, Adjustment = adjustment, CheckId = attachment?.Id }));
                }
            }
            return(Json(new { Error = "Invoice not exist." }));
        }