def trans(): to_put = list() order_number = OrderNumber.next(mobicage_legal_entity) order_key = db.Key.from_path(Order.kind(), order_number, parent=customer.key()) order = Order(key=order_key) order.contact_id = legal_entity.contact_id order.date = _now order.vat_pct = mobicage_legal_entity.vat_percent if legal_entity.country_code == mobicage_legal_entity.country_code else 0 order.amount = int(round(total_amount)) order.vat = int(round(vat_amount)) order.total_amount = int(round(total_amount + vat_amount)) order.is_subscription_order = False order.is_subscription_extension_order = False order.team_id = mobicage_team.id order.manager = customer.manager order.status = Order.STATUS_SIGNED to_put.append(order) for i, (product_code, item) in enumerate(product_totals.iteritems()): order_item = OrderItem(parent=order_key) order_item.number = i + 1 order_item.comment = products[product_code].default_comment( SHOP_DEFAULT_LANGUAGE) order_item.product_code = product_code order_item.count = item['count'] order_item.price = item['price'] to_put.append(order_item) charge_key = Charge.create_key(allocate_id(Charge), order_number, customer.id) charge = Charge(key=charge_key) charge.date = _now charge.type = Charge.TYPE_ORDER_DELIVERY charge.amount = order.amount charge.vat_pct = order.vat_pct charge.vat = order.vat charge.total_amount = order.total_amount charge.manager = order.manager charge.team_id = order.team_id charge.charge_number = ChargeNumber.next(mobicage_legal_entity) charge.currency_code = legal_entity.currency_code to_put.append(charge) invoice_number = InvoiceNumber.next(mobicage_legal_entity) invoice = Invoice(key_name=invoice_number, parent=charge, amount=charge.amount, vat_pct=charge.vat_pct, vat=charge.vat, total_amount=charge.total_amount, currency_code=legal_entity.currency_code, date=_now, payment_type=Invoice.PAYMENT_MANUAL_AFTER, operator=charge.manager, paid=False, legal_entity_id=mobicage_legal_entity.id, pdf=invoice_pdf_contents) charge.invoice_number = invoice_number to_put.append(invoice) put_and_invalidate_cache(*to_put) return order, charge, invoice
def trans(): old_order, team = db.get( (old_order_key, RegioManagerTeam.create_key(customer.team_id))) if not old_order: return BoolReturnStatusTO.create( False, translate( get_solution_settings(service_user).main_language, SOLUTION_COMMON, u'cart_empty'), False) # Duplicate the order to_put = list() to_delete = list() properties = copy_model_properties(old_order) properties['status'] = Order.STATUS_SIGNED properties['date_signed'] = now() new_order_key = Order.create_key( customer.id, OrderNumber.next(team.legal_entity_key)) new_order = Order(key=new_order_key, **properties) new_order.team_id = team.id to_delete.append(old_order) # duplicate all of the order items old_order_items = OrderItem.list_by_order(old_order_key) all_products = db.get([ Product.create_key(item.product_code) for item in old_order_items ]) is_subscription_extension_order = False for product in all_products: if product.is_subscription_extension: is_subscription_extension_order = True break new_order.is_subscription_extension_order = is_subscription_extension_order if is_subscription_extension_order: subscription_order = Order.get_by_order_number( customer.id, customer.subscription_order_number) new_order.next_charge_date = subscription_order.next_charge_date added_apps = list() should_create_shoptask = False for old_item in old_order_items: properties = copy_model_properties(old_item) new_item = OrderItem(parent=new_order_key, **properties) to_put.append(new_item) to_delete.append(old_item) if hasattr(old_item, 'app_id'): added_apps.append(old_item.app_id) else: should_create_shoptask = True to_put.append(new_order) db.put(to_put) db.delete(to_delete) deferred.defer(generate_and_put_order_pdf_and_send_mail, customer, new_order_key, service_user, _transactional=True) # No need for signing here, immediately create a charge. azzert(new_order.total_amount > 0) charge = Charge(parent=new_order_key) charge.date = now() charge.type = Charge.TYPE_ORDER_DELIVERY charge.amount = new_order.amount charge.vat_pct = new_order.vat_pct charge.vat = new_order.vat charge.total_amount = new_order.total_amount charge.manager = new_order.manager charge.team_id = new_order.team_id charge.status = Charge.STATUS_PENDING charge.date_executed = now() charge.currency_code = team.legal_entity.currency_code charge.put() # Update the regiomanager statistics so these kind of orders show up in the monthly statistics deferred.defer(update_regiomanager_statistic, gained_value=new_order.amount / 100, manager=new_order.manager, _transactional=True) # Update the customer service si = get_default_service_identity(users.User(customer.service_email)) si.appIds.extend(added_apps) si.put() deferred.defer(re_index, si.user, _transactional=True) # Update the customer object so the newly added apps are added. customer.app_ids.extend(added_apps) customer.extra_apps_count += len(added_apps) customer.put() get_payed(customer.id, new_order, charge) # charge the credit card channel.send_message(service_user, 'common.billing.orders.update') channel_data = { 'customer': customer.name, 'no_manager': True, 'amount': charge.amount / 100, 'currency': charge.currency } server_settings = get_server_settings() send_to = server_settings.supportWorkers send_to.append(MC_DASHBOARD.email()) channel.send_message(map(users.User, send_to), 'shop.monitoring.signed_order', info=channel_data) if should_create_shoptask: prospect_id = customer.prospect_id if prospect_id is None: prospect = create_prospect_from_customer(customer) prospect_id = prospect.id deferred.defer(create_task_for_order, customer.team_id, prospect_id, new_order.order_number, _transactional=True) return BoolReturnStatusTO.create(True, None)