} } $deliverable_qb = org_openpsa_sales_salesproject_deliverable_dba::new_query_builder(); $deliverables = $deliverable_qb->execute(); foreach ($deliverables as $deliverable) { $relatedto_qb = org_openpsa_relatedto_dba::new_query_builder(); $relatedto_qb->add_constraint('toGuid', '=', $deliverable->guid); $relatedto_qb->add_constraint('fromClass', '=', 'org_openpsa_invoices_invoice_dba'); $relatedtos = $relatedto_qb->execute(); if (sizeof($relatedtos) == 0) { echo "Deliverable " . $deliverable->title . " has no invoice relatedtos, skipping\n"; flush(); } foreach ($relatedtos as $relatedto) { $invoice = new org_openpsa_invoices_invoice_dba($relatedto->fromGuid); $items = $invoice->get_invoice_items(); if (sizeof($items) == 0) { echo "Invoice " . $invoice->get_label() . " has no items, creating one for deliverable\n"; flush(); $item = new org_openpsa_invoices_invoice_item_dba(); $item->invoice = $invoice->id; $item->deliverable = $deliverable->id; $item->pricePerUnit = $invoice->sum; $item->units = 1; $item->description = $deliverable->title . ' (auto-generated)'; $item->create(); } else { $found = false; foreach ($items as $item) { if ($item->deliverable == $deliverable->id) { echo "Found invoice item for deliverable " . $deliverable->title . "\n";