function create_recurrent_invoices($customer_id, $branch_id, $order_no, $tmpl_no, $date, $from, $to)
{
    global $Refs;
    $doc = new Cart(ST_SALESORDER, array($order_no));
    get_customer_details_to_order($doc, $customer_id, $branch_id);
    $doc->trans_type = ST_SALESORDER;
    $doc->trans_no = 0;
    $doc->document_date = $date;
    $doc->due_date = get_invoice_duedate($doc->payment, $doc->document_date);
    $doc->reference = $Refs->get_next($doc->trans_type);
    if ($doc->Comments != "") {
        $doc->Comments .= "\n";
    }
    $doc->Comments .= sprintf(_("Recurrent Invoice covers period %s - %s."), $from, add_days($to, -1));
    foreach ($doc->line_items as $line_no => $item) {
        $line =& $doc->line_items[$line_no];
        $line->price = get_price($line->stock_id, $doc->customer_currency, $doc->sales_type, $doc->price_factor, $doc->document_date);
    }
    $cart = $doc;
    $cart->trans_type = ST_SALESINVOICE;
    $cart->reference = $Refs->get_next($cart->trans_type);
    $invno = $cart->write(1);
    if ($invno == -1) {
        display_error(_("The entered reference is already in use."));
        display_footer_exit();
    }
    update_last_sent_recurrent_invoice($tmpl_no, $to);
    return $invno;
}
         $_SESSION['Items'] = new import_sales_cart($type, 0, false);
         $_SESSION['Items']->document_date = $date;
         $_SESSION['Items']->order_no = $reference;
         //order_no is the source document's original
         $com = get_customer_details_to_order($_SESSION['Items'], $customer_id, $branchNo);
         display_notification_centered($com);
         if ($com != "") {
             display_notification_centered("Error");
             $error = true;
         }
         copy_to_cart($customer_id, $branchNo, $sales_type_name, $reference, $date, $payment_id, $dimension_id, $dimension2_id, $freightcost = 0, $delfrom, $deldate, $delto, $deladdress, $contactphone, $email, $custref, $shipvia, $comments, $exrate = null);
         $firstlinecopied = true;
     }
     if ($prev_ref == $reference) {
         $docline = $docline + 1;
         $com = get_customer_details_to_order($_SESSION['Items'], $customer_id, $branchNo);
         display_notification_centered($com);
         if ($com != "") {
             display_notification_centered("Error");
             $error = true;
         }
         copy_to_cart($customer_id, $branchNo, $sales_type_name, $reference, $date, $payment_id, $dimension_id, $dimension2_id, $freightcost = 0, $delfrom, $deldate, $delto, $deladdress, $contactphone, $email, $custref, $shipvia, $comments, $exrate = null);
     }
     import_add_to_order($_SESSION['Items'], $item_code, $quantity, $price, $discountpercentage, $item_description);
     $_SESSION['Items']->cust_ref = $reference;
     if (!check_import_item_data($line_no = $docline, $item_code, $item_description, $quantity, $unit, $price, $discountpercentage) || !can_process($line, $customer_id, $branchNo, $reference, $date, $dimension_id, $dimension2_id, $freightcost = 0, $delfrom, $deldate, $delto, $deladdress, $contactphone, $email, $custref, $shipvia, $comments, $exrate)) {
         display_notification_centered("Error");
         $error = true;
     }
 }
 if ($prev_ref != $reference && $type < 4) {