public function actionCreateInvoice($id)
 {
     $model = LbContracts::model()->findByPk($id);
     //********* add new invoice *************/
     $invoiceModel = new LbInvoice();
     // get basic info to assign to this record
     $ownCompany = LbCustomer::model()->getOwnCompany();
     $ownCompanyAddress = null;
     $customerCompany = new LbCustomer();
     $customerCompany->lb_customer_name = 'Click to choose customer';
     // get own company address
     if ($ownCompany->lb_record_primary_key) {
         // auto assign owner company
         $invoiceModel->lb_invoice_company_id = $ownCompany->lb_record_primary_key;
         $own_company_addresses = LbCustomerAddress::model()->getActiveAddresses($ownCompany->lb_record_primary_key, $ownCompany::LB_QUERY_RETURN_TYPE_MODELS_ARRAY);
         if (count($own_company_addresses)) {
             $ownCompanyAddress = $own_company_addresses[0];
             // auto assign owner company's address
             $invoiceModel->lb_invoice_company_address_id = $ownCompanyAddress->lb_record_primary_key;
         }
     }
     // add infor customer in invoice
     $invoiceModel->lb_invoice_customer_id = $model->lb_customer_id;
     $invoiceModel->lb_invoice_customer_address_id = $model->lb_address_id;
     $invoiceModel->lb_invoice_attention_contact_id = $model->lb_contact_id;
     if ($invoiceModel->save()) {
         // Save Lien ket contract va invoice
         $contrctInvoiceModel = new LbContractInvoice();
         $contrctInvoiceModel->lb_contract_id = $model->lb_record_primary_key;
         $contrctInvoiceModel->lb_invoice_id = $invoiceModel->lb_record_primary_key;
         $contrctInvoiceModel->save();
         //Update invoice no vaf status I-OPEN
         $invoiceModel->confirm();
         // add invoice to database right away.
         // other fields will be updated on the invoice page later on
         $invoiceModel->saveUnconfirmed();
         //
         // == Prepare line items grid
         // add 1 line-item by default
         //
         $blankItem = new LbInvoiceItem();
         $blankItem->lb_invoice_id = $invoiceModel->lb_record_primary_key;
         $blankItem->lb_invoice_item_description = "Contract payment for contract " . $model->lb_contract_no . ", " . $model->lb_contract_type . ", for period " . $model->lb_contract_date_start . " to " . $model->lb_contract_date_end;
         $blankItem->lb_invoice_item_quantity = 1;
         $blankItem->lb_invoice_item_value = 0;
         $blankItem->lb_invoice_item_total = 0;
         $blankItem->lb_invoice_item_type = LbInvoiceItem::LB_INVOICE_ITEM_TYPE_LINE;
         $blankItem->save();
         // add tax
         $blankTax = new LbInvoiceItem();
         $default_tax = LbTax::model()->getDefaultTax();
         //$value_tax = ($default_tax->lb_tax_value/100) * $model->lb_contract_amount;
         $blankTax->lb_invoice_id = $invoiceModel->lb_record_primary_key;
         $blankTax->lb_invoice_item_description = $default_tax !== null ? $default_tax->lb_record_primary_key : 'Tax';
         $blankTax->lb_invoice_item_quantity = 1;
         $blankTax->lb_invoice_item_value = $default_tax !== null ? $default_tax->lb_tax_value : '0';
         $blankTax->lb_invoice_item_total = 0;
         $blankTax->lb_invoice_item_type = LbInvoiceItem::LB_INVOICE_ITEM_TYPE_TAX;
         $blankTax->save();
         $invoiceItemModel = new LbInvoiceItem('search');
         $invoiceItemModel->unsetAttributes();
         // clear any default values
         $invoiceItemModel->lb_invoice_id = $invoiceModel->lb_record_primary_key;
         // totals - create a blank total record, set everything to zero: subtotal, after disc, after tax, paid, outstanding....
         $invoiceTotal = new LbInvoiceTotal();
         $invoiceTotal->lb_invoice_id = $invoiceModel->lb_record_primary_key;
         $invoiceTotal->lb_invoice_revision_id = 0;
         // latest version
         $invoiceTotal->lb_invoice_subtotal = 0;
         $invoiceTotal->lb_invoice_total_after_discounts = 0;
         $invoiceTotal->lb_invoice_total_after_taxes = 0;
         $invoiceTotal->lb_invoice_total_outstanding = 0;
         $invoiceTotal->lb_invoice_total_paid = 0;
         $invoiceTotal->save();
         // == end items grid data prep
         // Uncomment the following line if AJAX validation is needed
         // $this->performAjaxValidation($model);
         $invoiceDiscountModel = new LbInvoiceItem();
         $invoiceDiscountModel->lb_invoice_item_type = LbInvoiceItem::LB_INVOICE_ITEM_TYPE_DISCOUNT;
         $invoiceTaxModel = new LbInvoiceItem();
         $invoiceTaxModel->lb_invoice_item_type = LbInvoiceItem::LB_INVOICE_ITEM_TYPE_TAX;
         //*** End add invoice **/
         //LBApplication::redire(array('lbInvoice/default/view','id'=>$invoiceModel->lb_record_primary_key));
         $this->redirect(array('view', 'id' => $model->lb_record_primary_key));
     }
 }