Esempio n. 1
0
        public ActionResult UpdatePublish(int id, string PubInvoiceList)
        {
            if (id <= 0)
            {
                throw new HttpRequestValidationException();
            }
            IPublishService      pubSrc = IoC.Resolve <IPublishService>();
            ITaxAuthorityService taxSrv = IoC.Resolve <ITaxAuthorityService>();
            Publish opub = pubSrc.Getbykey(id);

            try
            {
                TryUpdateModel <Publish>(opub);
                opub.TaxAuthorityName = (from tax in taxSrv.Query where tax.Code == opub.TaxAuthorityCode select tax.Name).FirstOrDefault();
                JavaScriptSerializer   jss = new JavaScriptSerializer();
                IList <PublishInvoice> lst = jss.Deserialize <IList <PublishInvoice> >(PubInvoiceList);
                foreach (var it in lst)
                {
                    IPublishInvoiceService pubinvSrv = IoC.Resolve <IPublishInvoiceService>();
                    ICompanyService        _comSrv   = IoC.Resolve <ICompanyService>();
                    Company _currentcompany          = _comSrv.Getbykey(((EInvoiceContext)FXContext.Current).CurrentCompany.id);
                    string  invSer = string.Format("{0}/{1}E", it.InvSerialPrefix, it.InvSerialSuffix);
                    var     old    = pubinvSrv.Query.Where(p => p.RegisterID == it.RegisterID && p.InvSerial == invSer && p.StartDate > it.StartDate && p.FromNo < it.FromNo && p.ComId == _currentcompany.id).FirstOrDefault();
                    if (old != null)
                    {
                        Messages.AddErrorMessage(string.Format("Thông báo có ký hiệu {0}, phải có ngày bắt đầu từ ngày: {1}", old.InvSerial, old.StartDate.ToString("dd/MM/yyyy")));
                        PublishModel model = new PublishModel();
                        model.mPublish = opub;
                        ITaxAuthorityService taxSrc = IoC.Resolve <ITaxAuthorityService>();
                        model.TaxList = new SelectList(from tax in taxSrc.Query select tax, "Code", "Name", opub.TaxAuthorityCode);
                        IRegisterTempService regisSrc = IoC.Resolve <IRegisterTempService>();
                        model.RegTempList    = new SelectList(from re in regisSrc.Query where re.ComId == _currentcompany.id select re, "Id", "Name");
                        model.PubInvoiceList = opub.PublishInvoices.SerializeJSON <PublishInvoice>();
                        return(View("EditRPublish", model));
                    }
                    else
                    {
                        old = pubinvSrv.Query.Where(p => p.RegisterID == it.RegisterID && p.InvSerial == invSer && p.StartDate <it.StartDate && p.FromNo> it.FromNo && p.ComId == _currentcompany.id).FirstOrDefault();
                        if (old != null)
                        {
                            Messages.AddErrorMessage(string.Format("Thông báo có ký hiệu {0}, phải có ngày bắt đầu nhỏ hơn ngày: {1}", old.InvSerial, old.StartDate.ToString("dd/MM/yyyy")));
                            PublishModel model = new PublishModel();
                            model.mPublish = opub;
                            ITaxAuthorityService taxSrc = IoC.Resolve <ITaxAuthorityService>();
                            model.TaxList = new SelectList(from tax in taxSrc.Query select tax, "Code", "Name", opub.TaxAuthorityCode);
                            IRegisterTempService regisSrc = IoC.Resolve <IRegisterTempService>();
                            model.RegTempList    = new SelectList(from re in regisSrc.Query where re.ComId == _currentcompany.id select re, "Id", "Name");
                            model.PubInvoiceList = opub.PublishInvoices.SerializeJSON <PublishInvoice>();
                            return(View("EditRPublish", model));
                        }
                    }
                }
                string mess = "";
                if (pubSrc.Update(opub, lst, out mess) == true)
                {
                    StringBuilder InvInfo = new StringBuilder();
                    for (int i = 0; i < lst.Count; i++)
                    {
                        InvInfo.AppendFormat("{0};{1};{2};{3};{4}_", lst[i].RegisterID, lst[i].InvSerial, lst[i].Quantity, lst[i].FromNo, lst[i].ToNo);
                    }
                    log.Info("Edit Publish by: " + HttpContext.User.Identity.Name + "|InvInfo:" + InvInfo.ToString());
                    Messages.AddFlashMessage("Sửa thành công");
                    return(RedirectToAction("Index"));
                }
                else
                {
                    log.Error("Update Publish:" + mess);
                    ICompanyService _comSrv         = IoC.Resolve <ICompanyService>();
                    Company         _currentcompany = _comSrv.Getbykey(((EInvoiceContext)FXContext.Current).CurrentCompany.id);
                    PublishModel    model           = new PublishModel();
                    model.mPublish = opub;
                    ITaxAuthorityService taxSrc = IoC.Resolve <ITaxAuthorityService>();
                    model.TaxList = new SelectList(from tax in taxSrc.Query select tax, "Code", "Name", opub.TaxAuthorityCode);
                    IRegisterTempService regisSrc = IoC.Resolve <IRegisterTempService>();
                    model.RegTempList    = new SelectList(from re in regisSrc.Query where re.ComId == _currentcompany.id select re, "Id", "Name");
                    model.PubInvoiceList = opub.PublishInvoices.SerializeJSON <PublishInvoice>();
                    Messages.AddErrorMessage(mess);
                    return(View("EditRPublish", model));
                }
            }
            catch (Exception ex)
            {
                log.Error("EditRPublish -" + ex);
                Messages.AddErrorMessage("Có lỗi xảy ra, vui lòng thực hiện lại!");
                PublishModel model = new PublishModel();
                model.mPublish = opub;
                ICompanyService      _comSrv         = IoC.Resolve <ICompanyService>();
                Company              _currentcompany = _comSrv.Getbykey(((EInvoiceContext)FXContext.Current).CurrentCompany.id);
                ITaxAuthorityService taxSrc          = IoC.Resolve <ITaxAuthorityService>();
                model.TaxList = new SelectList(from tax in taxSrc.Query select tax, "Code", "Name", opub.TaxAuthorityCode);
                IRegisterTempService regisSrc = IoC.Resolve <IRegisterTempService>();
                model.RegTempList    = new SelectList(from re in regisSrc.Query where re.ComId == _currentcompany.id select re, "Id", "Name");
                model.PubInvoiceList = opub.PublishInvoices.SerializeJSON <PublishInvoice>();
                return(View("EditRPublish", model));
            }
        }