Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions forms-bridge/addons/dolibarr/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* @package formsbridge
*/

use FORMS_BRIDGE\Dolibarr_Form_Bridge;

if ( ! defined( 'ABSPATH' ) ) {
exit();
}
Expand Down Expand Up @@ -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;
}
8 changes: 4 additions & 4 deletions forms-bridge/addons/dolibarr/jobs/next-client-code.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@
}

/**
* 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.
*
* @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',
'input' => array(),
'output' => array(
array(
'name' => 'code_client',
'schema' => array( 'type' => 'integer' ),
'schema' => array( 'type' => 'string' ),
),
),
);
21 changes: 14 additions & 7 deletions forms-bridge/addons/dolibarr/jobs/next-project-ref.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,37 @@
}

/**
* 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',
'input' => array(),
'output' => array(
array(
'name' => 'ref',
'schema' => array( 'type' => 'integer' ),
'schema' => array( 'type' => 'string' ),
),
),
);
32 changes: 29 additions & 3 deletions forms-bridge/includes/jobs/date-fields-to-date.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand Down