1034 lines
30 KiB
PHP
1034 lines
30 KiB
PHP
<?php
|
|
if (!defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
|
/*
|
|
* InvoicePlane
|
|
*
|
|
* @author InvoicePlane Developers & Contributors
|
|
* @copyright Copyright (c) 2012 - 2017 InvoicePlane.com
|
|
* @license https://invoiceplane.com/license.txt
|
|
* @link https://invoiceplane.com
|
|
*/
|
|
|
|
/**
|
|
* Class Ajax
|
|
*/
|
|
class Ajax extends Admin_Controller
|
|
{
|
|
public $ajax_controller = true;
|
|
|
|
public function save()
|
|
{
|
|
|
|
|
|
$this->load->model('invoices/mdl_items');
|
|
$this->load->model('invoices/mdl_invoices');
|
|
$this->load->model('units/mdl_units');
|
|
$this->load->model('invoices/mdl_invoice_sumex');
|
|
|
|
$this->load->library('email');
|
|
|
|
$invoice_id = $this->input->post('invoice_id');
|
|
|
|
/*tamilbala */
|
|
//$deletestatus = $this->input->post('deletestatus');
|
|
/*tamilbala*/
|
|
|
|
//print_r($deletestatus);
|
|
//print_r('delete');
|
|
// $quoteid = $this->input->post('quoteid ');
|
|
|
|
// echo $quoteid;
|
|
// die();
|
|
|
|
|
|
$this->mdl_invoices->set_id($invoice_id);
|
|
|
|
if ($this->mdl_invoices->run_validation('validation_rules_save_invoice')) {
|
|
$items = json_decode($this->input->post('items'));
|
|
|
|
$quoteid = $this->input->post('quoteid');
|
|
$qu = $this->input->post('qu');
|
|
|
|
|
|
if(($quoteid == 0) && ($qu !=0))
|
|
{
|
|
$quoteid1=$qu;
|
|
}
|
|
|
|
else
|
|
{
|
|
$quoteid1=$quoteid;
|
|
}
|
|
|
|
///echo $quoteid1;
|
|
// print_r($items);
|
|
|
|
//die();
|
|
//print_r($items);
|
|
//die();
|
|
foreach ($items as $item) {
|
|
|
|
// Check if an item has either a quantity + price or name or description
|
|
if (!empty($item->item_name)) {
|
|
|
|
|
|
$item->item_quantity = ($item->item_quantity ? standardize_amount($item->item_quantity) : floatval(0));
|
|
|
|
$item->item_price = ($item->item_price ? standardize_amount($item->item_price) : floatval(0));
|
|
|
|
$item->item_discount_amount = ($item->item_discount_amount) ? standardize_amount($item->item_discount_amount) : null;
|
|
|
|
$item->item_product_id = ($item->item_product_id ? $item->item_product_id : null);
|
|
|
|
$itemproductid= $item->item_product_id;
|
|
$itemprice = $item->item_price;
|
|
$itemqty = $item->item_quantity;
|
|
|
|
|
|
// echo $item->item_price;
|
|
// die();
|
|
|
|
if($quoteid1 != 0)
|
|
{
|
|
|
|
|
|
$reqqty= $this->mdl_invoices->GetOrder_qty($quoteid1,$itemproductid);
|
|
|
|
$qidcheck= $this->mdl_invoices->Check_Qid($quoteid1);
|
|
|
|
$item_chek =$this->mdl_invoices->check_item($quoteid1,$itemproductid);
|
|
|
|
//echo count($item_chek);
|
|
//echo "</br>";
|
|
|
|
//die();
|
|
$innv='';
|
|
$Isqid='';
|
|
|
|
|
|
foreach($qidcheck as $q)
|
|
{
|
|
$Isqid = $q->quotecount;
|
|
$Isinv = $q->invoice_id;
|
|
|
|
if($Isinv == $invoice_id)
|
|
{
|
|
$innv = 1;
|
|
}
|
|
|
|
}
|
|
|
|
if($innv == 1)
|
|
{
|
|
$invorderdqty= $this->mdl_invoices->GetInvOrdered($invoice_id);
|
|
|
|
foreach($invorderdqty as $in)
|
|
{
|
|
$previousorder= $in->item_quantity;
|
|
}
|
|
}
|
|
|
|
else if($Isqid !='')
|
|
{
|
|
|
|
$invorderdqty= $this->mdl_invoices->GetquoteOrdered($quoteid1);
|
|
|
|
foreach($invorderdqty as $in)
|
|
{
|
|
$previousorder= $in->item_suppliedquantity;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
// echo $Isqid;
|
|
// die();
|
|
$orderqty=0;
|
|
$supqty=0;
|
|
$balqty=0;
|
|
|
|
if(!empty($reqqty))
|
|
{
|
|
|
|
foreach($reqqty as $req)
|
|
{
|
|
$orderqty = $req->orderedqty;
|
|
$supqty = $req->supqty;
|
|
$balqty = $req->balqty;
|
|
}
|
|
}
|
|
|
|
// echo "or".$orderqty."sup".$supqty."bal".$balqty."pre".$previousorder;
|
|
// die();
|
|
|
|
$oqty =$orderqty;
|
|
|
|
if($innv == 1)
|
|
{
|
|
$suppliedqty = $item->item_quantity+$supqty-$previousorder;
|
|
$balanceqty = $oqty - $suppliedqty;
|
|
|
|
}
|
|
|
|
else if($Isqid !='')
|
|
{
|
|
$suppliedqty=$item->item_quantity+$previousorder;
|
|
$balanceqty = $oqty - $suppliedqty;
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
{
|
|
|
|
$suppliedqty = ($item->item_quantity);
|
|
$balanceqty = ($oqty - ($item->item_quantity));
|
|
}
|
|
|
|
|
|
if($balanceqty == 0)
|
|
{
|
|
$item_status="0";
|
|
}
|
|
|
|
else
|
|
{
|
|
$item_status ="1";
|
|
}
|
|
|
|
$updatearray = array('item_balancequantity'=>$balanceqty,'item_suppliedquantity'=>$suppliedqty,'item_status'=>$item_status);
|
|
|
|
//print_r($updatearray);
|
|
// die();
|
|
if(count($item_chek) !=0)
|
|
{
|
|
$quoteupdate= $this->mdl_invoices->Update_Qty($quoteid1,$updatearray,$itemproductid);
|
|
}
|
|
}
|
|
|
|
else
|
|
{
|
|
$oqty =$item->item_quantity;
|
|
$suppliedqty =$oqty;
|
|
$balanceqty = 0;
|
|
|
|
}
|
|
|
|
//
|
|
$item->item_product_unit_id = ($item->item_product_unit_id ? $item->item_product_unit_id : null);
|
|
|
|
$item->item_product_unit = $this->mdl_units->get_name($item->item_product_unit_id, $item->item_quantity);
|
|
|
|
$item_id = ($item->item_id) ?: null;
|
|
|
|
|
|
unset($item->item_id);
|
|
|
|
if (!$item->item_task_id)
|
|
{
|
|
unset($item->item_task_id);
|
|
}
|
|
else
|
|
{
|
|
$this->load->model('tasks/mdl_tasks');
|
|
$this->mdl_tasks->update_status(4, $item->item_task_id);
|
|
}
|
|
// print_r($item);
|
|
// die();
|
|
|
|
// $prodate =
|
|
|
|
$dt = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
|
|
$prodate = $dt->format('Y-m-d');
|
|
|
|
|
|
$productcheck = $this->mdl_items->Check_invoice($invoice_id,$itemproductid);
|
|
|
|
//echo count($productcheck);
|
|
|
|
$iten_check = count($productcheck);
|
|
|
|
//echo $iten_check;
|
|
|
|
$Getmaterial_code = $this->mdl_items->GetMaterialCode($itemproductid);
|
|
|
|
$material_code='';
|
|
foreach($Getmaterial_code as $mas)
|
|
{
|
|
$material_code = $mas->MaterialCode;
|
|
}
|
|
//echo $material_code;die();
|
|
|
|
$GetclientId = $this->mdl_items->GetClientID($invoice_id);
|
|
|
|
$clientId='';
|
|
|
|
foreach($GetclientId as $cs)
|
|
{
|
|
$clientId = $cs->client_id;
|
|
}
|
|
|
|
$dt = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
|
|
$createddt = $dt->format('Y-m-d H:i:s');
|
|
|
|
|
|
$get_transctiontype = $this->mdl_items->GetTransactionType($invoice_id);
|
|
|
|
|
|
//$getcreditdet = $this->mdl_items->CheckCredit($invoice_id);
|
|
|
|
//print_r($getcreditdet);die();
|
|
|
|
//$crecheck = count($getcreditdet);
|
|
|
|
|
|
|
|
// print_r($get_transctiontype);die();
|
|
|
|
$count_of_inv='';
|
|
|
|
$tratype='';
|
|
|
|
$ReType ='';
|
|
|
|
foreach($get_transctiontype as $ge)
|
|
{
|
|
$count_of_inv= $ge->creditinvoice_parent_id;
|
|
}
|
|
|
|
|
|
|
|
if(count($count_of_inv)<=0)
|
|
{
|
|
$tratype='Reduce';
|
|
|
|
$ReType ='Invoice';
|
|
}
|
|
|
|
else
|
|
{
|
|
$tratype='Add';
|
|
$ReType ='Credit Note';
|
|
}
|
|
|
|
|
|
$creditQty = abs($itemqty);
|
|
|
|
|
|
//echo $creditQty;die();
|
|
|
|
if($count_of_inv>0)
|
|
{
|
|
// if($crecheck<=0)
|
|
// {
|
|
// $stockHistory = array('MaterialCode'=>$material_code,'Transaction_type'=>'Add','Ref_Type'=>'Credit Note','Ref_No'=>$invoice_id,'SupplierID'=>$clientId,'Quantity'=>$creditQty,'ItemValue'=>$itemprice,'CreatedOn'=>$createddt);
|
|
|
|
// //$saveStockHistory = $this->mdl_items->SaveStockHistory($stockHistory);
|
|
// }
|
|
|
|
// else
|
|
// {
|
|
// $stockHistory = array('MaterialCode'=>$material_code,'Quantity'=>$creditQty,'ItemValue'=>$itemprice,'UpdatedOn'=>$createddt);
|
|
|
|
// //$updateFinalHistory = $this->mdl_items->UpdateStockHistory($stockHistory,$invoice_id,$material_code);
|
|
|
|
// }
|
|
}
|
|
|
|
|
|
|
|
if($iten_check>0)
|
|
{
|
|
|
|
|
|
//echo 'if';die();
|
|
$final_productarray=array('ProductCode'=>$itemproductid,'Quantity'=>$itemqty,'Price'=>$itemprice,'Transactiondate'=>$prodate);
|
|
|
|
//$savefinal = $this->mdl_items->UpdateFinalHistory($final_productarray,$invoice_id,$itemproductid);
|
|
|
|
|
|
$stockHistory = array('MaterialCode'=>$material_code,'Quantity'=>$itemqty,'ItemValue'=>$itemprice,'UpdatedOn'=>$createddt);
|
|
|
|
//$updateFinalHistory = $this->mdl_items->UpdateStockHistory($stockHistory,$invoice_id,$material_code);
|
|
|
|
|
|
}
|
|
|
|
else
|
|
{
|
|
|
|
|
|
//echo 'else';die();
|
|
// $invoice_details= $this->mdl_items->GetInvoiceDeatisl($invoice_id);
|
|
// $inv_no='';
|
|
|
|
// if(!empty($invoice_details))
|
|
// {
|
|
// foreach($invoice_details as $in)
|
|
// {
|
|
// $inv_no = $in->invoice_number;
|
|
// }
|
|
// }
|
|
|
|
|
|
//$finalhistory= array('RefID'=>$inv_no,'invoice_id'=>$invoice_id,'TransactionType'=>'Outward','ProductCode'=>$itemproductid,'Quantity'=>$itemqty,'Price'=>$itemprice,'Transactiondate'=>$prodate);
|
|
|
|
//$saveinvhistory=$this->mdl_items->SaveFinalHistory($finalhistory);
|
|
|
|
|
|
|
|
// $tratype='';
|
|
|
|
// $get_transctiontype = $this->mdl_items->GetTransactionType($invoice_id);
|
|
|
|
// print_r($get_transctiontype);die();
|
|
|
|
|
|
//$stockHistory = array('MaterialCode'=>$material_code,'Transaction_type'=>'Reduce','Ref_Type'=>'Invoice','Ref_No'=>$invoice_id,'SupplierID'=>$clientId,'Quantity'=>$itemqty,'ItemValue'=>$itemprice,'CreatedOn'=>$createddt);
|
|
|
|
|
|
|
|
|
|
//$saveStockHistory = $this->mdl_items->SaveStockHistory($stockHistory);
|
|
}
|
|
// echo $invoice_id;
|
|
//echo $item_id;
|
|
$invexist= $this->mdl_items->Check_invoice($invoice_id,$item_id);
|
|
|
|
//echo count($invexist);
|
|
//die();
|
|
$this->mdl_items->save($item_id, $item);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
{
|
|
// Throw an error message and use the form validation for that
|
|
$this->load->library('form_validation');
|
|
// $this->form_validation->set_rules('item_name', trans('item'), 'required');
|
|
$this->form_validation->run();
|
|
|
|
$response = array(
|
|
'success' => 0,
|
|
'validation_errors' => array(
|
|
'item_name' => form_error('item_name', '', ''),
|
|
)
|
|
);
|
|
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
|
|
$invoicedetails=array('invoice_id'=>$invoice_id,'quote_id'=>$quoteid1);
|
|
|
|
$InvId_check= $this->mdl_invoices->CheckInvId($invoice_id);
|
|
|
|
|
|
|
|
foreach($InvId_check as $idc)
|
|
{
|
|
$cid = $idc->invcount;
|
|
}
|
|
|
|
|
|
if($quoteid1 !=0)
|
|
{
|
|
if($cid == 0)
|
|
{
|
|
|
|
$saveinvtable=$this->mdl_invoices->Save_quote_to_invtable($invoicedetails);
|
|
}
|
|
}
|
|
|
|
|
|
$invoice_status = $this->input->post('invoice_status_id');
|
|
|
|
//echo $invoice_status;
|
|
|
|
|
|
if ($this->input->post('invoice_discount_amount') === '')
|
|
{
|
|
$invoice_discount_amount = floatval(0);
|
|
}
|
|
else
|
|
{
|
|
$invoice_discount_amount = $this->input->post('invoice_discount_amount');
|
|
}
|
|
|
|
|
|
|
|
if ($this->input->post('invoice_discount_percent') === '') {
|
|
$invoice_discount_percent = floatval(0);
|
|
} else {
|
|
$invoice_discount_percent = $this->input->post('invoice_discount_percent');
|
|
}
|
|
|
|
// Generate new invoice number if needed
|
|
$invoice_number = $this->input->post('invoice_number');
|
|
|
|
if (empty($invoice_number) && $invoice_status != 1) {
|
|
$invoice_group_id = $this->mdl_invoices->get_invoice_group_id($invoice_id);
|
|
$invoice_number = $this->mdl_invoices->get_invoice_number($invoice_group_id);
|
|
}
|
|
|
|
|
|
|
|
$db_array = array(
|
|
'invoice_number' => $invoice_number,
|
|
'invoice_terms' => $this->input->post('invoice_terms'),
|
|
'invoice_date_created' => date_to_mysql($this->input->post('invoice_date_created')),
|
|
'invoice_date_due' => date_to_mysql($this->input->post('invoice_date_due')),
|
|
'invoice_password' => $this->input->post('invoice_password'),
|
|
'invoice_status_id' => $invoice_status,
|
|
'payment_method' => $this->input->post('payment_method'),
|
|
'invoice_discount_amount' => standardize_amount($invoice_discount_amount),
|
|
'invoice_discount_percent' => standardize_amount($invoice_discount_percent),
|
|
);
|
|
|
|
|
|
//print_r($db_array);
|
|
|
|
|
|
|
|
// check if status changed to sent, the feature is enabled and settings is set to sent
|
|
if ($this->config->item('disable_read_only') === false) {
|
|
if ($invoice_status == get_setting('read_only_toggle')) {
|
|
$db_array['is_read_only'] = 1;
|
|
}
|
|
}
|
|
|
|
$this->mdl_invoices->save($invoice_id, $db_array);
|
|
$sumexInvoice = $this->mdl_invoices->where('sumex_invoice', $invoice_id)->get()->num_rows();
|
|
|
|
|
|
|
|
if ($sumexInvoice >= 1) {
|
|
$sumex_array = array(
|
|
'sumex_invoice' => $invoice_id,
|
|
'sumex_reason' => $this->input->post('invoice_sumex_reason'),
|
|
'sumex_diagnosis' => $this->input->post('invoice_sumex_diagnosis'),
|
|
'sumex_treatmentstart' => date_to_mysql($this->input->post('invoice_sumex_treatmentstart')),
|
|
'sumex_treatmentend' => date_to_mysql($this->input->post('invoice_sumex_treatmentend')),
|
|
'sumex_casedate' => date_to_mysql($this->input->post('invoice_sumex_casedate')),
|
|
'sumex_casenumber' => $this->input->post('invoice_sumex_casenumber'),
|
|
'sumex_observations' => $this->input->post('invoice_sumex_observations')
|
|
);
|
|
$this->mdl_invoice_sumex->save($invoice_id, $sumex_array);
|
|
}
|
|
|
|
|
|
|
|
|
|
// Recalculate for discounts
|
|
$this->load->model('invoices/mdl_invoice_amounts');
|
|
// $this->mdl_invoice_amounts->calculate($invoice_id);
|
|
|
|
$response = array(
|
|
'success' => 1,
|
|
);
|
|
} else {
|
|
$this->load->helper('json_error');
|
|
$response = array(
|
|
'success' => 0,
|
|
'validation_errors' => json_errors()
|
|
);
|
|
}
|
|
//print_r($this->input->post('custom'));
|
|
$values = [];
|
|
if($quoteid1 == 0)
|
|
{
|
|
// Save all custom fields
|
|
if ($this->input->post('custom')) {
|
|
|
|
//print_r($this->input->post('custom'));
|
|
|
|
$varray=$this->input->post('custom');
|
|
|
|
|
|
$db_array = array();
|
|
|
|
|
|
foreach ($this->input->post('custom') as $custom)
|
|
{
|
|
|
|
// print_r($custom);
|
|
if (preg_match("/^(.*)\[\]$/i", $custom['name'], $matches))
|
|
{
|
|
$values[$matches[1]][] = $custom['value'];
|
|
|
|
//echo $custom['value'];
|
|
}
|
|
|
|
else
|
|
{
|
|
$values[$custom['name']] = $custom['value'];
|
|
|
|
//echo $values[$custom['name']];
|
|
}
|
|
}
|
|
|
|
foreach ($values as $key => $value)
|
|
{
|
|
|
|
preg_match("/^custom\[(.*?)\](?:\[\]|)$/", $key, $matches);
|
|
if ($matches)
|
|
{
|
|
|
|
$db_array[$matches[1]] = $value;
|
|
}
|
|
}
|
|
|
|
|
|
$this->load->model('custom_fields/mdl_invoice_custom');
|
|
|
|
|
|
$result = $this->mdl_invoice_custom->save_custom($invoice_id, $db_array);
|
|
|
|
//die();
|
|
if ($result !== true) {
|
|
$response = array(
|
|
'success' => 0,
|
|
'validation_errors' => $result
|
|
);
|
|
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|
|
}
|
|
}
|
|
|
|
else
|
|
{
|
|
//$this->load->model('invoices/mdl_items');
|
|
|
|
if ($this->input->post('custom')) {
|
|
|
|
// print_r($this->input->post('custom'));
|
|
|
|
$varray=$this->input->post('custom');
|
|
|
|
$Quotecustomvalue= $this->mdl_items->get_quoteproduct($quoteid1);
|
|
// print_r($Quotecustomvalue);
|
|
|
|
|
|
$pono = $Quotecustomvalue[0]->quote_pono;
|
|
$dmy = date('d-m-Y',strtotime($Quotecustomvalue[0]->po_date_created));
|
|
|
|
// echo $pono;
|
|
// echo $dmy;
|
|
// die();
|
|
|
|
|
|
$db_array = array();
|
|
|
|
|
|
foreach ($this->input->post('custom') as $custom)
|
|
{
|
|
|
|
|
|
if (preg_match("/^(.*)\[\]$/i", $custom['name'], $matches))
|
|
{
|
|
$values[$matches[1]][] = $custom['value'];
|
|
|
|
|
|
}
|
|
|
|
else
|
|
{
|
|
$values[$custom['name']] = $custom['value'];
|
|
|
|
|
|
}
|
|
}
|
|
|
|
foreach ($values as $key => $value)
|
|
{
|
|
|
|
if(($key !="custom[15]") && ($key !="custom[14]"))
|
|
{
|
|
|
|
|
|
|
|
}else{
|
|
|
|
if($key == "custom[14]")
|
|
{
|
|
$value=$pono;
|
|
}
|
|
else if($key == "custom[15]")
|
|
{
|
|
$value=$dmy;
|
|
}
|
|
|
|
}
|
|
|
|
preg_match("/^custom\[(.*?)\](?:\[\]|)$/", $key, $matches);
|
|
if ($matches)
|
|
{
|
|
|
|
$db_array[$matches[1]] = $value;
|
|
}
|
|
|
|
|
|
// print_r($db_array);
|
|
$this->load->model('custom_fields/mdl_invoice_custom');
|
|
|
|
|
|
$result = $this->mdl_invoice_custom->save_custom($invoice_id, $db_array);
|
|
|
|
|
|
}
|
|
//print_r($db_array);
|
|
|
|
if ($result !== true) {
|
|
$response = array(
|
|
'success' => 0,
|
|
'validation_errors' => $result
|
|
);
|
|
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
echo json_encode($response);
|
|
}
|
|
|
|
|
|
|
|
|
|
public function save_invoice_tax_rate()
|
|
{
|
|
|
|
|
|
$this->load->model('invoices/mdl_invoice_tax_rates');
|
|
|
|
if ($this->mdl_invoice_tax_rates->run_validation()) {
|
|
$this->mdl_invoice_tax_rates->save();
|
|
|
|
$response = array(
|
|
'success' => 1
|
|
);
|
|
} else {
|
|
$response = array(
|
|
'success' => 0,
|
|
'validation_errors' => $this->mdl_invoice_tax_rates->validation_errors
|
|
);
|
|
}
|
|
|
|
echo json_encode($response);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->load->model('invoices/mdl_invoices');
|
|
$this->load->model('invoices/mdl_items');
|
|
|
|
if ($this->mdl_invoices->run_validation()) {
|
|
$invoice_id = $this->mdl_invoices->create();
|
|
|
|
$response = array(
|
|
'success' => 1,
|
|
//'drpcheck'=>$dropcheck,
|
|
'invoice_id' => $invoice_id
|
|
);
|
|
} else {
|
|
$this->load->helper('json_error');
|
|
$response = array(
|
|
'success' => 0,
|
|
'validation_errors' => json_errors()
|
|
);
|
|
}
|
|
|
|
// $invoice_details= $this->mdl_items->GetInvoiceDeatis($invoice_id);
|
|
// $inv_no='';
|
|
|
|
// if(!empty($invoice_details))
|
|
// {
|
|
// foreach($invoice_details as $in)
|
|
// {
|
|
// $inv_no = $in->invoice_number;
|
|
// }
|
|
// }
|
|
|
|
// $finalhistory= array('RefID'=>$inv_no,'invoice_id'=>$invoice_id,'TransactionType'=>'Outward');
|
|
// $saveinvhistory=$this->mdl_items->SaveFinalHistory($finalhistory);
|
|
|
|
echo json_encode($response);
|
|
}
|
|
|
|
public function create_recurring()
|
|
{
|
|
$this->load->model('invoices/mdl_invoices_recurring');
|
|
|
|
if ($this->mdl_invoices_recurring->run_validation()) {
|
|
$this->mdl_invoices_recurring->save();
|
|
|
|
$response = array(
|
|
'success' => 1,
|
|
);
|
|
} else {
|
|
$this->load->helper('json_error');
|
|
$response = array(
|
|
'success' => 0,
|
|
'validation_errors' => json_errors()
|
|
);
|
|
}
|
|
|
|
echo json_encode($response);
|
|
}
|
|
|
|
public function get_item()
|
|
{
|
|
$this->load->model('invoices/mdl_items');
|
|
|
|
$item = $this->mdl_items->get_by_id($this->input->post('item_id'));
|
|
print_r($item);
|
|
echo json_encode($item);
|
|
}
|
|
|
|
public function modal_create_invoice()
|
|
{
|
|
|
|
|
|
$this->load->module('layout');
|
|
$this->load->model('invoice_groups/mdl_invoice_groups');
|
|
$this->load->model('tax_rates/mdl_tax_rates');
|
|
$this->load->model('clients/mdl_clients');
|
|
|
|
$data = array(
|
|
'invoice_groups' => $this->mdl_invoice_groups->get()->result(),
|
|
'tax_rates' => $this->mdl_tax_rates->get()->result(),
|
|
'client' => $this->mdl_clients->get_by_id($this->input->post('client_id')),
|
|
'clients' => $this->mdl_clients->get_latest(),
|
|
);
|
|
|
|
$this->layout->load_view('invoices/modal_create_invoice', $data);
|
|
}
|
|
|
|
|
|
|
|
// public function modal_create_poinvoice()
|
|
// {
|
|
|
|
|
|
// $this->load->module('layout');
|
|
// $this->load->model('invoice_groups/mdl_invoice_groups');
|
|
// $this->load->model('tax_rates/mdl_tax_rates');
|
|
// $this->load->model('clients/mdl_clients');
|
|
|
|
// $data = array(
|
|
// 'invoice_groups' => $this->mdl_invoice_groups->get()->result(),
|
|
// 'tax_rates' => $this->mdl_tax_rates->get()->result(),
|
|
// 'client' => $this->mdl_clients->get_by_id($this->input->post('client_id')),
|
|
// 'clients' => $this->mdl_clients->get_latest(),
|
|
// );
|
|
|
|
// $this->layout->load_view('invoices/modal_create_poinvoice', $data);
|
|
// }
|
|
|
|
|
|
|
|
public function test()
|
|
{
|
|
|
|
echo "erv";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function modal_create_recurring()
|
|
{
|
|
$this->load->module('layout');
|
|
|
|
$this->load->model('mdl_invoices_recurring');
|
|
|
|
$data = array(
|
|
'invoice_id' => $this->input->post('invoice_id'),
|
|
'recur_frequencies' => $this->mdl_invoices_recurring->recur_frequencies
|
|
);
|
|
|
|
$this->layout->load_view('invoices/modal_create_recurring', $data);
|
|
}
|
|
|
|
public function get_recur_start_date()
|
|
{
|
|
$invoice_date = $this->input->post('invoice_date');
|
|
$recur_frequency = $this->input->post('recur_frequency');
|
|
|
|
echo increment_user_date($invoice_date, $recur_frequency);
|
|
}
|
|
|
|
public function modal_change_client()
|
|
{
|
|
$this->load->module('layout');
|
|
$this->load->model('clients/mdl_clients');
|
|
|
|
$data = array(
|
|
'client_id' => $this->input->post('client_id'),
|
|
'invoice_id' => $this->input->post('invoice_id'),
|
|
'clients' => $this->mdl_clients->get_latest(),
|
|
);
|
|
|
|
$this->layout->load_view('invoices/modal_change_client', $data);
|
|
}
|
|
|
|
public function change_client()
|
|
{
|
|
$this->load->model('invoices/mdl_invoices');
|
|
$this->load->model('clients/mdl_clients');
|
|
|
|
// Get the client ID
|
|
$client_id = $this->input->post('client_id');
|
|
$client = $this->mdl_clients->where('ip_clients.client_id', $client_id)->get()->row();
|
|
|
|
if (!empty($client)) {
|
|
$invoice_id = $this->input->post('invoice_id');
|
|
|
|
$db_array = array(
|
|
'client_id' => $client_id,
|
|
);
|
|
$this->db->where('invoice_id', $invoice_id);
|
|
$this->db->update('ip_invoices', $db_array);
|
|
|
|
$response = array(
|
|
'success' => 1,
|
|
'invoice_id' => $invoice_id,
|
|
);
|
|
} else {
|
|
$this->load->helper('json_error');
|
|
$response = array(
|
|
'success' => 0,
|
|
'validation_errors' => json_errors()
|
|
);
|
|
}
|
|
|
|
echo json_encode($response);
|
|
}
|
|
|
|
public function modal_copy_invoice()
|
|
{
|
|
$this->load->module('layout');
|
|
|
|
$this->load->model('invoices/mdl_invoices');
|
|
$this->load->model('invoice_groups/mdl_invoice_groups');
|
|
$this->load->model('tax_rates/mdl_tax_rates');
|
|
|
|
$data = array(
|
|
'invoice_groups' => $this->mdl_invoice_groups->get()->result(),
|
|
'tax_rates' => $this->mdl_tax_rates->get()->result(),
|
|
'invoice_id' => $this->input->post('invoice_id'),
|
|
'invoice' => $this->mdl_invoices->where('ip_invoices.invoice_id', $this->input->post('invoice_id'))->get()->row(),
|
|
);
|
|
|
|
$this->layout->load_view('invoices/modal_copy_invoice', $data);
|
|
}
|
|
|
|
public function copy_invoice()
|
|
{
|
|
$this->load->model('invoices/mdl_invoices');
|
|
$this->load->model('invoices/mdl_items');
|
|
$this->load->model('invoices/mdl_invoice_tax_rates');
|
|
|
|
if ($this->mdl_invoices->run_validation()) {
|
|
$target_id = $this->mdl_invoices->save();
|
|
$source_id = $this->input->post('invoice_id');
|
|
|
|
$this->mdl_invoices->copy_invoice($source_id, $target_id);
|
|
|
|
$response = array(
|
|
'success' => 1,
|
|
'invoice_id' => $target_id
|
|
);
|
|
} else {
|
|
$this->load->helper('json_error');
|
|
$response = array(
|
|
'success' => 0,
|
|
'validation_errors' => json_errors()
|
|
);
|
|
}
|
|
|
|
echo json_encode($response);
|
|
}
|
|
|
|
public function modal_create_credit()
|
|
{
|
|
$this->load->module('layout');
|
|
|
|
$this->load->model('invoices/mdl_invoices');
|
|
$this->load->model('invoice_groups/mdl_invoice_groups');
|
|
$this->load->model('tax_rates/mdl_tax_rates');
|
|
|
|
$data = array(
|
|
'invoice_groups' => $this->mdl_invoice_groups->get()->result(),
|
|
'tax_rates' => $this->mdl_tax_rates->get()->result(),
|
|
'invoice_id' => $this->input->post('invoice_id'),
|
|
'invoice' => $this->mdl_invoices->where('ip_invoices.invoice_id', $this->input->post('invoice_id'))->get()->row()
|
|
);
|
|
|
|
$this->layout->load_view('invoices/modal_create_credit', $data);
|
|
}
|
|
|
|
public function create_credit()
|
|
{
|
|
$this->load->model('invoices/mdl_invoices');
|
|
$this->load->model('invoices/mdl_items');
|
|
$this->load->model('invoices/mdl_invoice_tax_rates');
|
|
|
|
if ($this->mdl_invoices->run_validation()) {
|
|
$target_id = $this->mdl_invoices->save();
|
|
$source_id = $this->input->post('invoice_id');
|
|
|
|
$this->mdl_invoices->copy_credit_invoice($source_id, $target_id);
|
|
|
|
$sourceinv_array= array('is_read_only'=>'1','invoice_status_id'=>'2');
|
|
|
|
// Set source invoice to read-only
|
|
if ($this->config->item('disable_read_only') == false) {
|
|
$this->mdl_invoices->where('invoice_id', $source_id);
|
|
$this->mdl_invoices->update('ip_invoices', $sourceinv_array);
|
|
}
|
|
|
|
// Set target invoice to credit invoice
|
|
$this->mdl_invoices->where('invoice_id', $target_id);
|
|
$this->mdl_invoices->update('ip_invoices', array('creditinvoice_parent_id' => $source_id));
|
|
|
|
$this->mdl_invoices->where('invoice_id', $target_id);
|
|
$this->mdl_invoices->update('ip_invoice_amounts', array('invoice_sign' => '-1'));
|
|
|
|
$response = array(
|
|
'success' => 1,
|
|
'invoice_id' => $target_id
|
|
);
|
|
} else {
|
|
$this->load->helper('json_error');
|
|
$response = array(
|
|
'success' => 0,
|
|
'validation_errors' => json_errors()
|
|
);
|
|
}
|
|
|
|
echo json_encode($response);
|
|
}
|
|
|
|
}
|