Dans le signalétique des articles, placer des boutons permettant de créer des nouveaux rayons, familles, sous-familles ou des catégories

0000002230     -      13/03/2024

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".

buttons_new_rfs

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 :

Zoom
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 :

Zoom
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 :

Zoom
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;
            }

        }
    }
}

Le code pour les catégories est :

Il faut adapter la valeur de la constant catNo au numéro de la catégorie.

Zoom
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using MercatorApi;
using System.Windows.Forms;


namespace MercatorUi.MovableControls.ButtonsCodes
{
    public static class Script
    {
        const int catNo = 1;

        public static void Exec(MercatorUi.MovableControls.MovableButton clickedButton)
        {
            MercatorUi.Forms.Sig.SigForm sigForm = (MercatorUi.Forms.Sig.SigForm)clickedButton.Form;
            string catTb = sigForm.Sig.Module == MercatorUi.Sig._SigEnum.STOCK ? "CAT_STCK" : "CAT_" + sigForm.Sig.Module;

            string newCatLib = Dialogs.AskString((string.Format("Quelle nouvelle catégorie {0} {1} ?", catNo, sigForm.Sig.Caption[MercatorUi.Globals.Langue])), "");
            if (string.IsNullOrEmpty(newCatLib))
                return;
            newCatLib = newCatLib.Trim();
            if (MercatorController.xFunctions.xLookUpString(catTb, "NOM", newCatLib, "ID", "type=" + catNo).TrimEnd() != "")
            {
                Dialogs.Stop("Cette catégorie existe déjà !");
                return;
            }
            string newCatId = Api.Ident();
            using (SqlCommand cmd = new SqlCommand(string.Format("insert into {0} (id,nom,type) values (@id,@nom,@type)", catTb)))
            {
                cmd.Parameters.AddWithValue("id", newCatId).SqlDbType = SqlDbType.Char;
                cmd.Parameters.AddWithValue("nom", newCatLib).SqlDbType = SqlDbType.Char;
                cmd.Parameters.AddWithValue("type", catNo);
                if (!Api.SqlExec(Globals.RepData, cmd))
                    return;
            }
            List<Control> l = clickedButton.Form.FindMovableControlsBySource("S_CAT" + catNo);
            if (l.Count > 0)
            {
                MovableControls.MovableComboBox combo = (MovableControls.MovableComboBox)l[0];
                System.ComponentModel.BindingList<MercatorUi._BaseClasses.MercatorComboItem> itemList = (System.ComponentModel.BindingList<MercatorUi._BaseClasses.MercatorComboItem>)combo.DataSource;
                itemList.Add(new MercatorUi._BaseClasses.MercatorComboItem(newCatLib, newCatId));
                combo.SelectedValue = newCatId;
            }
        }
    }
}