def create(item_id, quantity, order, row): """ Create a job (MANUFACTURING by default). If the item cannot be manufactured or if the corp does not own the blueprint needed for the item, a SUPPLY job is created. The number of runs is calculated from the needed quantity """ item = Type.objects.select_related(depth=2).get(pk=item_id) try: if item.blueprint is None: # item cannot be manufactured bp = None activity = Job.SUPPLY duration = 0 runs = quantity else: bpid = item.blueprintTypeID activity = Job.MANUFACTURING bp = OwnedBlueprint.objects.filter(typeID=bpid, copy=False).order_by('-me')[0] runs = quantity / item.portionSize if quantity % item.portionSize: runs += 1 duration = bp.manufacturing_time(runs) except IndexError: if item.metaGroupID == 2 and item.blueprint.parent_blueprint is not None: # we're trying to manufacture a T2 item without owning its BPO # we must create an OwnedBlueprint for this job only (that will # be consumed with it) # The invention policies are to be specified in InventionPolicies activity = Job.MANUFACTURING bp = InventionPolicy.blueprint(item.blueprint) runs = quantity / item.portionSize if quantity % item.portionSize: runs += 1 duration = bp.manufacturing_time(runs) bp.save() else: bp = None activity = Job.SUPPLY duration = 0 runs = quantity return Job.objects.create(order=order, row=row, item_id=item_id, blueprint=bp, runs=runs, activity=activity, duration=duration)