using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using MercatorApi;
using MercatorUi;
using MercatorUi.Forms.Sig;
using MercatorUi.Wait;

namespace Main
{
    public class Customizer : MercatorUi.ICustomizers.IExec
    {
        public void Main(MercatorUi.ICustomizers.ExecAction Action)
        {
            if (Action == MercatorUi.ICustomizers.ExecAction.MenuCreated)
            {
                //Een entry toevoegen in het menu Beheer om de minimum stockhoeveelheid te herberekenen voor een bepaalde periode.
                var menu = _Divers.MenuElementByText(_Divers.Iif_langue(MercatorUi.Globals.Langue, "Verwaltung", "Administration", "Beheer", "Gestion"));
                if (menu != null)
                    menu.SubItems.Add(new MercatorUi._BaseClasses.MenuElement("Màj stocks MIN par période", "", null, StockMin_Click));
            }
        }

        void StockMin_Click(object sender, EventArgs e)
        {
            // Bijwerken van de minimumvoorraad op basis van verkopen uit een vorige periode
            var depot = Dialogs.AskDepot("Voor welk depot?");
            if (depot == null)
                return;

            bool bDefaultDepot = (Globals.DepotDef == depot.Id);

            var dates = Dialogs.AskDates("Op welke periode moet men zich baseren om de MIN-stock te herberekenen?");
            if (dates == null || dates.GetLength(0) < 2)
                return;

            string filter = "";
            Form wonTop = Globals.Main.WonTopForm();
            if (wonTop != null)
            {
                var sigForm = wonTop as SigForm;
                if (sigForm != null && sigForm.Sig.Module == MercatorUi.Sig._SigEnum.STOCK)
                    filter = sigForm.Filter;
            }

            if (!string.IsNullOrEmpty(filter))
                if (!Dialogs.AnswerYesNo(string.Format("Filter toepassen '{0}' ?", filter)))
                    filter = "";
                else
                    filter = " and " + filter;

            string sqlQuery = @"select LIGNES_V.ID_ARTICLE, sum(LIGNES_V.Q) as Tot_Q
                                   from PIEDS_V
                                    inner join LIGNES_V on PIEDS_V.ID = LIGNES_V.ID and PIEDS_V.JOURNAL = LIGNES_V.JOURNAL and PIEDS_V.PIECE = LIGNES_V.PIECE
                                    inner join STOCK on LIGNES_V.ID_ARTICLE = STOCK.S_ID -- this join is needed for filters to work
                                   where (PIEDS_V.TYPE = 1)
                                         and (LIGNES_V.ID_ARTICLE <> '')
                                         and (PIEDS_V.DATE between @DATE_1 and @DATE_2)
                                    and LIGNES_V.Q <> 0
                                    and PIEDS_V.ID_DEPOT = @ID_DEPOT
                                    {0}
                                   group by ID_ARTICLE
                                   order by ID_ARTICLE";

            sqlQuery = string.Format(sqlQuery, filter);

            WaitStatic.WaitWindow("Verkochte hoeveelheden berekenen op deze periode...");
            DataSet dataSet = Api.Zselect(Globals.RepData, sqlQuery, new MercatorSqlParam("@DATE_1"dates[0]), new MercatorSqlParam("@DATE_2"dates[1]), new MercatorSqlParam("@ID_DEPOT"depot.Id));
            WaitStatic.WaitClear();

            if (dataSet != null && dataSet.Tables[0].Rows.Count > 0)
            {
                if (Dialogs.AnswerYesNo(string.Format("{0} entries gevonden.\r\n\r\n Update de minimum-stock van deze artikelen?", dataSet.Tables[0].Rows.Count)))
                {
                    Progress.ProgressCreate(dataSet.Tables[0].Rows.Count);
                    var stringBuilder = new StringBuilder();

                    // Nulstelling van alle artikelen die door de filter worden teruggegeven (of van alle artikelen indien er geen filter is).
                    if (bDefaultDepot)
                        stringBuilder.AppendFormat("update STOCK set S_STOCKMIN = 0 where S_STOCKMIN<>0{0}\r\n", filter);
                    stringBuilder.AppendFormat("update dispo set dispo.STOCKMIN = 0 from DISPO inner join STOCK on dispo.ID_STOCK = stock.S_ID where STOCKMIN<>0 and ID_MAGASIN = '{0}'{1}\r\n\r\n", depot.Id, filter);

                    Api.TrimDataTable(dataSet.Tables[0]);
                    foreach (DataRow row in dataSet.Tables[0].Rows)
                    {
                        decimal Q = Convert.ToDecimal(row["Tot_Q"]);
                        string s_id = (string)row["ID_ARTICLE"];

                        // Toevoeging 10%
                        Q = Math.Ceiling(Q * 1.1m);

                        if (bDefaultDepot)
                            stringBuilder.AppendFormat("update STOCK set S_STOCKMIN = {0} where S_ID = '{1}'\r\n", Q, Api.UnquoteSql(s_id));

                        stringBuilder.AppendFormat("update DISPO set STOCKMIN = {0} where ID_STOCK = '{1}' and ID_MAGASIN = '{2}'\r\n\r\n", Q, s_id, depot.Id);

                        Progress.ProgressIncrement(1);
                    }

                    WaitStatic.WaitWindow("Update minimum stock...");

                    bool result = Api.SqlExec(Globals.RepData, stringBuilder.ToString());
                    WaitStatic.WaitClear();
                    Progress.ProgressDestroy();

                    if (result)
                        Dialogs.Stop("Update met succes beëindigd!");
                }
            }
            else
                Dialogs.Stop("Geen enkele verkoop werd gevonden voor deze periode en dit depot!");
        }
    }
}