/// <summary> /// Record payment on invoice in QBO /// </summary> /// <param name="context"></param> /// <param name="invoice"></param> /// <returns>payment</returns> public static Payment ReceivePayment(ServiceContext context, Invoice invoice) { Payment newPayment = new Payment { TxnDate = DateTime.Now.Date, TxnDateSpecified = true, CustomerRef = invoice.CustomerRef, PaymentType = PaymentTypeEnum.Cash }; LinkedTxn linkInvoice = new LinkedTxn { TxnId = invoice.Id, TxnType = TxnTypeEnum.Invoice.ToString() }; List <LinkedTxn> linkedTxnList = new List <LinkedTxn> { linkInvoice }; Line line = new Line { Amount = invoice.TotalAmt, AmountSpecified = true }; line.LinkedTxn = linkedTxnList.ToArray(); List <Line> lines = new List <Line>(); lines.Add(line); newPayment.Line = lines.ToArray(); newPayment.TotalAmt = invoice.TotalAmt; newPayment.TotalAmtSpecified = true; Payment apiResponse = QBOHelper.Helper.AddToQBO(context, newPayment); return(apiResponse); }
internal static BillPayment CreateBillPayment_CheckPayment() { BillPayment billPayment = new BillPayment(); Vendor vendor = new Vendor(); //vendor.DisplayName = "00124a1f-ff2a-41ad-b3f9-8"; vendor.Id = "35"; BillPaymentCheck billPaymentCheck = new BillPaymentCheck(); billPaymentCheck.BankAccountRef = new ReferenceType() { //name = "Bank (1091836770)", Value = "136" }; CheckPayment checkPayment = new CheckPayment(); checkPayment.AcctNum = "AcctNum" + GetGuid().Substring(0, 5); checkPayment.BankName = "BankName" + GetGuid().Substring(0, 5); checkPayment.CheckNum = "CheckNum" + GetGuid().Substring(0, 5); checkPayment.NameOnAcct = "Name" + GetGuid().Substring(0, 5); checkPayment.Status = "Status" + GetGuid().Substring(0, 5); billPaymentCheck.CheckDetail = checkPayment; PhysicalAddress payeeAddr = new PhysicalAddress(); payeeAddr.Line1 = "Line 1"; payeeAddr.Line2 = "Line 2"; payeeAddr.City = "Mountain View"; payeeAddr.CountrySubDivisionCode = "CA"; payeeAddr.PostalCode = "94043"; billPaymentCheck.PayeeAddr = payeeAddr; billPaymentCheck.PrintStatus = PrintStatusEnum.NeedToPrint; billPaymentCheck.PrintStatusSpecified = true; List <Line> lineList = new List <Line>(); Line line1 = new Line(); //line.LineNum = "LineNum"; //line.Description = "Description"; line1.Amount = 100; line1.AmountSpecified = true; List <LinkedTxn> LinkedTxnList1 = new List <LinkedTxn>(); LinkedTxn linkedTxn1 = new LinkedTxn(); linkedTxn1.TxnId = "547"; linkedTxn1.TxnType = TxnTypeEnum.Bill.ToString(); LinkedTxnList1.Add(linkedTxn1); line1.LinkedTxn = LinkedTxnList1.ToArray(); lineList.Add(line1); billPayment.AnyIntuitObject = billPaymentCheck; billPayment.PayType = BillPaymentTypeEnum.Check; billPayment.PayTypeSpecified = true; billPayment.Line = lineList.ToArray(); billPayment.TotalAmt = 100; billPayment.TotalAmtSpecified = true; billPayment.VendorRef = new ReferenceType() { //name = vendor.DisplayName, Value = vendor.Id }; return(billPayment); }
/// <summary> /// This workflow covers create customer, item, estimate and invoice and updating estimate /// </summary> /// <returns></returns> public async Task <ActionResult> JobsWorkflow() { //Make QBO api calls using .Net SDK if (Session["realmId"] != null) { string realmId = Session["realmId"].ToString(); try { //Initialize OAuth2RequestValidator and ServiceContext ServiceContext serviceContext = base.IntializeContext(realmId); DataService dataService = new DataService(serviceContext); #region add customer // Add Customer Customer customer = new Customer(); String customerName = "Brad Smith"; //Check if the customer already exists QueryService <Customer> customerQueryService = new QueryService <Customer>(serviceContext); List <Customer> existingCustomers = customerQueryService.ExecuteIdsQuery($"Select * From Customer WHERE DisplayName='{customerName}'").ToList <Customer>(); if (existingCustomers.Count > 0) { //Use the existing customer if already in QuickBooks customer = existingCustomers[0]; } else { //Create a new customer in QuickBooks customer.DisplayName = customerName; customer = dataService.Add(customer); } #endregion #region add item // Add Item Item item = new Item(); String itemName = "Hair Bronzing"; //Check if the item already exists QueryService <Item> itemQueryService = new QueryService <Item>(serviceContext); List <Item> existingItems = itemQueryService.ExecuteIdsQuery($"Select * From Item WHERE Name='{itemName}'").ToList <Item>(); if (existingItems.Count > 0) { //Use the existing item if it already exists item = existingItems[0]; } else { //Find an account for the new item Account account = new Account(); String servicesAccountName = "Services Income"; //Check if the item's account already exists QueryService <Account> serviceAccountQueryService = new QueryService <Account>(serviceContext); List <Account> existingServiceAccounts = serviceAccountQueryService.ExecuteIdsQuery($"Select * From Account Where Name='{servicesAccountName}'").ToList <Account>(); if (existingServiceAccounts.Count > 0) { //Use the existing account if it is already in QuickBooks account = existingServiceAccounts[0]; } else { //Create a new services account in QuickBooks account.Name = servicesAccountName; account.AccountType = AccountTypeEnum.Income; account.AccountTypeSpecified = true; account = dataService.Add <Account>(account); } //Create a new item in QuickBooks item.Name = itemName; item.Type = ItemTypeEnum.Service; item.TypeSpecified = true; //Set the item's income account to the service account item.IncomeAccountRef = new ReferenceType() { type = objectNameEnumType.Account.ToString(), Value = account.Id }; item = dataService.Add(item); } #endregion #region create estimate // Create Estimate Estimate estimate = new Estimate(); estimate.CustomerRef = new ReferenceType() { type = objectNameEnumType.Customer.ToString(), Value = customer.Id }; List <Line> lineList = new List <Line>(); Line line = new Line(); line.Description = "Wake up with perfect hair!"; line.Amount = new Decimal(9000.00); line.AmountSpecified = true; line.DetailType = LineDetailTypeEnum.SalesItemLineDetail; line.DetailTypeSpecified = true; line.AnyIntuitObject = new SalesItemLineDetail() { ItemRef = new ReferenceType() { Value = item.Id } }; lineList.Add(line); estimate.Line = lineList.ToArray(); estimate = dataService.Add <Estimate>(estimate); #endregion #region update estimate amount // Update Amount in Estimate lineList = estimate.Line.ToList(); foreach (Line estimateLine in lineList) { if (estimateLine.DetailType == LineDetailTypeEnum.SalesItemLineDetail) { //Find the estimate line to update if (((SalesItemLineDetail)estimateLine.AnyIntuitObject).ItemRef.Value == item.Id) { estimateLine.Amount = 18000; estimateLine.AmountSpecified = true; } } } estimate = dataService.Update <Estimate>(estimate); #endregion #region convert/link estimate to invoice // Convert Estimate to Invoice Invoice invoice = new Invoice(); invoice.CustomerRef = estimate.CustomerRef; invoice.Line = estimate.Line; //Include a reference to the Estimate on the new Invoice LinkedTxn estimateLink = new LinkedTxn() { TxnType = "Estimate", TxnId = estimate.Id }; invoice.LinkedTxn = new LinkedTxn[] { estimateLink }; invoice = dataService.Add <Invoice>(invoice); // Update Invoice to add $5 Discount Account discountAccount = new Account(); String discountAccountName = "Discounts given"; //Check if the discount account already exists QueryService <Account> discountAccountQueryService = new QueryService <Account>(serviceContext); List <Account> existingDiscountAccounts = discountAccountQueryService.ExecuteIdsQuery($"Select * From Account Where Name='{discountAccountName}'").ToList <Account>(); if (existingDiscountAccounts.Count > 0) { //Use the existing discount account if it is already in QuickBooks discountAccount = existingDiscountAccounts[0]; } else { //Create a new discount account in QuickBooks discountAccount.Name = discountAccountName; discountAccount.AccountSubType = AccountSubTypeEnum.DiscountsRefundsGiven.ToString(); discountAccount.SubAccountSpecified = true; discountAccount = dataService.Add <Account>(discountAccount); } lineList = invoice.Line.ToList(); line = new Line(); line.Amount = new Decimal(5.00); line.AmountSpecified = true; line.DetailType = LineDetailTypeEnum.DiscountLineDetail; line.DetailTypeSpecified = true; //Use the discount account on a new invoice line line.AnyIntuitObject = new DiscountLineDetail() { PercentBased = false, PercentBasedSpecified = true, DiscountAccountRef = new ReferenceType() { Value = discountAccount.Id } }; //Add the new discount line to the invoice lineList.Add(line); invoice.Line = lineList.ToArray(); //Set DepositSpecified explicity to false, as it may not be supported by all QBO SKUs invoice.DepositSpecified = false; invoice = dataService.Update <Invoice>(invoice); #endregion return(View("Index", (object)("QBO API calls Success!"))); } catch (Exception ex) { return(View("Index", (object)"QBO API calls Failed!")); } } else { return(View("Index", (object)"QBO API call Failed!")); } }
public JsonResult MakePaymentData(PaymentModel objPaymentModel, PaymentModel ObjData) { eTracLoginModel ObjLoginModel = null; long Vendor = 0; var result = ""; var resultPayment = new BillPayment(); if (Session["eTrac"] != null) { ObjLoginModel = (eTracLoginModel)(Session["eTrac"]); } try { if (objPaymentModel != null && ObjData != null) { objPaymentModel.UserId = ObjLoginModel.UserId; string realmId = CallbackController.RealMId.ToString(); // Session["realmId"].ToString(); try { if (realmId != null) { string AccessToken = CallbackController.AccessToken.ToString(); //Session["access_token"].ToString(); var principal = User as ClaimsPrincipal; OAuth2RequestValidator oauthValidator = new OAuth2RequestValidator(AccessToken); // Create a ServiceContext with Auth tokens and realmId ServiceContext serviceContext = new ServiceContext(realmId, IntuitServicesType.QBO, oauthValidator); serviceContext.IppConfiguration.MinorVersion.Qbo = "23"; DataService commonServiceQBO = new DataService(serviceContext); // Create a QuickBooks QueryService using ServiceContext QueryService <Vendor> querySvc = new QueryService <Vendor>(serviceContext); List <Vendor> vendorList = querySvc.ExecuteIdsQuery("SELECT * FROM Vendor MaxResults 1000").ToList(); QueryService <Account> querySvcAccount = new QueryService <Account>(serviceContext); List <Account> accountData = querySvcAccount.ExecuteIdsQuery("SELECT * FROM Account MaxResults 1000").ToList(); var VendorDetails = _IVendorManagement.GetCompanyQuickBookId(Convert.ToInt64(ObjData.VendorId)); //var getAccountDetails = _IVendorManagement.GetAccountDetailsByVendorId(Convert.ToInt64(objPaymentModel.OpeartorCAD_Id)); // (Convert.ToInt64(ObjData.VendorId)); var getAccountDetails = _IVendorManagement.GetAccountDetailsByVendorId(Convert.ToInt64(objPaymentModel.CompanyAccountId)); // (Convert.ToInt64(ObjData.VendorId)); var getBill = _IBillDataManager.GetBillQBKId(Convert.ToInt64(ObjData.BillNo)); QueryService <Bill> querySvcBill = new QueryService <Bill>(serviceContext); List <Bill> billData = querySvcBill.ExecuteIdsQuery("SELECT * FROM Bill MaxResults 1000").ToList(); var bill = billData.Where(x => x.Id == getBill.ToString()).FirstOrDefault(); // var vendorData = vendorList.Where(x => x.Id == "64").FirstOrDefault(); var payment = new BillPayment(); //Vendor Reference var reference = new ReferenceType(); var accountRef = new AccountBasedExpenseLineDetail(); var billPaymentCheck = new BillPaymentCheck(); var billPaymentCredit = new BillPaymentCreditCard(); var line = new Line(); var lineList = new List <Line>(); if (VendorDetails > 0) { var vendorData = vendorList.Where(x => x.Id == VendorDetails.ToString()).FirstOrDefault(); //Vendor Reference payment.VendorRef = new ReferenceType() { name = vendorData.DisplayName, Value = vendorData.Id }; } line.LineNum = "1"; var any = new IntuitAnyType(); if (getAccountDetails != null) { var ayintuit = new IntuitAnyType(); var accountsDetails = accountData.Where(x => x.Id == getAccountDetails.QuickbookAcountId.ToString()).FirstOrDefault();//getAccountDetails.QuickbookAcountId.ToString() if (ObjData.PaymentMode == "Wired") { payment.PayType = BillPaymentTypeEnum.CreditCard; var CCD = new CreditCardPayment(); billPaymentCredit.CCAccountRef = new ReferenceType() { name = accountsDetails.Name, Value = accountsDetails.Id, }; payment.AnyIntuitObject = billPaymentCredit; } else if (ObjData.PaymentMode == "Card") { payment.PayType = BillPaymentTypeEnum.CreditCard; var CCD = new CreditCardPayment(); billPaymentCredit.CCAccountRef = new ReferenceType() { name = accountsDetails.Name, Value = accountsDetails.Id, }; payment.AnyIntuitObject = billPaymentCredit; } else if (ObjData.PaymentMode == "Check") { var checking = new CheckPayment(); payment.PayType = BillPaymentTypeEnum.Check; billPaymentCheck.BankAccountRef = new ReferenceType() { name = accountsDetails.Name, Value = accountsDetails.Id }; billPaymentCheck.PrintStatus = PrintStatusEnum.NeedToPrint; payment.AnyIntuitObject = billPaymentCheck; } } payment.APAccountRef = new ReferenceType() { name = "Accounts Payable (A/P)", Value = "33" }; QueryService <Department> querySvcDept = new QueryService <Department>(serviceContext); var LocationName = _IBillDataManager.GetLocationDataByLocId(Convert.ToInt64(ObjData.LocationId)); payment.DepartmentRef = new ReferenceType() { name = LocationName.LocationName, Value = LocationName.QBK_Id.ToString() }; line.Amount = Convert.ToDecimal(ObjData.BillAmount); line.AmountSpecified = true; var linkedlist = new List <LinkedTxn>(); var linked = new LinkedTxn(); linked.TxnId = bill.Id; linked.TxnType = "Bill"; linkedlist.Add(linked); line.LinkedTxn = linkedlist.ToArray(); line.DetailType = LineDetailTypeEnum.PaymentLineDetail; lineList.Add(line); payment.Line = lineList.ToArray(); //payment.PayType = BillPaymentTypeEnum.CreditCard; payment.PayTypeSpecified = true; payment.TotalAmt = Convert.ToDecimal(ObjData.BillAmount); payment.TotalAmtSpecified = true; var metaData = new ModificationMetaData(); metaData.CreateTime = Convert.ToDateTime(ObjData.BillDate); payment.MetaData = metaData; payment.PayTypeSpecified = true; resultPayment = commonServiceQBO.Add(payment) as BillPayment; //To close PO after Payment. Update Payment in Quickbook. QueryService <PurchaseOrder> querySvcPO = new QueryService <PurchaseOrder>(serviceContext); List <PurchaseOrder> POList = querySvcPO.ExecuteIdsQuery("SELECT * FROM PurchaseOrder MaxResults 1000").ToList(); if (ObjData.BillType == "PO") { var getPOQData = _IPaymentManager.GetPODetails(objPaymentModel, ObjData); if (getPOQData.QuickBookPOId > 0) { var data = POList.Where(x => x.Id == getPOQData.QuickBookPOId.ToString()).FirstOrDefault(); data.POStatus = PurchaseOrderStatusEnum.Closed; var update = commonServiceQBO.Update(data) as PurchaseOrder; } } } else { ViewBag.Message = CommonMessage.FailureMessage(); result = CommonMessage.FailureMessage(); ViewBag.AlertMessageClass = ObjAlertMessageClass.Danger; return(Json(result, JsonRequestBehavior.AllowGet)); } } catch (Exception ex) { ViewBag.Message = ex.Message; ViewBag.AlertMessageClass = ObjAlertMessageClass.Danger; } result = _IPaymentManager.MakePayment(objPaymentModel, ObjData); if (result != null) { return(Json(result, JsonRequestBehavior.AllowGet)); } else { ViewBag.Message = CommonMessage.FailureMessage(); ViewBag.AlertMessageClass = ObjAlertMessageClass.Danger; return(Json(result, JsonRequestBehavior.AllowGet)); } } else { ViewBag.Message = CommonMessage.FailureMessage(); ViewBag.AlertMessageClass = ObjAlertMessageClass.Danger; } } catch (Exception ex) { return(Json(ex.Message, JsonRequestBehavior.AllowGet)); } //return null; return(Json(result, JsonRequestBehavior.AllowGet)); }
/* * Create a BillPayment. * A BillPayment object represents the payment transaction for a bill that the business owner receives from a vendor for goods or services purchased from the vendor. * QuickBooks Online supports bill payments through a credit card or a checking account. */ private static BillPayment CreateBillPaymentCreditCard(ServiceContext serviceContext, Vendor vendor, Bill bill) { DataService dataService = new DataService(serviceContext); QueryService <Account> accountQuerySvc = new QueryService <Account>(serviceContext); //Create a bill payment and associate it to the vendor. Add VendorCredit BillPayment billPayment = new BillPayment(); billPayment.PayType = BillPaymentTypeEnum.Check; billPayment.PayTypeSpecified = true; billPayment.TotalAmt = 300; billPayment.TotalAmtSpecified = true; billPayment.TxnDate = DateTime.UtcNow.Date; billPayment.TxnDateSpecified = true; billPayment.PrivateNote = "PrivateNote"; billPayment.VendorRef = new ReferenceType() { name = vendor.DisplayName, type = "Vendor", Value = vendor.Id }; #region create bank account //Create a Bank Account of type Credit Card. The bill payment will be via this account. If not present create a credit card account Account bankAccount = accountQuerySvc.ExecuteIdsQuery("SELECT * FROM Account WHERE AccountType='Credit Card' AND Classification='Liability'").FirstOrDefault(); if (bankAccount == null) { Account accountb = new Account(); String guid = Guid.NewGuid().ToString("N"); accountb.Name = "Name_" + guid; // accountb.FullyQualifiedName = bankAccount.Name; accountb.Classification = AccountClassificationEnum.Liability; accountb.ClassificationSpecified = true; accountb.AccountType = AccountTypeEnum.CreditCard; accountb.AccountTypeSpecified = true; accountb.CurrencyRef = new ReferenceType() { name = "United States Dollar", Value = "USD" }; bankAccount = dataService.Add <Account>(accountb); } #endregion BillPaymentCreditCard billPaymentCreditCard = new BillPaymentCreditCard(); billPaymentCreditCard.CCAccountRef = new ReferenceType() { name = bankAccount.Name, Value = bankAccount.Id }; CreditCardPayment creditCardPayment = new CreditCardPayment(); creditCardPayment.CreditChargeInfo = new CreditChargeInfo() { Amount = new Decimal(10.00), AmountSpecified = true, Number = "124124124", NameOnAcct = bankAccount.Name, CcExpiryMonth = 10, CcExpiryMonthSpecified = true, CcExpiryYear = 2015, CcExpiryYearSpecified = true, BillAddrStreet = "BillAddrStreetba7cca47", PostalCode = "560045", CommercialCardCode = "CardCodeba7cca47", CCTxnMode = CCTxnModeEnum.CardPresent, CCTxnType = CCTxnTypeEnum.Charge }; billPaymentCreditCard.CCDetail = creditCardPayment; billPayment.AnyIntuitObject = billPaymentCreditCard; //Create a line and it to the BillPayment List <Line> lineList = new List <Line>(); Line line1 = new Line(); line1.Amount = bill.TotalAmt; line1.AmountSpecified = true; List <LinkedTxn> LinkedTxnList1 = new List <LinkedTxn>(); LinkedTxn linkedTxn1 = new LinkedTxn(); linkedTxn1.TxnId = bill.Id; linkedTxn1.TxnType = TxnTypeEnum.Bill.ToString(); LinkedTxnList1.Add(linkedTxn1); line1.LinkedTxn = LinkedTxnList1.ToArray(); lineList.Add(line1); Line line = new Line(); line.Amount = 300; line.AmountSpecified = true; List <LinkedTxn> LinkedTxnList = new List <LinkedTxn>(); LinkedTxn linkedTxn = new LinkedTxn(); linkedTxn.TxnId = bill.Id; linkedTxn.TxnType = TxnTypeEnum.VendorCredit.ToString(); LinkedTxnList.Add(linkedTxn); line.LinkedTxn = LinkedTxnList.ToArray(); lineList.Add(line); billPayment.Line = lineList.ToArray(); BillPayment billPaymentAdded = dataService.Add <BillPayment>(billPayment); return(billPaymentAdded); }