def functionGetStock(): form = SearchForm() if request.method == 'POST' and form.validate_on_submit(): stock = Stock.select(Product.id.alias('product_id'), Product.product_name.alias('product_name'), Product.product_code.alias('product_code'), Product.description.alias('product_description'), Product.price.alias('product_price'), Stock.id.alias('stock_id'), Stock.amount.alias('stock_amount'))\ .join(Product, on=(Stock.product_id == Product.id))\ .where( Product.product_code.contains(form.search.data) | Product.product_name.contains(form.search.data) | Product.description.contains(form.search.data) ) else: stock = Stock.select(Product.id.alias('product_id'), Product.product_name.alias('product_name'), Product.product_code.alias('product_code'), Product.description.alias('product_description'), Product.price.alias('product_price'), Stock.id.alias('stock_id'), Stock.amount.alias('stock_amount')).join( Product, on=(Stock.product_id == Product.id)) return render_template('stock/list_stock.html', current_user=current_user, form=form, len_list=len(stock), list_stock=stock)
def functionInsertMemorandumAndDetailMemorandum(): form = SearchForm() row = {"status": "success", "message": "Success"} data = request.get_json() data_user_id = data['user_id'] detailMemorandum = data['detailMemorandum'] memorandumCode = functionGetMemorandumCode() if detailMemorandum: try: print("data['status']", data['status'] == 1) status_memo = False if data['status'] == 1: status_memo = True else: data['total_remaining'] = data['total_price'] result_memorandum = Memorandum.insert( memo_number=memorandumCode, memo_date=datetime.datetime.now(), memo_end_date=datetime.datetime.now() + datetime.timedelta(days=30), user_id=data_user_id, description=data['description'], total_amount=data['total_amount'], total_price=data['total_price'], total_remaining=data['total_remaining'], status=data['status'], created_by=current_user.id, created_at=datetime.datetime.now()).execute() try: if status_memo: UserAccount.insert( user_id=data_user_id, memo_id=result_memorandum, debit=0, credit=0, balance=0, created_by=current_user.id, created_at=datetime.datetime.now()).execute() else: UserAccount.insert( user_id=data_user_id, memo_id=result_memorandum, debit=0, credit=data['total_price'], balance=data['total_remaining'], created_by=current_user.id, created_at=datetime.datetime.now()).execute() except Exception as e: row['status'] = "danger" row['message'] = "Failed to Insert User Account" return row for data in detailMemorandum: DetailMemorandum.insert( memo_id=result_memorandum, product_id=data['product_id'], amount=data['amount'], unit="kg", created_by=current_user.id, created_at=datetime.datetime.now()).execute() try: stock = Stock.select().where( Stock.product_id == data['product_id']) amount_remaining = stock[0].amount - int(data['amount']) query_stock_update = Stock.update( amount=amount_remaining, updated_by=current_user.id, updated_at=datetime.datetime.now()) query_stock_update.where( Stock.product_id == data['product_id']).execute() except Exception as e: print("Except Update Stock", e) row['status'] = "danger" row['message'] = "Failed to Update Stock" return row try: DetailStock.insert(stock_id=stock[0].id, user_id=data_user_id, amount_in=0, amount_out=data['amount'], amount_balance=amount_remaining, start_date=datetime.datetime.now(), created_at=datetime.datetime.now(), created_by=current_user.id).execute() except Exception as e: print("Except Insert Detail Stock", e) row['status'] = "danger" row['message'] = "Failed to Insert Detail Stock" return row row['status'] = "success" row['message'] = "Successfully Inserted" except Exception as e: print("Except", e) row['status'] = "danger" row['message'] = "Failed to Insert" else: row['status'] = "danger" row['message'] = "There's No Data" return row
def functionInsertDetailProductRaw(): form = SearchForm() row = {"status": "failed", "message": "failed"} data = request.get_json() product_raw_id = data['productRawID'] productRawDescription = data['productRawDescription'] detailMemorandum = data['detailMemorandum'] if detailMemorandum: try: for row in detailMemorandum: # Get Stock by product id stock_id = 0 stock_amount = 0 stock = Stock.select().where( Stock.product_id == row['product_id']) if stock: try: stock_id = stock[0].id stock_amount = int(row['amount']) + stock[0].amount # Update Amount Stock + detailMemorandum['amount'] where product_id query_stock_update = Stock.update( amount=stock_amount, updated_by=current_user.id, updated_at=datetime.datetime.now()) query_stock_update.where( Stock.id == stock_id).execute() except Exception as e: print("Stock Update Failed", e) row['status'] = "danger" row['message'] = "Failed to Update Stock" return row else: print("MASUK ELSE") try: stock_amount = int(row['amount']) # Insert Amount Stock + detailMemorandum['amount'] where product_id query_stock_insert = Stock.insert( product_id=row['product_id'], amount=stock_amount, created_by=current_user.id, created_at=datetime.datetime.now()).execute() print("query_stock_insert", query_stock_insert) stock_id = query_stock_insert except Exception as e: print("Stock Update Failed", e) row['status'] = "danger" row['message'] = "Failed to Update Stock" return row print("stock_id", stock_id) try: # DetailStock, stock_id, user_id amount_in+ amount_balance+ DetailStock.insert( stock_id=stock_id, user_id=current_user.id, amount_in=int(row['amount']), amount_out=0, amount_balance=stock_amount, start_date=datetime.datetime.now(), created_by=current_user.id, created_at=datetime.datetime.now()).execute() except Exception as e: print("DetailStock Insert Failed", e) row['status'] = "danger" row['message'] = "Failed to Insert Detail Stock" return row try: # DetailProductRaw, product_raw_id product_id description amount DetailProductRaw.insert( product_raw_id=product_raw_id, product_id=row['product_id'], description=productRawDescription, amount=row['amount'], created_by=current_user.id, created_at=datetime.datetime.now()).execute() row['status'] = "success" row['message'] = "Successfully Inserted" except Exception as e: print("DetailStockRaw Insert Failed", e) row['status'] = "danger" row['message'] = "Failed to Insert Detail Product Raw" except Exception as e: print("Insert Failed", e) row['status'] = "danger" row['message'] = "Failed to Insert" return row return row