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 MisMatches { get; set; } public List 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: { #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 Guid WorkorderItemPartID = (Guid)e.Cell.Row.Cells["WorkorderItemPartID"].Value; Workorder w = Workorder.GetWorkorderByRelative(RootObjectTypes.WorkorderItemPart, WorkorderItemPartID); foreach (WorkorderItem wi in w.WorkorderItems) { if (w == null) break; foreach (WorkorderItemPart wp in wi.Parts) { if (wp.ID == WorkorderItemPartID) { wp.PriceOverride = (decimal)e.Cell.Row.Cells["QBPrice"].Value; w.Save(); w = null; break; } } } } 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 Guid WorkorderItemPartID = (Guid)e.Cell.Row.Cells["WorkorderItemPartID"].Value; Workorder w = Workorder.GetWorkorderByRelative(RootObjectTypes.WorkorderItemPart, WorkorderItemPartID); Guid PartID = Guid.Empty; foreach (WorkorderItem wi in w.WorkorderItems) { if (w == null) break; foreach (WorkorderItemPart wp in wi.Parts) { if (wp.ID == WorkorderItemPartID) { PartID = wp.PartID; wp.PriceOverride = (decimal)e.Cell.Row.Cells["QBPrice"].Value; w.Save(); w = null; break; } } } if (PartID != Guid.Empty) { Part p = Part.GetItem(PartID); p.Retail = (decimal)e.Cell.Row.Cells["QBPrice"].Value; p.Save(); } } 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(); #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(); } finally { d.Dispose(); } #endregion link problem } break; } } }//eof }//eoc }//eons