function actionAjaxCreateInvoice($id)
 {
     $model = $this->loadModel($id);
     $quotationItem = LbQuotationItem::model()->getquotationItems($id, 'ModelArray');
     $quotationDiscount = LbQuotationDiscount::model()->getQuotationDiscounts($id);
     $quotationTax = LbQuotationTax::model()->getTaxQuotation($id);
     $quotationTotal = LbQuotationTotal::model()->getQuotationTotal($id);
     $invoiceModel = new LbInvoice();
     $invoiceTotal = new LbInvoiceTotal();
     $invoice_number_int = LbInvoice::model()->getInvoiceNextNum();
     $invoiceModel->lb_invoice_no = LbInvoice::model()->formatInvoiceNextNumFormatted($invoice_number_int);
     $invoiceModel->lb_invoice_status_code = LbInvoice::LB_INVOICE_STATUS_CODE_OPEN;
     // invoice date
     $invoiceModel->lb_invoice_date = date('Y-m-d');
     $invoiceModel->lb_invoice_due_date = date('Y-m-d');
     // invoice group
     $invoiceModel->lb_invoice_group = LbInvoice::LB_INVOICE_GROUP_INVOICE;
     // invoice base64_decode
     $invoiceModel->lb_invoice_encode = LbInvoice::model()->setBase64_decodeInvoice();
     // invoice note
     $invoiceModel->lb_invoice_note = $model->lb_quotation_note;
     $invoiceModel->lb_invoice_company_id = $model->lb_company_id;
     $invoiceModel->lb_invoice_company_address_id = $model->lb_company_address_id;
     $invoiceModel->lb_invoice_customer_id = $model->lb_quotation_customer_id;
     $invoiceModel->lb_invoice_customer_address_id = $model->lb_quotation_customer_address_id;
     $invoiceModel->lb_invoice_attention_contact_id = $model->lb_quotation_attention_id;
     $invoiceModel->lb_quotation_id = $model->lb_record_primary_key;
     $invoiceModel->lb_invoice_subject = $model->lb_quotation_subject;
     // invoice internal note
     $invoiceModel->lb_invoice_internal_note = $model->lb_quotation_internal_note;
     if ($invoiceModel->save()) {
         // copy line item
         foreach ($quotationItem as $q_item) {
             $invoiceItemModel = new LbInvoiceItem();
             $invoiceItemModel->lb_invoice_id = $invoiceModel->lb_record_primary_key;
             $invoiceItemModel->lb_invoice_item_description = $q_item->lb_quotation_item_description;
             $invoiceItemModel->lb_invoice_item_quantity = $q_item->lb_quotation_item_quantity;
             $invoiceItemModel->lb_invoice_item_value = $q_item->lb_quotation_item_price;
             $invoiceItemModel->lb_invoice_item_total = $q_item->lb_quotation_item_total;
             $invoiceItemModel->lb_invoice_item_type = LbInvoiceItem::LB_INVOICE_ITEM_TYPE_LINE;
             $invoiceItemModel->save();
         }
         // copy tax item
         foreach ($quotationTax->data as $q_tax) {
             $invoiceTaxModel = new LbInvoiceItem();
             $invoiceTaxModel->lb_invoice_id = $invoiceModel->lb_record_primary_key;
             $invoiceTaxModel->lb_invoice_item_description = $q_tax->lb_quotation_tax_id;
             $invoiceTaxModel->lb_invoice_item_quantity = 1;
             $invoiceTaxModel->lb_invoice_item_value = $q_tax->lb_quotation_tax_value;
             $invoiceTaxModel->lb_invoice_item_total = $q_tax->lb_quotation_tax_total;
             $invoiceTaxModel->lb_invoice_item_type = LbInvoiceItem::LB_INVOICE_ITEM_TYPE_TAX;
             $invoiceTaxModel->save();
         }
         // copy discount item
         foreach ($quotationDiscount->data as $q_discount) {
             $invoiceDiscountModel = new LbInvoiceItem();
             $invoiceDiscountModel->lb_invoice_id = $invoiceModel->lb_record_primary_key;
             $invoiceDiscountModel->lb_invoice_item_description = $q_discount->lb_quotation_discount_description;
             $invoiceDiscountModel->lb_invoice_item_quantity = 1;
             $invoiceDiscountModel->lb_invoice_item_value = $q_discount->lb_quotation_discount_total;
             $invoiceDiscountModel->lb_invoice_item_total = $q_discount->lb_quotation_discount_total;
             $invoiceDiscountModel->lb_invoice_item_type = LbInvoiceItem::LB_INVOICE_ITEM_TYPE_DISCOUNT;
             $invoiceDiscountModel->save();
         }
         // Copy Total
         $invoiceTotal->lb_invoice_id = $invoiceModel->lb_record_primary_key;
         $invoiceTotal->lb_invoice_subtotal = $quotationTotal->lb_quotation_subtotal;
         $invoiceTotal->lb_invoice_total_after_discounts = $quotationTotal->lb_quotation_total_after_discount;
         $invoiceTotal->lb_invoice_total_after_taxes = $quotationTotal->lb_quotation_total_after_total;
         $invoiceTotal->lb_invoice_total_outstanding = $quotationTotal->lb_quotation_total_after_total;
         $invoiceTotal->lb_invoice_revision_id = 0;
         $invoiceTotal->save();
         // redirect Invoice
         //            $url_invoice = $model->customer ? LbInvoice::model()->getViewURL($model->customer->lb_customer_name,null,$invoiceModel->lb_record_primary_key) : LbInvoice::model()->getViewURL("No customer",null,$invoiceModel->lb_record_primary_key);
         //            $this->redirect($url_invoice);
     }
 }
 public function actionAjaxCopyInvoice($id)
 {
     //Add thong tin quotation
     $inv_new = new LbInvoice();
     $invoice = LbInvoice::model()->findByPk($id);
     $inv_new->lb_invoice_no = LbInvoice::model()->formatInvoiceNextNumFormatted(0);
     $inv_new->lb_invoice_date = date('Y-m-d');
     $inv_new->lb_invoice_due_date = date('Y-m-d');
     $inv_new->lb_invoice_status_code = LbInvoice::LB_INVOICE_STATUS_CODE_DRAFT;
     $inv_new->lb_invoice_company_id = $invoice->lb_invoice_company_id;
     $inv_new->lb_invoice_company_address_id = $invoice->lb_invoice_company_address_id;
     $inv_new->lb_invoice_customer_id = $invoice->lb_invoice_customer_id;
     $inv_new->lb_invoice_customer_address_id = $invoice->lb_invoice_customer_address_id;
     $inv_new->lb_invoice_attention_contact_id = $invoice->lb_invoice_attention_contact_id;
     $inv_new->lb_invoice_internal_note = $invoice->lb_invoice_internal_note;
     $inv_new->lb_invoice_subject = $invoice->lb_invoice_subject;
     $inv_new->lb_invoice_note = $invoice->lb_invoice_note;
     $inv_new->lb_invoice_encode = LbInvoice::model()->setBase64_decodeInvoice();
     if ($inv_new->save()) {
         // Add thong tin tax
         $invoice_tax_arr = LbInvoiceItem::model()->getInvoiceTaxes($invoice->lb_record_primary_key);
         foreach ($invoice_tax_arr->data as $tax_item) {
             $inv_tax_new = new LbInvoiceItem();
             $inv_tax_new->lb_invoice_id = $inv_new->lb_record_primary_key;
             $inv_tax_new->lb_invoice_item_description = $tax_item->lb_invoice_item_description;
             $inv_tax_new->lb_invoice_item_quantity = $tax_item->lb_invoice_item_quantity;
             $inv_tax_new->lb_invoice_item_value = $tax_item->lb_invoice_item_value;
             $inv_tax_new->lb_invoice_item_total = $tax_item->lb_invoice_item_total;
             $inv_tax_new->lb_invoice_item_type = LbInvoiceItem::LB_INVOICE_ITEM_TYPE_TAX;
             $inv_tax_new->save();
         }
         //END
         //Add thong tin discount
         $invoice_discount_arr = LbInvoiceItem::model()->getInvoiceDiscounts($invoice->lb_record_primary_key);
         foreach ($invoice_discount_arr->data as $discount_item) {
             $inv_discount_new = new LbInvoiceItem();
             $inv_discount_new->lb_invoice_id = $inv_new->lb_record_primary_key;
             $inv_discount_new->lb_invoice_item_description = 'Discount';
             $inv_discount_new->lb_invoice_item_quantity = $discount_item->lb_invoice_item_quantity;
             $inv_discount_new->lb_invoice_item_value = $discount_item->lb_invoice_item_value;
             $inv_discount_new->lb_invoice_item_total = $discount_item->lb_invoice_item_total;
             $inv_discount_new->lb_invoice_item_type = LbInvoiceItem::LB_INVOICE_ITEM_TYPE_DISCOUNT;
             $inv_discount_new->save();
         }
         //END
         //Add Thong tin total
         $invoice_total = LbInvoiceTotal::model()->getInvoiceTotal($invoice->lb_record_primary_key);
         $inv_total_new = new LbInvoiceTotal();
         $inv_total_new->lb_invoice_id = $inv_new->lb_record_primary_key;
         $inv_total_new->lb_invoice_revision_id = $invoice_total->lb_invoice_revision_id;
         $inv_total_new->lb_invoice_subtotal = $invoice_total->lb_invoice_subtotal;
         $inv_total_new->lb_invoice_total_after_discounts = $invoice_total->lb_invoice_total_after_discounts;
         $inv_total_new->lb_invoice_total_after_taxes = $invoice_total->lb_invoice_total_after_taxes;
         $inv_total_new->lb_invoice_total_outstanding = $invoice_total->lb_invoice_total_outstanding;
         $inv_total_new->lb_invoice_total_paid = $invoice_total->lb_invoice_total_paid;
         $inv_total_new->save();
         //Add thong tin item
         $invoice_item_arr = LbInvoiceItem::model()->getInvoiceItems($invoice->lb_record_primary_key);
         foreach ($invoice_item_arr->data as $item) {
             $inv_item_new = new LbInvoiceItem();
             $inv_item_new->lb_invoice_id = $inv_new->lb_record_primary_key;
             $inv_item_new->lb_invoice_item_description = $item->lb_invoice_item_description;
             $inv_item_new->lb_invoice_item_quantity = $item->lb_invoice_item_quantity;
             $inv_item_new->lb_invoice_item_value = $item->lb_invoice_item_value;
             $inv_item_new->lb_invoice_item_total = $item->lb_invoice_item_total;
             $inv_item_new->lb_invoice_item_type = LbInvoiceItem::LB_INVOICE_ITEM_TYPE_LINE;
             $inv_item_new->save();
         }
         //END
         $customer_name = $inv_new->customer ? $inv_new->customer->lb_customer_name : "No customer";
         $url = LbInvoice::model()->getViewURLByIdNormalized($inv_new->lb_record_primary_key, $customer_name);
         echo '{"status":"success","url":"' . $url . '"}';
     }
 }
 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));
     }
 }