How do you autopopulate a related field in custom module from a related field of another module?

I'm fighting against this issue since a couple of weeks and I can't figure out a way to do that.

I have a custom module for the orders management and I need to produce invoices automatically.
I'm following the example of 'ConverToInvoice.php' from Quotes module so, if I click on that button (under the Edit button in Detail View) I should create a new Invoice whose fields are pre-filled and  editable.
I have to grab fields' values from current order management.
My first problem is that a lot of those fields are related, too.

For example, I need to fill a related field called 'Suppliers' in my invoice. The value I need comes from a module (Suppliers) related to the Account, which is related to the current order management.

How can I easily grab that value and put it in my suppliers field?

I tryed to follow this guide  but it didn't worked.. probably I miss something.
I really need help, as soon as possible.

Thank you.

Parents
  • Ok that's a good news.
    I'm in troubles with 'load_relationship()' function now..
    As you told me, I called that function  on
    $bean->load_relationship($order_to_account_relationship);
    but I receive a PHP Fatal Error:
    Call to a member function load_relationship() on a non-object in /../custom/modules/Cproj_order_management01/converToInvoice.php on line 95, referer: ..=Cproj_order_management01&action=DetailView&record=420d92b1-1cde-725b-b6ef-522725fbaf91
    I'm not understanding what's the matter. I tried to declarate a new object for $bean but it didin't solve the problem.
    This is the whole code I wrote since now.

    require_once('modules/Cproj_order_management01/Cproj_order_management01.php');
    require_once('modules/AOS_Invoices/AOS_Invoices.php');
    require_once('modules/Accounts/Account.php');
    require_once('modules/a122_Suppliers/a122_Suppliers.php');
    require_once('modules/Relationships/Relationship.php');

    global $timedate;
       //your function
    function getRelationshipByModules ($m1, $m2)
    {
    global $db,$dictionary,$beanList;
    $rel = new Relationship;
    if($rel_info = $rel->retrieve_by_sides($m1, $m2, $db)){
    $bean = BeanFactory::getBean($m1);
    $rel_name = $rel_info['relationship_name'];
    foreach($bean->field_defs as $field=>$def){
    if(isset($def['relationship']) && $def['relationship']==$rel_name) {
      return(array($def['name'], $m1));
    }
    }
    } elseif($rel_info = $rel->retrieve_by_sides($m2, $m1, $db)){
    $bean = BeanFactory::getBean($m2);
    $rel_name = $rel_info['relationship_name'];
    foreach($bean->field_defs as $field=>$def){
    if(isset($def['relationship']) && $def['relationship']==$rel_name) {  
                   return(array($def['name'], $m2));
    }
       }
    }
    return(FALSE);
    }
    $invoice=new AOS_Invoices();
    //$om=new Cproj_order_management01();

    $suppliers=array();

    $order_to_account_relationship = getRelationshipByModules ('Cproj_order_management01', 'Accounts');
    $account_to_supplier_relationship = getRelationshipByModules('Accounts', 'a122_Suppliers');
    $bean->load_relationship($order_to_account_relationship);
    foreach($bean->$order_to_account_relationship->getBeans() as $account){
    $account->load_relationship($account_to_supplier_relationship);
    foreach($account->$account_to_supplier_relationship->getBeans() as $supplier){
     
    if(!array_key_exists($supplier->id, $suppliers)) 
          $suppliers[ $supplier->id] = $supplier->name;
    }
    }
    //when It works I'm gonna use that fields to fill my invoice fields
    [......]

    $invoice->save();

    ob_clean();
    header('Location: index.php?module=AOS_Invoices&action=EditView&record='.$invoice->id);
    I'm going crazy. Thanks for your help, again.

Reply
  • Ok that's a good news.
    I'm in troubles with 'load_relationship()' function now..
    As you told me, I called that function  on
    $bean->load_relationship($order_to_account_relationship);
    but I receive a PHP Fatal Error:
    Call to a member function load_relationship() on a non-object in /../custom/modules/Cproj_order_management01/converToInvoice.php on line 95, referer: ..=Cproj_order_management01&action=DetailView&record=420d92b1-1cde-725b-b6ef-522725fbaf91
    I'm not understanding what's the matter. I tried to declarate a new object for $bean but it didin't solve the problem.
    This is the whole code I wrote since now.

    require_once('modules/Cproj_order_management01/Cproj_order_management01.php');
    require_once('modules/AOS_Invoices/AOS_Invoices.php');
    require_once('modules/Accounts/Account.php');
    require_once('modules/a122_Suppliers/a122_Suppliers.php');
    require_once('modules/Relationships/Relationship.php');

    global $timedate;
       //your function
    function getRelationshipByModules ($m1, $m2)
    {
    global $db,$dictionary,$beanList;
    $rel = new Relationship;
    if($rel_info = $rel->retrieve_by_sides($m1, $m2, $db)){
    $bean = BeanFactory::getBean($m1);
    $rel_name = $rel_info['relationship_name'];
    foreach($bean->field_defs as $field=>$def){
    if(isset($def['relationship']) && $def['relationship']==$rel_name) {
      return(array($def['name'], $m1));
    }
    }
    } elseif($rel_info = $rel->retrieve_by_sides($m2, $m1, $db)){
    $bean = BeanFactory::getBean($m2);
    $rel_name = $rel_info['relationship_name'];
    foreach($bean->field_defs as $field=>$def){
    if(isset($def['relationship']) && $def['relationship']==$rel_name) {  
                   return(array($def['name'], $m2));
    }
       }
    }
    return(FALSE);
    }
    $invoice=new AOS_Invoices();
    //$om=new Cproj_order_management01();

    $suppliers=array();

    $order_to_account_relationship = getRelationshipByModules ('Cproj_order_management01', 'Accounts');
    $account_to_supplier_relationship = getRelationshipByModules('Accounts', 'a122_Suppliers');
    $bean->load_relationship($order_to_account_relationship);
    foreach($bean->$order_to_account_relationship->getBeans() as $account){
    $account->load_relationship($account_to_supplier_relationship);
    foreach($account->$account_to_supplier_relationship->getBeans() as $supplier){
     
    if(!array_key_exists($supplier->id, $suppliers)) 
          $suppliers[ $supplier->id] = $supplier->name;
    }
    }
    //when It works I'm gonna use that fields to fill my invoice fields
    [......]

    $invoice->save();

    ob_clean();
    header('Location: index.php?module=AOS_Invoices&action=EditView&record='.$invoice->id);
    I'm going crazy. Thanks for your help, again.

Children
No Data