20
20
from commercetools .testing .utils import (
21
21
create_commercetools_response ,
22
22
create_from_draft ,
23
+ get_product_from_storage ,
23
24
money_to_typed ,
24
25
set_custom_field ,
25
26
set_line_item_custom_field ,
@@ -88,6 +89,9 @@ def _create_from_cart_draft(self, draft: models.OrderFromCartDraft, id: str):
88
89
return order
89
90
90
91
def _create_from_import_draft (self , draft : models .OrderImportDraft , id : str ):
92
+ line_items = [
93
+ self ._create_line_item_from_draft (line ) for line in draft .line_items
94
+ ]
91
95
custom_line_items = [
92
96
self ._create_custom_line_item_from_draft (line )
93
97
for line in draft .custom_line_items
@@ -98,7 +102,7 @@ def _create_from_import_draft(self, draft: models.OrderImportDraft, id: str):
98
102
version = 1 ,
99
103
created_at = datetime .datetime .now (datetime .timezone .utc ),
100
104
last_modified_at = datetime .datetime .now (datetime .timezone .utc ),
101
- line_items = [] ,
105
+ line_items = line_items ,
102
106
custom_line_items = custom_line_items ,
103
107
total_price = money_to_typed (draft .total_price ),
104
108
taxed_price = create_from_draft (draft .taxed_price ),
@@ -113,13 +117,98 @@ def _create_from_import_draft(self, draft: models.OrderImportDraft, id: str):
113
117
)
114
118
return order
115
119
120
+ def _create_line_item_from_draft (self , draft : models .LineItemImportDraft ):
121
+ tax_rate = 0
122
+ tax_included = False
123
+
124
+ if draft .tax_rate :
125
+ tax_rate = draft .tax_rate .amount
126
+ tax_included = draft .tax_rate .included_in_price
127
+
128
+ currency_code = draft .price .value .currency_code
129
+ total_net_cents = draft .price .value .cent_amount * draft .quantity
130
+ total_gross_cents = total_net_cents
131
+
132
+ if tax_rate :
133
+ if tax_included :
134
+ total_net_cents = total_gross_cents / (1 + tax_rate )
135
+ else :
136
+ total_gross_cents = total_net_cents * (1 + tax_rate )
137
+
138
+ total_net = money_to_typed (
139
+ models .Money (cent_amount = total_net_cents , currency_code = currency_code )
140
+ )
141
+ total_gross = money_to_typed (
142
+ models .Money (cent_amount = total_gross_cents , currency_code = currency_code )
143
+ )
144
+ taxed_price = None
145
+ if tax_rate :
146
+ taxed_price = models .TaxedItemPrice (
147
+ total_net = total_net ,
148
+ total_gross = total_gross ,
149
+ )
150
+
151
+ product = get_product_from_storage (
152
+ self ._storage , product_id = draft .product_id , sku = draft .variant .sku
153
+ )
154
+
155
+ current = product .master_data .current
156
+ variant = current .master_variant
157
+
158
+ return models .LineItem (
159
+ id = str (uuid .uuid4 ()),
160
+ product_id = draft .product_id ,
161
+ product_slug = current .slug ,
162
+ product_type = product .product_type ,
163
+ variant = variant ,
164
+ name = current .name ,
165
+ quantity = draft .quantity ,
166
+ price_mode = models .LineItemPriceMode .PLATFORM ,
167
+ line_item_mode = models .LineItemMode .STANDARD ,
168
+ price = create_from_draft (draft .price ),
169
+ total_price = total_net ,
170
+ state = [],
171
+ discounted_price_per_quantity = [],
172
+ tax_rate = draft .tax_rate ,
173
+ taxed_price = taxed_price ,
174
+ custom = create_from_draft (draft .custom ),
175
+ shipping_details = create_from_draft (draft .shipping_details ),
176
+ )
177
+
116
178
def _create_custom_line_item_from_draft (self , draft : models .CustomLineItemDraft ):
179
+ tax_rate = 0
180
+ tax_included = False
181
+
182
+ if draft .external_tax_rate :
183
+ tax_rate = draft .external_tax_rate .amount
184
+ tax_included = draft .external_tax_rate .included_in_price
185
+
117
186
total_net_cents = draft .money .cent_amount * draft .quantity
187
+ total_gross_cents = total_net_cents
188
+
189
+ if tax_rate :
190
+ if tax_included :
191
+ total_net_cents = total_gross_cents / (1 + tax_rate )
192
+ else :
193
+ total_gross_cents = total_net_cents * (1 + tax_rate )
194
+
118
195
total_net = money_to_typed (
119
196
models .Money (
120
197
cent_amount = total_net_cents , currency_code = draft .money .currency_code
121
198
)
122
199
)
200
+ total_gross = money_to_typed (
201
+ models .Money (
202
+ cent_amount = total_gross_cents , currency_code = draft .money .currency_code
203
+ )
204
+ )
205
+ taxed_price = None
206
+ if tax_rate :
207
+ taxed_price = models .TaxedItemPrice (
208
+ total_net = total_net ,
209
+ total_gross = total_gross ,
210
+ )
211
+
123
212
return models .CustomLineItem (
124
213
id = str (uuid .uuid4 ()),
125
214
name = draft .name ,
@@ -131,6 +220,7 @@ def _create_custom_line_item_from_draft(self, draft: models.CustomLineItemDraft)
131
220
discounted_price_per_quantity = [],
132
221
tax_category = draft .tax_category ,
133
222
tax_rate = create_from_draft (draft .external_tax_rate ),
223
+ taxed_price = taxed_price ,
134
224
custom = create_from_draft (draft .custom ),
135
225
shipping_details = create_from_draft (draft .shipping_details ),
136
226
)
0 commit comments