StatSaleClass.php 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  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. $lsAfterSale = 0;
  274. $lsGiveBalance = 0;
  275. $lsIncome = 0;
  276. $lsSendCost = 0;
  277. $lsServiceFee = 0;
  278. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  279. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  280. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  281. foreach ($hdOrderList as $hdKey => $hdOrder) {
  282. $actPrice = $hdOrder['mainPay'] ?? 0;
  283. $onlinePay = $hdOrder['onlinePay'] ?? 1;
  284. $repeat = $hdOrder['repeat'] ?? 0;
  285. $forward = $hdOrder['forward'] ?? 0;
  286. if ($hdOrder['tkPrice'] && $hdOrder['tkPrice'] > 0) {
  287. $actPrice = bcsub($actPrice, $hdOrder['tkPrice'], 2);
  288. }
  289. if ($forward == 1) {
  290. //零售售后付款
  291. $lsAfterSale = bcadd($lsAfterSale, $actPrice, 2);
  292. } else {
  293. $cash = $hdOrder['cash'] ?? 0;
  294. $serviceFee = $hdOrder['serviceFee'] ?? 0;
  295. $sendCost = $hdOrder['sendCost'] ?? 0;
  296. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  297. $lsIncome = bcadd($lsIncome, $cash, 2);
  298. //如果$actPrice<=0,则退款取消先不计算成本
  299. if ($actPrice > 0) {
  300. $lsSendCost = bcadd($lsSendCost, $sendCost, 2);
  301. $lsServiceFee = bcadd($lsServiceFee, $serviceFee, 2);
  302. }
  303. $totalOrderNum++;
  304. if ($onlinePay == 2 && floatval($cash) <= 0 && $repeat == 1) {
  305. $payCodeIncome = bcadd($payCodeIncome, $actPrice, 2);
  306. $payCodeIncome = floatval($payCodeIncome);
  307. }
  308. $giveBalance = $hdOrder['giveBalance'] ?? 0;
  309. $giveBalanceTk = $hdOrder['giveBalanceTk'] ?? 0;
  310. $remainGiveBalance = bcsub($giveBalance, $giveBalanceTk, 2);
  311. $lsGiveBalance = bcadd($lsGiveBalance, $remainGiveBalance, 2);
  312. }
  313. }
  314. $condition = ['mainId' => $mainId, 'addTime' => ['between', [$currentStartTime, $currentEndTime]],];
  315. $salary = 0;
  316. $salaryList = SalaryClass::getAllByCondition($condition, null, '*', null, true);
  317. if (!empty($salaryList)) {
  318. foreach ($salaryList as $item) {
  319. $salary = bcadd($salary, $item->amount, 2);
  320. }
  321. }
  322. $condition = [
  323. 'mainId' => $mainId,
  324. 'payTime' => ['between', [$currentStartTime, $currentEndTime]],
  325. 'delStatus' => 0,
  326. ];
  327. $expendList = ExpendClass::getAllByCondition($condition, null, '*', null, true);
  328. $expendTypeMap = dict::getDict('expendTypeMap');
  329. $expendAmount = [];
  330. if (!empty($expendList)) {
  331. foreach ($expendList as $item) {
  332. $type = $item->type ?? 0;
  333. $amount = $item->amount ?? 0;
  334. $name = $expendTypeMap[$type] ?? '暂无';
  335. if (!isset($expendAmount[$type]['amount'])) {
  336. $expendAmount[$type]['amount'] = 0;
  337. }
  338. $expendAmount[$type]['name'] = $name;
  339. $expendAmount[$type]['amount'] = floatval(bcadd($expendAmount[$type]['amount'], $amount, 2));
  340. }
  341. }
  342. $income = [
  343. ['name' => '批发', 'id' => 'pf', 'amount' => floatval($pfIncome)],
  344. ['name' => '零售', 'id' => 'ls', 'amount' => floatval($lsIncome)],
  345. ['name' => '调拨出库', 'id' => 'allot', 'amount' => floatval($stockOut)],
  346. ];
  347. $expend = [
  348. ['name' => '批发运费', 'id' => 'pfSendCost', 'amount' => floatval($pfSendCost)],
  349. ['name' => '批发包装费', 'id' => 'pfSendCost', 'amount' => floatval($pfPack)],
  350. ['name' => '零售运费', 'id' => 'lsSendCost', 'amount' => floatval($lsSendCost)],
  351. ['name' => '零售售后付款', 'id' => 'lsSendCost', 'amount' => floatval($lsAfterSale)],
  352. ['name' => '美团手续费', 'id' => 'mtFee', 'amount' => floatval($lsServiceFee)],
  353. ['name' => '花材采购', 'id' => 'cg', 'amount' => floatval($cg)],
  354. ['name' => '绿植资材采购', 'id' => 'cg', 'amount' => floatval($lzCgAmount)],
  355. ['name' => '零售花材采购', 'id' => 'cg', 'amount' => floatval($lsCgAmount)],
  356. ['name' => '调拨入库', 'id' => 'allot', 'amount' => floatval($stockIn)],
  357. ['name' => '员工工资', 'id' => 'salary', 'amount' => floatval($salary)],
  358. ['name' => '收款码收入', 'id' => 'salary', 'amount' => floatval($payCodeIncome)],
  359. ['name' => '零售充值赠送', 'id' => 'salary', 'amount' => floatval($lsGiveBalance)],
  360. ];
  361. if (!empty($expendAmount)) {
  362. foreach ($expendAmount as $it) {
  363. $expend[] = $it;
  364. }
  365. }
  366. $balance = 0;
  367. foreach ($income as $it) {
  368. $amount = $it['amount'] ?? 0;
  369. $balance = bcadd($amount, $balance, 2);
  370. }
  371. foreach ($expend as $it) {
  372. $amount = $it['amount'] ?? 0;
  373. $balance = bcsub($balance, $amount, 2);
  374. }
  375. return ['income' => $income, 'expend' => $expend, 'balance' => floatval($balance), 'totalOrderNum' => $totalOrderNum];
  376. }
  377. //按花材统计收入 ssh 20231111
  378. public static function incomeOutItem($mainId)
  379. {
  380. $get = Yii::$app->request->get();
  381. $export = $get['export'] ?? 0;
  382. $where = [];
  383. $where['mainId'] = $mainId;
  384. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  385. $startTime = $get['startTime'] ?? '';
  386. $endTime = $get['endTime'] ?? '';
  387. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  388. $start = $period['startTime'];
  389. $end = $period['endTime'];
  390. $where['time'] = ['between', [$start, $end]];
  391. $currentStartDate = date('Y-m-d', strtotime($start));
  392. $currentEndDate = date('Y-m-d', strtotime($end));
  393. $currentStartTime = $currentStartDate . ' 00:00:00';
  394. $currentEndTime = $currentEndDate . ' 23:59:59';
  395. $s = strtotime($currentStartTime);
  396. $e = strtotime($currentEndTime);
  397. $p = bcsub($e, $s);
  398. $xx = bcmul(86400, 90);
  399. if ($p > $xx) {
  400. util::fail('查询时间不能超过三个月,编号666');
  401. }
  402. $productList = ProductClass::getAllByCondition(['mainId' => $mainId], null, 'id,name', 'id');
  403. if (empty($productList)) {
  404. util::fail('没有找到花材');
  405. }
  406. $arr = [];
  407. //打包费和运费
  408. $packFreight = 0;
  409. $onlyRefundCg = 0;
  410. $onlyRefund = 0;
  411. $totalDiscountAmount = 0;
  412. $totalLabourCost = 0;
  413. //采购入库
  414. $tbCgIncome = [];
  415. $tbCgIncomeStaff = [];
  416. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  417. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  418. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  419. if (!empty($cgList)) {
  420. foreach ($cgList as $cgKey => $cgItem) {
  421. $packingCharge = $cgItem['packingCharge'] ?? 0;
  422. $shortCharge = $cgItem['shortCharge'] ?? 0;
  423. $longCharge = $cgItem['longCharge'] ?? 0;
  424. $pickCharge = $cgItem['pickCharge'] ?? 0;
  425. $localCharge = $cgItem['localCharge'] ?? 0;
  426. $packFreight = bcadd($packFreight, $packingCharge, 2);
  427. $packFreight = bcadd($packFreight, $shortCharge, 2);
  428. $packFreight = bcadd($packFreight, $longCharge, 2);
  429. $packFreight = bcadd($packFreight, $pickCharge, 2);
  430. $packFreight = bcadd($packFreight, $localCharge, 2);
  431. $tkPrice = $cgItem['tkPrice'] ?? 0;
  432. $orderSn = $cgItem['orderSn'] ?? '';
  433. $cgItemInfoList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  434. if (!empty($cgItemInfoList)) {
  435. foreach ($cgItemInfoList as $cgData) {
  436. $itemNum = $cgData->itemNum ?? 0;
  437. $productId = $cgData->productId ?? 0;
  438. $refundNum = $cgData->refundNum ?? 0;
  439. $remainNum = bcsub($itemNum, $refundNum);
  440. $bigPrice = $cgData->bigPrice ?? 0;
  441. $amount = bcmul($remainNum, $bigPrice, 2);
  442. $itemName = $productList[$productId]['name'] ?? '已删除';
  443. $arr[$productId]['name'] = $itemName;
  444. $arr[$productId]['id'] = $productId;
  445. if (isset($arr[$productId]['expend'])) {
  446. $arr[$productId]['expend'] = bcadd($amount, $arr[$productId]['expend'], 2);
  447. $arr[$productId]['outNum'] = bcadd($arr[$productId]['outNum'], $remainNum, 2);
  448. } else {
  449. $arr[$productId]['expend'] = $amount;
  450. $arr[$productId]['outNum'] = $remainNum;
  451. $arr[$productId]['inNum'] = 0;
  452. $arr[$productId]['income'] = 0;
  453. }
  454. }
  455. if ($tkPrice > 0) {
  456. $cgRefundOrderList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  457. if (!empty($cgRefundOrderList)) {
  458. foreach ($cgRefundOrderList as $cgRefundOrder) {
  459. if ($cgRefundOrder->refundType == 2) {
  460. $onlyRefundCg = bcadd($cgRefundOrder->refundPrice, $onlyRefundCg, 2);
  461. }
  462. }
  463. }
  464. }
  465. }
  466. }
  467. }
  468. //批发开单
  469. $pfIncome = 0;
  470. $tbSaleIncome = [];
  471. $tbSaleIncomeStaff = [];
  472. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  473. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  474. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  475. if (!empty($ghsOrderList)) {
  476. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  477. $book = $ghsOrder['book'] ?? 0;
  478. $status = $ghsOrder['status'] ?? 0;
  479. if ($book == 1 && $status == 2) {
  480. //预订单待发货的不算收入
  481. continue;
  482. }
  483. if ($status == 0 || $status == 5) {
  484. continue;
  485. }
  486. $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
  487. $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
  488. $orderSn = $ghsOrder['orderSn'] ?? '';
  489. $actPrice = $ghsOrder['actPrice'] ?? 0;
  490. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  491. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  492. $labourCost = $ghsOrder['labourCost'] ?? 0;
  493. $discountAmount = $ghsOrder['discountAmount'] ?? 0;
  494. $totalLabourCost = bcadd($labourCost, $totalLabourCost, 2);
  495. $totalDiscountAmount = bcadd($totalDiscountAmount, $discountAmount, 2);
  496. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  497. if (!empty($orderItemList)) {
  498. foreach ($orderItemList as $orderItem) {
  499. $refundNum = $orderItem->refundNum ?? 0;
  500. $name = $orderItem->name ?? '';
  501. $productId = $orderItem->productId ?? 0;
  502. $num = $orderItem->xhNum ?? 0;
  503. $remainNum = bcsub($num, $refundNum);
  504. $xhUnitPrice = $orderItem->xhUnitPrice ?? 0;
  505. $amount = bcmul($remainNum, $xhUnitPrice, 2);
  506. $itemName = $productList[$productId]['name'] ?? '已删除';
  507. $arr[$productId]['name'] = $itemName;
  508. $arr[$productId]['id'] = $productId;
  509. if (isset($arr[$productId]['income'])) {
  510. $arr[$productId]['income'] = bcadd($amount, $arr[$productId]['income'], 2);
  511. $arr[$productId]['inNum'] = bcadd($arr[$productId]['inNum'], $remainNum, 2);
  512. } else {
  513. $arr[$productId]['expend'] = 0;
  514. $arr[$productId]['outNum'] = 0;
  515. $arr[$productId]['inNum'] = $remainNum;
  516. $arr[$productId]['income'] = $amount;
  517. }
  518. //单独统计
  519. $belongCost = $orderItem->belongCost ?? 0;
  520. if ($belongCost == 1) {
  521. if (isset($tbSaleIncome[$productId])) {
  522. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  523. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  524. } else {
  525. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  526. }
  527. if (isset($tbSaleIncomeStaff[$productId]) && isset($tbSaleIncomeStaff[$productId][$shopAdminId])) {
  528. $tbSaleIncomeStaff[$productId][$shopAdminId]['num'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['num'], $remainNum);
  529. $tbSaleIncomeStaff[$productId][$shopAdminId]['price'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['price'], $amount, 2);
  530. } else {
  531. $tbSaleIncomeStaff[$productId][$shopAdminId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount, 'staffName' => $shopAdminName];
  532. }
  533. }
  534. }
  535. if ($tkPrice > 0) {
  536. $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  537. if (!empty($refundList)) {
  538. foreach ($refundList as $refund) {
  539. if ($refund->refundType == 1) {
  540. //退货退款前面已经考虑,这里不再处理 OK
  541. } else {
  542. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  543. }
  544. }
  545. }
  546. }
  547. }
  548. }
  549. }
  550. //零售开单
  551. $lsIncome = 0;
  552. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  553. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  554. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  555. foreach ($hdOrderList as $hdKey => $hdOrder) {
  556. $actPrice = $hdOrder['mainPay'] ?? 0;
  557. $cash = $hdOrder['cash'] ?? 0;
  558. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  559. $lsIncome = bcadd($lsIncome, $cash, 2);
  560. $orderSn = $hdOrder['orderSn'] ?? '';
  561. $tkPrice = $hdOrder['tkPrice'] ?? 0;
  562. $orderItemList = \bizHd\order\classes\OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  563. if (!empty($orderItemList)) {
  564. foreach ($orderItemList as $orderItem) {
  565. $unitPrice = $orderItem->unitPrice ?? 0;
  566. $refundNum = $orderItem->refundNum ?? 0;
  567. $num = $orderItem->num ?? 0;
  568. $productId = $orderItem->itemId ?? 0;
  569. $remainNum = bcsub($num, $refundNum);
  570. $amount = bcmul($remainNum, $unitPrice, 2);
  571. $itemName = $productList[$productId]['name'] ?? '已删除';
  572. $arr[$productId]['name'] = $itemName;
  573. $arr[$productId]['id'] = $productId;
  574. if (isset($arr[$productId]['income'])) {
  575. $arr[$productId]['income'] = bcadd($amount, $arr[$productId]['income'], 2);
  576. $arr[$productId]['inNum'] = bcadd($arr[$productId]['inNum'], $remainNum, 2);
  577. } else {
  578. $arr[$productId]['expend'] = 0;
  579. $arr[$productId]['outNum'] = 0;
  580. $arr[$productId]['inNum'] = $remainNum;
  581. $arr[$productId]['income'] = $amount;
  582. }
  583. }
  584. if ($tkPrice > 0) {
  585. $refundList = HdRefundClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  586. if (!empty($refundList)) {
  587. foreach ($refundList as $refund) {
  588. if ($refund->refundType == 2) {
  589. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  590. }
  591. }
  592. }
  593. }
  594. }
  595. }
  596. $totalIncome = 0;
  597. $totalExpend = 0;
  598. if (!empty($arr)) {
  599. foreach ($arr as $key => $val) {
  600. $income = $val['income'] ?? 0;
  601. $income = floatval($income);
  602. $totalIncome = bcadd($totalIncome, $income, 2);
  603. $expend = $val['expend'] ?? 0;
  604. $expend = floatval($expend);
  605. $totalExpend = bcadd($totalExpend, $expend, 2);
  606. $profit = bcsub($income, $expend, 2);
  607. $arr[$key]['profit'] = floatval($profit);
  608. $arr[$key]['income'] = floatval($income);
  609. $arr[$key]['expend'] = floatval($expend);
  610. }
  611. }
  612. //总成本算上物流打包等费用
  613. $totalExpend = bcadd($totalExpend, $packFreight, 2);
  614. //总收入算上采购仅退款
  615. $totalIncome = bcadd($totalIncome, $onlyRefundCg, 2);
  616. //总收入加上人工费
  617. $totalIncome = bcadd($totalIncome, $totalLabourCost, 2);
  618. //总收入减掉优惠金额
  619. $totalIncome = bcsub($totalIncome, $totalDiscountAmount, 2);
  620. //总成本算上销售仅退款
  621. $totalExpend = bcadd($totalExpend, $onlyRefund, 2);
  622. $totalProfit = bcsub($totalIncome, $totalExpend, 2);
  623. $totalIncome = floatval($totalIncome);
  624. $totalExpend = floatval($totalExpend);
  625. $totalProfit = floatval($totalProfit);
  626. $arr = array_values($arr);
  627. $arr = arrayUtil::arraySort($arr, 'profit');
  628. if ($export == 1) {
  629. $statData = [
  630. ['id' => 0, 'name' => '物流打包费', 'income' => 0, 'expend' => $packFreight, 'profit' => -$packFreight, 'inNum' => 0, 'outNum' => 0],
  631. ['id' => 0, 'name' => '采购仅退款', 'income' => $onlyRefundCg, 'expend' => 0, 'profit' => $onlyRefundCg, 'inNum' => 0, 'outNum' => 0],
  632. ['id' => 0, 'name' => '销售仅退款', 'income' => 0, 'expend' => $onlyRefund, 'profit' => -$onlyRefund, 'inNum' => 0, 'outNum' => 0],
  633. ['id' => 0, 'name' => '附加人工费', 'income' => $totalLabourCost, 'expend' => 0, 'profit' => $totalLabourCost, 'inNum' => 0, 'outNum' => 0],
  634. ['id' => 0, 'name' => '优惠金额', 'income' => -$totalDiscountAmount, 'expend' => 0, 'profit' => -$totalDiscountAmount, 'inNum' => 0, 'outNum' => 0],
  635. ['id' => 0, 'name' => '【汇总金额】', 'income' => $totalIncome, 'expend' => $totalExpend, 'profit' => $totalProfit, 'inNum' => 0, 'outNum' => 0],
  636. ];
  637. $list = array_merge($arr, $statData);
  638. self::exportIncomeOutItem($list, $mainId);
  639. }
  640. util::success(['list' => $arr, 'packFreight' => $packFreight, 'onlyRefundCg' => $onlyRefundCg, 'onlyRefund' => $onlyRefund,
  641. 'totalIncome' => $totalIncome,
  642. 'totalExpend' => $totalExpend,
  643. 'totalProfit' => $totalProfit,
  644. 'tbSaleIncome' => $tbSaleIncome,
  645. 'tbCgIncome' => $tbCgIncome,
  646. 'tbCgIncomeStaff' => $tbCgIncomeStaff,
  647. 'tbSaleIncomeStaff' => $tbSaleIncomeStaff,
  648. 'totalLabourCost' => $totalLabourCost,
  649. 'totalDiscountAmount' => $totalDiscountAmount,
  650. ]);
  651. }
  652. //按分类统计收入 ssh 20231111
  653. public static function classProfile($mainId)
  654. {
  655. $get = Yii::$app->request->get();
  656. $export = $get['export'] ?? 0;
  657. $where = [];
  658. $where['mainId'] = $mainId;
  659. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  660. $startTime = $get['startTime'] ?? '';
  661. $endTime = $get['endTime'] ?? '';
  662. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  663. $start = $period['startTime'];
  664. $end = $period['endTime'];
  665. $where['time'] = ['between', [$start, $end]];
  666. $currentStartDate = date('Y-m-d', strtotime($start));
  667. $currentEndDate = date('Y-m-d', strtotime($end));
  668. $currentStartTime = $currentStartDate . ' 00:00:00';
  669. $currentEndTime = $currentEndDate . ' 23:59:59';
  670. $s = strtotime($currentStartTime);
  671. $e = strtotime($currentEndTime);
  672. $p = bcsub($e, $s);
  673. $xx = bcmul(86400, 90);
  674. if ($p > $xx) {
  675. util::fail('查询时间不能超过三个月,编号780');
  676. }
  677. $classList = ItemClassClass::getAllByCondition(['mainId' => $mainId], null, 'id,name', 'id');
  678. if (empty($classList)) {
  679. util::fail('没有找到花材');
  680. }
  681. $arr = [];
  682. //打包费和运费
  683. $packFreight = 0;
  684. $onlyRefundCg = 0;
  685. $onlyRefund = 0;
  686. $totalDiscountAmount = 0;
  687. $totalLabourCost = 0;
  688. //采购入库
  689. $tbCgIncome = [];
  690. $tbCgIncomeStaff = [];
  691. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  692. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  693. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  694. if (!empty($cgList)) {
  695. foreach ($cgList as $cgKey => $cgItem) {
  696. $packingCharge = $cgItem['packingCharge'] ?? 0;
  697. $shortCharge = $cgItem['shortCharge'] ?? 0;
  698. $longCharge = $cgItem['longCharge'] ?? 0;
  699. $pickCharge = $cgItem['pickCharge'] ?? 0;
  700. $localCharge = $cgItem['localCharge'] ?? 0;
  701. $cgStaffId = $cgItem['cgStaffId'] ?? 0;
  702. $cgStaffName = $cgItem['cgStaffName'] ?? '';
  703. $actPrice = $cgItem['actPrice'] ?? 0;
  704. if ($actPrice <= 0) {
  705. continue;
  706. }
  707. $packFreight = bcadd($packFreight, $packingCharge, 2);
  708. $packFreight = bcadd($packFreight, $shortCharge, 2);
  709. $packFreight = bcadd($packFreight, $longCharge, 2);
  710. $packFreight = bcadd($packFreight, $pickCharge, 2);
  711. $packFreight = bcadd($packFreight, $localCharge, 2);
  712. $tkPrice = $cgItem['tkPrice'] ?? 0;
  713. $orderSn = $cgItem['orderSn'] ?? '';
  714. $cgItemInfoList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  715. if (!empty($cgItemInfoList)) {
  716. foreach ($cgItemInfoList as $cgData) {
  717. $itemNum = $cgData->itemNum ?? 0;
  718. $productId = $cgData->productId ?? 0;
  719. $name = $cgData->name ?? '';
  720. $refundNum = $cgData->refundNum ?? 0;
  721. $remainNum = bcsub($itemNum, $refundNum);
  722. $bigPrice = $cgData->bigPrice ?? 0;
  723. $amount = bcmul($remainNum, $bigPrice, 2);
  724. $classId = $cgData->classId;
  725. $className = $classList[$classId]['name'] ?? '已删除';
  726. if ($className == '运费') {
  727. //noticeUtil::push("{$cgData->orderSn}运费项", '15280215347');
  728. }
  729. $arr[$classId]['className'] = $className;
  730. $arr[$classId]['classId'] = $classId;
  731. if (isset($arr[$classId]['expend'])) {
  732. $arr[$classId]['expend'] = bcadd($amount, $arr[$classId]['expend'], 2);
  733. $arr[$classId]['cgNum'] = bcadd($arr[$classId]['cgNum'], $remainNum);
  734. } else {
  735. $arr[$classId]['expend'] = $amount;
  736. $arr[$classId]['income'] = 0;
  737. $arr[$classId]['cgNum'] = $remainNum;
  738. $arr[$classId]['saleNum'] = 0;
  739. }
  740. //单独统计
  741. $belongCost = $cgData->belongCost ?? 0;
  742. if ($belongCost == 1) {
  743. if (isset($tbCgIncome[$productId])) {
  744. $tbCgIncome[$productId]['num'] = bcadd($tbCgIncome[$productId]['num'], $remainNum);
  745. $tbCgIncome[$productId]['price'] = bcadd($tbCgIncome[$productId]['price'], $amount, 2);
  746. } else {
  747. $tbCgIncome[$productId] = ['num' => $remainNum, 'name' => $name, 'price' => $amount];
  748. }
  749. if (isset($tbCgIncomeStaff[$productId]) && isset($tbCgIncomeStaff[$productId][$cgStaffId])) {
  750. $tbCgIncomeStaff[$productId][$cgStaffId]['num'] = bcadd($tbCgIncomeStaff[$productId][$cgStaffId]['num'], $remainNum);
  751. $tbCgIncomeStaff[$productId][$cgStaffId]['price'] = bcadd($tbCgIncomeStaff[$productId][$cgStaffId]['price'], $amount, 2);
  752. } else {
  753. $tbCgIncomeStaff[$productId][$cgStaffId] = ['num' => $remainNum, 'name' => $name, 'price' => $amount, 'staffName' => $cgStaffName,];
  754. }
  755. }
  756. }
  757. if ($tkPrice > 0) {
  758. $cgRefundOrderList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  759. if (!empty($cgRefundOrderList)) {
  760. foreach ($cgRefundOrderList as $cgRefundOrder) {
  761. if ($cgRefundOrder->refundType == 2) {
  762. $onlyRefundCg = bcadd($cgRefundOrder->refundPrice, $onlyRefundCg, 2);
  763. }
  764. }
  765. }
  766. }
  767. }
  768. }
  769. }
  770. //批发开单
  771. $pfIncome = 0;
  772. $tbSaleIncome = [];
  773. $tbSaleIncomeStaff = [];
  774. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  775. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  776. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  777. if (!empty($ghsOrderList)) {
  778. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  779. $book = $ghsOrder['book'] ?? 0;
  780. $status = $ghsOrder['status'] ?? 0;
  781. if ($book == 1 && $status == 2) {
  782. //预订单待发货的不算收入
  783. continue;
  784. }
  785. if ($status == 0 || $status == 5) {
  786. continue;
  787. }
  788. $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
  789. $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
  790. $orderSn = $ghsOrder['orderSn'] ?? '';
  791. $actPrice = $ghsOrder['actPrice'] ?? 0;
  792. if ($actPrice <= 0) {
  793. continue;
  794. }
  795. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  796. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  797. $labourCost = $ghsOrder['labourCost'] ?? 0;
  798. $discountAmount = $ghsOrder['discountAmount'] ?? 0;
  799. $totalLabourCost = bcadd($labourCost, $totalLabourCost, 2);
  800. $totalDiscountAmount = bcadd($totalDiscountAmount, $discountAmount, 2);
  801. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  802. if (!empty($orderItemList)) {
  803. foreach ($orderItemList as $orderItem) {
  804. $refundNum = $orderItem->refundNum ?? 0;
  805. $name = $orderItem->name ?? '';
  806. $productId = $orderItem->productId ?? 0;
  807. $num = $orderItem->xhNum ?? 0;
  808. $remainNum = bcsub($num, $refundNum);
  809. $classId = $orderItem->classId ?? 0;
  810. $className = $classList[$classId]['name'] ?? '已删除';
  811. $xhUnitPrice = $orderItem->xhUnitPrice ?? 0;
  812. $amount = bcmul($remainNum, $xhUnitPrice, 2);
  813. $arr[$classId]['className'] = $className;
  814. $arr[$classId]['classId'] = $classId;
  815. if (isset($arr[$classId]['income'])) {
  816. $arr[$classId]['income'] = bcadd($amount, $arr[$classId]['income'], 2);
  817. $arr[$classId]['saleNum'] = bcadd($arr[$classId]['saleNum'], $remainNum);
  818. } else {
  819. $arr[$classId]['expend'] = 0;
  820. $arr[$classId]['income'] = $amount;
  821. $arr[$classId]['saleNum'] = $remainNum;
  822. $arr[$classId]['cgNum'] = 0;
  823. }
  824. //单独统计
  825. $belongCost = $orderItem->belongCost ?? 0;
  826. if ($belongCost == 1) {
  827. if (isset($tbSaleIncome[$productId])) {
  828. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  829. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  830. } else {
  831. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  832. }
  833. if (isset($tbSaleIncomeStaff[$productId]) && isset($tbSaleIncomeStaff[$productId][$shopAdminId])) {
  834. $tbSaleIncomeStaff[$productId][$shopAdminId]['num'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['num'], $remainNum);
  835. $tbSaleIncomeStaff[$productId][$shopAdminId]['price'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['price'], $amount, 2);
  836. } else {
  837. $tbSaleIncomeStaff[$productId][$shopAdminId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount, 'staffName' => $shopAdminName];
  838. }
  839. }
  840. }
  841. if ($tkPrice > 0) {
  842. $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  843. if (!empty($refundList)) {
  844. foreach ($refundList as $refund) {
  845. if ($refund->refundType == 1) {
  846. //退货退款前面已经考虑,这里不再处理 OK
  847. } else {
  848. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  849. }
  850. }
  851. }
  852. }
  853. }
  854. }
  855. }
  856. //零售开单
  857. $lsIncome = 0;
  858. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  859. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  860. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  861. foreach ($hdOrderList as $hdKey => $hdOrder) {
  862. $actPrice = $hdOrder['mainPay'] ?? 0;
  863. $cash = $hdOrder['cash'] ?? 0;
  864. if ($actPrice <= 0 && $cash <= 0) {
  865. continue;
  866. }
  867. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  868. $lsIncome = bcadd($lsIncome, $cash, 2);
  869. $orderSn = $hdOrder['orderSn'] ?? '';
  870. $tkPrice = $hdOrder['tkPrice'] ?? 0;
  871. $orderItemList = \bizHd\order\classes\OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  872. if (!empty($orderItemList)) {
  873. foreach ($orderItemList as $orderItem) {
  874. $classId = $orderItem->classId ?? 0;
  875. $unitPrice = $orderItem->unitPrice ?? 0;
  876. $refundNum = $orderItem->refundNum ?? 0;
  877. $num = $orderItem->num ?? 0;
  878. $productId = $orderItem->itemId ?? 0;
  879. $name = $orderItem->name ?? '';
  880. $remainNum = bcsub($num, $refundNum);
  881. $amount = bcmul($remainNum, $unitPrice, 2);
  882. $className = $classList[$classId]['name'] ?? '已删除';
  883. $arr[$classId]['className'] = $className;
  884. $arr[$classId]['classId'] = $classId;
  885. if (isset($arr[$classId]['income'])) {
  886. $arr[$classId]['income'] = bcadd($amount, $arr[$classId]['income'], 2);
  887. $arr[$classId]['saleNum'] = bcadd($arr[$classId]['saleNum'], $remainNum);
  888. } else {
  889. $arr[$classId]['expend'] = 0;
  890. $arr[$classId]['income'] = $amount;
  891. $arr[$classId]['saleNum'] = $remainNum;
  892. $arr[$classId]['cgNum'] = 0;
  893. }
  894. $belongCost = $orderItem->belongCost ?? 0;
  895. if ($belongCost == 1) {
  896. if (isset($tbSaleIncome[$productId])) {
  897. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  898. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  899. } else {
  900. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  901. }
  902. }
  903. }
  904. if ($tkPrice > 0) {
  905. $refundList = HdRefundClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  906. if (!empty($refundList)) {
  907. foreach ($refundList as $refund) {
  908. if ($refund->refundType == 2) {
  909. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  910. }
  911. }
  912. }
  913. }
  914. }
  915. }
  916. $totalIncome = 0;
  917. $totalExpend = 0;
  918. if (!empty($arr)) {
  919. foreach ($arr as $key => $val) {
  920. $income = $val['income'] ?? 0;
  921. $income = floatval($income);
  922. $totalIncome = bcadd($totalIncome, $income, 2);
  923. $expend = $val['expend'] ?? 0;
  924. $expend = floatval($expend);
  925. $totalExpend = bcadd($totalExpend, $expend, 2);
  926. $profit = bcsub($income, $expend, 2);
  927. $arr[$key]['profit'] = floatval($profit);
  928. $arr[$key]['income'] = floatval($income);
  929. $arr[$key]['expend'] = floatval($expend);
  930. }
  931. }
  932. //总成本算上物流打包等费用
  933. $totalExpend = bcadd($totalExpend, $packFreight, 2);
  934. //总收入算上采购仅退款
  935. $totalIncome = bcadd($totalIncome, $onlyRefundCg, 2);
  936. //总收入加上人工费
  937. $totalIncome = bcadd($totalIncome, $totalLabourCost, 2);
  938. //总收入减掉优惠金额
  939. $totalIncome = bcsub($totalIncome, $totalDiscountAmount, 2);
  940. //总成本算上销售仅退款
  941. $totalExpend = bcadd($totalExpend, $onlyRefund, 2);
  942. $totalProfit = bcsub($totalIncome, $totalExpend, 2);
  943. $totalIncome = floatval($totalIncome);
  944. $totalExpend = floatval($totalExpend);
  945. $totalProfit = floatval($totalProfit);
  946. $arr = array_values($arr);
  947. $arr = arrayUtil::arraySort($arr, 'profit');
  948. if ($export == 1) {
  949. $statData = [
  950. ['classId' => 0, 'className' => '物流打包费', 'income' => 0, 'expend' => $packFreight, 'profit' => -$packFreight, 'saleNum' => 0, 'cgNum' => 0],
  951. ['classId' => 0, 'className' => '采购仅退款', 'income' => $onlyRefundCg, 'expend' => 0, 'profit' => $onlyRefundCg, 'saleNum' => 0, 'cgNum' => 0],
  952. ['classId' => 0, 'className' => '销售仅退款', 'income' => 0, 'expend' => $onlyRefund, 'profit' => -$onlyRefund, 'saleNum' => 0, 'cgNum' => 0],
  953. ['classId' => 0, 'className' => '附加人工费', 'income' => $totalLabourCost, 'expend' => 0, 'profit' => $totalLabourCost, 'saleNum' => 0, 'cgNum' => 0],
  954. ['classId' => 0, 'className' => '优惠金额', 'income' => -$totalDiscountAmount, 'expend' => 0, 'profit' => -$totalDiscountAmount, 'saleNum' => 0, 'cgNum' => 0],
  955. ['classId' => 0, 'className' => '【汇总金额】', 'income' => $totalIncome, 'expend' => $totalExpend, 'profit' => $totalProfit, 'saleNum' => 0, 'cgNum' => 0],
  956. ];
  957. $list = array_merge($arr, $statData);
  958. self::exportIncomeClass($list, $mainId);
  959. }
  960. util::success(['list' => $arr, 'packFreight' => $packFreight, 'onlyRefundCg' => $onlyRefundCg, 'onlyRefund' => $onlyRefund,
  961. 'totalIncome' => $totalIncome,
  962. 'totalExpend' => $totalExpend,
  963. 'totalProfit' => $totalProfit,
  964. 'tbSaleIncome' => $tbSaleIncome,
  965. 'tbCgIncome' => $tbCgIncome,
  966. 'tbCgIncomeStaff' => $tbCgIncomeStaff,
  967. 'tbSaleIncomeStaff' => $tbSaleIncomeStaff,
  968. 'totalLabourCost' => $totalLabourCost,
  969. 'totalDiscountAmount' => $totalDiscountAmount,
  970. ]);
  971. }
  972. public static function exportIncomeOutItem($list, $mainId)
  973. {
  974. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  975. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  976. $objPHPExcel = new \PHPExcel();
  977. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  978. ->setLastModifiedBy("Maarten Balliauw")
  979. ->setTitle("Office 2007 XLSX Document")
  980. ->setSubject("Office 2007 XLSX Document")
  981. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  982. ->setKeywords("office 2007 openxml php")
  983. ->setCategory("file");
  984. $ghsTitle = '供货商';
  985. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  986. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  987. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  988. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  989. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  990. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  991. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  992. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  993. $objPHPExcel->getActiveSheet()->setCellValue('B1', '销售收入');
  994. $objPHPExcel->getActiveSheet()->setCellValue('C1', '销售数量');
  995. $objPHPExcel->getActiveSheet()->setCellValue('D1', '采购成本');
  996. $objPHPExcel->getActiveSheet()->setCellValue('E1', '采购数量');
  997. $objPHPExcel->getActiveSheet()->setCellValue('F1', '利润');
  998. //设置宽度
  999. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
  1000. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  1001. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  1002. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  1003. //加粗
  1004. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1005. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1006. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1007. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1008. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1009. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1010. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1011. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1012. $baseRow = 2;
  1013. foreach ($list as $key => $custom) {
  1014. $i = $baseRow + $key;
  1015. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['name']);
  1016. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['income']);
  1017. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['inNum']);
  1018. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['expend']);
  1019. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['outNum']);
  1020. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['profit']);
  1021. //居左
  1022. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1023. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1024. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1025. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1026. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1027. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1028. }
  1029. $fileName = '收支花材-' . date("m-d");
  1030. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1031. $objPHPExcel->setActiveSheetIndex(0);
  1032. $dir = './priceTable/' . $mainId;
  1033. if (file_exists($dir) == false) {
  1034. mkdir($dir, 0777, true);
  1035. }
  1036. $date = $fileName;
  1037. $file = $date . '.xls';
  1038. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1039. if (file_exists($dir . '/' . $file)) {
  1040. unlink($dir . '/' . $file);
  1041. }
  1042. $objWriter->save($dir . '/' . $file);
  1043. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1044. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1045. }
  1046. public static function exportIncomeClass($list, $mainId)
  1047. {
  1048. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1049. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1050. $objPHPExcel = new \PHPExcel();
  1051. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1052. ->setLastModifiedBy("Maarten Balliauw")
  1053. ->setTitle("Office 2007 XLSX Document")
  1054. ->setSubject("Office 2007 XLSX Document")
  1055. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1056. ->setKeywords("office 2007 openxml php")
  1057. ->setCategory("file");
  1058. $ghsTitle = '供货商';
  1059. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1060. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1061. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1062. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1063. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1064. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1065. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1066. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  1067. $objPHPExcel->getActiveSheet()->setCellValue('B1', '销售数量');
  1068. $objPHPExcel->getActiveSheet()->setCellValue('C1', '销售收入');
  1069. $objPHPExcel->getActiveSheet()->setCellValue('D1', '采购数量');
  1070. $objPHPExcel->getActiveSheet()->setCellValue('E1', '采购成本');
  1071. $objPHPExcel->getActiveSheet()->setCellValue('F1', '利润');
  1072. //设置宽度
  1073. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
  1074. //加粗
  1075. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1076. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1077. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1078. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1079. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  1080. $objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setSize(9)->setBold(true);
  1081. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1082. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1083. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1084. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1085. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1086. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1087. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1088. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1089. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1090. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1091. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1092. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1093. $baseRow = 2;
  1094. foreach ($list as $key => $custom) {
  1095. $i = $baseRow + $key;
  1096. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['className']);
  1097. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['saleNum']);
  1098. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['income']);
  1099. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['cgNum']);
  1100. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['expend']);
  1101. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['profit']);
  1102. //居左
  1103. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1104. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1105. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1106. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1107. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1108. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1109. }
  1110. $fileName = '收支分类-' . time();
  1111. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1112. $objPHPExcel->setActiveSheetIndex(0);
  1113. $dir = './priceTable/' . $mainId;
  1114. if (file_exists($dir) == false) {
  1115. mkdir($dir, 0777, true);
  1116. }
  1117. $date = $fileName;
  1118. $file = $date . '.xls';
  1119. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1120. if (file_exists($dir . '/' . $file)) {
  1121. unlink($dir . '/' . $file);
  1122. }
  1123. $objWriter->save($dir . '/' . $file);
  1124. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1125. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1126. }
  1127. public static function exportItemSale($list, $mainId)
  1128. {
  1129. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1130. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1131. $objPHPExcel = new \PHPExcel();
  1132. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1133. ->setLastModifiedBy("Maarten Balliauw")
  1134. ->setTitle("Office 2007 XLSX Document")
  1135. ->setSubject("Office 2007 XLSX Document")
  1136. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1137. ->setKeywords("office 2007 openxml php")
  1138. ->setCategory("file");
  1139. $ghsTitle = '供货商';
  1140. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1141. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1142. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1143. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1144. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1145. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1146. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1147. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  1148. $objPHPExcel->getActiveSheet()->setCellValue('B1', '总数量');
  1149. $objPHPExcel->getActiveSheet()->setCellValue('C1', '总金额');
  1150. $objPHPExcel->getActiveSheet()->setCellValue('D1', '毛利');
  1151. $objPHPExcel->getActiveSheet()->setCellValue('E1', '毛利率');
  1152. //设置宽度
  1153. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
  1154. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  1155. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  1156. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
  1157. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
  1158. //加粗
  1159. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1160. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1161. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1162. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1163. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  1164. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1165. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1166. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1167. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1168. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1169. $baseRow = 2;
  1170. foreach ($list as $key => $custom) {
  1171. $i = $baseRow + $key;
  1172. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['name']);
  1173. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['num']);
  1174. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['amount']);
  1175. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['gross']);
  1176. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['mll'] . '%');
  1177. //居左
  1178. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1179. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1180. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1181. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1182. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1183. }
  1184. $fileName = '花材销量-' . date("m-d");
  1185. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1186. $objPHPExcel->setActiveSheetIndex(0);
  1187. $dir = './priceTable/' . $mainId;
  1188. if (file_exists($dir) == false) {
  1189. mkdir($dir, 0777, true);
  1190. }
  1191. $date = $fileName;
  1192. $file = $date . '.xls';
  1193. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1194. if (file_exists($dir . '/' . $file)) {
  1195. unlink($dir . '/' . $file);
  1196. }
  1197. $objWriter->save($dir . '/' . $file);
  1198. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1199. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1200. }
  1201. public static function send($mainId)
  1202. {
  1203. $get = Yii::$app->request->get();
  1204. $where = [];
  1205. $where['mainId'] = $mainId;
  1206. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  1207. $startTime = $get['startTime'] ?? '';
  1208. $endTime = $get['endTime'] ?? '';
  1209. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  1210. $start = $period['startTime'];
  1211. $end = $period['endTime'];
  1212. $where['time'] = ['between', [$start, $end]];
  1213. $currentStartDate = date('Y-m-d', strtotime($start));
  1214. $currentEndDate = date('Y-m-d', strtotime($end));
  1215. $currentStartTime = $currentStartDate . ' 00:00:00';
  1216. $currentEndTime = $currentEndDate . ' 23:59:59';
  1217. $s = strtotime($currentStartTime);
  1218. $e = strtotime($currentEndTime);
  1219. $p = bcsub($e, $s);
  1220. $xx = bcmul(86400, 90);
  1221. if ($p > $xx) {
  1222. util::fail('查询时间不能超过三个月,编号699');
  1223. }
  1224. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  1225. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  1226. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  1227. $arr = [];
  1228. if (!empty($ghsOrderList)) {
  1229. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  1230. $book = $ghsOrder['book'] ?? 0;
  1231. $status = $ghsOrder['status'] ?? 0;
  1232. if ($book == 1 && $status == 2) {
  1233. //预订单待发货的不算收入
  1234. continue;
  1235. }
  1236. if ($status == 0 || $status == 5) {
  1237. continue;
  1238. }
  1239. $orderSn = $ghsOrder['orderSn'] ?? '';
  1240. $actPrice = $ghsOrder['actPrice'] ?? 0;
  1241. if ($actPrice <= 0) {
  1242. continue;
  1243. }
  1244. $sendStaffId = $ghsOrder['sendStaffId'] ?? 0;
  1245. $sendStaffName = isset($ghsOrder['sendStaffName']) && !empty($ghsOrder['sendStaffName']) ? $ghsOrder['sendStaffName'] : '未记名';
  1246. if (isset($arr[$sendStaffId])) {
  1247. $arr[$sendStaffId]['orderNum'] = bcadd($arr[$sendStaffId]['orderNum'], 1);
  1248. $arr[$sendStaffId]['amount'] = bcadd($arr[$sendStaffId]['amount'], $actPrice, 2);
  1249. } else {
  1250. $arr[$sendStaffId] = ['staffName' => $sendStaffName, 'orderNum' => 1, 'itemNum' => 0, 'amount' => $actPrice, 'staffId' => $sendStaffId];
  1251. }
  1252. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  1253. if (!empty($orderItemList)) {
  1254. foreach ($orderItemList as $orderItem) {
  1255. $refundNum = $orderItem->refundNum ?? 0;
  1256. $num = $orderItem->xhNum ?? 0;
  1257. $remainNum = bcsub($num, $refundNum);
  1258. $arr[$sendStaffId]['itemNum'] = bcadd($arr[$sendStaffId]['itemNum'], $remainNum);
  1259. }
  1260. }
  1261. }
  1262. }
  1263. if (!empty($arr)) {
  1264. $arr = array_values($arr);
  1265. }
  1266. return $arr;
  1267. }
  1268. }