Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.75 KB | None | 0 0
  1. [HttpPost]
  2.         public ActionResult Signature(Guid id, string nameWincor, string signatureWincor, string nameCustomer,
  3.             string signatureCustomer, Guid masterCardId, bool? closeOverPhone, bool? closeOutsideCustomerSite, bool? closeZakazka, string outsideCustomerSiteComment,
  4.             string userLatitude = "0", string userLongitude = "0", bool? kauflandFormProcessed = null, bool? tmobileFormProcessed = null,
  5.             string vzdalenost = null, string cestovniCas = null, string adressStart = null, string adressEnd = null,
  6.             string[] travelOrderPartName = null, string[] travelOrderPartSerialNumber = null, string[] travelOrderPartStockNumber = null, Guid? partId = null, string copyCP = "0")
  7.  
  8.         {
  9.  
  10.             logger.DebugFormat("Začátek uzavírání ML přes AsmCards. CardId: {0},{1}", id, copyCP);
  11.  
  12.             Cards card = CardService.GetCardById(db, id);
  13.  
  14.             logger.DebugFormat("Uzavírání ML přes AsmCards. Číslo ML: {0}", card.Number);
  15.             ViewBag.Card = card;
  16.             CardViewModel cardViewModel = GetCardViewModelById(id);
  17.             ViewBag.IsCompleted = false;
  18.             ViewBag.IsClosed = CardCompletionService.IsCardClosed(id);
  19.  
  20.             ViewBag.IsCompletionAllowed = !ViewBag.IsClosed;
  21.             if (ViewBag.IsClosed)
  22.             {
  23.                 logger.DebugFormat("ML již uzavřen. Číslo ML: {0}", card.Number);
  24.                 ModelState.AddModelError("", "Tento montážní lístek je již uzavřen.");
  25.                 return View(cardViewModel);
  26.             }
  27.  
  28.             //validace Uzavřeno po telefonu, musí být i uzavřeno mimo výkon práce, naplnění řetězce "Uzavřeno po telefonu"
  29.             if (closeOverPhone == true)
  30.             {
  31.                 closeOutsideCustomerSite = true;
  32.                 outsideCustomerSiteComment = "Uzavřeno po telefonu";
  33.             }
  34.  
  35.             //validace Důvodů uzavřeno mimo výkon práce, kontrola naplneni retezce
  36.             if (closeOutsideCustomerSite == true)
  37.             {
  38.                 if (string.IsNullOrWhiteSpace(outsideCustomerSiteComment))
  39.                 {
  40.                     logger.DebugFormat("Chyba validace ML {0}: Důvod uzavření mimo výkon práce musí být uveden.", card.Number);
  41.                     ModelState.AddModelError("", "Důvod uzavření mimo výkon práce musí být uveden");
  42.                     return View(cardViewModel);
  43.                 }
  44.             }
  45.  
  46.             // kontrola na uzavírání ML pro Tesco
  47.             if (CardService.Tesco10(id, out bool IsTesco10, out string errorMessage1))
  48.             {
  49.                 if (IsTesco10)
  50.                 {
  51.                     signatureCustomer = "";
  52.                     nameCustomer = "";
  53.                     userLatitude = "0";
  54.                     userLongitude = "0";
  55.                 }
  56.             }
  57.             else
  58.             {
  59.                 logger.DebugFormat("Nepodařilo se zjistit z mailu  Tesco10%", errorMessage1);
  60.                 ModelState.AddModelError("", "Nepodařilo se zjistit z mailu  Tesco10%");
  61.                 return View(cardViewModel);
  62.             }
  63.  
  64.             // kontrola na uzavírání cestovního příkazu
  65.             if (CardService.TravelOrderWithouSpareParts(id, out bool IsTravelOrderWithout, out string errorMessage3))
  66.             {
  67.                 if (IsTravelOrderWithout)
  68.                 {
  69.                     OperatingService.WriteTimeAndDistanceTravelOrder(id, vzdalenost, cestovniCas, adressStart,
  70.                         adressEnd, out string errorMessage2);
  71.                     if (errorMessage2 != string.Empty)
  72.                     {
  73.                         ModelState.AddModelError("", errorMessage2);
  74.                     }
  75.                     string message;
  76.                     var result = ReplacementPartService.CreateTravelOrderReplacementParts(id, travelOrderPartName,
  77.                         travelOrderPartSerialNumber, travelOrderPartStockNumber, out message);
  78.                     if (message != string.Empty)
  79.                     {
  80.                         ModelState.AddModelError("", message);
  81.                         return View(cardViewModel);
  82.                     }
  83.                 }
  84.             }
  85.             else
  86.             {
  87.                 logger.DebugFormat("Nepodařilo se zjistit z mailu  TravelOrder.", card.Number);
  88.                 ModelState.AddModelError("", "Nepodařilo se zjistit z mailu  TravelOrder");
  89.                 return View(cardViewModel);
  90.             }
  91.             string newCardIdCP = "";
  92.             if (copyCP == "1")
  93.             {
  94.                 string newCardId;
  95.                 string errorMessageCP;
  96.                 // provedení kopie            
  97.                 if (AsmCardsService.CopyML(id, cardViewModel.UserId, out newCardId, out errorMessageCP))
  98.                 {
  99.                     TempData["Message"] = "Založení pokračovacího CP k původnímu provedeno";
  100.                     ModelState.AddModelError("", "");
  101.                     newCardIdCP = newCardId;
  102.                     //string messageCo;
  103.                     //var resultCoordinates = CardService.CopyTravelOrderEndCoordinatesToStart(id, new Guid(newCardId), out messageCo);
  104.                     //if (messageCo != string.Empty)
  105.                     //{
  106.                     //    ModelState.AddModelError("Startovací souřadnice se nenaplnily", messageCo);
  107.                     //    return View(cardViewModel);
  108.                     //}
  109.  
  110.                     string message;
  111.                     var result = ReplacementPartService.CopyTravelOrderReplacementParts(id,  new Guid(newCardId), out message);
  112.                     if (message != string.Empty)
  113.                     {
  114.                         ModelState.AddModelError("Převední ND se neprovedlo", message);
  115.                         return View(cardViewModel);
  116.                     }
  117.                 }
  118.                 else
  119.                 {
  120.                     logger.DebugFormat("Chyba - pokračovací CP neproveden,selhání služby", errorMessageCP);
  121.                     ModelState.AddModelError("", "Chyba - pokračovací CP neproveden,selhání služby");
  122.                     return View(cardViewModel);
  123.                 }
  124.  
  125.             }
  126.  
  127.  
  128.             // kontrola na oprávnění pro uzavření ML
  129.             string currentUserName = Membership.GetUser().UserName;
  130.             bool? canSign = db.Users.First(u => u.AccountName == currentUserName).canSign;
  131.             if (canSign != true)
  132.             {
  133.                 logger.DebugFormat("Uživatel nemá oprávnění k uzavření montážního lístku. Číslo ML: {0}", card.Number);
  134.                 ModelState.AddModelError("", "Uživatel nemá oprávnění k uzavření montážního lístku.");
  135.                 return View(cardViewModel);
  136.             }
  137.  
  138.             // kontrola na neodeslaný vadný ND
  139.             int defect = (int)ReplacementPartFlags.Defect;
  140.             int senddefect = (int)ReplacementPartFlags.DefectWN;
  141.             var isDefect = db.ReplacementParts.FirstOrDefault(r => r.CardId == id && ((r.Flags & defect) != 0) && ((r.Flags & senddefect) == 0));
  142.  
  143.             if (isDefect != null)
  144.             {
  145.                 logger.DebugFormat("Montážní lístek obsahuje neodeslaný vadný ND! Odešlete nebo stornujte !! Číslo ML: {0}", card.Number);
  146.                 ModelState.AddModelError("", "Montážní lístek obsahuje neodeslaný vadný ND!");
  147.                 return View(cardViewModel);
  148.             }
  149.  
  150.             // kontrola na neodeslaný požadavek na ND
  151.             int demand = (int)ReplacementPartFlags.Demand;
  152.             int senddemand = (int)ReplacementPartFlags.DemandSend;
  153.             var isDemand = db.ReplacementParts.FirstOrDefault(r => r.CardId == id && ((r.Flags & demand) != 0) && ((r.Flags & senddemand) == 0));
  154.  
  155.             if (isDemand != null)
  156.             {
  157.                 logger.DebugFormat("Montážní lístek obsahuje neodeslaný požadavek na ND! Odešlete nebo stornujte !! Číslo ML: {0}", card.Number);
  158.                 ModelState.AddModelError("", "Montážní lístek obsahuje neodeslaný požadavek na ND!");
  159.                 return View(cardViewModel);
  160.             }
  161.  
  162.             // kontrola na nevyřízený požadavek na ND
  163.             //int demand = (int)ReplacementPartFlags.Demand;
  164.             //int senddemand = (int)ReplacementPartFlags.DemandSend;
  165.             var isDemandSendMl = db.ReplacementParts.FirstOrDefault(r => r.CardId == id &&  ((r.Flags & senddemand) != 0));
  166.  
  167.             if (isDemandSendMl != null)
  168.             {
  169.                 logger.DebugFormat("Montážní lístek obsahuje nevyřízený požadavek na ND! Vyměńte požadovaný díl zákazníkovi !! Číslo ML: {0}", card.Number);
  170.                 ModelState.AddModelError("", "Montážní lístek obsahuje nevyřízený požadavek na ND!");
  171.                 return View(cardViewModel);
  172.             }
  173.  
  174.             // uprava počátečního času práce vzhledem k času uzavření ML
  175.             bool success1 = OperatingService.UpdateStartTimeClose(id, out string message1);
  176.  
  177.             if (success1 == false)
  178.             {
  179.                 logger.DebugFormat("Nepodařilo se opravit počáteční čas práce technika !! Číslo ML: {0}", card.Number);
  180.                 ModelState.AddModelError("", "Nepodařilo se opravit počáteční čas práce technika !");
  181.                 return View(cardViewModel);
  182.             }
  183.  
  184.  
  185.             // Ukončení zakázky na tlačítko - nebude se v zakázce pokračovat
  186.             if (closeZakazka == true)
  187.             {
  188.                 // Nový dojezd se bude počítat že k původnímu dojezdu přičtu standardní dojezd k zákazníkovi
  189.                 // Smazání položek zakázky následující za vybraným ML
  190.                 if (!OrderService.ClearNextOrderItem(id))
  191.                 {
  192.                     logger.DebugFormat("Nepodařilo se smazat následující položky zakázky. Číslo ML: {0}", card.Number);
  193.                     ModelState.AddModelError("", string.Format("Nepodařilo se smazat položky zakázky za ML číslo: {0}.", card.Number));
  194.                     return View(cardViewModel);
  195.                 }
  196.             }
  197.  
  198.             // přístupové údaje do AsmCards
  199.             string loginName = ConfigurationManager.AppSettings["AsmCardsLoginName"];
  200.             string password = ConfigurationManager.AppSettings["AsmCardsPassword"];
  201.  
  202.             // definice proměnné
  203.             bool isInCustomerLocation = true;
  204.             bool customerCoordinatesExist = cardViewModel.CustomersWgsw > 0 && cardViewModel.CustomersWgsl > 0;
  205.  
  206.             string separator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
  207.             if (!userLatitude.Contains(separator))
  208.             {
  209.                 userLatitude = userLatitude.Replace(".", separator).Replace(",", separator);
  210.                 userLongitude = userLongitude.Replace(".", separator).Replace(",", separator);
  211.             }
  212.  
  213.             // kontrola souradnic
  214.             if (customerCoordinatesExist)
  215.             {
  216.                 // ziskani cisla auta pro web dispecink
  217.                 Guid userId = UserService.GetUserId(id);
  218.                 int carId = UserService.GetWdCarId(userId);
  219.  
  220.                 double distanceInM;
  221.  
  222.                 try
  223.                 {
  224.                     CustomerShop customerShop = card.CustomerShopId == null ? null : CustomerService.GetCutomerShopById(card.CustomerShopId);
  225.  
  226.                     // kontrola polohy podle souřadnic provozovny
  227.                     if (customerShop != null && customerShop.wgsw != null && customerShop.wgsl != null)
  228.                     {
  229.                         isInCustomerLocation = CardService
  230.                             .CheckCurrentPositionWithCustomerLocationAndWritePositionToMl(cardViewModel.CardId, (double)customerShop.wgsw,
  231.                                 (double)customerShop.wgsl, carId, Convert.ToDecimal(userLatitude), Convert.ToDecimal(userLongitude), out distanceInM);
  232.  
  233.                     }
  234.                     // kontrola polohy podle souřadnic z ML
  235.                     else
  236.                     {
  237.                         isInCustomerLocation = CardService
  238.                             .CheckCurrentPositionWithCustomerLocationAndWritePositionToMl(cardViewModel.CardId, cardViewModel.CustomersWgsw,
  239.                                 cardViewModel.CustomersWgsl, carId, Convert.ToDecimal(userLatitude), Convert.ToDecimal(userLongitude), out distanceInM);
  240.                     }
  241.                 }
  242.                 catch { }
  243.             }
  244.  
  245.             // uzavření ML
  246.             string errorMessage = string.Empty;
  247.             bool completed = CardCompletionService.CloseViaAsmCards((Guid)Membership.GetUser().ProviderUserKey, loginName, password,
  248.                 cardViewModel.CustomerId, card.CardId, card.Number, nameCustomer, signatureCustomer, nameWincor, signatureWincor, masterCardId,
  249.                 closeOverPhone == true, closeOutsideCustomerSite == true, outsideCustomerSiteComment, isInCustomerLocation,
  250.                 Convert.ToDecimal(userLatitude), Convert.ToDecimal(userLongitude), out errorMessage);
  251.  
  252.             ViewBag.IsCompleted = completed;
  253.  
  254.             // zpracování chyb
  255.             if (!completed && errorMessage == string.Empty)
  256.             {
  257.                 logger.DebugFormat("Neznámá chyba při uzavírání ML. Číslo ML: {0}", card.Number);
  258.                 ModelState.AddModelError("", "Chyba při uzavírání ML.");
  259.             }
  260.             else if (errorMessage != string.Empty)
  261.             {
  262.                 logger.DebugFormat("{0} Číslo ML: {1}", errorMessage, card.Number);
  263.                 ModelState.AddModelError("", errorMessage);
  264.             }
  265.  
  266.             // odeslání mailu s informací o závadě náhradního dílu
  267.             string sendDefectDescriptionToMail = ReplacementPartService.SparePartDefectDescriptionGoToMail(card.CardId, (Guid)Membership.GetUser().ProviderUserKey, cardViewModel.CustomerId, card.Number);
  268.             if (sendDefectDescriptionToMail != string.Empty)
  269.             {
  270.                 logger.DebugFormat("Chyba při odesílání mailu s informací o závadě ND");
  271.                 ModelState.AddModelError("", sendDefectDescriptionToMail);
  272.             }
  273.  
  274.             if (copyCP == "1")
  275.             {
  276.                 string messageCo;
  277.                 var resultCoordinates = CardService.CopyTravelOrderEndCoordinatesToStart(id, new Guid(newCardIdCP), out messageCo);
  278.                 if (messageCo != string.Empty)
  279.                 {
  280.                     logger.DebugFormat("Chyba - Startovací souřadnice se nenaplnily", errorMessage);
  281.                     ModelState.AddModelError("Startovací souřadnice se nenaplnily", messageCo);
  282.                 }
  283.             }
  284.  
  285.  
  286.             if (ModelState.IsValid)
  287.             {
  288.                 bool IsTravelOrder;
  289.                 if (CardService.TravelOrder(card.CardId, out IsTravelOrder, out errorMessage))
  290.                 {
  291.                     if (IsTravelOrder)
  292.                     {
  293.                         isInCustomerLocation = true;
  294.                     }
  295.  
  296.                 }
  297.                 else
  298.                 {
  299.                     logger.DebugFormat("Nepodařilo se zjistit z mailu  TravelOrder", errorMessage);
  300.                     ModelState.AddModelError("", "Nepodařilo se zjistit z mailu  TravelOrder");
  301.                 }
  302.  
  303.                 Guid customerId = CustomerService.IdCustomer(id);
  304.                 string customerName = CustomerService.NameCustomer(customerId);
  305.                 if (isInCustomerLocation == false)
  306.                 {
  307.                     Guid userId = UserService.GetUserId(id);
  308.                     if (userId == new Guid(ConfigurationManager.AppSettings["UserOutLocal"]))
  309.                     {
  310.                         string userName = UserService.NameServicemen(userId);
  311.                         string phoneloc = (ConfigurationManager.AppSettings["PhoneNumbersLoc"]);
  312.                         string text = "Technik " + userName + " uzavřel ML  " + card.Number + " mimo lokalitu zákazníka " + customerName + " !!";
  313.                         CardService.WriteSendSMSMessage(phoneloc, text);
  314.                     }
  315.  
  316.                 }
  317.  
  318.                 TempData["DataUrl"] = "data-url=/Card";
  319.                 logger.DebugFormat("Konec uzavírání ML přes AsmCards. isInCustomerLocation={0}", isInCustomerLocation);
  320.  
  321.                 var currentuserId = (Guid)Membership.GetUser().ProviderUserKey;
  322.                 string action = card.UserId != currentuserId ? "UserCards" : "Index";
  323.  
  324.                 return RedirectToAction(action, "Card", new { closed = card.Number, isLocation = (isInCustomerLocation ? "yes" : "no") });
  325.             }
  326.  
  327.             logger.DebugFormat("Konec uzavírání ML přes AsmCards. isInCustomerLocation={0}", isInCustomerLocation);
  328.  
  329.             return View(cardViewModel);
  330.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement