PHP实现打印出库单,有没有实现过?

PHP实现打印出库单,有没有实现过?

https://mp.weixin.qq.com/s/X3JldplICRq7KR0HNFcpuw

背景

有时候你在实现一个出库订单之类的功能模块,这里也有可能要你的站点也实现相应的打印出库单预览,今天给大家分享用laravel(TP也行),PHP原生的也行。有需要的可以学习学习

PHP实现打印出库单,有没有实现过?

源码实现,php原生的话,需要include相关的print文件

 

laravel,首先先引入print插件包,我是放在app目录里

PHP实现打印出库单,有没有实现过?

路由文件

Route::any('admin/outWares/{outWares}/printer', ['as'=> 'admin.outWares.printer', 'uses' => 'PrinterController@index']);

控制器文件 PrinterController

public function index($id)
    {

        $outWare = $this->outWareRepository->findWithoutFail($id);
        $since = $outWare->outWareSince;
        if (empty($outWare)) {
            Flash::error('OutWare not found');
            return redirect(route('admin.outWares.index'));
        }

        //处理地址,当为自提时,地址为仓库的地址
        if($outWare->mode == 0){
            $province_name = count($outWare->regionProvince->where('region_type', 1)) ? current($outWare->regionProvince->where('region_type', 1))[0]->region_name : '';
            $city_name = count($outWare->regionCity->where('region_type', 2)) ? current(current($outWare->regionCity->where('region_type', 2)))->region_name : '';
            $district_name = count($outWare->regionDistrict->where('region_type', 3)) ? current(current($outWare->regionDistrict->where('region_type', 3)))->region_name : '';
            $address = $province_name.$city_name.$district_name.$outWare->address;
        }else{

            $province_name = count($outWare->ware->regionProvince->where('region_type', 1)) ? current($outWare->ware->regionProvince->where('region_type', 1))[0]->region_name : '';
            $city_name = count($outWare->ware->regionCity->where('region_type', 2)) ? current(current($outWare->ware->regionCity->where('region_type', 2)))->region_name : '';
            $district_name = count($outWare->ware->regionDistrict->where('region_type', 3)) ? current(current($outWare->ware->regionDistrict->where('region_type', 3)))->region_name : '';
            $address = isset($outWare->ware) ? $province_name.$city_name.$district_name.$outWare->ware->address : '';
        }

        $consignee = [];
        if($outWare->mode==0){
            $consignee = $outWare->customer_name;
            $consignee_phone = $outWare->customer_phone;
        }else{
            foreach($since as $so){
                $consignee[$so->consignee] = $so->consignee_phone;
            }
            list($keys, $values) = array_divide($consignee);
            if(count($keys) > 0){
                $consignee = implode('<br>',$keys);
                $consignee_phone = implode('<br>',$values);
            }else{
                $consignee = '';
                $consignee_phone = '';
            }

        }

        if($outWare->demand_time == '0000-00-00 00:00:00' || empty($outWare->demand_time)){
            $demand_time = '';
        }else{
            $demand_time = date('Y-m-d',strtotime($outWare->demand_time));
        }

        $out_ware_detail = $this->getWareDetail($outWare->outWareDetail);
        $data = [
            'out_sn'        => $outWare->out_sn,
            'ware'          => isset($outWare->ware) ? $outWare->ware->name : '',
            'company'       => isset($outWare->company) ? $outWare->company : '',
            'telephone'     => isset($outWare->ware) ? $outWare->ware->phone_1 : '',
            'consignor'     => isset($outWare->ware) ? $outWare->ware->director_1 : '',
            'consignee'         => $consignee,
            'consignee_phone'   => $consignee_phone,
            'remark'            => $outWare->remark,
            'demand_time'       => $demand_time,
            'created_at'        => $outWare->created_at->format('Y-m-d')
        ];
        $address = $this->getWareAddress($address);

        $this->TCPDF($data,$out_ware_detail,$address);
    }

一些数据的处理,这里只做参考

/**
     * Function:处理地址样式居中
     * User:wucy
     * @param $address
     * @return string
     */
    public function getWareAddress($address)
    {
        if(strlen($address) < 80){
            return <<<Eof
            <td rowspan="2" colspan="2" style="font-size: 16px;width: 455px;line-height:60px;">{
   $address}</td>
Eof;
        }else{
            return <<<Eof
            <td rowspan="2" colspan="2" style="font-size: 16px;width: 455px;">{
   $address}</td>
Eof;
        }
    }

    /**
     * Function:获取出库单商品详情
     * User:wucy
     * @param $outWareDetail
     * @return string
     */
    public function getWareDetail($outWareDetail)
    {
        $temp_row_data = [];
        $collection = collect($outWareDetail);
        $grouped = $collection->groupBy(function ($item, $key) {
            return $item['sku_id'];
        });

        $i=1;
        foreach ($grouped as $key => $item){
            $temp_row_data[$key] = [
                'key_num'    => $i++,
                'goods_name' => isset($item[0]->goodsSku) ? $item[0]->goodsSku->goods->goods_name : '--',
                'attr_name'  => isset($item[0]->goodsSku) ? $item[0]->goodsSku->value_name : '--',
                'goods_unit' => isset($item[0]->goodsSku) ? $item[0]->goodsSku->goods->goods_unit : '--',
                'total'      => abs($item->sum('goods_number')),
                'remark_detail'=>isset($item[0]) ? $item[0]->remark_detail : '--',
            ];
        }
        //dd($temp_row_data);

        if ($temp_row_data) {
            $item = '';
            foreach ($temp_row_data as $v) {
                $item.= $this->getRowsTable($v);
            }
            return $item;
        }

    }

    /**
     * Function:
     * User:wucy
     * @param $data
     * @return string
     */
    public function getRowsTable($data)
    {
        if($data){
            return <<<Eof
             <tr>
                <td style="font-size: 16px;text-align: center;">{
   $data['key_num']}</td>
                <td style="font-size: 16px;">{
   $data['goods_name']}</td>
                <td style="font-size: 16px;text-align: center;">{
   $data['attr_name']}</td>
                <td style="font-size: 16px;text-align: center;">{
   $data['goods_unit']}</td>
                <td style="font-size: 16px;text-align: center;">{
   $data['total']}</td>
                <td></td>
                <td></td>
                <td>{
   $data['remark_detail']}</td>
            </tr>    
Eof;
        }
    }

模板文件

/**
     * Function:TCPDF
     * User:wucy
     * @param $data
     * @param $out_ware_detail
     */
    public function TCPDF($data,$out_ware_detail,$address)
    {
        // create new PDF document
        $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

        // set document information
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor('仓库系统');
        $pdf->SetTitle('出库单');
        $pdf->SetSubject('TCPDF Tutorial');
        $pdf->SetKeywords('TCPDF, PDF, example, test, guide');

        // set default header data
        //$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 048', PDF_HEADER_STRING);

        // set header and footer fonts
        //$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        //$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
        $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(false);

        // set default monospaced font
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

        // set margins
        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

        // set auto page breaks
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

        // set image scale factor
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

        //$pdf->SetFont('stsongstdlight','', 14);
        $pdf->SetFont('droidsansfallback','', 14);


        // add a page
        $pdf->AddPage();
        $pdf->Write(0, '', '', 0, 'L', true, 0, false, false, 0);

        $pdf->setCellHeightRatio(1.3);
        $pdf->SetLineWidth(2);

        $tbl = <<<EOD
             <table cellpadding="0" cellspacing="0">
                <tr>
                    <th><img src="/adminResource/img/logo_5100.png" width="140" height="40"></th>
                    <th style="font-size: 25px;font-weight: bold">出库单</th>
                </tr>
             </table>
             <table cellpadding="0" cellspacing="0">
                <tr>
                    <td style="font-weight: bold">单据日期:{
   $data['created_at']}</td>
                    <td colspan="1"></td>
                    <td style="font-weight: bold;text-align: right;width:377px;">出库单号:{
   $data['out_sn']}</td>      
                </tr>
             </table>
             <table cellpadding="2" cellspacing="0" border="1" summary="出库单">
                    <tr>
                        <th style="font-size: 18px;width:80px;font-weight: bold;">发货仓</th>
                        <td style="font-size: 16px;width: 100px;">{
   $data['ware']}</td>
                        <th style="font-size: 18px;width:120px;font-weight: bold">收货公司</th>
                        <td style="font-size: 16px;width: 150px;">{
   $data['company']}</td>
                        <th rowspan="2" style="font-size: 18px;width:130px;font-weight: bold;line-height:60px;">提货/收货地址</th>
                        {
    $address}
                    </tr>

                    <tr>
                        <th style="font-size: 18px;font-weight: bold">发货人</th>
                        <td style="font-size: 16px;">{
   $data['consignor']}</td>
                        <th style="font-size: 18px;font-weight: bold">发货人电话</th>
                        <td style="font-size: 16px;">{
   $data['telephone']}</td>
                    </tr>

                    <tr>
                        <th style="font-size: 18px;font-weight: bold">提货人/收货人信息</th>
                        <td colspan="2" style="font-size: 16px;line-height:60px;">{
   $data['consignee']}</td>
                        <td style="font-size: 16px;line-height:60px;">{
   $data['consignee_phone']}</td>
                        <th style="font-size: 18px;font-weight: bold;line-height:60px;">要求配送时间</th>
                        <td colspan="2" style="font-size: 16px;line-height:60px;">{
   $data['demand_time']}</td>
                    </tr>

                    <tr>
                        <th style="font-size: 18px;font-weight: bold">订单备注</th>
                        <td colspan="6" style="font-size: 16px;">{
   $data['remark']}</td>
                    </tr>

                    <tr>
                        <th colspan="7" style="font-size: 18px;text-align: center;font-weight: bold">出库明细</th>
                    </tr>

                    <tr>
                        <td style="font-size: 18px;text-align: center;font-weight: bold;width:80px;">编号</td>
                        <td style="font-size: 18px;text-align: center;font-weight: bold;width:275px;">货品名称</td>
                        <td style="font-size: 18px;text-align: center;font-weight: bold;width:60px;">属性</td>
                        <td style="font-size: 18px;text-align: center;font-weight: bold;width:70px;">单位</td>
                        <td style="font-size: 18px;text-align: center;font-weight: bold;width:90px;">出货数量</td>
                        <td style="font-size: 18px;text-align: center;font-weight: bold;width:90px;">实发数量</td>
                        <td style="font-size: 18px;text-align: center;font-weight: bold;width:90px;">实收数量</td>
                        <td style="font-size: 18px;text-align: center;font-weight: bold;width:280px;">备注</td>
                    </tr>
                    {
    $out_ware_detail}
                    <tr>
                        <th style="font-size: 18px;font-weight: bold">签收人</th>
                        <td colspan="3"></td>
                        <th style="font-size: 18px;font-weight: bold">签收日期</th>
                        <td colspan="3"></td>
                    </tr>
                    <b>请签收人签字后务必将扫描件发至我司联系人邮箱,否则默认实收与实发数量一致</b>
                </table>
EOD;

        $pdf->writeHTML($tbl, true, false, false, false, '');

        // -----------------------------------------------------------------------------

        //Close and output PDF document
        $pdf->Output('出库单_'.date('YmdHis').'.pdf', 'I');
    }

全部文件都分享了,因为需求不一样,这里只做参考!

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/112907.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • rabbitmq使用mqtt协议[通俗易懂]

    rabbitmq使用mqtt协议[通俗易懂]提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录前言一、rabbitmq是什么?二、mqtt协议是什么?三、使用步骤1.引入库2.读入数据总结前言在网上学习物联网,发现有人可以用springboot+rabbitmq可以搭建物联网(IOT)平台,rabbitmq不是消息队列吗,原来rabbitmq有两种协议,消息队列是用的AMQP协议,而用在智能硬件中的是MQTT协议。一、rabbitmq是什么?示例:pandas是基于NumPy的一种工具,该工具是为了解

    2022年10月3日
    0
  • ThingsBoard——Docker重启失败,报错Connection to localhost:5432 refused的解决方法

    ThingsBoard——Docker重启失败,报错Connection to localhost:5432 refused的解决方法一、问题现在还没编译好thingsboard源代码,用的是docker搭建起来的环境。在写自定义节点,要打包好扔到docker里,再重启docker。后来发现经常重启失败,报错的错误也都是这样:2022-03-0508:53:23,164[main]ERRORcom.zaxxer.hikari.pool.HikariPool-HikariPool-1-Exceptionduringpoolinitialization.org.postgresql.util.PSQLExcepti

    2022年6月19日
    59
  • 移动端避免使用100vh[通俗易懂]

    移动端避免使用100vh[通俗易懂]CSS中的视口单位听起来很棒。如果要设置元素的样式以占据整个屏幕的高度,则可以设置height:100vh,您拥有一个完美的全屏元素,该元素会随着视口的变化而调整大小!可悲的是,事实并…

    2022年6月9日
    65
  • YUI Compressor插件压缩后war中的js/css文件未压缩的解决方法(被maven打包顶替了)

    YUI Compressor插件压缩后war中的js/css文件未压缩的解决方法(被maven打包顶替了)YUICompressorMaven插件可以压缩/合并js或css文件,经常用在Maven项目中,但最近发现在wabapp中执行了mvninstall命令进行发布之后,终端中显示插件已经执行了压缩的动作,但在输出文件夹或者war包中js和css文件都还是未压缩的原始文件。这样执行mvninstall命令之后发现虽然执行了压缩任务,但是在目标目录下和war包中的js和css…

    2022年7月18日
    20
  • floyed 算法

    floyed 算法/**floyed是用动态规划解决完全最短路的算法,一次调用即可得到任意两个点间的最短路径复杂度为O(n^3),适用于稠密图,顶点数一般在100以内适用结构简单,易于编写floyed算法还可解决传递闭包,判断图是否为连通图在解题时候一般不会只考floyed而是利用floyed得到的结果,进行下一步解题就像二分算法一样,提一

    2022年6月25日
    37
  • vim编辑页面怎么退出_如何退出Vim编辑器?[通俗易懂]

    vim编辑页面怎么退出_如何退出Vim编辑器?[通俗易懂]皈依舞在输入命令之前,击中ESC钥匙..进入后,按下回归来确认。ESC完成当前命令并将Vim切换到正常模式..如果你按下:,:将出现在底部屏幕上。这证实了您实际上是在输入命令而不是编辑文件。大多数命令都有缩略语,可选部分括在括号中:c[ommand].标记为‘*’的命令仅为Vim(未在Vi中实现)。安全-退出(如果有未保存的更改,则失败)::q[uit]退出电流窗户..如果这是最后一个窗口就退出V…

    2022年5月29日
    41

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号