function actionAjaxSavePayment() { $model = new LbPayment(); if (isset($_POST['LbPayment'])) { $payment_arr = $_POST['LbPayment']; $payment_arr = $_POST['LbPayment']; $model->lb_payment_no = LbPayment::model()->FormatPaymentNo(LbPayment::model()->getPaymentNextNum()); $model->lb_payment_customer_id = $_POST['customer_id']; $model->lb_payment_method = $payment_arr['lb_payment_method']; $model->lb_payment_date = $payment_arr['lb_payment_date']; //$model->lb_payment_notes=$payment_arr['lb_payment_notes']; $model->lb_payment_total = $_POST['total_payment']; //$model->lb_invoice_id = $_POST[] if ($model->save()) { //LbPayment::model()->setPaymentNextNum(); // save payment item $count_payment = count($_POST['payment_check']); $payment_invoice = $_POST['payment_check']; $payment_note = $_POST['payment_note']; $payment_item_amount = $_POST['payment']; for ($i = 0; $i < $count_payment; $i++) { if ($payment_item_amount[$i] > 0) { $paymentItemModel = new LbPaymentItem(); $paymentItemModel->lb_payment_id = $model->lb_record_primary_key; $paymentItemModel->lb_invoice_id = $payment_invoice[$i]; $paymentItemModel->lb_payment_item_note = $payment_note[$i]; $paymentItemModel->lb_payment_item_amount = $payment_item_amount[$i]; if ($paymentItemModel->save()) { $invoice_total = LbInvoiceTotal::model()->getInvoiceTotal($paymentItemModel->lb_invoice_id); if ($invoice_total) { $invoice_total->calculateInvoicetotalPaid($paymentItemModel->lb_invoice_id); $total_outstanding = $invoice_total->calculateInvoiceTotalOutstanding(); if (isset($total_outstanding)) { //Update Status LbInvoice::model()->InvoiceUpdateStatus($payment_invoice[$i]); } } echo '{"status":"success"}'; } else { echo '{"status":"fail"}'; } } } } } }
/** * * * * @return int total invoice paid */ public function calculateInvoicetotalPaid($invoice_id) { $paymentItem = LbPaymentItem::model()->findAll('lb_invoice_id = ' . intval($invoice_id)); $total_paid = 0; foreach ($paymentItem as $data) { $total_paid += $data->lb_payment_item_amount; } $this->lb_invoice_total_paid = $total_paid; $this->save(); return $this->lb_invoice_total_paid; }
echo '<div class="invoice-total-container" style="margin-top: -20px ;margin-bottom:30px;border-left:solid 1px #dddddd;border-right:solid 1px #dddddd; border-bottom:solid 1px #dddddd;border-right:solid 1px #dddddd;" >'; // total after tax echo '<div class="invoice-total-label" style="margin-left:236px;">' . Yii::t('lang', 'Total') . ' (' . $currency_name . '):</div>'; echo "<div id='invoice-total-{$model->lb_record_primary_key}' class='invoice-total-value'>"; echo number_format($invoiceTotal->lb_invoice_total_after_taxes, 2, $lb_decimal_symbol, $lb_thousand_separator); echo '</div>'; // paid echo '<div class="invoice-total-label" style="margin-left:236px;">' . Yii::t('lang', 'Paid') . ' (' . $currency_name . '):</div>'; echo "<div id='invoice-paid-{$model->lb_record_primary_key}' class='invoice-total-value'>" . number_format($invoiceTotal->calculateInvoicetotalPaid($model->lb_record_primary_key), 2, $lb_decimal_symbol, $lb_thousand_separator) . "</div>"; // outstanding echo '<div style="margin-left:236px;" class="invoice-total-label">' . Yii::t('lang', 'Outstanding') . ' (' . $currency_name . '):</div>'; echo "<div id='invoice-outstanding-{$model->lb_record_primary_key}' class='invoice-total-value'>" . number_format($invoiceTotal->calculateInvoiceTotalOutstanding(), 2, $lb_decimal_symbol, $lb_thousand_separator) . "</div>"; echo '</div>'; echo '</div>'; //Payment $PaymentInvoice = LbPaymentItem::model()->getAllPaymentByInvoice($model->lb_record_primary_key); echo '<div id="container-invoice-line-items-section" style="margin-top: 30px;clear:both;">'; $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array('id' => 'lb-invoice-items-form', 'enableAjaxValidation' => false, 'type' => 'horizontal')); /** LineItem Grid's $data is LbInvoiceItem Each line item's fields (description, quantity, unit price and total) are marked by the line item's primary key. */ //$modelPayment = LBPayment::model()->getPaymentById($PaymentInvoice); //$this->widget('ext.rezvan.RDatePicker',array( // 'name'=>'message[messageDate]', // 'value'=>$modelPayment->lb_payment_date, // 'options' => array( // 'format' => 'yyyy/mm/dd', // 'viewformat' => 'yyyy/mm/dd', // 'placement' => 'right',
<?php $PaymentInvoice = LbPaymentItem::model()->getAllPaymentByInvoice($invoice_id); $method = LBPayment::model()->method; if ($invoice_status != LbInvoice::LB_INVOICE_STATUS_CODE_DRAFT) { $grid_payment_id = 'invoice-line-payment-grid-' . $invoice_id; $this->widget('bootstrap.widgets.TbGridView', array('id' => $grid_payment_id, 'type' => 'bordered', 'dataProvider' => $PaymentInvoice, 'columns' => array(array('class' => 'bootstrap.widgets.TbButtonColumn', 'template' => "{delete}", 'deleteButtonUrl' => '"' . LbInvoice::model()->getActionURLNormalized("ajaxDeletePayment") . '" . "?id={$data->lb_record_primary_key}&invoice_id={$data->lb_invoice_id}"', 'afterDelete' => 'function(link,success,data){ if(success) { var responseJSON = $.parseJSON(data); var status = responseJSON.status; if(status == "I_PAID") { $("#lb_invocie_status").text("Paid"); } else $("#lb_invocie_status").text("Open"); $("#invoice-outstanding-' . $invoice_id . '").text(responseJSON.outstanding); $("#invoice-paid-' . $invoice_id . '").text(responseJSON.paid); } } ', 'htmlOptions' => array('style' => 'width: 10px'), 'headerHtmlOptions' => array('class' => 'lb-grid-header')), array('header' => Yii::t('lang', 'Payment No'), 'type' => 'raw', 'value' => 'LbPayment::model()->getPaymentById($data->lb_payment_id)->lb_payment_no ', 'htmlOptions' => array('width' => '100', 'style' => 'text-align: right;'), 'headerHtmlOptions' => array('class' => 'lb-grid-header')), array('header' => Yii::t('lang', 'Payment Date'), 'type' => 'raw', 'value' => ' CHtml::activeTextField(LbPayment::model()->getPaymentById($data->lb_payment_id),"lb_payment_date", array("style"=>"width: 100px;text-align: right; padding-right: 0px; border-top: none; border-left: none; border-right: none; box-shadow: none;", "class"=>"lbinvoice-line-payment_date", "name"=>"lb_invoice_item_payment_{$data->lb_record_primary_key}",
function actionajaxDeletePayment() { if (isset($_GET['id'])) { $payment_item_id = $_GET['id']; } //delete payment item $payment = LbPaymentItem::model()->deleteByPk($payment_item_id); $invoice_id = $_GET['invoice_id']; $invoiceTotal = LbInvoiceTotal::model()->getInvoiceTotal($invoice_id); $taxRecord['paid'] = $invoiceTotal->calculateInvoicetotalPaid($invoice_id); $taxRecord['outstanding'] = $invoiceTotal->calculateInvoiceTotalOutstanding(); if ($invoiceTotal->calculateInvoiceTotalOutstanding() <= 0) { $taxRecord['status'] = 'I_PAID'; } else { $taxRecord['status'] = 'Open'; } //update invoice total // if($payment->delete()) // echo 'success'; LBApplication::renderPlain($this, array('content' => CJSON::encode($taxRecord))); }
if ($YStart == $YEnd && $MStart == $MEnd) { $PaymentAll = LbPayment::model()->getAllPayment($FromDate, $Todate, $customer_id, false, false, $i); } else { $PaymentAll = LbPayment::model()->getAllPayment($FromDate, $numberDate, $customer_id, false, false, $i); } if (count($PaymentAll) > 0) { $paymentAmount = 0; foreach ($PaymentAll as $data) { if (LbCustomer::model()->isCustomer($data->attributes['lb_payment_customer_id']) != null) { $customer_information = LbCustomer::model()->find('lb_record_primary_key=' . intval($data->attributes['lb_payment_customer_id'])); $customer_name = $customer_information->lb_customer_name; echo '<tr>'; echo '<td>' . $data->attributes['lb_payment_date'] . '</td>'; echo '<td>' . $customer_name . '</td>'; echo '<td>' . $data->attributes['lb_payment_notes'] . '</td>'; $payment_item = LbPaymentItem::model()->getAllPaymentInvoice($data->lb_record_primary_key); echo '<td>'; foreach ($payment_item as $payment) { // echo $payment->lb_invoice_id; $invoiceNo = LbInvoice::model()->find('lb_record_primary_key=' . intval($payment->lb_invoice_id)); if (isset($invoiceNo->lb_record_primary_key)) { echo $invoiceNo->lb_invoice_no . ':$' . $payment->lb_payment_item_amount . '; '; $paymentAmount = $paymentAmount + $payment->lb_payment_item_amount; $AmountMonth = $AmountMonth + $payment->lb_payment_item_amount; } } echo '</td>'; $total = $total + $data->attributes['lb_payment_total']; echo '<td>' . $data->attributes['lb_payment_total'] . '</td>'; echo '</tr>'; }
echo Yii::t('lang', 'Method'); ?> </th> <th width="150" class="lb-grid-header"><?php echo Yii::t('lang', 'Date'); ?> </th> <th class="lb-grid-header"><?php echo Yii::t('lang', 'Notes'); ?> </th> </tr> </thead> <tbody> <?php $paymentItem = LbPaymentItem::model()->findAll('lb_invoice_id=' . intval($data_invocie->lb_record_primary_key)); foreach ($paymentItem as $data_paymentItem) { $payment = LbPayment::model()->findByPk(intval($data_paymentItem->lb_payment_id)); ?> <tr> <td style="text-align: right"><?php echo number_format($data_paymentItem->lb_payment_item_amount, 2); ?> </td> <td style="text-align: center"><?php echo $method[$payment->lb_payment_method]; ?> </td> <td style="text-align: center"><?php echo $payment->lb_payment_date; ?>
</td> </tr> </table> </td> </tr></table></td></tr>'; $a .= '<tr><td> <table border="1" style="width:90%;margin-top:10px;" cellpadding="0" cellspacing="0"> <tr style="background-color:#ddd"> <td style="width:20%;height:30px;">Receipt Nos</td> <td height="40" style="width:20%">Amount Paid</td> <td style="text-align:center;width:20%">Method</td> <td style="width:20%">Data</td> <td style="width:20%">Notes</td> </tr>'; $data_paymentItem = LbPaymentItem::model()->findAll('lb_invoice_id=:invoice_id', array(':invoice_id' => $model->lb_record_primary_key)); foreach ($data_paymentItem as $payment_invoice) { $payment = LbPayment::model()->findByPk($payment_invoice->lb_payment_id); $a .= '<tr> <td class="lb-grid" style="text-align: center">' . $payment->lb_payment_no . '</td> <td class="lb-grid" style="text-align: right;">' . $strnum->adddotstring($payment_invoice->lb_payment_item_amount, $thousand, $decimal) . '</td>'; if (isset($payment)) { $a .= ' <td class="lb-grid" style="text-align: center">' . $method[$payment->lb_payment_method] . '</td> <td style="text-align: center">' . date('d-M-Y', strtotime($payment->lb_payment_date)) . '</td> <td width="200px">' . $payment_invoice->lb_payment_item_note . '</td>'; } $a .= '</tr>'; } $a .= '<tr> <td height="20" style="text-align: left;color: #6E8900;float:right;"><span style="float: left;text-align: left;">Total Paid: </span>$' . $strnum->adddotstring($invoice_total->lb_invoice_total_paid, $thousand, $decimal) . '</td>
public function getAllPaymentInvoice($lb_payment_id = false) { $criteria = new CDbCriteria(); $criteria->condition = "lb_payment_id = {$lb_payment_id}"; return LbPaymentItem::model()->findAll($criteria); }
public function actionRenew($id) { $contractModel = LbContracts::model()->findByPk($id); $documentModel = new LbContractDocument(); $paymentModel = new LbPayment(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['LbContracts'])) { $LbContracts = $_POST['LbContracts']; $model = new LbContracts(); $model->attributes = $_POST['LbContracts']; $model->lb_customer_id = $contractModel->lb_customer_id; $model->lb_address_id = $contractModel->lb_address_id; $model->lb_contact_id = $contractModel->lb_contact_id; $model->lb_contract_type = $contractModel->lb_contract_type; $model->lb_contract_status = LbContracts::LB_CONTRACT_STATUS_ACTIVE; $model->lb_contract_parent = $contractModel->lb_record_primary_key; if ($model->save()) { //********* Upload document *****************// $document = LbContractDocument::model()->getContractDocument($id); foreach ($document->data as $dataDocument) { $documentModel = new LbContractDocument(); $documentModel->lb_contract_id = $model->lb_record_primary_key; $documentModel->lb_document_url = $dataDocument->lb_document_url; $documentModel->lb_document_name = $dataDocument->lb_document_name; $documentModel->lb_document_url_icon = $dataDocument->lb_document_url_icon; $documentModel->save(); // DONE } //**** End upload doxument ****// /////////////////////////////////////////////////////////////////////////////// //********* add new invoice *************/ if (isset($LbContracts['check_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 = $contractModel->lb_customer_id; $invoiceModel->lb_invoice_customer_address_id = $contractModel->lb_address_id; $invoiceModel->lb_invoice_attention_contact_id = $contractModel->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 = $model->lb_contract_amount; $blankItem->lb_invoice_item_total = $model->lb_contract_amount; $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 = $model->lb_contract_amount; $invoiceTotal->lb_invoice_total_after_discounts = $model->lb_contract_amount; $invoiceTotal->lb_invoice_total_after_taxes = $model->lb_contract_amount; $invoiceTotal->lb_invoice_total_outstanding = $model->lb_contract_amount; $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 **/ /////////////////////////////////////////////////////////////////////////////////////////////// //************** Add payment *************// if (isset($LbContracts['payment_amount']) && $LbContracts['payment_amount'] != 0) { $paymentModel = new LbPayment(); $paymentModel->attributes = $_POST['LbPayment']; $paymentModel->lb_payment_customer_id = $contractModel->lb_customer_id; $paymentModel->lb_payment_no = LbPayment::model()->FormatPaymentNo(); $paymentModel->lb_payment_total = $LbContracts['payment_amount']; if ($paymentModel->save()) { LbPayment::model()->setPaymentNextNum(LbPayment::model()->getPaymentNextNum()); $paymentItemModel = new LbPaymentItem(); $paymentItemModel->lb_payment_id = $paymentModel->lb_record_primary_key; $paymentItemModel->lb_invoice_id = $invoiceModel->lb_record_primary_key; $paymentItemModel->lb_payment_item_note = ""; $paymentItemModel->lb_payment_item_amount = $LbContracts['payment_amount']; if ($paymentItemModel->save()) { $invoice_total = LbInvoiceTotal::model()->getInvoiceTotal($paymentItemModel->lb_invoice_id); if ($invoice_total) { $invoice_total->calculateInvoicetotalPaid($paymentItemModel->lb_invoice_id); $total_outstanding = $invoice_total->calculateInvoiceTotalOutstanding(); if ($total_outstanding) { //Update Status LbInvoice::model()->InvoiceUpdateStatus($invoiceModel->lb_record_primary_key); } } echo "{succes}"; } } } } } $contractModel->lb_contract_status = LbContracts::LB_CONTRACT_STATUS_HAS_RENEWED; $contractModel->save(); $this->redirect(array('view', 'id' => $model->lb_record_primary_key)); } } $customer_id = ""; if (isset($_POST['customer_id'])) { $customer_id = $_POST['customer_id']; } $this->render('_form_renew', array('model' => $contractModel, 'paymentModel' => $paymentModel)); }