Files
2018-06-29 19:47:36 +00:00

638 lines
27 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AyaNova.PlugIn;
using GZTW.AyaNova.BLL;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;
using System.Data;
using Infragistics.Documents.Excel;
namespace AyaNova.Plugin.RepairTekCPC
{
class RepairTek : IAyaNovaPlugin
{
//Keep all the object types we want to deal with in a collection
//so that we can quickly check it when asked
private static List<RootObjectTypes> ObjectsWeCanDealWith = null;
//Holds the image resources from AyaNova
//so we can display the correct icons in our plugin
System.Resources.ResourceManager resman = null;
//Holds the current logged in user's localized text
//lookup object
LocalizedTextTable LocaleText = null;
#region IAyaNovaPlugin Members
#region interface properties
public string PluginName
{
get { return "RepairTek - RMA Updates"; }
}
public string PluginVersion
{
get { return "7.2"; }
}
public string About
{
get
{
return "AyaNova RepairTek - RMA Updates plugin\r\n" +
"Copyright 2009-2014 Ground Zero Tech-Works Inc.";
}
}
public Guid PluginID
{
get { return new Guid("{DD982142-5142-4E89-80A4-958235AE4A74}"); }
}
public System.Drawing.Image PluginSmallIcon
{
get { return null; }
}
public System.Drawing.Image PluginLargeIcon
{
get { return null; }
}
public System.Resources.ResourceManager AyaNovaResourceManager
{
set { resman = value; }
get { return resman; }
}
#endregion interface properties
#region Initialization and Close
public bool Initialize(Version AyaNovaVersion, LocalizedTextTable localizedText)
{
LocaleText = localizedText;
ObjectsWeCanDealWith = new List<RootObjectTypes>();
ObjectsWeCanDealWith.Add(RootObjectTypes.Nothing);
return true;
}
public void Close()
{
;
}
#endregion Initialization and close
#region ShowMenu?
public bool SingleObjectMenuShow(RootObjectTypes objectType)
{
return (ObjectsWeCanDealWith.Contains(objectType));
}
public bool MultipleObjectsMenuShow(RootObjectTypes objectType)
{
return false;
}
#endregion show menu?
#region Menu options
public List<AyaNovaPluginMenuItem> SingleObjectMenuOptions(RootObjectTypes objectType, object ayaNovaObject)
{
if (!ObjectsWeCanDealWith.Contains(objectType)) return null;
List<AyaNovaPluginMenuItem> list = new List<AyaNovaPluginMenuItem>();
list.Add(new AyaNovaPluginMenuItem("REPAIRTEK4", "RepairTek - RMA Updates", null, null));
return list;
}
public List<AyaNovaPluginMenuItem> MultipleObjectsMenuOptions(RootObjectTypes objectType)
{
return null;
}
#endregion
#region Menu Commands
#region LIST OBJECT COMMAND
/// <summary>
/// LIST OBJECT
/// </summary>
/// <param name="commandKey"></param>
/// <param name="objectType"></param>
/// <param name="objectIDList"></param>
/// <param name="listObject"></param>
/// <returns></returns>
public bool CommandSelectedForList(string commandKey, RootObjectTypes objectType, List<Guid> objectIDList, object listObject)
{
return false;
}
#endregion list object command
#region SINGLE OBJECT COMMAND
/// <summary>
/// SINGLE OBJECT
/// </summary>
/// <param name="commandKey"></param>
/// <param name="objectType"></param>
/// <param name="ayaNovaObject"></param>
public void CommandSelectedForSingleObject(string commandKey, RootObjectTypes objectType, object ayaNovaObject)
{
switch (commandKey)
{
case "REPAIRTEK4":
{
Update();
}
break;
}
}
#endregion single object command
#endregion menu commands
#endregion
/*
THIS IS THE "REPAIRTEK RMA Updates" PLUGIN - REPAIRTEK4
*/
#region REPAIRTEK4 - RMA Updates: Update workorders from spreadsheet
#region specifications
/*
*
* Specifics of plugin needed:
First row of the xlxs file being imported will always just be headers. Include code to not import first row.
I have identified specifics below on what to be done if existing data is already in a particular field (because he may import the same file a few times, so I have confirmed specifics with him for what situations for what fields)
xlxs file will always be in the format as per the sample (i.e. first column will always be WO#, 6th column will always be the TICKET # field which equates to the Workorder.Label.CustomerReferenceNumber, etc)
First column WO# will equate to an existing workorder number in the database being imported into.
If the import file has a workorder number that is not in the database, to display at the end of the import process a list of any workorder #'s in the xlxs file that are NOT in the database.
6th column field in the xlxs file is identified as the TICKET # field
TICKET # field equates to the Workorder.Label.CustomerReferenceNumber which is the default localized Client Reference #: field in the service workorder header area.
It is understood that text longer than 254 will be truncated.
If there is no data in the TICKET # field in the import file, the import to continue importing this xlxs row's other fields
If there is data already in the TICKET # field in the database, NOT to overwrite this data in this field.
**IMPORTANT**AND If there is data already in the TICKET # field in the database before updating the workorder, to identify at the end of the import IF the data in the TICKET # field in the import file matches the already entered data in the TICKET # field of the actual workorder.
(suggestion would be to identify the WO# and could be as simple as WO#xxxxx data in TICKET# fields do not match import file
7th column field in the xlxs file is identified as the NOTIFICATION field
NOTIFICATION field equates to the Workorder.Label.CustomerContactName which is the default localized Contact: field in the service workorder header area.
It is understood that text longer than 254 will be truncated
If there is no data in the NOTIFICATION field in the import file, the import to continue importing this xlxs row's other fields
If there is data already in the NOTIFICATION field in the workorder, DO overwrite the data
8th column field in the xlxs file is identified as the PROBLEM DESCRIPTI field
PROBLEM DESCRIPTI field equates to the O.Project which is the default localized Project: field in the service workorder header area
**This is a selection field
Refer to the provided database for ID numbers of the already set up PROBLEM DESCRIPTI (or if its easier for you, than just go by the Name)
If there is no data in the PROBLEM DESCRIPTI field in the import file, the import to continue importing this xlxs row's other fields
If there is data already in the PROBLEM DESCRIPTI field in the workorder, DO overwrite the data
**IMPORTANT** If there is NOT an existing PROBLEM DESCRIPTI with the same ID or Name in the AyaNova database, to identify at the end of the import that this WO# in the import file has a PROBLEM DESCRIPTI not coded for (or however you want to language it, but in layman's terms letting him know what WO# needed attention because of a PROBLEM DESCRIPTI issue)
9th column field in your xlxs file xlxs file is identified as the CANADA POST WC field
CANADA POST WC field equates to the Workorder.Label.Summary which is the default localized Summary: field in the service workorder header area.
It is understood that text longer than 254 will be truncated
If there is no data in the CANADA POST WC field in the import file, the import to continue importing this xlxs row's other fields
Data could be alpha and/or numbers, enter exactly as displayed in the import file (i.e. if it starts with a 0 make sure it enters the full number including starting with a 0) I don't see a problem with this, as the field is a text field, but he made a point of bringing this up so I wanted to pass on.
If there is data already in the CANADA POST WC field in the workorder, DO overwrite the data
10th column field in the xlxs file is identified as the INBOUND TRACKING # field
INBOUND TRACKING # field equates to the WorkorderItem.Label.Summary which is the default localized Workorder Item Summary: field in the service workorder ITEM area.
It is understood that text longer than 254 will be truncated
If there is no data in the INBOUND TRACKING # field in the import file, the import to continue importing this xlxs row's other fields
If there is data already in the INBOUND TRACKING # field in the workorder, DO overwrite the data
11th column field in the xlxs file is identified as the DATA field (in the actual database, this is further as DATA TRAPPED REQUEST)
DATA field equates to the WorkorderItem.Label.PriorityID and O.Priority which is the default localized Priority : field in the workorder item area
**This is a selection field
If there is no data in the DATA field in the import file, the import to continue importing this xlxs row's other fields
If there is data already in the DATA field in the workorder, DO overwrite the data
If there is NOT an existing DATA with the same ID or Name in the AyaNova database, to identify at the end of the import that this WO# in the import file has a DATA not coded for (or however you want to language it, but in layman's terms letting him know what WO# needed attention because of a DATA issue)
*
*
*
*
* UPDATE: 5/21/2014:
12th column field:
"...and now Frank would like you to customize the existing CPC adding the following:
customize the existing CPC plugin so that whatever text is in the CONTACT field of the row (column 12 of the row as shown in the example xlsx that is attached)
* is copied into the Travel Details field (Key: WorkorderItemTravel.Label.TravelDetails) of that service workorder.
There will only ever be one Travel record in the service workorder
If there is NOT a Travel record, Frank would like the display at the end when the plugin is done running, to identify which
workorders did not have a Travel record (so that he can then create the travel record, and run the CPC again to have the text filled in using that plugin"
*
* UPDATE: 5/27/2014 Frank wants it to create a travel if none, not just report it as an error.
*
*
*
*
* Update for new version Update RMA here are the specs, all the above and in addition:
*
make a copy of the existing RepairTekCPC plugin to use, this new and different plugin to have a new name RMA Updates
Where before the RepairTekCPC plugin would simply import whatever is in that column G of the spreadsheet into the workorder customer contact name field, now instead do the following:
* ** (NOTE WHAT IS REFERRED TO AS COLUMN "G" IS ACTUALLY COLUMN 7) **
* If the field in column G has ONLY numeric digits, then go ahead and import what is in the column G field into that AyaNova workorder's Contact field.
* AND set the Workorder Status field to REPLACED, READY TO BE REPAIRED (which is an already created Workorder Status)
REPLACED, READY TO BE REPAIRED is to be an already created Workorder Status (NOTE THIS IS A CHANGE from what I previously identified would be selected, make sure John, that THIS is the wo status set to)
the plugin will not check what was previously selected in the Workorder Status
If the field in column G has alpha or mixed alpha/numeric digits,
DO NOT import what is in this column G into that AyaNova workorder's Contact field,
and leave the Workorder Status in that workorder as is.
If the field in column G is blank, leave the Workorder Status in that workorder as is.
*
* */
#endregion specifications
/// <summary>
/// Update workorders
/// </summary>
private void Update()
{
//track ops to report upon completion
System.Text.StringBuilder sb = new StringBuilder();
sb.AppendLine("Import results:");
#region Open the xls file
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = false;
ofd.Title = "";
ofd.DefaultExt = "xlsx";
ofd.FileName = "*.xlsx";
if (ofd.ShowDialog() != DialogResult.OK) return;
Workbook workbook;
try
{
workbook = Workbook.Load(ofd.FileName);
sb.AppendLine("File " + ofd.FileName + " opened for processing...");
}
catch (Exception ex)
{
MessageBox.Show("Error opening file:\r\n" + ex.ToString());
return;
}
ofd.Dispose();
#endregion open the xls
//Projects list
ProjectList projects = ProjectList.GetList("");
//fetch priority list
PriorityPickList priorities = PriorityPickList.GetList();
//default status
WorkorderStatusPickList wspl = WorkorderStatusPickList.GetList();
Guid ReplacedReadyToBeRepairedStatusId = GetMatchingStatus(wspl, "REPLACED, READY TO BE REPAIRED");
if (ReplacedReadyToBeRepairedStatusId == Guid.Empty)
{
MessageBox.Show("Error: Workorder status of \"REPLACED, READY TO BE REPAIRED\" not found in AyaNova database and is required to proceed.");
return;
}
//Iterate the workorders
Cursor.Current = Cursors.WaitCursor;
int nRow = 0;
foreach (WorksheetRow r in workbook.Worksheets[0].Rows)
{
bool bWasError = false;//used for cleaner reporting at end
nRow++;
Workorder w;
string sWorkorderNumber = SafeGetCellValue(r, 0);
#region fetch workorder
//skip over empty rows
if (string.IsNullOrEmpty(sWorkorderNumber))
{
sb.AppendLine("INFO:\t[Row " + nRow.ToString() + "]\tskipped - empty row");
continue;
}
//skip over the title row
if (sWorkorderNumber.StartsWith("W"))
{
sb.AppendLine("INFO:\t[Row " + nRow.ToString() + "]\tlooks like headers, skipping");
continue;
}
int nWo = 0;
if (!System.Int32.TryParse(sWorkorderNumber, out nWo))
{
sb.AppendLine("ERROR:\t[Row " + nRow.ToString() + "]\ta valid workorder number was not found in import value \"" + sWorkorderNumber + "\". Skipping this row");
continue;
}
Guid woID = WorkorderInternalIDFetcher.GetItem(nWo.ToString(), WorkorderTypes.Service);
if (woID == Guid.Empty)
{
sb.AppendLine("ERROR:\t[Row " + nRow.ToString() + "]\tworkorder \"" + sWorkorderNumber + "\" not found in AyaNova. Skipping this row");
continue;
}
w = Workorder.GetItem(woID);
if (!w.IsEditable)
{
sb.AppendLine("WARN:\t[Row " + nRow.ToString() + "]\tworkorder \"" + sWorkorderNumber + "\" is not editable. Is it Closed or user has insufficient rights? Skipping this row");
continue;
}
#endregion get workorder
#region Set values from spreadsheet row
//6th column TICKET # (Workorder.Label.CustomerReferenceNumber)
string sTicketNumber = SafeGetCellValue254(r, 5);
if (!string.IsNullOrWhiteSpace(sTicketNumber))
{
if (string.IsNullOrWhiteSpace(w.CustomerReferenceNumber))
{
//easiest case, just import and move on
w.CustomerReferenceNumber = sTicketNumber;
}
else
{
//if they differ then log it
if (w.CustomerReferenceNumber != sTicketNumber)
{
sb.AppendLine("WARN:\t[Row " + nRow.ToString() + "]\tworkorder \"" + sWorkorderNumber + "\" pre-existing data in workorder TICKET# field (\"" + w.CustomerReferenceNumber + "\") does not match import file value (\"" + sTicketNumber + "\")");
bWasError = true;
}
}
}
//7th column "G" NOTIFICATION field (Workorder.Label.CustomerContactName)
//Update November 2014 - If sNotification is all numeric digits then
//Import into customer contact name field
//AND ALSO set the workorder's status field to the status associated with "REPLACED, READY TO BE REPAIRED"
//In all other cases, change nothing (i.e. alphanumeric mixed or all alpha or empty
//in 7th column's value in import spreadsheet
string sNotification = SafeGetCellValue254(r, 6);
if (!string.IsNullOrWhiteSpace(sNotification) && DigitsOnly(sNotification))
{
w.CustomerContactName = sNotification;
w.WorkorderService.WorkorderStatusID = ReplacedReadyToBeRepairedStatusId;
}
//8th column PROBLEM DESCRIPTI (O.Project matchup to insert in Workorder.ProjectID)
string sProblem = SafeGetCellValue(r, 7);
Guid projectId = GetMatchingProject(projects, sProblem);
if (projectId != Guid.Empty)
{
w.ProjectID = projectId;
}
else
{
//no match, was it because it couldn't match a value which should be logged, or just empty which doesn't need to be logged?
if (!string.IsNullOrWhiteSpace(sProblem))
{
sb.AppendLine("WARNING:\t[Row " + nRow.ToString() + "]\tworkorder \"" + sWorkorderNumber + "\" PROBLEM DESCRIPTION field value: \"" + sProblem + "\" can't be matched to existing data in AyaNova");
bWasError = true;
}
}
//9th column CANADA POST WC field (Workorder.Label.Summary)
string sCanadaPostWC = SafeGetCellValue254(r, 8);
//always overwrite unless empty
if (!string.IsNullOrWhiteSpace(sCanadaPostWC))
w.Summary = sCanadaPostWC;
//10th column INBOUND TRACKING # field (WorkorderItem.Label.Summary)
string sInboundTrackingNumber = SafeGetCellValue254(r, 9);
//always overwrite unless empty
if (!string.IsNullOrWhiteSpace(sInboundTrackingNumber))
{
//iterate just in case there is more than one woitem though there should not be, best to be safe though.
foreach (WorkorderItem wi in w.WorkorderItems)
{
wi.Summary = sInboundTrackingNumber;
}
}
//11th column DATA field (WorkorderItem.Label.PriorityID matchup from list)
string sData = SafeGetCellValue(r, 10);
Guid priorityId = GetMatchingPriority(priorities, sData);
if (priorityId != Guid.Empty)
{
//iterate just in case there is more than one woitem though there should not be, best to be safe though.
foreach (WorkorderItem wi in w.WorkorderItems)
{
wi.PriorityID = priorityId;
}
}
else
{
//no match, was it because it couldn't match a value which should be logged, or just empty which doesn't need to be logged?
if (!string.IsNullOrWhiteSpace(sData))
{
sb.AppendLine("WARNING:\t[Row " + nRow.ToString() + "]\tworkorder \"" + sWorkorderNumber + "\" DATA field value: \"" + sData + "\" can't be matched to existing data in AyaNova");
bWasError = true;
}
}
//12th column CONTACT field (WorkorderItemTravel.Label.TravelDetails)
string sContact = SafeGetCellValue(r, 11);
if (!string.IsNullOrWhiteSpace(sContact))
{
if (!w.WorkorderItems[0].HasTravel)
w.WorkorderItems[0].Travels.Add(w.WorkorderItems[0]);
w.WorkorderItems[0].Travels[0].TravelDetails = sContact;
////if there is a travel record then update it
// if (w.WorkorderItems[0].HasTravel)
// {
// }
// else
// {
// //No travel item? Then it's an error and needs to be appended to the error table
// sb.AppendLine("WARNING:\t[Row " + nRow.ToString() + "]\tworkorder \"" + sWorkorderNumber + "\" CONTACT field value: \"" + sData + "\" can't be set because there is no travel record");
// bWasError = true;
// }
}
#endregion set values from spreadsheet row
if (w.IsSavable)
{
w.Save();
sb.AppendLine("INFO:\t[Row " + nRow.ToString() + "]\tworkorder \"" + sWorkorderNumber + "\" updated in AyaNova");
}
else
{
if (!w.IsDirty)
{
sb.AppendLine("INFO:\t[Row " + nRow.ToString() + "]\tworkorder \"" + sWorkorderNumber + "\" no changes " + (bWasError ? "made" : "required"));
}
else//must be due to broken rules
sb.AppendLine("ERROR:\t[Row " + nRow.ToString() + "]\tworkorder \"" + sWorkorderNumber + "\" couldn't be saved due to broken rules, error was:\r\n----------------------------------\r\n" + w.BrokenRulesText + "\r\n----------------------------------");
}
}
Cursor.Current = Cursors.Default;
sb.AppendLine("All rows in spreadsheet processed!");
//Report results
ReportResults(sb);
}
private void ReportResults(StringBuilder sb)
{
CopyableMessageBox dlgCMB = new CopyableMessageBox(sb.ToString());
dlgCMB.ShowDialog();
}
//safe get cell value
private string SafeGetCellValue(WorksheetRow r, int nColumn)
{
object o = r.Cells[nColumn].Value;
if (o == null) return "";
else
return o.ToString();
}
//safe get cell value no longer than 254 characters
private string SafeGetCellValue254(WorksheetRow r, int nColumn)
{
object o = r.Cells[nColumn].Value;
if (o == null) return "";
else
{
string ret = o.ToString();
return ret.Length <= 254 ? ret : ret.Substring(0, 254);
}
}
/// <summary>
/// Attempt to match priority from text in spreadsheet
/// if no match return guid empty
/// </summary>
/// <param name="ppl"></param>
/// <param name="sToMatch"></param>
/// <returns></returns>
Guid GetMatchingPriority(PriorityPickList ppl, string sToMatch)
{
foreach (PriorityPickList.PriorityPickListInfo i in ppl)
{
if (i.Name.Equals(sToMatch, StringComparison.InvariantCultureIgnoreCase))
{
return i.ID;
}
}
return Guid.Empty;
}
/// <summary>
/// Attempt to match Project from text in spreadsheet
/// if no match return guid empty
/// </summary>
/// <param name="l"></param>
/// <param name="sToMatch"></param>
/// <returns></returns>
Guid GetMatchingProject(ProjectList l, string sToMatch)
{
foreach (ProjectList.ProjectListInfo i in l)
{
if (i.LT_O_Project.Display.Equals(sToMatch, StringComparison.InvariantCultureIgnoreCase))
{
return i.LT_O_Project.Value;
}
}
return Guid.Empty;
}
/// <summary>
/// Attempt to match status from text in spreadsheet
/// if no match return guid empty
/// </summary>
/// <param name="ppl"></param>
/// <param name="sToMatch"></param>
/// <returns></returns>
Guid GetMatchingStatus(WorkorderStatusPickList ppl, string sToMatch)
{
foreach (WorkorderStatusPickList.WorkorderStatusPickListInfo i in ppl)
{
if (i.Name.Equals(sToMatch, StringComparison.InvariantCultureIgnoreCase))
{
return i.ID;
}
}
return Guid.Empty;
}
/// <summary>
/// True if string is only digits, false otherwise
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
bool DigitsOnly(string s)
{
foreach (char c in s)
{
if (c < '0' || c > '9')
return false;
}
return true;
}
#endregion update
}
}