Ejemplo n.º 1
0
        //Алгоритм:
        //1. INSERT в "DocSecondHandReturns"
        //2. UPDATE Status в "DocSecondHandPurches"

        internal async Task <DocSecondHandReturn> mPutPostDocSecondHandReturn(
            DbConnectionSklad db,
            DbConnectionSklad dbRead,
            string UO_Action,
            //DbConnectionSklad dbRead,
            //Models.Sklad.Sys.SysSetting sysSetting,
            DocSecondHandReturn docSecondHandReturn,
            //bool InsertUpdate, //true - Insert, false - Update
            EntityState entityState,          //EntityState.Added, Modified
                                              //Models.Sklad.Doc.DocSecondHandReturnTab[] docSecondHandReturnTabCollection,

            Classes.Account.Login.Field field //Дополнительные данные о сотруднике
            )
        {
            #region Проверка статуса аппарата, только если:
            //DirSecondHandStatusID == 9
            //DirSecondHandStatusID_789 == 7

            Models.Sklad.Doc.DocSecondHandPurch docSecondHandPurch = await db.DocSecondHandPurches.FindAsync(docSecondHandReturn.DocSecondHandPurchID);

            if (docSecondHandPurch.DirSecondHandStatusID != 10 || docSecondHandPurch.DirSecondHandStatusID_789 != 7)
            {
                throw new System.InvalidOperationException("Аппарат не готов к продаже или уже возвращён!");
            }

            #endregion

            #region Меняем статус аппарата

            docSecondHandPurch.DirSecondHandStatusID = 9;
            docSecondHandPurch.DirReturnTypeID       = docSecondHandReturn.DirReturnTypeID;
            docSecondHandPurch.DirDescriptionID      = docSecondHandReturn.DirDescriptionID;

            db.Entry(docSecondHandPurch).State = EntityState.Modified;
            await db.SaveChangesAsync();

            #endregion


            #region 1. Doc

            //Модель
            Models.Sklad.Doc.Doc doc = new Models.Sklad.Doc.Doc();
            //Присваиваем значения
            doc.ListObjectID     = ListObjectID;
            doc.IsImport         = false;
            doc.NumberInt        = docSecondHandReturn.NumberInt;
            doc.NumberReal       = docSecondHandReturn.DocSecondHandReturnID;
            doc.DirEmployeeID    = field.DirEmployeeID;
            doc.DirPaymentTypeID = docSecondHandReturn.DirPaymentTypeID;
            doc.Payment          = docSecondHandReturn.Payment;
            if (docSecondHandReturn.DirContractorID != null)
            {
                doc.DirContractorID = Convert.ToInt32(docSecondHandReturn.DirContractorID);
            }
            else
            {
                doc.DirContractorID = docSecondHandReturn.DirContractorIDOrg;
            }
            doc.DirContractorIDOrg = docSecondHandReturn.DirContractorIDOrg;
            doc.Discount           = docSecondHandReturn.Discount;
            doc.DirVatValue        = docSecondHandReturn.DirVatValue;
            doc.Base        = docSecondHandReturn.Base;
            doc.Description = docSecondHandReturn.Description;
            doc.DocDate     = DateTime.Now; //docSecondHandReturn.DocDate;
                                            //doc.DocDisc = docSecondHandReturn.DocDisc;
            if (UO_Action == "held")
            {
                doc.Held = true;
            }
            else
            {
                doc.Held = false;
            }
            doc.DocID           = docSecondHandReturn.DocID;
            doc.DocIDBase       = docSecondHandReturn.DocIDBase;
            doc.KKMSCheckNumber = docSecondHandReturn.KKMSCheckNumber;
            doc.KKMSIdCommand   = docSecondHandReturn.KKMSIdCommand;
            doc.KKMSEMail       = docSecondHandReturn.KKMSEMail;
            doc.KKMSPhone       = docSecondHandReturn.KKMSPhone;

            //Класс
            Docs.Docs docs = new Docs.Docs(db, dbRead, doc, entityState);
            //doc = await docs.Save();
            await Task.Run(() => docs.Save());

            //Нужно вернуть "docSecondHandReturn" со всем полями!
            docSecondHandReturn.DocID = doc.DocID;

            #endregion

            #region 2. DocSecondHandReturn

            docSecondHandReturn.DocID = doc.DocID;

            db.Entry(docSecondHandReturn).State = entityState;
            await db.SaveChangesAsync();

            #region 2.1. UpdateNumberInt, если INSERT

            if (entityState == EntityState.Added && (docSecondHandReturn.doc.NumberInt == null || docSecondHandReturn.doc.NumberInt.Length == 0))
            {
                doc.NumberInt  = docSecondHandReturn.DocSecondHandReturnID.ToString();
                doc.NumberReal = docSecondHandReturn.DocSecondHandReturnID;
                docs           = new Docs.Docs(db, dbRead, doc, EntityState.Modified);
                await Task.Run(() => docs.Save());
            }
            else if (entityState == EntityState.Added)
            {
                doc.NumberReal = docSecondHandReturn.DocSecondHandReturnID;
                docs           = new Docs.Docs(db, dbRead, doc, EntityState.Modified);
                await Task.Run(() => docs.Save());
            }

            #endregion


            #endregion


            #region Касса или Банк


            #region 1. Получаем валюту из склада

            int    DirCurrencyID = 0, DirCurrencyMultiplicity = 0; //, DirCashOfficeID = 0, DirBankID = 0;;
            double DirCurrencyRate = 0;

            var query = await Task.Run(() =>
                                       (
                                           from x in db.DirWarehouses
                                           where x.DirWarehouseID == docSecondHandReturn.DirWarehouseID
                                           select new
            {
                //DirCashOfficeID= x.dirCashOffice.DirCashOfficeID,
                DirCurrencyID_Bank = x.dirBank.DirCurrencyID,
                DirCurrencyRate_Bank = x.dirBank.dirCurrency.DirCurrencyRate,
                DirCurrencyMultiplicity_Bank = x.dirBank.dirCurrency.DirCurrencyMultiplicity,

                //DirBankID = x.dirBank.DirBankID,
                DirCurrencyID_Cash = x.dirCashOffice.DirCurrencyID,
                DirCurrencyRate_Cash = x.dirCashOffice.dirCurrency.DirCurrencyRate,
                DirCurrencyMultiplicity_Cash = x.dirCashOffice.dirCurrency.DirCurrencyMultiplicity,
            }
                                       ).ToListAsync());

            if (query.Count() > 0)
            {
                if (doc.DirPaymentTypeID == 1)
                {
                    //DirCashOfficeID = Convert.ToInt32(query[0].DirCashOfficeID);
                    DirCurrencyID           = query[0].DirCurrencyID_Cash;
                    DirCurrencyRate         = query[0].DirCurrencyRate_Cash;
                    DirCurrencyMultiplicity = query[0].DirCurrencyMultiplicity_Cash;
                }
                else if (doc.DirPaymentTypeID == 2)
                {
                    //DirBankID = Convert.ToInt32(query[0].DirBankID);
                    DirCurrencyID           = query[0].DirCurrencyID_Bank;
                    DirCurrencyRate         = query[0].DirCurrencyRate_Bank;
                    DirCurrencyMultiplicity = query[0].DirCurrencyMultiplicity_Bank;
                }
                else
                {
                    throw new System.InvalidOperationException("Не выбран метод оплаты: Касса или Банк!");
                }
            }

            #endregion


            #region 2. Заполняем Модель

            Models.Sklad.Pay.Pay pay = new Models.Sklad.Pay.Pay();
            //pay.DirCashOfficeID = Convert.ToInt32(DirCashOfficeID);
            //pay.DirBankID = Convert.ToInt32(DirBankID);
            //Валюта
            pay.DirCurrencyID           = DirCurrencyID;
            pay.DirCurrencyRate         = DirCurrencyRate;
            pay.DirCurrencyMultiplicity = DirCurrencyMultiplicity;

            pay.DirEmployeeID    = field.DirEmployeeID;
            pay.DirPaymentTypeID = doc.DirPaymentTypeID;
            //pay.DirXName = ""; //no
            //pay.DirXSumTypeID = 0; //no
            pay.DocCashBankID = null;
            pay.DocID         = doc.DocID;
            pay.DocXID        = docSecondHandReturn.DocSecondHandReturnID;
            pay.DocXSumDate   = doc.DocDate;
            pay.DocXSumSum    = docSecondHandReturn.PriceCurrency - doc.Discount; // - получили при сохранении Спецификации (выше) docSecondHandReturn.PriceVAT

            //DocSecondHandPurchID - найти!!!
            pay.Base = "Возврат документа №" + docSecondHandPurch.DocSecondHandPurchID; //pay.Base = "Оплата за коды товаров: " + NomenName; // - получили при сохранении Спецификации (выше)

            //pay.Description = "";
            pay.KKMSCheckNumber = docSecondHandReturn.KKMSCheckNumber;
            pay.KKMSIdCommand   = docSecondHandReturn.KKMSIdCommand;
            pay.KKMSEMail       = docSecondHandReturn.KKMSEMail;
            pay.KKMSPhone       = docSecondHandReturn.KKMSPhone;

            pay.Discount = doc.Discount;

            #endregion


            #region 3. Сохраняем

            PartionnyAccount.Controllers.Sklad.Pay.PayController payController = new Pay.PayController();
            doc = await Task.Run(() => payController.mPutPostPay(db, pay, EntityState.Modified, field)); //sysSetting

            #endregion


            #endregion


            #region 4. Log

            logService.DocSecondHandPurchID   = docSecondHandPurch.DocSecondHandPurchID;
            logService.DirSecondHandLogTypeID = 14;
            logService.DirEmployeeID          = field.DirEmployeeID;
            logService.DirSecondHandStatusID  = docSecondHandPurch.DirSecondHandStatusID;
            logService.DirWarehouseIDFrom     = docSecondHandPurch.DirWarehouseID;
            logService.DirWarehouseIDTo       = docSecondHandPurch.DirWarehouseID;
            //logService.Msg = "Аппарат принят на точку №" + docSecondHandPurch.DirWarehouseID;

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

            #endregion


            return(docSecondHandReturn);
        }
Ejemplo n.º 2
0
        internal async Task <DocSalary> mPutPostDocSalary(
            DbConnectionSklad db,
            DbConnectionSklad dbRead,
            string UO_Action,
            //DbConnectionSklad dbRead,
            //Models.Sklad.Sys.SysSetting sysSetting,
            DocSalary docSalary,
            //bool InsertUpdate, //true - Insert, false - Update
            EntityState entityState, //EntityState.Added, Modified
            Models.Sklad.Doc.DocSalaryTab[] docSalaryTabCollection,

            Classes.Account.Login.Field field //Дополнительные данные о сотруднике
            )
        {
            if (UO_Action == "save" || UO_Action == "save_close" || UO_Action == "held")
            {
                #region 1. Doc

                //Модель
                Models.Sklad.Doc.Doc doc = new Models.Sklad.Doc.Doc();
                //Присваиваем значения
                doc.ListObjectID  = ListObjectID;
                doc.IsImport      = false;
                doc.NumberInt     = docSalary.NumberInt;
                doc.NumberReal    = docSalary.DocSalaryID;
                doc.DirEmployeeID = field.DirEmployeeID;
                //doc.DirPaymentTypeID = docSalary.DirPaymentTypeID;
                doc.Payment            = 0; //docSalary.Payment;
                doc.DirContractorID    = docSalary.DirContractorIDOrg;
                doc.DirContractorIDOrg = docSalary.DirContractorIDOrg;
                doc.Discount           = docSalary.Discount;
                doc.DirVatValue        = 0;
                doc.Base        = docSalary.Base;
                doc.Description = docSalary.Description;
                doc.DocDate     = docSalary.DocDate;
                //doc.DocDisc = docSalary.DocDisc;
                if (UO_Action == "held")
                {
                    doc.Held = true;
                }
                else
                {
                    doc.Held = false;
                }
                doc.DocID     = docSalary.DocID;
                doc.DocIDBase = docSalary.DocIDBase;

                //Класс
                Docs.Docs docs = new Docs.Docs(db, dbRead, doc, entityState);
                //doc = await docs.Save();
                await Task.Run(() => docs.Save());

                //Нужно вернуть "docSalary" со всем полями!
                docSalary.DocID = doc.DocID;

                #endregion

                #region 2. DocSalary

                docSalary.DocID = doc.DocID;

                db.Entry(docSalary).State = entityState;
                await db.SaveChangesAsync();

                #region 2.1. UpdateNumberInt, если INSERT

                if (entityState == EntityState.Added && (docSalary.doc.NumberInt == null || docSalary.doc.NumberInt.Length == 0))
                {
                    doc.NumberInt  = docSalary.DocSalaryID.ToString();
                    doc.NumberReal = docSalary.DocSalaryID;
                    docs           = new Docs.Docs(db, dbRead, doc, EntityState.Modified);
                    await Task.Run(() => docs.Save());
                }
                else if (entityState == EntityState.Added)
                {
                    doc.NumberReal = docSalary.DocSalaryID;
                    docs           = new Docs.Docs(db, dbRead, doc, EntityState.Modified);
                    await Task.Run(() => docs.Save());
                }

                #endregion


                #endregion

                #region 3. DocSalaryTab

                //2.1. Удаляем записи в БД, если UPDATE
                if (entityState == EntityState.Modified)
                {
                    SQLiteParameter parDocSalaryID = new SQLiteParameter("@DocSalaryID", System.Data.DbType.Int32)
                    {
                        Value = docSalary.DocSalaryID
                    };
                    db.Database.ExecuteSqlCommand("DELETE FROM DocSalaryTabs WHERE DocSalaryID=@DocSalaryID;", parDocSalaryID);
                }

                //2.2. Проставляем ID-шник "DocSalaryID" для всех позиций спецификации
                for (int i = 0; i < docSalaryTabCollection.Count(); i++)
                {
                    docSalaryTabCollection[i].DocSalaryTabID  = null;
                    docSalaryTabCollection[i].DocSalaryID     = Convert.ToInt32(docSalary.DocSalaryID);
                    db.Entry(docSalaryTabCollection[i]).State = EntityState.Added;
                }
                await db.SaveChangesAsync();

                #endregion
            }

            #region n. Подтверждение транзакции *** *** *** *** *** *

            //ts.Commit(); //.Complete();

            #endregion


            return(docSalary);
        }