private void OKButton_Click(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor; var invoiceItem1 = new InvoiceItem() { Amount = Convert.ToDouble(costTextBox.Text), Description = DescTextBox1.Text, QuickBooksID = qbItemIDTextBox.Text, Quantity = 1, }; var invoiceItem2 = new InvoiceItem() { Amount = Convert.ToDouble(costBTextBox.Text), Description = DescTextBox2.Text, QuickBooksID = qbItemIDBTextBox.Text, Quantity = 1, }; var invoiceItem3 = new InvoiceItem() { Amount = Convert.ToDouble(costCTextBox.Text), Description = DescTextBox3.Text, QuickBooksID = qbItemIDCTextBox.Text, Quantity = 1, }; var invoiceItem4 = new InvoiceItem() { Amount = Convert.ToDouble(costDTextBox.Text), Description = DescTextBox4.Text, QuickBooksID = qbItemIDDTextBox.Text, Quantity = 1, }; if (AppendAddressCheckBox.Checked) { invoiceItem1.Description = DescTextBox1.Text + " - (" + addressTextBox.Text + ")"; invoiceItem2.Description = DescTextBox2.Text + " - (" + addressTextBox.Text + ")"; invoiceItem3.Description = DescTextBox3.Text + " - (" + addressTextBox.Text + ")"; invoiceItem4.Description = DescTextBox4.Text + " - (" + addressTextBox.Text + ")"; } //get item account //int index = ItemComboBox1.SelectedIndex; //Item item1 = pQBitems1[index]; qBInvoiceIDTextBox.Text = AddModQBInvoice(CustomerNameTextBox.Text, qBInvoiceIDTextBox.Text, invoiceItem1, invoiceItem2, invoiceItem3, invoiceItem4); qbItemIDTextBox.Text = invoiceItem1.QuickBooksID; qbItemIDBTextBox.Text = invoiceItem2.QuickBooksID; qbItemIDCTextBox.Text = invoiceItem3.QuickBooksID; qbItemIDDTextBox.Text = invoiceItem4.QuickBooksID; this.customerJobBindingSource.EndEdit(); this.tableAdapterManager.UpdateAll(this.mMSDataSet); Cursor.Current = Cursors.Default; }
public string AddModQBInvoice(string szCustomerName, string szInvoiceQuickbookID, InvoiceItem invoiceItem1, DateTime jobDate, string itemA_ID) { bool sessionBegun = false; bool connectionOpen = false; QBSessionManager sessionManager = null; string invoiceID = ""; bool bItemInvoice1 = false; try { //DateTime curDate = DateTime.Now; DateTime curDate = jobDate; //otherwise find for the lastest one /* int prevMonth = DateTime.Today.AddMonths(-1).Month; int curMonth = DateTime.Today.Month; int nextMonth = DateTime.Today.AddMonths(1).Month; int year = DateTime.Today.Year; */ //int prevMonth = curDate.AddMonths(-1).Month; int curMonth = curDate.Month; int nextMonth = curDate.AddMonths(1).Month; int year = curDate.Year; int nextyear = curDate.AddYears(1).Year; DateTime fromDate; DateTime toDate; Item item1 = pQBitems1[ItemComboBox1.SelectedIndex]; //Create the session Manager object sessionManager = new QBSessionManager(); //Create the message set request object to hold our request IMsgSetRequest requestMsgSet = sessionManager.CreateMsgSetRequest("US", 8, 0); requestMsgSet.Attributes.OnError = ENRqOnError.roeContinue; sessionManager.OpenConnection(MMSUtility.QBAppID, MMSUtility.QBAppName); connectionOpen = true; sessionManager.BeginSession("", ENOpenMode.omDontCare); sessionBegun = true; //find the customer information ICustomerQuery search = requestMsgSet.AppendCustomerQueryRq(); search.ORCustomerListQuery.CustomerListFilter.ORNameFilter.NameFilter.MatchCriterion.SetValue(ENMatchCriterion.mcStartsWith); search.ORCustomerListQuery.CustomerListFilter.ORNameFilter.NameFilter.Name.SetValue(szCustomerName); IMsgSetResponse responseMsgSet = sessionManager.DoRequests(requestMsgSet); IResponse response = responseMsgSet.ResponseList.GetAt(0); ICustomerRetList customerRetList = (ICustomerRetList)response.Detail; if (customerRetList == null) { //MessageBox.Show("Cannot find customer in QB!\nPlease check QB for Customer Name."); return invoiceID; } ICustomerRet customerRet = customerRetList.GetAt(0); Customer customer = new Customer(); customer.Name = customerRet.Name.GetValue(); customer.QuickBooksID = customerRet.ListID.GetValue(); customer.EditSequence = customerRet.EditSequence.GetValue(); if (customerRet.FirstName != null) customer.FirstName = customerRet.FirstName.GetValue(); if (customerRet.LastName != null) customer.LastName = customerRet.LastName.GetValue(); bool notFound = true; if (szInvoiceQuickbookID.Length > 0) { //get the lastest associated invoices for the particular customer requestMsgSet.ClearRequests(); IInvoiceQuery invoiceQueryRq = requestMsgSet.AppendInvoiceQueryRq(); invoiceQueryRq.IncludeLineItems.SetValue(true); //if invoiceID exist, use that invoiceQueryRq.ORInvoiceQuery.TxnIDList.Add(szInvoiceQuickbookID); //Send the request and get the response from QuickBooks responseMsgSet = sessionManager.DoRequests(requestMsgSet); response = responseMsgSet.ResponseList.GetAt(0); IInvoiceRetList invoiceRetList1 = (IInvoiceRetList)response.Detail; if (invoiceRetList1 != null) { notFound = false; } } // there is no existing invoice based on id, then search for invoice on date if (notFound) { //get the from prevmonth to curmonth invoice if ((curDate.Day >= 1) && (curDate.Day <= 19)) { fromDate = new DateTime(year, curMonth, 1); toDate = new DateTime(year, curMonth, 20); } //get curmonth to nextmonth invoice else { if (nextMonth == 1) { fromDate = new DateTime(year, curMonth, 21); toDate = new DateTime(nextyear, nextMonth, 20); } else { //if next month is jan, then account for next year fromDate = new DateTime(year, curMonth, 21); toDate = new DateTime(year, nextMonth, 20); } } //get the lastest associated invoices for the particular customer requestMsgSet.ClearRequests(); IInvoiceQuery invoiceQueryRq = requestMsgSet.AppendInvoiceQueryRq(); invoiceQueryRq.IncludeLineItems.SetValue(true); invoiceQueryRq.ORInvoiceQuery.InvoiceFilter.EntityFilter.OREntityFilter.FullNameList.Add(szCustomerName); invoiceQueryRq.ORInvoiceQuery.InvoiceFilter.ORDateRangeFilter.TxnDateRangeFilter.ORTxnDateRangeFilter.TxnDateFilter.FromTxnDate.SetValue(fromDate); invoiceQueryRq.ORInvoiceQuery.InvoiceFilter.ORDateRangeFilter.TxnDateRangeFilter.ORTxnDateRangeFilter.TxnDateFilter.ToTxnDate.SetValue(toDate); //ENPaidStatus status = ENPaidStatus.psNotPaidOnly; //invoiceQueryRq.ORInvoiceQuery.InvoiceFilter.PaidStatus.SetValue(status); } //Send the request and get the response from QuickBooks responseMsgSet = sessionManager.DoRequests(requestMsgSet); response = responseMsgSet.ResponseList.GetAt(0); IInvoiceRetList invoiceRetList = (IInvoiceRetList)response.Detail; //check for FC int numIndex = -1; if (invoiceRetList != null) { numIndex = 0; IInvoiceRet invoiceRet1 = invoiceRetList.GetAt(numIndex); string refNum = invoiceRet1.RefNumber.GetValue(); if (refNum.StartsWith("FC")) { //get the next one if (invoiceRetList.Count > 1) numIndex = 1; else numIndex = -1; } } //if need new invoice if ((invoiceRetList == null) || (numIndex == -1)) { Invoice invoice = new Invoice(); invoice.Customer = customer; requestMsgSet.ClearRequests(); IInvoiceAdd invoiceAddRq = requestMsgSet.AppendInvoiceAddRq(); //get the current month DateTime invoiceDate; if ((curDate.Day >= 1) && (curDate.Day <= 19)) { invoiceDate = new DateTime(year, curMonth, 20); } //get next month else { if (nextMonth == 1) { invoiceDate = new DateTime(nextyear, nextMonth, 20); } else { //if next month is jan, then account for next year invoiceDate = new DateTime(year, nextMonth, 20); }; } invoiceAddRq.TxnDate.SetValue(invoiceDate); invoiceAddRq.CustomerRef.ListID.SetValue(invoice.Customer.QuickBooksID); ///////////////////////////////////////////////////////////////////////////////// if (invoiceItem1.Amount >= 0) { if (invoiceItem1.Amount == 0) { string msg = "Warning: Invoice = $0 for" + szCustomerName; MMSUtility.LogFile(msg, "", "AddModQBInvoice", 0, "QBBatchInvoiceDialog"); } IORInvoiceLineAdd orInvoiceLineAdd1 = invoiceAddRq.ORInvoiceLineAddList.Append(); orInvoiceLineAdd1.InvoiceLineAdd.ItemRef.ListID.SetValue(item1.QuickBooksID); orInvoiceLineAdd1.InvoiceLineAdd.ServiceDate.SetValue(invoiceItem1.Date); orInvoiceLineAdd1.InvoiceLineAdd.Amount.SetValue(invoiceItem1.Amount); orInvoiceLineAdd1.InvoiceLineAdd.Quantity.SetValue(invoiceItem1.Quantity); orInvoiceLineAdd1.InvoiceLineAdd.Desc.SetValue(invoiceItem1.Description); bItemInvoice1 = true; } //Send the request and get the response from QuickBooks responseMsgSet = sessionManager.DoRequests(requestMsgSet); response = responseMsgSet.ResponseList.GetAt(0); IInvoiceRet invoiceRet = (IInvoiceRet)response.Detail; if (bItemInvoice1) invoiceItem1.QuickBooksID = invoiceRet.ORInvoiceLineRetList.GetAt(0).InvoiceLineRet.TxnLineID.GetValue(); invoice.QuickBooksID = invoiceRet.TxnID.GetValue(); invoiceID = invoice.QuickBooksID; } else { //get the latest invoice IInvoiceRet invoiceRet = invoiceRetList.GetAt(numIndex); var invoice = new Invoice { Customer = customer, QuickBooksID = invoiceRet.TxnID.GetValue(), EditSequence = invoiceRet.EditSequence.GetValue() }; //invoiceRet.TxnNumber.GetValue(); requestMsgSet.ClearRequests(); IInvoiceMod invoiceModRq = requestMsgSet.AppendInvoiceModRq(); invoiceModRq.TxnID.SetValue(invoice.QuickBooksID); invoiceModRq.EditSequence.SetValue(invoice.EditSequence); //if no assocated item, then add new item to the current invoice //andy change this if (invoiceRet.ORInvoiceLineRetList != null) { for (int j = 0; j < invoiceRet.ORInvoiceLineRetList.Count; j++) { IORInvoiceLineRet ORInvoiceLineRet = invoiceRet.ORInvoiceLineRetList.GetAt(j); if (itemA_ID != ORInvoiceLineRet.InvoiceLineRet.TxnLineID.GetValue()) { IORInvoiceLineMod ORinvoicemod1 = invoiceModRq.ORInvoiceLineModList.Append(); ORinvoicemod1.InvoiceLineMod.TxnLineID.SetValue(ORInvoiceLineRet.InvoiceLineRet.TxnLineID.GetValue()); } } } //this add the new line if (invoiceItem1.Amount >= 0) { if (invoiceItem1.Amount == 0) { string msg = "Warning: Invoice = $0 for" + szCustomerName; MMSUtility.LogFile(msg, "", "AddModQBInvoice", 0, "QBBatchInvoiceDialog"); } IORInvoiceLineMod ORinvoicemod1 = invoiceModRq.ORInvoiceLineModList.Append(); ORinvoicemod1.InvoiceLineMod.TxnLineID.SetValue("-1"); ORinvoicemod1.InvoiceLineMod.ServiceDate.SetValue(invoiceItem1.Date); ORinvoicemod1.InvoiceLineMod.ItemRef.ListID.SetValue(item1.QuickBooksID); ORinvoicemod1.InvoiceLineMod.Amount.SetValue(invoiceItem1.Amount); ORinvoicemod1.InvoiceLineMod.Quantity.SetValue(invoiceItem1.Quantity); ORinvoicemod1.InvoiceLineMod.Desc.SetValue(invoiceItem1.Description); bItemInvoice1 = true; } //Send the request and get the response from QuickBooks responseMsgSet = sessionManager.DoRequests(requestMsgSet); response = responseMsgSet.ResponseList.GetAt(0); invoiceRet = (IInvoiceRet)response.Detail; invoice.QuickBooksID = invoiceRet.TxnID.GetValue(); invoiceID = invoice.QuickBooksID; int nIndex = invoiceRet.ORInvoiceLineRetList.Count - 1; if (bItemInvoice1) invoiceItem1.QuickBooksID = invoiceRet.ORInvoiceLineRetList.GetAt(nIndex).InvoiceLineRet.TxnLineID.GetValue(); } } catch (Exception ex) { MMSUtility.LogFile(ex.Message, "", "AddModQBInvoice", ex.LineNumber(), "QBBatchInvoiceDialog"); MessageBox.Show(ex.Message, "Error"); } finally { //End the session and close the connection to QuickBooks if (sessionBegun) { sessionManager.EndSession(); } if (connectionOpen) { sessionManager.CloseConnection(); } } return invoiceID; }
/* private void RouteButton_Click(object sender, EventArgs e) { string propertyID = ""; int nOrderNum = 0; string szRouteName = ""; string szNotes = ""; AssignRouteDialog dlg = new AssignRouteDialog(); if (dlg.ShowDialog() == DialogResult.OK) { szRouteName = dlg.GetRouteText(); foreach (DataGridViewRow dataGridViewRow in extendedDataGridView1.SelectedRows) { propertyID = dataGridViewRow.Cells[13].Value.ToString(); if (routeTableAdapter1.GetRouteExist(szRouteName, Convert.ToInt32(propertyID)) == 0) routeTableAdapter1.Insert(Convert.ToInt32(propertyID), nOrderNum, szRouteName, szNotes); } MainForm pParent = (MainForm)this.ParentForm; pParent.RefreshRouteView(); } }*/ //////////I////////////////////////////////////////////////////////////////////// private void QBInvoiceButton_Click(object sender, EventArgs e) { int saveStatus = JobStatusFilterComboBox.SelectedIndex; MainForm pParent = (MainForm)this.ParentForm; double qbCost1 = 0; string szList = ""; try { if (this.extendedDataGridView1.SelectedCells.Count > 0) { string jobID = this.extendedDataGridView1.SelectedCells[0].Value.ToString(); string date = this.extendedDataGridView1.SelectedCells[1].Value.ToString(); string szCustomerName = lastNameTextBox.Text + ", " + firstNameTextBox.Text; if (firstNameTextBox.Text == "") //for company name, no last name { szCustomerName = lastNameTextBox.Text; } //string szInvoiceQuickbookID = qBInvoiceIDTextBox.Text; if (this.extendedDataGridView1.SelectedRows.Count == 1) { if (date == "") { MessageBox.Show("Job has no date!"); return; } DateTime jobDate1 = DateTime.Parse(date); QBInvoiceDialog dlg = new QBInvoiceDialog(szCustomerName, Convert.ToInt32(jobID), QBitems1, QBitems2, QBitems3, QBitems4); if (dlg.ShowDialog() == DialogResult.OK) { if (dlg.bNewInvoiceCreated) pParent.SetStatusLabelTimer(5, "QB New Invoice Created...."); else pParent.SetStatusLabelTimer(5, "QB Invoice is Updated...."); //use itemid to record jobtype for reminders customerJobTableAdapter.UpdateQBItemCode(dlg.GetSelectedItem, Convert.ToInt32(jobID)); customerJobTableAdapter.UpdateQBDescriptionA(dlg.GetSelectedDescription, Convert.ToInt32(jobID)); } } else if (this.extendedDataGridView1.SelectedRows.Count > 1) { QBBatchInvoiceDialog dlg = new QBBatchInvoiceDialog(QBitems1); //fill selected list foreach (DataGridViewRow dataGridViewRow in extendedDataGridView1.SelectedRows) { if (dataGridViewRow.Cells[17].Value.ToString() != "") { decimal dCost = Convert.ToDecimal(dataGridViewRow.Cells[17].Value.ToString()); szList = "Invoice = " + dCost.ToString("C"); szList = szList + " -- " + dataGridViewRow.Cells[2].Value.ToString() + ", " + dataGridViewRow.Cells[3].Value.ToString(); szList = szList + " -- " + dataGridViewRow.Cells[6].Value.ToString(); //ADDRESS dlg.AddToListBox(szList); } } if (dlg.ShowDialog() == DialogResult.OK) { pParent.ProgressBarInit(0, this.extendedDataGridView1.SelectedRows.Count, 1); Cursor.Current = Cursors.WaitCursor; foreach (DataGridViewRow dataGridViewRow in extendedDataGridView1.SelectedRows) { jobID = dataGridViewRow.Cells[0].Value.ToString(); date = this.extendedDataGridView1.SelectedCells[1].Value.ToString(); if (date != "") { DateTime jobDate1 = DateTime.Parse(date); szCustomerName = dataGridViewRow.Cells[2].Value.ToString() + ", " + dataGridViewRow.Cells[3].Value.ToString(); //for company name with no first name if (dataGridViewRow.Cells[3].Value.ToString().Length == 0) { szCustomerName = dataGridViewRow.Cells[2].Value.ToString(); } string QBInvoiceID = dataGridViewRow.Cells[13].Value.ToString(); pParent.SetStatusLabel("QB Invoicing - " + szCustomerName); pParent.ProgressBarUpdate(); string cost = dataGridViewRow.Cells[17].Value.ToString(); if ((date != null) && (dataGridViewRow.Cells[17].Value.ToString() != "")) { qbCost1 = 0; qbCost1 = Convert.ToDouble(dataGridViewRow.Cells[17].Value.ToString()); var invoiceItem1 = new InvoiceItem() { Amount = qbCost1, QuickBooksID = dataGridViewRow.Cells[13].Value.ToString(), Date = DateTime.Parse(dataGridViewRow.Cells[1].Value.ToString()), Description = dlg.DescText1, Quantity = 1, }; //append address to description if (dlg.AppendAddress) { invoiceItem1.Description = dlg.DescText1 + " - (" + dataGridViewRow.Cells[6].Value.ToString() + ")"; } string ItemA_ID = dataGridViewRow.Cells[14].Value.ToString(); QBInvoiceID = dlg.AddModQBInvoice(szCustomerName, QBInvoiceID, invoiceItem1, jobDate1, ItemA_ID); if (QBInvoiceID != "") { customerJobTableAdapter.UpdateQBData((decimal) qbCost1, 0, 0, QBInvoiceID, invoiceItem1.QuickBooksID, null, null, Convert.ToInt32(jobID)); //use itemid to record jobtype for reminders customerJobTableAdapter.UpdateQBItemCode(dlg.GetSelectedItem, Convert.ToInt32(jobID)); customerJobTableAdapter.UpdateQBDescriptionA(dlg.DescText1, Convert.ToInt32(jobID)); } System.Threading.Thread.Sleep(1000); } } } Cursor.Current = Cursors.Default; } } RefreshView(); JobStatusFilterComboBox.SelectedIndex = saveStatus; JobTypeFilterComboBox_SelectedIndexChanged(null, null); pParent.SetStatusLabelTimer(5, "QB Updated...."); } } catch (Exception exe) { MMSUtility.LogFile(exe.Message, e.ToString(), ((Control)sender).Name, exe.LineNumber(), "ScheduleView"); } }