public async Task <IHttpActionResult> PostDocServicePurch1Tab(DocServicePurch1Tab docServicePurch1Tab, HttpRequestMessage request)
        {
            try
            {
                #region Проверяем Логин и Пароль + Изменяем строку соединения + Права + Разные Функции

                //Получаем Куку
                System.Web.HttpCookie authCookie = System.Web.HttpContext.Current.Request.Cookies["CookieIPOL"];

                // Проверяем Логин и Пароль
                Classes.Account.Login.Field field = await Task.Run(() => login.Return(authCookie, true));

                if (!field.Access)
                {
                    return(Ok(returnServer.Return(false, Classes.Language.Sklad.Language.msg10)));
                }

                //Изменяем строку соединения
                db = new DbConnectionSklad(connectionString.Return(field.DirCustomersID, null, true));

                //Права (1 - Write, 2 - Read, 3 - No Access)
                int iRight = await Task.Run(() => accessRight.Access(connectionString.Return(field.DirCustomersID, null, true), field.DirEmployeeID, "RightDocServicePurch1Tabs"));

                if (iRight != 1)
                {
                    return(Ok(returnServer.Return(false, Classes.Language.Sklad.Language.msg57(0))));
                }


                //Разные Функции
                function.NumberDecimalSeparator();

                //Получам настройки
                Models.Sklad.Sys.SysSetting sysSetting = await db.SysSettings.FindAsync(1);

                #endregion

                #region Параметры

                var    paramList           = request.GetQueryNameValuePairs();
                int    iDirServiceStatusID = Convert.ToInt32(paramList.FirstOrDefault(kv => string.Compare(kv.Key, "DirServiceStatusID", true) == 0).Value);
                string sDiagnosticRresults = paramList.FirstOrDefault(kv => string.Compare(kv.Key, "sDiagnosticRresults", true) == 0).Value;

                //Получаем колекцию "Спецификации"
                Models.Sklad.Doc.DocServicePurch1Tab[] DocServicePurch1TabCollection = null;
                if (!String.IsNullOrEmpty(docServicePurch1Tab.recordsDataX))
                {
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    DocServicePurch1TabCollection = serializer.Deserialize <Models.Sklad.Doc.DocServicePurch1Tab[]>("[" + docServicePurch1Tab.recordsDataX + "]");
                }

                if (DocServicePurch1TabCollection.Length > 0)
                {
                    docServicePurch1Tab          = DocServicePurch1TabCollection[0];
                    docServicePurch1Tab.PriceVAT = docServicePurch1Tab.PriceCurrency;
                    if (docServicePurch1Tab.DirServiceJobNomenID == 0)
                    {
                        docServicePurch1Tab.DirServiceJobNomenID = null;
                    }
                    if (docServicePurch1Tab.DocServicePurch1TabID == null || docServicePurch1Tab.DocServicePurch1TabID < 1)
                    {
                        docServicePurch1Tab.DocServicePurch1TabID   = null;
                        docServicePurch1Tab.DirEmployeeID           = field.DirEmployeeID;
                        docServicePurch1Tab.DirCurrencyID           = sysSetting.DirCurrencyID;
                        docServicePurch1Tab.DirCurrencyRate         = 1;
                        docServicePurch1Tab.DirCurrencyMultiplicity = 1;

                        if (docServicePurch1Tab.DirServiceJobNomenID == null)
                        {
                            bool bRight = await Task.Run(() => accessRight.AccessCheck(connectionString.Return(field.DirCustomersID, null, true), field.DirEmployeeID, "RightDocServiceWorkshopsTab1AddCheck"));

                            if (!bRight)
                            {
                                return(Ok(returnServer.Return(false, Classes.Language.Sklad.Language.msg57(0))));
                            }
                        }
                    }
                }

                //Проверка точки === === ===
                Models.Sklad.Doc.DocServicePurch docServicePurch = await db.DocServicePurches.FindAsync(docServicePurch1Tab.DocServicePurchID);

                if (field.DirEmployeeID != 1)
                {
                    int?DirWarehouseID = docServicePurch.DirWarehouseID;
                    var query          = await
                                         (
                        from x in db.DirEmployeeWarehouse
                        where x.DirEmployeeID == field.DirEmployeeID && x.DirWarehouseID == DirWarehouseID
                        select x
                                         ).ToListAsync();
                    if (query.Count() == 0)
                    {
                        return(Ok(returnServer.Return(false, "У Вас нет доступа к этой точке!")));
                    }
                }

                #endregion


                #region Сохранение


                using (System.Data.Entity.DbContextTransaction ts = db.Database.BeginTransaction())
                {
                    db.Configuration.AutoDetectChangesEnabled = false;

                    try
                    {
                        #region Save docServicePurch

                        //Models.Sklad.Doc.DocServicePurch docServicePurch = await db.DocServicePurches.FindAsync(docServicePurch1Tab.DocServicePurchID);
                        int?DirServiceStatusIDOLD = docServicePurch.DirServiceStatusID;
                        docServicePurch.DirServiceStatusID = iDirServiceStatusID;
                        db.Entry(docServicePurch).State    = EntityState.Modified;


                        #region Лог - если поменялся статус

                        if (DirServiceStatusIDOLD != iDirServiceStatusID)
                        {
                            //Пишем в Лог о смене статуса и мастера, если такое было
                            logService.DocServicePurchID   = docServicePurch1Tab.DocServicePurchID;
                            logService.DirServiceLogTypeID = 1;
                            logService.DirEmployeeID       = field.DirEmployeeID;
                            logService.DirServiceStatusID  = iDirServiceStatusID;
                            if (!String.IsNullOrEmpty(sDiagnosticRresults))
                            {
                                logService.Msg = sDiagnosticRresults;
                            }

                            await logServicesController.mPutPostLogServices(db, logService, EntityState.Added);
                        }

                        #endregion

                        #endregion


                        #region Save docServicePurch1Tab


                        #region Лог

                        //Пишем в Лог о смене статуса и мастера, если такое было
                        logService.DocServicePurchID   = docServicePurch1Tab.DocServicePurchID;
                        logService.DirServiceLogTypeID = 5;
                        logService.DirEmployeeID       = field.DirEmployeeID;
                        logService.DirServiceStatusID  = iDirServiceStatusID;

                        if (docServicePurch1Tab.DocServicePurch1TabID == null)
                        {
                            logService.Msg = "Создание записи " + docServicePurch1Tab.DirServiceJobNomenName + " на сумму " + docServicePurch1Tab.PriceCurrency;
                        }
                        else
                        {
                            logService.Msg = "Изменение записи " + docServicePurch1Tab.DirServiceJobNomenName + " на сумму " + docServicePurch1Tab.PriceCurrency;
                        }
                        if (!String.IsNullOrEmpty(sDiagnosticRresults))
                        {
                            logService.Msg += "<br /> Результат Диагностики: " + sDiagnosticRresults;
                        }

                        await logServicesController.mPutPostLogServices(db, logService, EntityState.Added);

                        #endregion

                        docServicePurch1Tab.DirServiceStatusID = iDirServiceStatusID;

                        if (docServicePurch1Tab.DocServicePurch1TabID > 0)
                        {
                            db.Entry(docServicePurch1Tab).State = EntityState.Modified;
                        }
                        else
                        {
                            docServicePurch1Tab.DiagnosticRresults = sDiagnosticRresults;
                            docServicePurch1Tab.TabDate            = DateTime.Now;

                            db.Entry(docServicePurch1Tab).State = EntityState.Added;
                        }
                        await Task.Run(() => db.SaveChangesAsync());

                        #endregion


                        ts.Commit();
                    }
                    catch (Exception ex)
                    {
                        try { ts.Rollback(); ts.Dispose(); } catch { }
                        try { db.Database.Connection.Close(); db.Database.Connection.Dispose(); } catch { }

                        return(Ok(returnServer.Return(false, exceptionEntry.Return(ex))));
                    }
                }



                dynamic collectionWrapper = new
                {
                    DocServicePurch1TabID   = docServicePurch1Tab.DocServicePurch1TabID,
                    DirEmployeeID           = docServicePurch1Tab.DirEmployeeID,
                    DirCurrencyID           = docServicePurch1Tab.DirCurrencyID,
                    DirCurrencyRate         = docServicePurch1Tab.DirCurrencyRate,
                    DirCurrencyMultiplicity = docServicePurch1Tab.DirCurrencyMultiplicity
                };
                return(Ok(returnServer.Return(true, collectionWrapper))); //return Ok(returnServer.Return(true, ""));

                #endregion
            }
            catch (Exception ex)
            {
                return(Ok(returnServer.Return(false, exceptionEntry.Return(ex))));
            }
        }
        public async Task <IHttpActionResult> DeleteDocServicePurch1Tab(int id, HttpRequestMessage request)
        {
            #region Проверяем Логин и Пароль + Изменяем строку соединения + Права + Разные Функции

            //Получаем Куку
            System.Web.HttpCookie authCookie = System.Web.HttpContext.Current.Request.Cookies["CookieIPOL"];

            // Проверяем Логин и Пароль
            Classes.Account.Login.Field field = await Task.Run(() => login.Return(authCookie, true));

            if (!field.Access)
            {
                return(Ok(returnServer.Return(false, Classes.Language.Sklad.Language.msg10)));
            }

            //Изменяем строку соединения
            db = new DbConnectionSklad(connectionString.Return(field.DirCustomersID, null, true));

            //Права (1 - Write, 2 - Read, 3 - No Access)
            int iRight = await Task.Run(() => accessRight.Access(connectionString.Return(field.DirCustomersID, null, true), field.DirEmployeeID, "RightDocServicePurch1Tabs"));

            if (iRight != 1)
            {
                return(Ok(returnServer.Return(false, Classes.Language.Sklad.Language.msg57(0))));
            }

            //Разные Функции
            function.NumberDecimalSeparator();

            //Получам настройки
            Models.Sklad.Sys.SysSetting sysSetting = await db.SysSettings.FindAsync(1);

            #endregion

            #region Параметры

            var    paramList           = request.GetQueryNameValuePairs();
            int    iDirServiceStatusID = Convert.ToInt32(paramList.FirstOrDefault(kv => string.Compare(kv.Key, "DirServiceStatusID", true) == 0).Value);
            string sDiagnosticRresults = paramList.FirstOrDefault(kv => string.Compare(kv.Key, "sDiagnosticRresults", true) == 0).Value;

            #endregion


            #region Удаление

            try
            {
                DocServicePurch1Tab docServicePurch1Tab = await db.DocServicePurch1Tabs.FindAsync(id);

                if (docServicePurch1Tab == null)
                {
                    //return Ok(returnServer.Return(false, Classes.Language.Sklad.Language.msg99));

                    dynamic collectionWrapper2 = new
                    {
                        ID  = 0,
                        Msg = Classes.Language.Sklad.Language.msg99
                    };
                    return(Ok(returnServer.Return(true, collectionWrapper2)));
                }

                //Проверка точки === === ===
                Models.Sklad.Doc.DocServicePurch docServicePurch = await db.DocServicePurches.FindAsync(docServicePurch1Tab.DocServicePurchID);

                if (field.DirEmployeeID != 1)
                {
                    int?DirWarehouseID = docServicePurch.DirWarehouseID;
                    var query          = await
                                         (
                        from x in db.DirEmployeeWarehouse
                        where x.DirEmployeeID == field.DirEmployeeID && x.DirWarehouseID == DirWarehouseID
                        select x
                                         ).ToListAsync();
                    if (query.Count() == 0)
                    {
                        return(Ok(returnServer.Return(false, "У Вас нет доступа к этой точке!")));
                    }
                }


                using (System.Data.Entity.DbContextTransaction ts = db.Database.BeginTransaction())
                {
                    db.Configuration.AutoDetectChangesEnabled = false;

                    try
                    {
                        #region Лог

                        //Пишем в Лог о смене статуса и мастера, если такое было
                        logService.DocServicePurchID   = docServicePurch1Tab.DocServicePurchID;
                        logService.DirServiceLogTypeID = 5;
                        logService.DirEmployeeID       = field.DirEmployeeID;
                        logService.DirServiceStatusID  = null;
                        logService.Msg  = "Удаление записи " + docServicePurch1Tab.DirServiceJobNomenName + " на сумму " + docServicePurch1Tab.PriceCurrency;
                        logService.Msg += "<br />Причина удаление: " + sDiagnosticRresults;

                        await logServicesController.mPutPostLogServices(db, logService, EntityState.Added);

                        #endregion

                        #region Save

                        db.DocServicePurch1Tabs.Remove(docServicePurch1Tab);
                        await db.SaveChangesAsync();

                        #endregion

                        ts.Commit();
                    }
                    catch (Exception ex)
                    {
                        try { ts.Rollback(); ts.Dispose(); } catch { }
                        try { db.Database.Connection.Close(); db.Database.Connection.Dispose(); } catch { }

                        return(Ok(returnServer.Return(false, exceptionEntry.Return(ex))));
                    }
                }


                dynamic collectionWrapper = new
                {
                    ID  = docServicePurch1Tab.DocServicePurch1TabID,
                    Msg = Classes.Language.Sklad.Language.msg19
                };
                return(Ok(returnServer.Return(true, collectionWrapper))); //return Ok(returnServer.Return(true, "")
            }
            catch (Exception ex)
            {
                return(Ok(returnServer.Return(false, exceptionEntry.Return(ex))));
            }

            #endregion
        }
 public async Task <IHttpActionResult> PutDocServicePurch1Tab(int id, DocServicePurch1Tab docServicePurch1Tab)
 {
     return(Ok(returnServer.Return(false, Classes.Language.Sklad.Language.msg57(0))));
 }