public async Task <int> Create(GarmentShippingCreditNoteViewModel viewModel)
        {
            var model = MapToModel(viewModel);

            int Created = await _repository.InsertAsync(model);

            return(Created);
        }
        private GarmentShippingCreditNoteViewModel MapToViewModel(GarmentShippingNoteModel model)
        {
            GarmentShippingCreditNoteViewModel viewModel = new GarmentShippingCreditNoteViewModel
            {
                Active            = model.Active,
                Id                = model.Id,
                CreatedAgent      = model.CreatedAgent,
                CreatedBy         = model.CreatedBy,
                CreatedUtc        = model.CreatedUtc,
                DeletedAgent      = model.DeletedAgent,
                DeletedBy         = model.DeletedBy,
                DeletedUtc        = model.DeletedUtc,
                IsDeleted         = model.IsDeleted,
                LastModifiedAgent = model.LastModifiedAgent,
                LastModifiedBy    = model.LastModifiedBy,
                LastModifiedUtc   = model.LastModifiedUtc,

                noteNo      = model.NoteNo,
                date        = model.Date,
                description = model.Description,
                receiptNo   = model.ReceiptNo,
                receiptDate = model.ReceiptDate,
                buyer       = new Buyer
                {
                    Id   = model.BuyerId,
                    Code = model.BuyerCode,
                    Name = model.BuyerName
                },
                totalAmount = model.TotalAmount,

                items = (model.Items ?? new List <GarmentShippingNoteItemModel>()).Select(i => new GarmentShippingNoteItemViewModel
                {
                    Active            = i.Active,
                    Id                = i.Id,
                    CreatedAgent      = i.CreatedAgent,
                    CreatedBy         = i.CreatedBy,
                    CreatedUtc        = i.CreatedUtc,
                    DeletedAgent      = i.DeletedAgent,
                    DeletedBy         = i.DeletedBy,
                    DeletedUtc        = i.DeletedUtc,
                    IsDeleted         = i.IsDeleted,
                    LastModifiedAgent = i.LastModifiedAgent,
                    LastModifiedBy    = i.LastModifiedBy,
                    LastModifiedUtc   = i.LastModifiedUtc,

                    description = i.Description,
                    currency    = new Currency
                    {
                        Id   = i.CurrencyId,
                        Code = i.CurrencyCode
                    },
                    amount = i.Amount
                }).ToList()
            };

            return(viewModel);
        }
        private GarmentShippingNoteModel MapToModel(GarmentShippingCreditNoteViewModel viewModel)
        {
            var items = (viewModel.items ?? new List <GarmentShippingNoteItemViewModel>()).Select(i =>
            {
                i.currency = i.currency ?? new Currency();
                return(new GarmentShippingNoteItemModel(i.description, i.currency.Id.GetValueOrDefault(), i.currency.Code, i.amount)
                {
                    Id = i.Id
                });
            }).ToList();

            viewModel.buyer = viewModel.buyer ?? new Buyer();
            GarmentShippingNoteModel model = new GarmentShippingNoteModel(GarmentShippingNoteTypeEnum.CN, GenerateNo(), viewModel.date.GetValueOrDefault(), viewModel.buyer.Id, viewModel.buyer.Code, viewModel.buyer.Name, viewModel.description, viewModel.receiptNo, viewModel.receiptDate.GetValueOrDefault(), 0, null, null, viewModel.totalAmount, items);

            return(model);
        }
        public MemoryStream GeneratePdfTemplate(GarmentShippingCreditNoteViewModel viewModel, int timeoffset)
        {
            Font normal_font     = FontFactory.GetFont(BaseFont.COURIER, 10, Font.NORMAL);
            Font underlined_font = FontFactory.GetFont(BaseFont.COURIER, 10, Font.UNDERLINE);
            Font big_font        = FontFactory.GetFont(BaseFont.COURIER, 16, Font.BOLD);

            Document     document = new Document(PageSize.A4, 20, 20, 20, 20);
            MemoryStream stream   = new MemoryStream();
            PdfWriter    writer   = PdfWriter.GetInstance(document, stream);

            document.Open();

            var chunkHeader = new Chunk("PT. DAN LIRIS", big_font);

            chunkHeader.SetHorizontalScaling(1.5f);
            document.Add(new Paragraph(chunkHeader));

            PdfPTable tableHeadOffice = new PdfPTable(2);

            tableHeadOffice.SetWidths(new float[] { 1.5f, 6.5f });

            PdfPCell cellHeadOffice = new PdfPCell {
                Border = Rectangle.NO_BORDER
            };

            cellHeadOffice.Phrase = new Phrase("Head Office : ", normal_font);
            tableHeadOffice.AddCell(cellHeadOffice);
            cellHeadOffice.Phrase = new Phrase("Jl. Merapi No. 23, Kel. Banaran Kec. Grogol Kab. Sukoharjo\nTelp.(0271)714400, Fax.(0271)735222\ne-Mail:", normal_font);
            cellHeadOffice.Phrase.Add(new Chunk("*****@*****.**", underlined_font));
            tableHeadOffice.AddCell(cellHeadOffice);
            Chunk chunkAddress = new Chunk("MESSRS :\n" + viewModel.buyer.Name + "\n" + viewModel.buyer.Address, normal_font);

            chunkAddress.SetHorizontalScaling(0.8f);
            Phrase phraseBuyerHeader = new Phrase(chunkAddress);

            //phraseBuyerHeader.Add(new Chunk(new VerticalPositionMark()));
            //phraseBuyerHeader.Add(new Chunk("CREDIT NOTE", normal_font));

            phraseBuyerHeader.Add(new Chunk(new VerticalPositionMark()));
            phraseBuyerHeader.Add(new Chunk("DATE : " + viewModel.date.GetValueOrDefault().ToOffset(new TimeSpan(timeoffset, 0, 0)).ToString("dd MMMM yyyy", new System.Globalization.CultureInfo("en-EN")), normal_font));



            tableHeadOffice.AddCell(new PdfPCell
            {
                Border      = Rectangle.NO_BORDER,
                Colspan     = 2,
                PaddingLeft = 10f,
                Phrase      = phraseBuyerHeader
            });

            new PdfPCell(tableHeadOffice);
            tableHeadOffice.ExtendLastRow = false;
            tableHeadOffice.SpacingAfter  = 5f;
            document.Add(tableHeadOffice);

            document.Add(new Paragraph("CREDIT NOTE", big_font)
            {
                Alignment = Element.ALIGN_CENTER, SpacingAfter = 5f
            });

            document.Add(new Paragraph(viewModel.noteNo, big_font)
            {
                Alignment = Element.ALIGN_RIGHT, SpacingAfter = 5f
            });

            PdfPTable tableItems = new PdfPTable(2);

            tableItems.SetWidths(new float[] { 3f, 1f });

            tableItems.AddCell(new PdfPCell
            {
                Border = Rectangle.TOP_BORDER | Rectangle.BOTTOM_BORDER,
                HorizontalAlignment = Element.ALIGN_CENTER,
                Phrase = new Phrase("D e s c r i p t i o n", normal_font)
            });
            tableItems.AddCell(new PdfPCell
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER,
                HorizontalAlignment = Element.ALIGN_CENTER,
                Phrase = new Phrase("Amount", normal_font)
            });

            PdfPTable tableItemsContent = new PdfPTable(2);

            tableItemsContent.SetWidths(new float[] { 3f, 1f });

            tableItemsContent.AddCell(new PdfPCell
            {
                Border = Rectangle.NO_BORDER,
                Phrase = new Phrase(viewModel.description, normal_font)
            });

            tableItemsContent.AddCell(new PdfPCell
            {
                Border = Rectangle.LEFT_BORDER,
                Phrase = new Phrase(" ", normal_font)
            });

            foreach (var item in viewModel.items)
            {
                tableItemsContent.AddCell(new PdfPCell
                {
                    Border = Rectangle.NO_BORDER,
                    Phrase = new Phrase(item.description, normal_font)
                });
                Phrase phraseAmount = new Phrase();
                phraseAmount.Add(new Chunk(item.currency.Code, normal_font));
                phraseAmount.Add(new Chunk(new VerticalPositionMark()));
                phraseAmount.Add(new Chunk(item.amount.ToString("n"), normal_font));
                tableItemsContent.AddCell(new PdfPCell
                {
                    Border = Rectangle.LEFT_BORDER,
                    Phrase = phraseAmount
                });
            }

            PdfPCell pdfPCellItemsContent = new PdfPCell
            {
                Colspan       = 2,
                Padding       = 0,
                Border        = Rectangle.NO_BORDER,
                MinimumHeight = 400
            };

            new PdfPCell(tableItemsContent);
            pdfPCellItemsContent.AddElement(tableItemsContent);
            tableItems.AddCell(pdfPCellItemsContent);

            tableItems.AddCell(new PdfPCell
            {
                Border = Rectangle.TOP_BORDER,
                HorizontalAlignment = Element.ALIGN_RIGHT,
                PaddingRight        = 10,
                Phrase = new Phrase("TOTAL", normal_font)
            });
            Phrase phraseTotalAmount = new Phrase();

            phraseTotalAmount.Add(new Chunk(viewModel.items.FirstOrDefault().currency.Code, normal_font));
            phraseTotalAmount.Add(new Chunk(new VerticalPositionMark()));
            phraseTotalAmount.Add(new Chunk(viewModel.totalAmount.ToString("n"), normal_font));
            tableItems.AddCell(new PdfPCell
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER,
                Phrase = phraseTotalAmount
            });
            //tableItems.AddCell(new PdfPCell
            //{
            //    Colspan = 2,
            //    PaddingBottom = 10f,
            //    Border = Rectangle.NO_BORDER,
            //    Phrase = new Phrase("KETERANGAN   : " + viewModel.description, normal_font)
            //});
            string amountToText = "";

            amountToText = CurrencyToText.ToWords(Convert.ToDecimal(viewModel.totalAmount));
            if (viewModel.items.FirstOrDefault().currency.Code == "USD")
            {
                tableItems.AddCell(new PdfPCell
                {
                    Colspan       = 2,
                    PaddingBottom = 10f,
                    Border        = Rectangle.NO_BORDER,
                    Phrase        = new Phrase("SAY : US DOLLARS " + amountToText.ToUpper() + " ONLY ///", normal_font)
                                    //Phrase = new Phrase("SAY : US DOLLARS " + NumberToTextEN.toWords(viewModel.totalAmount).Trim().ToUpper() + " ONLY ///", normal_font)
                });
            }
            else
            {
                tableItems.AddCell(new PdfPCell
                {
                    Colspan       = 2,
                    PaddingBottom = 10f,
                    Border        = Rectangle.NO_BORDER,
                    Phrase        = new Phrase("TERBILANG : " + NumberToTextIDN.terbilang(viewModel.totalAmount).Trim().ToUpper() + " RUPIAH ///", normal_font)
                });
            }

            tableItems.AddCell(new PdfPCell
            {
                Border       = Rectangle.NO_BORDER,
                PaddingRight = 10f,
                Phrase       = new Phrase(" ", normal_font)
            });
            Phrase phraseSign = new Phrase();

            phraseSign.Add(new Chunk("S.E. & O\n" + viewModel.date.GetValueOrDefault().ToOffset(new TimeSpan(timeoffset, 0, 0)).ToString("dd MMMM yyyy", new System.Globalization.CultureInfo("en-EN")) + "\n\n\n\n", normal_font));
            Chunk chunkSignName = new Chunk("WAHYU PAMUNGKAS", normal_font);

            chunkSignName.SetUnderline(1, -1);
            phraseSign.Add(chunkSignName);
            phraseSign.Add(new Chunk("\nAUTHORIZED SIGNATURE", normal_font));
            tableItems.AddCell(new PdfPCell
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_CENTER,
                Phrase = phraseSign
            });

            new PdfPCell(tableItems);
            tableItems.ExtendLastRow = false;
            tableItems.SpacingAfter  = 5f;
            document.Add(tableItems);

            document.Close();
            byte[] byteInfo = stream.ToArray();
            stream.Write(byteInfo, 0, byteInfo.Length);
            stream.Position = 0;

            return(stream);
        }
        public async Task <int> Update(int id, GarmentShippingCreditNoteViewModel viewModel)
        {
            var model = MapToModel(viewModel);

            return(await _repository.UpdateAsync(id, model));
        }