Example #1
0
 /**
  * helper function - contains code to mark invoice as sent,
  * maybe move it to invoice-class ?
  *
  * @param org_openpsa_invoices_invoice_dba $invoice contains invoice
  */
 private function _mark_as_sent(org_openpsa_invoices_invoice_dba $invoice)
 {
     if (!$invoice->sent) {
         $invoice->sent = time();
         if ($invoice->update()) {
             $this->_request_data['message']['message'] = sprintf($this->_l10n->get('marked invoice %s sent'), $invoice->get_label());
         } else {
             $this->_request_data['message']['message'] = sprintf($this->_l10n->get('could not mark invoice %s paid'), $invoice->get_label());
             return false;
         }
         $mc = new org_openpsa_relatedto_collector($invoice->guid, 'org_openpsa_projects_task_dba');
         $tasks = $mc->get_related_objects();
         // Close "Send invoice" task
         foreach ($tasks as $task) {
             if (org_openpsa_projects_workflow::complete($task) && !isset($args["no_redirect"])) {
                 midcom::get('uimessages')->add($this->_l10n->get('org.openpsa.invoices'), sprintf($this->_l10n->get('marked task "%s" finished'), $task->title), 'ok');
             }
         }
     }
     return true;
 }
Example #2
0
 public static function render_and_attach_pdf(org_openpsa_invoices_invoice_dba $invoice)
 {
     if ($invoice->date == 0 || $invoice->deliverydate == 0) {
         $time = time();
         if ($invoice->date == 0) {
             $invoice->date = $time;
         }
         if ($invoice->deliverydate == 0) {
             $invoice->deliverydate = $time;
         }
         $invoice->update();
     }
     // renders the pdf and attaches it to the invoice
     $client_class = midcom_baseclasses_components_configuration::get('org.openpsa.invoices', 'config')->get('invoice_pdfbuilder_class');
     if (!class_exists($client_class)) {
         debug_add('Could not find PDF renderer, aborting silently', MIDCOM_LOG_INFO);
         return false;
     }
     $pdf_builder = new $client_class($invoice);
     // tmp filename
     $tmp_dir = $GLOBALS["midcom_config"]["midcom_tempdir"];
     $title = str_replace("#", "", $invoice->get_label());
     $tmp_file = $tmp_dir . "/" . $title . ".pdf";
     // render pdf to tmp filename
     $render = $pdf_builder->render($tmp_file);
     // cleanup old attachments
     $pdf_files = org_openpsa_helpers::get_attachment_urls($invoice, "pdf_file");
     if (count($pdf_files) > 0) {
         foreach ($pdf_files as $guid => $url) {
             $attachment = new midcom_db_attachment($guid);
             $attachment->delete();
         }
     }
     $attachment = $invoice->create_attachment($title . '.pdf', $title, "application/pdf");
     if (!$attachment) {
         debug_add("Failed to create invoice attachment for pdf", MIDCOM_LOG_ERROR);
         return false;
     }
     $copy = $attachment->copy_from_file($tmp_file);
     if (!$copy) {
         debug_add("Failed to copy pdf from " . $tmp_file . " to attachment", MIDCOM_LOG_ERROR);
         return false;
     }
     // set parameter for datamanager to find the pdf
     if (!$invoice->set_parameter("midcom.helper.datamanager2.type.blobs", "guids_pdf_file", $attachment->guid . ":" . $attachment->guid) || !$attachment->set_parameter('org.openpsa.invoices', 'auto_generated', md5_file($tmp_file))) {
         debug_add("Failed to create attachment parameters, last midgard error was: " . midcom_connection::get_error_string(), MIDCOM_LOG_ERROR);
         return false;
     }
     return true;
 }
Example #3
0
$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";
                    flush();
                    $found = true;