oleh Vincy. Terakhir diubah pada 10 Maret 2021.
Faktur adalah dokumen komersial yang diberikan oleh penjual (atau penyedia layanan) kepada penerima yang merinci transaksi. Itu harus memiliki rincian tentang item, kuantitas, jumlah, diskon, pajak, pengiriman, syarat pembayaran dan informasi lebih lanjut.
Jika Anda menjalankan toko eCommerce, dengan setiap pembelian Anda harus mengirim faktur, dengan opsi untuk mengekspor sebagai PDF akan bagus. Ini adalah fitur yang memungkinkan Anda meningkatkan kehadiran merek Anda.
Ada banyak generator faktur yang tersedia secara online gratis. Ketika Anda dapat membuatnya sendiri dengan cepat, lalu mengapa Anda harus pergi ke tempat lain? Artikel ini akan membantu Anda membuat faktur pembelian eCommerce dengan template yang rapi dan bersih.
Pembuatan faktur pembelian adalah bagian dari perangkat lunak aplikasi eCommerce. Ini adalah dokumen atau tagihan yang mengkonfirmasi transaksi antara penjual dan pelanggan. Ini adalah persyaratan hukum di banyak negara.
Kode ini akan menghasilkan faktur dalam format PDF. Kami telah melihat cara menghasilkan PDF menggunakan skrip PHP.
Apa yang ada di dalam?
- Tentang contoh ini
- pesanan pembelian eCommerce dengan tautan pembuatan faktur
- Kode PHP untuk menghasilkan PDF faktur
- Skrip basis data
- Faktur pembelian eCommerce keluaran PDF
Tentang contoh ini
Contoh ini menunjukkan daftar pesanan pembelian di halaman arahan. Data pesanan ini bersifat dinamis dari database.
Ini memiliki data pembelian dengan kontrol HTML untuk memicu pembuatan faktur.
Dokumen faktur keluaran yang dibuat oleh contoh ini dalam format PDF. Ini menggunakan perpustakaan TCPDF PHP untuk menghasilkan PDF.
Unduh TCPDF dan masukkan ke dalam folder aplikasi. Tentukan jalur file kelas TCPDF di PDFService.php
Kode pembuatan PDF membuat penangan penulisan dokumen PDF. Ini mengatur header dokumen, judul, isi isi dan banyak lagi.
Ini mempersiapkan HTML untuk konten tubuh ke faktur yang akan dihasilkan. HTML ini menyematkan data pembelian dari database.
pesanan pembelian eCommerce dengan tautan pembuatan faktur
Ini adalah kode HTML untuk halaman arahan untuk menampilkan daftar pesanan pembelian.
Daftar pesanan ini menampilkan data pembelian berdasarkan baris seperti nama produk, jumlah, tanggal pembelian, dan lainnya.
Ini berisi tautan pembuatan faktur di setiap baris. Saat mengklik tautan ini, PDF faktur pembelian akan terbuka di tab baru.
Rincian pesanan berasal dari database. Ini menggunakan pengidentifikasi unik sebagai referensi untuk setiap pembelian.
index.php
<?php use PhppotOrder; require_once __DIR__ . '/Model/Order.php'; $orderModel = new Order(); $orderResult = $orderModel->getAllOrders(); ?> <html> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSRF Protection using PHP</title> <link rel="stylesheet" type="text/css" href="https://phppot.com/php/generate-ecommerce-purchase-invoice-pdf-using-php-script/assets/css/style.css" /> </head> <body> <div class="phppot-container"> <h1 class="page-heading">Generate PDF invoice with FPDF</h1> <table class="table"> <thead class="thead-dark"> <tr> <th scope="col">Order Ref</th> <th scope="col" class="text-right">Amount</th> <th scope="col" class="text-right">Order Status</th> <th scope="col" class="text-right">Invoice</th> </tr> </thead> <tbody> <?php foreach ($orderResult as $k => $v) { ?> <tr> <td><?php echo $orderResult[$k]["order_ref"];?></td> <td class="text-right"><?php echo $orderResult[$k]["amount"];?></td> <td class="text-right"><?php echo $orderResult[$k]["order_status"];?></td> <td class="text-right"><a target="_blank" title="Generate Invoice" href="./invoice.php?id=<?php echo $orderResult[$k]["id"];?>"><?php echo $orderResult[$k]["order_invoice"];?></a></td> </tr> <?php }?> </tbody> </table> </div> </body> </html>
Templat faktur PDF
Saat mengklik tautan pembuatan faktur, kode PHP mengintegrasikan PDF faktur pembelian eCommerce.
Kode ini menunjukkan template HTML untuk menghasilkan PDF.
Template/template-faktur-pembelian.php
<?php use PhppotOrder; require_once __DIR__ . '/../Model/Order.php'; function getHTMLPurchaseDataToPDF($result, $orderItemResult, $orderedDate, $due_date) { ob_start(); ?> <html> <head>Receipt of Purchase - <?php echo $result[0]["order_invoice"]; ?> </head> <body> <div style="text-align:right;"> <b>Sender:</b> Phppot </div> <div style="text-align: left;border-top:1px solid #000;"> <div style="font-size: 24px;color: #666;">INVOICE</div> </div> <table style="line-height: 1.5;"> <tr><td><b>Invoice:</b> #<?php echo $result[0]["order_invoice"]; ?> </td> <td style="text-align:right;"><b>Receiver:</b></td> </tr> <tr> <td><b>Date:</b> <?php echo $orderedDate; ?></td> <td style="text-align:right;"><?php echo $result[0]["customer_first_name"] . ' ' . $result[0]["customer_last_name"]; ?></td> </tr> <tr> <td><b>Payment Due:</b><?php echo $due_date; ?> </td> <td style="text-align:right;"><?php echo $result[0]["customer_company"]; ?></td> </tr> <tr> <td></td> <td style="text-align:right;"><?php echo $result[0]["customer_address"]; ?></td> </tr> </table> <div></div> <div style="border-bottom:1px solid #000;"> <table style="line-height: 2;"> <tr style="font-weight: bold;border:1px solid #cccccc;background-color:#f2f2f2;"> <td style="border:1px solid #cccccc;width:200px;">Item Description</td> <td style = "text-align:right;border:1px solid #cccccc;width:85px">Price ($)</td> <td style = "text-align:right;border:1px solid #cccccc;width:75px;">Quantity</td> <td style = "text-align:right;border:1px solid #cccccc;">Subtotal ($)</td> </tr> <?php $total = 0; $productModel = new Order(); foreach ($orderItemResult as $k => $v) { $price = $orderItemResult[$k]["item_price"] * $orderItemResult[$k]["quantity"]; $total += $price; $productResult = $productModel->getProduct($orderItemResult[$k]["product_id"]); ?> <tr> <td style="border:1px solid #cccccc;"><?php echo $productResult[0]["product_title"]; ?></td> <td style = "text-align:right; border:1px solid #cccccc;"><?php echo number_format($orderItemResult[$k]["item_price"], 2); ?></td> <td style = "text-align:right; border:1px solid #cccccc;"><?php echo $orderItemResult[$k]["quantity"]; ?></td> <td style = "text-align:right; border:1px solid #cccccc;"><?php echo number_format($price, 2); ?></td> </tr> <?php } ?> <tr style = "font-weight: bold;"> <td></td><td></td> <td style = "text-align:right;">Total ($)</td> <td style = "text-align:right;"><?php echo number_format($total, 2); ?></td> </tr> </table></div> <p><u>Kindly make your payment to</u>:<br/> Bank: American Bank of Commerce<br/> A/C: 05346346543634563423<br/> BIC: 23141434<br/> </p> <p><i>Note: Please send a remittance advice by email to vincy@phppot.com</i></p> </body> </html> <?php return ob_get_clean(); } ?>
Kode PHP untuk menghasilkan PDF faktur
Di sisi server, kode PHP dalam file invoice.php mengambil data pesanan pembelian dari database.
Operasi terkait database berada di kelas DataSource aplikasi.
Setelah mengambil hasil, kode ini memanggil fungsi pembuatan faktur. Fungsi ini menerima data dinamis sebagai argumennya. Pada artikel sebelumnya, kita telah melihat cara menulis data MySQL ke dalam dokumen PDF.
Template faktur menunjukkan nomor faktur, tanggal dan jatuh tempo. Juga, ini menampilkan daftar barang yang dibeli dan detail pelanggan.
invoice.php
<?php use PhppotOrder; require_once __DIR__ . '/Model/Order.php'; $orderModel = new Order(); $result = $orderModel->getPdfGenerateValues($_GET["id"]); $orderItemResult = $orderModel->getOrderItems($result[0]["id"]); if (! empty($result)) { require_once __DIR__ . "/lib/PDFService.php"; $pdfService = new PDFService(); $pdfService->generatePDF($result, $orderItemResult); }
lib/PDFService.php
<?php use PhppotConfig; ini_set('display_errors', 'On'); error_reporting(E_ALL); require_once __DIR__ . '/../lib/Config.php'; $config = new Config(); class PDFService { function generatePDF($result, $orderItemResult) { require_once __DIR__ . '/../vendor/tcpdf/tcpdf.php'; $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); $pdf->SetHeaderData('', PDF_HEADER_LOGO_WIDTH, '', '', array( 0, 0, 0 ), array( 255, 255, 255 )); $pdf->SetTitle('Invoice - ' . $result[0]["order_invoice"]); $pdf->SetMargins(20, 10, 20, true); if (@file_exists(dirname(__FILE__) . '/lang/eng.php')) { require_once (dirname(__FILE__) . '/lang/eng.php'); $pdf->setLanguageArray($l); } $pdf->SetFont('helvetica', '', 11); $pdf->AddPage(); $orderedDate = date('d F Y', strtotime($result[0]["order_at"])); $due_date = date("d F Y", strtotime('+' . Config::TERMS . 'days', strtotime($orderedDate))); require_once __DIR__ . '/../Template/purchase-invoice-template.php'; $html = getHTMLPurchaseDataToPDF($result, $orderItemResult, $orderedDate, $due_date); $filename = "Invoice-" . $result[0]["order_invoice"]; $pdf->writeHTML($html, true, false, true, false, ''); ob_end_clean(); $pdf->Output($filename . '.pdf', 'I'); } } ?>
Skrip basis data
Skrip database ini memiliki data untuk pesanan pembelian dan tabel produk. Impor skrip ini untuk menjalankan program ini di lingkungan Anda.
sql/database.sql
-- -------------------------------------------------------- -- -- Table structure for table `tbl_order` -- CREATE TABLE `tbl_order` ( `id` int(11) NOT NULL, `order_ref` varchar(255) NOT NULL, `order_invoice` int(11) NOT NULL, `customer_first_name` varchar(255) NOT NULL, `customer_last_name` varchar(255) NOT NULL, `customer_address` varchar(255) NOT NULL, `customer_company` varchar(255) NOT NULL, `amount` decimal(10,2) NOT NULL, `order_status` varchar(100) NOT NULL, `order_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- -- Dumping data for table `tbl_order` -- INSERT INTO `tbl_order` (`id`, `order_ref`, `order_invoice`, `customer_first_name`, `customer_last_name`, `customer_address`, `customer_company`, `amount`, `order_status`, `order_at`) VALUES (1, '1iRxJtOtAlai4v6LEuGI', 786567, 'David', 'Richard', 'United States', 'Cloud Technologies', '3300.00', 'Completed', '2021-01-12 09:51:38'), (2, '1iRxJtOtAlai4v6LEuGR', 486231, 'William', 'George', 'United States', 'Smart Tech Info', '500.00', 'Completed', '2021-01-12 09:38:27'); -- -------------------------------------------------------- -- -- Table structure for table `tbl_order_items` -- CREATE TABLE `tbl_order_items` ( `id` int(11) NOT NULL, `order_id` int(10) UNSIGNED NOT NULL, `product_id` int(11) NOT NULL, `item_price` decimal(10,2) NOT NULL, `quantity` int(10) UNSIGNED NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Dumping data for table `tbl_order_items` -- INSERT INTO `tbl_order_items` (`id`, `order_id`, `product_id`, `item_price`, `quantity`) VALUES (1, 1, 1, '1500.00', 2), (2, 1, 2, '300.00', 1), (6, 2, 3, '500.00', 1); -- -------------------------------------------------------- -- -- Table structure for table `tbl_product` -- CREATE TABLE `tbl_product` ( `id` int(11) NOT NULL, `product_title` varchar(255) NOT NULL, `price` decimal(10,2) NOT NULL, `create_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Dumping data for table `tbl_product` -- INSERT INTO `tbl_product` (`id`, `product_title`, `price`, `create_at`) VALUES (1, 'FinePix Pro2 3D Camera', '1500.00', '2021-01-11 13:00:22'), (2, 'Luxury Ultra thin Wrist Watch', '300.00', '2021-01-11 13:00:34'), (3, 'Luxury Tv', '500.00', '2021-01-11 13:00:34'); -- -- Indexes for dumped tables -- -- -- Indexes for table `tbl_order` -- ALTER TABLE `tbl_order` ADD PRIMARY KEY (`id`); -- -- Indexes for table `tbl_order_items` -- ALTER TABLE `tbl_order_items` ADD PRIMARY KEY (`id`); -- -- Indexes for table `tbl_product` -- ALTER TABLE `tbl_product` ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `tbl_order` -- ALTER TABLE `tbl_order` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3; -- -- AUTO_INCREMENT for table `tbl_order_items` -- ALTER TABLE `tbl_order_items` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7; -- -- AUTO_INCREMENT for table `tbl_product` -- ALTER TABLE `tbl_product` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=31;
Faktur pembelian eCommerce keluaran PDF
Bagian ini menunjukkan tangkapan layar keluaran dari contoh ini.
Tangkapan layar berikut menunjukkan tampilan halaman arahan yang menampilkan daftar pesanan pembelian.
Output ini menunjukkan screenshot dokumen PDF faktur pembelian eCommerce di bawah ini.
Kesimpulan
Pada artikel ini, kami telah membuat contoh sederhana untuk membuat faktur pembelian eCommerce dalam format PDF.
Kami telah melihat bagaimana mengintegrasikan perpustakaan PDF PHP untuk menjalankan contoh ini.
Kode ini menggunakan database sebagai sumber untuk menghasilkan PDF dengan data dinamis. Akan sangat membantu untuk menggunakan kode ini dalam aplikasi Anda dengan mengubah database target. Hanya perlu sedikit penyesuaian dalam kode dalam kasus seperti itu.
Dokumen ini dapat disempurnakan lebih lanjut dengan lebih detail. Contoh: Penagihan, Detail pengiriman, dan lainnya.
Semoga, ini akan menjadi awal yang baik untuk membantu Anda menyiapkan PDF faktur pembelian secara terprogram.
Unduh
Kembali ke Atas
Source link