//------------------------------------------------------------------------------------------- protected void LedgerItemAdd_Click(object sender, EventArgs e) { //List.ShowFooter = true; //List.EditItemIndex = List.Items.Count; LedgerType ledgerType = (LedgerType)Enum.Parse(typeof(LedgerType), Request["ledgertype"], true); using (WeavverEntityContainer data = new WeavverEntityContainer()) { Accounting_LedgerItems item = new Accounting_LedgerItems(); item.Id = Guid.NewGuid(); item.OrganizationId = BasePage.SelectedOrganization.Id; item.LedgerType = ledgerType.ToString(); item.Code = ((CodeType)Enum.Parse(typeof(CodeType), CodeTypeList.SelectedValue, true)).ToString(); item.PostAt = DateTime.Parse(LedgerItemPostAt.Text).ToUniversalTime(); item.AccountId = new Guid(Request["AccountId"]); item.Memo = LedgerItemName.Text; item.Amount = Decimal.Parse(LedgerItemAmount.Text); data.Accounting_LedgerItems.AddObject(item); try { data.SaveChanges(); ErrorMsg.Text = ""; OnDataSaved(this, EventArgs.Empty); } catch (IValidatorException ex) { ErrorMsg.Text = ex.Message; } } }
//------------------------------------------------------------------------------------------- protected void TransferFunds_Click(object sender, EventArgs e) { using (WeavverEntityContainer data = new WeavverEntityContainer()) { DateTime postAtDate = DateTime.UtcNow; if (DateTime.TryParse(PostAt.Text, out postAtDate)) postAtDate = postAtDate.ToUniversalTime(); Accounting_Accounts accountFrom = (from accounts in data.Accounting_Accounts where accounts.Id == new Guid(FromAccount.SelectedValue) select accounts).FirstOrDefault(); Accounting_Accounts accountTo = (from toAccounts in data.Accounting_Accounts where toAccounts.Id == new Guid(ToAccount.SelectedValue) select toAccounts).FirstOrDefault(); Guid transactionId = Guid.NewGuid(); Accounting_LedgerItems debitAccount1 = new Accounting_LedgerItems(); debitAccount1.OrganizationId = BasePage.SelectedOrganization.Id; debitAccount1.TransactionId = transactionId; debitAccount1.PostAt = postAtDate; debitAccount1.AccountId = accountFrom.Id; debitAccount1.LedgerType = accountFrom.LedgerType; debitAccount1.Code = CodeType.Withdrawal.ToString(); debitAccount1.Memo = String.Format("Transfer to {0}", accountTo.Name); debitAccount1.Amount = Decimal.Parse(Amount.Text) * -1.0m; data.Accounting_LedgerItems.AddObject(debitAccount1); Accounting_LedgerItems creditAccount2 = new Accounting_LedgerItems(); creditAccount2.OrganizationId = BasePage.SelectedOrganization.Id; creditAccount2.TransactionId = transactionId; creditAccount2.PostAt = postAtDate; creditAccount2.AccountId = new Guid(ToAccount.SelectedValue); creditAccount2.LedgerType = accountTo.LedgerType; creditAccount2.Code = CodeType.Deposit.ToString(); creditAccount2.Memo = String.Format("Transfer from {0}", accountFrom.Name); creditAccount2.Amount = Decimal.Parse(Amount.Text); data.Accounting_LedgerItems.AddObject(creditAccount2); data.SaveChanges(); // Response.Redirect("~/Accounting_LedgerItems/List.aspx?TransactionId=" + transactionId.ToString()); } OnDataSaved(this, EventArgs.Empty); }
//------------------------------------------------------------------------------------------- protected void MakePayment_Click(object sender, EventArgs e) { using (WeavverEntityContainer data = new WeavverEntityContainer()) { DateTime postAtDate = DateTime.UtcNow; if (DateTime.TryParse(PostAt.Text, out postAtDate)) postAtDate = postAtDate.ToUniversalTime(); Logistics_Organizations accountFrom = (from accounts in data.Logistics_Organizations where accounts.Id == new Guid(FromAccount.SelectedValue) select accounts).FirstOrDefault(); Accounting_Accounts accountTo = (from toAccounts in data.Accounting_Accounts where toAccounts.Id == new Guid(ToAccount.SelectedValue) select toAccounts).FirstOrDefault(); Guid transactionId = Guid.NewGuid(); Accounting_LedgerItems creditFinancialAccount = new Accounting_LedgerItems(); creditFinancialAccount.OrganizationId = SelectedOrganization.Id; creditFinancialAccount.TransactionId = transactionId; creditFinancialAccount.PostAt = postAtDate; creditFinancialAccount.AccountId = accountFrom.Id; creditFinancialAccount.LedgerType = LedgerType.Receivable.ToString(); creditFinancialAccount.Code = CodeType.Deposit.ToString(); creditFinancialAccount.Memo = String.Format("Check {0} to {1}", CheckNum.Text, accountTo.Name); creditFinancialAccount.Amount = Decimal.Parse(Amount.Text); data.Accounting_LedgerItems.AddObject(creditFinancialAccount); Accounting_LedgerItems creditReceivableAccount = new Accounting_LedgerItems(); creditReceivableAccount.OrganizationId = SelectedOrganization.Id; creditReceivableAccount.TransactionId = transactionId; creditReceivableAccount.PostAt = postAtDate; creditReceivableAccount.LedgerType = accountTo.LedgerType; creditReceivableAccount.AccountId = new Guid(ToAccount.SelectedValue); creditReceivableAccount.Code = CodeType.Payment.ToString(); creditReceivableAccount.Memo = String.Format("Check {0} from {1}", CheckNum.Text, accountFrom.Name); creditReceivableAccount.Amount = Decimal.Parse(Amount.Text); data.Accounting_LedgerItems.AddObject(creditReceivableAccount); data.SaveChanges(); Response.Redirect("~/Accounting_LedgerItems/List.aspx?TransactionId=" + transactionId.ToString()); } }
//------------------------------------------------------------------------------------------- public IGatewayResponse Bill(WeavverEntityContainer data, Sales_Orders order, Logistics_Addresses primaryAddress, Logistics_Addresses billingAddress) { string memo = "WEB PURCHASE"; // Add the credit to the ledger. Accounting_LedgerItems item = new Accounting_LedgerItems(); item.Id = Guid.NewGuid(); item.OrganizationId = OrganizationId; if (order.Orderee.HasValue) item.AccountId = order.Orderee.Value; else item.AccountId = order.Id; item.LedgerType = LedgerType.Receivable.ToString(); item.TransactionId = order.Id; item.PostAt = DateTime.UtcNow; item.Code = CodeType.Payment.ToString(); item.Memo = "Payment from Card " + Number.Substring(Number.Length - 4); item.Amount = Math.Abs(order.Total.Value); // order.BillingContactEmail // Submit to Authorize.Net var request = new AuthorizationRequest(Number, ExpirationMonth.ToString("D2") + ExpirationYear.ToString("D2"), order.Total.Value, memo, true); request.AddCustomer("", order.PrimaryContactNameFirst, order.PrimaryContactNameLast, primaryAddress.Line1, primaryAddress.State, primaryAddress.ZipCode); request.AddMerchantValue("OrderId", order.Id.ToString()); request.AddMerchantValue("CreatedBy", order.CreatedBy.ToString()); request.AddMerchantValue("LedgerItemId", item.Id.ToString()); request.AddShipping("", order.BillingContactNameFirst, order.BillingContactNameLast, billingAddress.Line1, billingAddress.State, billingAddress.ZipCode); var gate = new Gateway(ConfigurationManager.AppSettings["authorize.net_loginid"], ConfigurationManager.AppSettings["authorize.net_transactionkey"], (ConfigurationManager.AppSettings["authorize.net_testmode"] == "true")); var response = gate.Send(request, memo); item.ExternalId = response.TransactionID; if (!response.Approved) { //item.Voided = true; //item.VoidedBy = Guid.Empty; item.Memo += "\r\nPayment failed: Code " + response.ResponseCode + ", " + response.Message; } data.Accounting_LedgerItems.Add(item); return response; }
//------------------------------------------------------------------------------------------- public List<Accounting_LedgerItems> ProjectLedgerItems(int periods) { List<Accounting_LedgerItems> Items = new List<Accounting_LedgerItems>(); for (int i = 0; i < periods; i++) { Guid newId = Guid.NewGuid(); Accounting_LedgerItems item = new Accounting_LedgerItems { Id = newId, OrganizationId = OrganizationId, ExternalId = newId.ToString(), PostAt = Position.AddMonths(i), AccountId = (AccountFrom == OrganizationId) ? AccountTo : AccountFrom, LedgerType = (AccountFrom == OrganizationId) ? LedgerType.Payable.ToString() : LedgerType.Receivable.ToString(), Code = (AccountFrom == OrganizationId) ? CodeType.Purchase.ToString() : CodeType.Sale.ToString(), Amount = Amount * -1.0m, CreatedBy = Id, UpdatedBy = Id }; string timespan = ""; if (BillingDirection == BillingDirectionEnum.PreBill.ToString()) { timespan = Position.ToLocalTime().AddMonths(i).ToString("MM/dd/yy") + " to " + Position.ToLocalTime().AddMonths(i + 1).ToString("MM/dd/yy"); } else if (BillingDirection == BillingDirectionEnum.PostBill.ToString()) { timespan = Position.ToLocalTime().AddMonths(i - 1).ToString("MM/dd/yy") + " to " + Position.ToLocalTime().AddMonths(i).ToString("MM/dd/yy"); } if (Memo.Contains("%timespan%")) item.Memo = Memo.Replace("%timespan%", timespan); else item.Memo = Memo + ", " + timespan; Items.Add(item); } return Items; }
//------------------------------------------------------------------------------------------- private void PlaceOrder() { if (Page.IsValid) { using (WeavverEntityContainer data = new WeavverEntityContainer()) { Communication_MessageTemplates orderPlacedTemplate = (from x in data.Communication_MessageTemplates where x.Id == new Guid("f6df7db7-5efb-475d-8e32-3dd7e293ce3b") select x).First(); string msgBody = orderPlacedTemplate.Body.Replace("{cart}", ShoppingCart.ToString()); string placedat = DateTime.Now.ToString("MM/dd/yy"); // (ShoppingCart.CreatedAt.HasValue) ? ShoppingCart.CreatedAt.Value.ToString("MM/dd/yy") : "unknown"; string orderee = PrimaryContact.FirstName.Text + " " + PrimaryContact.LastName.Text; string mPrimaryAddress = PrimaryContact.AddressLine1.Text + "\r\n" + PrimaryContact.AddressLine2.Text; string mBillingAddress = BillingContact.AddressLine1.Text + "\r\n" + BillingContact.AddressLine2.Text; msgBody = msgBody.Replace("{name}", orderee); msgBody = msgBody.Replace("{placed}", placedat); string primarycontact = "Name: " + orderee + "\r\n"; primarycontact += "Organization: " + PrimaryContact.Organization.Text + "\r\n"; primarycontact += "Address: " + mPrimaryAddress.Trim() + "\r\n"; primarycontact += "Zip Code: " + PrimaryContact.ZipCode.Text; msgBody = msgBody.Replace("{primary_contact}", primarycontact.Trim()); string billingcontact = "Name: " + BillingContact.FirstName.Text + " " + BillingContact.LastName.Text + "\r\n"; billingcontact += "Organization: " + BillingContact.Organization.Text + "\r\n"; billingcontact += "Address: " + mBillingAddress.Trim() + "\r\n"; billingcontact += "Zip Code: " + BillingContact.ZipCode.Text; msgBody = msgBody.Replace("{billing_contact}", billingcontact.Trim()); Logistics_Addresses primaryAddress = new Logistics_Addresses(); primaryAddress.OrganizationId = SelectedOrganization.Id; primaryAddress.Id = Guid.NewGuid(); primaryAddress.Name = "Primary Address"; primaryAddress.Line1 = PrimaryContact.AddressLine1.Text; primaryAddress.Line2 = PrimaryContact.AddressLine2.Text; primaryAddress.City = ""; primaryAddress.State = ""; primaryAddress.ZipCode = PrimaryContact.ZipCode.Text; data.Logistics_Addresses.AddObject(primaryAddress); // DatabaseHelper.Link(newOrder, primaryAddress); // newOrder.PrimaryAddress = primaryAddress; Logistics_Addresses billingAddress = new Logistics_Addresses(); billingAddress.OrganizationId = SelectedOrganization.Id; billingAddress.Id = Guid.NewGuid(); billingAddress.Name = "Billing Address"; billingAddress.Line1 = BillingContact.AddressLine1.Text; billingAddress.Line2 = BillingContact.AddressLine2.Text; billingAddress.City = ""; billingAddress.State = ""; billingAddress.ZipCode = BillingContact.ZipCode.Text; data.Logistics_Addresses.AddObject(billingAddress); // DatabaseHelper.Link(newOrder, billingAddress); // newOrder.BillingAddress = billingAddress; Sales_Orders newOrder = new Sales_Orders(); newOrder.Id = Guid.NewGuid(); newOrder.OrganizationId = SelectedOrganization.Id; if (LoggedInUser != null) newOrder.Orderee = LoggedInUser.OrganizationId; newOrder.Cart = msgBody; newOrder.Status = "Placed"; newOrder.Total = ShoppingCart.Total; newOrder.PrimaryContactEmail = PrimaryContact.EmailAddress.Text; newOrder.PrimaryContactNameFirst = PrimaryContact.FirstName.Text; newOrder.PrimaryContactNameLast = PrimaryContact.LastName.Text; newOrder.PrimaryContactOrganization = PrimaryContact.Organization.Text; newOrder.PrimaryContactPhoneNum = PrimaryContact.PhoneNumber.Text; newOrder.PrimaryContactPhoneExt = PrimaryContact.PhoneExtension.Text; newOrder.PrimaryContactAddress = primaryAddress.Id; newOrder.BillingContactEmail = BillingContact.EmailAddress.Text; newOrder.BillingContactNameFirst = BillingContact.FirstName.Text; newOrder.BillingContactNameLast = BillingContact.LastName.Text; newOrder.BillingContactOrganization = BillingContact.Organization.Text; newOrder.BillingContactPhoneNum = BillingContact.PhoneNumber.Text; newOrder.BillingContactPhoneExt = BillingContact.PhoneExtension.Text; newOrder.BillingContactAddress = billingAddress.Id; newOrder.Notes = SpecialInstructions.Text; data.Sales_Orders.AddObject(newOrder); Accounting_CreditCards card = new Accounting_CreditCards(); card.Id = Guid.NewGuid(); card.OrganizationId = SelectedOrganization.Id; card.Number = PaymentMethod1.CardNumber.Text; card.SecurityCode = PaymentMethod1.SecCode.Text; card.ExpirationMonth = Int32.Parse(PaymentMethod1.ExpMonth.SelectedValue); card.ExpirationYear = Int32.Parse(PaymentMethod1.ExpYear.SelectedValue); data.Accounting_CreditCards.AddObject(card); // DatabaseHelper.Link(newOrder, card); foreach (Sales_ShoppingCartItems item in ShoppingCart.Items) { item.SessionId = null; item.OrderId = newOrder.Id; data.Sales_ShoppingCartItems.Attach(item); if (item.Monthly > 0m && item.Quantity > 0) { // Set up the ARB process Accounting_RecurringBillables rBillable = new Accounting_RecurringBillables(); rBillable.Id = Guid.NewGuid(); rBillable.OrganizationId = SelectedOrganization.Id; rBillable.Service = item.Id; rBillable.BillingDirection = "Pre-Bill"; rBillable.BillingPeriod = BillingPeriodType.Monthly.ToString(); rBillable.Memo = item.Name + ", %timespan%\r\n" + item.Notes; rBillable.StartAt = DateTime.UtcNow; rBillable.Status = RecurringBillableStatus.Enabled.ToString(); rBillable.Position = DateTime.UtcNow.AddMonths(1); rBillable.AccountFrom = LoggedInUser.OrganizationId; rBillable.AccountTo = SelectedOrganization.Id; rBillable.Amount = item.Monthly; data.Accounting_RecurringBillables.AddObject(rBillable); //DatabaseHelper.Link(newOrder, rBillable); } for (int i = 0; i < item.Quantity; i++) { // Insert the initial ledger items Accounting_LedgerItems ledgerItemDebit = new Accounting_LedgerItems(); ledgerItemDebit.OrganizationId = SelectedOrganization.Id; ledgerItemDebit.Id = Guid.NewGuid(); ledgerItemDebit.TransactionId = newOrder.Id; ledgerItemDebit.PostAt = DateTime.UtcNow; ledgerItemDebit.AccountId = newOrder.Id; ledgerItemDebit.LedgerType = LedgerType.Receivable.ToString(); ledgerItemDebit.Code = CodeType.Sale.ToString(); ledgerItemDebit.Memo = item.Name + "\r\n" + item.Notes; ledgerItemDebit.Amount = Math.Abs(item.UnitCost) * -1.0m; data.Accounting_LedgerItems.AddObject(ledgerItemDebit); // It is unnecessary to link these since we provide a link to the Receivable ledger. //// DatabaseHelper.Link(newOrder, ledgerItemDebit); } } AuthorizeNet.IGatewayResponse resp = card.Bill(data, newOrder, primaryAddress, billingAddress); if (resp.Approved) { // Accounting.ProcessCommision(newOrder, SelectedOrganization.ReferredBy); if (LoggedInUser != null && !LoggedInUser.Activated) { data.System_Users.Attach(LoggedInUser); LoggedInUser.Activated = true; } data.SaveChanges(); ShoppingCart.Items.Clear(); // Send receipt MailMessage msg = new MailMessage("Weavver Sales <*****@*****.**>", PrimaryContact.EmailAddress.Text); msg.Subject = orderPlacedTemplate.Subject; msg.Body = msgBody; SmtpClient sClient = new SmtpClient(ConfigurationManager.AppSettings["smtp_server"]); try { sClient.Send(msg); var type = typeof(IOrderEvents); var types = AppDomain.CurrentDomain.GetAssemblies().ToList() .Where(y => y.FullName.Contains("WeavverExtension")) .SelectMany(s => s.GetTypes()) .Where(p => type.IsAssignableFrom(p) && p.IsClass); foreach (var interfacedClassType in types) { foreach (Sales_ShoppingCartItems purchasedItem in ShoppingCart.Items) { if (purchasedItem.Quantity > 0) { object o = Activator.CreateInstance(interfacedClassType); var orderEvents = o as IOrderEvents; orderEvents.Provision(newOrder, purchasedItem); } } } } catch (Exception ex) { Weavver.Utilities.ErrorHandling.LogError(Request, "/workflows/sales_orderplace", ex); ShowError("Your order was placed but a receipt could not be sent due to a system error. Please contact customer service for further assistance."); return; } Response.Redirect("~/CMS_Pages/Details.aspx?id=071c0768-84ed-4770-9519-a98806a87c68&OrderId=" + newOrder.Id.ToString()); } else { ShowError("Your card did not go through because '" + resp.Message + "'. Please check the card number and try again."); return; } } } }
//------------------------------------------------------------------------------------------- protected void Page_Load(object sender, EventArgs e) { IsPublic = true; if (Request["x_response_code"] == "1") { using (WeavverEntityContainer data = new WeavverEntityContainer()) { Accounting_LedgerItems item = new Accounting_LedgerItems(); item.OrganizationId = SelectedOrganization.Id; item.Id = Guid.NewGuid(); item.PostAt = DateTime.UtcNow; item.AccountId = new Guid(Request["accountid"]); item.LedgerType = LedgerType.Savings.ToString(); item.Code = CodeType.Deposit.ToString(); item.Memo = Request["x_first_name"] + " " + Request["x_last_name"] + ", TXNID#" + Request["x_trans_id"] + ", ACCT#" + Request["x_account_number"]; item.Amount = Decimal.Parse(Request["x_amount"]); item.CreatedAt = DateTime.UtcNow; item.CreatedBy = (LoggedInUser == null) ? Guid.Empty : LoggedInUser.Id; item.UpdatedAt = DateTime.UtcNow; item.UpdatedBy = (LoggedInUser == null) ? Guid.Empty : LoggedInUser.Id; Guid orderId = Guid.Empty; if (Guid.TryParse(Request["OrderId"], out orderId)) { item.TransactionId = orderId; } data.Accounting_LedgerItems.AddObject(item); data.SaveChanges(); } } else { string toHash = ConfigurationManager.AppSettings["authorize.net_hash"] + Request["authorize.net_loginid"] + Request["x_trans_id"] + Request["x_amount"]; MailMessage message = new MailMessage(); message.From = new MailAddress("*****@*****.**"); message.To.Add(new MailAddress(ConfigurationManager.AppSettings["admin_address"])); message.Subject = "Unhandled Silent Post"; message.Body = "Silent post to: \r\n\r\n"; //message.Body += Request["aspxerrorpath"] + "\r\n\r\n"; message.Body += "To hash: " + toHash + "\r\n"; for (int i = 0; i < Request.ServerVariables.Count; i++) { message.Body += Request.ServerVariables.Keys[i] + ": " + Request.ServerVariables[i] + "\r\n"; } message.Body += "\r\nQuery String:\r\n"; for (int i = 0; i < Request.QueryString.Count; i++) { message.Body += Request.QueryString.Keys[i] + ": " + Request.QueryString[i] + "\r\n"; } message.Body += "\r\nPOST:\r\n"; for (int i = 0; i < Request.Form.Count; i++) { message.Body += Request.Form.Keys[i] + ": " + Request.Form[i] + "\r\n"; } SmtpClient client = new SmtpClient(ConfigurationManager.AppSettings["smtp_server"]); client.Send(message); } }