如何在控制器中的 laravel store 功能中进行保存和打印?

问题描述 投票:0回答:1

我有一个执行保存和打印的按钮。我已经可以在

store()
功能中进行保存,但不能进行打印。当我说打印时,它意味着打开传递数据的打印对话框。

这是我的按钮:

<button type="button" id="save-and-print" name="action_create" value="save_and_print">Save and print</button>
<button type="submit" name="action_create" value="save">Save</button>

ReceiveItemsController.php

public function index() {
        return view('receive-items.index', ['has_id' => false]);
    }

    public function indexWithId($product_id) {
        return view('receive-items.index', ['product_id' => $product_id, 'has_id' => true]);
    }

    public function create() {
        return view('receive-items.index');
    }

    public function store(Request $request) {  
        $allow_print = true;
        $product_ids = array_map('intval', $request->product_id);
        $qty_per_item = array_map('intval', $request->qty);

        ReceiveItems::create([
            'created_at' => Carbon::now()->timestamp,
            'type' => 'Receiving',
            'product_id' => $product_ids,
            'no_of_items' => $request->no_of_items,
            'qty_per_item' => $qty_per_item,
            'total' => $request->total,
            'payment' => 'Already paid'
        ]);


        $print_notification = array(
            'message' => 'Saved successfully! Printing now...',
            'alert-type' => 'success'
        );

        $success_notification = array(
            'message' => 'New products has been added successfully!',
            'alert-type' => 'success'
        );

        $receive_items = $this->printVoucher($product_ids, $qty_per_item);

        if ($request->post('action_create') == 'save_and_print') {
            return view('receive-items.voucher', ['receive_items' => $receive_items,'allow_print' => $allow_print]);
        } elseif ($request->get('action_create') == 'save') {
            return redirect()->route('receive-items.index')->with($success_notification);
        }
    }

public function printVoucher($product_ids, $qty_per_item) {
        $items = [];
        foreach($product_ids as $product_item_no => $product_id) {
            $products = Product::where('id', '=', $product_id); 
            $voucher_cost = $products->value('cost');

            $items[] = array(
                'product_item_no' => $product_item_no + 1,
                'product_id' => $product_id,
                'product_name' => $products->value('name'),
                'size' => $products->value('size'),
                'qty_addend' => $qty_per_item[$product_item_no],
                'voucher_cost' => $voucher_cost,
                'ext_cost' => number_format($voucher_cost * $qty_per_item[$product_item_no], 2),

            );
        }
        return $items;
    }

路线:

 Route::post('/print/voucher', function() { return view('receive-items.voucher'); });

voucher.blade.php

 <!DOCTYPE html>
<html lang="en">
    <head>
        <title>Voucher</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <style type="text/css" media="screen">
            * {
                font-family: Arial, Helvetica, sans-serif;
            }
            html, body{
                box-sizing: border-box;
                margin: 0;
                padding: 20px;
                height: 100%;
                font-size: 13px;
                font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
                line-height: 1.5;
            }
            h4 {
                 margin-top: 0;
                 margin-bottom: 0.5rem;
            }
            p {
                 margin-top: 0;
                 margin-bottom: 1rem;
            }
            strong {
                 font-weight: bolder;
            }
            img {
                 vertical-align: middle;
                 border-style: none;
            }
            table {
                 border-collapse: collapse;
            }
            th {
                 text-align: inherit;
            }
            h4, .h4 {
                 margin-bottom: 0.5rem;
                 font-weight: 500;
                 line-height: 1.2;
            }
            h4, .h4 {
                 font-size: 1.5rem;
            }
            .table {
                 width: 100%;
                 margin-bottom: 1rem;
                 color: #212529;
            }
            .table th, .table td {
                 padding: 0.75rem;
                 vertical-align: top;
                 border-top: 1px solid #dee2e6;
            }
            .table thead th {
                 vertical-align: bottom;
                 border-bottom: 2px solid #dee2e6;
            }
            .table tbody  tbody {
                 border-top: 2px solid #dee2e6;
            }
            /**
            * =utils
            **/
            .border-0 {
                border: none !important;
            }
            .mt-5 {
                 margin-top: 3rem !important;
            }
            .pr-0, .px-0 {
                 padding-right: 0 !important;
            }
            .pl-0, .px-0 {
                 padding-left: 0 !important;
            }
            .text-right {
                 text-align: right !important;
            }
            .text-center {
                 text-align: center !important;
            }
            .text-uppercase {
                 text-transform: uppercase !important;
            }
            /**
            * =layout
            **/
            .coupon {
                position: relative;
                display:  inline-block;
                overflow:  hidden;
                border-radius: 10px;
            }

            .coupon-con {
                position: relative;
                width: 324px;
                height: 210px;
            }

            .coupon-con::before {
                content: '';
                position: absolute;
                left: -210px;
                display: block;
                width: 20px;
                height: 20px;
                border-radius: 50%;
                border: 200px solid #eee;
                clip: rect(0, auto, 210px, auto);
            }

            .coupon-con::after {
                content: '';
                position: absolute;
                right: -210px;
                display: block;
                width: 20px;
                height: 20px;
                border-radius: 50%;
                border: 200px solid #eee;
                clip: rect(0, auto, 210px, auto);
            }

            .cover-top {
                background-color: #eee;
                position: absolute;
                top: 0;
                width: inherit;
                height: 75px;
                border-top-left-radius: 10px;
                border-top-right-radius: 10px;
            }

            .coupon-btm {
                position: relative;
                width: 324px;
                height: 110px;
            }

            .coupon-btm::before {
                content: '';
                position: absolute;
                top: -210px;
                left: -210px;
                display: block;
                width: 20px;
                height: 20px;
                border-radius: 50%;
                border: 200px solid #eee;
                clip: rect(210px, auto, auto, auto);
            }

            .coupon-btm::after {
                content: '';
                position: absolute;
                top: -210px;
                right: -210px;
                display: block;
                width: 20px;
                height: 20px;
                border-radius: 50%;
                border: 200px solid #eee;
                clip: rect(210px, auto, auto, auto);
            }

            .cover-btm {
                background-color: #eee;
                position: absolute;
                bottom: 0;
                width: inherit;
                height: 75px;
                border-bottom-left-radius: 10px;
                border-bottom-right-radius: 10px;
            }
        </style>
    </head>
    <body>
        <div class="wrap">
            <div class="coupon">
                <div class="coupon-con">
                    <div class="cover-top"></div>
                    <div style="position: absolute; z-index: 9999; width: 100%;">
                        <img src="{{ asset('custom') }}/img/logo.png" alt="logo" width="150" />
                        <table class="table">
                            <thead>
                                <th>Item</th>
                                <th>Qty.</th>
                                <th>Cost</th>
                                <th>Ext. Cost</th>
                            </thead>
                            <tbody>
                                {{-- Items --}}
                                @foreach($receive_items as $item)
                                <tr>
                                    <td class="pl-0">
                                        <center>{{ $item['product_item_no'] }}</center>
                                        <br />
                                        <center></center>{{ $item['product_name'] }}</center>
                                    </td>
                                    <td>
                                        <center>{{ $item['qty_addend'] }}</center>
                                    </td>
                                    <td>
                                        <center>{{ $item['voucher_cost'] }}</center>
                                    </td>
                                    <td>
                                        <center>{{ $item['ext_cost'] }}</center>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                            <tfoot>
                                <tr>
                                    <td><strong>Total</strong></td>
                                    <td><strong></strong></td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
                <div class="coupon-btm">
                    <div style="position: absolute; z-index: 9999; width: 100%;">
                        <table class="table">
                            <tbody>
                                <tr>
                                    <td class="border-0 pl-0" width="50%">
                                        <center><p><strong>Receiving Voucher</strong></p></center>
                                        <center><p>{{ $receive_items[0]['product_item_no'] }}</p></center>
                                    </td>
                                    <td class="border-0 pl-0" width="50%">
                                        <p><center><strong>Voucher Date</strong></center></p>
                                        <p><center></center></p>
                                    </td>
                                </tr>      
                            </tbody>
                        </table>
                    </div>
                    <div class="cover-btm"></div>
                </div>
            </div>
        </div>
    </body>
    @if($allow_print)
    <script type="text/javascript">
        (function() {
          window.print()
        })();
    </script>
    @endif
</html>

此代码仅使用

voucher.blade.php
数据打开
$received_items
,但不打开打印对话框。也许路线有问题?

非常感谢任何帮助。

php jquery ajax laravel routes
1个回答
1
投票

您似乎正在使用两个刀片文件。所以只需将

window.print()
添加到
receive-items.voucher.blade.php
如下

<script>
(function() {
  window.print()
})();
</script>

一旦 dom 加载,打印窗口就会打开。

其他信息

您也可以在下面做空您的点赞

if ($request->post('action_create') == 'save_and_print')
    return view('receive-items.voucher', ['receive_items' => $receive_items]);

  return redirect()->route('receive-items.index');
    

更新 由于要求打印

index.blade.php

将额外的变量传递给index.blade,以便您可以使用打印选项。像下面这样

public function index() {
   
$allowPrint = true; // Your logic goes here
        return view('receive-items.index', ['receive_items' => $receive_items,'allowPrint' => $allowPrint]);
   
}

index.blade.php

@if($allowPrint)
<script>
(function() {
  window.print()
})();
</script>
@endif
© www.soinside.com 2019 - 2024. All rights reserved.