A Document Content TypeDocument content type with Taxonomy Term like this
When user selectselects CAT A, then do some query to find last number of node with CAT A. If there is no data found then it will give a starting number, if there is data found it will increment last number of node CAT A.
Every Categorycategory will have its own number format and reset yearly. This number will be stored in the Doc NumberDoc Number Field.
function custom_node_presave($node) {
// dsm($node);
// $test = $node->changed;
// $term = taxonomy_term_load($tid); // find term
// $name = $term->name; // get term name
if ($node->type == 'dokumen') { // if type = dokumen
if (empty($node->nid)) { // if no nid then this is new
$tid = $node->field_jenis_peraturan['und'][0]['tid']; // taxonomy id
// do query to find last number
$result = db_query_range(
'select
'select n.nid,
n.type,
n.title,
nomor.field_nomor_dokumen_value,
tahun.field_tahun_value,
tin.tid,
term.name
from node n term.name
left join field_data_field_nomor_dokumen nomor
from node n left join field_data_field_nomor_dokumen nomor on n.nid = nomor.entity_id
left join taxonomy_index tin
left join taxonomy_index tin on n.nid = tin.nid
left join taxonomy_term_data term
left join taxonomy_term_data term on tin.tid = term.tid
left join field_data_field_tahun tahun
left join field_data_field_tahun tahun on n.nid = tahun.entity_id
where tin.tid = :tid',
array(':tid' => $tid));
if (!$result) { // if empty no data found
$node->field_nomor_dokumen['und'][0]['value'] = 1; // set starting number
$node->field_tahun['und'][0]['value'] = date("Y"); // set this year
}else{
else {
// if data found then set data to var $no & $tahun
foreach ($result as $item) {
$no = $item->nomor.field_nomor_dokumen_value;
$tahun = $item->tahun.field_tahun_value;
}
$ynow = date("Y"); // get current year
if ($tahun < $ynow) { // if year in last data < current year
$node->field_nomor_dokumen['und'][0]['value'] = '1'; // set starting number
$node->field_tahun['und'][0]['value'] = date("Y"); // set year = current year
}
} else {
++$no; // last number increment by 1
$node->field_nomor_dokumen['und'][0]['value'] = $no;
$node->field_tahun['und'][0]['value'] = date("Y"); // set year = current year
}
}
}
}
I hope I 'veI've explained well.