'Custom Method to copy an existing Request
- creates a req with req_item records'

@staticmethod
def req_copy_all(r, **attr):

    db = current.db
    s3db = current.s3db
    table = s3db.req_req
    settings = current.deployment_settings
    now = current.request.now
    record = r.record
    req_id = record.id
    if settings.get_req_use_req_number():
        code = s3db.supply_get_shipping_code(settings.get_req_shortname(), record.site_id, table.req_ref)
    else:
        code = None
    if (record.date_required and (record.date_required < now)):
        date_required = (now + datetime.timedelta(days=14))
    else:
        date_required = record.date_required
    new_req_id = table.insert(type=record.type, req_ref=code, date=now, date_required=date_required, priority=record.priority, site_id=record.site_id, purpose=record.purpose, requester_id=record.requester_id, transport_req=record.transport_req, security_req=record.security_req, comments=record.comments)
    if (record.type == 1):
        ritable = s3db.req_req_item
        items = db((ritable.req_id == req_id)).select(ritable.id, ritable.item_entity_id, ritable.item_id, ritable.item_pack_id, ritable.quantity, ritable.pack_value, ritable.currency, ritable.site_id, ritable.comments)
        if items:
            insert = ritable.insert
            for item in items:
                insert(req_id=new_req_id, item_entity_id=item.item_entity_id, item_id=item.item_id, item_pack_id=item.item_pack_id, quantity=item.quantity, pack_value=item.pack_value, currency=item.currency, site_id=item.site_id, comments=item.comments)
    elif (record.type == 3):
        rstable = s3db.req_req_skill
        skills = db((rstable.req_id == req_id)).select(rstable.id, rstable.skill_id, rstable.quantity, rstable.site_id, rstable.comments)
        if skills:
            insert = rstable.insert
            for skill in skills:
                insert(req_id=new_req_id, skill_id=skill.skill_id, quantity=skill.quantity, site_id=skill.site_id, comments=skill.comments)
    redirect(URL(f='req', args=[new_req_id, 'update']))
