@@ -32,6 +32,10 @@ class LivePurchaseOrderLines extends Component
3232
3333 public $ quantity ;
3434
35+ public $ tax_amount ;
36+
37+ public $ tax_rate ;
38+
3539 public $ amount ;
3640
3741 public $ comments ;
@@ -86,6 +90,7 @@ public function mount($purchaseOrder, $purchaseOrderLines, $old = null, $fromOrd
8690 }
8791
8892 $ this ->price [$ this ->i ] = $ old ['price ' ] ?? null ;
93+ $ this ->tax_amount [$ this ->i ] = $ old ['tax_amount ' ] ?? null ;
8994 $ this ->amount [$ this ->i ] = $ old ['amount ' ] ?? null ;
9095 $ this ->comments [$ this ->i ] = $ old ['comments ' ] ?? null ;
9196 }
@@ -111,6 +116,7 @@ public function mount($purchaseOrder, $purchaseOrderLines, $old = null, $fromOrd
111116 }
112117
113118 $ this ->price [$ this ->i ] = $ purchaseOrderLine ->price / 100 ;
119+ $ this ->tax_amount [$ this ->i ] = $ purchaseOrderLine ->tax_amount / 100 ;
114120 $ this ->amount [$ this ->i ] = $ purchaseOrderLine ->amount / 100 ;
115121 $ this ->comments [$ this ->i ] = $ purchaseOrderLine ->comments ;
116122 }
@@ -127,6 +133,7 @@ public function add($i)
127133 $ this ->i = $ i ;
128134 $ this ->price [$ i ] = null ;
129135 $ this ->quantity [$ i ] = null ;
136+ $ this ->tax_rate [$ i ] = null ;
130137 array_push ($ this ->inputs , $ i );
131138
132139 $ this ->dispatchBrowserEvent ('addedItem ' , ['id ' => $ this ->i ]);
@@ -154,8 +161,12 @@ public function calculateAmounts()
154161
155162 for ($ i = 1 ; $ i <= $ this ->i ; $ i ++) {
156163 if (isset ($ this ->product_id [$ i ])) {
157- if ($ product = \VentureDrake \LaravelCrm \Models \Product::find ($ this ->product_id [$ i ])) {
158- $ taxRate = $ product ->taxRate ->rate ?? $ product ->tax_rate ?? 0 ;
164+ $ product = \VentureDrake \LaravelCrm \Models \Product::find ($ this ->product_id [$ i ]);
165+
166+ if ($ product && $ product ->taxRate ) {
167+ $ taxRate = $ product ->taxRate ->rate ;
168+ } elseif ($ product && $ product ->tax_rate ) {
169+ $ taxRate = $ product ->tax_rate ;
159170 } elseif ($ taxRate = TaxRate::where ('default ' , 1 )->first ()) {
160171 $ taxRate = $ taxRate ->rate ;
161172 } elseif ($ taxRate = $ this ->settingService ->get ('tax_rate ' )) {
@@ -164,15 +175,18 @@ public function calculateAmounts()
164175 $ taxRate = 0 ;
165176 }
166177
178+ $ this ->tax_rate [$ i ] = $ taxRate ;
179+
167180 if (is_numeric ($ this ->price [$ i ]) && is_numeric ($ this ->quantity [$ i ])) {
168181 $ this ->amount [$ i ] = $ this ->price [$ i ] * $ this ->quantity [$ i ];
169182 $ this ->price [$ i ] = $ this ->currencyFormat ($ this ->price [$ i ]);
183+ $ this ->tax_amount [$ i ] = $ this ->currencyFormat ($ this ->amount [$ i ] * ($ taxRate / 100 ));
170184 } else {
171185 $ this ->amount [$ i ] = 0 ;
172186 }
173187
174- $ this ->sub_total += $ this ->amount [$ i ];
175- $ this ->tax += $ this ->amount [$ i ] * ($ taxRate / 100 );
188+ $ this ->sub_total += round ( $ this ->amount [$ i ], 2 ) ;
189+ $ this ->tax += round ( $ this ->amount [$ i ] * ($ taxRate / 100 ), 2 );
176190 $ this ->amount [$ i ] = $ this ->currencyFormat ($ this ->amount [$ i ]);
177191 }
178192 }
0 commit comments