From ba7b765d266d222f6838e8aa1283b386fb954a05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Garc=C3=ADa?= Date: Tue, 13 Jan 2026 12:09:05 +0100 Subject: [PATCH 1/2] fix: dolibarr next project ref job --- forms-bridge/addons/dolibarr/api.php | 57 ++++++++++++++++++- .../addons/dolibarr/jobs/next-client-code.php | 8 +-- .../addons/dolibarr/jobs/next-project-ref.php | 21 ++++--- 3 files changed, 73 insertions(+), 13 deletions(-) diff --git a/forms-bridge/addons/dolibarr/api.php b/forms-bridge/addons/dolibarr/api.php index 6cddd30..247dc74 100644 --- a/forms-bridge/addons/dolibarr/api.php +++ b/forms-bridge/addons/dolibarr/api.php @@ -5,8 +5,6 @@ * @package formsbridge */ -use FORMS_BRIDGE\Dolibarr_Form_Bridge; - if ( ! defined( 'ABSPATH' ) ) { exit(); } @@ -363,3 +361,58 @@ function forms_bridge_dolibarr_create_thirdparty( return $response['data']; } + +/** + * Retrives the last project ref and returns the next value from the serie. + * + * @param array $payload Bridge payload. + * @param Dolibarr_Form_Bridge $bridge Bridge object. + * + * @return string Next project ref. + */ +function forms_bridge_dolibarr_get_next_project_ref( $payload, $bridge ) { + $response = $bridge + ->patch( + array( + 'name' => 'dolibar-get-next-project-ref', + 'endpoint' => '/api/index.php/projects', + 'method' => 'GET', + ) + ) + ->submit( + array( + 'sortfield' => 't.rowid', + 'sortorder' => 'DESC', + 'properties' => 'ref', + 'limit' => 1, + ) + ); + + if ( is_wp_error( $response ) ) { + return $response; + } + + if ( isset( $response['data'][0]['ref'] ) ) { + $previous_project_ref = $response['data'][0]['ref'] ?: 'PJ0000-000'; + } else { + $previous_project_ref = 'PJ0000-000'; + } + + [$prefix, $number] = explode( '-', $previous_project_ref ); + + if ( ! $number ) { + $number = '0'; + } + + $next = strval( intval( $number ) + 1 ); + $digits = strlen( $number ); + $count = strlen( $next ); + + while ( $count < $digits ) { + $next = '0' . $next; + ++$count; + } + + $prefix = 'PJ' . date( 'y' ) . date( 'm' ); + return $prefix . '-' . $next; +} diff --git a/forms-bridge/addons/dolibarr/jobs/next-client-code.php b/forms-bridge/addons/dolibarr/jobs/next-client-code.php index effbde7..37620b0 100644 --- a/forms-bridge/addons/dolibarr/jobs/next-client-code.php +++ b/forms-bridge/addons/dolibarr/jobs/next-client-code.php @@ -10,7 +10,7 @@ } /** - * Sets code_client to -1 on the payload to inform Dolibarr to set this field to the + * Sets code_client to 'auto' on the payload to inform Dolibarr to set this field to the * next value in the serie on thidparty creation. * * @param array $payload Bridge payload. @@ -18,14 +18,14 @@ * @return array */ function forms_bridge_dolibarr_next_code_client( $payload ) { - $payload['code_client'] = -1; + $payload['code_client'] = 'auto'; return $payload; } return array( 'title' => __( 'Next code client', 'forms-bridge' ), 'description' => __( - 'Sets code_client to -1 to let Dolibarr fulfill the field with the next value of the serie', + 'Sets code_client to "auto" to let Dolibarr to fulfill the field with the next value of the serie', 'forms-bridge' ), 'method' => 'forms_bridge_dolibarr_next_code_client', @@ -33,7 +33,7 @@ function forms_bridge_dolibarr_next_code_client( $payload ) { 'output' => array( array( 'name' => 'code_client', - 'schema' => array( 'type' => 'integer' ), + 'schema' => array( 'type' => 'string' ), ), ), ); diff --git a/forms-bridge/addons/dolibarr/jobs/next-project-ref.php b/forms-bridge/addons/dolibarr/jobs/next-project-ref.php index 70e1760..b79ae25 100644 --- a/forms-bridge/addons/dolibarr/jobs/next-project-ref.php +++ b/forms-bridge/addons/dolibarr/jobs/next-project-ref.php @@ -10,22 +10,29 @@ } /** - * Sets ref to -1 on the payload to inform Dolibarr to set this field to the next - * project ref in hte serie on project creation. + * It queries the next valid project ref and sets its value as the 'ref' attribute of + * the payload. * - * @param array $payload Bridge payload. + * @param array $payload Bridge payload. + * @param Dolibarr_Form_Bridge $bridge Bridge object. * * @return array */ -function forms_bridge_dolibarr_next_project_ref( $payload ) { - $payload['ref'] = -1; +function forms_bridge_dolibarr_next_project_ref( $payload, $bridge ) { + $project_ref = forms_bridge_dolibarr_get_next_project_ref( $payload, $bridge ); + + if ( is_wp_error( $project_ref ) ) { + return $project_ref; + } + + $payload['ref'] = $project_ref; return $payload; } return array( 'title' => __( 'Next project ref', 'forms-bridge' ), 'description' => __( - 'Sets ref to -1 to let Dolibarr fulfill the field with the next value of the serie', + 'Query the next valid project ref', 'forms-bridge', ), 'method' => 'forms_bridge_dolibarr_next_project_ref', @@ -33,7 +40,7 @@ function forms_bridge_dolibarr_next_project_ref( $payload ) { 'output' => array( array( 'name' => 'ref', - 'schema' => array( 'type' => 'integer' ), + 'schema' => array( 'type' => 'string' ), ), ), ); From 18e95d1ca29c006017c2bd33a8724975dfe36184 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Garc=C3=ADa?= Date: Tue, 13 Jan 2026 12:32:37 +0100 Subject: [PATCH 2/2] fix: date fields to date warnings --- .../includes/jobs/date-fields-to-date.php | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/forms-bridge/includes/jobs/date-fields-to-date.php b/forms-bridge/includes/jobs/date-fields-to-date.php index e3b0833..880d5a3 100644 --- a/forms-bridge/includes/jobs/date-fields-to-date.php +++ b/forms-bridge/includes/jobs/date-fields-to-date.php @@ -69,18 +69,44 @@ function forms_bridge_job_format_date_fields( $payload ) { $separator = '/'; } + $year = null; + $month = null; + $day = null; + switch ( substr( $date_format, 0, 1 ) ) { case 'y': - [$year, $month, $day] = explode( $separator, $date ); + $chunks = explode( $separator, $date ); + + if ( 3 === count( $chunks ) ) { + [$year, $month, $day] = $chunks; + } + break; case 'm': - [$month, $day, $year] = explode( $separator, $date ); + $chunks = explode( $separator, $date ); + + if ( 3 === count( $chunks ) ) { + [$month, $day, $year] = $chunks; + } + break; case 'd': - [$day, $month, $year] = explode( $separator, $date ); + $chunks = explode( $separator, $date ); + + if ( 3 === count( $chunks ) ) { + [$day, $month, $year] = $chunks; + } + break; } + if ( ! $year || ! $month || ! $day ) { + return new WP_Error( + 'invalid-date', + __( 'Invalid date format', 'forms-bridge' ) + ); + } + $date = "{$year}-{$month}-{$day}"; if ( preg_match( '/(am|pm)/i', $hour, $matches ) ) {