private void Connect(ICompany company) { if (company != null && company.Connected) { return; } if (company == null) { throw new Exception("SapDiApi company is null - cant connect"); } var connectionValues = new Dictionary <string, string>(); try { _connectionString.Split(";").ToList() .ForEach(str => { var s = str.Split("="); if (s.Length == 2) { connectionValues.Add(s[0].ToUpper(), s[1]); } }); company.CompanyDB = connectionValues["COMPANYDB"]; company.Server = connectionValues["SERVER"]; ; company.LicenseServer = connectionValues["LICENSESERVER"]; company.SLDServer = connectionValues["SLDSERVER"]; company.DbUserName = connectionValues["DBUSERNAME"]; company.DbPassword = connectionValues["DBPASSWORD"]; company.UserName = connectionValues["USERNAME"]; company.Password = connectionValues["PASSWORD"]; company.DbServerType = connectionValues["DBSERVERTYPE"].ToUpper() switch { "MSSQL2012" => BoDataServerTypes.dst_MSSQL2012, "MSSQL2014" => BoDataServerTypes.dst_MSSQL2014, "MSSQL2016" => BoDataServerTypes.dst_MSSQL2016, "MSSQL" => BoDataServerTypes.dst_MSSQL, _ => company.DbServerType }; company.UseTrusted = connectionValues["USETRUSTED"] == "TRUE"; } catch { //don't expose the connection string through an exception throw new Exception("connection string error!"); } var ret = company.Connect(); var errMsg = company.GetLastErrorDescription(); var errNo = company.GetLastErrorCode(); if (errNo != 0) { var msg = $"DI-API Connect error: ErrorCode {errNo} = {errMsg}"; throw new Exception(msg); } }
public static void ThrowExceptionForLastError(this ICompany company, string message) { var messageException = $"{message}:\n Código do Erro [{company.GetLastErrorCode()}] - [{company.GetLastErrorDescription()}]"; throw new Exception(messageException); }
public String MakePayment(int userId, int paymentId) { var user = db.DeviceUser.Where(w => w.DeviceUserId == userId).ToList().FirstOrDefault(); String connection = user.Shop == null ? "" : user.Shop.ConnectionString; using (var db = new ApplicationDbContext(connection)) { String lastMessage = ""; String key = ""; var p = db.Payments .Include(i => i.Transfer) .Include(i => i.Invoices) .Where(w => w.PaymentId == paymentId) .ToList() .FirstOrDefault(); if (p != null) { if (String.IsNullOrEmpty(p.DocEntry)) { if (_connection.Connect(connection) == 0) { company = _connection.GetCompany(); Double totalAmount = p.TotalAmount; Double amountLeft = totalAmount; Payments payment = company.GetBusinessObject(BoObjectTypes.oPaymentsDrafts); payment.DocObjectCode = BoPaymentsObjectType.bopot_IncomingPayments; payment.DocType = BoRcptTypes.rCustomer; payment.CardCode = p.Client.CardCode; payment.DocDate = DateTime.Now; payment.VatDate = DateTime.Now; payment.DueDate = DateTime.Now; payment.Remarks = p.Comment; payment.CounterReference = p.ReferenceNumber != null && p.ReferenceNumber.Count() > 20 ? p.ReferenceNumber.Substring(0, 20) : p.ReferenceNumber; if (payment.UserFields.Fields.Count > 0) { payment.UserFields.Fields.Item("U_Cobrador").Value = p.DeviceUser.CollectId; } //if (p.Cash != null) //{ // payment.CashAccount = p.Cash.GeneralAccount; // payment.CashSum = p.Cash.Amount; //} if (p.Transfer != null) { payment.TransferAccount = p.Transfer.GeneralAccount; payment.TransferDate = p.Transfer.Date; payment.TransferReference = p.Transfer.ReferenceNumber; payment.TransferSum = p.Transfer.Amount; payment.DueDate = p.Transfer.Date; } //if (p.Checks != null) //{ // foreach (Check check in p.Checks) // { // payment.Checks.CheckAccount = check.GeneralAccount; // payment.Checks.CheckSum = check.Amount; // payment.Checks.DueDate = check.DueDate; // payment.Checks.BankCode = check.Bank.FormatCode; // payment.Checks.Add(); // } //} if (p.Invoices != null) { foreach (InvoiceItem invoice in p.Invoices) { if (amountLeft > 0) { payment.Invoices.DocEntry = invoice.DocEntry; payment.Invoices.InvoiceType = BoRcptInvTypes.it_Invoice; //Si aun hay cash entonces pago la factura completa sino la pago incompleta payment.Invoices.SumApplied = invoice.PayedAmount <= amountLeft ? invoice.PayedAmount : amountLeft; amountLeft -= payment.Invoices.SumApplied; payment.Invoices.Add(); } } int errorCode = payment.Add(); if (errorCode != 0) { lastMessage = "Error Code: " + company.GetLastErrorCode().ToString() + " - " + company.GetLastErrorDescription(); } else { key = company.GetNewObjectKey(); } } payment = null; company.Disconnect(); } else { lastMessage = "Error Msg: " + _connection.GetErrorMessage().ToString(); } var pay = db.Payments .Where(w => w.PaymentId == paymentId) .ToList() .FirstOrDefault(); if (pay != null) { pay.DocEntry = key; pay.LastErrorMessage = lastMessage; pay.Status = String.IsNullOrEmpty(key) ? PaymentStatus.Error : PaymentStatus.CreadoEnSAP; db.Entry(pay).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } } } return(lastMessage); } }
public String MakePayment(int paymentId, int userId) { String lastMessage = ""; var user = db.DeviceUser.Where(w => w.DeviceUserId == userId).ToList().FirstOrDefault(); String connection = user.Shop == null ? "" : user.Shop.ConnectionString; var p = db.Payments .Include(i => i.Cash).Include(i => i.Transfer).Include(i => i.Checks).Include(i => i.Invoices) .ToList() .Where(w => w.PaymentId == paymentId) .FirstOrDefault(); using (var db = new ApplicationDbContext(connection)) { if (p != null) { if (_connection.Connect(connection) == 0) { company = _connection.GetCompany(); Double totalAmount = p.TotalAmount; Double amountLeft = totalAmount; Payments payment = company.GetBusinessObject(BoObjectTypes.oIncomingPayments); payment.DocType = BoRcptTypes.rCustomer; //payment.DocTypte = BoRcptTypes.rCustomer; payment.CardCode = p.Client.CardCode; payment.DocDate = DateTime.Now; payment.VatDate = DateTime.Now; payment.DueDate = DateTime.Now; if (p.Cash != null) { payment.CashAccount = p.Cash.GeneralAccount; payment.CashSum = p.Cash.Amount; } if (p.Transfer != null) { payment.TransferAccount = p.Transfer.GeneralAccount; payment.TransferDate = p.Transfer.Date; payment.TransferReference = p.Transfer.ReferenceNumber; payment.TransferSum = p.Transfer.Amount; } if (p.Checks != null) { foreach (Check check in p.Checks) { payment.Checks.CheckAccount = check.GeneralAccount; payment.Checks.CheckSum = check.Amount; payment.Checks.DueDate = check.DueDate; payment.Checks.BankCode = check.Bank.FormatCode; payment.Checks.Add(); } } if (p.Invoices != null) { var invoices = p.Invoices .Where(w => w.PaymentId == p.PaymentId) .ToList(); foreach (InvoiceItem invoice in invoices) { if (amountLeft > 0) { payment.Invoices.DocEntry = invoice.DocEntry; payment.Invoices.InvoiceType = BoRcptInvTypes.it_Invoice; //Si aun hay cash entonces pago la factura completa sino la pago incompleta payment.Invoices.SumApplied = invoice.TotalAmount <= amountLeft ? invoice.TotalAmount : amountLeft; amountLeft -= payment.Invoices.SumApplied; payment.Invoices.Add(); } } } int errorCode = payment.Add(); if (errorCode != 0) { lastMessage = "Error Code: " + company.GetLastErrorCode().ToString() + " - " + company.GetLastErrorDescription(); } else { String key = company.GetNewObjectKey(); p.DocEntry = key; db.Entry(p).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } company.Disconnect(); } else { lastMessage = "Error Msg: " + _connection.GetErrorMessage().ToString(); } p.LastErrorMessage = lastMessage; db.Entry(p).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } return(lastMessage); } }