public function executeCommission(sfWebRequest $request)
 {
     $requestparams = $request->getParameter("invoice");
     $day = $requestparams["date"]["day"];
     $month = $requestparams["date"]["month"];
     $year = $requestparams["date"]["year"];
     //if no date, date is today. else date is date given
     if (!$year or !$month or !$day) {
         $date = MyDateTime::today();
     } else {
         $date = new MyDateTime($year, $month, $day, 0, 0, 0);
     }
     $invoice = new Invoice();
     $invoice->setDate($date->getstartofmonth()->tomysql());
     $purchase = new Purchase();
     $purchase->setDate($date->getendofmonth()->tomysql());
     $this->date = $date;
     $this->form = new InvoiceForm($invoice);
     $this->toform = new PurchaseForm($purchase);
     //set up an array of employees indexed by employee id
     $this->rawemployees = Doctrine_Query::create()->from('Employee e')->where('e.commission > 0 ')->execute();
     $this->employees = array();
     foreach ($this->rawemployees as $employee) {
         $this->employees[$employee->getId()] = $employee;
     }
     $this->empinvoices = array();
     foreach ($this->employees as $employee) {
         $this->empinvoices[$employee->getId()] = Doctrine_Query::create()->from('Invoice i, i.Employee e, i.Invoicedetail id, id.Product p')->where('i.salesman_id=' . $employee->getId())->andwhere('i.date >= "' . $invoice->getDate() . '"')->andwhere('i.date <=  "' . $purchase->getDate() . '"')->orWhere('i.technician_id=' . $employee->getId())->andwhere('i.date >= "' . $invoice->getDate() . '"')->andwhere('i.date <=  "' . $purchase->getDate() . '"')->orderBy('i.invno')->execute();
     }
     $commissiontotals = array();
     foreach ($this->empinvoices as $empid => $employeedata) {
         $totalcommission = 0;
         foreach ($employeedata as $invoice) {
             if ($invoice->getStatus() == "Paid") {
                 $totalcommission += $invoice->getCommissionTotal($this->employees[$empid]);
             }
         }
         $commissiontotals[$empid] = $totalcommission;
     }
     $this->commissiontotals = $commissiontotals;
     $this->grandtotalcommission = 0;
     foreach ($commissiontotals as $total) {
         $this->grandtotalcommission += $total;
     }
 }
 public function executeDsrmulti(sfWebRequest $request)
 {
     $requestparams = $request->getParameter("invoice");
     $day = $requestparams["date"]["day"];
     $month = $requestparams["date"]["month"];
     $year = $requestparams["date"]["year"];
     $invoice = new Invoice();
     if (!$day or !$month or !$year) {
         $invoice->setDate(MyDate::today());
     } else {
         $invoice->setDate($year . "-" . $month . "-" . $day);
     }
     $requestparams = $request->getParameter("purchase");
     $day = $requestparams["date"]["day"];
     $month = $requestparams["date"]["month"];
     $year = $requestparams["date"]["year"];
     $purchase = new Purchase();
     if (!$day or !$month or !$year) {
         $purchase->setDate(MyDate::today());
     } else {
         $purchase->setDate($year . "-" . $month . "-" . $day);
     }
     $this->form = new InvoiceForm($invoice);
     $this->toform = new PurchaseForm($purchase);
     $this->purchases = PurchaseTable::fetchByDateRange($invoice->getDate(), $purchase->getDate());
     $this->events = EventTable::fetchByDatenParentclass($purchase->getDate(), "Purchase");
     $this->cashsales = 0;
     $this->chequesales = 0;
     $this->creditsales = 0;
     $this->cashother = 0;
     $this->chequeother = 0;
     $this->creditother = 0;
     $this->cashtotal = 0;
     $this->chequetotal = 0;
     $this->credittotal = 0;
     $this->deducttotal = 0;
     foreach ($this->purchases as $purchase) {
         if ($purchase->getStatus() != "Cancelled") {
             $this->cashsales += $purchase->getCash();
             $this->chequesales += $purchase->getCheque();
             $this->creditsales += $purchase->getCredit();
             $this->cashtotal += $purchase->getCash();
             $this->chequetotal += $purchase->getCheque();
             $this->credittotal += $purchase->getCredit();
             //$this->deducttotal+=$purchase->getDsrdeduction();
         }
     }
     foreach ($this->events as $event) {
         $purchase = $event->getParent();
         if ($purchase->getStatus() != "Cancelled") {
             $this->cashother += $event->getDetail("cashamt");
             $this->chequeother += $event->getDetail("chequeamt");
             $this->creditother += $event->getDetail("creditamt");
             $this->cashtotal += $event->getDetail("cashamt");
             $this->chequetotal += $event->getDetail("chequeamt");
             $this->credittotal += $event->getDetail("creditamt");
             $this->deducttotal += $event->getDetail3();
         }
     }
     $this->total = $this->cashtotal + $this->chequetotal + $this->credittotal;
 }