L'objectif de cette programmation est de permettre l'ajout direct de nouveaux rayons, familles et sous-familles, à partir du signalétique des articles. Cela se fait en ajoutant 3 boutons comme suit via le paramétrage de l'écran "articles".
Le/la rayon / famille / sous-famille créé(e) dans le fiche est automatiquement associé(e) à la fiche en cours.
Le code du bouton "Rayon" est :
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MercatorApi;
using MercatorController;
using MercatorUi;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace MercatorUi.MovableControls.ButtonsCodes
{
public static class Script
{
public static void Exec(MercatorUi.MovableControls.MovableButton clickedButton)
{
string newRayonLib = Dialogs.AskString(("Quel nouvel rayon ?"), "");
if (string.IsNullOrEmpty(newRayonLib))
return;
newRayonLib = newRayonLib.Trim();
if (MercatorController.xFunctions.xLookUpString("RAYONS", "NOM", newRayonLib, "ID").TrimEnd() != "")
{
Dialogs.Stop("Ce rayon existe déjà !");
return;
}
string newRayonId = Api.Ident();
using (SqlCommand cmd = new SqlCommand("insert into RAYONS (id,nom) values (@id,@nom)"))
{
cmd.Parameters.AddWithValue("id", newRayonId).SqlDbType = SqlDbType.Char;
cmd.Parameters.AddWithValue("nom", newRayonLib).SqlDbType = SqlDbType.Char;
if (!Api.SqlExec(Globals.RepData, cmd))
return;
}
List<Control> l = clickedButton.Form.FindMovableControlsBySource("S_ID_RAYON");
if (l.Count > 0)
{
MovableControls.MovableComboBox combo = (MovableControls.MovableComboBox)l[0];
DataTable dt_rayons = (DataTable)combo.DataSource;
DataRow dr_new = dt_rayons.NewRow();
Api.DataRowResetContent(dr_new);
dr_new["id"] = newRayonId;
dr_new["nom"] = newRayonLib;
dt_rayons.Rows.Add(dr_new);
combo.SelectedValue = newRayonId;
}
}
}
}
Le code du bouton "Famille" est :
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MercatorApi;
using MercatorController;
using MercatorUi;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.ComponentModel;
namespace MercatorUi.MovableControls.ButtonsCodes
{
public static class Script
{
public static void Exec(MercatorUi.MovableControls.MovableButton clickedButton)
{
string s_id_rayon = clickedButton.Form.DataSource.Rows[0]["s_id_rayon"].ToString();
if (s_id_rayon == "")
{
Dialogs.Stop("Vous devez d'abord choisir un rayon !");
return;
}
string newFamilleLib = Dialogs.AskString(("Quelle nouvelle famille ?"), "");
if (string.IsNullOrEmpty(newFamilleLib))
return;
newFamilleLib = newFamilleLib.Trim();
if (MercatorController.xFunctions.xLookUpString("FAMILLES", "NOM", newFamilleLib, "ID", string.Format("id_rayon='{0}'", Api.UnquoteSql(s_id_rayon))).TrimEnd() != "")
{
Dialogs.Stop("Cette famille existe déjà !");
return;
}
string newFamilleId = Api.Ident();
using (SqlCommand cmd = new SqlCommand("insert into FAMILLES (id,nom,id_rayon) values (@id,@nom,@id_rayon)"))
{
cmd.Parameters.AddWithValue("id", newFamilleId).SqlDbType = SqlDbType.Char;
cmd.Parameters.AddWithValue("nom", newFamilleLib).SqlDbType = SqlDbType.Char;
cmd.Parameters.AddWithValue("id_rayon", s_id_rayon).SqlDbType = SqlDbType.Char;
if (!Api.SqlExec(Globals.RepData, cmd))
return;
}
List<Control> l = clickedButton.Form.FindMovableControlsBySource("S_ID_FAMIL");
if (l.Count > 0)
{
MovableControls.MovableComboBox combo = (MovableControls.MovableComboBox)l[0];
BindingList<MercatorUi._BaseClasses.MercatorComboItem> l_familles = (BindingList<MercatorUi._BaseClasses.MercatorComboItem>)combo.DataSource;
l_familles.Add(new MercatorUi._BaseClasses.MercatorComboItem(newFamilleLib, newFamilleId));
combo.SelectedValue = newFamilleId;
}
}
}
}
Le code du bouton "Sous-famille" est :
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MercatorApi;
using MercatorController;
using MercatorUi;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.ComponentModel;
namespace MercatorUi.MovableControls.ButtonsCodes
{
public static class Script
{
public static void Exec(MercatorUi.MovableControls.MovableButton clickedButton)
{
string s_id_famil = clickedButton.Form.DataSource.Rows[0]["s_id_famil"].ToString();
if (s_id_famil == "")
{
Dialogs.Stop("Vous devez d'abord choisir une famille !");
return;
}
string newSsFamilleLib = Dialogs.AskString(("Quelle nouvelle sous-famille ?"), "");
if (string.IsNullOrEmpty(newSsFamilleLib))
return;
newSsFamilleLib = newSsFamilleLib.Trim();
if (MercatorController.xFunctions.xLookUpString("SS_FAMIL", "NOM", newSsFamilleLib, "ID", string.Format("id_famille='{0}'", Api.UnquoteSql(s_id_famil))).TrimEnd() != "")
{
Dialogs.Stop("Cette sous-famille existe déjà !");
return;
}
string newSsFamilleId = Api.Ident();
using (SqlCommand cmd = new SqlCommand("insert into SS_FAMIL (id,nom,id_famille) values (@id,@nom,@id_famille)"))
{
cmd.Parameters.AddWithValue("id", newSsFamilleId).SqlDbType = SqlDbType.Char;
cmd.Parameters.AddWithValue("nom", newSsFamilleLib).SqlDbType = SqlDbType.Char;
cmd.Parameters.AddWithValue("id_famille", s_id_famil).SqlDbType = SqlDbType.Char;
if (!Api.SqlExec(Globals.RepData, cmd))
return;
}
List<Control> l = clickedButton.Form.FindMovableControlsBySource("S_ID_SSFAM");
if (l.Count > 0)
{
MovableControls.MovableComboBox combo = (MovableControls.MovableComboBox)l[0];
BindingList<MercatorUi._BaseClasses.MercatorComboItem> l_familles = (BindingList<MercatorUi._BaseClasses.MercatorComboItem>)combo.DataSource;
l_familles.Add(new MercatorUi._BaseClasses.MercatorComboItem(newSsFamilleLib, newSsFamilleId));
combo.SelectedValue = newSsFamilleId;
}
}
}
}