- <?php
- class Invoices extends Controller
- {
- function invoices()
- {
- parent::Controller();
- $this->load->helper('manager');
- if(!is_manager())
- {
- redirect(site_url());
- }
- $this->load->library('form_validation');
- $this->load->library('cezpdf');
- #$this->load->helper('invoices');
- $this->load->helper('sequences');
- $this->load->model('invoices_model', 'model');
- $this->load->model('shop_spares_model', 'shop_spares_model');
- //
- $this->lang->load_translation(); // Carico la lingua selezionata
- }
- /**
- * -----------------------------------------------------------------------------
- * Rilanciamo semplicemente alla lista delle marche
- * @since 30-09-2009
- * -----------+-----------------------------------------------------------------
- */
- function index()
- {
- redirect('/invoices/roll');
- }
- /**
- * -----------------------------------------------------------------------------
- * Si occupa di settare una session usata nella clausola WHERE quando faccio
- * la query in ... TODO
- *
- * @since 25-08-2009
- * @return void
- * -----------+-----------------------------------------------------------------
- *
- * -----------+-----------------------------------------------------------------
- */
- function set_filter()
- {
- $this->session->set_userdata('moto_model_filter', $this->input->post('moto_model_filter'));
- $this->session->set_userdata('moto_brand_filter', $this->input->post('moto_brand_filter'));
- redirect('/invoices/roll');
- }
- /**
- * -----------------------------------------------------------------------------
- * Visualizza la lista (roll) di tutte le fatture, previo filtro
- *
- * @since 23-10-2009
- * @return void
- * -----------------------------------------------------------------------------
- */
- function roll()
- {
- $data_view['o'] = $this->model->roll();
- $data_view['title'] = 'Fatture';
- $data_view['tpl'] = 'invoices/view_invoices_roll';
- $this->load->view('view_manager', $data_view);
- }
- /**
- * -----------------------------------------------------------------------------
- * Alias per edit/0 (giusto per chiarezza)
- *
- * -----------+-----------------------------------------------------------------
- * 05-07-2010 | Creata funzione
- * -----------+-----------------------------------------------------------------
- */
- function create()
- {
- redirect('/invoices/edit/0');
- }
- #-------------------------------------------------------------------------------
- /**
- * -----------------------------------------------------------------------------
- * Routine di editazione di un articolo
- * Notare le form_validation per tutti i campi:
- * serve per il ripopolamento automatico della form in caso di errore
- * di validazione.
- *
- * -----------+-----------------------------------------------------------------
- * 05-07-2010 | Creata funzione
- * -----------+-----------------------------------------------------------------
- */
- function edit($id = 0)
- {
- $id *= 1;
- log_message('devel', "invoices/edit/$id: start");
- if($id == 0)
- {
- // Creiamo lo slot nella tabella invoice_headers
- $id = $this->model->create_empty_invoice();
- }
- $data_view['payment_methods'] = $this->model->get_payment_methods();
- $data_view['header'] = $this->model->get_header($id);
- $data_view['rows'] = $this->model->get_rows($id);
- $data_view['customers'] = $this->model->get_customers();
- $data_view['tpl'] = 'invoices/view_invoices_edit';
- $data_view['title'] = 'Gestione fatture';
- $this->load->view('view_manager', $data_view);
- }
- /**
- * -----------------------------------------------------------------------------
- * Crea la nota credito (SENZA restituzione) di una certa fattura
- * -----------+-----------------------------------------------------------------
- * 23-08-2010 | Creata funzione
- * -----------+-----------------------------------------------------------------
- */
- function credit_note_without_return($id_invoice)
- {
- log_message('devel', "invoices/credit_note_without_return: start");
- // Creiamo lo slot nella tabella invoice_headers
- $id_credit_note = $this->model->credit_note_without_return($id_invoice);
- redirect("/invoices/edit/$id_credit_note");
- }
- /**
- * -----------------------------------------------------------------------------
- * Crea la nota credito (CON restituzione) di una certa fattura
- * -----------+-----------------------------------------------------------------
- * 24-08-2010 | Creata funzione
- * -----------+-----------------------------------------------------------------
- */
- function credit_note_with_return($id_invoice)
- {
- log_message('devel', "invoices/credit_note_with_return: start");
- // Creiamo lo slot nella tabella invoice_headers
- $id_credit_note = $this->model->credit_note_with_return($id_invoice);
- redirect("/invoices/edit/$id_credit_note");
- }
- /**
- * -----------------------------------------------------------------------------
- * Modifica il id_customer di una certa fattura
- * -----------+-----------------------------------------------------------------
- * 05-07-2010 | Creata funzione
- * -----------+-----------------------------------------------------------------
- */
- function set_header($id_invoice = 0)
- {
- $id_invoice *= 1;
- if($id_invoice)
- {
- //dump($_POST); die();
- // modifichiamo l'id_customer nella tabella invoice_headers
- $data['id_invoice'] = $id_invoice;
- $data['id_customer'] = $this->input->post('id_customer');
- $data['invoice_number'] = $this->input->post('invoice_number');
- $data['date'] = $this->input->post('date');
- $data['k'] = $this->input->post('k');
- $data['shipping_price'] = $this->input->post('shipping_price');
- $data['shipping_price_rottamoto'] = $this->input->post('shipping_price_rottamoto');
- $data['payment_method'] = $this->input->post('payment_method');
- $data['packages'] = $this->input->post('packages');
- $this->model->set_header($data);
- }
- redirect("/invoices/edit/$id_invoice");
- }
- /**
- * -----------------------------------------------------------------------------
- * Crea una nuova riga di una fattura
- * -----------+-----------------------------------------------------------------
- * 06-07-2010 | Creata funzione
- * -----------+-----------------------------------------------------------------
- */
- function new_row($id_invoice = 0)
- {
- $id_invoice *= 1;
- redirect("/invoices/edit_row/0/$id_invoice");
- }
- /**
- * -----------------------------------------------------------------------------
- * Visualizza il tpl per modificare una riga di una fattura
- * -----------+-----------------------------------------------------------------
- * 06-07-2010 | Creata funzione
- * -----------+-----------------------------------------------------------------
- */
- function edit_row($id_row = 0, $id_invoice = 0)
- {
- $id_invoice *= 1;
- $id_row *= 1;
- if($id_row == 0)
- {
- $id_row = $this->model->create_empty_row($id_invoice);
- }
- $data_view['row'] = $this->model->get_row($id_row);
- $data_view['tpl'] = 'invoices/view_row_edit';
- $data_view['title'] = 'view_row_edit';
- $this->load->view('view_manager', $data_view);
- }
- /**
- * -----------------------------------------------------------------------------
- * Salva la riga di una fattura facendo i vari calcoli
- * -----------+-----------------------------------------------------------------
- * 06-07-2010 | Creata funzione
- * 26-05-2011 | Gestione vari casi prima del save_row()
- * -----------+-----------------------------------------------------------------
- */
- function save_row($id_row = 0)
- {
- $id_row *= 1;
- $data['id_row'] = $id_row;
- $data['id_invoice'] = $this->input->post('id_invoice');
- $data['id_item'] = $this->input->post('id_item');
- $previous_id_item = $this->input->post('previous_id_item');
- $data['description'] = $this->input->post('description');
- $data['subprice'] = $this->input->post('subprice');
- $data['qty'] = $this->input->post('qty');
- $data['iva_percent'] = $this->input->post('iva_percent');
- if($previous_id_item == 0 and $data['id_item'] == 0) // Qualcosa è andato storto
- {
- $this->session->set_flashdata('flashmsg', '<span class="notice">' . 'Manca l\'id annuncio' . '</span>');
- redirect("/invoices/edit_row/{$id_row}");
- }
- elseif($previous_id_item == 0 and $this->shop_spares_model->get_status($data['id_item']) == 'D')
- {
- $this->model->set_status($data['id_item'], 'V');
- $this->model->save_row($data);
- $this->session->set_flashdata('flashmsg', '<span class="notice">' . lang('record_saved') . '</span>');
- redirect("/invoices/edit_row/{$id_row}");
- }
- elseif($previous_id_item == 0 and $this->shop_spares_model->get_status($data['id_item']) != 'D')
- {
- $this->session->set_flashdata('flashmsg', '<span class="notice">' . 'articolo non disponibile' . '</span>');
- redirect("/invoices/edit_row/{$id_row}");
- }
- else
- {
- $this->model->save_row($data);
- $this->session->set_flashdata('flashmsg', '<span class="notice">' . lang('record_saved') . '</span>');
- redirect("/invoices/edit_row/{$id_row}");
- }
- }
- /**
- * -----------------------------------------------------------------------------
- * Cancellazione di una riga di una fattura
- * -----------+-----------------------------------------------------------------
- * 06-07-2010 | Creata funzione
- * 25-05-2011 | Inserita gestione del set_status
- * 26-05-2011 | La cancellazione della riga è soggetta a delle condizioni
- * | e non più incondizionata.
- * -----------+-----------------------------------------------------------------
- */
- function delete_row($id_row = 0, $id_invoice = 0)
- {
- $id_invoice *= 1;
- $id_row *= 1;
- $row = $this->model->get_row($id_row); // x recuperare id_item
- if($row['id_item'])
- {
- $shop_item_status = $this->shop_spares_model->get_status($row['id_item']); //V|Z|D
- $invoice_type = $this->model->get_invoice_type($id_invoice); //F|N|R
- if($invoice_type == 'F' and $shop_item_status == 'V')
- {
- $this->model->set_status($row['id_item'], 'D');
- $this->model->delete_row($id_row);
- }
- elseif($invoice_type == 'R' and $shop_item_status == 'D')
- {
- $this->model->set_status($row['id_item'], 'V');
- $this->model->delete_row($id_row);
- }
- elseif($invoice_type == 'N' and $shop_item_status == 'Z')
- {
- $this->model->set_status($row['id_item'], 'V');
- $this->model->delete_row($id_row);
- }
- }
- redirect("/invoices/edit/{$id_invoice}");
- }
- /**
- * excel() genera l'excel per il mese specificato
- */
- function excel($year = 0, $month = 0)
- {
- // $year *= 1;
- // $month *= 1;
- // if($year < 2010 or $year > date('Y'))
- // {
- // $year = date('Y');
- // }
- //
- //
- // if($month < 0 or $month > 12)
- // {
- // $month = date('m');
- // }
- $rows = $this->model->excel($year, $month);
- $data_view['o'] = $rows;
- $data_view['tpl'] = 'invoices/view_excel';
- $data_view['title'] = 'EXCEL';
- $this->load->view('view_manager_100', $data_view);
- }
- /**
- * -----------------------------------------------------------------------------
- * Genera il pdf di una certa fattura
- * -----------+-----------------------------------------------------------------
- * 06-07-2010 | Creata funzione
- * -----------+-----------------------------------------------------------------
- */
- function pdf($id_invoice = 0)
- {
- $id_invoice *= 1;
- $header = $this->model->get_header($id_invoice);
- $rows = $this->model->get_rows($id_invoice);
- // Se è una nota credito, recuperiamo i riferimenti della fattura originaria
- if($header[0]['invoice_type'] == 'N')
- {
- $headerNC = $this->model->get_header($header[0]['credit_note_reference']);
- }
- $data = array();
- $data['total_price'] = 0.0;
- $data['total_subprice'] = 0.0;
- $data['total_qty'] = 0.0;
- $data['total_iva'] = 0.0;
- $data['id_item'] = '';
- $data['description'] = '';
- foreach($rows as $row)
- {
- $data['total_price'] += $row['price'];
- $data['total_qty'] += $row['qty'];
- $data['total_iva'] += $row['price'] - ($row['price'] / (1.0 + $row['iva_percent'] / 100.0));
- $data['total_subprice'] += $row['subprice'] * $row['qty'];
- }
- $rows[] = array('id_item' => '', 'description' => 'COSTI DI SPEDIZIONE', 'qty' => '', 'subprice' => '', 'iva_percent' => '', 'price' => currency($header[0]['shipping_price']));
- // Rettangolo nero in alto
- $this->cezpdf->setLineStyle(1);
- $this->cezpdf->rectangle(31, 700, 534, 112);
- // Scritte nel rettangolo
- $this->cezpdf->addText(40, 780, 22, "Rottamoto S.a.S. di Andrea D'Amelio & C.");
- $this->cezpdf->addText(40, 750, 12, "Via Frati della Mensa, 1/3 - 20063 Cassano D'Adda MI");
- $this->cezpdf->addText(40, 730, 12, "Cod.Fisc. e P.IVA 05997580963");
- // Indirizzo di fatturazione
- $this->cezpdf->ezSetDy(-150);
- $saved_y = $this->cezpdf->y;
- $this->cezpdf->addText(40, $this->cezpdf->y, 10, '<i>Indirizzo di fatturazione</i>');
- $this->cezpdf->ezSetDy(-30);
- $this->cezpdf->addText(40, $this->cezpdf->y, 12, $header[0]['business_name']);
- $this->cezpdf->ezSetDy(-15);
- $this->cezpdf->addText(40, $this->cezpdf->y, 12, $header[0]['address']);
- $this->cezpdf->ezSetDy(-15);
- $this->cezpdf->addText(40, $this->cezpdf->y, 12, $header[0]['postal_code'] . ' ' . $header[0]['town'] . ' (' . $header[0]['prov'] . ')');
- $this->cezpdf->ezSetDy(-15);
- $this->cezpdf->addText(40, $this->cezpdf->y, 12, $header[0]['country']);
- $this->cezpdf->ezSetDy(-15);
- $this->cezpdf->ezSetDy(-15);
- $this->cezpdf->addText(40, $this->cezpdf->y, 12, 'Cod.Fisc.: ' . $header[0]['cf']);
- $this->cezpdf->ezSetDy(-15);
- $this->cezpdf->addText(40, $this->cezpdf->y, 12, 'P.IVA: ' . $header[0]['vat']);
- $this->cezpdf->ezSetDy(-15);
- $this->cezpdf->ezSetDy(-15);
- if($header[0]['invoice_type'] == 'F')
- {
- $this->cezpdf->addText(40, $this->cezpdf->y, 12, 'Nr. Fattura: ' . $header[0]['invoice_number']);
- $this->cezpdf->ezSetDy(-15);
- }
- elseif($header[0]['invoice_type'] == 'N')
- {
- $this->cezpdf->addText(40, $this->cezpdf->y, 12, 'Nr. Nota credito: ' . $header[0]['invoice_number'] . ' (Rif. fattura nr. ' . $headerNC[0]['invoice_number'] . ' del ' . $headerNC[0]['date'] . ')');
- $this->cezpdf->ezSetDy(-15);
- }
- else
- {
- show_error('Tipo documento non valido: ' . $header[0]['invoice_type']);
- }
- $this->cezpdf->addText(40, $this->cezpdf->y, 12, 'Data documento: ' . $header[0]['date']);
- $this->cezpdf->ezSetDy(-15);
- // Indirizzo di spedizione
- $this->cezpdf->ezSetY($saved_y);
- $this->cezpdf->addText(300, $this->cezpdf->y, 10, '<i>Indirizzo di consegna</i>');
- $this->cezpdf->ezSetDy(-30);
- $this->cezpdf->addText(300, $this->cezpdf->y, 12, $header[0]['delivery_business_name']);
- $this->cezpdf->ezSetDy(-15);
- $this->cezpdf->addText(300, $this->cezpdf->y, 12, $header[0]['delivery_address']);
- $this->cezpdf->ezSetDy(-15);
- $this->cezpdf->addText(300, $this->cezpdf->y, 12, $header[0]['delivery_postal_code'] . ' ' . $header[0]['delivery_town'] . ' (' . $header[0]['delivery_prov'] . ')');
- $this->cezpdf->ezSetDy(-15);
- $this->cezpdf->addText(300, $this->cezpdf->y, 12, $header[0]['delivery_country']);
- $this->cezpdf->ezSetDy(-15);
- if(!empty($header[0]['reference']))
- {
- $this->cezpdf->ezSetDy(-15);
- $this->cezpdf->addText(300, $this->cezpdf->y, 12, 'c.a. ' . $header[0]['reference']);
- $this->cezpdf->ezSetDy(-15);
- }
- $this->cezpdf->rectangle(290, $this->cezpdf->y, 275, $saved_y - $this->cezpdf->y + 15);
- /*
- * Qui la tabella con le righe del documento
- */
- $this->cezpdf->ezSetDy(-100);
- $col_names = array(
- 'id_item' => 'Cod.',
- 'description' => 'Descrizione',
- 'qty' => 'Q.ta\'',
- 'subprice' => 'Prezzo',
- 'iva_percent' => 'IVA%',
- 'price' => 'Importo'
- );
- $this->cezpdf->ezTable($rows, $col_names, '', array('xPos' => 'center', 'width' => 530, 'shaded' => 0));
- $this->cezpdf->ezSetDy(-15);
- $this->cezpdf->addText(40, $this->cezpdf->y, 10, 'Operazione soggetta al regime del margine ai sensi dell\'art. 36 e seguenti DL 41 del 23/2/1996');
- $this->cezpdf->ezSetDy(-15);
- $this->cezpdf->addText(40, $this->cezpdf->y, 10, 'Trattasi esclusivamente di ricambi usati.');
- $this->cezpdf->ezSetDy(-15);
- //
- $saved_y = $this->cezpdf->y;
- //
- $totals_data = array(
- //array('key'=>'Totale oggetti', 'value'=> $data['total_qty']),
- array('key' => 'Imponibile', 'value' => currency($data['total_subprice'])),
- array('key' => 'IVA', 'value' => currency($data['total_iva'])),
- array('key' => 'Importo', 'value' => currency($data['total_price'])),
- array('key' => '+ Spese di spedizione', 'value' => currency($header[0]['shipping_price'] * $header[0]['packages'])),
- array('key' => 'Totale documento', 'value' => currency(($header[0]['shipping_price'] * $header[0]['packages']) + $data['total_price']))
- );
- $this->cezpdf->ezTable($totals_data, true, '', array('fontSize' => 14, 'showHeadings' => 0, 'shaded' => 0, 'showLines' => 1, 'xPos' => 'right', 'xOrientation' => 'left', 'cols' => array('value' => array('justification' => 'right'))));
- //
- $this->cezpdf->ezSetY($saved_y - 100);
- //
- $payment_methods = $this->model->get_payment_methods();
- $this->cezpdf->addText(40, $this->cezpdf->y, 12, 'Modalita\' di pagamento: ' . $payment_methods[$header[0]['payment_method']]);
- //
- $this->cezpdf->ezStream();
- }
- #-------------------------------------------------------------------------------
- }
- // end of class