def item_new(): if 'cancel' in request.form: return redirect(url_for('.item_list')) item_new_schema = ItemNewSchema() item_new_form = Form( item_new_schema, action=url_for('.item_new'), buttons=get_form_buttons() ) if 'submit' in request.form: print request.form try: appstruct = item_new_form.validate(request.form.items()) except ValidationFailure as e: current_app.logger.warning(e.error) template_context = {'item_new_form': item_new_form} return render_template(ITEM_NEW_TEMPLATE, **template_context) new_item = Item() for key in appstruct: if hasattr(new_item, key): if appstruct[key] == colander.null: setattr(new_item, key, None) else: setattr(new_item, key, appstruct[key]) Session.add(new_item) Session.flush() clients = get_client_list() client_items_to_add = [] for client in clients: client_item = Session.query(ClientItem)\ .filter(ClientItem.client_id == client.id, ClientItem.item_id == new_item.id, ClientItem.deleted == False).first() if not client_item: client_item = ClientItem(client.id, new_item.id, new_item.price) client_items_to_add.append(client_item) if client_items_to_add: Session.add_all(client_items_to_add) Session.flush() flash(u'Uspješno ste dodali novi artikl', 'success') return redirect(url_for('.item_list')) template_context = { 'page_title': u'Novi artikl', 'item_new_form': item_new_form } return render_template(ITEM_NEW_TEMPLATE, **template_context)
def client_new(): if 'cancel' in request.form: return redirect(url_for('.client_list')) client_new_schema = ClientNewValidator() client_new_form = Form(client_new_schema, action=url_for('.client_new'), buttons=get_form_buttons()) if request.method == 'POST': try: controls = request.form.items(multi=True) appstruct = client_new_form.validate(controls) except ValidationFailure as e: current_app.logger.warning(e.error) template_context = {'client_new_form': client_new_form} return render_template(CLIENT_NEW_TEMPLATE, **template_context) new_client = Client(appstruct) Session.add(new_client) Session.flush() items = get_item_list() client_items_to_add = [] for item in items: client_item = ClientItem(new_client.id, item.id, item.price) client_items_to_add.append(client_item) Session.add_all(client_items_to_add) Session.flush() flash(u'Uspješno ste dodali novog klijenta', 'success') return redirect(url_for('.client_list')) template_context = { 'page_title': u'Novi klijent', 'client_new_form': client_new_form } return render_template(CLIENT_NEW_TEMPLATE, **template_context)
def receipt_new(selected_client_id=0): if 'cancel' in request.form: return redirect(url_for('.receipt_list')) client_list = get_client_list_ordered() if not client_list: flash(string_constants.NO_CLIENTS, 'danger') return redirect(url_for('.receipt_list')) client_id_name_list = [(client.id, client.get_name()) for client in client_list] client_id_name_list.insert(0, ('', '-Odaberi klijenta-')) if selected_client_id == 0: client_id = client_list[0].id else: client_id = selected_client_id client_item_list = get_client_items(client_id) if not client_item_list: flash(string_constants.NO_ITEMS, 'danger') return redirect(url_for('.receipt_list')) item_data_list = [{ 'id': client_item.id, 'name': client_item.item.get_name(), 'ean': client_item.item.get_ean(), 'measurement_unit': client_item.item.get_measurement_unit(), 'price': client_item.get_price_float(), 'price_formatted': client_item.get_price_formatted(), 'return_amount': client_item.item.get_return_amount() } for client_item in client_item_list] item_id_name_list = [(client_item.id, client_item.item.name) for client_item in client_item_list] item_id_name_list.insert(0, ('', '-Odaberi artikl-')) payment_types = [(payment_type, payment_name) for payment_type, payment_name in Receipt.get_payment_option_dict().iteritems()] receipt_new_schema = ReceiptNewValidator().bind( items=item_id_name_list, item_data_list=json.dumps(item_data_list), clients=client_id_name_list, payment_types=payment_types) default_user = Session.query(User).filter( User.firstname == u'Tomi').first() appstruct = { 'operator': u'{0} {1}'.format(default_user.firstname, default_user.lastname), } if selected_client_id != 0: appstruct['client_id'] = selected_client_id receipt_new_form = Form(receipt_new_schema, action=url_for('.receipt_new', client_id=client_id), buttons=get_form_buttons(), formid='receipt-new-form', appstruct=appstruct) template_context = { 'page_title': u'Novi račun', 'receipt_new_form': receipt_new_form, 'item_data_list': json.dumps(item_data_list), 'tax_percent': Receipt.TAX_PERCENT, } if 'submit' in request.form: try: controls = request.form.items(multi=True) appstruct = receipt_new_form.validate(controls) except ValidationFailure as e: current_app.logger.warning(e.error) return render_template(RECEIPT_NEW_TEMPLATE, **template_context) new_receipt = Receipt() new_receipt.tax_percent = Receipt.TAX_PERCENT new_receipt.user_id = default_user.id for key in appstruct: if hasattr(new_receipt, key): if key in ('issued_date', 'currency_date'): parsed_date = datetime.strptime(appstruct[key], Receipt.date_format) setattr(new_receipt, key, parsed_date) elif key == 'payment_type': setattr(new_receipt, key, Receipt.SLIP) else: setattr(new_receipt, key, appstruct[key]) Session.add(new_receipt) Session.flush() added_receipt_items_list = [] for item in appstruct['receipt_items']: client_item = get_client_item(item['client_item_id']) client_item.item.stock_quantity -= item['quantity'] new_receipt_item = ReceiptItem() new_receipt_item.receipt_id = new_receipt.id new_receipt_item.name = client_item.item.name for key in item: if hasattr(new_receipt_item, key): setattr(new_receipt_item, key, item[key]) added_receipt_items_list.append(new_receipt_item) Session.add_all(added_receipt_items_list) Session.flush() flash(u'Uspješno ste dodali novi račun', 'success') return redirect(url_for('.receipt_list')) return render_template(RECEIPT_NEW_TEMPLATE, **template_context)