]> _ Git - cubeextranet.git/commitdiff
Wait #5051 @10
authorstephen@cubedesigners.com <stephen@cubedesigners.com@f5622870-0f3c-0410-866d-9cb505b7a8ef>
Thu, 9 Jun 2022 15:55:09 +0000 (15:55 +0000)
committerstephen@cubedesigners.com <stephen@cubedesigners.com@f5622870-0f3c-0410-866d-9cb505b7a8ef>
Thu, 9 Jun 2022 15:55:09 +0000 (15:55 +0000)
inc/ws/Controlleur/class.ws.services.php

index 9f5485b1e884e2d86aafb98acb8232084d686a1e..10d0e83e3a077b85dc1245797f7e8184f99687f1 100644 (file)
@@ -1413,6 +1413,153 @@ class wsServices extends cubeFlashGateway
         die($res);
     }
 
+    // Endpoint for the CFOC Fluidbooks (custom cart)
+    public function CFOC() {
+        $this->outputXML = false;
+
+        if (empty($_POST['cart_items'])) {
+            die('Error: invalid data received');
+        }
+
+        $user_details = json_decode($_POST['user_details'], true);
+        $cart_items = json_decode($_POST['cart_items'], true);
+        $cart_totals = json_decode($_POST['cart_totals'], true);
+        $column_headings = json_decode($_POST['column_headings'], true);
+
+        $excel = new PHPExcel();
+        $excel->setActiveSheetIndex(0);
+        $defaultStyle = $excel->getDefaultStyle();
+        $defaultStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
+        $defaultStyle->getAlignment()->setWrapText(true);
+        $defaultStyle->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
+        $sheet = $excel->getActiveSheet();
+        $sheet->setTitle('Récapitulatif de commande');
+        $euro_currency_format = '#,##0.00 [$€-40C]'; // Used to format fields that contain currencies
+
+        // Setup column headings
+        $current_column_index = 0;
+        $current_row = 1;
+        foreach ($column_headings as $column_heading) {
+            $sheet->setCellValueByColumnAndRow($current_column_index, $current_row, $column_heading);
+            $cell_style = $sheet->getStyleByColumnAndRow($current_column_index, $current_row);
+            $cell_style->getFont()->setBold(true);
+            $cell_style->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_MEDIUM);
+            $current_column_index++;
+        }
+
+        // Output cart data
+        foreach ($cart_items as $cart_item) {
+            $current_row++;
+
+            // Look up the value by the column keys to ensure the correct order of values
+            foreach(array_keys($column_headings) as $column_index => $column_key) {
+                $sheet->setCellValueByColumnAndRow($column_index, $current_row, $cart_item[$column_key]);
+
+                $current_cell_style = $sheet->getStyleByColumnAndRow($column_index, $current_row);
+
+                if (in_array($column_key, ['PRIX HT', 'PRIX TTC'])) {
+                    $current_cell_style->getNumberFormat()->setFormatCode($euro_currency_format);
+                } elseif (in_array($column_key, ['QTE', 'QTE MINI'])) {
+                    $current_cell_style->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER);
+                }
+
+            }
+        }
+
+        // Add the cart summary
+        $current_row += 2; // Add an extra row gap before the summary
+        foreach ($cart_totals as $total_label => $total_value) {
+            $label_style = $sheet->getStyleByColumnAndRow(0, $current_row);
+            $label_style->getFont()->setBold(true);
+            $label_style->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
+            $label_style->getAlignment()->setWrapText(false);
+            $sheet->setCellValueByColumnAndRow(0, $current_row, $total_label . ' :');
+
+            $sheet->getStyleByColumnAndRow(1, $current_row)->getNumberFormat()->setFormatCode($euro_currency_format);
+            $sheet->setCellValueByColumnAndRow(1, $current_row, $total_value);
+
+            $current_row++;
+        }
+
+
+        // Autosize columns once the data has been added
+        for ($i = 0; $i < count($column_headings); $i++) {
+            $sheet->getColumnDimensionByColumn($i)->setAutoSize(true);
+        }
+
+        //================================
+        // Add user details in a new sheet
+        // The sheet must be added to the workbook before we can access the cell styles, otherwise it fails silently :(
+        $details_sheet = $excel->addSheet(new PHPExcel_Worksheet($excel, 'Coordonnées'), 0); // Setting index to zero will put this sheet first in the workbook
+
+        $current_row = 1;
+        foreach ($user_details as $user_detail) {
+            // User details are displayed as Label: Value in the A & B columns
+            $details_label_style = $details_sheet->getStyleByColumnAndRow(0, $current_row);
+            $details_label_style->getFont()->setBold(true);
+            $details_label_style->getAlignment()->setWrapText(false);
+            $details_label_style->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
+            $details_sheet->setCellValueByColumnAndRow(0, $current_row, $user_detail['label'] . ' :');
+
+            $details_sheet->setCellValueByColumnAndRow(1, $current_row, $user_detail['value']);
+            $current_row++;
+        }
+
+        // Auto-size both columns
+        $details_sheet->getColumnDimensionByColumn(0)->setAutoSize(true);
+        $details_sheet->getColumnDimensionByColumn(1)->setAutoSize(true);
+
+        //==== Save the Excel spreadsheet to disk temporarily
+        // $writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007'); // The .xlsx extension seems to prevent the e-mail from delivering...
+        $writer = PHPExcel_IOFactory::createWriter($excel, 'Excel5'); // Older .xls format is fine for our needs
+        $xls_file = CubeIT_Files::tempnam();
+        $writer->save($xls_file);
+
+        //==== Prepare the e-mail
+        $email_to = [
+            'pro@cfoc.com',
+            'e.destraschnov@cfoc.com',
+        ];
+        $email_cc = [
+            $user_details['email']['value'],
+        ];
+        $return_address = 'postmaster@fluidbook.com';
+        $email_from = "CFOC <$return_address>";
+        $email_reply_to = 'CFOC <pro@cfoc.com>';
+        $email_subject = $_POST['email_subject'] ?? 'Récapitulatif de votre commande CFOC';
+        $email_body = "Bonjour,\n\nVotre commande est en cours de traitement (récapitulatif en pièce jointe). Nous vous recontacterons dans les meilleurs délais.\n\nCordialement,\n\nL'équipe CFOC";
+
+        // Send the email
+        $mail = new cubeMail();
+        $mail->returnPath = $return_address;
+        $mail->charset = 'UTF-8';
+        $mail->to = implode(',', $email_to);
+        $mail->cc = implode(',', $email_cc);
+        $mail->from = $email_from;
+        $mail->replyTo = $email_reply_to;
+        $mail->subject = $email_subject;
+        $mail->body = $email_body;
+
+        // Attach the generated spreadsheet file
+        // I found when using the .xlsx extension, the mail isn't delivered. Interestingly, it's not the actual format
+        // that causes the problem because a .xlsx file named as .xls would be delivered. It must be a mail server
+        // configuration but the older version of Excel is fine for our needs, so that's what is used for the format.
+        $mail->addFile('CFOC-' . date('YmdHis') . '.xls', $xls_file);
+        $result['success'] = $mail->send();
+
+        unlink($xls_file); // Tidy up temporary file after sending
+
+        if ($result['success']) {
+            $result['message'] = 'Merci, votre commande a été bien reçue. Nous vous recontacterons dans les meilleurs délais.';
+        } else {
+            $result['message'] = "Désolé, une erreur s'est produite. Veuillez réessayer ou contactez pro@cfoc.com.";
+        }
+
+        header('Content-type: application/json');
+        ob_end_clean();
+        echo json_encode($result);
+    }
+
     public static function searchGFXDevice($gpu, $raw, $gfxVersion = 40)
     {
         // First intention direct request