Le code doit être placé dans un customizer Billing créé à partir de la séquence correspondante. Il implémente les interfaces suivantes :
Il exploite essentiellement ces évènements du BillingEngine du document en cours :
- AfterInsertItem
- BeforeBeforePaymentOrSave
- AfterSave
Le code s'établit comme suit :
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Linq;
using MercatorApi;
using MercatorUi;
using MercatorExtensions;
namespace Billing
{
public class Customizer : MercatorUi.ICustomizers.IBillingEngineCreated, MercatorUi.ICustomizers.IBillingEngineClosed
{
public void BillingEngineCreated(MercatorUi.Engine.Gescom.BillingEngine BillingEngine)
{
BillingEngine.AfterInsertItem += AfterInsertItem;
BillingEngine.BeforeBeforePaymentOrSave += BeforeBeforePaymentOrSave;
BillingEngine.AfterSave += AfterSave;
}
public void BillingEngineClosed(MercatorUi.Engine.Gescom.BillingEngine BillingEngine)
{
BillingEngine.AfterInsertItem -= AfterInsertItem;
BillingEngine.BeforeBeforePaymentOrSave -= BeforeBeforePaymentOrSave;
BillingEngine.AfterSave -= AfterSave;
}
void AfterInsertItem(object sender, MercatorUi.Engine.Gescom.BillingEngine.AfterInsertItemEventArgs e)
{
MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;
if (e.LignesVRecord.S_CLE1 == "UTILBONVAL")
{
string id_bonval = Dialogs.AskString("Veuillez scanner le code barre", "", true, 35);
if (!string.IsNullOrWhiteSpace(id_bonval))
{
id_bonval = id_bonval.TrimEnd();
int nbBon = billingEngine.LignesVRecords.Where(l => l.ID_BONVAL == id_bonval).Count();
if (nbBon == 0)
{
string reqSql = "select count(*) as nbUtil, sum(q) as q, sum(pu) as pu from lignes_v where id_bonval=@id_bonval";
DataSet ds = Api.Zselect(Globals.RepData, reqSql, new MercatorSqlParam("@id_bonval", id_bonval, SqlDbType.Char));
if (ds != null)
{
if (ds.Tables[0].Rows[0].Value<Int16>("nbUtil") == 0)
{
Dialogs.Stop("Ce bon à valoir n'existe pas !");
e.CancelInsertItem = true;
}
else if (ds.Tables[0].Rows[0].Value<Int16>("nbUtil") > 1)
{
Dialogs.Stop("Ce bon à valoir a déjà été utilisé !");
e.CancelInsertItem = true;
}
else
{
e.LignesVRecord.Q = ds.Tables[0].Rows[0].Value<double>("q") * -1;
e.LignesVRecord.PU = ds.Tables[0].Rows[0].Value<double>("pu");
e.LignesVRecord.ID_BONVAL = id_bonval;
}
}
else
{
e.CancelInsertItem = true;
}
}
else
{
Dialogs.Stop("Ce bon à valoir a déjà été utilisé au sein de cette vente !");
e.CancelInsertItem = true;
}
}
else
{
Dialogs.Stop("Code-Barres Vide !");
e.CancelInsertItem = true;
}
}
}
private void BeforeBeforePaymentOrSave(object sender, MercatorUi.Engine.Gescom.BillingEngine.BeforeBeforePaymentOrSaveEventArgs e)
{
MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;
if (billingEngine.PiedsVRecord.TOT_TTC_FB.CompareTo(0, Globals.DEC_DEV_B) < 0)
{
MercatorUi.Sig.Sig sigStock = MercatorUi.Sig._SigsStatic.SigByModule(MercatorUi.Sig._SigEnum.STOCK);
string reqSql = string.Format("select {0} from stock (NOLOCK) where s_cle1 ='BONVAL'", sigStock.FieldsListForSelectRtrimNoBytes);
DataSet dsStock = Api.Zselect(Globals.RepData, reqSql);
if ((dsStock == null) || (dsStock.Tables[0].Rows.Count == 0))
{
Dialogs.Stop("Article non trouvé !");
}
else
{
int nLigne = billingEngine.AppendLine();
if (nLigne < 0)
{
Dialogs.Stop("AppendLine BillingEngine : " + billingEngine.LastError + (Api.LastError == "" ? " - " + Api.LastError : ""));
}
else if (!billingEngine.InsertItem(dsStock.Tables[0].Rows[0], billingEngine.LIGNES.Rows[nLigne]))
{
Dialogs.Stop("InsertItem BillingEngine : " + billingEngine.LastError + (Api.LastError == "" ? " - " + Api.LastError : ""));
}
else
{
billingEngine.LignesVRecords[nLigne].PU = Math.Abs(billingEngine.PiedsVRecord.TOT_TTC_FB);
billingEngine.LignesVRecords[nLigne].ID_BONVAL = billingEngine.PiedsVRecord.ID + billingEngine.LignesVRecords[nLigne].DL_ID;
billingEngine.UpdateAmounts();
}
}
}
}
void AfterSave(object sender, EventArgs e)
{
MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;
if (billingEngine.LignesVRecords.Where(l => l.S_CLE1 == "BONVAL").Count() != 0)
{
DataSet ds;
foreach (DataRow drBC in billingEngine.LIGNES.Select("S_CLE1='BONVAL'"))
{
ds = new DataSet();
DataTable dt_lignes = billingEngine.LIGNES.Clone();
dt_lignes.ImportRow(drBC);
DataTable dt_pieds = billingEngine.PIEDS.Table.Clone();
dt_pieds.ImportRow(billingEngine.PIEDS);
ds.Tables.Add(dt_lignes);
ds.Tables.Add(dt_pieds);
ds.Tables.Add(Api.DataTableFromDico(MercatorUi.Globals.ParamIdentif, "ParamIdentif"));
/* Edit Layout
//string reportFileName = Globals.MainDir + "BonValoir.repx";
string reportFileName = @"<MainDir\BonValoir.repx";
string reportLayout = MercatorUi.Reporting.ReportingStatic.Reporting.EditLayout(Api.JustStem(reportFileName), Api.FileToStr(reportFileName, Encoding.UTF8), ds);
Api.StrToFile(reportLayout, reportFileName, Encoding.UTF8);
*/
List<MercatorUi.Reporting.OutputDescriptor> listOutputDescriptors = new List<MercatorUi.Reporting.OutputDescriptor>();
// Preview
//listOutputDescriptors.Add(new MercatorUi.Reporting.OutputDescriptorPreview ());
//
listOutputDescriptors.Add(new MercatorUi.Reporting.OutputDescriptorPrint());
//MercatorUi.Reporting.ReportingStatic.Reporting.RunReport("BonValoir", Globals.MainDir + "BonValoir.repx", ds, listOutputDescriptors);
MercatorUi.Reporting.ReportingStatic.Reporting.RunReport("BonValoir", @"<MainDir\BonValoir.repx", ds, listOutputDescriptors);
}
}
}
}
}
Le modèle d'impression est disponible dans le fichier zip.
Au préalable, il faut :
- ajouter le champ lignes_v.id_bonval
- Ajouter un index SQL sur ce champ
- Mettre à jour le schéma de la base de données via les outils avancés
- Créer 2 articles (sans gestion de stock)En législation Belge, nous conseillons de mettre un taux de TVA à 0% sur ces articles, ainsi que de définir un compte de la classe 46.
- "Bon à valoir" (clé 1 = "BONVAL")
- "Utilisation bon à valoir" (clé 1 = "UTILBONVAL").
alter table LIGNES_V add ID_BONVAL char(35) not null default ''
create index ID_BONVAL on dbo.LIGNES_V (ID_BONVAL)
A télécharger :
0000002200.zip (2 Kb - 24/04/2013)