def get_cart_resources(cart_id): """creates a list of resources to be displayed in ApplyGridsWidget""" resources = [] with session_scope() as session: records = retrieve_records(session, Order, cart_id=cart_id) n = 0 for rec in records: n += 1 price = f'{rec.resource.price_disc:,.2f}' qty = 0 loc_ids = [] for loc in rec.locations: qty += loc.qty loc_ids.append(loc.branch_id) locations = [] for loc_id in loc_ids: branch_code = get_branch_code(session, loc_id) locations.append(branch_code) locations = ','.join(sorted(locations)) resources.append( (rec.did, n, rec.resource.title, rec.resource.author, rec.resource.isbn, price, rec.comment, qty, locations)) return resources
def delete_locations_from_selected_orders(order_ids): with session_scope() as session: for oid in order_ids: loc_recs = retrieve_records(session, OrderLocation, order_id=oid) for oloc in loc_recs: mlogger.debug(f'Deleting OrderLocation.did={oloc.did} ' f'of Order.did={oid}') delete_record(session, OrderLocation, did=oloc.did)
def apply_fund_to_cart(system_id, cart_id, fund_codes): try: with session_scope() as session: cart_rec = retrieve_record(session, Cart, did=cart_id) ord_recs = retrieve_records(session, Order, cart_id=cart_id) for code in fund_codes: fund_rec = retrieve_record(session, Fund, code=code, system_id=system_id) fund_audn, fund_mat, fund_branch, fund_lib = valid_fund_ids( fund_rec) for orec in ord_recs: audn_match = False mat_match = False library_match = False if orec.audn_id in fund_audn: audn_match = True if orec.matType_id in fund_mat: mat_match = True if cart_rec.library_id in fund_lib: library_match = True for oloc in orec.locations: if oloc.branch_id in fund_branch: mlogger.debug('OrdRec-Fund branch {} match'.format( oloc.branch_id)) if audn_match and library_match and mat_match: # update mlogger.debug( 'Complete match. Updating OrderLocation.') update_record(session, OrderLocation, oloc.did, fund_id=fund_rec.did) else: mlogger.debug( 'Incomplete match: lib={}, audn={}, ' 'mat={}.'.format(library_match, audn_match, mat_match)) except Exception as exc: _, _, exc_traceback = sys.exc_info() tb = format_traceback(exc, exc_traceback) mlogger.error('Unhandled error on applying funds to cart.' f'Traceback: {tb}') raise BabelError(exc)
def assign_wlo_to_cart(cart_id): try: with session_scope() as session: # determne how many wlo are needed and reserve them last_wlo_rec = retrieve_last_record(session, Wlos) order_count = count_records(session, Order, cart_id=cart_id) wlo_numbers = wlo_pool(last_wlo_rec.did, order_count) orders = retrieve_records(session, Order, cart_id=cart_id) for o in orders: if o.wlo is None: wlo = wlo_numbers.__next__() update_record(session, Order, o.did, wlo=wlo) insert(session, Wlos, did=wlo) except Exception as exc: _, _, exc_traceback = sys.exc_info() tb = format_traceback(exc, exc_traceback) mlogger.error('Unhandled error on assigning wlo to cart.' f'Traceback: {tb}') raise BabelError(exc)
def validate_cart_data(cart_id): issues = OrderedDict() iss_count = 0 with session_scope() as session: cart_rec = retrieve_record(session, Cart, did=cart_id) if cart_rec.system_id == 1 and cart_rec.library_id != 1: iss_count += 1 issues[0] = "BPL cart library parameter must be set to 'branches'" elif cart_rec.system_id == 2: if not cart_rec.library_id or cart_rec.library_id == 3: iss_count += 1 issues[0] = 'NYPL carts must specify library' n = 0 order_records = retrieve_records(session, Order, cart_id=cart_id) for o in order_records: ord_issues = [] n += 1 if not o.lang_id: iss_count += 1 ord_issues.append('language') if not o.audn_id: iss_count += 1 ord_issues.append('audience') if not o.vendor_id: iss_count += 1 ord_issues.append('vendor') if not o.matType_id: iss_count += 1 ord_issues.append('material type') if not o.resource.title: iss_count += 1 ord_issues.append('title') if not o.resource.price_disc: iss_count += 1 ord_issues.append('discount price') grid_issues = OrderedDict() m = 0 if o.locations: for l in o.locations: m += 1 loc_issues = [] if not l.branch_id: iss_count += 1 loc_issues.append('branch') if not l.shelfcode_id: iss_count += 1 loc_issues.append('shelf code') if not l.qty: iss_count += 1 loc_issues.append('quantity') if not l.fund_id: iss_count += 1 loc_issues.append('fund') else: # verify fund here valid_fund = validate_fund(session, l.fund_id, cart_rec.system_id, cart_rec.library_id, o.audn_id, o.matType_id, l.branch_id) if not valid_fund: loc_issues.append('(incorrect) fund') if loc_issues: grid_issues[m] = loc_issues else: iss_count += 1 ord_issues.append('locations') if ord_issues or grid_issues: issues[n] = (ord_issues, grid_issues) return iss_count, issues
def apply_globals_to_cart(cart_id, widgets): try: with session_scope() as session: # order data okwargs = get_ids_for_order_boxes_values(widgets) # locations dist_id, grid_name = widgets['globgrid'] grid_rec = retrieve_record(session, DistGrid, distset_id=dist_id, name=grid_name) mlogger.debug(f'Applying globally grid {grid_rec}') # resource data rkwargs = {} discount = None if 'discEnt' in widgets: if widgets['discEnt'].get() != '': discount = Decimal(widgets['discEnt'].get()) if 'priceEnt' in widgets: if widgets['priceEnt'].get() != '': list_price = Decimal(widgets['priceEnt'].get()) rkwargs['price_list'] = list_price if discount: rkwargs['price_disc'] = list_price - ( (list_price * discount) / Decimal(100)) else: rkwargs['price_disc'] = list_price mlogger.debug('Global update to prices: {}, discount: {}'.format( rkwargs, discount)) ord_recs = retrieve_records(session, Order, cart_id=cart_id) for rec in ord_recs: if grid_rec: olocs = [] for l in grid_rec.gridlocations: olocs.append( OrderLocation(order_id=rec.did, branch_id=l.branch_id, shelfcode_id=l.shelfcode_id, qty=l.qty)) okwargs['locations'] = olocs update_record(session, Order, rec.did, **okwargs) if rkwargs: update_record(session, Resource, rec.resource.did, **rkwargs) session.flush() if discount is not None: rkwargs['price_disc'] = rec.resource.price_list - ( (rec.resource.price_list * discount) / Decimal(100)) update_record(session, Resource, rec.resource.did, **rkwargs) rkwargs = {} except Exception as exc: _, _, exc_traceback = sys.exc_info() tb = format_traceback(exc, exc_traceback) mlogger.error('Unhandled error on applying globals to cart.' f'Traceback: {tb}') raise BabelError(exc)
def apply_grid_to_selected_orders(order_ids, grid_id, append=False): """ Datastore transaction that appends or replaces current OrderLocation records args: order_ids: list, list of datastore order dids grid_id: int, datastore DistGrid.did append: boolean, True appends to existing locations, False replaces existing locations """ with session_scope() as session: # retrieve grid location data grid_rec = retrieve_record(session, DistGrid, did=grid_id) if append: # add to existing locations for oid in order_ids: ord_rec = retrieve_record(session, Order, did=oid) for gloc in grid_rec.gridlocations: # find duplicates and merge dup = False for oloc in ord_rec.locations: if oloc.branch_id == gloc.branch_id and \ oloc.shelfcode_id == gloc.shelfcode_id: # add quantity to existing oloc dup = True mlogger.debug( 'Updating existing ' f'OrderLocation.did={oloc.did} ' f'with new qty={oloc.qty + gloc.qty}') update_record(session, OrderLocation, oloc.did, order_id=oid, branch_id=oloc.branch_id, shelfcode_id=oloc.shelfcode_id, qty=oloc.qty + gloc.qty, fund_id=oloc.fund_id) if not dup: mlogger.debug( f'Inserting new OrderLocation for Order.did={oid} ' f'based on DistGrid.did={gloc.did}') insert_or_ignore(session, OrderLocation, order_id=oid, branch_id=gloc.branch_id, shelfcode_id=gloc.shelfcode_id, qty=gloc.qty) else: # replace existing locations for oid in order_ids: # delete exiting locaations loc_recs = retrieve_records(session, OrderLocation, order_id=oid) for oloc in loc_recs: mlogger.debug(f'Deleting OrderLocation.did={oloc.did} ' f'of order.did={oid}') delete_record(session, OrderLocation, did=oloc.did) for gloc in grid_rec.gridlocations: mlogger.debug(f'Inserting new OrderLocation based on ' f'DistGrid.did={gloc.did}') insert_or_ignore(session, OrderLocation, order_id=oid, branch_id=gloc.branch_id, shelfcode_id=gloc.shelfcode_id, qty=gloc.qty)