From 965125256a711b75934903491c4c485c390b8a77 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Tue, 12 Jul 2022 21:05:00 +0000 Subject: [PATCH] --- AyaNovaQBI/MainForm.cs | 89 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 86 insertions(+), 3 deletions(-) diff --git a/AyaNovaQBI/MainForm.cs b/AyaNovaQBI/MainForm.cs index db751cb..29a5822 100644 --- a/AyaNovaQBI/MainForm.cs +++ b/AyaNovaQBI/MainForm.cs @@ -215,6 +215,7 @@ namespace AyaNovaQBI private DataTable _GridTable = null; private List _MisMatches = new List(); private List _PartPriceOverrides = new List(); + private List _GridTableSourceList = new List(); /// /// Initialize invoices dataset /// from scratch. If a previous @@ -247,12 +248,13 @@ namespace AyaNovaQBI _GridTable.Clear(); } _MisMatches.Clear(); + _GridTableSourceList.Clear(); //[HttpGet("accounting-list-billable/{workOrderStatusId}")] var r = await util.GetAsync($"workorder/accounting-list-billable/{util.QDat.PreWOStatus}"); - var v = r.ObjectResponse["data"].ToObject>(); + _GridTableSourceList = r.ObjectResponse["data"].ToObject>(); - foreach (WorkOrderAccountingListItem z in v) + foreach (WorkOrderAccountingListItem z in _GridTableSourceList) { w.Step = "WO: " + z.Serial; DataRow row = _GridTable.NewRow(); @@ -291,21 +293,102 @@ namespace AyaNovaQBI /// private async Task InvoiceSelected(bool bOneWoPerInvoice) { + + + if (grid.SelectedRows.Count < 1) { MessageBox.Show("There are no Work order rows selected\r\nSelect one or more rows to invoice"); return; } + + //build a list of selected work orders ordered by customer and that have linked=true to process here so that they are in customer order and only good ones that can be billed + + List InvoiceableWorkOrdersList = new List(); + foreach (DataGridViewRow r in grid.SelectedRows) + { + if ((bool)r.Cells["Linked"].Value == true) + { + InvoiceableWorkOrdersList.Add(_GridTableSourceList.First(z => z.Id == (long)r.Cells["id"].Value)); + } + } + + if (InvoiceableWorkOrdersList.Count < 1) + { + MessageBox.Show("There are no invoiceable Work order rows selected\r\nSelect one or more rows to invoice"); + return; + } + + //We now have a list of work orders from grid selections ready to be invoiced and ordered by customer for grouping purposes + //An array list to hold the guid's of workorders being invoiced List al = new List(); - this.Refresh(); + Cursor.Current = Cursors.WaitCursor; ArrayList alErrors = new ArrayList(); Waiting w = new Waiting(); w.Show(); w.Ops = "Invoicing to QuickBooks..."; + + if (bOneWoPerInvoice) + { + foreach(var invwo in InvoiceableWorkOrdersList) + { + al.Add(invwo.Id); + w.Step = "Invoicing single workorder "; + await util.Invoice(al, alErrors); + al.Clear(); + } + } + else + { + + /* + var results = persons.GroupBy( + p => p.PersonId, + p => p.car, + (key, g) => new { PersonId = key, Cars = g.ToList() }); + */ + var grouped = InvoiceableWorkOrdersList.GroupBy(z => z.CustomerId, z => z.Id, (Key, g) => new { CustomerId=Key, WorkOrders=g.ToList() }); + foreach(var vv in grouped) + { + string sss=vv.CustomerId.ToString(); + } + } + + //long currentCustomerId = -1; + //int CountOfInvoiceableWorkOrders = InvoiceableWorkOrdersList.Count; + //for (int i = 0; i < CountOfInvoiceableWorkOrders; i++) + //{ + // bool finalRecord = CountOfInvoiceableWorkOrders - 1 == i; + // WorkOrderAccountingListItem invwo = InvoiceableWorkOrdersList[i]; + // if (bOneWoPerInvoice) + // { + // al.Add(invwo.Id); + // w.Step = "Invoicing single workorder "; + // await util.Invoice(al, alErrors); + // //and clear al workorders list + // al.Clear(); + // } + // else + // { + // //change of customer or last one in loop? + // if ((finalRecord || invwo.CustomerId != currentCustomerId) && al.Count > 0) + // { + // //invoice out this batch + // w.Step = "Batch invoicing " + al.Count.ToString() + ((al.Count > 1) ? " Work Orders" : " Work Order"); + // await util.Invoice(al, alErrors); + // al.Clear(); + // } + // currentCustomerId = invwo.CustomerId; + // al.Add(invwo.Id); + + // } + //} + + //work through selected items top to bottom foreach (DataGridViewRow r in grid.SelectedRows) {