297 lines
16 KiB
C#
297 lines
16 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace AyaNovaQBI
|
|
{
|
|
public partial class FixInvoiceProblems : Form
|
|
{
|
|
public List<util.MisMatch> MisMatches { get; set; }
|
|
public List<long> PartPriceOverrides { get; set; }
|
|
public bool ChangesMade { get; set; } = false;
|
|
public FixInvoiceProblems()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void FixInvoiceProblems_Load(object sender, EventArgs e)
|
|
{
|
|
grid.DataSource = MisMatches;
|
|
btnOK.Text = util.AyaTranslations["OK"];
|
|
}
|
|
|
|
private void btnOK_Click(object sender, EventArgs e)
|
|
{
|
|
DialogResult = DialogResult.OK;
|
|
Close();
|
|
}
|
|
|
|
private async void grid_CellContentClick(object sender, DataGridViewCellEventArgs e)
|
|
{
|
|
var senderGrid = (DataGridView)sender;
|
|
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn)
|
|
{
|
|
|
|
var mm = MisMatches[e.RowIndex];//grid event index is same row as collection index so this saves hassles with rows and accessors
|
|
|
|
switch (mm.Reason)
|
|
{
|
|
case util.MisMatchReason.NothingToInvoice:
|
|
MessageBox.Show("Nothing to invoice");
|
|
break;
|
|
case util.MisMatchReason.PriceDifferent:
|
|
{
|
|
try
|
|
{
|
|
#region price problem
|
|
FixPriceDifference d = new FixPriceDifference();
|
|
d.OptionTitle = mm.Name + "\r\n" +
|
|
"Price on work order: " + mm.AyaPrice.ToString("c") + "\r\n" +
|
|
"Price in QuickBooks: " + mm.QBPrice.ToString("c");
|
|
if (d.ShowDialog() == DialogResult.Cancel) return;
|
|
//Fixup price here
|
|
switch (d.SelectedResolution)
|
|
{
|
|
case "AYAONCE":
|
|
//Add to price override list so price on workorder item part record is used
|
|
PartPriceOverrides.Add(mm.WorkOrderItemPartId);
|
|
break;
|
|
case "QBONCE":
|
|
{
|
|
//Change the workorder item part price only
|
|
//to the quickbooks price, don't touch the default aya part price
|
|
var a = await util.GetAsync($"workorder/{mm.WorkOrderId}");
|
|
WorkOrder w = a.ObjectResponse["data"].ToObject<WorkOrder>();
|
|
if (w == null)
|
|
throw new Exception($"FixInvoiceProblems:QBONCE:PRICE: WorkOrder with id {mm.WorkOrderId} was not found in AyaNova and may have just been deleted.\r\nUnable to update wo.");
|
|
|
|
var wip = w.Items.First(z => z.Id == mm.WorkOrderItemId).Parts.First(z => z.Id == mm.WorkOrderItemPartId);
|
|
wip.PriceOverride = mm.QBPrice;
|
|
await util.PostAsync("workorder", Newtonsoft.Json.JsonConvert.SerializeObject(w));
|
|
}
|
|
|
|
break;
|
|
case "CHANGEAYA":
|
|
{
|
|
//Change this workorder item parts price to the qb price
|
|
//and change ayanova's default price for this part to the qb price
|
|
|
|
var a = await util.GetAsync($"workorder/{mm.WorkOrderId}");
|
|
WorkOrder w = a.ObjectResponse["data"].ToObject<WorkOrder>();
|
|
if (w == null)
|
|
throw new Exception($"FixInvoiceProblems:CHANGEAYA:PRICE: WorkOrder with id {mm.WorkOrderId} was not found in AyaNova and may have just been deleted.\r\nUnable to update wo.");
|
|
|
|
var wip = w.Items.First(z => z.Id == mm.WorkOrderItemId).Parts.First(z => z.Id == mm.WorkOrderItemPartId);
|
|
long PartId = wip.PartId;
|
|
wip.PriceOverride = mm.QBPrice;
|
|
await util.PostAsync("workorder", Newtonsoft.Json.JsonConvert.SerializeObject(w));
|
|
|
|
if (PartId != 0)
|
|
{
|
|
var partResponse = await util.GetAsync($"part/{PartId}");
|
|
Part p = partResponse.ObjectResponse["data"].ToObject<Part>();
|
|
if (w == null)
|
|
throw new Exception($"FixInvoiceProblems:CHANGEAYA:PRICE: Part with id {PartId} was not found in AyaNova and may have just been deleted.\r\nUnable to update Part.");
|
|
p.Retail = mm.QBPrice;
|
|
await util.PostAsync("part", Newtonsoft.Json.JsonConvert.SerializeObject(p));
|
|
}
|
|
}
|
|
break;
|
|
case "CHANGEQB":
|
|
//Change the QB price to use the price on this workorder item part
|
|
await util.ChangeQBItemPrice(mm.QBListID, mm.AyaPrice);
|
|
break;
|
|
}
|
|
|
|
//remove the object from the grid as it's now dealt with
|
|
e.Cell.Row.Delete(false);
|
|
ChangesMade = true;
|
|
//If all done then close up
|
|
if (grid.Rows.Count == 0)
|
|
this.Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
await util.CrackDisplayAndIntegrationLogException(ex, "FixInvoiceProblems:PRICE");
|
|
}
|
|
|
|
#endregion price prob.
|
|
}
|
|
break;
|
|
case util.MisMatchReason.NotLinkedToQB:
|
|
{
|
|
#region link problem
|
|
LinkOrImportAyaObject d = new LinkOrImportAyaObject();
|
|
try
|
|
{
|
|
d.AyaItem = e.Cell.Row.Cells["Name"].Value.ToString();
|
|
//Default for an import
|
|
//otherwise in a link is just reset to the qb item name selected
|
|
string QBItemName = e.Cell.Row.Cells["Name"].Value.ToString();
|
|
|
|
//Attempt a link or import
|
|
//in any case of failure or new link not required
|
|
//bails inside switch
|
|
switch ((RootObjectTypes)e.Cell.Row.Cells["ObjectType"].Value)
|
|
{
|
|
case RootObjectTypes.Client:
|
|
d.CanImport = true;
|
|
d.QBItems = util.QBClients;
|
|
if (d.ShowDialog() == DialogResult.Cancel) return;
|
|
if (d.Choice == "IMPORT")
|
|
{
|
|
ArrayList alErrors = new ArrayList();
|
|
util.ImportAyaClient((Guid)e.Cell.Row.Cells["RootObjectID"].Value, alErrors);
|
|
//display errors if any
|
|
if (alErrors.Count != 0)
|
|
{
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.Append("Import failed with error:\r\n\r\n");
|
|
foreach (object o in alErrors)
|
|
{
|
|
sb.Append((string)o);
|
|
sb.Append("\r\n************\r\n");
|
|
|
|
}
|
|
|
|
CopyableMessageBox cb = new CopyableMessageBox(sb.ToString());
|
|
cb.ShowDialog();
|
|
cb.Dispose();
|
|
return;
|
|
|
|
}
|
|
else
|
|
goto REMOVEITEMS;
|
|
}
|
|
else
|
|
{
|
|
//it's a link by default
|
|
if (d.SelectedQBItem == null || d.SelectedQBItem == "" || !util.QBClients.Rows.Contains(d.SelectedQBItem))
|
|
return;
|
|
QBItemName = d.SelectedQBItemName;
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
case RootObjectTypes.Rate:
|
|
d.CanImport = false;
|
|
d.QBItems = util.QBItems;
|
|
if (d.ShowDialog() == DialogResult.Cancel) return;
|
|
|
|
if (d.SelectedQBItem == null || d.SelectedQBItem == "" || !util.QBItems.Rows.Contains(d.SelectedQBItem))
|
|
return;
|
|
QBItemName = d.SelectedQBItemName;
|
|
break;
|
|
case RootObjectTypes.Part:
|
|
d.CanImport = false;
|
|
d.QBItems = util.QBItems;
|
|
if (d.ShowDialog() == DialogResult.Cancel) return;
|
|
|
|
if (d.SelectedQBItem == null || d.SelectedQBItem == "" || !util.QBItems.Rows.Contains(d.SelectedQBItem))
|
|
return;
|
|
QBItemName = d.SelectedQBItemName;
|
|
|
|
break;
|
|
case RootObjectTypes.WorkorderItemOutsideService:
|
|
d.CanImport = false;
|
|
d.QBItems = util.QBItems;
|
|
if (d.ShowDialog() == DialogResult.Cancel) return;
|
|
|
|
|
|
if (d.SelectedQBItem == null || d.SelectedQBItem == "" || !util.QBItems.Rows.Contains(d.SelectedQBItem))
|
|
return;
|
|
|
|
util.QDat.OutsideServiceChargeAs = d.SelectedQBItem;
|
|
|
|
//Case 299
|
|
util.QBI.AIObject = util.QDat.XMLData;
|
|
//util.QBI.AIObject = util.QDat;
|
|
|
|
util.QBI = (Integration)util.QBI.Save();
|
|
util.QDat.IsDirty = false;
|
|
goto REMOVEITEMS;
|
|
|
|
|
|
case RootObjectTypes.WorkorderItemLoan:
|
|
d.CanImport = false;
|
|
d.QBItems = util.QBItems;
|
|
if (d.ShowDialog() == DialogResult.Cancel) return;
|
|
|
|
|
|
if (d.SelectedQBItem == null || d.SelectedQBItem == "" || !util.QBItems.Rows.Contains(d.SelectedQBItem))
|
|
return;
|
|
|
|
util.QDat.WorkorderItemLoanChargeAs = d.SelectedQBItem;
|
|
//Case 299
|
|
util.QBI.AIObject = util.QDat.XMLData;
|
|
//util.QBI.AIObject = util.QDat;
|
|
util.QBI = (Integration)util.QBI.Save();
|
|
util.QDat.IsDirty = false;
|
|
goto REMOVEITEMS;
|
|
|
|
|
|
case RootObjectTypes.WorkorderItemMiscExpense:
|
|
d.CanImport = false;
|
|
d.QBItems = util.QBItems;
|
|
if (d.ShowDialog() == DialogResult.Cancel) return;
|
|
|
|
|
|
if (d.SelectedQBItem == null || d.SelectedQBItem == "" || !util.QBItems.Rows.Contains(d.SelectedQBItem))
|
|
return;
|
|
|
|
util.QDat.MiscExpenseChargeAs = d.SelectedQBItem;
|
|
//Case 299
|
|
util.QBI.AIObject = util.QDat.XMLData;
|
|
//util.QBI.AIObject = util.QDat;
|
|
util.QBI = (Integration)util.QBI.Save();
|
|
util.QDat.IsDirty = false;
|
|
goto REMOVEITEMS;
|
|
|
|
|
|
}
|
|
|
|
|
|
//add the new link
|
|
IntegrationMap m = util.QBI.Maps.Add(util.QBI);
|
|
m.RootObjectID = (Guid)e.Cell.Row.Cells["RootObjectID"].Value;
|
|
m.RootObjectType = (RootObjectTypes)e.Cell.Row.Cells["ObjectType"].Value;
|
|
m.ForeignID = d.SelectedQBItem;
|
|
m.Name = QBItemName;
|
|
m.LastSync = System.DateTime.Now;
|
|
util.QBI = (Integration)util.QBI.Save();
|
|
|
|
REMOVEITEMS:
|
|
|
|
//remove the object from the grid as it's now dealt with
|
|
e.Cell.Row.Delete(false);
|
|
_ChangesMade = true;
|
|
//If all done then close up
|
|
if (grid.Rows.Count == 0)
|
|
this.Close();
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
await util.CrackDisplayAndIntegrationLogException(ex, "FixInvoiceProblems:NOTLINKED");
|
|
}
|
|
finally
|
|
{
|
|
d.Dispose();
|
|
}
|
|
#endregion link problem
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}//eof
|
|
}//eoc
|
|
}//eons
|