public static function add_invoice_line_items(Harvest_Invoice $invoice, SI_Invoice $new_invoice) { // Format the csv string to compensate for bad formatting and missing values. $csv_string = $invoice->csv_line_items; // Create rows for each line item. Making sure to not break with wrapped line items. // https://bugs.php.net/bug.php?id=55763 $lines = preg_split('/[\\r\\n]{1,2}(?=(?:[^\\"]*\\"[^\\"]*\\")*(?![^\\"]*\\"))/', $csv_string); // Build array for each line item $row_arrays = array_map('str_getcsv', $lines); // Header row $header = array_shift($row_arrays); // Rename the key names to match what SI uses $header = str_replace(array('unit_price', 'quantity', 'description', 'kind', 'amount'), array('rate', 'qty', 'desc', 'type', 'total'), $header); // Build a line item associated array with header names $line_items = array(); foreach ($row_arrays as $row) { if (strlen(implode('', $row)) > 0) { // Don't add empty rows $line_items[] = array_combine($header, $row); } } // Set the line items meta $new_invoice->set_line_items($line_items); }