Example #1
0
function send_invoice(&$_employer, $_paid_postings, $_subscription_period)
{
    if (!is_a($_employer, 'Employer')) {
        return false;
    }
    $today = date('Y-m-d');
    $branch = $_employer->getAssociatedBranch();
    $sales = 'sales.' . strtolower($branch[0]['country']) . '@yellowelevator.com';
    $branch[0]['address'] = str_replace(array("\r\n", "\r"), "\n", $branch[0]['address']);
    $branch['address_lines'] = explode("\n", $branch[0]['address']);
    $currency = Currency::getSymbolFromCountryCode($branch[0]['country']);
    if ($_paid_postings > 0) {
        // 0. get the job postings pricing and currency
        $posting_rates = $GLOBALS['postings_rates'];
        $price = $posting_rates[$currency];
        // 1. generate invoice in the system
        $data = array();
        $data['issued_on'] = $today;
        $data['type'] = 'P';
        $data['employer'] = $_POST['id'];
        $data['payable_by'] = sql_date_add($today, $_employer->getPaymentTermsInDays(), 'day');
        $invoice = Invoice::create($data);
        if ($invoice === false) {
            echo 'ko';
            exit;
        }
        $amount = $price * $_paid_postings;
        $desc = $_paid_postings . ' Job Posting(s) @ ' . $currency . ' $' . $price;
        $item_added = Invoice::addItem($invoice, $amount, '1', $desc);
        $items = array();
        $items[0]['itemdesc'] = $desc;
        $items[0]['amount'] = number_format($amount, '2', '.', ', ');
        // 2. generate the invoice as PDF file
        $pdf = new PaidPostingsInvoice();
        $pdf->AliasNbPages();
        $pdf->SetAuthor('Yellow Elevator. This invoice was automatically generated. Signature is not required.');
        $pdf->SetTitle($GLOBALS['COMPANYNAME'] . ' - Invoice ' . pad($invoice, 11, '0'));
        $pdf->SetCurrency($currency);
        $pdf->SetBranch($branch);
        $pdf->AddPage();
        $pdf->SetFont('Arial', '', 10);
        $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFillColor(54, 54, 54);
        $pdf->Cell(60, 5, "Invoice Number", 1, 0, 'C', 1);
        $pdf->Cell(1);
        $pdf->Cell(33, 5, "Issuance Date", 1, 0, 'C', 1);
        $pdf->Cell(1);
        $pdf->Cell(33, 5, "Payable By", 1, 0, 'C', 1);
        $pdf->Cell(1);
        $pdf->Cell(0, 5, "Amount Payable (" . $currency . ")", 1, 0, 'C', 1);
        $pdf->Ln(6);
        $pdf->SetTextColor(0, 0, 0);
        $pdf->Cell(60, 5, pad($invoice, 11, '0'), 1, 0, 'C');
        $pdf->Cell(1);
        $pdf->Cell(33, 5, sql_date_format($data['issued_on']), 1, 0, 'C');
        $pdf->Cell(1);
        $pdf->Cell(33, 5, sql_date_format($data['payable_by']), 1, 0, 'C');
        $pdf->Cell(1);
        $pdf->Cell(0, 5, number_format($amount, '2', '.', ', '), 1, 0, 'C');
        $pdf->Ln(6);
        $pdf->SetTextColor(255, 255, 255);
        $pdf->Cell(60, 5, "User ID", 1, 0, 'C', 1);
        $pdf->Cell(1);
        $pdf->Cell(0, 5, "Employer Name", 1, 0, 'C', 1);
        $pdf->Ln(6);
        $pdf->SetTextColor(0, 0, 0);
        $pdf->Cell(60, 5, $_employer->getId(), 1, 0, 'C');
        $pdf->Cell(1);
        $pdf->Cell(0, 5, $_employer->getName(), 1, 0, 'C');
        $pdf->Ln(10);
        $table_header = array("No.", "Item", "Amount (" . $currency . ")");
        $pdf->FancyTable($table_header, $items, number_format($amount, '2', '.', ', '));
        $pdf->Ln(13);
        $pdf->SetFont('', 'I');
        $pdf->Cell(0, 0, "This invoice was automatically generated. Signature is not required.", 0, 0, 'C');
        $pdf->Ln(6);
        $pdf->Cell(0, 5, "Payment Notice", 'LTR', 0, 'C');
        $pdf->Ln();
        $pdf->Cell(0, 5, "- Payment shall be made payable to " . $branch[0]['branch'] . ".", 'LR', 0, 'C');
        $pdf->Ln();
        $pdf->Cell(0, 5, "- To facilitate the processing of the payment, please write down the invoice number(s) on your cheque(s)/payment slip(s)", 'LBR', 0, 'C');
        $pdf->Ln(10);
        $pdf->Cell(0, 0, "E. & O. E.", 0, 0, 'C');
        $pdf->Close();
        $pdf->Output($GLOBALS['data_path'] . '/subscription_invoices/' . $invoice . '.pdf', 'F');
        // 3. sends it as an email
        $attachment = chunk_split(base64_encode(file_get_contents($GLOBALS['data_path'] . '/subscription_invoices/' . $invoice . '.pdf')));
        $subject = "Subscription Invoice " . pad($invoice, 11, '0');
        $headers = 'From: YellowElevator.com <*****@*****.**>' . "\n";
        $headers .= 'Bcc: ' . $sales . "\n";
        $headers .= 'MIME-Version: 1.0' . "\n";
        $headers .= 'Content-Type: multipart/mixed; boundary="yel_mail_sep_' . $invoice . '";' . "\n\n";
        $body = '--yel_mail_sep_' . $invoice . "\n";
        $body .= 'Content-Type: multipart/alternative; boundary="yel_mail_sep_alt_' . $invoice . '"' . "\n";
        $body .= '--yel_mail_sep_alt_' . $invoice . "\n";
        $body .= 'Content-Type: text/plain; charset="iso-8859-1"' . "\n";
        $body .= 'Content-Transfer-Encoding: 7bit"' . "\n";
        $mail_lines = file('../private/mail/employer_posting_invoice.txt');
        $message = '';
        foreach ($mail_lines as $line) {
            $message .= $line;
        }
        $message = str_replace('%employer%', $_employer->getName(), $message);
        $message = str_replace('%postings%', $_POST['paid_postings'], $message);
        $message = str_replace('%price%', $price, $message);
        $message = str_replace('%currency%', $currency, $message);
        $message = str_replace('%amount%', number_format($amount, 2, '.', ', '), $message);
        $issued_date = explode('-', $data['issued_on']);
        $issued_timestamp = $issued_date[0] . $issued_date[1] . $issued_date[2];
        $message = str_replace('%purchased_on%', date('j M', $issued_timestamp) . ', ' . $issued_date[0], $message);
        $body .= $message . "\n";
        $body .= '--yel_mail_sep_alt_' . $invoice . "--\n\n";
        $body .= '--yel_mail_sep_' . $invoice . "\n";
        $body .= 'Content-Type: application/pdf; name="yel_invoice_' . pad($invoice, 11, '0') . '.pdf"' . "\n";
        $body .= 'Content-Transfer-Encoding: base64' . "\n";
        $body .= 'Content-Disposition: attachment' . "\n";
        $body .= $attachment . "\n";
        $body .= '--yel_mail_sep_' . $invoice . "--\n\n";
        mail($_employer->getEmailAddress(), $subject, $body, $headers);
        // $handle = fopen('/tmp/email_to_'. $_employer->getEmailAddress(). '.txt', 'w');
        // fwrite($handle, 'Subject: '. $subject. "\n\n");
        // fwrite($handle, $body);
        // fclose($handle);
        unlink($GLOBALS['data_path'] . '/subscription_invoices/' . $invoice . '.pdf');
    }
    if ($_subscription_period > 0) {
        $single_subscription_prices = array('MYR' => 1000, 'SGD' => 1000);
        // 0. get the job postings pricing and currency
        $subscriptions_rates = $GLOBALS['subscriptions_rates'];
        $amount = $subscriptions_rates[$currency][$_subscription_period];
        if ($_subscription_period == 1) {
            $amount = $single_subscription_prices[$currency];
        }
        $admin_fee = 0.05 * $amount;
        $total = $admin_fee + $amount;
        // 1. generate invoice in the system
        $data = array();
        $data['issued_on'] = $today;
        $data['type'] = 'J';
        $data['employer'] = $_employer->getId();
        $data['payable_by'] = sql_date_add($today, $_employer->getPaymentTermsInDays(), 'day');
        $invoice = Invoice::create($data);
        if ($invoice === false) {
            echo 'ko';
            exit;
        }
        $desc = $_subscription_period . ' month(s) of subscription';
        $item_added = Invoice::addItem($invoice, $total, '1', $desc);
        $items = array();
        $items[0]['itemdesc'] = $desc;
        $items[0]['amount'] = number_format($amount, '2', '.', ', ');
        $items[1]['itemdesc'] = 'Administration Fee';
        $items[1]['amount'] = number_format($admin_fee, '2', '.', ', ');
        // 2. generate the PDF version to be attached to sales.xx and the employer
        $pdf = new SubscriptionInvoice();
        $pdf->AliasNbPages();
        $pdf->SetAuthor('Yellow Elevator. This invoice was automatically generated. Signature is not required.');
        $pdf->SetTitle($GLOBALS['COMPANYNAME'] . ' - Invoice ' . pad($invoice, 11, '0'));
        $pdf->SetCurrency($currency);
        $pdf->SetBranch($branch);
        $pdf->AddPage();
        $pdf->SetFont('Arial', '', 10);
        $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFillColor(54, 54, 54);
        $pdf->Cell(60, 5, "Invoice Number", 1, 0, 'C', 1);
        $pdf->Cell(1);
        $pdf->Cell(33, 5, "Issuance Date", 1, 0, 'C', 1);
        $pdf->Cell(1);
        $pdf->Cell(33, 5, "Payable By", 1, 0, 'C', 1);
        $pdf->Cell(1);
        $pdf->Cell(0, 5, "Amount Payable (" . $currency . ")", 1, 0, 'C', 1);
        $pdf->Ln(6);
        $pdf->SetTextColor(0, 0, 0);
        $pdf->Cell(60, 5, pad($invoice, 11, '0'), 1, 0, 'C');
        $pdf->Cell(1);
        $pdf->Cell(33, 5, sql_date_format($data['issued_on']), 1, 0, 'C');
        $pdf->Cell(1);
        $pdf->Cell(33, 5, sql_date_format($data['payable_by']), 1, 0, 'C');
        $pdf->Cell(1);
        $pdf->Cell(0, 5, number_format($total, '2', '.', ', '), 1, 0, 'C');
        $pdf->Ln(6);
        $pdf->SetTextColor(255, 255, 255);
        $pdf->Cell(60, 5, "User ID", 1, 0, 'C', 1);
        $pdf->Cell(1);
        $pdf->Cell(0, 5, "Employer Name", 1, 0, 'C', 1);
        $pdf->Ln(6);
        $pdf->SetTextColor(0, 0, 0);
        $pdf->Cell(60, 5, $_employer->getId(), 1, 0, 'C');
        $pdf->Cell(1);
        $pdf->Cell(0, 5, $_employer->getName(), 1, 0, 'C');
        $pdf->Ln(10);
        $table_header = array("No.", "Item", "Amount (" . $currency . ")");
        $pdf->FancyTable($table_header, $items, number_format($total, '2', '.', ', '));
        $pdf->Ln(13);
        $pdf->SetFont('', 'I');
        $pdf->Cell(0, 0, "This invoice was automatically generated. Signature is not required.", 0, 0, 'C');
        $pdf->Ln(6);
        $pdf->Cell(0, 5, "Payment Notice", 'LTR', 0, 'C');
        $pdf->Ln();
        $pdf->Cell(0, 5, "- Payment shall be made payable to " . $branch[0]['branch'] . ".", 'LR', 0, 'C');
        $pdf->Ln();
        $pdf->Cell(0, 5, "- To facilitate the processing of the payment, please write down the invoice number(s) on your cheque(s)/payment slip(s)", 'LBR', 0, 'C');
        $pdf->Ln(10);
        $pdf->Cell(0, 0, "E. & O. E.", 0, 0, 'C');
        $pdf->Close();
        $pdf->Output($GLOBALS['data_path'] . '/subscription_invoices/' . $invoice . '.pdf', 'F');
        // 3. attach it to email
        $attachment = chunk_split(base64_encode(file_get_contents($GLOBALS['data_path'] . '/subscription_invoices/' . $invoice . '.pdf')));
        $subject = "Subscription Invoice " . pad($invoice, 11, '0');
        $headers = 'From: YellowElevator.com <*****@*****.**>' . "\n";
        $headers .= 'Bcc: ' . $sales . "\n";
        $headers .= 'MIME-Version: 1.0' . "\n";
        $headers .= 'Content-Type: multipart/mixed; boundary="yel_mail_sep_' . $invoice . '";' . "\n\n";
        $body = '--yel_mail_sep_' . $invoice . "\n";
        $body .= 'Content-Type: multipart/alternative; boundary="yel_mail_sep_alt_' . $invoice . '"' . "\n";
        $body .= '--yel_mail_sep_alt_' . $invoice . "\n";
        $body .= 'Content-Type: text/plain; charset="iso-8859-1"' . "\n";
        $body .= 'Content-Transfer-Encoding: 7bit"' . "\n";
        $mail_lines = file('../private/mail/employer_subscription_invoice.txt');
        $message = '';
        foreach ($mail_lines as $line) {
            $message .= $line;
        }
        $message = str_replace('%employer%', $_employer->getName(), $message);
        $message = str_replace('%period%', $_subscription_period, $message);
        $message = str_replace('%currency%', $currency, $message);
        $message = str_replace('%amount%', number_format($total, 2, '.', ', '), $message);
        $issued_date = explode('-', $data['issued_on']);
        $issued_timestamp = $issued_date[0] . $issued_date[1] . $issued_date[2];
        $message = str_replace('%purchased_on%', date('j M', $issued_timestamp) . ', ' . $issued_date[0], $message);
        $body .= $message . "\n";
        $body .= '--yel_mail_sep_alt_' . $invoice . "--\n\n";
        $body .= '--yel_mail_sep_' . $invoice . "\n";
        $body .= 'Content-Type: application/pdf; name="yel_invoice_' . pad($invoice, 11, '0') . '.pdf"' . "\n";
        $body .= 'Content-Transfer-Encoding: base64' . "\n";
        $body .= 'Content-Disposition: attachment' . "\n";
        $body .= $attachment . "\n";
        $body .= '--yel_mail_sep_' . $invoice . "--\n\n";
        mail($_employer->getEmailAddress(), $subject, $body, $headers);
        /*$handle = fopen('/tmp/email_to_'. $_employer->getEmailAddress(). '.txt', 'w');
          fwrite($handle, 'Subject: '. $subject. "\n\n");
          fwrite($handle, $body);
          fclose($handle);*/
        unlink($GLOBALS['data_path'] . '/subscription_invoices/' . $invoice . '.pdf');
    }
    return true;
}
Example #2
0
     exit;
 }
 $desc = $_POST['period'] . ' month(s) of subscription';
 $item_added = Invoice::add_item($invoice, $_POST['amount'], '1', $desc);
 $items = array();
 $items[0]['itemdesc'] = $desc;
 $items[0]['amount'] = number_format($_POST['price'], '2', '.', ', ');
 $items[1]['itemdesc'] = 'Administration Fee';
 $items[1]['amount'] = number_format($_POST['admin_fee'], '2', '.', ', ');
 // 2. generate the PDF version to be attached to sales.xx and the employer
 $branch_raw = $employer->get_branch();
 $sales = 'sales.' . strtolower($branch_raw[0]['country']) . '@yellowelevator.com';
 $branch_raw[0]['address'] = str_replace(array("\r\n", "\r"), "\n", $branch_raw[0]['address']);
 $branch_raw['address_lines'] = explode("\n", $branch_raw[0]['address']);
 $currency = $_POST['currency'];
 $pdf = new SubscriptionInvoice();
 $pdf->AliasNbPages();
 $pdf->SetAuthor('Yellow Elevator. This invoice was automatically generated. Signature is not required.');
 $pdf->SetTitle($GLOBALS['COMPANYNAME'] . ' - Invoice ' . pad($invoice, 11, '0'));
 $pdf->SetCurrency($currency);
 $pdf->SetBranch($branch_raw);
 $pdf->AddPage();
 $pdf->SetFont('Arial', '', 10);
 $pdf->SetTextColor(255, 255, 255);
 $pdf->SetFillColor(54, 54, 54);
 $pdf->Cell(60, 5, "Invoice Number", 1, 0, 'C', 1);
 $pdf->Cell(1);
 $pdf->Cell(33, 5, "Issuance Date", 1, 0, 'C', 1);
 $pdf->Cell(1);
 $pdf->Cell(33, 5, "Payable By", 1, 0, 'C', 1);
 $pdf->Cell(1);