public void InsertSchedularToken() { string IsProduction = ConfigurationSettings.AppSettings["IsProduction"].ToString(); string SlikaUrlChargeToken = ConfigurationSettings.AppSettings["SlikaUrlChargeToken"].ToString(); DateTime CurrentDate = DateTime.Now; int Day = CurrentDate.Day; int Month = CurrentDate.Month; using (var Context = new Context()) { var UsersToPay = Context.Users.Where(x => x.Active == "active" && x.DateForMonthlyPay.Value.Day == Day && x.DateForMonthlySum > 0 && x.DateForMonthlySeq != x.DateForMonthlyPrev && // ההסטוריה שונה x.cc_token != null && x.cc_token != "" ).ToList(); foreach (User up in UsersToPay) { if (up.Rivoni) { if (Month != 1 && Month != 4 && Month != 7 && Month != 10) { continue; } } AutoPayObj ap = GetAutoPayObj(up); var Farm = Context.Farms.Where(y => y.Id == up.Farm_Id).FirstOrDefault(); var Meta = JObject.Parse(Farm.Meta); string api_key = Meta["api_key"].ToString(); string api_email = Meta["api_email"].ToString(); var ua_uuid = Meta["ua_uuid"]; var reqObjAshrai = new { api_key = api_key, developer_email = "*****@*****.**", sum = up.DateForMonthlySum, cc_token = up.cc_token, cc_4_digits = up.cc_4_digits, cc_payer_name = up.cc_payer_name, cc_payer_id = up.cc_payer_id, cc_expire_month = up.cc_expire_month, cc_expire_year = up.cc_expire_year, cc_type_id = up.cc_type_id, cc_type_name = up.cc_type_name, }; string DATA = Newtonsoft.Json.JsonConvert.SerializeObject(reqObjAshrai); var SlikaUrl = SlikaUrlChargeToken; var client = new HttpClient(); HttpContent content = new StringContent(DATA, UTF8Encoding.UTF8, "application/json"); HttpResponseMessage messge = client.PostAsync(SlikaUrl, content).Result; dynamic responseToken = ""; if (messge.IsSuccessStatusCode) { responseToken = JsonConvert.DeserializeObject(messge.Content.ReadAsStringAsync().Result); if (responseToken.success == "true") { DocCreation doc = new DocCreation(); List <dynamic> Payment = new List <dynamic>(); Payment.Add(new { payment_type = 3, date = up.DateForMonthlyPay.Value.ToString("dd/MM/yyyy"), payment = up.DateForMonthlySum, cc_type = up.cc_type_id, cc_type_name = up.cc_type_name, cc_number = up.cc_4_digits, cc_deal_type = 1, cc_num_of_payments = 1, cc_payment_num = 1, }); var reqObj = new { api_key = api_key, api_email = api_email, ua_uuid = ua_uuid, developer_email = "*****@*****.**", developer_phone = "0505913817", type = 320,//קבלה חשבונית מס description = ap.InvoiceTitle, customer_name = up.FirstName + " " + up.LastName, customer_email = up.AnotherEmail, customer_address = up.Address, comment = "מס לקוח: " + up.ClientNumber + ", ת.ז.: " + up.IdNumber, item = new dynamic[] { new { details = " תאריכי שיעורים: " + ap.InvoiceDates, amount = 1, price = up.DateForMonthlySum, price_inc_vat = true } }, payment = Payment, price_total = up.DateForMonthlySum, }; dynamic response = doc.execute(((IsProduction == "0") ? Constants.ENV_TEST : Constants.ENV_PRODUCTION), reqObj); // אם זה הצליח if (response[5].ToString() == "True") { Payments p = new Payments(); p.Date = CurrentDate; p.doc_type = "MasKabala"; p.InvoiceNum = response[2].ToString(); p.InvoicePdf = response[0].ToString(); p.Price = up.DateForMonthlySum; p.InvoiceDetails = " חיוב אוטמטי תאריכי שיעורים: " + ap.InvoiceDates; p.UserId = up.Id; p.InvoiceSum = up.DateForMonthlySum; p.lessons = ap.lessons; p.month = ap.month; p.untilmonth = ap.untilmonth; Context.Payments.Add(p); up.DateForMonthlyPrev = ((up.DateForMonthlyPrev == null) ? 0 : up.DateForMonthlyPrev) + 1; Context.Entry(up).State = System.Data.Entity.EntityState.Modified; Context.SaveChanges(); } } } } // זה נועד לאפס את העסק שאם ירצה להוסיף var UsersToDeleteAuto = Context.Users.Where(x => x.Active == "active" && x.DateForMonthlyPay.Value.Day == Day && x.DateForMonthlySum > 0 && x.DateForMonthlySeq == x.DateForMonthlyPrev && // ההסטוריה שונה x.cc_token != null && x.cc_token != "" ).ToList(); foreach (User upd in UsersToDeleteAuto) { upd.DateForMonthlyPay = null; upd.DateForMonthlySum = null; upd.DateForMonthlySeq = null; upd.DateForMonthlyPrev = null; upd.Rivoni = false; Context.Entry(upd).State = System.Data.Entity.EntityState.Modified; Context.SaveChanges(); } } }
private AutoPayObj GetAutoPayObj(User up) { DateTime CurrentDate = DateTime.Now; AutoPayObj ap = new AutoPayObj(); ap.lessons = 0; ap.month = null; ap.untilmonth = null; ap.InvoiceDates = ""; // details ap.InvoiceTitle = ""; //desc if (up.PayType == "lessonCost") { int lessCount = Convert.ToInt32(up.DateForMonthlySum) / Convert.ToInt32(up.Cost); ap.lessons = lessCount; ap.InvoiceTitle = "חיוב אוטמטי עבור " + lessCount.ToString() + " שיעורים "; } else { ap.month = CurrentDate; ap.InvoiceTitle = "חיוב אוטמטי עבור חודש אחד "; if (up.Rivoni) { ap.untilmonth = CurrentDate.AddMonths(3); ap.InvoiceTitle = "חיוב אוטמטי עבור 3 חודשים "; } } using (var Context = new Context()) { var LessonsDates = (from sl in Context.StudentLessons join l in Context.Lessons on sl.Lesson_Id equals l.Id where sl.User_Id == up.Id && l.Start >= CurrentDate select new { StartDate = l.Start }).ToList(); for (int i = 0; i < LessonsDates.Count; i++) { ap.InvoiceDates += "," + LessonsDates[i].StartDate.ToString("dd/MM/yy"); if ( (i + 1 == ap.lessons) || (!up.Rivoni && LessonsDates[i].StartDate > CurrentDate.AddMonths(1)) || (up.Rivoni && LessonsDates[i].StartDate > CurrentDate.AddMonths(3)) ) { break; } } } return(ap); }