Пример #1
0
 function item($id = FALSE)
 {
     if ($_POST) {
         unset($_POST['send']);
         $_POST = array_map('htmlspecialchars', $_POST);
         if ($_POST['name'] != "") {
             $_POST['name'] = $_POST['name'];
             $_POST['value'] = $_POST['value'];
             $_POST['type'] = $_POST['type'];
         } else {
             if ($_POST['item_id'] == "-") {
                 $this->session->set_flashdata('message', 'error:' . $this->lang->line('messages_add_item_error'));
                 redirect('estimates/view/' . $_POST['invoice_id']);
             } else {
                 $itemvalue = Item::find_by_id($_POST['item_id']);
                 $_POST['name'] = $itemvalue->name;
                 $_POST['type'] = $itemvalue->type;
                 $_POST['value'] = $itemvalue->value;
             }
         }
         $item = InvoiceHasItem::create($_POST);
         if (!$item) {
             $this->session->set_flashdata('message', 'error:' . $this->lang->line('messages_add_item_error'));
         } else {
             $this->session->set_flashdata('message', 'success:' . $this->lang->line('messages_add_item_success'));
         }
         redirect('estimates/view/' . $_POST['invoice_id']);
     } else {
         $this->view_data['estimate'] = Invoice::find($id);
         $this->view_data['items'] = Item::find('all', array('conditions' => array('inactive=?', '0')));
         $this->theme_view = 'modal';
         $this->view_data['title'] = $this->lang->line('application_add_item');
         $this->view_data['form_action'] = 'estimates/item';
         $this->content_view = 'estimates/_item';
     }
 }
Пример #2
0
 function index()
 {
     $this->theme_view = 'blank';
     $this->load->helper(array('dompdf', 'file'));
     $timestamp = time();
     $core_settings = Setting::first();
     $date = date("Y-m-d");
     if ($core_settings->cronjob == "1" && time() > $core_settings->last_cronjob + 300) {
         $core_settings->last_cronjob = time();
         $core_settings->save();
         $this->load->database();
         //Check Subscriptions
         $sql = 'SELECT * FROM subscriptions WHERE status != "Inactive" AND end_date > "' . $date . '" AND "' . $date . '" >= next_payment ORDER BY next_payment';
         $res = $this->db->query($sql);
         $res = $res->result();
         foreach ($res as $key2 => $value2) {
             $eventline = 'New invoice created for subscription <a href="' . base_url() . 'subscriptions/view/' . $value2->id . '">#' . $value2->reference . '</a>';
             $subscription = Subscription::find($value2->id);
             $invoice = Invoice::last();
             $invoice_reference = Setting::first();
             if ($subscription) {
                 $_POST['subscription_id'] = $subscription->id;
                 $_POST['company_id'] = $subscription->company_id;
                 if ($subscription->subscribed != 0) {
                     $_POST['status'] = "Paid";
                 } else {
                     $_POST['status'] = "Open";
                 }
                 $_POST['currency'] = $subscription->currency;
                 $_POST['issue_date'] = $subscription->next_payment;
                 $_POST['due_date'] = date('Y-m-d', strtotime('+3 day', strtotime($subscription->next_payment)));
                 $_POST['currency'] = $subscription->currency;
                 $_POST['terms'] = $subscription->terms;
                 $_POST['discount'] = $subscription->discount;
                 $_POST['reference'] = $invoice_reference->invoice_reference;
                 $invoice = Invoice::create($_POST);
                 $invoiceid = Invoice::last();
                 $items = SubscriptionHasItem::find('all', array('conditions' => array('subscription_id=?', $value2->id)));
                 foreach ($items as $value) {
                     $itemvalues = array('invoice_id' => $invoiceid->id, 'item_id' => $value->item_id, 'amount' => $value->amount, 'description' => $value->description, 'value' => $value->value, 'name' => $value->name, 'type' => $value->type);
                     InvoiceHasItem::create($itemvalues);
                 }
                 $invoice_reference->update_attributes(array('invoice_reference' => $invoice_reference->invoice_reference + 1));
                 if ($invoice) {
                     $subscription->next_payment = date('Y-m-d', strtotime($subscription->frequency, strtotime($subscription->next_payment)));
                     $subscription->save();
                     //Send Invoice to Client via email
                     $this->load->library('parser');
                     $data["invoice"] = Invoice::find($invoiceid->id);
                     $data['items'] = InvoiceHasItem::find('all', array('conditions' => array('invoice_id=?', $invoiceid->id)));
                     $data["core_settings"] = Setting::first();
                     // Generate PDF
                     $html = $this->load->view($data["core_settings"]->template . '/' . 'invoices/preview', $data, true);
                     $filename = $this->lang->line('application_invoice') . '_' . $data["invoice"]->reference;
                     pdf_create($html, $filename, FALSE);
                     //email
                     $this->email->from($data["core_settings"]->email, $data["core_settings"]->company);
                     $this->email->to($data["invoice"]->company->client->email);
                     $this->email->subject($data["core_settings"]->invoice_mail_subject);
                     $this->email->attach("files/temp/" . $filename . ".pdf");
                     $due_date = date($data["core_settings"]->date_format, human_to_unix($data["invoice"]->due_date . ' 00:00:00'));
                     //Set parse values
                     $parse_data = array('client_contact' => $data["invoice"]->company->client->firstname . ' ' . $data["invoice"]->company->client->lastname, 'due_date' => $due_date, 'invoice_id' => $data["invoice"]->reference, 'client_link' => $data["core_settings"]->domain, 'company' => $data["core_settings"]->company, 'logo' => '<img src="' . base_url() . '' . $data["core_settings"]->logo . '" alt="' . $data["core_settings"]->company . '"/>', 'invoice_logo' => '<img src="' . base_url() . '' . $data["core_settings"]->invoice_logo . '" alt="' . $data["core_settings"]->company . '"/>');
                     $email_invoice = read_file('./application/views/' . $data["core_settings"]->template . '/templates/email_invoice.html');
                     $message = $this->parser->parse_string($email_invoice, $parse_data);
                     $this->email->message($message);
                     if ($this->email->send()) {
                         $data["invoice"]->update_attributes(array('status' => 'Sent', 'sent_date' => date("Y-m-d")));
                     }
                     log_message('error', $eventline);
                     unlink("files/temp/" . $filename . ".pdf");
                 }
             }
         }
         //Check Subscriptions end
         // Auto Backup every 7 days
         if ($core_settings->autobackup == "1" && time() > $core_settings->last_autobackup + 7 * 24 * 60 * 60) {
             $this->load->dbutil();
             $prefs = array('format' => 'zip', 'filename' => 'Database-auto-full-backup_' . date('Y-m-d_H-i'));
             $backup =& $this->dbutil->backup($prefs);
             if (!write_file('./files/backup/Database-auto-full-backup_' . date('Y-m-d_H-i') . '.zip', $backup)) {
                 log_message('error', "Error while creating auto database backup!");
             } else {
                 $core_settings->last_autobackup = time();
                 $core_settings->save();
                 log_message('error', "Auto backup has been created.");
             }
         }
         echo "Success";
     }
 }
Пример #3
0
 function item($id = FALSE)
 {
     if ($_POST) {
         unset($_POST['send']);
         $_POST = array_map('htmlspecialchars', $_POST);
         if ($_POST['name'] != "") {
             $_POST['name'] = $_POST['name'];
             $_POST['value'] = $_POST['value'];
             $_POST['type'] = $_POST['type'];
         } else {
             if ($_POST['item_id'] == "-") {
                 $this->session->set_flashdata('message', 'error:' . $this->lang->line('messages_add_item_error'));
                 redirect('invoices/view/' . $_POST['invoice_id']);
             } else {
                 $rebill = explode("_", $_POST['item_id']);
                 if ($rebill[0] == "rebill") {
                     $itemvalue = Expense::find_by_id($rebill[1]);
                     $_POST['name'] = $itemvalue->description;
                     $_POST['type'] = $_POST['item_id'];
                     $_POST['value'] = $itemvalue->value;
                     $itemvalue->rebill = 2;
                     $itemvalue->invoice_id = $_POST['invoice_id'];
                     $itemvalue->save();
                 } else {
                     $itemvalue = Item::find_by_id($_POST['item_id']);
                     $_POST['name'] = $itemvalue->name;
                     $_POST['type'] = $itemvalue->type;
                     $_POST['value'] = $itemvalue->value;
                 }
             }
         }
         $item = InvoiceHasItem::create($_POST);
         if (!$item) {
             $this->session->set_flashdata('message', 'error:' . $this->lang->line('messages_add_item_error'));
         } else {
             $this->session->set_flashdata('message', 'success:' . $this->lang->line('messages_add_item_success'));
         }
         redirect('invoices/view/' . $_POST['invoice_id']);
     } else {
         $this->view_data['invoice'] = Invoice::find($id);
         $this->view_data['items'] = Item::find('all', array('conditions' => array('inactive=?', '0')));
         $this->view_data['rebill'] = Expense::find('all', array('conditions' => array('project_id=? and (rebill=? or invoice_id=?)', $this->view_data['invoice']->project_id, 1, $id)));
         $this->theme_view = 'modal';
         $this->view_data['title'] = $this->lang->line('application_add_item');
         $this->view_data['form_action'] = 'invoices/item';
         $this->content_view = 'invoices/_item';
     }
 }
Пример #4
0
 function item($id = FALSE)
 {
     if ($_POST) {
         unset($_POST['send']);
         $_POST = array_map('htmlspecialchars', $_POST);
         $id = $_POST['invoice_id'];
         $invoice = Invoice::find($id);
         $item_type = 'regular';
         if ($invoice->invoice_type == $this->invoice_shipment_type) {
             $item_type = 'shipping';
         }
         $is_new_item = false;
         if (isset($_POST['new_item']) && htmlspecialchars($_POST['new_item']) == "1") {
             $is_new_item = true;
             $filename = $savename = $type = '';
             if ($invoice->invoice_type != $this->invoice_shipment_type) {
                 $config['upload_path'] = self::ITEM_UPLOAD_PATH;
                 $config['encrypt_name'] = TRUE;
                 $config['allowed_types'] = '*';
                 $this->load->library('upload', $config);
                 if (!$this->upload->do_upload()) {
                     $error = $this->upload->display_errors('', ' ');
                     $this->session->set_flashdata('message', 'error:' . $error);
                     redirect('estimates/view/' . $id);
                 } else {
                     $data = array('upload_data' => $this->upload->data());
                     $filename = $data['upload_data']['orig_name'];
                     $savename = $data['upload_data']['file_name'];
                     $type = $data['upload_data']['file_type'];
                 }
             }
             unset($_POST['send']);
             unset($_POST['userfile']);
             unset($_POST['new_item']);
             unset($_POST['file-name']);
             unset($_POST['files']);
             $item_name = $item_description = $_POST['name'];
             $cost = $original_cost = $_POST['value'];
             $sku = $_POST['sku'];
             $inactive = $_POST['inactive'];
             $item_data = array('photo' => $savename, 'photo_type' => $type, 'photo_original_name' => $filename, 'type' => $item_type, 'name' => $item_name, 'value' => $original_cost, 'description' => $item_description, 'sku' => $sku, 'inactive' => $inactive);
             $item = Item::create($item_data);
             $item_id = $_POST['item_id'] = $item->id;
         } else {
             unset($_POST['send']);
             unset($_POST['userfile']);
             unset($_POST['file-name']);
             unset($_POST['files']);
             unset($_POST['new_item']);
             unset($_POST['name']);
             unset($_POST['sku']);
             unset($_POST['inactive']);
             $_POST = array_map('htmlspecialchars', $_POST);
             if ($_POST['item_id'] == "-" || $_POST['item_id'] == "0") {
                 $this->session->set_flashdata('message', 'error:' . $this->lang->line('messages_add_item_error'));
                 redirect('estimates/view/' . $id);
             }
             $item_id = $_POST['item_id'];
             $item_details = Item::find($item_id);
             $item_name = $item_details->name;
             $item_description = $item_details->description;
             $cost = empty($_POST['value']) ? $item_details->value : $_POST['value'];
             $original_cost = $item_details->value;
             $savename = $item_details->photo;
             $type = $item_details->photo_type;
             $filename = $item_details->photo_original_name;
             $sku = $item_details->sku;
             $item_type = $item_details->type;
             $inactive = $item_details->inactive;
         }
         $estimate_item_exist = InvoiceHasItem::count(array('conditions' => array('invoice_id=? AND item_id=?', $id, $item_id)));
         if ($estimate_item_exist) {
             $estimate_item = false;
             $error = $this->lang->line('messages_project_save_item_exist');
             $this->session->set_flashdata('message', 'error:' . $error);
             redirect('estimates/view/' . $id);
         } else {
             $invoice_item_data = array('invoice_id' => $id, 'item_id' => $item_id, 'project_item_id' => '0', 'photo' => $savename, 'photo_type' => $type, 'photo_original_name' => $filename, 'name' => $item_name, 'amount' => $_POST['amount'], 'description' => $item_description, 'sku' => $sku, 'value' => $cost, 'type' => $item_type, 'original_cost' => $original_cost, 'shipping_item' => $invoice->invoice_type == 'Shipment' ? '1' : '0');
             $estimate_item = InvoiceHasItem::create($invoice_item_data);
         }
         if (!$estimate_item) {
             $this->session->set_flashdata('message', 'error:' . $this->lang->line('messages_add_item_error'));
         } else {
             $this->projectlib->updateInvoiceTotal($invoice);
             $this->session->set_flashdata('message', 'success:' . $this->lang->line('messages_add_item_success'));
         }
         redirect('estimates/view/' . $_POST['invoice_id']);
     } else {
         $this->view_data['estimate'] = Invoice::find($id);
         $this->view_data['estimate_type'] = $this->view_data['estimate']->invoice_type;
         $this->view_data['items'] = Item::find('all', array('conditions' => array('inactive=?', '0')));
         $this->theme_view = 'modal';
         $this->view_data['title'] = $this->lang->line('application_add_item');
         $this->view_data['form_action'] = 'estimates/item';
         $this->content_view = 'estimates/_item';
     }
 }
 function create_invoice($id = FALSE)
 {
     $subscription = Subscription::find($id);
     $invoice = Invoice::last();
     $invoice_reference = Setting::first();
     if ($subscription) {
         $_POST['subscription_id'] = $subscription->id;
         $_POST['company_id'] = $subscription->company_id;
         if ($subscription->subscribed != 0) {
             $_POST['status'] = "Paid";
         } else {
             $_POST['status'] = "Open";
         }
         $_POST['currency'] = $subscription->currency;
         $_POST['issue_date'] = $subscription->next_payment;
         $_POST['due_date'] = date('Y-m-d', strtotime('+14 day', strtotime($subscription->next_payment)));
         $_POST['currency'] = $subscription->currency;
         $_POST['terms'] = $subscription->terms;
         $_POST['discount'] = $subscription->discount;
         $_POST['tax'] = $subscription->tax;
         $_POST['second_tax'] = $subscription->second_tax;
         $_POST['reference'] = $invoice_reference->invoice_reference;
         $invoice = Invoice::create($_POST);
         $invoiceid = Invoice::last();
         $items = SubscriptionHasItem::find('all', array('conditions' => array('subscription_id=?', $id)));
         foreach ($items as $value) {
             $itemvalues = array('invoice_id' => $invoiceid->id, 'item_id' => $value->item_id, 'amount' => $value->amount, 'description' => $value->description, 'value' => $value->value, 'name' => $value->name, 'type' => $value->type);
             InvoiceHasItem::create($itemvalues);
         }
         $invoice_reference->update_attributes(array('invoice_reference' => $invoice_reference->invoice_reference + 1));
         if (!$invoice) {
             $this->session->set_flashdata('message', 'error:' . $this->lang->line('messages_create_invoice_error'));
         } else {
             $subscription->next_payment = date('Y-m-d', strtotime($subscription->frequency, strtotime($subscription->next_payment)));
             $subscription->save();
             $this->session->set_flashdata('message', 'success:' . $this->lang->line('messages_create_invoice_success'));
         }
         redirect('subscriptions/view/' . $id);
     }
 }
Пример #6
0
 function planOrder($id = FALSE, $condition = FALSE, $plan_id = FALSE)
 {
     $this->load->helper('notification');
     $this->view_data['submenu'] = array($this->lang->line('application_back') => 'cprojects', $this->lang->line('application_overview') => 'cprojects/view/' . $id, $this->lang->line('application_tasks') => 'cprojects/tasks/' . $id, $this->lang->line('application_media') => 'cprojects/media/' . $id);
     switch ($condition) {
         case 'create':
             $plan = ProjectHasItem::find($plan_id);
             $current_inventory = $plan->shipping_available_inventory;
             if ($_POST) {
                 if (!isset($plan_id) || empty($plan_id)) {
                     $this->session->set_flashdata('message', 'error:' . $this->lang->line('messages_project_make_plan_item_empty'));
                     redirect('cprojects/view/' . $id);
                 }
                 if (!$current_inventory) {
                     $this->session->set_flashdata('message', 'error:' . $this->lang->line('messages_project_make_plan_inventory_empty'));
                     redirect('cprojects/view/' . $id);
                 }
                 $quantity = $_POST['amount'];
                 $current_inventory_available = $current_inventory - $quantity;
                 if (!$quantity) {
                     $this->session->set_flashdata('message', 'error:' . $this->lang->line('messages_project_make_plan_qty_empty'));
                     redirect('cprojects/view/' . $id);
                 }
                 if ($current_inventory_available < 0) {
                     $this->session->set_flashdata('message', 'error:' . $this->lang->line('messages_project_make_plan_qty_empty'));
                     redirect('cprojects/view/' . $id);
                 }
                 unset($_POST['send']);
                 unset($_POST['files']);
                 unset($_POST['amount']);
                 $_POST['shipping_lebel'] = '';
                 $config['upload_path'] = './files/media';
                 $config['encrypt_name'] = TRUE;
                 $config['allowed_types'] = '*';
                 $this->load->library('upload', $config);
                 if ($this->upload->do_upload()) {
                     $data = array('upload_data' => $this->upload->data());
                     $_POST['shipping_lebel'] = $data['upload_data']['file_name'];
                 }
                 unset($_POST['userfile']);
                 unset($_POST['dummy']);
                 $_POST = array_map('htmlspecialchars', $_POST);
                 $shipping_address = array('shipping_name' => $_POST['shipping_name'], 'shipping_company' => $_POST['shipping_company'], 'shipping_address' => $_POST['shipping_address'], 'shipping_city' => $_POST['shipping_city'], 'shipping_state' => $_POST['shipping_state'], 'shipping_zip' => $_POST['shipping_zip'], 'shipping_country' => $_POST['shipping_country'], 'shipping_phone' => $_POST['shipping_phone'], 'shipping_email' => $_POST['shipping_email'], 'shipping_website' => $_POST['shipping_website']);
                 unset($_POST['shipping_name']);
                 unset($_POST['shipping_company']);
                 unset($_POST['shipping_address']);
                 unset($_POST['shipping_city']);
                 unset($_POST['shipping_state']);
                 unset($_POST['shipping_zip']);
                 unset($_POST['shipping_country']);
                 unset($_POST['shipping_phone']);
                 unset($_POST['shipping_email']);
                 unset($_POST['shipping_website']);
                 $core_settings = Setting::first();
                 $_POST['reference'] = $core_settings->invoice_reference;
                 $_POST['project_id'] = $id;
                 $_POST['company_id'] = $this->client->company->id;
                 $_POST['status'] = 'Sent';
                 $_POST['issue_date'] = date('Y-m-d');
                 $_POST['due_date'] = date('Y-m-d', strtotime('+1 day'));
                 $_POST['currency'] = $core_settings->currency;
                 $_POST['terms'] = $core_settings->invoice_terms;
                 $_POST['invoice_type'] = $this->invoice_shipment_type;
                 $invoice = Invoice::create($_POST);
                 $new_invoice_reference = $_POST['reference'] + 1;
                 $invoice_id = $invoice->id;
                 $this->projectlib->addInvoiceAddress($invoice_id, true, $shipping_address);
                 $invoice_reference = Setting::first();
                 $invoice_reference->update_attributes(array('invoice_reference' => $new_invoice_reference));
                 $invoice_item_data = array('invoice_id' => $invoice_id, 'item_id' => $plan->item_id, 'project_item_id' => $plan->id, 'photo' => $plan->photo, 'photo_type' => $plan->photo_type, 'photo_original_name' => $plan->photo_original_name, 'name' => $plan->name, 'amount' => $quantity, 'type' => $plan->type, 'description' => $plan->description, 'sku' => $plan->sku, 'value' => $plan->cost, 'original_cost' => $plan->original_cost, 'shipping_item' => '1', 'shipping_method' => $plan->shipping_method, 'shipping_box_size_length' => $plan->shipping_box_size_length, 'shipping_box_size_width' => $plan->shipping_box_size_width, 'shipping_box_size_height' => $plan->shipping_box_size_height, 'shipping_box_size_weight' => $plan->shipping_box_size_weight, 'shipping_pcs_in_carton' => $plan->shipping_pcs_in_carton);
                 $item_add = InvoiceHasItem::create($invoice_item_data);
                 $plan->shipping_available_inventory = $current_inventory_available;
                 if (!$current_inventory_available) {
                     $plan->payment_status = 'invoiced';
                 }
                 $plan->save();
                 $this->projectlib->updateInvoiceTotal($invoice);
                 $this->projectlib->sendInvoice($invoice_id, false);
                 if (!$invoice) {
                     $this->session->set_flashdata('message', 'error:' . $this->lang->line('messages_create_invoice_error'));
                 } else {
                     $this->session->set_flashdata('message', 'success:' . $this->lang->line('messages_create_invoice_success'));
                 }
                 redirect('cinvoices/view/' . $invoice_id);
             } else {
                 $this->load->library('geolib');
                 $this->view_data['geolib'] = $this->geolib;
                 $this->view_data['max_qty'] = $plan->shipping_available_inventory;
                 $this->theme_view = 'modal';
                 $this->view_data['title'] = $this->lang->line('application_create_shipping_plan');
                 $this->view_data['form_action'] = 'cprojects/planOrder/' . $id . '/create/' . $plan_id;
                 $this->content_view = 'projects/client_views/_create_plan';
             }
             break;
         default:
             $this->view_data['project'] = Project::find($id);
             $this->content_view = 'cprojects/view/' . $id;
             break;
     }
 }