public bool Accetta(DatabaseContext db, PERSONA venditore, int?idPayPal, ref string messaggio) { DateTime dataModifica = DateTime.Now; TipoScambio tipoScambio = TipoScambio.AMano; if (this.OFFERTA_SPEDIZIONE.Count() > 0) { tipoScambio = TipoScambio.Spedizione; } // verifico la possibilità di acquisto del compratore VerificaAcquisto statoAcquisto = AnnuncioModel.CheckAcquisto(this.PERSONA, this.PERSONA.CONTO_CORRENTE.CONTO_CORRENTE_CREDITO.ToList(), tipoScambio, false, true); if (statoAcquisto == VerificaAcquisto.Ok) { this.ANNUNCIO.DATA_VENDITA = dataModifica; this.ANNUNCIO.STATO = (int)StatoVendita.VENDUTO; } else if (statoAcquisto == VerificaAcquisto.VerificaCartaCredito) { this.ANNUNCIO.DATA_VENDITA = dataModifica; this.ANNUNCIO.STATO = (int)StatoVendita.BARATTOINCORSO; } else if (statoAcquisto != VerificaAcquisto.VerificaCartaCredito && statoAcquisto != VerificaAcquisto.SpedizioneDaPagare) { // se il compratore non può acquistare annuncio return(false); } this.ANNUNCIO.DATA_MODIFICA = dataModifica; this.ANNUNCIO.ID_COMPRATORE = this.PERSONA.ID; db.ANNUNCIO.Attach(this.OffertaOriginale.ANNUNCIO); db.Entry(this.OffertaOriginale.ANNUNCIO).State = System.Data.Entity.EntityState.Modified; // salvataggio offerta this.SESSIONE_COMPRATORE = null; this.DATA_MODIFICA = dataModifica; this.STATO = (int)StatoOfferta.ACCETTATA; this.OffertaOriginale.STATO = this.STATO; this.OffertaOriginale.DATA_MODIFICA = this.DATA_MODIFICA; this.OffertaOriginale.SESSIONE_COMPRATORE = this.SESSIONE_COMPRATORE; db.OFFERTA.Attach(this.OffertaOriginale); db.Entry(this.OffertaOriginale).State = System.Data.Entity.EntityState.Modified; int salvataggi = db.SaveChanges(); if (salvataggi > 1) { CORRIERE_SERVIZIO_SPEDIZIONE spedizioneVenditore = null; ANNUNCIO_TIPO_SCAMBIO tipoScambioVenditore = this.ANNUNCIO.ANNUNCIO_TIPO_SCAMBIO .FirstOrDefault(m => m.TIPO_SCAMBIO == (int)tipoScambio); tipoScambioVenditore.DATA_MODIFICA = DateTime.Now; tipoScambioVenditore.STATO = (int)StatoScambio.SELEZIONATO; db.ANNUNCIO_TIPO_SCAMBIO.Attach(tipoScambioVenditore); db.Entry(tipoScambioVenditore).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); // salvataggio spedizione if (tipoScambio == TipoScambio.Spedizione) { var tipoScambioSpedizioneVenditore = tipoScambioVenditore.ANNUNCIO_TIPO_SCAMBIO_SPEDIZIONE.FirstOrDefault(); spedizioneVenditore = db.CORRIERE_SERVIZIO_SPEDIZIONE.FirstOrDefault(m => m.ID == tipoScambioSpedizioneVenditore.ID_CORRIERE_SERVIZIO_SPEDIZIONE); var offertaSpedizione = this.OFFERTA_SPEDIZIONE.FirstOrDefault(); spedizioneVenditore.ID_INDIRIZZO_DESTINATARIO = offertaSpedizione.ID_INDIRIZZO_DESTINATARIO; spedizioneVenditore.NOMINATIVO_DESTINATARIO = offertaSpedizione.NOMINATIVO_DESTINATARIO; spedizioneVenditore.TELEFONO_DESTINATARIO = offertaSpedizione.TELEFONO_DESTINATARIO; spedizioneVenditore.INFO_EXTRA_DESTINATARIO = offertaSpedizione.INFO_EXTRA; spedizioneVenditore.STATO = (int)StatoSpedizione.SOSPESA; //db.CORRIERE_SERVIZIO_SPEDIZIONE.Attach(spedizioneVenditore); db.SaveChanges(); } TRANSAZIONE transazione = SaveTransazionePerVenditore(db, venditore, spedizioneVenditore); // tolgo i punti al mittente DoPagamento(db, transazione); int?idTransazionePerAcquirente = SaveTransazionePerAcquirente(db, venditore, idPayPal); // cambio stato dei baratti offerti for (int i = 0; i < this.OffertaOriginale.OFFERTA_BARATTO.Count(); i++) { var baratto = this.OffertaOriginale.OFFERTA_BARATTO.ToList()[i]; DoBaratto(db, ref baratto, dataModifica, idTransazionePerAcquirente); } AnnullaOfferteEffettuate(db, this.ID_ANNUNCIO); AnnullaOfferteRicevute(db, this.ID_ANNUNCIO, this.ID); this.STATO = this.STATO; return(true); } return(false); }
private TRANSAZIONE SaveTransazionePerVenditore(DatabaseContext db, PERSONA venditore, CORRIERE_SERVIZIO_SPEDIZIONE spedizione) { // compratore paga venditore TRANSAZIONE transazione = new TRANSAZIONE(); transazione.ID_CONTO_MITTENTE = this.PERSONA.ID_CONTO_CORRENTE; transazione.ID_CONTO_DESTINATARIO = venditore.ID_CONTO_CORRENTE; transazione.NOME = Language.BuyTransaction + " " + this.ANNUNCIO.NOME; if (transazione.NOME.Length > 100) { transazione.NOME = transazione.NOME.Substring(0, 99); } transazione.PUNTI = this.PUNTI; transazione.SOLDI = Utility.cambioValuta(this.PUNTI); transazione.TIPO = (int)TipoPagamento.HAPPY; transazione.DATA_INSERIMENTO = DateTime.Now; transazione.TEST = 0; transazione.STATO = (int)StatoPagamento.ACCETTATO; db.TRANSAZIONE.Add(transazione); if (db.SaveChanges() <= 0) { throw new Exception(string.Format(ExceptionMessage.NotSavedBidTransaction, this.ID)); } TRANSAZIONE_ANNUNCIO transazioneAnnuncio = new Models.TRANSAZIONE_ANNUNCIO(); transazioneAnnuncio.ID_TRANSAZIONE = transazione.ID; transazioneAnnuncio.ID_ANNUNCIO = this.ID_ANNUNCIO; transazioneAnnuncio.PUNTI = (decimal)transazione.PUNTI; transazioneAnnuncio.SOLDI = (decimal)transazione.SOLDI; if (spedizione != null) { transazioneAnnuncio.PUNTI_SPEDIZIONE = (decimal)spedizione.PUNTI; transazioneAnnuncio.SOLDI_SPEDIZIONE = (decimal)spedizione.SOLDI; } transazioneAnnuncio.DATA_INSERIMENTO = DateTime.Now; transazioneAnnuncio.STATO = (int)StatoPagamento.ACCETTATO; db.TRANSAZIONE_ANNUNCIO.Add(transazioneAnnuncio); if (db.SaveChanges() <= 0) { throw new Exception(string.Format(ExceptionMessage.NotSavedBidTransaction, this.ID)); } return(transazione); }