Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 19.57 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3.  
  4. class Invoices extends Controller
  5. {
  6.  
  7.  
  8.  
  9.     function invoices()
  10.     {
  11.         parent::Controller();
  12.         $this->load->helper('manager');
  13.  
  14.         if(!is_manager())
  15.         {
  16.             redirect(site_url());
  17.         }
  18.  
  19.  
  20.         $this->load->library('form_validation');
  21.         $this->load->library('cezpdf');
  22. #$this->load->helper('invoices');
  23.         $this->load->helper('sequences');
  24.         $this->load->model('invoices_model', 'model');
  25.         $this->load->model('shop_spares_model', 'shop_spares_model');
  26. //
  27.         $this->lang->load_translation();  // Carico la lingua selezionata
  28.     }
  29.  
  30.  
  31.  
  32.     /**
  33.      * -----------------------------------------------------------------------------
  34.      * Rilanciamo semplicemente alla lista delle marche
  35.      * @since 30-09-2009
  36.      * -----------+-----------------------------------------------------------------
  37.      */
  38.     function index()
  39.     {
  40.         redirect('/invoices/roll');
  41.     }
  42.  
  43.  
  44.  
  45.     /**
  46.      * -----------------------------------------------------------------------------
  47.      * Si occupa di settare una session usata nella clausola WHERE quando faccio
  48.      * la query in ... TODO
  49.      *
  50.      * @since 25-08-2009
  51.      * @return void
  52.      * -----------+-----------------------------------------------------------------
  53.      *
  54.      * -----------+-----------------------------------------------------------------
  55.      */
  56.     function set_filter()
  57.     {
  58.         $this->session->set_userdata('moto_model_filter', $this->input->post('moto_model_filter'));
  59.         $this->session->set_userdata('moto_brand_filter', $this->input->post('moto_brand_filter'));
  60.         redirect('/invoices/roll');
  61.     }
  62.  
  63.  
  64.  
  65.     /**
  66.      * -----------------------------------------------------------------------------
  67.      * Visualizza la lista (roll) di tutte le fatture, previo filtro
  68.      *
  69.      * @since 23-10-2009
  70.      * @return void
  71.      * -----------------------------------------------------------------------------
  72.      */
  73.     function roll()
  74.     {
  75.         $data_view['o'] = $this->model->roll();
  76.         $data_view['title'] = 'Fatture';
  77.         $data_view['tpl'] = 'invoices/view_invoices_roll';
  78.         $this->load->view('view_manager', $data_view);
  79.     }
  80.  
  81.  
  82.  
  83.     /**
  84.      * -----------------------------------------------------------------------------
  85.      * Alias per edit/0  (giusto per chiarezza)
  86.      *
  87.      * -----------+-----------------------------------------------------------------
  88.      * 05-07-2010 | Creata funzione
  89.      * -----------+-----------------------------------------------------------------
  90.      */
  91.     function create()
  92.     {
  93.         redirect('/invoices/edit/0');
  94.     }
  95.  
  96. #-------------------------------------------------------------------------------
  97.  
  98.  
  99.  
  100.     /**
  101.      * -----------------------------------------------------------------------------
  102.      * Routine di editazione di un articolo
  103.      * Notare le form_validation per tutti i campi:
  104.      * serve per il ripopolamento automatico della form in caso di errore
  105.      * di validazione.
  106.      *
  107.      * -----------+-----------------------------------------------------------------
  108.      * 05-07-2010 | Creata funzione
  109.      * -----------+-----------------------------------------------------------------
  110.      */
  111.     function edit($id = 0)
  112.     {
  113.         $id *= 1;
  114.  
  115.         log_message('devel', "invoices/edit/$id: start");
  116.  
  117.         if($id == 0)
  118.         {
  119. // Creiamo lo slot nella tabella invoice_headers
  120.             $id = $this->model->create_empty_invoice();
  121.         }
  122.         $data_view['payment_methods'] = $this->model->get_payment_methods();
  123.         $data_view['header'] = $this->model->get_header($id);
  124.         $data_view['rows'] = $this->model->get_rows($id);
  125.         $data_view['customers'] = $this->model->get_customers();
  126.         $data_view['tpl'] = 'invoices/view_invoices_edit';
  127.         $data_view['title'] = 'Gestione fatture';
  128.         $this->load->view('view_manager', $data_view);
  129.     }
  130.  
  131.  
  132.  
  133.     /**
  134.      * -----------------------------------------------------------------------------
  135.      * Crea la nota credito (SENZA restituzione) di una certa fattura
  136.      * -----------+-----------------------------------------------------------------
  137.      * 23-08-2010 | Creata funzione
  138.      * -----------+-----------------------------------------------------------------
  139.      */
  140.     function credit_note_without_return($id_invoice)
  141.     {
  142.  
  143.         log_message('devel', "invoices/credit_note_without_return: start");
  144. // Creiamo lo slot nella tabella invoice_headers
  145.         $id_credit_note = $this->model->credit_note_without_return($id_invoice);
  146.         redirect("/invoices/edit/$id_credit_note");
  147.     }
  148.  
  149.  
  150.  
  151.     /**
  152.      * -----------------------------------------------------------------------------
  153.      * Crea la nota credito (CON restituzione) di una certa fattura
  154.      * -----------+-----------------------------------------------------------------
  155.      * 24-08-2010 | Creata funzione
  156.      * -----------+-----------------------------------------------------------------
  157.      */
  158.     function credit_note_with_return($id_invoice)
  159.     {
  160.  
  161.         log_message('devel', "invoices/credit_note_with_return: start");
  162. // Creiamo lo slot nella tabella invoice_headers
  163.         $id_credit_note = $this->model->credit_note_with_return($id_invoice);
  164.         redirect("/invoices/edit/$id_credit_note");
  165.     }
  166.  
  167.  
  168.  
  169.     /**
  170.      * -----------------------------------------------------------------------------
  171.      * Modifica il id_customer di una certa fattura
  172.      * -----------+-----------------------------------------------------------------
  173.      * 05-07-2010 | Creata funzione
  174.      * -----------+-----------------------------------------------------------------
  175.      */
  176.     function set_header($id_invoice = 0)
  177.     {
  178.         $id_invoice *= 1;
  179.         if($id_invoice)
  180.         {
  181.            
  182.             //dump($_POST); die();
  183.            
  184.            
  185. // modifichiamo l'id_customer nella tabella invoice_headers
  186.             $data['id_invoice'] = $id_invoice;
  187.             $data['id_customer'] = $this->input->post('id_customer');
  188.             $data['invoice_number'] = $this->input->post('invoice_number');
  189.             $data['date'] = $this->input->post('date');
  190.             $data['k'] = $this->input->post('k');
  191.             $data['shipping_price'] = $this->input->post('shipping_price');
  192.             $data['shipping_price_rottamoto'] = $this->input->post('shipping_price_rottamoto');
  193.             $data['payment_method'] = $this->input->post('payment_method');
  194.             $data['packages'] = $this->input->post('packages');
  195.  
  196.             $this->model->set_header($data);
  197.         }
  198.         redirect("/invoices/edit/$id_invoice");
  199.     }
  200.  
  201.  
  202.  
  203.     /**
  204.      * -----------------------------------------------------------------------------
  205.      * Crea una nuova riga di una fattura
  206.      * -----------+-----------------------------------------------------------------
  207.      * 06-07-2010 | Creata funzione
  208.      * -----------+-----------------------------------------------------------------
  209.      */
  210.     function new_row($id_invoice = 0)
  211.     {
  212.         $id_invoice *= 1;
  213.         redirect("/invoices/edit_row/0/$id_invoice");
  214.     }
  215.  
  216.  
  217.  
  218.     /**
  219.      * -----------------------------------------------------------------------------
  220.      * Visualizza il tpl per modificare una riga di una fattura
  221.      * -----------+-----------------------------------------------------------------
  222.      * 06-07-2010 | Creata funzione
  223.      * -----------+-----------------------------------------------------------------
  224.      */
  225.     function edit_row($id_row = 0, $id_invoice = 0)
  226.     {
  227.         $id_invoice *= 1;
  228.         $id_row *= 1;
  229.  
  230.         if($id_row == 0)
  231.         {
  232.             $id_row = $this->model->create_empty_row($id_invoice);
  233.         }
  234.  
  235.         $data_view['row'] = $this->model->get_row($id_row);
  236.         $data_view['tpl'] = 'invoices/view_row_edit';
  237.         $data_view['title'] = 'view_row_edit';
  238.         $this->load->view('view_manager', $data_view);
  239.     }
  240.  
  241.  
  242.  
  243.     /**
  244.      * -----------------------------------------------------------------------------
  245.      * Salva la riga di una fattura facendo i vari calcoli
  246.      * -----------+-----------------------------------------------------------------
  247.      * 06-07-2010 | Creata funzione
  248.      * 26-05-2011 | Gestione vari casi prima del save_row()
  249.      * -----------+-----------------------------------------------------------------
  250.      */
  251.     function save_row($id_row = 0)
  252.     {
  253.         $id_row *= 1;
  254.  
  255.         $data['id_row'] = $id_row;
  256.         $data['id_invoice'] = $this->input->post('id_invoice');
  257.         $data['id_item'] = $this->input->post('id_item');
  258.         $previous_id_item = $this->input->post('previous_id_item');
  259.         $data['description'] = $this->input->post('description');
  260.         $data['subprice'] = $this->input->post('subprice');
  261.         $data['qty'] = $this->input->post('qty');
  262.         $data['iva_percent'] = $this->input->post('iva_percent');
  263.  
  264.         if($previous_id_item == 0 and $data['id_item'] == 0) // Qualcosa è andato storto
  265.         {
  266.             $this->session->set_flashdata('flashmsg', '<span class="notice">' . 'Manca l\'id annuncio' . '</span>');
  267.             redirect("/invoices/edit_row/{$id_row}");
  268.         }
  269.         elseif($previous_id_item == 0 and $this->shop_spares_model->get_status($data['id_item']) == 'D')
  270.         {
  271.             $this->model->set_status($data['id_item'], 'V');
  272.             $this->model->save_row($data);
  273.             $this->session->set_flashdata('flashmsg', '<span class="notice">' . lang('record_saved') . '</span>');
  274.             redirect("/invoices/edit_row/{$id_row}");
  275.         }
  276.         elseif($previous_id_item == 0 and $this->shop_spares_model->get_status($data['id_item']) != 'D')
  277.         {
  278.             $this->session->set_flashdata('flashmsg', '<span class="notice">' . 'articolo non disponibile' . '</span>');
  279.             redirect("/invoices/edit_row/{$id_row}");
  280.         }
  281.         else
  282.         {
  283.             $this->model->save_row($data);
  284.             $this->session->set_flashdata('flashmsg', '<span class="notice">' . lang('record_saved') . '</span>');
  285.             redirect("/invoices/edit_row/{$id_row}");
  286.         }
  287.     }
  288.  
  289.  
  290.  
  291.     /**
  292.      * -----------------------------------------------------------------------------
  293.      * Cancellazione di una riga di una fattura
  294.      * -----------+-----------------------------------------------------------------
  295.      * 06-07-2010 | Creata funzione
  296.      * 25-05-2011 | Inserita gestione del set_status
  297.      * 26-05-2011 | La cancellazione della riga è soggetta a delle condizioni
  298.      *            | e non più incondizionata.
  299.      * -----------+-----------------------------------------------------------------
  300.      */
  301.     function delete_row($id_row = 0, $id_invoice = 0)
  302.     {
  303.         $id_invoice *= 1;
  304.         $id_row *= 1;
  305.  
  306.         $row = $this->model->get_row($id_row);  // x recuperare id_item
  307.  
  308.         if($row['id_item'])
  309.         {
  310.             $shop_item_status = $this->shop_spares_model->get_status($row['id_item']); //V|Z|D
  311.             $invoice_type = $this->model->get_invoice_type($id_invoice); //F|N|R
  312.  
  313.             if($invoice_type == 'F' and $shop_item_status == 'V')
  314.             {
  315.                 $this->model->set_status($row['id_item'], 'D');
  316.                 $this->model->delete_row($id_row);
  317.             }
  318.             elseif($invoice_type == 'R' and $shop_item_status == 'D')
  319.             {
  320.                 $this->model->set_status($row['id_item'], 'V');
  321.                 $this->model->delete_row($id_row);
  322.             }
  323.             elseif($invoice_type == 'N' and $shop_item_status == 'Z')
  324.             {
  325.                 $this->model->set_status($row['id_item'], 'V');
  326.                 $this->model->delete_row($id_row);
  327.             }
  328.         }
  329.  
  330.         redirect("/invoices/edit/{$id_invoice}");
  331.     }
  332.  
  333.  
  334.  
  335.     /**
  336.      * excel() genera l'excel per il mese specificato
  337.      */
  338.     function excel($year = 0, $month = 0)
  339.     {
  340. //        $year *= 1;
  341. //        $month *= 1;
  342.  
  343. //        if($year < 2010 or $year > date('Y'))
  344. //        {
  345. //            $year = date('Y');
  346. //        }
  347. //
  348. //
  349. //        if($month < 0 or $month > 12)
  350. //        {
  351. //            $month = date('m');
  352. //        }
  353.  
  354.         $rows = $this->model->excel($year, $month);
  355.         $data_view['o'] = $rows;
  356.         $data_view['tpl'] = 'invoices/view_excel';
  357.         $data_view['title'] = 'EXCEL';
  358.         $this->load->view('view_manager_100', $data_view);
  359.     }
  360.  
  361.  
  362.  
  363.     /**
  364.      * -----------------------------------------------------------------------------
  365.      * Genera il pdf di una certa fattura
  366.      * -----------+-----------------------------------------------------------------
  367.      * 06-07-2010 | Creata funzione
  368.      * -----------+-----------------------------------------------------------------
  369.      */
  370.     function pdf($id_invoice = 0)
  371.     {
  372.         $id_invoice *= 1;
  373.  
  374.         $header = $this->model->get_header($id_invoice);
  375.         $rows = $this->model->get_rows($id_invoice);
  376.  
  377. // Se è una nota credito, recuperiamo i riferimenti della fattura originaria
  378.         if($header[0]['invoice_type'] == 'N')
  379.         {
  380.             $headerNC = $this->model->get_header($header[0]['credit_note_reference']);
  381.         }
  382.  
  383.  
  384.         $data = array();
  385.         $data['total_price'] = 0.0;
  386.         $data['total_subprice'] = 0.0;
  387.         $data['total_qty'] = 0.0;
  388.         $data['total_iva'] = 0.0;
  389.         $data['id_item'] = '';
  390.         $data['description'] = '';
  391.  
  392.         foreach($rows as $row)
  393.         {
  394.             $data['total_price'] += $row['price'];
  395.             $data['total_qty'] += $row['qty'];
  396.             $data['total_iva'] += $row['price'] - ($row['price'] / (1.0 + $row['iva_percent'] / 100.0));
  397.             $data['total_subprice'] += $row['subprice'] * $row['qty'];
  398.         }
  399.         $rows[] = array('id_item' => '', 'description' => 'COSTI DI SPEDIZIONE', 'qty' => '', 'subprice' => '', 'iva_percent' => '', 'price' => currency($header[0]['shipping_price']));
  400.  
  401. // Rettangolo nero in alto
  402.         $this->cezpdf->setLineStyle(1);
  403.         $this->cezpdf->rectangle(31, 700, 534, 112);
  404.  
  405. // Scritte nel rettangolo
  406.         $this->cezpdf->addText(40, 780, 22, "Rottamoto S.a.S. di Andrea D'Amelio & C.");
  407.         $this->cezpdf->addText(40, 750, 12, "Via Frati della Mensa, 1/3 - 20063 Cassano D'Adda MI");
  408.         $this->cezpdf->addText(40, 730, 12, "Cod.Fisc. e P.IVA 05997580963");
  409.  
  410.  
  411. // Indirizzo di fatturazione
  412.         $this->cezpdf->ezSetDy(-150);
  413.         $saved_y = $this->cezpdf->y;
  414.         $this->cezpdf->addText(40, $this->cezpdf->y, 10, '<i>Indirizzo di fatturazione</i>');
  415.         $this->cezpdf->ezSetDy(-30);
  416.         $this->cezpdf->addText(40, $this->cezpdf->y, 12, $header[0]['business_name']);
  417.         $this->cezpdf->ezSetDy(-15);
  418.         $this->cezpdf->addText(40, $this->cezpdf->y, 12, $header[0]['address']);
  419.         $this->cezpdf->ezSetDy(-15);
  420.         $this->cezpdf->addText(40, $this->cezpdf->y, 12, $header[0]['postal_code'] . ' ' . $header[0]['town'] . ' (' . $header[0]['prov'] . ')');
  421.         $this->cezpdf->ezSetDy(-15);
  422.         $this->cezpdf->addText(40, $this->cezpdf->y, 12, $header[0]['country']);
  423.         $this->cezpdf->ezSetDy(-15);
  424.         $this->cezpdf->ezSetDy(-15);
  425.         $this->cezpdf->addText(40, $this->cezpdf->y, 12, 'Cod.Fisc.: ' . $header[0]['cf']);
  426.         $this->cezpdf->ezSetDy(-15);
  427.         $this->cezpdf->addText(40, $this->cezpdf->y, 12, 'P.IVA: ' . $header[0]['vat']);
  428.         $this->cezpdf->ezSetDy(-15);
  429.         $this->cezpdf->ezSetDy(-15);
  430.  
  431.         if($header[0]['invoice_type'] == 'F')
  432.         {
  433.             $this->cezpdf->addText(40, $this->cezpdf->y, 12, 'Nr. Fattura: ' . $header[0]['invoice_number']);
  434.             $this->cezpdf->ezSetDy(-15);
  435.         }
  436.         elseif($header[0]['invoice_type'] == 'N')
  437.         {
  438.             $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'] . ')');
  439.             $this->cezpdf->ezSetDy(-15);
  440.         }
  441.         else
  442.         {
  443.             show_error('Tipo documento non valido: ' . $header[0]['invoice_type']);
  444.         }
  445.         $this->cezpdf->addText(40, $this->cezpdf->y, 12, 'Data documento: ' . $header[0]['date']);
  446.         $this->cezpdf->ezSetDy(-15);
  447.  
  448.  
  449. // Indirizzo di spedizione
  450.         $this->cezpdf->ezSetY($saved_y);
  451.         $this->cezpdf->addText(300, $this->cezpdf->y, 10, '<i>Indirizzo di consegna</i>');
  452.         $this->cezpdf->ezSetDy(-30);
  453.         $this->cezpdf->addText(300, $this->cezpdf->y, 12, $header[0]['delivery_business_name']);
  454.         $this->cezpdf->ezSetDy(-15);
  455.         $this->cezpdf->addText(300, $this->cezpdf->y, 12, $header[0]['delivery_address']);
  456.         $this->cezpdf->ezSetDy(-15);
  457.         $this->cezpdf->addText(300, $this->cezpdf->y, 12, $header[0]['delivery_postal_code'] . ' ' . $header[0]['delivery_town'] . ' (' . $header[0]['delivery_prov'] . ')');
  458.         $this->cezpdf->ezSetDy(-15);
  459.         $this->cezpdf->addText(300, $this->cezpdf->y, 12, $header[0]['delivery_country']);
  460.         $this->cezpdf->ezSetDy(-15);
  461.         if(!empty($header[0]['reference']))
  462.         {
  463.             $this->cezpdf->ezSetDy(-15);
  464.             $this->cezpdf->addText(300, $this->cezpdf->y, 12, 'c.a. ' . $header[0]['reference']);
  465.             $this->cezpdf->ezSetDy(-15);
  466.         }
  467.         $this->cezpdf->rectangle(290, $this->cezpdf->y, 275, $saved_y - $this->cezpdf->y + 15);
  468.  
  469.  
  470.         /*
  471.          * Qui la tabella con le righe del documento
  472.          */
  473.         $this->cezpdf->ezSetDy(-100);
  474.         $col_names = array(
  475.             'id_item' => 'Cod.',
  476.             'description' => 'Descrizione',
  477.             'qty' => 'Q.ta\'',
  478.             'subprice' => 'Prezzo',
  479.             'iva_percent' => 'IVA%',
  480.             'price' => 'Importo'
  481.         );
  482.         $this->cezpdf->ezTable($rows, $col_names, '', array('xPos' => 'center', 'width' => 530, 'shaded' => 0));
  483.         $this->cezpdf->ezSetDy(-15);
  484.         $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');
  485.         $this->cezpdf->ezSetDy(-15);
  486.         $this->cezpdf->addText(40, $this->cezpdf->y, 10, 'Trattasi esclusivamente di ricambi usati.');
  487.         $this->cezpdf->ezSetDy(-15);
  488. //     
  489.         $saved_y = $this->cezpdf->y;
  490. //
  491.         $totals_data = array(
  492. //array('key'=>'Totale oggetti', 'value'=> $data['total_qty']),
  493.             array('key' => 'Imponibile', 'value' => currency($data['total_subprice'])),
  494.             array('key' => 'IVA', 'value' => currency($data['total_iva'])),
  495.             array('key' => 'Importo', 'value' => currency($data['total_price'])),
  496.             array('key' => '+ Spese di spedizione', 'value' => currency($header[0]['shipping_price'] * $header[0]['packages'])),
  497.             array('key' => 'Totale documento', 'value' => currency(($header[0]['shipping_price'] * $header[0]['packages']) + $data['total_price']))
  498.         );
  499.         $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'))));
  500. //
  501.         $this->cezpdf->ezSetY($saved_y - 100);
  502. //
  503.         $payment_methods = $this->model->get_payment_methods();
  504.         $this->cezpdf->addText(40, $this->cezpdf->y, 12, 'Modalita\' di pagamento: ' . $payment_methods[$header[0]['payment_method']]);
  505.  
  506. //
  507.  
  508.         $this->cezpdf->ezStream();
  509.     }
  510.  
  511. #-------------------------------------------------------------------------------
  512. }
  513.  
  514. // end of class