protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["entry"] == null || !bool.Parse(Session["entry"].ToString()) || Request.QueryString["merchantId"] == null ||
            Session["token"] == null || Request.QueryString["token"] == null ||
            Session["token"].ToString() != Request.QueryString["token"].ToString() || Session["operatore"] == null ||
            Session["Utente"] == null || Session["settings"] == null || Request.QueryString["amzOrd"] == null)
        {
            Session.Abandon();
            Response.Redirect("login.aspx?path=amzPanoramica");
        }

        u        = (UtilityMaietta.Utente)Session["Utente"];
        settings = (UtilityMaietta.genSettings)Session["settings"];
        op       = (LavClass.Operatore)Session["operatore"];
        Year     = (int)Session["year"];

        LavClass.MafraInit folder = LavClass.MAFRA_INIT(Server.MapPath(""));
        if (folder.mafraPath == "")
        {
            folder.mafraPath = Server.MapPath("\\");
        }
        settings.ReplacePath(@folder.mafraInOut1[0], @folder.mafraInOut1[1]);
        settings.ReplacePath(@folder.mafraInOut2[0], @folder.mafraInOut2[1]);
        amzSettings = new AmzIFace.AmazonSettings(settings.lavAmazonSettingsFile, Year);
        amzSettings.ReplacePath(@folder.mafraInOut1[0], @folder.mafraInOut1[1]);
        amzSettings.ReplacePath(@folder.mafraInOut2[0], @folder.mafraInOut2[1]);

        /*string folder = LavClass.MAFRA_FOLDER(Server.MapPath(""));
         * if (folder == "")
         *  folder = Server.MapPath("\\");
         * settings.ReplacePath(@"G:\", @"\\10.0.0.80\c$\");
         * settings.ReplacePath(@"F:\", @"\\10.0.0.2\c$\");
         * amzSettings = new AmzIFace.AmazonSettings(settings.lavAmazonSettingsFile, Year);
         * amzSettings.ReplacePath(@"G:\", @"\\10.0.0.80\c$\");
         * amzSettings.ReplacePath(@"F:\", @"\\10.0.0.2\c$\");*/
        Session["settings"]    = settings;
        Session["amzSettings"] = amzSettings;
        aMerchant = new AmzIFace.AmazonMerchant(1, amzSettings.Year, amzSettings.marketPlacesFile, amzSettings);
        freeProds = Request.QueryString["freeProds"] != null && int.Parse(Request.QueryString["freeProds"].ToString()) > 0;

        OleDbConnection cnn = new OleDbConnection(settings.OleDbConnString);
        OleDbConnection wc  = new OleDbConnection(settings.lavOleDbConnection);

        wc.Open();
        cnn.Open();
        string errore = "";

        AmazonOrder.Order o;
        if (!CheckNomeLavoro(wc, Request.QueryString["amzOrd"].ToString(), amzSettings.AmazonMagaCode)) // ENTRA SE LAVORAZIONE NON ESISTENTE
        {
            if (Session[Request.QueryString["amzOrd"].ToString()] != null)
            {
                o = (AmazonOrder.Order)Session[Request.QueryString["amzOrd"].ToString()];
            }
            else
            {
                o = AmazonOrder.Order.ReadOrderByNumOrd(Request.QueryString["amzOrd"].ToString(), amzSettings, aMerchant, out errore);
            }

            if (o == null || errore != "")
            {
                Response.Write("Impossibile contattare amazon, riprova più tardi!<br />Errore: " + errore);
                cnn.Close();
                wc.Close();
                return;
            }

            string invnumb = (Request.QueryString["invnumb"] != null && Request.QueryString["invnumb"].ToString() != "") ?
                             "Ricevuta nr.:@ " + Request.QueryString["invnumb"].ToString() + " @": "";
            if (o.Items == null)
            {
                o.RequestItemsAndSKU(amzSettings, aMerchant, settings, cnn, wc);
            }

            AmazonOrder.Order.lavInfo info = OpenLavorazioneFromAmz(o, wc, cnn, amzSettings, settings, invnumb);
            InsertPrimoStorico(info.lavID, wc, op, settings);
            LavClass.SchedaLavoro.MakeFolder(settings, info.rivID, info.lavID, info.userID);
        }
        wc.Close();
        cnn.Close();

        Response.Redirect("amzPanoramica.aspx?token=" + Request.QueryString["token"].ToString() +
                          "&amzOrd=" + Request.QueryString["amzOrd"].ToString() + MakeQueryParams());
    }
    protected void LavGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.Cells.Count > idCol)
        {
            e.Row.Cells[clienteFID - 3].Visible = false;
            e.Row.Cells[operCol].Visible        = false;
            e.Row.Cells[usidCol].Visible        = false;
            e.Row.Cells[ordCol].Visible         = false;
            e.Row.Cells[idStatoCol].Visible     = false;
            //e.Row.Cells[opDispCol].Visible = false;
        }

        if (e.Row.RowIndex == -1)
        {
            return;
        }

        AmazonOrder.Order.lavInfo idlav = new AmazonOrder.Order.lavInfo();
        idlav.lavID  = int.Parse(e.Row.Cells[idCol].Text);
        idlav.rivID  = int.Parse(e.Row.Cells[rivcodCol].Text);
        idlav.userID = int.Parse(e.Row.Cells[clienteFID - 3].Text);

        e.Row.Cells[obCol].Text      = LavClass.Obiettivo.GetFromFile(obFile, "obiettivo", "id", int.Parse(e.Row.Cells[obCol].Text)).nome;
        e.Row.Cells[obCol].Font.Bold = true;
        LavClass.Priorita pr = LavClass.Priorita.GetFromFile(prFile, "npriorita", "id", int.Parse(e.Row.Cells[prCol].Text));
        e.Row.Cells[prCol].Text      = pr.nome;
        e.Row.Cells[prCol].Font.Bold = true;
        e.Row.Cells[prCol].BackColor = pr.colore;

        string allegati = "";

        if (LavClass.SchedaLavoro.HasEmptyAttach(idlav.lavID, idlav.rivID, idlav.userID, settings))
        {
            allegati = "<br /><font color='black' size='2px'><b>senza allegato</b></font>";
        }
        else if (!LavClass.SchedaLavoro.HasAllegati(idlav.lavID, idlav.rivID, idlav.userID, settings))
        {
            allegati = "<br /><font color='red' size='2px'><b>nessun allegato</b></font>";
        }
        e.Row.Cells[statoCol].Text = "<b>" + e.Row.Cells[statoCol].Text.Replace("#", "</b><br /><font size='1'>") + "</font>" + allegati;

        e.Row.Cells[userCol].Font.Size = 9;
        LavClass.Operatore userRow = LavClass.Operatore.GetFromFile(operFile, "operatore", "id", int.Parse(e.Row.Cells[userCol].Text), tipoOpFile, "tipo", "id");
        LavClass.Operatore operRow = LavClass.Operatore.GetFromFile(operFile, "operatore", "id", int.Parse(e.Row.Cells[operCol].Text), tipoOpFile, "tipo", "id");

        e.Row.Cells[userCol].Text = "<b>" + userRow.ToString() + "</b><br />(" + operRow.ToString() + ")";

        e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml(e.Row.Cells[usidCol].Text);

        //if (checkCookie(int.Parse(e.Row.Cells[idCol].Text), op.id, settings.lavCookieFile).ToLower() != LavClass.CookieLav.NormalizeCookie(e.Row.Cells[statoCol].Text))
        if (checkCookie(int.Parse(e.Row.Cells[idCol].Text), op.id, cookieX).ToLower() != LavClass.CookieLav.NormalizeCookie(e.Row.Cells[statoCol].Text))
        {
            e.Row.Cells[blinkCol].Text = "<img src='pics/star-blink.gif' width='30px' height='30px' />";
        }
        else
        {
            e.Row.Cells[blinkCol].Text = "";
        }

        e.Row.Cells[isNoteCol].Text = (HttpUtility.HtmlDecode(e.Row.Cells[isNoteCol].Text).Trim() != "") ?
                                      "<img src='pics/postit.png' width='30px' height='30px' title='" + HttpUtility.HtmlDecode(e.Row.Cells[isNoteCol].Text).Trim() + "' />" : "";

        string idvett;

        e.Row.Cells[isVettoreCol].Text = (idvett = HttpUtility.HtmlDecode(e.Row.Cells[isVettoreCol].Text).Trim()) != "0" && File.Exists(Server.MapPath("pics/vettori/" + idvett + ".png")) ?
                                         "<img src='pics/vettori/" + idvett + ".png' width='30px' height='30px' />" : "";
        //title='" + HttpUtility.HtmlDecode(e.Row.Cells[isVettoreCol].Text).Trim() + "' />" : "";

        e.Row.Cells[nomeLavCol].Font.Bold = true;
        e.Row.Cells[nomeLavCol].Font.Size = 12;

        Label lbId = new Label();

        lbId.Text      = int.Parse(e.Row.Cells[idCol].Text).ToString().PadLeft(5, '0');
        lbId.ID        = "lab_" + e.Row.Cells[idCol].Text;
        lbId.Width     = 45;
        lbId.Font.Size = 12;
        lbId.Font.Bold = true;
        e.Row.Cells[idCol].Controls.Add(lbId);

        if (e.Row.Cells[linkCol].Text != "0")
        {
            e.Row.Cells[linkCol].Text = "<a href='lavDettaglio.aspx?id=" + e.Row.Cells[idCol].Text + "&token=" + Session["token"].ToString() + "&merchantId=" + Request.QueryString["merchantId"].ToString() +
                                        "' target='_self'><img src='pics/info.png' width='30px' height='30px' /></a>";
        }
        else
        {
            e.Row.Cells[linkCol].Text = "";
        }

        e.Row.Cells[inserCol].Text      = "<b>" + e.Row.Cells[inserCol].Text.Replace(" ", "</b><br />");
        e.Row.Cells[inserCol].Font.Size = 9;

        if (DateTime.Parse(e.Row.Cells[consCol].Text) < DateTime.Today)
        {
            e.Row.Cells[consCol].BackColor = System.Drawing.Color.Red;
            e.Row.Cells[consCol].Style.Add("text-decoration", "blink");
            e.Row.Cells[consCol].Font.Bold = true;
        }
        Button b = new Button();

        e.Row.Cells[rdbCol].Controls.Add(b);
        b.ID            = "btn_" + e.Row.Cells[idCol].Text;
        b.Click        += b_Click;
        b.Text          = "*";
        b.OnClientClick = "btnVisible(" + idlav.lavID + ");scrollBottom();";

        AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();

        trigger.ControlID = b.UniqueID;
        trigger.EventName = "Click";
        updPan1.Triggers.Add(trigger);
    }
    private AmazonOrder.Order.lavInfo OpenLavorazioneFromAmz(AmazonOrder.Order order, OleDbConnection wc, OleDbConnection cnn, AmzIFace.AmazonSettings amzs, UtilityMaietta.genSettings s, string invoice)
    {
        string mailcl = order.buyer.emailCompratore;

        LavClass.UtenteLavoro ul = new LavClass.UtenteLavoro(mailcl, amzs.AmazonMagaCode, wc, cnn, s);
        LavClass.Operatore    opLav;
        LavClass.Macchina     mc = new LavClass.Macchina(amzs.lavMacchinaDef, s.lavMacchinaFile, s);
        LavClass.TipoStampa   ts = new LavClass.TipoStampa(amzs.lavTipoStampaDef, s.lavTipoStampaFile);
        LavClass.Obiettivo    ob = new LavClass.Obiettivo(amzs.lavObiettivoDef, s.lavObiettiviFile);
        //LavClass.Priorita pr = new LavClass.Priorita(amzs.lavPrioritaDef, s.lavPrioritaFile);
        LavClass.Priorita pr = new LavClass.Priorita(((order.ShipmentServiceLevelCategory.ShipmentLevelIs(AmazonOrder.ShipmentLevel.ESPRESSA)) ? amzs.lavPrioritaDefExpr : amzs.lavPrioritaDefStd),
                                                     s.lavPrioritaFile);
        LavClass.Operatore approvatore = new LavClass.Operatore(amzs.lavApprovatoreDef, s.lavOperatoreFile, s.lavTipoOperatoreFile);
        int lavid = 0;

        //double myprice;

        if (ul.id == 0) // NON HO MAIL UTENTE
        {
            // INSERISCI SAVE UTENTE
            UtilityMaietta.clienteFattura amazonRiv = new UtilityMaietta.clienteFattura(amzs.AmazonMagaCode, cnn, s);
            LavClass.UtenteLavoro.SaveUtente(amazonRiv, wc, order.buyer.nomeCompratore + " c/o " + order.destinatario.nome, order.buyer.emailCompratore, order.destinatario.ToString(), order.destinatario.ToStringFormatted());
            ul = new LavClass.UtenteLavoro(order.buyer.emailCompratore, amzs.AmazonMagaCode, wc, cnn, s);
        }

        if (ul.HasOperatorePref())
        {
            opLav = ul.OperatorePreferito();
        }
        else
        {
            opLav = new LavClass.Operatore(amzs.lavOperatoreDef, s.lavOperatoreFile, s.lavTipoOperatoreFile);
        }

        //string postit = ((order.GetSiglaVettore(cnn, amzs)) == postaSigla) ? "Spedizione con " + postaSigla : "";

        // INSERISCI SAVE LAVORAZIONE
        string testo = (invoice != "") ? "Lavorazione <b>" + order.canaleVendita.ToUpper() + "</b> automatica.<br /><br />" + invoice :
                       "Lavorazione <b>" + order.canaleVendita.ToUpper() + "</b> automatica.";

        lavid = LavClass.SchedaLavoro.SaveLavoro(wc, amzs.AmazonMagaCode, ul.id, opLav.id, mc.id, ts.id, ob.id, DateTime.Now, op,
                                                 null, null, true, approvatore.id, false, testo, "", order.dataSpedizione, order.orderid, pr.id);

        // ADD PRODOTTI
        ArrayList distinctMaietta;

        if (freeProds && Session["freeProds"] != null) /// VENGO DA RICEVUTA FREEINVOICE E CON SPUNTA CREA LAVORAZIONE
        {
            distinctMaietta = new ArrayList((List <AmzIFace.CodiciDist>)Session["freeProds"]);
            foreach (AmzIFace.CodiciDist codD in distinctMaietta)
            {
                LavClass.ProdottoLavoro.SaveProdotto(lavid, codD.maietta.idprodotto, codD.qt, "", codD.totPrice / codD.qt, false, wc);
            }
        }
        else if (order.Items != null)                                       /// VENGO DA PANORAMICA
        {
            distinctMaietta = FillDistinctCodes(order.Items, aMerchant, s); //, DateTime.Today);

            foreach (AmzIFace.CodiciDist codD in distinctMaietta)
            {
                LavClass.ProdottoLavoro.SaveProdotto(lavid, codD.maietta.idprodotto, codD.qt, "", codD.totPrice / codD.qt, false, wc);
            }
        }

        AmazonOrder.Order.lavInfo li = new AmazonOrder.Order.lavInfo();
        li.lavID  = lavid;
        li.rivID  = amzs.AmazonMagaCode;
        li.userID = ul.id;

        return(li);
    }