public ActionResult GeneratePOD(Guid?id)
        {
            Proof pod = Db.Proofs
                        .Include(x => x.Assignment)
                        .Include(x => x.Assignment.Order)
                        .Include(x => x.Pictures)
                        .Include(x => x.Piano)
                        .Include(x => x.Piano.Statuses)
                        .FirstOrDefault(x => x.Id == id)
            ;

            string templateFormsPath = Path.Combine(UploadsFormsPath, pod.Assignment.Order.DeliveryForm);
            string podFileName       = "POD" + pod.Assignment.Order.OrderNumber + ".pdf";
            string podFilePath       = Path.Combine(DownloadsFormsPath, pod.Id.ToString() + ".pdf");

            string signature        = Path.Combine(SignPath, pod.Signature);
            string signatureResized = Path.Combine(SignPath, "1" + pod.Signature);

            if (!System.IO.File.Exists(signatureResized))
            {
                ImageResizer.Resize(signature, signatureResized, 150, 150, true);
            }

            PODFormsGenerater.GeneratePODForm(templateFormsPath, podFilePath, pod.ReceivedBy, pod.ReceivingTime?.ToString(), signatureResized);

            return(File(podFilePath, " application/octet-stream", podFileName));
        }
        public JsonResult <JsonResponse> SyncDeliver([FromBody] SyncDeliverModel model)
        {
            try
            {
                IsTokenValid(RequestTypeEnum.SyncDeliver);

                var assignment = db.Assignments
                                 .Include(x => x.Proofs)
                                 .Include(x => x.Order)
                                 .Include(x => x.Order.Pianos)
                                 .FirstOrDefault(x => x.Id.ToString() == model.assignmentId);

                if (assignment == null)
                {
                    throw new Exception("Assignment Not Found");
                }

                var order = db.Orders
                            .Include(x => x.Pianos)
                            .FirstOrDefault(x => x.Id == assignment.OrderId);

                if (order == null)
                {
                    throw new Exception("Assignment Order Not Found");
                }

                var piano = order.Pianos.FirstOrDefault(x => x.Id.ToString() == model.Id);

                if (piano == null)
                {
                    throw new Exception("Unit Not Found");
                }

                string receiverSignature, receiverSignatureFilePath;
                try
                {
                    receiverSignature         = $"{piano.Id.ToString()}_delivery.png";
                    receiverSignatureFilePath = $"{SignPath}{receiverSignature}";
                    ImageUtility.Base64ToImage(model.receiverSignature).Save(receiverSignatureFilePath);
                }
                catch (Exception ex)
                {
                    throw new Exception("Error saving sign " + ex.Message);
                }

                piano = db.Pianos.Include(x => x.Statuses).FirstOrDefault(x => x.Id.ToString() == model.Id);
                var pianoStatus = (PianoStatusEnum)System.Enum.Parse(typeof(PianoStatusEnum), model.pianoStatus);

                // Add statues
                if (!piano.Statuses.Any(x => x.Status == (int)pianoStatus))
                {
                    db.PianoStatuses.Add(new PianoStatus()
                    {
                        Id         = Guid.NewGuid(),
                        PianoId    = piano.Id,
                        CreatedAt  = DateTime.Now,
                        Status     = (int)pianoStatus,
                        Comments   = "Delivered",
                        StatusTime = DateTime.Now,
                        StatusBy   = StringConstants.System,
                    });
                }

                // Just save once
                var  proof = assignment.Proofs.FirstOrDefault(x => x.AssignmentId == assignment.Id && x.PianoId == piano.Id && x.ProofType == (int)ProofTypeEnum.Delivery);
                Guid id    = Guid.NewGuid();
                if (proof == null)
                {
                    db.Proofs.Add(new Proof()
                    {
                        Id        = id,
                        CreatedAt = DateTime.Now,
                        ProofType = (int)ProofTypeEnum.Delivery,

                        ReceivedBy    = model.receiverName,
                        Signature     = receiverSignature,
                        ReceivingTime = DateTime.ParseExact(model.deliveredAt, StringConstants.TimeStampFormatApp, CultureInfo.InvariantCulture),
                        Notes         = StringConstants.EncorePianoApp,

                        Bench1UnloadStatus       = model.bench1Unloaded == 1,
                        Bench2UnloadStatus       = model.bench2Unloaded == 1,
                        CasterCupsUnloadStatus   = model.casterCupsUnloaded == 1,
                        CoverUnloadStatus        = model.coverUnloaded == 1,
                        LampUnloadStatus         = model.lampUnloaded == 1,
                        OwnersManualUnloadStatus = model.ownersManualUnloaded == 1,
                        Misc1UnloadStatus        = model.misc1Unloaded == 1,
                        Misc2UnloadStatus        = model.misc2Unloaded == 1,
                        Misc3UnloadStatus        = model.misc3Unloaded == 1,
                        AssignmentId             = assignment.Id,
                        PianoId = piano.Id,
                    });
                }
                db.SaveChanges();

                if (!string.IsNullOrEmpty(order.DeliveryForm))
                {
                    BackgroundJob.Enqueue(() => PODFormsGenerater.GeneratePODForm(db, UploadsFormsPath, DownloadsFormsPath, SignPath, id));
                }

                return(Json(new JsonResponse()
                {
                    IsSucess = true, IsTokenValid = true
                }));
            }
            catch (Exception ex)
            {
                return(Json(new JsonResponse()
                {
                    IsSucess = false, ErrorMessage = ex.Message
                }));
            }
        }