public static TariffeCarburante GetTariffa(DateTime dataDa, string nazione, string distributore, string tipoCarburante)
        {
            TariffeCarburante tariffa = null;
            Dao       dao             = new Dao();
            Hashtable parameters      = new Hashtable();

            parameters.Add("@DataDa", dataDa);
            parameters.Add("@Nazione", nazione);
            parameters.Add("@Distributore", distributore);
            parameters.Add("@TipoCarburante", tipoCarburante);
            DataSet data = dao.ExecuteStoredProcedure("TX.TariffeCarburante_GetTariffa", parameters);

            if (data.Tables[0].Rows.Count > 0)
            {
                tariffa                = new TariffeCarburante();
                tariffa.Id             = Convert.ToInt32(data.Tables[0].Rows[0]["Id"]);
                tariffa.Distributore   = Convert.ToString(data.Tables[0].Rows[0]["Distributore"]);
                tariffa.Nazione        = Convert.ToString(data.Tables[0].Rows[0]["Nazione"]);
                tariffa.DataDa         = Convert.ToDateTime(data.Tables[0].Rows[0]["DataDa"]);
                tariffa.PrezzoLt       = Convert.ToDouble(data.Tables[0].Rows[0]["PrezzoLt"]);
                tariffa.Valuta         = Convert.ToString(data.Tables[0].Rows[0]["Valuta"]);
                tariffa.TipoCarburante = Convert.ToString(data.Tables[0].Rows[0]["TipoCarburante"]);
            }
            return(tariffa);
        }
Example #2
0
        /// <summary>Interroga TXTango riguardo ai rifornimenti del viaggio, inserisce nel database la note spese, e ritorna un evento contenente lo stato del viaggio impostato a "CLOSED".</summary>
        /// <param name="login">L'oggetto login da inviare a TXTango per l'autenticazione della richiesta.</param>
        /// <returns>Eventi</returns>
        public Eventi TXGetRefuelReport(TXTango.Login login)
        {
            Eventi evento = null;

            // se non sono presenti le date di inizio e fine viaggio non posso ricavare correttamente i rifornimenti
            // (si rischia di registrare nuovamente le spese di un viaggio antecedente).
            if (this.DataInizio == DateTime.MinValue || this.DataFine == DateTime.MinValue)
            {
                throw new Exception("Impossibile ricavare i rifornimenti poichè non sono presenti le date di inizio o fine viaggio. IdViaggio: " + this.Id + " KeyViaggio: " + this.KeyViaggio);
            }

            RefuelReportSelection reportSelection = new RefuelReportSelection();

            reportSelection.Vehicles    = new IdentifierVehicle[1];
            reportSelection.Vehicles[0] = new IdentifierVehicle()
            {
                IdentifierVehicleType = enumIdentifierVehicleType.TRANSICS_ID, Id = this.CodiceMezzo
            };
            reportSelection.DateTimeRangeSelection = new DateTimeRangeSelection()
            {
                StartDate = (DateTime)this.DataInizio, EndDate = this.DataFine
            };

            ServiceSoapClient service = new ServiceSoapClient();
            GetRefuelReport   result  = service.Get_RefuelReport(login, reportSelection);

            evento             = new Eventi();
            evento.Stato       = ConfigurationManager.AppSettings["TXTANGO_STATO_CLOSED"];
            evento.SyncData    = DateTime.Now;
            evento.SyncTask    = ConfigurationManager.AppSettings["TXTANGO_TASK_GET_REFUELS"];
            evento.SyncTipo    = ConfigurationManager.AppSettings["TXTEMP_FROM_TXTANGO"];
            evento.XmlRequest  = Serializer.SerializeObject(reportSelection, SerializationType.XML);
            evento.XmlResponse = Serializer.SerializeObject(result, SerializationType.XML);

            if (result.Errors.Length > 0)
            {
                log.Error("Errore TXTango: " + result.Errors[0].ErrorCode.ToString());
                evento.SyncStato = ConfigurationManager.AppSettings["TXTEMP_STATO_ERRORE"];
                evento.Note      = "Vedi XmlResponse per i dettagli sugli errori.";
            }
            else
            {
                // per ogni rifornimento creo una nota spese
                NoteSpese         notaSpesa         = null;
                TariffeCarburante tariffaCarburante = null;
                DateTime          dataRifornimento  = DateTime.MinValue;
                string            tipoCarburante    = "";
                double            litri             = 0;
                string            distributore      = "";
                string            nazione           = "";
                int costiCalcolati = 0;

                for (int i = 0; i < result.FuelReportItems.Length; i++)
                {
                    try {
                        // dati del rifornimento
                        nazione = result.FuelReportItems[i].CountryCode;
                        litri   = result.FuelReportItems[i].Quantity;
                        if (result.FuelReportItems[i].BeginDate != null)
                        {
                            dataRifornimento = result.FuelReportItems[i].BeginDate.Value;
                        }
                        if (result.FuelReportItems[i].AddressInfo != null)
                        {
                            distributore = result.FuelReportItems[i].AddressInfo.Name.ToUpper();
                        }
                        if (result.FuelReportItems[i].FuelType != null)
                        {
                            tipoCarburante = result.FuelReportItems[i].FuelType.Name.ToUpper();
                        }
                        //tipoCarburante = (tipoCarburante != "") ? tipoCarburante : "DIESEL";

                        // inizializzo la nota spesa
                        notaSpesa             = new NoteSpese();
                        notaSpesa.IdViaggio   = this.Id;
                        notaSpesa.Codice      = "Refuel";
                        notaSpesa.Tipo        = "COST";
                        notaSpesa.Data        = dataRifornimento;
                        notaSpesa.Indirizzo   = nazione;
                        notaSpesa.Descrizione = "Rifornimento Distributore: " + distributore;
                        notaSpesa.Note        = "Carburante: " + tipoCarburante + " - Litraggio: " + litri;

                        // cerco la tariffa carburante
                        tariffaCarburante = TXTariffeCarburante.GetTariffa(dataRifornimento, nazione, distributore, tipoCarburante);
                        if (tariffaCarburante != null)
                        {
                            litri            = result.FuelReportItems[i].Quantity;
                            notaSpesa.Prezzo = (litri * tariffaCarburante.PrezzoLt);
                            notaSpesa.Valuta = tariffaCarburante.Valuta;
                            costiCalcolati++;
                        }
                        else
                        {
                            // nessuna tariffa trovata
                            notaSpesa.Note = "Nessuna tariffa da applicare. " + notaSpesa.Note;
                        }

                        notaSpesa.Insert();
                    } catch (Exception ex) {
                        log.Error(ex.Message, ex);
                    }
                }

                evento.SyncStato = ConfigurationManager.AppSettings["TXTEMP_STATO_SINCRONIZZATO"];
                evento.Note      = "Rifornimenti effettuati: " + result.FuelReportItems.Length + " Costi Automaticamente Calcolati: " + costiCalcolati + ".";
            }

            return(evento);
        }