Example #1
0
        public async Task <IActionResult> Get(int id)
        {
            try
            {
                var result = await service.GetOrder(id);

                return(Ok(result));
            }
            catch (Exception ex)
            {
                return(BadRequest(new ErrorMessage(ex.Message)));
            }
        }
Example #2
0
        public async Task <ActionResult> OrderPembelianExcel(int id)
        {
            string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

            try
            {
                var pembelian = await _pembelianService.GetOrder(id);

                string fileName = $"Order Pembelian No:{pembelian.Nomor}.xlsx";

                using var workbook = new XLWorkbook();
                IXLWorksheet worksheet = workbook.Worksheets.Add("Order");
                worksheet.Cell(1, 1).Value = "ORDER PEMBELIAN";
                worksheet.Cell(2, 1).Value = $"Kepada : {pembelian.Supplier.Nama}";

                var range = worksheet.Range("A1:G1");
                range.Merge().Style.Font.SetBold().Font.FontSize = 16;

                var range2 = worksheet.Range("A2:G2");
                range2.Merge().Style.Font.SetBold().Font.FontSize = 14;

                var start = 4;
                worksheet.Cell(start, 1).Value = "Nomor";
                worksheet.Cell(start, 2).Value = "Article";
                worksheet.Cell(start, 3).Value = "Nama";
                worksheet.Cell(start, 4).Value = "Jumlah";
                worksheet.Cell(start, 5).Value = "Harga";
                worksheet.Cell(start, 6).Value = $"Discount";
                worksheet.Cell(start, 7).Value = "Total";

                var items = pembelian.Items.ToList();
                int nomor = 1;
                for (int index = 1; index <= items.Count; index++)
                {
                    var total = items[index - 1].Total;
                    worksheet.Cell(index + start, 1).Value = nomor;
                    worksheet.Cell(index + start, 2).Value = items[index - 1].Product.CodeArticle;
                    worksheet.Cell(index + start, 3).Value = items[index - 1].Product.Name + " | " + items[index - 1].Product.Size;
                    worksheet.Cell(index + start, 4).Value = items[index - 1].Quntity;
                    worksheet.Cell(index + start, 5).Value = items[index - 1].Price;


                    worksheet.Cell(index + start, 6).Value = total * 10 / 100;
                    worksheet.Cell(index + start, 7).Value = total - (total * 10 / 100);

                    worksheet.Cell(index + start, 5).Style.NumberFormat.Format = "0,000.00";
                    worksheet.Cell(index + start, 6).Style.NumberFormat.Format = "0,000.00";
                    worksheet.Cell(index + start, 7).Style.NumberFormat.Format = "0,000.00";
                    nomor++;
                }

                worksheet.Range($"A{start}:G{items.Count + start}").Style.Border.InsideBorder  = XLBorderStyleValues.Thin;
                worksheet.Range($"A{start}:G{items.Count + start}").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;

                foreach (var item in worksheet.ColumnsUsed())
                {
                    item.AdjustToContents();
                }


                worksheet.Cell(items.Count + 8, 1).Value  = "Hormat Kami";
                worksheet.Cell(items.Count + 12, 1).Value = "Elish";
                var rangeAsign = worksheet.Range($"A{items.Count + 8}:B{items.Count + 8}");
                rangeAsign.Merge().Style.Font.SetBold().Font.FontSize = 12;

                using var stream = new MemoryStream();
                workbook.SaveAs(stream);
                var content = stream.ToArray();
                return(File(content, contentType, fileName));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }