Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
5.3 kB
3
Indexable
Never
public function generatePdf($ticketId, $dateExpires = '', $orderId = null, $code = 'invalid-code', $couponId = null, $message = '', $securityHash = null){

        $model = Template::find()->where(['active' => 1])->one();
        $isCoupon = false;

        $ticketTypeId = ProductType::find()->where(['handle' => 'ticket'])->one()->id;
        $couponTypeId = ProductType::find()->where(['handle' => 'coupon'])->one()->id;

        $productService = new Products();
        $ticket = $productService->getProductById($ticketId,2);
        $coupon = null;

//        if ($ticket !== NULL && $ticket->typeId == $ticketTypeId || $ticket->typeId == $couponTypeId){
//            throw new HttpException('The product with the given id does not exists, or it is not a coupon');
//        }

        $ticketImageUrl = $ticket->ticketAsset->one()->getUrl();


        if ($model === NULL){
            throw new HttpException('There is no active template yet');
        }

        $hasOrder = false;
        $item = null;

        if (isset($orderId)){
            $order = Order::findOne($orderId);
            $lineItems = $order->lineItems;
            /** @var LineItem $lineItem */
            foreach ($lineItems as $lineItem){
                if(isset($lineItem->snapshot['productId']) && $ticketId == $lineItem->snapshot['productId']){
                    $hasOrder = true;
                    $model->text = strlen($lineItem->note) > 0 ? $lineItem->note : (strlen($message) > 0 ? $message : '');
                    $item = $lineItem;
                }
            }
        }else{
            $order = null;
        }

        $templateHandle = '';

        if ($ticket->typeId == $ticketTypeId){

            $templateHandle = 'ticket';
            $coupon = Purchased::findOne($couponId);
            $isCoupon = false;
            if (isset($coupon)){
                if ($coupon->securityHash !== $securityHash){
                    throw new ForbiddenHttpException('Odkaz, z ktorého sa pokúšate získať prístup k položke, je neplatný');
                }
                $code = $coupon->ticket_code;
            }
        }

        if ($ticket->typeId == $couponTypeId){
            $templateHandle = 'coupon';
            $coupon = Coupon::findOne($couponId);
            $isCoupon = true;
            if (isset($coupon)){
                if ($coupon->securityHash !== $securityHash){
                    throw new ForbiddenHttpException('Odkaz, z ktorého sa pokúšate získať prístup k položke, je neplatný');
                }
                $code = $coupon->code;
                $dateExpires = $coupon->dateExpires;
            }
        }

        if(strlen($message) > 0)
            $model->text = $message;

        $width = $ticket->ticketAsset->one()->getWidth();
        $height = $ticket->ticketAsset->one()->getHeight();
        $pxmm = 25.4/48;

        $widthmm = $width*$pxmm;
        $heightmm = $height*$pxmm;

        $scaleFactor = $width / 500;

//        $body = Craft::$app->view->renderString('{% block content %} '.$model->body.' {% endblock %}',[
//            'ticket' => $ticket,
//            'template' => $model,
//            'code' => $code,
//            'scaleFactor' => $scaleFactor,
//        ]);

        $response = $this->renderCustomTemplate("template/{$templateHandle}",[
            'ticket' => $ticket,
            'template' => $model,
            'code' => $code,
            'scaleFactor' => $scaleFactor,
            'coupon' => $coupon,
        ]);

        $body = $response->data;

        $html = $this->renderCustomTemplate('template/pdf',[
            'template' => $model,
            'ticket' => $ticket,
            'body' => $body,
            'ticketImageUrl' => $ticketImageUrl,
            'isCoupon' => $isCoupon,
            'isGift' => false,
            'hasCode' => true,
            'hasOrder' => $hasOrder,
            'order' => $order,
            'lineItem' => $item,
            'code' => $code,
            'dateExpires' => $dateExpires,
            'scaleFactor' => $scaleFactor,
        ]);

        $mpdf = new Mpdf([
            'fontDir' => [
                Craft::getAlias('@webroot/fonts')
            ],
            'fontdata' =>  [
                'montserrat' => [
                    'R' => 'Montserrat-Regular.ttf',
                ]
            ],
            'mode' => 'utf-8',
            'format' => [$heightmm,$widthmm],
            'orientation' => 'L',
//            'setAutoTopMargin' => 'stretch',
            'autoMarginPadding' => 0,
            'bleedMargin' => 0,
            'crossMarkMargin' => 0,
            'cropMarkMargin' => 0,
            'nonPrintMargin' => 0,
            'margBuffer' => 0,
            'collapseBlockMargins' => false,
            'margin_left' => 0,
            'margin_right' => 0,
            'margin_top' => 0,
            'margin_bottom' => 0,
            'margin_header' => 0,
            'margin_footer' => 0,
        ]);

        $mpdf->SetFont('Montserrat');

        $mpdf->SetDisplayMode('fullpage');
        $mpdf->WriteHTML($html->data);

        return $mpdf;

    }
Leave a Comment