You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 1 година
преди 2 месеца
преди 1 година
преди 1 година
преди 7 месеца
преди 5 години
преди 7 месеца
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 7 месеца
преди 7 месеца
преди 7 месеца
преди 7 месеца
преди 7 месеца
преди 7 месеца
преди 7 месеца
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * Copyright Guillaume Bourgeois (2018)
  4. *
  5. * contact@souke.fr
  6. *
  7. * Ce logiciel est un programme informatique servant à aider les producteurs
  8. * à distribuer leur production en circuits courts.
  9. *
  10. * Ce logiciel est régi par la licence CeCILL soumise au droit français et
  11. * respectant les principes de diffusion des logiciels libres. Vous pouvez
  12. * utiliser, modifier et/ou redistribuer ce programme sous les conditions
  13. * de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
  14. * sur le site "http://www.cecill.info".
  15. *
  16. * En contrepartie de l'accessibilité au code source et des droits de copie,
  17. * de modification et de redistribution accordés par cette licence, il n'est
  18. * offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
  19. * seule une responsabilité restreinte pèse sur l'auteur du programme, le
  20. * titulaire des droits patrimoniaux et les concédants successifs.
  21. *
  22. * A cet égard l'attention de l'utilisateur est attirée sur les risques
  23. * associés au chargement, à l'utilisation, à la modification et/ou au
  24. * développement et à la reproduction du logiciel par l'utilisateur étant
  25. * donné sa spécificité de logiciel libre, qui peut le rendre complexe à
  26. * manipuler et qui le réserve donc à des développeurs et des professionnels
  27. * avertis possédant des connaissances informatiques approfondies. Les
  28. * utilisateurs sont donc invités à charger et tester l'adéquation du
  29. * logiciel à leurs besoins dans des conditions permettant d'assurer la
  30. * sécurité de leurs systèmes et ou de leurs données et, plus généralement,
  31. * à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
  32. *
  33. * Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
  34. * pris connaissance de la licence CeCILL, et que vous en avez accepté les
  35. * termes.
  36. */
  37. namespace backend\controllers;
  38. use common\helpers\CSV;
  39. use common\helpers\GlobalParam;
  40. use common\helpers\MeanPayment;
  41. use common\helpers\Price;
  42. use domain\Order\Order\OrderRepositoryQuery;
  43. use domain\Order\OrderStatus\OrderStatus;
  44. use backend\forms\ReportPaymentsForm;
  45. use Yii;
  46. use yii\filters\AccessControl;
  47. class ReportController extends BackendController
  48. {
  49. var $enableCsrfValidation = false;
  50. public function behaviors()
  51. {
  52. return [
  53. 'access' => [
  54. 'class' => AccessControl::class,
  55. 'rules' => [
  56. [
  57. 'allow' => true,
  58. 'roles' => ['@'],
  59. 'matchCallback' => function ($rule, $action) {
  60. return $this->getUserModule()
  61. ->getAuthorizationChecker()
  62. ->isGrantedAsProducer($this->getUserCurrent());
  63. }
  64. ]
  65. ],
  66. ],
  67. ];
  68. }
  69. public function actionIndex()
  70. {
  71. $this->checkProductsPointsSale();
  72. return $this->render('index');
  73. }
  74. public function actionAjaxInit()
  75. {
  76. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  77. $userModule = $this->getUserModule();
  78. $pointSaleModule = $this->getPointSaleModule();
  79. $distributionModule = $this-> getDistributionModule();
  80. $usersArray = $userModule->findUsers();
  81. foreach($usersArray as $key => $user) {
  82. $usersArray[$key]['username'] = $userModule->getSolver()->getUsernameFromArray($user, true);
  83. }
  84. $pointsSaleArray = $pointSaleModule->findPointSales();
  85. $firstDistribution = $distributionModule->findOneFirstDistribution();
  86. $lastDistribution = $distributionModule->findOneLastDistribution();
  87. $firstYear = date('Y', strtotime($firstDistribution->date));
  88. $lastYear = date('Y', strtotime($lastDistribution->date));
  89. $distributionYearsArray = [];
  90. for ($year = $firstYear; $year <= $lastYear; $year++) {
  91. $distributionYearsArray[] = $year;
  92. }
  93. $distributionsByMonthArray = [];
  94. $distributionsArray = $distributionModule->findDistributionsWithOrders();
  95. foreach ($distributionsArray as $distribution) {
  96. $month = date('Y-m', strtotime($distribution->date));
  97. if (!isset($distributionsByMonthArray[$month])) {
  98. $distributionsByMonthArray[$month] = [
  99. 'display' => 0,
  100. 'year' => date('Y', strtotime($distribution->date)),
  101. 'month' => strftime('%B', strtotime($distribution->date)),
  102. 'distributions' => []
  103. ];
  104. }
  105. $distribution->date = strftime('%A %d %B %Y', strtotime($distribution->date));
  106. $distributionsByMonthArray[$month]['distributions'][] = $distribution;
  107. }
  108. return [
  109. 'usersArray' => $usersArray,
  110. 'pointsSaleArray' => $pointsSaleArray,
  111. 'distributionYearsArray' => $distributionYearsArray,
  112. 'distributionsByMonthArray' => $distributionsByMonthArray
  113. ];
  114. }
  115. public function actionAjaxReport()
  116. {
  117. $posts = Yii::$app->request->post();
  118. $isDownload = $posts['isDownload'] == 1;
  119. $resArray = [];
  120. $conditionUsers = $this->_generateConditionSqlReport($posts, 'users', 'id_user');
  121. $conditionPointsSale = $this->_generateConditionSqlReport($posts, 'pointsSale', 'id_point_sale');
  122. $conditionDistributions = $this->_generateConditionSqlReport($posts, 'distributions', 'id_distribution');
  123. $res = Yii::$app->db->createCommand("SELECT product.name, SUM(product_order.quantity) AS quantity, SUM(product_order.price * product_order.quantity) AS total
  124. FROM `order`, product_order, product
  125. WHERE `order`.id = product_order.id_order
  126. AND product.id_producer = " . ((int)GlobalParam::getCurrentProducerId()) . "
  127. AND product_order.id_product = product.id
  128. AND ".OrderRepositoryQuery::getSqlFilterIsValid()."
  129. " . $conditionUsers . "
  130. " . $conditionPointsSale . "
  131. " . $conditionDistributions . "
  132. GROUP BY product.id
  133. ORDER BY product.order ASC
  134. ")
  135. ->queryAll();
  136. $totalGlobal = 0;
  137. foreach ($res as $line) {
  138. $roundedTotal = round($line['total'], 2);
  139. $total = $isDownload ? $roundedTotal : Price::format($roundedTotal);
  140. $quantity = $line['quantity'];
  141. if((int) $quantity != $quantity) {
  142. $quantity = round($quantity, 3);
  143. }
  144. if ($line['quantity'] > 0) {
  145. $resArray[] = [
  146. 'name' => $line['name'],
  147. 'quantity' => $quantity,
  148. 'total' => $total,
  149. ];
  150. $totalGlobal += $line['total'];
  151. }
  152. }
  153. $roundedTotalGlobal = round($totalGlobal, 2);
  154. $totalGlobalFormat = $isDownload ? $roundedTotalGlobal : Price::format($roundedTotalGlobal).' HT';
  155. $resArray[] = [
  156. 'name' => '',
  157. 'quantity' => '',
  158. 'total' => $totalGlobalFormat,
  159. ];
  160. if($isDownload) {
  161. CSV::send('rapport.csv', $resArray);
  162. }
  163. else {
  164. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  165. return $resArray;
  166. }
  167. }
  168. public function _generateConditionSqlReport($posts, $name, $fieldOrder)
  169. {
  170. $condition = '';
  171. if (isset($posts[$name]) && strlen($posts[$name])) {
  172. $idsArray = explode(',', $posts[$name]);
  173. for ($i = 0; $i < count($idsArray); $i++) {
  174. $idsArray[$i] = (int)$idsArray[$i];
  175. }
  176. $condition = 'AND `order`.' . $fieldOrder . ' IN (' . implode(',', $idsArray) . ') ';
  177. }
  178. return $condition;
  179. }
  180. public function actionPayments()
  181. {
  182. $dateStart = $dateEnd = null;
  183. $reportPaymentsForm = new ReportPaymentsForm();
  184. if($reportPaymentsForm->load(Yii::$app->request->post()) && $reportPaymentsForm->validate()) {
  185. $dateStart = date('Y-m-d', strtotime(str_replace('/', '-', $reportPaymentsForm->date_start)));
  186. $dateEnd = date('Y-m-d', strtotime(str_replace('/', '-', $reportPaymentsForm->date_end)));
  187. }
  188. if($dateStart && $dateEnd) {
  189. $orderModule = $this->getOrderModule();
  190. $ordersArray = $orderModule->getRepository()
  191. ->findOrdersByPeriod(
  192. new \DateTime($dateStart),
  193. new \DateTime($dateEnd)
  194. );
  195. $datas = [
  196. [
  197. 'Date',
  198. 'Client',
  199. 'Montant',
  200. 'Facture',
  201. 'Crédit',
  202. 'Espèce',
  203. 'Chèque',
  204. 'Virement',
  205. 'Carte bancaire',
  206. 'Total'
  207. ]
  208. ];
  209. $sumAmountTotalSpentByCredit = 0;
  210. $sumAmountTotalSpentByMoney = 0;
  211. $sumAmountTotalSpentByCheque = 0;
  212. $sumAmountTotalSpentByTransfer = 0;
  213. $sumAmountTotalSpentByCreditCard = 0;
  214. foreach($ordersArray as $order) {
  215. $orderModule->getBuilder()->initOrder($order);
  216. $hasInvoice = false;
  217. $referenceInvoice = 'Non';
  218. if($order->invoice) {
  219. $hasInvoice = true;
  220. $referenceInvoice = $order->invoice->reference."";
  221. }
  222. $amountTotalSpentByCredit = $orderModule->getSolver()->getAmountTotalSpentByMeanPayment($order, MeanPayment::CREDIT);
  223. $sumAmountTotalSpentByCredit += $amountTotalSpentByCredit;
  224. $amountTotalSpentByMoney = $orderModule->getSolver()->getAmountTotalSpentByMeanPayment($order, MeanPayment::MONEY);
  225. $sumAmountTotalSpentByMoney += $amountTotalSpentByMoney;
  226. $amountTotalSpentByCheque = $orderModule->getSolver()->getAmountTotalSpentByMeanPayment($order, MeanPayment::CHEQUE);
  227. $sumAmountTotalSpentByCheque += $amountTotalSpentByCheque;
  228. $amountTotalSpentByTransfer = $orderModule->getSolver()->getAmountTotalSpentByMeanPayment($order, MeanPayment::TRANSFER);
  229. $sumAmountTotalSpentByTransfer += $amountTotalSpentByTransfer;
  230. $amountTotalSpentByCreditCard = $orderModule->getSolver()->getAmountTotalSpentByMeanPayment($order, MeanPayment::CREDIT_CARD);
  231. $sumAmountTotalSpentByCreditCard += $amountTotalSpentByCreditCard;
  232. $datas[] = [
  233. $order->distribution->date,
  234. $orderModule->getSolver()->getOrderUsername($order),
  235. CSV::formatNumber($orderModule->getSolver()->getOrderAmountWithTax($order)),
  236. $referenceInvoice,
  237. $hasInvoice ? '' : CSV::formatNumber($amountTotalSpentByCredit),
  238. $hasInvoice ? '' : CSV::formatNumber($amountTotalSpentByMoney),
  239. $hasInvoice ? '' : CSV::formatNumber($amountTotalSpentByCheque),
  240. $hasInvoice ? '' : CSV::formatNumber($amountTotalSpentByTransfer),
  241. $hasInvoice ? '' : CSV::formatNumber($amountTotalSpentByCreditCard)
  242. ];
  243. }
  244. $datas[] = [
  245. '',
  246. '',
  247. '',
  248. 'Totaux paiements',
  249. CSV::formatNumber($sumAmountTotalSpentByCredit),
  250. CSV::formatNumber($sumAmountTotalSpentByMoney),
  251. CSV::formatNumber($sumAmountTotalSpentByCheque),
  252. CSV::formatNumber($sumAmountTotalSpentByTransfer),
  253. CSV::formatNumber($sumAmountTotalSpentByCreditCard),
  254. CSV::formatNumber($sumAmountTotalSpentByCredit + $sumAmountTotalSpentByMoney + $sumAmountTotalSpentByCheque +
  255. $sumAmountTotalSpentByTransfer + $sumAmountTotalSpentByCreditCard)
  256. ];
  257. CSV::send('commandes.csv', $datas);
  258. }
  259. return $this->render('payments', [
  260. 'reportPaymentsForm' => $reportPaymentsForm
  261. ]);
  262. }
  263. }