public static function pay_subscription($subscription_id, $month, $account_from, $finish_month = false) { /* Pak de rekeningnummer van CityPark. */ $account_to = Config::$bank_account; /* Haal de maand prijs op en vermenigvuldig dit met $month. */ $amount = $month * Settings::get('citypark_subscription_month_cost'); /* Kijken of we de laatste paar dagen moeten berekenen. */ if ($finish_month) { /* Haal totaal aantal dagen op van deze maand. */ $days_in_month = Date('t'); /* Bereken het resterende aantal dagen op van deze maand. */ $days = $days_in_month - Date('j'); /* Bereken de kosten van 1 dag deze maand. */ $amount_one_day = $amount / $days_in_month; /* Bereken de kosten van de overgebleven dagen. */ $amount_left = $amount_one_day * $days; /* Rond het getal af omhoog, en replace een eventuele komma met een punt. */ $amount = str_replace(',', '.', round($amount_left, 2, PHP_ROUND_HALF_UP)); } /* Kijken of de transactie is gelukt. */ if (Bank_api::transfer($amount, $account_from, $account_to)) { /* Succesvol overgemaakt, update query maken. */ $update = Database::query("UPDATE subscription SET last_paid = NOW() WHERE sub_number = " . Database::escape($subscription_id)); /* Kijken of de query is gelukt. */ if ($update) { /* Customer ID ophalen. */ $select = Database::query("SELECT customer_id FROM subscription WHERE sub_number = " . Database::escape($subscription_id) . " LIMIT 1"); /* Kijken of de select query gelukt is. */ if ($select) { /* Haal de customer ID op. */ $cid = $select->fetch_object()->customer_id; /* Probeer onderstaande. */ try { /* Nieuw invoice object aanmaken. */ $invoice = new Invoice(); /* Nieuw factuur aanmaken. */ $invoice_id = $invoice->new_invoice($cid); /* Factuur regel aanmaken. */ /* $amount bevat in dit geval de prijs wat er betaald is. */ $invoice->add_item('Abonnement kosten', 'Van ' . Date('d-m-Y') . ' tot ' . Date('t-m-Y') . '.', '1', $amount); /* Factuur als betaald instellen. */ $invoice->set_paid(); } catch (Exception $e) { /* Er ging wat fout bij het aanmaken van het factuur. */ throw new Exception($e->getMessage()); } /* Return een true. */ return true; } else { /* Foutmelding. */ throw new Exception('Er ging wat fout bij het ophalen van uw gegevens voor het factuur.<br />' . Database::error()); } } else { /* Query is gefaalt, gooi foutmelding. */ throw new Exception('Er ging wat fout bij het updaten van de betaling in de database.<br />' . Database::error()); } } else { /* Kon niet overmaken. */ return false; } }
print_array(Invoice::get($id)); } else { echo "failed"; exit; } ?> </p><p style="font-weight: bold;">Add an item... </p><p><?php if (Invoice::add_item($id, 270.98, 23, 'some references')) { print_array(Invoice::get_items_of($id)); } else { echo "failed"; exit; } ?> </p><p style="font-weight: bold;">Add another item... </p><p><?php if (Invoice::add_item($id, 0.05, 93, 'some other charges')) { print_array(Invoice::get_items_of($id)); } else { echo "failed"; exit; } ?> </p><p style="font-weight: bold;">Pay an invoice... </p><p><?php $data = array(); $data['id'] = $id; $data['paid_on'] = sql_date_add(today(), 30, 'day'); $data['paid_through'] = 'CHQ'; $data['paid_id'] = 'a_cheque_number'; if (Invoice::update($data)) { print_array(Invoice::get($id)); } else {
if ($_POST['action'] == 'buy_subscriptions') { $mysqli = Database::connect(); $employer = new Employer($_POST['id']); // 1. generate invoice in the system $data = array(); $data['issued_on'] = today(); $data['type'] = 'J'; $data['employer'] = $_POST['id']; $data['payable_by'] = sql_date_add($data['issued_on'], $employer->get_payment_terms_days(), 'day'); $invoice = Invoice::create($data); if ($invoice === false) { echo 'ko'; 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'));
} if ($is_replacement && !$is_free_replacement) { $referral_desc = 'Replacement fee for Invoice: ' . pad($previous_invoice, 11, '0'); } $item_added = Invoice::add_item($invoice, $subtotal, $_POST['id'], $referral_desc); if (!$item_added) { echo "ko"; exit; } if (!$is_free_replacement) { $item_added = Invoice::add_item($invoice, $discount, $_POST['id'], 'Discount'); if (!$item_added) { echo "ko"; exit; } $item_added = Invoice::add_item($invoice, $extra_charges, $_POST['id'], 'Extra charges'); if (!$item_added) { echo "ko"; exit; } } else { if ($credit_amount > 0) { $credit_note_desc = 'Refund of balance for Invoice: ' . pad($previous_invoice, 11, '0'); $filename = generate_random_string_of(8) . '.' . generate_random_string_of(8); $issued_on = today(); $expire_on = sql_date_add($issued_on, 30, 'day'); Invoice::accompany_credit_note_with($previous_invoice, $invoice, $issued_on, $credit_amount); $branch = $employer->get_branch(); $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']);
if (!$invoice) { log_activity('Cannot create blank invoice. All the jobs for employer: ' . $id . ' skipped.', 'yellowel_job_invoice_generator.log'); continue; } log_activity('Created a blank invoice with ID: ' . $invoice, 'yellowel_job_invoice_generator.log'); log_activity('Entering inner main loop...', 'yellowel_job_invoice_generator.log'); $sub_query = ''; foreach ($jobs as $i => $job) { $desc = '[' . $job['id'] . '] ' . $job['title']; $total_fee = 10; if (count($extensions) > 0 && !empty($extensions)) { if (array_key_exists($job['id'], $extensions)) { $total_fee += 10 * $extensions[$job['id']]; } } $item_added = Invoice::add_item($invoice, $total_fee, $job['id'], $desc); if (!$item_added) { log_activity('Cannot create invoice item of job: ' . $job['id'] . '. The rest of the jobs for employer: ' . $id . ' skipped.', 'yellowel_job_invoice_generator.log'); continue; } $sub_query .= $job['id']; if ($i < count($jobs) - 1) { $sub_query .= ", "; } } if (!empty($sub_query)) { log_activity('All invoice items created successfully for employer: ' . $id, 'yellowel_job_invoice_generator.log'); $sub_query = '(' . $sub_query . ')'; $query = "UPDATE jobs SET invoiced = 'Y' WHERE id IN " . $sub_query; if (!$mysqli->execute($query)) { log_activity('Cannot mark jobs for employer: ' . $id . ' as invoiced. Jobs: ' . $sub_query, 'yellowel_job_invoice_generator.log');