internal protected static string GenerateOrder(int Id, string Path)
        {
            var result = "";

            ServisniZasah sz = new ServisniZasah();

            sz = ServisniZasah.GetZasah(Id);

            Company oCompany = new Company();

            oCompany = SAPDIAPI.Connect();

            // Get the quotation
            Documents oOrder = (Documents)oCompany.GetBusinessObject(BoObjectTypes.oOrders);

            oOrder.GetByKey(Convert.ToInt32(sz.Zakazka));

            //Check connection before updating

            if (oCompany.Connected)

            {
            }

            oCompany.Disconnect();
            return(result);
        }
        internal protected static void UpdateQuotation(string DocEntry, int ServisniZasahId)
        {
            int currency = 0;

            string        connectionString = ConfigurationManager.ConnectionStrings["SQL"].ConnectionString;
            StringBuilder sql = new StringBuilder();

            sql.Append(" select DocEntry, Docnum from ODRF where ");
            sql.Append($" DocEntry = '{DocEntry}' and ObjType = 23");

            log.Debug($"Nacteni meny {sql.ToString()}");
            SqlConnection cnn = new SqlConnection(connectionString);
            //SqlConnection con = new SqlConnection(cnn);

            SqlCommand cmd = new SqlCommand
            {
                Connection  = cnn,
                CommandText = sql.ToString()
            };

            cnn.Open();
            cmd.ExecuteNonQuery();
            SqlDataReader dr = cmd.ExecuteReader();

            if (dr.HasRows)
            {
                //MAKES IT HERE
                while (dr.Read())
                {
                    try
                    {
                        currency = dr.GetInt32(dr.GetOrdinal("DocNum"));
                    }
                    catch (Exception ex) { log.Error("Error number: " + ex.HResult + " - " + ex.Message + " - " + ex.Data + " - " + ex.InnerException); }
                }
            }
            cnn.Close();

            ServisniZasah sz = new ServisniZasah();

            sz               = ServisniZasah.GetZasah(ServisniZasahId);
            sz.Nabidka       = DocEntry;
            sz.NabidkaDocNum = currency.ToString();

            using (var db = new Model1Container())
            {
                try
                {
                    db.Entry(sz).State = EntityState.Modified;
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    log.Error("Error number: " + ex.HResult + " - " + ex.Message + " - " + ex.Data + " - " + ex.InnerException);
                }
            }
        }
        internal protected static ServisniZasah GetZasah(int Id)
        {
            ServisniZasah sz = new ServisniZasah();

            using (var db = new Model1Container())
            {
                sz = db.ServisniZasah.Where(t => t.Id == Id)
                     .Include(t => t.ServisniZasahPrvek)
                     .Include(x => x.ServisniZasahPrvek.Select(y => y.ServisniZasahPrvekSerioveCislo))
                     .Include(x => x.ServisniZasahPrvek.Select(y => y.Artikl))
                     //.Include(x => x.ServisniZasahPrvek.Select(y => y.RadkyKusovniku))
                     .Include(t => t.Zakaznik)

                     .FirstOrDefault();
            }
            sz.Poruchy = ServisniZasah.Recalculcateporuchy(sz.Id);
            return(sz);
        }
        internal protected static string GetCurrencyForprvek(int szp)
        {
            string        currency;
            ServisniZasah sz = new ServisniZasah();

            using (var db = new Model1Container())
            {
                sz = db.ServisniZasah.Where(t => t.Id == szp).FirstOrDefault();
            }
            CenaArtikluZakaznik caz = new CenaArtikluZakaznik();

            caz = CenaArtikluZakaznik.GetCena(215, sz.ZakaznikID);
            if (caz.ZCMena == "")
            {
                currency = caz.ZCMena;
            }
            else
            {
                currency = caz.CenikMena;
            }
            return(currency);
        }
        internal protected static decimal GetCenaForprvek(ServisniZasahPrvek szp)
        {
            decimal       cena;
            ServisniZasah sz = new ServisniZasah();

            using (var db = new Model1Container())
            {
                sz = db.ServisniZasah.Where(t => t.Id == szp.ServisniZasahId).FirstOrDefault();
            }
            CenaArtikluZakaznik caz = new CenaArtikluZakaznik();

            caz = CenaArtikluZakaznik.GetCena(szp.ArtiklID.Value, sz.ZakaznikID);
            if (caz.ZCCena != 0)
            {
                cena = caz.ZCCena;
            }
            else
            {
                cena = caz.CenikCena;
            }
            return(cena);
        }
        internal protected static void UpdateHeader(int Id)
        {
            ServisniZasah sz = new ServisniZasah();

            using (var db = new Model1Container())
            {
                sz = db.ServisniZasah.Where(t => t.Id == Id).FirstOrDefault();
                //načtení ceny za dopravu
                var     km = CenaArtikluZakaznik.GetCena("SP02", sz.ZakaznikID);
                decimal kmcena;
                //pokud není nastavena zvláštní cena tak potom ceníková
                if (km.ZCCena != 0)
                {
                    kmcena = km.ZCCena;
                }
                else
                {
                    kmcena = km.CenikCena;
                }
                //výpočet ceny celkem za dopravu
                sz.CestaCelkem = sz.Km * kmcena;
                //načtení ceny za práci servisních techniků
                var     prace = CenaArtikluZakaznik.GetCena("SP01", sz.ZakaznikID);
                decimal pracecena;
                // pokud není nastavena zvláštní cena tak potom ceníková
                if (prace.ZCCena != 0)
                {
                    pracecena = prace.ZCCena;
                }
                else
                {
                    pracecena = prace.CenikCena;
                }
                sz.PraceSazba = pracecena;
                //výpočet celkové ceny za práci
                sz.PraceCelkem = sz.Pracelidi * sz.PraceSazba * sz.PraceHod;
                //výpočet počtu prvků na servisním zásahu
                var prvku = db.ServisniZasahPrvek.Where(t => t.ServisniZasahId == sz.Id).Count();


                var reklamprvku = db.ServisniZasahPrvek.Where(t => t.Reklamace == true && t.ServisniZasahId == sz.Id).Count();

                var poruseni = db.ServisniZasahPrvek.Where(t => t.Reklamace == true && t.PoruseniZarucnichPodminek == true && t.ServisniZasahId == sz.Id).Count();


                var x = db.ServisniZasahPrvek.Where(t => t.ServisniZasahId == Id)
                        .Where(t => t.Reklamace == true && t.PoruseniZarucnichPodminek == true || t.Reklamace == false)
                        .Select(t => t.CenaCelkem)
                        .Sum();

                if (x == null)
                {
                    x = 0;
                }
                if (sz.Reklamace == false || (sz.Reklamace == true && sz.PoruseniZarucnichPodminek == true))
                {
                    sz.Celkem = sz.CestaCelkem + sz.PraceCelkem + x;
                }
                else
                {
                    sz.Celkem = x;
                }

                sz.Mena = GetCurrencyForprvek(sz.Id);
                try
                {
                    db.Entry(sz).State = EntityState.Modified;
                    db.SaveChanges();
                }
                catch (Exception ex) { log.Error("Error number: " + ex.HResult + " - " + ex.Message + " - " + ex.Data + " - " + ex.InnerException); };
            }
        }
        internal protected static string GenerateDL(int Id)

        {
            ServisniZasah sz = new ServisniZasah();

            sz = ServisniZasah.GetZasah(Id);

            string docEntry = "";
            int    retVal   = -1;

            Company oCompany = new Company();

            oCompany = SAPDIAPI.Connect();

            //Check connection before updating

            if (oCompany.Connected)
            {
                Documents oDraft = (Documents)oCompany.GetBusinessObject(BoObjectTypes.oDrafts);

                oDraft.CardCode   = sz.Zakaznik.KodSAP;
                oDraft.DocDate    = DateTime.Now;
                oDraft.DocDueDate = DateTime.Now;
                oDraft.TaxDate    = DateTime.Now;
                oDraft.VatDate    = DateTime.Now;
                oDraft.UserFields.Fields.Item("U_VCZ_R014").Value  = "SC";
                oDraft.UserFields.Fields.Item("U_VCZ_P343").Value  = "S";
                oDraft.UserFields.Fields.Item("U_VST_Oppor").Value = "100";
                oDraft.DocumentsOwner  = 61;
                oDraft.SalesPersonCode = 47;
                oDraft.DocType         = BoDocumentTypes.dDocument_Items;
                oDraft.DocumentSubType = BoDocumentSubType.bod_None;
                oDraft.DocObjectCode   = BoObjectTypes.oDeliveryNotes;
                oDraft.Project         = sz.Projekt;

                /*
                 * oDelivery.SpecialLines.LineType = BoDocSpecialLineType.dslt_Text;
                 * oDelivery.SpecialLines.LineText = "úvodník";
                 * oDelivery.SpecialLines.Add();
                 */


                foreach (var item in sz.ServisniZasahPrvek.Where(t => t.ArtiklID != null))
                {
                    /*
                     * oDelivery.SpecialLines.LineType = BoDocSpecialLineType.dslt_Text;
                     * // oDelivery.SpecialLines.AfterLineNumber = 0;
                     * oDelivery.SpecialLines.LineText = "Položka";
                     * oDelivery.SpecialLines.Add();
                     */

                    var artikl = Artikl.GetArtiklById(item.ArtiklID.Value);
                    oDraft.Lines.ItemCode          = artikl.KodSAP;
                    oDraft.Lines.Quantity          = Convert.ToDouble(item.Pocet);
                    oDraft.Lines.Price             = Convert.ToDouble(item.CenaZaKus);
                    oDraft.Lines.WarehouseCode     = "Servis";
                    oDraft.Lines.CostingCode       = "OB";
                    oDraft.Lines.COGSCostingCode   = "OB";
                    oDraft.Lines.LineTotal         = Convert.ToDouble(item.CenaCelkem);
                    oDraft.Lines.ProjectCode       = sz.Projekt;
                    oDraft.Lines.UnitsOfMeasurment = 1;
                    oDraft.Lines.Add();
                }
                /*KM*/
                if (sz.CestaCelkem > 0)
                {
                    oDraft.Lines.ItemCode          = "SP02";
                    oDraft.Lines.Quantity          = Convert.ToDouble(sz.Km);
                    oDraft.Lines.Price             = Convert.ToDouble(sz.CestaCelkem / sz.Km);
                    oDraft.Lines.WarehouseCode     = "Servis";
                    oDraft.Lines.CostingCode       = "OB";
                    oDraft.Lines.COGSCostingCode   = "OB";
                    oDraft.Lines.LineTotal         = Convert.ToDouble(sz.CestaCelkem);
                    oDraft.Lines.ProjectCode       = sz.Projekt;
                    oDraft.Lines.UnitsOfMeasurment = 1;
                    oDraft.Lines.Add();
                }
                /*PRACE*/
                if (sz.PraceCelkem > 0)
                {
                    oDraft.Lines.ItemCode          = "SP01";
                    oDraft.Lines.Quantity          = Convert.ToDouble(sz.PraceHod * sz.Pracelidi);
                    oDraft.Lines.Price             = Convert.ToDouble(sz.PraceHod);
                    oDraft.Lines.WarehouseCode     = "Servis";
                    oDraft.Lines.CostingCode       = "OB";
                    oDraft.Lines.COGSCostingCode   = "OB";
                    oDraft.Lines.LineTotal         = Convert.ToDouble(sz.PraceCelkem);
                    oDraft.Lines.ProjectCode       = sz.Projekt;
                    oDraft.Lines.UnitsOfMeasurment = 1;
                    oDraft.Lines.Add();
                }



                try
                {
                    retVal = oDraft.Add();
                }
                catch (Exception ex)
                {
                    log.Error("Error number: " + ex.HResult + " - " + ex.Message + " - " + ex.Data + " - " + ex.InnerException);
                }
                if (retVal == 0)
                {
                    oCompany.GetNewObjectCode(out docEntry);
                }


                var x = oCompany.GetLastErrorCode();
                var y = oCompany.GetLastErrorDescription();


                if (retVal == 0)
                {
                    oCompany.GetNewObjectCode(out docEntry);
                }

                Documents oDraft2 = (Documents)oCompany.GetBusinessObject(BoObjectTypes.oDrafts);
                oDraft2.GetByKey(Convert.ToInt32(docEntry));
                int iLine = 0;

                while (iLine < oDraft2.Lines.Count)

                {
                    int iLine2 = 0;
                    oDraft2.Lines.SetCurrentLine(iLine);
                    while (iLine2 < oDraft2.Lines.Count)
                    {
                        if (oDraft2.Lines.LineNum == System.Convert.ToInt32(iLine2))

                        {
                            oDraft2.Lines.SetCurrentLine(iLine2);

                            log.Debug(oDraft2.Lines.LineNum + " - " + oDraft2.Lines.ItemCode + " - " + oDraft2.Lines.CostingCode + " - " + oDraft2.Lines.ProjectCode);
                            //oDraft2.Lines. = iLine2;
                            oDraft2.Lines.CostingCode     = "OB";
                            oDraft2.Lines.COGSCostingCode = "OB";
                            oDraft2.Lines.ProjectCode     = sz.Projekt;
                            log.Debug(oDraft2.Lines.LineNum + " - " + oDraft2.Lines.ItemCode + " - " + oDraft2.Lines.CostingCode + " - " + oDraft2.Lines.ProjectCode);
                        }
                        iLine2++;
                    }

                    iLine++;
                }

                try
                {
                    retVal = oDraft2.Update();
                }
                catch (Exception ex)
                {
                    log.Error("Error number: " + ex.HResult + " - " + ex.Message + " - " + ex.Data + " - " + ex.InnerException);
                }
                if (retVal == 0)
                {
                    oCompany.GetNewObjectCode(out docEntry);
                }

                x = oCompany.GetLastErrorCode();
                y = oCompany.GetLastErrorDescription();


                //oCompany.GetLastError(out ErrCode, out ErrMsg);
            }
            else
            {
                log.Error("else on connected to sap");
            }

            oCompany.Disconnect();

            return(docEntry);
        }
        internal protected static string GenerateOrder(int Id)

        {
            ServisniZasah sz = new ServisniZasah();

            sz = ServisniZasah.GetZasah(Id);

            //bool bRetVal = false;
            string  docEntry     = "";
            int     retVal       = -1;
            string  QuotDocEntry = sz.NabidkaDocNum;
            Company oCompany     = new Company();

            oCompany = SAPDIAPI.Connect();

            // Get the quotation
            Documents oQuotation = (Documents)oCompany.GetBusinessObject(BoObjectTypes.oQuotations);

            oQuotation.GetByKey(Convert.ToInt32(sz.Nabidka));

            //Check connection before updating

            if (oCompany.Connected)

            {
                Documents oDelivery = (Documents)oCompany.GetBusinessObject(BoObjectTypes.oOrders);
                oDelivery.CardCode        = oQuotation.CardCode;
                oDelivery.DocDate         = DateTime.Now;
                oDelivery.DocDueDate      = DateTime.Now;
                oDelivery.TaxDate         = DateTime.Now;
                oDelivery.VatDate         = DateTime.Now;
                oDelivery.DocumentsOwner  = 61;
                oDelivery.SalesPersonCode = 47;
                oDelivery.DocType         = BoDocumentTypes.dDocument_Items;
                oDelivery.DocumentSubType = BoDocumentSubType.bod_None;
                oDelivery.DocObjectCode   = BoObjectTypes.oOrders;
                oDelivery.Project         = sz.Projekt;

                for (int i = 0; i < oQuotation.Lines.Count; i++)

                {
                    oQuotation.Lines.SetCurrentLine(i);
                    oDelivery.Lines.BaseEntry       = oQuotation.DocEntry;
                    oDelivery.Lines.BaseLine        = oQuotation.Lines.LineNum;
                    oDelivery.Lines.BaseType        = 23;
                    oDelivery.Lines.ItemCode        = oQuotation.Lines.ItemCode;
                    oDelivery.Lines.Quantity        = oQuotation.Lines.Quantity;
                    oDelivery.Lines.Price           = oQuotation.Lines.Price;
                    oDelivery.Lines.WarehouseCode   = oQuotation.Lines.WarehouseCode;
                    oDelivery.Lines.CostingCode     = "OB";
                    oDelivery.Lines.COGSCostingCode = "OB";
                    //oDelivery.Lines.Currency = sz.Mena;
                    oDelivery.Lines.LineTotal   = oQuotation.Lines.LineTotal;
                    oDelivery.Lines.ProjectCode = sz.Projekt;
                    //oDelivery.Lines.Rate = 1;
                    oDelivery.Lines.UnitsOfMeasurment = oQuotation.Lines.UnitsOfMeasurment;
                    //oDelivery.Lines.TaxCode = "E21T";
                    oDelivery.Lines.Add();
                }


                try
                {
                    retVal = oDelivery.Add();
                }
                catch (Exception ex) { log.Error("Error number: " + ex.HResult + " - " + ex.Message + " - " + ex.Data + " - " + ex.InnerException); }

                if (retVal == 0)
                {
                    oCompany.GetNewObjectCode(out docEntry);
                }



                var x = oCompany.GetLastErrorCode();
                var y = oCompany.GetLastErrorDescription();
                //oCompany.GetLastError(out ErrCode, out ErrMsg);
            }

            oCompany.Disconnect();

            return(docEntry);
        }