Example #1
0
 /**
  * delete invoice
  * line items, discounts, taxes, totals, trails, ...
  * @return bool|void
  */
 public function delete()
 {
     // delete invoice
     $id = $this->lb_record_primary_key;
     $result = parent::delete();
     if ($result) {
         // delete line items
         $line_items = LbInvoiceItem::model()->getInvoiceItems($id, self::LB_QUERY_RETURN_TYPE_MODELS_ARRAY);
         foreach ($line_items as $l_item) {
             $l_item->delete();
         }
         // delete discounts
         $discounts = LbInvoiceItem::model()->getInvoiceDiscounts($id, self::LB_QUERY_RETURN_TYPE_MODELS_ARRAY);
         foreach ($discounts as $disc) {
             $disc->delete();
         }
         // delete taxes
         $taxes = LbInvoiceItem::model()->getInvoiceTaxes($id, self::LB_QUERY_RETURN_TYPE_MODELS_ARRAY);
         foreach ($taxes as $tx) {
             $tx->delete();
         }
         // delete totals
         $total = LbInvoiceTotal::model()->getInvoiceTotal($id);
         if ($total) {
             $total->delete();
         }
         // delete invoice in contract
         $invoiceByContract = LbContractInvoice::model()->find('lb_invoice_id=' . intval($id));
         if (count($invoiceByContract) > 0) {
             $invoiceByContract->delete();
         }
     }
 }
Example #2
0
 /**
  * Calculate invoice total after taxes (subtotal - discount - taxes)
  * save, and return value
  *
  * @return int total after taxes
  */
 public function calculateInvoiceTotalAfterTaxes()
 {
     // get all taxes
     $invoice_taxes = LbInvoiceItem::model()->getInvoiceTaxes($this->lb_invoice_id, self::LB_QUERY_RETURN_TYPE_MODELS_ARRAY);
     // calculate tax total, and invoice total after tax
     $tax_total = 0;
     foreach ($invoice_taxes as $tax) {
         $tax_total += $tax->lb_invoice_item_total;
     }
     $this->lb_invoice_total_after_taxes = $this->lb_invoice_total_after_discounts + $tax_total;
     $this->save();
     return $this->lb_invoice_total_after_taxes;
 }
                    </tfoot>
  
                    <?php 
} else {
    $customer_arr = LbCustomer::model()->getCompanies($sort = 'lb_customer_name ASC', LbCustomer::LB_QUERY_RETURN_TYPE_MODELS_ARRAY);
    $invoiceValue = 0;
    $gst = 0;
    foreach ($customer_arr as $customer) {
        ?>
                            <tbody>
                            <?php 
        $a = LbInvoice::model()->getInvoiceMonth($customer['lb_record_primary_key'], $date_from, $date_to);
        foreach ($a as $data) {
            $customer_id = false;
            $invoice_information = LbInvoiceTotal::model()->getInvoiceById($data->lb_record_primary_key);
            $invoiceTax = LbInvoiceItem::model()->getInvoiceTaxById($data->lb_record_primary_key, "TAX");
            $invoiceValue = $invoiceValue + $invoice_information->lb_invoice_total_after_discounts;
            echo '<tr>';
            echo '<td>' . $data->lb_invoice_no . '</td>';
            echo '<td>' . $data->lb_invoice_date . '</td>';
            echo '<td>';
            if ($data->lb_invoice_customer_id) {
                $customer_id = $data->lb_invoice_customer_id;
                echo LbCustomer::model()->customerInformation($customer_id)->attributes['lb_customer_name'];
            }
            echo '</td>';
            echo '<td>$' . $invoice_information->lb_invoice_total_after_discounts . '</td>';
            echo '<td>';
            $totalTax = 0;
            if ($invoiceTax) {
                $totaltax = $invoice_information->lb_invoice_total_after_taxes - $invoice_information->lb_invoice_total_after_discounts;
Example #4
0
 public function getInvoiceTaxById($invoice_id = false, $type = false)
 {
     $criteria = new CDbCriteria();
     if ($invoice_id) {
         $criteria->compare('lb_invoice_id', $invoice_id);
     }
     if ($type) {
         $criteria->compare('lb_invoice_item_type', $type);
     }
     return LbInvoiceItem::model()->find($criteria);
 }
Example #5
0
                    </tr>
                    ' . $subject . '
                    <tr><td>&nbsp;</td></tr>
                    <tr>
                        <tr>
                            <td colspan="2">
                                <table border="1" style="width:100%;border-collapse:collapse;" cellpadding="0" cellspacing="0">
                                    <tr >
                                        <th width="32" height="25" align="center">#</th>
                                        <th width="390">Item</th>
                                        <th width="90" align="center">Quantity</th>
                                        <th width="90" align="center">Unit Price</th>
                                        <th width="90" align="center">Total</th>
                                    </tr>';
//$item_invoice_arr = LbInvoiceItem :: model()->findAll('lb_invoice_id='.$model->lb_record_primary_key,array());
$item_invoice_arr = LbInvoiceItem::model()->getInvoiceItems($model->lb_record_primary_key, 'ModelArray');
//print_r($item_invoice_arr);
$i = 0;
foreach ($item_invoice_arr as $item_invoice_row) {
    $i++;
    $tbl .= '<tr>
                                        <td width="32" height="35" align="center">' . $i . '</td>
                                        <td width="390">' . $item_invoice_row['lb_invoice_item_description'] . '</td>
                                        <td width="90" align="right">' . $strnum->adddotstring($item_invoice_row['lb_invoice_item_quantity'], $thousand, $decimal) . '</td>
                                        <td width="90" align="right">' . $strnum->adddotstring($item_invoice_row['lb_invoice_item_value'], $thousand, $decimal) . '</td>
                                        <td width="90" align="right">' . $strnum->adddotstring($item_invoice_row['lb_invoice_item_total'], $thousand, $decimal) . '</td>
                                    </tr>';
}
$tbl .= '</table>
                            </td>
                        </tr>
Example #6
0
 public function IsTaxExistInvoiceORQuotation($tax_id)
 {
     $tax_invoice = LbInvoiceItem::model()->find('lb_invoice_item_description=' . $tax_id);
     $tax_quotation = LbQuotationTax::model()->find('lb_quotation_tax_id=' . $tax_id);
     if (count($tax_quotation) > 0 || count($tax_invoice) > 0) {
         return true;
     }
     return false;
 }
 public function actionCreatePaymentPo($id)
 {
     $invoiceItem = new LbInvoiceItem();
     $result = $invoiceItem->addBlankItem($id);
     if ($result) {
         $response = array();
         $response['success'] = YES;
         $response['invoice_item_id'] = $invoiceItem->lb_record_primary_key;
         LBApplication::renderPlain($this, array('content' => CJSON::encode($response)));
     }
 }
 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 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));
     }
 }