I had a case like this.

A Document Content Type with Taxonomy Term like this

**Title**

**Category :** 

 - CAT A
 - CAT B
 - CAT C

**Doc Number**


When user select 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 starting number, if there is data found it will increment last number of node CAT A.

Every Category will have its own number format and reset yearly. 
This number will be stored in the Doc Number Field.

Whether there is a good idea for this case? I know PHP a little, but what makes me confused is how to know which category is selected before I save this node.

**Update**
Obstacles that I faced was to make automatic numbering based on the selected category.

Each category has its own number and reset every year. After some reading on Drupal I made my custom module like this.

    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 n.nid,
                                n.type,
                                n.title,
                                nomor.field_nomor_dokumen_value,
                                tahun.field_tahun_value,
                                tin.tid,
                                term.name
                            from node n left join field_data_field_nomor_dokumen nomor on n.nid = nomor.entity_id 
                                left join taxonomy_index tin on n.nid = tin.nid
                                left join taxonomy_term_data term on tin.tid = term.tid
                                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{ 
                    // 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 've explained well