def create_source_from_state(self, state, creator=None, ip_address=None, save=False, order_to_update=None): """ Create an order source from a state dict unserialized from JSON. :param state: State dictionary :type state: dict :param creator: Creator user :type creator: django.contrib.auth.models.User|None :param save: Flag whether order customer and addresses is saved to database :type save: boolean :param order_to_update: Order object to edit :type order_to_update: shuup.core.models.Order|None :return: The created order source, or None if something failed along the way :rtype: OrderSource|None """ if not self.is_valid: # pragma: no cover raise ValueError("Create a new JsonOrderCreator for each order.") # We'll be mutating the state to make it easier to track we've done everything, # so it's nice to deepcopy things first. state = deepcopy(state) # First, initialize an OrderSource. source = self._initialize_source_from_state( state, creator=creator, ip_address=ip_address, save=save, order_to_update=order_to_update) if not source: return None # Then, copy some lines into it. self._process_lines(source, state) if not self.is_valid: # If we encountered any errors thus far, don't bother going forward return None if not self.is_valid: return None if order_to_update: for code in order_to_update.codes: source.add_code(code) if source.is_cash_order(): processor = source.payment_method.payment_processor taxful_total = source.taxful_total_price rounded = nickel_round( taxful_total, quant=processor.rounding_quantize, rounding=processor.rounding_mode.value) remainder = rounded - taxful_total line_data = dict( line_id="rounding", type=OrderLineType.ROUNDING, quantity=1, shop=source.shop, text="Rounding", base_unit_price=source.create_price(remainder.value), tax_class=None, line_source=LineSource.ADMIN ) source.add_line(**line_data) source.get_final_lines() return source
def get_costs(self, service, source): total_price = source.total_price_of_products rounded = nickel_round(total_price, self.quant, self.mode.value) remainder = rounded - total_price yield ServiceCost(remainder, _("rounding"))