skip to Main Content

I have to update the column opcash when the user creates a new entry on the previous date i have to update the prev date opcash column and also today opcash column where opcash is the sum of total credit amount – sum of total debit amount..And that opcash have to be added with total credit amount when the user enters on credit column and have to be subtracted when the user enters the amount on debit column the process continue for all the days..My problem is the opcash value is correct when the user proceeds with correct order..that is if the user creates a entry on a continous day that is if the user creates a entry on 25-12-2018,26-12-2018,27-12-2018..If the user creates a entry on 26-12-2018 and if the user creates a entry on 25-12-2018 the opcash value is updated on 25-12-2018 but not updated on 26-12-2018..Please help me to change the opcash value when the user create a new entry on the previous day..

 public function insert()
 {
    $session_data = $this->session->userdata('logged_in');
    $data['username'] = $session_data['username'];
    $dc=$this->input->post('dc');
$startdate = $this->input->post('TDate');
$date = str_replace('/', '-', $startdate);
$newDate = date("d/m/Y", strtotime($date));  
        if($dc=='c'){
        $amount=$this->input->post('credit1');
                    } 
        else if ($dc=='d') {
        $amount=$this->input->post('debit');
    }

$data=array(
  'date'=>$newDate,
  'code' =>$this->input->post('TName'),
  'project' =>$this->input->post('TName'),
  'part' =>$this->input->post('part1'),
  'part1' =>$this->input->post('part2'),
  'dc'=>$this->input->post('dc'),
  'amount'=>$amount,
   );
$this->db->insert('daybook',$data);
this->db->where('dc','c');
$this->db->select_sum('amount');
$total_credit= $this->db->get_where('daybook',array('date' => $newDate))- 
>row_array();
$this->db->where('dc','d');
$this->db->select_sum('amount');
$total_debit= $this->db->get_where('daybook',array('date' => $newDate))- 
>row_array();
$prev_date = date('d/m/Y', strtotime('-1 day'));
$result2 = $this->db->query("SELECT opcash FROM daytot WHERE 
tdate='$newDate'")->row_array();
$dc=$this->input->post('dc');
$total_credit1=0;
$total_debit1=0;
$opcash=0;
if($dc=='d')
{
 $amount=$this->input->post('debit');
 $opcash=$result2['opcash']-$amount;
}
if($dc=='c')
{
$amount=$this->input->post('credit1');
$opcash=$result2['opcash']+$amount;
}

 $data1 = array(
'tdate'=>$newDate,
'total_credit'=>$total_credit['amount'],
'total_debit'=>$total_debit['amount'],
'opcash'=>$opcash,
 );
 $data2 = array(
'tdate'=>$newDate,
'total_credit'=>$total_credit['amount'],
'total_debit'=>'0',
'opcash'=>$this->input->post('amount')+$result2['opcash'],
 );
 $data3 = array(
'tdate'=>$newDate,
'total_credit'=>'0',
'total_debit'=>$total_debit['amount'],
'opcash'=>$result2['opcash']-$this->input->post('amount'),
);
    $startdate = $this->input->post('TDate');
$date = str_replace('/', '-', $startdate);
$newDate = date("d/m/Y", strtotime($date));
    $objQuery = $this->db->get_where('daytot', array('tdate' => $newDate));
   if ($objQuery->num_rows() > 0) { //UPDATE
$this->db->where('tdate', $newDate);
$this->db->update('daytot', $data1);
 }
else{
$dc=$this->input->post('dc');
if ($dc=='c') {
$this->db->insert('daytot', $data2);
  }
if ($dc=='d') {
$this->db->insert('daytot', $data3);
}
}
$this->session->set_flashdata('Add', 'Inserted Successfully');
redirect('BookKeeping/daybook','refresh');
}

Daybook

    Table structure for table `daybook`
    --

    CREATE TABLE `daybook` (
      `recno` int(5) NOT NULL,
      `date` varchar(15) NOT NULL,
      `code` varchar(10) NOT NULL,
      `project` varchar(10) NOT NULL,
      `part` varchar(255) NOT NULL,
      `part1` varchar(255) NOT NULL,
      `amount` int(5) NOT NULL,
      `dc` varchar(5) NOT NULL,
      `ledreno` int(10) NOT NULL,
      `vouno` int(10) NOT NULL,
      `pvouno` varchar(100) NOT NULL,
      `type` varchar(100) NOT NULL,
      `mark` varchar(100) NOT NULL,
      `slno` varchar(100) NOT NULL,
      `shift` varchar(100) NOT NULL,
      `pacno` int(10) NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

    --
    -- Dumping data for table `daybook`
    --

    INSERT INTO `daybook` (`recno`, `date`, `code`, `project`, `part`, `part1`, `amount`, `dc`, `ledreno`, `vouno`, `pvouno`, `type`, `mark`, `slno`, `shift`, `pacno`) VALUES
    (439, '24/12/2018', '1', '1', 'account', 'details', 500, 'c', 0, 0, '', '', '', '', '', 0),
    (440, '24/12/2018', '1', '1', 'praveen accounts', 'laavanya accounts', 500, 'c', 0, 0, '', '', '', '', '', 0),
    (441, '25/12/2018', '1', '1', 'praveen kumar accounts', 'praveen kumar accounts', 1000, 'c', 0, 0, '', '', '', '', '', 0),
    (442, '25/12/2018', '1', '1', 'abcdef', 'ghijklm', 100, 'd', 0, 0, '', '', '', '', '', 0),
    (443, '25/12/2018', '1', '1', 'xxxxxx', 'yyyyyyy', 100, 'c', 0, 0, '', '', '', '', '', 0),
    (444, '24/12/2018', '1', '1', 'abcdef', 'ghijklm', 1000, 'c', 0, 0, '', '', '', '', '', 0);

    --
    -- Indexes for dumped tables
    --

    --
    -- Indexes for table `daybook`
    --
    ALTER TABLE `daybook`
      ADD PRIMARY KEY (`recno`);

    --
    -- AUTO_INCREMENT for dumped tables
    --

    --
    -- AUTO_INCREMENT for table `daybook`
    --
    ALTER TABLE `daybook`
      MODIFY `recno` int(5) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=445;COMMIT;

    Daytot
-- phpMyAdmin SQL Dump
-- version 4.7.0
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Dec 26, 2018 at 07:37 AM
-- Server version: 10.1.25-MariaDB
-- PHP Version: 5.6.31

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `ci_fina`
--

-- --------------------------------------------------------

--
-- Table structure for table `daytot`
--

CREATE TABLE `daytot` (
  `tdate` varchar(15) NOT NULL,
  `id` int(11) NOT NULL,
  `total_credit` int(11) NOT NULL,
  `total_debit` int(11) NOT NULL,
  `opcash` int(11) NOT NULL,
  `clcash` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `daytot`
--

INSERT INTO `daytot` (`tdate`, `id`, `total_credit`, `total_debit`, `opcash`, `clcash`) VALUES
('24/12/2018', 76, 2000, 0, 2000, 0),
('25/12/2018', 77, 1100, 100, 2000, 0);

--
-- Indexes for dumped tables
--

--
-- Indexes for table `daytot`
--
ALTER TABLE `daytot`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `daytot`
--
ALTER TABLE `daytot`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=78;COMMIT;

  [1]: https://i.stack.imgur.com/REU36.png
  [2]: https://i.stack.imgur.com/fVagp.png

2

Answers


  1. Try to change the date format in your query. Normally the date format in the table is Y-m-d:

    $newDate = date("Y-m-d", strtotime($date));
    
    Login or Signup to reply.
  2. I created a SQLFiddle to show how you can get the same results as your daytot table without having to maintain that table. It also shows the date columns as DATE type instead of VARCHAR.

    By using the DATE type in your table instead of the VARCHAR, you prevent several possible problems in the future. It also allows you to sort and select by date correctly, to select date ranges, etc. It also eliminates any problems related to when rows are inserted into the daybook table, and handles updates and deletes to the daybook table.

    SQL Fiddle

    MySQL 5.6 Schema Setup:

        CREATE TABLE `daybook` (
          `recno` int(5) NOT NULL,
          `date` DATE NOT NULL,
          `code` varchar(10) NOT NULL,
          `project` varchar(10) NOT NULL,
          `part` varchar(255) NOT NULL,
          `part1` varchar(255) NOT NULL,
          `amount` int(5) NOT NULL,
          `dc` varchar(5) NOT NULL,
          `ledreno` int(10) NOT NULL,
          `vouno` int(10) NOT NULL,
          `pvouno` varchar(100) NOT NULL,
          `type` varchar(100) NOT NULL,
          `mark` varchar(100) NOT NULL,
          `slno` varchar(100) NOT NULL,
          `shift` varchar(100) NOT NULL,
          `pacno` int(10) NOT NULL
        ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
        --
        -- Dumping data for table `daybook`
        --
    
        INSERT INTO `daybook` (`recno`, `date`, `code`, `project`, `part`, `part1`, `amount`, `dc`, `ledreno`, `vouno`, `pvouno`, `type`, `mark`, `slno`, `shift`, `pacno`) VALUES
        (439, '2018/12/24', '1', '1', 'account', 'details', 500, 'c', 0, 0, '', '', '', '', '', 0),
        (440, '2018/12/24', '1', '1', 'praveen accounts', 'laavanya accounts', 500, 'c', 0, 0, '', '', '', '', '', 0),
        (441, '2018/12/25', '1', '1', 'praveen kumar accounts', 'praveen kumar accounts', 1000, 'c', 0, 0, '', '', '', '', '', 0),
        (442, '2018/12/25', '1', '1', 'abcdef', 'ghijklm', 100, 'd', 0, 0, '', '', '', '', '', 0),
        (443, '2018/12/25', '1', '1', 'xxxxxx', 'yyyyyyy', 100, 'c', 0, 0, '', '', '', '', '', 0),
        (444, '2018/12/24', '1', '1', 'abcdef', 'ghijklm', 1000, 'c', 0, 0, '', '', '', '', '', 0);
    
        --
        -- Indexes for dumped tables
        --
    
        --
        -- Indexes for table `daybook`
        --
        ALTER TABLE `daybook`
          ADD PRIMARY KEY (`recno`);
    
        --
        -- AUTO_INCREMENT for dumped tables
        --
    
        --
        -- AUTO_INCREMENT for table `daybook`
        --
        ALTER TABLE `daybook`
          MODIFY `recno` int(5) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=445;COMMIT;
    
    -- phpMyAdmin SQL Dump
    -- version 4.7.0
    -- https://www.phpmyadmin.net/
    --
    -- Host: 127.0.0.1
    -- Generation Time: Dec 26, 2018 at 07:37 AM
    -- Server version: 10.1.25-MariaDB
    -- PHP Version: 5.6.31
    
    SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
    SET AUTOCOMMIT = 0;
    START TRANSACTION;
    SET time_zone = "+00:00";
    
    
    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!40101 SET NAMES utf8mb4 */;
    
    --
    -- Database: `ci_fina`
    --
    
    -- --------------------------------------------------------
    
    --
    -- Table structure for table `daytot`
    --
    
    CREATE TABLE `daytot` (
      `tdate` DATE NOT NULL,
      `id` int(11) NOT NULL,
      `total_credit` int(11) NOT NULL,
      `total_debit` int(11) NOT NULL,
      `opcash` int(11) NOT NULL,
      `clcash` int(11) NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
    --
    -- Dumping data for table `daytot`
    --
    
    INSERT INTO `daytot` (`tdate`, `id`, `total_credit`, `total_debit`, `opcash`, `clcash`) VALUES
    ('2018/12/24', 76, 2000, 0, 2000, 0),
    ('2018/12/25', 77, 1100, 100, 2000, 0);
    
    --
    -- Indexes for dumped tables
    --
    
    --
    -- Indexes for table `daytot`
    --
    ALTER TABLE `daytot`
      ADD PRIMARY KEY (`id`);
    
    --
    -- AUTO_INCREMENT for dumped tables
    --
    
    --
    -- AUTO_INCREMENT for table `daytot`
    --
    ALTER TABLE `daytot`
      MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=78;COMMIT;
    

    Query 1:

    SELECT 
      `date` as `tdate`,
      SUM(IF(`dc` = 'c',`amount`,0)) as `total_credit`,
      SUM(IF(`dc` = 'd',`amount`,0)) as `total_debit`,
      SUM(IF(`dc` = 'c',`amount`,0)) - SUM(IF(`dc` = 'd',`amount`,0)) as `opcash`,
      0 as `clcash`
    FROM `daybook`
    GROUP BY `date`
    

    Results:

    |      tdate | total_credit | total_debit | opcash | clcash |
    |------------|--------------|-------------|--------|--------|
    | 2018-12-24 |         2000 |           0 |   2000 |      0 |
    | 2018-12-25 |         1100 |         100 |   1000 |      0 |
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search