psys / order-invoice-manager-bundle
创建和保存订单及其发票。使用自定义库导出发票。
v1.0.8
2024-08-25 19:59 UTC
Requires
- doctrine/doctrine-bundle: >=2.11
- doctrine/orm: >=2.17
- psys/utils: >=1.0.6
- twig/twig: >=3.10
README
当你需要创建和管理订单以及创建和导出发票,但你不是在运行典型的在线商店时,因此像Shopify、WooCommerce等库可能会过度。
安装
composer require psys/order-invoice-manager-bundle
1. 添加到 config/bundles.php
Psys\OrderInvoiceManagerBundle\PsysOrderInvoiceManagerBundle::class => ['all' => true],
2. 将配置文件中的内容(查看config文件夹)添加到您的配置文件中。
3. 运行
symfony console make:migration
symfony console doctrine:migrations:migrate
4. 通过运行以下命令初始化数据库设置
INSERT INTO oimb_settings (option, value) VALUES ('invoice_proforma_sequential_number', '1'); INSERT INTO oimb_settings (option, value) VALUES ('invoice_final_sequential_number','1');
5. 定义您导出发票的方法(可选)
您可以使用任何想要的库。此示例使用Mpdf。
namespace App\Lib; use Doctrine\ORM\EntityManagerInterface; use Psys\OrderInvoiceManagerBundle\Entity\Order; use Psys\OrderInvoiceManagerBundle\Model\InvoiceManager\InvoiceManager; use Symfony\Component\HttpFoundation\Response; use Twig\Environment; class MyInvoiceManager extends InvoiceManager { public function __construct ( private Environment $twig, private EntityManagerInterface $entityManager, ) { parent::__construct($entityManager); } public function createPDF (Order $order, $type, string $outputMode) { $html = $this->twig->render('invoice/index.html.twig', [ 'order' => $order, 'type' => $type, ]); $mpdf = new \Mpdf\Mpdf(); $mpdf->WriteHTML($html); if ($outputMode === 'HttpInline') { return $mpdf->OutputHttpInline(); } } }
6. 定义您自己的订单(强制)或产品(可选)类别
namespace App\Lib; enum MyOrderCategory :int { case FOO = 1; case BAR = 1; }
示例用法
创建新的订单及其形式发票
use Psys\OrderInvoiceManagerBundle\Entity\Invoice; use Psys\OrderInvoiceManagerBundle\Entity\InvoiceBuyer; use Psys\OrderInvoiceManagerBundle\Entity\InvoiceProforma; use Psys\OrderInvoiceManagerBundle\Entity\InvoiceSeller; use Psys\OrderInvoiceManagerBundle\Entity\Order; use Psys\OrderInvoiceManagerBundle\Entity\Product; use Psys\OrderInvoiceManagerBundle\Model\OrderManager\AmountType; use Psys\OrderInvoiceManagerBundle\Model\OrderManager\PaymentMode; use Psys\OrderInvoiceManagerBundle\Model\OrderManager\State; use App\Lib\MyInvoiceManager; public function create_order (OrderManager $orderManager, MyInvoiceManager $invoiceManager) { $ent_Order = new Order(); $ent_Order->setCategory(MyOrderCategory::FOO); $ent_Order->setPaymentMode(PaymentMode::BANK_ACCOUNT_REGULAR); $ent_Order->setPaymentModeBankAccount('5465878565/6556'); $ent_Order->setUser($this->getUser()); $ent_Order->setCreatedAt(new \DateTimeImmutable()); $ent_Order->setState(State::NEW); $ent_Order->addProducts( (new Product()) ->setName('Foo') ->setPriceVatIncluded(1599) // If not set, it will be automatically calculated from price exclusive of VAT ->setPriceVatExcluded(1300) // If not set, it will be automatically calculated from price inclusive of VAT ->setVatRate(21) ->setAmount(1) ->setAmountType(AmountType::ITEM) ); $ent_InvoiceProforma = (new InvoiceProforma()) ->setCreatedAt(new \DateTimeImmutable()) ->setDueDate(new \DateTimeImmutable('+14 days')); $invoiceManager->setSequentialNumber($ent_InvoiceProforma); $ent_InvoiceProforma->setReferenceNumber(date('Y').$ent_InvoiceProforma->getSequentialNumber()); // Use custom formatting for the reference number $ent_Invoice = (new Invoice()) ->setInvoiceProforma($ent_InvoiceProforma) ->setInvoiceBuyer ( (new InvoiceBuyer()) ->setName('Some Buyer') ->setStreetAddress1('Street 123') ->setCity('Some City') ->setPostcode('25689') ->setVatIdentificationNumber('5468484') ->setCompanyIdentificationNumber('5655') ) ->setInvoiceSeller ( (new InvoiceSeller()) ->setName('Some Seller') ->setStreetAddress1('Street 123') ->setCity('Some City') ->setPostcode('25689') ->setVatIdentificationNumber('5468484') ->setCompanyIdentificationNumber('5655') ); $invoiceManager->setUniqueVariableSymbol($ent_Invoice); $ent_Order->setInvoice($ent_Invoice); $orderManager->processAndSaveNewOrder($ent_Order); }
重置序列号
use App\Lib\MyInvoiceManager; public function reset_sequential_numbers (MyInvoiceManager $invoiceManager) { $invoiceManager->resetSequentialNumbersEveryYear(); // Premade for cron. This cron needs to be run 1 to 10 minutes before a new year. $invoiceManager->resetSequentialNumbers(); // Use for resetting sequential numbers whenever you want }