public ActionResult Search(string username, string faultID, string dateFrom, string dateTo)
        {
            if ((username != null || username != "") && (faultID == null || faultID == "") && (dateFrom == null || dateFrom == "") && (dateTo == null || dateTo == ""))
            {
                //WITH username

                Account account = new UserAccountClient().GetAccountByUsername(username);
                List<Fault> faultListWithAccount = new FaultClient().GetFaultsByAccountID(account.ID).ToList();

                foreach (Fault f in faultListWithAccount)
                {
                    List<FaultLog> flTemp = new FaultClient().GetAllFaultLogsByFaultID(f.ID).ToList();
                    foreach (FaultLog fl in flTemp)
                    {
                        faultLogList.Add(fl);
                    }
                }
            }
            else if ((username == null || username == "") && (faultID != null || faultID != "") && (dateFrom == null || dateFrom == "") && (dateTo == null || dateTo == ""))
            {
                //WITH faultID

                int fID = Convert.ToInt32(faultID);

                faultLogList = new FaultClient().GetAllFaultLogsByFaultID(fID).ToList();
            }
            else if ((username == null || username == "") && (faultID == null || faultID == "") && (dateFrom != null || dateFrom != "") && (dateTo != null || dateTo != ""))
            {
                //WITH DATES

                DateTime from = Convert.ToDateTime(dateFrom);
                DateTime to = Convert.ToDateTime(dateTo);

                faultLogList = new FaultClient().GetFaultLogsByDate(from, to).ToList();
            }
            else if ((username != null || !username.Equals("")) && (faultID != null || !faultID.Equals("")) && (dateFrom != null || !dateFrom.Equals("")) && (dateTo != null || !dateTo.Equals("")))
            {
                //ALL COMBINATIONS

                DateTime from = Convert.ToDateTime(dateFrom);
                DateTime to = Convert.ToDateTime(dateTo);
                int fID = Convert.ToInt32(faultID);

                Account account = new UserAccountClient().GetAccountByUsername(username);
                //List<Fault> faultListWithAccount = new FaultClient().GetFaultsByAccountID(account.ID).ToList();

                faultLogList = new FaultClient().GetFaultsByAllThreeCombinations(account.ID, fID, from, to).ToList();
            }
            else
            {
                //ERROR - CHOOSE EITHER ONE OR ALL.

                ModelState.AddModelError("", "Search must be done by either all three options, by username, by faultID, or by date-from and date-to.  Please enter the correct choice.");
            }

            return View("Index", faultLogList);
        }
        public ActionResult ReportFault(GenerateFaultModel model, int pID)
        {
            try
            {
                if (Session["accountID"] != null)
                {
                    int ticketNumber;
                    bool available = false;

                    do
                    {
                        ticketNumber = new FaultClient().GenerateRandomNumber();
                        if (new FaultClient().GetFaultByTicketNumber(ticketNumber) != null)
                        {
                            ticketNumber = new FaultClient().GenerateRandomNumber();
                            available = false;
                        }
                        else
                        {
                            available = true;
                        }
                    }
                    while (!available);

                    BarCodeData barCodeData = new BarCodeData();
                    barCodeData.Height = 125;
                    barCodeData.Width = 225;
                    barCodeData.Angle = 0;
                    barCodeData.Ratio = 5;
                    barCodeData.Module = 0;
                    barCodeData.Left = 25;
                    barCodeData.Top = 0;
                    barCodeData.CheckSum = false;
                    barCodeData.FontName = "Arial";
                    barCodeData.BarColor = "Black";
                    barCodeData.BGColor = "White";
                    barCodeData.FontSize = 10.0f;
                    barCodeData.barcodeOption = BarcodeOption.Both;
                    barCodeData.barcodeType = BarcodeType.Code_2_5_interleaved;
                    barCodeData.checkSumMethod = CheckSumMethod.None;
                    barCodeData.showTextPosition = ShowTextPosition.BottomCenter;
                    barCodeData.BarCodeImageFormat = ImageFormats.PNG;

                    Byte[] imgBarcode = new BarCodeSoapClient().GenerateBarCode(barCodeData, randomNum.ToString());

                    MemoryStream memStream = new MemoryStream(imgBarcode);
                    Bitmap bm = new Bitmap(memStream);
                    bm.Save(HttpContext.Response.OutputStream, ImageFormat.Jpeg);

                    System.Drawing.Image image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(imgBarcode));

                    Fault fault = new Fault();
                    fault.TicketNumber = ticketNumber;
                    fault.ProductID = pID;
                    fault.AccountID = (int)Session["accountID"];
                    fault.Barcode = imgBarcode;

                    new FaultClient().AddFault(fault);

                    FaultLog faultLog = new FaultLog();
                    faultLog.FaultID = new FaultClient().GetFaultByTicketNumber(ticketNumber).ID;
                    faultLog.Description = model.Description;
                    faultLog.DateOfReport = DateTime.Today;
                    faultLog.Status = "Reported";

                    new FaultClient().AddFaultLog(faultLog);
                    User user = new UserAccountClient().GetUserByAccountID((int)Session["accountID"]);

                    memStream.Position = 0;
                    string body = string.Format(@"Dear " + user.Name + " " + user.Surname + ",<br /><br />A new fault report has been made."+
                            "Please find attached your barcode image. <br />Ticket Number: " + ticketNumber + "<br /><br />Regards,<br />Electros Ltd. Staff");

                    //SEND EMAIL HERE
                    MailMessage newMessage = new MailMessage();

                    newMessage.From = new MailAddress("*****@*****.**");
                    newMessage.To.Add(new MailAddress(user.Email));
                    newMessage.Subject = "Fault Report";
                    newMessage.Attachments.Add(new Attachment(memStream, "Barcodeimg.jpg", "image/jpg"));
                    newMessage.IsBodyHtml = true;
                    newMessage.Body = body;
                    SmtpClient smtpClient = new SmtpClient("smtp.go.net.mt");
                    smtpClient.Send(newMessage);

                    return RedirectToAction("Index", "GenerateFault");
                }
                else
                {
                    return RedirectToAction("Login", "Login");
                }
            }
            catch (Exception e)
            {
                ViewBag.Error = "An error has occured.";
                return RedirectToAction("Index", "GenerateFault");
            }
        }
        public ActionResult ShowReport(int pID, int oID)
        {
            try
            {
                PrintStatementModel model = new PrintStatementModel();
                Order o = new OrderClient().GetOrderByID(oID);
                model.myOrder = o;

                ProductOrder po = new OrderClient().GetProductOrderByOrderIDAndProductID(o.ID, pID);
                model.myProductOrder = po;

                List<FaultLog> fl = new List<FaultLog>();

                Product p = new ProductClient().GetProductByID(pID);
                model.myProduct = p;

                List<Fault> f = new FaultClient().GetFaultsByAccountIDandProductID((int)Session["accountID"], pID).ToList();
                model.myFaultList = f;

                Common.FaultLog faultLog = new Common.FaultLog();
                List<Common.FaultLog> faultLogList = new List<Common.FaultLog>();
                List<Common.Fault> flist = new List<Common.Fault>();

                flist = new DSA_Assignment1_Sit1.FaultServ.FaultClient().GetFaultsByAccountIDandProductID((int)Session["accountID"], pID).ToList();

                foreach (Common.Fault fa in flist)
                {
                    List<Common.FaultLog> flTemp = new DSA_Assignment1_Sit1.FaultServ.FaultClient().GetAllFaultLogsByFaultID(fa.ID).ToList();

                    model.myFaultLog = flTemp;
                }

                if (model.myFaultLog == null)
                {
                    List<Order> orderList = new OrderClient().GetBoughtOrdersByAccountID((int)Session["accountID"]).ToList();

                    foreach (Order or in orderList)
                    {
                        List<ProductOrder> productOrderlist = new OrderClient().GetProductOrderByOrderID(or.ID).ToList();

                        foreach (ProductOrder por in productOrderlist)
                        {
                            allPO.Add(por);
                        }
                    }

                    ViewBag.Error = "No fault logs recorded.";
                    return RedirectToAction("PurchaseHistory", allPO);
                }
                //SEND EMAIL

                //get user email
                User user = new UserAccountClient().GetUserByAccountID((int)Session["accountID"]);

                //Render email

                Document document = new Document();
                MemoryStream memoryStream = new MemoryStream();
                PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);

                document.Open();
                document.Add(new Paragraph("Electros Ltd."));
                document.Add(new Paragraph());
                document.Add(new Paragraph("Item: "));
                document.Add(new Paragraph("Product ID          " + "Name                    " + "Price          " + "Date Of Purchase         " + "Warranty Expiry"));
                document.Add(new Paragraph(model.myProduct.ID + "                    " + model.myProduct.Name + "          " + model.myProduct.Price + "          " + model.myOrder.DateOfOrder.ToShortDateString() + "             " + model.myProductOrder.WarrantyExpiry.ToShortDateString()));
                document.Add(new Paragraph());
                document.Add(new Paragraph("Faults: "));
                document.Add(new Paragraph("Fault ID          Date          Fault Details                              Status"));
                foreach (FaultLog flo in model.myFaultLog)
                {
                    document.Add(new Paragraph(flo.FaultID + "          " + flo.DateOfReport.ToShortDateString() + "          " + flo.Description + "                              " + flo.Status));
                }
                writer.CloseStream = false;
                document.Close();
                memoryStream.Position = 0;

                MailMessage newMessage = new MailMessage();
                newMessage.From = new MailAddress("*****@*****.**");
                newMessage.To.Add(new MailAddress(user.Email));
                newMessage.Subject = "Print Statement";
                newMessage.Body = "Dear " + user.Name + " " + user.Surname + ",  please find attached details of the product's faults.";
                Attachment attachment = new Attachment(memoryStream, "ReportStatement.pdf");
                newMessage.Attachments.Add(attachment);
                SmtpClient smtpClient = new SmtpClient("smtp.go.net.mt");
                smtpClient.Send(newMessage);

                return new RazorPDF.PdfResult(model, "ShowReport");
            }
            catch (Exception e)
            {
                List<Order> orderList = new OrderClient().GetBoughtOrdersByAccountID((int)Session["accountID"]).ToList();

                foreach (Order o in orderList)
                {
                    List<ProductOrder> productOrderlist = new OrderClient().GetProductOrderByOrderID(o.ID).ToList();

                    foreach (ProductOrder po in productOrderlist)
                    {
                        allPO.Add(po);
                    }
                }
                ViewBag.Error = "An error has occured.";
                return RedirectToAction("PurchaseHistory" , allPO);
            }
        }
        public ActionResult Update(GenerateFaultModel model, int faultID)
        {
            FaultLog faultLog = new FaultLog();
            faultLog.Status = model.Status;
            faultLog.Description = model.Description;
            faultLog.FaultID = faultID;
            faultLog.DateOfReport = DateTime.Today;

            new FaultClient().AddFaultLog(faultLog);
            faultLogs.Add(faultLog);

            Fault f = new FaultClient().GetFaultByID(faultID);

            User u = new UserAccountClient().GetUserByAccountID(f.AccountID);

            MailMessage newMessage = new MailMessage();
            newMessage.From = new MailAddress("*****@*****.**");
            newMessage.To.Add(new MailAddress(u.Email));
            newMessage.Subject = "Fault Report";
            newMessage.Body = "One of our representatives just updated your product with the latest service.  Please check our website for the update details.";
            SmtpClient smtpClient = new SmtpClient("smtp.go.net.mt");
            smtpClient.Send(newMessage);

            return View("Index", faultLogs);
        }
        public ActionResult Search(string username, string faultID, string dateFrom, string dateTo)
        {
            if (Session["accountID"] != null)
            {
                if ((username != null || username != "") && (faultID == null || faultID == "") && (dateFrom == null || dateFrom == "") && (dateTo == null || dateTo == ""))
                {
                    //WITH username
                    if (new UserAccountClient().GetAccountByUsername(username) != null)
                    {
                        Account account = new UserAccountClient().GetAccountByUsername(username);
                        List<Fault> faultListWithAccount = new FaultClient().GetFaultsByAccountID(account.ID).ToList();

                        foreach (Fault f in faultListWithAccount)
                        {
                            List<FaultLog> flTemp = new FaultClient().GetAllFaultLogsByFaultID(f.ID).ToList();
                            foreach (FaultLog fl in flTemp)
                            {
                                faultLogList.Add(fl);
                            }
                        }
                    }
                    else
                    {
                        TempData["Error"] = "Username doesn't exist.";
                        return RedirectToAction("Index", faultLogs);
                    }
                }
                else if ((username == null || username == "") && (faultID != null || faultID != "") && (dateFrom == null || dateFrom == "") && (dateTo == null || dateTo == ""))
                {
                    //WITH faultID

                    int fID = 0;

                    try
                    {
                        fID = Convert.ToInt32(faultID);
                    }
                    catch (Exception e)
                    {
                        TempData["Error"] = "No such fault ID was found.";
                        return RedirectToAction("Index", faultLogs);
                    }

                    if (new FaultClient().GetAllFaultLogsByFaultID(fID).ToList() != null)
                    {
                        faultLogList = new FaultClient().GetAllFaultLogsByFaultID(fID).ToList();
                    }
                    else
                    {
                        TempData["Error"] = "No such fault ID was found.";
                        return RedirectToAction("Index", faultLogs);
                    }
                }
                else if ((username == null || username == "") && (faultID == null || faultID == "") && (dateFrom != null || dateFrom != "") && (dateTo != null || dateTo != ""))
                {
                    //WITH DATES
                    try
                    {
                        DateTime from = Convert.ToDateTime(dateFrom);
                        DateTime to = Convert.ToDateTime(dateTo);

                        faultLogList = new FaultClient().GetFaultLogsByDate(from, to).ToList();
                    }
                    catch (Exception e)
                    {
                        TempData["Error"] = "Date format is incorrect.";
                        return RedirectToAction("Index", faultLogs);
                    }
                }
                else if ((username != null || !username.Equals("")) && (faultID != null || !faultID.Equals("")) && (dateFrom != null || !dateFrom.Equals("")) && (dateTo != null || !dateTo.Equals("")))
                {
                    //ALL COMBINATIONS

                    int fID = 0;

                    try
                    {
                        fID = Convert.ToInt32(faultID);
                    }
                    catch (Exception e)
                    {
                        TempData["Error"] = "No such fault ID was found.";
                        return RedirectToAction("Index", faultLogs);
                    }

                    if (new UserAccountClient().GetAccountByUsername(username) != null && new FaultClient().GetAllFaultLogsByFaultID(fID).ToList() != null)
                    {
                        DateTime from;
                        DateTime to;
                        try
                        {
                            from = Convert.ToDateTime(dateFrom);
                            to = Convert.ToDateTime(dateTo);
                        }
                        catch (Exception e)
                        {
                            TempData["Error"] = "Dates are not in the correct format.";
                            return RedirectToAction("Index", faultLogs);
                        }
                        int faID = Convert.ToInt32(faultID);

                        Account account = new UserAccountClient().GetAccountByUsername(username);

                        faultLogList = new FaultClient().GetFaultsByAllThreeCombinations(account.ID, faID, from, to).ToList();
                    }
                    else
                    {
                        TempData["Error"] = "Please enter correct data.";
                        return RedirectToAction("Index", faultLogs);
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Search must be done by either all three options, by username, by faultID, or by date-from and date-to.  Please enter the correct choice.");
                }

                return View("Index", faultLogList);
            }
            else
            {
                return RedirectToAction("Login", "Login");
            }
        }
        public ActionResult Update(GenerateFaultModel model, int faultID)
        {
            try
            {
                if (Session["accountID"] != null)
                {
                    FaultLog faultLog = new FaultLog();

                    //faultLog.Status = model.Status;

                    int statusID = Convert.ToInt32(model.Status);
                    switch (statusID)
                    {
                        case 1:
                            faultLog.Status = "Reported";
                            sendSMS("Reported");
                            break;
                        case 2:
                            faultLog.Status = "Picked up - Transit to main office";
                            sendSMS("Picked up - Transit to main office");
                            break;
                        case 3:
                            faultLog.Status = "Service in progress";
                            sendSMS("Service in progress");
                            break;
                        case 4:
                            faultLog.Status = "Service completed - Ready for delivery";
                            sendSMS("Service completed - Ready for delivery");
                            break;
                        case 5:
                            faultLog.Status = "Picked up - Transit to customer";
                            sendSMS("Picked up - Transit to customer");
                            break;
                        case 6:
                            faultLog.Status = "Fault Completed";
                            sendSMS("Fault Completed");
                            break;
                        default:
                            faultLog.Status = "Reported";
                            sendSMS("Reported");
                            break;
                    }

                    faultLog.Description = model.Description;
                    faultLog.FaultID = faultID;
                    faultLog.DateOfReport = DateTime.Today;

                    new FaultClient().AddFaultLog(faultLog);
                    faultLogs.Add(faultLog);

                    Fault f = new FaultClient().GetFaultByID(faultID);

                    User u = new UserAccountClient().GetUserByAccountID(f.AccountID);

                    MailMessage newMessage = new MailMessage();
                    newMessage.From = new MailAddress("*****@*****.**");
                    newMessage.To.Add(new MailAddress(u.Email));
                    newMessage.Subject = "Fault Report";
                    newMessage.Body = "One of our representatives just updated your product with the latest service.  Please check our website for the update details.";
                    SmtpClient smtpClient = new SmtpClient("smtp.go.net.mt");
                    smtpClient.Send(newMessage);

                    return View("Index", faultLogs);
                }
                else
                {
                    return RedirectToAction("Login", "Login");
                }
            }
            catch (Exception e)
            {
                TempData["Error"] = "An error has occured.";
                return RedirectToAction("Index", faultLogs);
            }
        }