Here's my test code up till now:
I've set a fixed SO number from a test install just to test. Please ignore the exact_payment_cond' field since it is only required if you have this module (which only one installation has). This works, but I think the nicest way would be to create a full workflow task with configuration so you can also set things like the invoice expiration date/number of days in relation to creation date.
PHP Code:
<?php
error_reporting(E_ERROR);
ini_set("display_errors", "on");
/*=========== Retrieve SO =============*/
// Retrieve SO crmid
global $adb;
$query = 'SELECT salesorderid FROM vtiger_salesorder WHERE salesorder_no=?';
$params = array('ORD3777');
$result = $adb->pquery($query, $params);
$so_crmid = $adb->query_result($result, 0, 'salesorderid');
// Get the webservice object for this SO
include_once 'include/Webservices/Retrieve.php';
include_once 'include/Webservices/Create.php';
include_once 'modules/Users/Users.php';
$user = new Users();
$current_user = $user->retrieveCurrentUserInfoFromFile(Users::getActiveAdminId());
try {
$wsid = vtws_getWebserviceEntityId('SalesOrder', $so_crmid);
$wsso = vtws_retrieve($wsid, $current_user);
// echo "<pre>";
// print_r($wsso);
// echo "</pre>";
} catch (WebServiceException $ex) {
echo $ex->getMessage();
}
// Create an invoice from this SO
try {
// Get the account NO
list($ws_acc_id, $crm_acc_id) = explode('x', $wsso['account_id']);
$query = 'SELECT account_no FROM vtiger_account WHERE accountid=?';
$params = array($crm_acc_id);
$result = $adb->pquery($query, $params);
$acc_no = $adb->query_result($result, 0, 'account_no');
// refactor the SO webservice object for invoice injection
if (is_array($wsso)) {
$data = $wsso;
// Overwrite data that is different for invoices
$data['LineItems'] = $wsso['pdoInformation'];
$data['invoicedate'] = date('d-m-Y');
$data['salesorder_id'] = $wsso['id'];
$data['customerno'] = $acc_no;
$data['invoicestatus'] = 'Created';
$data['sostatus'] = 'Created';
$data['exact_payment_cond'] = '3 - Betaling binnen 30 dagen';
}
$invoice = vtws_create('Invoice', $data, $current_user);
// echo "<pre>";
// print_r($data);
// echo "</pre>";
} catch (WebServiceException $ex) {
echo $ex->getMessage();
}