| Code Coverage
| Classes and Traits | Functions and Methods | Lines
Total | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 8 | CRAP | 0.00% covered (danger) | 0.00% | 0 / 46
RejectedExport | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 8 | 132.00 | 0.00% covered (danger) | 0.00% | 0 / 46
setIds | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 2
setRejectedBills | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 2
collection | 0.00% covered (danger) | 0.00% | 0 / 1 | 12.00 | 0.00% covered (danger) | 0.00% | 0 / 5
getByIds | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 10
getByRejectedBills | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 6
prependFields | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 2
injectRecord | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 10
columnFormats | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 9

1 | <?php
2 |
3 | namespace App\Exports;
4 |
5 | use App\RejectedBill;
6 | use App\RejectedBillItem;
7 | use Illuminate\Database\Eloquent\Builder;
8 | use Illuminate\Database\Eloquent\Collection;
9 | use Maatwebsite\Excel\Concerns\FromCollection;
10 | use Maatwebsite\Excel\Concerns\ShouldAutoSize;
11 | use Maatwebsite\Excel\Concerns\WithColumnFormatting;
12 | use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
13 | use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
14 |
15 | class RejectedExport extends \PhpOffice\PhpSpreadsheet\Cell\StringValueBinder implements FromCollection,ShouldAutoSize,WithColumnFormatting
16 | ,WithCustomValueBinder
17 | {
18 | protected $ids;
19 | static public $max=10000;
20 | protected $rejectedBills;
21 | public function setIds(Array $ids)
22 | {
23 | $this->ids=$ids;
24 | }
25 | public function setRejectedBills($rejectedBills)
26 | {
27 | $this->rejectedBills=$rejectedBills;
28 | }
29 | public function collection()
30 | {
31 | if($this->ids){
32 | return $this->getByIds();
33 | }
34 | if($this->rejectedBills){
35 | return $this->getByRejectedBills();
36 | }
37 | return null;
38 | }
39 | private function getByIds(){
40 | $rejectedItems=RejectedBillItem::whereHas('rejectedBill',function(Builder $query){
41 | $query->whereIn('id',$this->ids);
42 | if(count($this->ids)>self::$max){
43 | $query->where('is_finished',false);
44 | }
45 | });
46 | $rejectedItems->get();
47 | $rejecteds = new Collection();
48 | $this->prependFields($rejecteds);
49 | $rejectedItems->each(function(RejectedBillItem $item)use($rejecteds){
50 | $this->injectRecord($rejecteds,$item->rejectedBill,$item);
51 | });
52 | return $rejecteds;
53 | }
54 | private function getByRejectedBills(){
55 | $rejecteds = new Collection();
56 | $this->prependFields($rejecteds);
57 | $this->rejectedBills->each(function(RejectedBill $rejectedBill)use($rejecteds){
58 | $rejectedBill->items()->each(function (RejectedBillItem $item)use($rejecteds,$rejectedBill){
59 | $this->injectRecord($rejecteds,$rejectedBill,$item);
60 | });
61 | });
62 | return $rejecteds;
63 | }
64 | private function prependFields($collection){
65 | $collection->prepend(['日期','审核号','客户名称','订单号','姓名',
66 | '手机','原单单号','退回单号','退回公司','到付费用',
67 | '是否入库','商品条码','商品名称','数量','是否正品',
68 | '批次号','生产日期','效期','备注']);
69 | return $collection;
70 | }
71 | private function injectRecord($collection,$bill,$item){
72 | $collection->push([
73 | $item['created_at'],
74 | $bill['checked_numbers'],$bill['owner_name'],
75 | $bill['order_number'],$bill['sender'],
76 | $bill['mobile_sender'],$bill['logistic_number'],
77 | $bill['logistic_number_return'],$bill['logistic_name'],
78 | $bill['fee_collected'],$bill['is_loaded_str'],
79 | $item['barcode_goods'],$item['name_goods'],$item['amount'],$item['quality_label'],
80 | $item['batch_number'],$item['made_at'],$item['validity_at'],$item['remark']]);
81 | return $collection;
82 | }
83 |
84 |
85 | /**
86 | * @return array
87 | */
88 | public function columnFormats(): array
89 | {
90 | return [
91 | 'B' => NumberFormat::FORMAT_TEXT,
92 | 'C' => NumberFormat::FORMAT_TEXT,
93 | 'D' => NumberFormat::FORMAT_TEXT,
94 | 'E' => NumberFormat::FORMAT_TEXT,
95 | 'F' => NumberFormat::FORMAT_TEXT,
96 | 'G' => NumberFormat::FORMAT_TEXT,
97 | 'H' => NumberFormat::FORMAT_TEXT,
98 | 'L' => NumberFormat::FORMAT_TEXT,
99 | 'N' => NumberFormat::FORMAT_TEXT,
100 | ];
101 | }
102 | }