示例#1
0
 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');
     $template = array();
     foreach (glob(MODPATH . 'larvconomy/xsl/bills/*') as $file) {
         $file_paths = explode('/', $file);
         $template_file = explode('.', end($file_paths));
         $template[] = reset($template_file);
     }
     $this->xml_content_bill_template = $this->xml_content->appendChild($this->dom->createElement('templates'));
     xml::to_XML($template, $this->xml_content_bill_template, 'template');
     if (!isset($_SESSION['bills']['items'])) {
         $_SESSION['bills']['items']['1item'] = 1;
     }
     if (count($_POST)) {
         $post = new Validation($_POST);
         $post->filter('trim');
         $post_array = $post->as_array();
         if (isset($post_array['add_item'])) {
             $_SESSION['bills']['items'][count($_SESSION['bills']['items']) + 1 . 'item'] = count($_SESSION['bills']['items']) + 1;
             $this->set_formdata($post_array);
         } else {
             $items = array();
             $sum = 0;
             $vat_sum = 0;
             foreach ($_SESSION['bills']['items'] as $item_nr) {
                 $item = array('artnr' => $post->get('artnr_item_' . $item_nr), 'spec' => $post->get('spec_item_' . $item_nr), 'price' => (double) $post->get('price_item_' . $item_nr), 'qty' => (double) $post->get('qty_item_' . $item_nr), 'delivery_date' => date('Y-m-d', time()));
                 if ($item != array('artnr' => '', 'spec' => '', 'price' => 0, 'qty' => 0, 'delivery_date' => date('Y-m-d', time()))) {
                     $items[] = $item;
                     $sum += $item['qty'] * $item['price'] * 1.25;
                     $vat_sum += $item['qty'] * $item['price'] * 0.25;
                 }
             }
             if (count($items) && $post->validate()) {
                 $bill_id = Bill::new_bill($post->get('customer_id'), strtotime($post->get('due_date')), $post->get('contact'), $items, $post->get('comment'), $post->get('template'), $post->get('mail_body'));
                 $this->add_message('Created bill nr ' . $bill_id);
                 unset($_SESSION['bills']['items']);
                 // Create the transaction
                 $data = array('accounting_date' => date('Y-m-d', time()), 'transfer_date' => '0000-00-00', 'description' => 'Bill ' . $bill_id, 'vat' => $vat_sum, 'sum' => $sum, 'employee_id' => NULL, 'journal_id' => NULL);
                 $transaction = new Transaction(NULL, $data);
                 // End of Create the transaction
                 // Set new default due date
                 $this->set_formdata(array('due_date' => date('Y-m-d', time() + 20 * 24 * 60 * 60)));
                 // Make the PDF
                 if (Kohana::$config->load('larv.htpassword.password') && Kohana::$config->load('larv.htpassword.username')) {
                     shell_exec('wkhtmltopdf --ignore-load-errors --username ' . Kohana::$config->load('larv.htpassword.username') . ' --password ' . Kohana::$config->load('larv.htpassword.password') . ' "' . $_SERVER['SERVER_NAME'] . URL::site('bill?billnr=' . $bill_id . '&template=' . $post->get('template')) . '" "' . APPPATH . 'user_content/pdf/bill_' . $bill_id . '.pdf"');
                 } else {
                     shell_exec('wkhtmltopdf --ignore-load-errors "' . $_SERVER['SERVER_NAME'] . URL::site('bill?billnr=' . $bill_id . '&template=' . $post->get('template')) . '" "' . APPPATH . 'user_content/pdf/bill_' . $bill_id . '.pdf"');
                 }
                 if (isset($_FILES)) {
                     Bill::upload($_FILES, 'attachments/' . $bill_id);
                 }
             } else {
                 $this->add_error('Not enough data');
                 $post->set('due_date', date('Y-m-d', strtotime($post->get('due_date'))));
                 $post->set('mail_body', Kohana::$config->load('larv.email.bill_message'));
                 $this->set_formdata($post->as_array());
             }
         }
     } else {
         $this->set_formdata(array('due_date' => date('Y-m-d', time() + 20 * 24 * 60 * 60), 'mail_body' => Kohana::$config->load('larv.email.bill_message')));
     }
     xml::to_XML($_SESSION['bills'], $this->xml_content);
 }