using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MercatorApi;
using MercatorUi;
using System.Windows.Forms;
using System.Linq;
using MercatorExtensions;
namespace SigStock
{
public class Customizer : MercatorUi.ICustomizers.IFormLoadCustomizer, MercatorUi.ICustomizers.IFormClosedCustomizer
{
public void FormLoadCustomize(Form form)
{
MercatorUi.Forms.Sig.SigForm sigForm = (MercatorUi.Forms.Sig.SigForm)form;
MercatorUi.Forms.Sig.SigGrids.ArtFou artFou = sigForm.MovableControls.Values.OfType<MercatorUi.Forms.Sig.SigGrids.ArtFou>().FirstOrDefault();
if (artFou != null)
{
artFou.AfterColumnsCreated += Artfou_AfterColumnsCreated;
artFou.Grid.CellFormatting += Artfou_CellFormatting;
artFou.Grid.CellValidated += Artfou_CellValidated;
}
}
public void FormClosedCustomize(Form form)
{
MercatorUi.Forms.Sig.SigForm sigForm = (MercatorUi.Forms.Sig.SigForm)form;
MercatorUi.Forms.Sig.SigGrids.ArtFou artFou = sigForm.MovableControls.Values.OfType<MercatorUi.Forms.Sig.SigGrids.ArtFou>().FirstOrDefault();
if (artFou != null)
{
artFou.AfterColumnsCreated -= Artfou_AfterColumnsCreated;
artFou.Grid.CellFormatting -= Artfou_CellFormatting;
artFou.Grid.CellValidated -= Artfou_CellValidated;
}
}
private void Artfou_AfterColumnsCreated(object sender, EventArgs e) // ajouter la colonne "Prix Net"
{
MercatorUi.Forms.Sig.SigGrids.ArtFou artFou = (MercatorUi.Forms.Sig.SigGrids.ArtFou)sender;
artFou.Grid.Columns.Add("prix_net", _Divers.Iif_langue(Globals.Langue, "Net Pr.", "Nettoprijs", "Prix net"));
artFou.Grid.Columns["prix_net"].Width = 85;
artFou.Grid.Columns["prix_net"].ReadOnly = true;
artFou.Grid.Columns["prix_net"].DefaultCellStyle.ForeColor = System.Drawing.Color.Gray;
artFou.Grid.Columns["prix_net"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
// Cette colonne est volontairement non liée à un DataPropertyName.
// Elle sera calculée par l'évènement CellFormatting de la grille
}
private void Artfou_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridView grid = (DataGridView)sender;
if ((e.ColumnIndex > -1) && (e.RowIndex > -1) && (grid.Columns[e.ColumnIndex].Name == "prix_net") && grid.Columns.Contains("prix") && grid.Columns.Contains("remise") && grid.Columns.Contains("n_dec"))
refreshPrixNetCell(grid, e.RowIndex, e);
}
private void refreshPrixNetCell(DataGridView grid, int rowIndex, DataGridViewCellFormattingEventArgs e)
{
double prix = Convert.ToDouble(grid.Rows[rowIndex].Cells["prix"].Value);
double remise = Convert.ToDouble(grid.Rows[rowIndex].Cells["remise"].Value);
double remise2 = 0;
if (grid.Columns.Contains("remise2"))
remise2 = Convert.ToDouble(grid.Rows[rowIndex].Cells["remise2"].Value);
double remise3 = 0;
if (grid.Columns.Contains("remise3"))
remise3 = Convert.ToDouble(grid.Rows[rowIndex].Cells["remise3"].Value);
double remise4 = 0;
if (grid.Columns.Contains("remise4"))
remise4 = Convert.ToDouble(grid.Rows[rowIndex].Cells["remise4"].Value);
int n_dec = Convert.ToInt32(grid.Rows[rowIndex].Cells["n_dec"].Value);
string mask = "# ### ### ##0" + (n_dec > 0 ? "." : "") + Api.Replicate("0", n_dec);
string prix_net = string.Format("{0:" + mask + "}", Math.Round(prix * ((100 - remise) / 100) * ((100 - remise2) / 100) * ((100 - remise3) / 100) * ((100 - remise4) / 100), 2));
if (e != null)
e.Value = prix_net; // on vient de Artfou_CellFormatting
else
grid.Rows[rowIndex].Cells["prix_net"].Value = prix_net; // on vient de Artfou_CellValidated
}
private void Artfou_CellValidated(object sender, DataGridViewCellEventArgs e) // si on change le contenu de prix, remise, ... -> il faut adapter le prix net sur cette ligne
{
DataGridView grid = (DataGridView)sender;
if ((e.ColumnIndex > -1) && (e.RowIndex > -1) && grid.Columns[e.ColumnIndex].Name.EqualsOr("prix", "remise", "remise2", "remise3", "remise4"))
refreshPrixNetCell(grid, e.RowIndex, null);
}
}
}