@@ -49,6 +49,14 @@ public function getFilename(): string
4949 return $ this ->filename ?? $ this ->generateFilename ();
5050 }
5151
52+ public function getCurrency (): string
53+ {
54+ /** @var ?PdfInvoiceItem $firstItem */
55+ $ firstItem = Arr::first ($ this ->items );
56+
57+ return $ firstItem ?->currency ?? config ('invoices.default_currency ' );
58+ }
59+
5260 public function getLogo (): string
5361 {
5462 $ type = pathinfo ($ this ->logo , PATHINFO_EXTENSION );
@@ -62,42 +70,26 @@ public function getLogo(): string
6270 */
6371 public function subTotalAmount (): Money
6472 {
65- if (empty ($ this ->items )) {
66- return Money::ofMinor (0 , config ('invoices.default_currency ' ));
67- }
68-
69- $ firstItem = Arr::first ($ this ->items );
70-
71- $ currency = $ firstItem ->currency ;
72-
7373 return array_reduce (
7474 $ this ->items ,
7575 fn (Money $ total , PdfInvoiceItem $ item ) => $ total ->plus ($ item ->subTotalAmount ()),
76- Money::of (0 , $ currency )
76+ Money::of (0 , $ this -> getCurrency () )
7777 );
7878 }
7979
8080 public function totalTaxAmount (): Money
8181 {
82- if (empty ($ this ->items )) {
83- return Money::ofMinor (0 , config ('invoices.default_currency ' ));
84- }
85-
86- $ firstItem = Arr::first ($ this ->items );
87-
88- $ currency = $ firstItem ->currency ;
89-
9082 return array_reduce (
9183 $ this ->items ,
9284 fn (Money $ total , PdfInvoiceItem $ item ) => $ total ->plus ($ item ->totalTaxAmount ()),
93- Money::of (0 , $ currency )
85+ Money::of (0 , $ this -> getCurrency () )
9486 );
9587 }
9688
9789 public function totalDiscountAmount (): Money
9890 {
9991 if (! $ this ->discounts ) {
100- return Money::of (0 , $ this ->subTotalAmount ()-> getCurrency ());
92+ return Money::of (0 , $ this ->getCurrency ());
10193 }
10294
10395 $ subtotal = $ this ->subTotalAmount ();
@@ -109,18 +101,10 @@ public function totalDiscountAmount(): Money
109101
110102 public function totalAmount (): Money
111103 {
112- if (empty ($ this ->items )) {
113- return Money::ofMinor (0 , config ('invoices.default_currency ' ));
114- }
115-
116- $ firstItem = Arr::first ($ this ->items );
117-
118- $ currency = $ firstItem ->currency ;
119-
120104 $ total = array_reduce (
121105 $ this ->items ,
122106 fn (Money $ total , PdfInvoiceItem $ item ) => $ total ->plus ($ item ->totalAmount ()),
123- Money::of (0 , $ currency )
107+ Money::of (0 , $ this -> getCurrency () )
124108 );
125109
126110 return $ total ->minus ($ this ->totalDiscountAmount ());
0 commit comments