Skip to content

Commit

Permalink
Now the system can send mails
Browse files Browse the repository at this point in the history
  • Loading branch information
lillem4n committed Nov 12, 2011
1 parent 45b6817 commit 22c24ab
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 179 deletions.
63 changes: 29 additions & 34 deletions classes/controller/admin/bills.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,36 @@ public function action_index()
{
$this->xml_content_bills = $this->xml_content->appendChild($this->dom->createElement('bills'));
xml::to_XML(Bills::get(), $this->xml_content_bills, 'bill', 'id');

$session = Session::instance();
if ($message = $session->get_once('message'))
$this->add_message($message);
elseif ($error = $session->get_once('error'))
$this->add_error($error);
}

public function action_email()
{
//Get the bill data.
$bill = New Bill($_GET['invoice']);
$costumer = $bill->get_customer_data('costumer_email');

$mail = new Mail();
$mail->from('Larv IT AB', '[email protected]');
$mail->to($costumer);
$mail->subject('Invoice from Larv IT');
$mail->content('Invoice sent');
$mail->attachment(url::base('http',FALSE).'/user_content/pdf/bill_'.$_GET['invoice'].'.pdf');

if($mail->send())
{
die('Email was sent and costumer = '.$costumer);
}
else
{
die('email was not sent and costumer = '.$costumer);
}

// if email sent then update bills.invoice_sent with CURRENT_TIMESTAMP
}


public function action_new_bill()

public function action_email()
{
$session = Session::instance();
$bill_id = $this->request->param('options');
$bill = new Bill($bill_id);
if ($bill->send_mail())
$session->set('message', 'Mail sent for bill #'.$bill_id);
else
$session->set('error', 'Mail for bill #'.$bill_id.' failed!');

$this->redirect();

// if email sent then update bills.invoice_sent with CURRENT_TIMESTAMP
}


public function action_bill()
{
$this->xml_content_customers = $this->xml_content->appendChild($this->dom->createElement('customers'));
xml::to_XML(Customers::get_customers(), $this->xml_content_customers, 'customer', 'id');

if ( ! isset($_SESSION['bills']['items']))
{
$_SESSION['bills']['items']['1item'] = 1;
}
if ( ! isset($_SESSION['bills']['items'])) $_SESSION['bills']['items']['1item'] = 1;

if (count($_POST))
{
Expand Down Expand Up @@ -106,8 +99,10 @@ public function action_new_bill()
$transaction = new Transaction(NULL, $data);
// End of Create the transaction

// Make the PDF
// Set new default due date
$this->set_formdata(array('due_date' => date('Y-m-d', time() + 20*24*60*60)));

// Make the PDF
shell_exec('wkhtmltopdf '.$_SERVER['SERVER_NAME'].URL::site('bill?billnr='.$bill_id).' '.APPPATH.'user_content/pdf/bill_'.$bill_id.'.pdf');
}
else
Expand Down Expand Up @@ -137,6 +132,6 @@ public function action_mark_as_paid()
$bill->pay($pay_date);
$this->redirect();
}


}
8 changes: 4 additions & 4 deletions classes/controller/admin/customers.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function action_add_customer()

if ($post->validate())
{
$customer_id = Customer::add_customer($post->as_array());
$customer_id = Customer::add($post->as_array());
$this->add_message('Customer '.$post->get('name').' added with ID #'.$customer_id);
}
else
Expand All @@ -42,7 +42,7 @@ public function action_edit_customer()

$customer_model = new Customer($customer_id);

xml::to_XML(array('customer' => $customer_model->get_customer_data()), $this->xml_content, NULL, 'id');
xml::to_XML(array('customer' => $customer_model->get()), $this->xml_content, NULL, 'id');

if (count($_POST))
{
Expand All @@ -51,12 +51,12 @@ public function action_edit_customer()

if ($post->validate())
{
$customer_model->set_customer_data($post->as_array());
$customer_model->set($post->as_array());
$this->add_message('Customer "'.$post->get('name').'" updated');
}
}

$this->set_formdata($customer_model->get_customer_data());
$this->set_formdata($customer_model->get());
}

}
49 changes: 39 additions & 10 deletions classes/model/bill.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ public function __construct($id)
$this->data['items'] = $this->pdo->query('SELECT * FROM bills_items WHERE bill_id = '.$this->id.' ORDER BY item_id')->fetchAll(PDO::FETCH_ASSOC);
}

public function get($detail = FALSE)
{
if ($detail == FALSE) return $this->data;
elseif (isset($this->data[$detail])) return $this->data[$detail];

return FALSE;
}

/**
* Add a bill
*
Expand Down Expand Up @@ -51,14 +59,14 @@ public static function new_bill($customer_id, $due_date, $contact, $items, $comm
self::$prepared_insert->execute(array(
date('Y-m-d', $due_date),
intval($customer_id),
$customer_model->get_customer_data('name'),
$customer_model->get_customer_data('orgnr'),
$customer_model->get_customer_data('contact'),
$customer_model->get_customer_data('tel'),
$customer_model->get_customer_data('email'),
$customer_model->get_customer_data('street'),
$customer_model->get_customer_data('zip'),
$customer_model->get_customer_data('city'),
$customer_model->get('name'),
$customer_model->get('orgnr'),
$customer_model->get('contact'),
$customer_model->get('tel'),
$customer_model->get('email'),
$customer_model->get('street'),
$customer_model->get('zip'),
$customer_model->get('city'),
$comment,
$contact
));
Expand All @@ -85,10 +93,31 @@ public function pay($date = FALSE)
{
if ($date === FALSE) $date = date('Y-m-d', time());

$this->pdo->query('UPDATE bills SET paid_date = \''.date('Y-m-d', strtotime($date)).'\' WHERE id = '.$this->id);
$this->pdo->query('UPDATE transactions SET transfer_date = \''.date('Y-m-d', strtotime($date)).'\' WHERE description = \'Bill '.$this->id.'\';');
$this->pdo->query('UPDATE bills SET paid_date = \''.date('Y-m-d', strtotime($date)).'\' WHERE id = '.$this->pdo->quote($this->id));
$this->pdo->query('UPDATE transactions SET transfer_date = \''.date('Y-m-d', strtotime($date)).'\' WHERE description = \'Bill '.$this->pdo->quote($this->id).'\';');

return TRUE;
}

public function send_mail()
{
try
{
$email_response = (bool) Email::factory(Kohana::$config->load('larv.email.bill_subject'),Kohana::$config->load('larv.email.bill_message'))
->to($this->get('customer_email'))
->from(Kohana::$config->load('larv.email.from'), Kohana::$config->load('larv.email.from_name'))
->attach_file(APPPATH.'user_content/pdf/bill_'.$this->id.'.pdf')
->send($errors);
}
catch (Swift_RfcComplianceException $e)
{
// If the email address does not pass RFC Compliance
return FALSE;
}

if ($email_response) $this->pdo->query('UPDATE bills SET email_sent = CURRENT_TIMESTAMP() WHERE id = '.$this->pdo->quote($this->id));

return $email_response;
}

}
33 changes: 16 additions & 17 deletions classes/model/customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
class Model_Customer extends Model
{

private $customer_id;
private $customer_data;
private $id;
private $data;

public function __construct($customer_id)
public function __construct($id)
{
parent::__construct();

$this->customer_id = (int)$customer_id;
$this->customer_data = $this->pdo->query('SELECT * FROM customers WHERE id = '.$this->pdo->quote($this->customer_id))->fetch(PDO::FETCH_ASSOC);
$this->id = (int) $id;
$this->data = $this->pdo->query('SELECT * FROM customers WHERE id = '.$this->id)->fetch(PDO::FETCH_ASSOC);
}

public static function add_customer($customer_data)
public static function add($customer_data)
{
$pdo = Kohana_pdo::instance();

// Here we should really do a check so those columns actually exists. It might be an SQL-injection exploit!
$sql = 'INSERT INTO customers ('.implode(',', array_keys($customer_data)).') VALUES(';
foreach ($customer_data as $data) $sql .= $pdo->quote($data).',';
$sql = substr($sql, 0, strlen($sql) - 1).');';
Expand All @@ -27,26 +28,24 @@ public static function add_customer($customer_data)
return $pdo->lastInsertId();
}

public function get_customer_data($field = FALSE)
public function get($field = FALSE)
{
if ($field && isset($this->customer_data[$field]))
{
return $this->customer_data[$field];
}
if ($field && isset($this->data[$field]))
return $this->data[$field];
elseif ($field != FALSE && ! isset($this->data[$field]))
return FALSE;

return $this->customer_data;
return $this->data;
}

public function set_customer_data($customer_data)
public function set($customer_data)
{
$this->customer_data = $customer_data;
$this->data = $customer_data;

$sql = 'UPDATE customers SET ';
foreach ($customer_data as $field => $data)
{
$sql .= $field.' = '.$this->pdo->quote($data).',';
}
$sql = substr($sql, 0, strlen($sql) - 1).' WHERE id = '.$this->pdo->quote($this->customer_id);
$sql = substr($sql, 0, strlen($sql) - 1).' WHERE id = '.$this->id;
return $this->pdo->exec($sql);
}

Expand Down
27 changes: 16 additions & 11 deletions config/larv.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@

return array
(
'soc_fee_levels' => array
(
'soc_fee_levels' => array(
array(
'start_age' => 0,
'end_age' => 26,
'level' => 15.49,
'start_age' => 0,
'end_age' => 26,
'level' => 15.49,
),
array(
'start_age' => 27,
'end_age' => 65,
'level' => 31.42,
'start_age' => 27,
'end_age' => 65,
'level' => 31.42,
),
array(
'start_age' => 66,
'end_age' => 73,
'level' => 10.21,
'start_age' => 66,
'end_age' => 73,
'level' => 10.21,
),
),
'email' => array(
'from' => '[email protected]',
'from_name' => 'Larv IT AB',
'bill_message' => 'This email contains an invoice from Larv IT AB',
'bill_subject' => 'Invoice',
),
);
Loading

0 comments on commit 22c24ab

Please sign in to comment.