def create_purchase_order(request, supplier_id): """Creates a purchase order and assigns all RequestedItems to PurchaseItems that are associated with the new purchase order""" supplier = get_object_or_404(Supplier, pk=supplier_id) po = PurchaseOrder(supplier=supplier) po.save() ritem_list = supplier.get_requested_items() for ritem in ritem_list: ritem.satatus = RequestedItem.StatusEnums.STATUS_IN_ORDER sizi = ritem.size_info coli = ritem.colour_info styi = ritem.style_info cost = Decimal(0.00) sale_infos = SaleInfo.objects.filter(supplier=supplier, gallery_item=ritem.gallery_item) if sale_infos.exists(): if sale_infos.count() == 0: raise BaseException("Unable to locate matching sale information") elif sale_infos.count() == 1: si = list(sale_infos[:1])[0] cost = si.supplier_cost else: for si in sale_infos: if si.is_match(sizi, coli, styi): print "si is %s" % type(si) print "Selected this bad boy! %s: %s" % (si, si.supplier_cost) cost = si.supplier_cost break pitem = PurchaseItem(size_info=ritem.size_info, colour_info=ritem.colour_info, style_info=ritem.style_info, purchase_order=po, requested_item=ritem, sale_cost=ritem.gallery_item.unit_cost, purchase_cost=cost) pitem.save() return redirect('/admin/inventory/purchaseorder/%s' % po.id)
def available_items(request): """Method to obtain a table of available Purchase Items""" # IF any items show up that are delivered or allocated, something went wrong - # items should only be allocated or delivered if they have a requester available_pitems = PurchaseItem.get_available_items() groups = PurchaseItem.generate_grouped_purchase_items(available_pitems) total_cost = 0 total_count = 0 for g in groups: total_count += g.count total_cost += g.c_purchase_cost return render(request, 'inventory/available_items.html', {'groups': groups, 'cost': total_cost, 'count': total_count})
def edit_available_items(request): """Edits all available items""" # IF any items show up that are delivered or allocated, something went wrong - # items should only be allocated or delivered if they have a requester mems = ClubMember.objects.all() available_pitems = PurchaseItem.get_available_items() return render(request, 'inventory/edit_available_items.html', { 'pitems': available_pitems, 'members': mems})
def list_switch_items(request, pitem_id): """Method to display a list of available purchase items with which the current item can be switched. This is useful when a member has selected something with the wrong attribute, e.g. size.""" original = get_object_or_404(PurchaseItem, pk=pitem_id) available_pitems = PurchaseItem.get_available_items() return render(request, 'inventory/switch_items.html', { 'original': original, 'pitems': available_pitems})
def save_available_item_changes(request): """Saves any changes made to available items""" available_pitems = PurchaseItem.get_available_items() for pitem in available_pitems: updated = False try: ident = 'pitem-mem-%s' % pitem.pk selected_member_id = None if ident in request.POST: selected_member_id = int(request.POST['pitem-mem-%s' % pitem.pk]) if (pitem.get_requester() is None and selected_member_id != 0) or \ (pitem.get_requester() is not None and selected_member_id != pitem.requester.pk): if selected_member_id == 0: pitem.requested_item.requester = None pitem.requested_item.save() else: pitem.requested_item.requester = ClubMember.objects.get(pk=request.POST['pitem-mem-%s' % pitem.pk]) pitem.requested_item.save() updated = True messages.success(request, "Updated %s (%s) to be requested by %s" % (pitem, pitem.pk, pitem.get_requester())) except (KeyError, ClubMember.DoesNotExist): messages.error(request, "Error attempting to update requester for %s (%s)" % (pitem, pitem.pk)) # Update finalised try: # This is hacky, but for now, we'll assume it's the best way ident = 'pitem-finalised-%s' % pitem.pk finalised = ident in request.POST if pitem.payment_fulfilled != finalised: pitem.payment_fulfilled = finalised messages.success(request, "Updated %s (%s) finalised" % (pitem, pitem.pk)) updated = True except (KeyError, ClubMember.DoesNotExist): messages.error(request, "Error attempting to update finalised for %s (%s)" % (pitem, pitem.pk)) # Update amount received try: ident = 'pitem-amt-rcvd-%s' % pitem.pk if ident in request.POST: amount_received = Decimal(request.POST[ident]) if amount_received != pitem.amount_received: pitem.amount_received = amount_received updated = True messages.success(request, "Updated %s (%s) to amount %s" % (pitem, pitem.pk, pitem.amount_received)) except (KeyError, ClubMember.DoesNotExist): messages.error(request, "Error attempting to update amount received for %s (%s)" % (pitem, pitem.pk)) if updated: pitem.save() return HttpResponseRedirect(reverse('inventory:edit_available_items'))
def member_items(request, member_id): """Method to obtain a table of available Purchase Items""" member = get_object_or_404(ClubMember, pk=member_id) available_pitems = PurchaseItem.objects.\ exclude(requested_item__order_status__exact= RequestedItem.StatusEnums.STATUS_SELECTED).\ filter(requested_item__requester__exact=member) groups = PurchaseItem.generate_grouped_purchase_items(available_pitems) total_cost = 0 total_count = 0 for g in groups: total_count += g.count total_cost += g.c_purchase_cost return render(request, 'inventory/member_items.html', {'groups': groups, 'cost': total_cost, 'count': total_count, 'member': member})
def mark_as_received_full_amount(self, request, queryset): """Marks the selected purchase info items as received and sets the received amount to the sale cost""" for item in queryset: self._mark_as_received_full_amount(item) return HttpResponseRedirect(PurchaseItem.get_admin_list_url())
def get_purchase_items_link(self, obj): """Gets a link to the admin page with the filter for this purchase order""" info = (PurchaseItem.get_admin_list_url(), obj.pk) url = '%s?purchase_order__id__exact=%s' % info return '<a href="%s">%s</a>' % (url, 'View purchase items')
def get_purchase_items_table(self, obj): """Gets a table of purchase items, grouped by attributes for this purchase order""" title = 'Purchase item information for %s' % obj table = PurchaseItem.generate_grouped_purchase_item_table(obj.purchaseitem_set.all(), table_title=title) return table