StatSaleClass.php 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. <?php
  2. namespace bizGhs\stat\classes;
  3. use biz\stat\classes\StatCgClass;
  4. use biz\stat\classes\StatStockInClass;
  5. use biz\stat\classes\StatStockOutClass;
  6. use bizGhs\cg\classes\CgRefundClass;
  7. use bizGhs\custom\classes\CustomClass;
  8. use bizGhs\expend\classes\ExpendClass;
  9. use bizGhs\item\classes\ItemClassClass;
  10. use bizGhs\order\classes\OrderItemClass;
  11. use bizGhs\order\classes\PurchaseOrderClass;
  12. use bizGhs\order\classes\PurchaseOrderItemClass;
  13. use bizGhs\order\classes\RefundOrderClass;
  14. use bizGhs\order\classes\StockWastageOrderClass;
  15. use bizGhs\product\classes\ProductClass;
  16. use bizGhs\staff\classes\SalaryClass;
  17. use bizHd\goods\classes\PlantCgClass;
  18. use bizHd\order\classes\OrderClass as HdOrderClass;
  19. use bizGhs\order\classes\OrderClass;
  20. use bizHd\purchase\classes\PurchaseClass;
  21. use bizHd\refund\classes\HdRefundClass;
  22. use common\components\arrayUtil;
  23. use common\components\dateUtil;
  24. use common\components\dict;
  25. use common\components\noticeUtil;
  26. use common\components\util;
  27. use Yii;
  28. use bizGhs\base\classes\BaseClass;
  29. class StatSaleClass extends BaseClass
  30. {
  31. public static $baseFile = '\bizGhs\stat\models\StatSale';
  32. public static function wastage($mainId)
  33. {
  34. $get = Yii::$app->request->get();
  35. $where = [];
  36. $where['mainId'] = $mainId;
  37. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  38. $startTime = $get['startTime'] ?? '';
  39. $endTime = $get['endTime'] ?? '';
  40. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  41. $start = $period['startTime'];
  42. $end = $period['endTime'];
  43. $currentStartDate = date('Y-m-d', strtotime($start));
  44. $currentEndDate = date('Y-m-d', strtotime($end));
  45. $currentStartTime = $currentStartDate . ' 00:00:00';
  46. $currentEndTime = $currentEndDate . ' 23:59:59';
  47. $where['addTime'] = ['between', [$currentStartTime, $currentEndTime]];
  48. $list = StockWastageOrderClass::getAllByCondition($where, null, '*');
  49. $totalPrice = 0;
  50. if (!empty($list)) {
  51. foreach ($list as $order) {
  52. $price = $order['price'] ?? 0;
  53. $totalPrice = bcadd($totalPrice, $price, 2);
  54. }
  55. }
  56. $totalPrice = floatval($totalPrice);
  57. return ['totalPrice' => $totalPrice];
  58. }
  59. //结账跟踪 ssh 20230503
  60. public static function OrderClear($mainId)
  61. {
  62. $get = Yii::$app->request->get();
  63. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  64. $timeStyle = $get['timeStyle'] ?? 0;
  65. if (isset($get['searchTime']) && !empty($get['searchTime'])) {
  66. $searchTime = $get['searchTime'];
  67. $startTime = $get['startTime'] ?? '';
  68. $endTime = $get['endTime'] ?? '';
  69. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  70. $start = $period['startTime'];
  71. $end = $period['endTime'];
  72. $currentStartDate = date('Y-m-d', strtotime($start));
  73. $currentEndDate = date('Y-m-d', strtotime($end));
  74. $currentStartTime = $currentStartDate . ' 00:00:00';
  75. $currentEndTime = $currentEndDate . ' 23:59:59';
  76. if ($timeStyle == 0) {
  77. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  78. } else {
  79. $where['clearTime'] = ['between', [$currentStartTime, $currentEndTime]];
  80. }
  81. } else {
  82. $payTime = 'today';
  83. $startTime = $get['startTime'] ?? '';
  84. $endTime = $get['endTime'] ?? '';
  85. $period = dateUtil::formatTime($payTime, $startTime, $endTime, true);
  86. $start = $period['startTime'];
  87. $end = $period['endTime'];
  88. $currentStartDate = date('Y-m-d', strtotime($start));
  89. $currentEndDate = date('Y-m-d', strtotime($end));
  90. $currentStartTime = $currentStartDate . ' 00:00:00';
  91. $currentEndTime = $currentEndDate . ' 23:59:59';
  92. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  93. }
  94. $customId = $get['customId'] ?? 0;
  95. if (!empty($customId)) {
  96. $where['customId'] = $customId;
  97. }
  98. $unClear = $get['unClear'] ?? 0;
  99. if ($unClear == 1) {
  100. $where['clearId'] = 0;
  101. $where['remainDebtPrice>'] = 0;
  102. }
  103. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  104. return ['list' => $ghsOrderList];
  105. }
  106. //客户下单榜 ssh 20230502
  107. public static function statCustomCg($mainId)
  108. {
  109. $get = Yii::$app->request->get();
  110. $where = [];
  111. $where['mainId'] = $mainId;
  112. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  113. $startTime = $get['startTime'] ?? '';
  114. $endTime = $get['endTime'] ?? '';
  115. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  116. $start = $period['startTime'];
  117. $end = $period['endTime'];
  118. $where['time'] = ['between', [$start, $end]];
  119. $currentStartDate = date('Y-m-d', strtotime($start));
  120. $currentEndDate = date('Y-m-d', strtotime($end));
  121. $currentStartTime = $currentStartDate . ' 00:00:00';
  122. $currentEndTime = $currentEndDate . ' 23:59:59';
  123. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  124. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  125. $customList = CustomClass::getAllByCondition(['ownMainId' => $mainId], null, 'id,name,py', 'id');
  126. $arr = [];
  127. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  128. $totalAmount = 0;
  129. $totalNum = 0;
  130. if (!empty($ghsOrderList)) {
  131. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  132. $actPrice = $ghsOrder['actPrice'] ?? 0;
  133. $orderPrice = $ghsOrder['orderPrice'] ?? 0;
  134. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  135. $customId = $ghsOrder['customId'] ?? 0;
  136. $status = $ghsOrder['status'] ?? 0;
  137. $book = $ghsOrder['book'] ?? 0;
  138. if ($status == 1 || $status == 5) {
  139. continue;
  140. }
  141. if ($book == 1 && $status == 2) {
  142. continue;
  143. }
  144. $customName = $customList[$customId]['name'] ?? '';
  145. $py = $customList[$customId]['py'] ?? '';
  146. $totalNum++;
  147. $totalAmount = bcadd($totalAmount, $actPrice, 2);
  148. if (isset($arr[$customId])) {
  149. $arr[$customId]['num']++;
  150. $arr[$customId]['amount'] = bcadd($arr[$customId]['amount'], $actPrice, 2);
  151. $arr[$customId]['orderPrice'] = bcadd($arr[$customId]['orderPrice'], $orderPrice, 2);
  152. $arr[$customId]['tkPrice'] = bcadd($arr[$customId]['tkPrice'], $tkPrice, 2);
  153. } else {
  154. $arr[$customId] = [
  155. 'customId' => $customId,
  156. 'customName' => $customName,
  157. 'py' => $py,
  158. 'num' => 1,
  159. 'amount' => $actPrice,
  160. 'levelName' => '',
  161. 'orderPrice' => $orderPrice,
  162. 'tkPrice' => $tkPrice,
  163. ];
  164. }
  165. }
  166. $arr = arrayUtil::arraySort($arr, 'amount');
  167. }
  168. $totalAmount = floatval($totalAmount);
  169. return ['list' => $arr, 'totalAmount' => $totalAmount, 'totalNum' => $totalNum];
  170. }
  171. public static function profile($mainId)
  172. {
  173. //收入:批发开单、零售开单(含门店、美团和微信客服)、出库
  174. //支出:采购、调拨入库、批发退款、零售退款、店租、水电、伙食
  175. $get = Yii::$app->request->get();
  176. $where = [];
  177. $where['mainId'] = $mainId;
  178. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  179. $startTime = $get['startTime'] ?? '';
  180. $endTime = $get['endTime'] ?? '';
  181. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  182. $start = $period['startTime'];
  183. $end = $period['endTime'];
  184. $where['time'] = ['between', [$start, $end]];
  185. $currentStartDate = date('Y-m-d', strtotime($start));
  186. $currentEndDate = date('Y-m-d', strtotime($end));
  187. $currentStartTime = $currentStartDate . ' 00:00:00';
  188. $currentEndTime = $currentEndDate . ' 23:59:59';
  189. //出库
  190. $stockOut = StatStockOutClass::getSum($where);
  191. //采购入库
  192. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  193. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  194. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  195. $cg = 0;
  196. if (!empty($cgList)) {
  197. foreach ($cgList as $cgKey => $cgItem) {
  198. $actPrice = $cgItem['actPrice'] ?? 0;
  199. $paidPrice = $cgItem['paidPrice'] ?? 0;
  200. if ($actPrice <= 0) {
  201. // 如果$actPrice=0,表示这个采购单已经全部退款,不需要再统计成本,本来$paidPrice要同步清零,但没有,这个问题由徐州邂逅花坊马总,提出的需求发现
  202. $cg = bcadd($cg, 0, 2);
  203. } else {
  204. $total = bcadd($actPrice, $paidPrice, 2);
  205. //物流费用不算成本。意思是说,长短途运费+提货费+本地运费,这四项和打包费一起,参与到平摊花材运费成本的计算,但那四项不参与总的采购成本登记,因为,有另外地方会独立登记运费成本。
  206. //$paidPrice 字段说明,此字段表示收货方已付款金额,包括 $localCharge本地运费、 $pickCharge提货费、 $shortCharge短途运费(到付时算,如果不是到付,不算在$paidPrice里)、$longCharge长途运费(到付时算,如果不是到付,不算在$paidPrice里)
  207. //请搜索关键词 cg_item_cost ,有多处相似情况要考虑
  208. if (isset($cgItem['isCost']) && $cgItem['isCost'] == 0) {
  209. $shortCharge = $cgItem['shortCharge'] ?? 0;
  210. $longCharge = $cgItem['longCharge'] ?? 0;
  211. $pickCharge = $cgItem['pickCharge'] ?? 0;
  212. $localCharge = $cgItem['localCharge'] ?? 0;
  213. $a1 = bcadd($shortCharge, $longCharge, 2);
  214. $a2 = bcadd($pickCharge, $localCharge, 2);
  215. $a = bcadd($a1, $a2, 2);
  216. //注意下面是减号 bcsub
  217. $total = bcsub($total, $a, 2);
  218. }
  219. $cg = bcadd($cg, $total, 2);
  220. }
  221. }
  222. }
  223. $cg = floatval($cg);
  224. $lsCgWhere = ['mainId' => $mainId, 'status' => 4, 'belongCost' => 1];
  225. $lsCgWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  226. $lsCgList = PurchaseClass::getAllByCondition($lsCgWhere, null, '*');
  227. $lsCgAmount = 0;
  228. if (!empty($lsCgList)) {
  229. foreach ($lsCgList as $lsCgItem) {
  230. $actPrice = $lsCgItem['actPrice'] ?? 0;
  231. $lsCgAmount = bcadd($lsCgAmount, $actPrice, 2);
  232. }
  233. }
  234. $lsCgAmount = floatval($lsCgAmount);
  235. $lzWhere = ['mainId' => $mainId, 'status' => 3];
  236. $lzWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  237. $lzCgList = PlantCgClass::getAllByCondition($lzWhere, null, '*');
  238. $lzCgAmount = 0;
  239. if (!empty($lzCgList)) {
  240. foreach ($lzCgList as $lzInfo) {
  241. $actPrice = $lzInfo['actPrice'] ?? 0;
  242. $lzCgAmount = bcadd($actPrice, $lzCgAmount, 2);
  243. }
  244. }
  245. $lzCgAmount = floatval($lzCgAmount);
  246. //调拨入库
  247. $stockIn = StatStockInClass::getSum($where);
  248. $totalOrderNum = 0;
  249. //批发开单
  250. $pfIncome = 0;
  251. $pfSendCost = 0;
  252. $pfPack = 0;
  253. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  254. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  255. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  256. if (!empty($ghsOrderList)) {
  257. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  258. $actPrice = $ghsOrder['actPrice'] ?? 0;
  259. $sendCost = $ghsOrder['sendCost'] ?? 0;
  260. $refundSendCost = $ghsOrder['refundSendCost'] ?? 0;
  261. $remainSendCost = bcsub($sendCost, $refundSendCost, 2);
  262. $packCost = $ghsOrder['packCost'] ?? 0;
  263. $refundPackCost = $ghsOrder['refundPackCost'] ?? 0;
  264. $remainPackCost = bcsub($packCost, $refundPackCost, 2);
  265. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  266. $pfSendCost = bcadd($pfSendCost, $remainSendCost, 2);
  267. $pfPack = bcadd($pfPack, $remainPackCost, 2);
  268. $totalOrderNum++;
  269. }
  270. }
  271. //零售开单
  272. $payCodeIncome = 0;
  273. $lsIncome = 0;
  274. $lsSendCost = 0;
  275. $lsServiceFee = 0;
  276. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  277. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  278. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  279. foreach ($hdOrderList as $hdKey => $hdOrder) {
  280. $actPrice = $hdOrder['mainPay'] ?? 0;
  281. $onlinePay = $hdOrder['onlinePay'] ?? 1;
  282. $repeat = $hdOrder['repeat'] ?? 0;
  283. if ($hdOrder['tkPrice'] && $hdOrder['tkPrice'] > 0) {
  284. $actPrice = bcsub($actPrice, $hdOrder['tkPrice'], 2);
  285. }
  286. $cash = $hdOrder['cash'] ?? 0;
  287. $serviceFee = $hdOrder['serviceFee'] ?? 0;
  288. $sendCost = $hdOrder['sendCost'] ?? 0;
  289. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  290. $lsIncome = bcadd($lsIncome, $cash, 2);
  291. //退款取消的先不计算成本
  292. if ($actPrice > 0) {
  293. $lsSendCost = bcadd($lsSendCost, $sendCost, 2);
  294. $lsServiceFee = bcadd($lsServiceFee, $serviceFee, 2);
  295. }
  296. $totalOrderNum++;
  297. if ($onlinePay == 2 && floatval($cash) <= 0 && $repeat == 1) {
  298. $payCodeIncome = bcadd($payCodeIncome, $actPrice, 2);
  299. $payCodeIncome = floatval($payCodeIncome);
  300. }
  301. }
  302. $condition = ['mainId' => $mainId, 'addTime' => ['between', [$currentStartTime, $currentEndTime]],];
  303. $salary = 0;
  304. $salaryList = SalaryClass::getAllByCondition($condition, null, '*', null, true);
  305. if (!empty($salaryList)) {
  306. foreach ($salaryList as $item) {
  307. $salary = bcadd($salary, $item->amount, 2);
  308. }
  309. }
  310. $condition = [
  311. 'mainId' => $mainId,
  312. 'payTime' => ['between', [$currentStartTime, $currentEndTime]],
  313. 'delStatus' => 0,
  314. ];
  315. $expendList = ExpendClass::getAllByCondition($condition, null, '*', null, true);
  316. $expendTypeMap = dict::getDict('expendTypeMap');
  317. $expendAmount = [];
  318. if (!empty($expendList)) {
  319. foreach ($expendList as $item) {
  320. $type = $item->type ?? 0;
  321. $amount = $item->amount ?? 0;
  322. $name = $expendTypeMap[$type] ?? '暂无';
  323. if (isset($expendAmount[$type]['amount']) == false) {
  324. $expendAmount[$type]['amount'] = 0;
  325. }
  326. $expendAmount[$type]['name'] = $name;
  327. $expendAmount[$type]['amount'] = floatval(bcadd($expendAmount[$type]['amount'], $amount, 2));
  328. }
  329. }
  330. $income = [
  331. ['name' => '批发', 'id' => 'pf', 'amount' => floatval($pfIncome)],
  332. ['name' => '零售', 'id' => 'ls', 'amount' => floatval($lsIncome)],
  333. ['name' => '调拨出库', 'id' => 'allot', 'amount' => floatval($stockOut)],
  334. ];
  335. $expend = [
  336. ['name' => '批发运费', 'id' => 'pfSendCost', 'amount' => floatval($pfSendCost)],
  337. ['name' => '批发包装费', 'id' => 'pfSendCost', 'amount' => floatval($pfPack)],
  338. ['name' => '零售运费', 'id' => 'lsSendCost', 'amount' => floatval($lsSendCost)],
  339. ['name' => '美团手续费', 'id' => 'mtFee', 'amount' => floatval($lsServiceFee)],
  340. ['name' => '花材采购', 'id' => 'cg', 'amount' => floatval($cg)],
  341. ['name' => '绿植资材采购', 'id' => 'cg', 'amount' => floatval($lzCgAmount)],
  342. ['name' => '零售花材采购', 'id' => 'cg', 'amount' => floatval($lsCgAmount)],
  343. ['name' => '调拨入库', 'id' => 'allot', 'amount' => floatval($stockIn)],
  344. ['name' => '员工工资', 'id' => 'salary', 'amount' => floatval($salary)],
  345. ];
  346. if (!empty($expendAmount)) {
  347. foreach ($expendAmount as $it) {
  348. $expend[] = $it;
  349. }
  350. }
  351. $balance = 0;
  352. foreach ($income as $it) {
  353. $amount = $it['amount'] ?? 0;
  354. $balance = bcadd($amount, $balance, 2);
  355. }
  356. foreach ($expend as $it) {
  357. $amount = $it['amount'] ?? 0;
  358. $balance = bcsub($balance, $amount, 2);
  359. }
  360. return ['income' => $income, 'expend' => $expend, 'balance' => floatval($balance), 'totalOrderNum' => $totalOrderNum, 'payCodeIncome' => $payCodeIncome];
  361. }
  362. //按花材统计收入 ssh 20231111
  363. public static function incomeOutItem($mainId)
  364. {
  365. $get = Yii::$app->request->get();
  366. $export = $get['export'] ?? 0;
  367. $where = [];
  368. $where['mainId'] = $mainId;
  369. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  370. $startTime = $get['startTime'] ?? '';
  371. $endTime = $get['endTime'] ?? '';
  372. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  373. $start = $period['startTime'];
  374. $end = $period['endTime'];
  375. $where['time'] = ['between', [$start, $end]];
  376. $currentStartDate = date('Y-m-d', strtotime($start));
  377. $currentEndDate = date('Y-m-d', strtotime($end));
  378. $currentStartTime = $currentStartDate . ' 00:00:00';
  379. $currentEndTime = $currentEndDate . ' 23:59:59';
  380. $s = strtotime($currentStartTime);
  381. $e = strtotime($currentEndTime);
  382. $p = bcsub($e, $s);
  383. $xx = bcmul(86400, 90);
  384. if ($p > $xx) {
  385. util::fail('查询时间不能超过三个月');
  386. }
  387. $productList = ProductClass::getAllByCondition(['mainId' => $mainId], null, 'id,name', 'id');
  388. if (empty($productList)) {
  389. util::fail('没有找到花材');
  390. }
  391. $arr = [];
  392. //打包费和运费
  393. $packFreight = 0;
  394. $onlyRefundCg = 0;
  395. $onlyRefund = 0;
  396. $totalDiscountAmount = 0;
  397. $totalLabourCost = 0;
  398. //采购入库
  399. $tbCgIncome = [];
  400. $tbCgIncomeStaff = [];
  401. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  402. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  403. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  404. if (!empty($cgList)) {
  405. foreach ($cgList as $cgKey => $cgItem) {
  406. $packingCharge = $cgItem['packingCharge'] ?? 0;
  407. $shortCharge = $cgItem['shortCharge'] ?? 0;
  408. $longCharge = $cgItem['longCharge'] ?? 0;
  409. $pickCharge = $cgItem['pickCharge'] ?? 0;
  410. $localCharge = $cgItem['localCharge'] ?? 0;
  411. $packFreight = bcadd($packFreight, $packingCharge, 2);
  412. $packFreight = bcadd($packFreight, $shortCharge, 2);
  413. $packFreight = bcadd($packFreight, $longCharge, 2);
  414. $packFreight = bcadd($packFreight, $pickCharge, 2);
  415. $packFreight = bcadd($packFreight, $localCharge, 2);
  416. $tkPrice = $cgItem['tkPrice'] ?? 0;
  417. $orderSn = $cgItem['orderSn'] ?? '';
  418. $cgItemInfoList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  419. if (!empty($cgItemInfoList)) {
  420. foreach ($cgItemInfoList as $cgData) {
  421. $itemNum = $cgData->itemNum ?? 0;
  422. $productId = $cgData->productId ?? 0;
  423. $refundNum = $cgData->refundNum ?? 0;
  424. $remainNum = bcsub($itemNum, $refundNum);
  425. $bigPrice = $cgData->bigPrice ?? 0;
  426. $amount = bcmul($remainNum, $bigPrice, 2);
  427. $itemName = $productList[$productId]['name'] ?? '已删除';
  428. $arr[$productId]['name'] = $itemName;
  429. $arr[$productId]['id'] = $productId;
  430. if (isset($arr[$productId]['expend'])) {
  431. $arr[$productId]['expend'] = bcadd($amount, $arr[$productId]['expend'], 2);
  432. $arr[$productId]['outNum'] = bcadd($arr[$productId]['outNum'], $remainNum, 2);
  433. } else {
  434. $arr[$productId]['expend'] = $amount;
  435. $arr[$productId]['outNum'] = $remainNum;
  436. $arr[$productId]['inNum'] = 0;
  437. $arr[$productId]['income'] = 0;
  438. }
  439. }
  440. if ($tkPrice > 0) {
  441. $cgRefundOrderList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  442. if (!empty($cgRefundOrderList)) {
  443. foreach ($cgRefundOrderList as $cgRefundOrder) {
  444. if ($cgRefundOrder->refundType == 2) {
  445. $onlyRefundCg = bcadd($cgRefundOrder->refundPrice, $onlyRefundCg, 2);
  446. }
  447. }
  448. }
  449. }
  450. }
  451. }
  452. }
  453. //批发开单
  454. $pfIncome = 0;
  455. $tbSaleIncome = [];
  456. $tbSaleIncomeStaff = [];
  457. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  458. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  459. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  460. if (!empty($ghsOrderList)) {
  461. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  462. $book = $ghsOrder['book'] ?? 0;
  463. $status = $ghsOrder['status'] ?? 0;
  464. if ($book == 1 && $status == 2) {
  465. //预订单待发货的不算收入
  466. continue;
  467. }
  468. if ($status == 0 || $status == 5) {
  469. continue;
  470. }
  471. $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
  472. $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
  473. $orderSn = $ghsOrder['orderSn'] ?? '';
  474. $actPrice = $ghsOrder['actPrice'] ?? 0;
  475. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  476. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  477. $labourCost = $ghsOrder['labourCost'] ?? 0;
  478. $discountAmount = $ghsOrder['discountAmount'] ?? 0;
  479. $totalLabourCost = bcadd($labourCost, $totalLabourCost, 2);
  480. $totalDiscountAmount = bcadd($totalDiscountAmount, $discountAmount, 2);
  481. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  482. if (!empty($orderItemList)) {
  483. foreach ($orderItemList as $orderItem) {
  484. $refundNum = $orderItem->refundNum ?? 0;
  485. $name = $orderItem->name ?? '';
  486. $productId = $orderItem->productId ?? 0;
  487. $num = $orderItem->xhNum ?? 0;
  488. $remainNum = bcsub($num, $refundNum);
  489. $xhUnitPrice = $orderItem->xhUnitPrice ?? 0;
  490. $amount = bcmul($remainNum, $xhUnitPrice, 2);
  491. $itemName = $productList[$productId]['name'] ?? '已删除';
  492. $arr[$productId]['name'] = $itemName;
  493. $arr[$productId]['id'] = $productId;
  494. if (isset($arr[$productId]['income'])) {
  495. $arr[$productId]['income'] = bcadd($amount, $arr[$productId]['income'], 2);
  496. $arr[$productId]['inNum'] = bcadd($arr[$productId]['inNum'], $remainNum, 2);
  497. } else {
  498. $arr[$productId]['expend'] = 0;
  499. $arr[$productId]['outNum'] = 0;
  500. $arr[$productId]['inNum'] = $remainNum;
  501. $arr[$productId]['income'] = $amount;
  502. }
  503. //单独统计
  504. $belongCost = $orderItem->belongCost ?? 0;
  505. if ($belongCost == 1) {
  506. if (isset($tbSaleIncome[$productId])) {
  507. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  508. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  509. } else {
  510. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  511. }
  512. if (isset($tbSaleIncomeStaff[$productId]) && isset($tbSaleIncomeStaff[$productId][$shopAdminId])) {
  513. $tbSaleIncomeStaff[$productId][$shopAdminId]['num'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['num'], $remainNum);
  514. $tbSaleIncomeStaff[$productId][$shopAdminId]['price'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['price'], $amount, 2);
  515. } else {
  516. $tbSaleIncomeStaff[$productId][$shopAdminId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount, 'staffName' => $shopAdminName];
  517. }
  518. }
  519. }
  520. if ($tkPrice > 0) {
  521. $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  522. if (!empty($refundList)) {
  523. foreach ($refundList as $refund) {
  524. if ($refund->refundType == 1) {
  525. //退货退款前面已经考虑,这里不再处理 OK
  526. } else {
  527. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  528. }
  529. }
  530. }
  531. }
  532. }
  533. }
  534. }
  535. //零售开单
  536. $lsIncome = 0;
  537. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  538. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  539. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  540. foreach ($hdOrderList as $hdKey => $hdOrder) {
  541. $actPrice = $hdOrder['mainPay'] ?? 0;
  542. $cash = $hdOrder['cash'] ?? 0;
  543. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  544. $lsIncome = bcadd($lsIncome, $cash, 2);
  545. $orderSn = $hdOrder['orderSn'] ?? '';
  546. $tkPrice = $hdOrder['tkPrice'] ?? 0;
  547. $orderItemList = \bizHd\order\classes\OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  548. if (!empty($orderItemList)) {
  549. foreach ($orderItemList as $orderItem) {
  550. $unitPrice = $orderItem->unitPrice ?? 0;
  551. $refundNum = $orderItem->refundNum ?? 0;
  552. $num = $orderItem->num ?? 0;
  553. $productId = $orderItem->itemId ?? 0;
  554. $remainNum = bcsub($num, $refundNum);
  555. $amount = bcmul($remainNum, $unitPrice, 2);
  556. $itemName = $productList[$productId]['name'] ?? '已删除';
  557. $arr[$productId]['name'] = $itemName;
  558. $arr[$productId]['id'] = $productId;
  559. if (isset($arr[$productId]['income'])) {
  560. $arr[$productId]['income'] = bcadd($amount, $arr[$productId]['income'], 2);
  561. $arr[$productId]['inNum'] = bcadd($arr[$productId]['inNum'], $remainNum, 2);
  562. } else {
  563. $arr[$productId]['expend'] = 0;
  564. $arr[$productId]['outNum'] = 0;
  565. $arr[$productId]['inNum'] = $remainNum;
  566. $arr[$productId]['income'] = $amount;
  567. }
  568. }
  569. if ($tkPrice > 0) {
  570. $refundList = HdRefundClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  571. if (!empty($refundList)) {
  572. foreach ($refundList as $refund) {
  573. if ($refund->refundType == 2) {
  574. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  575. }
  576. }
  577. }
  578. }
  579. }
  580. }
  581. $totalIncome = 0;
  582. $totalExpend = 0;
  583. if (!empty($arr)) {
  584. foreach ($arr as $key => $val) {
  585. $income = $val['income'] ?? 0;
  586. $income = floatval($income);
  587. $totalIncome = bcadd($totalIncome, $income, 2);
  588. $expend = $val['expend'] ?? 0;
  589. $expend = floatval($expend);
  590. $totalExpend = bcadd($totalExpend, $expend, 2);
  591. $profit = bcsub($income, $expend, 2);
  592. $arr[$key]['profit'] = floatval($profit);
  593. $arr[$key]['income'] = floatval($income);
  594. $arr[$key]['expend'] = floatval($expend);
  595. }
  596. }
  597. //总成本算上物流打包等费用
  598. $totalExpend = bcadd($totalExpend, $packFreight, 2);
  599. //总收入算上采购仅退款
  600. $totalIncome = bcadd($totalIncome, $onlyRefundCg, 2);
  601. //总收入加上人工费
  602. $totalIncome = bcadd($totalIncome, $totalLabourCost, 2);
  603. //总收入减掉优惠金额
  604. $totalIncome = bcsub($totalIncome, $totalDiscountAmount, 2);
  605. //总成本算上销售仅退款
  606. $totalExpend = bcadd($totalExpend, $onlyRefund, 2);
  607. $totalProfit = bcsub($totalIncome, $totalExpend, 2);
  608. $totalIncome = floatval($totalIncome);
  609. $totalExpend = floatval($totalExpend);
  610. $totalProfit = floatval($totalProfit);
  611. $arr = array_values($arr);
  612. $arr = arrayUtil::arraySort($arr, 'profit');
  613. if ($export == 1) {
  614. $statData = [
  615. ['id' => 0, 'name' => '物流打包费', 'income' => 0, 'expend' => $packFreight, 'profit' => -$packFreight, 'inNum' => 0, 'outNum' => 0],
  616. ['id' => 0, 'name' => '采购仅退款', 'income' => $onlyRefundCg, 'expend' => 0, 'profit' => $onlyRefundCg, 'inNum' => 0, 'outNum' => 0],
  617. ['id' => 0, 'name' => '销售仅退款', 'income' => 0, 'expend' => $onlyRefund, 'profit' => -$onlyRefund, 'inNum' => 0, 'outNum' => 0],
  618. ['id' => 0, 'name' => '附加人工费', 'income' => $totalLabourCost, 'expend' => 0, 'profit' => $totalLabourCost, 'inNum' => 0, 'outNum' => 0],
  619. ['id' => 0, 'name' => '优惠金额', 'income' => -$totalDiscountAmount, 'expend' => 0, 'profit' => -$totalDiscountAmount, 'inNum' => 0, 'outNum' => 0],
  620. ['id' => 0, 'name' => '【汇总金额】', 'income' => $totalIncome, 'expend' => $totalExpend, 'profit' => $totalProfit, 'inNum' => 0, 'outNum' => 0],
  621. ];
  622. $list = array_merge($arr, $statData);
  623. self::exportIncomeOutItem($list, $mainId);
  624. }
  625. util::success(['list' => $arr, 'packFreight' => $packFreight, 'onlyRefundCg' => $onlyRefundCg, 'onlyRefund' => $onlyRefund,
  626. 'totalIncome' => $totalIncome,
  627. 'totalExpend' => $totalExpend,
  628. 'totalProfit' => $totalProfit,
  629. 'tbSaleIncome' => $tbSaleIncome,
  630. 'tbCgIncome' => $tbCgIncome,
  631. 'tbCgIncomeStaff' => $tbCgIncomeStaff,
  632. 'tbSaleIncomeStaff' => $tbSaleIncomeStaff,
  633. 'totalLabourCost' => $totalLabourCost,
  634. 'totalDiscountAmount' => $totalDiscountAmount,
  635. ]);
  636. }
  637. //按分类统计收入 ssh 20231111
  638. public static function classProfile($mainId)
  639. {
  640. $get = Yii::$app->request->get();
  641. $export = $get['export'] ?? 0;
  642. $where = [];
  643. $where['mainId'] = $mainId;
  644. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  645. $startTime = $get['startTime'] ?? '';
  646. $endTime = $get['endTime'] ?? '';
  647. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  648. $start = $period['startTime'];
  649. $end = $period['endTime'];
  650. $where['time'] = ['between', [$start, $end]];
  651. $currentStartDate = date('Y-m-d', strtotime($start));
  652. $currentEndDate = date('Y-m-d', strtotime($end));
  653. $currentStartTime = $currentStartDate . ' 00:00:00';
  654. $currentEndTime = $currentEndDate . ' 23:59:59';
  655. $s = strtotime($currentStartTime);
  656. $e = strtotime($currentEndTime);
  657. $p = bcsub($e, $s);
  658. $xx = bcmul(86400, 90);
  659. if ($p > $xx) {
  660. util::fail('查询时间不能超过三个月');
  661. }
  662. $classList = ItemClassClass::getAllByCondition(['mainId' => $mainId], null, 'id,name', 'id');
  663. if (empty($classList)) {
  664. util::fail('没有找到花材');
  665. }
  666. $arr = [];
  667. //打包费和运费
  668. $packFreight = 0;
  669. $onlyRefundCg = 0;
  670. $onlyRefund = 0;
  671. $totalDiscountAmount = 0;
  672. $totalLabourCost = 0;
  673. //采购入库
  674. $tbCgIncome = [];
  675. $tbCgIncomeStaff = [];
  676. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  677. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  678. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  679. if (!empty($cgList)) {
  680. foreach ($cgList as $cgKey => $cgItem) {
  681. $packingCharge = $cgItem['packingCharge'] ?? 0;
  682. $shortCharge = $cgItem['shortCharge'] ?? 0;
  683. $longCharge = $cgItem['longCharge'] ?? 0;
  684. $pickCharge = $cgItem['pickCharge'] ?? 0;
  685. $localCharge = $cgItem['localCharge'] ?? 0;
  686. $cgStaffId = $cgItem['cgStaffId'] ?? 0;
  687. $cgStaffName = $cgItem['cgStaffName'] ?? '';
  688. $actPrice = $cgItem['actPrice'] ?? 0;
  689. if ($actPrice <= 0) {
  690. continue;
  691. }
  692. $packFreight = bcadd($packFreight, $packingCharge, 2);
  693. $packFreight = bcadd($packFreight, $shortCharge, 2);
  694. $packFreight = bcadd($packFreight, $longCharge, 2);
  695. $packFreight = bcadd($packFreight, $pickCharge, 2);
  696. $packFreight = bcadd($packFreight, $localCharge, 2);
  697. $tkPrice = $cgItem['tkPrice'] ?? 0;
  698. $orderSn = $cgItem['orderSn'] ?? '';
  699. $cgItemInfoList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  700. if (!empty($cgItemInfoList)) {
  701. foreach ($cgItemInfoList as $cgData) {
  702. $itemNum = $cgData->itemNum ?? 0;
  703. $productId = $cgData->productId ?? 0;
  704. $name = $cgData->name ?? '';
  705. $refundNum = $cgData->refundNum ?? 0;
  706. $remainNum = bcsub($itemNum, $refundNum);
  707. $bigPrice = $cgData->bigPrice ?? 0;
  708. $amount = bcmul($remainNum, $bigPrice, 2);
  709. $classId = $cgData->classId;
  710. $className = $classList[$classId]['name'] ?? '已删除';
  711. if ($className == '运费') {
  712. //noticeUtil::push("{$cgData->orderSn}运费项", '15280215347');
  713. }
  714. $arr[$classId]['className'] = $className;
  715. $arr[$classId]['classId'] = $classId;
  716. if (isset($arr[$classId]['expend'])) {
  717. $arr[$classId]['expend'] = bcadd($amount, $arr[$classId]['expend'], 2);
  718. $arr[$classId]['cgNum'] = bcadd($arr[$classId]['cgNum'], $remainNum);
  719. } else {
  720. $arr[$classId]['expend'] = $amount;
  721. $arr[$classId]['income'] = 0;
  722. $arr[$classId]['cgNum'] = $remainNum;
  723. $arr[$classId]['saleNum'] = 0;
  724. }
  725. //单独统计
  726. $belongCost = $cgData->belongCost ?? 0;
  727. if ($belongCost == 1) {
  728. if (isset($tbCgIncome[$productId])) {
  729. $tbCgIncome[$productId]['num'] = bcadd($tbCgIncome[$productId]['num'], $remainNum);
  730. $tbCgIncome[$productId]['price'] = bcadd($tbCgIncome[$productId]['price'], $amount, 2);
  731. } else {
  732. $tbCgIncome[$productId] = ['num' => $remainNum, 'name' => $name, 'price' => $amount];
  733. }
  734. if (isset($tbCgIncomeStaff[$productId]) && isset($tbCgIncomeStaff[$productId][$cgStaffId])) {
  735. $tbCgIncomeStaff[$productId][$cgStaffId]['num'] = bcadd($tbCgIncomeStaff[$productId][$cgStaffId]['num'], $remainNum);
  736. $tbCgIncomeStaff[$productId][$cgStaffId]['price'] = bcadd($tbCgIncomeStaff[$productId][$cgStaffId]['price'], $amount, 2);
  737. } else {
  738. $tbCgIncomeStaff[$productId][$cgStaffId] = ['num' => $remainNum, 'name' => $name, 'price' => $amount, 'staffName' => $cgStaffName,];
  739. }
  740. }
  741. }
  742. if ($tkPrice > 0) {
  743. $cgRefundOrderList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  744. if (!empty($cgRefundOrderList)) {
  745. foreach ($cgRefundOrderList as $cgRefundOrder) {
  746. if ($cgRefundOrder->refundType == 2) {
  747. $onlyRefundCg = bcadd($cgRefundOrder->refundPrice, $onlyRefundCg, 2);
  748. }
  749. }
  750. }
  751. }
  752. }
  753. }
  754. }
  755. //批发开单
  756. $pfIncome = 0;
  757. $tbSaleIncome = [];
  758. $tbSaleIncomeStaff = [];
  759. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  760. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  761. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  762. if (!empty($ghsOrderList)) {
  763. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  764. $book = $ghsOrder['book'] ?? 0;
  765. $status = $ghsOrder['status'] ?? 0;
  766. if ($book == 1 && $status == 2) {
  767. //预订单待发货的不算收入
  768. continue;
  769. }
  770. if ($status == 0 || $status == 5) {
  771. continue;
  772. }
  773. $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
  774. $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
  775. $orderSn = $ghsOrder['orderSn'] ?? '';
  776. $actPrice = $ghsOrder['actPrice'] ?? 0;
  777. if ($actPrice <= 0) {
  778. continue;
  779. }
  780. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  781. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  782. $labourCost = $ghsOrder['labourCost'] ?? 0;
  783. $discountAmount = $ghsOrder['discountAmount'] ?? 0;
  784. $totalLabourCost = bcadd($labourCost, $totalLabourCost, 2);
  785. $totalDiscountAmount = bcadd($totalDiscountAmount, $discountAmount, 2);
  786. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  787. if (!empty($orderItemList)) {
  788. foreach ($orderItemList as $orderItem) {
  789. $refundNum = $orderItem->refundNum ?? 0;
  790. $name = $orderItem->name ?? '';
  791. $productId = $orderItem->productId ?? 0;
  792. $num = $orderItem->xhNum ?? 0;
  793. $remainNum = bcsub($num, $refundNum);
  794. $classId = $orderItem->classId ?? 0;
  795. $className = $classList[$classId]['name'] ?? '已删除';
  796. $xhUnitPrice = $orderItem->xhUnitPrice ?? 0;
  797. $amount = bcmul($remainNum, $xhUnitPrice, 2);
  798. $arr[$classId]['className'] = $className;
  799. $arr[$classId]['classId'] = $classId;
  800. if (isset($arr[$classId]['income'])) {
  801. $arr[$classId]['income'] = bcadd($amount, $arr[$classId]['income'], 2);
  802. $arr[$classId]['saleNum'] = bcadd($arr[$classId]['saleNum'], $remainNum);
  803. } else {
  804. $arr[$classId]['expend'] = 0;
  805. $arr[$classId]['income'] = $amount;
  806. $arr[$classId]['saleNum'] = $remainNum;
  807. $arr[$classId]['cgNum'] = 0;
  808. }
  809. //单独统计
  810. $belongCost = $orderItem->belongCost ?? 0;
  811. if ($belongCost == 1) {
  812. if (isset($tbSaleIncome[$productId])) {
  813. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  814. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  815. } else {
  816. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  817. }
  818. if (isset($tbSaleIncomeStaff[$productId]) && isset($tbSaleIncomeStaff[$productId][$shopAdminId])) {
  819. $tbSaleIncomeStaff[$productId][$shopAdminId]['num'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['num'], $remainNum);
  820. $tbSaleIncomeStaff[$productId][$shopAdminId]['price'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['price'], $amount, 2);
  821. } else {
  822. $tbSaleIncomeStaff[$productId][$shopAdminId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount, 'staffName' => $shopAdminName];
  823. }
  824. }
  825. }
  826. if ($tkPrice > 0) {
  827. $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  828. if (!empty($refundList)) {
  829. foreach ($refundList as $refund) {
  830. if ($refund->refundType == 1) {
  831. //退货退款前面已经考虑,这里不再处理 OK
  832. } else {
  833. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  834. }
  835. }
  836. }
  837. }
  838. }
  839. }
  840. }
  841. //零售开单
  842. $lsIncome = 0;
  843. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  844. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  845. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  846. foreach ($hdOrderList as $hdKey => $hdOrder) {
  847. $actPrice = $hdOrder['mainPay'] ?? 0;
  848. $cash = $hdOrder['cash'] ?? 0;
  849. if ($actPrice <= 0 && $cash <= 0) {
  850. continue;
  851. }
  852. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  853. $lsIncome = bcadd($lsIncome, $cash, 2);
  854. $orderSn = $hdOrder['orderSn'] ?? '';
  855. $tkPrice = $hdOrder['tkPrice'] ?? 0;
  856. $orderItemList = \bizHd\order\classes\OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  857. if (!empty($orderItemList)) {
  858. foreach ($orderItemList as $orderItem) {
  859. $classId = $orderItem->classId ?? 0;
  860. $unitPrice = $orderItem->unitPrice ?? 0;
  861. $refundNum = $orderItem->refundNum ?? 0;
  862. $num = $orderItem->num ?? 0;
  863. $productId = $orderItem->itemId ?? 0;
  864. $name = $orderItem->name ?? '';
  865. $remainNum = bcsub($num, $refundNum);
  866. $amount = bcmul($remainNum, $unitPrice, 2);
  867. $className = $classList[$classId]['name'] ?? '已删除';
  868. $arr[$classId]['className'] = $className;
  869. $arr[$classId]['classId'] = $classId;
  870. if (isset($arr[$classId]['income'])) {
  871. $arr[$classId]['income'] = bcadd($amount, $arr[$classId]['income'], 2);
  872. $arr[$classId]['saleNum'] = bcadd($arr[$classId]['saleNum'], $remainNum);
  873. } else {
  874. $arr[$classId]['expend'] = 0;
  875. $arr[$classId]['income'] = $amount;
  876. $arr[$classId]['saleNum'] = $remainNum;
  877. $arr[$classId]['cgNum'] = 0;
  878. }
  879. $belongCost = $orderItem->belongCost ?? 0;
  880. if ($belongCost == 1) {
  881. if (isset($tbSaleIncome[$productId])) {
  882. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  883. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  884. } else {
  885. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  886. }
  887. }
  888. }
  889. if ($tkPrice > 0) {
  890. $refundList = HdRefundClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  891. if (!empty($refundList)) {
  892. foreach ($refundList as $refund) {
  893. if ($refund->refundType == 2) {
  894. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  895. }
  896. }
  897. }
  898. }
  899. }
  900. }
  901. $totalIncome = 0;
  902. $totalExpend = 0;
  903. if (!empty($arr)) {
  904. foreach ($arr as $key => $val) {
  905. $income = $val['income'] ?? 0;
  906. $income = floatval($income);
  907. $totalIncome = bcadd($totalIncome, $income, 2);
  908. $expend = $val['expend'] ?? 0;
  909. $expend = floatval($expend);
  910. $totalExpend = bcadd($totalExpend, $expend, 2);
  911. $profit = bcsub($income, $expend, 2);
  912. $arr[$key]['profit'] = floatval($profit);
  913. $arr[$key]['income'] = floatval($income);
  914. $arr[$key]['expend'] = floatval($expend);
  915. }
  916. }
  917. //总成本算上物流打包等费用
  918. $totalExpend = bcadd($totalExpend, $packFreight, 2);
  919. //总收入算上采购仅退款
  920. $totalIncome = bcadd($totalIncome, $onlyRefundCg, 2);
  921. //总收入加上人工费
  922. $totalIncome = bcadd($totalIncome, $totalLabourCost, 2);
  923. //总收入减掉优惠金额
  924. $totalIncome = bcsub($totalIncome, $totalDiscountAmount, 2);
  925. //总成本算上销售仅退款
  926. $totalExpend = bcadd($totalExpend, $onlyRefund, 2);
  927. $totalProfit = bcsub($totalIncome, $totalExpend, 2);
  928. $totalIncome = floatval($totalIncome);
  929. $totalExpend = floatval($totalExpend);
  930. $totalProfit = floatval($totalProfit);
  931. $arr = array_values($arr);
  932. $arr = arrayUtil::arraySort($arr, 'profit');
  933. if ($export == 1) {
  934. $statData = [
  935. ['classId' => 0, 'className' => '物流打包费', 'income' => 0, 'expend' => $packFreight, 'profit' => -$packFreight, 'saleNum' => 0, 'cgNum' => 0],
  936. ['classId' => 0, 'className' => '采购仅退款', 'income' => $onlyRefundCg, 'expend' => 0, 'profit' => $onlyRefundCg, 'saleNum' => 0, 'cgNum' => 0],
  937. ['classId' => 0, 'className' => '销售仅退款', 'income' => 0, 'expend' => $onlyRefund, 'profit' => -$onlyRefund, 'saleNum' => 0, 'cgNum' => 0],
  938. ['classId' => 0, 'className' => '附加人工费', 'income' => $totalLabourCost, 'expend' => 0, 'profit' => $totalLabourCost, 'saleNum' => 0, 'cgNum' => 0],
  939. ['classId' => 0, 'className' => '优惠金额', 'income' => -$totalDiscountAmount, 'expend' => 0, 'profit' => -$totalDiscountAmount, 'saleNum' => 0, 'cgNum' => 0],
  940. ['classId' => 0, 'className' => '【汇总金额】', 'income' => $totalIncome, 'expend' => $totalExpend, 'profit' => $totalProfit, 'saleNum' => 0, 'cgNum' => 0],
  941. ];
  942. $list = array_merge($arr, $statData);
  943. self::exportIncomeClass($list, $mainId);
  944. }
  945. util::success(['list' => $arr, 'packFreight' => $packFreight, 'onlyRefundCg' => $onlyRefundCg, 'onlyRefund' => $onlyRefund,
  946. 'totalIncome' => $totalIncome,
  947. 'totalExpend' => $totalExpend,
  948. 'totalProfit' => $totalProfit,
  949. 'tbSaleIncome' => $tbSaleIncome,
  950. 'tbCgIncome' => $tbCgIncome,
  951. 'tbCgIncomeStaff' => $tbCgIncomeStaff,
  952. 'tbSaleIncomeStaff' => $tbSaleIncomeStaff,
  953. 'totalLabourCost' => $totalLabourCost,
  954. 'totalDiscountAmount' => $totalDiscountAmount,
  955. ]);
  956. }
  957. public static function exportIncomeOutItem($list, $mainId)
  958. {
  959. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  960. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  961. $objPHPExcel = new \PHPExcel();
  962. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  963. ->setLastModifiedBy("Maarten Balliauw")
  964. ->setTitle("Office 2007 XLSX Document")
  965. ->setSubject("Office 2007 XLSX Document")
  966. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  967. ->setKeywords("office 2007 openxml php")
  968. ->setCategory("file");
  969. $ghsTitle = '供货商';
  970. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  971. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  972. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  973. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  974. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  975. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  976. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  977. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  978. $objPHPExcel->getActiveSheet()->setCellValue('B1', '销售收入');
  979. $objPHPExcel->getActiveSheet()->setCellValue('C1', '销售数量');
  980. $objPHPExcel->getActiveSheet()->setCellValue('D1', '采购成本');
  981. $objPHPExcel->getActiveSheet()->setCellValue('E1', '采购数量');
  982. $objPHPExcel->getActiveSheet()->setCellValue('F1', '利润');
  983. //设置宽度
  984. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
  985. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  986. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  987. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  988. //加粗
  989. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  990. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  991. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  992. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  993. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  994. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  995. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  996. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  997. $baseRow = 2;
  998. foreach ($list as $key => $custom) {
  999. $i = $baseRow + $key;
  1000. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['name']);
  1001. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['income']);
  1002. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['inNum']);
  1003. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['expend']);
  1004. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['outNum']);
  1005. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['profit']);
  1006. //居左
  1007. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1008. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1009. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1010. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1011. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1012. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1013. }
  1014. $fileName = '收支花材-' . date("m-d");
  1015. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1016. $objPHPExcel->setActiveSheetIndex(0);
  1017. $dir = './priceTable/' . $mainId;
  1018. if (file_exists($dir) == false) {
  1019. mkdir($dir, 0777, true);
  1020. }
  1021. $date = $fileName;
  1022. $file = $date . '.xls';
  1023. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1024. if (file_exists($dir . '/' . $file)) {
  1025. unlink($dir . '/' . $file);
  1026. }
  1027. $objWriter->save($dir . '/' . $file);
  1028. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1029. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1030. }
  1031. public static function exportIncomeClass($list, $mainId)
  1032. {
  1033. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1034. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1035. $objPHPExcel = new \PHPExcel();
  1036. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1037. ->setLastModifiedBy("Maarten Balliauw")
  1038. ->setTitle("Office 2007 XLSX Document")
  1039. ->setSubject("Office 2007 XLSX Document")
  1040. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1041. ->setKeywords("office 2007 openxml php")
  1042. ->setCategory("file");
  1043. $ghsTitle = '供货商';
  1044. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1045. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1046. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1047. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1048. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1049. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1050. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1051. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  1052. $objPHPExcel->getActiveSheet()->setCellValue('B1', '销售数量');
  1053. $objPHPExcel->getActiveSheet()->setCellValue('C1', '销售收入');
  1054. $objPHPExcel->getActiveSheet()->setCellValue('D1', '采购数量');
  1055. $objPHPExcel->getActiveSheet()->setCellValue('E1', '采购成本');
  1056. $objPHPExcel->getActiveSheet()->setCellValue('F1', '利润');
  1057. //设置宽度
  1058. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
  1059. //加粗
  1060. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1061. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1062. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1063. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1064. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  1065. $objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setSize(9)->setBold(true);
  1066. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1067. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1068. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1069. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1070. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1071. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1072. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1073. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1074. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1075. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1076. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1077. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1078. $baseRow = 2;
  1079. foreach ($list as $key => $custom) {
  1080. $i = $baseRow + $key;
  1081. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['className']);
  1082. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['saleNum']);
  1083. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['income']);
  1084. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['cgNum']);
  1085. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['expend']);
  1086. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['profit']);
  1087. //居左
  1088. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1089. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1090. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1091. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1092. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1093. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1094. }
  1095. $fileName = '收支分类-' . time();
  1096. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1097. $objPHPExcel->setActiveSheetIndex(0);
  1098. $dir = './priceTable/' . $mainId;
  1099. if (file_exists($dir) == false) {
  1100. mkdir($dir, 0777, true);
  1101. }
  1102. $date = $fileName;
  1103. $file = $date . '.xls';
  1104. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1105. if (file_exists($dir . '/' . $file)) {
  1106. unlink($dir . '/' . $file);
  1107. }
  1108. $objWriter->save($dir . '/' . $file);
  1109. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1110. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1111. }
  1112. public static function exportItemSale($list, $mainId)
  1113. {
  1114. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1115. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1116. $objPHPExcel = new \PHPExcel();
  1117. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1118. ->setLastModifiedBy("Maarten Balliauw")
  1119. ->setTitle("Office 2007 XLSX Document")
  1120. ->setSubject("Office 2007 XLSX Document")
  1121. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1122. ->setKeywords("office 2007 openxml php")
  1123. ->setCategory("file");
  1124. $ghsTitle = '供货商';
  1125. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1126. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1127. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1128. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1129. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1130. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1131. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1132. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  1133. $objPHPExcel->getActiveSheet()->setCellValue('B1', '总数量');
  1134. $objPHPExcel->getActiveSheet()->setCellValue('C1', '总金额');
  1135. $objPHPExcel->getActiveSheet()->setCellValue('D1', '毛利');
  1136. $objPHPExcel->getActiveSheet()->setCellValue('E1', '毛利率');
  1137. //设置宽度
  1138. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
  1139. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  1140. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  1141. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
  1142. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
  1143. //加粗
  1144. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1145. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1146. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1147. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1148. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  1149. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1150. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1151. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1152. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1153. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1154. $baseRow = 2;
  1155. foreach ($list as $key => $custom) {
  1156. $i = $baseRow + $key;
  1157. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['name']);
  1158. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['num']);
  1159. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['amount']);
  1160. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['gross']);
  1161. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['mll'] . '%');
  1162. //居左
  1163. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1164. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1165. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1166. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1167. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1168. }
  1169. $fileName = '花材销量-' . date("m-d");
  1170. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1171. $objPHPExcel->setActiveSheetIndex(0);
  1172. $dir = './priceTable/' . $mainId;
  1173. if (file_exists($dir) == false) {
  1174. mkdir($dir, 0777, true);
  1175. }
  1176. $date = $fileName;
  1177. $file = $date . '.xls';
  1178. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1179. if (file_exists($dir . '/' . $file)) {
  1180. unlink($dir . '/' . $file);
  1181. }
  1182. $objWriter->save($dir . '/' . $file);
  1183. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1184. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1185. }
  1186. public static function send($mainId)
  1187. {
  1188. $get = Yii::$app->request->get();
  1189. $where = [];
  1190. $where['mainId'] = $mainId;
  1191. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  1192. $startTime = $get['startTime'] ?? '';
  1193. $endTime = $get['endTime'] ?? '';
  1194. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  1195. $start = $period['startTime'];
  1196. $end = $period['endTime'];
  1197. $where['time'] = ['between', [$start, $end]];
  1198. $currentStartDate = date('Y-m-d', strtotime($start));
  1199. $currentEndDate = date('Y-m-d', strtotime($end));
  1200. $currentStartTime = $currentStartDate . ' 00:00:00';
  1201. $currentEndTime = $currentEndDate . ' 23:59:59';
  1202. $s = strtotime($currentStartTime);
  1203. $e = strtotime($currentEndTime);
  1204. $p = bcsub($e, $s);
  1205. $xx = bcmul(86400, 90);
  1206. if ($p > $xx) {
  1207. util::fail('查询时间不能超过三个月');
  1208. }
  1209. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  1210. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  1211. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  1212. $arr = [];
  1213. if (!empty($ghsOrderList)) {
  1214. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  1215. $book = $ghsOrder['book'] ?? 0;
  1216. $status = $ghsOrder['status'] ?? 0;
  1217. if ($book == 1 && $status == 2) {
  1218. //预订单待发货的不算收入
  1219. continue;
  1220. }
  1221. if ($status == 0 || $status == 5) {
  1222. continue;
  1223. }
  1224. $orderSn = $ghsOrder['orderSn'] ?? '';
  1225. $actPrice = $ghsOrder['actPrice'] ?? 0;
  1226. if ($actPrice <= 0) {
  1227. continue;
  1228. }
  1229. $sendStaffId = $ghsOrder['sendStaffId'] ?? 0;
  1230. $sendStaffName = isset($ghsOrder['sendStaffName']) && !empty($ghsOrder['sendStaffName']) ? $ghsOrder['sendStaffName'] : '未记名';
  1231. if (isset($arr[$sendStaffId])) {
  1232. $arr[$sendStaffId]['orderNum'] = bcadd($arr[$sendStaffId]['orderNum'], 1);
  1233. $arr[$sendStaffId]['amount'] = bcadd($arr[$sendStaffId]['amount'], $actPrice, 2);
  1234. } else {
  1235. $arr[$sendStaffId] = ['staffName' => $sendStaffName, 'orderNum' => 1, 'itemNum' => 0, 'amount' => $actPrice, 'staffId' => $sendStaffId];
  1236. }
  1237. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  1238. if (!empty($orderItemList)) {
  1239. foreach ($orderItemList as $orderItem) {
  1240. $refundNum = $orderItem->refundNum ?? 0;
  1241. $num = $orderItem->xhNum ?? 0;
  1242. $remainNum = bcsub($num, $refundNum);
  1243. $arr[$sendStaffId]['itemNum'] = bcadd($arr[$sendStaffId]['itemNum'], $remainNum);
  1244. }
  1245. }
  1246. }
  1247. }
  1248. if (!empty($arr)) {
  1249. $arr = array_values($arr);
  1250. }
  1251. return $arr;
  1252. }
  1253. }