using framework_business;
using framework_library;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Text;
using System.Data;
using System.Runtime.Serialization;
using System.Collections;
public partial class controls_module_custom_billingInstructions : System.Web.UI.UserControl, ISurfaceControlBase
{
public void ReloadControl(oSurfaceFieldData controlFieldData, bool alternateView = false, bool readOnly = false)
{
try
{
try
{
// Add ValidationGroup="Properties" to btnAddBillingInstruction button
ISurfaceBase parent = (ISurfaceBase)this.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent;
string parentName = parent.AppRelativeVirtualPath;
int iLastIndex = parentName.LastIndexOf("/") + 1;
string validate = parentName.Substring(iLastIndex).Replace("_form.ascx", "");
btnAddBillingInstruction.ValidationGroup = validate;
// TODO: Fix after tenants
btnAddBillingInstruction.Visible = false;
}
catch
{
}
SurfaceId = controlFieldData.surfaceId;
SurfaceItemId = controlFieldData.surfaceItemId;
foreach (oSurfaceField field in xData.GetTypedByCriteriaSpecific("recId", typeof(oSurfaceField), "recId", controlFieldData.surfaceFieldID.ToString()))
{
SurfaceFieldId = field.recId;
}
ReadBillingInstructionsForSurface(SurfaceItemId);
}
catch (Exception ex)
{
exception.HandleException("billingInstructions:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
public void SaveControlData(ref oSurfaceFieldData _surfaceFieldData)
{
//throw new NotImplementedException();
try
{
GetRepeaterData();
// If the SurfaceItemId = 0, we need to update the Billing Instructions Object with the Property ID
if (SurfaceItemId == 0)
UpdateBillingInstructionsObject(_surfaceFieldData.surfaceItemId, BillingInstructions_PlaceHolderValue);
PropertyBillingInstructions pbi = new PropertyBillingInstructions();
pbi.BillingInstructions = BillingInstructions;
//if (SurfaceItemId == 0)
SurfaceItemId = _surfaceFieldData.surfaceItemId;
pbi.SurfaceItemId = SurfaceItemId;
CreateBillingInstructionJSON(pbi);
}
catch (Exception ex)
{
exception.HandleException("billingInstructions:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
finally
{
// Reload the parent control if a new Primary Image has been uploaded
ISurfaceBase parent = (ISurfaceBase)this.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent;
foreach (oSurfaceItem item in xData.GetTypedByCriteriaSpecific("recId", typeof(oSurfaceItem), "recId", SurfaceItemId.ToString()))
{
parent.ReloadControl(item, false, false);
}
}
}
///
/// Called from btnSaveBillingInstruction_Click
///
private void SaveOnNew()
{
// SaveOnNew already refreshes parent. Don't refresh again.
GetRepeaterData();
PropertyBillingInstructions pbi = new PropertyBillingInstructions();
pbi.BillingInstructions = BillingInstructions;
if (SurfaceItemId == 0)
SurfaceItemId = BillingInstructions_PlaceHolderValue;
pbi.SurfaceItemId = SurfaceItemId;
CreateBillingInstructionJSON(pbi);
}
private void GetRepeaterData()
{
HiddenField hfBillingInstructions = (HiddenField)this.FindControl("sfGallery").FindControl("hfBillingInstructions");
if (!string.IsNullOrEmpty(hfBillingInstructions.Value))
{
string billingInstructionChanges = hfBillingInstructions.Value.ToString();
List lstInstructions = billingInstructionChanges.Split(';').ToList();
foreach (string item in lstInstructions)
{
List lstChange = item.Split('|').ToList();
//0AccountNozzz|AccountNo|0
string controlValue = lstChange[0].ToString();
string controlName = lstChange[1].ToString();
int iRowIdentifier = lstChange[2].ToInt();
UpdateBillingInstructionsObject(controlName, controlValue, iRowIdentifier);
}
}
}
protected void Page_Load(object sender, EventArgs e)
{//lnkDelete_Click(object sender, EventArgs e)
//if (Request.Form["__EVENTTARGET"] == "lnkDelete")
//{
// lnkDelete_Click(this, new CommandEventArgs("delete", "zzz"));
//}
}
///
/// Read the Billing Instructions JSON string from the database for this specific Surface ID
///
/// This Surface ID
private void ReadBillingInstructionsForSurface(int SurfaceItemId)
{
PropertyBillingInstructions pbi = new PropertyBillingInstructions();
// Read the JSON Text from the database for this Surface ID
DataTable dtData = xData.GetDynamicByCriteriaSpecific("recId", typeof(oBillingInstructions), "entityId", SurfaceItemId.ToString(), "recId", "prop1_");
string biJson = dtData.TableIsValid() ? dtData.Rows[0]["billingInstructions"].ToString() : "";
if (!String.IsNullOrWhiteSpace(biJson))
{
pbi = CreateBillingInstructionsObject(biJson);
}
// Populate form
DataTable dtTable = new DataTable();
BillingInstructionCount = 7; // By default we will always have 8 rows for billing instructions
if (pbi.BillingInstructions == null || pbi.BillingInstructions.Count() == 0)
{
// Populate with the default
pbi.BillingInstructions = GenerateDefaultBillingInstructionsObject(BillingInstructionCount);
dtTable = pbi.BillingInstructions.ConvertToDataTable();
}
else
{
dtTable = pbi.BillingInstructions.ConvertToDataTable();
}
// List
BillingInstructions = pbi.BillingInstructions;
rptBillingInstructions.DataSource = dtTable;
rptBillingInstructions.DataBind();
}
#region Create Billing Instruction Object
///
/// Deserialize the JSON Object
///
private PropertyBillingInstructions CreateBillingInstructionsObject(string jsonstring)
{
PropertyBillingInstructions pbi = JsonConvert.DeserializeObject(jsonstring);
return pbi;
}
#endregion
private void CreateBillingInstructionJSON(PropertyBillingInstructions pbi)
{
string jsonString = JsonConvert.SerializeObject(pbi, Formatting.Indented);
// Save the JSON Object and Surface ID to the database
oBillingInstructions instructions = new oBillingInstructions();
// Don't save any billing instructions if the Property ID is 0 (when creating a new one). Default it to the temporary ID
if (SurfaceItemId == 0)
{ // When creating a new property, billing instructions must be saved
instructions.entityId = BillingInstructions_PlaceHolderValue;
}
else
instructions.entityId = SurfaceItemId;
instructions.billingInstructions = jsonString;
//instructions.recId = -1;
List lstParms = instructions.GetType().GetProperties().ToList().ToDynamicParamList(instructions, "recId");
xData.SaveTypedByProc("recId", typeof(oBillingInstructions), instructions, "sproc_PROP1_BillingInstructions_AddUpdate", lstParms);
}
private List GenerateDefaultBillingInstructionsObject(int BillingInstructionCount)
{
List instructions = new List();
for (int row = 0; row <= BillingInstructionCount; row++)
{
BillingInstruction instruction = new BillingInstruction();
instruction.SurfaceItemId = SurfaceItemId;
instruction.RowIdentifier = row;
instruction.Account = DetermineDefaultAccountName(row);
instruction.SupplierName = "";
instruction.AccountNo = "";
instruction.ChargeToTenant = "false";
instruction.AgentToPaySupplier = "false";
instruction.AgentToPayToLandlord = "false";
instructions.Add(instruction);
}
return instructions;
}
private string DetermineDefaultAccountName(int rowIdentifier)
{
switch (rowIdentifier)
{
case 0:
return "Body Corporate Levy";
case 1:
return "Council Property Rates";
case 2:
return "Electricity";
case 3:
return "Water";
case 4:
return "Refuse";
case 5:
return "Sewerage";
case 6:
return "Other";
case 7:
return "Other";
default:
return "";
}
}
#region Properties
private int SurfaceItemId
{
get
{
if (ViewState["surfaceItemId"] != null)
return (int)ViewState["surfaceItemId"];
else
return BillingInstructions_PlaceHolderValue; // Return temporary ID 0;
}
set
{
ViewState["surfaceItemId"] = value;
}
}
private int SurfaceFieldId
{
get
{
if (ViewState["surfaceFieldId"] != null)
return Convert.ToInt32(ViewState["surfaceFieldId"]);
else
return 0;
}
set
{
ViewState["surfaceFieldId"] = value;
}
}
private int SurfaceId
{
get
{
if (ViewState["surfaceId"] != null)
return Convert.ToInt32(ViewState["surfaceId"]);
else
return 0;
}
set
{
ViewState["surfaceId"] = value;
}
}
private int RecordLimit
{
get
{
if (ViewState["recordLimit"] == null)
return 0;
else
return (int)ViewState["recordLimit"];
}
set
{
ViewState["recordLimit"] = value;
}
}
private int BillingInstructionCount
{
get
{
if (ViewState["billingInstructionCount"] != null)
return (int)ViewState["billingInstructionCount"];
else
return 0;
}
set
{
ViewState["billingInstructionCount"] = value;
}
}
// Temporary solution Property for New Properties with Billing Instructions
private int BillingInstructions_PlaceHolderValue
{
get
{
if (ViewState["billingInstructionsPlaceHolderValue"] != null)
{
return (int)ViewState["billingInstructionsPlaceHolderValue"];
}
else
{
// Will only get 1 record back
ArrayList arrList = xData.GetCustomCollection("recId", typeof(oBillingInstructions), "SELECT MAX(entityId) AS 'maxEntity' FROM dbo.prop1_BillingInstructions", "", "publ_");
int placeHolderValue = 1000000;
foreach (oBillingInstructions instruction in arrList)
{
int maxEntityId = instruction.entityId;
if (maxEntityId > 1000000)
{
placeHolderValue = maxEntityId + 10;
}
}
return placeHolderValue;
}
}
set
{
ViewState["billingInstructionsPlaceHolderValue"] = value;
}
}
private List BillingInstructions
{
get
{
return (List)ViewState["billingInstructions"];
}
set
{
ViewState["billingInstructions"] = value;
}
}
#endregion
protected void rptBillingInstructions_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
List lstItems = ((DataRowView)e.Item.DataItem).Row.ItemArray.Select(c => c.ToString()).ToList();
int iRowIdentifier = Convert.ToInt32(lstItems[1]);
#region Account
Label lblRow_AccountName = (Label)e.Item.FindControl("lblRow_AccountName");
TextBox txtRowNew_Account = (TextBox)e.Item.FindControl("txtRow_AccountName");
txtRowNew_Account.ID = $"txtRowNew_Account_{iRowIdentifier}";
txtRowNew_Account.AutoPostBack = false;
if (txtRowNew_Account.Attributes["onchange"].Count() == 0)
txtRowNew_Account.Attributes.Add("onchange", "AppendChangeValue(this,'Account')");
#endregion
#region Supplier Name
TextBox txtRowNew_SupplierName = (TextBox)e.Item.FindControl("txtRow_SupplierName");
txtRowNew_SupplierName.ID = $"txtRowNew_SupplierName_{iRowIdentifier}";
txtRowNew_SupplierName.AutoPostBack = false;
if (txtRowNew_SupplierName.Attributes["onchange"].Count() == 0)
txtRowNew_SupplierName.Attributes.Add("onchange", "AppendChangeValue(this,'SupplierName')");
#endregion
#region AccountNo
TextBox txtRowNew_AccountNo = (TextBox)e.Item.FindControl("txtRow_AccountNo");
txtRowNew_AccountNo.ID = $"txtRowNew_AccountNo_{iRowIdentifier}";
txtRowNew_AccountNo.AutoPostBack = false;
if (txtRowNew_AccountNo.Attributes["onchange"].Count() == 0)
txtRowNew_AccountNo.Attributes.Add("onchange", "AppendChangeValue(this,'AccountNo')");
#endregion
#region Charge to Tenant
Label lblRow_ChargeToTenant = (Label)e.Item.FindControl("lblRow_ChargeToTenant");
CheckBox ckRowNew_ChargeToTenant = (CheckBox)e.Item.FindControl("ckRow_ChargeToTenant");
ckRowNew_ChargeToTenant.ID = $"ckRowNew_ChargeToTenant_{iRowIdentifier}";
ckRowNew_ChargeToTenant.AutoPostBack = false;
if (ckRowNew_ChargeToTenant.Attributes["onclick"].Count() == 0)
ckRowNew_ChargeToTenant.Attributes.Add("onclick", "AppendChangeValue(this,'ChargeToTenant')");
#endregion
#region Agent to Pay Supplier
CheckBox ckRowNew_AgentToPaySupplier = (CheckBox)e.Item.FindControl("ckRow_AgentToPaySupplier");
ckRowNew_AgentToPaySupplier.ID = $"ckRowNew_AgentToPaySupplier_{iRowIdentifier}";
ckRowNew_AgentToPaySupplier.AutoPostBack = false;
if (ckRowNew_AgentToPaySupplier.Attributes["onclick"].Count() == 0)
ckRowNew_AgentToPaySupplier.Attributes.Add("onclick", "AppendChangeValue(this,'AgentToPaySupplier')");
#endregion
#region Agent to Pay Landlord
CheckBox ckRowNew_AgentToPayToLandlord = (CheckBox)e.Item.FindControl("ckRow_AgentToPayToLandlord");
ckRowNew_AgentToPayToLandlord.ID = $"ckRowNew_AgentToPayToLandlord_{iRowIdentifier}";
ckRowNew_AgentToPayToLandlord.AutoPostBack = false;
if (ckRowNew_AgentToPayToLandlord.Attributes["onclick"].Count() == 0)
ckRowNew_AgentToPayToLandlord.Attributes.Add("onclick", "AppendChangeValue(this,'AgentToPayToLandlord')");
#endregion
#region Delete Button on each row
LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");
lnkDelete.ID = $"lnkDelete_{iRowIdentifier}";
lnkDelete.CommandName = "delete";
lnkDelete.CommandArgument = iRowIdentifier.ToString();
//lnkDelete.ImageUrl = "/images/gridDelete.png";
//lnkDelete.ToolTip = "Delete this instruction";
lnkDelete.CssClass = "glyphicon glyphicon-remove palette-remove";
lnkDelete.Attributes.Add("data-toggle", "tooltip");
lnkDelete.Attributes.Add("title", "Delete this instruction");
////lnkDelete_Click
//lnkDelete.Command += new CommandEventHandler(lnkDelete_Click);
//if ((lnkDelete.Attributes["OnClientClick"] == null) || (lnkDelete.Attributes["OnClientClick"].Count() == 0))
// lnkDelete.Attributes.Add("OnClientClick", $"showConfirm('lnkDelete_{iRowIdentifier}'); return false;"); // OnClientClick=" showConfirm('lnkDelete'); return false;"
#endregion
#region Control Visibility and logic
string sAccount = lstItems[2];
string sSupplierName = lstItems[3];
string sAccountNo = lstItems[4];
bool blnChargeToTenant = false;
if (!(string.IsNullOrWhiteSpace(lstItems[5])))
blnChargeToTenant = Convert.ToBoolean(lstItems[5]);
bool blnAgentToPaySupplier = false;
if (!(string.IsNullOrWhiteSpace(lstItems[6])))
blnAgentToPaySupplier = Convert.ToBoolean(lstItems[6]);
bool blnAgentToPayToLandlord = false;
if (!(string.IsNullOrWhiteSpace(lstItems[7])))
blnAgentToPayToLandlord = Convert.ToBoolean(lstItems[7]);
txtRowNew_Account.Text = sAccount;
lblRow_AccountName.Text = sAccount;
txtRowNew_SupplierName.Text = sSupplierName;
txtRowNew_AccountNo.Text = sAccountNo;
ckRowNew_ChargeToTenant.Checked = blnChargeToTenant;
lblRow_ChargeToTenant.Text = "N/A";
ckRowNew_AgentToPaySupplier.Checked = blnAgentToPaySupplier;
ckRowNew_AgentToPayToLandlord.Checked = blnAgentToPayToLandlord;
if (iRowIdentifier <= BillingInstructionCount)
{ // The first 8 rows must default to the specified Account Names
txtRowNew_Account.Visible = false;
lblRow_AccountName.Visible = true;
lnkDelete.Visible = false;
lnkDelete.CommandName = "";
lnkDelete.CommandArgument = "";
lnkDelete.Attributes.Remove("OnClientClick");
}
else
{ // Any user added billing instructions, will allow the user to specify the Account Name
txtRowNew_Account.Visible = true;
lblRow_AccountName.Visible = false;
lnkDelete.Visible = true;
lnkDelete.CommandName = "delete";
lnkDelete.CommandArgument = iRowIdentifier.ToString();
//OnClientClick=" showConfirm('lnkDelete'); return false;"
}
if (iRowIdentifier <= 1)
{ // The first two rows must read N/A for Charge to Tenant
ckRowNew_ChargeToTenant.Visible = false;
lblRow_ChargeToTenant.Visible = true;
}
else
{
ckRowNew_ChargeToTenant.Visible = true;
lblRow_ChargeToTenant.Visible = false;
}
#endregion
}
protected void rptBillingInstructions_ItemCommand(object source, RepeaterCommandEventArgs e)
{
try
{
int rowId = Convert.ToInt32(e.CommandArgument);
switch (e.CommandName)
{
case "delete":
DeleteInstruction(rowId);
break;
}
}
catch (Exception ex)
{
exception.HandleException("surface:", MethodBase.GetCurrentMethod().Name, ex, Session["attachment"]);
Response.Redirect("/error", false);
}
finally
{
// Reload the parent control if a new Primary Image has been uploaded
ISurfaceBase parent = (ISurfaceBase)this.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent;
foreach (oSurfaceItem item in xData.GetTypedByCriteriaSpecific("recId", typeof(oSurfaceItem), "recId", SurfaceItemId.ToString()))
{
parent.ReloadControl(item, false, false);
}
}
}
private void DeleteInstruction(int rowID)
{
// Let's say we have a rowID = 8
List instructions = BillingInstructions;
// Read the instruction where the RowIdentifier = 8
var instructionToRemove = instructions.Single(r => r.RowIdentifier == rowID);
if (instructionToRemove != null) // If there is such an instruction...
{
// Remove instruction with RowIdentifier = 8
instructions.Remove(instructionToRemove);
#region Removal and reset Logic
// We now need to update the list RowIdentifier property because the removed row = 8 will have a gap
/*
For example, in a list of 12 items we now have a gap between 7 and 9 if 8 was removed
- ...
- 5
- 6
- 7
- 9
- 10
- 11
- 12
*/
// See if there are any rows after the one we removed
// Count the rows where the RowIdentifier > 8
// This returns 4
#endregion
int iCount = (from r in instructions
where r.RowIdentifier > rowID
select r).Count();
if (iCount > 0) // If there are rows after the one we removed
{
// Then the next RowIdentifier = rowID = 8 + 1 => 9
int nextRowID = rowID + 1;
// Starting from a zero based index (0, 1, 2, 3) ...
for (int i = 0; i <= iCount - 1; i++)
{
int rowToUpdate = nextRowID + i; // ... with i = 0, the next row will be nextRowID = 9 + i = 0 => 9
// so get the instruction where the RowIdentifier = 9
BillingInstruction instr = (from r in instructions
where r.RowIdentifier == rowToUpdate
select r).First();
// Update the RowIdentifier to be one less
instr.RowIdentifier = instr.RowIdentifier - 1;
}
}
}
//BillingInstructions = instructions;
PropertyBillingInstructions pbi = new PropertyBillingInstructions();
pbi.BillingInstructions = instructions;
pbi.SurfaceItemId = SurfaceItemId;
CreateBillingInstructionJSON(pbi);
}
protected void txtRow_BillingInstruction_TextChanged(object sender, EventArgs e)
{
//string clientControlID = ((System.Web.UI.Control)sender).ClientID;
//int iRowIdentifier = clientControlID.Substring(clientControlID.LastIndexOf('_') + 1).ToInt();
//clientControlID = clientControlID.Remove(clientControlID.LastIndexOf('_'), 2);
//string controlName = clientControlID.Substring(clientControlID.LastIndexOf('_') + 1);
//string clientValue = ((System.Web.UI.WebControls.TextBox)sender).Text;
//UpdateBillingInstructionsObject(controlName, clientValue, iRowIdentifier);
}
protected void ckRow_BillingInstruction_CheckedChanged(object sender, EventArgs e)
{
//string clientControlID = ((System.Web.UI.Control)sender).ClientID;
//int iRowIdentifier = clientControlID.Substring(clientControlID.LastIndexOf('_') + 1).ToInt();
//clientControlID = clientControlID.Remove(clientControlID.LastIndexOf('_'), 2);
//string controlName = clientControlID.Substring(clientControlID.LastIndexOf('_') + 1);
//string clientValue = ((System.Web.UI.WebControls.CheckBox)sender).Checked.ToString().ToLower();
//UpdateBillingInstructionsObject(controlName, clientValue, iRowIdentifier);
}
private void UpdateBillingInstructionsObject(string controlName, string controlValue, int iRowIdentifier)
{
List instructions = BillingInstructions;
int iCount = (from r in instructions
where r.RowIdentifier == iRowIdentifier
select r).Count();
if (iCount > 0)
{
// Update
BillingInstruction instr = (from r in instructions
where r.RowIdentifier == iRowIdentifier
select r).First();
// Account will only be updated on items the user adds, not the default accounts.
if (nameof(instr.Account).Equals(controlName)) { instr.Account = controlValue; }
if (nameof(instr.SupplierName).Equals(controlName)) { instr.SupplierName = controlValue; }
if (nameof(instr.AccountNo).Equals(controlName)) { instr.AccountNo = controlValue; }
if (nameof(instr.ChargeToTenant).Equals(controlName)) { instr.ChargeToTenant = controlValue; }
if (nameof(instr.AgentToPaySupplier).Equals(controlName)) { instr.AgentToPaySupplier = controlValue; }
if (nameof(instr.AgentToPayToLandlord).Equals(controlName)) { instr.AgentToPayToLandlord = controlValue; }
}
else
{
// Add
BillingInstruction instruction = new BillingInstruction();
string accountName = DetermineDefaultAccountName(iRowIdentifier);
instruction.SurfaceItemId = SurfaceItemId;
instruction.RowIdentifier = iRowIdentifier;
if (nameof(instruction.Account).Equals(controlName)) { instruction.Account = accountName.Equals("") ? controlValue : accountName; }
if (nameof(instruction.SupplierName).Equals(controlName)) { instruction.SupplierName = controlValue; }
if (nameof(instruction.AccountNo).Equals(controlName)) { instruction.AccountNo = controlValue; }
if (nameof(instruction.ChargeToTenant).Equals(controlName)) { instruction.ChargeToTenant = controlValue; }
if (nameof(instruction.AgentToPaySupplier).Equals(controlName)) { instruction.AgentToPaySupplier = controlValue; }
if (nameof(instruction.AgentToPayToLandlord).Equals(controlName)) { instruction.AgentToPayToLandlord = controlValue; }
instructions.Add(instruction);
}
}
private void UpdateBillingInstructionsObject(int newEntityId, int oldEntityId)
{
// Find the billing instructions witht he temporary placeholder ID
//int oldEntityId = BillingInstructions_PlaceHolderValue;
List instructions = BillingInstructions;
int iCount = (from r in instructions
where r.SurfaceItemId == oldEntityId
select r).Count();
if (iCount > 0)
{
// Update
List instr = (from r in instructions
where r.SurfaceItemId == oldEntityId
select r).ToList();
foreach(BillingInstruction instructionItem in instr)
{
instructionItem.SurfaceItemId = newEntityId;
}
#region ...
//// Account will only be updated on items the user adds, not the default accounts.
//if (nameof(instr.Account).Equals(controlName)) { instr.Account = controlValue; }
//if (nameof(instr.SupplierName).Equals(controlName)) { instr.SupplierName = controlValue; }
//if (nameof(instr.AccountNo).Equals(controlName)) { instr.AccountNo = controlValue; }
//if (nameof(instr.ChargeToTenant).Equals(controlName)) { instr.ChargeToTenant = controlValue; }
//if (nameof(instr.AgentToPaySupplier).Equals(controlName)) { instr.AgentToPaySupplier = controlValue; }
//if (nameof(instr.AgentToPayToLandlord).Equals(controlName)) { instr.AgentToPayToLandlord = controlValue; }
#endregion
}
#region ...
//else
//{
// // Add
// BillingInstruction instruction = new BillingInstruction();
// string accountName = DetermineDefaultAccountName(iRowIdentifier);
// instruction.SurfaceItemId = SurfaceItemId;
// instruction.RowIdentifier = iRowIdentifier;
// if (nameof(instruction.Account).Equals(controlName)) { instruction.Account = accountName.Equals("") ? controlValue : accountName; }
// if (nameof(instruction.SupplierName).Equals(controlName)) { instruction.SupplierName = controlValue; }
// if (nameof(instruction.AccountNo).Equals(controlName)) { instruction.AccountNo = controlValue; }
// if (nameof(instruction.ChargeToTenant).Equals(controlName)) { instruction.ChargeToTenant = controlValue; }
// if (nameof(instruction.AgentToPaySupplier).Equals(controlName)) { instruction.AgentToPaySupplier = controlValue; }
// if (nameof(instruction.AgentToPayToLandlord).Equals(controlName)) { instruction.AgentToPayToLandlord = controlValue; }
// instructions.Add(instruction);
//}
#endregion
PropertyBillingInstructions pbi = new PropertyBillingInstructions();
pbi.BillingInstructions = instructions;
string jsonString = JsonConvert.SerializeObject(pbi, Formatting.Indented);
// Save the JSON Object and Surface ID to the database
oBillingInstructions instrToUpdate = new oBillingInstructions();
instrToUpdate.entityId = oldEntityId; // We can't update the entityId to the propertyID yet, because then this Billing Instruction Update will fail. Only update the entityID when saving the property
instrToUpdate.billingInstructions = jsonString;
//instructions.recId = -1;
List lstParms = instrToUpdate.GetType().GetProperties().ToList().ToDynamicParamList(instrToUpdate, "recId");
xData.SaveTypedByProc("recId", typeof(oBillingInstructions), instrToUpdate, "sproc_PROP1_BillingInstructions_AddUpdate", lstParms);
// Now update the entityID field on the Billing Instructions table
xData.RunCustomQuery($"UPDATE prop1_BillingInstructions SET entityId = {newEntityId} WHERE entityId = {oldEntityId}");
}
protected void btnAddBillingInstruction_Click(object sender, EventArgs e)
{
try
{
//radUploadSurfaceImageGallery.UploadedFiles.Clear();
//chkPrimaryImage.Checked = false;
////setup attachments
//Session["moduleId"] = pNums.Module.SurfaceGallery.GetHashCode();
//Session["entityId"] = SurfaceGalleryItemId;
//string fieldName = String.Empty;
//if (utils.verifySession("fieldNameNote"))
// fieldName = Session["fieldNameNote"].ToString();
//Session["fieldNameFile"] = SurfaceGalleryFieldId;
//dynamic attachments = (IControlBase)Page.Master.FindControl("attachments");
//attachments.ReloadControl();
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "mySurfaceBillingInstructionModal", "$('#" + pnlModal.ClientID + "').closest('div').find('.modSurfaceBillingInstructions').modal();", true);
}
catch (Exception ex)
{
exception.HandleException("billingInstructions:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
protected void btnSaveBillingInstruction_Click(object sender, EventArgs e)
{
try
{
// Get the last row added to the Billing Instructions Object
List instructions = BillingInstructions;
int iNextRow = instructions.Max(r => r.RowIdentifier) + 1;
//txtRowNew_Account
TextBox txtRowNew_Account = (TextBox)this.FindControl("sfGallery").FindControl("txtRowNew_Account");
string sAccount = txtRowNew_Account.Text;
if (sAccount.Length > 0)
{
UpdateBillingInstructionsObject("Account", sAccount, iNextRow);
}
//txtRowNew_SupplierName
TextBox txtRowNew_SupplierName = (TextBox)this.FindControl("sfGallery").FindControl("txtRowNew_SupplierName");
string sSupplierName = txtRowNew_SupplierName.Text;
if (sSupplierName.Length > 0)
{
UpdateBillingInstructionsObject("SupplierName", sSupplierName, iNextRow);
}
//txtRowNew_AccountNo
TextBox txtRowNew_AccountNo = (TextBox)this.FindControl("sfGallery").FindControl("txtRowNew_AccountNo");
string sAccountNo = txtRowNew_AccountNo.Text;
if (sAccountNo.Length > 0)
{
UpdateBillingInstructionsObject("AccountNo", sAccountNo, iNextRow);
}
//ckRowNew_ChargeToTenant
CheckBox ckRowNew_ChargeToTenant = (CheckBox)this.FindControl("sfGallery").FindControl("ckRowNew_ChargeToTenant");
bool blnChargeToTenant = ckRowNew_ChargeToTenant.Checked;
UpdateBillingInstructionsObject("ChargeToTenant", blnChargeToTenant.ToString().ToLower(), iNextRow);
//ckRowNew_AgentToPaySupplier
CheckBox ckRowNew_AgentToPaySupplier = (CheckBox)this.FindControl("sfGallery").FindControl("ckRowNew_AgentToPaySupplier");
bool blnAgentToPaySupplier = ckRowNew_AgentToPaySupplier.Checked;
UpdateBillingInstructionsObject("AgentToPaySupplier", blnAgentToPaySupplier.ToString().ToLower(), iNextRow);
//ckRowNew_AgentToPayToLandlord
CheckBox ckRowNew_AgentToPayToLandlord = (CheckBox)this.FindControl("sfGallery").FindControl("ckRowNew_AgentToPayToLandlord");
bool blnAgentToPayToLandlord = ckRowNew_AgentToPayToLandlord.Checked;
UpdateBillingInstructionsObject("AgentToPayToLandlord", blnAgentToPayToLandlord.ToString().ToLower(), iNextRow);
//if (SurfaceItemId == 0)
// UpdateBillingInstructionsObject(BillingInstructions_PlaceHolderValue, 0);
ISurfaceBase parent = (ISurfaceBase)this.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent;
parent.SaveControl(false, false, false);
//SaveOnNew();
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "mySurfaceBillingInstructionModalClose", "$('#" + pnlModal.ClientID + "').closest('div').find('.modSurfaceBillingInstructions').modal('hide');$('body').removeClass('modal-open');", true);
}
catch (Exception ex)
{
exception.HandleException("billingInstructions:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
finally
{
// Clear the previous entries
txtRowNew_Account.Text = "";
txtRowNew_SupplierName.Text = "";
txtRowNew_AccountNo.Text = "";
ckRowNew_ChargeToTenant.Checked = false;
ckRowNew_AgentToPaySupplier.Checked = false;
ckRowNew_AgentToPayToLandlord.Checked = false;
//// Reload the parent control if a new Primary Image has been uploaded
//ISurfaceBase parent = (ISurfaceBase)this.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent;
//foreach (oSurfaceItem item in xData.GetTypedByCriteriaSpecific("recId", typeof(oSurfaceItem), "recId", SurfaceItemId.ToString()))
//{
// parent.ReloadControl(item, false, false);
//}
}
}
//protected void lnkDelete_Click(object sender, CommandEventArgs e)
//{
// //LinkButton lnkBtn = (LinkButton)(sender);
// //string rowID = lnkBtn.CommandArgument;
// bool confirmValue = Convert.ToBoolean(this.hfDeleteConfirmation.Value);
//}
}
internal class PropertyBillingInstructions
{
public int SurfaceItemId { get; set; }
public List BillingInstructions { get; set; }
}
[Serializable]
internal class BillingInstruction : ISerializable
{
public int SurfaceItemId { get; set; }
public int RowIdentifier { get; set; }
public string Account { get; set; }
public string SupplierName { get; set; }
public string AccountNo { get; set; }
public string ChargeToTenant { get; set; } // This is a boolean, but keep as a Yes/No string to make the JSON Readable
public string AgentToPaySupplier { get; set; }
public string AgentToPayToLandlord { get; set; }
public BillingInstruction()
{
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("SurfaceItemId", SurfaceItemId);
info.AddValue("RowIdentifier", RowIdentifier);
info.AddValue("Account", Account);
info.AddValue("SupplierName", SupplierName);
info.AddValue("AccountNo", AccountNo);
info.AddValue("ChargeToTenant", ChargeToTenant);
info.AddValue("AgentToPaySupplier", AgentToPaySupplier);
info.AddValue("AgentToPayToLandlord", AgentToPayToLandlord);
}
protected BillingInstruction(SerializationInfo info, StreamingContext context)
{
SurfaceItemId = info.GetInt32("SurfaceItemId");
RowIdentifier = info.GetInt32("RowIdentifier");
Account = info.GetString("Account");
SupplierName = info.GetString("SupplierName");
AccountNo = info.GetString("AccountNo");
ChargeToTenant = info.GetString("ChargeToTenant");
AgentToPaySupplier = info.GetString("AgentToPaySupplier");
AgentToPayToLandlord = info.GetString("AgentToPayToLandlord");
}
}