/**
  * @return void
  * @author Carlos Escribano <*****@*****.**>
  **/
 public function save($con = null)
 {
     parent::save();
     $currency_decimals = sfConfig::get('app_currency_decimals', array());
     foreach ($this->getValues() as $key => $value) {
         switch ($key) {
             case 'company_logo':
                 if ("on" == $this->getValue('company_logo_delete') && is_file($old = sfConfig::get('sf_upload_dir') . DIRECTORY_SEPARATOR . PropertyTable::get('company_logo'))) {
                     @unlink($old);
                     PropertyTable::set('company_logo', null);
                 }
                 if ($value) {
                     $fname = $value->generateFilename();
                     $value->save(sfConfig::get('sf_upload_dir') . DIRECTORY_SEPARATOR . $fname);
                     PropertyTable::set('company_logo', $fname);
                 }
                 break;
             case 'company_logo_delete':
                 break;
             case 'currency':
                 PropertyTable::set('currency_decimals', array_key_exists($value, $currency_decimals) ? $currency_decimals[$value] : 2);
                 PropertyTable::set('currency', $value);
                 break;
             case 'seriess':
             case 'taxes':
                 break;
             default:
                 PropertyTable::set($key, $value);
                 break;
         }
     }
 }
 public function configure()
 {
     $decorator = new myFormSchemaFormatter($this->getWidgetSchema());
     $this->widgetSchema->addFormFormatter('custom', $decorator);
     $this->widgetSchema->setFormFormatterName('custom');
     $common_fields = Doctrine::getTable(self::getModelName())->getColumnNames();
     $model_fields = Doctrine::getTable($this->getModelName())->getColumnNames();
     foreach (array_diff($common_fields, $model_fields) as $extra) {
         unset($this[$extra]);
     }
     $this->widgetSchema['customer_id'] = new sfWidgetFormInputHidden();
     $this->widgetSchema['recurring_invoice_id'] = new sfWidgetFormInputHidden();
     $this->widgetSchema['tags'] = new sfWidgetFormInputHidden();
     $this->widgetSchema['type'] = new sfWidgetFormInputHidden(array(), array('value' => $this->getModelName()));
     $this->widgetSchema['series_id'] = new sfWidgetFormSelect(array('choices' => SeriesTable::getChoicesForSelect()));
     $this->widgetSchema['series_id']->setDefault(sfContext::getInstance()->getUser()->getProfile()->getSeries());
     $this->widgetSchema['invoicing_address'] = new sfWidgetFormTextarea();
     $this->widgetSchema['shipping_address'] = new sfWidgetFormTextarea();
     $this->widgetSchema['terms'] = new sfWidgetFormTextarea();
     $common_defaults = array('customer_name' => 'Client Name', 'customer_identification' => 'Client Legal Id', 'contact_person' => 'Contact Person', 'invoicing_address' => 'Invoicing Address', 'shipping_address' => 'Shipping Address', 'customer_email' => 'Client Email Address');
     $this->widgetSchema->setHelps(array_merge($this->widgetSchema->getHelps(), $common_defaults));
     $this->setDefault('tags', implode(',', $this->object->getTags()));
     $this->setDefault('terms', PropertyTable::get('legal_terms'));
     // validators
     $this->validatorSchema['tags'] = new sfValidatorString(array('required' => false));
     $this->validatorSchema['customer_email'] = new sfValidatorEmail(array('max_length' => 100, 'required' => false), array('invalid' => 'Invalid client email address'));
     $this->validatorSchema['customer_name'] = new sfValidatorString(array('required' => true));
     $this->validatorSchema['series_id'] = new sfValidatorString(array('required' => true), array('required' => 'The invoice serie is mandatory'));
     $iforms = array();
     foreach ($this->object->getItems() as $item) {
         $iforms[$item->id] = new ItemForm($item);
     }
     $itemForms = new FormsContainer($iforms, 'ItemForm');
     $this->embedForm('Items', $itemForms);
 }
 /**
  * Updates a Customer object matching the object's data.
  *
  * @return void
  * @author Carlos Escribano <*****@*****.**>
  **/
 public function updateCustomer($obj)
 {
     $customer = $this->getCustomerMatch($obj);
     if ($customer->isNew() || !in_array('customers', PropertyTable::get('siwapp_modules', array()))) {
         $customer->setDataFrom($obj);
     }
     $obj->setCustomer($customer);
     $customer->save();
 }
Exemple #4
0
 public function setAmounts()
 {
     $this->setBaseAmount($this->calculate('base_amount'));
     $this->setDiscountAmount($this->calculate('discount_amount'));
     $this->setNetAmount($this->getBaseAmount() - $this->getDiscountAmount());
     $this->setTaxAmount($this->calculate('tax_amount'));
     $rounded_gross = round($this->getNetAmount() + $this->getTaxAmount(), PropertyTable::get('currency_decimals', 2));
     $this->setGrossAmount($rounded_gross);
     return $this;
 }
 public function configure()
 {
     $optional_mods = sfConfig::get('app_modules_optional');
     foreach (sfConfig::get('app_modules_optional') as $mod => $opts) {
         $choices[$mod] = $opts['verbose_name'];
     }
     $this->setWidgets(array('siwapp_modules' => new sfWidgetFormSelectCheckbox(array('choices' => $choices), array('class' => 'siwapp_modules'))));
     $this->widgetSchema->setLabels(array('siwapp_modules' => 'Siwapp Modules'));
     $this->setDefaults(array('siwapp_modules' => PropertyTable::get('siwapp_modules')));
     $this->widgetSchema->setNameFormat('config[%s]');
     $this->setValidators(array('siwapp_modules' => new sfValidatorPass()));
 }
 public function loadUserSettings()
 {
     $currency = PropertyTable::get('currency', 'USD');
     $currency_decimals = PropertyTable::get('currency_decimals', 2);
     $siwapp_mandatory_modules = array_keys(sfConfig::get('app_modules_mandatory'));
     $siwapp_optional_modules = PropertyTable::get('siwapp_modules', array());
     $this->setAttribute('currency', $currency);
     $this->setAttribute('currency_decimals', $currency_decimals);
     $this->setAttribute('siwapp_modules', array_merge($siwapp_mandatory_modules, $siwapp_optional_modules));
     $culture = $this->getLanguage();
     if ($this->getCountry()) {
         $culture .= '_' . $this->getCountry();
     }
     $this->setCulture($culture);
 }
 public function __construct($invoice)
 {
     parent::__construct();
     $this->setTo($invoice->customer_email, $invoice->customer_name);
     // To get all the properties loaded for the template
     foreach ($invoice->Items as $it) {
         $it->refreshRelated();
     }
     $data[] = $invoice;
     $model = get_class($invoice);
     $printer = new Printer($model, TemplateTable::getTemplateForModel($model)->getId());
     try {
         $body = $printer->render($data);
         $pdf = $printer->renderPdf($data)->output();
         $attachment = new Swift_Attachment($pdf, $model . '-' . $invoice->getId() . '.pdf', 'application/pdf');
         $this->setSubject(PropertyTable::get('company_name') . ' [' . $model . ': ' . $invoice . ']')->setBody($printer->render($data), 'text/html')->attach($attachment);
         $this->setReadyState(true);
     } catch (LogicException $e) {
         $this->setReadyState(false);
     }
 }
 /**
  * Receives the Invoice form data and calculates each row total amount and
  * full invoice totals. Returns a json
  *
  * @param 'invoice' from Request
  * @return JSON through Response
  */
 public function executeCalculate(sfWebRequest $request)
 {
     $currency = PropertyTable::get('currency');
     $format = new sfNumberFormat($this->culture);
     $data = $request->getParameter('invoice');
     $this->getResponse()->setHttpHeader('Content-Type', 'application/json; charset=utf-8');
     $invoice = new Invoice();
     $items = array();
     $totals = array();
     if (isset($data['Items'])) {
         foreach ((array) $data['Items'] as $itemId => $itemData) {
             if ($itemData['remove']) {
                 continue;
             }
             $item = new Item();
             $item->setUnitaryCost($itemData['unitary_cost']);
             $item->setQuantity($itemData['quantity']);
             $item->setDiscount($itemData['discount']);
             if (isset($itemData['taxes_list'])) {
                 $taxes = Doctrine::getTable('Tax')->createQuery()->whereIn('id', $itemData['taxes_list'])->execute();
                 $item->Taxes = $taxes;
             }
             $items[$itemId] = $format->format($item->getGrossAmount(), 'c', $currency);
             $invoice->Items[] = $item;
         }
         $totals['base'] = $format->format($invoice->calculate('base_amount', true), 'c', $currency);
         $totals['discount'] = $format->format($invoice->calculate('discount_amount', true), 'c', $currency);
         $totals['net'] = $format->format($invoice->calculate('net_amount', true), 'c', $currency);
         $totals['taxes'] = $format->format($invoice->calculate('tax_amount', true), 'c', $currency);
         $totals['gross'] = $format->format($invoice->calculate('gross_amount', true), 'c', $currency);
     } else {
         $zero = $format->format(0, 'c', $currency);
         $totals['base'] = $zero;
         $totals['discount'] = $zero;
         $totals['net'] = $zero;
         $totals['taxes'] = $zero;
         $totals['gross'] = $zero;
     }
     return $this->renderText(json_encode(array('items' => $items, 'totals' => $totals)));
 }
Exemple #9
0
 public static function getDecimals()
 {
     return PropertyTable::get('currency_decimals', 2);
 }
Exemple #10
0
 /**
  * this checks if recalculation of totals and status is needed
  * of the opened invoices
  *
  * @return void
  **/
 private function checkIfUpdateTotals()
 {
     // if the property is not set, we set it here
     if (!PropertyTable::get('last_calculation_date')) {
         PropertyTable::set('last_calculation_date', '1970-01-01');
     }
     $last = new sfDate(PropertyTable::get('last_calculation_date'));
     $today = new sfDate();
     if ($today->diff($last, sfTime::DAY) > 0) {
         CommonTable::calculateTotals();
         PropertyTable::set('last_calculation_date', $today->format('Y-m-d'));
     }
 }
Exemple #11
0
 public function setAmount($v)
 {
     $v = round($v, PropertyTable::get('currency_decimals', 2));
     return $this->_set('amount', $v);
 }