示例#1
0
        public string publishInv(ApiPublish publish)
        {
            Company currentCom = ((EInvoiceContext)FXContext.Current).CurrentCompany;
            ILog    log        = LogManager.GetLogger(typeof(PublishController));

            if (!LockTable.Contains(String.Format("{0}${1}", publish.pattern, currentCom.id)))
            {
                object lockobj = new object();
                LockTable.Add(String.Format("{0}${1}", publish.pattern, currentCom.id), lockobj);
            }
            lock (LockTable[String.Format("{0}${1}", publish.pattern, currentCom.id)])
            {
                IDeliver         _DeliverService = currentCom.Config.ContainsKey("IDeliver") ? InvServiceFactory.GetDeliver(currentCom.Config["IDeliver"]) : null;
                IInvoiceService  IInvSrv         = InvServiceFactory.GetService(publish.pattern, currentCom.id);
                IList <IInvoice> lst             = IInvSrv.GetByID(currentCom.id, publish.invIDs).OrderBy(p => p.CreateDate).ToList();
                try
                {
                    if (lst.Count() <= 50)
                    {
                        Launcher.Instance.Launch(publish.pattern, publish.serial, lst.ToArray());
                    }
                    else
                    {
                        for (int i = 0; i < lst.Count() / 50; i++)
                        {
                            Launcher.Instance.Launch(publish.pattern, publish.serial, lst.Skip(i * 50).Take(50).ToArray());
                        }
                    }
                    try
                    {
                        if (_DeliverService != null)
                        {
                            _DeliverService.PrepareDeliver(lst.ToArray(), currentCom);
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex);
                    }
                    return("OK");
                }
                catch (EInvoice.Core.Launching.NoFactory.OpenTranException ex)
                {
                    log.Error(ex);
                    return("ERR:14");
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                    return("ERR:5 " + ex.Message);//loi phat hanh hoa don
                }
            }
        }
示例#2
0
        public IHttpActionResult ImportAndPublishInv(ApiPublish publish)
        {
            ILog log = LogManager.GetLogger(typeof(PublishController));
            //check valiadate xml
            XmlSchemaValidator validator = new XmlSchemaValidator();

            //xmlInvData = convertSpecialCharacter(xmlInvData);
            if (!validator.ValidXmlDoc(publish.xmlData, "", AppDomain.CurrentDomain.BaseDirectory + @"xmlvalidate\VATInVoice_ws.xsd"))
            {
                log.Error(publish.xmlData);
                log.Error("ERR:3-" + validator.ValidationError);
                return(Ok("ERR:3-" + validator.ValidationError));  //du lieu dau vao khong hop le
            }

            if (publish.convert == 1)
            {
                publish.xmlData = DataHelper.convertTCVN3ToUnicode(publish.xmlData);
            }

            Company  _currentCompany = ((EInvoiceContext)FXContext.Current).CurrentCompany;
            IDeliver _DeliverService = _currentCompany.Config.ContainsKey("IDeliver") ? InvServiceFactory.GetDeliver(_currentCompany.Config["IDeliver"]) : null;
            int      comID           = _currentCompany.id;

            if (_currentCompany == null)
            {
                return(NotFound());
            }
            IPublishInvoiceService _PubInvSrv = IoC.Resolve <IPublishInvoiceService>();
            int    patternNull = string.IsNullOrEmpty(publish.pattern) ? 0 : 1;
            int    serialNull  = string.IsNullOrEmpty(publish.serial) ? 0 : 1;
            string nameInvoice = "";

            switch ((patternNull + serialNull))
            {
            case 0:
                PublishInvoice pubinv = _PubInvSrv.GetFirst(comID, new int[] { 1, 2 });
                if (pubinv != null)
                {
                    IRegisterTempService regisSrv = IoC.Resolve <IRegisterTempService>();
                    RegisterTemp         rtemp    = regisSrv.Getbykey(pubinv.RegisterID);
                    publish.pattern = pubinv.InvPattern;
                    publish.serial  = pubinv.InvSerial;
                    nameInvoice     = rtemp.NameInvoice;
                    _PubInvSrv.UnbindSession(pubinv);
                    regisSrv.UnbindSession(rtemp);
                }
                else
                {
                    return(Ok("ERR:20"));     //tham so pattern va serial khong hop le
                }
                break;

            case 1:
                PublishInvoice pubFirst = null;
                try
                {
                    if (!string.IsNullOrWhiteSpace(publish.pattern))
                    {
                        pubFirst = (from p in _PubInvSrv.Query where (p.ComId == comID) && (p.InvPattern == publish.pattern) && (p.Status == 1 || p.Status == 2) select p).FirstOrDefault();
                    }
                    else
                    {
                        pubFirst = (from p in _PubInvSrv.Query where (p.ComId == comID) && (p.InvSerial == publish.serial) && (p.Status == 1 || p.Status == 2) select p).FirstOrDefault();
                    }
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                    return(Ok("ERR:20"));      //khong can return cung duoc
                }
                if (pubFirst == null)
                {
                    return(Ok("ERR:20"));
                }
                else
                {
                    IRegisterTempService regisSrv = IoC.Resolve <IRegisterTempService>();
                    RegisterTemp         rtemp    = regisSrv.Getbykey(pubFirst.RegisterID);
                    nameInvoice     = rtemp.NameInvoice;
                    publish.pattern = pubFirst.InvPattern;
                    publish.serial  = pubFirst.InvSerial;
                    _PubInvSrv.UnbindSession(pubFirst);
                    regisSrv.UnbindSession(rtemp);
                }
                break;

            case 2:
                // PublishInvoice pub = _PubInvSrv.CurrentPubInv(comID, pattern, serial, 1);
                PublishInvoice pub = null;
                try
                {
                    pub = (from p in _PubInvSrv.Query where (p.ComId == comID) && (p.InvPattern == publish.pattern) && (p.InvSerial == publish.serial) && (p.Status == 1 || p.Status == 2) select p).First();
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                    return(Ok("ERR:20"));       //khong can return cung duoc
                }
                if (pub == null)
                {
                    return(Ok("ERR:20"));
                }
                else
                {
                    IRegisterTempService regisSrv = IoC.Resolve <IRegisterTempService>();
                    RegisterTemp         rtemp    = regisSrv.Getbykey(pub.RegisterID);
                    nameInvoice = rtemp.NameInvoice;
                    _PubInvSrv.UnbindSession(pub);
                    regisSrv.UnbindSession(rtemp);
                }
                break;
            }
            log.Info("pattern-serial: " + publish.pattern + "-" + publish.serial);
            XElement               elem             = XElement.Parse(publish.xmlData);
            IList <IInvoice>       lstINV           = new List <IInvoice>();
            string                 invKeyAndNumList = "";
            IEnumerable <XElement> listTemp         = elem.Elements("Inv");
            IList <String>         lstKey           = new List <String>();

            //check xem con du so hoa don ko
            if (getMaxInvNumber(comID, publish.pattern, publish.serial) < listTemp.Count())
            {
                return(Ok("ERR:6"));    //khong con du so hoa don cho lo phat hanh
            }
            //check xem lo truyen vao lon hon MaxBlockInv hay ko
            int    maxBlockInv;
            string temp = System.Configuration.ConfigurationManager.AppSettings["MaxBlockInv"];

            if (string.IsNullOrEmpty(temp) || !Int32.TryParse(temp, out maxBlockInv))
            {
                maxBlockInv = 5000;
            }
            if (listTemp.Count() > maxBlockInv)
            {
                return(Ok("ERR:10"));    // lo hoa don truyen vao lon hon maxBlockInv;
            }
            try
            {
                foreach (XElement e in listTemp)
                {
                    XElement ele  = e.Element("Invoice");
                    string   fKey = e.Element("key").Value.Trim();
                    lstKey.Add(fKey);
                    IInvoice inv  = InvServiceFactory.NewInstance(publish.pattern, comID);
                    string   read = string.Concat(ele);
                    inv.DeserializeFromXML(read);
                    inv.Name     = nameInvoice;
                    inv.Pattern  = publish.pattern;
                    inv.Serial   = publish.serial;
                    inv.CreateBy = inv.CreateBy ?? System.Web.HttpContext.Current.User.Identity.Name;
                    inv.Fkey     = fKey;
                    lstINV.Add(inv);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return(Ok("ERR:5"));//loi phat hanh hoa don
            }

            try
            {
                Launcher.Instance.Launch(publish.pattern, publish.serial, lstINV.ToArray());
                if (_DeliverService != null)
                {
                    _DeliverService.PrepareDeliver(lstINV.ToArray(), _currentCompany);
                }
                for (int i = 0; i < lstINV.Count; i++)
                {
                    invKeyAndNumList += lstKey[i] + "_" + lstINV[i].No + ",";
                }
                //remove the last "," character
                invKeyAndNumList = invKeyAndNumList.Remove(invKeyAndNumList.Length - 1, 1);
                return(Ok("OK:" + publish.pattern + ";" + publish.serial + "-" + invKeyAndNumList));
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return(Ok("ERR:13"));
            }
        }
示例#3
0
 public void PublishInv(string pattern, string serial, IInvoice[] mInvoiceList, string username = null)
 {
     try
     {
         Company  currentCom      = ((EInvoiceContext)FXContext.Current).CurrentCompany;
         IDeliver _DeliverService = currentCom.Config.ContainsKey("IDeliver") ? InvServiceFactory.GetDeliver(currentCom.Config["IDeliver"]) : null;
         if (mInvoiceList.Length <= 50)
         {
             Launcher.Instance.Launch(pattern, serial, mInvoiceList);
             Message = "OK";
         }
         else
         {
             for (int i = 0; i < mInvoiceList.Length / 50; i++)
             {
                 Launcher.Instance.Launch(pattern, serial, mInvoiceList.Skip(i * 50).Take(50).ToArray());
             }
             Message = "OK";
         }
         if (_DeliverService != null)
         {
             _DeliverService.PrepareDeliver(mInvoiceList, currentCom);
         }
     }
     catch (Exception ex)
     {
         Message = ex.Message;
     }
 }