function Approve_Commission()
 {
     $Workroom_Id = $_POST['Workroom_Id'];
     $Main_Download_Id = $Workroom_Id;
     //get_main_item_id_by_workroom_id($Workroom_Id);
     if (get_post_meta($Main_Download_Id, 'Final_Approved', true) == 'YES') {
         $this->set_error('Commission is already approved.');
     }
     // Check if all accepted user entered commission percentage is equall to 100
     $Unfinished_Queues = get_teams_as_workroom($_POST['Workroom_Id']);
     $Total_Commission_Amount = 0;
     foreach ($Unfinished_Queues as $Queues_Item) {
         //$Queues_Author_Id = get_post_field( 'post_author', $Queues_Item );
         $Queues_Author_Id = $Queues_Item->Collaborator_Id;
         $status = $Queues_Item->Invitation_Status;
         $Item_Id = $Queues_Item->Item_Id;
         $Main_Item_Id = $Queues_Item->Main_Item_Id;
         if ($status == 'Accepted') {
             $user_entered_amount = (int) get_user_meta($Queues_Author_Id, 'download-' . $Main_Download_Id, true);
             if ($user_entered_amount == 0) {
                 $this->set_error('You can not approve Comession, ' . get_the_author_meta('first_name', $Queues_Author_Id) . ' ' . get_the_author_meta('last_name', $Queues_Author_Id) . '  have not entered any amount for commission.');
             }
             // Check if he has approved his entered commission.
             $is_approved = get_user_meta($Queues_Author_Id, 'download-' . $Main_Download_Id . '-approved', true);
             if ($is_approved != 'YES') {
                 $this->set_error('You can not approve Comession, ' . get_the_author_meta('first_name', $Queues_Author_Id) . ' ' . get_the_author_meta('last_name', $Queues_Author_Id) . ' have not Approved his commission.');
             }
             /*if($Item_Id=='NOT-SOLD'){
             			$this->set_error('You can not approve Comession, The item is still not sold as Complete Version. Please ask '.get_the_author_meta('first_name', $Queues_Author_Id).' '.get_the_author_meta('last_name', $Queues_Author_Id).' to repost item with complete version.');
             		}*/
             $Total_Commission_Amount = $Total_Commission_Amount + $user_entered_amount;
         }
     }
     if ($Total_Commission_Amount == 100) {
         //Check if last ITEM is sold as Finished.
         $Last_Id = get_last_product_this_item($Main_Download_Id);
         $Collaboration_Product_Status = get_post_meta($Last_Id, '_edd_product_status');
         $status = '';
         if (is_array($Collaboration_Product_Status) && $Collaboration_Product_Status[0] != '') {
             $status = $Collaboration_Product_Status[0];
         }
         /*if(!is_finished($status)){
         			$this->set_error('The Product is still not sold as Finished. You can only approve Commission if product is Completely sold as Finished.');
         		}*/
         update_post_meta($Main_Download_Id, 'Final_Approved', 'YES');
         update_post_meta($Workroom_Id, 'Collaboration_Status', 'complete');
         //========================================================================
         //Generate Alert Id
         $Alert_Id = uniqid();
         //End Alert Id
         //update_post_meta($page_id,'Edd_Alert_Id',$Alert_Id);
         //Add Notifications and Email
         $author_id = get_current_user_id();
         $Alert_Title = '' . get_the_author_meta('display_name', $author_id) . ' Approved Final Commission in ' . get_the_title($Workroom_Id);
         $Teams = get_teams_as_workroom($Workroom_Id);
         foreach ($Teams as $team) {
             if ($team->Collaborator_Id == $author_id) {
                 continue 1;
             }
             $CommissionAlert = new stdClass();
             $CommissionAlert->Collaborator_Name = get_the_author_meta('display_name', $team->Collaborator_Id);
             $CommissionAlert->Workroom_Name = get_the_title($Workroom_Id);
             //---------------------
             $CommissionAlert->Notification_Link = $this->Get_Update_Notification_Link($Workroom_Id, $team->Collaborator_Id, $Alert_Id);
             $CommissionAlert->Notification_From = get_the_author_meta('display_name', $author_id);
             $CommissionAlert->Status = "Final Approved";
             ob_start();
             include collaboration_plugin_dir . 'templates/frontend/emails/collaboration-approved-commission-template.php';
             $Get_Template = ob_get_clean();
             ob_start();
             include collaboration_plugin_dir . 'templates/frontend/emails/collaboration-email-css.php';
             $css = ob_get_clean();
             //Add Alert
             $collection = array('Edd_Alert_Message' => $Get_Template, 'Edd_Alert_Title' => $Alert_Title, 'Edd_Alert_Id' => $Alert_Id, 'Edd_Alert_To' => $team->Collaborator_Id, 'Edd_Alert_From' => $author_id, 'Edd_Alert_Message_Url' => $this->Get_Update_Notification_Link($Workroom_Id, $team->Collaborator_Id, $Alert_Id, false));
             edd_add_alerts($collection);
             //End Add Alerts
             wp_mail('*****@*****.**', $Alert_Title, $css . $Get_Template);
             wp_mail(get_the_author_meta('user_email', $team->Collaborator_Id), $Alert_Title, $css . $Get_Template);
         }
         //End Adding notifications and emails
         //======================================================================================
         $url = add_query_arg(array('task' => 'new-product', 'wid' => $Workroom_Id), get_permalink(get_page_by_path('fes-vendor-dashboard')));
         $this->set_success('You\'ve successfully Approved commission.  Click <a href="' . $url . '" class="button" id="repost">here</a> to repost product');
     } else {
         $this->set_error('All users entered amount commission persentage is not equall to 100. Please check again and try to approve later.');
     }
 }
function get_last_product_this_item($post_id)
{
    $child_id = get_post_meta($post_id, 'Edd_Child', true);
    if ($child_id == '') {
        return $post_id;
    }
    return get_last_product_this_item($child_id);
}