Update purchasePrice on Purchase Line from Trade Agreement - microsoft-dynamics

I have to update purchase orders that were created in past. Requirement is to update the purchase price by applying new trade agreements that were created later in the system after the purchase order creation. I have written a piece of code but the combinations in trade agreement are endless.
Is there any faster way to get the price that is active for the combinations?
Thank you
public static PriceDiscTable checkPriceDiscTable(PriceDiscAccountRelation _accountRelation,
PriceDiscItemRelation _itemRelation,
InventLocationId _inventLocationId)
{
RecId priceDiscTableRec;
PriceDiscTable priceDiscTable;
TableGroupAll accountCode, ItemCode;
PriceDiscAccountRelation accountRelation;
PriceDiscItemRelation itemRelation;
int totalcases = 3;
int prioritycase = 0;
while(prioritycase < totalcases)
{
switch (priorityCase)
{
case 0:
accountCode = TableGroupAll::Table;
accountRelation = _accountRelation;
itemCode = TableGroupAll::Table;
itemRelation = _itemRelation;
priceDiscTableRec = DAL_PriceFromAgrement::findPriceDiscTable(accountCode, accountRelation,
itemCode, itemRelation, _inventLocationId);
priceDiscTable = PriceDiscTable::findRecId(priceDiscTableRec);
if(priceDiscTable.recId)
{
return priceDiscTable;
}
break;
case 1:
accountCode = TableGroupAll::GroupId;
accountRelation = _accountRelation;
itemCode = TableGroupAll::Table;
itemRelation = _itemRelation;
priceDiscTableRec = DAL_PriceFromAgrement::findPriceDiscTable(accountCode, accountRelation,
itemCode, itemRelation, _inventLocationId);
if(priceDiscTable.recId)
{
return priceDiscTable;
}
break;
case 2:
accountCode = TableGroupAll::All;
accountRelation = "";
itemCode = TableGroupAll::Table;
itemRelation = _itemRelation;
priceDiscTableRec = DAL_PriceFromAgrement::findPriceDiscTable(accountCode, accountRelation,
itemCode, itemRelation, _inventLocationId);
if(priceDiscTable.recId)
{
return priceDiscTable;
}
break;
}
prioritycase++;
}
return priceDiscTable;
}

The helper class is already there to do this for you.
The class name is PriceDisc.
Take a look at the method purchPriceAgreement on InventTable.
...
vendTable= VendTable::find(_accountNum);
priceDisc = new PriceDisc(ModuleInventPurchSales::Purch,
this.ItemId,
_inventDim,
inventTableModule.UnitId,
_priceDate,
_qty,
vendTable.AccountNum,
_currencyCode);
if (!priceDisc.findPrice(vendTable.PriceGroup, false))
...
price = priceDisc.price();
priceMarkup = priceDisc.markup();
priceUnit = priceDisc.priceUnit();
...
etc

Related

Changing if-else into switch statements?

I am using multiple else/if statements and I want to use switch statements. I tried but don't know how to fill in multiple answers.
How can I convert the following else/if statements to switch?
if (this.state.item.type === 'dashboard') {
settings.view_name = this.state.view_name;
settings.layout = this.state.layout;
settings.inline_edit = this.state.inlineEdit;
settings.show_add = this.state.showAdd;
settings.tab_queries = this.state.tabQueries;
settings.carousel = this.state.carousel;
settings.template = this.state.template;
settings.sort = this.state.sort;
settings.templateOptions = this.state.templateOptions;
} else if (this.state.item.type === 'list_option_percentage_conditions') {
settings.pie_charts = this.state.pie_charts;
} else if (this.state.item.type === 'count_list_options') {
settings.count_list_field = this.state.count_list_field;
settings.date_field = this.state.date_field;
settings.use_creation_date = this.state.use_creation_date;
settings.scheme = this.state.scheme;
} else if (this.state.item.type === 'sum_fields') {
settings.sum_field = this.state.sum_field;
settings.scheme = this.state.scheme;
}
I want it to look something like this:
switch (this.state.iten.type) {
case "dashboard":
answer = "";
break;
case "list_option_percentage_conditions":
answer = "";
case "count_list_options":
answer = "";
break;
case "sum_fields":
answer = "";
break;
}

How to sort a string without sorting commands

I have an assignment for one of my classes where I need to sort a string alphabetically without using any commands besides the simple ones that are already in here. Whenever I use it, it works for the most part except it will leave words like Fred & Eric, or Hazel & Ian (first letter's are next to each other in the alphabet). The string that is being compared to the others is set as "two" then all others are compared against it. The B string is just one that is being changed with the A string. If anyone knows why this is, that would be greatly appreciated!
for (int ct = 0; ct < kh; ct++){
hold = A[ct];
bool pass = false;
for (int ct2 = ct+1; ct2 < kh; ct2++){
two = A[ct2];
if (two[0] < hold[0]){
save = A[ct2];
A[ct2] = A[ct];
A[ct] = save;
hold = two;
save = B[ct2];
B[ct2] = B[ct];
B[ct] = save;
}
else if (two[0] == hold[0]){
if (two[1] < hold [1]){
save = A[ct2];
A[ct2] = A[ct];
A[ct] = save;
hold = two;
save = B[ct2];
B[ct2] = B[ct];
B[ct] = save;
}
}
else if (two[1] == hold[1]){
if (two[2] < hold [2]){
save = A[ct2];
A[ct2] = A[ct];
A[ct] = save;
hold = two;
save = B[ct2];
B[ct2] = B[ct];
B[ct] = save;
}
}
}
}

Best way to refactor If-statement

I have the following code:
if (adSetting.Core_standard_application_role)
{
rc = new IntegrationRoleCompany();
rc.RoleCompany = firmSettings.FirmNo.ToString();
rc.RoleName = "Core standard application role";
rcList.Add(rc);
}
if (adSetting.Expense_Invoice_Application_Access)
{
rc = new IntegrationRoleCompany();
rc.RoleCompany = firmSettings.FirmNo.ToString();
rc.RoleName = "Expense Invoice Application Access";
rcList.Add(rc);
}
The problem is that I have 20 if-checks where I check addSetting.Property. Now to the question:
What is the best and most effective way to refactor this if-statements?
you could have a 20x2 array with the adSetting.STUFF_AS_STRING -> rc.RoleName mapping.
then loop over the array in a for loop
pseudo code:
for (var i=0; i<theArray.length; i++) {
adSettingStr, RoleName = theArray[i]
if (adSetting[adSettingStr]) {
rc = new IntegrationRoleCompany();
rc.RoleCompany = firmSettings.FirmNo.ToString();
rc.RoleName = RoleName;
rcList.Add(rc);
}
}

Transaction Advance Search for Sales Order in NetSuite returning Errors when otherRefNum Operator used

Any idea why the below code would return an error(Failure) for its status?
private SearchResult getTxns()
{
TransactionSearchAdvanced tsa = new TransactionSearchAdvanced();
tsa.columns = new TransactionSearchRow();
tsa.columns.basic = new TransactionSearchRowBasic();
tsa.columns.basic.tranId = new SearchColumnStringField[] { new SearchColumnStringField() };
tsa.criteria = new TransactionSearch();
tsa.criteria.basic = new TransactionSearchBasic();
tsa.criteria.basic.mainLine = new SearchBooleanField();
tsa.criteria.basic.mainLine.searchValue = true;
tsa.criteria.basic.mainLine.searchValueSpecified = true;
tsa.criteria.basic.type = new SearchEnumMultiSelectField();
tsa.criteria.basic.type.#operator = SearchEnumMultiSelectFieldOperator.anyOf;
tsa.criteria.basic.type.operatorSpecified = true;
tsa.criteria.basic.type.searchValue = new string[] { "_salesOrder" };
tsa.criteria.basic.otherRefNum = new SearchTextNumberField();
tsa.criteria.basic.otherRefNum.#operator = SearchTextNumberFieldOperator.equalTo;
tsa.criteria.basic.type.operatorSpecified = true;
tsa.criteria.basic.type.searchValue = new string[] { "BBnB 1001" };
SearchResult sr = _service.search(tsa);
return sr;
}
The following is the error that is returned in the results.
Status Code: INVALID_SEARCH_OPERATOR
Status Message: You need to provide a valid search field operator.
However, this operator appears in the NetSuite UI itself when I do a search. Also, I find it in the NetSuite documentation here.
I am using v2013_1_0 for the webservices version of the wsdl.
SOLUTION
Solution found to be in the last block of code. Was attempting to set otherRefNum and was referencing Type. Here is the corrected code.
private SearchResult getTxns()
{
TransactionSearchAdvanced tsa = new TransactionSearchAdvanced();
tsa.columns = new TransactionSearchRow();
tsa.columns.basic = new TransactionSearchRowBasic();
tsa.columns.basic.tranId = new SearchColumnStringField[] { new SearchColumnStringField() };
tsa.criteria = new TransactionSearch();
tsa.criteria.basic = new TransactionSearchBasic();
tsa.criteria.basic.mainLine = new SearchBooleanField();
tsa.criteria.basic.mainLine.searchValue = true;
tsa.criteria.basic.mainLine.searchValueSpecified = true;
tsa.criteria.basic.type = new SearchEnumMultiSelectField();
tsa.criteria.basic.type.#operator = SearchEnumMultiSelectFieldOperator.anyOf;
tsa.criteria.basic.type.operatorSpecified = true;
tsa.criteria.basic.type.searchValue = new string[] { "_salesOrder" };
tsa.criteria.basic.otherRefNum = new SearchTextNumberField();
tsa.criteria.basic.otherRefNum.#operator = SearchTextNumberFieldOperator.equalTo;
tsa.criteria.basic.otherRefNum.operatorSpecified = true;
tsa.criteria.basic.otherRefNum.searchValue = "BBnB 1001";
SearchResult sr = _service.search(tsa);
return sr;
}
The problem is with your SearchEnumMultiSelectField operator. equalto is not a valid operator for this filter; you will need to use anyOf instead.
-- EDIT - Adapated from original comment --
A SearchTextNumberField does not accept an array of Strings. Instead try tsa.criteria.basic.otherRefNum.searchValue = "BBnB 1001";

List of windows users remotely

I would like to know how to retrieve the list of users that are logged onto a Remote machine. I can do it with qwinsta /server:xxxx, but would like to do it in C#.
check out wmi in .net under system.management.
something like:
ConnectionOptions conn = new ConnectionOptions();
conn.Authority = "ntdlmdomain:NAMEOFDOMAIN";
conn.Username = "";
conn.Password = "";
ManagementScope ms = new ManagementScope(#"\\remotecomputer\root\cimv2", conn);
ms.Connect();
ObjectQuery qry = new ObjectQuery("select * from Win32_ComputerSystem");
ManagementObjectSearcher search = new ManagementObjectSearcher(ms, qry);
ManagementObjectCollection return = search.Get();
foreach (ManagementObject rec in return)
{
Console.WriteLine("Logged in user: " + rec["UserName"].ToString());
}
You may not need the ConnectionOptions...
I ended up using the qwinsta /server:server1 command from C# -- it was much easier
thanks Ken
All this checks all 8 servers at the same time -- I put the result in sql server
but you can do what ever you want
query_citrix.bat script
cd C:.......\bin\citrix_boxes
qwinsta -server:servername or ip > servername.txt
string sAppCitrixPath = Application.StartupPath.ToString() + "\\citrix_boxes\\";
//Run Script for current citrix boxes
Process proc = new Process();
ProcessStartInfo si = new ProcessStartInfo();
si.FileName = sAppCitrixPath + "query_citrix.bat";
proc.StartInfo = si;
proc.Start();
proc.WaitForExit();
int exitCode = proc.ExitCode;
proc.Close();
if (exitCode == 0)
{
//Execute update who is on the Citrix_Boxes Currently
DirectoryInfo dic = new DirectoryInfo(sAppCitrixPath);
FileInfo[] fic = dic.GetFiles("*.txt");
for (int i = 0; i < fic.Length; i++)
{
ParseQWinStaServerFile(fic[i].FullName.ToString(), fic[i].Name.ToString().ToUpper().Replace(".TXT",""));
File.Delete(fic[i].FullName.ToString());
}
}
private void ParseQWinStaServerFile(string sLocation,string sServer)
{
using (StreamReader sr = File.OpenText(sLocation))
{
string sRecord = String.Empty;
char[] cSep = new char[] {' '};
bool bFirst = true;
while ((sRecord = sr.ReadLine()) != null)
{
if (bFirst == false)
{
string[] items = sRecord.Split(cSep, StringSplitOptions.RemoveEmptyEntries);
//Make sure all columns are present on the split for valid records
if (sRecord.Substring(19, 1) != " ") // check position of user id to see if it's their
{
//Send the user id and server name where you want to.
//here is your user
id = items[1].ToString().ToLower().Trim()
//here is your server
};
}
else
{
bFirst = false;
}
}
}
}