Skip to content

Commit 8403446

Browse files
authored
Merge pull request #15 from codeccoop/fix/dolibarr-series
dolibarr series
2 parents c60c397 + 18e95d1 commit 8403446

File tree

4 files changed

+102
-16
lines changed

4 files changed

+102
-16
lines changed

forms-bridge/addons/dolibarr/api.php

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
* @package formsbridge
66
*/
77

8-
use FORMS_BRIDGE\Dolibarr_Form_Bridge;
9-
108
if ( ! defined( 'ABSPATH' ) ) {
119
exit();
1210
}
@@ -363,3 +361,58 @@ function forms_bridge_dolibarr_create_thirdparty(
363361

364362
return $response['data'];
365363
}
364+
365+
/**
366+
* Retrives the last project ref and returns the next value from the serie.
367+
*
368+
* @param array $payload Bridge payload.
369+
* @param Dolibarr_Form_Bridge $bridge Bridge object.
370+
*
371+
* @return string Next project ref.
372+
*/
373+
function forms_bridge_dolibarr_get_next_project_ref( $payload, $bridge ) {
374+
$response = $bridge
375+
->patch(
376+
array(
377+
'name' => 'dolibar-get-next-project-ref',
378+
'endpoint' => '/api/index.php/projects',
379+
'method' => 'GET',
380+
)
381+
)
382+
->submit(
383+
array(
384+
'sortfield' => 't.rowid',
385+
'sortorder' => 'DESC',
386+
'properties' => 'ref',
387+
'limit' => 1,
388+
)
389+
);
390+
391+
if ( is_wp_error( $response ) ) {
392+
return $response;
393+
}
394+
395+
if ( isset( $response['data'][0]['ref'] ) ) {
396+
$previous_project_ref = $response['data'][0]['ref'] ?: 'PJ0000-000';
397+
} else {
398+
$previous_project_ref = 'PJ0000-000';
399+
}
400+
401+
[$prefix, $number] = explode( '-', $previous_project_ref );
402+
403+
if ( ! $number ) {
404+
$number = '0';
405+
}
406+
407+
$next = strval( intval( $number ) + 1 );
408+
$digits = strlen( $number );
409+
$count = strlen( $next );
410+
411+
while ( $count < $digits ) {
412+
$next = '0' . $next;
413+
++$count;
414+
}
415+
416+
$prefix = 'PJ' . date( 'y' ) . date( 'm' );
417+
return $prefix . '-' . $next;
418+
}

forms-bridge/addons/dolibarr/jobs/next-client-code.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@
1010
}
1111

1212
/**
13-
* Sets code_client to -1 on the payload to inform Dolibarr to set this field to the
13+
* Sets code_client to 'auto' on the payload to inform Dolibarr to set this field to the
1414
* next value in the serie on thidparty creation.
1515
*
1616
* @param array $payload Bridge payload.
1717
*
1818
* @return array
1919
*/
2020
function forms_bridge_dolibarr_next_code_client( $payload ) {
21-
$payload['code_client'] = -1;
21+
$payload['code_client'] = 'auto';
2222
return $payload;
2323
}
2424

2525
return array(
2626
'title' => __( 'Next code client', 'forms-bridge' ),
2727
'description' => __(
28-
'Sets code_client to -1 to let Dolibarr fulfill the field with the next value of the serie',
28+
'Sets code_client to "auto" to let Dolibarr to fulfill the field with the next value of the serie',
2929
'forms-bridge'
3030
),
3131
'method' => 'forms_bridge_dolibarr_next_code_client',
3232
'input' => array(),
3333
'output' => array(
3434
array(
3535
'name' => 'code_client',
36-
'schema' => array( 'type' => 'integer' ),
36+
'schema' => array( 'type' => 'string' ),
3737
),
3838
),
3939
);

forms-bridge/addons/dolibarr/jobs/next-project-ref.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,37 @@
1010
}
1111

1212
/**
13-
* Sets ref to -1 on the payload to inform Dolibarr to set this field to the next
14-
* project ref in hte serie on project creation.
13+
* It queries the next valid project ref and sets its value as the 'ref' attribute of
14+
* the payload.
1515
*
16-
* @param array $payload Bridge payload.
16+
* @param array $payload Bridge payload.
17+
* @param Dolibarr_Form_Bridge $bridge Bridge object.
1718
*
1819
* @return array
1920
*/
20-
function forms_bridge_dolibarr_next_project_ref( $payload ) {
21-
$payload['ref'] = -1;
21+
function forms_bridge_dolibarr_next_project_ref( $payload, $bridge ) {
22+
$project_ref = forms_bridge_dolibarr_get_next_project_ref( $payload, $bridge );
23+
24+
if ( is_wp_error( $project_ref ) ) {
25+
return $project_ref;
26+
}
27+
28+
$payload['ref'] = $project_ref;
2229
return $payload;
2330
}
2431

2532
return array(
2633
'title' => __( 'Next project ref', 'forms-bridge' ),
2734
'description' => __(
28-
'Sets ref to -1 to let Dolibarr fulfill the field with the next value of the serie',
35+
'Query the next valid project ref',
2936
'forms-bridge',
3037
),
3138
'method' => 'forms_bridge_dolibarr_next_project_ref',
3239
'input' => array(),
3340
'output' => array(
3441
array(
3542
'name' => 'ref',
36-
'schema' => array( 'type' => 'integer' ),
43+
'schema' => array( 'type' => 'string' ),
3744
),
3845
),
3946
);

forms-bridge/includes/jobs/date-fields-to-date.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,44 @@ function forms_bridge_job_format_date_fields( $payload ) {
6969
$separator = '/';
7070
}
7171

72+
$year = null;
73+
$month = null;
74+
$day = null;
75+
7276
switch ( substr( $date_format, 0, 1 ) ) {
7377
case 'y':
74-
[$year, $month, $day] = explode( $separator, $date );
78+
$chunks = explode( $separator, $date );
79+
80+
if ( 3 === count( $chunks ) ) {
81+
[$year, $month, $day] = $chunks;
82+
}
83+
7584
break;
7685
case 'm':
77-
[$month, $day, $year] = explode( $separator, $date );
86+
$chunks = explode( $separator, $date );
87+
88+
if ( 3 === count( $chunks ) ) {
89+
[$month, $day, $year] = $chunks;
90+
}
91+
7892
break;
7993
case 'd':
80-
[$day, $month, $year] = explode( $separator, $date );
94+
$chunks = explode( $separator, $date );
95+
96+
if ( 3 === count( $chunks ) ) {
97+
[$day, $month, $year] = $chunks;
98+
}
99+
81100
break;
82101
}
83102

103+
if ( ! $year || ! $month || ! $day ) {
104+
return new WP_Error(
105+
'invalid-date',
106+
__( 'Invalid date format', 'forms-bridge' )
107+
);
108+
}
109+
84110
$date = "{$year}-{$month}-{$day}";
85111

86112
if ( preg_match( '/(am|pm)/i', $hour, $matches ) ) {

0 commit comments

Comments
 (0)