actions.class.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. <?php
  2. /**
  3. * kataoSupplierInvoice actions.
  4. *
  5. * @package www.katao.fr
  6. * @subpackage kataoSupplierInvoice
  7. * @author Your name here
  8. * @version SVN: $Id: actions.class.php 9301 2008-05-27 01:08:46Z dwhittle $
  9. */
  10. class kataoSupplierInvoiceActions extends wpActions {
  11. public function executeIndex() {
  12. $criteria = new Criteria();
  13. $criteria->addJoin(KataoSupplierInvoicePeer::KATAO_PERIOD_ID, KataoPeriodPeer::ID);
  14. $sort_method = $this->getCriteriaSortMethod();
  15. switch ($this->sort = $this->getRequestParameter('sort', 'date')) {
  16. case 'reference':
  17. $criteria->$sort_method(KataoSupplierInvoicePeer::REFERENCE);
  18. break;
  19. case 'status':
  20. $criteria->$sort_method(KataoSupplierInvoicePeer::STATUS);
  21. break;
  22. case 'supplier':
  23. $criteria->$sort_method(KataoSupplierInvoicePeer::SUPPLIER_NAME);
  24. break;
  25. case 'total':
  26. $criteria->$sort_method(KataoSupplierInvoicePeer::TOTAL_TVA);
  27. break;
  28. case 'period':
  29. $criteria->$sort_method(KataoPeriodPeer::NAME);
  30. break;
  31. default:
  32. $criteria->$sort_method(KataoSupplierInvoicePeer::INVOICE_DATE);
  33. }
  34. if ('' != $this->filter_status = $this->getUser()->getAttribute('katao_supplier_invoice_filter_status')) {
  35. $criteria->add(KataoSupplierInvoicePeer::STATUS, $this->filter_status);
  36. }
  37. if ('' != $this->filter_supplier = $this->getUser()->getAttribute('katao_supplier_invoice_filter_supplier')) {
  38. $criteria->add(KataoSupplierInvoicePeer::KATAO_SUPPLIER_ID, $this->filter_supplier);
  39. }
  40. if ('' != $this->filter_period = $this->getUser()->getAttribute('katao_supplier_invoice_filter_period', KataoPeriodPeer::getActivePeriod()->getId())) {
  41. $criteria->add(KataoSupplierInvoicePeer::KATAO_PERIOD_ID, $this->filter_period);
  42. }
  43. $pager = new sfPropelPager('KataoSupplierInvoice', sfConfig::get('app_pager'));
  44. $pager->setCriteria($criteria);
  45. $pager->setPage($this->getRequestParameter('page', 1));
  46. $pager->init();
  47. $this->pager = $pager;
  48. $this->katao_supplier_invoiceList = $pager->getResults();
  49. }
  50. public function executeFilter($request) {
  51. $this->getUser()->setAttribute('katao_supplier_invoice_filter_status', $request->getParameter('filter_status'));
  52. $this->getUser()->setAttribute('katao_supplier_invoice_filter_supplier', $request->getParameter('filter_supplier'));
  53. $this->getUser()->setAttribute('katao_supplier_invoice_filter_period', $request->getParameter('filter_period'));
  54. return $this->redirect('kataoSupplierInvoice/index');
  55. }
  56. public function executeFilterReset($request) {
  57. $this->getUser()->setAttribute('katao_supplier_invoice_filter_status', '');
  58. $this->getUser()->setAttribute('katao_supplier_invoice_filter_supplier', '');
  59. $this->getUser()->setAttribute('katao_supplier_invoice_filter_period', KataoPeriodPeer::getActivePeriod()->getId());
  60. return $this->redirect('kataoSupplierInvoice/index');
  61. }
  62. public function executeCreate($request) {
  63. $this->katao_supplier_invoice = new KataoSupplierInvoice();
  64. $this->katao_supplier_invoice_orders = array();
  65. $this->setTemplate('edit');
  66. }
  67. public function executeEdit($request) {
  68. $this->katao_supplier_invoice = KataoSupplierInvoicePeer::retrieveByPk($request->getParameter('id'));
  69. $this->redirectWithErrorUnless($this->katao_supplier_invoice, 'Facture #' . $request->getParameter('id') . ' inconnue.');
  70. }
  71. public function executeUpdate($request) {
  72. $this->katao_supplier_invoice = KataoSupplierInvoicePeer::retrieveByPk($request->getParameter('id'));
  73. if (is_null($this->katao_supplier_invoice)) {
  74. $this->katao_supplier_invoice = new KataoSupplierInvoice();
  75. }
  76. try {
  77. $con = Propel::getConnection();
  78. $con->begin();
  79. $this->katao_supplier_invoice->setKataoPeriodId($request->getParameter('period'));
  80. $this->katao_supplier_invoice->setKataoSupplierId($request->getParameter('supplier'));
  81. $this->katao_supplier_invoice->setReference($request->getParameter('reference'));
  82. list($d, $m, $y) = sfContext::getInstance()->getI18N()->getDateForCulture($request->getParameter('invoice_date'), 'fr');
  83. $this->katao_supplier_invoice->setInvoiceDate("$y-$m-$d");
  84. list($d, $m, $y) = sfContext::getInstance()->getI18N()->getDateForCulture($request->getParameter('due_date'), 'fr');
  85. $this->katao_supplier_invoice->setDueDate("$y-$m-$d");
  86. $this->katao_supplier_invoice->setComment($request->getParameter('comment'));
  87. if ($this->katao_supplier_invoice->isNew()) {
  88. // save supplier/katao data if new invoice
  89. $katao_supplier =/*(KataoSupplier)*/ $this->katao_supplier_invoice->getKataoSupplier();
  90. $this->katao_supplier_invoice->setSupplierName($katao_supplier->getName());
  91. $this->katao_supplier_invoice->setSupplierEmail($katao_supplier->getEmail());
  92. $this->katao_supplier_invoice->setSupplierAddress1($katao_supplier->getAddress1());
  93. $this->katao_supplier_invoice->setSupplierAddress2($katao_supplier->getAddress2());
  94. $this->katao_supplier_invoice->setSupplierZip($katao_supplier->getZip());
  95. $this->katao_supplier_invoice->setSupplierCity($katao_supplier->getCity());
  96. $this->katao_supplier_invoice->setKataoName(wpConfig::getName());
  97. $this->katao_supplier_invoice->setKataoAddress(wpConfig::getAddress());
  98. $this->katao_supplier_invoice->setKataoSiretNumber(wpConfig::getSiretNumber());
  99. $this->katao_supplier_invoice->setKataoRcsNumber(wpConfig::getRCSNumber());
  100. $this->katao_supplier_invoice->setKataoCapital(wpConfig::getCapital());
  101. }
  102. $tva_rates = array('0.2' => 1, '0.055' => 2, '0' => 0);
  103. $without_taxes_by_tva_rate = $request->getParameter('without_taxes_by_tva_rate[]');
  104. $discount_by_tva_rate = $request->getParameter('discount_by_tva_rate[]');
  105. $total_by_tva_rate = $request->getParameter('total_by_tva_rate[]');
  106. foreach ($tva_rates as $tva_rate_value => $tva_rate_const) {
  107. $setTauxBasesHt = sprintf('setTaux%dBasesHt', $tva_rate_const);
  108. $this->katao_supplier_invoice->$setTauxBasesHt(!empty($without_taxes_by_tva_rate[$tva_rate_const])?$without_taxes_by_tva_rate[$tva_rate_const]:0);
  109. $setTauxDiscountHt = sprintf('setTaux%dDiscountHt', $tva_rate_const);
  110. $this->katao_supplier_invoice->$setTauxDiscountHt(!empty($discount_by_tva_rate[$tva_rate_const])?$discount_by_tva_rate[$tva_rate_const]:0);
  111. $setTauxAmount = sprintf('setTaux%dAmount', $tva_rate_const);
  112. $this->katao_supplier_invoice->$setTauxAmount(!empty($total_by_tva_rate[$tva_rate_const])?$total_by_tva_rate[$tva_rate_const]:0);
  113. }
  114. $this->katao_supplier_invoice->setTotalHt($request->getParameter('without_taxes_total'));
  115. $this->katao_supplier_invoice->setTotalTva($request->getParameter('tva_total'));
  116. $this->katao_supplier_invoice->setFeesShippingHt($request->getParameter('fees_shipping_ht'));
  117. $this->katao_supplier_invoice->setFeesShippingRate($request->getParameter('fees_shipping_rate'));
  118. $this->katao_supplier_invoice->setFeesShippingTva($request->getParameter('fees_shipping_tva'));
  119. $this->katao_supplier_invoice->setFeesBillingHt($request->getParameter('fees_billing_ht'));
  120. $this->katao_supplier_invoice->setFeesBillingRate($request->getParameter('fees_billing_rate'));
  121. $this->katao_supplier_invoice->setFeesBillingTva($request->getParameter('fees_billing_tva'));
  122. $this->katao_supplier_invoice->save($con);
  123. if (!$this->katao_supplier_invoice->isNew()) {
  124. // delete existing relations between invoice and orders
  125. $criteria = new Criteria();
  126. $criteria->add(KataoSupplierInvoiceOrderPeer::KATAO_SUPPLIER_INVOICE_ID, $this->katao_supplier_invoice->getId());
  127. KataoSupplierInvoiceOrderPeer::doDelete($criteria, $con);
  128. // delete existing relations between invoice and products
  129. $criteria = new Criteria();
  130. $criteria->add(KataoSupplierInvoiceProductPeer::KATAO_SUPPLIER_INVOICE_ID, $this->katao_supplier_invoice->getId());
  131. KataoSupplierInvoiceProductPeer::doDelete($criteria, $con);
  132. }
  133. // save new relations between invoice and orders
  134. $orders = $request->getParameter('orders[]');
  135. if ($orders && is_array($orders) && 0 < count($orders)) {
  136. foreach ($orders as $katao_order_id) {
  137. $katao_supplier_invoice_order = new KataoSupplierInvoiceOrder();
  138. $katao_supplier_invoice_order->setKataoSupplierInvoice($this->katao_supplier_invoice);
  139. $katao_supplier_invoice_order->setKataoOrderId($katao_order_id);
  140. $katao_supplier_invoice_order->save($con);
  141. }
  142. }
  143. // save new relations between invoice and products
  144. // and update product price
  145. $product_quantity = $request->getParameter('product_quantity[]');
  146. $product_price_unit = $request->getParameter('product_price_unit[]');
  147. $product_price_total = $request->getParameter('product_price_total[]');
  148. foreach ($product_quantity as $katao_product_id => $quantity) {
  149. if (null != $katao_product = KataoProductPeer::retrieveByPK($katao_product_id, $con)) {
  150. $katao_supplier_invoice_product = new KataoSupplierInvoiceProduct();
  151. $katao_supplier_invoice_product->setKataoSupplierInvoice($this->katao_supplier_invoice);
  152. $katao_supplier_invoice_product->setKataoProductId($katao_product_id);
  153. $katao_supplier_invoice_product->setProductReference($katao_product->getReference());
  154. $katao_supplier_invoice_product->setProductName($katao_product->getName());
  155. $katao_supplier_invoice_product->setProductPriceEuro(!empty($product_price_unit[$katao_product_id])?$product_price_unit[$katao_product_id]:$katao_product->getUnitPriceEuro());
  156. $katao_supplier_invoice_product->setProductMargin($katao_product->getMargin());
  157. $katao_supplier_invoice_product->setProductTvaRate($katao_product->getTvaRate());
  158. $katao_supplier_invoice_product->setQuantity($quantity);
  159. $katao_supplier_invoice_product->setProductPriceTotal(!empty($product_price_total[$katao_product_id])?$product_price_total[$katao_product_id]:($quantity * $katao_product->getUnitPriceEuro() * (1 + $katao_product->getMargin()) * (1 + $katao_product->getTvaRate())));
  160. $katao_supplier_invoice_product->save($con);
  161. $katao_product->setUnitPriceEuro($katao_supplier_invoice_product->getProductPriceEuro());
  162. $katao_product->save($con);
  163. }
  164. }
  165. $con->commit();
  166. }
  167. catch (Exception $e) {
  168. $con->rollback();
  169. }
  170. wpFlashMessages::addConfirmation('Facture "' . $this->katao_supplier_invoice->getReference() . '" sauvée avec succès.');
  171. $this->redirect('kataoSupplierInvoice/index');
  172. }
  173. public function executePay($request) {
  174. $this->redirectWithErrorUnless($this->katao_supplier_invoice = KataoSupplierInvoicePeer::retrieveByPk($request->getParameter('id')), 'Facture #' . $request->getParameter('id') . ' inconnue.');
  175. $this->redirectWithErrorIf(KataoSupplierInvoice::STATUS_RECEIVED != $this->katao_supplier_invoice->getStatus());
  176. if ($request->isMethod('post')) {
  177. $this->katao_supplier_invoice->setStatus(KataoSupplierInvoice::STATUS_PAID);
  178. $this->katao_supplier_invoice->setPaymentAmountEuro($request->getParameter('payment_amount_euro'));
  179. $this->katao_supplier_invoice->setPaymentAmountSol($request->getParameter('payment_amount_sol'));
  180. list($d, $m, $y) = sfContext::getInstance()->getI18N()->getDateForCulture($request->getParameter('payment_date'), 'fr');
  181. $this->katao_supplier_invoice->setPaymentDate("$y-$m-$d");
  182. $this->katao_supplier_invoice->setPaymentMode($request->getParameter('payment_mode'));
  183. if (KataoSupplierInvoice::PAYMENT_MODE_CREDIT_CARD != $this->katao_supplier_invoice->getPaymentMode()) {
  184. $this->katao_supplier_invoice->setPaymentDetails($request->getParameter('payment_details'));
  185. }
  186. $this->katao_supplier_invoice->save();
  187. wpFlashMessages::addConfirmation('Facture fournisseur "' . $this->katao_supplier_invoice->getReference() . '" payée avec succès.');
  188. $this->redirect('kataoSupplierInvoice/index');
  189. }
  190. // $this->katao_supplier_invoice->initPaymentAmounts();
  191. }
  192. public function executeExportPdf($request) {
  193. $this->redirectWithErrorUnless($katao_supplier_invoice = KataoSupplierInvoicePeer::retrieveByPk($request->getParameter('id')), 'Facture #' . $request->getParameter('id') . ' inconnue.');
  194. $content = $katao_supplier_invoice->generatePDF();
  195. $response = $this->getResponse();
  196. $response->setContentType('application/pdf');
  197. $response->setHttpHeader('Content-disposition', 'attachment; filename="' . $katao_supplier_invoice->getReference() . '.pdf"');
  198. $response->setHttpHeader('Content-Length', strlen($content));
  199. $response->setHttpHeader('Pragma', 'public');
  200. $response->setHttpHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
  201. $response->setHttpHeader('Expires', '0');
  202. $response->setContent($content);
  203. return sfView::NONE;
  204. }
  205. public function executeExportPDFAll($request) {
  206. $criteria = new Criteria();
  207. $criteria->addAscendingOrderByColumn(KataoSupplierInvoicePeer::ID);
  208. return $this->exportSupplierInvoicesToPDF(KataoSupplierInvoicePeer::doSelect($criteria));
  209. }
  210. public function executeExportPDFSelected($request) {
  211. if (!wpPersistenceManager::isEmpty('katao_supplier_invoice')) {
  212. $criteria = new Criteria();
  213. $criteria->add(KataoSupplierInvoicePeer::ID, array_keys(wpPersistenceManager::getSelectedItems('katao_supplier_invoice')), Criteria::IN);
  214. $criteria->addAscendingOrderByColumn(KataoSupplierInvoicePeer::ID);
  215. return $this->exportSupplierInvoicesToPDF(KataoSupplierInvoicePeer::doSelect($criteria));
  216. } else {
  217. wpFlashMessages::addWarning('Merci de sélectionner au moins une facture.');
  218. $this->redirect('kataoSupplierInvoice/index');
  219. }
  220. }
  221. protected function exportSupplierInvoicesToPDF($katao_supplier_invoices) {
  222. $file = tempnam('tmp', 'zip');
  223. $archive = new ZipArchive();
  224. $archive->open($file, ZipArchive::OVERWRITE);
  225. foreach ($katao_supplier_invoices as/*(KataoSupplierInvoice)*/ $katao_supplier_invoice) {
  226. $archive->addFromString($katao_supplier_invoice->getReference() . '.pdf', $katao_supplier_invoice->generatePDF());
  227. }
  228. $archive->close();
  229. $response = $this->getResponse();
  230. $response->setContentType('application/zip');
  231. $response->setHttpHeader('Content-disposition', 'attachment; filename="KATAO_Factures_Fournisseurs_' . date('Y-m-d') . '.zip"');
  232. $response->setHttpHeader('Content-Length', filesize($file));
  233. $response->setHttpHeader('Pragma', 'public');
  234. $response->setHttpHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
  235. $response->setHttpHeader('Expires', '0');
  236. $response->setContent(file_get_contents($file));
  237. return sfView::NONE;
  238. }
  239. public function executeDelete($request) {
  240. $this->redirectWithErrorUnless($katao_supplier_invoice = KataoSupplierInvoicePeer::retrieveByPk($request->getParameter('id')), 'Facture #' . $request->getParameter('id') . ' inconnue.');
  241. $katao_supplier_invoice->delete();
  242. wpFlashMessages::addConfirmation('Facture "' . $katao_supplier_invoice->getReference() . '" supprimée avec succès.');
  243. $this->redirect('kataoSupplierInvoice/index');
  244. }
  245. public function executeDeleteMultiple($request) {
  246. if (!wpPersistenceManager::isEmpty('katao_supplier_invoice')) {
  247. $criteria = new Criteria();
  248. $criteria->add(KataoSupplierInvoicePeer::ID, array_keys(wpPersistenceManager::getSelectedItems('katao_supplier_invoice')), Criteria::IN);
  249. KataoSupplierInvoicePeer::doDelete($criteria);
  250. wpPersistenceManager::cleanSelection('katao_supplier_invoice');
  251. wpFlashMessages::addConfirmation('Les factures sélectionnées ont été supprimées avec succès.');
  252. } else {
  253. wpFlashMessages::addWarning('Merci de sélectionner au moins une facture.');
  254. }
  255. $this->redirect('kataoSupplierInvoice/index');
  256. }
  257. public function executeUpdateOrdersList($request) {
  258. $katao_supplier_invoice_orders = KataoSupplierInvoiceOrderPeer::getAllByKataoSupplierInvoiceId($request->getParameter('id'));
  259. $return = '';
  260. $katao_orders = KataoOrderPeer::getPossibleOrdersForSupplierInvoice($request->getParameter('period'), $request->getParameter('supplier'));
  261. if (0 < count($katao_orders)) {
  262. foreach ($katao_orders as/*(KataoOrder)*/ $katao_order) {
  263. $return .= sprintf('
  264. <div>
  265. <input type="checkbox" name="orders[]" value="%d" id="orders_%d" class="supplier-invoice-order" onclick="updateProductsList();"%s />
  266. <label for="orders_%d">%s <i>pour %s</i></label>
  267. </div>', $katao_order->getId(), $katao_order->getId(), !empty($katao_supplier_invoice_orders[$katao_order->getId()])?' checked="checked"':'', $katao_order->getId(), $katao_order->getNumber(), $katao_order->getKataoNode()->getCity());
  268. }
  269. } else {
  270. $return = '<i>Merci de choisir une période et un fournisseur</i>';
  271. }
  272. return $this->renderText($return);
  273. }
  274. public function executeUpdateProductsList($request) {
  275. $return = '<br /><i>Merci de sélectionner des commandes</i>';
  276. $tva_rates = array('0.2' => 1, '0.055' => 2, '0' => 0);
  277. $total_by_tva_rate = array('0.2' => 0, '0.055' => 0, '0' => 0);
  278. $discount_by_tva_rate = $total_by_tva_rate;
  279. $products = array();
  280. if (null != $katao_supplier_invoice = KataoSupplierInvoicePeer::retrieveByPK($request->getParameter('id'))) {
  281. foreach ($katao_supplier_invoice->getKataoSupplierInvoiceProducts() as/*(KataoSupplierInvoiceProduct)*/ $katao_supplier_invoice_product) {
  282. $quantity = $katao_supplier_invoice_product->getQuantity();
  283. $price = $katao_supplier_invoice_product->getProductPriceEuro();
  284. $sub_total = $katao_supplier_invoice_product->getProductPriceTotal();
  285. $total_by_tva_rate[(string)$katao_supplier_invoice_product->getProductTvaRate()] += $quantity * $price;
  286. $products[$katao_supplier_invoice_product->getKataoProductId()]['name'] = $katao_supplier_invoice_product->getProductName();
  287. $products[$katao_supplier_invoice_product->getKataoProductId()]['price'] = $price;
  288. $products[$katao_supplier_invoice_product->getKataoProductId()]['tva'] = $katao_supplier_invoice_product->getProductTvaRate();
  289. $products[$katao_supplier_invoice_product->getKataoProductId()]['quantity'] = $quantity;
  290. $products[$katao_supplier_invoice_product->getKataoProductId()]['sub_total'] = $sub_total;
  291. }
  292. $fees_shipping_ht = $katao_supplier_invoice->getFeesShippingHt();
  293. $fees_shipping_rate = $katao_supplier_invoice->getFeesShippingRate();
  294. $fees_shipping_tva = $katao_supplier_invoice->getFeesShippingTva();
  295. $fees_billing_ht = $katao_supplier_invoice->getFeesBillingHt();
  296. $fees_billing_rate = $katao_supplier_invoice->getFeesBillingRate();
  297. $fees_billing_tva = $katao_supplier_invoice->getFeesBillingTva();
  298. } else {
  299. if ('' != $orders = $request->getParameter('orders')) {
  300. $order_ids = explode(',', $orders);
  301. if (0 < count($order_ids)) {
  302. foreach (KataoOrderPeer::retrieveByPKs($order_ids) as/*(KataoOrder)*/ $katao_order) {
  303. foreach ($katao_order->getKataoOrderProducts() as/*(KataoOrderProduct)*/ $katao_order_product) {
  304. $quantity = $katao_order_product->getHasBeenDelivered()?$katao_order_product->getQuantityDelivered():$katao_order_product->getQuantity();
  305. $price = $katao_order_product->getProductPriceEuro();
  306. $sub_total = $quantity * $price;
  307. $total_by_tva_rate[(string)$katao_order_product->getProductTvaRate()] += $quantity * $katao_order_product->getProductPriceEuro();
  308. $products[$katao_order_product->getKataoProductId()]['name'] = $katao_order_product->getProductName();
  309. $products[$katao_order_product->getKataoProductId()]['price'] = $price;
  310. $products[$katao_order_product->getKataoProductId()]['tva'] = $katao_order_product->getProductTvaRate();
  311. if (!isset($products[$katao_order_product->getKataoProductId()]['quantity'])) {
  312. $products[$katao_order_product->getKataoProductId()]['quantity'] = 0;
  313. }
  314. $products[$katao_order_product->getKataoProductId()]['quantity'] += $quantity;
  315. if (!isset($products[$katao_order_product->getKataoProductId()]['sub_total'])) {
  316. $products[$katao_order_product->getKataoProductId()]['sub_total'] = 0;
  317. }
  318. $products[$katao_order_product->getKataoProductId()]['sub_total'] += $sub_total;
  319. }
  320. }
  321. }
  322. }
  323. $fees_shipping_ht = 0;
  324. $fees_shipping_rate = '0';
  325. $fees_shipping_tva = 0;
  326. $fees_billing_ht = 0;
  327. $fees_billing_rate = '0';
  328. $fees_billing_tva = 0;
  329. }
  330. $total = 0;
  331. if (0 < count($products)) {
  332. $return = '
  333. <table class="admin-sub-list">
  334. <tr>
  335. <th>Désignation</th>
  336. <th nowrap="nowrap" width="1%" style="text-align:center!important">Quantité</th>
  337. <th nowrap="nowrap" width="1%" style="text-align:right!important">P.U. HT</th>
  338. <th nowrap="nowrap" width="1%" style="text-align:right!important">Montant HT</th>
  339. <th nowrap="nowrap" width="1%" style="text-align:center!important">TVA</th>
  340. </tr>';
  341. foreach ($products as $product_id => $product_data) {
  342. $return .= '
  343. <tr>
  344. <td>' . $product_data['name'] . '</td>
  345. <td nowrap="nowrap" style="text-align:center!important"><input type="text" name="product_quantity[' . $product_id . ']" value="' . $product_data['quantity'] . '" id="product_quantity_' . $product_id . '" class="product-quantity-' . $tva_rates[(string)$product_data['tva']] . '" onchange="updateProductTotalPrice(' . $product_id . ');" style="width: 45px; text-align: center" /></td>
  346. <td nowrap="nowrap" style="text-align:right!important"><input type="text" name="product_price_unit[' . $product_id . ']" value="' . round($product_data['price'], 2) . '" id="product_price_unit_' . $product_id . '" class="product-price-unit-' . $tva_rates[(string)$product_data['tva']] . '" onchange="updateProductTotalPrice(' . $product_id . ');" style="width: 45px; text-align: right" />&nbsp;€</td>
  347. <td nowrap="nowrap" style="text-align:right!important"><input type="text" name="product_price_total[' . $product_id . ']" value="' . round($product_data['sub_total'], 2) . '" id="product_price_total_' . $product_id . '" class="product-price-total-' . $tva_rates[(string)$product_data['tva']] . '" onchange="updateTotalPrices();" style="width: 60px; text-align: right" />&nbsp;€</td>
  348. <td nowrap="nowrap" style="text-align:right!important">' . (100 * $product_data['tva']) . '%</td>
  349. </tr>';
  350. }
  351. $return .= '
  352. </table>
  353. <table border="0" cellpadding="0" cellspacing="0">
  354. <tr valign="top">
  355. <td>
  356. <u>TVA (montant, remises) :</u>
  357. <table class="admin-sub-list" style="width:1%!important">
  358. <tr>
  359. <th nowrap="nowrap" width="1%">Taux</th>
  360. <th nowrap="nowrap" width="1%" style="text-align:right!important">Bases HT</th>
  361. <th nowrap="nowrap" width="1%" style="text-align:right!important">Remises HT</th>
  362. <th nowrap="nowrap" width="1%" style="text-align:right!important">Montant</th>
  363. </tr>';
  364. $with_taxes_total = 0;
  365. $without_taxes_total = 0;
  366. $tva_total = 0;
  367. foreach ($total_by_tva_rate as $tva_rate => $tva_rate_total) {
  368. $tva_rate_discount = !empty($discount_by_tva_rate[$tva_rate])?$discount_by_tva_rate[$tva_rate]:0;
  369. if (!is_null($katao_supplier_invoice)) {
  370. $getTauxBasesHt = sprintf('getTaux%dBasesHt', $tva_rates[(string)$tva_rate]);
  371. $tva_rate_total = $katao_supplier_invoice->$getTauxBasesHt();
  372. $getTauxDiscountHt = sprintf('getTaux%dDiscountHt', $tva_rates[(string)$tva_rate]);
  373. $tva_rate_discount = $katao_supplier_invoice->$getTauxDiscountHt();
  374. $getTauxAmount = sprintf('getTaux%dAmount', $tva_rates[(string)$tva_rate]);
  375. $total_by_tva_rate2 = $katao_supplier_invoice->$getTauxAmount();
  376. } else {
  377. $total_by_tva_rate2 = round($tva_rate_total * $tva_rate, 2);
  378. }
  379. $without_taxes_total += $tva_rate_total;
  380. $tva_total += round(($tva_rate_total - $tva_rate_discount) * $tva_rate, 2);
  381. $return .= '
  382. <tr>
  383. <td nowrap="nowrap" style="text-align:right!important">' . (100 * $tva_rate) . '%</td>
  384. <td nowrap="nowrap" style="text-align:right!important"><input type="text" name="without_taxes_by_tva_rate[' . $tva_rates[(string)$tva_rate] . ']" value="' . $tva_rate_total . '" id="without_taxes_by_tva_rate_' . $tva_rates[(string)$tva_rate] . '" onchange="updateTotalPricesTTC();" style="width: 45px; text-align: right" />&nbsp;€</td>
  385. <td nowrap="nowrap" style="text-align:right!important"><input type="text" name="discount_by_tva_rate[' . $tva_rates[(string)$tva_rate] . ']" value="' . $tva_rate_discount . '" id="discount_by_tva_rate_' . $tva_rates[(string)$tva_rate] . '" onchange="updateTotalPricesTTC();" style="width: 45px; text-align: right" />&nbsp;€</td>
  386. <td nowrap="nowrap" style="text-align:right!important"><input type="text" name="total_by_tva_rate[' . $tva_rates[(string)$tva_rate] . ']" value="' . $total_by_tva_rate2 . '" id="total_by_tva_rate_' . $tva_rates[(string)$tva_rate] . '" onchange="updateTotalTVA();" style="width: 45px; text-align: right" />&nbsp;€</td>
  387. </tr>';
  388. }
  389. if (!is_null($katao_supplier_invoice)) {
  390. $without_taxes_total = $katao_supplier_invoice->getTotalHt();
  391. $tva_total = $katao_supplier_invoice->getTotalTva();
  392. }
  393. $return .= '
  394. </table>
  395. </td>
  396. <td>
  397. <u>Frais divers :</u>
  398. <table class="admin-sub-list" style="width:1%!important">
  399. <tr>
  400. <th nowrap="nowrap" width="1%">Frais</th>
  401. <th nowrap="nowrap" width="1%" style="text-align:right!important">HT</th>
  402. <th nowrap="nowrap" width="1%" style="text-align:right!important">Taux</th>
  403. <th nowrap="nowrap" width="1%" style="text-align:right!important">TVA</th>
  404. </tr>
  405. <tr>
  406. <td>Port</td>
  407. <td nowrap="nowrap" style="text-align:right!important"><input type="text" name="fees_shipping_ht" id="fees_shipping_ht" value="' . $fees_shipping_ht . '" onchange="updateFeesTVA();" style="width: 45px; text-align: right" />&nbsp;€</td>
  408. <td nowrap="nowrap" style="text-align:right!important"><select name="fees_shipping_rate" id="fees_shipping_rate" onchange="updateFeesTVA();">';
  409. foreach (array('0' => '0%', '0.055' => '5.5%', '0.2' => '20%') as $value => $label) {
  410. $return .= '<option value="' . $value . '"' . sprintf('%s', ($value == $fees_shipping_rate)?' selected="selected"':'') . '>' . $label . '</option>';
  411. }
  412. $return .= '</select></td>
  413. <td nowrap="nowrap" style="text-align:right!important"><input type="text" name="fees_shipping_tva" id="fees_shipping_tva" value="' . $fees_shipping_tva . '" onchange="updateTotalTVA();" style="width: 45px; text-align: right" />&nbsp;€</td>
  414. </tr>
  415. <tr>
  416. <td>Facturation</td>
  417. <td nowrap="nowrap" style="text-align:right!important"><input type="text" name="fees_billing_ht" id="fees_billing_ht" value="' . $fees_billing_ht . '" onchange="updateFeesTVA();" style="width: 45px; text-align: right" />&nbsp;€</td>
  418. <td nowrap="nowrap" style="text-align:right!important"><select name="fees_billing_rate" id="fees_billing_rate" onchange="updateFeesTVA();">';
  419. foreach (array('0' => '0%', '0.055' => '5.5%', '0.2' => '20%') as $value => $label) {
  420. $return .= '<option value="' . $value . '"' . sprintf('%s', ($value == $fees_billing_rate)?' selected="selected"':'') . '>' . $label . '</option>';
  421. }
  422. $return .= '</select></td>
  423. <td nowrap="nowrap" style="text-align:right!important"><input type="text" name="fees_billing_tva" id="fees_billing_tva" value="' . $fees_billing_tva . '" onchange="updateTotalTVA();" style="width: 45px; text-align: right" />&nbsp;€</td>
  424. </tr>
  425. </table>
  426. </td>
  427. <td>
  428. <u>Totaux :</u>
  429. <table class="admin-sub-list" style="width:1%!important">
  430. <tr>
  431. <th nowrap="nowrap" width="1%" style="text-align:right!important">HT</th>
  432. <th nowrap="nowrap" width="1%" style="text-align:right!important">TVA</th>
  433. <th nowrap="nowrap" width="1%" style="text-align:right!important">TTC</th>
  434. </tr>
  435. <td nowrap="nowrap" style="text-align:right!important"><input type="text" name="without_taxes_total" id="without_taxes_total" value="' . $without_taxes_total . '" onchange="updateTotalTTC();" style="width: 45px; text-align: right" />&nbsp;€</td>
  436. <td nowrap="nowrap" style="text-align:right!important"><input type="text" name="tva_total" id="tva_total" value="' . $tva_total . '" onchange="updateTotalTTC();" style="width: 45px; text-align: right" />&nbsp;€</td>
  437. <td><span id="ttc_total">' . ($without_taxes_total + $tva_total) . '</span>&nbsp;€</td>
  438. </tr>
  439. </table>
  440. </td>
  441. </tr>
  442. </table>';
  443. }
  444. return $this->renderText($return);
  445. }
  446. }