2020from commercetools .testing .utils import (
2121 create_commercetools_response ,
2222 create_from_draft ,
23+ get_product_from_storage ,
2324 money_to_typed ,
2425 set_custom_field ,
2526 set_line_item_custom_field ,
@@ -88,6 +89,9 @@ def _create_from_cart_draft(self, draft: models.OrderFromCartDraft, id: str):
8889 return order
8990
9091 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+ ]
9195 custom_line_items = [
9296 self ._create_custom_line_item_from_draft (line )
9397 for line in draft .custom_line_items
@@ -98,7 +102,7 @@ def _create_from_import_draft(self, draft: models.OrderImportDraft, id: str):
98102 version = 1 ,
99103 created_at = datetime .datetime .now (datetime .timezone .utc ),
100104 last_modified_at = datetime .datetime .now (datetime .timezone .utc ),
101- line_items = [] ,
105+ line_items = line_items ,
102106 custom_line_items = custom_line_items ,
103107 total_price = money_to_typed (draft .total_price ),
104108 taxed_price = create_from_draft (draft .taxed_price ),
@@ -113,13 +117,98 @@ def _create_from_import_draft(self, draft: models.OrderImportDraft, id: str):
113117 )
114118 return order
115119
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+
116178 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+
117186 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+
118195 total_net = money_to_typed (
119196 models .Money (
120197 cent_amount = total_net_cents , currency_code = draft .money .currency_code
121198 )
122199 )
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+
123212 return models .CustomLineItem (
124213 id = str (uuid .uuid4 ()),
125214 name = draft .name ,
@@ -131,6 +220,7 @@ def _create_custom_line_item_from_draft(self, draft: models.CustomLineItemDraft)
131220 discounted_price_per_quantity = [],
132221 tax_category = draft .tax_category ,
133222 tax_rate = create_from_draft (draft .external_tax_rate ),
223+ taxed_price = taxed_price ,
134224 custom = create_from_draft (draft .custom ),
135225 shipping_details = create_from_draft (draft .shipping_details ),
136226 )
0 commit comments