在Maatwebsite \ Excel中使用Concerns WithMultipleSheets时如何监听WithEvents registerEvents

时间:2019-07-02 09:51:53

标签: excel laravel

类QuotationExport实现WithMultipleSheets {     使用Exportable;

public function __construct($quotation)
{
    $this->quotation = $quotation;
}

/**
 * @return array
 */
public function sheets(): array
{

    $tbl1     = QuotationDetails::getTableName();
    $tbl2     = Quotations::getTableName();
    $tbl4     = Mstclass::getTableName();
    $tbl3     = Mstitem::getTableName();
    $m        = WebConfigs::where('key', '=', 'ClassProduct')->first();
    $products = Quotations::join($tbl1, $tbl1 . ".quotation_id", "=", $tbl2 . ".id")
        ->join($tbl4, $tbl4 . ".ClassKey", "=", $tbl1 . ".product_id")
        ->where(
            [
                [$tbl4 . ".Class", "=", $m->value],
                [$tbl1 . ".quotation_id", "=", $this->quotation->id],
            ]
        )
        ->select([
            $tbl4 . ".*",
            DB::Raw("SUM(" . $tbl1 . ".quantity) as Q,SUM(" . $tbl1 . ".saleprice) as S,SUM(" . $tbl1 . ".price) as F,SUM(" . $tbl1 . ".discount) as D"),
        ])
        ->orderby($tbl4 . ".Class", "ASC")
        ->groupby($tbl4 . ".ClassKey")
        ->get();

    $sheets = [
        new SheetBefore($this->quotation, $products),
    ];
    foreach ($products as $key => $value) {
        $sheets[] = new SheetDetail($this->quotation, $value->ClassKey);
    }
    return $sheets;
}

} SheetBefore类实现FromView,WithTitle,WithEvents {     公共功能__construct($ quotation,$ products)     {         $ this-> quotation = $ quotation;         $ this-> products = $ products;     }     / **      * @返回生成器      * /

public function view(): View
{
    $data['productions'] = $this->products;
    $data['quotation']   = $this->quotation;
    return view('quotations.beforeexport', $data);
}

/**
 * @return string
 */
public function title(): string
{
    return $this->quotation->quotation_number;
}
public function registerEvents(): array
{
    return [
        BeforeImport::class => [self::class, 'beforeImport'],

    ];
}
public static function beforeImport(AfterSheet $event)
{

    $sheet = $event->sheet;
    $sheet->setOrientation(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE);
    $sheet->getParent()->getDefaultStyle()->getFont()->setName('Arial');
    $sheet->getParent()->getDefaultStyle()->getFont()->setSize(12);
    $sheet->getParent()->getDefaultStyle()->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
    $sheet->getParent()->getDefaultStyle()->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
    $sheet->getParent()->getDefaultStyle()->getAlignment()->setWrapText(true);
    $sheet->styleCells('A1',
        [
            'borders'   => [
                'allBorders' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                    'color'       => ['argb' => 'ffffffff'],
                ],
            ],
            'font'      => [
                'size' => 24,
                'bold' => true,
            ],
            'alignment' => [
                'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_BOTTOM,
            ],
        ]
    );

}

} SheetDetail类实现FromView,WithTitle {

public function __construct($quotation, $ClassKey)
{
    $this->quotation = $quotation;
    $this->m         = WebConfigs::where('key', '=', 'ClassProduct')->first();
    $this->Class     = Mstclass::where("ClassKey", "=", $ClassKey)->where('Class', '=', $this->m->value)->first();
}
/**
 * @return Builder
 */

public function view(): View
{

    $tbl2                         = Quotations::getTableName();
    $MstclassTBL                  = Mstclass::getTableName();
    $MstitemclassTBL              = Mstitemclass::getTableName();
    $MstitemTBL                   = Mstitem::getTableName();
    $MstitempriceTBL              = Mstitemprice::getTableName();
    $QuotationDetailsTBL          = QuotationDetails::getTableName();
    $QuotationDetailItemsTBL      = QuotationDetailItems::getTableName();
    $QuotationTBL                 = Quotations::getTableName();
    $QuotationDetailOtherItemsTBL = QuotationDetailOtherItems::getTableName();
    $OrdersTBL                    = Orders::getTableName();
    $this->productions            = Quotations::
        join($QuotationDetailsTBL, $QuotationDetailsTBL . ".quotation_id", '=', $tbl2 . ".id")
        ->join($MstclassTBL, $MstclassTBL . ".ClassKey", "=", $QuotationDetailsTBL . ".product_id")
        ->join($QuotationDetailItemsTBL, $QuotationDetailItemsTBL . ".detail_id", "=", $QuotationDetailsTBL . ".id")
        ->join($MstitemTBL, $MstitemTBL . ".ItemId", "=", $QuotationDetailItemsTBL . ".item_id")
        ->join($MstitempriceTBL, $MstitempriceTBL . ".ItemId", "=", $MstitemTBL . ".ItemId")
        ->join("mstfactoryitem", "mstfactoryitem.FactoryCode", "=", $MstitemTBL . ".FactoryCode")
        ->join($MstitemclassTBL, $MstitemclassTBL . ".ItemClassId", "=", $MstitemTBL . ".ItemClassId")
        ->select([DB::Raw("SUM(" . $QuotationDetailsTBL . ".quantity) AS Q"), $MstitempriceTBL . ".*", $MstclassTBL . ".*", $QuotationDetailItemsTBL . ".*", $QuotationDetailsTBL . ".*", $MstitemTBL . ".*", $MstitemclassTBL . ".*"])
        ->where([
            [$QuotationDetailsTBL . ".quotation_id", "=", $this->quotation->id],
            [$MstclassTBL . ".Class", "=", $this->m->value],
            [$QuotationDetailsTBL . ".product_id", "=", $this->Class->ClassKey],
        ])
        ->groupby($QuotationDetailsTBL . ".id")
        ->get();
    $data['productions'] = $this->productions;
    $data['quotation']   = $this->quotation;
    return view('quotations.detailexport', $data);
}

/**
 * @return string
 */
public function title(): string
{
    return $this->Class->ClassName;
}

}

0 个答案:

没有答案