protected void ShowResults(string CustomerName, long pCustomerId, ClientUsageTbl pOriginalUsageData) { pnlOrderDetails.Visible = false; tbxCustomerName.Text = CustomerName; List <ClientUsageTbl> _NewAndOld = new List <ClientUsageTbl>(); _NewAndOld.Add(pOriginalUsageData); _NewAndOld.Add(new ClientUsageTbl().GetUsageData(pCustomerId)); dgCustomerUsage.AutoGenerateColumns = false; dgCustomerUsage.DataSource = _NewAndOld; dgCustomerUsage.DataBind(); pnlCustomerDetailsUpdated.Visible = true; }
protected void btnForceNext_Click(object sender, EventArgs e) { TrackerDotNet.classes.TrackerTools _TTools = new TrackerDotNet.classes.TrackerTools(); DateTime _dt = _TTools.GetClosestNextRoastDate(DateTime.Now.AddDays(14).Date); // add a fortnight; ClientUsageTbl _ClientUsage = new ClientUsageTbl(); _ClientUsage.ForceNextCoffeeDate(_dt.AddDays(3), StringToInt64(CompanyIDLabel.Text)); CustomersTbl _CustomerTbl = new CustomersTbl(); _CustomerTbl.IncrementReminderCount(StringToInt64(CompanyIDLabel.Text)); dgCustomerUsage.DataBind(); string _ScriptToRun = "showMessage('" + String.Format("{0} force to skip a week of prediction", CompanyNameTextBox.Text) + "');"; ScriptManager.RegisterStartupScript(Page, Page.GetType(), "CustomerInserted", _ScriptToRun, true); }
/// <summary> /// Update the predictive dates for the customer ID provided /// </summary> /// <param name="pCustomerID">customer di to update</param> /// <param name="pLastCount">the last count known</param> /// <param name="pDefaultDate">The date to set if the client has no orders</param> public bool UpdatePredictions(long pCustomerID, long pLastCount) { bool _Success = false; // We need to update all the items that are affected by the latest update. First select the last row in client usage table, // then determine what has changed. Now retrieve the last line selected clientusageline of client that is selected on the current form // round ave consumption up to the nearest 2 decimals DateTime _dtInstall = GetInstallDate(pCustomerID); if ((pLastCount > 0) && (_dtInstall != DateTime.MinValue)) // NULLDATE { CalcAndSaveNextRequiredDates(pCustomerID); //, pLastCount); } else // do defaults { double _AveConsump = CalcAveConsumption(pCustomerID); //---> this is stupid maths (x*y)/y = x AveConsump = Round((AveConsump * ConstTypicalNumCupsPerKg) / ConstTypicalNumCupsPerKg, 2) if (_AveConsump <= 0) { _AveConsump = TrackerTools.CONST_TYPICALAVECONSUMPTION; // if the value makes not sens the force it; should perhaps look at a average that is system wide? } DateTime _dtNextCoffee = DateTime.Now.AddDays(20); ClientUsageTbl _UsageData = new ClientUsageTbl(); _UsageData.CustomerID = pCustomerID; _UsageData.LastCupCount = pLastCount; _UsageData.NextCoffeeBy = _dtNextCoffee; _UsageData.NextCleanOn = _dtNextCoffee.AddDays(20); _UsageData.NextFilterEst = _dtNextCoffee.AddDays(30); _UsageData.NextDescaleEst = _dtNextCoffee.AddDays(30); // Descale _UsageData.NextServiceEst = _dtNextCoffee.AddYears(1); // Service _UsageData.DailyConsumption = _AveConsump; // DailyConsumption _UsageData.CleanAveCount = TrackerTools.CONST_TYPICALCLEAN_CONSUMPTION; // CleanAve _UsageData.FilterAveCount = TrackerTools.CONST_TYPICALFILTER_CONSUMPTION; // FilterAve _UsageData.DescaleAveCount = TrackerTools.CONST_TYPICALDECAL_CONSUMPTION; // Descale Ave _UsageData.ServiceAveCount = 10000; // Service Ave ClientUsageTbl _UsageDAL = new ClientUsageTbl(); _UsageDAL.Update(_UsageData); } // else return(_Success); }
/// <summary> /// Generic Function to get the next date of a serivce type, depending on average cup count and last date an qty added /// </summary> /// <param name="pCustomerID">ID of the customer to check/param> /// <param name="iDefDate">default date value</param> /// <param name="pServiceType">the Service Type to Check</param> /// <returns>NextRequireDate of an item</returns> /// <remarks> /// Logic: using the customer see if the service type has been consumed, if so calculate the average between each date of use /// otherwise use the AverageConsumption and the install date to guess the next date. Return that date /// </remarks> public bool CalcAndSaveNextRequiredDates(long pCustomerID) // , long pLastCupCount) { bool _Success = false; #region LogicCalcAndSave /* * logic: * * 1. retrieve the list of items from TrackedServiceItemTbl, so that the ThisItemSetsDailyAverage is at the top an the others are below * for each item: * 2. retrieve the last item of this service type provided. * * 3a. if the date it was last provided > install date, then: * calculate the average use for that item and set the next_date_required it to the last_date + days to go * if this is serviceitemcoffee then set daily average variable to be used for general averages * 3b. else * using the typical average for the item add that to install date and using the daily average calculate when next will need * if this date is < now make it now * * 4. for each item save this to the summary table for the client, or at least save eac hitem to a list then use the list to * create an update command to update all items for this customer as per the fileds retrieved. * */ #endregion // 1. Retrieve list of TrackedServiceItemTbl, so that the Theh Item that SetsDailyAverage is at the top TrackedServiceItemTbl _TrackedServiceItemTbl = new TrackedServiceItemTbl(); List <TrackedServiceItemTbl> _TrackerServiceItems = _TrackedServiceItemTbl.GetAll("ThisItemSetsDailyAverage, ServiceTypeID"); double _DailyAverage = TrackerTools.CONST_TYPICALAVECONSUMPTION; int _ServiceItemNum = 0; List <ClientServiceItem> _ClientServiceItems = new List <ClientServiceItem>(); // do the primary item while (_TrackerServiceItems[_ServiceItemNum].ThisItemSetsDailyAverage) // should be only one but just in case { ClientServiceItem _ClientServiceItem = SetClientServiceData(pCustomerID, _TrackerServiceItems[_ServiceItemNum], 0); // 0 means set Daily Average _DailyAverage = _ClientServiceItem.ThisItemsAverage; _ClientServiceItems.Add(_ClientServiceItem); _ServiceItemNum++; } // now do the secpndary items while (_ServiceItemNum < _TrackerServiceItems.Count) // should be pnly one bu just in case { ClientServiceItem _ClientServiceItem = SetClientServiceData(pCustomerID, _TrackerServiceItems[_ServiceItemNum], _DailyAverage); _ClientServiceItems.Add(_ClientServiceItem); _ServiceItemNum++; } // #region OldVBACodeCalc //_NumDays = (int)Math.Round((200 * _LastCleanUpdateData.LastQty) / _AveConsump); //if (_LastCleanUpdateData.UsageDate.CompareTo(DateTime.MinValue) // NULLDATE == 0) // _dtNextClean.AddDays(_NumDays); //// Adjust for holidays if neccessary //_dtNextClean = AddHolidayExtension(pCustomerID, _dtNextClean, TrackerTools.CONST_STRING_SERVTYPECLEAN) // // lblStatus.Caption = "Doing next filter, decal and service calculations" //// //Do FitlerDate calc // // if they are clients the require us to predict when they will need fitlers or decalificaiton // // tablets then get the Last Date // dtNextFilter = CalcNextRequiredDate(pCustomerID, 300, AveConsump, CONST_SERVTYPEFilter, nFilterAve) // dtNextDescal = CalcNextRequiredDate(pCustomerID, 350, AveConsump, CONST_SERVTYPEDescale, nDescaleAve) // dtNextService = CalcNextRequiredDate(pCustomerID, 5500, AveConsump, CONST_SERVTYPEService, nServiceAve) // // LastServiceDate = LookupLastServiceDate() // // if there has already been a filter then use average otherwise assume 220 as the cup count //Else // // do default values // If dtInstall <> ConstNullDate Then // dtNextCoffee = DateAdd("d", 14, dtInstall) // dtNextClean = DateAdd("d", 28, dtInstall) // Assume it is at least 28 days away #endregion /* 4. for each item save this to the summary table for the client, or at least save eac hitem to a list then use the list to * create an update command to update all items for this customer as per the fileds retrieved.*/ // check if a record exists in the client usage table ClientUsageTbl _ClientUsageTbl = new ClientUsageTbl(); if (_ClientUsageTbl.UsageDataExists(pCustomerID)) { _Success = UpdateClientUsageTable(pCustomerID, _ClientServiceItems); } else { _Success = InsertClientUsageTable(pCustomerID, _ClientServiceItems); } return(_Success); }
/// <summary> /// Gets the average consumption as per the clients data /// </summary> /// <param name="pCustomerID">Customer ID to retrieve data of</param> /// <returns>Average consumption as stored in the ClientUsageTbl or if none calculates it</returns> public double GetAveConsumption(long pCustomerID) { ClientUsageTbl _ClientUsageTbl = new ClientUsageTbl(); return(_ClientUsageTbl.GetAverageConsumption(pCustomerID)); }
//protected void MarkTempOrdersItemsAsDone(long pCustomerID) //{ // string _SQLUpdate = "UPDATE OrdersTbl SET OrdersTbl.Done = True WHERE CustomderId = " + pCustomerID.ToString() + // "AND EXISTS (SELECT RequiredByDate FROM TempOrdersHeaderTbl " + // " WHERE (RequiredByDate = OrdersTbl.RequiredByDate))"; // TrackerDb _TrackerDb = new TrackerDb(); // _TrackerDb.ExecuteNonQuerySQL(_SQLUpdate); // // ResetCustomerReminderCount(pCustomerId); // //''''''''''''' // //' mark orders as done - should do this last // //''' // //lblStatus.Caption = "Marking orders as done" // //dbs.Execute ("UPDATE OrdersTbl SET OrdersTbl.Done = True " + _ // // "WHERE (((Exists (Select TempOrdersTbl.OrderId from TempOrdersTbl where " + _ // // "TempOrdersTbl.OrderId = OrdersTbl.OrderId))<>False))") // //''''''' // //' Resetting count and enable, only if coffee item // //' // //dbs.Execute ("UPDATE DISTINCTROW CustomersTbl SET ReminderCount = 0, enabled = True WHERE CustomerID=" + lblCustomerID.Caption) //} // Logic for processing order as done: // 1. move items consumed from temp aable to the items consumed for the contact // 2. using the summary of the data on the form do calculations around predictions // on when the contact needs items again. // 3. Notify client of items deliverred (add option to contacts table) // 4. return to the previous page. protected void btnDone_Click(object sender, EventArgs e) { const long MAXQTYINSTOCK = 50; TrackerTools _TrackerTools = new TrackerTools(); _TrackerTools.SetTrackerSessionErrorString(string.Empty); Label _CustomerIDLabel = (Label)(fvOrderDone.FindControl("CustomerIDLabel")); Label _CustomerNameLabel = (Label)(fvOrderDone.FindControl("CompanyNameLabel")); long _CustomerID = Convert.ToInt64(_CustomerIDLabel.Text); TextBox _OrderDateLabel = (TextBox)(fvOrderDone.FindControl("ByDateTextBox")); DateTime _OrderDate = Convert.ToDateTime(_OrderDateLabel.Text); double _CoffeeStock = 0; // store the current data for display later: ClientUsageTbl _OriginalUsageDAL = new ClientUsageTbl(); ClientUsageTbl _OriginalUsageData = _OriginalUsageDAL.GetUsageData(_CustomerID); if (!string.IsNullOrEmpty(_TrackerTools.GetTrackerSessionErrorString())) { Response.Write(_TrackerTools.GetTrackerSessionErrorString()); } TempOrdersDAL _TempOrdersDAL = new TempOrdersDAL(); // bool _HasCoffeeInTempOrder = _TempOrdersDAL.HasCoffeeInTempOrder(); if (!string.IsNullOrEmpty(_TrackerTools.GetTrackerSessionErrorString())) { Response.Write(_TrackerTools.GetTrackerSessionErrorString()); } /* NEED TO ADD Code to check for zzname */ // check cup count not in wrong place if ((tbxStock.Text.Length > 0) && (Convert.ToInt64(tbxStock.Text) > MAXQTYINSTOCK)) { ltrlStatus.Text = "<b>The stock quantity appears very high please check that you have enterred the correct value in kilograms.</b>"; } else { _CoffeeStock = String.IsNullOrEmpty(tbxStock.Text) ? 0 : Convert.ToDouble(tbxStock.Text); // First we must get the latest cup count ltrlStatus.Text = "Calculating the latest cup count"; GeneralTrackerDbTools _GeneralTrackerDb = new GeneralTrackerDbTools(); GeneralTrackerDbTools.LineUsageData _LatestCustomerData = _GeneralTrackerDb.GetLatestUsageData(_CustomerID, TrackerTools.CONST_SERVTYPECOFFEE); if (!string.IsNullOrEmpty(_TrackerTools.GetTrackerSessionErrorString())) { Response.Write(_TrackerTools.GetTrackerSessionErrorString()); } bool _bIsActual = (tbxCount.MaxLength > 0); // if there is a cup count long _lCupCount = 0; if (tbxCount.MaxLength > 0) { _lCupCount = Convert.ToInt64(tbxCount.Text); } // Calculate the cup count if we do not have it on the form or they entered a value that makes no sense if ((_lCupCount < 1) || (_lCupCount < _LatestCustomerData.LastCount)) { ltrlStatus.Text = "Calculating the latest est cup count"; _lCupCount = _GeneralTrackerDb.CalcEstCupCount(_CustomerID, _LatestCustomerData, _HasCoffeeInTempOrder); _bIsActual = false; } //' add items to consumption log _lCupCount = AddItemsToClientUsageTbl(_CustomerID, _bIsActual, _lCupCount, _CoffeeStock, _OrderDate); // update the last cup count for the client if (!_OriginalUsageDAL.UpdateUsageCupCount(_CustomerID, _lCupCount)) { ltrlStatus.Text = "error updating last count"; } // now update the predictions _GeneralTrackerDb.UpdatePredictions(_CustomerID, _lCupCount); // update all items in the order table as done that are done _TempOrdersDAL.MarkTempOrdersItemsAsDone(); // reset count and enable client _GeneralTrackerDb.ResetCustomerReminderCount(_CustomerID, _HasCoffeeInTempOrder); // destroy the temp table that we used to create this temp orders _TempOrdersDAL.KillTempOrdersData(); ShowResults(_CustomerNameLabel.Text, _CustomerID, _OriginalUsageData); } }
//protected void MarkTempOrdersItemsAsDone(long pCustomerID) //{ // string _SQLUpdate = "UPDATE OrdersTbl SET OrdersTbl.Done = True WHERE CustomderId = " + pCustomerID.ToString() + // "AND EXISTS (SELECT RequiredByDate FROM TempOrdersHeaderTbl " + // " WHERE (RequiredByDate = OrdersTbl.RequiredByDate))"; // TrackerDb _TrackerDb = new TrackerDb(); // _TrackerDb.ExecuteNonQuerySQL(_SQLUpdate); // // ResetCustomerReminderCount(pCustomerId); // //''''''''''''' // //' mark orders as done - should do this last // //''' // //lblStatus.Caption = "Marking orders as done" // //dbs.Execute ("UPDATE OrdersTbl SET OrdersTbl.Done = True " + _ // // "WHERE (((Exists (Select TempOrdersTbl.OrderId from TempOrdersTbl where " + _ // // "TempOrdersTbl.OrderId = OrdersTbl.OrderId))<>False))") // //''''''' // //' Resetting count and enable, only if coffee item // //' // //dbs.Execute ("UPDATE DISTINCTROW CustomersTbl SET ReminderCount = 0, enabled = True WHERE CustomerID=" + lblCustomerID.Caption) //} // Logic for processing order as done: // 1. move items consumed from temp aable to the items consumed for the contact // 2. using the summary of the data on the form do calculations around predictions // on when the contact needs items again. // 3. Notify client of items deliverred (add option to contacts table) // 4. return to the previous page. protected void btnDone_Click(object sender, EventArgs e) { const long MAXQTYINSTOCK = 50; TrackerTools _TrackerTools = new TrackerTools(); _TrackerTools.SetTrackerSessionErrorString(string.Empty); Label _CustomerIDLabel = (Label)(fvOrderDone.FindControl("CustomerIDLabel")); Label _CustomerNameLabel = (Label)(fvOrderDone.FindControl("CompanyNameLabel")); long _CustomerID = Convert.ToInt64(_CustomerIDLabel.Text); TextBox _OrderDateLabel = (TextBox)(fvOrderDone.FindControl("ByDateTextBox")); DateTime _OrderDate = Convert.ToDateTime(_OrderDateLabel.Text); double _CoffeeStock = 0; // store the current data for display later: ClientUsageTbl _OriginalUsageDAL = new ClientUsageTbl(); ClientUsageTbl _OriginalUsageData = _OriginalUsageDAL.GetUsageData(_CustomerID); if (!string.IsNullOrEmpty(_TrackerTools.GetTrackerSessionErrorString())) { Response.Write(_TrackerTools.GetTrackerSessionErrorString()); } TempOrdersDAL _TempOrdersDAL = new TempOrdersDAL(); // bool _HasCoffeeInTempOrder = _TempOrdersDAL.HasCoffeeInTempOrder(); if (!string.IsNullOrEmpty(_TrackerTools.GetTrackerSessionErrorString())) { Response.Write(_TrackerTools.GetTrackerSessionErrorString()); } /* NEED TO ADD Code to check for zzname */ // check cup count not in wrong place if ((tbxStock.Text.Length > 0) && (Convert.ToDouble(tbxStock.Text) > MAXQTYINSTOCK)) { showMessageBox _MsgBox = new showMessageBox(this.Page, "Stock seems high", "The stock quantity appears very high please check that you have enterred the correct value in kilograms.</b>"); } else { _CoffeeStock = String.IsNullOrEmpty(tbxStock.Text) ? 0 : Math.Round(Convert.ToDouble(tbxStock.Text), 2); // First we must get the latest cup count ltrlStatus.Text = "Calculating the latest cup count"; GeneralTrackerDbTools _GeneralTrackerDb = new GeneralTrackerDbTools(); GeneralTrackerDbTools.LineUsageData _LatestCustomerData = _GeneralTrackerDb.GetLatestUsageData(_CustomerID, TrackerTools.CONST_SERVTYPECOFFEE); if (!string.IsNullOrEmpty(_TrackerTools.GetTrackerSessionErrorString())) { showMessageBox _smb = new showMessageBox(this.Page, "Tracker Session Error", _TrackerTools.GetTrackerSessionErrorString()); } bool _bIsActual = (tbxCount.MaxLength > 0); // if there is a cup count long _lCupCount = 0; if (tbxCount.MaxLength > 0) { _lCupCount = Convert.ToInt64(tbxCount.Text); } // Calculate the cup count if we do not have it on the form or they entered a value that makes no sense if ((_lCupCount < 1) || (_lCupCount < _LatestCustomerData.LastCount)) { ltrlStatus.Text = "Calculating the latest est cup count"; _lCupCount = _GeneralTrackerDb.CalcEstCupCount(_CustomerID, _LatestCustomerData, _HasCoffeeInTempOrder); _bIsActual = false; } // clear any repairs that may have been logged RepairsTbl _Repairs = new RepairsTbl(); _Repairs.SetStatusDoneByTempOrder(); //' add items to consumption log _lCupCount = AddItemsToClientUsageTbl(_CustomerID, _bIsActual, _lCupCount, _CoffeeStock, _OrderDate); // update the last cup count for the client if (!_OriginalUsageDAL.UpdateUsageCupCount(_CustomerID, _lCupCount)) { showMessageBox _smb = new showMessageBox(this.Page, "Error", "Error updating last count"); ltrlStatus.Text = "error updating last count"; } // now update the predictions _GeneralTrackerDb.UpdatePredictions(_CustomerID, _lCupCount); // update all items in the order table as done that are done _TempOrdersDAL.MarkTempOrdersItemsAsDone(); // reset count and enable client _GeneralTrackerDb.ResetCustomerReminderCount(_CustomerID, _HasCoffeeInTempOrder); if (_HasCoffeeInTempOrder) { _GeneralTrackerDb.SetClientCoffeeOnlyIfInfo(_CustomerID); } // may need to add another check here to handle if they have ordered maintenance stuff. switch (rbtnSendConfirm.SelectedValue) { case "none": break; case "postbox": SendDeliveredEmail(_CustomerID, "placed your order in the your post box."); break; case "dispatched": SendDeliveredEmail(_CustomerID, "dispatched you order."); break; case "done": SendDeliveredEmail(_CustomerID, "delivered your order and it has signed for."); break; default: break; } // destroy the temp table that we used to create this temp orders _TempOrdersDAL.KillTempOrdersData(); ShowResults(_CustomerNameLabel.Text, _CustomerID, _OriginalUsageData); } }
/// <summary> /// Handle the send button click /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnSend_Click(object sender, EventArgs e) { uprgSendEmail.DisplayAfter = 0; string _OrderType = ""; TempCoffeeCheckup _TempCoffeeCheckup = new TempCoffeeCheckup(); // get all the contacts that need to be reminded List <ContactToRemindWithItems> _ContactsToRemind = _TempCoffeeCheckup.GetAllContactAndItems(); SendCheckEmailTextsData _EmailTextData = new SendCheckEmailTextsData(); CustomersTbl _Customer = new CustomersTbl(); int _RemindersSent = 0, _RemindersFailed = 0; for (int i = 0; i < _ContactsToRemind.Count; i++) { try { _EmailTextData.Header = tbxEmailIntro.Text; _EmailTextData.Footer = tbxEmailFooter.Text; if (String.Concat(_ContactsToRemind[i].EmailAddress, _ContactsToRemind[i].AltEmailAddress).Contains("@")) { // customer has an email address _ContactsToRemind[i].ReminderCount++; if (_ContactsToRemind[i].ReminderCount < CONST_MAXREMINDERS) { _EmailTextData.Body = PersonalizeBodyText(_ContactsToRemind[i]); // look through items to see if this is a reoccuring or auto order _OrderType = GetTheOrderType(_ContactsToRemind[i]); // if auto fulfil add to orders with auto add in the note if (String.IsNullOrWhiteSpace(_OrderType)) { _EmailTextData.Body += "We will only place an order on your request, no order has been added, this just a reminder."; } else { _EmailTextData.Body += "This is a" + (IsVowel(_OrderType[0]) ? "n " : " ") + _OrderType + ", please respond to cancel."; } if (_ContactsToRemind[i].ReminderCount == CONST_MAXREMINDERS - 1) { _EmailTextData.Body = String.Concat("This is your last reminder email. Next time reminders will be disabled until you order again.", _EmailTextData.Body); } // else next reminder in week*reminder count??? #if _DEBUG if (SendReminder(_EmailTextData, _ContactsToRemind[i], _OrderType, _RemindersSent)) #else if (SendReminder(_EmailTextData, _ContactsToRemind[i], _OrderType)) #endif { _RemindersSent++; } else { _RemindersFailed++; } //--- delay reminders for those that have too many, and update reminder count for all if (_ContactsToRemind[i].ReminderCount >= CONST_FORCEREMINDERDELAYCOUNT) { DateTime _ForceDate = _ContactsToRemind[i].NextPrepDate.AddDays(10 * (_ContactsToRemind[i].ReminderCount - CONST_FORCEREMINDERDELAYCOUNT + 1)); ClientUsageTbl _ClientUsage = new ClientUsageTbl(); _ClientUsage.ForceNextCoffeeDate(_ForceDate, _ContactsToRemind[i].CustomerID); } // icrement count and set last date _Customer.SetSentReminderAndIncrementReminderCount(DateTime.Now.Date, _ContactsToRemind[i].CustomerID); ////---- check the last sent date? } else { _Customer.DisableCustomer(_ContactsToRemind[i].CustomerID); _RemindersFailed++; } #if _DEBUG // if (_RemindersSent > 5) break; #endif } } catch (Exception _Ex) { // just incse we have an error we normally do not have showMessageBox _ExMsg = new showMessageBox(this.Page, "Error sending...", _Ex.Message); _RemindersFailed++; } } showMessageBox _MsgBox = new showMessageBox(this.Page, "Reminder emails status", String.Format("Reminders processed. Sent: {0}; Failed: {1}", _RemindersSent, _RemindersFailed)); string _RedirectURL = String.Format("{0}?LastSentDate={1:d}", ResolveUrl("~/Pages/SentRemindersSheet.aspx"), DateTime.Now.Date); Response.Redirect(_RedirectURL); PrepPageData(); }