def update_reception(uid): original_reception = get_reception(uid) data = request.json # only support update sales assignment for now if data is None or data.get('sales_id', None) is None: abort(400, description=gettext('invalid json request')) new_assigned_sales = User.find(data['sales_id']) if new_assigned_sales is None: abort(400, description=gettext('salespeople with id %(id)s is not found', id=data['sales_id'])) # cancel original reception original_reception.status = 'cancelled' # update customer associate sales if the reception is not created from an appointment and customer is created today. new_customer = None if (not original_reception.appointment_id) \ and original_reception.customer.created_on.date() == datetime.today().date(): # create new customer from old one new_customer_dict = obj_to_dict(original_reception.customer) new_customer_dict.pop('id') new_customer_dict.pop('last_reception_date') new_customer_dict.pop('next_appointment_date') new_customer_dict['sales_id'] = data['sales_id'] new_customer = Customer(**new_customer_dict) # cancel original customer original_reception.customer.status = 'cancelled' original_reception.save_and_flush() reception_cancelled.send(reception=original_reception) # create new reception new_reception_dict = obj_to_dict(original_reception) new_reception_dict.pop('id') new_reception_dict.pop('status') if new_customer: new_reception_dict.pop('customer_id') new_reception_dict['customer'] = new_customer new_reception_dict.pop('sales') new_reception_dict['sales_id'] = new_assigned_sales.id new_reception = Reception(**new_reception_dict) new_reception.prev_rx_id = original_reception.id new_reception.created_on = datetime.now() new_reception.customer.last_reception_date = new_reception.created_on new_reception.save_and_flush() new_reception_created.send(new_reception=new_reception) return new_reception
populate_customer_from_request(data, data.get('store_id'), data.get('sales_id')) except NoPermissionOnCustomerException, npe: raise IncorrectSalesAssignmentException( gettext( u'the customer belongs to %(sales_name)s, force assign to %(new_sales)s', sales_name=npe.customer.sales.username, new_sales=User.find(data.get('sales_id')).username)) reception = Reception(**data) # default receptionist_id if not passed from client if convert_int(reception.receptionist_id) <= 0: reception.receptionist_id = get_user_id() reception.customer.enlist() reception.created_on = datetime.now() reception.customer.last_reception_date = reception.created_on if reception.appointment_id is not None: appt = Appointment.find(reception.appointment_id) if not appt: reception.appointment_id = None else: if appt.type == 'deliver': reception.rx_type = 'other' elif not Reception.has_reception_before(appt.customer.id): reception.rx_type = 'appt_new' else: reception.rx_type = 'appt' appt.status = 'closed'