def test_order_copy_by_updating_order_source_from_order(admin_user): shop = get_default_shop() line_data = { "type": OrderLineType.PRODUCT, "product": get_default_product(), "supplier": get_default_supplier(), "quantity": 1, "base_unit_price": shop.create_price(10), } source = seed_source(admin_user) source.add_line(**line_data) source.payment_data = None creator = OrderCreator() order = creator.create_order(source) new_source = OrderSource(shop) new_source.update_from_order(order) new_source.add_line(**line_data) new_order = creator.create_order(new_source) assert new_order assert order.billing_address == new_order.billing_address assert order.taxful_total_price == new_order.taxful_total_price
def _initialize_source_from_state(self, state, creator, ip_address, save, order_to_update=None): shop_data = state.pop("shop", None).get("selected", {}) shop = self.safe_get_first(Shop, pk=shop_data.pop("id", None)) if not shop: self.add_error(ValidationError(_("Please choose a valid shop."), code="no_shop")) return None source = OrderSource(shop=shop) if order_to_update: source.update_from_order(order_to_update) customer_data = state.pop("customer", None) billing_address_data = customer_data.pop("billingAddress", {}) shipping_address_data = ( billing_address_data if customer_data.pop("shipToBillingAddress", False) else customer_data.pop("shippingAddress", {})) is_company = customer_data.pop("isCompany", False) save_address = customer_data.pop("saveAddress", False) customer = self._get_customer(customer_data, billing_address_data, is_company, save) if not customer: return billing_address = MutableAddress.from_data(billing_address_data) shipping_address = MutableAddress.from_data(shipping_address_data) if save: billing_address.save() shipping_address.save() if save and save_address: customer.default_billing_address = billing_address customer.default_shipping_address = shipping_address customer.save() methods_data = state.pop("methods", None) or {} shipping_method = methods_data.pop("shippingMethod") if not shipping_method: self.add_error(ValidationError(_("Please select shipping method."), code="no_shipping_method")) payment_method = methods_data.pop("paymentMethod") if not payment_method: self.add_error(ValidationError(_("Please select payment method."), code="no_payment_method")) if self.errors: return source.update( creator=creator, ip_address=ip_address, customer=customer, billing_address=billing_address, shipping_address=shipping_address, status=OrderStatus.objects.get_default_initial(), shipping_method=self.safe_get_first(ShippingMethod, pk=shipping_method.get("id")), payment_method=self.safe_get_first(PaymentMethod, pk=payment_method.get("id")), ) return source
def _initialize_source_from_state(self, state, creator, ip_address, save, order_to_update=None): shop_data = state.pop("shop", None).get("selected", {}) shop = self.safe_get_first(Shop, pk=shop_data.pop("id", None)) if not shop: self.add_error( ValidationError(_("Please choose a valid shop."), code="no_shop")) return None source = OrderSource(shop=shop) if order_to_update: source.update_from_order(order_to_update) customer_data = state.pop("customer", None) billing_address_data = customer_data.pop("billingAddress", {}) shipping_address_data = (billing_address_data if customer_data.pop( "shipToBillingAddress", False) else customer_data.pop( "shippingAddress", {})) is_company = customer_data.pop("isCompany", False) save_address = customer_data.pop("saveAddress", False) customer = self._get_customer(customer_data, billing_address_data, is_company, save) if not customer: return billing_address = MutableAddress.from_data(billing_address_data) shipping_address = MutableAddress.from_data(shipping_address_data) if save: billing_address.save() shipping_address.save() if save and save_address: customer.default_billing_address = billing_address customer.default_shipping_address = shipping_address customer.save() methods_data = state.pop("methods", None) or {} shipping_method = methods_data.pop("shippingMethod") if not shipping_method: self.add_error( ValidationError(_("Please select shipping method."), code="no_shipping_method")) payment_method = methods_data.pop("paymentMethod") if not payment_method: self.add_error( ValidationError(_("Please select payment method."), code="no_payment_method")) if self.errors: return source.update( creator=creator, ip_address=ip_address, customer=customer, billing_address=billing_address, shipping_address=shipping_address, status=OrderStatus.objects.get_default_initial(), shipping_method=self.safe_get_first(ShippingMethod, pk=shipping_method.get("id")), payment_method=self.safe_get_first(PaymentMethod, pk=payment_method.get("id")), ) return source