How to populate a PDF Template with data from a Custom Relate field in Line Items

SOLUTION (THIS IS NOT UPGRADE SAFE, YOU CAN MAKE IT UPGRADE SAFE THOUGH):

After some more tinkering here in the past hour, I came up with the solution:

The fields are actually populated from modules/AOS_PDF_Templates/generatePdf.php.

If you navigate down to the populate_service_lines function, you’ll notice that this where the field values are actually populated. So a number of changes have to occur here:

  1. You need to grab the field which is storing the ID of your relate field for each line item. So you can modify lines 48-55 like so:
    You’ll notice, we now select service_policy_id, and we throw that into a variable called $lineItemsPolicies. Its important that you leave the ā€˜[$row[ā€˜id’]]’ attached to the array keys for later on.
$lineItems = array();
    $sql = "SELECT pg.id, pg.product_id, pg.group_id, pg.service_policy_id FROM aos_products_quotes pg LEFT JOIN aos_line_item_groups lig ON pg.group_id = lig.id WHERE pg.parent_type = '".$module->object_name."' AND pg.parent_id = '".$module->id."' AND pg.deleted = 0 ORDER BY lig.number ASC, pg.number ASC";
    $res = $module->db->query($sql);
	while($row = $module->db->fetchByAssoc($res)){
            $lineItemsGroups[$row['group_id']][$row['id']]= $row['product_id'];
            $lineItems[$row['id']]= $row['product_id'];
            $lineItemsPolicies[$row['id']]= $row['service_policy_id'];
	}
  1. Now that we have the ID’s, we need to pass in this variable to the ā€˜populate_group_lines’ function, as well as pass the variable into the populate_service_lines function. I would expect this to be self-explanatory.

  2. Now that we have done that, we need to cycle through the field_defs, find the ā€œ$product_quote = new AOS_Products_Quotes();ā€ line inside the populate_service_lines function. Copy that text along with the foreach statement and put it right below. You’ll only need to make a few adjustments. Here is my code, just replace the module name with yours:

$policy = new p_policies();
    foreach($policy->field_defs as $name => $arr){
        if(!((isset($arr['dbType']) && strtolower($arr['dbType']) == 'id') || $arr['type'] == 'id' || $arr['type'] == 'link')){

            $curNum = strpos($text,'$p_policies_'.$name);

            if($curNum)
            {
                if($curNum < $firstNum || $firstNum == 0)
                {
                    $firstValue = '$p_policies_'.$name;
                    $firstNum = $curNum;
                }
                if($curNum > $lastNum)
                {
                    $lastValue = '$p_policies_'.$name;
                    $lastNum = $curNum;
                }
            }
        }
    }
  1. The Last step, this where the data actually gets populated onto the PDF, at the very bottom of the page, you’ll see where they are counting the $lineItems variable. and then looping through each line item and populating the data. You simply need to add your module to the $obb variable and set the ID that you passed into this function as the value.
//Converting Line Items
        if(count($lineItems) != 0){
            foreach($lineItems as $id => $productId){
                if($productId == null || $productId == '0'){
                    $obb['AOS_Products_Quotes'] = $id;
                    $obb['p_policies'] = $lineItemsPolicies[$id]; 
                    //The line above is added, since the $productId will be blank or null, sitting inside the IF statement is fine. Notice how we are simply selecting
                    // which line item ID, and that tells us which policy ID is related, hence earlier why I said to leave the row ID in place.
                    $text .= templateParser::parse_template($linePart, $obb);
                }
            }
        }

Hope this helps someone!

1 Like