KataoProductForm.class.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * KataoProduct form.
  4. *
  5. * @package katao
  6. * @subpackage form
  7. * @author Your name here
  8. * @version SVN: $Id: sfPropelFormTemplate.php 10377 2008-07-21 07:10:32Z dwhittle $
  9. */
  10. class KataoProductForm extends BaseKataoProductForm {
  11. public function configure() {
  12. unset($this->validatorSchema['url_identifier'], $this->validatorSchema['description'], $this->validatorSchema['created_at'], $this->validatorSchema['updated_at']);
  13. unset($this->widgetSchema['url_identifier'], $this->widgetSchema['description'], $this->widgetSchema['created_at'], $this->widgetSchema['updated_at']);
  14. $this->widgetSchema['reference'] = new sfWidgetFormInput(array(), array('style' => 'width: 250px'));
  15. $this->widgetSchema['name'] = new sfWidgetFormInput(array(), array('style' => 'width: 250px'));
  16. $this->widgetSchema['accounting_code_purchase'] = new sfWidgetFormInput(array(), array('style' => 'width: 40px'));
  17. $this->widgetSchema['accounting_code_sell'] = new sfWidgetFormInput(array(), array('style' => 'width: 40px'));
  18. $this->widgetSchema['min_order_number'] = new sfWidgetFormInput(array(), array('style' => 'width: 20px'));
  19. $this->widgetSchema['delivery_delay'] = new sfWidgetFormInput(array(), array('style' => 'width: 20px'));
  20. $this->widgetSchema['max_sol_amount'] = new sfWidgetFormInput(array(), array('style' => 'width: 54px'));
  21. $this->widgetSchema['unit_price_euro'] = new sfWidgetFormInput(array(), array('style' => 'width: 54px'));
  22. $this->widgetSchema['margin'] = new sfWidgetFormInput(array(), array('style' => 'width: 54px'));
  23. $this->widgetSchema['katao_product_family_id'] = new sfWidgetFormSelect(array('choices' => KataoProductFamilyPeer::getAllGroupedByCategory()));
  24. $this->validatorSchema['accounting_code_purchase'] = new sfValidatorPass();
  25. $this->validatorSchema['accounting_code_sell'] = new sfValidatorPass();
  26. $sf_user = sfContext::getInstance()->getUser();
  27. if ($sf_user->isJustReferer()) {
  28. $this->widgetSchema['katao_supplier_id'] = new sfWidgetFormSelect(array('choices' => KataoSupplierPeer::getAllByRefererId($sf_user->getInstance()->getKataoMemberId())));
  29. } elseif ($sf_user->isJustDelegate()) {
  30. $this->widgetSchema['katao_supplier_id'] = new sfWidgetFormSelect(array('choices' => KataoSupplierPeer::getAllLocalToNode($sf_user->getInstance()->getKataoMember()->getKataoNodeId())));
  31. } else {
  32. $this->widgetSchema['katao_supplier_id'] = new sfWidgetFormSelect(array('choices' => KataoSupplierPeer::getAllSimple()));
  33. }
  34. if ($sf_user->isJustSupplier()) {
  35. unset($this->validatorSchema['accounting_code_purchase'], $this->validatorSchema['accounting_code_sell']);
  36. unset($this->widgetSchema['accounting_code_purchase'], $this->widgetSchema['accounting_code_sell']);
  37. }
  38. if ($this->getObject()->isNew()) {
  39. $this->setDefault('margin', 0.15);
  40. }
  41. $this->widgetSchema['tva_rate'] = new sfWidgetFormSelect(array('choices' => array('0' => '0%', '0.055' => '5.5%', '0.2' => '20%')));
  42. $this->widgetSchema['is_archived'] = new wpWidgetFormInputCheckbox();
  43. $this->widgetSchema['is_archived']->setOption('value_attribute_value', 1);
  44. if ((bool)$this->getObject()->getIsArchived()) {
  45. $this->widgetSchema['is_archived']->setAttribute('checked', 'checked');
  46. }
  47. $this->widgetSchema['authorize_decimal'] = new wpWidgetFormInputCheckbox();
  48. $this->widgetSchema['authorize_decimal']->setOption('value_attribute_value', 1);
  49. if ((bool)$this->getObject()->getAuthorizeDecimal()) {
  50. $this->widgetSchema['authorize_decimal']->setAttribute('checked', 'checked');
  51. }
  52. $this->widgetSchema['picture'] = new wpWidgetFormInputFile(array(), array('file_src' => $this->getObject()->getPicture()));
  53. $this->validatorSchema['picture'] = new sfValidatorFile(array('required' => false, 'mime_types' => array('image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png', 'image/gif', 'image/jpeg; charset=binary')), array('mime_types' => 'Merci de choisir une image au format jpg, png ou gif (%mime_type%)'));
  54. $this->widgetSchema->setLabels(array(
  55. 'reference' => 'Référence',
  56. 'name' => 'Nom',
  57. 'katao_product_family_id' => 'Rayon/Famille',
  58. 'katao_supplier_id' => 'Fournisseur',
  59. 'max_sol_amount' => 'Montant maxi',
  60. 'unit_price_euro' => 'Prix d\'achat HT',
  61. 'tva_rate' => 'Taux de TVA',
  62. 'min_order_number' => 'Colisage',
  63. 'margin' => 'Marge',
  64. 'picture' => 'Photo',
  65. 'delivery_delay' => 'Délai de livraison',
  66. 'is_archived' => 'Archivé ?',
  67. 'accounting_code_purchase' => 'Achat',
  68. 'accounting_code_sell' => 'Vente',
  69. 'authorize_decimal' => 'Autoriser quantités avec décimales',
  70. ));
  71. $this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'checkMaxSolAmount')), array('invalid' => 'Le montant de la monnaie complémentaire est trop important.')));
  72. }
  73. public function checkMaxSolAmount($validator, $value) {
  74. $max_sol_amount = (int)sfContext::getInstance()->getRequest()->getParameter('katao_product[max_sol_amount]');
  75. $price = (float)sfContext::getInstance()->getRequest()->getParameter('katao_product[unit_price_euro]');
  76. $tva_rate = (float)sfContext::getInstance()->getRequest()->getParameter('katao_product[tva_rate]');
  77. $margin = (float)sfContext::getInstance()->getRequest()->getParameter('katao_product[margin]') / 100;
  78. if (10 * round($price * (1 + $margin) * (1 + $tva_rate), 2) < $max_sol_amount) {
  79. throw new sfValidatorError($validator, 'invalid');
  80. }
  81. return $value;
  82. }
  83. public function doSave($con = null) {
  84. $file = $this->getValue('picture');
  85. if ($file && is_object($file)) {
  86. if (is_file(sfConfig::get('sf_web_dir') . $this->getObject()->getPicture())) {
  87. unlink(sfConfig::get('sf_web_dir') . $this->getObject()->getPicture());
  88. }
  89. $filename = sha1($file->getOriginalName()) . $file->getExtension($file->getOriginalExtension());
  90. $file->save(sfConfig::get('sf_upload_dir') . '/repository/produits/' . $filename);
  91. } elseif (!empty($_REQUEST['RemovePicture'])) {
  92. if (is_file(sfConfig::get('sf_web_dir') . $this->getObject()->getPicture())) {
  93. unlink(sfConfig::get('sf_web_dir') . $this->getObject()->getPicture());
  94. }
  95. $this->getObject()->setPicture(null);
  96. }
  97. return parent::doSave($con);
  98. }
  99. public function updateObject($values = null) {
  100. $picture = $this->getObject()->getPicture();
  101. $object =/*(KataoProduct)*/ parent::updateObject($values);
  102. $file = $this->getValue('picture');
  103. if ($file && is_object($file)) {
  104. $object->setPicture(str_replace(sfConfig::get('sf_web_dir'), '', $object->getPicture()));
  105. } else {
  106. $object->setPicture($picture);
  107. }
  108. $object->setMargin($this->getValue('margin') / 100);
  109. return $object;
  110. }
  111. }