예제 #1
0
        public IHttpActionResult Put(GetTermSignatureModel model)
        {
            var signatureData = String.Empty;

            var data = deliveryNoteSignaturesManager.GetEntities(o => o.TermId == model.id).FirstOrDefault();

            if (data != null)
            {
                signatureData = data.Signature;
            }

            return(Ok(new { SignatureData = signatureData }));
        }
예제 #2
0
        public IHttpActionResult Post([FromBody] ChangeStateTermModel model)
        {
            var term = termManager.GetById(model.termId);
            ClientTermViewModel result = null;

            var user = userManager.GetByLogin(model.Login);

            if (user != null && user.Token == model.Token && term != null)
            {
                term.User   = user;
                term.Status = model.status;

                switch ((TermStatusTypes)model.status)
                {
                case TermStatusTypes.BeginTrip:
                    term.BeginTripFromOffice = model.BeginTripFromOffice;
                    term.BeginTrip           = DateTime.Now;
                    break;

                case TermStatusTypes.EndTrip:
                    term.EndTrip = DateTime.Now;
                    break;

                case TermStatusTypes.BeginWork:
                    term.BeginWork = DateTime.Now;
                    break;

                case TermStatusTypes.CheckPositions:

                    var termPositions = term.TermPositions.Where(o => !o.DeleteDate.HasValue).ToList();
                    foreach (var position in model.Positions)
                    {
                        var termPosition = termPositions.FirstOrDefault(o => o.Id == position.Id);
                        termPosition.ProccessedAmount = position.ProccessedAmount;
                    }

                    termManager.SaveChanges();
                    break;

                case TermStatusTypes.CheckMaterials:

                    var termMaterials = term.TermPositions.Where(o => !o.DeleteDate.HasValue).SelectMany(o => o.TermPositionMaterialRsps).ToList();
                    foreach (var material in model.Materials)
                    {
                        var termMaterial = termMaterials.FirstOrDefault(o => o.Id == material.Id);
                        if (termMaterial != null)
                        {
                            termMaterial.Amount = material.Amount;
                        }
                    }

                    var materialPositions = term.Positions.Where(o => !o.DeleteDate.HasValue).ToList();
                    foreach (var material in model.Materials)
                    {
                        var position = materialPositions.FirstOrDefault(o => o.Id == material.Id);
                        if (position != null && material.Amount.HasValue)
                        {
                            position.Amount = material.Amount.Value;
                        }
                    }

                    termManager.SaveChanges();
                    break;

                case TermStatusTypes.EndWork:
                    term.EndWork = DateTime.Now;

                    var dataDirectory = Path.Combine(HttpRuntime.AppDomainAppPath, "App_Data");
                    var path          = Path.Combine(dataDirectory, Contracts.Configuration.DeliveryNoteFileName);

                    if (!model.sendDeliveryNotePerEmail)
                    {
                        var signature = deliveryNoteSignaturesManager.GetEntities(o => o.TermId == model.termId).FirstOrDefault();
                        if (signature != null)
                        {
                            signature.Signature = model.signature;
                        }
                        else
                        {
                            deliveryNoteSignaturesManager.AddEntity(new DeliveryNoteSignatures()
                            {
                                TermId    = model.termId,
                                Signature = model.signature,
                            });
                        }

                        deliveryNoteSignaturesManager.SaveChanges();
                    }

                    termMaterials = term.TermPositions.Where(o => !o.DeleteDate.HasValue).
                                    SelectMany(o => o.TermPositionMaterialRsps.Where(t => !t.DeleteDate.HasValue && t.Amount.HasValue)).ToList();

                    foreach (var termMaterial in termMaterials)
                    {
                        AutoMaterialHelper.CalculateUsedMaterial(termMaterial.Amount.Value, null, termMaterial);
                    }

                    termManager.SaveChanges();


                    var stream = printerManager.PrepareDeliveryNotePrintData(term.Id, path, termManager);

                    var fileName  = String.Format("Lieferscheine_{0}_{1}_{2}_{3}.pdf", term.Id, DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
                    var directory = Path.Combine(HttpRuntime.AppDomainAppPath, "Lieferscheine");
                    var filePath  = Path.Combine(directory, fileName);

                    if (!Directory.Exists(directory))
                    {
                        Directory.CreateDirectory(directory);
                    }

                    using (var fileStream = new FileStream(filePath, FileMode.Create))
                    {
                        stream.WriteTo(fileStream);

                        term.DeliveryNoteFileName = fileName;
                        termManager.SaveChanges();

                        var attachments = new List <Attachment>();
                        stream.Position = 0;
                        attachments.Add(new Attachment(stream, fileName));

                        if (model.sendDeliveryNotePerEmail)
                        {
                            SendMail(NotificationServerConfigSection.Instance.SmtpServer,
                                     NotificationServerConfigSection.Instance.SmtpServerAccountName,
                                     NotificationServerConfigSection.Instance.SmtpServerAccountPassword,
                                     term.Orders.Customers.Email,
                                     String.Format("Lieferschein {0}", DateTime.Now.ToShortDateString()),
                                     String.Format("<p>Sehr geehrte {0},</p><p>anbei der Lieferschein vom {1}.</p><p>Mit freundlichen Grüßen,</p><p>{2}</p>",
                                                   term.Orders.Customers.Name, DateTime.Now.ToShortDateString(), "Firma Zierer Gebäude & Systemtechnik"),
                                     NotificationServerConfigSection.Instance.SmtpServerPort,
                                     NotificationServerConfigSection.Instance.EnableSsl,
                                     (SmtpDeliveryMethod)NotificationServerConfigSection.Instance.SmtpDeliveryMethod, true,
                                     attachments);
                        }
                    }

                    break;

                case TermStatusTypes.BeginReturnTrip:
                    term.BeginReturnTrip = DateTime.Now;
                    break;

                case TermStatusTypes.EndReturnTrip:
                    term.EndReturnTrip = DateTime.Now;
                    break;
                }

                termManager.SaveChanges();

                if (term != null)
                {
                    result = TermViewModelHelper.ToModel(term, model.withPositions, model.withMaterials);
                }

                return(Ok(result));
            }

            return(BadRequest());
        }