protected function processForm(sfWebRequest $request, sfForm $form)
 {
     $requestparamspurchase = $request->getParameter("purchase");
     $requestparams = $request->getParameter($form->getName());
     $barcoderoot = $requestparams["product_id"] . str_pad($requestparamspurchase["pono"], 5, "0", STR_PAD_LEFT);
     $requestparams["barcode"] = MyBarcode::standardize($barcoderoot);
     $form->bind($requestparams);
     if ($form->isValid()) {
         $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';
         try {
             $purchasedetail = $form->save();
             $purchasedetail->updateStockentry();
             $purchasedetail->updateProduct();
             //custom calculation
             if ($purchasedetail->getDescription() == "") {
                 $purchasedetail->setDescription($purchasedetail->getProduct()->getDescription());
             }
             $purchasedetail->calc();
             $purchasedetail->save();
             $purchase = $purchasedetail->getPurchase();
             $purchase->calc();
             $purchase->save();
         } catch (Doctrine_Validator_Exception $e) {
             $errorStack = $form->getObject()->getErrorStack();
             $message = get_class($form->getObject()) . ' has ' . count($errorStack) . " field" . (count($errorStack) > 1 ? 's' : null) . " with validation errors: ";
             foreach ($errorStack as $field => $errors) {
                 $message .= "{$field} (" . implode(", ", $errors) . "), ";
             }
             $message = trim($message, ', ');
             $this->getUser()->setFlash('error', $message);
             return sfView::SUCCESS;
         }
         $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $purchasedetail)));
         if ($request->hasParameter('_save_and_add')) {
             $this->getUser()->setFlash('notice', $notice . ' You can add another one below.');
             $this->redirect('@purchasedetail_new?purchase_id=' . $purchase->getId());
         } else {
             $this->getUser()->setFlash('notice', $notice);
             $this->redirect('purchase/view?id=' . $purchasedetail->getPurchaseId());
         }
     } else {
         $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
     }
 }
    $col = $count % 3;
    $pos[$row][$col] = $print;
}
//barcode height
$h = 11;
//column width
$w = 70;
foreach ($pos as $line) {
    if (array_key_exists(0, $line) and array_key_exists(0, $line) and $line[0] >= 0) {
        $pdf->write1DBarcode(MyBarcode::standardize($products[$line[0]]->getId() . str_pad($purchase->getPono(), 5, "0", STR_PAD_LEFT)), 'EAN13', '12.5', '', '', $h, 0.4, $style, 'T');
    }
    if (array_key_exists(1, $line) and array_key_exists(1, $line) and $line[1] >= 0) {
        $pdf->write1DBarcode(MyBarcode::standardize($products[$line[1]]->getId() . str_pad($purchase->getPono(), 5, "0", STR_PAD_LEFT)), 'EAN13', '82.5', '', '', $h, 0.4, $style, 'T');
    }
    if (array_key_exists(2, $line) and array_key_exists(2, $line) and $line[2] >= 0) {
        $pdf->write1DBarcode(MyBarcode::standardize($products[$line[2]]->getId() . str_pad($purchase->getPono(), 5, "0", STR_PAD_LEFT)), 'EAN13', '152.5', '', '', $h, 0.4, $style, 'T');
    }
    $pdf->write1DBarcode('4800529501110', 'EAN13', '500', '', '', $h, 0.4, $style, 'N');
    if (array_key_exists(0, $line) and array_key_exists(0, $line) and $line[0] >= 0) {
        $pdf->Cell($w, 0, html_entity_decode($products[$line[0]]->getName()), 0, 0);
    } else {
        $pdf->Cell($w, 0, '', 0, 0);
    }
    if (array_key_exists(1, $line) and array_key_exists(1, $line) and $line[1] >= 0) {
        $pdf->Cell($w, 0, html_entity_decode($products[$line[1]]->getName()), 0, 0);
    } else {
        $pdf->Cell($w, 0, '', 0, 0);
    }
    if (array_key_exists(2, $line) and array_key_exists(2, $line) and $line[2] >= 0) {
        $pdf->Cell($w, 0, html_entity_decode($products[$line[2]]->getName()), 0, 0);
    } else {
 protected function processForm(sfWebRequest $request, sfForm $form)
 {
     //if barcode exists, extract product id and purchase no.
     $requestparams = $request->getParameter("invoicedetail");
     $barcode = $requestparams["barcode"];
     $product_code = "";
     $product_id = "";
     $pono = "";
     //process barcode if exists
     if ($barcode) {
         $barcode = str_pad($barcode, 13, "0", STR_PAD_LEFT);
         $strlen = strlen($barcode);
         $product_id = substr($barcode, 0, $strlen - 6);
         $pono = substr($barcode, $strlen - 6, 5);
         $requestparams["product_id"] = $product_id;
     } else {
         $barcode = str_pad($requestparams["product_id"], 7, "0", STR_PAD_LEFT) . "00000";
         $barcode = MyBarcode::standardize($barcode);
         $requestparams["barcode"] = $barcode;
     }
     $form->bind($requestparams);
     if ($form->isValid()) {
         $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';
         try {
             $invoicedetail = $form->save();
             $invoice = $invoicedetail->getInvoice();
             if (!$invoice->isCancelled()) {
                 $invoicedetail->updateProduct();
             }
             //if invoicedetail is edited while invoice is closed
             if ($invoice->getIsTemporary() == 0) {
                 $invoicedetail->updateStockentry();
             }
             //custom calculation
             if ($invoicedetail->getDescription() == "") {
                 $invoicedetail->setDescription($invoicedetail->getProduct()->getDescription());
             }
             $invoicedetail->calc();
             $invoicedetail->save();
             $invoice->calc();
             $invoice->save();
         } catch (Doctrine_Validator_Exception $e) {
             $errorStack = $form->getObject()->getErrorStack();
             $message = get_class($form->getObject()) . ' has ' . count($errorStack) . " field" . (count($errorStack) > 1 ? 's' : null) . " with validation errors: ";
             foreach ($errorStack as $field => $errors) {
                 $message .= "{$field} (" . implode(", ", $errors) . "), ";
             }
             $message = trim($message, ', ');
             $this->getUser()->setFlash('error', $message);
             return sfView::SUCCESS;
         }
         $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $invoicedetail)));
         if ($request->hasParameter('_save_and_add')) {
             $this->getUser()->setFlash('notice', $notice . ' You can add another one below.');
             $this->redirect('@invoicedetail_new?invoice_id=' . $invoice->getId());
         } else {
             $this->getUser()->setFlash('notice', $notice);
             $this->redirect('invoice/view?id=' . $invoicedetail->getInvoiceId());
         }
     } else {
         $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
     }
 }