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

网友投稿 1103 2022-09-02

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

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

背景

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

效果图

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

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

路由文件

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('
',$keys);

                $consignee_phone = implode('
',$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 <<

            

{$address}

            

Eof;         }else{

            return <<

            

{$address}

            

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 <<

             

                

{$data['key_num']}

                

{$data['goods_name']}

                

{$data['attr_name']}

                

{$data['goods_unit']}

                

{$data['total']}

                

                

                

{$data['remark_detail']}

            

    

             

                

                

                

                

                

                

                

                

            

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 = <<

                

                    

                    

                

             

             

                

                    

                    

                    

                

             

             

                    

                        

                        

                        

                        

                        

                        {$address}

                    

                    

                        

                        

                        

                        

                    

                    

                        

                        

                        

                        

                        

                    

                    

                        

                        

                    

                    

                        

                    

                    

                        

                        

                        

                        

                        

                        

                        

                        

                    

                    {$out_ware_detail}

                    

                        

                        

                        

                        

                    

                    请签收人签字后务必将扫描件发至我司联系人邮箱,否则默认实收与实发数量一致

                

EOD;

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

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

        //Close and output PDF document

        $pdf->Output('出库单_'.date('YmdHis').'.pdf', 'I');

    }

因为需求不一样,这里只做参考!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spark003-基础概念
下一篇:PyPackage01---Pandas14_对类别型变量做映射
相关文章

 发表评论

暂时没有评论,来抢沙发吧~