public override bool execute( )
        {
            if (base.execute() == false)
            {
                return(false);
            }

            Registry("execute exec_cancel_Loja ");

            /// USER [ execute ]

            T_Loja loj = new T_Loja(this);

            loj.ExclusiveAccess();

            if (!loj.select_rows_cnpj(input_st_cnpj))
            {
                PublishError("Loja não disponível");
                return(false);
            }

            if (!loj.fetch())
            {
                return(false);
            }

            if (loj.get_tg_cancel() == Context.TRUE)
            {
                PublishError("Loja previamente cancelada");
                return(false);
            }

            st_cod_loja = loj.get_st_loja();

            loj.set_tg_cancel(Context.TRUE);

            if (!loj.synchronize_T_Loja())
            {
                return(false);
            }

            PublishNote("Loja cancelada com sucesso");

            /// USER [ execute ] END

            Registry("execute done exec_cancel_Loja ");

            return(true);
        }
Beispiel #2
0
        public override bool execute( )
        {
            if (base.execute() == false)
            {
                return(false);
            }

            Registry("execute schedule_faturamento ");

            /// USER [ execute ]

            T_Empresa emp   = new T_Empresa(this);
            DateTime  dtFat = DateTime.Now.AddDays(-1);

            #region - ## Busca todas as empresas -

            if (emp.selectAll())
            {
                // ## Busco todos os registros

                while (emp.fetch())
                {
                    // ## Se dia é hoje

                    if (emp.get_int_nu_periodoFat() == dtFat.Day &&
                        emp.get_tg_isentoFat() == Context.FALSE)
                    {
                        T_Faturamento fat     = new T_Faturamento(this);
                        T_Faturamento fat_upd = new T_Faturamento(this);

                        DateTime aux = dtFat;

                        // ## Para casos onde o mês deve ser o próximo

                        if (emp.get_int_nu_diaVenc() < dtFat.Day)
                        {
                            aux = dtFat.AddMonths(1);
                        }

                        DateTime dtVenc = new DateTime(aux.Year,
                                                       aux.Month,
                                                       (int)emp.get_int_nu_diaVenc());

                        fat.set_dt_vencimento(GetDataBaseTime(dtVenc));
                        fat.set_fk_empresa(emp.get_identity());
                        fat.set_tg_situacao(TipoSitFat.Pendente);

                        // ## Crio registro

                        fat.create_T_Faturamento();

                        long vr_total_fat = 0;

                        // ## Crio registro detalhe

                        #region - 1 - mensalidade -

                        {
                            T_FaturamentoDetalhes fat_det = new T_FaturamentoDetalhes(this);

                            fat_det.set_fk_fatura(fat.get_identity());
                            fat_det.set_vr_cobranca(emp.get_vr_mensalidade());
                            fat_det.set_tg_tipoFat(TipoFat.TBM);
                            fat_det.set_tg_desconto(Context.FALSE);
                            fat_det.set_fk_empresa(emp.get_identity());
                            fat_det.set_fk_loja(Context.FALSE);

                            vr_total_fat += fat_det.get_int_vr_cobranca();

                            fat_det.create_T_FaturamentoDetalhes();
                        }

                        #endregion

                        #region - 2 - valor por transações -

                        if (emp.get_int_vr_transacao() > 0)
                        {
                            LOG_Transacoes l_tr = new LOG_Transacoes(this);

                            l_tr.SetCountMode();

                            l_tr.select_rows_dt_emp(GetDataBaseTime(dtFat.AddMonths(-1)),
                                                    GetDataBaseTime(dtFat),
                                                    emp.get_identity());

                            long trans = l_tr.GetCount() - emp.get_int_nu_franquia();

                            if (trans > 0)
                            {
                                T_FaturamentoDetalhes fat_det = new T_FaturamentoDetalhes(this);

                                fat_det.set_fk_fatura(fat.get_identity());
                                fat_det.set_vr_cobranca((trans * emp.get_int_vr_transacao()).ToString());
                                fat_det.set_tg_tipoFat(TipoFat.FixoTrans);
                                fat_det.set_nu_quantidade(l_tr.GetCount().ToString());
                                fat_det.set_tg_desconto(Context.FALSE);
                                fat_det.set_fk_empresa(emp.get_identity());
                                fat_det.set_fk_loja(Context.FALSE);

                                vr_total_fat += fat_det.get_int_vr_cobranca();

                                fat_det.create_T_FaturamentoDetalhes();
                            }
                        }

                        #endregion

                        #region - 3 - valor percentual por transações -

                        if (emp.get_int_nu_pctValor() > 0)
                        {
                            LOG_Transacoes l_tr = new LOG_Transacoes(this);

                            // ## Busca por período e empresa

                            if (l_tr.select_rows_dt_emp(GetDataBaseTime(dtFat.AddMonths(-1)),
                                                        GetDataBaseTime(dtFat),
                                                        emp.get_identity()))
                            {
                                long vr_trans = 0;

                                while (l_tr.fetch())
                                {
                                    if (l_tr.get_tg_confirmada() == TipoConfirmacao.Confirmada)
                                    {
                                        vr_trans += l_tr.get_int_vr_total();
                                    }
                                }

                                if (vr_trans > 0)
                                {
                                    vr_trans = vr_trans * emp.get_int_nu_pctValor() / 10000;

                                    T_FaturamentoDetalhes fat_det = new T_FaturamentoDetalhes(this);

                                    fat_det.set_fk_fatura(fat.get_identity());
                                    fat_det.set_vr_cobranca(vr_trans.ToString());
                                    fat_det.set_tg_tipoFat(TipoFat.Percent);
                                    fat_det.set_tg_desconto(Context.FALSE);
                                    fat_det.set_fk_empresa(emp.get_identity());
                                    fat_det.set_fk_loja(Context.FALSE);

                                    vr_total_fat += fat_det.get_int_vr_cobranca();

                                    fat_det.create_T_FaturamentoDetalhes();
                                }
                            }
                        }

                        #endregion

                        #region - 4 - valor cartão ativo -

                        if (emp.get_int_vr_cartaoAtivo() > 0)
                        {
                            T_Cartao cart = new T_Cartao(this);

                            // ## Busca por período e empresa

                            if (cart.select_rows_empresa(emp.get_st_empresa()))
                            {
                                T_FaturamentoDetalhes fat_det = new T_FaturamentoDetalhes(this);

                                fat_det.set_fk_fatura(fat.get_identity());
                                fat_det.set_vr_cobranca((cart.RowCount() * emp.get_int_vr_cartaoAtivo()).ToString());
                                fat_det.set_tg_tipoFat(TipoFat.CartaoAtiv);
                                fat_det.set_tg_desconto(Context.FALSE);
                                fat_det.set_fk_empresa(emp.get_identity());
                                fat_det.set_fk_loja(Context.FALSE);

                                vr_total_fat += fat_det.get_int_vr_cobranca();

                                fat_det.create_T_FaturamentoDetalhes();
                            }
                        }

                        #endregion

                        #region - 5 - valores extras -

                        T_FaturamentoDetalhes fat_extras = new T_FaturamentoDetalhes(this);

                        if (fat_extras.select_rows_emp(emp.get_identity(), TipoFat.Extras))
                        {
                            while (fat_extras.fetch())
                            {
                                vr_total_fat += fat_extras.get_int_vr_cobranca();

                                T_FaturamentoDetalhes fat_extras_upd = new T_FaturamentoDetalhes(this);

                                fat_extras_upd.ExclusiveAccess();

                                fat_extras_upd.selectIdentity(fat_extras.get_identity());
                                fat_extras_upd.set_fk_fatura(fat.get_identity());

                                fat_extras_upd.synchronize_T_FaturamentoDetalhes();
                                fat_extras_upd.ReleaseExclusive();
                            }
                        }

                        #endregion

                        if (emp.get_int_vr_minimo() > vr_total_fat)
                        {
                            vr_total_fat = emp.get_int_vr_minimo();
                        }

                        fat_upd.ExclusiveAccess();
                        fat_upd.selectIdentity(fat.get_identity());

                        fat_upd.set_vr_cobranca(vr_total_fat.ToString());

                        fat_upd.synchronize_T_Faturamento();
                        fat_upd.ReleaseExclusive();
                    }
                }
            }

            #endregion

            T_Loja loj = new T_Loja(this);

            // ## Busca todas as lojas

            if (loj.selectAll())
            {
                // ## Busco todos os registros

                while (loj.fetch())
                {
                    // ## Se dia é hoje

                    Trace("Loja: " + loj.get_st_nome());

                    if (loj.get_tg_cancel() == Context.TRUE)
                    {
                        continue;
                    }

                    Trace("Loja ok ");

                    if (loj.get_int_nu_periodoFat() == dtFat.Day &&
                        loj.get_tg_isentoFat() == Context.FALSE)
                    {
                        Trace("[A]");

                        T_Faturamento fat     = new T_Faturamento(this);
                        T_Faturamento fat_upd = new T_Faturamento(this);

                        DateTime aux = dtFat;

                        // ## Para casos onde o mês deve ser o próximo

                        if (loj.get_int_nu_diavenc() < dtFat.Day)
                        {
                            Trace("[B]");
                            aux = dtFat.AddMonths(1);
                        }

                        Trace("[C]");

                        int zday = Convert.ToInt32(loj.get_int_nu_diavenc());

                        Trace("[C] 1 " + aux.Year.ToString());
                        Trace("[C] 2 " + aux.Month.ToString());
                        Trace("[C] 3 " + zday.ToString());

                        DateTime dtVenc = new DateTime(aux.Year, aux.Month, zday);

                        Trace("[D]");

                        fat.set_dt_vencimento(GetDataBaseTime(dtVenc));
                        fat.set_fk_loja(loj.get_identity());
                        fat.set_tg_situacao(TipoSitFat.Pendente);

                        // ## Crio registro

                        fat.create_T_Faturamento();

                        long vr_total_fat = 0;

                        // ## Crio registro detalhe

                        #region - 1 - mensalidade -

                        {
                            Trace("[E]");

                            T_FaturamentoDetalhes fat_det = new T_FaturamentoDetalhes(this);

                            fat_det.set_fk_fatura(fat.get_identity());
                            fat_det.set_vr_cobranca(loj.get_vr_mensalidade());
                            fat_det.set_tg_tipoFat(TipoFat.TBM);
                            fat_det.set_tg_desconto(Context.FALSE);
                            fat_det.set_fk_empresa(Context.FALSE);
                            fat_det.set_fk_loja(loj.get_identity());

                            vr_total_fat += fat_det.get_int_vr_cobranca();

                            fat_det.create_T_FaturamentoDetalhes();
                        }

                        #endregion

                        #region - 2 - valor por transações -

                        if (loj.get_int_vr_transacao() > 0)
                        {
                            Trace("[F]");

                            LOG_Transacoes l_tr = new LOG_Transacoes(this);

                            l_tr.SetCountMode();

                            l_tr.select_rows_dt_loj(GetDataBaseTime(dtFat.AddMonths(-1)),
                                                    GetDataBaseTime(dtFat),
                                                    loj.get_identity());

                            long trans = l_tr.GetCount() - loj.get_int_nu_franquia();

                            if (trans > 0)
                            {
                                Trace("[G]");

                                T_FaturamentoDetalhes fat_det = new T_FaturamentoDetalhes(this);

                                fat_det.set_fk_fatura(fat.get_identity());
                                fat_det.set_vr_cobranca((trans * loj.get_int_vr_transacao()).ToString());
                                fat_det.set_tg_tipoFat(TipoFat.FixoTrans);
                                fat_det.set_nu_quantidade(l_tr.GetCount().ToString());
                                fat_det.set_tg_desconto(Context.FALSE);
                                fat_det.set_fk_empresa(Context.FALSE);
                                fat_det.set_fk_loja(loj.get_identity());

                                vr_total_fat += fat_det.get_int_vr_cobranca();

                                fat_det.create_T_FaturamentoDetalhes();
                            }
                        }

                        #endregion

                        #region - 3 - valor percentual por transações -

                        if (loj.get_int_nu_pctValor() > 0)
                        {
                            Trace("[H]");

                            LOG_Transacoes l_tr = new LOG_Transacoes(this);

                            // ## Busca por período e empresa

                            if (l_tr.select_rows_dt_loj(GetDataBaseTime(dtFat.AddMonths(-1)),
                                                        GetDataBaseTime(dtFat),
                                                        loj.get_identity()))
                            {
                                Trace("[I]");

                                long vr_trans = 0;

                                while (l_tr.fetch())
                                {
                                    if (l_tr.get_tg_confirmada() == TipoConfirmacao.Confirmada)
                                    {
                                        vr_trans += l_tr.get_int_vr_total();
                                    }
                                }

                                if (vr_trans > 0)
                                {
                                    Trace("[J]");

                                    vr_trans = vr_trans * loj.get_int_nu_pctValor() / 10000;

                                    T_FaturamentoDetalhes fat_det = new T_FaturamentoDetalhes(this);

                                    fat_det.set_fk_fatura(fat.get_identity());
                                    fat_det.set_vr_cobranca(vr_trans.ToString());
                                    fat_det.set_tg_tipoFat(TipoFat.Percent);
                                    fat_det.set_tg_desconto(Context.FALSE);
                                    fat_det.set_fk_empresa(Context.FALSE);
                                    fat_det.set_fk_loja(loj.get_identity());

                                    vr_total_fat += fat_det.get_int_vr_cobranca();

                                    fat_det.create_T_FaturamentoDetalhes();
                                }
                            }
                        }

                        #endregion

                        #region - 4 - valores extras -

                        T_FaturamentoDetalhes fat_extras = new T_FaturamentoDetalhes(this);

                        if (fat_extras.select_rows_emp(loj.get_identity(), TipoFat.Extras))
                        {
                            Trace("[K]");

                            while (fat_extras.fetch())
                            {
                                Trace("[L]");

                                vr_total_fat += fat_extras.get_int_vr_cobranca();

                                T_FaturamentoDetalhes fat_extras_upd = new T_FaturamentoDetalhes(this);

                                fat_extras_upd.ExclusiveAccess();

                                fat_extras_upd.selectIdentity(fat_extras.get_identity());
                                fat_extras_upd.set_fk_fatura(fat.get_identity());

                                fat_extras_upd.synchronize_T_FaturamentoDetalhes();
                                fat_extras_upd.ReleaseExclusive();
                            }
                        }

                        #endregion

                        if (vr_total_fat < loj.get_int_vr_minimo())
                        {
                            vr_total_fat = loj.get_int_vr_minimo();
                        }

                        fat_upd.ExclusiveAccess();
                        fat_upd.selectIdentity(fat.get_identity());

                        fat_upd.set_vr_cobranca(vr_total_fat.ToString());

                        fat_upd.synchronize_T_Faturamento();
                        fat_upd.ReleaseExclusive();

                        Trace("[M]");
                    }
                }
            }

            /// USER [ execute ] END

            Registry("execute done schedule_faturamento ");

            return(true);
        }
        public override bool execute( )
        {
            if (base.execute() == false)
            {
                return(false);
            }

            Registry("execute fetch_dadosLoja ");

            /// USER [ execute ]

            // ## Copia para saida

            output_cont_dl.set_st_loja(loj.get_st_loja());
            output_cont_dl.set_nu_CNPJ(loj.get_nu_CNPJ());
            output_cont_dl.set_st_nome(loj.get_st_nome());
            output_cont_dl.set_st_social(loj.get_st_social());
            output_cont_dl.set_st_endereco(loj.get_st_endereco());
            output_cont_dl.set_st_enderecoInst(loj.get_st_enderecoInst());
            output_cont_dl.set_nu_inscEst(loj.get_nu_inscEst());

            output_cont_dl.set_st_cidade(loj.get_st_cidade());
            output_cont_dl.set_st_estado(loj.get_st_estado());
            output_cont_dl.set_nu_CEP(loj.get_nu_CEP());
            output_cont_dl.set_nu_telefone(loj.get_nu_telefone());
            output_cont_dl.set_nu_fax(loj.get_nu_fax());
            output_cont_dl.set_st_contato(loj.get_st_contato());
            output_cont_dl.set_vr_mensalidade(loj.get_vr_mensalidade());
            output_cont_dl.set_nu_contaDeb(loj.get_nu_contaDeb());
            output_cont_dl.set_st_obs(loj.get_st_obs());

            output_cont_dl.set_nu_pctValor(loj.get_nu_pctValor());
            output_cont_dl.set_vr_transacao(loj.get_vr_transacao());
            output_cont_dl.set_vr_minimo(loj.get_vr_minimo());
            output_cont_dl.set_nu_franquia(loj.get_nu_franquia());
            output_cont_dl.set_nu_periodoFat(loj.get_nu_periodoFat());
            output_cont_dl.set_nu_diavenc(loj.get_nu_diavenc());
            output_cont_dl.set_nu_bancoFat(loj.get_nu_bancoFat());
            output_cont_dl.set_tg_tipoCobranca(loj.get_tg_tipoCobranca());
            output_cont_dl.set_tg_blocked(loj.get_tg_blocked());
            output_cont_dl.set_tg_cancel(loj.get_tg_cancel());
            output_cont_dl.set_tg_isento(loj.get_tg_isentoFat());
            output_cont_dl.set_st_senhaWeb(loj.get_st_senha());

            LINK_LojaEmpresa loj_emp = new LINK_LojaEmpresa(this);
            T_Empresa        emp     = new T_Empresa(this);

            string convs = "";

            if (loj_emp.select_fk_loja(loj.get_identity()))
            {
                while (loj_emp.fetch())
                {
                    emp.selectIdentity(loj_emp.get_fk_empresa());

                    convs += emp.get_st_empresa().TrimStart('0') + ",";
                }
            }

            output_cont_dl.set_st_convenios(convs.TrimEnd(','));

            /// USER [ execute ] END

            Registry("execute done fetch_dadosLoja ");

            return(true);
        }
        public override bool execute( )
        {
            if (base.execute() == false)
            {
                return(false);
            }

            Registry("execute fetch_rel_tarifas ");

            /// USER [ execute ]

            StringBuilder sb_emp     = new StringBuilder();
            StringBuilder sb_content = new StringBuilder();

            money mon_help = new money();

            T_Empresa emp = new T_Empresa(this);

            emp.select_rows_tarifas();

            while (emp.fetch())
            {
                DadosEmpresa de = new DadosEmpresa();

                de.set_st_fantasia("(E) " + emp.get_st_empresa() + " " + emp.get_st_fantasia());

                DataPortable tmp = de as DataPortable;

                sb_emp.Append(MemorySave(ref tmp));
                sb_emp.Append(",");

                {
                    Rel_Tarifas rt = new Rel_Tarifas();

                    rt.set_st_emp(de.get_st_fantasia());
                    //rt.set_st_desc ( "Empresa" );
                    rt.set_st_val(emp.get_st_fantasia());

                    DataPortable port_rt = rt as DataPortable;

                    sb_content.Append(MemorySave(ref port_rt));
                    sb_content.Append(",");
                }

                {
                    Rel_Tarifas rt = new Rel_Tarifas();

                    rt.set_st_emp(de.get_st_fantasia());
                    //rt.set_st_desc ( "Mensalidade" );
                    rt.set_st_val(mon_help.formatToMoney(emp.get_vr_mensalidade()));

                    DataPortable port_rt = rt as DataPortable;

                    sb_content.Append(MemorySave(ref port_rt));
                    sb_content.Append(",");
                }

                {
                    Rel_Tarifas rt = new Rel_Tarifas();

                    rt.set_st_emp(de.get_st_fantasia());
                    //rt.set_st_desc ( "Percentual sobre transação" );

                    string pct = emp.get_nu_pctValor().PadLeft(3, '0');

                    pct = pct.Insert(pct.Length - 2, ",") + "%";

                    rt.set_st_val(pct);

                    DataPortable port_rt = rt as DataPortable;

                    sb_content.Append(MemorySave(ref port_rt));
                    sb_content.Append(",");
                }

                {
                    Rel_Tarifas rt = new Rel_Tarifas();

                    rt.set_st_emp(de.get_st_fantasia());
                    //rt.set_st_desc ( "Valor transação" );
                    rt.set_st_val(mon_help.formatToMoney(emp.get_vr_transacao()));

                    DataPortable port_rt = rt as DataPortable;

                    sb_content.Append(MemorySave(ref port_rt));
                    sb_content.Append(",");
                }

                {
                    Rel_Tarifas rt = new Rel_Tarifas();

                    rt.set_st_emp(de.get_st_fantasia());
                    //rt.set_st_desc ( "Valor mínimo" );
                    rt.set_st_val(mon_help.formatToMoney(emp.get_vr_minimo()));

                    DataPortable port_rt = rt as DataPortable;

                    sb_content.Append(MemorySave(ref port_rt));
                    sb_content.Append(",");
                }

                {
                    Rel_Tarifas rt = new Rel_Tarifas();

                    rt.set_st_emp(de.get_st_fantasia());
                    //rt.set_st_desc ( "Franquia de transações" );
                    rt.set_st_val(emp.get_nu_franquia());

                    DataPortable port_rt = rt as DataPortable;

                    sb_content.Append(MemorySave(ref port_rt));
                    sb_content.Append(",");
                }

                {
                    Rel_Tarifas rt = new Rel_Tarifas();

                    rt.set_st_emp(de.get_st_fantasia());
                    //rt.set_st_desc ( "Valor por cartão ativo" );
                    rt.set_st_val(mon_help.formatToMoney(emp.get_vr_cartaoAtivo()));

                    DataPortable port_rt = rt as DataPortable;

                    sb_content.Append(MemorySave(ref port_rt));
                    sb_content.Append(",");
                }

                {
                    Rel_Tarifas rt = new Rel_Tarifas();

                    rt.set_st_emp(de.get_st_fantasia());
                    //rt.set_st_desc ( "Isento de Fatura" );

                    if (emp.get_tg_isentoFat() == Context.TRUE)
                    {
                        rt.set_st_val("SIM");
                    }
                    else
                    {
                        rt.set_st_val("NÃO");
                    }

                    DataPortable port_rt = rt as DataPortable;

                    sb_content.Append(MemorySave(ref port_rt));
                    sb_content.Append(",");
                }

                {
                    Rel_Tarifas rt = new Rel_Tarifas();

                    rt.set_st_emp(de.get_st_fantasia());
                    //rt.set_st_desc ( "Situação" );

                    if (emp.get_tg_blocked() == Context.TRUE)
                    {
                        rt.set_st_val("Bloq.");
                    }
                    else
                    {
                        rt.set_st_val("Ativo");
                    }

                    DataPortable port_rt = rt as DataPortable;

                    sb_content.Append(MemorySave(ref port_rt));
                    sb_content.Append(",");
                }
            }

            T_Loja loj = new T_Loja(this);

            loj.select_rows_tarifas();

            while (loj.fetch())
            {
                DadosEmpresa de = new DadosEmpresa();

                string id = "(" + loj.get_st_loja() + ") CNPJ: " + loj.get_nu_CNPJ() + " " + loj.get_st_nome() + " - " + loj.get_st_social();

                de.set_st_fantasia(id);

                DataPortable tmp = de as DataPortable;

                sb_emp.Append(MemorySave(ref tmp));
                sb_emp.Append(",");

                {
                    Rel_Tarifas rt = new Rel_Tarifas();

                    rt.set_st_emp(de.get_st_fantasia());
                    //rt.set_st_desc ( "Empresa" );
                    rt.set_st_val(id);

                    DataPortable port_rt = rt as DataPortable;

                    sb_content.Append(MemorySave(ref port_rt));
                    sb_content.Append(",");
                }

                {
                    Rel_Tarifas rt = new Rel_Tarifas();

                    rt.set_st_emp(id);
                    //rt.set_st_desc ( "Mensalidade" );
                    rt.set_st_val(mon_help.formatToMoney(loj.get_vr_mensalidade()));

                    DataPortable port_rt = rt as DataPortable;

                    sb_content.Append(MemorySave(ref port_rt));
                    sb_content.Append(",");
                }

                {
                    Rel_Tarifas rt = new Rel_Tarifas();

                    rt.set_st_emp(id);
                    //rt.set_st_desc ( "Percentual sobre transação" );

                    string pct = loj.get_nu_pctValor().PadLeft(3, '0');

                    pct = pct.Insert(pct.Length - 2, ",") + "%";

                    rt.set_st_val(pct);

                    DataPortable port_rt = rt as DataPortable;

                    sb_content.Append(MemorySave(ref port_rt));
                    sb_content.Append(",");
                }

                {
                    Rel_Tarifas rt = new Rel_Tarifas();

                    rt.set_st_emp(id);
                    //rt.set_st_desc ( "Valor transação" );
                    rt.set_st_val(mon_help.formatToMoney(loj.get_vr_transacao()));

                    DataPortable port_rt = rt as DataPortable;

                    sb_content.Append(MemorySave(ref port_rt));
                    sb_content.Append(",");
                }

                {
                    Rel_Tarifas rt = new Rel_Tarifas();

                    rt.set_st_emp(id);
                    //rt.set_st_desc ( "Valor mínimo" );
                    rt.set_st_val(mon_help.formatToMoney(loj.get_vr_minimo()));

                    DataPortable port_rt = rt as DataPortable;

                    sb_content.Append(MemorySave(ref port_rt));
                    sb_content.Append(",");
                }

                {
                    Rel_Tarifas rt = new Rel_Tarifas();

                    rt.set_st_emp(id);
                    //rt.set_st_desc ( "Franquia de transações" );
                    rt.set_st_val(loj.get_nu_franquia());

                    DataPortable port_rt = rt as DataPortable;

                    sb_content.Append(MemorySave(ref port_rt));
                    sb_content.Append(",");
                }

                {
                    Rel_Tarifas rt = new Rel_Tarifas();

                    rt.set_st_emp(de.get_st_fantasia());
                    //rt.set_st_desc ( "Valor por cartão ativo" );
                    rt.set_st_val("0");

                    DataPortable port_rt = rt as DataPortable;

                    sb_content.Append(MemorySave(ref port_rt));
                    sb_content.Append(",");
                }

                {
                    Rel_Tarifas rt = new Rel_Tarifas();

                    rt.set_st_emp(id);
                    //rt.set_st_desc ( "Isento de Fatura" );

                    if (loj.get_tg_isentoFat() == Context.TRUE)
                    {
                        rt.set_st_val("SIM");
                    }
                    else
                    {
                        rt.set_st_val("NÃO");
                    }

                    DataPortable port_rt = rt as DataPortable;

                    sb_content.Append(MemorySave(ref port_rt));
                    sb_content.Append(",");
                }

                {
                    Rel_Tarifas rt = new Rel_Tarifas();

                    rt.set_st_emp(de.get_st_fantasia());
                    //rt.set_st_desc ( "Situação" );

                    if (loj.get_tg_blocked() == Context.TRUE)
                    {
                        rt.set_st_val("Bloq.");
                    }
                    else
                    {
                        rt.set_st_val("Ativo");
                    }

                    if (loj.get_tg_cancel() == Context.TRUE)
                    {
                        rt.set_st_val("Cancel.");
                    }

                    DataPortable port_rt = rt as DataPortable;

                    sb_content.Append(MemorySave(ref port_rt));
                    sb_content.Append(",");
                }
            }

            // content
            {
                string list_ids = sb_content.ToString().TrimEnd(',');

                DataPortable dp = new DataPortable();

                dp.setValue("ids", list_ids);

                output_st_csv = MemorySave(ref dp);
            }

            // entidades
            {
                string list_ids = sb_emp.ToString().TrimEnd(',');

                DataPortable dp = new DataPortable();

                dp.setValue("ids", list_ids);

                output_st_csv_emp = MemorySave(ref dp);
            }

            /// USER [ execute ] END

            Registry("execute done fetch_rel_tarifas ");

            return(true);
        }
        public override bool authenticate( )
        {
            Registry("authenticate exec_pos_vendaEmpresarialSITEF ");

            /// USER [ authenticate ]

            cart  = new T_Cartao(this);
            emp   = new T_Empresa(this);
            l_nsu = new LOG_NSU(this);
            info  = new T_InfoAdicionais(this);
            prot  = new T_Proprietario(this);
            loj   = new T_Loja(this);

            // Default é erro genérico
            var_codResp = "9999";

            // Normal
            var_nu_nsuAtual    = Context.NONE;
            var_nu_nsuEntidade = Context.NONE;

            // Cancelamento
            var_nu_nsuOrig    = Context.NONE;
            var_nu_nsuEntOrig = Context.NONE;

            // Valores básicos de comércio
            var_vr_total    = input_cont_pe.get_vr_valor();
            var_nu_parcelas = input_cont_pe.get_nu_parcelas();

            var_codResp = "0606";

            // alterado

            #region - valida loja AO INVES DE TERMINAL (antigo) -

            // ## Busca terminal pelo seu código

            // pega os 7 ultimos
            if (!loj.select_rows_loja(input_cont_pe.get_st_terminal().Substring(1).TrimStart('0')))
            {
                output_st_msg = "Loja inexistente";
                var_codResp   = "0303";
                return(false);
            }

            if (!loj.fetch())
            {
                output_st_msg = "Erro aplicativo";
                return(false);
            }

            // alterado!
            // busca o primeiro terminal vinculado

            if (!term.select_fk_loja(loj.get_identity()))
            {
                output_st_msg = "Erro do terminal";
                return(false);
            }

            term.fetch();

            #endregion

            // continua igual

            #region - valida empresa -

            // ## Busca empresa informada

            if (!emp.select_rows_empresa(input_cont_pe.get_st_empresa()))
            {
                output_st_msg = "Empresa inexistente";
                var_codResp   = "0303";
                return(false);
            }

            if (!emp.fetch())
            {
                output_st_msg = "Erro de aplicativo";
                return(false);
            }

            // ## Caso empresa bloqueada, sair

            if (emp.get_tg_bloq() == Context.TRUE)
            {
                output_st_msg = "Empresa bloqueada";
                var_codResp   = "0303";
                return(false);
            }

            #endregion

            // alterado!

            #region - valida relação da Loja do Terminal com Empresa (Convênio)

            LINK_LojaEmpresa loj_emp = new LINK_LojaEmpresa(this);

            if (!loj_emp.select_fk_empresa_loja(emp.get_identity(), loj.get_identity()))
            {
                output_st_msg = "Loja não conveniada";
                var_codResp   = "0303";
                return(false);
            }

            if (loj.get_tg_blocked() == Context.TRUE)
            {
                output_st_msg = "Loja bloqueada";
                var_codResp   = "0303";
                return(false);
            }

            if (loj.get_tg_cancel() == Context.TRUE)
            {
                output_st_msg = "Loja cancelada";
                var_codResp   = "0303";
                return(false);
            }

            #endregion

            // continua igual

            #region - valida cartão -

            if (!cart.select_rows_tudo(input_cont_pe.get_st_empresa(),
                                       input_cont_pe.get_st_matricula(),
                                       input_cont_pe.get_st_titularidade()))
            {
                output_st_msg = "Cartão inexistente";
                var_codResp   = "0606";
                return(false);
            }

            if (!cart.fetch())
            {
                output_st_msg = "Erro aplicativo";
                return(false);
            }

            // ## Verifica bloqueio

            if (cart.get_tg_status() == CartaoStatus.Bloqueado)
            {
                output_st_msg = "Cartão inválido";
                var_codResp   = "0505";
                return(false);
            }

            if (cart.get_tg_emitido() != StatusExpedicao.Expedido)
            {
                output_st_msg = "Cartão inválido";
                var_codResp   = "0505";
                return(false);
            }

            if (cart.get_tg_tipoCartao() == TipoCartao.educacional)
            {
                // ## No caso educacional, permitir somente venda
                // ## em uma parcela

                if (input_cont_pe.get_nu_parcelas().TrimStart('0') != "1")
                {
                    output_st_msg = "Somente uma parcela";
                    var_codResp   = "0606";
                    return(false);
                }
            }

            // ## Conferir vencto do cartão

            /*
             * if ( cart.get_tg_tipoCartao() == TipoCartao.empresarial )
             * {
             *      int year  = 2000 + Convert.ToInt32 ( cart.get_st_venctoCartao().Substring ( 2,2 ) );
             *      int month = Convert.ToInt32 ( cart.get_st_venctoCartao().Substring ( 0,2 ) );
             *      int day   = 1;
             *
             * DateTime tim_venc = new DateTime ( year, month, day );
             *
             * if ( tim_venc < DateTime.Now )
             *      {
             * output_st_msg     = "Cartão vencido";
             * var_codResp       = "7676";
             * return false;
             *      }
             * }
             */

            #endregion

            var_vr_total    = input_cont_pe.get_vr_valor();
            var_nu_parcelas = input_cont_pe.get_nu_parcelas();

            SQL_LOGGING_ENABLE = false;

            // continua igual

            #region - Verifica disponivel mensal nas parcelas -

            T_Parcelas parc = new T_Parcelas(this);

            string myId = cart.get_identity();

            if (cart.get_st_titularidade() != "01")
            {
                cart.select_rows_tudo(cart.get_st_empresa(),
                                      cart.get_st_matricula(),
                                      "01");
                cart.fetch();
            }

            vr_dispMes = cart.get_int_vr_limiteMensal() + cart.get_int_vr_extraCota();
            vr_dispTot = cart.get_int_vr_limiteTotal() + cart.get_int_vr_extraCota();

            vr_valor = Convert.ToInt64(input_cont_pe.get_vr_valor());

            if (cart.get_tg_tipoCartao() != TipoCartao.presente)
            {
                new ApplicationUtil().GetSaldoDisponivel(ref cart, ref vr_dispMes, ref vr_dispTot);

                int tmp_nu_parc = Convert.ToInt32(input_cont_pe.get_nu_parcelas());

                if (tmp_nu_parc > 1)
                {
                    if (vr_valor > vr_dispTot)
                    {
                        output_st_msg = "limite excedido";
                        var_codResp   = "2727";

                        SQL_LOGGING_ENABLE = true;

                        return(false);
                    }

                    LOG_Transacoes ltr     = new LOG_Transacoes(this);
                    T_Parcelas     parcTot = new T_Parcelas(this);

                    string tmp = input_cont_pe.get_st_valores();

                    ArrayList lstCartoes = new ArrayList();

                    T_Cartao c_t = new T_Cartao(this);

                    c_t.select_rows_empresa_matricula(cart.get_st_empresa(),
                                                      cart.get_st_matricula());

                    while (c_t.fetch())
                    {
                        lstCartoes.Add(c_t.get_identity());
                    }

                    for (int t = 1, index_pos = 0; t <= tmp_nu_parc; ++t)
                    {
                        long valor_unit_parc = Convert.ToInt64(tmp.Substring(index_pos, 12));

                        index_pos += 12;

                        if (valor_unit_parc > cart.get_int_vr_limiteMensal())
                        {
                            output_st_msg = "limite excedido";
                            var_codResp   = "2727";

                            SQL_LOGGING_ENABLE = true;

                            return(false);
                        }

                        long dispMesParc = cart.get_int_vr_limiteMensal();

                        // Verifica disponivel mensal nas parcelas
                        if (parcTot.select_rows_cartao_mensal(ref lstCartoes, t.ToString()))                              // este mês
                        {
                            while (parcTot.fetch())
                            {
                                if (ltr.selectIdentity(parcTot.get_fk_log_transacoes()))                                      // busca transação
                                {
                                    if (ltr.get_tg_confirmada() == TipoConfirmacao.Confirmada ||
                                        ltr.get_tg_confirmada() == TipoConfirmacao.Pendente)
                                    {
                                        dispMesParc -= parcTot.get_int_vr_valor();
                                    }
                                }
                            }
                        }

                        if (valor_unit_parc > dispMesParc)
                        {
                            output_st_msg = "limite excedido";
                            var_codResp   = "2727";

                            SQL_LOGGING_ENABLE = true;

                            return(false);
                        }
                    }
                }
                else
                {
                    if (vr_valor > vr_dispMes || vr_valor > vr_dispTot)
                    {
                        output_st_msg = "limite excedido";
                        var_codResp   = "2727";

                        SQL_LOGGING_ENABLE = true;

                        return(false);
                    }
                }
            }
            else
            {
                if (vr_valor > cart.get_int_vr_limiteTotal())
                {
                    output_st_msg = "limite excedido";
                    var_codResp   = "2727";

                    SQL_LOGGING_ENABLE = true;

                    return(false);
                }
            }

            if (myId != cart.get_identity())
            {
                // restaurar cartão dep
                cart.selectIdentity(myId);
            }

            #endregion

            SQL_LOGGING_ENABLE = true;

            /// USER [ authenticate ] END

            Registry("authenticate done exec_pos_vendaEmpresarialSITEF ");

            return(true);
        }