Implement adding new inventory items
This commit is contained in:
		
							parent
							
								
									1f23ebfe2d
								
							
						
					
					
						commit
						29888851e7
					
				| @ -50,3 +50,4 @@ ssh -nNTvL 5432:fsmi-db.fsmi.org:5432 fsmi-login.fsmi.uni-karlsruhe.de | ||||
| - [ ] Make it possible to edit entries | ||||
| - [ ] Improve project structure | ||||
| - [ ] Figure out/Add documentation about building `entry.js` | ||||
| - [ ] Use `flask.flash` for error messages | ||||
|  | ||||
| @ -339,7 +339,9 @@ viewSearchResult model = | ||||
|     , td [] [ text model.locationName ] | ||||
|     , td [] [ text <| showBool model.available ] | ||||
|     , td [] | ||||
|       [ button [ onClick <| GotoItemEditor model ] [ text "Als Vorlage verwenden" ] | ||||
|       [ Html.form [ onSubmit <| GotoItemEditor model ] | ||||
|         [ button [] [ text "Als Vorlage verwenden" ] | ||||
|         ] | ||||
|       ] | ||||
|     ] | ||||
| 
 | ||||
|  | ||||
| @ -7,8 +7,6 @@ from psycopg2.extras import RealDictCursor | ||||
| 
 | ||||
| def get_db(): | ||||
|     if "db" not in g: | ||||
|         # TODO: Make this configurable and use a default that works | ||||
|         # on the pool computers. | ||||
|         g.db = psycopg2.connect(current_app.config["DB_CONNECTION_STRING"]) | ||||
|         run_query_on(g.db, "add_views.sql", None) | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										26
									
								
								jon/db/entry/add_item_and_snack_entry.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								jon/db/entry/add_item_and_snack_entry.sql
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,26 @@ | ||||
| DO $$ | ||||
| DECLARE new_item_id integer; | ||||
| DECLARE new_snack_id integer; | ||||
| BEGIN | ||||
| 
 | ||||
| -- Create a new inventory line | ||||
| INSERT INTO garfield.inventory_items | ||||
| (item_barcode, name, item_group, location, tax_group, sales_units, unit_price) | ||||
| VALUES | ||||
| (%(barcode)s, %(name)s, %(group_id)s, %(location_id)s, %(tax_group_id)s, %(sales_units)s, %(net_unit_price)s) | ||||
| RETURNING item_id INTO new_item_id; | ||||
| 
 | ||||
| -- Delete any old snacks with the same barcode in the given location | ||||
| PERFORM garfield.snack_delete(snack_id) | ||||
| FROM garfield.snacks | ||||
| WHERE snack_barcode = %(barcode)s | ||||
|   AND location_id = %(location_id)s; | ||||
| 
 | ||||
| -- Create a new snack entry... | ||||
| SELECT garfield.snack_create(%(name)s, %(barcode)s, %(gross_unit_price)s, %(tax_group_id)s, %(location_id)s) | ||||
| INTO new_snack_id; | ||||
| 
 | ||||
| -- ... and map it to the new inventory line | ||||
| PERFORM garfield.inventory_map_snack(new_snack_id, new_item_id); | ||||
| 
 | ||||
| END$$ | ||||
							
								
								
									
										29
									
								
								jon/entry.py
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								jon/entry.py
									
									
									
									
									
								
							| @ -1,4 +1,4 @@ | ||||
| from flask import Blueprint, redirect, render_template, request, session | ||||
| from flask import Blueprint, flash, redirect, render_template, request, session | ||||
| 
 | ||||
| 
 | ||||
| from . import db | ||||
| @ -16,12 +16,37 @@ def index(): | ||||
|     ) | ||||
| 
 | ||||
| 
 | ||||
| @bp.post("/add-new-items") | ||||
| def add_new_entries(): | ||||
|     print(session) | ||||
| 
 | ||||
|     i_know_what_im_doing = "i-know-what-im-doing" in request.form | ||||
|     if not i_know_what_im_doing: | ||||
|         return "Du weißt nicht was du tust", 400 | ||||
| 
 | ||||
|     orders = session.get("cart", default=[]) | ||||
|     if not orders: | ||||
|         return "Keine Aufträge", 404 | ||||
|      | ||||
|     # I'm aware of execute_many and extras.execute_values but we don't need to | ||||
|     # optimize here (yet?). This way it's a bit easier to use anyways. | ||||
|     for order in orders: | ||||
|         with db.run_query("entry/add_item_and_snack_entry.sql", order) as cursor: | ||||
|             pass | ||||
|     db.get_db().commit() | ||||
| 
 | ||||
|     # Reset the cart | ||||
|     session["cart"] = [] | ||||
| 
 | ||||
|     return redirect(request.referrer) | ||||
| 
 | ||||
| 
 | ||||
| @bp.post("/delete-order") | ||||
| def delete_order(): | ||||
|     try: | ||||
|         order_index = int(request.form["order-index"]) | ||||
|     except: | ||||
|         return f"Incomplete or mistyped form", 400 | ||||
|         return "Incomplete or mistyped form", 400 | ||||
| 
 | ||||
|     cart = session.get("cart", default=[]) | ||||
|     del cart[order_index] | ||||
|  | ||||
| @ -34,4 +34,9 @@ | ||||
|   </tr> | ||||
|   {% endfor %} | ||||
| </table> | ||||
| <form method="POST" action="/entry/add-new-items"> | ||||
|   <input type="checkbox" name="i-know-what-im-doing" id="i-know-what-im-doing"> | ||||
|   <label for="i-know-what-im-doing">I weiß, was ich tue</label> | ||||
|   <button>Neue Einträge anlegen</button> | ||||
| </form> | ||||
| {% endblock %} | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user