Page 1 of 1

Cheque EFT payments - discount wont get saved

PostPosted: Thu Aug 11, 2016 5:57 pm
by Riyaz
Hi There

Have created a plugin which automatically calculates the Home Discount Amount for invoices that are eligible for discount (considering paid within certain days as mentioned in the Creditor) ,

This discount processing triggers when the SELECT button is clicked on the Creditor Cheque/EFT payment screen.

Am facing two issues here

#1, After the discounts are auto populated in the respective Home Discount Amount grid cell , if I click SAVE button, the discount goes back to ZERO

#2, After the discounts are auto populated in the respective Home Discount Amount grid cell , if I DELETE any of the grid records prior to saving, the discount goes back to ZERO

What works is

if I manually enter a discount amount, that is perfectly retained under both the above cases.

Your help is highly appreciated, pls let me know on whats happening here. I've also tried to save the values in the database as it alters the gridlines to see if that matters, but apparently not.

Re: Cheque EFT payments - discount wont get saved

PostPosted: Thu Aug 11, 2016 7:47 pm
by Mike.Sheen
Hi Riyaz,

Can you provide the plugin you have which demonstrates the issue? We'll be able to troubleshoot this much quicker that way.

Mike

Re: Cheque EFT payments - discount wont get saved

PostPosted: Thu Aug 11, 2016 8:33 pm
by Riyaz
Thanks for responding, pls find the plugin attached

Re: Cheque EFT payments - discount wont get saved

PostPosted: Tue Aug 16, 2016 1:07 pm
by Riyaz
Hi there

Pls let me know on this, thanks

Re: Cheque EFT payments - discount wont get saved

PostPosted: Fri Aug 19, 2016 3:10 pm
by Riyaz
Hello Jiwa Admins

Can anyone pls kindly get back on this, I would really appreciate it. Thanks

Re: Cheque EFT payments - discount wont get saved

PostPosted: Wed Aug 24, 2016 9:48 pm
by Mike.Sheen
Riyaz wrote:Hello Jiwa Admins

Can anyone pls kindly get back on this, I would really appreciate it. Thanks


The problem you have is because you are looking at grid cell contents, and also setting those instead of the business logic properties. Pretty much everywhere in Jiwa the rule is to talk to the business logic properties when examining and setting values. Don't talk to the grid unless you want to simply alter the appearance. If you want to alter values, talk to the business logic.

If you set cell contents on a grid via code the business logic doesn't receive the new value and this saving it will cause it to go back to the prior value.

To give you a some guidance, I've looked at your plugin and can see in your SaveStart where you begin to iterate through the grid cell contents for each row and make decisions / calculations based on that - Don't do that - instead use the business logic attached to the form. In your plugin you already added a handler to the SaveStart of the business logic within the Form, so the sender argument is the business logic:

Code: Select all
private void SaveStart(object sender, System.EventArgs e)
{      
   var creditorChequePaymentBusinessLogic = (JiwaFinancials.Jiwa.JiwaCreditorChqPay.CreditorChequePayment)sender;
   
   foreach (JiwaFinancials.Jiwa.JiwaCreditorChqPay.Creditor creditor in creditorChequePaymentBusinessLogic.Creditors)
   {
      // read payment terms here for the creditor.  No sense doing it on the following inner loop as they're the same creditor.
      
      foreach (JiwaFinancials.Jiwa.JiwaCreditorChqPay.clsPayLineInvoice invoice in creditor.InvoicePaymentLines)
      {            
         // apply discount here: *** NOTE *** You might want to opt to only do this if creditorChequePamentBusinessLogic.InsertFlag == true,
         // as that is when it is first saved after being created
         //BUT wrap that condition around ALL the code in this handler not just this inner loop, otherwise you'd be hitting the database on every save, not just the initial save.
         invoice.DiscountAmount = ? ;
      }
   }
}


This should set you on your way.

Mike

PS: I insist you use parameters for your SQL queries - your building of a string is BAD. YOU MUST FIX THIS. A malicious user could create a creditor with account no. of '111; DELETE FROM CR_Main' and your query would happily delete all creditors from the database - this is a basic SQL Injection attack, and all your queries direct to the database should prevent against that - using parameters with your SQL command will guard against this.

You should also wrap all queries in a using statement to make sure everything is disposed of correctly.

Also, don't use GetNewConnection like you are. Use the current SQL connection or you're going to have serious integrity issues!

So instead of:

Code: Select all
SqlCommand cmd=new SqlCommand("select EarlyPayDisOnRemitDays,EarlyPayDisOnRemitAmt from CR_Main where AccountNo ='"+ AccountNo +"'",JiwaFinancials.Jiwa.JiwaApplication.Manager.Instance.Database.GetNewConnection(JiwaFinancials.Jiwa.JiwaApplication.Manager.Instance.Database.ConnectionString));
      SqlDataReader reader= JiwaFinancials.Jiwa.JiwaApplication.Manager.Instance.Database.ExecuteReader(cmd);


use:

Code: Select all
int EarlyPayDisOnRemitDays = 0;
decimal EarlyPayDisOnRemitAmt = 0;

var db = JiwaFinancials.Jiwa.JiwaApplication.Manager.Instance.Database;
System.Data.SqlClient.SqlParameter SQLParam = null;
System.Data.SqlClient.SqlDataReader SQLReader = null;
try
{
   string SQL = @"SELECT EarlyPayDisOnRemitDays, EarlyPayDisOnRemitAmt
         FROM CR_Main WHERE
         AccountNo = @AccountNo";

   using (SqlCommand SQLCmd = new SqlCommand(SQL, db.SQLConnection, db.SQLTransaction))
   {
      SQLParam = new SqlParameter("@AccountNo", System.Data.SqlDbType.VarChar);
      SQLParam.Value = AccountNo;
      SQLCmd.Parameters.Add(SQLParam);
      
      SQLReader = db.ExecuteReader(SQLCmd);

      if (SQLReader.Read() == true)
      {
         EarlyPayDisOnRemitDays = int.Parse(db.Sanitise(SQLReader, "EarlyPayDisOnRemitDays").ToString());
         EarlyPayDisOnRemitAmt = decimal.Parse(db.Sanitise(SQLReader, "EarlyPayDisOnRemitAmt").ToString());
      }
   }

   SQLReader.Close();
}
finally
{
   if (SQLReader != null)
   {
      SQLReader.Close();
   }
}


That way you will use the current connection and transaction. Not doing it as per above will cause very hard to troubleshoot issues (weird errors sometimes, but not always).

Re: Cheque EFT payments - discount wont get saved

PostPosted: Mon Aug 29, 2016 1:37 pm
by Riyaz
Hi Mike

Thanks very much for getting back, as advised by yourself I had moved the code under SaveStart of BusinessLogic region, BUT it wont trigger. I tried having a MessageBox in it it as well to see if it passes through the BusinessLogic section.

I then tried to create a simple plugin to just messagebox on save event under BusinessLogic region, still wont work. Not sure what I'm missing here. Pls see the plugin attached, if possible advise a sample coding in C# as well.

P.S: I also tried the SaveStart event of JiwaCreditorChqPay , instead of JiwaCreditorChqPayUI as used in the attached plugin. But still no luck.

thanks for helping

Re: Cheque EFT payments - discount wont get saved

PostPosted: Mon Sep 12, 2016 1:38 pm
by Riyaz
Hi There

Pls let me know on this, I would really appreciate it

Re: Cheque EFT payments - discount wont get saved

PostPosted: Thu Nov 10, 2016 8:20 pm
by Riyaz
Hi There

Pls advise on this, thanks

Re: Cheque EFT payments - discount wont get saved  Topic is solved

PostPosted: Sun Nov 13, 2016 1:06 pm
by Mike.Sheen
Riyaz wrote:Hi Mike

Thanks very much for getting back, as advised by yourself I had moved the code under SaveStart of BusinessLogic region, BUT it wont trigger. I tried having a MessageBox in it it as well to see if it passes through the BusinessLogic section.

I then tried to create a simple plugin to just messagebox on save event under BusinessLogic region, still wont work. Not sure what I'm missing here. Pls see the plugin attached, if possible advise a sample coding in C# as well.

P.S: I also tried the SaveStart event of JiwaCreditorChqPay , instead of JiwaCreditorChqPayUI as used in the attached plugin. But still no luck.

thanks for helping


In your provided sample you've tried to hook into the form's tool click from within the business logic class - this won't work as the business logic doesn't know about the form.

If you want to perform actions based on a ribbon tool click, then you need to add those handlers in the FormPlugin class instead.

I've attached a modified version of your plugin which does this.

PS: Your plugin name is somewhat confusing - Creditor EFT payments has nothing to do with debtors!